blob: 03190943ce1268bd92d2ba45de94babb84286ece [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"
Victor Hsieh73c4f792021-10-01 18:13:52 +000035#include "odr_dexopt.h"
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000036#include "odr_metrics.h"
37#include "odrefresh/odrefresh.h"
38
39namespace art {
40namespace odrefresh {
41
Jiakai Zhang83c38e22021-11-03 20:54:55 +000042struct 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 Zhang3ba3edf2021-08-11 08:25:40 +000050class OnDeviceRefresh final {
51 public:
52 explicit OnDeviceRefresh(const OdrConfig& config);
53
Jiakai Zhangccbcfb12021-08-23 15:10:35 +000054 // Constructor with injections. For testing and internal use only.
55 OnDeviceRefresh(const OdrConfig& config,
56 const std::string& cache_info_filename,
Victor Hsieh73c4f792021-10-01 18:13:52 +000057 std::unique_ptr<ExecUtils> exec_utils,
58 std::unique_ptr<OdrDexopt> odr_dexopt);
Jiakai Zhangccbcfb12021-08-23 15:10:35 +000059
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000060 // 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 Zhang83c38e22021-11-03 20:54:55 +000064 /*out*/ CompilationOptions* compilation_options) const;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000065
66 WARN_UNUSED ExitCode Compile(OdrMetrics& metrics,
Jiakai Zhang83c38e22021-11-03 20:54:55 +000067 const CompilationOptions& compilation_options) const;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000068
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000069 WARN_UNUSED bool RemoveArtifactsDirectory() const;
70
Jiakai Zhang83c38e22021-11-03 20:54:55 +000071 // Returns a set of all system server jars.
72 std::set<std::string> AllSystemServerJars() const {
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +000073 return {all_systemserver_jars_.begin(), all_systemserver_jars_.end()};
Jiakai Zhang83c38e22021-11-03 20:54:55 +000074 }
75
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000076 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 Hsiehfb007612021-12-01 16:51:20 -080086 // Reads the ART APEX cache information (if any) found in the output artifact directory.
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000087 std::optional<com::android::art::CacheInfo> ReadCacheInfo() const;
88
89 // Write ART APEX cache information to `kOnDeviceRefreshOdrefreshArtifactDirectory`.
90 void WriteCacheInfo() const;
91
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000092 std::vector<com::android::art::Component> GenerateBootClasspathComponents() const;
93
94 std::vector<com::android::art::Component> GenerateBootExtensionCompilableComponents() const;
95
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +000096 std::vector<com::android::art::SystemServerComponent> GenerateSystemServerComponents() const;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000097
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 Zhang4c7ffab2021-11-26 18:25:56 +0000104 // 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 Zhang3ba3edf2021-08-11 08:25:40 +0000112
113 // Checks whether all boot extension artifacts are present. Returns true if all are present, false
114 // otherwise.
Jiakai Zhang432d6eb2021-11-11 15:52:00 +0000115 // 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 Zhang3ba3edf2021-08-11 08:25:40 +0000121
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 Zhang83c38e22021-11-03 20:54:55 +0000124 // Adds the paths to the jars that are missing artifacts in `jars_with_missing_artifacts`.
Jiakai Zhang432d6eb2021-11-11 15:52:00 +0000125 // 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 Zhang83c38e22021-11-03 20:54:55 +0000129 /*out*/ std::set<std::string>* jars_missing_artifacts,
Jiakai Zhang432d6eb2021-11-11 15:52:00 +0000130 /*out*/ std::vector<std::string>* checked_artifacts = nullptr) const;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000131
Jiakai Zhang432d6eb2021-11-11 15:52:00 +0000132 // 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 Zhang3ba3edf2021-08-11 08:25:40 +0000135 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 Zhang432d6eb2021-11-11 15:52:00 +0000140 /*out*/ std::vector<std::string>* checked_artifacts) const;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000141
Jiakai Zhang432d6eb2021-11-11 15:52:00 +0000142 // 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 Zhang83c38e22021-11-03 20:54:55 +0000144 // Adds the paths to the jars that needs to be compiled in `jars_to_compile`.
Jiakai Zhang432d6eb2021-11-11 15:52:00 +0000145 // If `checked_artifacts` is present, adds checked artifacts to `checked_artifacts`.
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000146 bool CheckSystemServerArtifactsAreUpToDate(
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000147 OdrMetrics& metrics,
148 const std::vector<com::android::apex::ApexInfo>& apex_info_list,
149 const std::optional<com::android::art::CacheInfo>& cache_info,
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000150 /*out*/ std::set<std::string>* jars_to_compile,
Jiakai Zhang432d6eb2021-11-11 15:52:00 +0000151 /*out*/ std::vector<std::string>* checked_artifacts) const;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000152
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000153 WARN_UNUSED bool CompileBootExtensionArtifacts(const InstructionSet isa,
154 const std::string& staging_dir,
155 OdrMetrics& metrics,
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000156 const std::function<void()>& on_dex2oat_success,
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000157 std::string* error_msg) const;
158
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000159 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 Zhang3ba3edf2021-08-11 08:25:40 +0000165
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 Zhang9b7ddf62021-11-01 11:36:52 +0000175 // Set of system_server components in SYSTEMSERVERCLASSPATH that should be compiled.
176 std::unordered_set<std::string> systemserver_classpath_jars_;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000177
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 Zhang9b7ddf62021-11-01 11:36:52 +0000182 // 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 Zhang3ba3edf2021-08-11 08:25:40 +0000187 const time_t start_time_;
188
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000189 std::unique_ptr<ExecUtils> exec_utils_;
190
Victor Hsieh73c4f792021-10-01 18:13:52 +0000191 std::unique_ptr<OdrDexopt> odr_dexopt_;
192
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000193 DISALLOW_COPY_AND_ASSIGN(OnDeviceRefresh);
194};
195
196} // namespace odrefresh
197} // namespace art
198
199#endif // ART_ODREFRESH_ODREFRESH_H_