• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    // Returns the reference profile path of the primary APK.
81    // If there are multiple primary APKs registed via RegisterAppInfo, the method
82    // will return the profile of the first APK, sorted by the location name.
83    //
84    // Returns an empty string if there is no primary APK.
85    std::string GetPrimaryApkReferenceProfile();
86  
87    // Whether we've received a call to RegisterAppInfo.
88    bool HasRegisteredAppInfo();
89  
90    // The registered code type for a given code path. Note that this will
91    // be kUnknown until an explicit registration for that path has been made.
92    CodeType GetRegisteredCodeType(const std::string& code_path);
93  
94   private:
95    // Encapsulates optimization information about a particular code location.
96    struct CodeLocationInfo {
97      // The type of the code location (primary, split, secondary, unknown).
98      CodeType code_type{CodeType::kUnknown};
99  
100      // The compiler filter of the oat file. Note that this contains
101      // the output of OatFileAssistant#GetOptimizationStatus() which may
102      // contain values outside the scope of the CompilerFilter enum.
103      std::optional<std::string> compiler_filter;
104  
105      // The compiler reason of the oat file. Note that this contains
106      // the output of OatFileAssistant#GetOptimizationStatus().
107      std::optional<std::string> compilation_reason;
108  
109      // The odes status as produced by OatFileAssistant#GetOptimizationStatus().
110      std::optional<std::string> odex_status;
111  
112      // The path to the primary profile if given.
113      std::optional<std::string> cur_profile_path;
114  
115      // The path to the reference profile if given.
116      std::optional<std::string> ref_profile_path;
117    };
118  
119    // The name of the package if set.
120    std::optional<std::string> package_name_ GUARDED_BY(update_mutex_);
121  
122    // The registered code locations.
123    SafeMap<std::string, CodeLocationInfo> registered_code_locations_ GUARDED_BY(update_mutex_);
124  
125    // Lock to touch the state ot the AppInfo object.
126    art::Mutex update_mutex_ BOTTOM_MUTEX_ACQUIRED_AFTER;
127  
128    friend std::ostream& operator<<(std::ostream& os, AppInfo& rhs);
129  };
130  
131  std::ostream& operator<<(std::ostream& os, AppInfo& rhs);
132  
133  }  // namespace art
134  
135  #endif  // ART_RUNTIME_APP_INFO_H_
136