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 | |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 219 | bool has_profiles = !profile_files_.empty() || !profile_files_fd_.empty(); |
| 220 | bool has_reference_profile = !reference_profile_file_.empty() || |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 221 | FdIsValid(reference_profile_file_fd_); |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 222 | |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 223 | if (!test_profile_.empty()) { |
| 224 | if (test_profile_method_ratio_ > 100) { |
| 225 | Usage("Invalid ratio for --generate-test-profile-method-ratio"); |
| 226 | } |
| 227 | if (test_profile_class_ratio_ > 100) { |
| 228 | Usage("Invalid ratio for --generate-test-profile-class-ratio"); |
| 229 | } |
| 230 | return; |
| 231 | } |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame^] | 232 | if (!apk_files_.empty() && !apks_fd_.empty()) { |
| 233 | Usage("APK files should not be specified with both --apk-fd and --apk"); |
| 234 | } |
| 235 | if (!create_profile_from_file_.empty()) { |
| 236 | if (apk_files_.empty() && apks_fd_.empty()) { |
| 237 | Usage("APK files must be specified"); |
| 238 | } |
| 239 | if (dex_locations_.empty()) { |
| 240 | Usage("DEX locations must be specified"); |
| 241 | } |
| 242 | if (reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) { |
| 243 | Usage("Reference profile must be specified with --reference-profile-file or " |
| 244 | "--reference-profile-file-fd"); |
| 245 | } |
| 246 | if (has_profiles) { |
| 247 | Usage("Profile must be specified with --reference-profile-file or " |
| 248 | "--reference-profile-file-fd"); |
| 249 | } |
| 250 | return; |
| 251 | } |
| 252 | // --dump-only and --dump-classes may be specified with only --reference-profiles present. |
| 253 | if (!dump_only_ && !dump_classes_ && !has_profiles) { |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 254 | Usage("No profile files specified."); |
| 255 | } |
| 256 | if (!profile_files_.empty() && !profile_files_fd_.empty()) { |
| 257 | Usage("Profile files should not be specified with both --profile-file-fd and --profile-file"); |
| 258 | } |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame^] | 259 | if (!dump_only_ && !dump_classes_ && !has_reference_profile) { |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 260 | Usage("No reference profile file specified."); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 261 | } |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 262 | if (!reference_profile_file_.empty() && FdIsValid(reference_profile_file_fd_)) { |
| 263 | Usage("Reference profile should not be specified with both " |
| 264 | "--reference-profile-file-fd and --reference-profile-file"); |
| 265 | } |
| 266 | if ((!profile_files_.empty() && FdIsValid(reference_profile_file_fd_)) || |
| 267 | (!dump_only_ && !profile_files_fd_.empty() && !FdIsValid(reference_profile_file_fd_))) { |
| 268 | Usage("Options --profile-file-fd and --reference-profile-file-fd " |
| 269 | "should only be used together"); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
| 273 | ProfileAssistant::ProcessingResult ProcessProfiles() { |
| 274 | ProfileAssistant::ProcessingResult result; |
| 275 | if (profile_files_.empty()) { |
| 276 | // The file doesn't need to be flushed here (ProcessProfiles will do it) |
| 277 | // so don't check the usage. |
| 278 | File file(reference_profile_file_fd_, false); |
| 279 | result = ProfileAssistant::ProcessProfiles(profile_files_fd_, reference_profile_file_fd_); |
| 280 | CloseAllFds(profile_files_fd_, "profile_files_fd_"); |
| 281 | } else { |
| 282 | result = ProfileAssistant::ProcessProfiles(profile_files_, reference_profile_file_); |
| 283 | } |
| 284 | return result; |
| 285 | } |
| 286 | |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame^] | 287 | void OpenApkFilesFromLocations(std::vector<std::unique_ptr<const DexFile>>* dex_files) { |
| 288 | bool use_apk_fd_list = !apks_fd_.empty(); |
| 289 | if (use_apk_fd_list) { |
| 290 | CHECK(apk_files_.empty()); |
| 291 | CHECK_EQ(dex_locations_.size(), apks_fd_.size()); |
| 292 | } else { |
| 293 | CHECK_EQ(dex_locations_.size(), apk_files_.size()); |
| 294 | CHECK(!apk_files_.empty()); |
| 295 | } |
| 296 | static constexpr bool kVerifyChecksum = true; |
| 297 | for (size_t i = 0; i < dex_locations_.size(); ++i) { |
| 298 | std::string error_msg; |
| 299 | std::vector<std::unique_ptr<const DexFile>> dex_files_for_location; |
| 300 | if (use_apk_fd_list) { |
| 301 | if (DexFile::OpenZip(apks_fd_[i], |
| 302 | dex_locations_[i], |
| 303 | kVerifyChecksum, |
| 304 | &error_msg, |
| 305 | &dex_files_for_location)) { |
| 306 | } else { |
| 307 | LOG(WARNING) << "OpenZip failed for '" << dex_locations_[i] << "' " << error_msg; |
| 308 | continue; |
| 309 | } |
| 310 | } else { |
| 311 | if (DexFile::Open(apk_files_[i].c_str(), |
| 312 | dex_locations_[i], |
| 313 | kVerifyChecksum, |
| 314 | &error_msg, |
| 315 | &dex_files_for_location)) { |
| 316 | } else { |
| 317 | LOG(WARNING) << "Open failed for '" << dex_locations_[i] << "' " << error_msg; |
| 318 | continue; |
| 319 | } |
| 320 | } |
| 321 | for (std::unique_ptr<const DexFile>& dex_file : dex_files_for_location) { |
| 322 | dex_files->emplace_back(std::move(dex_file)); |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
David Sehr | b18991b | 2017-02-08 20:58:10 -0800 | [diff] [blame] | 327 | int DumpOneProfile(const std::string& banner, |
| 328 | const std::string& filename, |
| 329 | int fd, |
| 330 | const std::vector<std::unique_ptr<const DexFile>>* dex_files, |
| 331 | std::string* dump) { |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 332 | if (!filename.empty()) { |
| 333 | fd = open(filename.c_str(), O_RDWR); |
| 334 | if (fd < 0) { |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame^] | 335 | LOG(ERROR) << "Cannot open " << filename << strerror(errno); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 336 | return -1; |
| 337 | } |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 338 | } |
| 339 | ProfileCompilationInfo info; |
| 340 | if (!info.Load(fd)) { |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame^] | 341 | LOG(ERROR) << "Cannot load profile info from fd=" << fd << "\n"; |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 342 | return -1; |
| 343 | } |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 344 | std::string this_dump = banner + "\n" + info.DumpInfo(dex_files) + "\n"; |
| 345 | *dump += this_dump; |
| 346 | if (close(fd) < 0) { |
| 347 | PLOG(WARNING) << "Failed to close descriptor"; |
| 348 | } |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | int DumpProfileInfo() { |
| 353 | static const char* kEmptyString = ""; |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 354 | static const char* kOrdinaryProfile = "=== profile ==="; |
| 355 | static const char* kReferenceProfile = "=== reference profile ==="; |
| 356 | |
| 357 | // Open apk/zip files and and read dex files. |
| 358 | MemMap::Init(); // for ZipArchive::OpenFromFd |
David Sehr | b18991b | 2017-02-08 20:58:10 -0800 | [diff] [blame] | 359 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame^] | 360 | OpenApkFilesFromLocations(&dex_files); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 361 | std::string dump; |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 362 | // Dump individual profile files. |
| 363 | if (!profile_files_fd_.empty()) { |
| 364 | for (int profile_file_fd : profile_files_fd_) { |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 365 | int ret = DumpOneProfile(kOrdinaryProfile, |
| 366 | kEmptyString, |
| 367 | 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 | } |
| 375 | if (!profile_files_.empty()) { |
| 376 | for (const std::string& profile_file : profile_files_) { |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 377 | int ret = DumpOneProfile(kOrdinaryProfile, profile_file, kInvalidFd, &dex_files, &dump); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 378 | if (ret != 0) { |
| 379 | return ret; |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | // Dump reference profile file. |
| 384 | if (FdIsValid(reference_profile_file_fd_)) { |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 385 | int ret = DumpOneProfile(kReferenceProfile, |
| 386 | kEmptyString, |
| 387 | reference_profile_file_fd_, |
| 388 | &dex_files, |
| 389 | &dump); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 390 | if (ret != 0) { |
| 391 | return ret; |
| 392 | } |
| 393 | } |
| 394 | if (!reference_profile_file_.empty()) { |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 395 | int ret = DumpOneProfile(kReferenceProfile, |
| 396 | reference_profile_file_, |
| 397 | kInvalidFd, |
| 398 | &dex_files, |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 399 | &dump); |
| 400 | if (ret != 0) { |
| 401 | return ret; |
| 402 | } |
| 403 | } |
| 404 | if (!FdIsValid(dump_output_to_fd_)) { |
| 405 | std::cout << dump; |
| 406 | } else { |
| 407 | unix_file::FdFile out_fd(dump_output_to_fd_, false /*check_usage*/); |
| 408 | if (!out_fd.WriteFully(dump.c_str(), dump.length())) { |
| 409 | return -1; |
| 410 | } |
| 411 | } |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | bool ShouldOnlyDumpProfile() { |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 416 | return dump_only_; |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 417 | } |
| 418 | |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame^] | 419 | bool GetClassNames(int fd, |
| 420 | std::vector<std::unique_ptr<const DexFile>>* dex_files, |
| 421 | std::set<std::string>* class_names) { |
| 422 | ProfileCompilationInfo profile_info; |
| 423 | if (!profile_info.Load(fd)) { |
| 424 | LOG(ERROR) << "Cannot load profile info"; |
| 425 | return false; |
| 426 | } |
| 427 | profile_info.GetClassNames(dex_files, class_names); |
| 428 | return true; |
| 429 | } |
| 430 | |
| 431 | bool GetClassNames(std::string profile_file, |
| 432 | std::vector<std::unique_ptr<const DexFile>>* dex_files, |
| 433 | std::set<std::string>* class_names) { |
| 434 | int fd = open(profile_file.c_str(), O_RDONLY); |
| 435 | if (!FdIsValid(fd)) { |
| 436 | LOG(ERROR) << "Cannot open " << profile_file << strerror(errno); |
| 437 | return false; |
| 438 | } |
| 439 | if (!GetClassNames(fd, dex_files, class_names)) { |
| 440 | return false; |
| 441 | } |
| 442 | if (close(fd) < 0) { |
| 443 | PLOG(WARNING) << "Failed to close descriptor"; |
| 444 | } |
| 445 | return true; |
| 446 | } |
| 447 | |
| 448 | int DumpClasses() { |
| 449 | // Open apk/zip files and and read dex files. |
| 450 | MemMap::Init(); // for ZipArchive::OpenFromFd |
| 451 | // Open the dex files to get the names for classes. |
| 452 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 453 | OpenApkFilesFromLocations(&dex_files); |
| 454 | // Build a vector of class names from individual profile files. |
| 455 | std::set<std::string> class_names; |
| 456 | if (!profile_files_fd_.empty()) { |
| 457 | for (int profile_file_fd : profile_files_fd_) { |
| 458 | if (!GetClassNames(profile_file_fd, &dex_files, &class_names)) { |
| 459 | return -1; |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | if (!profile_files_.empty()) { |
| 464 | for (const std::string& profile_file : profile_files_) { |
| 465 | if (!GetClassNames(profile_file, &dex_files, &class_names)) { |
| 466 | return -1; |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | // Concatenate class names from reference profile file. |
| 471 | if (FdIsValid(reference_profile_file_fd_)) { |
| 472 | if (!GetClassNames(reference_profile_file_fd_, &dex_files, &class_names)) { |
| 473 | return -1; |
| 474 | } |
| 475 | } |
| 476 | if (!reference_profile_file_.empty()) { |
| 477 | if (!GetClassNames(reference_profile_file_, &dex_files, &class_names)) { |
| 478 | return -1; |
| 479 | } |
| 480 | } |
| 481 | // Dump the class names. |
| 482 | std::string dump; |
| 483 | for (const std::string& class_name : class_names) { |
| 484 | dump += class_name + std::string("\n"); |
| 485 | } |
| 486 | if (!FdIsValid(dump_output_to_fd_)) { |
| 487 | std::cout << dump; |
| 488 | } else { |
| 489 | unix_file::FdFile out_fd(dump_output_to_fd_, false /*check_usage*/); |
| 490 | if (!out_fd.WriteFully(dump.c_str(), dump.length())) { |
| 491 | return -1; |
| 492 | } |
| 493 | } |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | bool ShouldOnlyDumpClasses() { |
| 498 | return dump_classes_; |
| 499 | } |
| 500 | |
| 501 | // Read lines from the given file, dropping comments and empty lines. Post-process each line with |
| 502 | // the given function. |
| 503 | template <typename T> |
| 504 | static T* ReadCommentedInputFromFile( |
| 505 | const char* input_filename, std::function<std::string(const char*)>* process) { |
| 506 | std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in)); |
| 507 | if (input_file.get() == nullptr) { |
| 508 | LOG(ERROR) << "Failed to open input file " << input_filename; |
| 509 | return nullptr; |
| 510 | } |
| 511 | std::unique_ptr<T> result( |
| 512 | ReadCommentedInputStream<T>(*input_file, process)); |
| 513 | input_file->close(); |
| 514 | return result.release(); |
| 515 | } |
| 516 | |
| 517 | // Read lines from the given stream, dropping comments and empty lines. Post-process each line |
| 518 | // with the given function. |
| 519 | template <typename T> |
| 520 | static T* ReadCommentedInputStream( |
| 521 | std::istream& in_stream, |
| 522 | std::function<std::string(const char*)>* process) { |
| 523 | std::unique_ptr<T> output(new T()); |
| 524 | while (in_stream.good()) { |
| 525 | std::string dot; |
| 526 | std::getline(in_stream, dot); |
| 527 | if (android::base::StartsWith(dot, "#") || dot.empty()) { |
| 528 | continue; |
| 529 | } |
| 530 | if (process != nullptr) { |
| 531 | std::string descriptor((*process)(dot.c_str())); |
| 532 | output->insert(output->end(), descriptor); |
| 533 | } else { |
| 534 | output->insert(output->end(), dot); |
| 535 | } |
| 536 | } |
| 537 | return output.release(); |
| 538 | } |
| 539 | |
| 540 | int CreateProfile() { |
| 541 | MemMap::Init(); // for ZipArchive::OpenFromFd |
| 542 | // Open the profile output file if needed. |
| 543 | int fd = reference_profile_file_fd_; |
| 544 | if (!FdIsValid(fd)) { |
| 545 | CHECK(!reference_profile_file_.empty()); |
| 546 | fd = open(reference_profile_file_.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644); |
| 547 | if (fd < 0) { |
| 548 | LOG(ERROR) << "Cannot open " << reference_profile_file_ << strerror(errno); |
| 549 | return -1; |
| 550 | } |
| 551 | } |
| 552 | // Read the user-specified list of classes (dot notation rather than descriptors). |
| 553 | std::unique_ptr<std::unordered_set<std::string>> |
| 554 | user_class_list(ReadCommentedInputFromFile<std::unordered_set<std::string>>( |
| 555 | create_profile_from_file_.c_str(), nullptr)); // No post-processing. |
| 556 | std::unordered_set<std::string> matched_user_classes; |
| 557 | // Open the dex files to look up class names. |
| 558 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 559 | OpenApkFilesFromLocations(&dex_files); |
| 560 | // Iterate over the dex files looking for class names in the input stream. |
| 561 | std::set<DexCacheResolvedClasses> resolved_class_set; |
| 562 | for (auto& dex_file : dex_files) { |
| 563 | // Compute the set of classes to be added for this dex file first. This |
| 564 | // avoids creating an entry in the profile information for dex files that |
| 565 | // contribute no classes. |
| 566 | std::unordered_set<dex::TypeIndex> classes_to_be_added; |
| 567 | for (const auto& klass : *user_class_list) { |
| 568 | std::string descriptor = DotToDescriptor(klass.c_str()); |
| 569 | const DexFile::TypeId* type_id = dex_file->FindTypeId(descriptor.c_str()); |
| 570 | if (type_id == nullptr) { |
| 571 | continue; |
| 572 | } |
| 573 | classes_to_be_added.insert(dex_file->GetIndexForTypeId(*type_id)); |
| 574 | matched_user_classes.insert(klass); |
| 575 | } |
| 576 | if (classes_to_be_added.empty()) { |
| 577 | continue; |
| 578 | } |
| 579 | // Insert the DexCacheResolved Classes into the set expected for |
| 580 | // AddMethodsAndClasses. |
| 581 | std::set<DexCacheResolvedClasses>::iterator dex_resolved_classes = |
| 582 | resolved_class_set.emplace(dex_file->GetLocation(), |
| 583 | dex_file->GetBaseLocation(), |
| 584 | dex_file->GetLocationChecksum()).first; |
| 585 | dex_resolved_classes->AddClasses(classes_to_be_added.begin(), classes_to_be_added.end()); |
| 586 | } |
| 587 | // Warn the user if we didn't find matches for every class. |
| 588 | for (const auto& klass : *user_class_list) { |
| 589 | if (matched_user_classes.find(klass) == matched_user_classes.end()) { |
| 590 | LOG(WARNING) << "requested class '" << klass << "' was not matched in any dex file"; |
| 591 | } |
| 592 | } |
| 593 | // Generate the profile data structure. |
| 594 | ProfileCompilationInfo info; |
| 595 | std::vector<MethodReference> methods; // No methods for now. |
| 596 | info.AddMethodsAndClasses(methods, resolved_class_set); |
| 597 | // Write the profile file. |
| 598 | CHECK(info.Save(fd)); |
| 599 | if (close(fd) < 0) { |
| 600 | PLOG(WARNING) << "Failed to close descriptor"; |
| 601 | } |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | bool ShouldCreateProfile() { |
| 606 | return !create_profile_from_file_.empty(); |
| 607 | } |
| 608 | |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 609 | int GenerateTestProfile() { |
George Burgess IV | 71f7726 | 2016-10-25 13:08:17 -0700 | [diff] [blame] | 610 | 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] | 611 | if (profile_test_fd < 0) { |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame^] | 612 | LOG(ERROR) << "Cannot open " << test_profile_ << strerror(errno); |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 613 | return -1; |
| 614 | } |
| 615 | |
| 616 | bool result = ProfileCompilationInfo::GenerateTestProfile(profile_test_fd, |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame^] | 617 | test_profile_num_dex_, |
| 618 | test_profile_method_ratio_, |
| 619 | test_profile_class_ratio_); |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 620 | close(profile_test_fd); // ignore close result. |
| 621 | return result ? 0 : -1; |
| 622 | } |
| 623 | |
| 624 | bool ShouldGenerateTestProfile() { |
| 625 | return !test_profile_.empty(); |
| 626 | } |
| 627 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 628 | private: |
| 629 | static void ParseFdForCollection(const StringPiece& option, |
| 630 | const char* arg_name, |
| 631 | std::vector<int>* fds) { |
| 632 | int fd; |
| 633 | ParseUintOption(option, arg_name, &fd, Usage); |
| 634 | fds->push_back(fd); |
| 635 | } |
| 636 | |
| 637 | static void CloseAllFds(const std::vector<int>& fds, const char* descriptor) { |
| 638 | for (size_t i = 0; i < fds.size(); i++) { |
| 639 | if (close(fds[i]) < 0) { |
| 640 | PLOG(WARNING) << "Failed to close descriptor for " << descriptor << " at index " << i; |
| 641 | } |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | void LogCompletionTime() { |
David Sehr | 52683cf | 2016-05-05 09:02:38 -0700 | [diff] [blame] | 646 | static constexpr uint64_t kLogThresholdTime = MsToNs(100); // 100ms |
| 647 | uint64_t time_taken = NanoTime() - start_ns_; |
David Sehr | ed79301 | 2016-05-05 13:39:43 -0700 | [diff] [blame] | 648 | if (time_taken > kLogThresholdTime) { |
David Sehr | d855718 | 2016-05-06 12:29:35 -0700 | [diff] [blame] | 649 | LOG(WARNING) << "profman took " << PrettyDuration(time_taken); |
David Sehr | ed79301 | 2016-05-05 13:39:43 -0700 | [diff] [blame] | 650 | } |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | std::vector<std::string> profile_files_; |
| 654 | std::vector<int> profile_files_fd_; |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 655 | std::vector<std::string> dex_locations_; |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame^] | 656 | std::vector<std::string> apk_files_; |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 657 | std::vector<int> apks_fd_; |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 658 | std::string reference_profile_file_; |
| 659 | int reference_profile_file_fd_; |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 660 | bool dump_only_; |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame^] | 661 | bool dump_classes_; |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 662 | int dump_output_to_fd_; |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 663 | std::string test_profile_; |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame^] | 664 | std::string create_profile_from_file_; |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 665 | uint16_t test_profile_num_dex_; |
| 666 | uint16_t test_profile_method_ratio_; |
| 667 | uint16_t test_profile_class_ratio_; |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 668 | uint64_t start_ns_; |
| 669 | }; |
| 670 | |
| 671 | // See ProfileAssistant::ProcessingResult for return codes. |
| 672 | static int profman(int argc, char** argv) { |
| 673 | ProfMan profman; |
| 674 | |
| 675 | // Parse arguments. Argument mistakes will lead to exit(EXIT_FAILURE) in UsageError. |
| 676 | profman.ParseArgs(argc, argv); |
| 677 | |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 678 | if (profman.ShouldGenerateTestProfile()) { |
| 679 | return profman.GenerateTestProfile(); |
| 680 | } |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 681 | if (profman.ShouldOnlyDumpProfile()) { |
| 682 | return profman.DumpProfileInfo(); |
| 683 | } |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame^] | 684 | if (profman.ShouldOnlyDumpClasses()) { |
| 685 | return profman.DumpClasses(); |
| 686 | } |
| 687 | if (profman.ShouldCreateProfile()) { |
| 688 | return profman.CreateProfile(); |
| 689 | } |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 690 | // Process profile information and assess if we need to do a profile guided compilation. |
| 691 | // This operation involves I/O. |
| 692 | return profman.ProcessProfiles(); |
| 693 | } |
| 694 | |
| 695 | } // namespace art |
| 696 | |
| 697 | int main(int argc, char **argv) { |
| 698 | return art::profman(argc, argv); |
| 699 | } |
| 700 | |