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