Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 17 | #include "errno.h" |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <sys/file.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <unistd.h> |
| 23 | |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 24 | #include <fstream> |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 25 | #include <iostream> |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 26 | #include <set> |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 27 | #include <string> |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 28 | #include <unordered_set> |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 29 | #include <vector> |
| 30 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 31 | #include "android-base/stringprintf.h" |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 32 | #include "android-base/strings.h" |
| 33 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 34 | #include "base/dumpable.h" |
| 35 | #include "base/scoped_flock.h" |
| 36 | #include "base/stringpiece.h" |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 37 | #include "base/time_utils.h" |
| 38 | #include "base/unix_file/fd_file.h" |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 39 | #include "dex_file.h" |
Calin Juravle | 33083d6 | 2017-01-18 15:29:12 -0800 | [diff] [blame] | 40 | #include "jit/profile_compilation_info.h" |
David Sehr | f57589f | 2016-10-17 10:09:33 -0700 | [diff] [blame] | 41 | #include "runtime.h" |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 42 | #include "utils.h" |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 43 | #include "zip_archive.h" |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 44 | #include "profile_assistant.h" |
| 45 | |
| 46 | namespace art { |
| 47 | |
| 48 | static int original_argc; |
| 49 | static char** original_argv; |
| 50 | |
| 51 | static std::string CommandLine() { |
| 52 | std::vector<std::string> command; |
| 53 | for (int i = 0; i < original_argc; ++i) { |
| 54 | command.push_back(original_argv[i]); |
| 55 | } |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 56 | return android::base::Join(command, ' '); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 57 | } |
| 58 | |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 59 | static constexpr int kInvalidFd = -1; |
| 60 | |
| 61 | static bool FdIsValid(int fd) { |
| 62 | return fd != kInvalidFd; |
| 63 | } |
| 64 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 65 | static void UsageErrorV(const char* fmt, va_list ap) { |
| 66 | std::string error; |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 67 | android::base::StringAppendV(&error, fmt, ap); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 68 | LOG(ERROR) << error; |
| 69 | } |
| 70 | |
| 71 | static void UsageError(const char* fmt, ...) { |
| 72 | va_list ap; |
| 73 | va_start(ap, fmt); |
| 74 | UsageErrorV(fmt, ap); |
| 75 | va_end(ap); |
| 76 | } |
| 77 | |
| 78 | NO_RETURN static void Usage(const char *fmt, ...) { |
| 79 | va_list ap; |
| 80 | va_start(ap, fmt); |
| 81 | UsageErrorV(fmt, ap); |
| 82 | va_end(ap); |
| 83 | |
| 84 | UsageError("Command: %s", CommandLine().c_str()); |
| 85 | UsageError("Usage: profman [options]..."); |
| 86 | UsageError(""); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 87 | UsageError(" --dump-only: dumps the content of the specified profile files"); |
| 88 | UsageError(" to standard output (default) in a human readable form."); |
| 89 | UsageError(""); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 90 | UsageError(" --dump-output-to-fd=<number>: redirects --dump-only output to a file descriptor."); |
| 91 | UsageError(""); |
| 92 | UsageError(" --dump-classes: dumps a sorted list of classes that are in the specified profile"); |
| 93 | UsageError(" file to standard output (default) in a human readable form."); |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 94 | UsageError(""); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 95 | UsageError(" --profile-file=<filename>: specify profiler output file to use for compilation."); |
| 96 | UsageError(" Can be specified multiple time, in which case the data from the different"); |
| 97 | UsageError(" profiles will be aggregated."); |
| 98 | UsageError(""); |
| 99 | UsageError(" --profile-file-fd=<number>: same as --profile-file but accepts a file descriptor."); |
| 100 | UsageError(" Cannot be used together with --profile-file."); |
| 101 | UsageError(""); |
| 102 | UsageError(" --reference-profile-file=<filename>: specify a reference profile."); |
| 103 | UsageError(" The data in this file will be compared with the data obtained by merging"); |
| 104 | UsageError(" all the files specified with --profile-file or --profile-file-fd."); |
| 105 | UsageError(" If the exit code is EXIT_COMPILE then all --profile-file will be merged into"); |
| 106 | UsageError(" --reference-profile-file. "); |
| 107 | UsageError(""); |
| 108 | UsageError(" --reference-profile-file-fd=<number>: same as --reference-profile-file but"); |
| 109 | UsageError(" accepts a file descriptor. Cannot be used together with"); |
| 110 | UsageError(" --reference-profile-file."); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 111 | UsageError(""); |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 112 | UsageError(" --generate-test-profile=<filename>: generates a random profile file for testing."); |
| 113 | UsageError(" --generate-test-profile-num-dex=<number>: number of dex files that should be"); |
| 114 | UsageError(" included in the generated profile. Defaults to 20."); |
| 115 | UsageError(" --generate-test-profile-method-ratio=<number>: the percentage from the maximum"); |
| 116 | UsageError(" number of methods that should be generated. Defaults to 5."); |
| 117 | UsageError(" --generate-test-profile-class-ratio=<number>: the percentage from the maximum"); |
| 118 | UsageError(" number of classes that should be generated. Defaults to 5."); |
| 119 | UsageError(""); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 120 | UsageError(" --create-profile-from=<filename>: creates a profile from a list of classes."); |
| 121 | UsageError(""); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 122 | UsageError(""); |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 123 | UsageError(" --dex-location=<string>: location string to use with corresponding"); |
| 124 | UsageError(" apk-fd to find dex files"); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 125 | UsageError(""); |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 126 | UsageError(" --apk-fd=<number>: file descriptor containing an open APK to"); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 127 | UsageError(" search for dex files"); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 128 | UsageError(" --apk-=<filename>: an APK to search for dex files"); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 129 | UsageError(""); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 130 | |
| 131 | exit(EXIT_FAILURE); |
| 132 | } |
| 133 | |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 134 | // Note: make sure you update the Usage if you change these values. |
| 135 | static constexpr uint16_t kDefaultTestProfileNumDex = 20; |
| 136 | static constexpr uint16_t kDefaultTestProfileMethodRatio = 5; |
| 137 | static constexpr uint16_t kDefaultTestProfileClassRatio = 5; |
| 138 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 139 | class ProfMan FINAL { |
| 140 | public: |
| 141 | ProfMan() : |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 142 | reference_profile_file_fd_(kInvalidFd), |
| 143 | dump_only_(false), |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 144 | dump_classes_(false), |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 145 | dump_output_to_fd_(kInvalidFd), |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 146 | test_profile_num_dex_(kDefaultTestProfileNumDex), |
| 147 | test_profile_method_ratio_(kDefaultTestProfileMethodRatio), |
| 148 | test_profile_class_ratio_(kDefaultTestProfileClassRatio), |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 149 | start_ns_(NanoTime()) {} |
| 150 | |
| 151 | ~ProfMan() { |
| 152 | LogCompletionTime(); |
| 153 | } |
| 154 | |
| 155 | void ParseArgs(int argc, char **argv) { |
| 156 | original_argc = argc; |
| 157 | original_argv = argv; |
| 158 | |
David Sehr | f57589f | 2016-10-17 10:09:33 -0700 | [diff] [blame] | 159 | InitLogging(argv, Runtime::Aborter); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 160 | |
| 161 | // Skip over the command name. |
| 162 | argv++; |
| 163 | argc--; |
| 164 | |
| 165 | if (argc == 0) { |
| 166 | Usage("No arguments specified"); |
| 167 | } |
| 168 | |
| 169 | for (int i = 0; i < argc; ++i) { |
| 170 | const StringPiece option(argv[i]); |
| 171 | const bool log_options = false; |
| 172 | if (log_options) { |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 173 | LOG(INFO) << "profman: option[" << i << "]=" << argv[i]; |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 174 | } |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 175 | if (option == "--dump-only") { |
| 176 | dump_only_ = true; |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 177 | } else if (option == "--dump-classes") { |
| 178 | dump_classes_ = true; |
| 179 | } else if (option.starts_with("--create-profile-from=")) { |
| 180 | create_profile_from_file_ = option.substr(strlen("--create-profile-from=")).ToString(); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 181 | } else if (option.starts_with("--dump-output-to-fd=")) { |
| 182 | ParseUintOption(option, "--dump-output-to-fd", &dump_output_to_fd_, Usage); |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 183 | } else if (option.starts_with("--profile-file=")) { |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 184 | profile_files_.push_back(option.substr(strlen("--profile-file=")).ToString()); |
| 185 | } else if (option.starts_with("--profile-file-fd=")) { |
| 186 | ParseFdForCollection(option, "--profile-file-fd", &profile_files_fd_); |
| 187 | } else if (option.starts_with("--reference-profile-file=")) { |
| 188 | reference_profile_file_ = option.substr(strlen("--reference-profile-file=")).ToString(); |
| 189 | } else if (option.starts_with("--reference-profile-file-fd=")) { |
| 190 | ParseUintOption(option, "--reference-profile-file-fd", &reference_profile_file_fd_, Usage); |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 191 | } else if (option.starts_with("--dex-location=")) { |
| 192 | dex_locations_.push_back(option.substr(strlen("--dex-location=")).ToString()); |
| 193 | } else if (option.starts_with("--apk-fd=")) { |
| 194 | ParseFdForCollection(option, "--apk-fd", &apks_fd_); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 195 | } else if (option.starts_with("--apk=")) { |
| 196 | apk_files_.push_back(option.substr(strlen("--apk=")).ToString()); |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 197 | } else if (option.starts_with("--generate-test-profile=")) { |
| 198 | test_profile_ = option.substr(strlen("--generate-test-profile=")).ToString(); |
| 199 | } else if (option.starts_with("--generate-test-profile-num-dex=")) { |
| 200 | ParseUintOption(option, |
| 201 | "--generate-test-profile-num-dex", |
| 202 | &test_profile_num_dex_, |
| 203 | Usage); |
| 204 | } else if (option.starts_with("--generate-test-profile-method-ratio")) { |
| 205 | ParseUintOption(option, |
| 206 | "--generate-test-profile-method-ratio", |
| 207 | &test_profile_method_ratio_, |
| 208 | Usage); |
| 209 | } else if (option.starts_with("--generate-test-profile-class-ratio")) { |
| 210 | ParseUintOption(option, |
| 211 | "--generate-test-profile-class-ratio", |
| 212 | &test_profile_class_ratio_, |
| 213 | Usage); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 214 | } else { |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 215 | Usage("Unknown argument '%s'", option.data()); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
David Sehr | 153da0e | 2017-02-15 08:54:51 -0800 | [diff] [blame^] | 219 | // Validate global consistency between file/fd options. |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 220 | if (!profile_files_.empty() && !profile_files_fd_.empty()) { |
| 221 | Usage("Profile files should not be specified with both --profile-file-fd and --profile-file"); |
| 222 | } |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 223 | if (!reference_profile_file_.empty() && FdIsValid(reference_profile_file_fd_)) { |
| 224 | Usage("Reference profile should not be specified with both " |
| 225 | "--reference-profile-file-fd and --reference-profile-file"); |
| 226 | } |
David Sehr | 153da0e | 2017-02-15 08:54:51 -0800 | [diff] [blame^] | 227 | if (!apk_files_.empty() && !apks_fd_.empty()) { |
| 228 | Usage("APK files should not be specified with both --apk-fd and --apk"); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
| 232 | ProfileAssistant::ProcessingResult ProcessProfiles() { |
David Sehr | 153da0e | 2017-02-15 08:54:51 -0800 | [diff] [blame^] | 233 | // Validate that at least one profile file was passed, as well as a reference profile. |
| 234 | if (profile_files_.empty() && profile_files_fd_.empty()) { |
| 235 | Usage("No profile files specified."); |
| 236 | } |
| 237 | if (reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) { |
| 238 | Usage("No reference profile file specified."); |
| 239 | } |
| 240 | if ((!profile_files_.empty() && FdIsValid(reference_profile_file_fd_)) || |
| 241 | (!profile_files_fd_.empty() && !FdIsValid(reference_profile_file_fd_))) { |
| 242 | Usage("Options --profile-file-fd and --reference-profile-file-fd " |
| 243 | "should only be used together"); |
| 244 | } |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 245 | ProfileAssistant::ProcessingResult result; |
| 246 | if (profile_files_.empty()) { |
| 247 | // The file doesn't need to be flushed here (ProcessProfiles will do it) |
| 248 | // so don't check the usage. |
| 249 | File file(reference_profile_file_fd_, false); |
| 250 | result = ProfileAssistant::ProcessProfiles(profile_files_fd_, reference_profile_file_fd_); |
| 251 | CloseAllFds(profile_files_fd_, "profile_files_fd_"); |
| 252 | } else { |
| 253 | result = ProfileAssistant::ProcessProfiles(profile_files_, reference_profile_file_); |
| 254 | } |
| 255 | return result; |
| 256 | } |
| 257 | |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 258 | void OpenApkFilesFromLocations(std::vector<std::unique_ptr<const DexFile>>* dex_files) { |
| 259 | bool use_apk_fd_list = !apks_fd_.empty(); |
| 260 | if (use_apk_fd_list) { |
David Sehr | 153da0e | 2017-02-15 08:54:51 -0800 | [diff] [blame^] | 261 | // Get the APKs from the collection of FDs. |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 262 | CHECK_EQ(dex_locations_.size(), apks_fd_.size()); |
David Sehr | 153da0e | 2017-02-15 08:54:51 -0800 | [diff] [blame^] | 263 | } else if (!apk_files_.empty()) { |
| 264 | // Get the APKs from the collection of filenames. |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 265 | CHECK_EQ(dex_locations_.size(), apk_files_.size()); |
David Sehr | 153da0e | 2017-02-15 08:54:51 -0800 | [diff] [blame^] | 266 | } else { |
| 267 | // No APKs were specified. |
| 268 | CHECK(dex_locations_.empty()); |
| 269 | return; |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 270 | } |
| 271 | static constexpr bool kVerifyChecksum = true; |
| 272 | for (size_t i = 0; i < dex_locations_.size(); ++i) { |
| 273 | std::string error_msg; |
| 274 | std::vector<std::unique_ptr<const DexFile>> dex_files_for_location; |
| 275 | if (use_apk_fd_list) { |
| 276 | if (DexFile::OpenZip(apks_fd_[i], |
| 277 | dex_locations_[i], |
| 278 | kVerifyChecksum, |
| 279 | &error_msg, |
| 280 | &dex_files_for_location)) { |
| 281 | } else { |
| 282 | LOG(WARNING) << "OpenZip failed for '" << dex_locations_[i] << "' " << error_msg; |
| 283 | continue; |
| 284 | } |
| 285 | } else { |
| 286 | if (DexFile::Open(apk_files_[i].c_str(), |
| 287 | dex_locations_[i], |
| 288 | kVerifyChecksum, |
| 289 | &error_msg, |
| 290 | &dex_files_for_location)) { |
| 291 | } else { |
| 292 | LOG(WARNING) << "Open failed for '" << dex_locations_[i] << "' " << error_msg; |
| 293 | continue; |
| 294 | } |
| 295 | } |
| 296 | for (std::unique_ptr<const DexFile>& dex_file : dex_files_for_location) { |
| 297 | dex_files->emplace_back(std::move(dex_file)); |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
David Sehr | b18991b | 2017-02-08 20:58:10 -0800 | [diff] [blame] | 302 | int DumpOneProfile(const std::string& banner, |
| 303 | const std::string& filename, |
| 304 | int fd, |
| 305 | const std::vector<std::unique_ptr<const DexFile>>* dex_files, |
| 306 | std::string* dump) { |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 307 | if (!filename.empty()) { |
| 308 | fd = open(filename.c_str(), O_RDWR); |
| 309 | if (fd < 0) { |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 310 | LOG(ERROR) << "Cannot open " << filename << strerror(errno); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 311 | return -1; |
| 312 | } |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 313 | } |
| 314 | ProfileCompilationInfo info; |
| 315 | if (!info.Load(fd)) { |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 316 | LOG(ERROR) << "Cannot load profile info from fd=" << fd << "\n"; |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 317 | return -1; |
| 318 | } |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 319 | std::string this_dump = banner + "\n" + info.DumpInfo(dex_files) + "\n"; |
| 320 | *dump += this_dump; |
| 321 | if (close(fd) < 0) { |
| 322 | PLOG(WARNING) << "Failed to close descriptor"; |
| 323 | } |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | int DumpProfileInfo() { |
David Sehr | 153da0e | 2017-02-15 08:54:51 -0800 | [diff] [blame^] | 328 | // Validate that at least one profile file or reference was specified. |
| 329 | if (profile_files_.empty() && profile_files_fd_.empty() && |
| 330 | reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) { |
| 331 | Usage("No profile files or reference profile specified."); |
| 332 | } |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 333 | static const char* kEmptyString = ""; |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 334 | static const char* kOrdinaryProfile = "=== profile ==="; |
| 335 | static const char* kReferenceProfile = "=== reference profile ==="; |
| 336 | |
| 337 | // Open apk/zip files and and read dex files. |
| 338 | MemMap::Init(); // for ZipArchive::OpenFromFd |
David Sehr | b18991b | 2017-02-08 20:58:10 -0800 | [diff] [blame] | 339 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 340 | OpenApkFilesFromLocations(&dex_files); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 341 | std::string dump; |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 342 | // Dump individual profile files. |
| 343 | if (!profile_files_fd_.empty()) { |
| 344 | for (int profile_file_fd : profile_files_fd_) { |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 345 | int ret = DumpOneProfile(kOrdinaryProfile, |
| 346 | kEmptyString, |
| 347 | profile_file_fd, |
| 348 | &dex_files, |
| 349 | &dump); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 350 | if (ret != 0) { |
| 351 | return ret; |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | if (!profile_files_.empty()) { |
| 356 | for (const std::string& profile_file : profile_files_) { |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 357 | int ret = DumpOneProfile(kOrdinaryProfile, profile_file, kInvalidFd, &dex_files, &dump); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 358 | if (ret != 0) { |
| 359 | return ret; |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | // Dump reference profile file. |
| 364 | if (FdIsValid(reference_profile_file_fd_)) { |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 365 | int ret = DumpOneProfile(kReferenceProfile, |
| 366 | kEmptyString, |
| 367 | reference_profile_file_fd_, |
| 368 | &dex_files, |
| 369 | &dump); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 370 | if (ret != 0) { |
| 371 | return ret; |
| 372 | } |
| 373 | } |
| 374 | if (!reference_profile_file_.empty()) { |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 375 | int ret = DumpOneProfile(kReferenceProfile, |
| 376 | reference_profile_file_, |
| 377 | kInvalidFd, |
| 378 | &dex_files, |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 379 | &dump); |
| 380 | if (ret != 0) { |
| 381 | return ret; |
| 382 | } |
| 383 | } |
| 384 | if (!FdIsValid(dump_output_to_fd_)) { |
| 385 | std::cout << dump; |
| 386 | } else { |
| 387 | unix_file::FdFile out_fd(dump_output_to_fd_, false /*check_usage*/); |
| 388 | if (!out_fd.WriteFully(dump.c_str(), dump.length())) { |
| 389 | return -1; |
| 390 | } |
| 391 | } |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | bool ShouldOnlyDumpProfile() { |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 396 | return dump_only_; |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 397 | } |
| 398 | |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 399 | bool GetClassNames(int fd, |
| 400 | std::vector<std::unique_ptr<const DexFile>>* dex_files, |
| 401 | std::set<std::string>* class_names) { |
| 402 | ProfileCompilationInfo profile_info; |
| 403 | if (!profile_info.Load(fd)) { |
| 404 | LOG(ERROR) << "Cannot load profile info"; |
| 405 | return false; |
| 406 | } |
| 407 | profile_info.GetClassNames(dex_files, class_names); |
| 408 | return true; |
| 409 | } |
| 410 | |
| 411 | bool GetClassNames(std::string profile_file, |
| 412 | std::vector<std::unique_ptr<const DexFile>>* dex_files, |
| 413 | std::set<std::string>* class_names) { |
| 414 | int fd = open(profile_file.c_str(), O_RDONLY); |
| 415 | if (!FdIsValid(fd)) { |
| 416 | LOG(ERROR) << "Cannot open " << profile_file << strerror(errno); |
| 417 | return false; |
| 418 | } |
| 419 | if (!GetClassNames(fd, dex_files, class_names)) { |
| 420 | return false; |
| 421 | } |
| 422 | if (close(fd) < 0) { |
| 423 | PLOG(WARNING) << "Failed to close descriptor"; |
| 424 | } |
| 425 | return true; |
| 426 | } |
| 427 | |
| 428 | int DumpClasses() { |
David Sehr | 153da0e | 2017-02-15 08:54:51 -0800 | [diff] [blame^] | 429 | // Validate that at least one profile file or reference was specified. |
| 430 | if (profile_files_.empty() && profile_files_fd_.empty() && |
| 431 | reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) { |
| 432 | Usage("No profile files or reference profile specified."); |
| 433 | } |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 434 | // Open apk/zip files and and read dex files. |
| 435 | MemMap::Init(); // for ZipArchive::OpenFromFd |
| 436 | // Open the dex files to get the names for classes. |
| 437 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 438 | OpenApkFilesFromLocations(&dex_files); |
| 439 | // Build a vector of class names from individual profile files. |
| 440 | std::set<std::string> class_names; |
| 441 | if (!profile_files_fd_.empty()) { |
| 442 | for (int profile_file_fd : profile_files_fd_) { |
| 443 | if (!GetClassNames(profile_file_fd, &dex_files, &class_names)) { |
| 444 | return -1; |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | if (!profile_files_.empty()) { |
| 449 | for (const std::string& profile_file : profile_files_) { |
| 450 | if (!GetClassNames(profile_file, &dex_files, &class_names)) { |
| 451 | return -1; |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | // Concatenate class names from reference profile file. |
| 456 | if (FdIsValid(reference_profile_file_fd_)) { |
| 457 | if (!GetClassNames(reference_profile_file_fd_, &dex_files, &class_names)) { |
| 458 | return -1; |
| 459 | } |
| 460 | } |
| 461 | if (!reference_profile_file_.empty()) { |
| 462 | if (!GetClassNames(reference_profile_file_, &dex_files, &class_names)) { |
| 463 | return -1; |
| 464 | } |
| 465 | } |
| 466 | // Dump the class names. |
| 467 | std::string dump; |
| 468 | for (const std::string& class_name : class_names) { |
| 469 | dump += class_name + std::string("\n"); |
| 470 | } |
| 471 | if (!FdIsValid(dump_output_to_fd_)) { |
| 472 | std::cout << dump; |
| 473 | } else { |
| 474 | unix_file::FdFile out_fd(dump_output_to_fd_, false /*check_usage*/); |
| 475 | if (!out_fd.WriteFully(dump.c_str(), dump.length())) { |
| 476 | return -1; |
| 477 | } |
| 478 | } |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | bool ShouldOnlyDumpClasses() { |
| 483 | return dump_classes_; |
| 484 | } |
| 485 | |
| 486 | // Read lines from the given file, dropping comments and empty lines. Post-process each line with |
| 487 | // the given function. |
| 488 | template <typename T> |
| 489 | static T* ReadCommentedInputFromFile( |
| 490 | const char* input_filename, std::function<std::string(const char*)>* process) { |
| 491 | std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in)); |
| 492 | if (input_file.get() == nullptr) { |
| 493 | LOG(ERROR) << "Failed to open input file " << input_filename; |
| 494 | return nullptr; |
| 495 | } |
| 496 | std::unique_ptr<T> result( |
| 497 | ReadCommentedInputStream<T>(*input_file, process)); |
| 498 | input_file->close(); |
| 499 | return result.release(); |
| 500 | } |
| 501 | |
| 502 | // Read lines from the given stream, dropping comments and empty lines. Post-process each line |
| 503 | // with the given function. |
| 504 | template <typename T> |
| 505 | static T* ReadCommentedInputStream( |
| 506 | std::istream& in_stream, |
| 507 | std::function<std::string(const char*)>* process) { |
| 508 | std::unique_ptr<T> output(new T()); |
| 509 | while (in_stream.good()) { |
| 510 | std::string dot; |
| 511 | std::getline(in_stream, dot); |
| 512 | if (android::base::StartsWith(dot, "#") || dot.empty()) { |
| 513 | continue; |
| 514 | } |
| 515 | if (process != nullptr) { |
| 516 | std::string descriptor((*process)(dot.c_str())); |
| 517 | output->insert(output->end(), descriptor); |
| 518 | } else { |
| 519 | output->insert(output->end(), dot); |
| 520 | } |
| 521 | } |
| 522 | return output.release(); |
| 523 | } |
| 524 | |
| 525 | int CreateProfile() { |
David Sehr | 153da0e | 2017-02-15 08:54:51 -0800 | [diff] [blame^] | 526 | // Validate parameters for this command. |
| 527 | if (apk_files_.empty() && apks_fd_.empty()) { |
| 528 | Usage("APK files must be specified"); |
| 529 | } |
| 530 | if (dex_locations_.empty()) { |
| 531 | Usage("DEX locations must be specified"); |
| 532 | } |
| 533 | if (reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) { |
| 534 | Usage("Reference profile must be specified with --reference-profile-file or " |
| 535 | "--reference-profile-file-fd"); |
| 536 | } |
| 537 | if (!profile_files_.empty() || !profile_files_fd_.empty()) { |
| 538 | Usage("Profile must be specified with --reference-profile-file or " |
| 539 | "--reference-profile-file-fd"); |
| 540 | } |
| 541 | // for ZipArchive::OpenFromFd |
| 542 | MemMap::Init(); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 543 | // Open the profile output file if needed. |
| 544 | int fd = reference_profile_file_fd_; |
| 545 | if (!FdIsValid(fd)) { |
| 546 | CHECK(!reference_profile_file_.empty()); |
| 547 | fd = open(reference_profile_file_.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644); |
| 548 | if (fd < 0) { |
| 549 | LOG(ERROR) << "Cannot open " << reference_profile_file_ << strerror(errno); |
| 550 | return -1; |
| 551 | } |
| 552 | } |
| 553 | // Read the user-specified list of classes (dot notation rather than descriptors). |
| 554 | std::unique_ptr<std::unordered_set<std::string>> |
| 555 | user_class_list(ReadCommentedInputFromFile<std::unordered_set<std::string>>( |
| 556 | create_profile_from_file_.c_str(), nullptr)); // No post-processing. |
| 557 | std::unordered_set<std::string> matched_user_classes; |
| 558 | // Open the dex files to look up class names. |
| 559 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 560 | OpenApkFilesFromLocations(&dex_files); |
| 561 | // Iterate over the dex files looking for class names in the input stream. |
| 562 | std::set<DexCacheResolvedClasses> resolved_class_set; |
| 563 | for (auto& dex_file : dex_files) { |
| 564 | // Compute the set of classes to be added for this dex file first. This |
| 565 | // avoids creating an entry in the profile information for dex files that |
| 566 | // contribute no classes. |
| 567 | std::unordered_set<dex::TypeIndex> classes_to_be_added; |
| 568 | for (const auto& klass : *user_class_list) { |
| 569 | std::string descriptor = DotToDescriptor(klass.c_str()); |
| 570 | const DexFile::TypeId* type_id = dex_file->FindTypeId(descriptor.c_str()); |
| 571 | if (type_id == nullptr) { |
| 572 | continue; |
| 573 | } |
| 574 | classes_to_be_added.insert(dex_file->GetIndexForTypeId(*type_id)); |
| 575 | matched_user_classes.insert(klass); |
| 576 | } |
| 577 | if (classes_to_be_added.empty()) { |
| 578 | continue; |
| 579 | } |
| 580 | // Insert the DexCacheResolved Classes into the set expected for |
| 581 | // AddMethodsAndClasses. |
| 582 | std::set<DexCacheResolvedClasses>::iterator dex_resolved_classes = |
| 583 | resolved_class_set.emplace(dex_file->GetLocation(), |
| 584 | dex_file->GetBaseLocation(), |
| 585 | dex_file->GetLocationChecksum()).first; |
| 586 | dex_resolved_classes->AddClasses(classes_to_be_added.begin(), classes_to_be_added.end()); |
| 587 | } |
| 588 | // Warn the user if we didn't find matches for every class. |
| 589 | for (const auto& klass : *user_class_list) { |
| 590 | if (matched_user_classes.find(klass) == matched_user_classes.end()) { |
| 591 | LOG(WARNING) << "requested class '" << klass << "' was not matched in any dex file"; |
| 592 | } |
| 593 | } |
| 594 | // Generate the profile data structure. |
| 595 | ProfileCompilationInfo info; |
| 596 | std::vector<MethodReference> methods; // No methods for now. |
| 597 | info.AddMethodsAndClasses(methods, resolved_class_set); |
| 598 | // Write the profile file. |
| 599 | CHECK(info.Save(fd)); |
| 600 | if (close(fd) < 0) { |
| 601 | PLOG(WARNING) << "Failed to close descriptor"; |
| 602 | } |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | bool ShouldCreateProfile() { |
| 607 | return !create_profile_from_file_.empty(); |
| 608 | } |
| 609 | |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 610 | int GenerateTestProfile() { |
David Sehr | 153da0e | 2017-02-15 08:54:51 -0800 | [diff] [blame^] | 611 | // Validate parameters for this command. |
| 612 | if (test_profile_method_ratio_ > 100) { |
| 613 | Usage("Invalid ratio for --generate-test-profile-method-ratio"); |
| 614 | } |
| 615 | if (test_profile_class_ratio_ > 100) { |
| 616 | Usage("Invalid ratio for --generate-test-profile-class-ratio"); |
| 617 | } |
| 618 | // ShouldGenerateTestProfile confirms !test_profile_.empty(). |
George Burgess IV | 71f7726 | 2016-10-25 13:08:17 -0700 | [diff] [blame] | 619 | int profile_test_fd = open(test_profile_.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644); |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 620 | if (profile_test_fd < 0) { |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 621 | LOG(ERROR) << "Cannot open " << test_profile_ << strerror(errno); |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 622 | return -1; |
| 623 | } |
| 624 | |
| 625 | bool result = ProfileCompilationInfo::GenerateTestProfile(profile_test_fd, |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 626 | test_profile_num_dex_, |
| 627 | test_profile_method_ratio_, |
| 628 | test_profile_class_ratio_); |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 629 | close(profile_test_fd); // ignore close result. |
| 630 | return result ? 0 : -1; |
| 631 | } |
| 632 | |
| 633 | bool ShouldGenerateTestProfile() { |
| 634 | return !test_profile_.empty(); |
| 635 | } |
| 636 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 637 | private: |
| 638 | static void ParseFdForCollection(const StringPiece& option, |
| 639 | const char* arg_name, |
| 640 | std::vector<int>* fds) { |
| 641 | int fd; |
| 642 | ParseUintOption(option, arg_name, &fd, Usage); |
| 643 | fds->push_back(fd); |
| 644 | } |
| 645 | |
| 646 | static void CloseAllFds(const std::vector<int>& fds, const char* descriptor) { |
| 647 | for (size_t i = 0; i < fds.size(); i++) { |
| 648 | if (close(fds[i]) < 0) { |
| 649 | PLOG(WARNING) << "Failed to close descriptor for " << descriptor << " at index " << i; |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | void LogCompletionTime() { |
David Sehr | 52683cf | 2016-05-05 09:02:38 -0700 | [diff] [blame] | 655 | static constexpr uint64_t kLogThresholdTime = MsToNs(100); // 100ms |
| 656 | uint64_t time_taken = NanoTime() - start_ns_; |
David Sehr | ed79301 | 2016-05-05 13:39:43 -0700 | [diff] [blame] | 657 | if (time_taken > kLogThresholdTime) { |
David Sehr | d855718 | 2016-05-06 12:29:35 -0700 | [diff] [blame] | 658 | LOG(WARNING) << "profman took " << PrettyDuration(time_taken); |
David Sehr | ed79301 | 2016-05-05 13:39:43 -0700 | [diff] [blame] | 659 | } |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | std::vector<std::string> profile_files_; |
| 663 | std::vector<int> profile_files_fd_; |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 664 | std::vector<std::string> dex_locations_; |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 665 | std::vector<std::string> apk_files_; |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 666 | std::vector<int> apks_fd_; |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 667 | std::string reference_profile_file_; |
| 668 | int reference_profile_file_fd_; |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 669 | bool dump_only_; |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 670 | bool dump_classes_; |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 671 | int dump_output_to_fd_; |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 672 | std::string test_profile_; |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 673 | std::string create_profile_from_file_; |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 674 | uint16_t test_profile_num_dex_; |
| 675 | uint16_t test_profile_method_ratio_; |
| 676 | uint16_t test_profile_class_ratio_; |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 677 | uint64_t start_ns_; |
| 678 | }; |
| 679 | |
| 680 | // See ProfileAssistant::ProcessingResult for return codes. |
| 681 | static int profman(int argc, char** argv) { |
| 682 | ProfMan profman; |
| 683 | |
| 684 | // Parse arguments. Argument mistakes will lead to exit(EXIT_FAILURE) in UsageError. |
| 685 | profman.ParseArgs(argc, argv); |
| 686 | |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 687 | if (profman.ShouldGenerateTestProfile()) { |
| 688 | return profman.GenerateTestProfile(); |
| 689 | } |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 690 | if (profman.ShouldOnlyDumpProfile()) { |
| 691 | return profman.DumpProfileInfo(); |
| 692 | } |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 693 | if (profman.ShouldOnlyDumpClasses()) { |
| 694 | return profman.DumpClasses(); |
| 695 | } |
| 696 | if (profman.ShouldCreateProfile()) { |
| 697 | return profman.CreateProfile(); |
| 698 | } |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 699 | // Process profile information and assess if we need to do a profile guided compilation. |
| 700 | // This operation involves I/O. |
| 701 | return profman.ProcessProfiles(); |
| 702 | } |
| 703 | |
| 704 | } // namespace art |
| 705 | |
| 706 | int main(int argc, char **argv) { |
| 707 | return art::profman(argc, argv); |
| 708 | } |
| 709 | |