blob: c2298f038cfb73b135d9bc61bf65c47a579fafde [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 Zhang39f6d002022-02-10 18:20:50 +000044using ::art::odrefresh::ShouldDisableRefresh;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000045using ::art::odrefresh::ZygoteKind;
46
Alan Stokesd98612e2022-01-10 17:59:01 +000047void UsageMsgV(const char* fmt, va_list ap) {
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000048 std::string error;
49 android::base::StringAppendV(&error, fmt, ap);
50 if (isatty(fileno(stderr))) {
51 std::cerr << error << std::endl;
52 } else {
53 LOG(ERROR) << error;
54 }
55}
56
Alan Stokesd98612e2022-01-10 17:59:01 +000057void UsageMsg(const char* fmt, ...) {
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000058 va_list ap;
59 va_start(ap, fmt);
Alan Stokesd98612e2022-01-10 17:59:01 +000060 UsageMsgV(fmt, ap);
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000061 va_end(ap);
62}
63
64NO_RETURN void ArgumentError(const char* fmt, ...) {
65 va_list ap;
66 va_start(ap, fmt);
Alan Stokesd98612e2022-01-10 17:59:01 +000067 UsageMsgV(fmt, ap);
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000068 va_end(ap);
Alan Stokesd98612e2022-01-10 17:59:01 +000069 UsageMsg("Try '--help' for more information.");
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000070 exit(EX_USAGE);
71}
72
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +000073bool ParseZygoteKind(const char* input, ZygoteKind* zygote_kind) {
74 std::string_view z(input);
75 if (z == "zygote32") {
76 *zygote_kind = ZygoteKind::kZygote32;
77 return true;
78 } else if (z == "zygote32_64") {
79 *zygote_kind = ZygoteKind::kZygote32_64;
80 return true;
81 } else if (z == "zygote64_32") {
82 *zygote_kind = ZygoteKind::kZygote64_32;
83 return true;
84 } else if (z == "zygote64") {
85 *zygote_kind = ZygoteKind::kZygote64;
86 return true;
87 }
88 return false;
89}
90
91std::string GetEnvironmentVariableOrDie(const char* name) {
92 const char* value = getenv(name);
93 LOG_ALWAYS_FATAL_IF(value == nullptr, "%s is not defined.", name);
94 return value;
95}
96
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +000097std::string GetEnvironmentVariableOrDefault(const char* name, std::string default_value) {
98 const char* value = getenv(name);
99 if (value == nullptr) {
100 return default_value;
101 }
102 return value;
103}
104
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000105bool ArgumentMatches(std::string_view argument, std::string_view prefix, std::string* value) {
106 if (android::base::StartsWith(argument, prefix)) {
107 *value = std::string(argument.substr(prefix.size()));
108 return true;
109 }
110 return false;
111}
112
113bool ArgumentEquals(std::string_view argument, std::string_view expected) {
114 return argument == expected;
115}
116
Jiakai Zhangfcdc7cf2022-01-07 14:40:26 +0000117int InitializeConfig(int argc, char** argv, OdrConfig* config) {
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000118 config->SetApexInfoListFile("/apex/apex-info-list.xml");
119 config->SetArtBinDir(art::GetArtBinDir());
120 config->SetBootClasspath(GetEnvironmentVariableOrDie("BOOTCLASSPATH"));
121 config->SetDex2oatBootclasspath(GetEnvironmentVariableOrDie("DEX2OATBOOTCLASSPATH"));
122 config->SetSystemServerClasspath(GetEnvironmentVariableOrDie("SYSTEMSERVERCLASSPATH"));
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000123 config->SetStandaloneSystemServerJars(
124 GetEnvironmentVariableOrDefault("STANDALONE_SYSTEMSERVER_JARS", /*default_value=*/""));
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000125 config->SetIsa(art::kRuntimeISA);
126
Victor Hsieh3644cb42021-11-16 10:49:28 -0800127 std::string zygote;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000128 int n = 1;
129 for (; n < argc - 1; ++n) {
130 const char* arg = argv[n];
131 std::string value;
Victor Hsiehd0d10852022-01-04 16:17:13 -0800132 if (ArgumentEquals(arg, "--compilation-os-mode")) {
Jiakai Zhang34dcce52022-01-07 16:45:11 +0000133 config->SetCompilationOsMode(true);
Alan Stokesd4d21bf2021-10-13 11:07:25 +0100134 } else if (ArgumentMatches(arg, "--dalvik-cache=", &value)) {
135 art::OverrideDalvikCacheSubDirectory(value);
Alan Stokes1f8d2612021-12-14 14:27:45 +0000136 config->SetArtifactDirectory(GetApexDataDalvikCacheDirectory(art::InstructionSet::kNone));
Victor Hsieh3644cb42021-11-16 10:49:28 -0800137 } else if (ArgumentMatches(arg, "--zygote-arch=", &value)) {
138 zygote = value;
Victor Hsieha39356a2021-12-16 11:40:39 -0800139 } else if (ArgumentMatches(arg, "--system-server-compiler-filter=", &value)) {
140 config->SetSystemServerCompilerFilter(value);
Victor Hsieh27a740c2021-11-18 13:35:41 -0800141 } else if (ArgumentMatches(arg, "--staging-dir=", &value)) {
142 config->SetStagingDir(value);
Jiakai Zhangfcdc7cf2022-01-07 14:40:26 +0000143 } else if (ArgumentEquals(arg, "--dry-run")) {
144 config->SetDryRun();
145 } else if (ArgumentEquals(arg, "--partial-compilation")) {
146 config->SetPartialCompilation(true);
147 } else if (ArgumentEquals(arg, "--no-refresh")) {
148 config->SetRefresh(false);
Jiakai Zhang8046b622022-01-28 21:40:17 +0000149 } else if (ArgumentEquals(arg, "--minimal")) {
150 config->SetMinimal(true);
Jiakai Zhangfcdc7cf2022-01-07 14:40:26 +0000151 } else {
Alan Stokesd98612e2022-01-10 17:59:01 +0000152 ArgumentError("Unrecognized argument: '%s'", arg);
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000153 }
154 }
Victor Hsieh3644cb42021-11-16 10:49:28 -0800155
156 if (zygote.empty()) {
157 // Use ro.zygote by default, if not overridden by --zygote-arch flag.
158 zygote = android::base::GetProperty("ro.zygote", {});
159 }
160 ZygoteKind zygote_kind;
161 if (!ParseZygoteKind(zygote.c_str(), &zygote_kind)) {
162 LOG(FATAL) << "Unknown zygote: " << QuotePath(zygote);
163 }
164 config->SetZygoteKind(zygote_kind);
165
Victor Hsieha39356a2021-12-16 11:40:39 -0800166 if (config->GetSystemServerCompilerFilter().empty()) {
167 std::string filter =
168 android::base::GetProperty("dalvik.vm.systemservercompilerfilter", "speed");
169 config->SetSystemServerCompilerFilter(filter);
170 }
171
Jiakai Zhang39f6d002022-02-10 18:20:50 +0000172 if (ShouldDisableRefresh(
173 android::base::GetProperty("ro.build.version.sdk", /*default_value=*/""))) {
174 config->SetRefresh(false);
175 }
176
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000177 return n;
178}
179
Alan Stokesd4d21bf2021-10-13 11:07:25 +0100180NO_RETURN void UsageHelp(const char* argv0) {
181 std::string name(android::base::Basename(argv0));
Alan Stokesd98612e2022-01-10 17:59:01 +0000182 UsageMsg("Usage: %s [OPTION...] ACTION", name.c_str());
Jiakai Zhang884e22f2021-12-23 20:04:46 +0000183 UsageMsg("On-device refresh tool for boot classpath and system server");
Alan Stokesd98612e2022-01-10 17:59:01 +0000184 UsageMsg("following an update of the ART APEX.");
185 UsageMsg("");
186 UsageMsg("Valid ACTION choices are:");
187 UsageMsg("");
188 UsageMsg("--check Check compilation artifacts are up-to-date based on metadata.");
Jiakai Zhang884e22f2021-12-23 20:04:46 +0000189 UsageMsg("--compile Compile boot classpath and system_server jars when necessary.");
190 UsageMsg("--force-compile Unconditionally compile the bootclass path and system_server jars.");
Alan Stokesd98612e2022-01-10 17:59:01 +0000191 UsageMsg("--help Display this help information.");
192 UsageMsg("");
193 UsageMsg("Available OPTIONs are:");
194 UsageMsg("");
195 UsageMsg("--dry-run");
196 UsageMsg("--partial-compilation Only generate artifacts that are out-of-date or");
197 UsageMsg(" missing.");
198 UsageMsg("--no-refresh Do not refresh existing artifacts.");
Alan Stokesd98612e2022-01-10 17:59:01 +0000199 UsageMsg("--compilation-os-mode Indicate that odrefresh is running in Compilation");
200 UsageMsg(" OS.");
201 UsageMsg("--dalvik-cache=<DIR> Write artifacts to .../<DIR> rather than");
202 UsageMsg(" .../dalvik-cache");
Alan Stokesd98612e2022-01-10 17:59:01 +0000203 UsageMsg("--staging-dir=<DIR> Write temporary artifacts to <DIR> rather than");
204 UsageMsg(" .../staging");
205 UsageMsg("--zygote-arch=<STRING> Zygote kind that overrides ro.zygote");
206 UsageMsg("--system-server-compiler-filter=<STRING>");
207 UsageMsg(" Compiler filter that overrides");
208 UsageMsg(" dalvik.vm.systemservercompilerfilter");
Jiakai Zhang8046b622022-01-28 21:40:17 +0000209 UsageMsg("--minimal Generate a minimal boot image only.");
Alan Stokesd4d21bf2021-10-13 11:07:25 +0100210
211 exit(EX_USAGE);
212}
213
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000214} // namespace
215
Orion Hodsona182c932021-09-27 13:51:22 +0100216int main(int argc, char** argv) {
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000217 // odrefresh is launched by `init` which sets the umask of forked processed to
218 // 077 (S_IRWXG | S_IRWXO). This blocks the ability to make files and directories readable
219 // by others and prevents system_server from loading generated artifacts.
220 umask(S_IWGRP | S_IWOTH);
221
Orion Hodsona182c932021-09-27 13:51:22 +0100222 // Explicitly initialize logging (b/201042799).
223 android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
224
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000225 OdrConfig config(argv[0]);
226 int n = InitializeConfig(argc, argv, &config);
227 argv += n;
228 argc -= n;
229 if (argc != 1) {
Alan Stokesd98612e2022-01-10 17:59:01 +0000230 ArgumentError("Expected 1 argument, but have %d.", argc);
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000231 }
232
Alan Stokesd4d21bf2021-10-13 11:07:25 +0100233 OdrMetrics metrics(config.GetArtifactDirectory());
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000234 OnDeviceRefresh odr(config);
Victor Hsieh08e9f5f2021-12-16 10:22:02 -0800235
236 std::string_view action(argv[0]);
237 CompilationOptions compilation_options;
238 if (action == "--check") {
239 // Fast determination of whether artifacts are up to date.
240 return odr.CheckArtifactsAreUpToDate(metrics, &compilation_options);
241 } else if (action == "--compile") {
242 const ExitCode exit_code = odr.CheckArtifactsAreUpToDate(metrics, &compilation_options);
243 if (exit_code != ExitCode::kCompilationRequired) {
244 return exit_code;
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000245 }
Victor Hsieh08e9f5f2021-12-16 10:22:02 -0800246 OdrCompilationLog compilation_log;
247 if (!compilation_log.ShouldAttemptCompile(metrics.GetTrigger())) {
248 LOG(INFO) << "Compilation skipped because it was attempted recently";
249 return ExitCode::kOkay;
250 }
251 ExitCode compile_result = odr.Compile(metrics, compilation_options);
252 compilation_log.Log(metrics.GetArtApexVersion(),
253 metrics.GetArtApexLastUpdateMillis(),
254 metrics.GetTrigger(),
255 compile_result);
256 return compile_result;
257 } else if (action == "--force-compile") {
258 // Clean-up existing files.
259 if (!odr.RemoveArtifactsDirectory()) {
260 metrics.SetStatus(OdrMetrics::Status::kIoError);
261 return ExitCode::kCleanupFailed;
262 }
263 return odr.Compile(metrics,
264 CompilationOptions{
Jiakai Zhang884e22f2021-12-23 20:04:46 +0000265 .compile_boot_classpath_for_isas = config.GetBootClasspathIsas(),
266 .system_server_jars_to_compile = odr.AllSystemServerJars(),
Victor Hsieh08e9f5f2021-12-16 10:22:02 -0800267 });
268 } else if (action == "--help") {
269 UsageHelp(argv[0]);
270 } else {
Alan Stokesd98612e2022-01-10 17:59:01 +0000271 ArgumentError("Unknown argument: ", action);
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000272 }
Jiakai Zhang3ba3edf2021-08-11 08:25:40 +0000273}