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> |
Jiakai Zhang | 45d0881 | 2022-05-04 14:34:01 +0100 | [diff] [blame^] | 21 | #include <unordered_map> |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 22 | |
| 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 | 45d0881 | 2022-05-04 14:34:01 +0100 | [diff] [blame^] | 38 | using ::android::base::GetProperty; |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 39 | using ::art::odrefresh::CompilationOptions; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 40 | using ::art::odrefresh::ExitCode; |
Jiakai Zhang | 45d0881 | 2022-05-04 14:34:01 +0100 | [diff] [blame^] | 41 | using ::art::odrefresh::kSystemProperties; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 42 | using ::art::odrefresh::OdrCompilationLog; |
| 43 | using ::art::odrefresh::OdrConfig; |
| 44 | using ::art::odrefresh::OdrMetrics; |
| 45 | using ::art::odrefresh::OnDeviceRefresh; |
| 46 | using ::art::odrefresh::QuotePath; |
Jiakai Zhang | 38fd254 | 2022-03-07 15:58:59 +0000 | [diff] [blame] | 47 | using ::art::odrefresh::ShouldDisablePartialCompilation; |
Jiakai Zhang | 39f6d00 | 2022-02-10 18:20:50 +0000 | [diff] [blame] | 48 | using ::art::odrefresh::ShouldDisableRefresh; |
Jiakai Zhang | 45d0881 | 2022-05-04 14:34:01 +0100 | [diff] [blame^] | 49 | using ::art::odrefresh::SystemPropertyConfig; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 50 | using ::art::odrefresh::ZygoteKind; |
| 51 | |
Alan Stokes | d98612e | 2022-01-10 17:59:01 +0000 | [diff] [blame] | 52 | void UsageMsgV(const char* fmt, va_list ap) { |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 53 | 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 Stokes | d98612e | 2022-01-10 17:59:01 +0000 | [diff] [blame] | 62 | void UsageMsg(const char* fmt, ...) { |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 63 | va_list ap; |
| 64 | va_start(ap, fmt); |
Alan Stokes | d98612e | 2022-01-10 17:59:01 +0000 | [diff] [blame] | 65 | UsageMsgV(fmt, ap); |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 66 | va_end(ap); |
| 67 | } |
| 68 | |
| 69 | NO_RETURN void ArgumentError(const char* fmt, ...) { |
| 70 | va_list ap; |
| 71 | va_start(ap, fmt); |
Alan Stokes | d98612e | 2022-01-10 17:59:01 +0000 | [diff] [blame] | 72 | UsageMsgV(fmt, ap); |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 73 | va_end(ap); |
Alan Stokes | d98612e | 2022-01-10 17:59:01 +0000 | [diff] [blame] | 74 | UsageMsg("Try '--help' for more information."); |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 75 | exit(EX_USAGE); |
| 76 | } |
| 77 | |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 78 | bool 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 | |
| 96 | std::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 Zhang | 9b7ddf6 | 2021-11-01 11:36:52 +0000 | [diff] [blame] | 102 | std::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 Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 110 | bool 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 | |
| 118 | bool ArgumentEquals(std::string_view argument, std::string_view expected) { |
| 119 | return argument == expected; |
| 120 | } |
| 121 | |
Jiakai Zhang | fcdc7cf | 2022-01-07 14:40:26 +0000 | [diff] [blame] | 122 | int InitializeConfig(int argc, char** argv, OdrConfig* config) { |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 123 | 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 Zhang | 9b7ddf6 | 2021-11-01 11:36:52 +0000 | [diff] [blame] | 128 | config->SetStandaloneSystemServerJars( |
| 129 | GetEnvironmentVariableOrDefault("STANDALONE_SYSTEMSERVER_JARS", /*default_value=*/"")); |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 130 | config->SetIsa(art::kRuntimeISA); |
| 131 | |
Victor Hsieh | 3644cb4 | 2021-11-16 10:49:28 -0800 | [diff] [blame] | 132 | std::string zygote; |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 133 | int n = 1; |
| 134 | for (; n < argc - 1; ++n) { |
| 135 | const char* arg = argv[n]; |
| 136 | std::string value; |
Victor Hsieh | d0d1085 | 2022-01-04 16:17:13 -0800 | [diff] [blame] | 137 | if (ArgumentEquals(arg, "--compilation-os-mode")) { |
Jiakai Zhang | 34dcce5 | 2022-01-07 16:45:11 +0000 | [diff] [blame] | 138 | config->SetCompilationOsMode(true); |
Alan Stokes | d4d21bf | 2021-10-13 11:07:25 +0100 | [diff] [blame] | 139 | } else if (ArgumentMatches(arg, "--dalvik-cache=", &value)) { |
| 140 | art::OverrideDalvikCacheSubDirectory(value); |
Alan Stokes | 1f8d261 | 2021-12-14 14:27:45 +0000 | [diff] [blame] | 141 | config->SetArtifactDirectory(GetApexDataDalvikCacheDirectory(art::InstructionSet::kNone)); |
Victor Hsieh | 3644cb4 | 2021-11-16 10:49:28 -0800 | [diff] [blame] | 142 | } else if (ArgumentMatches(arg, "--zygote-arch=", &value)) { |
| 143 | zygote = value; |
Victor Hsieh | a39356a | 2021-12-16 11:40:39 -0800 | [diff] [blame] | 144 | } else if (ArgumentMatches(arg, "--system-server-compiler-filter=", &value)) { |
| 145 | config->SetSystemServerCompilerFilter(value); |
Victor Hsieh | 27a740c | 2021-11-18 13:35:41 -0800 | [diff] [blame] | 146 | } else if (ArgumentMatches(arg, "--staging-dir=", &value)) { |
| 147 | config->SetStagingDir(value); |
Jiakai Zhang | fcdc7cf | 2022-01-07 14:40:26 +0000 | [diff] [blame] | 148 | } 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 Zhang | 8046b62 | 2022-01-28 21:40:17 +0000 | [diff] [blame] | 154 | } else if (ArgumentEquals(arg, "--minimal")) { |
| 155 | config->SetMinimal(true); |
Jiakai Zhang | fcdc7cf | 2022-01-07 14:40:26 +0000 | [diff] [blame] | 156 | } else { |
Alan Stokes | d98612e | 2022-01-10 17:59:01 +0000 | [diff] [blame] | 157 | ArgumentError("Unrecognized argument: '%s'", arg); |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 158 | } |
| 159 | } |
Victor Hsieh | 3644cb4 | 2021-11-16 10:49:28 -0800 | [diff] [blame] | 160 | |
| 161 | if (zygote.empty()) { |
| 162 | // Use ro.zygote by default, if not overridden by --zygote-arch flag. |
Jiakai Zhang | 45d0881 | 2022-05-04 14:34:01 +0100 | [diff] [blame^] | 163 | zygote = GetProperty("ro.zygote", {}); |
Victor Hsieh | 3644cb4 | 2021-11-16 10:49:28 -0800 | [diff] [blame] | 164 | } |
| 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 Hsieh | a39356a | 2021-12-16 11:40:39 -0800 | [diff] [blame] | 171 | if (config->GetSystemServerCompilerFilter().empty()) { |
Jiakai Zhang | 45d0881 | 2022-05-04 14:34:01 +0100 | [diff] [blame^] | 172 | std::string filter = GetProperty("dalvik.vm.systemservercompilerfilter", "speed"); |
Victor Hsieh | a39356a | 2021-12-16 11:40:39 -0800 | [diff] [blame] | 173 | config->SetSystemServerCompilerFilter(filter); |
| 174 | } |
| 175 | |
Jiakai Zhang | 38fd254 | 2022-03-07 15:58:59 +0000 | [diff] [blame] | 176 | if (!config->HasPartialCompilation() && |
| 177 | ShouldDisablePartialCompilation( |
Jiakai Zhang | 45d0881 | 2022-05-04 14:34:01 +0100 | [diff] [blame^] | 178 | GetProperty("ro.build.version.security_patch", /*default_value=*/""))) { |
Jiakai Zhang | 38fd254 | 2022-03-07 15:58:59 +0000 | [diff] [blame] | 179 | config->SetPartialCompilation(false); |
| 180 | } |
| 181 | |
Jiakai Zhang | 45d0881 | 2022-05-04 14:34:01 +0100 | [diff] [blame^] | 182 | if (ShouldDisableRefresh(GetProperty("ro.build.version.sdk", /*default_value=*/""))) { |
Jiakai Zhang | 39f6d00 | 2022-02-10 18:20:50 +0000 | [diff] [blame] | 183 | config->SetRefresh(false); |
| 184 | } |
| 185 | |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 186 | return n; |
| 187 | } |
| 188 | |
Jiakai Zhang | 45d0881 | 2022-05-04 14:34:01 +0100 | [diff] [blame^] | 189 | void 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 Stokes | d4d21bf | 2021-10-13 11:07:25 +0100 | [diff] [blame] | 196 | NO_RETURN void UsageHelp(const char* argv0) { |
| 197 | std::string name(android::base::Basename(argv0)); |
Alan Stokes | d98612e | 2022-01-10 17:59:01 +0000 | [diff] [blame] | 198 | UsageMsg("Usage: %s [OPTION...] ACTION", name.c_str()); |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 199 | UsageMsg("On-device refresh tool for boot classpath and system server"); |
Alan Stokes | d98612e | 2022-01-10 17:59:01 +0000 | [diff] [blame] | 200 | 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 Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 205 | 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 Stokes | d98612e | 2022-01-10 17:59:01 +0000 | [diff] [blame] | 207 | 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 Stokes | d98612e | 2022-01-10 17:59:01 +0000 | [diff] [blame] | 215 | 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 Stokes | d98612e | 2022-01-10 17:59:01 +0000 | [diff] [blame] | 219 | 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 Zhang | 8046b62 | 2022-01-28 21:40:17 +0000 | [diff] [blame] | 225 | UsageMsg("--minimal Generate a minimal boot image only."); |
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 | } |
Jiakai Zhang | 45d0881 | 2022-05-04 14:34:01 +0100 | [diff] [blame^] | 248 | GetSystemProperties(config.MutableSystemProperties()); |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 249 | |
Alan Stokes | d4d21bf | 2021-10-13 11:07:25 +0100 | [diff] [blame] | 250 | OdrMetrics metrics(config.GetArtifactDirectory()); |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 251 | OnDeviceRefresh odr(config); |
Victor Hsieh | 08e9f5f | 2021-12-16 10:22:02 -0800 | [diff] [blame] | 252 | |
| 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 Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 262 | } |
Victor Hsieh | 08e9f5f | 2021-12-16 10:22:02 -0800 | [diff] [blame] | 263 | 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 Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 282 | .compile_boot_classpath_for_isas = config.GetBootClasspathIsas(), |
| 283 | .system_server_jars_to_compile = odr.AllSystemServerJars(), |
Victor Hsieh | 08e9f5f | 2021-12-16 10:22:02 -0800 | [diff] [blame] | 284 | }); |
| 285 | } else if (action == "--help") { |
| 286 | UsageHelp(argv[0]); |
| 287 | } else { |
Jiakai Zhang | 45d0881 | 2022-05-04 14:34:01 +0100 | [diff] [blame^] | 288 | ArgumentError("Unknown argument: %s", action.data()); |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 289 | } |
Jiakai Zhang | 3ba3edf | 2021-08-11 08:25:40 +0000 | [diff] [blame] | 290 | } |