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