blob: c16fadd828142c5c9177c39cdb5cc0e9baa338a7 [file] [log] [blame]
Calin Juravle2e2db782016-02-23 12:00:03 +00001/*
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 Gampe8cf9cb32017-07-19 09:28:38 -070017#include <errno.h>
Calin Juravle2e2db782016-02-23 12:00:03 +000018#include <stdio.h>
19#include <stdlib.h>
20#include <sys/file.h>
David Sehr2b80ed42018-05-08 08:58:15 -070021#include <sys/mman.h>
Calin Juravlee10c1e22018-01-26 20:10:15 -080022#include <sys/param.h>
Calin Juravle2e2db782016-02-23 12:00:03 +000023#include <unistd.h>
24
David Sehr7c80f2d2017-02-07 16:47:58 -080025#include <fstream>
Calin Juravle876f3502016-03-24 16:16:34 +000026#include <iostream>
David Sehr7c80f2d2017-02-07 16:47:58 -080027#include <set>
Calin Juravle2e2db782016-02-23 12:00:03 +000028#include <string>
David Sehr7c80f2d2017-02-07 16:47:58 -080029#include <unordered_set>
Calin Juravle2e2db782016-02-23 12:00:03 +000030#include <vector>
31
Andreas Gampe46ee31b2016-12-14 10:11:49 -080032#include "android-base/stringprintf.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080033#include "android-base/strings.h"
34
Calin Juravle2e2db782016-02-23 12:00:03 +000035#include "base/dumpable.h"
Andreas Gampe57943812017-12-06 21:39:13 -080036#include "base/logging.h" // For InitLogging.
Calin Juravle2e2db782016-02-23 12:00:03 +000037#include "base/scoped_flock.h"
38#include "base/stringpiece.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000039#include "base/time_utils.h"
40#include "base/unix_file/fd_file.h"
David Sehrc431b9d2018-03-02 12:01:51 -080041#include "base/utils.h"
David Sehr79e26072018-04-06 17:58:50 -070042#include "base/zip_archive.h"
Mathieu Chartier2f794552017-06-19 10:58:08 -070043#include "boot_image_profile.h"
David Sehr312f3b22018-03-19 08:39:26 -070044#include "dex/bytecode_utils.h"
David Sehr9e734c72018-01-04 17:56:19 -080045#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 Sehr312f3b22018-03-19 08:39:26 -070049#include "dex/type_reference.h"
David Sehr82d046e2018-04-23 08:14:19 -070050#include "profile/profile_compilation_info.h"
Mathieu Chartierdbddc222017-05-24 12:04:13 -070051#include "profile_assistant.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000052
53namespace art {
54
55static int original_argc;
56static char** original_argv;
57
58static std::string CommandLine() {
59 std::vector<std::string> command;
60 for (int i = 0; i < original_argc; ++i) {
61 command.push_back(original_argv[i]);
62 }
Andreas Gampe9186ced2016-12-12 14:28:21 -080063 return android::base::Join(command, ' ');
Calin Juravle2e2db782016-02-23 12:00:03 +000064}
65
David Sehr4fcdd6d2016-05-24 14:52:31 -070066static constexpr int kInvalidFd = -1;
67
68static bool FdIsValid(int fd) {
69 return fd != kInvalidFd;
70}
71
Calin Juravle2e2db782016-02-23 12:00:03 +000072static void UsageErrorV(const char* fmt, va_list ap) {
73 std::string error;
Andreas Gampe46ee31b2016-12-14 10:11:49 -080074 android::base::StringAppendV(&error, fmt, ap);
Calin Juravle2e2db782016-02-23 12:00:03 +000075 LOG(ERROR) << error;
76}
77
78static void UsageError(const char* fmt, ...) {
79 va_list ap;
80 va_start(ap, fmt);
81 UsageErrorV(fmt, ap);
82 va_end(ap);
83}
84
85NO_RETURN static void Usage(const char *fmt, ...) {
86 va_list ap;
87 va_start(ap, fmt);
88 UsageErrorV(fmt, ap);
89 va_end(ap);
90
91 UsageError("Command: %s", CommandLine().c_str());
92 UsageError("Usage: profman [options]...");
93 UsageError("");
David Sehr4fcdd6d2016-05-24 14:52:31 -070094 UsageError(" --dump-only: dumps the content of the specified profile files");
95 UsageError(" to standard output (default) in a human readable form.");
96 UsageError("");
David Sehr7c80f2d2017-02-07 16:47:58 -080097 UsageError(" --dump-output-to-fd=<number>: redirects --dump-only output to a file descriptor.");
98 UsageError("");
Mathieu Chartier34067262017-04-06 13:55:46 -070099 UsageError(" --dump-classes-and-methods: dumps a sorted list of classes and methods that are");
100 UsageError(" in the specified profile file to standard output (default) in a human");
101 UsageError(" readable form. The output is valid input for --create-profile-from");
Calin Juravle876f3502016-03-24 16:16:34 +0000102 UsageError("");
Calin Juravle2e2db782016-02-23 12:00:03 +0000103 UsageError(" --profile-file=<filename>: specify profiler output file to use for compilation.");
104 UsageError(" Can be specified multiple time, in which case the data from the different");
105 UsageError(" profiles will be aggregated.");
106 UsageError("");
107 UsageError(" --profile-file-fd=<number>: same as --profile-file but accepts a file descriptor.");
108 UsageError(" Cannot be used together with --profile-file.");
109 UsageError("");
110 UsageError(" --reference-profile-file=<filename>: specify a reference profile.");
111 UsageError(" The data in this file will be compared with the data obtained by merging");
112 UsageError(" all the files specified with --profile-file or --profile-file-fd.");
113 UsageError(" If the exit code is EXIT_COMPILE then all --profile-file will be merged into");
114 UsageError(" --reference-profile-file. ");
115 UsageError("");
116 UsageError(" --reference-profile-file-fd=<number>: same as --reference-profile-file but");
117 UsageError(" accepts a file descriptor. Cannot be used together with");
118 UsageError(" --reference-profile-file.");
David Sehr7c80f2d2017-02-07 16:47:58 -0800119 UsageError("");
Calin Juravle7bcdb532016-06-07 16:14:47 +0100120 UsageError(" --generate-test-profile=<filename>: generates a random profile file for testing.");
121 UsageError(" --generate-test-profile-num-dex=<number>: number of dex files that should be");
122 UsageError(" included in the generated profile. Defaults to 20.");
Shubham Ajmerad704f0b2017-07-26 16:33:35 -0700123 UsageError(" --generate-test-profile-method-percentage=<number>: the percentage from the maximum");
Calin Juravle7bcdb532016-06-07 16:14:47 +0100124 UsageError(" number of methods that should be generated. Defaults to 5.");
Shubham Ajmerad704f0b2017-07-26 16:33:35 -0700125 UsageError(" --generate-test-profile-class-percentage=<number>: the percentage from the maximum");
Calin Juravle7bcdb532016-06-07 16:14:47 +0100126 UsageError(" number of classes that should be generated. Defaults to 5.");
Jeff Haof0a31f82017-03-27 15:50:37 -0700127 UsageError(" --generate-test-profile-seed=<number>: seed for random number generator used when");
128 UsageError(" generating random test profiles. Defaults to using NanoTime.");
Calin Juravle7bcdb532016-06-07 16:14:47 +0100129 UsageError("");
Mathieu Chartier34067262017-04-06 13:55:46 -0700130 UsageError(" --create-profile-from=<filename>: creates a profile from a list of classes and");
131 UsageError(" methods.");
David Sehr7c80f2d2017-02-07 16:47:58 -0800132 UsageError("");
David Sehr546d24f2016-06-02 10:46:19 -0700133 UsageError(" --dex-location=<string>: location string to use with corresponding");
134 UsageError(" apk-fd to find dex files");
David Sehr4fcdd6d2016-05-24 14:52:31 -0700135 UsageError("");
David Sehr546d24f2016-06-02 10:46:19 -0700136 UsageError(" --apk-fd=<number>: file descriptor containing an open APK to");
David Sehr4fcdd6d2016-05-24 14:52:31 -0700137 UsageError(" search for dex files");
David Sehr7c80f2d2017-02-07 16:47:58 -0800138 UsageError(" --apk-=<filename>: an APK to search for dex files");
David Brazdilf13ac7c2018-01-30 10:09:08 +0000139 UsageError(" --skip-apk-verification: do not attempt to verify APKs");
David Sehr4fcdd6d2016-05-24 14:52:31 -0700140 UsageError("");
Mathieu Chartier2f794552017-06-19 10:58:08 -0700141 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 Chartier8eecddf2017-07-12 16:05:54 -0700149 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.");
Calin Juravle2dba0ab2018-01-22 19:22:24 -0800152 UsageError(" --copy-and-update-profile-key: if present, profman will copy the profile from");
153 UsageError(" the file passed with --profile-fd(file) to the profile passed with");
154 UsageError(" --reference-profile-fd(file) and update at the same time the profile-key");
155 UsageError(" of entries corresponding to the apks passed with --apk(-fd).");
Mathieu Chartier2f794552017-06-19 10:58:08 -0700156 UsageError("");
Calin Juravle2e2db782016-02-23 12:00:03 +0000157
158 exit(EXIT_FAILURE);
159}
160
Calin Juravle7bcdb532016-06-07 16:14:47 +0100161// Note: make sure you update the Usage if you change these values.
162static constexpr uint16_t kDefaultTestProfileNumDex = 20;
Shubham Ajmerad704f0b2017-07-26 16:33:35 -0700163static constexpr uint16_t kDefaultTestProfileMethodPercentage = 5;
164static constexpr uint16_t kDefaultTestProfileClassPercentage = 5;
Calin Juravle7bcdb532016-06-07 16:14:47 +0100165
Calin Juravlee0ac1152017-02-13 19:03:47 -0800166// Separators used when parsing human friendly representation of profiles.
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800167static const std::string kMethodSep = "->"; // NOLINT [runtime/string] [4]
168static const std::string kMissingTypesMarker = "missing_types"; // NOLINT [runtime/string] [4]
169static const std::string kInvalidClassDescriptor = "invalid_class"; // NOLINT [runtime/string] [4]
170static const std::string kInvalidMethod = "invalid_method"; // NOLINT [runtime/string] [4]
171static const std::string kClassAllMethods = "*"; // NOLINT [runtime/string] [4]
Calin Juravlee0ac1152017-02-13 19:03:47 -0800172static constexpr char kProfileParsingInlineChacheSep = '+';
173static constexpr char kProfileParsingTypeSep = ',';
174static constexpr char kProfileParsingFirstCharInSignature = '(';
Mathieu Chartierea650f32017-05-24 12:04:13 -0700175static constexpr char kMethodFlagStringHot = 'H';
176static constexpr char kMethodFlagStringStartup = 'S';
177static constexpr char kMethodFlagStringPostStartup = 'P';
Calin Juravlee0ac1152017-02-13 19:03:47 -0800178
David Sehr2b80ed42018-05-08 08:58:15 -0700179NO_RETURN static void Abort(const char* msg) {
180 LOG(ERROR) << "Aborted: " << msg;
181 exit(1);
182}
183
Calin Juravlee0ac1152017-02-13 19:03:47 -0800184// TODO(calin): This class has grown too much from its initial design. Split the functionality
185// into smaller, more contained pieces.
Calin Juravle2e2db782016-02-23 12:00:03 +0000186class ProfMan FINAL {
187 public:
188 ProfMan() :
David Sehr4fcdd6d2016-05-24 14:52:31 -0700189 reference_profile_file_fd_(kInvalidFd),
190 dump_only_(false),
Mathieu Chartier34067262017-04-06 13:55:46 -0700191 dump_classes_and_methods_(false),
Mathieu Chartier2f794552017-06-19 10:58:08 -0700192 generate_boot_image_profile_(false),
David Sehr4fcdd6d2016-05-24 14:52:31 -0700193 dump_output_to_fd_(kInvalidFd),
Calin Juravle7bcdb532016-06-07 16:14:47 +0100194 test_profile_num_dex_(kDefaultTestProfileNumDex),
Shubham Ajmerad704f0b2017-07-26 16:33:35 -0700195 test_profile_method_percerntage_(kDefaultTestProfileMethodPercentage),
196 test_profile_class_percentage_(kDefaultTestProfileClassPercentage),
Jeff Haof0a31f82017-03-27 15:50:37 -0700197 test_profile_seed_(NanoTime()),
Calin Juravle2dba0ab2018-01-22 19:22:24 -0800198 start_ns_(NanoTime()),
199 copy_and_update_profile_key_(false) {}
Calin Juravle2e2db782016-02-23 12:00:03 +0000200
201 ~ProfMan() {
202 LogCompletionTime();
203 }
204
205 void ParseArgs(int argc, char **argv) {
206 original_argc = argc;
207 original_argv = argv;
208
David Sehr2b80ed42018-05-08 08:58:15 -0700209 MemMap::Init();
210 InitLogging(argv, Abort);
Calin Juravle2e2db782016-02-23 12:00:03 +0000211
212 // Skip over the command name.
213 argv++;
214 argc--;
215
216 if (argc == 0) {
217 Usage("No arguments specified");
218 }
219
220 for (int i = 0; i < argc; ++i) {
221 const StringPiece option(argv[i]);
222 const bool log_options = false;
223 if (log_options) {
Calin Juravle876f3502016-03-24 16:16:34 +0000224 LOG(INFO) << "profman: option[" << i << "]=" << argv[i];
Calin Juravle2e2db782016-02-23 12:00:03 +0000225 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700226 if (option == "--dump-only") {
227 dump_only_ = true;
Mathieu Chartier34067262017-04-06 13:55:46 -0700228 } else if (option == "--dump-classes-and-methods") {
229 dump_classes_and_methods_ = true;
David Sehr7c80f2d2017-02-07 16:47:58 -0800230 } else if (option.starts_with("--create-profile-from=")) {
231 create_profile_from_file_ = option.substr(strlen("--create-profile-from=")).ToString();
David Sehr4fcdd6d2016-05-24 14:52:31 -0700232 } else if (option.starts_with("--dump-output-to-fd=")) {
233 ParseUintOption(option, "--dump-output-to-fd", &dump_output_to_fd_, Usage);
Mathieu Chartier2f794552017-06-19 10:58:08 -0700234 } else if (option == "--generate-boot-image-profile") {
235 generate_boot_image_profile_ = true;
236 } else if (option.starts_with("--boot-image-class-threshold=")) {
237 ParseUintOption(option,
238 "--boot-image-class-threshold",
239 &boot_image_options_.image_class_theshold,
240 Usage);
241 } else if (option.starts_with("--boot-image-clean-class-threshold=")) {
242 ParseUintOption(option,
243 "--boot-image-clean-class-threshold",
244 &boot_image_options_.image_class_clean_theshold,
245 Usage);
Mathieu Chartier8eecddf2017-07-12 16:05:54 -0700246 } else if (option.starts_with("--boot-image-sampled-method-threshold=")) {
247 ParseUintOption(option,
248 "--boot-image-sampled-method-threshold",
249 &boot_image_options_.compiled_method_threshold,
250 Usage);
Calin Juravle876f3502016-03-24 16:16:34 +0000251 } else if (option.starts_with("--profile-file=")) {
Calin Juravle2e2db782016-02-23 12:00:03 +0000252 profile_files_.push_back(option.substr(strlen("--profile-file=")).ToString());
253 } else if (option.starts_with("--profile-file-fd=")) {
254 ParseFdForCollection(option, "--profile-file-fd", &profile_files_fd_);
255 } else if (option.starts_with("--reference-profile-file=")) {
256 reference_profile_file_ = option.substr(strlen("--reference-profile-file=")).ToString();
257 } else if (option.starts_with("--reference-profile-file-fd=")) {
258 ParseUintOption(option, "--reference-profile-file-fd", &reference_profile_file_fd_, Usage);
David Sehr546d24f2016-06-02 10:46:19 -0700259 } else if (option.starts_with("--dex-location=")) {
260 dex_locations_.push_back(option.substr(strlen("--dex-location=")).ToString());
261 } else if (option.starts_with("--apk-fd=")) {
262 ParseFdForCollection(option, "--apk-fd", &apks_fd_);
David Sehr7c80f2d2017-02-07 16:47:58 -0800263 } else if (option.starts_with("--apk=")) {
264 apk_files_.push_back(option.substr(strlen("--apk=")).ToString());
Calin Juravle7bcdb532016-06-07 16:14:47 +0100265 } else if (option.starts_with("--generate-test-profile=")) {
266 test_profile_ = option.substr(strlen("--generate-test-profile=")).ToString();
267 } else if (option.starts_with("--generate-test-profile-num-dex=")) {
268 ParseUintOption(option,
269 "--generate-test-profile-num-dex",
270 &test_profile_num_dex_,
271 Usage);
Shubham Ajmerad704f0b2017-07-26 16:33:35 -0700272 } else if (option.starts_with("--generate-test-profile-method-percentage")) {
Calin Juravle7bcdb532016-06-07 16:14:47 +0100273 ParseUintOption(option,
Shubham Ajmerad704f0b2017-07-26 16:33:35 -0700274 "--generate-test-profile-method-percentage",
275 &test_profile_method_percerntage_,
Calin Juravle7bcdb532016-06-07 16:14:47 +0100276 Usage);
Shubham Ajmerad704f0b2017-07-26 16:33:35 -0700277 } else if (option.starts_with("--generate-test-profile-class-percentage")) {
Calin Juravle7bcdb532016-06-07 16:14:47 +0100278 ParseUintOption(option,
Shubham Ajmerad704f0b2017-07-26 16:33:35 -0700279 "--generate-test-profile-class-percentage",
280 &test_profile_class_percentage_,
Calin Juravle7bcdb532016-06-07 16:14:47 +0100281 Usage);
Jeff Haof0a31f82017-03-27 15:50:37 -0700282 } else if (option.starts_with("--generate-test-profile-seed=")) {
283 ParseUintOption(option, "--generate-test-profile-seed", &test_profile_seed_, Usage);
Calin Juravle02c08792018-02-15 19:40:48 -0800284 } else if (option.starts_with("--copy-and-update-profile-key")) {
285 copy_and_update_profile_key_ = true;
Calin Juravle2e2db782016-02-23 12:00:03 +0000286 } else {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700287 Usage("Unknown argument '%s'", option.data());
Calin Juravle2e2db782016-02-23 12:00:03 +0000288 }
289 }
290
David Sehr153da0e2017-02-15 08:54:51 -0800291 // Validate global consistency between file/fd options.
Calin Juravle2e2db782016-02-23 12:00:03 +0000292 if (!profile_files_.empty() && !profile_files_fd_.empty()) {
293 Usage("Profile files should not be specified with both --profile-file-fd and --profile-file");
294 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700295 if (!reference_profile_file_.empty() && FdIsValid(reference_profile_file_fd_)) {
296 Usage("Reference profile should not be specified with both "
297 "--reference-profile-file-fd and --reference-profile-file");
298 }
David Sehr153da0e2017-02-15 08:54:51 -0800299 if (!apk_files_.empty() && !apks_fd_.empty()) {
300 Usage("APK files should not be specified with both --apk-fd and --apk");
Calin Juravle2e2db782016-02-23 12:00:03 +0000301 }
302 }
303
Calin Juravlee10c1e22018-01-26 20:10:15 -0800304 struct ProfileFilterKey {
305 ProfileFilterKey(const std::string& dex_location, uint32_t checksum)
306 : dex_location_(dex_location), checksum_(checksum) {}
307 const std::string dex_location_;
308 uint32_t checksum_;
309
310 bool operator==(const ProfileFilterKey& other) const {
311 return checksum_ == other.checksum_ && dex_location_ == other.dex_location_;
312 }
313 bool operator<(const ProfileFilterKey& other) const {
314 return checksum_ == other.checksum_
315 ? dex_location_ < other.dex_location_
316 : checksum_ < other.checksum_;
317 }
318 };
319
Calin Juravle2e2db782016-02-23 12:00:03 +0000320 ProfileAssistant::ProcessingResult ProcessProfiles() {
David Sehr153da0e2017-02-15 08:54:51 -0800321 // Validate that at least one profile file was passed, as well as a reference profile.
322 if (profile_files_.empty() && profile_files_fd_.empty()) {
323 Usage("No profile files specified.");
324 }
325 if (reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
326 Usage("No reference profile file specified.");
327 }
328 if ((!profile_files_.empty() && FdIsValid(reference_profile_file_fd_)) ||
329 (!profile_files_fd_.empty() && !FdIsValid(reference_profile_file_fd_))) {
330 Usage("Options --profile-file-fd and --reference-profile-file-fd "
331 "should only be used together");
332 }
Calin Juravlee10c1e22018-01-26 20:10:15 -0800333
334 // Check if we have any apks which we should use to filter the profile data.
335 std::set<ProfileFilterKey> profile_filter_keys;
336 if (!GetProfileFilterKeyFromApks(&profile_filter_keys)) {
337 return ProfileAssistant::kErrorIO;
338 }
339
340 // Build the profile filter function. If the set of keys is empty it means we
341 // don't have any apks; as such we do not filter anything.
342 const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn =
343 [profile_filter_keys](const std::string& dex_location, uint32_t checksum) {
344 if (profile_filter_keys.empty()) {
345 // No --apk was specified. Accept all dex files.
346 return true;
347 } else {
348 bool res = profile_filter_keys.find(
349 ProfileFilterKey(dex_location, checksum)) != profile_filter_keys.end();
350 return res;
351 }
352 };
353
Calin Juravle2e2db782016-02-23 12:00:03 +0000354 ProfileAssistant::ProcessingResult result;
Calin Juravle2dba0ab2018-01-22 19:22:24 -0800355
Calin Juravle2e2db782016-02-23 12:00:03 +0000356 if (profile_files_.empty()) {
357 // The file doesn't need to be flushed here (ProcessProfiles will do it)
358 // so don't check the usage.
359 File file(reference_profile_file_fd_, false);
Calin Juravle2dba0ab2018-01-22 19:22:24 -0800360 result = ProfileAssistant::ProcessProfiles(profile_files_fd_,
Calin Juravlee10c1e22018-01-26 20:10:15 -0800361 reference_profile_file_fd_,
362 filter_fn);
Calin Juravle2e2db782016-02-23 12:00:03 +0000363 CloseAllFds(profile_files_fd_, "profile_files_fd_");
364 } else {
Calin Juravlee10c1e22018-01-26 20:10:15 -0800365 result = ProfileAssistant::ProcessProfiles(profile_files_,
366 reference_profile_file_,
367 filter_fn);
Calin Juravle2e2db782016-02-23 12:00:03 +0000368 }
369 return result;
370 }
371
Calin Juravlee10c1e22018-01-26 20:10:15 -0800372 bool GetProfileFilterKeyFromApks(std::set<ProfileFilterKey>* profile_filter_keys) {
373 auto process_fn = [profile_filter_keys](std::unique_ptr<const DexFile>&& dex_file) {
374 // Store the profile key of the location instead of the location itself.
375 // This will make the matching in the profile filter method much easier.
376 profile_filter_keys->emplace(ProfileCompilationInfo::GetProfileDexFileKey(
377 dex_file->GetLocation()), dex_file->GetLocationChecksum());
378 };
379 return OpenApkFilesFromLocations(process_fn);
380 }
381
382 bool OpenApkFilesFromLocations(std::vector<std::unique_ptr<const DexFile>>* dex_files) {
383 auto process_fn = [dex_files](std::unique_ptr<const DexFile>&& dex_file) {
384 dex_files->emplace_back(std::move(dex_file));
385 };
386 return OpenApkFilesFromLocations(process_fn);
387 }
388
389 bool OpenApkFilesFromLocations(
390 std::function<void(std::unique_ptr<const DexFile>&&)> process_fn) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800391 bool use_apk_fd_list = !apks_fd_.empty();
392 if (use_apk_fd_list) {
David Sehr153da0e2017-02-15 08:54:51 -0800393 // Get the APKs from the collection of FDs.
Calin Juravlee10c1e22018-01-26 20:10:15 -0800394 if (dex_locations_.empty()) {
395 // Try to compute the dex locations from the file paths of the descriptions.
396 // This will make it easier to invoke profman with --apk-fd and without
397 // being force to pass --dex-location when the location would be the apk path.
398 if (!ComputeDexLocationsFromApkFds()) {
399 return false;
400 }
401 } else {
402 if (dex_locations_.size() != apks_fd_.size()) {
403 Usage("The number of apk-fds must match the number of dex-locations.");
404 }
405 }
David Sehr153da0e2017-02-15 08:54:51 -0800406 } else if (!apk_files_.empty()) {
Calin Juravle02c08792018-02-15 19:40:48 -0800407 if (dex_locations_.empty()) {
408 // If no dex locations are specified use the apk names as locations.
409 dex_locations_ = apk_files_;
410 } else if (dex_locations_.size() != apk_files_.size()) {
411 Usage("The number of apk-fds must match the number of dex-locations.");
412 }
David Sehr153da0e2017-02-15 08:54:51 -0800413 } else {
414 // No APKs were specified.
415 CHECK(dex_locations_.empty());
Calin Juravlee10c1e22018-01-26 20:10:15 -0800416 return true;
David Sehr7c80f2d2017-02-07 16:47:58 -0800417 }
418 static constexpr bool kVerifyChecksum = true;
419 for (size_t i = 0; i < dex_locations_.size(); ++i) {
David Sehr2b80ed42018-05-08 08:58:15 -0700420 std::unique_ptr<File> apk_file;
Calin Juravle1bfe4bd2018-04-26 16:00:11 -0700421 // We do not need to verify the apk for processing profiles.
David Sehr7c80f2d2017-02-07 16:47:58 -0800422 if (use_apk_fd_list) {
David Sehr2b80ed42018-05-08 08:58:15 -0700423 apk_file.reset(new File(apks_fd_[i], false/*checkUsage*/));
David Sehr7c80f2d2017-02-07 16:47:58 -0800424 } else {
David Sehr2b80ed42018-05-08 08:58:15 -0700425 apk_file.reset(new File(apk_files_[i], O_RDONLY, false/*checkUsage*/));
426 if (apk_file == nullptr) {
427 LOG(ERROR) << "Open failed for '" << dex_locations_[i] << "' ";
Calin Juravlee10c1e22018-01-26 20:10:15 -0800428 return false;
David Sehr7c80f2d2017-02-07 16:47:58 -0800429 }
430 }
David Sehr2b80ed42018-05-08 08:58:15 -0700431 std::string error_msg;
432 std::unique_ptr<MemMap> mmap(MemMap::MapFile(apk_file->GetLength(),
433 PROT_READ,
434 MAP_PRIVATE,
435 apk_file->Fd(),
436 /*start*/0,
437 /*low_4gb*/false,
438 dex_locations_[i].c_str(),
439 &error_msg));
440 if (mmap == nullptr) {
441 LOG(ERROR) << "MemMap failed for '" << dex_locations_[i] << "' " << error_msg;
442 return false;
443 }
444 const DexFileLoader dex_file_loader;
445 std::vector<std::unique_ptr<const DexFile>> dex_files_for_location;
446 if (!dex_file_loader.OpenAll(mmap->Begin(),
447 mmap->Size(),
448 dex_locations_[i],
449 /* verify */ false,
450 kVerifyChecksum,
451 &error_msg,
452 &dex_files_for_location)) {
453 LOG(ERROR) << "OpenAll failed for '" << dex_locations_[i] << "' " << error_msg;
454 return false;
455 }
David Sehr7c80f2d2017-02-07 16:47:58 -0800456 for (std::unique_ptr<const DexFile>& dex_file : dex_files_for_location) {
Calin Juravlee10c1e22018-01-26 20:10:15 -0800457 process_fn(std::move(dex_file));
David Sehr7c80f2d2017-02-07 16:47:58 -0800458 }
David Sehr2b80ed42018-05-08 08:58:15 -0700459 // Leak apk_file and mmap for now.
460 // TODO: close fds, etc.
461 apk_file.release();
462 mmap.release();
David Sehr7c80f2d2017-02-07 16:47:58 -0800463 }
Calin Juravlee10c1e22018-01-26 20:10:15 -0800464 return true;
465 }
466
467 // Get the dex locations from the apk fds.
468 // The methods reads the links from /proc/self/fd/ to find the original apk paths
469 // and puts them in the dex_locations_ vector.
470 bool ComputeDexLocationsFromApkFds() {
471 // We can't use a char array of PATH_MAX size without exceeding the frame size.
472 // So we use a vector as the buffer for the path.
473 std::vector<char> buffer(PATH_MAX, 0);
474 for (size_t i = 0; i < apks_fd_.size(); ++i) {
475 std::string fd_path = "/proc/self/fd/" + std::to_string(apks_fd_[i]);
476 ssize_t len = readlink(fd_path.c_str(), buffer.data(), buffer.size() - 1);
477 if (len == -1) {
478 PLOG(ERROR) << "Could not open path from fd";
479 return false;
480 }
481
482 buffer[len] = '\0';
483 dex_locations_.push_back(buffer.data());
484 }
485 return true;
David Sehr7c80f2d2017-02-07 16:47:58 -0800486 }
487
Mathieu Chartier2f794552017-06-19 10:58:08 -0700488 std::unique_ptr<const ProfileCompilationInfo> LoadProfile(const std::string& filename, int fd) {
489 if (!filename.empty()) {
490 fd = open(filename.c_str(), O_RDWR);
491 if (fd < 0) {
492 LOG(ERROR) << "Cannot open " << filename << strerror(errno);
493 return nullptr;
494 }
495 }
496 std::unique_ptr<ProfileCompilationInfo> info(new ProfileCompilationInfo);
497 if (!info->Load(fd)) {
498 LOG(ERROR) << "Cannot load profile info from fd=" << fd << "\n";
499 return nullptr;
500 }
501 return info;
502 }
503
David Sehrb18991b2017-02-08 20:58:10 -0800504 int DumpOneProfile(const std::string& banner,
505 const std::string& filename,
506 int fd,
507 const std::vector<std::unique_ptr<const DexFile>>* dex_files,
508 std::string* dump) {
Mathieu Chartier2f794552017-06-19 10:58:08 -0700509 std::unique_ptr<const ProfileCompilationInfo> info(LoadProfile(filename, fd));
510 if (info == nullptr) {
511 LOG(ERROR) << "Cannot load profile info from filename=" << filename << " fd=" << fd;
Calin Juravle876f3502016-03-24 16:16:34 +0000512 return -1;
513 }
Mathieu Chartier2f794552017-06-19 10:58:08 -0700514 *dump += banner + "\n" + info->DumpInfo(dex_files) + "\n";
David Sehr4fcdd6d2016-05-24 14:52:31 -0700515 return 0;
516 }
517
518 int DumpProfileInfo() {
David Sehr153da0e2017-02-15 08:54:51 -0800519 // Validate that at least one profile file or reference was specified.
520 if (profile_files_.empty() && profile_files_fd_.empty() &&
521 reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
522 Usage("No profile files or reference profile specified.");
523 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700524 static const char* kEmptyString = "";
David Sehr546d24f2016-06-02 10:46:19 -0700525 static const char* kOrdinaryProfile = "=== profile ===";
526 static const char* kReferenceProfile = "=== reference profile ===";
527
David Sehrb18991b2017-02-08 20:58:10 -0800528 std::vector<std::unique_ptr<const DexFile>> dex_files;
David Sehr7c80f2d2017-02-07 16:47:58 -0800529 OpenApkFilesFromLocations(&dex_files);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700530 std::string dump;
David Sehr4fcdd6d2016-05-24 14:52:31 -0700531 // Dump individual profile files.
532 if (!profile_files_fd_.empty()) {
533 for (int profile_file_fd : profile_files_fd_) {
David Sehr546d24f2016-06-02 10:46:19 -0700534 int ret = DumpOneProfile(kOrdinaryProfile,
535 kEmptyString,
536 profile_file_fd,
537 &dex_files,
538 &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700539 if (ret != 0) {
540 return ret;
541 }
542 }
543 }
544 if (!profile_files_.empty()) {
545 for (const std::string& profile_file : profile_files_) {
David Sehr546d24f2016-06-02 10:46:19 -0700546 int ret = DumpOneProfile(kOrdinaryProfile, profile_file, kInvalidFd, &dex_files, &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700547 if (ret != 0) {
548 return ret;
549 }
550 }
551 }
552 // Dump reference profile file.
553 if (FdIsValid(reference_profile_file_fd_)) {
David Sehr546d24f2016-06-02 10:46:19 -0700554 int ret = DumpOneProfile(kReferenceProfile,
555 kEmptyString,
556 reference_profile_file_fd_,
557 &dex_files,
558 &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700559 if (ret != 0) {
560 return ret;
561 }
562 }
563 if (!reference_profile_file_.empty()) {
David Sehr546d24f2016-06-02 10:46:19 -0700564 int ret = DumpOneProfile(kReferenceProfile,
565 reference_profile_file_,
566 kInvalidFd,
567 &dex_files,
David Sehr4fcdd6d2016-05-24 14:52:31 -0700568 &dump);
569 if (ret != 0) {
570 return ret;
571 }
572 }
573 if (!FdIsValid(dump_output_to_fd_)) {
574 std::cout << dump;
575 } else {
576 unix_file::FdFile out_fd(dump_output_to_fd_, false /*check_usage*/);
577 if (!out_fd.WriteFully(dump.c_str(), dump.length())) {
578 return -1;
579 }
580 }
Calin Juravle876f3502016-03-24 16:16:34 +0000581 return 0;
582 }
583
584 bool ShouldOnlyDumpProfile() {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700585 return dump_only_;
Calin Juravle876f3502016-03-24 16:16:34 +0000586 }
587
Mathieu Chartier34067262017-04-06 13:55:46 -0700588 bool GetClassNamesAndMethods(int fd,
589 std::vector<std::unique_ptr<const DexFile>>* dex_files,
590 std::set<std::string>* out_lines) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800591 ProfileCompilationInfo profile_info;
592 if (!profile_info.Load(fd)) {
593 LOG(ERROR) << "Cannot load profile info";
594 return false;
595 }
Mathieu Chartier34067262017-04-06 13:55:46 -0700596 for (const std::unique_ptr<const DexFile>& dex_file : *dex_files) {
597 std::set<dex::TypeIndex> class_types;
Mathieu Chartierea650f32017-05-24 12:04:13 -0700598 std::set<uint16_t> hot_methods;
599 std::set<uint16_t> startup_methods;
600 std::set<uint16_t> post_startup_methods;
601 std::set<uint16_t> combined_methods;
602 if (profile_info.GetClassesAndMethods(*dex_file.get(),
603 &class_types,
604 &hot_methods,
605 &startup_methods,
606 &post_startup_methods)) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700607 for (const dex::TypeIndex& type_index : class_types) {
608 const DexFile::TypeId& type_id = dex_file->GetTypeId(type_index);
609 out_lines->insert(std::string(dex_file->GetTypeDescriptor(type_id)));
610 }
Mathieu Chartierea650f32017-05-24 12:04:13 -0700611 combined_methods = hot_methods;
612 combined_methods.insert(startup_methods.begin(), startup_methods.end());
613 combined_methods.insert(post_startup_methods.begin(), post_startup_methods.end());
614 for (uint16_t dex_method_idx : combined_methods) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700615 const DexFile::MethodId& id = dex_file->GetMethodId(dex_method_idx);
616 std::string signature_string(dex_file->GetMethodSignature(id).ToString());
617 std::string type_string(dex_file->GetTypeDescriptor(dex_file->GetTypeId(id.class_idx_)));
618 std::string method_name(dex_file->GetMethodName(id));
Mathieu Chartierea650f32017-05-24 12:04:13 -0700619 std::string flags_string;
620 if (hot_methods.find(dex_method_idx) != hot_methods.end()) {
621 flags_string += kMethodFlagStringHot;
622 }
623 if (startup_methods.find(dex_method_idx) != startup_methods.end()) {
624 flags_string += kMethodFlagStringStartup;
625 }
626 if (post_startup_methods.find(dex_method_idx) != post_startup_methods.end()) {
627 flags_string += kMethodFlagStringPostStartup;
628 }
629 out_lines->insert(flags_string +
630 type_string +
631 kMethodSep +
632 method_name +
633 signature_string);
Mathieu Chartier34067262017-04-06 13:55:46 -0700634 }
635 }
636 }
David Sehr7c80f2d2017-02-07 16:47:58 -0800637 return true;
638 }
639
Mathieu Chartier34067262017-04-06 13:55:46 -0700640 bool GetClassNamesAndMethods(const std::string& profile_file,
641 std::vector<std::unique_ptr<const DexFile>>* dex_files,
642 std::set<std::string>* out_lines) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800643 int fd = open(profile_file.c_str(), O_RDONLY);
644 if (!FdIsValid(fd)) {
645 LOG(ERROR) << "Cannot open " << profile_file << strerror(errno);
646 return false;
647 }
Mathieu Chartier34067262017-04-06 13:55:46 -0700648 if (!GetClassNamesAndMethods(fd, dex_files, out_lines)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800649 return false;
650 }
651 if (close(fd) < 0) {
652 PLOG(WARNING) << "Failed to close descriptor";
653 }
654 return true;
655 }
656
Mathieu Chartierea650f32017-05-24 12:04:13 -0700657 int DumpClassesAndMethods() {
David Sehr153da0e2017-02-15 08:54:51 -0800658 // Validate that at least one profile file or reference was specified.
659 if (profile_files_.empty() && profile_files_fd_.empty() &&
660 reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
661 Usage("No profile files or reference profile specified.");
662 }
Calin Juravlee10c1e22018-01-26 20:10:15 -0800663
David Sehr7c80f2d2017-02-07 16:47:58 -0800664 // Open the dex files to get the names for classes.
665 std::vector<std::unique_ptr<const DexFile>> dex_files;
666 OpenApkFilesFromLocations(&dex_files);
667 // Build a vector of class names from individual profile files.
668 std::set<std::string> class_names;
669 if (!profile_files_fd_.empty()) {
670 for (int profile_file_fd : profile_files_fd_) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700671 if (!GetClassNamesAndMethods(profile_file_fd, &dex_files, &class_names)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800672 return -1;
673 }
674 }
675 }
676 if (!profile_files_.empty()) {
677 for (const std::string& profile_file : profile_files_) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700678 if (!GetClassNamesAndMethods(profile_file, &dex_files, &class_names)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800679 return -1;
680 }
681 }
682 }
683 // Concatenate class names from reference profile file.
684 if (FdIsValid(reference_profile_file_fd_)) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700685 if (!GetClassNamesAndMethods(reference_profile_file_fd_, &dex_files, &class_names)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800686 return -1;
687 }
688 }
689 if (!reference_profile_file_.empty()) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700690 if (!GetClassNamesAndMethods(reference_profile_file_, &dex_files, &class_names)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800691 return -1;
692 }
693 }
694 // Dump the class names.
695 std::string dump;
696 for (const std::string& class_name : class_names) {
697 dump += class_name + std::string("\n");
698 }
699 if (!FdIsValid(dump_output_to_fd_)) {
700 std::cout << dump;
701 } else {
702 unix_file::FdFile out_fd(dump_output_to_fd_, false /*check_usage*/);
703 if (!out_fd.WriteFully(dump.c_str(), dump.length())) {
704 return -1;
705 }
706 }
707 return 0;
708 }
709
Mathieu Chartier34067262017-04-06 13:55:46 -0700710 bool ShouldOnlyDumpClassesAndMethods() {
711 return dump_classes_and_methods_;
David Sehr7c80f2d2017-02-07 16:47:58 -0800712 }
713
714 // Read lines from the given file, dropping comments and empty lines. Post-process each line with
715 // the given function.
716 template <typename T>
717 static T* ReadCommentedInputFromFile(
718 const char* input_filename, std::function<std::string(const char*)>* process) {
719 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
720 if (input_file.get() == nullptr) {
721 LOG(ERROR) << "Failed to open input file " << input_filename;
722 return nullptr;
723 }
724 std::unique_ptr<T> result(
725 ReadCommentedInputStream<T>(*input_file, process));
726 input_file->close();
727 return result.release();
728 }
729
730 // Read lines from the given stream, dropping comments and empty lines. Post-process each line
731 // with the given function.
732 template <typename T>
733 static T* ReadCommentedInputStream(
734 std::istream& in_stream,
735 std::function<std::string(const char*)>* process) {
736 std::unique_ptr<T> output(new T());
737 while (in_stream.good()) {
738 std::string dot;
739 std::getline(in_stream, dot);
740 if (android::base::StartsWith(dot, "#") || dot.empty()) {
741 continue;
742 }
743 if (process != nullptr) {
744 std::string descriptor((*process)(dot.c_str()));
745 output->insert(output->end(), descriptor);
746 } else {
747 output->insert(output->end(), dot);
748 }
749 }
750 return output.release();
751 }
752
Calin Juravlee0ac1152017-02-13 19:03:47 -0800753 // Find class klass_descriptor in the given dex_files and store its reference
754 // in the out parameter class_ref.
755 // Return true if the definition of the class was found in any of the dex_files.
756 bool FindClass(const std::vector<std::unique_ptr<const DexFile>>& dex_files,
757 const std::string& klass_descriptor,
Mathieu Chartierdbddc222017-05-24 12:04:13 -0700758 /*out*/TypeReference* class_ref) {
Calin Juravle08556882017-05-26 16:40:45 -0700759 constexpr uint16_t kInvalidTypeIndex = std::numeric_limits<uint16_t>::max() - 1;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800760 for (const std::unique_ptr<const DexFile>& dex_file_ptr : dex_files) {
761 const DexFile* dex_file = dex_file_ptr.get();
Calin Juravle08556882017-05-26 16:40:45 -0700762 if (klass_descriptor == kInvalidClassDescriptor) {
763 if (kInvalidTypeIndex >= dex_file->NumTypeIds()) {
764 // The dex file does not contain all possible type ids which leaves us room
765 // to add an "invalid" type id.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700766 *class_ref = TypeReference(dex_file, dex::TypeIndex(kInvalidTypeIndex));
Calin Juravle08556882017-05-26 16:40:45 -0700767 return true;
768 } else {
769 // The dex file contains all possible type ids. We don't have any free type id
770 // that we can use as invalid.
771 continue;
772 }
773 }
774
Calin Juravlee0ac1152017-02-13 19:03:47 -0800775 const DexFile::TypeId* type_id = dex_file->FindTypeId(klass_descriptor.c_str());
776 if (type_id == nullptr) {
777 continue;
778 }
779 dex::TypeIndex type_index = dex_file->GetIndexForTypeId(*type_id);
780 if (dex_file->FindClassDef(type_index) == nullptr) {
781 // Class is only referenced in the current dex file but not defined in it.
782 continue;
783 }
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700784 *class_ref = TypeReference(dex_file, type_index);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800785 return true;
786 }
787 return false;
788 }
789
Mathieu Chartier34067262017-04-06 13:55:46 -0700790 // Find the method specified by method_spec in the class class_ref.
Calin Juravle08556882017-05-26 16:40:45 -0700791 uint32_t FindMethodIndex(const TypeReference& class_ref,
792 const std::string& method_spec) {
793 const DexFile* dex_file = class_ref.dex_file;
794 if (method_spec == kInvalidMethod) {
795 constexpr uint16_t kInvalidMethodIndex = std::numeric_limits<uint16_t>::max() - 1;
796 return kInvalidMethodIndex >= dex_file->NumMethodIds()
797 ? kInvalidMethodIndex
Andreas Gampee2abbc62017-09-15 11:59:26 -0700798 : dex::kDexNoIndex;
Calin Juravle08556882017-05-26 16:40:45 -0700799 }
800
Calin Juravlee0ac1152017-02-13 19:03:47 -0800801 std::vector<std::string> name_and_signature;
802 Split(method_spec, kProfileParsingFirstCharInSignature, &name_and_signature);
803 if (name_and_signature.size() != 2) {
804 LOG(ERROR) << "Invalid method name and signature " << method_spec;
Andreas Gampee2abbc62017-09-15 11:59:26 -0700805 return dex::kDexNoIndex;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800806 }
Calin Juravle08556882017-05-26 16:40:45 -0700807
Calin Juravlee0ac1152017-02-13 19:03:47 -0800808 const std::string& name = name_and_signature[0];
809 const std::string& signature = kProfileParsingFirstCharInSignature + name_and_signature[1];
Calin Juravlee0ac1152017-02-13 19:03:47 -0800810
811 const DexFile::StringId* name_id = dex_file->FindStringId(name.c_str());
812 if (name_id == nullptr) {
Mathieu Chartier66aae3b2017-05-18 10:38:28 -0700813 LOG(WARNING) << "Could not find name: " << name;
Andreas Gampee2abbc62017-09-15 11:59:26 -0700814 return dex::kDexNoIndex;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800815 }
816 dex::TypeIndex return_type_idx;
817 std::vector<dex::TypeIndex> param_type_idxs;
818 if (!dex_file->CreateTypeList(signature, &return_type_idx, &param_type_idxs)) {
Mathieu Chartier66aae3b2017-05-18 10:38:28 -0700819 LOG(WARNING) << "Could not create type list" << signature;
Andreas Gampee2abbc62017-09-15 11:59:26 -0700820 return dex::kDexNoIndex;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800821 }
822 const DexFile::ProtoId* proto_id = dex_file->FindProtoId(return_type_idx, param_type_idxs);
823 if (proto_id == nullptr) {
Mathieu Chartier66aae3b2017-05-18 10:38:28 -0700824 LOG(WARNING) << "Could not find proto_id: " << name;
Andreas Gampee2abbc62017-09-15 11:59:26 -0700825 return dex::kDexNoIndex;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800826 }
827 const DexFile::MethodId* method_id = dex_file->FindMethodId(
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700828 dex_file->GetTypeId(class_ref.TypeIndex()), *name_id, *proto_id);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800829 if (method_id == nullptr) {
Mathieu Chartier66aae3b2017-05-18 10:38:28 -0700830 LOG(WARNING) << "Could not find method_id: " << name;
Andreas Gampee2abbc62017-09-15 11:59:26 -0700831 return dex::kDexNoIndex;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800832 }
833
Mathieu Chartier34067262017-04-06 13:55:46 -0700834 return dex_file->GetIndexForMethodId(*method_id);
835 }
Calin Juravlee0ac1152017-02-13 19:03:47 -0800836
Mathieu Chartier34067262017-04-06 13:55:46 -0700837 // Given a method, return true if the method has a single INVOKE_VIRTUAL in its byte code.
838 // Upon success it returns true and stores the method index and the invoke dex pc
839 // in the output parameters.
840 // The format of the method spec is "inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;".
841 //
842 // TODO(calin): support INVOKE_INTERFACE and the range variants.
Mathieu Chartierdbddc222017-05-24 12:04:13 -0700843 bool HasSingleInvoke(const TypeReference& class_ref,
Mathieu Chartier34067262017-04-06 13:55:46 -0700844 uint16_t method_index,
845 /*out*/uint32_t* dex_pc) {
846 const DexFile* dex_file = class_ref.dex_file;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800847 uint32_t offset = dex_file->FindCodeItemOffset(
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700848 *dex_file->FindClassDef(class_ref.TypeIndex()),
Mathieu Chartier34067262017-04-06 13:55:46 -0700849 method_index);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800850 const DexFile::CodeItem* code_item = dex_file->GetCodeItem(offset);
851
852 bool found_invoke = false;
Mathieu Chartier698ebbc2018-01-05 11:00:42 -0800853 for (const DexInstructionPcPair& inst : CodeItemInstructionAccessor(*dex_file, code_item)) {
Rico Windc24fa5d2018-04-25 12:44:58 +0200854 if (inst->Opcode() == Instruction::INVOKE_VIRTUAL ||
855 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800856 if (found_invoke) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700857 LOG(ERROR) << "Multiple invoke INVOKE_VIRTUAL found: "
858 << dex_file->PrettyMethod(method_index);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800859 return false;
860 }
861 found_invoke = true;
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800862 *dex_pc = inst.DexPc();
Calin Juravlee0ac1152017-02-13 19:03:47 -0800863 }
864 }
865 if (!found_invoke) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700866 LOG(ERROR) << "Could not find any INVOKE_VIRTUAL: " << dex_file->PrettyMethod(method_index);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800867 }
868 return found_invoke;
869 }
870
871 // Process a line defining a class or a method and its inline caches.
872 // Upon success return true and add the class or the method info to profile.
Calin Juravle589e71e2017-03-03 16:05:05 -0800873 // The possible line formats are:
874 // "LJustTheCass;".
Calin Juravlee0ac1152017-02-13 19:03:47 -0800875 // "LTestInline;->inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;".
Calin Juravle08556882017-05-26 16:40:45 -0700876 // "LTestInline;->inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,invalid_class".
Calin Juravle589e71e2017-03-03 16:05:05 -0800877 // "LTestInline;->inlineMissingTypes(LSuper;)I+missing_types".
878 // "LTestInline;->inlineNoInlineCaches(LSuper;)I".
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700879 // "LTestInline;->*".
Calin Juravle08556882017-05-26 16:40:45 -0700880 // "invalid_class".
881 // "LTestInline;->invalid_method".
Calin Juravlee0ac1152017-02-13 19:03:47 -0800882 // The method and classes are searched only in the given dex files.
883 bool ProcessLine(const std::vector<std::unique_ptr<const DexFile>>& dex_files,
884 const std::string& line,
885 /*out*/ProfileCompilationInfo* profile) {
886 std::string klass;
887 std::string method_str;
Mathieu Chartierea650f32017-05-24 12:04:13 -0700888 bool is_hot = false;
889 bool is_startup = false;
890 bool is_post_startup = false;
891 const size_t method_sep_index = line.find(kMethodSep, 0);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800892 if (method_sep_index == std::string::npos) {
Mathieu Chartierea650f32017-05-24 12:04:13 -0700893 klass = line.substr(0);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800894 } else {
Mathieu Chartierea650f32017-05-24 12:04:13 -0700895 // The method prefix flags are only valid for method strings.
896 size_t start_index = 0;
897 while (start_index < line.size() && line[start_index] != 'L') {
898 const char c = line[start_index];
899 if (c == kMethodFlagStringHot) {
900 is_hot = true;
901 } else if (c == kMethodFlagStringStartup) {
902 is_startup = true;
903 } else if (c == kMethodFlagStringPostStartup) {
904 is_post_startup = true;
905 } else {
906 LOG(WARNING) << "Invalid flag " << c;
907 return false;
908 }
909 ++start_index;
910 }
911 klass = line.substr(start_index, method_sep_index - start_index);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800912 method_str = line.substr(method_sep_index + kMethodSep.size());
913 }
914
Calin Juravleee9cb412018-02-13 20:32:35 -0800915 uint32_t flags = 0;
916 if (is_hot) {
917 flags |= ProfileCompilationInfo::MethodHotness::kFlagHot;
918 }
919 if (is_startup) {
920 flags |= ProfileCompilationInfo::MethodHotness::kFlagStartup;
921 }
922 if (is_post_startup) {
923 flags |= ProfileCompilationInfo::MethodHotness::kFlagPostStartup;
924 }
925
Vladimir Markof3c52b42017-11-17 17:32:12 +0000926 TypeReference class_ref(/* dex_file */ nullptr, dex::TypeIndex());
Calin Juravlee0ac1152017-02-13 19:03:47 -0800927 if (!FindClass(dex_files, klass, &class_ref)) {
Mathieu Chartier3f121342017-03-06 11:16:20 -0800928 LOG(WARNING) << "Could not find class: " << klass;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800929 return false;
930 }
931
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700932 if (method_str.empty() || method_str == kClassAllMethods) {
933 // Start by adding the class.
Calin Juravlee0ac1152017-02-13 19:03:47 -0800934 std::set<DexCacheResolvedClasses> resolved_class_set;
935 const DexFile* dex_file = class_ref.dex_file;
936 const auto& dex_resolved_classes = resolved_class_set.emplace(
937 dex_file->GetLocation(),
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700938 DexFileLoader::GetBaseLocation(dex_file->GetLocation()),
Mathieu Chartierea650f32017-05-24 12:04:13 -0700939 dex_file->GetLocationChecksum(),
940 dex_file->NumMethodIds());
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700941 dex_resolved_classes.first->AddClass(class_ref.TypeIndex());
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700942 std::vector<ProfileMethodInfo> methods;
943 if (method_str == kClassAllMethods) {
944 // Add all of the methods.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700945 const DexFile::ClassDef* class_def = dex_file->FindClassDef(class_ref.TypeIndex());
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700946 const uint8_t* class_data = dex_file->GetClassData(*class_def);
947 if (class_data != nullptr) {
948 ClassDataItemIterator it(*dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700949 it.SkipAllFields();
Mathieu Chartierb7c273c2017-11-10 18:07:56 -0800950 while (it.HasNextMethod()) {
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700951 if (it.GetMethodCodeItemOffset() != 0) {
952 // Add all of the methods that have code to the profile.
953 const uint32_t method_idx = it.GetMemberIndex();
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700954 methods.push_back(ProfileMethodInfo(MethodReference(dex_file, method_idx)));
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700955 }
956 it.Next();
957 }
958 }
959 }
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700960 // TODO: Check return values?
Calin Juravleee9cb412018-02-13 20:32:35 -0800961 profile->AddMethods(methods, static_cast<ProfileCompilationInfo::MethodHotness::Flag>(flags));
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700962 profile->AddClasses(resolved_class_set);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800963 return true;
964 }
965
966 // Process the method.
967 std::string method_spec;
968 std::vector<std::string> inline_cache_elems;
969
Mathieu Chartierea650f32017-05-24 12:04:13 -0700970 // If none of the flags are set, default to hot.
971 is_hot = is_hot || (!is_hot && !is_startup && !is_post_startup);
972
Calin Juravlee0ac1152017-02-13 19:03:47 -0800973 std::vector<std::string> method_elems;
Calin Juravle589e71e2017-03-03 16:05:05 -0800974 bool is_missing_types = false;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800975 Split(method_str, kProfileParsingInlineChacheSep, &method_elems);
976 if (method_elems.size() == 2) {
977 method_spec = method_elems[0];
Calin Juravle589e71e2017-03-03 16:05:05 -0800978 is_missing_types = method_elems[1] == kMissingTypesMarker;
979 if (!is_missing_types) {
980 Split(method_elems[1], kProfileParsingTypeSep, &inline_cache_elems);
981 }
Calin Juravlee0ac1152017-02-13 19:03:47 -0800982 } else if (method_elems.size() == 1) {
983 method_spec = method_elems[0];
984 } else {
985 LOG(ERROR) << "Invalid method line: " << line;
986 return false;
987 }
988
Mathieu Chartier34067262017-04-06 13:55:46 -0700989 const uint32_t method_index = FindMethodIndex(class_ref, method_spec);
Andreas Gampee2abbc62017-09-15 11:59:26 -0700990 if (method_index == dex::kDexNoIndex) {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800991 return false;
992 }
Mathieu Chartier34067262017-04-06 13:55:46 -0700993
Mathieu Chartier34067262017-04-06 13:55:46 -0700994 std::vector<ProfileMethodInfo::ProfileInlineCache> inline_caches;
995 if (is_missing_types || !inline_cache_elems.empty()) {
996 uint32_t dex_pc;
997 if (!HasSingleInvoke(class_ref, method_index, &dex_pc)) {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800998 return false;
999 }
Vladimir Markof3c52b42017-11-17 17:32:12 +00001000 std::vector<TypeReference> classes(inline_cache_elems.size(),
1001 TypeReference(/* dex_file */ nullptr, dex::TypeIndex()));
Mathieu Chartier34067262017-04-06 13:55:46 -07001002 size_t class_it = 0;
1003 for (const std::string& ic_class : inline_cache_elems) {
1004 if (!FindClass(dex_files, ic_class, &(classes[class_it++]))) {
1005 LOG(ERROR) << "Could not find class: " << ic_class;
1006 return false;
1007 }
1008 }
1009 inline_caches.emplace_back(dex_pc, is_missing_types, classes);
Calin Juravlee0ac1152017-02-13 19:03:47 -08001010 }
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -07001011 MethodReference ref(class_ref.dex_file, method_index);
Mathieu Chartierea650f32017-05-24 12:04:13 -07001012 if (is_hot) {
Calin Juravleee9cb412018-02-13 20:32:35 -08001013 profile->AddMethod(ProfileMethodInfo(ref, inline_caches),
1014 static_cast<ProfileCompilationInfo::MethodHotness::Flag>(flags));
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -07001015 }
1016 if (flags != 0) {
Calin Juravleee9cb412018-02-13 20:32:35 -08001017 if (!profile->AddMethodIndex(
1018 static_cast<ProfileCompilationInfo::MethodHotness::Flag>(flags), ref)) {
Mathieu Chartierea650f32017-05-24 12:04:13 -07001019 return false;
1020 }
Mathieu Chartiere46f3a82017-06-19 19:54:12 -07001021 DCHECK(profile->GetMethodHotness(ref).IsInProfile());
Mathieu Chartierea650f32017-05-24 12:04:13 -07001022 }
Calin Juravlee0ac1152017-02-13 19:03:47 -08001023 return true;
1024 }
1025
Mathieu Chartier2f794552017-06-19 10:58:08 -07001026 int OpenReferenceProfile() const {
1027 int fd = reference_profile_file_fd_;
1028 if (!FdIsValid(fd)) {
1029 CHECK(!reference_profile_file_.empty());
1030 fd = open(reference_profile_file_.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
1031 if (fd < 0) {
1032 LOG(ERROR) << "Cannot open " << reference_profile_file_ << strerror(errno);
1033 return kInvalidFd;
1034 }
1035 }
1036 return fd;
1037 }
1038
Calin Juravlee0ac1152017-02-13 19:03:47 -08001039 // Creates a profile from a human friendly textual representation.
1040 // The expected input format is:
1041 // # Classes
1042 // Ljava/lang/Comparable;
1043 // Ljava/lang/Math;
1044 // # Methods with inline caches
1045 // LTestInline;->inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;
1046 // LTestInline;->noInlineCache(LSuper;)I
David Sehr7c80f2d2017-02-07 16:47:58 -08001047 int CreateProfile() {
David Sehr153da0e2017-02-15 08:54:51 -08001048 // Validate parameters for this command.
1049 if (apk_files_.empty() && apks_fd_.empty()) {
1050 Usage("APK files must be specified");
1051 }
1052 if (dex_locations_.empty()) {
1053 Usage("DEX locations must be specified");
1054 }
1055 if (reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
1056 Usage("Reference profile must be specified with --reference-profile-file or "
1057 "--reference-profile-file-fd");
1058 }
1059 if (!profile_files_.empty() || !profile_files_fd_.empty()) {
1060 Usage("Profile must be specified with --reference-profile-file or "
1061 "--reference-profile-file-fd");
1062 }
David Sehr7c80f2d2017-02-07 16:47:58 -08001063 // Open the profile output file if needed.
Mathieu Chartier2f794552017-06-19 10:58:08 -07001064 int fd = OpenReferenceProfile();
David Sehr7c80f2d2017-02-07 16:47:58 -08001065 if (!FdIsValid(fd)) {
David Sehr7c80f2d2017-02-07 16:47:58 -08001066 return -1;
David Sehr7c80f2d2017-02-07 16:47:58 -08001067 }
Calin Juravlee0ac1152017-02-13 19:03:47 -08001068 // Read the user-specified list of classes and methods.
David Sehr7c80f2d2017-02-07 16:47:58 -08001069 std::unique_ptr<std::unordered_set<std::string>>
Calin Juravlee0ac1152017-02-13 19:03:47 -08001070 user_lines(ReadCommentedInputFromFile<std::unordered_set<std::string>>(
David Sehr7c80f2d2017-02-07 16:47:58 -08001071 create_profile_from_file_.c_str(), nullptr)); // No post-processing.
Calin Juravlee0ac1152017-02-13 19:03:47 -08001072
1073 // Open the dex files to look up classes and methods.
David Sehr7c80f2d2017-02-07 16:47:58 -08001074 std::vector<std::unique_ptr<const DexFile>> dex_files;
1075 OpenApkFilesFromLocations(&dex_files);
Calin Juravlee0ac1152017-02-13 19:03:47 -08001076
1077 // Process the lines one by one and add the successful ones to the profile.
David Sehr7c80f2d2017-02-07 16:47:58 -08001078 ProfileCompilationInfo info;
Calin Juravlee0ac1152017-02-13 19:03:47 -08001079
1080 for (const auto& line : *user_lines) {
1081 ProcessLine(dex_files, line, &info);
1082 }
1083
David Sehr7c80f2d2017-02-07 16:47:58 -08001084 // Write the profile file.
1085 CHECK(info.Save(fd));
1086 if (close(fd) < 0) {
1087 PLOG(WARNING) << "Failed to close descriptor";
1088 }
1089 return 0;
1090 }
1091
Mathieu Chartier2f794552017-06-19 10:58:08 -07001092 bool ShouldCreateBootProfile() const {
1093 return generate_boot_image_profile_;
1094 }
1095
1096 int CreateBootProfile() {
Mathieu Chartier2f794552017-06-19 10:58:08 -07001097 // Open the profile output file.
1098 const int reference_fd = OpenReferenceProfile();
1099 if (!FdIsValid(reference_fd)) {
1100 PLOG(ERROR) << "Error opening reference profile";
1101 return -1;
1102 }
1103 // Open the dex files.
1104 std::vector<std::unique_ptr<const DexFile>> dex_files;
1105 OpenApkFilesFromLocations(&dex_files);
1106 if (dex_files.empty()) {
1107 PLOG(ERROR) << "Expected dex files for creating boot profile";
1108 return -2;
1109 }
1110 // Open the input profiles.
1111 std::vector<std::unique_ptr<const ProfileCompilationInfo>> profiles;
1112 if (!profile_files_fd_.empty()) {
1113 for (int profile_file_fd : profile_files_fd_) {
1114 std::unique_ptr<const ProfileCompilationInfo> profile(LoadProfile("", profile_file_fd));
1115 if (profile == nullptr) {
1116 return -3;
1117 }
1118 profiles.emplace_back(std::move(profile));
1119 }
1120 }
1121 if (!profile_files_.empty()) {
1122 for (const std::string& profile_file : profile_files_) {
1123 std::unique_ptr<const ProfileCompilationInfo> profile(LoadProfile(profile_file, kInvalidFd));
1124 if (profile == nullptr) {
1125 return -4;
1126 }
1127 profiles.emplace_back(std::move(profile));
1128 }
1129 }
1130 ProfileCompilationInfo out_profile;
1131 GenerateBootImageProfile(dex_files,
1132 profiles,
1133 boot_image_options_,
1134 VLOG_IS_ON(profiler),
1135 &out_profile);
1136 out_profile.Save(reference_fd);
1137 close(reference_fd);
1138 return 0;
1139 }
1140
David Sehr7c80f2d2017-02-07 16:47:58 -08001141 bool ShouldCreateProfile() {
1142 return !create_profile_from_file_.empty();
1143 }
1144
Calin Juravle7bcdb532016-06-07 16:14:47 +01001145 int GenerateTestProfile() {
David Sehr153da0e2017-02-15 08:54:51 -08001146 // Validate parameters for this command.
Shubham Ajmerad704f0b2017-07-26 16:33:35 -07001147 if (test_profile_method_percerntage_ > 100) {
1148 Usage("Invalid percentage for --generate-test-profile-method-percentage");
David Sehr153da0e2017-02-15 08:54:51 -08001149 }
Shubham Ajmerad704f0b2017-07-26 16:33:35 -07001150 if (test_profile_class_percentage_ > 100) {
1151 Usage("Invalid percentage for --generate-test-profile-class-percentage");
David Sehr153da0e2017-02-15 08:54:51 -08001152 }
Jeff Haof0a31f82017-03-27 15:50:37 -07001153 // If given APK files or DEX locations, check that they're ok.
1154 if (!apk_files_.empty() || !apks_fd_.empty() || !dex_locations_.empty()) {
1155 if (apk_files_.empty() && apks_fd_.empty()) {
1156 Usage("APK files must be specified when passing DEX locations to --generate-test-profile");
1157 }
1158 if (dex_locations_.empty()) {
1159 Usage("DEX locations must be specified when passing APK files to --generate-test-profile");
1160 }
1161 }
David Sehr153da0e2017-02-15 08:54:51 -08001162 // ShouldGenerateTestProfile confirms !test_profile_.empty().
George Burgess IV71f77262016-10-25 13:08:17 -07001163 int profile_test_fd = open(test_profile_.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
Calin Juravle7bcdb532016-06-07 16:14:47 +01001164 if (profile_test_fd < 0) {
David Sehr7c80f2d2017-02-07 16:47:58 -08001165 LOG(ERROR) << "Cannot open " << test_profile_ << strerror(errno);
Calin Juravle7bcdb532016-06-07 16:14:47 +01001166 return -1;
1167 }
Jeff Haof0a31f82017-03-27 15:50:37 -07001168 bool result;
1169 if (apk_files_.empty() && apks_fd_.empty() && dex_locations_.empty()) {
1170 result = ProfileCompilationInfo::GenerateTestProfile(profile_test_fd,
1171 test_profile_num_dex_,
Shubham Ajmerad704f0b2017-07-26 16:33:35 -07001172 test_profile_method_percerntage_,
1173 test_profile_class_percentage_,
Jeff Haof0a31f82017-03-27 15:50:37 -07001174 test_profile_seed_);
1175 } else {
Jeff Haof0a31f82017-03-27 15:50:37 -07001176 // Open the dex files to look up classes and methods.
1177 std::vector<std::unique_ptr<const DexFile>> dex_files;
1178 OpenApkFilesFromLocations(&dex_files);
1179 // Create a random profile file based on the set of dex files.
1180 result = ProfileCompilationInfo::GenerateTestProfile(profile_test_fd,
1181 dex_files,
Shubham Ajmerad704f0b2017-07-26 16:33:35 -07001182 test_profile_method_percerntage_,
1183 test_profile_class_percentage_,
Jeff Haof0a31f82017-03-27 15:50:37 -07001184 test_profile_seed_);
1185 }
Calin Juravle7bcdb532016-06-07 16:14:47 +01001186 close(profile_test_fd); // ignore close result.
1187 return result ? 0 : -1;
1188 }
1189
1190 bool ShouldGenerateTestProfile() {
1191 return !test_profile_.empty();
1192 }
1193
Calin Juravle2dba0ab2018-01-22 19:22:24 -08001194 bool ShouldCopyAndUpdateProfileKey() const {
1195 return copy_and_update_profile_key_;
1196 }
1197
Calin Juravle02c08792018-02-15 19:40:48 -08001198 int32_t CopyAndUpdateProfileKey() {
Calin Juravle2dba0ab2018-01-22 19:22:24 -08001199 // Validate that at least one profile file was passed, as well as a reference profile.
1200 if (!(profile_files_.size() == 1 ^ profile_files_fd_.size() == 1)) {
1201 Usage("Only one profile file should be specified.");
1202 }
1203 if (reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
1204 Usage("No reference profile file specified.");
1205 }
1206
1207 if (apk_files_.empty() && apks_fd_.empty()) {
1208 Usage("No apk files specified");
1209 }
1210
Calin Juravle02c08792018-02-15 19:40:48 -08001211 static constexpr int32_t kErrorFailedToUpdateProfile = -1;
1212 static constexpr int32_t kErrorFailedToSaveProfile = -2;
1213 static constexpr int32_t kErrorFailedToLoadProfile = -3;
1214
Calin Juravle2dba0ab2018-01-22 19:22:24 -08001215 bool use_fds = profile_files_fd_.size() == 1;
1216
1217 ProfileCompilationInfo profile;
1218 // Do not clear if invalid. The input might be an archive.
Calin Juravle02c08792018-02-15 19:40:48 -08001219 bool load_ok = use_fds
1220 ? profile.Load(profile_files_fd_[0])
1221 : profile.Load(profile_files_[0], /*clear_if_invalid*/ false);
1222 if (load_ok) {
Calin Juravle2dba0ab2018-01-22 19:22:24 -08001223 // Open the dex files to look up classes and methods.
1224 std::vector<std::unique_ptr<const DexFile>> dex_files;
1225 OpenApkFilesFromLocations(&dex_files);
1226 if (!profile.UpdateProfileKeys(dex_files)) {
Calin Juravle02c08792018-02-15 19:40:48 -08001227 return kErrorFailedToUpdateProfile;
Calin Juravle2dba0ab2018-01-22 19:22:24 -08001228 }
Calin Juravle02c08792018-02-15 19:40:48 -08001229 bool result = use_fds
1230 ? profile.Save(reference_profile_file_fd_)
1231 : profile.Save(reference_profile_file_, /*bytes_written*/ nullptr);
1232 return result ? 0 : kErrorFailedToSaveProfile;
Calin Juravle2dba0ab2018-01-22 19:22:24 -08001233 } else {
Calin Juravle02c08792018-02-15 19:40:48 -08001234 return kErrorFailedToLoadProfile;
Calin Juravle2dba0ab2018-01-22 19:22:24 -08001235 }
1236 }
1237
Calin Juravle2e2db782016-02-23 12:00:03 +00001238 private:
1239 static void ParseFdForCollection(const StringPiece& option,
1240 const char* arg_name,
1241 std::vector<int>* fds) {
1242 int fd;
1243 ParseUintOption(option, arg_name, &fd, Usage);
1244 fds->push_back(fd);
1245 }
1246
1247 static void CloseAllFds(const std::vector<int>& fds, const char* descriptor) {
1248 for (size_t i = 0; i < fds.size(); i++) {
1249 if (close(fds[i]) < 0) {
Calin Juravlee10c1e22018-01-26 20:10:15 -08001250 PLOG(WARNING) << "Failed to close descriptor for "
1251 << descriptor << " at index " << i << ": " << fds[i];
Calin Juravle2e2db782016-02-23 12:00:03 +00001252 }
1253 }
1254 }
1255
1256 void LogCompletionTime() {
David Sehr52683cf2016-05-05 09:02:38 -07001257 static constexpr uint64_t kLogThresholdTime = MsToNs(100); // 100ms
1258 uint64_t time_taken = NanoTime() - start_ns_;
David Sehred793012016-05-05 13:39:43 -07001259 if (time_taken > kLogThresholdTime) {
David Sehrd8557182016-05-06 12:29:35 -07001260 LOG(WARNING) << "profman took " << PrettyDuration(time_taken);
David Sehred793012016-05-05 13:39:43 -07001261 }
Calin Juravle2e2db782016-02-23 12:00:03 +00001262 }
1263
1264 std::vector<std::string> profile_files_;
1265 std::vector<int> profile_files_fd_;
David Sehr546d24f2016-06-02 10:46:19 -07001266 std::vector<std::string> dex_locations_;
David Sehr7c80f2d2017-02-07 16:47:58 -08001267 std::vector<std::string> apk_files_;
David Sehr546d24f2016-06-02 10:46:19 -07001268 std::vector<int> apks_fd_;
Calin Juravle2e2db782016-02-23 12:00:03 +00001269 std::string reference_profile_file_;
1270 int reference_profile_file_fd_;
David Sehr4fcdd6d2016-05-24 14:52:31 -07001271 bool dump_only_;
Mathieu Chartier34067262017-04-06 13:55:46 -07001272 bool dump_classes_and_methods_;
Mathieu Chartier2f794552017-06-19 10:58:08 -07001273 bool generate_boot_image_profile_;
David Sehr4fcdd6d2016-05-24 14:52:31 -07001274 int dump_output_to_fd_;
Mathieu Chartier2f794552017-06-19 10:58:08 -07001275 BootImageOptions boot_image_options_;
Calin Juravle7bcdb532016-06-07 16:14:47 +01001276 std::string test_profile_;
David Sehr7c80f2d2017-02-07 16:47:58 -08001277 std::string create_profile_from_file_;
Calin Juravle7bcdb532016-06-07 16:14:47 +01001278 uint16_t test_profile_num_dex_;
Shubham Ajmerad704f0b2017-07-26 16:33:35 -07001279 uint16_t test_profile_method_percerntage_;
1280 uint16_t test_profile_class_percentage_;
Jeff Haof0a31f82017-03-27 15:50:37 -07001281 uint32_t test_profile_seed_;
Calin Juravle2e2db782016-02-23 12:00:03 +00001282 uint64_t start_ns_;
Calin Juravle2dba0ab2018-01-22 19:22:24 -08001283 bool copy_and_update_profile_key_;
Calin Juravle2e2db782016-02-23 12:00:03 +00001284};
1285
1286// See ProfileAssistant::ProcessingResult for return codes.
1287static int profman(int argc, char** argv) {
1288 ProfMan profman;
1289
1290 // Parse arguments. Argument mistakes will lead to exit(EXIT_FAILURE) in UsageError.
1291 profman.ParseArgs(argc, argv);
1292
Calin Juravlee10c1e22018-01-26 20:10:15 -08001293 // Initialize MemMap for ZipArchive::OpenFromFd.
1294 MemMap::Init();
1295
Calin Juravle7bcdb532016-06-07 16:14:47 +01001296 if (profman.ShouldGenerateTestProfile()) {
1297 return profman.GenerateTestProfile();
1298 }
Calin Juravle876f3502016-03-24 16:16:34 +00001299 if (profman.ShouldOnlyDumpProfile()) {
1300 return profman.DumpProfileInfo();
1301 }
Mathieu Chartier34067262017-04-06 13:55:46 -07001302 if (profman.ShouldOnlyDumpClassesAndMethods()) {
Mathieu Chartierea650f32017-05-24 12:04:13 -07001303 return profman.DumpClassesAndMethods();
David Sehr7c80f2d2017-02-07 16:47:58 -08001304 }
1305 if (profman.ShouldCreateProfile()) {
1306 return profman.CreateProfile();
1307 }
Mathieu Chartier2f794552017-06-19 10:58:08 -07001308
1309 if (profman.ShouldCreateBootProfile()) {
1310 return profman.CreateBootProfile();
1311 }
Calin Juravle02c08792018-02-15 19:40:48 -08001312
1313 if (profman.ShouldCopyAndUpdateProfileKey()) {
1314 return profman.CopyAndUpdateProfileKey();
1315 }
1316
Calin Juravle2e2db782016-02-23 12:00:03 +00001317 // Process profile information and assess if we need to do a profile guided compilation.
1318 // This operation involves I/O.
1319 return profman.ProcessProfiles();
1320}
1321
1322} // namespace art
1323
1324int main(int argc, char **argv) {
1325 return art::profman(argc, argv);
1326}