Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 1 | /* |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 2 | * Copyright (C) 2021 The Android Open Source Project |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 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_ODREFRESH_ODREFRESH_H_ |
| 18 | #define ART_ODREFRESH_ODREFRESH_H_ |
| 19 | |
| 20 | #include <ctime> |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 21 | #include <functional> |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 22 | #include <memory> |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 23 | #include <optional> |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 24 | #include <set> |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 25 | #include <string> |
Jiakai Zhang | 9b7ddf6 | 2021-11-01 11:36:52 +0000 | [diff] [blame] | 26 | #include <unordered_set> |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 27 | #include <vector> |
| 28 | |
Jiakai Zhang | 432d6eb | 2021-11-11 15:52:00 +0000 | [diff] [blame] | 29 | #include "android-base/result.h" |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 30 | #include "com_android_apex.h" |
| 31 | #include "com_android_art.h" |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 32 | #include "exec_utils.h" |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 33 | #include "odr_artifacts.h" |
| 34 | #include "odr_config.h" |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 35 | #include "odr_dexopt.h" |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 36 | #include "odr_metrics.h" |
| 37 | #include "odrefresh/odrefresh.h" |
| 38 | |
| 39 | namespace art { |
| 40 | namespace odrefresh { |
| 41 | |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 42 | struct CompilationOptions { |
| 43 | // If not empty, compile the bootclasspath extensions for ISAs in the list. |
| 44 | std::vector<InstructionSet> compile_boot_extensions_for_isas; |
| 45 | |
| 46 | // If not empty, compile the system server jars in the list. |
| 47 | std::set<std::string> system_server_jars_to_compile; |
| 48 | }; |
| 49 | |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 50 | class OnDeviceRefresh final { |
| 51 | public: |
| 52 | explicit OnDeviceRefresh(const OdrConfig& config); |
| 53 | |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 54 | // Constructor with injections. For testing and internal use only. |
| 55 | OnDeviceRefresh(const OdrConfig& config, |
| 56 | const std::string& cache_info_filename, |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 57 | std::unique_ptr<ExecUtils> exec_utils, |
| 58 | std::unique_ptr<OdrDexopt> odr_dexopt); |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 59 | |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 60 | // Returns the exit code, a list of ISAs that boot extensions should be compiled for, and a |
| 61 | // boolean indicating whether the system server should be compiled. |
| 62 | WARN_UNUSED ExitCode |
| 63 | CheckArtifactsAreUpToDate(OdrMetrics& metrics, |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 64 | /*out*/ CompilationOptions* compilation_options) const; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 65 | |
| 66 | WARN_UNUSED ExitCode Compile(OdrMetrics& metrics, |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 67 | const CompilationOptions& compilation_options) const; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 68 | |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 69 | WARN_UNUSED bool RemoveArtifactsDirectory() const; |
| 70 | |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 71 | // Returns a set of all system server jars. |
| 72 | std::set<std::string> AllSystemServerJars() const { |
Jiakai Zhang | 9b7ddf6 | 2021-11-01 11:36:52 +0000 | [diff] [blame] | 73 | return {all_systemserver_jars_.begin(), all_systemserver_jars_.end()}; |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 76 | private: |
| 77 | time_t GetExecutionTimeUsed() const; |
| 78 | |
| 79 | time_t GetExecutionTimeRemaining() const; |
| 80 | |
| 81 | time_t GetSubprocessTimeout() const; |
| 82 | |
| 83 | // Gets the `ApexInfo` for active APEXes. |
| 84 | std::optional<std::vector<com::android::apex::ApexInfo>> GetApexInfoList() const; |
| 85 | |
Victor Hsieh | fb00761 | 2021-12-01 16:51:20 -0800 | [diff] [blame] | 86 | // Reads the ART APEX cache information (if any) found in the output artifact directory. |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 87 | std::optional<com::android::art::CacheInfo> ReadCacheInfo() const; |
| 88 | |
| 89 | // Write ART APEX cache information to `kOnDeviceRefreshOdrefreshArtifactDirectory`. |
| 90 | void WriteCacheInfo() const; |
| 91 | |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 92 | std::vector<com::android::art::Component> GenerateBootClasspathComponents() const; |
| 93 | |
| 94 | std::vector<com::android::art::Component> GenerateBootExtensionCompilableComponents() const; |
| 95 | |
Jiakai Zhang | 9b7ddf6 | 2021-11-01 11:36:52 +0000 | [diff] [blame] | 96 | std::vector<com::android::art::SystemServerComponent> GenerateSystemServerComponents() const; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 97 | |
| 98 | std::string GetBootImageExtensionImage(bool on_system) const; |
| 99 | |
| 100 | std::string GetBootImageExtensionImagePath(bool on_system, const InstructionSet isa) const; |
| 101 | |
| 102 | std::string GetSystemServerImagePath(bool on_system, const std::string& jar_path) const; |
| 103 | |
Jiakai Zhang | 4c7ffab | 2021-11-26 18:25:56 +0000 | [diff] [blame] | 104 | // Removes files that are not in the list. |
| 105 | android::base::Result<void> CleanupArtifactDirectory( |
| 106 | const std::vector<std::string>& artifacts_to_keep) const; |
| 107 | |
| 108 | // Loads artifacts to memory and writes them back. This essentially removes the existing artifacts |
| 109 | // from fs-verity so that odsign will not encounter "file exists" error when it adds the existing |
| 110 | // artifacts to fs-verity. |
| 111 | android::base::Result<void> RefreshExistingArtifacts() const; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 112 | |
| 113 | // Checks whether all boot extension artifacts are present. Returns true if all are present, false |
| 114 | // otherwise. |
Jiakai Zhang | 432d6eb | 2021-11-11 15:52:00 +0000 | [diff] [blame] | 115 | // If `checked_artifacts` is present, adds checked artifacts to `checked_artifacts`. |
| 116 | WARN_UNUSED bool BootExtensionArtifactsExist( |
| 117 | bool on_system, |
| 118 | const InstructionSet isa, |
| 119 | /*out*/ std::string* error_msg, |
| 120 | /*out*/ std::vector<std::string>* checked_artifacts = nullptr) const; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 121 | |
| 122 | // Checks whether all system_server artifacts are present. The artifacts are checked in their |
| 123 | // order of compilation. Returns true if all are present, false otherwise. |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 124 | // Adds the paths to the jars that are missing artifacts in `jars_with_missing_artifacts`. |
Jiakai Zhang | 432d6eb | 2021-11-11 15:52:00 +0000 | [diff] [blame] | 125 | // If `checked_artifacts` is present, adds checked artifacts to `checked_artifacts`. |
| 126 | WARN_UNUSED bool SystemServerArtifactsExist( |
| 127 | bool on_system, |
| 128 | /*out*/ std::string* error_msg, |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 129 | /*out*/ std::set<std::string>* jars_missing_artifacts, |
Jiakai Zhang | 432d6eb | 2021-11-11 15:52:00 +0000 | [diff] [blame] | 130 | /*out*/ std::vector<std::string>* checked_artifacts = nullptr) const; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 131 | |
Jiakai Zhang | 432d6eb | 2021-11-11 15:52:00 +0000 | [diff] [blame] | 132 | // Checks whether all boot extension artifacts are up to date. Returns true if all are present, |
| 133 | // false otherwise. |
| 134 | // If `checked_artifacts` is present, adds checked artifacts to `checked_artifacts`. |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 135 | WARN_UNUSED bool CheckBootExtensionArtifactsAreUpToDate( |
| 136 | OdrMetrics& metrics, |
| 137 | const InstructionSet isa, |
| 138 | const com::android::apex::ApexInfo& art_apex_info, |
| 139 | const std::optional<com::android::art::CacheInfo>& cache_info, |
Jiakai Zhang | 432d6eb | 2021-11-11 15:52:00 +0000 | [diff] [blame] | 140 | /*out*/ std::vector<std::string>* checked_artifacts) const; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 141 | |
Jiakai Zhang | 432d6eb | 2021-11-11 15:52:00 +0000 | [diff] [blame] | 142 | // Checks whether all system_server artifacts are up to date. The artifacts are checked in their |
| 143 | // order of compilation. Returns true if all are present, false otherwise. |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 144 | // Adds the paths to the jars that needs to be compiled in `jars_to_compile`. |
Jiakai Zhang | 432d6eb | 2021-11-11 15:52:00 +0000 | [diff] [blame] | 145 | // If `checked_artifacts` is present, adds checked artifacts to `checked_artifacts`. |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 146 | bool CheckSystemServerArtifactsAreUpToDate( |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 147 | OdrMetrics& metrics, |
| 148 | const std::vector<com::android::apex::ApexInfo>& apex_info_list, |
| 149 | const std::optional<com::android::art::CacheInfo>& cache_info, |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 150 | /*out*/ std::set<std::string>* jars_to_compile, |
Jiakai Zhang | 432d6eb | 2021-11-11 15:52:00 +0000 | [diff] [blame] | 151 | /*out*/ std::vector<std::string>* checked_artifacts) const; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 152 | |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 153 | WARN_UNUSED bool CompileBootExtensionArtifacts(const InstructionSet isa, |
| 154 | const std::string& staging_dir, |
| 155 | OdrMetrics& metrics, |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 156 | const std::function<void()>& on_dex2oat_success, |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 157 | std::string* error_msg) const; |
| 158 | |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 159 | WARN_UNUSED bool CompileSystemServerArtifacts( |
| 160 | const std::string& staging_dir, |
| 161 | OdrMetrics& metrics, |
| 162 | const std::set<std::string>& system_server_jars_to_compile, |
| 163 | const std::function<void()>& on_dex2oat_success, |
| 164 | std::string* error_msg) const; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 165 | |
| 166 | // Configuration to use. |
| 167 | const OdrConfig& config_; |
| 168 | |
| 169 | // Path to cache information file that is used to speed up artifact checking. |
| 170 | const std::string cache_info_filename_; |
| 171 | |
| 172 | // List of boot extension components that should be compiled. |
| 173 | std::vector<std::string> boot_extension_compilable_jars_; |
| 174 | |
Jiakai Zhang | 9b7ddf6 | 2021-11-01 11:36:52 +0000 | [diff] [blame] | 175 | // Set of system_server components in SYSTEMSERVERCLASSPATH that should be compiled. |
| 176 | std::unordered_set<std::string> systemserver_classpath_jars_; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 177 | |
| 178 | // List of all boot classpath components. Used as the dependencies for compiling the |
| 179 | // system_server. |
| 180 | std::vector<std::string> boot_classpath_jars_; |
| 181 | |
Jiakai Zhang | 9b7ddf6 | 2021-11-01 11:36:52 +0000 | [diff] [blame] | 182 | // List of all system_server components, including those in SYSTEMSERVERCLASSPATH and those in |
| 183 | // STANDALONE_SYSTEMSERVER_JARS (jars that system_server loads dynamically using separate |
| 184 | // classloaders). |
| 185 | std::vector<std::string> all_systemserver_jars_; |
| 186 | |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 187 | const time_t start_time_; |
| 188 | |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 189 | std::unique_ptr<ExecUtils> exec_utils_; |
| 190 | |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 191 | std::unique_ptr<OdrDexopt> odr_dexopt_; |
| 192 | |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 193 | DISALLOW_COPY_AND_ASSIGN(OnDeviceRefresh); |
| 194 | }; |
| 195 | |
| 196 | } // namespace odrefresh |
| 197 | } // namespace art |
| 198 | |
| 199 | #endif // ART_ODREFRESH_ODREFRESH_H_ |