blob: e565171265cee423d1dc05aaf204aa6f4ab04894 [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
Calin Juravle876f3502016-03-24 16:16:34 +000017#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"
Calin Juravlee0ac1152017-02-13 19:03:47 -080039#include "bytecode_utils.h"
David Sehr4fcdd6d2016-05-24 14:52:31 -070040#include "dex_file.h"
Calin Juravle33083d62017-01-18 15:29:12 -080041#include "jit/profile_compilation_info.h"
Mathieu Chartierdbddc222017-05-24 12:04:13 -070042#include "profile_assistant.h"
David Sehrf57589f2016-10-17 10:09:33 -070043#include "runtime.h"
Mathieu Chartierdbddc222017-05-24 12:04:13 -070044#include "type_reference.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000045#include "utils.h"
David Sehr546d24f2016-06-02 10:46:19 -070046#include "zip_archive.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000047
48namespace art {
49
50static int original_argc;
51static char** original_argv;
52
53static std::string CommandLine() {
54 std::vector<std::string> command;
55 for (int i = 0; i < original_argc; ++i) {
56 command.push_back(original_argv[i]);
57 }
Andreas Gampe9186ced2016-12-12 14:28:21 -080058 return android::base::Join(command, ' ');
Calin Juravle2e2db782016-02-23 12:00:03 +000059}
60
David Sehr4fcdd6d2016-05-24 14:52:31 -070061static constexpr int kInvalidFd = -1;
62
63static bool FdIsValid(int fd) {
64 return fd != kInvalidFd;
65}
66
Calin Juravle2e2db782016-02-23 12:00:03 +000067static void UsageErrorV(const char* fmt, va_list ap) {
68 std::string error;
Andreas Gampe46ee31b2016-12-14 10:11:49 -080069 android::base::StringAppendV(&error, fmt, ap);
Calin Juravle2e2db782016-02-23 12:00:03 +000070 LOG(ERROR) << error;
71}
72
73static void UsageError(const char* fmt, ...) {
74 va_list ap;
75 va_start(ap, fmt);
76 UsageErrorV(fmt, ap);
77 va_end(ap);
78}
79
80NO_RETURN static void Usage(const char *fmt, ...) {
81 va_list ap;
82 va_start(ap, fmt);
83 UsageErrorV(fmt, ap);
84 va_end(ap);
85
86 UsageError("Command: %s", CommandLine().c_str());
87 UsageError("Usage: profman [options]...");
88 UsageError("");
David Sehr4fcdd6d2016-05-24 14:52:31 -070089 UsageError(" --dump-only: dumps the content of the specified profile files");
90 UsageError(" to standard output (default) in a human readable form.");
91 UsageError("");
David Sehr7c80f2d2017-02-07 16:47:58 -080092 UsageError(" --dump-output-to-fd=<number>: redirects --dump-only output to a file descriptor.");
93 UsageError("");
Mathieu Chartier34067262017-04-06 13:55:46 -070094 UsageError(" --dump-classes-and-methods: dumps a sorted list of classes and methods that are");
95 UsageError(" in the specified profile file to standard output (default) in a human");
96 UsageError(" readable form. The output is valid input for --create-profile-from");
Calin Juravle876f3502016-03-24 16:16:34 +000097 UsageError("");
Calin Juravle2e2db782016-02-23 12:00:03 +000098 UsageError(" --profile-file=<filename>: specify profiler output file to use for compilation.");
99 UsageError(" Can be specified multiple time, in which case the data from the different");
100 UsageError(" profiles will be aggregated.");
101 UsageError("");
102 UsageError(" --profile-file-fd=<number>: same as --profile-file but accepts a file descriptor.");
103 UsageError(" Cannot be used together with --profile-file.");
104 UsageError("");
105 UsageError(" --reference-profile-file=<filename>: specify a reference profile.");
106 UsageError(" The data in this file will be compared with the data obtained by merging");
107 UsageError(" all the files specified with --profile-file or --profile-file-fd.");
108 UsageError(" If the exit code is EXIT_COMPILE then all --profile-file will be merged into");
109 UsageError(" --reference-profile-file. ");
110 UsageError("");
111 UsageError(" --reference-profile-file-fd=<number>: same as --reference-profile-file but");
112 UsageError(" accepts a file descriptor. Cannot be used together with");
113 UsageError(" --reference-profile-file.");
David Sehr7c80f2d2017-02-07 16:47:58 -0800114 UsageError("");
Calin Juravle7bcdb532016-06-07 16:14:47 +0100115 UsageError(" --generate-test-profile=<filename>: generates a random profile file for testing.");
116 UsageError(" --generate-test-profile-num-dex=<number>: number of dex files that should be");
117 UsageError(" included in the generated profile. Defaults to 20.");
118 UsageError(" --generate-test-profile-method-ratio=<number>: the percentage from the maximum");
119 UsageError(" number of methods that should be generated. Defaults to 5.");
120 UsageError(" --generate-test-profile-class-ratio=<number>: the percentage from the maximum");
121 UsageError(" number of classes that should be generated. Defaults to 5.");
Jeff Haof0a31f82017-03-27 15:50:37 -0700122 UsageError(" --generate-test-profile-seed=<number>: seed for random number generator used when");
123 UsageError(" generating random test profiles. Defaults to using NanoTime.");
Calin Juravle7bcdb532016-06-07 16:14:47 +0100124 UsageError("");
Mathieu Chartier34067262017-04-06 13:55:46 -0700125 UsageError(" --create-profile-from=<filename>: creates a profile from a list of classes and");
126 UsageError(" methods.");
David Sehr7c80f2d2017-02-07 16:47:58 -0800127 UsageError("");
David Sehr546d24f2016-06-02 10:46:19 -0700128 UsageError(" --dex-location=<string>: location string to use with corresponding");
129 UsageError(" apk-fd to find dex files");
David Sehr4fcdd6d2016-05-24 14:52:31 -0700130 UsageError("");
David Sehr546d24f2016-06-02 10:46:19 -0700131 UsageError(" --apk-fd=<number>: file descriptor containing an open APK to");
David Sehr4fcdd6d2016-05-24 14:52:31 -0700132 UsageError(" search for dex files");
David Sehr7c80f2d2017-02-07 16:47:58 -0800133 UsageError(" --apk-=<filename>: an APK to search for dex files");
David Sehr4fcdd6d2016-05-24 14:52:31 -0700134 UsageError("");
Calin Juravle2e2db782016-02-23 12:00:03 +0000135
136 exit(EXIT_FAILURE);
137}
138
Calin Juravle7bcdb532016-06-07 16:14:47 +0100139// Note: make sure you update the Usage if you change these values.
140static constexpr uint16_t kDefaultTestProfileNumDex = 20;
141static constexpr uint16_t kDefaultTestProfileMethodRatio = 5;
142static constexpr uint16_t kDefaultTestProfileClassRatio = 5;
143
Calin Juravlee0ac1152017-02-13 19:03:47 -0800144// Separators used when parsing human friendly representation of profiles.
145static const std::string kMethodSep = "->";
Calin Juravle589e71e2017-03-03 16:05:05 -0800146static const std::string kMissingTypesMarker = "missing_types";
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700147static const std::string kClassAllMethods = "*";
Calin Juravlee0ac1152017-02-13 19:03:47 -0800148static constexpr char kProfileParsingInlineChacheSep = '+';
149static constexpr char kProfileParsingTypeSep = ',';
150static constexpr char kProfileParsingFirstCharInSignature = '(';
151
152// TODO(calin): This class has grown too much from its initial design. Split the functionality
153// into smaller, more contained pieces.
Calin Juravle2e2db782016-02-23 12:00:03 +0000154class ProfMan FINAL {
155 public:
156 ProfMan() :
David Sehr4fcdd6d2016-05-24 14:52:31 -0700157 reference_profile_file_fd_(kInvalidFd),
158 dump_only_(false),
Mathieu Chartier34067262017-04-06 13:55:46 -0700159 dump_classes_and_methods_(false),
David Sehr4fcdd6d2016-05-24 14:52:31 -0700160 dump_output_to_fd_(kInvalidFd),
Calin Juravle7bcdb532016-06-07 16:14:47 +0100161 test_profile_num_dex_(kDefaultTestProfileNumDex),
162 test_profile_method_ratio_(kDefaultTestProfileMethodRatio),
163 test_profile_class_ratio_(kDefaultTestProfileClassRatio),
Jeff Haof0a31f82017-03-27 15:50:37 -0700164 test_profile_seed_(NanoTime()),
Calin Juravle2e2db782016-02-23 12:00:03 +0000165 start_ns_(NanoTime()) {}
166
167 ~ProfMan() {
168 LogCompletionTime();
169 }
170
171 void ParseArgs(int argc, char **argv) {
172 original_argc = argc;
173 original_argv = argv;
174
David Sehrf57589f2016-10-17 10:09:33 -0700175 InitLogging(argv, Runtime::Aborter);
Calin Juravle2e2db782016-02-23 12:00:03 +0000176
177 // Skip over the command name.
178 argv++;
179 argc--;
180
181 if (argc == 0) {
182 Usage("No arguments specified");
183 }
184
185 for (int i = 0; i < argc; ++i) {
186 const StringPiece option(argv[i]);
187 const bool log_options = false;
188 if (log_options) {
Calin Juravle876f3502016-03-24 16:16:34 +0000189 LOG(INFO) << "profman: option[" << i << "]=" << argv[i];
Calin Juravle2e2db782016-02-23 12:00:03 +0000190 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700191 if (option == "--dump-only") {
192 dump_only_ = true;
Mathieu Chartier34067262017-04-06 13:55:46 -0700193 } else if (option == "--dump-classes-and-methods") {
194 dump_classes_and_methods_ = true;
David Sehr7c80f2d2017-02-07 16:47:58 -0800195 } else if (option.starts_with("--create-profile-from=")) {
196 create_profile_from_file_ = option.substr(strlen("--create-profile-from=")).ToString();
David Sehr4fcdd6d2016-05-24 14:52:31 -0700197 } else if (option.starts_with("--dump-output-to-fd=")) {
198 ParseUintOption(option, "--dump-output-to-fd", &dump_output_to_fd_, Usage);
Calin Juravle876f3502016-03-24 16:16:34 +0000199 } else if (option.starts_with("--profile-file=")) {
Calin Juravle2e2db782016-02-23 12:00:03 +0000200 profile_files_.push_back(option.substr(strlen("--profile-file=")).ToString());
201 } else if (option.starts_with("--profile-file-fd=")) {
202 ParseFdForCollection(option, "--profile-file-fd", &profile_files_fd_);
203 } else if (option.starts_with("--reference-profile-file=")) {
204 reference_profile_file_ = option.substr(strlen("--reference-profile-file=")).ToString();
205 } else if (option.starts_with("--reference-profile-file-fd=")) {
206 ParseUintOption(option, "--reference-profile-file-fd", &reference_profile_file_fd_, Usage);
David Sehr546d24f2016-06-02 10:46:19 -0700207 } else if (option.starts_with("--dex-location=")) {
208 dex_locations_.push_back(option.substr(strlen("--dex-location=")).ToString());
209 } else if (option.starts_with("--apk-fd=")) {
210 ParseFdForCollection(option, "--apk-fd", &apks_fd_);
David Sehr7c80f2d2017-02-07 16:47:58 -0800211 } else if (option.starts_with("--apk=")) {
212 apk_files_.push_back(option.substr(strlen("--apk=")).ToString());
Calin Juravle7bcdb532016-06-07 16:14:47 +0100213 } else if (option.starts_with("--generate-test-profile=")) {
214 test_profile_ = option.substr(strlen("--generate-test-profile=")).ToString();
215 } else if (option.starts_with("--generate-test-profile-num-dex=")) {
216 ParseUintOption(option,
217 "--generate-test-profile-num-dex",
218 &test_profile_num_dex_,
219 Usage);
220 } else if (option.starts_with("--generate-test-profile-method-ratio")) {
221 ParseUintOption(option,
222 "--generate-test-profile-method-ratio",
223 &test_profile_method_ratio_,
224 Usage);
225 } else if (option.starts_with("--generate-test-profile-class-ratio")) {
226 ParseUintOption(option,
227 "--generate-test-profile-class-ratio",
228 &test_profile_class_ratio_,
229 Usage);
Jeff Haof0a31f82017-03-27 15:50:37 -0700230 } else if (option.starts_with("--generate-test-profile-seed=")) {
231 ParseUintOption(option, "--generate-test-profile-seed", &test_profile_seed_, Usage);
Calin Juravle2e2db782016-02-23 12:00:03 +0000232 } else {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700233 Usage("Unknown argument '%s'", option.data());
Calin Juravle2e2db782016-02-23 12:00:03 +0000234 }
235 }
236
David Sehr153da0e2017-02-15 08:54:51 -0800237 // Validate global consistency between file/fd options.
Calin Juravle2e2db782016-02-23 12:00:03 +0000238 if (!profile_files_.empty() && !profile_files_fd_.empty()) {
239 Usage("Profile files should not be specified with both --profile-file-fd and --profile-file");
240 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700241 if (!reference_profile_file_.empty() && FdIsValid(reference_profile_file_fd_)) {
242 Usage("Reference profile should not be specified with both "
243 "--reference-profile-file-fd and --reference-profile-file");
244 }
David Sehr153da0e2017-02-15 08:54:51 -0800245 if (!apk_files_.empty() && !apks_fd_.empty()) {
246 Usage("APK files should not be specified with both --apk-fd and --apk");
Calin Juravle2e2db782016-02-23 12:00:03 +0000247 }
248 }
249
250 ProfileAssistant::ProcessingResult ProcessProfiles() {
David Sehr153da0e2017-02-15 08:54:51 -0800251 // Validate that at least one profile file was passed, as well as a reference profile.
252 if (profile_files_.empty() && profile_files_fd_.empty()) {
253 Usage("No profile files specified.");
254 }
255 if (reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
256 Usage("No reference profile file specified.");
257 }
258 if ((!profile_files_.empty() && FdIsValid(reference_profile_file_fd_)) ||
259 (!profile_files_fd_.empty() && !FdIsValid(reference_profile_file_fd_))) {
260 Usage("Options --profile-file-fd and --reference-profile-file-fd "
261 "should only be used together");
262 }
Calin Juravle2e2db782016-02-23 12:00:03 +0000263 ProfileAssistant::ProcessingResult result;
264 if (profile_files_.empty()) {
265 // The file doesn't need to be flushed here (ProcessProfiles will do it)
266 // so don't check the usage.
267 File file(reference_profile_file_fd_, false);
268 result = ProfileAssistant::ProcessProfiles(profile_files_fd_, reference_profile_file_fd_);
269 CloseAllFds(profile_files_fd_, "profile_files_fd_");
270 } else {
271 result = ProfileAssistant::ProcessProfiles(profile_files_, reference_profile_file_);
272 }
273 return result;
274 }
275
David Sehr7c80f2d2017-02-07 16:47:58 -0800276 void OpenApkFilesFromLocations(std::vector<std::unique_ptr<const DexFile>>* dex_files) {
277 bool use_apk_fd_list = !apks_fd_.empty();
278 if (use_apk_fd_list) {
David Sehr153da0e2017-02-15 08:54:51 -0800279 // Get the APKs from the collection of FDs.
David Sehr7c80f2d2017-02-07 16:47:58 -0800280 CHECK_EQ(dex_locations_.size(), apks_fd_.size());
David Sehr153da0e2017-02-15 08:54:51 -0800281 } else if (!apk_files_.empty()) {
282 // Get the APKs from the collection of filenames.
David Sehr7c80f2d2017-02-07 16:47:58 -0800283 CHECK_EQ(dex_locations_.size(), apk_files_.size());
David Sehr153da0e2017-02-15 08:54:51 -0800284 } else {
285 // No APKs were specified.
286 CHECK(dex_locations_.empty());
287 return;
David Sehr7c80f2d2017-02-07 16:47:58 -0800288 }
289 static constexpr bool kVerifyChecksum = true;
290 for (size_t i = 0; i < dex_locations_.size(); ++i) {
291 std::string error_msg;
292 std::vector<std::unique_ptr<const DexFile>> dex_files_for_location;
293 if (use_apk_fd_list) {
294 if (DexFile::OpenZip(apks_fd_[i],
295 dex_locations_[i],
296 kVerifyChecksum,
297 &error_msg,
298 &dex_files_for_location)) {
299 } else {
300 LOG(WARNING) << "OpenZip failed for '" << dex_locations_[i] << "' " << error_msg;
301 continue;
302 }
303 } else {
304 if (DexFile::Open(apk_files_[i].c_str(),
305 dex_locations_[i],
306 kVerifyChecksum,
307 &error_msg,
308 &dex_files_for_location)) {
309 } else {
310 LOG(WARNING) << "Open failed for '" << dex_locations_[i] << "' " << error_msg;
311 continue;
312 }
313 }
314 for (std::unique_ptr<const DexFile>& dex_file : dex_files_for_location) {
315 dex_files->emplace_back(std::move(dex_file));
316 }
317 }
318 }
319
David Sehrb18991b2017-02-08 20:58:10 -0800320 int DumpOneProfile(const std::string& banner,
321 const std::string& filename,
322 int fd,
323 const std::vector<std::unique_ptr<const DexFile>>* dex_files,
324 std::string* dump) {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700325 if (!filename.empty()) {
326 fd = open(filename.c_str(), O_RDWR);
327 if (fd < 0) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800328 LOG(ERROR) << "Cannot open " << filename << strerror(errno);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700329 return -1;
330 }
Calin Juravle876f3502016-03-24 16:16:34 +0000331 }
332 ProfileCompilationInfo info;
333 if (!info.Load(fd)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800334 LOG(ERROR) << "Cannot load profile info from fd=" << fd << "\n";
Calin Juravle876f3502016-03-24 16:16:34 +0000335 return -1;
336 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700337 std::string this_dump = banner + "\n" + info.DumpInfo(dex_files) + "\n";
338 *dump += this_dump;
339 if (close(fd) < 0) {
340 PLOG(WARNING) << "Failed to close descriptor";
341 }
342 return 0;
343 }
344
345 int DumpProfileInfo() {
David Sehr153da0e2017-02-15 08:54:51 -0800346 // Validate that at least one profile file or reference was specified.
347 if (profile_files_.empty() && profile_files_fd_.empty() &&
348 reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
349 Usage("No profile files or reference profile specified.");
350 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700351 static const char* kEmptyString = "";
David Sehr546d24f2016-06-02 10:46:19 -0700352 static const char* kOrdinaryProfile = "=== profile ===";
353 static const char* kReferenceProfile = "=== reference profile ===";
354
355 // Open apk/zip files and and read dex files.
356 MemMap::Init(); // for ZipArchive::OpenFromFd
David Sehrb18991b2017-02-08 20:58:10 -0800357 std::vector<std::unique_ptr<const DexFile>> dex_files;
David Sehr7c80f2d2017-02-07 16:47:58 -0800358 OpenApkFilesFromLocations(&dex_files);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700359 std::string dump;
David Sehr4fcdd6d2016-05-24 14:52:31 -0700360 // Dump individual profile files.
361 if (!profile_files_fd_.empty()) {
362 for (int profile_file_fd : profile_files_fd_) {
David Sehr546d24f2016-06-02 10:46:19 -0700363 int ret = DumpOneProfile(kOrdinaryProfile,
364 kEmptyString,
365 profile_file_fd,
366 &dex_files,
367 &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700368 if (ret != 0) {
369 return ret;
370 }
371 }
372 }
373 if (!profile_files_.empty()) {
374 for (const std::string& profile_file : profile_files_) {
David Sehr546d24f2016-06-02 10:46:19 -0700375 int ret = DumpOneProfile(kOrdinaryProfile, profile_file, kInvalidFd, &dex_files, &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700376 if (ret != 0) {
377 return ret;
378 }
379 }
380 }
381 // Dump reference profile file.
382 if (FdIsValid(reference_profile_file_fd_)) {
David Sehr546d24f2016-06-02 10:46:19 -0700383 int ret = DumpOneProfile(kReferenceProfile,
384 kEmptyString,
385 reference_profile_file_fd_,
386 &dex_files,
387 &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700388 if (ret != 0) {
389 return ret;
390 }
391 }
392 if (!reference_profile_file_.empty()) {
David Sehr546d24f2016-06-02 10:46:19 -0700393 int ret = DumpOneProfile(kReferenceProfile,
394 reference_profile_file_,
395 kInvalidFd,
396 &dex_files,
David Sehr4fcdd6d2016-05-24 14:52:31 -0700397 &dump);
398 if (ret != 0) {
399 return ret;
400 }
401 }
402 if (!FdIsValid(dump_output_to_fd_)) {
403 std::cout << dump;
404 } else {
405 unix_file::FdFile out_fd(dump_output_to_fd_, false /*check_usage*/);
406 if (!out_fd.WriteFully(dump.c_str(), dump.length())) {
407 return -1;
408 }
409 }
Calin Juravle876f3502016-03-24 16:16:34 +0000410 return 0;
411 }
412
413 bool ShouldOnlyDumpProfile() {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700414 return dump_only_;
Calin Juravle876f3502016-03-24 16:16:34 +0000415 }
416
Mathieu Chartier34067262017-04-06 13:55:46 -0700417 bool GetClassNamesAndMethods(int fd,
418 std::vector<std::unique_ptr<const DexFile>>* dex_files,
419 std::set<std::string>* out_lines) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800420 ProfileCompilationInfo profile_info;
421 if (!profile_info.Load(fd)) {
422 LOG(ERROR) << "Cannot load profile info";
423 return false;
424 }
Mathieu Chartier34067262017-04-06 13:55:46 -0700425 for (const std::unique_ptr<const DexFile>& dex_file : *dex_files) {
426 std::set<dex::TypeIndex> class_types;
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700427 std::set<uint16_t> methods;
428 if (profile_info.GetClassesAndMethods(*dex_file.get(), &class_types, &methods)) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700429 for (const dex::TypeIndex& type_index : class_types) {
430 const DexFile::TypeId& type_id = dex_file->GetTypeId(type_index);
431 out_lines->insert(std::string(dex_file->GetTypeDescriptor(type_id)));
432 }
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700433 for (uint16_t dex_method_idx : methods) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700434 const DexFile::MethodId& id = dex_file->GetMethodId(dex_method_idx);
435 std::string signature_string(dex_file->GetMethodSignature(id).ToString());
436 std::string type_string(dex_file->GetTypeDescriptor(dex_file->GetTypeId(id.class_idx_)));
437 std::string method_name(dex_file->GetMethodName(id));
438 out_lines->insert(type_string + kMethodSep + method_name + signature_string);
439 }
440 }
441 }
David Sehr7c80f2d2017-02-07 16:47:58 -0800442 return true;
443 }
444
Mathieu Chartier34067262017-04-06 13:55:46 -0700445 bool GetClassNamesAndMethods(const std::string& profile_file,
446 std::vector<std::unique_ptr<const DexFile>>* dex_files,
447 std::set<std::string>* out_lines) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800448 int fd = open(profile_file.c_str(), O_RDONLY);
449 if (!FdIsValid(fd)) {
450 LOG(ERROR) << "Cannot open " << profile_file << strerror(errno);
451 return false;
452 }
Mathieu Chartier34067262017-04-06 13:55:46 -0700453 if (!GetClassNamesAndMethods(fd, dex_files, out_lines)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800454 return false;
455 }
456 if (close(fd) < 0) {
457 PLOG(WARNING) << "Failed to close descriptor";
458 }
459 return true;
460 }
461
462 int DumpClasses() {
David Sehr153da0e2017-02-15 08:54:51 -0800463 // Validate that at least one profile file or reference was specified.
464 if (profile_files_.empty() && profile_files_fd_.empty() &&
465 reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
466 Usage("No profile files or reference profile specified.");
467 }
David Sehr7c80f2d2017-02-07 16:47:58 -0800468 // Open apk/zip files and and read dex files.
469 MemMap::Init(); // for ZipArchive::OpenFromFd
470 // Open the dex files to get the names for classes.
471 std::vector<std::unique_ptr<const DexFile>> dex_files;
472 OpenApkFilesFromLocations(&dex_files);
473 // Build a vector of class names from individual profile files.
474 std::set<std::string> class_names;
475 if (!profile_files_fd_.empty()) {
476 for (int profile_file_fd : profile_files_fd_) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700477 if (!GetClassNamesAndMethods(profile_file_fd, &dex_files, &class_names)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800478 return -1;
479 }
480 }
481 }
482 if (!profile_files_.empty()) {
483 for (const std::string& profile_file : profile_files_) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700484 if (!GetClassNamesAndMethods(profile_file, &dex_files, &class_names)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800485 return -1;
486 }
487 }
488 }
489 // Concatenate class names from reference profile file.
490 if (FdIsValid(reference_profile_file_fd_)) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700491 if (!GetClassNamesAndMethods(reference_profile_file_fd_, &dex_files, &class_names)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800492 return -1;
493 }
494 }
495 if (!reference_profile_file_.empty()) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700496 if (!GetClassNamesAndMethods(reference_profile_file_, &dex_files, &class_names)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800497 return -1;
498 }
499 }
500 // Dump the class names.
501 std::string dump;
502 for (const std::string& class_name : class_names) {
503 dump += class_name + std::string("\n");
504 }
505 if (!FdIsValid(dump_output_to_fd_)) {
506 std::cout << dump;
507 } else {
508 unix_file::FdFile out_fd(dump_output_to_fd_, false /*check_usage*/);
509 if (!out_fd.WriteFully(dump.c_str(), dump.length())) {
510 return -1;
511 }
512 }
513 return 0;
514 }
515
Mathieu Chartier34067262017-04-06 13:55:46 -0700516 bool ShouldOnlyDumpClassesAndMethods() {
517 return dump_classes_and_methods_;
David Sehr7c80f2d2017-02-07 16:47:58 -0800518 }
519
520 // Read lines from the given file, dropping comments and empty lines. Post-process each line with
521 // the given function.
522 template <typename T>
523 static T* ReadCommentedInputFromFile(
524 const char* input_filename, std::function<std::string(const char*)>* process) {
525 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
526 if (input_file.get() == nullptr) {
527 LOG(ERROR) << "Failed to open input file " << input_filename;
528 return nullptr;
529 }
530 std::unique_ptr<T> result(
531 ReadCommentedInputStream<T>(*input_file, process));
532 input_file->close();
533 return result.release();
534 }
535
536 // Read lines from the given stream, dropping comments and empty lines. Post-process each line
537 // with the given function.
538 template <typename T>
539 static T* ReadCommentedInputStream(
540 std::istream& in_stream,
541 std::function<std::string(const char*)>* process) {
542 std::unique_ptr<T> output(new T());
543 while (in_stream.good()) {
544 std::string dot;
545 std::getline(in_stream, dot);
546 if (android::base::StartsWith(dot, "#") || dot.empty()) {
547 continue;
548 }
549 if (process != nullptr) {
550 std::string descriptor((*process)(dot.c_str()));
551 output->insert(output->end(), descriptor);
552 } else {
553 output->insert(output->end(), dot);
554 }
555 }
556 return output.release();
557 }
558
Calin Juravlee0ac1152017-02-13 19:03:47 -0800559 // Find class klass_descriptor in the given dex_files and store its reference
560 // in the out parameter class_ref.
561 // Return true if the definition of the class was found in any of the dex_files.
562 bool FindClass(const std::vector<std::unique_ptr<const DexFile>>& dex_files,
563 const std::string& klass_descriptor,
Mathieu Chartierdbddc222017-05-24 12:04:13 -0700564 /*out*/TypeReference* class_ref) {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800565 for (const std::unique_ptr<const DexFile>& dex_file_ptr : dex_files) {
566 const DexFile* dex_file = dex_file_ptr.get();
567 const DexFile::TypeId* type_id = dex_file->FindTypeId(klass_descriptor.c_str());
568 if (type_id == nullptr) {
569 continue;
570 }
571 dex::TypeIndex type_index = dex_file->GetIndexForTypeId(*type_id);
572 if (dex_file->FindClassDef(type_index) == nullptr) {
573 // Class is only referenced in the current dex file but not defined in it.
574 continue;
575 }
576 class_ref->dex_file = dex_file;
577 class_ref->type_index = type_index;
578 return true;
579 }
580 return false;
581 }
582
Mathieu Chartier34067262017-04-06 13:55:46 -0700583 // Find the method specified by method_spec in the class class_ref.
Mathieu Chartierdbddc222017-05-24 12:04:13 -0700584 uint32_t FindMethodIndex(const TypeReference& class_ref, const std::string& method_spec) {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800585 std::vector<std::string> name_and_signature;
586 Split(method_spec, kProfileParsingFirstCharInSignature, &name_and_signature);
587 if (name_and_signature.size() != 2) {
588 LOG(ERROR) << "Invalid method name and signature " << method_spec;
589 }
590 const std::string& name = name_and_signature[0];
591 const std::string& signature = kProfileParsingFirstCharInSignature + name_and_signature[1];
592 const DexFile* dex_file = class_ref.dex_file;
593
594 const DexFile::StringId* name_id = dex_file->FindStringId(name.c_str());
595 if (name_id == nullptr) {
Mathieu Chartier66aae3b2017-05-18 10:38:28 -0700596 LOG(WARNING) << "Could not find name: " << name;
Mathieu Chartier34067262017-04-06 13:55:46 -0700597 return DexFile::kDexNoIndex;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800598 }
599 dex::TypeIndex return_type_idx;
600 std::vector<dex::TypeIndex> param_type_idxs;
601 if (!dex_file->CreateTypeList(signature, &return_type_idx, &param_type_idxs)) {
Mathieu Chartier66aae3b2017-05-18 10:38:28 -0700602 LOG(WARNING) << "Could not create type list" << signature;
Mathieu Chartier34067262017-04-06 13:55:46 -0700603 return DexFile::kDexNoIndex;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800604 }
605 const DexFile::ProtoId* proto_id = dex_file->FindProtoId(return_type_idx, param_type_idxs);
606 if (proto_id == nullptr) {
Mathieu Chartier66aae3b2017-05-18 10:38:28 -0700607 LOG(WARNING) << "Could not find proto_id: " << name;
Mathieu Chartier34067262017-04-06 13:55:46 -0700608 return DexFile::kDexNoIndex;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800609 }
610 const DexFile::MethodId* method_id = dex_file->FindMethodId(
611 dex_file->GetTypeId(class_ref.type_index), *name_id, *proto_id);
612 if (method_id == nullptr) {
Mathieu Chartier66aae3b2017-05-18 10:38:28 -0700613 LOG(WARNING) << "Could not find method_id: " << name;
Mathieu Chartier34067262017-04-06 13:55:46 -0700614 return DexFile::kDexNoIndex;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800615 }
616
Mathieu Chartier34067262017-04-06 13:55:46 -0700617 return dex_file->GetIndexForMethodId(*method_id);
618 }
Calin Juravlee0ac1152017-02-13 19:03:47 -0800619
Mathieu Chartier34067262017-04-06 13:55:46 -0700620 // Given a method, return true if the method has a single INVOKE_VIRTUAL in its byte code.
621 // Upon success it returns true and stores the method index and the invoke dex pc
622 // in the output parameters.
623 // The format of the method spec is "inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;".
624 //
625 // TODO(calin): support INVOKE_INTERFACE and the range variants.
Mathieu Chartierdbddc222017-05-24 12:04:13 -0700626 bool HasSingleInvoke(const TypeReference& class_ref,
Mathieu Chartier34067262017-04-06 13:55:46 -0700627 uint16_t method_index,
628 /*out*/uint32_t* dex_pc) {
629 const DexFile* dex_file = class_ref.dex_file;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800630 uint32_t offset = dex_file->FindCodeItemOffset(
631 *dex_file->FindClassDef(class_ref.type_index),
Mathieu Chartier34067262017-04-06 13:55:46 -0700632 method_index);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800633 const DexFile::CodeItem* code_item = dex_file->GetCodeItem(offset);
634
635 bool found_invoke = false;
636 for (CodeItemIterator it(*code_item); !it.Done(); it.Advance()) {
637 if (it.CurrentInstruction().Opcode() == Instruction::INVOKE_VIRTUAL) {
638 if (found_invoke) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700639 LOG(ERROR) << "Multiple invoke INVOKE_VIRTUAL found: "
640 << dex_file->PrettyMethod(method_index);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800641 return false;
642 }
643 found_invoke = true;
644 *dex_pc = it.CurrentDexPc();
645 }
646 }
647 if (!found_invoke) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700648 LOG(ERROR) << "Could not find any INVOKE_VIRTUAL: " << dex_file->PrettyMethod(method_index);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800649 }
650 return found_invoke;
651 }
652
653 // Process a line defining a class or a method and its inline caches.
654 // Upon success return true and add the class or the method info to profile.
Calin Juravle589e71e2017-03-03 16:05:05 -0800655 // The possible line formats are:
656 // "LJustTheCass;".
Calin Juravlee0ac1152017-02-13 19:03:47 -0800657 // "LTestInline;->inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;".
Calin Juravle589e71e2017-03-03 16:05:05 -0800658 // "LTestInline;->inlineMissingTypes(LSuper;)I+missing_types".
659 // "LTestInline;->inlineNoInlineCaches(LSuper;)I".
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700660 // "LTestInline;->*".
Calin Juravlee0ac1152017-02-13 19:03:47 -0800661 // The method and classes are searched only in the given dex files.
662 bool ProcessLine(const std::vector<std::unique_ptr<const DexFile>>& dex_files,
663 const std::string& line,
664 /*out*/ProfileCompilationInfo* profile) {
665 std::string klass;
666 std::string method_str;
667 size_t method_sep_index = line.find(kMethodSep);
668 if (method_sep_index == std::string::npos) {
669 klass = line;
670 } else {
671 klass = line.substr(0, method_sep_index);
672 method_str = line.substr(method_sep_index + kMethodSep.size());
673 }
674
Mathieu Chartierdbddc222017-05-24 12:04:13 -0700675 TypeReference class_ref;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800676 if (!FindClass(dex_files, klass, &class_ref)) {
Mathieu Chartier3f121342017-03-06 11:16:20 -0800677 LOG(WARNING) << "Could not find class: " << klass;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800678 return false;
679 }
680
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700681 if (method_str.empty() || method_str == kClassAllMethods) {
682 // Start by adding the class.
Calin Juravlee0ac1152017-02-13 19:03:47 -0800683 std::set<DexCacheResolvedClasses> resolved_class_set;
684 const DexFile* dex_file = class_ref.dex_file;
685 const auto& dex_resolved_classes = resolved_class_set.emplace(
686 dex_file->GetLocation(),
687 dex_file->GetBaseLocation(),
688 dex_file->GetLocationChecksum());
689 dex_resolved_classes.first->AddClass(class_ref.type_index);
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700690 std::vector<ProfileMethodInfo> methods;
691 if (method_str == kClassAllMethods) {
692 // Add all of the methods.
693 const DexFile::ClassDef* class_def = dex_file->FindClassDef(class_ref.type_index);
694 const uint8_t* class_data = dex_file->GetClassData(*class_def);
695 if (class_data != nullptr) {
696 ClassDataItemIterator it(*dex_file, class_data);
697 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
698 it.Next();
699 }
700 while (it.HasNextDirectMethod() || it.HasNextVirtualMethod()) {
701 if (it.GetMethodCodeItemOffset() != 0) {
702 // Add all of the methods that have code to the profile.
703 const uint32_t method_idx = it.GetMemberIndex();
704 methods.push_back(ProfileMethodInfo(dex_file, method_idx));
705 }
706 it.Next();
707 }
708 }
709 }
710 profile->AddMethodsAndClasses(methods, resolved_class_set);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800711 return true;
712 }
713
714 // Process the method.
715 std::string method_spec;
716 std::vector<std::string> inline_cache_elems;
717
718 std::vector<std::string> method_elems;
Calin Juravle589e71e2017-03-03 16:05:05 -0800719 bool is_missing_types = false;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800720 Split(method_str, kProfileParsingInlineChacheSep, &method_elems);
721 if (method_elems.size() == 2) {
722 method_spec = method_elems[0];
Calin Juravle589e71e2017-03-03 16:05:05 -0800723 is_missing_types = method_elems[1] == kMissingTypesMarker;
724 if (!is_missing_types) {
725 Split(method_elems[1], kProfileParsingTypeSep, &inline_cache_elems);
726 }
Calin Juravlee0ac1152017-02-13 19:03:47 -0800727 } else if (method_elems.size() == 1) {
728 method_spec = method_elems[0];
729 } else {
730 LOG(ERROR) << "Invalid method line: " << line;
731 return false;
732 }
733
Mathieu Chartier34067262017-04-06 13:55:46 -0700734 const uint32_t method_index = FindMethodIndex(class_ref, method_spec);
735 if (method_index == DexFile::kDexNoIndex) {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800736 return false;
737 }
Mathieu Chartier34067262017-04-06 13:55:46 -0700738
739 std::vector<ProfileMethodInfo> pmi;
740 std::vector<ProfileMethodInfo::ProfileInlineCache> inline_caches;
741 if (is_missing_types || !inline_cache_elems.empty()) {
742 uint32_t dex_pc;
743 if (!HasSingleInvoke(class_ref, method_index, &dex_pc)) {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800744 return false;
745 }
Mathieu Chartierdbddc222017-05-24 12:04:13 -0700746 std::vector<TypeReference> classes(inline_cache_elems.size());
Mathieu Chartier34067262017-04-06 13:55:46 -0700747 size_t class_it = 0;
748 for (const std::string& ic_class : inline_cache_elems) {
749 if (!FindClass(dex_files, ic_class, &(classes[class_it++]))) {
750 LOG(ERROR) << "Could not find class: " << ic_class;
751 return false;
752 }
753 }
754 inline_caches.emplace_back(dex_pc, is_missing_types, classes);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800755 }
Calin Juravlee0ac1152017-02-13 19:03:47 -0800756 pmi.emplace_back(class_ref.dex_file, method_index, inline_caches);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800757 profile->AddMethodsAndClasses(pmi, std::set<DexCacheResolvedClasses>());
758 return true;
759 }
760
761 // Creates a profile from a human friendly textual representation.
762 // The expected input format is:
763 // # Classes
764 // Ljava/lang/Comparable;
765 // Ljava/lang/Math;
766 // # Methods with inline caches
767 // LTestInline;->inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;
768 // LTestInline;->noInlineCache(LSuper;)I
David Sehr7c80f2d2017-02-07 16:47:58 -0800769 int CreateProfile() {
David Sehr153da0e2017-02-15 08:54:51 -0800770 // Validate parameters for this command.
771 if (apk_files_.empty() && apks_fd_.empty()) {
772 Usage("APK files must be specified");
773 }
774 if (dex_locations_.empty()) {
775 Usage("DEX locations must be specified");
776 }
777 if (reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
778 Usage("Reference profile must be specified with --reference-profile-file or "
779 "--reference-profile-file-fd");
780 }
781 if (!profile_files_.empty() || !profile_files_fd_.empty()) {
782 Usage("Profile must be specified with --reference-profile-file or "
783 "--reference-profile-file-fd");
784 }
785 // for ZipArchive::OpenFromFd
786 MemMap::Init();
David Sehr7c80f2d2017-02-07 16:47:58 -0800787 // Open the profile output file if needed.
788 int fd = reference_profile_file_fd_;
789 if (!FdIsValid(fd)) {
790 CHECK(!reference_profile_file_.empty());
791 fd = open(reference_profile_file_.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
792 if (fd < 0) {
793 LOG(ERROR) << "Cannot open " << reference_profile_file_ << strerror(errno);
794 return -1;
795 }
796 }
Calin Juravlee0ac1152017-02-13 19:03:47 -0800797 // Read the user-specified list of classes and methods.
David Sehr7c80f2d2017-02-07 16:47:58 -0800798 std::unique_ptr<std::unordered_set<std::string>>
Calin Juravlee0ac1152017-02-13 19:03:47 -0800799 user_lines(ReadCommentedInputFromFile<std::unordered_set<std::string>>(
David Sehr7c80f2d2017-02-07 16:47:58 -0800800 create_profile_from_file_.c_str(), nullptr)); // No post-processing.
Calin Juravlee0ac1152017-02-13 19:03:47 -0800801
802 // Open the dex files to look up classes and methods.
David Sehr7c80f2d2017-02-07 16:47:58 -0800803 std::vector<std::unique_ptr<const DexFile>> dex_files;
804 OpenApkFilesFromLocations(&dex_files);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800805
806 // Process the lines one by one and add the successful ones to the profile.
David Sehr7c80f2d2017-02-07 16:47:58 -0800807 ProfileCompilationInfo info;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800808
809 for (const auto& line : *user_lines) {
810 ProcessLine(dex_files, line, &info);
811 }
812
David Sehr7c80f2d2017-02-07 16:47:58 -0800813 // Write the profile file.
814 CHECK(info.Save(fd));
815 if (close(fd) < 0) {
816 PLOG(WARNING) << "Failed to close descriptor";
817 }
818 return 0;
819 }
820
821 bool ShouldCreateProfile() {
822 return !create_profile_from_file_.empty();
823 }
824
Calin Juravle7bcdb532016-06-07 16:14:47 +0100825 int GenerateTestProfile() {
David Sehr153da0e2017-02-15 08:54:51 -0800826 // Validate parameters for this command.
827 if (test_profile_method_ratio_ > 100) {
828 Usage("Invalid ratio for --generate-test-profile-method-ratio");
829 }
830 if (test_profile_class_ratio_ > 100) {
831 Usage("Invalid ratio for --generate-test-profile-class-ratio");
832 }
Jeff Haof0a31f82017-03-27 15:50:37 -0700833 // If given APK files or DEX locations, check that they're ok.
834 if (!apk_files_.empty() || !apks_fd_.empty() || !dex_locations_.empty()) {
835 if (apk_files_.empty() && apks_fd_.empty()) {
836 Usage("APK files must be specified when passing DEX locations to --generate-test-profile");
837 }
838 if (dex_locations_.empty()) {
839 Usage("DEX locations must be specified when passing APK files to --generate-test-profile");
840 }
841 }
David Sehr153da0e2017-02-15 08:54:51 -0800842 // ShouldGenerateTestProfile confirms !test_profile_.empty().
George Burgess IV71f77262016-10-25 13:08:17 -0700843 int profile_test_fd = open(test_profile_.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
Calin Juravle7bcdb532016-06-07 16:14:47 +0100844 if (profile_test_fd < 0) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800845 LOG(ERROR) << "Cannot open " << test_profile_ << strerror(errno);
Calin Juravle7bcdb532016-06-07 16:14:47 +0100846 return -1;
847 }
Jeff Haof0a31f82017-03-27 15:50:37 -0700848 bool result;
849 if (apk_files_.empty() && apks_fd_.empty() && dex_locations_.empty()) {
850 result = ProfileCompilationInfo::GenerateTestProfile(profile_test_fd,
851 test_profile_num_dex_,
852 test_profile_method_ratio_,
853 test_profile_class_ratio_,
854 test_profile_seed_);
855 } else {
856 // Initialize MemMap for ZipArchive::OpenFromFd.
857 MemMap::Init();
858 // Open the dex files to look up classes and methods.
859 std::vector<std::unique_ptr<const DexFile>> dex_files;
860 OpenApkFilesFromLocations(&dex_files);
861 // Create a random profile file based on the set of dex files.
862 result = ProfileCompilationInfo::GenerateTestProfile(profile_test_fd,
863 dex_files,
864 test_profile_seed_);
865 }
Calin Juravle7bcdb532016-06-07 16:14:47 +0100866 close(profile_test_fd); // ignore close result.
867 return result ? 0 : -1;
868 }
869
870 bool ShouldGenerateTestProfile() {
871 return !test_profile_.empty();
872 }
873
Calin Juravle2e2db782016-02-23 12:00:03 +0000874 private:
875 static void ParseFdForCollection(const StringPiece& option,
876 const char* arg_name,
877 std::vector<int>* fds) {
878 int fd;
879 ParseUintOption(option, arg_name, &fd, Usage);
880 fds->push_back(fd);
881 }
882
883 static void CloseAllFds(const std::vector<int>& fds, const char* descriptor) {
884 for (size_t i = 0; i < fds.size(); i++) {
885 if (close(fds[i]) < 0) {
886 PLOG(WARNING) << "Failed to close descriptor for " << descriptor << " at index " << i;
887 }
888 }
889 }
890
891 void LogCompletionTime() {
David Sehr52683cf2016-05-05 09:02:38 -0700892 static constexpr uint64_t kLogThresholdTime = MsToNs(100); // 100ms
893 uint64_t time_taken = NanoTime() - start_ns_;
David Sehred793012016-05-05 13:39:43 -0700894 if (time_taken > kLogThresholdTime) {
David Sehrd8557182016-05-06 12:29:35 -0700895 LOG(WARNING) << "profman took " << PrettyDuration(time_taken);
David Sehred793012016-05-05 13:39:43 -0700896 }
Calin Juravle2e2db782016-02-23 12:00:03 +0000897 }
898
899 std::vector<std::string> profile_files_;
900 std::vector<int> profile_files_fd_;
David Sehr546d24f2016-06-02 10:46:19 -0700901 std::vector<std::string> dex_locations_;
David Sehr7c80f2d2017-02-07 16:47:58 -0800902 std::vector<std::string> apk_files_;
David Sehr546d24f2016-06-02 10:46:19 -0700903 std::vector<int> apks_fd_;
Calin Juravle2e2db782016-02-23 12:00:03 +0000904 std::string reference_profile_file_;
905 int reference_profile_file_fd_;
David Sehr4fcdd6d2016-05-24 14:52:31 -0700906 bool dump_only_;
Mathieu Chartier34067262017-04-06 13:55:46 -0700907 bool dump_classes_and_methods_;
David Sehr4fcdd6d2016-05-24 14:52:31 -0700908 int dump_output_to_fd_;
Calin Juravle7bcdb532016-06-07 16:14:47 +0100909 std::string test_profile_;
David Sehr7c80f2d2017-02-07 16:47:58 -0800910 std::string create_profile_from_file_;
Calin Juravle7bcdb532016-06-07 16:14:47 +0100911 uint16_t test_profile_num_dex_;
912 uint16_t test_profile_method_ratio_;
913 uint16_t test_profile_class_ratio_;
Jeff Haof0a31f82017-03-27 15:50:37 -0700914 uint32_t test_profile_seed_;
Calin Juravle2e2db782016-02-23 12:00:03 +0000915 uint64_t start_ns_;
916};
917
918// See ProfileAssistant::ProcessingResult for return codes.
919static int profman(int argc, char** argv) {
920 ProfMan profman;
921
922 // Parse arguments. Argument mistakes will lead to exit(EXIT_FAILURE) in UsageError.
923 profman.ParseArgs(argc, argv);
924
Calin Juravle7bcdb532016-06-07 16:14:47 +0100925 if (profman.ShouldGenerateTestProfile()) {
926 return profman.GenerateTestProfile();
927 }
Calin Juravle876f3502016-03-24 16:16:34 +0000928 if (profman.ShouldOnlyDumpProfile()) {
929 return profman.DumpProfileInfo();
930 }
Mathieu Chartier34067262017-04-06 13:55:46 -0700931 if (profman.ShouldOnlyDumpClassesAndMethods()) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800932 return profman.DumpClasses();
933 }
934 if (profman.ShouldCreateProfile()) {
935 return profman.CreateProfile();
936 }
Calin Juravle2e2db782016-02-23 12:00:03 +0000937 // Process profile information and assess if we need to do a profile guided compilation.
938 // This operation involves I/O.
939 return profman.ProcessProfiles();
940}
941
942} // namespace art
943
944int main(int argc, char **argv) {
945 return art::profman(argc, argv);
946}
947