blob: d989c8c849e574481499a71a31ce2c094fbef787 [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>
Calin Juravlee10c1e22018-01-26 20:10:15 -080021#include <sys/param.h>
Calin Juravle2e2db782016-02-23 12:00:03 +000022#include <unistd.h>
23
David Sehr7c80f2d2017-02-07 16:47:58 -080024#include <fstream>
Calin Juravle876f3502016-03-24 16:16:34 +000025#include <iostream>
David Sehr7c80f2d2017-02-07 16:47:58 -080026#include <set>
Calin Juravle2e2db782016-02-23 12:00:03 +000027#include <string>
David Sehr7c80f2d2017-02-07 16:47:58 -080028#include <unordered_set>
Calin Juravle2e2db782016-02-23 12:00:03 +000029#include <vector>
30
Andreas Gampe46ee31b2016-12-14 10:11:49 -080031#include "android-base/stringprintf.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080032#include "android-base/strings.h"
33
Calin Juravle2e2db782016-02-23 12:00:03 +000034#include "base/dumpable.h"
Andreas Gampe57943812017-12-06 21:39:13 -080035#include "base/logging.h" // For InitLogging.
David Sehr671af6c2018-05-17 11:00:35 -070036#include "base/mem_map.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000037#include "base/scoped_flock.h"
Mathieu Chartier0573f852018-10-01 13:52:12 -070038#include "base/stl_util.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000039#include "base/stringpiece.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000040#include "base/time_utils.h"
41#include "base/unix_file/fd_file.h"
David Sehrc431b9d2018-03-02 12:01:51 -080042#include "base/utils.h"
David Sehr79e26072018-04-06 17:58:50 -070043#include "base/zip_archive.h"
Mathieu Chartier2f794552017-06-19 10:58:08 -070044#include "boot_image_profile.h"
Mathieu Chartier818cb802018-05-11 05:30:16 +000045#include "dex/art_dex_file_loader.h"
David Sehr312f3b22018-03-19 08:39:26 -070046#include "dex/bytecode_utils.h"
Mathieu Chartier20f49922018-05-24 16:04:17 -070047#include "dex/class_accessor-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080048#include "dex/code_item_accessors-inl.h"
49#include "dex/dex_file.h"
50#include "dex/dex_file_loader.h"
51#include "dex/dex_file_types.h"
David Sehr312f3b22018-03-19 08:39:26 -070052#include "dex/type_reference.h"
David Sehr82d046e2018-04-23 08:14:19 -070053#include "profile/profile_compilation_info.h"
Mathieu Chartierdbddc222017-05-24 12:04:13 -070054#include "profile_assistant.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000055
56namespace art {
57
58static int original_argc;
59static char** original_argv;
60
61static std::string CommandLine() {
62 std::vector<std::string> command;
63 for (int i = 0; i < original_argc; ++i) {
64 command.push_back(original_argv[i]);
65 }
Andreas Gampe9186ced2016-12-12 14:28:21 -080066 return android::base::Join(command, ' ');
Calin Juravle2e2db782016-02-23 12:00:03 +000067}
68
David Sehr4fcdd6d2016-05-24 14:52:31 -070069static constexpr int kInvalidFd = -1;
70
71static bool FdIsValid(int fd) {
72 return fd != kInvalidFd;
73}
74
Calin Juravle2e2db782016-02-23 12:00:03 +000075static void UsageErrorV(const char* fmt, va_list ap) {
76 std::string error;
Andreas Gampe46ee31b2016-12-14 10:11:49 -080077 android::base::StringAppendV(&error, fmt, ap);
Calin Juravle2e2db782016-02-23 12:00:03 +000078 LOG(ERROR) << error;
79}
80
81static void UsageError(const char* fmt, ...) {
82 va_list ap;
83 va_start(ap, fmt);
84 UsageErrorV(fmt, ap);
85 va_end(ap);
86}
87
88NO_RETURN static void Usage(const char *fmt, ...) {
89 va_list ap;
90 va_start(ap, fmt);
91 UsageErrorV(fmt, ap);
92 va_end(ap);
93
94 UsageError("Command: %s", CommandLine().c_str());
95 UsageError("Usage: profman [options]...");
96 UsageError("");
David Sehr4fcdd6d2016-05-24 14:52:31 -070097 UsageError(" --dump-only: dumps the content of the specified profile files");
98 UsageError(" to standard output (default) in a human readable form.");
99 UsageError("");
David Sehr7c80f2d2017-02-07 16:47:58 -0800100 UsageError(" --dump-output-to-fd=<number>: redirects --dump-only output to a file descriptor.");
101 UsageError("");
Mathieu Chartier34067262017-04-06 13:55:46 -0700102 UsageError(" --dump-classes-and-methods: dumps a sorted list of classes and methods that are");
103 UsageError(" in the specified profile file to standard output (default) in a human");
104 UsageError(" readable form. The output is valid input for --create-profile-from");
Calin Juravle876f3502016-03-24 16:16:34 +0000105 UsageError("");
Calin Juravle2e2db782016-02-23 12:00:03 +0000106 UsageError(" --profile-file=<filename>: specify profiler output file to use for compilation.");
107 UsageError(" Can be specified multiple time, in which case the data from the different");
108 UsageError(" profiles will be aggregated.");
109 UsageError("");
110 UsageError(" --profile-file-fd=<number>: same as --profile-file but accepts a file descriptor.");
111 UsageError(" Cannot be used together with --profile-file.");
112 UsageError("");
113 UsageError(" --reference-profile-file=<filename>: specify a reference profile.");
114 UsageError(" The data in this file will be compared with the data obtained by merging");
115 UsageError(" all the files specified with --profile-file or --profile-file-fd.");
116 UsageError(" If the exit code is EXIT_COMPILE then all --profile-file will be merged into");
117 UsageError(" --reference-profile-file. ");
118 UsageError("");
119 UsageError(" --reference-profile-file-fd=<number>: same as --reference-profile-file but");
120 UsageError(" accepts a file descriptor. Cannot be used together with");
121 UsageError(" --reference-profile-file.");
David Sehr7c80f2d2017-02-07 16:47:58 -0800122 UsageError("");
Calin Juravle7bcdb532016-06-07 16:14:47 +0100123 UsageError(" --generate-test-profile=<filename>: generates a random profile file for testing.");
124 UsageError(" --generate-test-profile-num-dex=<number>: number of dex files that should be");
125 UsageError(" included in the generated profile. Defaults to 20.");
Shubham Ajmerad704f0b2017-07-26 16:33:35 -0700126 UsageError(" --generate-test-profile-method-percentage=<number>: the percentage from the maximum");
Calin Juravle7bcdb532016-06-07 16:14:47 +0100127 UsageError(" number of methods that should be generated. Defaults to 5.");
Shubham Ajmerad704f0b2017-07-26 16:33:35 -0700128 UsageError(" --generate-test-profile-class-percentage=<number>: the percentage from the maximum");
Calin Juravle7bcdb532016-06-07 16:14:47 +0100129 UsageError(" number of classes that should be generated. Defaults to 5.");
Jeff Haof0a31f82017-03-27 15:50:37 -0700130 UsageError(" --generate-test-profile-seed=<number>: seed for random number generator used when");
131 UsageError(" generating random test profiles. Defaults to using NanoTime.");
Calin Juravle7bcdb532016-06-07 16:14:47 +0100132 UsageError("");
Mathieu Chartier34067262017-04-06 13:55:46 -0700133 UsageError(" --create-profile-from=<filename>: creates a profile from a list of classes and");
134 UsageError(" methods.");
David Sehr7c80f2d2017-02-07 16:47:58 -0800135 UsageError("");
David Sehr546d24f2016-06-02 10:46:19 -0700136 UsageError(" --dex-location=<string>: location string to use with corresponding");
137 UsageError(" apk-fd to find dex files");
David Sehr4fcdd6d2016-05-24 14:52:31 -0700138 UsageError("");
David Sehr546d24f2016-06-02 10:46:19 -0700139 UsageError(" --apk-fd=<number>: file descriptor containing an open APK to");
David Sehr4fcdd6d2016-05-24 14:52:31 -0700140 UsageError(" search for dex files");
David Sehr7c80f2d2017-02-07 16:47:58 -0800141 UsageError(" --apk-=<filename>: an APK to search for dex files");
David Brazdilf13ac7c2018-01-30 10:09:08 +0000142 UsageError(" --skip-apk-verification: do not attempt to verify APKs");
David Sehr4fcdd6d2016-05-24 14:52:31 -0700143 UsageError("");
Mathieu Chartier2f794552017-06-19 10:58:08 -0700144 UsageError(" --generate-boot-image-profile: Generate a boot image profile based on input");
145 UsageError(" profiles. Requires passing in dex files to inspect properties of classes.");
146 UsageError(" --boot-image-class-threshold=<value>: specify minimum number of class occurrences");
147 UsageError(" to include a class in the boot image profile. Default is 10.");
148 UsageError(" --boot-image-clean-class-threshold=<value>: specify minimum number of clean class");
149 UsageError(" occurrences to include a class in the boot image profile. A clean class is a");
150 UsageError(" class that doesn't have any static fields or native methods and is likely to");
151 UsageError(" remain clean in the image. Default is 3.");
Mathieu Chartier8eecddf2017-07-12 16:05:54 -0700152 UsageError(" --boot-image-sampled-method-threshold=<value>: minimum number of profiles a");
153 UsageError(" non-hot method needs to be in order to be hot in the output profile. The");
154 UsageError(" default is max int.");
Calin Juravle2dba0ab2018-01-22 19:22:24 -0800155 UsageError(" --copy-and-update-profile-key: if present, profman will copy the profile from");
156 UsageError(" the file passed with --profile-fd(file) to the profile passed with");
157 UsageError(" --reference-profile-fd(file) and update at the same time the profile-key");
158 UsageError(" of entries corresponding to the apks passed with --apk(-fd).");
Mathieu Chartier2f794552017-06-19 10:58:08 -0700159 UsageError("");
Calin Juravle2e2db782016-02-23 12:00:03 +0000160
161 exit(EXIT_FAILURE);
162}
163
Calin Juravle7bcdb532016-06-07 16:14:47 +0100164// Note: make sure you update the Usage if you change these values.
165static constexpr uint16_t kDefaultTestProfileNumDex = 20;
Shubham Ajmerad704f0b2017-07-26 16:33:35 -0700166static constexpr uint16_t kDefaultTestProfileMethodPercentage = 5;
167static constexpr uint16_t kDefaultTestProfileClassPercentage = 5;
Calin Juravle7bcdb532016-06-07 16:14:47 +0100168
Calin Juravlee0ac1152017-02-13 19:03:47 -0800169// Separators used when parsing human friendly representation of profiles.
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800170static const std::string kMethodSep = "->"; // NOLINT [runtime/string] [4]
171static const std::string kMissingTypesMarker = "missing_types"; // NOLINT [runtime/string] [4]
172static const std::string kInvalidClassDescriptor = "invalid_class"; // NOLINT [runtime/string] [4]
173static const std::string kInvalidMethod = "invalid_method"; // NOLINT [runtime/string] [4]
174static const std::string kClassAllMethods = "*"; // NOLINT [runtime/string] [4]
Calin Juravlee0ac1152017-02-13 19:03:47 -0800175static constexpr char kProfileParsingInlineChacheSep = '+';
176static constexpr char kProfileParsingTypeSep = ',';
177static constexpr char kProfileParsingFirstCharInSignature = '(';
Mathieu Chartierea650f32017-05-24 12:04:13 -0700178static constexpr char kMethodFlagStringHot = 'H';
179static constexpr char kMethodFlagStringStartup = 'S';
180static constexpr char kMethodFlagStringPostStartup = 'P';
Calin Juravlee0ac1152017-02-13 19:03:47 -0800181
David Sehr671af6c2018-05-17 11:00:35 -0700182NO_RETURN static void Abort(const char* msg) {
183 LOG(ERROR) << msg;
184 exit(1);
185}
186
Calin Juravlee0ac1152017-02-13 19:03:47 -0800187// TODO(calin): This class has grown too much from its initial design. Split the functionality
188// into smaller, more contained pieces.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100189class ProfMan final {
Calin Juravle2e2db782016-02-23 12:00:03 +0000190 public:
191 ProfMan() :
David Sehr4fcdd6d2016-05-24 14:52:31 -0700192 reference_profile_file_fd_(kInvalidFd),
193 dump_only_(false),
Mathieu Chartier34067262017-04-06 13:55:46 -0700194 dump_classes_and_methods_(false),
Mathieu Chartier2f794552017-06-19 10:58:08 -0700195 generate_boot_image_profile_(false),
David Sehr4fcdd6d2016-05-24 14:52:31 -0700196 dump_output_to_fd_(kInvalidFd),
Calin Juravle7bcdb532016-06-07 16:14:47 +0100197 test_profile_num_dex_(kDefaultTestProfileNumDex),
Shubham Ajmerad704f0b2017-07-26 16:33:35 -0700198 test_profile_method_percerntage_(kDefaultTestProfileMethodPercentage),
199 test_profile_class_percentage_(kDefaultTestProfileClassPercentage),
Jeff Haof0a31f82017-03-27 15:50:37 -0700200 test_profile_seed_(NanoTime()),
Calin Juravle2dba0ab2018-01-22 19:22:24 -0800201 start_ns_(NanoTime()),
202 copy_and_update_profile_key_(false) {}
Calin Juravle2e2db782016-02-23 12:00:03 +0000203
204 ~ProfMan() {
205 LogCompletionTime();
206 }
207
208 void ParseArgs(int argc, char **argv) {
209 original_argc = argc;
210 original_argv = argv;
211
David Sehr671af6c2018-05-17 11:00:35 -0700212 MemMap::Init();
213 InitLogging(argv, Abort);
Calin Juravle2e2db782016-02-23 12:00:03 +0000214
215 // Skip over the command name.
216 argv++;
217 argc--;
218
219 if (argc == 0) {
220 Usage("No arguments specified");
221 }
222
223 for (int i = 0; i < argc; ++i) {
224 const StringPiece option(argv[i]);
225 const bool log_options = false;
226 if (log_options) {
Calin Juravle876f3502016-03-24 16:16:34 +0000227 LOG(INFO) << "profman: option[" << i << "]=" << argv[i];
Calin Juravle2e2db782016-02-23 12:00:03 +0000228 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700229 if (option == "--dump-only") {
230 dump_only_ = true;
Mathieu Chartier34067262017-04-06 13:55:46 -0700231 } else if (option == "--dump-classes-and-methods") {
232 dump_classes_and_methods_ = true;
David Sehr7c80f2d2017-02-07 16:47:58 -0800233 } else if (option.starts_with("--create-profile-from=")) {
234 create_profile_from_file_ = option.substr(strlen("--create-profile-from=")).ToString();
David Sehr4fcdd6d2016-05-24 14:52:31 -0700235 } else if (option.starts_with("--dump-output-to-fd=")) {
236 ParseUintOption(option, "--dump-output-to-fd", &dump_output_to_fd_, Usage);
Mathieu Chartier2f794552017-06-19 10:58:08 -0700237 } else if (option == "--generate-boot-image-profile") {
238 generate_boot_image_profile_ = true;
239 } else if (option.starts_with("--boot-image-class-threshold=")) {
240 ParseUintOption(option,
241 "--boot-image-class-threshold",
242 &boot_image_options_.image_class_theshold,
243 Usage);
244 } else if (option.starts_with("--boot-image-clean-class-threshold=")) {
245 ParseUintOption(option,
246 "--boot-image-clean-class-threshold",
247 &boot_image_options_.image_class_clean_theshold,
248 Usage);
Mathieu Chartier8eecddf2017-07-12 16:05:54 -0700249 } else if (option.starts_with("--boot-image-sampled-method-threshold=")) {
250 ParseUintOption(option,
251 "--boot-image-sampled-method-threshold",
252 &boot_image_options_.compiled_method_threshold,
253 Usage);
Calin Juravle876f3502016-03-24 16:16:34 +0000254 } else if (option.starts_with("--profile-file=")) {
Calin Juravle2e2db782016-02-23 12:00:03 +0000255 profile_files_.push_back(option.substr(strlen("--profile-file=")).ToString());
256 } else if (option.starts_with("--profile-file-fd=")) {
257 ParseFdForCollection(option, "--profile-file-fd", &profile_files_fd_);
258 } else if (option.starts_with("--reference-profile-file=")) {
259 reference_profile_file_ = option.substr(strlen("--reference-profile-file=")).ToString();
260 } else if (option.starts_with("--reference-profile-file-fd=")) {
261 ParseUintOption(option, "--reference-profile-file-fd", &reference_profile_file_fd_, Usage);
David Sehr546d24f2016-06-02 10:46:19 -0700262 } else if (option.starts_with("--dex-location=")) {
263 dex_locations_.push_back(option.substr(strlen("--dex-location=")).ToString());
264 } else if (option.starts_with("--apk-fd=")) {
265 ParseFdForCollection(option, "--apk-fd", &apks_fd_);
David Sehr7c80f2d2017-02-07 16:47:58 -0800266 } else if (option.starts_with("--apk=")) {
267 apk_files_.push_back(option.substr(strlen("--apk=")).ToString());
Calin Juravle7bcdb532016-06-07 16:14:47 +0100268 } else if (option.starts_with("--generate-test-profile=")) {
269 test_profile_ = option.substr(strlen("--generate-test-profile=")).ToString();
270 } else if (option.starts_with("--generate-test-profile-num-dex=")) {
271 ParseUintOption(option,
272 "--generate-test-profile-num-dex",
273 &test_profile_num_dex_,
274 Usage);
Shubham Ajmerad704f0b2017-07-26 16:33:35 -0700275 } else if (option.starts_with("--generate-test-profile-method-percentage")) {
Calin Juravle7bcdb532016-06-07 16:14:47 +0100276 ParseUintOption(option,
Shubham Ajmerad704f0b2017-07-26 16:33:35 -0700277 "--generate-test-profile-method-percentage",
278 &test_profile_method_percerntage_,
Calin Juravle7bcdb532016-06-07 16:14:47 +0100279 Usage);
Shubham Ajmerad704f0b2017-07-26 16:33:35 -0700280 } else if (option.starts_with("--generate-test-profile-class-percentage")) {
Calin Juravle7bcdb532016-06-07 16:14:47 +0100281 ParseUintOption(option,
Shubham Ajmerad704f0b2017-07-26 16:33:35 -0700282 "--generate-test-profile-class-percentage",
283 &test_profile_class_percentage_,
Calin Juravle7bcdb532016-06-07 16:14:47 +0100284 Usage);
Jeff Haof0a31f82017-03-27 15:50:37 -0700285 } else if (option.starts_with("--generate-test-profile-seed=")) {
286 ParseUintOption(option, "--generate-test-profile-seed", &test_profile_seed_, Usage);
Calin Juravle02c08792018-02-15 19:40:48 -0800287 } else if (option.starts_with("--copy-and-update-profile-key")) {
288 copy_and_update_profile_key_ = true;
Calin Juravle2e2db782016-02-23 12:00:03 +0000289 } else {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700290 Usage("Unknown argument '%s'", option.data());
Calin Juravle2e2db782016-02-23 12:00:03 +0000291 }
292 }
293
David Sehr153da0e2017-02-15 08:54:51 -0800294 // Validate global consistency between file/fd options.
Calin Juravle2e2db782016-02-23 12:00:03 +0000295 if (!profile_files_.empty() && !profile_files_fd_.empty()) {
296 Usage("Profile files should not be specified with both --profile-file-fd and --profile-file");
297 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700298 if (!reference_profile_file_.empty() && FdIsValid(reference_profile_file_fd_)) {
299 Usage("Reference profile should not be specified with both "
300 "--reference-profile-file-fd and --reference-profile-file");
301 }
David Sehr153da0e2017-02-15 08:54:51 -0800302 if (!apk_files_.empty() && !apks_fd_.empty()) {
303 Usage("APK files should not be specified with both --apk-fd and --apk");
Calin Juravle2e2db782016-02-23 12:00:03 +0000304 }
305 }
306
Calin Juravlee10c1e22018-01-26 20:10:15 -0800307 struct ProfileFilterKey {
308 ProfileFilterKey(const std::string& dex_location, uint32_t checksum)
309 : dex_location_(dex_location), checksum_(checksum) {}
310 const std::string dex_location_;
311 uint32_t checksum_;
312
313 bool operator==(const ProfileFilterKey& other) const {
314 return checksum_ == other.checksum_ && dex_location_ == other.dex_location_;
315 }
316 bool operator<(const ProfileFilterKey& other) const {
317 return checksum_ == other.checksum_
318 ? dex_location_ < other.dex_location_
319 : checksum_ < other.checksum_;
320 }
321 };
322
Calin Juravle2e2db782016-02-23 12:00:03 +0000323 ProfileAssistant::ProcessingResult ProcessProfiles() {
David Sehr153da0e2017-02-15 08:54:51 -0800324 // Validate that at least one profile file was passed, as well as a reference profile.
325 if (profile_files_.empty() && profile_files_fd_.empty()) {
326 Usage("No profile files specified.");
327 }
328 if (reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
329 Usage("No reference profile file specified.");
330 }
331 if ((!profile_files_.empty() && FdIsValid(reference_profile_file_fd_)) ||
332 (!profile_files_fd_.empty() && !FdIsValid(reference_profile_file_fd_))) {
333 Usage("Options --profile-file-fd and --reference-profile-file-fd "
334 "should only be used together");
335 }
Calin Juravlee10c1e22018-01-26 20:10:15 -0800336
337 // Check if we have any apks which we should use to filter the profile data.
338 std::set<ProfileFilterKey> profile_filter_keys;
339 if (!GetProfileFilterKeyFromApks(&profile_filter_keys)) {
340 return ProfileAssistant::kErrorIO;
341 }
342
343 // Build the profile filter function. If the set of keys is empty it means we
344 // don't have any apks; as such we do not filter anything.
345 const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn =
346 [profile_filter_keys](const std::string& dex_location, uint32_t checksum) {
347 if (profile_filter_keys.empty()) {
348 // No --apk was specified. Accept all dex files.
349 return true;
350 } else {
351 bool res = profile_filter_keys.find(
352 ProfileFilterKey(dex_location, checksum)) != profile_filter_keys.end();
353 return res;
354 }
355 };
356
Calin Juravle2e2db782016-02-23 12:00:03 +0000357 ProfileAssistant::ProcessingResult result;
Calin Juravle2dba0ab2018-01-22 19:22:24 -0800358
Calin Juravle2e2db782016-02-23 12:00:03 +0000359 if (profile_files_.empty()) {
360 // The file doesn't need to be flushed here (ProcessProfiles will do it)
361 // so don't check the usage.
362 File file(reference_profile_file_fd_, false);
Calin Juravle2dba0ab2018-01-22 19:22:24 -0800363 result = ProfileAssistant::ProcessProfiles(profile_files_fd_,
Calin Juravlee10c1e22018-01-26 20:10:15 -0800364 reference_profile_file_fd_,
365 filter_fn);
Calin Juravle2e2db782016-02-23 12:00:03 +0000366 CloseAllFds(profile_files_fd_, "profile_files_fd_");
367 } else {
Calin Juravlee10c1e22018-01-26 20:10:15 -0800368 result = ProfileAssistant::ProcessProfiles(profile_files_,
369 reference_profile_file_,
370 filter_fn);
Calin Juravle2e2db782016-02-23 12:00:03 +0000371 }
372 return result;
373 }
374
Calin Juravlee10c1e22018-01-26 20:10:15 -0800375 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(
Andreas Gampebc802de2018-06-20 17:24:11 -0700393 const std::function<void(std::unique_ptr<const DexFile>&&)>& process_fn) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800394 bool use_apk_fd_list = !apks_fd_.empty();
395 if (use_apk_fd_list) {
David Sehr153da0e2017-02-15 08:54:51 -0800396 // Get the APKs from the collection of FDs.
Calin Juravlee10c1e22018-01-26 20:10:15 -0800397 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 Sehr153da0e2017-02-15 08:54:51 -0800409 } else if (!apk_files_.empty()) {
Calin Juravle02c08792018-02-15 19:40:48 -0800410 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 Sehr153da0e2017-02-15 08:54:51 -0800416 } else {
417 // No APKs were specified.
418 CHECK(dex_locations_.empty());
Calin Juravlee10c1e22018-01-26 20:10:15 -0800419 return true;
David Sehr7c80f2d2017-02-07 16:47:58 -0800420 }
421 static constexpr bool kVerifyChecksum = true;
422 for (size_t i = 0; i < dex_locations_.size(); ++i) {
Mathieu Chartier818cb802018-05-11 05:30:16 +0000423 std::string error_msg;
424 const ArtDexFileLoader dex_file_loader;
425 std::vector<std::unique_ptr<const DexFile>> dex_files_for_location;
Calin Juravle1bfe4bd2018-04-26 16:00:11 -0700426 // We do not need to verify the apk for processing profiles.
David Sehr7c80f2d2017-02-07 16:47:58 -0800427 if (use_apk_fd_list) {
Mathieu Chartier818cb802018-05-11 05:30:16 +0000428 if (dex_file_loader.OpenZip(apks_fd_[i],
429 dex_locations_[i],
Andreas Gampe9b031f72018-10-04 11:03:34 -0700430 /* verify= */ false,
Mathieu Chartier818cb802018-05-11 05:30:16 +0000431 kVerifyChecksum,
432 &error_msg,
433 &dex_files_for_location)) {
434 } else {
435 LOG(ERROR) << "OpenZip failed for '" << dex_locations_[i] << "' " << error_msg;
Calin Juravlee10c1e22018-01-26 20:10:15 -0800436 return false;
David Sehr7c80f2d2017-02-07 16:47:58 -0800437 }
Mathieu Chartier818cb802018-05-11 05:30:16 +0000438 } else {
439 if (dex_file_loader.Open(apk_files_[i].c_str(),
440 dex_locations_[i],
Andreas Gampe9b031f72018-10-04 11:03:34 -0700441 /* verify= */ false,
Mathieu Chartier818cb802018-05-11 05:30:16 +0000442 kVerifyChecksum,
443 &error_msg,
444 &dex_files_for_location)) {
445 } else {
446 LOG(ERROR) << "Open failed for '" << dex_locations_[i] << "' " << error_msg;
447 return false;
448 }
David Sehr2b80ed42018-05-08 08:58:15 -0700449 }
David Sehr7c80f2d2017-02-07 16:47:58 -0800450 for (std::unique_ptr<const DexFile>& dex_file : dex_files_for_location) {
Calin Juravlee10c1e22018-01-26 20:10:15 -0800451 process_fn(std::move(dex_file));
David Sehr7c80f2d2017-02-07 16:47:58 -0800452 }
453 }
Calin Juravlee10c1e22018-01-26 20:10:15 -0800454 return true;
455 }
456
457 // Get the dex locations from the apk fds.
458 // The methods reads the links from /proc/self/fd/ to find the original apk paths
459 // and puts them in the dex_locations_ vector.
460 bool ComputeDexLocationsFromApkFds() {
461 // We can't use a char array of PATH_MAX size without exceeding the frame size.
462 // So we use a vector as the buffer for the path.
463 std::vector<char> buffer(PATH_MAX, 0);
464 for (size_t i = 0; i < apks_fd_.size(); ++i) {
465 std::string fd_path = "/proc/self/fd/" + std::to_string(apks_fd_[i]);
466 ssize_t len = readlink(fd_path.c_str(), buffer.data(), buffer.size() - 1);
467 if (len == -1) {
468 PLOG(ERROR) << "Could not open path from fd";
469 return false;
470 }
471
472 buffer[len] = '\0';
473 dex_locations_.push_back(buffer.data());
474 }
475 return true;
David Sehr7c80f2d2017-02-07 16:47:58 -0800476 }
477
Mathieu Chartier2f794552017-06-19 10:58:08 -0700478 std::unique_ptr<const ProfileCompilationInfo> LoadProfile(const std::string& filename, int fd) {
479 if (!filename.empty()) {
480 fd = open(filename.c_str(), O_RDWR);
481 if (fd < 0) {
482 LOG(ERROR) << "Cannot open " << filename << strerror(errno);
483 return nullptr;
484 }
485 }
486 std::unique_ptr<ProfileCompilationInfo> info(new ProfileCompilationInfo);
487 if (!info->Load(fd)) {
488 LOG(ERROR) << "Cannot load profile info from fd=" << fd << "\n";
489 return nullptr;
490 }
491 return info;
492 }
493
David Sehrb18991b2017-02-08 20:58:10 -0800494 int DumpOneProfile(const std::string& banner,
495 const std::string& filename,
496 int fd,
497 const std::vector<std::unique_ptr<const DexFile>>* dex_files,
498 std::string* dump) {
Mathieu Chartier2f794552017-06-19 10:58:08 -0700499 std::unique_ptr<const ProfileCompilationInfo> info(LoadProfile(filename, fd));
500 if (info == nullptr) {
501 LOG(ERROR) << "Cannot load profile info from filename=" << filename << " fd=" << fd;
Calin Juravle876f3502016-03-24 16:16:34 +0000502 return -1;
503 }
Mathieu Chartier0573f852018-10-01 13:52:12 -0700504 *dump += banner + "\n" + info->DumpInfo(MakeNonOwningPointerVector(*dex_files)) + "\n";
David Sehr4fcdd6d2016-05-24 14:52:31 -0700505 return 0;
506 }
507
508 int DumpProfileInfo() {
David Sehr153da0e2017-02-15 08:54:51 -0800509 // Validate that at least one profile file or reference was specified.
510 if (profile_files_.empty() && profile_files_fd_.empty() &&
511 reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
512 Usage("No profile files or reference profile specified.");
513 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700514 static const char* kEmptyString = "";
David Sehr546d24f2016-06-02 10:46:19 -0700515 static const char* kOrdinaryProfile = "=== profile ===";
516 static const char* kReferenceProfile = "=== reference profile ===";
Mathieu Chartier0573f852018-10-01 13:52:12 -0700517 static const char* kDexFiles = "=== Dex files ===";
David Sehr546d24f2016-06-02 10:46:19 -0700518
David Sehrb18991b2017-02-08 20:58:10 -0800519 std::vector<std::unique_ptr<const DexFile>> dex_files;
David Sehr7c80f2d2017-02-07 16:47:58 -0800520 OpenApkFilesFromLocations(&dex_files);
Mathieu Chartier0573f852018-10-01 13:52:12 -0700521
David Sehr4fcdd6d2016-05-24 14:52:31 -0700522 std::string dump;
Mathieu Chartier0573f852018-10-01 13:52:12 -0700523
524 // Dump checkfiles and corresponding checksums.
525 dump += kDexFiles;
526 dump += "\n";
527 for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
528 std::ostringstream oss;
529 oss << dex_file->GetLocation()
530 << " [checksum=" << std::hex << dex_file->GetLocationChecksum() << "]\n";
531 dump += oss.str();
532 }
533
David Sehr4fcdd6d2016-05-24 14:52:31 -0700534 // Dump individual profile files.
535 if (!profile_files_fd_.empty()) {
536 for (int profile_file_fd : profile_files_fd_) {
David Sehr546d24f2016-06-02 10:46:19 -0700537 int ret = DumpOneProfile(kOrdinaryProfile,
538 kEmptyString,
539 profile_file_fd,
540 &dex_files,
541 &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700542 if (ret != 0) {
543 return ret;
544 }
545 }
546 }
Mathieu Chartier0573f852018-10-01 13:52:12 -0700547 for (const std::string& profile_file : profile_files_) {
548 int ret = DumpOneProfile(kOrdinaryProfile, profile_file, kInvalidFd, &dex_files, &dump);
549 if (ret != 0) {
550 return ret;
David Sehr4fcdd6d2016-05-24 14:52:31 -0700551 }
552 }
553 // Dump reference profile file.
554 if (FdIsValid(reference_profile_file_fd_)) {
David Sehr546d24f2016-06-02 10:46:19 -0700555 int ret = DumpOneProfile(kReferenceProfile,
556 kEmptyString,
557 reference_profile_file_fd_,
558 &dex_files,
559 &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700560 if (ret != 0) {
561 return ret;
562 }
563 }
564 if (!reference_profile_file_.empty()) {
David Sehr546d24f2016-06-02 10:46:19 -0700565 int ret = DumpOneProfile(kReferenceProfile,
566 reference_profile_file_,
567 kInvalidFd,
568 &dex_files,
David Sehr4fcdd6d2016-05-24 14:52:31 -0700569 &dump);
570 if (ret != 0) {
571 return ret;
572 }
573 }
574 if (!FdIsValid(dump_output_to_fd_)) {
575 std::cout << dump;
576 } else {
Andreas Gampe9b031f72018-10-04 11:03:34 -0700577 unix_file::FdFile out_fd(dump_output_to_fd_, /*check_usage=*/ false);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700578 if (!out_fd.WriteFully(dump.c_str(), dump.length())) {
579 return -1;
580 }
581 }
Calin Juravle876f3502016-03-24 16:16:34 +0000582 return 0;
583 }
584
585 bool ShouldOnlyDumpProfile() {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700586 return dump_only_;
Calin Juravle876f3502016-03-24 16:16:34 +0000587 }
588
Mathieu Chartier34067262017-04-06 13:55:46 -0700589 bool GetClassNamesAndMethods(int fd,
590 std::vector<std::unique_ptr<const DexFile>>* dex_files,
591 std::set<std::string>* out_lines) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800592 ProfileCompilationInfo profile_info;
593 if (!profile_info.Load(fd)) {
594 LOG(ERROR) << "Cannot load profile info";
595 return false;
596 }
Mathieu Chartier34067262017-04-06 13:55:46 -0700597 for (const std::unique_ptr<const DexFile>& dex_file : *dex_files) {
598 std::set<dex::TypeIndex> class_types;
Mathieu Chartierea650f32017-05-24 12:04:13 -0700599 std::set<uint16_t> hot_methods;
600 std::set<uint16_t> startup_methods;
601 std::set<uint16_t> post_startup_methods;
602 std::set<uint16_t> combined_methods;
603 if (profile_info.GetClassesAndMethods(*dex_file.get(),
604 &class_types,
605 &hot_methods,
606 &startup_methods,
607 &post_startup_methods)) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700608 for (const dex::TypeIndex& type_index : class_types) {
609 const DexFile::TypeId& type_id = dex_file->GetTypeId(type_index);
610 out_lines->insert(std::string(dex_file->GetTypeDescriptor(type_id)));
611 }
Mathieu Chartierea650f32017-05-24 12:04:13 -0700612 combined_methods = hot_methods;
613 combined_methods.insert(startup_methods.begin(), startup_methods.end());
614 combined_methods.insert(post_startup_methods.begin(), post_startup_methods.end());
615 for (uint16_t dex_method_idx : combined_methods) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700616 const DexFile::MethodId& id = dex_file->GetMethodId(dex_method_idx);
617 std::string signature_string(dex_file->GetMethodSignature(id).ToString());
618 std::string type_string(dex_file->GetTypeDescriptor(dex_file->GetTypeId(id.class_idx_)));
619 std::string method_name(dex_file->GetMethodName(id));
Mathieu Chartierea650f32017-05-24 12:04:13 -0700620 std::string flags_string;
621 if (hot_methods.find(dex_method_idx) != hot_methods.end()) {
622 flags_string += kMethodFlagStringHot;
623 }
624 if (startup_methods.find(dex_method_idx) != startup_methods.end()) {
625 flags_string += kMethodFlagStringStartup;
626 }
627 if (post_startup_methods.find(dex_method_idx) != post_startup_methods.end()) {
628 flags_string += kMethodFlagStringPostStartup;
629 }
630 out_lines->insert(flags_string +
631 type_string +
632 kMethodSep +
633 method_name +
634 signature_string);
Mathieu Chartier34067262017-04-06 13:55:46 -0700635 }
636 }
637 }
David Sehr7c80f2d2017-02-07 16:47:58 -0800638 return true;
639 }
640
Mathieu Chartier34067262017-04-06 13:55:46 -0700641 bool GetClassNamesAndMethods(const std::string& profile_file,
642 std::vector<std::unique_ptr<const DexFile>>* dex_files,
643 std::set<std::string>* out_lines) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800644 int fd = open(profile_file.c_str(), O_RDONLY);
645 if (!FdIsValid(fd)) {
646 LOG(ERROR) << "Cannot open " << profile_file << strerror(errno);
647 return false;
648 }
Mathieu Chartier34067262017-04-06 13:55:46 -0700649 if (!GetClassNamesAndMethods(fd, dex_files, out_lines)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800650 return false;
651 }
652 if (close(fd) < 0) {
653 PLOG(WARNING) << "Failed to close descriptor";
654 }
655 return true;
656 }
657
Mathieu Chartierea650f32017-05-24 12:04:13 -0700658 int DumpClassesAndMethods() {
David Sehr153da0e2017-02-15 08:54:51 -0800659 // Validate that at least one profile file or reference was specified.
660 if (profile_files_.empty() && profile_files_fd_.empty() &&
661 reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
662 Usage("No profile files or reference profile specified.");
663 }
Calin Juravlee10c1e22018-01-26 20:10:15 -0800664
David Sehr7c80f2d2017-02-07 16:47:58 -0800665 // Open the dex files to get the names for classes.
666 std::vector<std::unique_ptr<const DexFile>> dex_files;
667 OpenApkFilesFromLocations(&dex_files);
668 // Build a vector of class names from individual profile files.
669 std::set<std::string> class_names;
670 if (!profile_files_fd_.empty()) {
671 for (int profile_file_fd : profile_files_fd_) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700672 if (!GetClassNamesAndMethods(profile_file_fd, &dex_files, &class_names)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800673 return -1;
674 }
675 }
676 }
677 if (!profile_files_.empty()) {
678 for (const std::string& profile_file : profile_files_) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700679 if (!GetClassNamesAndMethods(profile_file, &dex_files, &class_names)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800680 return -1;
681 }
682 }
683 }
684 // Concatenate class names from reference profile file.
685 if (FdIsValid(reference_profile_file_fd_)) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700686 if (!GetClassNamesAndMethods(reference_profile_file_fd_, &dex_files, &class_names)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800687 return -1;
688 }
689 }
690 if (!reference_profile_file_.empty()) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700691 if (!GetClassNamesAndMethods(reference_profile_file_, &dex_files, &class_names)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800692 return -1;
693 }
694 }
695 // Dump the class names.
696 std::string dump;
697 for (const std::string& class_name : class_names) {
698 dump += class_name + std::string("\n");
699 }
700 if (!FdIsValid(dump_output_to_fd_)) {
701 std::cout << dump;
702 } else {
Andreas Gampe9b031f72018-10-04 11:03:34 -0700703 unix_file::FdFile out_fd(dump_output_to_fd_, /*check_usage=*/ false);
David Sehr7c80f2d2017-02-07 16:47:58 -0800704 if (!out_fd.WriteFully(dump.c_str(), dump.length())) {
705 return -1;
706 }
707 }
708 return 0;
709 }
710
Mathieu Chartier34067262017-04-06 13:55:46 -0700711 bool ShouldOnlyDumpClassesAndMethods() {
712 return dump_classes_and_methods_;
David Sehr7c80f2d2017-02-07 16:47:58 -0800713 }
714
715 // Read lines from the given file, dropping comments and empty lines. Post-process each line with
716 // the given function.
717 template <typename T>
718 static T* ReadCommentedInputFromFile(
719 const char* input_filename, std::function<std::string(const char*)>* process) {
720 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
721 if (input_file.get() == nullptr) {
722 LOG(ERROR) << "Failed to open input file " << input_filename;
723 return nullptr;
724 }
725 std::unique_ptr<T> result(
726 ReadCommentedInputStream<T>(*input_file, process));
727 input_file->close();
728 return result.release();
729 }
730
731 // Read lines from the given stream, dropping comments and empty lines. Post-process each line
732 // with the given function.
733 template <typename T>
734 static T* ReadCommentedInputStream(
735 std::istream& in_stream,
736 std::function<std::string(const char*)>* process) {
737 std::unique_ptr<T> output(new T());
738 while (in_stream.good()) {
739 std::string dot;
740 std::getline(in_stream, dot);
741 if (android::base::StartsWith(dot, "#") || dot.empty()) {
742 continue;
743 }
744 if (process != nullptr) {
745 std::string descriptor((*process)(dot.c_str()));
746 output->insert(output->end(), descriptor);
747 } else {
748 output->insert(output->end(), dot);
749 }
750 }
751 return output.release();
752 }
753
Calin Juravlee0ac1152017-02-13 19:03:47 -0800754 // Find class klass_descriptor in the given dex_files and store its reference
755 // in the out parameter class_ref.
756 // Return true if the definition of the class was found in any of the dex_files.
757 bool FindClass(const std::vector<std::unique_ptr<const DexFile>>& dex_files,
758 const std::string& klass_descriptor,
Mathieu Chartierdbddc222017-05-24 12:04:13 -0700759 /*out*/TypeReference* class_ref) {
Calin Juravle08556882017-05-26 16:40:45 -0700760 constexpr uint16_t kInvalidTypeIndex = std::numeric_limits<uint16_t>::max() - 1;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800761 for (const std::unique_ptr<const DexFile>& dex_file_ptr : dex_files) {
762 const DexFile* dex_file = dex_file_ptr.get();
Calin Juravle08556882017-05-26 16:40:45 -0700763 if (klass_descriptor == kInvalidClassDescriptor) {
764 if (kInvalidTypeIndex >= dex_file->NumTypeIds()) {
765 // The dex file does not contain all possible type ids which leaves us room
766 // to add an "invalid" type id.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700767 *class_ref = TypeReference(dex_file, dex::TypeIndex(kInvalidTypeIndex));
Calin Juravle08556882017-05-26 16:40:45 -0700768 return true;
769 } else {
770 // The dex file contains all possible type ids. We don't have any free type id
771 // that we can use as invalid.
772 continue;
773 }
774 }
775
Calin Juravlee0ac1152017-02-13 19:03:47 -0800776 const DexFile::TypeId* type_id = dex_file->FindTypeId(klass_descriptor.c_str());
777 if (type_id == nullptr) {
778 continue;
779 }
780 dex::TypeIndex type_index = dex_file->GetIndexForTypeId(*type_id);
781 if (dex_file->FindClassDef(type_index) == nullptr) {
782 // Class is only referenced in the current dex file but not defined in it.
783 continue;
784 }
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700785 *class_ref = TypeReference(dex_file, type_index);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800786 return true;
787 }
788 return false;
789 }
790
Mathieu Chartier34067262017-04-06 13:55:46 -0700791 // Find the method specified by method_spec in the class class_ref.
Calin Juravle08556882017-05-26 16:40:45 -0700792 uint32_t FindMethodIndex(const TypeReference& class_ref,
793 const std::string& method_spec) {
794 const DexFile* dex_file = class_ref.dex_file;
795 if (method_spec == kInvalidMethod) {
796 constexpr uint16_t kInvalidMethodIndex = std::numeric_limits<uint16_t>::max() - 1;
797 return kInvalidMethodIndex >= dex_file->NumMethodIds()
798 ? kInvalidMethodIndex
Andreas Gampee2abbc62017-09-15 11:59:26 -0700799 : dex::kDexNoIndex;
Calin Juravle08556882017-05-26 16:40:45 -0700800 }
801
Calin Juravlee0ac1152017-02-13 19:03:47 -0800802 std::vector<std::string> name_and_signature;
803 Split(method_spec, kProfileParsingFirstCharInSignature, &name_and_signature);
804 if (name_and_signature.size() != 2) {
805 LOG(ERROR) << "Invalid method name and signature " << method_spec;
Andreas Gampee2abbc62017-09-15 11:59:26 -0700806 return dex::kDexNoIndex;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800807 }
Calin Juravle08556882017-05-26 16:40:45 -0700808
Calin Juravlee0ac1152017-02-13 19:03:47 -0800809 const std::string& name = name_and_signature[0];
810 const std::string& signature = kProfileParsingFirstCharInSignature + name_and_signature[1];
Calin Juravlee0ac1152017-02-13 19:03:47 -0800811
812 const DexFile::StringId* name_id = dex_file->FindStringId(name.c_str());
813 if (name_id == nullptr) {
Mathieu Chartier66aae3b2017-05-18 10:38:28 -0700814 LOG(WARNING) << "Could not find name: " << name;
Andreas Gampee2abbc62017-09-15 11:59:26 -0700815 return dex::kDexNoIndex;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800816 }
817 dex::TypeIndex return_type_idx;
818 std::vector<dex::TypeIndex> param_type_idxs;
819 if (!dex_file->CreateTypeList(signature, &return_type_idx, &param_type_idxs)) {
Mathieu Chartier66aae3b2017-05-18 10:38:28 -0700820 LOG(WARNING) << "Could not create type list" << signature;
Andreas Gampee2abbc62017-09-15 11:59:26 -0700821 return dex::kDexNoIndex;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800822 }
823 const DexFile::ProtoId* proto_id = dex_file->FindProtoId(return_type_idx, param_type_idxs);
824 if (proto_id == nullptr) {
Mathieu Chartier66aae3b2017-05-18 10:38:28 -0700825 LOG(WARNING) << "Could not find proto_id: " << name;
Andreas Gampee2abbc62017-09-15 11:59:26 -0700826 return dex::kDexNoIndex;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800827 }
828 const DexFile::MethodId* method_id = dex_file->FindMethodId(
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700829 dex_file->GetTypeId(class_ref.TypeIndex()), *name_id, *proto_id);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800830 if (method_id == nullptr) {
Mathieu Chartier66aae3b2017-05-18 10:38:28 -0700831 LOG(WARNING) << "Could not find method_id: " << name;
Andreas Gampee2abbc62017-09-15 11:59:26 -0700832 return dex::kDexNoIndex;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800833 }
834
Mathieu Chartier34067262017-04-06 13:55:46 -0700835 return dex_file->GetIndexForMethodId(*method_id);
836 }
Calin Juravlee0ac1152017-02-13 19:03:47 -0800837
Mathieu Chartier34067262017-04-06 13:55:46 -0700838 // Given a method, return true if the method has a single INVOKE_VIRTUAL in its byte code.
839 // Upon success it returns true and stores the method index and the invoke dex pc
840 // in the output parameters.
841 // The format of the method spec is "inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;".
842 //
843 // TODO(calin): support INVOKE_INTERFACE and the range variants.
Mathieu Chartierdbddc222017-05-24 12:04:13 -0700844 bool HasSingleInvoke(const TypeReference& class_ref,
Mathieu Chartier34067262017-04-06 13:55:46 -0700845 uint16_t method_index,
846 /*out*/uint32_t* dex_pc) {
847 const DexFile* dex_file = class_ref.dex_file;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800848 uint32_t offset = dex_file->FindCodeItemOffset(
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700849 *dex_file->FindClassDef(class_ref.TypeIndex()),
Mathieu Chartier34067262017-04-06 13:55:46 -0700850 method_index);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800851 const DexFile::CodeItem* code_item = dex_file->GetCodeItem(offset);
852
853 bool found_invoke = false;
Mathieu Chartier698ebbc2018-01-05 11:00:42 -0800854 for (const DexInstructionPcPair& inst : CodeItemInstructionAccessor(*dex_file, code_item)) {
Rico Windc24fa5d2018-04-25 12:44:58 +0200855 if (inst->Opcode() == Instruction::INVOKE_VIRTUAL ||
856 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800857 if (found_invoke) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700858 LOG(ERROR) << "Multiple invoke INVOKE_VIRTUAL found: "
859 << dex_file->PrettyMethod(method_index);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800860 return false;
861 }
862 found_invoke = true;
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800863 *dex_pc = inst.DexPc();
Calin Juravlee0ac1152017-02-13 19:03:47 -0800864 }
865 }
866 if (!found_invoke) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700867 LOG(ERROR) << "Could not find any INVOKE_VIRTUAL: " << dex_file->PrettyMethod(method_index);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800868 }
869 return found_invoke;
870 }
871
872 // Process a line defining a class or a method and its inline caches.
873 // Upon success return true and add the class or the method info to profile.
Calin Juravle589e71e2017-03-03 16:05:05 -0800874 // The possible line formats are:
875 // "LJustTheCass;".
Calin Juravlee0ac1152017-02-13 19:03:47 -0800876 // "LTestInline;->inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;".
Calin Juravle08556882017-05-26 16:40:45 -0700877 // "LTestInline;->inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,invalid_class".
Calin Juravle589e71e2017-03-03 16:05:05 -0800878 // "LTestInline;->inlineMissingTypes(LSuper;)I+missing_types".
879 // "LTestInline;->inlineNoInlineCaches(LSuper;)I".
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700880 // "LTestInline;->*".
Calin Juravle08556882017-05-26 16:40:45 -0700881 // "invalid_class".
882 // "LTestInline;->invalid_method".
Calin Juravlee0ac1152017-02-13 19:03:47 -0800883 // The method and classes are searched only in the given dex files.
884 bool ProcessLine(const std::vector<std::unique_ptr<const DexFile>>& dex_files,
885 const std::string& line,
886 /*out*/ProfileCompilationInfo* profile) {
887 std::string klass;
888 std::string method_str;
Mathieu Chartierea650f32017-05-24 12:04:13 -0700889 bool is_hot = false;
890 bool is_startup = false;
891 bool is_post_startup = false;
892 const size_t method_sep_index = line.find(kMethodSep, 0);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800893 if (method_sep_index == std::string::npos) {
Mathieu Chartierea650f32017-05-24 12:04:13 -0700894 klass = line.substr(0);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800895 } else {
Mathieu Chartierea650f32017-05-24 12:04:13 -0700896 // The method prefix flags are only valid for method strings.
897 size_t start_index = 0;
898 while (start_index < line.size() && line[start_index] != 'L') {
899 const char c = line[start_index];
900 if (c == kMethodFlagStringHot) {
901 is_hot = true;
902 } else if (c == kMethodFlagStringStartup) {
903 is_startup = true;
904 } else if (c == kMethodFlagStringPostStartup) {
905 is_post_startup = true;
906 } else {
907 LOG(WARNING) << "Invalid flag " << c;
908 return false;
909 }
910 ++start_index;
911 }
912 klass = line.substr(start_index, method_sep_index - start_index);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800913 method_str = line.substr(method_sep_index + kMethodSep.size());
914 }
915
Calin Juravleee9cb412018-02-13 20:32:35 -0800916 uint32_t flags = 0;
917 if (is_hot) {
918 flags |= ProfileCompilationInfo::MethodHotness::kFlagHot;
919 }
920 if (is_startup) {
921 flags |= ProfileCompilationInfo::MethodHotness::kFlagStartup;
922 }
923 if (is_post_startup) {
924 flags |= ProfileCompilationInfo::MethodHotness::kFlagPostStartup;
925 }
926
Andreas Gampe9b031f72018-10-04 11:03:34 -0700927 TypeReference class_ref(/* dex_file= */ nullptr, dex::TypeIndex());
Calin Juravlee0ac1152017-02-13 19:03:47 -0800928 if (!FindClass(dex_files, klass, &class_ref)) {
Mathieu Chartier3f121342017-03-06 11:16:20 -0800929 LOG(WARNING) << "Could not find class: " << klass;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800930 return false;
931 }
932
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700933 if (method_str.empty() || method_str == kClassAllMethods) {
934 // Start by adding the class.
Calin Juravlee0ac1152017-02-13 19:03:47 -0800935 std::set<DexCacheResolvedClasses> resolved_class_set;
936 const DexFile* dex_file = class_ref.dex_file;
937 const auto& dex_resolved_classes = resolved_class_set.emplace(
938 dex_file->GetLocation(),
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700939 DexFileLoader::GetBaseLocation(dex_file->GetLocation()),
Mathieu Chartierea650f32017-05-24 12:04:13 -0700940 dex_file->GetLocationChecksum(),
941 dex_file->NumMethodIds());
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700942 dex_resolved_classes.first->AddClass(class_ref.TypeIndex());
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700943 std::vector<ProfileMethodInfo> methods;
944 if (method_str == kClassAllMethods) {
Mathieu Chartier18e26872018-06-04 17:19:02 -0700945 ClassAccessor accessor(
946 *dex_file,
947 dex_file->GetIndexForClassDef(*dex_file->FindClassDef(class_ref.TypeIndex())));
Mathieu Chartier0d896bd2018-05-25 00:20:27 -0700948 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
Mathieu Chartier20f49922018-05-24 16:04:17 -0700949 if (method.GetCodeItemOffset() != 0) {
950 // Add all of the methods that have code to the profile.
951 methods.push_back(ProfileMethodInfo(method.GetReference()));
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700952 }
Mathieu Chartier0d896bd2018-05-25 00:20:27 -0700953 }
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700954 }
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700955 // TODO: Check return values?
Calin Juravleee9cb412018-02-13 20:32:35 -0800956 profile->AddMethods(methods, static_cast<ProfileCompilationInfo::MethodHotness::Flag>(flags));
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700957 profile->AddClasses(resolved_class_set);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800958 return true;
959 }
960
961 // Process the method.
962 std::string method_spec;
963 std::vector<std::string> inline_cache_elems;
964
Mathieu Chartierea650f32017-05-24 12:04:13 -0700965 // If none of the flags are set, default to hot.
966 is_hot = is_hot || (!is_hot && !is_startup && !is_post_startup);
967
Calin Juravlee0ac1152017-02-13 19:03:47 -0800968 std::vector<std::string> method_elems;
Calin Juravle589e71e2017-03-03 16:05:05 -0800969 bool is_missing_types = false;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800970 Split(method_str, kProfileParsingInlineChacheSep, &method_elems);
971 if (method_elems.size() == 2) {
972 method_spec = method_elems[0];
Calin Juravle589e71e2017-03-03 16:05:05 -0800973 is_missing_types = method_elems[1] == kMissingTypesMarker;
974 if (!is_missing_types) {
975 Split(method_elems[1], kProfileParsingTypeSep, &inline_cache_elems);
976 }
Calin Juravlee0ac1152017-02-13 19:03:47 -0800977 } else if (method_elems.size() == 1) {
978 method_spec = method_elems[0];
979 } else {
980 LOG(ERROR) << "Invalid method line: " << line;
981 return false;
982 }
983
Mathieu Chartier34067262017-04-06 13:55:46 -0700984 const uint32_t method_index = FindMethodIndex(class_ref, method_spec);
Andreas Gampee2abbc62017-09-15 11:59:26 -0700985 if (method_index == dex::kDexNoIndex) {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800986 return false;
987 }
Mathieu Chartier34067262017-04-06 13:55:46 -0700988
Mathieu Chartier34067262017-04-06 13:55:46 -0700989 std::vector<ProfileMethodInfo::ProfileInlineCache> inline_caches;
990 if (is_missing_types || !inline_cache_elems.empty()) {
991 uint32_t dex_pc;
992 if (!HasSingleInvoke(class_ref, method_index, &dex_pc)) {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800993 return false;
994 }
Vladimir Markof3c52b42017-11-17 17:32:12 +0000995 std::vector<TypeReference> classes(inline_cache_elems.size(),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700996 TypeReference(/* dex_file= */ nullptr, dex::TypeIndex()));
Mathieu Chartier34067262017-04-06 13:55:46 -0700997 size_t class_it = 0;
998 for (const std::string& ic_class : inline_cache_elems) {
999 if (!FindClass(dex_files, ic_class, &(classes[class_it++]))) {
1000 LOG(ERROR) << "Could not find class: " << ic_class;
1001 return false;
1002 }
1003 }
1004 inline_caches.emplace_back(dex_pc, is_missing_types, classes);
Calin Juravlee0ac1152017-02-13 19:03:47 -08001005 }
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -07001006 MethodReference ref(class_ref.dex_file, method_index);
Mathieu Chartierea650f32017-05-24 12:04:13 -07001007 if (is_hot) {
Calin Juravleee9cb412018-02-13 20:32:35 -08001008 profile->AddMethod(ProfileMethodInfo(ref, inline_caches),
1009 static_cast<ProfileCompilationInfo::MethodHotness::Flag>(flags));
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -07001010 }
1011 if (flags != 0) {
Calin Juravleee9cb412018-02-13 20:32:35 -08001012 if (!profile->AddMethodIndex(
1013 static_cast<ProfileCompilationInfo::MethodHotness::Flag>(flags), ref)) {
Mathieu Chartierea650f32017-05-24 12:04:13 -07001014 return false;
1015 }
Mathieu Chartiere46f3a82017-06-19 19:54:12 -07001016 DCHECK(profile->GetMethodHotness(ref).IsInProfile());
Mathieu Chartierea650f32017-05-24 12:04:13 -07001017 }
Calin Juravlee0ac1152017-02-13 19:03:47 -08001018 return true;
1019 }
1020
Mathieu Chartier2f794552017-06-19 10:58:08 -07001021 int OpenReferenceProfile() const {
1022 int fd = reference_profile_file_fd_;
1023 if (!FdIsValid(fd)) {
1024 CHECK(!reference_profile_file_.empty());
1025 fd = open(reference_profile_file_.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
1026 if (fd < 0) {
1027 LOG(ERROR) << "Cannot open " << reference_profile_file_ << strerror(errno);
1028 return kInvalidFd;
1029 }
1030 }
1031 return fd;
1032 }
1033
Calin Juravlee0ac1152017-02-13 19:03:47 -08001034 // Creates a profile from a human friendly textual representation.
1035 // The expected input format is:
1036 // # Classes
1037 // Ljava/lang/Comparable;
1038 // Ljava/lang/Math;
1039 // # Methods with inline caches
1040 // LTestInline;->inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;
1041 // LTestInline;->noInlineCache(LSuper;)I
David Sehr7c80f2d2017-02-07 16:47:58 -08001042 int CreateProfile() {
David Sehr153da0e2017-02-15 08:54:51 -08001043 // Validate parameters for this command.
1044 if (apk_files_.empty() && apks_fd_.empty()) {
1045 Usage("APK files must be specified");
1046 }
1047 if (dex_locations_.empty()) {
1048 Usage("DEX locations must be specified");
1049 }
1050 if (reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
1051 Usage("Reference profile must be specified with --reference-profile-file or "
1052 "--reference-profile-file-fd");
1053 }
1054 if (!profile_files_.empty() || !profile_files_fd_.empty()) {
1055 Usage("Profile must be specified with --reference-profile-file or "
1056 "--reference-profile-file-fd");
1057 }
David Sehr7c80f2d2017-02-07 16:47:58 -08001058 // Open the profile output file if needed.
Mathieu Chartier2f794552017-06-19 10:58:08 -07001059 int fd = OpenReferenceProfile();
David Sehr7c80f2d2017-02-07 16:47:58 -08001060 if (!FdIsValid(fd)) {
David Sehr7c80f2d2017-02-07 16:47:58 -08001061 return -1;
David Sehr7c80f2d2017-02-07 16:47:58 -08001062 }
Calin Juravlee0ac1152017-02-13 19:03:47 -08001063 // Read the user-specified list of classes and methods.
David Sehr7c80f2d2017-02-07 16:47:58 -08001064 std::unique_ptr<std::unordered_set<std::string>>
Calin Juravlee0ac1152017-02-13 19:03:47 -08001065 user_lines(ReadCommentedInputFromFile<std::unordered_set<std::string>>(
David Sehr7c80f2d2017-02-07 16:47:58 -08001066 create_profile_from_file_.c_str(), nullptr)); // No post-processing.
Calin Juravlee0ac1152017-02-13 19:03:47 -08001067
1068 // Open the dex files to look up classes and methods.
David Sehr7c80f2d2017-02-07 16:47:58 -08001069 std::vector<std::unique_ptr<const DexFile>> dex_files;
1070 OpenApkFilesFromLocations(&dex_files);
Calin Juravlee0ac1152017-02-13 19:03:47 -08001071
1072 // Process the lines one by one and add the successful ones to the profile.
David Sehr7c80f2d2017-02-07 16:47:58 -08001073 ProfileCompilationInfo info;
Calin Juravlee0ac1152017-02-13 19:03:47 -08001074
1075 for (const auto& line : *user_lines) {
1076 ProcessLine(dex_files, line, &info);
1077 }
1078
David Sehr7c80f2d2017-02-07 16:47:58 -08001079 // Write the profile file.
1080 CHECK(info.Save(fd));
1081 if (close(fd) < 0) {
1082 PLOG(WARNING) << "Failed to close descriptor";
1083 }
1084 return 0;
1085 }
1086
Mathieu Chartier2f794552017-06-19 10:58:08 -07001087 bool ShouldCreateBootProfile() const {
1088 return generate_boot_image_profile_;
1089 }
1090
1091 int CreateBootProfile() {
Mathieu Chartier2f794552017-06-19 10:58:08 -07001092 // Open the profile output file.
1093 const int reference_fd = OpenReferenceProfile();
1094 if (!FdIsValid(reference_fd)) {
1095 PLOG(ERROR) << "Error opening reference profile";
1096 return -1;
1097 }
1098 // Open the dex files.
1099 std::vector<std::unique_ptr<const DexFile>> dex_files;
1100 OpenApkFilesFromLocations(&dex_files);
1101 if (dex_files.empty()) {
1102 PLOG(ERROR) << "Expected dex files for creating boot profile";
1103 return -2;
1104 }
1105 // Open the input profiles.
1106 std::vector<std::unique_ptr<const ProfileCompilationInfo>> profiles;
1107 if (!profile_files_fd_.empty()) {
1108 for (int profile_file_fd : profile_files_fd_) {
1109 std::unique_ptr<const ProfileCompilationInfo> profile(LoadProfile("", profile_file_fd));
1110 if (profile == nullptr) {
1111 return -3;
1112 }
1113 profiles.emplace_back(std::move(profile));
1114 }
1115 }
1116 if (!profile_files_.empty()) {
1117 for (const std::string& profile_file : profile_files_) {
1118 std::unique_ptr<const ProfileCompilationInfo> profile(LoadProfile(profile_file, kInvalidFd));
1119 if (profile == nullptr) {
1120 return -4;
1121 }
1122 profiles.emplace_back(std::move(profile));
1123 }
1124 }
1125 ProfileCompilationInfo out_profile;
1126 GenerateBootImageProfile(dex_files,
1127 profiles,
1128 boot_image_options_,
1129 VLOG_IS_ON(profiler),
1130 &out_profile);
1131 out_profile.Save(reference_fd);
1132 close(reference_fd);
1133 return 0;
1134 }
1135
David Sehr7c80f2d2017-02-07 16:47:58 -08001136 bool ShouldCreateProfile() {
1137 return !create_profile_from_file_.empty();
1138 }
1139
Calin Juravle7bcdb532016-06-07 16:14:47 +01001140 int GenerateTestProfile() {
David Sehr153da0e2017-02-15 08:54:51 -08001141 // Validate parameters for this command.
Shubham Ajmerad704f0b2017-07-26 16:33:35 -07001142 if (test_profile_method_percerntage_ > 100) {
1143 Usage("Invalid percentage for --generate-test-profile-method-percentage");
David Sehr153da0e2017-02-15 08:54:51 -08001144 }
Shubham Ajmerad704f0b2017-07-26 16:33:35 -07001145 if (test_profile_class_percentage_ > 100) {
1146 Usage("Invalid percentage for --generate-test-profile-class-percentage");
David Sehr153da0e2017-02-15 08:54:51 -08001147 }
Jeff Haof0a31f82017-03-27 15:50:37 -07001148 // If given APK files or DEX locations, check that they're ok.
1149 if (!apk_files_.empty() || !apks_fd_.empty() || !dex_locations_.empty()) {
1150 if (apk_files_.empty() && apks_fd_.empty()) {
1151 Usage("APK files must be specified when passing DEX locations to --generate-test-profile");
1152 }
1153 if (dex_locations_.empty()) {
1154 Usage("DEX locations must be specified when passing APK files to --generate-test-profile");
1155 }
1156 }
David Sehr153da0e2017-02-15 08:54:51 -08001157 // ShouldGenerateTestProfile confirms !test_profile_.empty().
George Burgess IV71f77262016-10-25 13:08:17 -07001158 int profile_test_fd = open(test_profile_.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
Calin Juravle7bcdb532016-06-07 16:14:47 +01001159 if (profile_test_fd < 0) {
David Sehr7c80f2d2017-02-07 16:47:58 -08001160 LOG(ERROR) << "Cannot open " << test_profile_ << strerror(errno);
Calin Juravle7bcdb532016-06-07 16:14:47 +01001161 return -1;
1162 }
Jeff Haof0a31f82017-03-27 15:50:37 -07001163 bool result;
1164 if (apk_files_.empty() && apks_fd_.empty() && dex_locations_.empty()) {
1165 result = ProfileCompilationInfo::GenerateTestProfile(profile_test_fd,
1166 test_profile_num_dex_,
Shubham Ajmerad704f0b2017-07-26 16:33:35 -07001167 test_profile_method_percerntage_,
1168 test_profile_class_percentage_,
Jeff Haof0a31f82017-03-27 15:50:37 -07001169 test_profile_seed_);
1170 } else {
Jeff Haof0a31f82017-03-27 15:50:37 -07001171 // Open the dex files to look up classes and methods.
1172 std::vector<std::unique_ptr<const DexFile>> dex_files;
1173 OpenApkFilesFromLocations(&dex_files);
1174 // Create a random profile file based on the set of dex files.
1175 result = ProfileCompilationInfo::GenerateTestProfile(profile_test_fd,
1176 dex_files,
Shubham Ajmerad704f0b2017-07-26 16:33:35 -07001177 test_profile_method_percerntage_,
1178 test_profile_class_percentage_,
Jeff Haof0a31f82017-03-27 15:50:37 -07001179 test_profile_seed_);
1180 }
Calin Juravle7bcdb532016-06-07 16:14:47 +01001181 close(profile_test_fd); // ignore close result.
1182 return result ? 0 : -1;
1183 }
1184
1185 bool ShouldGenerateTestProfile() {
1186 return !test_profile_.empty();
1187 }
1188
Calin Juravle2dba0ab2018-01-22 19:22:24 -08001189 bool ShouldCopyAndUpdateProfileKey() const {
1190 return copy_and_update_profile_key_;
1191 }
1192
Calin Juravle02c08792018-02-15 19:40:48 -08001193 int32_t CopyAndUpdateProfileKey() {
Calin Juravle2dba0ab2018-01-22 19:22:24 -08001194 // Validate that at least one profile file was passed, as well as a reference profile.
1195 if (!(profile_files_.size() == 1 ^ profile_files_fd_.size() == 1)) {
1196 Usage("Only one profile file should be specified.");
1197 }
1198 if (reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
1199 Usage("No reference profile file specified.");
1200 }
1201
1202 if (apk_files_.empty() && apks_fd_.empty()) {
1203 Usage("No apk files specified");
1204 }
1205
Calin Juravle02c08792018-02-15 19:40:48 -08001206 static constexpr int32_t kErrorFailedToUpdateProfile = -1;
1207 static constexpr int32_t kErrorFailedToSaveProfile = -2;
1208 static constexpr int32_t kErrorFailedToLoadProfile = -3;
1209
Calin Juravle2dba0ab2018-01-22 19:22:24 -08001210 bool use_fds = profile_files_fd_.size() == 1;
1211
1212 ProfileCompilationInfo profile;
1213 // Do not clear if invalid. The input might be an archive.
Calin Juravle02c08792018-02-15 19:40:48 -08001214 bool load_ok = use_fds
1215 ? profile.Load(profile_files_fd_[0])
Andreas Gampe9b031f72018-10-04 11:03:34 -07001216 : profile.Load(profile_files_[0], /*clear_if_invalid=*/ false);
Calin Juravle02c08792018-02-15 19:40:48 -08001217 if (load_ok) {
Calin Juravle2dba0ab2018-01-22 19:22:24 -08001218 // Open the dex files to look up classes and methods.
1219 std::vector<std::unique_ptr<const DexFile>> dex_files;
1220 OpenApkFilesFromLocations(&dex_files);
1221 if (!profile.UpdateProfileKeys(dex_files)) {
Calin Juravle02c08792018-02-15 19:40:48 -08001222 return kErrorFailedToUpdateProfile;
Calin Juravle2dba0ab2018-01-22 19:22:24 -08001223 }
Calin Juravle02c08792018-02-15 19:40:48 -08001224 bool result = use_fds
1225 ? profile.Save(reference_profile_file_fd_)
Andreas Gampe9b031f72018-10-04 11:03:34 -07001226 : profile.Save(reference_profile_file_, /*bytes_written=*/ nullptr);
Calin Juravle02c08792018-02-15 19:40:48 -08001227 return result ? 0 : kErrorFailedToSaveProfile;
Calin Juravle2dba0ab2018-01-22 19:22:24 -08001228 } else {
Calin Juravle02c08792018-02-15 19:40:48 -08001229 return kErrorFailedToLoadProfile;
Calin Juravle2dba0ab2018-01-22 19:22:24 -08001230 }
1231 }
1232
Calin Juravle2e2db782016-02-23 12:00:03 +00001233 private:
1234 static void ParseFdForCollection(const StringPiece& option,
1235 const char* arg_name,
1236 std::vector<int>* fds) {
1237 int fd;
1238 ParseUintOption(option, arg_name, &fd, Usage);
1239 fds->push_back(fd);
1240 }
1241
1242 static void CloseAllFds(const std::vector<int>& fds, const char* descriptor) {
1243 for (size_t i = 0; i < fds.size(); i++) {
1244 if (close(fds[i]) < 0) {
Calin Juravlee10c1e22018-01-26 20:10:15 -08001245 PLOG(WARNING) << "Failed to close descriptor for "
1246 << descriptor << " at index " << i << ": " << fds[i];
Calin Juravle2e2db782016-02-23 12:00:03 +00001247 }
1248 }
1249 }
1250
1251 void LogCompletionTime() {
David Sehr52683cf2016-05-05 09:02:38 -07001252 static constexpr uint64_t kLogThresholdTime = MsToNs(100); // 100ms
1253 uint64_t time_taken = NanoTime() - start_ns_;
David Sehred793012016-05-05 13:39:43 -07001254 if (time_taken > kLogThresholdTime) {
David Sehrd8557182016-05-06 12:29:35 -07001255 LOG(WARNING) << "profman took " << PrettyDuration(time_taken);
David Sehred793012016-05-05 13:39:43 -07001256 }
Calin Juravle2e2db782016-02-23 12:00:03 +00001257 }
1258
1259 std::vector<std::string> profile_files_;
1260 std::vector<int> profile_files_fd_;
David Sehr546d24f2016-06-02 10:46:19 -07001261 std::vector<std::string> dex_locations_;
David Sehr7c80f2d2017-02-07 16:47:58 -08001262 std::vector<std::string> apk_files_;
David Sehr546d24f2016-06-02 10:46:19 -07001263 std::vector<int> apks_fd_;
Calin Juravle2e2db782016-02-23 12:00:03 +00001264 std::string reference_profile_file_;
1265 int reference_profile_file_fd_;
David Sehr4fcdd6d2016-05-24 14:52:31 -07001266 bool dump_only_;
Mathieu Chartier34067262017-04-06 13:55:46 -07001267 bool dump_classes_and_methods_;
Mathieu Chartier2f794552017-06-19 10:58:08 -07001268 bool generate_boot_image_profile_;
David Sehr4fcdd6d2016-05-24 14:52:31 -07001269 int dump_output_to_fd_;
Mathieu Chartier2f794552017-06-19 10:58:08 -07001270 BootImageOptions boot_image_options_;
Calin Juravle7bcdb532016-06-07 16:14:47 +01001271 std::string test_profile_;
David Sehr7c80f2d2017-02-07 16:47:58 -08001272 std::string create_profile_from_file_;
Calin Juravle7bcdb532016-06-07 16:14:47 +01001273 uint16_t test_profile_num_dex_;
Shubham Ajmerad704f0b2017-07-26 16:33:35 -07001274 uint16_t test_profile_method_percerntage_;
1275 uint16_t test_profile_class_percentage_;
Jeff Haof0a31f82017-03-27 15:50:37 -07001276 uint32_t test_profile_seed_;
Calin Juravle2e2db782016-02-23 12:00:03 +00001277 uint64_t start_ns_;
Calin Juravle2dba0ab2018-01-22 19:22:24 -08001278 bool copy_and_update_profile_key_;
Calin Juravle2e2db782016-02-23 12:00:03 +00001279};
1280
1281// See ProfileAssistant::ProcessingResult for return codes.
1282static int profman(int argc, char** argv) {
1283 ProfMan profman;
1284
1285 // Parse arguments. Argument mistakes will lead to exit(EXIT_FAILURE) in UsageError.
1286 profman.ParseArgs(argc, argv);
1287
Calin Juravlee10c1e22018-01-26 20:10:15 -08001288 // Initialize MemMap for ZipArchive::OpenFromFd.
1289 MemMap::Init();
1290
Calin Juravle7bcdb532016-06-07 16:14:47 +01001291 if (profman.ShouldGenerateTestProfile()) {
1292 return profman.GenerateTestProfile();
1293 }
Calin Juravle876f3502016-03-24 16:16:34 +00001294 if (profman.ShouldOnlyDumpProfile()) {
1295 return profman.DumpProfileInfo();
1296 }
Mathieu Chartier34067262017-04-06 13:55:46 -07001297 if (profman.ShouldOnlyDumpClassesAndMethods()) {
Mathieu Chartierea650f32017-05-24 12:04:13 -07001298 return profman.DumpClassesAndMethods();
David Sehr7c80f2d2017-02-07 16:47:58 -08001299 }
1300 if (profman.ShouldCreateProfile()) {
1301 return profman.CreateProfile();
1302 }
Mathieu Chartier2f794552017-06-19 10:58:08 -07001303
1304 if (profman.ShouldCreateBootProfile()) {
1305 return profman.CreateBootProfile();
1306 }
Calin Juravle02c08792018-02-15 19:40:48 -08001307
1308 if (profman.ShouldCopyAndUpdateProfileKey()) {
1309 return profman.CopyAndUpdateProfileKey();
1310 }
1311
Calin Juravle2e2db782016-02-23 12:00:03 +00001312 // Process profile information and assess if we need to do a profile guided compilation.
1313 // This operation involves I/O.
1314 return profman.ProcessProfiles();
1315}
1316
1317} // namespace art
1318
1319int main(int argc, char **argv) {
1320 return art::profman(argc, argv);
1321}