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 | |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 22 | #include "android-base/parseint.h" |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 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 | |
| 36 | namespace { |
| 37 | |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 38 | using ::art::odrefresh::CompilationOptions; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 39 | using ::art::odrefresh::ExitCode; |
| 40 | using ::art::odrefresh::OdrCompilationLog; |
| 41 | using ::art::odrefresh::OdrConfig; |
| 42 | using ::art::odrefresh::OdrMetrics; |
| 43 | using ::art::odrefresh::OnDeviceRefresh; |
| 44 | using ::art::odrefresh::QuotePath; |
| 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; |
| 132 | if (ArgumentMatches(arg, "--use-compilation-os=", &value)) { |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 133 | int cid; |
| 134 | if (!android::base::ParseInt(value, &cid)) { |
| 135 | ArgumentError("Failed to parse CID: %s", value.c_str()); |
| 136 | } |
| 137 | config->SetCompilationOsAddress(cid); |
Jiakai Zhang | 34dcce5 | 2022-01-07 16:45:11 +0000 | [diff] [blame] | 138 | } else if (ArgumentEquals(arg, "--compilation-os-mode")) { |
| 139 | config->SetCompilationOsMode(true); |
Alan Stokes | d4d21bf | 2021-10-13 11:07:25 +0100 | [diff] [blame] | 140 | } else if (ArgumentMatches(arg, "--dalvik-cache=", &value)) { |
| 141 | art::OverrideDalvikCacheSubDirectory(value); |
Alan Stokes | 1f8d261 | 2021-12-14 14:27:45 +0000 | [diff] [blame] | 142 | config->SetArtifactDirectory(GetApexDataDalvikCacheDirectory(art::InstructionSet::kNone)); |
Victor Hsieh | 884fe76 | 2021-10-13 13:55:41 -0700 | [diff] [blame] | 143 | } else if (ArgumentMatches(arg, "--max-execution-seconds=", &value)) { |
| 144 | int seconds; |
| 145 | if (!android::base::ParseInt(value, &seconds)) { |
| 146 | ArgumentError("Failed to parse integer: %s", value.c_str()); |
| 147 | } |
| 148 | config->SetMaxExecutionSeconds(seconds); |
| 149 | } else if (ArgumentMatches(arg, "--max-child-process-seconds=", &value)) { |
| 150 | int seconds; |
| 151 | if (!android::base::ParseInt(value, &seconds)) { |
| 152 | ArgumentError("Failed to parse integer: %s", value.c_str()); |
| 153 | } |
| 154 | config->SetMaxChildProcessSeconds(seconds); |
Victor Hsieh | 3644cb4 | 2021-11-16 10:49:28 -0800 | [diff] [blame] | 155 | } else if (ArgumentMatches(arg, "--zygote-arch=", &value)) { |
| 156 | zygote = value; |
Victor Hsieh | a39356a | 2021-12-16 11:40:39 -0800 | [diff] [blame] | 157 | } else if (ArgumentMatches(arg, "--system-server-compiler-filter=", &value)) { |
| 158 | config->SetSystemServerCompilerFilter(value); |
Victor Hsieh | 27a740c | 2021-11-18 13:35:41 -0800 | [diff] [blame] | 159 | } else if (ArgumentMatches(arg, "--staging-dir=", &value)) { |
| 160 | config->SetStagingDir(value); |
Jiakai Zhang | fcdc7cf | 2022-01-07 14:40:26 +0000 | [diff] [blame] | 161 | } else if (ArgumentEquals(arg, "--dry-run")) { |
| 162 | config->SetDryRun(); |
| 163 | } else if (ArgumentEquals(arg, "--partial-compilation")) { |
| 164 | config->SetPartialCompilation(true); |
| 165 | } else if (ArgumentEquals(arg, "--no-refresh")) { |
| 166 | config->SetRefresh(false); |
| 167 | } else { |
Alan Stokes | d98612e | 2022-01-10 17:59:01 +0000 | [diff] [blame^] | 168 | ArgumentError("Unrecognized argument: '%s'", arg); |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
Victor Hsieh | 3644cb4 | 2021-11-16 10:49:28 -0800 | [diff] [blame] | 171 | |
| 172 | if (zygote.empty()) { |
| 173 | // Use ro.zygote by default, if not overridden by --zygote-arch flag. |
| 174 | zygote = android::base::GetProperty("ro.zygote", {}); |
| 175 | } |
| 176 | ZygoteKind zygote_kind; |
| 177 | if (!ParseZygoteKind(zygote.c_str(), &zygote_kind)) { |
| 178 | LOG(FATAL) << "Unknown zygote: " << QuotePath(zygote); |
| 179 | } |
| 180 | config->SetZygoteKind(zygote_kind); |
| 181 | |
Victor Hsieh | a39356a | 2021-12-16 11:40:39 -0800 | [diff] [blame] | 182 | if (config->GetSystemServerCompilerFilter().empty()) { |
| 183 | std::string filter = |
| 184 | android::base::GetProperty("dalvik.vm.systemservercompilerfilter", "speed"); |
| 185 | config->SetSystemServerCompilerFilter(filter); |
| 186 | } |
| 187 | |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 188 | return n; |
| 189 | } |
| 190 | |
Alan Stokes | d4d21bf | 2021-10-13 11:07:25 +0100 | [diff] [blame] | 191 | NO_RETURN void UsageHelp(const char* argv0) { |
| 192 | std::string name(android::base::Basename(argv0)); |
Alan Stokes | d98612e | 2022-01-10 17:59:01 +0000 | [diff] [blame^] | 193 | UsageMsg("Usage: %s [OPTION...] ACTION", name.c_str()); |
| 194 | UsageMsg("On-device refresh tool for boot class path extensions and system server"); |
| 195 | UsageMsg("following an update of the ART APEX."); |
| 196 | UsageMsg(""); |
| 197 | UsageMsg("Valid ACTION choices are:"); |
| 198 | UsageMsg(""); |
| 199 | UsageMsg("--check Check compilation artifacts are up-to-date based on metadata."); |
| 200 | UsageMsg("--compile Compile boot class path extensions and system_server jars"); |
| 201 | UsageMsg(" when necessary."); |
| 202 | UsageMsg("--force-compile Unconditionally compile the boot class path extensions and"); |
| 203 | UsageMsg(" system_server jars."); |
| 204 | UsageMsg("--help Display this help information."); |
| 205 | UsageMsg(""); |
| 206 | UsageMsg("Available OPTIONs are:"); |
| 207 | UsageMsg(""); |
| 208 | UsageMsg("--dry-run"); |
| 209 | UsageMsg("--partial-compilation Only generate artifacts that are out-of-date or"); |
| 210 | UsageMsg(" missing."); |
| 211 | UsageMsg("--no-refresh Do not refresh existing artifacts."); |
| 212 | UsageMsg("--use-compilation-os=<CID> Run compilation in the VM with the given CID."); |
| 213 | UsageMsg(" (0 = do not use VM, -1 = use composd's VM)"); |
| 214 | UsageMsg("--compilation-os-mode Indicate that odrefresh is running in Compilation"); |
| 215 | UsageMsg(" OS."); |
| 216 | UsageMsg("--dalvik-cache=<DIR> Write artifacts to .../<DIR> rather than"); |
| 217 | UsageMsg(" .../dalvik-cache"); |
| 218 | UsageMsg("--max-execution-seconds=<N> Maximum timeout of all compilation combined"); |
| 219 | UsageMsg("--max-child-process-seconds=<N> Maximum timeout of each compilation task"); |
| 220 | UsageMsg("--staging-dir=<DIR> Write temporary artifacts to <DIR> rather than"); |
| 221 | UsageMsg(" .../staging"); |
| 222 | UsageMsg("--zygote-arch=<STRING> Zygote kind that overrides ro.zygote"); |
| 223 | UsageMsg("--system-server-compiler-filter=<STRING>"); |
| 224 | UsageMsg(" Compiler filter that overrides"); |
| 225 | UsageMsg(" dalvik.vm.systemservercompilerfilter"); |
Alan Stokes | d4d21bf | 2021-10-13 11:07:25 +0100 | [diff] [blame] | 226 | |
| 227 | exit(EX_USAGE); |
| 228 | } |
| 229 | |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 230 | } // namespace |
| 231 | |
Orion Hodson | a182c93 | 2021-09-27 13:51:22 +0100 | [diff] [blame] | 232 | int main(int argc, char** argv) { |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 233 | // 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 Hodson | a182c93 | 2021-09-27 13:51:22 +0100 | [diff] [blame] | 238 | // Explicitly initialize logging (b/201042799). |
| 239 | android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM)); |
| 240 | |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 241 | OdrConfig config(argv[0]); |
| 242 | int n = InitializeConfig(argc, argv, &config); |
| 243 | argv += n; |
| 244 | argc -= n; |
| 245 | if (argc != 1) { |
Alan Stokes | d98612e | 2022-01-10 17:59:01 +0000 | [diff] [blame^] | 246 | ArgumentError("Expected 1 argument, but have %d.", argc); |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Alan Stokes | d4d21bf | 2021-10-13 11:07:25 +0100 | [diff] [blame] | 249 | OdrMetrics metrics(config.GetArtifactDirectory()); |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 250 | OnDeviceRefresh odr(config); |
Victor Hsieh | 08e9f5f | 2021-12-16 10:22:02 -0800 | [diff] [blame] | 251 | |
| 252 | std::string_view action(argv[0]); |
| 253 | CompilationOptions compilation_options; |
| 254 | if (action == "--check") { |
| 255 | // Fast determination of whether artifacts are up to date. |
| 256 | return odr.CheckArtifactsAreUpToDate(metrics, &compilation_options); |
| 257 | } else if (action == "--compile") { |
| 258 | const ExitCode exit_code = odr.CheckArtifactsAreUpToDate(metrics, &compilation_options); |
| 259 | if (exit_code != ExitCode::kCompilationRequired) { |
| 260 | return exit_code; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 261 | } |
Victor Hsieh | 08e9f5f | 2021-12-16 10:22:02 -0800 | [diff] [blame] | 262 | OdrCompilationLog compilation_log; |
| 263 | if (!compilation_log.ShouldAttemptCompile(metrics.GetTrigger())) { |
| 264 | LOG(INFO) << "Compilation skipped because it was attempted recently"; |
| 265 | return ExitCode::kOkay; |
| 266 | } |
| 267 | ExitCode compile_result = odr.Compile(metrics, compilation_options); |
| 268 | compilation_log.Log(metrics.GetArtApexVersion(), |
| 269 | metrics.GetArtApexLastUpdateMillis(), |
| 270 | metrics.GetTrigger(), |
| 271 | compile_result); |
| 272 | return compile_result; |
| 273 | } else if (action == "--force-compile") { |
| 274 | // Clean-up existing files. |
| 275 | if (!odr.RemoveArtifactsDirectory()) { |
| 276 | metrics.SetStatus(OdrMetrics::Status::kIoError); |
| 277 | return ExitCode::kCleanupFailed; |
| 278 | } |
| 279 | return odr.Compile(metrics, |
| 280 | CompilationOptions{ |
| 281 | .compile_boot_extensions_for_isas = config.GetBootExtensionIsas(), |
| 282 | .system_server_jars_to_compile = odr.AllSystemServerJars(), |
| 283 | }); |
| 284 | } else if (action == "--help") { |
| 285 | UsageHelp(argv[0]); |
| 286 | } else { |
Alan Stokes | d98612e | 2022-01-10 17:59:01 +0000 | [diff] [blame^] | 287 | ArgumentError("Unknown argument: ", action); |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 288 | } |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 289 | } |