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" |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 39 | #include "bytecode_utils.h" |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 40 | #include "dex_file.h" |
Calin Juravle | 33083d6 | 2017-01-18 15:29:12 -0800 | [diff] [blame] | 41 | #include "jit/profile_compilation_info.h" |
David Sehr | f57589f | 2016-10-17 10:09:33 -0700 | [diff] [blame] | 42 | #include "runtime.h" |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 43 | #include "utils.h" |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 44 | #include "zip_archive.h" |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 45 | #include "profile_assistant.h" |
| 46 | |
| 47 | namespace art { |
| 48 | |
| 49 | static int original_argc; |
| 50 | static char** original_argv; |
| 51 | |
| 52 | static std::string CommandLine() { |
| 53 | std::vector<std::string> command; |
| 54 | for (int i = 0; i < original_argc; ++i) { |
| 55 | command.push_back(original_argv[i]); |
| 56 | } |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 57 | return android::base::Join(command, ' '); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 58 | } |
| 59 | |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 60 | static constexpr int kInvalidFd = -1; |
| 61 | |
| 62 | static bool FdIsValid(int fd) { |
| 63 | return fd != kInvalidFd; |
| 64 | } |
| 65 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 66 | static void UsageErrorV(const char* fmt, va_list ap) { |
| 67 | std::string error; |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 68 | android::base::StringAppendV(&error, fmt, ap); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 69 | LOG(ERROR) << error; |
| 70 | } |
| 71 | |
| 72 | static void UsageError(const char* fmt, ...) { |
| 73 | va_list ap; |
| 74 | va_start(ap, fmt); |
| 75 | UsageErrorV(fmt, ap); |
| 76 | va_end(ap); |
| 77 | } |
| 78 | |
| 79 | NO_RETURN static void Usage(const char *fmt, ...) { |
| 80 | va_list ap; |
| 81 | va_start(ap, fmt); |
| 82 | UsageErrorV(fmt, ap); |
| 83 | va_end(ap); |
| 84 | |
| 85 | UsageError("Command: %s", CommandLine().c_str()); |
| 86 | UsageError("Usage: profman [options]..."); |
| 87 | UsageError(""); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 88 | UsageError(" --dump-only: dumps the content of the specified profile files"); |
| 89 | UsageError(" to standard output (default) in a human readable form."); |
| 90 | UsageError(""); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 91 | UsageError(" --dump-output-to-fd=<number>: redirects --dump-only output to a file descriptor."); |
| 92 | UsageError(""); |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame^] | 93 | UsageError(" --dump-classes-and-methods: dumps a sorted list of classes and methods that are"); |
| 94 | UsageError(" in the specified profile file to standard output (default) in a human"); |
| 95 | UsageError(" readable form. The output is valid input for --create-profile-from"); |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 96 | UsageError(""); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 97 | UsageError(" --profile-file=<filename>: specify profiler output file to use for compilation."); |
| 98 | UsageError(" Can be specified multiple time, in which case the data from the different"); |
| 99 | UsageError(" profiles will be aggregated."); |
| 100 | UsageError(""); |
| 101 | UsageError(" --profile-file-fd=<number>: same as --profile-file but accepts a file descriptor."); |
| 102 | UsageError(" Cannot be used together with --profile-file."); |
| 103 | UsageError(""); |
| 104 | UsageError(" --reference-profile-file=<filename>: specify a reference profile."); |
| 105 | UsageError(" The data in this file will be compared with the data obtained by merging"); |
| 106 | UsageError(" all the files specified with --profile-file or --profile-file-fd."); |
| 107 | UsageError(" If the exit code is EXIT_COMPILE then all --profile-file will be merged into"); |
| 108 | UsageError(" --reference-profile-file. "); |
| 109 | UsageError(""); |
| 110 | UsageError(" --reference-profile-file-fd=<number>: same as --reference-profile-file but"); |
| 111 | UsageError(" accepts a file descriptor. Cannot be used together with"); |
| 112 | UsageError(" --reference-profile-file."); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 113 | UsageError(""); |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 114 | UsageError(" --generate-test-profile=<filename>: generates a random profile file for testing."); |
| 115 | UsageError(" --generate-test-profile-num-dex=<number>: number of dex files that should be"); |
| 116 | UsageError(" included in the generated profile. Defaults to 20."); |
| 117 | UsageError(" --generate-test-profile-method-ratio=<number>: the percentage from the maximum"); |
| 118 | UsageError(" number of methods that should be generated. Defaults to 5."); |
| 119 | UsageError(" --generate-test-profile-class-ratio=<number>: the percentage from the maximum"); |
| 120 | UsageError(" number of classes that should be generated. Defaults to 5."); |
Jeff Hao | f0a31f8 | 2017-03-27 15:50:37 -0700 | [diff] [blame] | 121 | UsageError(" --generate-test-profile-seed=<number>: seed for random number generator used when"); |
| 122 | UsageError(" generating random test profiles. Defaults to using NanoTime."); |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 123 | UsageError(""); |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame^] | 124 | UsageError(" --create-profile-from=<filename>: creates a profile from a list of classes and"); |
| 125 | UsageError(" methods."); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 126 | UsageError(""); |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 127 | UsageError(" --dex-location=<string>: location string to use with corresponding"); |
| 128 | UsageError(" apk-fd to find dex files"); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 129 | UsageError(""); |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 130 | UsageError(" --apk-fd=<number>: file descriptor containing an open APK to"); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 131 | UsageError(" search for dex files"); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 132 | UsageError(" --apk-=<filename>: an APK to search for dex files"); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 133 | UsageError(""); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 134 | |
| 135 | exit(EXIT_FAILURE); |
| 136 | } |
| 137 | |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 138 | // Note: make sure you update the Usage if you change these values. |
| 139 | static constexpr uint16_t kDefaultTestProfileNumDex = 20; |
| 140 | static constexpr uint16_t kDefaultTestProfileMethodRatio = 5; |
| 141 | static constexpr uint16_t kDefaultTestProfileClassRatio = 5; |
| 142 | |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 143 | // Separators used when parsing human friendly representation of profiles. |
| 144 | static const std::string kMethodSep = "->"; |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 145 | static const std::string kMissingTypesMarker = "missing_types"; |
Mathieu Chartier | d808e8b | 2017-03-21 13:37:41 -0700 | [diff] [blame] | 146 | static const std::string kClassAllMethods = "*"; |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 147 | static constexpr char kProfileParsingInlineChacheSep = '+'; |
| 148 | static constexpr char kProfileParsingTypeSep = ','; |
| 149 | static constexpr char kProfileParsingFirstCharInSignature = '('; |
| 150 | |
| 151 | // TODO(calin): This class has grown too much from its initial design. Split the functionality |
| 152 | // into smaller, more contained pieces. |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 153 | class ProfMan FINAL { |
| 154 | public: |
| 155 | ProfMan() : |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 156 | reference_profile_file_fd_(kInvalidFd), |
| 157 | dump_only_(false), |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame^] | 158 | dump_classes_and_methods_(false), |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 159 | dump_output_to_fd_(kInvalidFd), |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 160 | test_profile_num_dex_(kDefaultTestProfileNumDex), |
| 161 | test_profile_method_ratio_(kDefaultTestProfileMethodRatio), |
| 162 | test_profile_class_ratio_(kDefaultTestProfileClassRatio), |
Jeff Hao | f0a31f8 | 2017-03-27 15:50:37 -0700 | [diff] [blame] | 163 | test_profile_seed_(NanoTime()), |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 164 | start_ns_(NanoTime()) {} |
| 165 | |
| 166 | ~ProfMan() { |
| 167 | LogCompletionTime(); |
| 168 | } |
| 169 | |
| 170 | void ParseArgs(int argc, char **argv) { |
| 171 | original_argc = argc; |
| 172 | original_argv = argv; |
| 173 | |
David Sehr | f57589f | 2016-10-17 10:09:33 -0700 | [diff] [blame] | 174 | InitLogging(argv, Runtime::Aborter); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 175 | |
| 176 | // Skip over the command name. |
| 177 | argv++; |
| 178 | argc--; |
| 179 | |
| 180 | if (argc == 0) { |
| 181 | Usage("No arguments specified"); |
| 182 | } |
| 183 | |
| 184 | for (int i = 0; i < argc; ++i) { |
| 185 | const StringPiece option(argv[i]); |
| 186 | const bool log_options = false; |
| 187 | if (log_options) { |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 188 | LOG(INFO) << "profman: option[" << i << "]=" << argv[i]; |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 189 | } |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 190 | if (option == "--dump-only") { |
| 191 | dump_only_ = true; |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame^] | 192 | } else if (option == "--dump-classes-and-methods") { |
| 193 | dump_classes_and_methods_ = true; |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 194 | } else if (option.starts_with("--create-profile-from=")) { |
| 195 | create_profile_from_file_ = option.substr(strlen("--create-profile-from=")).ToString(); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 196 | } else if (option.starts_with("--dump-output-to-fd=")) { |
| 197 | ParseUintOption(option, "--dump-output-to-fd", &dump_output_to_fd_, Usage); |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 198 | } else if (option.starts_with("--profile-file=")) { |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 199 | profile_files_.push_back(option.substr(strlen("--profile-file=")).ToString()); |
| 200 | } else if (option.starts_with("--profile-file-fd=")) { |
| 201 | ParseFdForCollection(option, "--profile-file-fd", &profile_files_fd_); |
| 202 | } else if (option.starts_with("--reference-profile-file=")) { |
| 203 | reference_profile_file_ = option.substr(strlen("--reference-profile-file=")).ToString(); |
| 204 | } else if (option.starts_with("--reference-profile-file-fd=")) { |
| 205 | ParseUintOption(option, "--reference-profile-file-fd", &reference_profile_file_fd_, Usage); |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 206 | } else if (option.starts_with("--dex-location=")) { |
| 207 | dex_locations_.push_back(option.substr(strlen("--dex-location=")).ToString()); |
| 208 | } else if (option.starts_with("--apk-fd=")) { |
| 209 | ParseFdForCollection(option, "--apk-fd", &apks_fd_); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 210 | } else if (option.starts_with("--apk=")) { |
| 211 | apk_files_.push_back(option.substr(strlen("--apk=")).ToString()); |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 212 | } else if (option.starts_with("--generate-test-profile=")) { |
| 213 | test_profile_ = option.substr(strlen("--generate-test-profile=")).ToString(); |
| 214 | } else if (option.starts_with("--generate-test-profile-num-dex=")) { |
| 215 | ParseUintOption(option, |
| 216 | "--generate-test-profile-num-dex", |
| 217 | &test_profile_num_dex_, |
| 218 | Usage); |
| 219 | } else if (option.starts_with("--generate-test-profile-method-ratio")) { |
| 220 | ParseUintOption(option, |
| 221 | "--generate-test-profile-method-ratio", |
| 222 | &test_profile_method_ratio_, |
| 223 | Usage); |
| 224 | } else if (option.starts_with("--generate-test-profile-class-ratio")) { |
| 225 | ParseUintOption(option, |
| 226 | "--generate-test-profile-class-ratio", |
| 227 | &test_profile_class_ratio_, |
| 228 | Usage); |
Jeff Hao | f0a31f8 | 2017-03-27 15:50:37 -0700 | [diff] [blame] | 229 | } else if (option.starts_with("--generate-test-profile-seed=")) { |
| 230 | ParseUintOption(option, "--generate-test-profile-seed", &test_profile_seed_, Usage); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 231 | } else { |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 232 | Usage("Unknown argument '%s'", option.data()); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
David Sehr | 153da0e | 2017-02-15 08:54:51 -0800 | [diff] [blame] | 236 | // Validate global consistency between file/fd options. |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 237 | if (!profile_files_.empty() && !profile_files_fd_.empty()) { |
| 238 | Usage("Profile files should not be specified with both --profile-file-fd and --profile-file"); |
| 239 | } |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 240 | if (!reference_profile_file_.empty() && FdIsValid(reference_profile_file_fd_)) { |
| 241 | Usage("Reference profile should not be specified with both " |
| 242 | "--reference-profile-file-fd and --reference-profile-file"); |
| 243 | } |
David Sehr | 153da0e | 2017-02-15 08:54:51 -0800 | [diff] [blame] | 244 | if (!apk_files_.empty() && !apks_fd_.empty()) { |
| 245 | 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] | 246 | } |
| 247 | } |
| 248 | |
| 249 | ProfileAssistant::ProcessingResult ProcessProfiles() { |
David Sehr | 153da0e | 2017-02-15 08:54:51 -0800 | [diff] [blame] | 250 | // Validate that at least one profile file was passed, as well as a reference profile. |
| 251 | if (profile_files_.empty() && profile_files_fd_.empty()) { |
| 252 | Usage("No profile files specified."); |
| 253 | } |
| 254 | if (reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) { |
| 255 | Usage("No reference profile file specified."); |
| 256 | } |
| 257 | if ((!profile_files_.empty() && FdIsValid(reference_profile_file_fd_)) || |
| 258 | (!profile_files_fd_.empty() && !FdIsValid(reference_profile_file_fd_))) { |
| 259 | Usage("Options --profile-file-fd and --reference-profile-file-fd " |
| 260 | "should only be used together"); |
| 261 | } |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 262 | ProfileAssistant::ProcessingResult result; |
| 263 | if (profile_files_.empty()) { |
| 264 | // The file doesn't need to be flushed here (ProcessProfiles will do it) |
| 265 | // so don't check the usage. |
| 266 | File file(reference_profile_file_fd_, false); |
| 267 | result = ProfileAssistant::ProcessProfiles(profile_files_fd_, reference_profile_file_fd_); |
| 268 | CloseAllFds(profile_files_fd_, "profile_files_fd_"); |
| 269 | } else { |
| 270 | result = ProfileAssistant::ProcessProfiles(profile_files_, reference_profile_file_); |
| 271 | } |
| 272 | return result; |
| 273 | } |
| 274 | |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 275 | void OpenApkFilesFromLocations(std::vector<std::unique_ptr<const DexFile>>* dex_files) { |
| 276 | bool use_apk_fd_list = !apks_fd_.empty(); |
| 277 | if (use_apk_fd_list) { |
David Sehr | 153da0e | 2017-02-15 08:54:51 -0800 | [diff] [blame] | 278 | // Get the APKs from the collection of FDs. |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 279 | CHECK_EQ(dex_locations_.size(), apks_fd_.size()); |
David Sehr | 153da0e | 2017-02-15 08:54:51 -0800 | [diff] [blame] | 280 | } else if (!apk_files_.empty()) { |
| 281 | // Get the APKs from the collection of filenames. |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 282 | CHECK_EQ(dex_locations_.size(), apk_files_.size()); |
David Sehr | 153da0e | 2017-02-15 08:54:51 -0800 | [diff] [blame] | 283 | } else { |
| 284 | // No APKs were specified. |
| 285 | CHECK(dex_locations_.empty()); |
| 286 | return; |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 287 | } |
| 288 | static constexpr bool kVerifyChecksum = true; |
| 289 | for (size_t i = 0; i < dex_locations_.size(); ++i) { |
| 290 | std::string error_msg; |
| 291 | std::vector<std::unique_ptr<const DexFile>> dex_files_for_location; |
| 292 | if (use_apk_fd_list) { |
| 293 | if (DexFile::OpenZip(apks_fd_[i], |
| 294 | dex_locations_[i], |
| 295 | kVerifyChecksum, |
| 296 | &error_msg, |
| 297 | &dex_files_for_location)) { |
| 298 | } else { |
| 299 | LOG(WARNING) << "OpenZip failed for '" << dex_locations_[i] << "' " << error_msg; |
| 300 | continue; |
| 301 | } |
| 302 | } else { |
| 303 | if (DexFile::Open(apk_files_[i].c_str(), |
| 304 | dex_locations_[i], |
| 305 | kVerifyChecksum, |
| 306 | &error_msg, |
| 307 | &dex_files_for_location)) { |
| 308 | } else { |
| 309 | LOG(WARNING) << "Open failed for '" << dex_locations_[i] << "' " << error_msg; |
| 310 | continue; |
| 311 | } |
| 312 | } |
| 313 | for (std::unique_ptr<const DexFile>& dex_file : dex_files_for_location) { |
| 314 | dex_files->emplace_back(std::move(dex_file)); |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
David Sehr | b18991b | 2017-02-08 20:58:10 -0800 | [diff] [blame] | 319 | int DumpOneProfile(const std::string& banner, |
| 320 | const std::string& filename, |
| 321 | int fd, |
| 322 | const std::vector<std::unique_ptr<const DexFile>>* dex_files, |
| 323 | std::string* dump) { |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 324 | if (!filename.empty()) { |
| 325 | fd = open(filename.c_str(), O_RDWR); |
| 326 | if (fd < 0) { |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 327 | LOG(ERROR) << "Cannot open " << filename << strerror(errno); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 328 | return -1; |
| 329 | } |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 330 | } |
| 331 | ProfileCompilationInfo info; |
| 332 | if (!info.Load(fd)) { |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 333 | LOG(ERROR) << "Cannot load profile info from fd=" << fd << "\n"; |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 334 | return -1; |
| 335 | } |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 336 | std::string this_dump = banner + "\n" + info.DumpInfo(dex_files) + "\n"; |
| 337 | *dump += this_dump; |
| 338 | if (close(fd) < 0) { |
| 339 | PLOG(WARNING) << "Failed to close descriptor"; |
| 340 | } |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | int DumpProfileInfo() { |
David Sehr | 153da0e | 2017-02-15 08:54:51 -0800 | [diff] [blame] | 345 | // Validate that at least one profile file or reference was specified. |
| 346 | if (profile_files_.empty() && profile_files_fd_.empty() && |
| 347 | reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) { |
| 348 | Usage("No profile files or reference profile specified."); |
| 349 | } |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 350 | static const char* kEmptyString = ""; |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 351 | static const char* kOrdinaryProfile = "=== profile ==="; |
| 352 | static const char* kReferenceProfile = "=== reference profile ==="; |
| 353 | |
| 354 | // Open apk/zip files and and read dex files. |
| 355 | MemMap::Init(); // for ZipArchive::OpenFromFd |
David Sehr | b18991b | 2017-02-08 20:58:10 -0800 | [diff] [blame] | 356 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 357 | OpenApkFilesFromLocations(&dex_files); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 358 | std::string dump; |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 359 | // Dump individual profile files. |
| 360 | if (!profile_files_fd_.empty()) { |
| 361 | for (int profile_file_fd : profile_files_fd_) { |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 362 | int ret = DumpOneProfile(kOrdinaryProfile, |
| 363 | kEmptyString, |
| 364 | profile_file_fd, |
| 365 | &dex_files, |
| 366 | &dump); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 367 | if (ret != 0) { |
| 368 | return ret; |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | if (!profile_files_.empty()) { |
| 373 | for (const std::string& profile_file : profile_files_) { |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 374 | int ret = DumpOneProfile(kOrdinaryProfile, profile_file, kInvalidFd, &dex_files, &dump); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 375 | if (ret != 0) { |
| 376 | return ret; |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | // Dump reference profile file. |
| 381 | if (FdIsValid(reference_profile_file_fd_)) { |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 382 | int ret = DumpOneProfile(kReferenceProfile, |
| 383 | kEmptyString, |
| 384 | reference_profile_file_fd_, |
| 385 | &dex_files, |
| 386 | &dump); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 387 | if (ret != 0) { |
| 388 | return ret; |
| 389 | } |
| 390 | } |
| 391 | if (!reference_profile_file_.empty()) { |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 392 | int ret = DumpOneProfile(kReferenceProfile, |
| 393 | reference_profile_file_, |
| 394 | kInvalidFd, |
| 395 | &dex_files, |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 396 | &dump); |
| 397 | if (ret != 0) { |
| 398 | return ret; |
| 399 | } |
| 400 | } |
| 401 | if (!FdIsValid(dump_output_to_fd_)) { |
| 402 | std::cout << dump; |
| 403 | } else { |
| 404 | unix_file::FdFile out_fd(dump_output_to_fd_, false /*check_usage*/); |
| 405 | if (!out_fd.WriteFully(dump.c_str(), dump.length())) { |
| 406 | return -1; |
| 407 | } |
| 408 | } |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | bool ShouldOnlyDumpProfile() { |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 413 | return dump_only_; |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame^] | 416 | bool GetClassNamesAndMethods(int fd, |
| 417 | std::vector<std::unique_ptr<const DexFile>>* dex_files, |
| 418 | std::set<std::string>* out_lines) { |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 419 | ProfileCompilationInfo profile_info; |
| 420 | if (!profile_info.Load(fd)) { |
| 421 | LOG(ERROR) << "Cannot load profile info"; |
| 422 | return false; |
| 423 | } |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame^] | 424 | for (const std::unique_ptr<const DexFile>& dex_file : *dex_files) { |
| 425 | std::set<dex::TypeIndex> class_types; |
| 426 | ProfileCompilationInfo::MethodMap methods; |
| 427 | if (profile_info.GetClassesAndMethods(dex_file.get(), &class_types, &methods)) { |
| 428 | for (const dex::TypeIndex& type_index : class_types) { |
| 429 | const DexFile::TypeId& type_id = dex_file->GetTypeId(type_index); |
| 430 | out_lines->insert(std::string(dex_file->GetTypeDescriptor(type_id))); |
| 431 | } |
| 432 | for (const auto& pair : methods) { |
| 433 | // TODO: Process inline caches. |
| 434 | const uint16_t dex_method_idx = pair.first; |
| 435 | const DexFile::MethodId& id = dex_file->GetMethodId(dex_method_idx); |
| 436 | std::string signature_string(dex_file->GetMethodSignature(id).ToString()); |
| 437 | std::string type_string(dex_file->GetTypeDescriptor(dex_file->GetTypeId(id.class_idx_))); |
| 438 | std::string method_name(dex_file->GetMethodName(id)); |
| 439 | out_lines->insert(type_string + kMethodSep + method_name + signature_string); |
| 440 | } |
| 441 | } |
| 442 | } |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 443 | return true; |
| 444 | } |
| 445 | |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame^] | 446 | bool GetClassNamesAndMethods(const std::string& profile_file, |
| 447 | std::vector<std::unique_ptr<const DexFile>>* dex_files, |
| 448 | std::set<std::string>* out_lines) { |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 449 | int fd = open(profile_file.c_str(), O_RDONLY); |
| 450 | if (!FdIsValid(fd)) { |
| 451 | LOG(ERROR) << "Cannot open " << profile_file << strerror(errno); |
| 452 | return false; |
| 453 | } |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame^] | 454 | if (!GetClassNamesAndMethods(fd, dex_files, out_lines)) { |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 455 | return false; |
| 456 | } |
| 457 | if (close(fd) < 0) { |
| 458 | PLOG(WARNING) << "Failed to close descriptor"; |
| 459 | } |
| 460 | return true; |
| 461 | } |
| 462 | |
| 463 | int DumpClasses() { |
David Sehr | 153da0e | 2017-02-15 08:54:51 -0800 | [diff] [blame] | 464 | // Validate that at least one profile file or reference was specified. |
| 465 | if (profile_files_.empty() && profile_files_fd_.empty() && |
| 466 | reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) { |
| 467 | Usage("No profile files or reference profile specified."); |
| 468 | } |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 469 | // Open apk/zip files and and read dex files. |
| 470 | MemMap::Init(); // for ZipArchive::OpenFromFd |
| 471 | // Open the dex files to get the names for classes. |
| 472 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 473 | OpenApkFilesFromLocations(&dex_files); |
| 474 | // Build a vector of class names from individual profile files. |
| 475 | std::set<std::string> class_names; |
| 476 | if (!profile_files_fd_.empty()) { |
| 477 | for (int profile_file_fd : profile_files_fd_) { |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame^] | 478 | if (!GetClassNamesAndMethods(profile_file_fd, &dex_files, &class_names)) { |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 479 | return -1; |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | if (!profile_files_.empty()) { |
| 484 | for (const std::string& profile_file : profile_files_) { |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame^] | 485 | if (!GetClassNamesAndMethods(profile_file, &dex_files, &class_names)) { |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 486 | return -1; |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | // Concatenate class names from reference profile file. |
| 491 | if (FdIsValid(reference_profile_file_fd_)) { |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame^] | 492 | if (!GetClassNamesAndMethods(reference_profile_file_fd_, &dex_files, &class_names)) { |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 493 | return -1; |
| 494 | } |
| 495 | } |
| 496 | if (!reference_profile_file_.empty()) { |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame^] | 497 | if (!GetClassNamesAndMethods(reference_profile_file_, &dex_files, &class_names)) { |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 498 | return -1; |
| 499 | } |
| 500 | } |
| 501 | // Dump the class names. |
| 502 | std::string dump; |
| 503 | for (const std::string& class_name : class_names) { |
| 504 | dump += class_name + std::string("\n"); |
| 505 | } |
| 506 | if (!FdIsValid(dump_output_to_fd_)) { |
| 507 | std::cout << dump; |
| 508 | } else { |
| 509 | unix_file::FdFile out_fd(dump_output_to_fd_, false /*check_usage*/); |
| 510 | if (!out_fd.WriteFully(dump.c_str(), dump.length())) { |
| 511 | return -1; |
| 512 | } |
| 513 | } |
| 514 | return 0; |
| 515 | } |
| 516 | |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame^] | 517 | bool ShouldOnlyDumpClassesAndMethods() { |
| 518 | return dump_classes_and_methods_; |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | // Read lines from the given file, dropping comments and empty lines. Post-process each line with |
| 522 | // the given function. |
| 523 | template <typename T> |
| 524 | static T* ReadCommentedInputFromFile( |
| 525 | const char* input_filename, std::function<std::string(const char*)>* process) { |
| 526 | std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in)); |
| 527 | if (input_file.get() == nullptr) { |
| 528 | LOG(ERROR) << "Failed to open input file " << input_filename; |
| 529 | return nullptr; |
| 530 | } |
| 531 | std::unique_ptr<T> result( |
| 532 | ReadCommentedInputStream<T>(*input_file, process)); |
| 533 | input_file->close(); |
| 534 | return result.release(); |
| 535 | } |
| 536 | |
| 537 | // Read lines from the given stream, dropping comments and empty lines. Post-process each line |
| 538 | // with the given function. |
| 539 | template <typename T> |
| 540 | static T* ReadCommentedInputStream( |
| 541 | std::istream& in_stream, |
| 542 | std::function<std::string(const char*)>* process) { |
| 543 | std::unique_ptr<T> output(new T()); |
| 544 | while (in_stream.good()) { |
| 545 | std::string dot; |
| 546 | std::getline(in_stream, dot); |
| 547 | if (android::base::StartsWith(dot, "#") || dot.empty()) { |
| 548 | continue; |
| 549 | } |
| 550 | if (process != nullptr) { |
| 551 | std::string descriptor((*process)(dot.c_str())); |
| 552 | output->insert(output->end(), descriptor); |
| 553 | } else { |
| 554 | output->insert(output->end(), dot); |
| 555 | } |
| 556 | } |
| 557 | return output.release(); |
| 558 | } |
| 559 | |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 560 | // Find class klass_descriptor in the given dex_files and store its reference |
| 561 | // in the out parameter class_ref. |
| 562 | // Return true if the definition of the class was found in any of the dex_files. |
| 563 | bool FindClass(const std::vector<std::unique_ptr<const DexFile>>& dex_files, |
| 564 | const std::string& klass_descriptor, |
| 565 | /*out*/ProfileMethodInfo::ProfileClassReference* class_ref) { |
| 566 | for (const std::unique_ptr<const DexFile>& dex_file_ptr : dex_files) { |
| 567 | const DexFile* dex_file = dex_file_ptr.get(); |
| 568 | const DexFile::TypeId* type_id = dex_file->FindTypeId(klass_descriptor.c_str()); |
| 569 | if (type_id == nullptr) { |
| 570 | continue; |
| 571 | } |
| 572 | dex::TypeIndex type_index = dex_file->GetIndexForTypeId(*type_id); |
| 573 | if (dex_file->FindClassDef(type_index) == nullptr) { |
| 574 | // Class is only referenced in the current dex file but not defined in it. |
| 575 | continue; |
| 576 | } |
| 577 | class_ref->dex_file = dex_file; |
| 578 | class_ref->type_index = type_index; |
| 579 | return true; |
| 580 | } |
| 581 | return false; |
| 582 | } |
| 583 | |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame^] | 584 | // Find the method specified by method_spec in the class class_ref. |
| 585 | uint32_t FindMethodIndex(const ProfileMethodInfo::ProfileClassReference& class_ref, |
| 586 | const std::string& method_spec) { |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 587 | std::vector<std::string> name_and_signature; |
| 588 | Split(method_spec, kProfileParsingFirstCharInSignature, &name_and_signature); |
| 589 | if (name_and_signature.size() != 2) { |
| 590 | LOG(ERROR) << "Invalid method name and signature " << method_spec; |
| 591 | } |
| 592 | const std::string& name = name_and_signature[0]; |
| 593 | const std::string& signature = kProfileParsingFirstCharInSignature + name_and_signature[1]; |
| 594 | const DexFile* dex_file = class_ref.dex_file; |
| 595 | |
| 596 | const DexFile::StringId* name_id = dex_file->FindStringId(name.c_str()); |
| 597 | if (name_id == nullptr) { |
| 598 | LOG(ERROR) << "Could not find name: " << name; |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame^] | 599 | return DexFile::kDexNoIndex; |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 600 | } |
| 601 | dex::TypeIndex return_type_idx; |
| 602 | std::vector<dex::TypeIndex> param_type_idxs; |
| 603 | if (!dex_file->CreateTypeList(signature, &return_type_idx, ¶m_type_idxs)) { |
| 604 | LOG(ERROR) << "Could not create type list" << signature; |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame^] | 605 | return DexFile::kDexNoIndex; |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 606 | } |
| 607 | const DexFile::ProtoId* proto_id = dex_file->FindProtoId(return_type_idx, param_type_idxs); |
| 608 | if (proto_id == nullptr) { |
| 609 | LOG(ERROR) << "Could not find proto_id: " << name; |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame^] | 610 | return DexFile::kDexNoIndex; |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 611 | } |
| 612 | const DexFile::MethodId* method_id = dex_file->FindMethodId( |
| 613 | dex_file->GetTypeId(class_ref.type_index), *name_id, *proto_id); |
| 614 | if (method_id == nullptr) { |
| 615 | LOG(ERROR) << "Could not find method_id: " << name; |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame^] | 616 | return DexFile::kDexNoIndex; |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 617 | } |
| 618 | |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame^] | 619 | return dex_file->GetIndexForMethodId(*method_id); |
| 620 | } |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 621 | |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame^] | 622 | // Given a method, return true if the method has a single INVOKE_VIRTUAL in its byte code. |
| 623 | // Upon success it returns true and stores the method index and the invoke dex pc |
| 624 | // in the output parameters. |
| 625 | // The format of the method spec is "inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;". |
| 626 | // |
| 627 | // TODO(calin): support INVOKE_INTERFACE and the range variants. |
| 628 | bool HasSingleInvoke(const ProfileMethodInfo::ProfileClassReference& class_ref, |
| 629 | uint16_t method_index, |
| 630 | /*out*/uint32_t* dex_pc) { |
| 631 | const DexFile* dex_file = class_ref.dex_file; |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 632 | uint32_t offset = dex_file->FindCodeItemOffset( |
| 633 | *dex_file->FindClassDef(class_ref.type_index), |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame^] | 634 | method_index); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 635 | const DexFile::CodeItem* code_item = dex_file->GetCodeItem(offset); |
| 636 | |
| 637 | bool found_invoke = false; |
| 638 | for (CodeItemIterator it(*code_item); !it.Done(); it.Advance()) { |
| 639 | if (it.CurrentInstruction().Opcode() == Instruction::INVOKE_VIRTUAL) { |
| 640 | if (found_invoke) { |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame^] | 641 | LOG(ERROR) << "Multiple invoke INVOKE_VIRTUAL found: " |
| 642 | << dex_file->PrettyMethod(method_index); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 643 | return false; |
| 644 | } |
| 645 | found_invoke = true; |
| 646 | *dex_pc = it.CurrentDexPc(); |
| 647 | } |
| 648 | } |
| 649 | if (!found_invoke) { |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame^] | 650 | LOG(ERROR) << "Could not find any INVOKE_VIRTUAL: " << dex_file->PrettyMethod(method_index); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 651 | } |
| 652 | return found_invoke; |
| 653 | } |
| 654 | |
| 655 | // Process a line defining a class or a method and its inline caches. |
| 656 | // Upon success return true and add the class or the method info to profile. |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 657 | // The possible line formats are: |
| 658 | // "LJustTheCass;". |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 659 | // "LTestInline;->inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;". |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 660 | // "LTestInline;->inlineMissingTypes(LSuper;)I+missing_types". |
| 661 | // "LTestInline;->inlineNoInlineCaches(LSuper;)I". |
Mathieu Chartier | d808e8b | 2017-03-21 13:37:41 -0700 | [diff] [blame] | 662 | // "LTestInline;->*". |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 663 | // The method and classes are searched only in the given dex files. |
| 664 | bool ProcessLine(const std::vector<std::unique_ptr<const DexFile>>& dex_files, |
| 665 | const std::string& line, |
| 666 | /*out*/ProfileCompilationInfo* profile) { |
| 667 | std::string klass; |
| 668 | std::string method_str; |
| 669 | size_t method_sep_index = line.find(kMethodSep); |
| 670 | if (method_sep_index == std::string::npos) { |
| 671 | klass = line; |
| 672 | } else { |
| 673 | klass = line.substr(0, method_sep_index); |
| 674 | method_str = line.substr(method_sep_index + kMethodSep.size()); |
| 675 | } |
| 676 | |
| 677 | ProfileMethodInfo::ProfileClassReference class_ref; |
| 678 | if (!FindClass(dex_files, klass, &class_ref)) { |
Mathieu Chartier | 3f12134 | 2017-03-06 11:16:20 -0800 | [diff] [blame] | 679 | LOG(WARNING) << "Could not find class: " << klass; |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 680 | return false; |
| 681 | } |
| 682 | |
Mathieu Chartier | d808e8b | 2017-03-21 13:37:41 -0700 | [diff] [blame] | 683 | if (method_str.empty() || method_str == kClassAllMethods) { |
| 684 | // Start by adding the class. |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 685 | std::set<DexCacheResolvedClasses> resolved_class_set; |
| 686 | const DexFile* dex_file = class_ref.dex_file; |
| 687 | const auto& dex_resolved_classes = resolved_class_set.emplace( |
| 688 | dex_file->GetLocation(), |
| 689 | dex_file->GetBaseLocation(), |
| 690 | dex_file->GetLocationChecksum()); |
| 691 | dex_resolved_classes.first->AddClass(class_ref.type_index); |
Mathieu Chartier | d808e8b | 2017-03-21 13:37:41 -0700 | [diff] [blame] | 692 | std::vector<ProfileMethodInfo> methods; |
| 693 | if (method_str == kClassAllMethods) { |
| 694 | // Add all of the methods. |
| 695 | const DexFile::ClassDef* class_def = dex_file->FindClassDef(class_ref.type_index); |
| 696 | const uint8_t* class_data = dex_file->GetClassData(*class_def); |
| 697 | if (class_data != nullptr) { |
| 698 | ClassDataItemIterator it(*dex_file, class_data); |
| 699 | while (it.HasNextStaticField() || it.HasNextInstanceField()) { |
| 700 | it.Next(); |
| 701 | } |
| 702 | while (it.HasNextDirectMethod() || it.HasNextVirtualMethod()) { |
| 703 | if (it.GetMethodCodeItemOffset() != 0) { |
| 704 | // Add all of the methods that have code to the profile. |
| 705 | const uint32_t method_idx = it.GetMemberIndex(); |
| 706 | methods.push_back(ProfileMethodInfo(dex_file, method_idx)); |
| 707 | } |
| 708 | it.Next(); |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | profile->AddMethodsAndClasses(methods, resolved_class_set); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 713 | return true; |
| 714 | } |
| 715 | |
| 716 | // Process the method. |
| 717 | std::string method_spec; |
| 718 | std::vector<std::string> inline_cache_elems; |
| 719 | |
| 720 | std::vector<std::string> method_elems; |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 721 | bool is_missing_types = false; |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 722 | Split(method_str, kProfileParsingInlineChacheSep, &method_elems); |
| 723 | if (method_elems.size() == 2) { |
| 724 | method_spec = method_elems[0]; |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 725 | is_missing_types = method_elems[1] == kMissingTypesMarker; |
| 726 | if (!is_missing_types) { |
| 727 | Split(method_elems[1], kProfileParsingTypeSep, &inline_cache_elems); |
| 728 | } |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 729 | } else if (method_elems.size() == 1) { |
| 730 | method_spec = method_elems[0]; |
| 731 | } else { |
| 732 | LOG(ERROR) << "Invalid method line: " << line; |
| 733 | return false; |
| 734 | } |
| 735 | |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame^] | 736 | const uint32_t method_index = FindMethodIndex(class_ref, method_spec); |
| 737 | if (method_index == DexFile::kDexNoIndex) { |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 738 | return false; |
| 739 | } |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame^] | 740 | |
| 741 | std::vector<ProfileMethodInfo> pmi; |
| 742 | std::vector<ProfileMethodInfo::ProfileInlineCache> inline_caches; |
| 743 | if (is_missing_types || !inline_cache_elems.empty()) { |
| 744 | uint32_t dex_pc; |
| 745 | if (!HasSingleInvoke(class_ref, method_index, &dex_pc)) { |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 746 | return false; |
| 747 | } |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame^] | 748 | std::vector<ProfileMethodInfo::ProfileClassReference> classes(inline_cache_elems.size()); |
| 749 | size_t class_it = 0; |
| 750 | for (const std::string& ic_class : inline_cache_elems) { |
| 751 | if (!FindClass(dex_files, ic_class, &(classes[class_it++]))) { |
| 752 | LOG(ERROR) << "Could not find class: " << ic_class; |
| 753 | return false; |
| 754 | } |
| 755 | } |
| 756 | inline_caches.emplace_back(dex_pc, is_missing_types, classes); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 757 | } |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 758 | pmi.emplace_back(class_ref.dex_file, method_index, inline_caches); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 759 | profile->AddMethodsAndClasses(pmi, std::set<DexCacheResolvedClasses>()); |
| 760 | return true; |
| 761 | } |
| 762 | |
| 763 | // Creates a profile from a human friendly textual representation. |
| 764 | // The expected input format is: |
| 765 | // # Classes |
| 766 | // Ljava/lang/Comparable; |
| 767 | // Ljava/lang/Math; |
| 768 | // # Methods with inline caches |
| 769 | // LTestInline;->inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC; |
| 770 | // LTestInline;->noInlineCache(LSuper;)I |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 771 | int CreateProfile() { |
David Sehr | 153da0e | 2017-02-15 08:54:51 -0800 | [diff] [blame] | 772 | // Validate parameters for this command. |
| 773 | if (apk_files_.empty() && apks_fd_.empty()) { |
| 774 | Usage("APK files must be specified"); |
| 775 | } |
| 776 | if (dex_locations_.empty()) { |
| 777 | Usage("DEX locations must be specified"); |
| 778 | } |
| 779 | if (reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) { |
| 780 | Usage("Reference profile must be specified with --reference-profile-file or " |
| 781 | "--reference-profile-file-fd"); |
| 782 | } |
| 783 | if (!profile_files_.empty() || !profile_files_fd_.empty()) { |
| 784 | Usage("Profile must be specified with --reference-profile-file or " |
| 785 | "--reference-profile-file-fd"); |
| 786 | } |
| 787 | // for ZipArchive::OpenFromFd |
| 788 | MemMap::Init(); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 789 | // Open the profile output file if needed. |
| 790 | int fd = reference_profile_file_fd_; |
| 791 | if (!FdIsValid(fd)) { |
| 792 | CHECK(!reference_profile_file_.empty()); |
| 793 | fd = open(reference_profile_file_.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644); |
| 794 | if (fd < 0) { |
| 795 | LOG(ERROR) << "Cannot open " << reference_profile_file_ << strerror(errno); |
| 796 | return -1; |
| 797 | } |
| 798 | } |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 799 | // Read the user-specified list of classes and methods. |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 800 | std::unique_ptr<std::unordered_set<std::string>> |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 801 | user_lines(ReadCommentedInputFromFile<std::unordered_set<std::string>>( |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 802 | create_profile_from_file_.c_str(), nullptr)); // No post-processing. |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 803 | |
| 804 | // Open the dex files to look up classes and methods. |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 805 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 806 | OpenApkFilesFromLocations(&dex_files); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 807 | |
| 808 | // Process the lines one by one and add the successful ones to the profile. |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 809 | ProfileCompilationInfo info; |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 810 | |
| 811 | for (const auto& line : *user_lines) { |
| 812 | ProcessLine(dex_files, line, &info); |
| 813 | } |
| 814 | |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 815 | // Write the profile file. |
| 816 | CHECK(info.Save(fd)); |
| 817 | if (close(fd) < 0) { |
| 818 | PLOG(WARNING) << "Failed to close descriptor"; |
| 819 | } |
| 820 | return 0; |
| 821 | } |
| 822 | |
| 823 | bool ShouldCreateProfile() { |
| 824 | return !create_profile_from_file_.empty(); |
| 825 | } |
| 826 | |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 827 | int GenerateTestProfile() { |
David Sehr | 153da0e | 2017-02-15 08:54:51 -0800 | [diff] [blame] | 828 | // Validate parameters for this command. |
| 829 | if (test_profile_method_ratio_ > 100) { |
| 830 | Usage("Invalid ratio for --generate-test-profile-method-ratio"); |
| 831 | } |
| 832 | if (test_profile_class_ratio_ > 100) { |
| 833 | Usage("Invalid ratio for --generate-test-profile-class-ratio"); |
| 834 | } |
Jeff Hao | f0a31f8 | 2017-03-27 15:50:37 -0700 | [diff] [blame] | 835 | // If given APK files or DEX locations, check that they're ok. |
| 836 | if (!apk_files_.empty() || !apks_fd_.empty() || !dex_locations_.empty()) { |
| 837 | if (apk_files_.empty() && apks_fd_.empty()) { |
| 838 | Usage("APK files must be specified when passing DEX locations to --generate-test-profile"); |
| 839 | } |
| 840 | if (dex_locations_.empty()) { |
| 841 | Usage("DEX locations must be specified when passing APK files to --generate-test-profile"); |
| 842 | } |
| 843 | } |
David Sehr | 153da0e | 2017-02-15 08:54:51 -0800 | [diff] [blame] | 844 | // ShouldGenerateTestProfile confirms !test_profile_.empty(). |
George Burgess IV | 71f7726 | 2016-10-25 13:08:17 -0700 | [diff] [blame] | 845 | 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] | 846 | if (profile_test_fd < 0) { |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 847 | LOG(ERROR) << "Cannot open " << test_profile_ << strerror(errno); |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 848 | return -1; |
| 849 | } |
Jeff Hao | f0a31f8 | 2017-03-27 15:50:37 -0700 | [diff] [blame] | 850 | bool result; |
| 851 | if (apk_files_.empty() && apks_fd_.empty() && dex_locations_.empty()) { |
| 852 | result = ProfileCompilationInfo::GenerateTestProfile(profile_test_fd, |
| 853 | test_profile_num_dex_, |
| 854 | test_profile_method_ratio_, |
| 855 | test_profile_class_ratio_, |
| 856 | test_profile_seed_); |
| 857 | } else { |
| 858 | // Initialize MemMap for ZipArchive::OpenFromFd. |
| 859 | MemMap::Init(); |
| 860 | // Open the dex files to look up classes and methods. |
| 861 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 862 | OpenApkFilesFromLocations(&dex_files); |
| 863 | // Create a random profile file based on the set of dex files. |
| 864 | result = ProfileCompilationInfo::GenerateTestProfile(profile_test_fd, |
| 865 | dex_files, |
| 866 | test_profile_seed_); |
| 867 | } |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 868 | close(profile_test_fd); // ignore close result. |
| 869 | return result ? 0 : -1; |
| 870 | } |
| 871 | |
| 872 | bool ShouldGenerateTestProfile() { |
| 873 | return !test_profile_.empty(); |
| 874 | } |
| 875 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 876 | private: |
| 877 | static void ParseFdForCollection(const StringPiece& option, |
| 878 | const char* arg_name, |
| 879 | std::vector<int>* fds) { |
| 880 | int fd; |
| 881 | ParseUintOption(option, arg_name, &fd, Usage); |
| 882 | fds->push_back(fd); |
| 883 | } |
| 884 | |
| 885 | static void CloseAllFds(const std::vector<int>& fds, const char* descriptor) { |
| 886 | for (size_t i = 0; i < fds.size(); i++) { |
| 887 | if (close(fds[i]) < 0) { |
| 888 | PLOG(WARNING) << "Failed to close descriptor for " << descriptor << " at index " << i; |
| 889 | } |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | void LogCompletionTime() { |
David Sehr | 52683cf | 2016-05-05 09:02:38 -0700 | [diff] [blame] | 894 | static constexpr uint64_t kLogThresholdTime = MsToNs(100); // 100ms |
| 895 | uint64_t time_taken = NanoTime() - start_ns_; |
David Sehr | ed79301 | 2016-05-05 13:39:43 -0700 | [diff] [blame] | 896 | if (time_taken > kLogThresholdTime) { |
David Sehr | d855718 | 2016-05-06 12:29:35 -0700 | [diff] [blame] | 897 | LOG(WARNING) << "profman took " << PrettyDuration(time_taken); |
David Sehr | ed79301 | 2016-05-05 13:39:43 -0700 | [diff] [blame] | 898 | } |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | std::vector<std::string> profile_files_; |
| 902 | std::vector<int> profile_files_fd_; |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 903 | std::vector<std::string> dex_locations_; |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 904 | std::vector<std::string> apk_files_; |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 905 | std::vector<int> apks_fd_; |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 906 | std::string reference_profile_file_; |
| 907 | int reference_profile_file_fd_; |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 908 | bool dump_only_; |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame^] | 909 | bool dump_classes_and_methods_; |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 910 | int dump_output_to_fd_; |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 911 | std::string test_profile_; |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 912 | std::string create_profile_from_file_; |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 913 | uint16_t test_profile_num_dex_; |
| 914 | uint16_t test_profile_method_ratio_; |
| 915 | uint16_t test_profile_class_ratio_; |
Jeff Hao | f0a31f8 | 2017-03-27 15:50:37 -0700 | [diff] [blame] | 916 | uint32_t test_profile_seed_; |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 917 | uint64_t start_ns_; |
| 918 | }; |
| 919 | |
| 920 | // See ProfileAssistant::ProcessingResult for return codes. |
| 921 | static int profman(int argc, char** argv) { |
| 922 | ProfMan profman; |
| 923 | |
| 924 | // Parse arguments. Argument mistakes will lead to exit(EXIT_FAILURE) in UsageError. |
| 925 | profman.ParseArgs(argc, argv); |
| 926 | |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 927 | if (profman.ShouldGenerateTestProfile()) { |
| 928 | return profman.GenerateTestProfile(); |
| 929 | } |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 930 | if (profman.ShouldOnlyDumpProfile()) { |
| 931 | return profman.DumpProfileInfo(); |
| 932 | } |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame^] | 933 | if (profman.ShouldOnlyDumpClassesAndMethods()) { |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 934 | return profman.DumpClasses(); |
| 935 | } |
| 936 | if (profman.ShouldCreateProfile()) { |
| 937 | return profman.CreateProfile(); |
| 938 | } |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 939 | // Process profile information and assess if we need to do a profile guided compilation. |
| 940 | // This operation involves I/O. |
| 941 | return profman.ProcessProfiles(); |
| 942 | } |
| 943 | |
| 944 | } // namespace art |
| 945 | |
| 946 | int main(int argc, char **argv) { |
| 947 | return art::profman(argc, argv); |
| 948 | } |
| 949 | |