blob: 91061a691d6f8dfc49a3cd002539f0ea7e6762f0 [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>
21#include <sys/stat.h>
22#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"
35#include "base/scoped_flock.h"
36#include "base/stringpiece.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000037#include "base/time_utils.h"
38#include "base/unix_file/fd_file.h"
Mathieu Chartier2f794552017-06-19 10:58:08 -070039#include "boot_image_profile.h"
Calin Juravlee0ac1152017-02-13 19:03:47 -080040#include "bytecode_utils.h"
David Sehr4fcdd6d2016-05-24 14:52:31 -070041#include "dex_file.h"
Andreas Gampee2abbc62017-09-15 11:59:26 -070042#include "dex_file_types.h"
Calin Juravle33083d62017-01-18 15:29:12 -080043#include "jit/profile_compilation_info.h"
Mathieu Chartierdbddc222017-05-24 12:04:13 -070044#include "profile_assistant.h"
David Sehrf57589f2016-10-17 10:09:33 -070045#include "runtime.h"
Mathieu Chartierdbddc222017-05-24 12:04:13 -070046#include "type_reference.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000047#include "utils.h"
David Sehr546d24f2016-06-02 10:46:19 -070048#include "zip_archive.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000049
50namespace art {
51
52static int original_argc;
53static char** original_argv;
54
55static std::string CommandLine() {
56 std::vector<std::string> command;
57 for (int i = 0; i < original_argc; ++i) {
58 command.push_back(original_argv[i]);
59 }
Andreas Gampe9186ced2016-12-12 14:28:21 -080060 return android::base::Join(command, ' ');
Calin Juravle2e2db782016-02-23 12:00:03 +000061}
62
David Sehr4fcdd6d2016-05-24 14:52:31 -070063static constexpr int kInvalidFd = -1;
64
65static bool FdIsValid(int fd) {
66 return fd != kInvalidFd;
67}
68
Calin Juravle2e2db782016-02-23 12:00:03 +000069static void UsageErrorV(const char* fmt, va_list ap) {
70 std::string error;
Andreas Gampe46ee31b2016-12-14 10:11:49 -080071 android::base::StringAppendV(&error, fmt, ap);
Calin Juravle2e2db782016-02-23 12:00:03 +000072 LOG(ERROR) << error;
73}
74
75static void UsageError(const char* fmt, ...) {
76 va_list ap;
77 va_start(ap, fmt);
78 UsageErrorV(fmt, ap);
79 va_end(ap);
80}
81
82NO_RETURN static void Usage(const char *fmt, ...) {
83 va_list ap;
84 va_start(ap, fmt);
85 UsageErrorV(fmt, ap);
86 va_end(ap);
87
88 UsageError("Command: %s", CommandLine().c_str());
89 UsageError("Usage: profman [options]...");
90 UsageError("");
David Sehr4fcdd6d2016-05-24 14:52:31 -070091 UsageError(" --dump-only: dumps the content of the specified profile files");
92 UsageError(" to standard output (default) in a human readable form.");
93 UsageError("");
David Sehr7c80f2d2017-02-07 16:47:58 -080094 UsageError(" --dump-output-to-fd=<number>: redirects --dump-only output to a file descriptor.");
95 UsageError("");
Mathieu Chartier34067262017-04-06 13:55:46 -070096 UsageError(" --dump-classes-and-methods: dumps a sorted list of classes and methods that are");
97 UsageError(" in the specified profile file to standard output (default) in a human");
98 UsageError(" readable form. The output is valid input for --create-profile-from");
Calin Juravle876f3502016-03-24 16:16:34 +000099 UsageError("");
Calin Juravle2e2db782016-02-23 12:00:03 +0000100 UsageError(" --profile-file=<filename>: specify profiler output file to use for compilation.");
101 UsageError(" Can be specified multiple time, in which case the data from the different");
102 UsageError(" profiles will be aggregated.");
103 UsageError("");
104 UsageError(" --profile-file-fd=<number>: same as --profile-file but accepts a file descriptor.");
105 UsageError(" Cannot be used together with --profile-file.");
106 UsageError("");
107 UsageError(" --reference-profile-file=<filename>: specify a reference profile.");
108 UsageError(" The data in this file will be compared with the data obtained by merging");
109 UsageError(" all the files specified with --profile-file or --profile-file-fd.");
110 UsageError(" If the exit code is EXIT_COMPILE then all --profile-file will be merged into");
111 UsageError(" --reference-profile-file. ");
112 UsageError("");
113 UsageError(" --reference-profile-file-fd=<number>: same as --reference-profile-file but");
114 UsageError(" accepts a file descriptor. Cannot be used together with");
115 UsageError(" --reference-profile-file.");
David Sehr7c80f2d2017-02-07 16:47:58 -0800116 UsageError("");
Calin Juravle7bcdb532016-06-07 16:14:47 +0100117 UsageError(" --generate-test-profile=<filename>: generates a random profile file for testing.");
118 UsageError(" --generate-test-profile-num-dex=<number>: number of dex files that should be");
119 UsageError(" included in the generated profile. Defaults to 20.");
Shubham Ajmerad704f0b2017-07-26 16:33:35 -0700120 UsageError(" --generate-test-profile-method-percentage=<number>: the percentage from the maximum");
Calin Juravle7bcdb532016-06-07 16:14:47 +0100121 UsageError(" number of methods that should be generated. Defaults to 5.");
Shubham Ajmerad704f0b2017-07-26 16:33:35 -0700122 UsageError(" --generate-test-profile-class-percentage=<number>: the percentage from the maximum");
Calin Juravle7bcdb532016-06-07 16:14:47 +0100123 UsageError(" number of classes that should be generated. Defaults to 5.");
Jeff Haof0a31f82017-03-27 15:50:37 -0700124 UsageError(" --generate-test-profile-seed=<number>: seed for random number generator used when");
125 UsageError(" generating random test profiles. Defaults to using NanoTime.");
Calin Juravle7bcdb532016-06-07 16:14:47 +0100126 UsageError("");
Mathieu Chartier34067262017-04-06 13:55:46 -0700127 UsageError(" --create-profile-from=<filename>: creates a profile from a list of classes and");
128 UsageError(" methods.");
David Sehr7c80f2d2017-02-07 16:47:58 -0800129 UsageError("");
David Sehr546d24f2016-06-02 10:46:19 -0700130 UsageError(" --dex-location=<string>: location string to use with corresponding");
131 UsageError(" apk-fd to find dex files");
David Sehr4fcdd6d2016-05-24 14:52:31 -0700132 UsageError("");
David Sehr546d24f2016-06-02 10:46:19 -0700133 UsageError(" --apk-fd=<number>: file descriptor containing an open APK to");
David Sehr4fcdd6d2016-05-24 14:52:31 -0700134 UsageError(" search for dex files");
David Sehr7c80f2d2017-02-07 16:47:58 -0800135 UsageError(" --apk-=<filename>: an APK to search for dex files");
David Sehr4fcdd6d2016-05-24 14:52:31 -0700136 UsageError("");
Mathieu Chartier2f794552017-06-19 10:58:08 -0700137 UsageError(" --generate-boot-image-profile: Generate a boot image profile based on input");
138 UsageError(" profiles. Requires passing in dex files to inspect properties of classes.");
139 UsageError(" --boot-image-class-threshold=<value>: specify minimum number of class occurrences");
140 UsageError(" to include a class in the boot image profile. Default is 10.");
141 UsageError(" --boot-image-clean-class-threshold=<value>: specify minimum number of clean class");
142 UsageError(" occurrences to include a class in the boot image profile. A clean class is a");
143 UsageError(" class that doesn't have any static fields or native methods and is likely to");
144 UsageError(" remain clean in the image. Default is 3.");
Mathieu Chartier8eecddf2017-07-12 16:05:54 -0700145 UsageError(" --boot-image-sampled-method-threshold=<value>: minimum number of profiles a");
146 UsageError(" non-hot method needs to be in order to be hot in the output profile. The");
147 UsageError(" default is max int.");
Mathieu Chartier2f794552017-06-19 10:58:08 -0700148 UsageError("");
Calin Juravle2e2db782016-02-23 12:00:03 +0000149
150 exit(EXIT_FAILURE);
151}
152
Calin Juravle7bcdb532016-06-07 16:14:47 +0100153// Note: make sure you update the Usage if you change these values.
154static constexpr uint16_t kDefaultTestProfileNumDex = 20;
Shubham Ajmerad704f0b2017-07-26 16:33:35 -0700155static constexpr uint16_t kDefaultTestProfileMethodPercentage = 5;
156static constexpr uint16_t kDefaultTestProfileClassPercentage = 5;
Calin Juravle7bcdb532016-06-07 16:14:47 +0100157
Calin Juravlee0ac1152017-02-13 19:03:47 -0800158// Separators used when parsing human friendly representation of profiles.
159static const std::string kMethodSep = "->";
Calin Juravle589e71e2017-03-03 16:05:05 -0800160static const std::string kMissingTypesMarker = "missing_types";
Calin Juravle08556882017-05-26 16:40:45 -0700161static const std::string kInvalidClassDescriptor = "invalid_class";
162static const std::string kInvalidMethod = "invalid_method";
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700163static const std::string kClassAllMethods = "*";
Calin Juravlee0ac1152017-02-13 19:03:47 -0800164static constexpr char kProfileParsingInlineChacheSep = '+';
165static constexpr char kProfileParsingTypeSep = ',';
166static constexpr char kProfileParsingFirstCharInSignature = '(';
Mathieu Chartierea650f32017-05-24 12:04:13 -0700167static constexpr char kMethodFlagStringHot = 'H';
168static constexpr char kMethodFlagStringStartup = 'S';
169static constexpr char kMethodFlagStringPostStartup = 'P';
Calin Juravlee0ac1152017-02-13 19:03:47 -0800170
171// TODO(calin): This class has grown too much from its initial design. Split the functionality
172// into smaller, more contained pieces.
Calin Juravle2e2db782016-02-23 12:00:03 +0000173class ProfMan FINAL {
174 public:
175 ProfMan() :
David Sehr4fcdd6d2016-05-24 14:52:31 -0700176 reference_profile_file_fd_(kInvalidFd),
177 dump_only_(false),
Mathieu Chartier34067262017-04-06 13:55:46 -0700178 dump_classes_and_methods_(false),
Mathieu Chartier2f794552017-06-19 10:58:08 -0700179 generate_boot_image_profile_(false),
David Sehr4fcdd6d2016-05-24 14:52:31 -0700180 dump_output_to_fd_(kInvalidFd),
Calin Juravle7bcdb532016-06-07 16:14:47 +0100181 test_profile_num_dex_(kDefaultTestProfileNumDex),
Shubham Ajmerad704f0b2017-07-26 16:33:35 -0700182 test_profile_method_percerntage_(kDefaultTestProfileMethodPercentage),
183 test_profile_class_percentage_(kDefaultTestProfileClassPercentage),
Jeff Haof0a31f82017-03-27 15:50:37 -0700184 test_profile_seed_(NanoTime()),
Calin Juravle2e2db782016-02-23 12:00:03 +0000185 start_ns_(NanoTime()) {}
186
187 ~ProfMan() {
188 LogCompletionTime();
189 }
190
191 void ParseArgs(int argc, char **argv) {
192 original_argc = argc;
193 original_argv = argv;
194
Andreas Gampe51d80cc2017-06-21 21:05:13 -0700195 InitLogging(argv, Runtime::Abort);
Calin Juravle2e2db782016-02-23 12:00:03 +0000196
197 // Skip over the command name.
198 argv++;
199 argc--;
200
201 if (argc == 0) {
202 Usage("No arguments specified");
203 }
204
205 for (int i = 0; i < argc; ++i) {
206 const StringPiece option(argv[i]);
207 const bool log_options = false;
208 if (log_options) {
Calin Juravle876f3502016-03-24 16:16:34 +0000209 LOG(INFO) << "profman: option[" << i << "]=" << argv[i];
Calin Juravle2e2db782016-02-23 12:00:03 +0000210 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700211 if (option == "--dump-only") {
212 dump_only_ = true;
Mathieu Chartier34067262017-04-06 13:55:46 -0700213 } else if (option == "--dump-classes-and-methods") {
214 dump_classes_and_methods_ = true;
David Sehr7c80f2d2017-02-07 16:47:58 -0800215 } else if (option.starts_with("--create-profile-from=")) {
216 create_profile_from_file_ = option.substr(strlen("--create-profile-from=")).ToString();
David Sehr4fcdd6d2016-05-24 14:52:31 -0700217 } else if (option.starts_with("--dump-output-to-fd=")) {
218 ParseUintOption(option, "--dump-output-to-fd", &dump_output_to_fd_, Usage);
Mathieu Chartier2f794552017-06-19 10:58:08 -0700219 } else if (option == "--generate-boot-image-profile") {
220 generate_boot_image_profile_ = true;
221 } else if (option.starts_with("--boot-image-class-threshold=")) {
222 ParseUintOption(option,
223 "--boot-image-class-threshold",
224 &boot_image_options_.image_class_theshold,
225 Usage);
226 } else if (option.starts_with("--boot-image-clean-class-threshold=")) {
227 ParseUintOption(option,
228 "--boot-image-clean-class-threshold",
229 &boot_image_options_.image_class_clean_theshold,
230 Usage);
Mathieu Chartier8eecddf2017-07-12 16:05:54 -0700231 } else if (option.starts_with("--boot-image-sampled-method-threshold=")) {
232 ParseUintOption(option,
233 "--boot-image-sampled-method-threshold",
234 &boot_image_options_.compiled_method_threshold,
235 Usage);
Calin Juravle876f3502016-03-24 16:16:34 +0000236 } else if (option.starts_with("--profile-file=")) {
Calin Juravle2e2db782016-02-23 12:00:03 +0000237 profile_files_.push_back(option.substr(strlen("--profile-file=")).ToString());
238 } else if (option.starts_with("--profile-file-fd=")) {
239 ParseFdForCollection(option, "--profile-file-fd", &profile_files_fd_);
240 } else if (option.starts_with("--reference-profile-file=")) {
241 reference_profile_file_ = option.substr(strlen("--reference-profile-file=")).ToString();
242 } else if (option.starts_with("--reference-profile-file-fd=")) {
243 ParseUintOption(option, "--reference-profile-file-fd", &reference_profile_file_fd_, Usage);
David Sehr546d24f2016-06-02 10:46:19 -0700244 } else if (option.starts_with("--dex-location=")) {
245 dex_locations_.push_back(option.substr(strlen("--dex-location=")).ToString());
246 } else if (option.starts_with("--apk-fd=")) {
247 ParseFdForCollection(option, "--apk-fd", &apks_fd_);
David Sehr7c80f2d2017-02-07 16:47:58 -0800248 } else if (option.starts_with("--apk=")) {
249 apk_files_.push_back(option.substr(strlen("--apk=")).ToString());
Calin Juravle7bcdb532016-06-07 16:14:47 +0100250 } else if (option.starts_with("--generate-test-profile=")) {
251 test_profile_ = option.substr(strlen("--generate-test-profile=")).ToString();
252 } else if (option.starts_with("--generate-test-profile-num-dex=")) {
253 ParseUintOption(option,
254 "--generate-test-profile-num-dex",
255 &test_profile_num_dex_,
256 Usage);
Shubham Ajmerad704f0b2017-07-26 16:33:35 -0700257 } else if (option.starts_with("--generate-test-profile-method-percentage")) {
Calin Juravle7bcdb532016-06-07 16:14:47 +0100258 ParseUintOption(option,
Shubham Ajmerad704f0b2017-07-26 16:33:35 -0700259 "--generate-test-profile-method-percentage",
260 &test_profile_method_percerntage_,
Calin Juravle7bcdb532016-06-07 16:14:47 +0100261 Usage);
Shubham Ajmerad704f0b2017-07-26 16:33:35 -0700262 } else if (option.starts_with("--generate-test-profile-class-percentage")) {
Calin Juravle7bcdb532016-06-07 16:14:47 +0100263 ParseUintOption(option,
Shubham Ajmerad704f0b2017-07-26 16:33:35 -0700264 "--generate-test-profile-class-percentage",
265 &test_profile_class_percentage_,
Calin Juravle7bcdb532016-06-07 16:14:47 +0100266 Usage);
Jeff Haof0a31f82017-03-27 15:50:37 -0700267 } else if (option.starts_with("--generate-test-profile-seed=")) {
268 ParseUintOption(option, "--generate-test-profile-seed", &test_profile_seed_, Usage);
Calin Juravle2e2db782016-02-23 12:00:03 +0000269 } else {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700270 Usage("Unknown argument '%s'", option.data());
Calin Juravle2e2db782016-02-23 12:00:03 +0000271 }
272 }
273
David Sehr153da0e2017-02-15 08:54:51 -0800274 // Validate global consistency between file/fd options.
Calin Juravle2e2db782016-02-23 12:00:03 +0000275 if (!profile_files_.empty() && !profile_files_fd_.empty()) {
276 Usage("Profile files should not be specified with both --profile-file-fd and --profile-file");
277 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700278 if (!reference_profile_file_.empty() && FdIsValid(reference_profile_file_fd_)) {
279 Usage("Reference profile should not be specified with both "
280 "--reference-profile-file-fd and --reference-profile-file");
281 }
David Sehr153da0e2017-02-15 08:54:51 -0800282 if (!apk_files_.empty() && !apks_fd_.empty()) {
283 Usage("APK files should not be specified with both --apk-fd and --apk");
Calin Juravle2e2db782016-02-23 12:00:03 +0000284 }
285 }
286
287 ProfileAssistant::ProcessingResult ProcessProfiles() {
David Sehr153da0e2017-02-15 08:54:51 -0800288 // Validate that at least one profile file was passed, as well as a reference profile.
289 if (profile_files_.empty() && profile_files_fd_.empty()) {
290 Usage("No profile files specified.");
291 }
292 if (reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
293 Usage("No reference profile file specified.");
294 }
295 if ((!profile_files_.empty() && FdIsValid(reference_profile_file_fd_)) ||
296 (!profile_files_fd_.empty() && !FdIsValid(reference_profile_file_fd_))) {
297 Usage("Options --profile-file-fd and --reference-profile-file-fd "
298 "should only be used together");
299 }
Calin Juravle2e2db782016-02-23 12:00:03 +0000300 ProfileAssistant::ProcessingResult result;
301 if (profile_files_.empty()) {
302 // The file doesn't need to be flushed here (ProcessProfiles will do it)
303 // so don't check the usage.
304 File file(reference_profile_file_fd_, false);
305 result = ProfileAssistant::ProcessProfiles(profile_files_fd_, reference_profile_file_fd_);
306 CloseAllFds(profile_files_fd_, "profile_files_fd_");
307 } else {
308 result = ProfileAssistant::ProcessProfiles(profile_files_, reference_profile_file_);
309 }
310 return result;
311 }
312
David Sehr7c80f2d2017-02-07 16:47:58 -0800313 void OpenApkFilesFromLocations(std::vector<std::unique_ptr<const DexFile>>* dex_files) {
314 bool use_apk_fd_list = !apks_fd_.empty();
315 if (use_apk_fd_list) {
David Sehr153da0e2017-02-15 08:54:51 -0800316 // Get the APKs from the collection of FDs.
David Sehr7c80f2d2017-02-07 16:47:58 -0800317 CHECK_EQ(dex_locations_.size(), apks_fd_.size());
David Sehr153da0e2017-02-15 08:54:51 -0800318 } else if (!apk_files_.empty()) {
319 // Get the APKs from the collection of filenames.
David Sehr7c80f2d2017-02-07 16:47:58 -0800320 CHECK_EQ(dex_locations_.size(), apk_files_.size());
David Sehr153da0e2017-02-15 08:54:51 -0800321 } else {
322 // No APKs were specified.
323 CHECK(dex_locations_.empty());
324 return;
David Sehr7c80f2d2017-02-07 16:47:58 -0800325 }
326 static constexpr bool kVerifyChecksum = true;
327 for (size_t i = 0; i < dex_locations_.size(); ++i) {
328 std::string error_msg;
329 std::vector<std::unique_ptr<const DexFile>> dex_files_for_location;
330 if (use_apk_fd_list) {
331 if (DexFile::OpenZip(apks_fd_[i],
332 dex_locations_[i],
333 kVerifyChecksum,
334 &error_msg,
335 &dex_files_for_location)) {
336 } else {
337 LOG(WARNING) << "OpenZip failed for '" << dex_locations_[i] << "' " << error_msg;
338 continue;
339 }
340 } else {
341 if (DexFile::Open(apk_files_[i].c_str(),
342 dex_locations_[i],
343 kVerifyChecksum,
344 &error_msg,
345 &dex_files_for_location)) {
346 } else {
347 LOG(WARNING) << "Open failed for '" << dex_locations_[i] << "' " << error_msg;
348 continue;
349 }
350 }
351 for (std::unique_ptr<const DexFile>& dex_file : dex_files_for_location) {
352 dex_files->emplace_back(std::move(dex_file));
353 }
354 }
355 }
356
Mathieu Chartier2f794552017-06-19 10:58:08 -0700357 std::unique_ptr<const ProfileCompilationInfo> LoadProfile(const std::string& filename, int fd) {
358 if (!filename.empty()) {
359 fd = open(filename.c_str(), O_RDWR);
360 if (fd < 0) {
361 LOG(ERROR) << "Cannot open " << filename << strerror(errno);
362 return nullptr;
363 }
364 }
365 std::unique_ptr<ProfileCompilationInfo> info(new ProfileCompilationInfo);
366 if (!info->Load(fd)) {
367 LOG(ERROR) << "Cannot load profile info from fd=" << fd << "\n";
368 return nullptr;
369 }
370 return info;
371 }
372
David Sehrb18991b2017-02-08 20:58:10 -0800373 int DumpOneProfile(const std::string& banner,
374 const std::string& filename,
375 int fd,
376 const std::vector<std::unique_ptr<const DexFile>>* dex_files,
377 std::string* dump) {
Mathieu Chartier2f794552017-06-19 10:58:08 -0700378 std::unique_ptr<const ProfileCompilationInfo> info(LoadProfile(filename, fd));
379 if (info == nullptr) {
380 LOG(ERROR) << "Cannot load profile info from filename=" << filename << " fd=" << fd;
Calin Juravle876f3502016-03-24 16:16:34 +0000381 return -1;
382 }
Mathieu Chartier2f794552017-06-19 10:58:08 -0700383 *dump += banner + "\n" + info->DumpInfo(dex_files) + "\n";
David Sehr4fcdd6d2016-05-24 14:52:31 -0700384 return 0;
385 }
386
387 int DumpProfileInfo() {
David Sehr153da0e2017-02-15 08:54:51 -0800388 // Validate that at least one profile file or reference was specified.
389 if (profile_files_.empty() && profile_files_fd_.empty() &&
390 reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
391 Usage("No profile files or reference profile specified.");
392 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700393 static const char* kEmptyString = "";
David Sehr546d24f2016-06-02 10:46:19 -0700394 static const char* kOrdinaryProfile = "=== profile ===";
395 static const char* kReferenceProfile = "=== reference profile ===";
396
397 // Open apk/zip files and and read dex files.
398 MemMap::Init(); // for ZipArchive::OpenFromFd
David Sehrb18991b2017-02-08 20:58:10 -0800399 std::vector<std::unique_ptr<const DexFile>> dex_files;
David Sehr7c80f2d2017-02-07 16:47:58 -0800400 OpenApkFilesFromLocations(&dex_files);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700401 std::string dump;
David Sehr4fcdd6d2016-05-24 14:52:31 -0700402 // Dump individual profile files.
403 if (!profile_files_fd_.empty()) {
404 for (int profile_file_fd : profile_files_fd_) {
David Sehr546d24f2016-06-02 10:46:19 -0700405 int ret = DumpOneProfile(kOrdinaryProfile,
406 kEmptyString,
407 profile_file_fd,
408 &dex_files,
409 &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700410 if (ret != 0) {
411 return ret;
412 }
413 }
414 }
415 if (!profile_files_.empty()) {
416 for (const std::string& profile_file : profile_files_) {
David Sehr546d24f2016-06-02 10:46:19 -0700417 int ret = DumpOneProfile(kOrdinaryProfile, profile_file, kInvalidFd, &dex_files, &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700418 if (ret != 0) {
419 return ret;
420 }
421 }
422 }
423 // Dump reference profile file.
424 if (FdIsValid(reference_profile_file_fd_)) {
David Sehr546d24f2016-06-02 10:46:19 -0700425 int ret = DumpOneProfile(kReferenceProfile,
426 kEmptyString,
427 reference_profile_file_fd_,
428 &dex_files,
429 &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700430 if (ret != 0) {
431 return ret;
432 }
433 }
434 if (!reference_profile_file_.empty()) {
David Sehr546d24f2016-06-02 10:46:19 -0700435 int ret = DumpOneProfile(kReferenceProfile,
436 reference_profile_file_,
437 kInvalidFd,
438 &dex_files,
David Sehr4fcdd6d2016-05-24 14:52:31 -0700439 &dump);
440 if (ret != 0) {
441 return ret;
442 }
443 }
444 if (!FdIsValid(dump_output_to_fd_)) {
445 std::cout << dump;
446 } else {
447 unix_file::FdFile out_fd(dump_output_to_fd_, false /*check_usage*/);
448 if (!out_fd.WriteFully(dump.c_str(), dump.length())) {
449 return -1;
450 }
451 }
Calin Juravle876f3502016-03-24 16:16:34 +0000452 return 0;
453 }
454
455 bool ShouldOnlyDumpProfile() {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700456 return dump_only_;
Calin Juravle876f3502016-03-24 16:16:34 +0000457 }
458
Mathieu Chartier34067262017-04-06 13:55:46 -0700459 bool GetClassNamesAndMethods(int fd,
460 std::vector<std::unique_ptr<const DexFile>>* dex_files,
461 std::set<std::string>* out_lines) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800462 ProfileCompilationInfo profile_info;
463 if (!profile_info.Load(fd)) {
464 LOG(ERROR) << "Cannot load profile info";
465 return false;
466 }
Mathieu Chartier34067262017-04-06 13:55:46 -0700467 for (const std::unique_ptr<const DexFile>& dex_file : *dex_files) {
468 std::set<dex::TypeIndex> class_types;
Mathieu Chartierea650f32017-05-24 12:04:13 -0700469 std::set<uint16_t> hot_methods;
470 std::set<uint16_t> startup_methods;
471 std::set<uint16_t> post_startup_methods;
472 std::set<uint16_t> combined_methods;
473 if (profile_info.GetClassesAndMethods(*dex_file.get(),
474 &class_types,
475 &hot_methods,
476 &startup_methods,
477 &post_startup_methods)) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700478 for (const dex::TypeIndex& type_index : class_types) {
479 const DexFile::TypeId& type_id = dex_file->GetTypeId(type_index);
480 out_lines->insert(std::string(dex_file->GetTypeDescriptor(type_id)));
481 }
Mathieu Chartierea650f32017-05-24 12:04:13 -0700482 combined_methods = hot_methods;
483 combined_methods.insert(startup_methods.begin(), startup_methods.end());
484 combined_methods.insert(post_startup_methods.begin(), post_startup_methods.end());
485 for (uint16_t dex_method_idx : combined_methods) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700486 const DexFile::MethodId& id = dex_file->GetMethodId(dex_method_idx);
487 std::string signature_string(dex_file->GetMethodSignature(id).ToString());
488 std::string type_string(dex_file->GetTypeDescriptor(dex_file->GetTypeId(id.class_idx_)));
489 std::string method_name(dex_file->GetMethodName(id));
Mathieu Chartierea650f32017-05-24 12:04:13 -0700490 std::string flags_string;
491 if (hot_methods.find(dex_method_idx) != hot_methods.end()) {
492 flags_string += kMethodFlagStringHot;
493 }
494 if (startup_methods.find(dex_method_idx) != startup_methods.end()) {
495 flags_string += kMethodFlagStringStartup;
496 }
497 if (post_startup_methods.find(dex_method_idx) != post_startup_methods.end()) {
498 flags_string += kMethodFlagStringPostStartup;
499 }
500 out_lines->insert(flags_string +
501 type_string +
502 kMethodSep +
503 method_name +
504 signature_string);
Mathieu Chartier34067262017-04-06 13:55:46 -0700505 }
506 }
507 }
David Sehr7c80f2d2017-02-07 16:47:58 -0800508 return true;
509 }
510
Mathieu Chartier34067262017-04-06 13:55:46 -0700511 bool GetClassNamesAndMethods(const std::string& profile_file,
512 std::vector<std::unique_ptr<const DexFile>>* dex_files,
513 std::set<std::string>* out_lines) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800514 int fd = open(profile_file.c_str(), O_RDONLY);
515 if (!FdIsValid(fd)) {
516 LOG(ERROR) << "Cannot open " << profile_file << strerror(errno);
517 return false;
518 }
Mathieu Chartier34067262017-04-06 13:55:46 -0700519 if (!GetClassNamesAndMethods(fd, dex_files, out_lines)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800520 return false;
521 }
522 if (close(fd) < 0) {
523 PLOG(WARNING) << "Failed to close descriptor";
524 }
525 return true;
526 }
527
Mathieu Chartierea650f32017-05-24 12:04:13 -0700528 int DumpClassesAndMethods() {
David Sehr153da0e2017-02-15 08:54:51 -0800529 // Validate that at least one profile file or reference was specified.
530 if (profile_files_.empty() && profile_files_fd_.empty() &&
531 reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
532 Usage("No profile files or reference profile specified.");
533 }
David Sehr7c80f2d2017-02-07 16:47:58 -0800534 // Open apk/zip files and and read dex files.
535 MemMap::Init(); // for ZipArchive::OpenFromFd
536 // Open the dex files to get the names for classes.
537 std::vector<std::unique_ptr<const DexFile>> dex_files;
538 OpenApkFilesFromLocations(&dex_files);
539 // Build a vector of class names from individual profile files.
540 std::set<std::string> class_names;
541 if (!profile_files_fd_.empty()) {
542 for (int profile_file_fd : profile_files_fd_) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700543 if (!GetClassNamesAndMethods(profile_file_fd, &dex_files, &class_names)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800544 return -1;
545 }
546 }
547 }
548 if (!profile_files_.empty()) {
549 for (const std::string& profile_file : profile_files_) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700550 if (!GetClassNamesAndMethods(profile_file, &dex_files, &class_names)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800551 return -1;
552 }
553 }
554 }
555 // Concatenate class names from reference profile file.
556 if (FdIsValid(reference_profile_file_fd_)) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700557 if (!GetClassNamesAndMethods(reference_profile_file_fd_, &dex_files, &class_names)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800558 return -1;
559 }
560 }
561 if (!reference_profile_file_.empty()) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700562 if (!GetClassNamesAndMethods(reference_profile_file_, &dex_files, &class_names)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800563 return -1;
564 }
565 }
566 // Dump the class names.
567 std::string dump;
568 for (const std::string& class_name : class_names) {
569 dump += class_name + std::string("\n");
570 }
571 if (!FdIsValid(dump_output_to_fd_)) {
572 std::cout << dump;
573 } else {
574 unix_file::FdFile out_fd(dump_output_to_fd_, false /*check_usage*/);
575 if (!out_fd.WriteFully(dump.c_str(), dump.length())) {
576 return -1;
577 }
578 }
579 return 0;
580 }
581
Mathieu Chartier34067262017-04-06 13:55:46 -0700582 bool ShouldOnlyDumpClassesAndMethods() {
583 return dump_classes_and_methods_;
David Sehr7c80f2d2017-02-07 16:47:58 -0800584 }
585
586 // Read lines from the given file, dropping comments and empty lines. Post-process each line with
587 // the given function.
588 template <typename T>
589 static T* ReadCommentedInputFromFile(
590 const char* input_filename, std::function<std::string(const char*)>* process) {
591 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
592 if (input_file.get() == nullptr) {
593 LOG(ERROR) << "Failed to open input file " << input_filename;
594 return nullptr;
595 }
596 std::unique_ptr<T> result(
597 ReadCommentedInputStream<T>(*input_file, process));
598 input_file->close();
599 return result.release();
600 }
601
602 // Read lines from the given stream, dropping comments and empty lines. Post-process each line
603 // with the given function.
604 template <typename T>
605 static T* ReadCommentedInputStream(
606 std::istream& in_stream,
607 std::function<std::string(const char*)>* process) {
608 std::unique_ptr<T> output(new T());
609 while (in_stream.good()) {
610 std::string dot;
611 std::getline(in_stream, dot);
612 if (android::base::StartsWith(dot, "#") || dot.empty()) {
613 continue;
614 }
615 if (process != nullptr) {
616 std::string descriptor((*process)(dot.c_str()));
617 output->insert(output->end(), descriptor);
618 } else {
619 output->insert(output->end(), dot);
620 }
621 }
622 return output.release();
623 }
624
Calin Juravlee0ac1152017-02-13 19:03:47 -0800625 // Find class klass_descriptor in the given dex_files and store its reference
626 // in the out parameter class_ref.
627 // Return true if the definition of the class was found in any of the dex_files.
628 bool FindClass(const std::vector<std::unique_ptr<const DexFile>>& dex_files,
629 const std::string& klass_descriptor,
Mathieu Chartierdbddc222017-05-24 12:04:13 -0700630 /*out*/TypeReference* class_ref) {
Calin Juravle08556882017-05-26 16:40:45 -0700631 constexpr uint16_t kInvalidTypeIndex = std::numeric_limits<uint16_t>::max() - 1;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800632 for (const std::unique_ptr<const DexFile>& dex_file_ptr : dex_files) {
633 const DexFile* dex_file = dex_file_ptr.get();
Calin Juravle08556882017-05-26 16:40:45 -0700634 if (klass_descriptor == kInvalidClassDescriptor) {
635 if (kInvalidTypeIndex >= dex_file->NumTypeIds()) {
636 // The dex file does not contain all possible type ids which leaves us room
637 // to add an "invalid" type id.
638 class_ref->dex_file = dex_file;
639 class_ref->type_index = dex::TypeIndex(kInvalidTypeIndex);
640 return true;
641 } else {
642 // The dex file contains all possible type ids. We don't have any free type id
643 // that we can use as invalid.
644 continue;
645 }
646 }
647
Calin Juravlee0ac1152017-02-13 19:03:47 -0800648 const DexFile::TypeId* type_id = dex_file->FindTypeId(klass_descriptor.c_str());
649 if (type_id == nullptr) {
650 continue;
651 }
652 dex::TypeIndex type_index = dex_file->GetIndexForTypeId(*type_id);
653 if (dex_file->FindClassDef(type_index) == nullptr) {
654 // Class is only referenced in the current dex file but not defined in it.
655 continue;
656 }
657 class_ref->dex_file = dex_file;
658 class_ref->type_index = type_index;
659 return true;
660 }
661 return false;
662 }
663
Mathieu Chartier34067262017-04-06 13:55:46 -0700664 // Find the method specified by method_spec in the class class_ref.
Calin Juravle08556882017-05-26 16:40:45 -0700665 uint32_t FindMethodIndex(const TypeReference& class_ref,
666 const std::string& method_spec) {
667 const DexFile* dex_file = class_ref.dex_file;
668 if (method_spec == kInvalidMethod) {
669 constexpr uint16_t kInvalidMethodIndex = std::numeric_limits<uint16_t>::max() - 1;
670 return kInvalidMethodIndex >= dex_file->NumMethodIds()
671 ? kInvalidMethodIndex
Andreas Gampee2abbc62017-09-15 11:59:26 -0700672 : dex::kDexNoIndex;
Calin Juravle08556882017-05-26 16:40:45 -0700673 }
674
Calin Juravlee0ac1152017-02-13 19:03:47 -0800675 std::vector<std::string> name_and_signature;
676 Split(method_spec, kProfileParsingFirstCharInSignature, &name_and_signature);
677 if (name_and_signature.size() != 2) {
678 LOG(ERROR) << "Invalid method name and signature " << method_spec;
Andreas Gampee2abbc62017-09-15 11:59:26 -0700679 return dex::kDexNoIndex;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800680 }
Calin Juravle08556882017-05-26 16:40:45 -0700681
Calin Juravlee0ac1152017-02-13 19:03:47 -0800682 const std::string& name = name_and_signature[0];
683 const std::string& signature = kProfileParsingFirstCharInSignature + name_and_signature[1];
Calin Juravlee0ac1152017-02-13 19:03:47 -0800684
685 const DexFile::StringId* name_id = dex_file->FindStringId(name.c_str());
686 if (name_id == nullptr) {
Mathieu Chartier66aae3b2017-05-18 10:38:28 -0700687 LOG(WARNING) << "Could not find name: " << name;
Andreas Gampee2abbc62017-09-15 11:59:26 -0700688 return dex::kDexNoIndex;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800689 }
690 dex::TypeIndex return_type_idx;
691 std::vector<dex::TypeIndex> param_type_idxs;
692 if (!dex_file->CreateTypeList(signature, &return_type_idx, &param_type_idxs)) {
Mathieu Chartier66aae3b2017-05-18 10:38:28 -0700693 LOG(WARNING) << "Could not create type list" << signature;
Andreas Gampee2abbc62017-09-15 11:59:26 -0700694 return dex::kDexNoIndex;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800695 }
696 const DexFile::ProtoId* proto_id = dex_file->FindProtoId(return_type_idx, param_type_idxs);
697 if (proto_id == nullptr) {
Mathieu Chartier66aae3b2017-05-18 10:38:28 -0700698 LOG(WARNING) << "Could not find proto_id: " << name;
Andreas Gampee2abbc62017-09-15 11:59:26 -0700699 return dex::kDexNoIndex;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800700 }
701 const DexFile::MethodId* method_id = dex_file->FindMethodId(
702 dex_file->GetTypeId(class_ref.type_index), *name_id, *proto_id);
703 if (method_id == nullptr) {
Mathieu Chartier66aae3b2017-05-18 10:38:28 -0700704 LOG(WARNING) << "Could not find method_id: " << name;
Andreas Gampee2abbc62017-09-15 11:59:26 -0700705 return dex::kDexNoIndex;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800706 }
707
Mathieu Chartier34067262017-04-06 13:55:46 -0700708 return dex_file->GetIndexForMethodId(*method_id);
709 }
Calin Juravlee0ac1152017-02-13 19:03:47 -0800710
Mathieu Chartier34067262017-04-06 13:55:46 -0700711 // Given a method, return true if the method has a single INVOKE_VIRTUAL in its byte code.
712 // Upon success it returns true and stores the method index and the invoke dex pc
713 // in the output parameters.
714 // The format of the method spec is "inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;".
715 //
716 // TODO(calin): support INVOKE_INTERFACE and the range variants.
Mathieu Chartierdbddc222017-05-24 12:04:13 -0700717 bool HasSingleInvoke(const TypeReference& class_ref,
Mathieu Chartier34067262017-04-06 13:55:46 -0700718 uint16_t method_index,
719 /*out*/uint32_t* dex_pc) {
720 const DexFile* dex_file = class_ref.dex_file;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800721 uint32_t offset = dex_file->FindCodeItemOffset(
722 *dex_file->FindClassDef(class_ref.type_index),
Mathieu Chartier34067262017-04-06 13:55:46 -0700723 method_index);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800724 const DexFile::CodeItem* code_item = dex_file->GetCodeItem(offset);
725
726 bool found_invoke = false;
727 for (CodeItemIterator it(*code_item); !it.Done(); it.Advance()) {
728 if (it.CurrentInstruction().Opcode() == Instruction::INVOKE_VIRTUAL) {
729 if (found_invoke) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700730 LOG(ERROR) << "Multiple invoke INVOKE_VIRTUAL found: "
731 << dex_file->PrettyMethod(method_index);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800732 return false;
733 }
734 found_invoke = true;
735 *dex_pc = it.CurrentDexPc();
736 }
737 }
738 if (!found_invoke) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700739 LOG(ERROR) << "Could not find any INVOKE_VIRTUAL: " << dex_file->PrettyMethod(method_index);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800740 }
741 return found_invoke;
742 }
743
744 // Process a line defining a class or a method and its inline caches.
745 // Upon success return true and add the class or the method info to profile.
Calin Juravle589e71e2017-03-03 16:05:05 -0800746 // The possible line formats are:
747 // "LJustTheCass;".
Calin Juravlee0ac1152017-02-13 19:03:47 -0800748 // "LTestInline;->inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;".
Calin Juravle08556882017-05-26 16:40:45 -0700749 // "LTestInline;->inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,invalid_class".
Calin Juravle589e71e2017-03-03 16:05:05 -0800750 // "LTestInline;->inlineMissingTypes(LSuper;)I+missing_types".
751 // "LTestInline;->inlineNoInlineCaches(LSuper;)I".
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700752 // "LTestInline;->*".
Calin Juravle08556882017-05-26 16:40:45 -0700753 // "invalid_class".
754 // "LTestInline;->invalid_method".
Calin Juravlee0ac1152017-02-13 19:03:47 -0800755 // The method and classes are searched only in the given dex files.
756 bool ProcessLine(const std::vector<std::unique_ptr<const DexFile>>& dex_files,
757 const std::string& line,
758 /*out*/ProfileCompilationInfo* profile) {
759 std::string klass;
760 std::string method_str;
Mathieu Chartierea650f32017-05-24 12:04:13 -0700761 bool is_hot = false;
762 bool is_startup = false;
763 bool is_post_startup = false;
764 const size_t method_sep_index = line.find(kMethodSep, 0);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800765 if (method_sep_index == std::string::npos) {
Mathieu Chartierea650f32017-05-24 12:04:13 -0700766 klass = line.substr(0);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800767 } else {
Mathieu Chartierea650f32017-05-24 12:04:13 -0700768 // The method prefix flags are only valid for method strings.
769 size_t start_index = 0;
770 while (start_index < line.size() && line[start_index] != 'L') {
771 const char c = line[start_index];
772 if (c == kMethodFlagStringHot) {
773 is_hot = true;
774 } else if (c == kMethodFlagStringStartup) {
775 is_startup = true;
776 } else if (c == kMethodFlagStringPostStartup) {
777 is_post_startup = true;
778 } else {
779 LOG(WARNING) << "Invalid flag " << c;
780 return false;
781 }
782 ++start_index;
783 }
784 klass = line.substr(start_index, method_sep_index - start_index);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800785 method_str = line.substr(method_sep_index + kMethodSep.size());
786 }
787
Mathieu Chartierdbddc222017-05-24 12:04:13 -0700788 TypeReference class_ref;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800789 if (!FindClass(dex_files, klass, &class_ref)) {
Mathieu Chartier3f121342017-03-06 11:16:20 -0800790 LOG(WARNING) << "Could not find class: " << klass;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800791 return false;
792 }
793
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700794 if (method_str.empty() || method_str == kClassAllMethods) {
795 // Start by adding the class.
Calin Juravlee0ac1152017-02-13 19:03:47 -0800796 std::set<DexCacheResolvedClasses> resolved_class_set;
797 const DexFile* dex_file = class_ref.dex_file;
798 const auto& dex_resolved_classes = resolved_class_set.emplace(
799 dex_file->GetLocation(),
800 dex_file->GetBaseLocation(),
Mathieu Chartierea650f32017-05-24 12:04:13 -0700801 dex_file->GetLocationChecksum(),
802 dex_file->NumMethodIds());
Calin Juravlee0ac1152017-02-13 19:03:47 -0800803 dex_resolved_classes.first->AddClass(class_ref.type_index);
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700804 std::vector<ProfileMethodInfo> methods;
805 if (method_str == kClassAllMethods) {
806 // Add all of the methods.
807 const DexFile::ClassDef* class_def = dex_file->FindClassDef(class_ref.type_index);
808 const uint8_t* class_data = dex_file->GetClassData(*class_def);
809 if (class_data != nullptr) {
810 ClassDataItemIterator it(*dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700811 it.SkipAllFields();
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700812 while (it.HasNextDirectMethod() || it.HasNextVirtualMethod()) {
813 if (it.GetMethodCodeItemOffset() != 0) {
814 // Add all of the methods that have code to the profile.
815 const uint32_t method_idx = it.GetMemberIndex();
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700816 methods.push_back(ProfileMethodInfo(MethodReference(dex_file, method_idx)));
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700817 }
818 it.Next();
819 }
820 }
821 }
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700822 // TODO: Check return values?
823 profile->AddMethods(methods);
824 profile->AddClasses(resolved_class_set);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800825 return true;
826 }
827
828 // Process the method.
829 std::string method_spec;
830 std::vector<std::string> inline_cache_elems;
831
Mathieu Chartierea650f32017-05-24 12:04:13 -0700832 // If none of the flags are set, default to hot.
833 is_hot = is_hot || (!is_hot && !is_startup && !is_post_startup);
834
Calin Juravlee0ac1152017-02-13 19:03:47 -0800835 std::vector<std::string> method_elems;
Calin Juravle589e71e2017-03-03 16:05:05 -0800836 bool is_missing_types = false;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800837 Split(method_str, kProfileParsingInlineChacheSep, &method_elems);
838 if (method_elems.size() == 2) {
839 method_spec = method_elems[0];
Calin Juravle589e71e2017-03-03 16:05:05 -0800840 is_missing_types = method_elems[1] == kMissingTypesMarker;
841 if (!is_missing_types) {
842 Split(method_elems[1], kProfileParsingTypeSep, &inline_cache_elems);
843 }
Calin Juravlee0ac1152017-02-13 19:03:47 -0800844 } else if (method_elems.size() == 1) {
845 method_spec = method_elems[0];
846 } else {
847 LOG(ERROR) << "Invalid method line: " << line;
848 return false;
849 }
850
Mathieu Chartier34067262017-04-06 13:55:46 -0700851 const uint32_t method_index = FindMethodIndex(class_ref, method_spec);
Andreas Gampee2abbc62017-09-15 11:59:26 -0700852 if (method_index == dex::kDexNoIndex) {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800853 return false;
854 }
Mathieu Chartier34067262017-04-06 13:55:46 -0700855
Mathieu Chartier34067262017-04-06 13:55:46 -0700856 std::vector<ProfileMethodInfo::ProfileInlineCache> inline_caches;
857 if (is_missing_types || !inline_cache_elems.empty()) {
858 uint32_t dex_pc;
859 if (!HasSingleInvoke(class_ref, method_index, &dex_pc)) {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800860 return false;
861 }
Mathieu Chartierdbddc222017-05-24 12:04:13 -0700862 std::vector<TypeReference> classes(inline_cache_elems.size());
Mathieu Chartier34067262017-04-06 13:55:46 -0700863 size_t class_it = 0;
864 for (const std::string& ic_class : inline_cache_elems) {
865 if (!FindClass(dex_files, ic_class, &(classes[class_it++]))) {
866 LOG(ERROR) << "Could not find class: " << ic_class;
867 return false;
868 }
869 }
870 inline_caches.emplace_back(dex_pc, is_missing_types, classes);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800871 }
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700872 MethodReference ref(class_ref.dex_file, method_index);
Mathieu Chartierea650f32017-05-24 12:04:13 -0700873 if (is_hot) {
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700874 profile->AddMethod(ProfileMethodInfo(ref, inline_caches));
Mathieu Chartierea650f32017-05-24 12:04:13 -0700875 }
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700876 uint32_t flags = 0;
877 using Hotness = ProfileCompilationInfo::MethodHotness;
Mathieu Chartierea650f32017-05-24 12:04:13 -0700878 if (is_startup) {
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700879 flags |= Hotness::kFlagStartup;
Mathieu Chartierea650f32017-05-24 12:04:13 -0700880 }
881 if (is_post_startup) {
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700882 flags |= Hotness::kFlagPostStartup;
883 }
884 if (flags != 0) {
885 if (!profile->AddMethodIndex(static_cast<Hotness::Flag>(flags), ref)) {
Mathieu Chartierea650f32017-05-24 12:04:13 -0700886 return false;
887 }
Mathieu Chartiere46f3a82017-06-19 19:54:12 -0700888 DCHECK(profile->GetMethodHotness(ref).IsInProfile());
Mathieu Chartierea650f32017-05-24 12:04:13 -0700889 }
Calin Juravlee0ac1152017-02-13 19:03:47 -0800890 return true;
891 }
892
Mathieu Chartier2f794552017-06-19 10:58:08 -0700893 int OpenReferenceProfile() const {
894 int fd = reference_profile_file_fd_;
895 if (!FdIsValid(fd)) {
896 CHECK(!reference_profile_file_.empty());
897 fd = open(reference_profile_file_.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
898 if (fd < 0) {
899 LOG(ERROR) << "Cannot open " << reference_profile_file_ << strerror(errno);
900 return kInvalidFd;
901 }
902 }
903 return fd;
904 }
905
Calin Juravlee0ac1152017-02-13 19:03:47 -0800906 // Creates a profile from a human friendly textual representation.
907 // The expected input format is:
908 // # Classes
909 // Ljava/lang/Comparable;
910 // Ljava/lang/Math;
911 // # Methods with inline caches
912 // LTestInline;->inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;
913 // LTestInline;->noInlineCache(LSuper;)I
David Sehr7c80f2d2017-02-07 16:47:58 -0800914 int CreateProfile() {
David Sehr153da0e2017-02-15 08:54:51 -0800915 // Validate parameters for this command.
916 if (apk_files_.empty() && apks_fd_.empty()) {
917 Usage("APK files must be specified");
918 }
919 if (dex_locations_.empty()) {
920 Usage("DEX locations must be specified");
921 }
922 if (reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
923 Usage("Reference profile must be specified with --reference-profile-file or "
924 "--reference-profile-file-fd");
925 }
926 if (!profile_files_.empty() || !profile_files_fd_.empty()) {
927 Usage("Profile must be specified with --reference-profile-file or "
928 "--reference-profile-file-fd");
929 }
930 // for ZipArchive::OpenFromFd
931 MemMap::Init();
David Sehr7c80f2d2017-02-07 16:47:58 -0800932 // Open the profile output file if needed.
Mathieu Chartier2f794552017-06-19 10:58:08 -0700933 int fd = OpenReferenceProfile();
David Sehr7c80f2d2017-02-07 16:47:58 -0800934 if (!FdIsValid(fd)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800935 return -1;
David Sehr7c80f2d2017-02-07 16:47:58 -0800936 }
Calin Juravlee0ac1152017-02-13 19:03:47 -0800937 // Read the user-specified list of classes and methods.
David Sehr7c80f2d2017-02-07 16:47:58 -0800938 std::unique_ptr<std::unordered_set<std::string>>
Calin Juravlee0ac1152017-02-13 19:03:47 -0800939 user_lines(ReadCommentedInputFromFile<std::unordered_set<std::string>>(
David Sehr7c80f2d2017-02-07 16:47:58 -0800940 create_profile_from_file_.c_str(), nullptr)); // No post-processing.
Calin Juravlee0ac1152017-02-13 19:03:47 -0800941
942 // Open the dex files to look up classes and methods.
David Sehr7c80f2d2017-02-07 16:47:58 -0800943 std::vector<std::unique_ptr<const DexFile>> dex_files;
944 OpenApkFilesFromLocations(&dex_files);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800945
946 // Process the lines one by one and add the successful ones to the profile.
David Sehr7c80f2d2017-02-07 16:47:58 -0800947 ProfileCompilationInfo info;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800948
949 for (const auto& line : *user_lines) {
950 ProcessLine(dex_files, line, &info);
951 }
952
David Sehr7c80f2d2017-02-07 16:47:58 -0800953 // Write the profile file.
954 CHECK(info.Save(fd));
955 if (close(fd) < 0) {
956 PLOG(WARNING) << "Failed to close descriptor";
957 }
958 return 0;
959 }
960
Mathieu Chartier2f794552017-06-19 10:58:08 -0700961 bool ShouldCreateBootProfile() const {
962 return generate_boot_image_profile_;
963 }
964
965 int CreateBootProfile() {
966 // Initialize memmap since it's required to open dex files.
967 MemMap::Init();
968 // Open the profile output file.
969 const int reference_fd = OpenReferenceProfile();
970 if (!FdIsValid(reference_fd)) {
971 PLOG(ERROR) << "Error opening reference profile";
972 return -1;
973 }
974 // Open the dex files.
975 std::vector<std::unique_ptr<const DexFile>> dex_files;
976 OpenApkFilesFromLocations(&dex_files);
977 if (dex_files.empty()) {
978 PLOG(ERROR) << "Expected dex files for creating boot profile";
979 return -2;
980 }
981 // Open the input profiles.
982 std::vector<std::unique_ptr<const ProfileCompilationInfo>> profiles;
983 if (!profile_files_fd_.empty()) {
984 for (int profile_file_fd : profile_files_fd_) {
985 std::unique_ptr<const ProfileCompilationInfo> profile(LoadProfile("", profile_file_fd));
986 if (profile == nullptr) {
987 return -3;
988 }
989 profiles.emplace_back(std::move(profile));
990 }
991 }
992 if (!profile_files_.empty()) {
993 for (const std::string& profile_file : profile_files_) {
994 std::unique_ptr<const ProfileCompilationInfo> profile(LoadProfile(profile_file, kInvalidFd));
995 if (profile == nullptr) {
996 return -4;
997 }
998 profiles.emplace_back(std::move(profile));
999 }
1000 }
1001 ProfileCompilationInfo out_profile;
1002 GenerateBootImageProfile(dex_files,
1003 profiles,
1004 boot_image_options_,
1005 VLOG_IS_ON(profiler),
1006 &out_profile);
1007 out_profile.Save(reference_fd);
1008 close(reference_fd);
1009 return 0;
1010 }
1011
David Sehr7c80f2d2017-02-07 16:47:58 -08001012 bool ShouldCreateProfile() {
1013 return !create_profile_from_file_.empty();
1014 }
1015
Calin Juravle7bcdb532016-06-07 16:14:47 +01001016 int GenerateTestProfile() {
David Sehr153da0e2017-02-15 08:54:51 -08001017 // Validate parameters for this command.
Shubham Ajmerad704f0b2017-07-26 16:33:35 -07001018 if (test_profile_method_percerntage_ > 100) {
1019 Usage("Invalid percentage for --generate-test-profile-method-percentage");
David Sehr153da0e2017-02-15 08:54:51 -08001020 }
Shubham Ajmerad704f0b2017-07-26 16:33:35 -07001021 if (test_profile_class_percentage_ > 100) {
1022 Usage("Invalid percentage for --generate-test-profile-class-percentage");
David Sehr153da0e2017-02-15 08:54:51 -08001023 }
Jeff Haof0a31f82017-03-27 15:50:37 -07001024 // If given APK files or DEX locations, check that they're ok.
1025 if (!apk_files_.empty() || !apks_fd_.empty() || !dex_locations_.empty()) {
1026 if (apk_files_.empty() && apks_fd_.empty()) {
1027 Usage("APK files must be specified when passing DEX locations to --generate-test-profile");
1028 }
1029 if (dex_locations_.empty()) {
1030 Usage("DEX locations must be specified when passing APK files to --generate-test-profile");
1031 }
1032 }
David Sehr153da0e2017-02-15 08:54:51 -08001033 // ShouldGenerateTestProfile confirms !test_profile_.empty().
George Burgess IV71f77262016-10-25 13:08:17 -07001034 int profile_test_fd = open(test_profile_.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
Calin Juravle7bcdb532016-06-07 16:14:47 +01001035 if (profile_test_fd < 0) {
David Sehr7c80f2d2017-02-07 16:47:58 -08001036 LOG(ERROR) << "Cannot open " << test_profile_ << strerror(errno);
Calin Juravle7bcdb532016-06-07 16:14:47 +01001037 return -1;
1038 }
Jeff Haof0a31f82017-03-27 15:50:37 -07001039 bool result;
1040 if (apk_files_.empty() && apks_fd_.empty() && dex_locations_.empty()) {
1041 result = ProfileCompilationInfo::GenerateTestProfile(profile_test_fd,
1042 test_profile_num_dex_,
Shubham Ajmerad704f0b2017-07-26 16:33:35 -07001043 test_profile_method_percerntage_,
1044 test_profile_class_percentage_,
Jeff Haof0a31f82017-03-27 15:50:37 -07001045 test_profile_seed_);
1046 } else {
1047 // Initialize MemMap for ZipArchive::OpenFromFd.
1048 MemMap::Init();
1049 // Open the dex files to look up classes and methods.
1050 std::vector<std::unique_ptr<const DexFile>> dex_files;
1051 OpenApkFilesFromLocations(&dex_files);
1052 // Create a random profile file based on the set of dex files.
1053 result = ProfileCompilationInfo::GenerateTestProfile(profile_test_fd,
1054 dex_files,
Shubham Ajmerad704f0b2017-07-26 16:33:35 -07001055 test_profile_method_percerntage_,
1056 test_profile_class_percentage_,
Jeff Haof0a31f82017-03-27 15:50:37 -07001057 test_profile_seed_);
1058 }
Calin Juravle7bcdb532016-06-07 16:14:47 +01001059 close(profile_test_fd); // ignore close result.
1060 return result ? 0 : -1;
1061 }
1062
1063 bool ShouldGenerateTestProfile() {
1064 return !test_profile_.empty();
1065 }
1066
Calin Juravle2e2db782016-02-23 12:00:03 +00001067 private:
1068 static void ParseFdForCollection(const StringPiece& option,
1069 const char* arg_name,
1070 std::vector<int>* fds) {
1071 int fd;
1072 ParseUintOption(option, arg_name, &fd, Usage);
1073 fds->push_back(fd);
1074 }
1075
1076 static void CloseAllFds(const std::vector<int>& fds, const char* descriptor) {
1077 for (size_t i = 0; i < fds.size(); i++) {
1078 if (close(fds[i]) < 0) {
1079 PLOG(WARNING) << "Failed to close descriptor for " << descriptor << " at index " << i;
1080 }
1081 }
1082 }
1083
1084 void LogCompletionTime() {
David Sehr52683cf2016-05-05 09:02:38 -07001085 static constexpr uint64_t kLogThresholdTime = MsToNs(100); // 100ms
1086 uint64_t time_taken = NanoTime() - start_ns_;
David Sehred793012016-05-05 13:39:43 -07001087 if (time_taken > kLogThresholdTime) {
David Sehrd8557182016-05-06 12:29:35 -07001088 LOG(WARNING) << "profman took " << PrettyDuration(time_taken);
David Sehred793012016-05-05 13:39:43 -07001089 }
Calin Juravle2e2db782016-02-23 12:00:03 +00001090 }
1091
1092 std::vector<std::string> profile_files_;
1093 std::vector<int> profile_files_fd_;
David Sehr546d24f2016-06-02 10:46:19 -07001094 std::vector<std::string> dex_locations_;
David Sehr7c80f2d2017-02-07 16:47:58 -08001095 std::vector<std::string> apk_files_;
David Sehr546d24f2016-06-02 10:46:19 -07001096 std::vector<int> apks_fd_;
Calin Juravle2e2db782016-02-23 12:00:03 +00001097 std::string reference_profile_file_;
1098 int reference_profile_file_fd_;
David Sehr4fcdd6d2016-05-24 14:52:31 -07001099 bool dump_only_;
Mathieu Chartier34067262017-04-06 13:55:46 -07001100 bool dump_classes_and_methods_;
Mathieu Chartier2f794552017-06-19 10:58:08 -07001101 bool generate_boot_image_profile_;
David Sehr4fcdd6d2016-05-24 14:52:31 -07001102 int dump_output_to_fd_;
Mathieu Chartier2f794552017-06-19 10:58:08 -07001103 BootImageOptions boot_image_options_;
Calin Juravle7bcdb532016-06-07 16:14:47 +01001104 std::string test_profile_;
David Sehr7c80f2d2017-02-07 16:47:58 -08001105 std::string create_profile_from_file_;
Calin Juravle7bcdb532016-06-07 16:14:47 +01001106 uint16_t test_profile_num_dex_;
Shubham Ajmerad704f0b2017-07-26 16:33:35 -07001107 uint16_t test_profile_method_percerntage_;
1108 uint16_t test_profile_class_percentage_;
Jeff Haof0a31f82017-03-27 15:50:37 -07001109 uint32_t test_profile_seed_;
Calin Juravle2e2db782016-02-23 12:00:03 +00001110 uint64_t start_ns_;
1111};
1112
1113// See ProfileAssistant::ProcessingResult for return codes.
1114static int profman(int argc, char** argv) {
1115 ProfMan profman;
1116
1117 // Parse arguments. Argument mistakes will lead to exit(EXIT_FAILURE) in UsageError.
1118 profman.ParseArgs(argc, argv);
1119
Calin Juravle7bcdb532016-06-07 16:14:47 +01001120 if (profman.ShouldGenerateTestProfile()) {
1121 return profman.GenerateTestProfile();
1122 }
Calin Juravle876f3502016-03-24 16:16:34 +00001123 if (profman.ShouldOnlyDumpProfile()) {
1124 return profman.DumpProfileInfo();
1125 }
Mathieu Chartier34067262017-04-06 13:55:46 -07001126 if (profman.ShouldOnlyDumpClassesAndMethods()) {
Mathieu Chartierea650f32017-05-24 12:04:13 -07001127 return profman.DumpClassesAndMethods();
David Sehr7c80f2d2017-02-07 16:47:58 -08001128 }
1129 if (profman.ShouldCreateProfile()) {
1130 return profman.CreateProfile();
1131 }
Mathieu Chartier2f794552017-06-19 10:58:08 -07001132
1133 if (profman.ShouldCreateBootProfile()) {
1134 return profman.CreateBootProfile();
1135 }
Calin Juravle2e2db782016-02-23 12:00:03 +00001136 // Process profile information and assess if we need to do a profile guided compilation.
1137 // This operation involves I/O.
1138 return profman.ProcessProfiles();
1139}
1140
1141} // namespace art
1142
1143int main(int argc, char **argv) {
1144 return art::profman(argc, argv);
1145}
1146