blob: e48567fcedfc112ed9a85fae446e88ed2c20d33e [file] [log] [blame]
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +00001/*
Jiakai Zhangccbcfb12021-08-23 15:10:35 +00002 * Copyright (C) 2021 The Android Open Source Project
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +00003 *
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 Zhang83c38e22021-11-03 20:54:55 +000021#include <functional>
Jiakai Zhangccbcfb12021-08-23 15:10:35 +000022#include <memory>
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000023#include <optional>
Jiakai Zhang83c38e22021-11-03 20:54:55 +000024#include <set>
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000025#include <string>
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +000026#include <unordered_set>
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000027#include <vector>
28
Jiakai Zhang432d6eb2021-11-11 15:52:00 +000029#include "android-base/result.h"
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000030#include "com_android_apex.h"
31#include "com_android_art.h"
Jiakai Zhangccbcfb12021-08-23 15:10:35 +000032#include "exec_utils.h"
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000033#include "odr_artifacts.h"
34#include "odr_config.h"
35#include "odr_metrics.h"
36#include "odrefresh/odrefresh.h"
37
38namespace art {
39namespace odrefresh {
40
Jiakai Zhang83c38e22021-11-03 20:54:55 +000041struct CompilationOptions {
Jiakai Zhang884e22f2021-12-23 20:04:46 +000042 // If not empty, compile the bootclasspath jars for ISAs in the list.
43 std::vector<InstructionSet> compile_boot_classpath_for_isas;
Jiakai Zhang83c38e22021-11-03 20:54:55 +000044
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 Zhang3ba3edf2021-08-11 08:25:40 +000049class OnDeviceRefresh final {
50 public:
51 explicit OnDeviceRefresh(const OdrConfig& config);
52
Jiakai Zhangccbcfb12021-08-23 15:10:35 +000053 // Constructor with injections. For testing and internal use only.
54 OnDeviceRefresh(const OdrConfig& config,
55 const std::string& cache_info_filename,
Victor Hsieh7b798142022-01-18 15:13:14 -080056 std::unique_ptr<ExecUtils> exec_utils);
Jiakai Zhangccbcfb12021-08-23 15:10:35 +000057
Jiakai Zhang884e22f2021-12-23 20:04:46 +000058 // Returns the exit code and specifies what should be compiled in `compilation_options`.
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000059 WARN_UNUSED ExitCode
60 CheckArtifactsAreUpToDate(OdrMetrics& metrics,
Jiakai Zhang83c38e22021-11-03 20:54:55 +000061 /*out*/ CompilationOptions* compilation_options) const;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000062
63 WARN_UNUSED ExitCode Compile(OdrMetrics& metrics,
Jiakai Zhang83c38e22021-11-03 20:54:55 +000064 const CompilationOptions& compilation_options) const;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000065
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000066 WARN_UNUSED bool RemoveArtifactsDirectory() const;
67
Jiakai Zhang83c38e22021-11-03 20:54:55 +000068 // Returns a set of all system server jars.
69 std::set<std::string> AllSystemServerJars() const {
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +000070 return {all_systemserver_jars_.begin(), all_systemserver_jars_.end()};
Jiakai Zhang83c38e22021-11-03 20:54:55 +000071 }
72
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000073 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 Hsiehfb007612021-12-01 16:51:20 -080083 // Reads the ART APEX cache information (if any) found in the output artifact directory.
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000084 std::optional<com::android::art::CacheInfo> ReadCacheInfo() const;
85
Jiakai Zhang34dcce52022-01-07 16:45:11 +000086 // Writes ART APEX cache information to `kOnDeviceRefreshOdrefreshArtifactDirectory`.
87 android::base::Result<void> WriteCacheInfo() const;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000088
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000089 std::vector<com::android::art::Component> GenerateBootClasspathComponents() const;
90
Jiakai Zhang884e22f2021-12-23 20:04:46 +000091 std::vector<com::android::art::Component> GenerateBootClasspathCompilableComponents() const;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000092
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +000093 std::vector<com::android::art::SystemServerComponent> GenerateSystemServerComponents() const;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000094
Jiakai Zhang8046b622022-01-28 21:40:17 +000095 // 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 Zhang3ba3edf2021-08-11 08:25:40 +000098
Jiakai Zhang8046b622022-01-28 21:40:17 +000099 // 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 Zhang884e22f2021-12-23 20:04:46 +0000102
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 Zhang3ba3edf2021-08-11 08:25:40 +0000110
111 std::string GetSystemServerImagePath(bool on_system, const std::string& jar_path) const;
112
Jiakai Zhang4c7ffab2021-11-26 18:25:56 +0000113 // 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 Zhang39f6d002022-02-10 18:20:50 +0000117 // 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 Zhang4c7ffab2021-11-26 18:25:56 +0000120 android::base::Result<void> RefreshExistingArtifacts() const;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000121
Jiakai Zhang884e22f2021-12-23 20:04:46 +0000122 // Checks whether all boot classpath artifacts are present. Returns true if all are present, false
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000123 // otherwise.
Jiakai Zhang8046b622022-01-28 21:40:17 +0000124 // If `minimal` is true, checks the minimal boot image.
Jiakai Zhang432d6eb2021-11-11 15:52:00 +0000125 // If `checked_artifacts` is present, adds checked artifacts to `checked_artifacts`.
Jiakai Zhang884e22f2021-12-23 20:04:46 +0000126 WARN_UNUSED bool BootClasspathArtifactsExist(
Jiakai Zhang432d6eb2021-11-11 15:52:00 +0000127 bool on_system,
Jiakai Zhang8046b622022-01-28 21:40:17 +0000128 bool minimal,
Jiakai Zhang432d6eb2021-11-11 15:52:00 +0000129 const InstructionSet isa,
130 /*out*/ std::string* error_msg,
131 /*out*/ std::vector<std::string>* checked_artifacts = nullptr) const;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000132
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 Zhang83c38e22021-11-03 20:54:55 +0000135 // Adds the paths to the jars that are missing artifacts in `jars_with_missing_artifacts`.
Jiakai Zhang432d6eb2021-11-11 15:52:00 +0000136 // 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 Zhang83c38e22021-11-03 20:54:55 +0000140 /*out*/ std::set<std::string>* jars_missing_artifacts,
Jiakai Zhang432d6eb2021-11-11 15:52:00 +0000141 /*out*/ std::vector<std::string>* checked_artifacts = nullptr) const;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000142
Jiakai Zhang4d667b32022-05-04 14:34:01 +0100143 // Returns true if all of the system properties listed in `kSystemProperties` are set to the
Jiakai Zhang099f0e72022-05-13 18:47:09 +0100144 // default values. This function is usually called when cache-info.xml does not exist (i.e.,
145 // compilation has not been done before).
Jiakai Zhang4d667b32022-05-04 14:34:01 +0100146 WARN_UNUSED bool CheckSystemPropertiesAreDefault() const;
147
148 // Returns true if none of the system properties listed in `kSystemProperties` has changed since
Jiakai Zhang099f0e72022-05-13 18:47:09 +0100149 // the last compilation. This function is usually called when cache-info.xml exists.
Jiakai Zhang4d667b32022-05-04 14:34:01 +0100150 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 Zhang884e22f2021-12-23 20:04:46 +0000163 // Checks whether all boot classpath artifacts are up to date. Returns true if all are present,
Jiakai Zhang432d6eb2021-11-11 15:52:00 +0000164 // false otherwise.
165 // If `checked_artifacts` is present, adds checked artifacts to `checked_artifacts`.
Jiakai Zhang884e22f2021-12-23 20:04:46 +0000166 WARN_UNUSED bool CheckBootClasspathArtifactsAreUpToDate(
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000167 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 Zhang432d6eb2021-11-11 15:52:00 +0000171 /*out*/ std::vector<std::string>* checked_artifacts) const;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000172
Jiakai Zhang432d6eb2021-11-11 15:52:00 +0000173 // 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 Zhang83c38e22021-11-03 20:54:55 +0000175 // Adds the paths to the jars that needs to be compiled in `jars_to_compile`.
Jiakai Zhang432d6eb2021-11-11 15:52:00 +0000176 // If `checked_artifacts` is present, adds checked artifacts to `checked_artifacts`.
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000177 bool CheckSystemServerArtifactsAreUpToDate(
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000178 OdrMetrics& metrics,
179 const std::vector<com::android::apex::ApexInfo>& apex_info_list,
180 const std::optional<com::android::art::CacheInfo>& cache_info,
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000181 /*out*/ std::set<std::string>* jars_to_compile,
Jiakai Zhang432d6eb2021-11-11 15:52:00 +0000182 /*out*/ std::vector<std::string>* checked_artifacts) const;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000183
Jiakai Zhang8046b622022-01-28 21:40:17 +0000184 // Compiles boot classpath. If `minimal` is true, only compiles the jars in the ART module.
Jiakai Zhang884e22f2021-12-23 20:04:46 +0000185 WARN_UNUSED bool CompileBootClasspathArtifacts(const InstructionSet isa,
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000186 const std::string& staging_dir,
187 OdrMetrics& metrics,
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000188 const std::function<void()>& on_dex2oat_success,
Jiakai Zhang8046b622022-01-28 21:40:17 +0000189 bool minimal,
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000190 std::string* error_msg) const;
191
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000192 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 Zhang3ba3edf2021-08-11 08:25:40 +0000198
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 Zhang884e22f2021-12-23 20:04:46 +0000205 // List of boot classpath components that should be compiled.
206 std::vector<std::string> boot_classpath_compilable_jars_;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000207
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000208 // Set of system_server components in SYSTEMSERVERCLASSPATH that should be compiled.
209 std::unordered_set<std::string> systemserver_classpath_jars_;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000210
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 Zhang9b7ddf62021-11-01 11:36:52 +0000215 // 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 Zhang3ba3edf2021-08-11 08:25:40 +0000220 const time_t start_time_;
221
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000222 std::unique_ptr<ExecUtils> exec_utils_;
223
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000224 DISALLOW_COPY_AND_ASSIGN(OnDeviceRefresh);
225};
226
227} // namespace odrefresh
228} // namespace art
229
230#endif // ART_ODREFRESH_ODREFRESH_H_