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