Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 35 | namespace { |
| 36 | |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 37 | using ::art::odrefresh::CompilationOptions; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 38 | using ::art::odrefresh::ExitCode; |
| 39 | using ::art::odrefresh::OdrCompilationLog; |
| 40 | using ::art::odrefresh::OdrConfig; |
| 41 | using ::art::odrefresh::OdrMetrics; |
| 42 | using ::art::odrefresh::OnDeviceRefresh; |
| 43 | using ::art::odrefresh::QuotePath; |
Jiakai Zhang | 39f6d00 | 2022-02-10 18:20:50 +0000 | [diff] [blame^] | 44 | using ::art::odrefresh::ShouldDisableRefresh; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 45 | using ::art::odrefresh::ZygoteKind; |
| 46 | |
Alan Stokes | d98612e | 2022-01-10 17:59:01 +0000 | [diff] [blame] | 47 | void UsageMsgV(const char* fmt, va_list ap) { |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 48 | 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 Stokes | d98612e | 2022-01-10 17:59:01 +0000 | [diff] [blame] | 57 | void UsageMsg(const char* fmt, ...) { |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 58 | va_list ap; |
| 59 | va_start(ap, fmt); |
Alan Stokes | d98612e | 2022-01-10 17:59:01 +0000 | [diff] [blame] | 60 | UsageMsgV(fmt, ap); |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 61 | va_end(ap); |
| 62 | } |
| 63 | |
| 64 | NO_RETURN void ArgumentError(const char* fmt, ...) { |
| 65 | va_list ap; |
| 66 | va_start(ap, fmt); |
Alan Stokes | d98612e | 2022-01-10 17:59:01 +0000 | [diff] [blame] | 67 | UsageMsgV(fmt, ap); |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 68 | va_end(ap); |
Alan Stokes | d98612e | 2022-01-10 17:59:01 +0000 | [diff] [blame] | 69 | UsageMsg("Try '--help' for more information."); |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 70 | exit(EX_USAGE); |
| 71 | } |
| 72 | |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 73 | bool 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 | |
| 91 | std::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 Zhang | 9b7ddf6 | 2021-11-01 11:36:52 +0000 | [diff] [blame] | 97 | std::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 Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 105 | bool 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 | |
| 113 | bool ArgumentEquals(std::string_view argument, std::string_view expected) { |
| 114 | return argument == expected; |
| 115 | } |
| 116 | |
Jiakai Zhang | fcdc7cf | 2022-01-07 14:40:26 +0000 | [diff] [blame] | 117 | int InitializeConfig(int argc, char** argv, OdrConfig* config) { |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 118 | 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 Zhang | 9b7ddf6 | 2021-11-01 11:36:52 +0000 | [diff] [blame] | 123 | config->SetStandaloneSystemServerJars( |
| 124 | GetEnvironmentVariableOrDefault("STANDALONE_SYSTEMSERVER_JARS", /*default_value=*/"")); |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 125 | config->SetIsa(art::kRuntimeISA); |
| 126 | |
Victor Hsieh | 3644cb4 | 2021-11-16 10:49:28 -0800 | [diff] [blame] | 127 | std::string zygote; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 128 | int n = 1; |
| 129 | for (; n < argc - 1; ++n) { |
| 130 | const char* arg = argv[n]; |
| 131 | std::string value; |
Victor Hsieh | d0d1085 | 2022-01-04 16:17:13 -0800 | [diff] [blame] | 132 | if (ArgumentEquals(arg, "--compilation-os-mode")) { |
Jiakai Zhang | 34dcce5 | 2022-01-07 16:45:11 +0000 | [diff] [blame] | 133 | config->SetCompilationOsMode(true); |
Alan Stokes | d4d21bf | 2021-10-13 11:07:25 +0100 | [diff] [blame] | 134 | } else if (ArgumentMatches(arg, "--dalvik-cache=", &value)) { |
| 135 | art::OverrideDalvikCacheSubDirectory(value); |
Alan Stokes | 1f8d261 | 2021-12-14 14:27:45 +0000 | [diff] [blame] | 136 | config->SetArtifactDirectory(GetApexDataDalvikCacheDirectory(art::InstructionSet::kNone)); |
Victor Hsieh | 3644cb4 | 2021-11-16 10:49:28 -0800 | [diff] [blame] | 137 | } else if (ArgumentMatches(arg, "--zygote-arch=", &value)) { |
| 138 | zygote = value; |
Victor Hsieh | a39356a | 2021-12-16 11:40:39 -0800 | [diff] [blame] | 139 | } else if (ArgumentMatches(arg, "--system-server-compiler-filter=", &value)) { |
| 140 | config->SetSystemServerCompilerFilter(value); |
Victor Hsieh | 27a740c | 2021-11-18 13:35:41 -0800 | [diff] [blame] | 141 | } else if (ArgumentMatches(arg, "--staging-dir=", &value)) { |
| 142 | config->SetStagingDir(value); |
Jiakai Zhang | fcdc7cf | 2022-01-07 14:40:26 +0000 | [diff] [blame] | 143 | } 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 Zhang | 8046b62 | 2022-01-28 21:40:17 +0000 | [diff] [blame] | 149 | } else if (ArgumentEquals(arg, "--minimal")) { |
| 150 | config->SetMinimal(true); |
Jiakai Zhang | fcdc7cf | 2022-01-07 14:40:26 +0000 | [diff] [blame] | 151 | } else { |
Alan Stokes | d98612e | 2022-01-10 17:59:01 +0000 | [diff] [blame] | 152 | ArgumentError("Unrecognized argument: '%s'", arg); |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
Victor Hsieh | 3644cb4 | 2021-11-16 10:49:28 -0800 | [diff] [blame] | 155 | |
| 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 Hsieh | a39356a | 2021-12-16 11:40:39 -0800 | [diff] [blame] | 166 | if (config->GetSystemServerCompilerFilter().empty()) { |
| 167 | std::string filter = |
| 168 | android::base::GetProperty("dalvik.vm.systemservercompilerfilter", "speed"); |
| 169 | config->SetSystemServerCompilerFilter(filter); |
| 170 | } |
| 171 | |
Jiakai Zhang | 39f6d00 | 2022-02-10 18:20:50 +0000 | [diff] [blame^] | 172 | if (ShouldDisableRefresh( |
| 173 | android::base::GetProperty("ro.build.version.sdk", /*default_value=*/""))) { |
| 174 | config->SetRefresh(false); |
| 175 | } |
| 176 | |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 177 | return n; |
| 178 | } |
| 179 | |
Alan Stokes | d4d21bf | 2021-10-13 11:07:25 +0100 | [diff] [blame] | 180 | NO_RETURN void UsageHelp(const char* argv0) { |
| 181 | std::string name(android::base::Basename(argv0)); |
Alan Stokes | d98612e | 2022-01-10 17:59:01 +0000 | [diff] [blame] | 182 | UsageMsg("Usage: %s [OPTION...] ACTION", name.c_str()); |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 183 | UsageMsg("On-device refresh tool for boot classpath and system server"); |
Alan Stokes | d98612e | 2022-01-10 17:59:01 +0000 | [diff] [blame] | 184 | 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 Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 189 | 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 Stokes | d98612e | 2022-01-10 17:59:01 +0000 | [diff] [blame] | 191 | 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 Stokes | d98612e | 2022-01-10 17:59:01 +0000 | [diff] [blame] | 199 | 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 Stokes | d98612e | 2022-01-10 17:59:01 +0000 | [diff] [blame] | 203 | 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 Zhang | 8046b62 | 2022-01-28 21:40:17 +0000 | [diff] [blame] | 209 | UsageMsg("--minimal Generate a minimal boot image only."); |
Alan Stokes | d4d21bf | 2021-10-13 11:07:25 +0100 | [diff] [blame] | 210 | |
| 211 | exit(EX_USAGE); |
| 212 | } |
| 213 | |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 214 | } // namespace |
| 215 | |
Orion Hodson | a182c93 | 2021-09-27 13:51:22 +0100 | [diff] [blame] | 216 | int main(int argc, char** argv) { |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 217 | // 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 Hodson | a182c93 | 2021-09-27 13:51:22 +0100 | [diff] [blame] | 222 | // Explicitly initialize logging (b/201042799). |
| 223 | android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM)); |
| 224 | |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 225 | OdrConfig config(argv[0]); |
| 226 | int n = InitializeConfig(argc, argv, &config); |
| 227 | argv += n; |
| 228 | argc -= n; |
| 229 | if (argc != 1) { |
Alan Stokes | d98612e | 2022-01-10 17:59:01 +0000 | [diff] [blame] | 230 | ArgumentError("Expected 1 argument, but have %d.", argc); |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Alan Stokes | d4d21bf | 2021-10-13 11:07:25 +0100 | [diff] [blame] | 233 | OdrMetrics metrics(config.GetArtifactDirectory()); |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 234 | OnDeviceRefresh odr(config); |
Victor Hsieh | 08e9f5f | 2021-12-16 10:22:02 -0800 | [diff] [blame] | 235 | |
| 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 Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 245 | } |
Victor Hsieh | 08e9f5f | 2021-12-16 10:22:02 -0800 | [diff] [blame] | 246 | 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 Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 265 | .compile_boot_classpath_for_isas = config.GetBootClasspathIsas(), |
| 266 | .system_server_jars_to_compile = odr.AllSystemServerJars(), |
Victor Hsieh | 08e9f5f | 2021-12-16 10:22:02 -0800 | [diff] [blame] | 267 | }); |
| 268 | } else if (action == "--help") { |
| 269 | UsageHelp(argv[0]); |
| 270 | } else { |
Alan Stokes | d98612e | 2022-01-10 17:59:01 +0000 | [diff] [blame] | 271 | ArgumentError("Unknown argument: ", action); |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 272 | } |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 273 | } |