xref: /aosp_15_r20/art/runtime/app_info.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2021 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_RUNTIME_APP_INFO_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_APP_INFO_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <vector>
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
23*795d594fSAndroid Build Coastguard Worker #include "base/mutex.h"
24*795d594fSAndroid Build Coastguard Worker #include <base/safe_map.h>
25*795d594fSAndroid Build Coastguard Worker 
26*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
27*795d594fSAndroid Build Coastguard Worker 
28*795d594fSAndroid Build Coastguard Worker // Constants used by VMRuntime.java to interface with the runtime.
29*795d594fSAndroid Build Coastguard Worker // We could get them from the well known class but it's simpler to
30*795d594fSAndroid Build Coastguard Worker // redefine them here.
31*795d594fSAndroid Build Coastguard Worker 
32*795d594fSAndroid Build Coastguard Worker // VMRuntime.CODE_PATH_TYPE_PRIMARY_APK
33*795d594fSAndroid Build Coastguard Worker static constexpr int32_t kVMRuntimePrimaryApk = 1 << 0;
34*795d594fSAndroid Build Coastguard Worker // VMRuntime.CODE_PATH_TYPE_SPLIT_APK
35*795d594fSAndroid Build Coastguard Worker static constexpr int32_t kVMRuntimeSplitApk = 1 << 1;
36*795d594fSAndroid Build Coastguard Worker // VMRuntime.CODE_PATH_TYPE_SECONDARY_DEX
37*795d594fSAndroid Build Coastguard Worker static constexpr int32_t kVMRuntimeSecondaryDex = 1 << 2;
38*795d594fSAndroid Build Coastguard Worker 
39*795d594fSAndroid Build Coastguard Worker // Encapsulates the information the runtime has about the application.
40*795d594fSAndroid Build Coastguard Worker //
41*795d594fSAndroid Build Coastguard Worker // The app's info comes from 2 channels:
42*795d594fSAndroid Build Coastguard Worker //   1) during class loading, when we load oat files.
43*795d594fSAndroid Build Coastguard Worker //   2) during app startup, when the framework calls VMRuntime#registerAppInfo.
44*795d594fSAndroid Build Coastguard Worker // In general the class loading event happens before VMRuntime#registerAppInfo.
45*795d594fSAndroid Build Coastguard Worker class AppInfo {
46*795d594fSAndroid Build Coastguard Worker  public:
47*795d594fSAndroid Build Coastguard Worker   enum class CodeType {
48*795d594fSAndroid Build Coastguard Worker     kUnknown,
49*795d594fSAndroid Build Coastguard Worker     kPrimaryApk,
50*795d594fSAndroid Build Coastguard Worker     kSplitApk,
51*795d594fSAndroid Build Coastguard Worker     kSecondaryDex,
52*795d594fSAndroid Build Coastguard Worker   };
53*795d594fSAndroid Build Coastguard Worker 
54*795d594fSAndroid Build Coastguard Worker   // Converts VMRuntime.java constansts to a CodeType.
55*795d594fSAndroid Build Coastguard Worker   static CodeType FromVMRuntimeConstants(uint32_t code_type);
56*795d594fSAndroid Build Coastguard Worker 
57*795d594fSAndroid Build Coastguard Worker   AppInfo();
58*795d594fSAndroid Build Coastguard Worker 
59*795d594fSAndroid Build Coastguard Worker   // Registers the application code paths, types, and associated profiles.
60*795d594fSAndroid Build Coastguard Worker   void RegisterAppInfo(const std::string& package_name,
61*795d594fSAndroid Build Coastguard Worker                        const std::vector<std::string>& code_paths,
62*795d594fSAndroid Build Coastguard Worker                        const std::string& profile_output_filename,
63*795d594fSAndroid Build Coastguard Worker                        const std::string& ref_profile_filename,
64*795d594fSAndroid Build Coastguard Worker                        CodeType code_type);
65*795d594fSAndroid Build Coastguard Worker 
66*795d594fSAndroid Build Coastguard Worker   // Registers the optimization status for single code path.
67*795d594fSAndroid Build Coastguard Worker   void RegisterOdexStatus(const std::string& code_path,
68*795d594fSAndroid Build Coastguard Worker                           const std::string& compiler_filter,
69*795d594fSAndroid Build Coastguard Worker                           const std::string& compilation_reason,
70*795d594fSAndroid Build Coastguard Worker                           const std::string& odex_status);
71*795d594fSAndroid Build Coastguard Worker 
72*795d594fSAndroid Build Coastguard Worker   // Extracts the optimization status of the primary APK into the given arguments.
73*795d594fSAndroid Build Coastguard Worker   // If there are multiple primary APKs registed via RegisterAppInfo, the method
74*795d594fSAndroid Build Coastguard Worker   // will assign the status of the first APK, sorted by the location name.
75*795d594fSAndroid Build Coastguard Worker   //
76*795d594fSAndroid Build Coastguard Worker   // Assigns "unknown" if there is no primary APK or the optimization status was
77*795d594fSAndroid Build Coastguard Worker   // not set via RegisterOdexStatus,
78*795d594fSAndroid Build Coastguard Worker   void GetPrimaryApkOptimizationStatus(std::string* out_compiler_filter,
79*795d594fSAndroid Build Coastguard Worker                                        std::string* out_compilation_reason);
80*795d594fSAndroid Build Coastguard Worker 
81*795d594fSAndroid Build Coastguard Worker   // Returns the reference profile path of the primary APK.
82*795d594fSAndroid Build Coastguard Worker   // If there are multiple primary APKs registed via RegisterAppInfo, the method
83*795d594fSAndroid Build Coastguard Worker   // will return the profile of the first APK, sorted by the location name.
84*795d594fSAndroid Build Coastguard Worker   //
85*795d594fSAndroid Build Coastguard Worker   // Returns an empty string if there is no primary APK.
86*795d594fSAndroid Build Coastguard Worker   std::string GetPrimaryApkReferenceProfile();
87*795d594fSAndroid Build Coastguard Worker 
88*795d594fSAndroid Build Coastguard Worker   // Returns the path of the primary APK.
89*795d594fSAndroid Build Coastguard Worker   // If there are multiple primary APKs registed via RegisterAppInfo, the method
90*795d594fSAndroid Build Coastguard Worker   // will return the path of the first APK, sorted by the location name.
91*795d594fSAndroid Build Coastguard Worker   //
92*795d594fSAndroid Build Coastguard Worker   // Returns an empty string if there is no primary APK.
93*795d594fSAndroid Build Coastguard Worker   std::string GetPrimaryApkPath();
94*795d594fSAndroid Build Coastguard Worker 
95*795d594fSAndroid Build Coastguard Worker   // Whether we've received a call to RegisterAppInfo.
96*795d594fSAndroid Build Coastguard Worker   bool HasRegisteredAppInfo();
97*795d594fSAndroid Build Coastguard Worker 
98*795d594fSAndroid Build Coastguard Worker   // The registered code type for a given code path. Note that this will
99*795d594fSAndroid Build Coastguard Worker   // be kUnknown until an explicit registration for that path has been made.
100*795d594fSAndroid Build Coastguard Worker   CodeType GetRegisteredCodeType(const std::string& code_path);
101*795d594fSAndroid Build Coastguard Worker 
102*795d594fSAndroid Build Coastguard Worker  private:
103*795d594fSAndroid Build Coastguard Worker   // Encapsulates optimization information about a particular code location.
104*795d594fSAndroid Build Coastguard Worker   struct CodeLocationInfo {
105*795d594fSAndroid Build Coastguard Worker     // The type of the code location (primary, split, secondary, unknown).
106*795d594fSAndroid Build Coastguard Worker     CodeType code_type{CodeType::kUnknown};
107*795d594fSAndroid Build Coastguard Worker 
108*795d594fSAndroid Build Coastguard Worker     // The compiler filter of the oat file. Note that this contains
109*795d594fSAndroid Build Coastguard Worker     // the output of OatFileAssistant#GetOptimizationStatus() which may
110*795d594fSAndroid Build Coastguard Worker     // contain values outside the scope of the CompilerFilter enum.
111*795d594fSAndroid Build Coastguard Worker     std::optional<std::string> compiler_filter;
112*795d594fSAndroid Build Coastguard Worker 
113*795d594fSAndroid Build Coastguard Worker     // The compiler reason of the oat file. Note that this contains
114*795d594fSAndroid Build Coastguard Worker     // the output of OatFileAssistant#GetOptimizationStatus().
115*795d594fSAndroid Build Coastguard Worker     std::optional<std::string> compilation_reason;
116*795d594fSAndroid Build Coastguard Worker 
117*795d594fSAndroid Build Coastguard Worker     // The odes status as produced by OatFileAssistant#GetOptimizationStatus().
118*795d594fSAndroid Build Coastguard Worker     std::optional<std::string> odex_status;
119*795d594fSAndroid Build Coastguard Worker 
120*795d594fSAndroid Build Coastguard Worker     // The path to the primary profile if given.
121*795d594fSAndroid Build Coastguard Worker     std::optional<std::string> cur_profile_path;
122*795d594fSAndroid Build Coastguard Worker 
123*795d594fSAndroid Build Coastguard Worker     // The path to the reference profile if given.
124*795d594fSAndroid Build Coastguard Worker     std::optional<std::string> ref_profile_path;
125*795d594fSAndroid Build Coastguard Worker   };
126*795d594fSAndroid Build Coastguard Worker 
127*795d594fSAndroid Build Coastguard Worker   // The name of the package if set.
128*795d594fSAndroid Build Coastguard Worker   std::optional<std::string> package_name_ GUARDED_BY(update_mutex_);
129*795d594fSAndroid Build Coastguard Worker 
130*795d594fSAndroid Build Coastguard Worker   // The registered code locations.
131*795d594fSAndroid Build Coastguard Worker   SafeMap<std::string, CodeLocationInfo> registered_code_locations_ GUARDED_BY(update_mutex_);
132*795d594fSAndroid Build Coastguard Worker 
133*795d594fSAndroid Build Coastguard Worker   // Lock to touch the state ot the AppInfo object.
134*795d594fSAndroid Build Coastguard Worker   art::Mutex update_mutex_ BOTTOM_MUTEX_ACQUIRED_AFTER;
135*795d594fSAndroid Build Coastguard Worker 
136*795d594fSAndroid Build Coastguard Worker   friend std::ostream& operator<<(std::ostream& os, AppInfo& rhs);
137*795d594fSAndroid Build Coastguard Worker };
138*795d594fSAndroid Build Coastguard Worker 
139*795d594fSAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& os, AppInfo& rhs);
140*795d594fSAndroid Build Coastguard Worker 
141*795d594fSAndroid Build Coastguard Worker }  // namespace art
142*795d594fSAndroid Build Coastguard Worker 
143*795d594fSAndroid Build Coastguard Worker #endif  // ART_RUNTIME_APP_INFO_H_
144