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