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" |
| 35 | #include "odr_metrics.h" |
| 36 | #include "odrefresh/odrefresh.h" |
| 37 | |
| 38 | namespace art { |
| 39 | namespace odrefresh { |
| 40 | |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 41 | struct CompilationOptions { |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 42 | // If not empty, compile the bootclasspath jars for ISAs in the list. |
| 43 | std::vector<InstructionSet> compile_boot_classpath_for_isas; |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 44 | |
| 45 | // If not empty, compile the system server jars in the list. |
| 46 | std::set<std::string> system_server_jars_to_compile; |
| 47 | }; |
| 48 | |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 49 | class OnDeviceRefresh final { |
| 50 | public: |
| 51 | explicit OnDeviceRefresh(const OdrConfig& config); |
| 52 | |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 53 | // Constructor with injections. For testing and internal use only. |
| 54 | OnDeviceRefresh(const OdrConfig& config, |
| 55 | const std::string& cache_info_filename, |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 56 | std::unique_ptr<ExecUtils> exec_utils); |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 57 | |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 58 | // Returns the exit code and specifies what should be compiled in `compilation_options`. |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 59 | WARN_UNUSED ExitCode |
| 60 | CheckArtifactsAreUpToDate(OdrMetrics& metrics, |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 61 | /*out*/ CompilationOptions* compilation_options) const; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 62 | |
| 63 | WARN_UNUSED ExitCode Compile(OdrMetrics& metrics, |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 64 | const CompilationOptions& compilation_options) const; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 65 | |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 66 | WARN_UNUSED bool RemoveArtifactsDirectory() const; |
| 67 | |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 68 | // Returns a set of all system server jars. |
| 69 | std::set<std::string> AllSystemServerJars() const { |
Jiakai Zhang | 9b7ddf6 | 2021-11-01 11:36:52 +0000 | [diff] [blame] | 70 | return {all_systemserver_jars_.begin(), all_systemserver_jars_.end()}; |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 73 | private: |
| 74 | time_t GetExecutionTimeUsed() const; |
| 75 | |
| 76 | time_t GetExecutionTimeRemaining() const; |
| 77 | |
| 78 | time_t GetSubprocessTimeout() const; |
| 79 | |
| 80 | // Gets the `ApexInfo` for active APEXes. |
| 81 | std::optional<std::vector<com::android::apex::ApexInfo>> GetApexInfoList() const; |
| 82 | |
Victor Hsieh | fb00761 | 2021-12-01 16:51:20 -0800 | [diff] [blame] | 83 | // 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] | 84 | std::optional<com::android::art::CacheInfo> ReadCacheInfo() const; |
| 85 | |
Jiakai Zhang | 34dcce5 | 2022-01-07 16:45:11 +0000 | [diff] [blame] | 86 | // Writes ART APEX cache information to `kOnDeviceRefreshOdrefreshArtifactDirectory`. |
| 87 | android::base::Result<void> WriteCacheInfo() const; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 88 | |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 89 | std::vector<com::android::art::Component> GenerateBootClasspathComponents() const; |
| 90 | |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 91 | std::vector<com::android::art::Component> GenerateBootClasspathCompilableComponents() const; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 92 | |
Jiakai Zhang | 9b7ddf6 | 2021-11-01 11:36:52 +0000 | [diff] [blame] | 93 | std::vector<com::android::art::SystemServerComponent> GenerateSystemServerComponents() const; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 94 | |
Jiakai Zhang | 8046b62 | 2022-01-28 21:40:17 +0000 | [diff] [blame] | 95 | // Returns the symbolic boot image location (without ISA). If `minimal` is true, returns the |
| 96 | // symbolic location of the minimal boot image. |
| 97 | std::string GetBootImage(bool on_system, bool minimal) const; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 98 | |
Jiakai Zhang | 8046b62 | 2022-01-28 21:40:17 +0000 | [diff] [blame] | 99 | // Returns the real boot image location (with ISA). If `minimal` is true, returns the |
| 100 | // symbolic location of the minimal boot image. |
| 101 | std::string GetBootImagePath(bool on_system, bool minimal, const InstructionSet isa) const; |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 102 | |
| 103 | // Returns the symbolic boot image extension location (without ISA). Note that this only applies |
| 104 | // to boot images on /system. |
| 105 | std::string GetSystemBootImageExtension() const; |
| 106 | |
| 107 | // Returns the real boot image location extension (with ISA). Note that this only applies to boot |
| 108 | // images on /system. |
| 109 | std::string GetSystemBootImageExtensionPath(const InstructionSet isa) const; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 110 | |
| 111 | std::string GetSystemServerImagePath(bool on_system, const std::string& jar_path) const; |
| 112 | |
Jiakai Zhang | 4c7ffab | 2021-11-26 18:25:56 +0000 | [diff] [blame] | 113 | // Removes files that are not in the list. |
| 114 | android::base::Result<void> CleanupArtifactDirectory( |
| 115 | const std::vector<std::string>& artifacts_to_keep) const; |
| 116 | |
Jiakai Zhang | 39f6d00 | 2022-02-10 18:20:50 +0000 | [diff] [blame] | 117 | // Loads artifacts to memory and writes them back. This is a workaround for old versions of |
| 118 | // odsign, which encounters "file exists" error when it adds existing artifacts to fs-verity. This |
| 119 | // function essentially removes existing artifacts from fs-verity to avoid the error. |
Jiakai Zhang | 4c7ffab | 2021-11-26 18:25:56 +0000 | [diff] [blame] | 120 | android::base::Result<void> RefreshExistingArtifacts() const; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 121 | |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 122 | // Checks whether all boot classpath artifacts are present. Returns true if all are present, false |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 123 | // otherwise. |
Jiakai Zhang | 8046b62 | 2022-01-28 21:40:17 +0000 | [diff] [blame] | 124 | // If `minimal` is true, checks the minimal boot image. |
Jiakai Zhang | 432d6eb | 2021-11-11 15:52:00 +0000 | [diff] [blame] | 125 | // If `checked_artifacts` is present, adds checked artifacts to `checked_artifacts`. |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 126 | WARN_UNUSED bool BootClasspathArtifactsExist( |
Jiakai Zhang | 432d6eb | 2021-11-11 15:52:00 +0000 | [diff] [blame] | 127 | bool on_system, |
Jiakai Zhang | 8046b62 | 2022-01-28 21:40:17 +0000 | [diff] [blame] | 128 | bool minimal, |
Jiakai Zhang | 432d6eb | 2021-11-11 15:52:00 +0000 | [diff] [blame] | 129 | const InstructionSet isa, |
| 130 | /*out*/ std::string* error_msg, |
| 131 | /*out*/ std::vector<std::string>* checked_artifacts = nullptr) const; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 132 | |
| 133 | // Checks whether all system_server artifacts are present. The artifacts are checked in their |
| 134 | // order of compilation. Returns true if all are present, false otherwise. |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 135 | // 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] | 136 | // If `checked_artifacts` is present, adds checked artifacts to `checked_artifacts`. |
| 137 | WARN_UNUSED bool SystemServerArtifactsExist( |
| 138 | bool on_system, |
| 139 | /*out*/ std::string* error_msg, |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 140 | /*out*/ std::set<std::string>* jars_missing_artifacts, |
Jiakai Zhang | 432d6eb | 2021-11-11 15:52:00 +0000 | [diff] [blame] | 141 | /*out*/ std::vector<std::string>* checked_artifacts = nullptr) const; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 142 | |
Jiakai Zhang | 4d667b3 | 2022-05-04 14:34:01 +0100 | [diff] [blame] | 143 | // Returns true if all of the system properties listed in `kSystemProperties` are set to the |
Jiakai Zhang | 099f0e7 | 2022-05-13 18:47:09 +0100 | [diff] [blame] | 144 | // default values. This function is usually called when cache-info.xml does not exist (i.e., |
| 145 | // compilation has not been done before). |
Jiakai Zhang | 4d667b3 | 2022-05-04 14:34:01 +0100 | [diff] [blame] | 146 | WARN_UNUSED bool CheckSystemPropertiesAreDefault() const; |
| 147 | |
| 148 | // Returns true if none of the system properties listed in `kSystemProperties` has changed since |
Jiakai Zhang | 099f0e7 | 2022-05-13 18:47:09 +0100 | [diff] [blame] | 149 | // the last compilation. This function is usually called when cache-info.xml exists. |
Jiakai Zhang | 4d667b3 | 2022-05-04 14:34:01 +0100 | [diff] [blame] | 150 | WARN_UNUSED bool CheckSystemPropertiesHaveNotChanged( |
| 151 | const com::android::art::CacheInfo& cache_info) const; |
| 152 | |
| 153 | // Returns true if boot classpath artifacts on /system are usable if they exist. Note that this |
| 154 | // function does not check file existence. |
| 155 | WARN_UNUSED bool BootClasspathArtifactsOnSystemUsable( |
| 156 | const com::android::apex::ApexInfo& art_apex_info) const; |
| 157 | |
| 158 | // Returns true if system_server artifacts on /system are usable if they exist. Note that this |
| 159 | // function does not check file existence. |
| 160 | WARN_UNUSED bool SystemServerArtifactsOnSystemUsable( |
| 161 | const std::vector<com::android::apex::ApexInfo>& apex_info_list) const; |
| 162 | |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 163 | // Checks whether all boot classpath artifacts are up to date. Returns true if all are present, |
Jiakai Zhang | 432d6eb | 2021-11-11 15:52:00 +0000 | [diff] [blame] | 164 | // false otherwise. |
| 165 | // If `checked_artifacts` is present, adds checked artifacts to `checked_artifacts`. |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 166 | WARN_UNUSED bool CheckBootClasspathArtifactsAreUpToDate( |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 167 | OdrMetrics& metrics, |
| 168 | const InstructionSet isa, |
| 169 | const com::android::apex::ApexInfo& art_apex_info, |
| 170 | const std::optional<com::android::art::CacheInfo>& cache_info, |
Jiakai Zhang | 432d6eb | 2021-11-11 15:52:00 +0000 | [diff] [blame] | 171 | /*out*/ std::vector<std::string>* checked_artifacts) const; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 172 | |
Jiakai Zhang | 432d6eb | 2021-11-11 15:52:00 +0000 | [diff] [blame] | 173 | // Checks whether all system_server artifacts are up to date. The artifacts are checked in their |
| 174 | // order of compilation. Returns true if all are present, false otherwise. |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 175 | // 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] | 176 | // If `checked_artifacts` is present, adds checked artifacts to `checked_artifacts`. |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 177 | bool CheckSystemServerArtifactsAreUpToDate( |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 178 | OdrMetrics& metrics, |
| 179 | const std::vector<com::android::apex::ApexInfo>& apex_info_list, |
| 180 | const std::optional<com::android::art::CacheInfo>& cache_info, |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 181 | /*out*/ std::set<std::string>* jars_to_compile, |
Jiakai Zhang | 432d6eb | 2021-11-11 15:52:00 +0000 | [diff] [blame] | 182 | /*out*/ std::vector<std::string>* checked_artifacts) const; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 183 | |
Jiakai Zhang | 8046b62 | 2022-01-28 21:40:17 +0000 | [diff] [blame] | 184 | // Compiles boot classpath. If `minimal` is true, only compiles the jars in the ART module. |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 185 | WARN_UNUSED bool CompileBootClasspathArtifacts(const InstructionSet isa, |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 186 | const std::string& staging_dir, |
| 187 | OdrMetrics& metrics, |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 188 | const std::function<void()>& on_dex2oat_success, |
Jiakai Zhang | 8046b62 | 2022-01-28 21:40:17 +0000 | [diff] [blame] | 189 | bool minimal, |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 190 | std::string* error_msg) const; |
| 191 | |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 192 | WARN_UNUSED bool CompileSystemServerArtifacts( |
| 193 | const std::string& staging_dir, |
| 194 | OdrMetrics& metrics, |
| 195 | const std::set<std::string>& system_server_jars_to_compile, |
| 196 | const std::function<void()>& on_dex2oat_success, |
| 197 | std::string* error_msg) const; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 198 | |
| 199 | // Configuration to use. |
| 200 | const OdrConfig& config_; |
| 201 | |
| 202 | // Path to cache information file that is used to speed up artifact checking. |
| 203 | const std::string cache_info_filename_; |
| 204 | |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 205 | // List of boot classpath components that should be compiled. |
| 206 | std::vector<std::string> boot_classpath_compilable_jars_; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 207 | |
Jiakai Zhang | 9b7ddf6 | 2021-11-01 11:36:52 +0000 | [diff] [blame] | 208 | // Set of system_server components in SYSTEMSERVERCLASSPATH that should be compiled. |
| 209 | std::unordered_set<std::string> systemserver_classpath_jars_; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 210 | |
| 211 | // List of all boot classpath components. Used as the dependencies for compiling the |
| 212 | // system_server. |
| 213 | std::vector<std::string> boot_classpath_jars_; |
| 214 | |
Jiakai Zhang | 9b7ddf6 | 2021-11-01 11:36:52 +0000 | [diff] [blame] | 215 | // List of all system_server components, including those in SYSTEMSERVERCLASSPATH and those in |
| 216 | // STANDALONE_SYSTEMSERVER_JARS (jars that system_server loads dynamically using separate |
| 217 | // classloaders). |
| 218 | std::vector<std::string> all_systemserver_jars_; |
| 219 | |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 220 | const time_t start_time_; |
| 221 | |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 222 | std::unique_ptr<ExecUtils> exec_utils_; |
| 223 | |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 224 | DISALLOW_COPY_AND_ASSIGN(OnDeviceRefresh); |
| 225 | }; |
| 226 | |
| 227 | } // namespace odrefresh |
| 228 | } // namespace art |
| 229 | |
| 230 | #endif // ART_ODREFRESH_ODREFRESH_H_ |