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