blob: f8cc0b15dc2486d57b28da0b4c81ba5886f1fc2a [file] [log] [blame]
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +00001/*
2 * Copyright (C) 2020 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#include <sys/stat.h>
18
19#include <string>
20#include <string_view>
Jiakai Zhang45d08812022-05-04 14:34:01 +010021#include <unordered_map>
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000022
23#include "android-base/properties.h"
24#include "android-base/stringprintf.h"
25#include "android-base/strings.h"
26#include "arch/instruction_set.h"
27#include "base/file_utils.h"
28#include "base/globals.h"
29#include "odr_common.h"
30#include "odr_compilation_log.h"
31#include "odr_config.h"
32#include "odr_metrics.h"
33#include "odrefresh.h"
34#include "odrefresh/odrefresh.h"
35
36namespace {
37
Jiakai Zhang45d08812022-05-04 14:34:01 +010038using ::android::base::GetProperty;
Jiakai Zhang83c38e22021-11-03 20:54:55 +000039using ::art::odrefresh::CompilationOptions;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000040using ::art::odrefresh::ExitCode;
Jiakai Zhang45d08812022-05-04 14:34:01 +010041using ::art::odrefresh::kSystemProperties;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000042using ::art::odrefresh::OdrCompilationLog;
43using ::art::odrefresh::OdrConfig;
44using ::art::odrefresh::OdrMetrics;
45using ::art::odrefresh::OnDeviceRefresh;
46using ::art::odrefresh::QuotePath;
Jiakai Zhang38fd2542022-03-07 15:58:59 +000047using ::art::odrefresh::ShouldDisablePartialCompilation;
Jiakai Zhang39f6d002022-02-10 18:20:50 +000048using ::art::odrefresh::ShouldDisableRefresh;
Jiakai Zhang45d08812022-05-04 14:34:01 +010049using ::art::odrefresh::SystemPropertyConfig;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000050using ::art::odrefresh::ZygoteKind;
51
Alan Stokesd98612e2022-01-10 17:59:01 +000052void UsageMsgV(const char* fmt, va_list ap) {
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000053 std::string error;
54 android::base::StringAppendV(&error, fmt, ap);
55 if (isatty(fileno(stderr))) {
56 std::cerr << error << std::endl;
57 } else {
58 LOG(ERROR) << error;
59 }
60}
61
Alan Stokesd98612e2022-01-10 17:59:01 +000062void UsageMsg(const char* fmt, ...) {
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000063 va_list ap;
64 va_start(ap, fmt);
Alan Stokesd98612e2022-01-10 17:59:01 +000065 UsageMsgV(fmt, ap);
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000066 va_end(ap);
67}
68
69NO_RETURN void ArgumentError(const char* fmt, ...) {
70 va_list ap;
71 va_start(ap, fmt);
Alan Stokesd98612e2022-01-10 17:59:01 +000072 UsageMsgV(fmt, ap);
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000073 va_end(ap);
Alan Stokesd98612e2022-01-10 17:59:01 +000074 UsageMsg("Try '--help' for more information.");
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000075 exit(EX_USAGE);
76}
77
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000078bool ParseZygoteKind(const char* input, ZygoteKind* zygote_kind) {
79 std::string_view z(input);
80 if (z == "zygote32") {
81 *zygote_kind = ZygoteKind::kZygote32;
82 return true;
83 } else if (z == "zygote32_64") {
84 *zygote_kind = ZygoteKind::kZygote32_64;
85 return true;
86 } else if (z == "zygote64_32") {
87 *zygote_kind = ZygoteKind::kZygote64_32;
88 return true;
89 } else if (z == "zygote64") {
90 *zygote_kind = ZygoteKind::kZygote64;
91 return true;
92 }
93 return false;
94}
95
96std::string GetEnvironmentVariableOrDie(const char* name) {
97 const char* value = getenv(name);
98 LOG_ALWAYS_FATAL_IF(value == nullptr, "%s is not defined.", name);
99 return value;
100}
101
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000102std::string GetEnvironmentVariableOrDefault(const char* name, std::string default_value) {
103 const char* value = getenv(name);
104 if (value == nullptr) {
105 return default_value;
106 }
107 return value;
108}
109
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000110bool ArgumentMatches(std::string_view argument, std::string_view prefix, std::string* value) {
111 if (android::base::StartsWith(argument, prefix)) {
112 *value = std::string(argument.substr(prefix.size()));
113 return true;
114 }
115 return false;
116}
117
118bool ArgumentEquals(std::string_view argument, std::string_view expected) {
119 return argument == expected;
120}
121
Jiakai Zhangfcdc7cf2022-01-07 14:40:26 +0000122int InitializeConfig(int argc, char** argv, OdrConfig* config) {
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000123 config->SetApexInfoListFile("/apex/apex-info-list.xml");
124 config->SetArtBinDir(art::GetArtBinDir());
125 config->SetBootClasspath(GetEnvironmentVariableOrDie("BOOTCLASSPATH"));
126 config->SetDex2oatBootclasspath(GetEnvironmentVariableOrDie("DEX2OATBOOTCLASSPATH"));
127 config->SetSystemServerClasspath(GetEnvironmentVariableOrDie("SYSTEMSERVERCLASSPATH"));
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000128 config->SetStandaloneSystemServerJars(
129 GetEnvironmentVariableOrDefault("STANDALONE_SYSTEMSERVER_JARS", /*default_value=*/""));
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000130 config->SetIsa(art::kRuntimeISA);
131
Victor Hsieh3644cb42021-11-16 10:49:28 -0800132 std::string zygote;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000133 int n = 1;
134 for (; n < argc - 1; ++n) {
135 const char* arg = argv[n];
136 std::string value;
Victor Hsiehd0d10852022-01-04 16:17:13 -0800137 if (ArgumentEquals(arg, "--compilation-os-mode")) {
Jiakai Zhang34dcce52022-01-07 16:45:11 +0000138 config->SetCompilationOsMode(true);
Alan Stokesd4d21bf2021-10-13 11:07:25 +0100139 } else if (ArgumentMatches(arg, "--dalvik-cache=", &value)) {
140 art::OverrideDalvikCacheSubDirectory(value);
Alan Stokes1f8d2612021-12-14 14:27:45 +0000141 config->SetArtifactDirectory(GetApexDataDalvikCacheDirectory(art::InstructionSet::kNone));
Victor Hsieh3644cb42021-11-16 10:49:28 -0800142 } else if (ArgumentMatches(arg, "--zygote-arch=", &value)) {
143 zygote = value;
Victor Hsieha39356a2021-12-16 11:40:39 -0800144 } else if (ArgumentMatches(arg, "--system-server-compiler-filter=", &value)) {
145 config->SetSystemServerCompilerFilter(value);
Victor Hsieh27a740c2021-11-18 13:35:41 -0800146 } else if (ArgumentMatches(arg, "--staging-dir=", &value)) {
147 config->SetStagingDir(value);
Jiakai Zhangfcdc7cf2022-01-07 14:40:26 +0000148 } else if (ArgumentEquals(arg, "--dry-run")) {
149 config->SetDryRun();
150 } else if (ArgumentEquals(arg, "--partial-compilation")) {
151 config->SetPartialCompilation(true);
152 } else if (ArgumentEquals(arg, "--no-refresh")) {
153 config->SetRefresh(false);
Jiakai Zhang8046b622022-01-28 21:40:17 +0000154 } else if (ArgumentEquals(arg, "--minimal")) {
155 config->SetMinimal(true);
Jiakai Zhangfcdc7cf2022-01-07 14:40:26 +0000156 } else {
Alan Stokesd98612e2022-01-10 17:59:01 +0000157 ArgumentError("Unrecognized argument: '%s'", arg);
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000158 }
159 }
Victor Hsieh3644cb42021-11-16 10:49:28 -0800160
161 if (zygote.empty()) {
162 // Use ro.zygote by default, if not overridden by --zygote-arch flag.
Jiakai Zhang45d08812022-05-04 14:34:01 +0100163 zygote = GetProperty("ro.zygote", {});
Victor Hsieh3644cb42021-11-16 10:49:28 -0800164 }
165 ZygoteKind zygote_kind;
166 if (!ParseZygoteKind(zygote.c_str(), &zygote_kind)) {
167 LOG(FATAL) << "Unknown zygote: " << QuotePath(zygote);
168 }
169 config->SetZygoteKind(zygote_kind);
170
Victor Hsieha39356a2021-12-16 11:40:39 -0800171 if (config->GetSystemServerCompilerFilter().empty()) {
Jiakai Zhang45d08812022-05-04 14:34:01 +0100172 std::string filter = GetProperty("dalvik.vm.systemservercompilerfilter", "speed");
Victor Hsieha39356a2021-12-16 11:40:39 -0800173 config->SetSystemServerCompilerFilter(filter);
174 }
175
Jiakai Zhang38fd2542022-03-07 15:58:59 +0000176 if (!config->HasPartialCompilation() &&
177 ShouldDisablePartialCompilation(
Jiakai Zhang45d08812022-05-04 14:34:01 +0100178 GetProperty("ro.build.version.security_patch", /*default_value=*/""))) {
Jiakai Zhang38fd2542022-03-07 15:58:59 +0000179 config->SetPartialCompilation(false);
180 }
181
Jiakai Zhang45d08812022-05-04 14:34:01 +0100182 if (ShouldDisableRefresh(GetProperty("ro.build.version.sdk", /*default_value=*/""))) {
Jiakai Zhang39f6d002022-02-10 18:20:50 +0000183 config->SetRefresh(false);
184 }
185
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000186 return n;
187}
188
Jiakai Zhang45d08812022-05-04 14:34:01 +0100189void GetSystemProperties(std::unordered_map<std::string, std::string>* system_properties) {
190 for (const SystemPropertyConfig& system_property_config : *kSystemProperties.get()) {
191 (*system_properties)[system_property_config.name] =
192 GetProperty(system_property_config.name, system_property_config.default_value);
193 }
194}
195
Alan Stokesd4d21bf2021-10-13 11:07:25 +0100196NO_RETURN void UsageHelp(const char* argv0) {
197 std::string name(android::base::Basename(argv0));
Alan Stokesd98612e2022-01-10 17:59:01 +0000198 UsageMsg("Usage: %s [OPTION...] ACTION", name.c_str());
Jiakai Zhang884e22f2021-12-23 20:04:46 +0000199 UsageMsg("On-device refresh tool for boot classpath and system server");
Alan Stokesd98612e2022-01-10 17:59:01 +0000200 UsageMsg("following an update of the ART APEX.");
201 UsageMsg("");
202 UsageMsg("Valid ACTION choices are:");
203 UsageMsg("");
204 UsageMsg("--check Check compilation artifacts are up-to-date based on metadata.");
Jiakai Zhang884e22f2021-12-23 20:04:46 +0000205 UsageMsg("--compile Compile boot classpath and system_server jars when necessary.");
206 UsageMsg("--force-compile Unconditionally compile the bootclass path and system_server jars.");
Alan Stokesd98612e2022-01-10 17:59:01 +0000207 UsageMsg("--help Display this help information.");
208 UsageMsg("");
209 UsageMsg("Available OPTIONs are:");
210 UsageMsg("");
211 UsageMsg("--dry-run");
212 UsageMsg("--partial-compilation Only generate artifacts that are out-of-date or");
213 UsageMsg(" missing.");
214 UsageMsg("--no-refresh Do not refresh existing artifacts.");
Alan Stokesd98612e2022-01-10 17:59:01 +0000215 UsageMsg("--compilation-os-mode Indicate that odrefresh is running in Compilation");
216 UsageMsg(" OS.");
217 UsageMsg("--dalvik-cache=<DIR> Write artifacts to .../<DIR> rather than");
218 UsageMsg(" .../dalvik-cache");
Alan Stokesd98612e2022-01-10 17:59:01 +0000219 UsageMsg("--staging-dir=<DIR> Write temporary artifacts to <DIR> rather than");
220 UsageMsg(" .../staging");
221 UsageMsg("--zygote-arch=<STRING> Zygote kind that overrides ro.zygote");
222 UsageMsg("--system-server-compiler-filter=<STRING>");
223 UsageMsg(" Compiler filter that overrides");
224 UsageMsg(" dalvik.vm.systemservercompilerfilter");
Jiakai Zhang8046b622022-01-28 21:40:17 +0000225 UsageMsg("--minimal Generate a minimal boot image only.");
Alan Stokesd4d21bf2021-10-13 11:07:25 +0100226
227 exit(EX_USAGE);
228}
229
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000230} // namespace
231
Orion Hodsona182c932021-09-27 13:51:22 +0100232int main(int argc, char** argv) {
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000233 // odrefresh is launched by `init` which sets the umask of forked processed to
234 // 077 (S_IRWXG | S_IRWXO). This blocks the ability to make files and directories readable
235 // by others and prevents system_server from loading generated artifacts.
236 umask(S_IWGRP | S_IWOTH);
237
Orion Hodsona182c932021-09-27 13:51:22 +0100238 // Explicitly initialize logging (b/201042799).
239 android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
240
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000241 OdrConfig config(argv[0]);
242 int n = InitializeConfig(argc, argv, &config);
243 argv += n;
244 argc -= n;
245 if (argc != 1) {
Alan Stokesd98612e2022-01-10 17:59:01 +0000246 ArgumentError("Expected 1 argument, but have %d.", argc);
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000247 }
Jiakai Zhang45d08812022-05-04 14:34:01 +0100248 GetSystemProperties(config.MutableSystemProperties());
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000249
Alan Stokesd4d21bf2021-10-13 11:07:25 +0100250 OdrMetrics metrics(config.GetArtifactDirectory());
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000251 OnDeviceRefresh odr(config);
Victor Hsieh08e9f5f2021-12-16 10:22:02 -0800252
253 std::string_view action(argv[0]);
254 CompilationOptions compilation_options;
255 if (action == "--check") {
256 // Fast determination of whether artifacts are up to date.
257 return odr.CheckArtifactsAreUpToDate(metrics, &compilation_options);
258 } else if (action == "--compile") {
259 const ExitCode exit_code = odr.CheckArtifactsAreUpToDate(metrics, &compilation_options);
260 if (exit_code != ExitCode::kCompilationRequired) {
261 return exit_code;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000262 }
Victor Hsieh08e9f5f2021-12-16 10:22:02 -0800263 OdrCompilationLog compilation_log;
264 if (!compilation_log.ShouldAttemptCompile(metrics.GetTrigger())) {
265 LOG(INFO) << "Compilation skipped because it was attempted recently";
266 return ExitCode::kOkay;
267 }
268 ExitCode compile_result = odr.Compile(metrics, compilation_options);
269 compilation_log.Log(metrics.GetArtApexVersion(),
270 metrics.GetArtApexLastUpdateMillis(),
271 metrics.GetTrigger(),
272 compile_result);
273 return compile_result;
274 } else if (action == "--force-compile") {
275 // Clean-up existing files.
276 if (!odr.RemoveArtifactsDirectory()) {
277 metrics.SetStatus(OdrMetrics::Status::kIoError);
278 return ExitCode::kCleanupFailed;
279 }
280 return odr.Compile(metrics,
281 CompilationOptions{
Jiakai Zhang884e22f2021-12-23 20:04:46 +0000282 .compile_boot_classpath_for_isas = config.GetBootClasspathIsas(),
283 .system_server_jars_to_compile = odr.AllSystemServerJars(),
Victor Hsieh08e9f5f2021-12-16 10:22:02 -0800284 });
285 } else if (action == "--help") {
286 UsageHelp(argv[0]);
287 } else {
Jiakai Zhang45d08812022-05-04 14:34:01 +0100288 ArgumentError("Unknown argument: %s", action.data());
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000289 }
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000290}