blob: ffce81c692a3eb0a8a2ec96a8c18a33bd0fa00c6 [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>
21
22#include "android-base/properties.h"
23#include "android-base/stringprintf.h"
24#include "android-base/strings.h"
25#include "arch/instruction_set.h"
26#include "base/file_utils.h"
27#include "base/globals.h"
28#include "odr_common.h"
29#include "odr_compilation_log.h"
30#include "odr_config.h"
31#include "odr_metrics.h"
32#include "odrefresh.h"
33#include "odrefresh/odrefresh.h"
34
35namespace {
36
Jiakai Zhang83c38e22021-11-03 20:54:55 +000037using ::art::odrefresh::CompilationOptions;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000038using ::art::odrefresh::ExitCode;
39using ::art::odrefresh::OdrCompilationLog;
40using ::art::odrefresh::OdrConfig;
41using ::art::odrefresh::OdrMetrics;
42using ::art::odrefresh::OnDeviceRefresh;
43using ::art::odrefresh::QuotePath;
Jiakai Zhang38fd2542022-03-07 15:58:59 +000044using ::art::odrefresh::ShouldDisablePartialCompilation;
Jiakai Zhang39f6d002022-02-10 18:20:50 +000045using ::art::odrefresh::ShouldDisableRefresh;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000046using ::art::odrefresh::ZygoteKind;
47
Alan Stokesd98612e2022-01-10 17:59:01 +000048void UsageMsgV(const char* fmt, va_list ap) {
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000049 std::string error;
50 android::base::StringAppendV(&error, fmt, ap);
51 if (isatty(fileno(stderr))) {
52 std::cerr << error << std::endl;
53 } else {
54 LOG(ERROR) << error;
55 }
56}
57
Alan Stokesd98612e2022-01-10 17:59:01 +000058void UsageMsg(const char* fmt, ...) {
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000059 va_list ap;
60 va_start(ap, fmt);
Alan Stokesd98612e2022-01-10 17:59:01 +000061 UsageMsgV(fmt, ap);
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000062 va_end(ap);
63}
64
65NO_RETURN void ArgumentError(const char* fmt, ...) {
66 va_list ap;
67 va_start(ap, fmt);
Alan Stokesd98612e2022-01-10 17:59:01 +000068 UsageMsgV(fmt, ap);
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000069 va_end(ap);
Alan Stokesd98612e2022-01-10 17:59:01 +000070 UsageMsg("Try '--help' for more information.");
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000071 exit(EX_USAGE);
72}
73
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000074bool ParseZygoteKind(const char* input, ZygoteKind* zygote_kind) {
75 std::string_view z(input);
76 if (z == "zygote32") {
77 *zygote_kind = ZygoteKind::kZygote32;
78 return true;
79 } else if (z == "zygote32_64") {
80 *zygote_kind = ZygoteKind::kZygote32_64;
81 return true;
82 } else if (z == "zygote64_32") {
83 *zygote_kind = ZygoteKind::kZygote64_32;
84 return true;
85 } else if (z == "zygote64") {
86 *zygote_kind = ZygoteKind::kZygote64;
87 return true;
88 }
89 return false;
90}
91
92std::string GetEnvironmentVariableOrDie(const char* name) {
93 const char* value = getenv(name);
94 LOG_ALWAYS_FATAL_IF(value == nullptr, "%s is not defined.", name);
95 return value;
96}
97
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +000098std::string GetEnvironmentVariableOrDefault(const char* name, std::string default_value) {
99 const char* value = getenv(name);
100 if (value == nullptr) {
101 return default_value;
102 }
103 return value;
104}
105
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000106bool ArgumentMatches(std::string_view argument, std::string_view prefix, std::string* value) {
107 if (android::base::StartsWith(argument, prefix)) {
108 *value = std::string(argument.substr(prefix.size()));
109 return true;
110 }
111 return false;
112}
113
114bool ArgumentEquals(std::string_view argument, std::string_view expected) {
115 return argument == expected;
116}
117
Jiakai Zhangfcdc7cf2022-01-07 14:40:26 +0000118int InitializeConfig(int argc, char** argv, OdrConfig* config) {
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000119 config->SetApexInfoListFile("/apex/apex-info-list.xml");
120 config->SetArtBinDir(art::GetArtBinDir());
121 config->SetBootClasspath(GetEnvironmentVariableOrDie("BOOTCLASSPATH"));
122 config->SetDex2oatBootclasspath(GetEnvironmentVariableOrDie("DEX2OATBOOTCLASSPATH"));
123 config->SetSystemServerClasspath(GetEnvironmentVariableOrDie("SYSTEMSERVERCLASSPATH"));
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000124 config->SetStandaloneSystemServerJars(
125 GetEnvironmentVariableOrDefault("STANDALONE_SYSTEMSERVER_JARS", /*default_value=*/""));
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000126 config->SetIsa(art::kRuntimeISA);
127
Victor Hsieh3644cb42021-11-16 10:49:28 -0800128 std::string zygote;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000129 int n = 1;
130 for (; n < argc - 1; ++n) {
131 const char* arg = argv[n];
132 std::string value;
Victor Hsiehd0d10852022-01-04 16:17:13 -0800133 if (ArgumentEquals(arg, "--compilation-os-mode")) {
Jiakai Zhang34dcce52022-01-07 16:45:11 +0000134 config->SetCompilationOsMode(true);
Alan Stokesd4d21bf2021-10-13 11:07:25 +0100135 } else if (ArgumentMatches(arg, "--dalvik-cache=", &value)) {
136 art::OverrideDalvikCacheSubDirectory(value);
Alan Stokes1f8d2612021-12-14 14:27:45 +0000137 config->SetArtifactDirectory(GetApexDataDalvikCacheDirectory(art::InstructionSet::kNone));
Victor Hsieh3644cb42021-11-16 10:49:28 -0800138 } else if (ArgumentMatches(arg, "--zygote-arch=", &value)) {
139 zygote = value;
Victor Hsieha39356a2021-12-16 11:40:39 -0800140 } else if (ArgumentMatches(arg, "--system-server-compiler-filter=", &value)) {
141 config->SetSystemServerCompilerFilter(value);
Victor Hsieh27a740c2021-11-18 13:35:41 -0800142 } else if (ArgumentMatches(arg, "--staging-dir=", &value)) {
143 config->SetStagingDir(value);
Jiakai Zhangfcdc7cf2022-01-07 14:40:26 +0000144 } else if (ArgumentEquals(arg, "--dry-run")) {
145 config->SetDryRun();
146 } else if (ArgumentEquals(arg, "--partial-compilation")) {
147 config->SetPartialCompilation(true);
148 } else if (ArgumentEquals(arg, "--no-refresh")) {
149 config->SetRefresh(false);
Jiakai Zhang8046b622022-01-28 21:40:17 +0000150 } else if (ArgumentEquals(arg, "--minimal")) {
151 config->SetMinimal(true);
Jiakai Zhangfcdc7cf2022-01-07 14:40:26 +0000152 } else {
Alan Stokesd98612e2022-01-10 17:59:01 +0000153 ArgumentError("Unrecognized argument: '%s'", arg);
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000154 }
155 }
Victor Hsieh3644cb42021-11-16 10:49:28 -0800156
157 if (zygote.empty()) {
158 // Use ro.zygote by default, if not overridden by --zygote-arch flag.
159 zygote = android::base::GetProperty("ro.zygote", {});
160 }
161 ZygoteKind zygote_kind;
162 if (!ParseZygoteKind(zygote.c_str(), &zygote_kind)) {
163 LOG(FATAL) << "Unknown zygote: " << QuotePath(zygote);
164 }
165 config->SetZygoteKind(zygote_kind);
166
Victor Hsieha39356a2021-12-16 11:40:39 -0800167 if (config->GetSystemServerCompilerFilter().empty()) {
168 std::string filter =
169 android::base::GetProperty("dalvik.vm.systemservercompilerfilter", "speed");
170 config->SetSystemServerCompilerFilter(filter);
171 }
172
Jiakai Zhang38fd2542022-03-07 15:58:59 +0000173 if (!config->HasPartialCompilation() &&
174 ShouldDisablePartialCompilation(
175 android::base::GetProperty("ro.build.version.security_patch", /*default_value=*/""))) {
176 config->SetPartialCompilation(false);
177 }
178
Jiakai Zhang39f6d002022-02-10 18:20:50 +0000179 if (ShouldDisableRefresh(
180 android::base::GetProperty("ro.build.version.sdk", /*default_value=*/""))) {
181 config->SetRefresh(false);
182 }
183
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000184 return n;
185}
186
Alan Stokesd4d21bf2021-10-13 11:07:25 +0100187NO_RETURN void UsageHelp(const char* argv0) {
188 std::string name(android::base::Basename(argv0));
Alan Stokesd98612e2022-01-10 17:59:01 +0000189 UsageMsg("Usage: %s [OPTION...] ACTION", name.c_str());
Jiakai Zhang884e22f2021-12-23 20:04:46 +0000190 UsageMsg("On-device refresh tool for boot classpath and system server");
Alan Stokesd98612e2022-01-10 17:59:01 +0000191 UsageMsg("following an update of the ART APEX.");
192 UsageMsg("");
193 UsageMsg("Valid ACTION choices are:");
194 UsageMsg("");
195 UsageMsg("--check Check compilation artifacts are up-to-date based on metadata.");
Jiakai Zhang884e22f2021-12-23 20:04:46 +0000196 UsageMsg("--compile Compile boot classpath and system_server jars when necessary.");
197 UsageMsg("--force-compile Unconditionally compile the bootclass path and system_server jars.");
Alan Stokesd98612e2022-01-10 17:59:01 +0000198 UsageMsg("--help Display this help information.");
199 UsageMsg("");
200 UsageMsg("Available OPTIONs are:");
201 UsageMsg("");
202 UsageMsg("--dry-run");
203 UsageMsg("--partial-compilation Only generate artifacts that are out-of-date or");
204 UsageMsg(" missing.");
205 UsageMsg("--no-refresh Do not refresh existing artifacts.");
Alan Stokesd98612e2022-01-10 17:59:01 +0000206 UsageMsg("--compilation-os-mode Indicate that odrefresh is running in Compilation");
207 UsageMsg(" OS.");
208 UsageMsg("--dalvik-cache=<DIR> Write artifacts to .../<DIR> rather than");
209 UsageMsg(" .../dalvik-cache");
Alan Stokesd98612e2022-01-10 17:59:01 +0000210 UsageMsg("--staging-dir=<DIR> Write temporary artifacts to <DIR> rather than");
211 UsageMsg(" .../staging");
212 UsageMsg("--zygote-arch=<STRING> Zygote kind that overrides ro.zygote");
213 UsageMsg("--system-server-compiler-filter=<STRING>");
214 UsageMsg(" Compiler filter that overrides");
215 UsageMsg(" dalvik.vm.systemservercompilerfilter");
Jiakai Zhang8046b622022-01-28 21:40:17 +0000216 UsageMsg("--minimal Generate a minimal boot image only.");
Alan Stokesd4d21bf2021-10-13 11:07:25 +0100217
218 exit(EX_USAGE);
219}
220
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000221} // namespace
222
Orion Hodsona182c932021-09-27 13:51:22 +0100223int main(int argc, char** argv) {
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000224 // odrefresh is launched by `init` which sets the umask of forked processed to
225 // 077 (S_IRWXG | S_IRWXO). This blocks the ability to make files and directories readable
226 // by others and prevents system_server from loading generated artifacts.
227 umask(S_IWGRP | S_IWOTH);
228
Orion Hodsona182c932021-09-27 13:51:22 +0100229 // Explicitly initialize logging (b/201042799).
230 android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
231
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000232 OdrConfig config(argv[0]);
233 int n = InitializeConfig(argc, argv, &config);
234 argv += n;
235 argc -= n;
236 if (argc != 1) {
Alan Stokesd98612e2022-01-10 17:59:01 +0000237 ArgumentError("Expected 1 argument, but have %d.", argc);
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000238 }
239
Alan Stokesd4d21bf2021-10-13 11:07:25 +0100240 OdrMetrics metrics(config.GetArtifactDirectory());
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000241 OnDeviceRefresh odr(config);
Victor Hsieh08e9f5f2021-12-16 10:22:02 -0800242
243 std::string_view action(argv[0]);
244 CompilationOptions compilation_options;
245 if (action == "--check") {
246 // Fast determination of whether artifacts are up to date.
247 return odr.CheckArtifactsAreUpToDate(metrics, &compilation_options);
248 } else if (action == "--compile") {
249 const ExitCode exit_code = odr.CheckArtifactsAreUpToDate(metrics, &compilation_options);
250 if (exit_code != ExitCode::kCompilationRequired) {
251 return exit_code;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000252 }
Victor Hsieh08e9f5f2021-12-16 10:22:02 -0800253 OdrCompilationLog compilation_log;
254 if (!compilation_log.ShouldAttemptCompile(metrics.GetTrigger())) {
255 LOG(INFO) << "Compilation skipped because it was attempted recently";
256 return ExitCode::kOkay;
257 }
258 ExitCode compile_result = odr.Compile(metrics, compilation_options);
259 compilation_log.Log(metrics.GetArtApexVersion(),
260 metrics.GetArtApexLastUpdateMillis(),
261 metrics.GetTrigger(),
262 compile_result);
263 return compile_result;
264 } else if (action == "--force-compile") {
265 // Clean-up existing files.
266 if (!odr.RemoveArtifactsDirectory()) {
267 metrics.SetStatus(OdrMetrics::Status::kIoError);
268 return ExitCode::kCleanupFailed;
269 }
270 return odr.Compile(metrics,
271 CompilationOptions{
Jiakai Zhang884e22f2021-12-23 20:04:46 +0000272 .compile_boot_classpath_for_isas = config.GetBootClasspathIsas(),
273 .system_server_jars_to_compile = odr.AllSystemServerJars(),
Victor Hsieh08e9f5f2021-12-16 10:22:02 -0800274 });
275 } else if (action == "--help") {
276 UsageHelp(argv[0]);
277 } else {
Alan Stokesd98612e2022-01-10 17:59:01 +0000278 ArgumentError("Unknown argument: ", action);
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000279 }
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000280}