blob: 384e1291503b15b65fd267909303bed8ee302e29 [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"
David Sehrf57589f2016-10-17 10:09:33 -070042#include "runtime.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000043#include "utils.h"
David Sehr546d24f2016-06-02 10:46:19 -070044#include "zip_archive.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000045#include "profile_assistant.h"
46
47namespace art {
48
49static int original_argc;
50static char** original_argv;
51
52static std::string CommandLine() {
53 std::vector<std::string> command;
54 for (int i = 0; i < original_argc; ++i) {
55 command.push_back(original_argv[i]);
56 }
Andreas Gampe9186ced2016-12-12 14:28:21 -080057 return android::base::Join(command, ' ');
Calin Juravle2e2db782016-02-23 12:00:03 +000058}
59
David Sehr4fcdd6d2016-05-24 14:52:31 -070060static constexpr int kInvalidFd = -1;
61
62static bool FdIsValid(int fd) {
63 return fd != kInvalidFd;
64}
65
Calin Juravle2e2db782016-02-23 12:00:03 +000066static void UsageErrorV(const char* fmt, va_list ap) {
67 std::string error;
Andreas Gampe46ee31b2016-12-14 10:11:49 -080068 android::base::StringAppendV(&error, fmt, ap);
Calin Juravle2e2db782016-02-23 12:00:03 +000069 LOG(ERROR) << error;
70}
71
72static void UsageError(const char* fmt, ...) {
73 va_list ap;
74 va_start(ap, fmt);
75 UsageErrorV(fmt, ap);
76 va_end(ap);
77}
78
79NO_RETURN static void Usage(const char *fmt, ...) {
80 va_list ap;
81 va_start(ap, fmt);
82 UsageErrorV(fmt, ap);
83 va_end(ap);
84
85 UsageError("Command: %s", CommandLine().c_str());
86 UsageError("Usage: profman [options]...");
87 UsageError("");
David Sehr4fcdd6d2016-05-24 14:52:31 -070088 UsageError(" --dump-only: dumps the content of the specified profile files");
89 UsageError(" to standard output (default) in a human readable form.");
90 UsageError("");
David Sehr7c80f2d2017-02-07 16:47:58 -080091 UsageError(" --dump-output-to-fd=<number>: redirects --dump-only output to a file descriptor.");
92 UsageError("");
Mathieu Chartier34067262017-04-06 13:55:46 -070093 UsageError(" --dump-classes-and-methods: dumps a sorted list of classes and methods that are");
94 UsageError(" in the specified profile file to standard output (default) in a human");
95 UsageError(" readable form. The output is valid input for --create-profile-from");
Calin Juravle876f3502016-03-24 16:16:34 +000096 UsageError("");
Calin Juravle2e2db782016-02-23 12:00:03 +000097 UsageError(" --profile-file=<filename>: specify profiler output file to use for compilation.");
98 UsageError(" Can be specified multiple time, in which case the data from the different");
99 UsageError(" profiles will be aggregated.");
100 UsageError("");
101 UsageError(" --profile-file-fd=<number>: same as --profile-file but accepts a file descriptor.");
102 UsageError(" Cannot be used together with --profile-file.");
103 UsageError("");
104 UsageError(" --reference-profile-file=<filename>: specify a reference profile.");
105 UsageError(" The data in this file will be compared with the data obtained by merging");
106 UsageError(" all the files specified with --profile-file or --profile-file-fd.");
107 UsageError(" If the exit code is EXIT_COMPILE then all --profile-file will be merged into");
108 UsageError(" --reference-profile-file. ");
109 UsageError("");
110 UsageError(" --reference-profile-file-fd=<number>: same as --reference-profile-file but");
111 UsageError(" accepts a file descriptor. Cannot be used together with");
112 UsageError(" --reference-profile-file.");
David Sehr7c80f2d2017-02-07 16:47:58 -0800113 UsageError("");
Calin Juravle7bcdb532016-06-07 16:14:47 +0100114 UsageError(" --generate-test-profile=<filename>: generates a random profile file for testing.");
115 UsageError(" --generate-test-profile-num-dex=<number>: number of dex files that should be");
116 UsageError(" included in the generated profile. Defaults to 20.");
117 UsageError(" --generate-test-profile-method-ratio=<number>: the percentage from the maximum");
118 UsageError(" number of methods that should be generated. Defaults to 5.");
119 UsageError(" --generate-test-profile-class-ratio=<number>: the percentage from the maximum");
120 UsageError(" number of classes that should be generated. Defaults to 5.");
Jeff Haof0a31f82017-03-27 15:50:37 -0700121 UsageError(" --generate-test-profile-seed=<number>: seed for random number generator used when");
122 UsageError(" generating random test profiles. Defaults to using NanoTime.");
Calin Juravle7bcdb532016-06-07 16:14:47 +0100123 UsageError("");
Mathieu Chartier34067262017-04-06 13:55:46 -0700124 UsageError(" --create-profile-from=<filename>: creates a profile from a list of classes and");
125 UsageError(" methods.");
David Sehr7c80f2d2017-02-07 16:47:58 -0800126 UsageError("");
David Sehr546d24f2016-06-02 10:46:19 -0700127 UsageError(" --dex-location=<string>: location string to use with corresponding");
128 UsageError(" apk-fd to find dex files");
David Sehr4fcdd6d2016-05-24 14:52:31 -0700129 UsageError("");
David Sehr546d24f2016-06-02 10:46:19 -0700130 UsageError(" --apk-fd=<number>: file descriptor containing an open APK to");
David Sehr4fcdd6d2016-05-24 14:52:31 -0700131 UsageError(" search for dex files");
David Sehr7c80f2d2017-02-07 16:47:58 -0800132 UsageError(" --apk-=<filename>: an APK to search for dex files");
David Sehr4fcdd6d2016-05-24 14:52:31 -0700133 UsageError("");
Calin Juravle2e2db782016-02-23 12:00:03 +0000134
135 exit(EXIT_FAILURE);
136}
137
Calin Juravle7bcdb532016-06-07 16:14:47 +0100138// Note: make sure you update the Usage if you change these values.
139static constexpr uint16_t kDefaultTestProfileNumDex = 20;
140static constexpr uint16_t kDefaultTestProfileMethodRatio = 5;
141static constexpr uint16_t kDefaultTestProfileClassRatio = 5;
142
Calin Juravlee0ac1152017-02-13 19:03:47 -0800143// Separators used when parsing human friendly representation of profiles.
144static const std::string kMethodSep = "->";
Calin Juravle589e71e2017-03-03 16:05:05 -0800145static const std::string kMissingTypesMarker = "missing_types";
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700146static const std::string kClassAllMethods = "*";
Calin Juravlee0ac1152017-02-13 19:03:47 -0800147static constexpr char kProfileParsingInlineChacheSep = '+';
148static constexpr char kProfileParsingTypeSep = ',';
149static constexpr char kProfileParsingFirstCharInSignature = '(';
150
151// TODO(calin): This class has grown too much from its initial design. Split the functionality
152// into smaller, more contained pieces.
Calin Juravle2e2db782016-02-23 12:00:03 +0000153class ProfMan FINAL {
154 public:
155 ProfMan() :
David Sehr4fcdd6d2016-05-24 14:52:31 -0700156 reference_profile_file_fd_(kInvalidFd),
157 dump_only_(false),
Mathieu Chartier34067262017-04-06 13:55:46 -0700158 dump_classes_and_methods_(false),
David Sehr4fcdd6d2016-05-24 14:52:31 -0700159 dump_output_to_fd_(kInvalidFd),
Calin Juravle7bcdb532016-06-07 16:14:47 +0100160 test_profile_num_dex_(kDefaultTestProfileNumDex),
161 test_profile_method_ratio_(kDefaultTestProfileMethodRatio),
162 test_profile_class_ratio_(kDefaultTestProfileClassRatio),
Jeff Haof0a31f82017-03-27 15:50:37 -0700163 test_profile_seed_(NanoTime()),
Calin Juravle2e2db782016-02-23 12:00:03 +0000164 start_ns_(NanoTime()) {}
165
166 ~ProfMan() {
167 LogCompletionTime();
168 }
169
170 void ParseArgs(int argc, char **argv) {
171 original_argc = argc;
172 original_argv = argv;
173
David Sehrf57589f2016-10-17 10:09:33 -0700174 InitLogging(argv, Runtime::Aborter);
Calin Juravle2e2db782016-02-23 12:00:03 +0000175
176 // Skip over the command name.
177 argv++;
178 argc--;
179
180 if (argc == 0) {
181 Usage("No arguments specified");
182 }
183
184 for (int i = 0; i < argc; ++i) {
185 const StringPiece option(argv[i]);
186 const bool log_options = false;
187 if (log_options) {
Calin Juravle876f3502016-03-24 16:16:34 +0000188 LOG(INFO) << "profman: option[" << i << "]=" << argv[i];
Calin Juravle2e2db782016-02-23 12:00:03 +0000189 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700190 if (option == "--dump-only") {
191 dump_only_ = true;
Mathieu Chartier34067262017-04-06 13:55:46 -0700192 } else if (option == "--dump-classes-and-methods") {
193 dump_classes_and_methods_ = true;
David Sehr7c80f2d2017-02-07 16:47:58 -0800194 } else if (option.starts_with("--create-profile-from=")) {
195 create_profile_from_file_ = option.substr(strlen("--create-profile-from=")).ToString();
David Sehr4fcdd6d2016-05-24 14:52:31 -0700196 } else if (option.starts_with("--dump-output-to-fd=")) {
197 ParseUintOption(option, "--dump-output-to-fd", &dump_output_to_fd_, Usage);
Calin Juravle876f3502016-03-24 16:16:34 +0000198 } else if (option.starts_with("--profile-file=")) {
Calin Juravle2e2db782016-02-23 12:00:03 +0000199 profile_files_.push_back(option.substr(strlen("--profile-file=")).ToString());
200 } else if (option.starts_with("--profile-file-fd=")) {
201 ParseFdForCollection(option, "--profile-file-fd", &profile_files_fd_);
202 } else if (option.starts_with("--reference-profile-file=")) {
203 reference_profile_file_ = option.substr(strlen("--reference-profile-file=")).ToString();
204 } else if (option.starts_with("--reference-profile-file-fd=")) {
205 ParseUintOption(option, "--reference-profile-file-fd", &reference_profile_file_fd_, Usage);
David Sehr546d24f2016-06-02 10:46:19 -0700206 } else if (option.starts_with("--dex-location=")) {
207 dex_locations_.push_back(option.substr(strlen("--dex-location=")).ToString());
208 } else if (option.starts_with("--apk-fd=")) {
209 ParseFdForCollection(option, "--apk-fd", &apks_fd_);
David Sehr7c80f2d2017-02-07 16:47:58 -0800210 } else if (option.starts_with("--apk=")) {
211 apk_files_.push_back(option.substr(strlen("--apk=")).ToString());
Calin Juravle7bcdb532016-06-07 16:14:47 +0100212 } else if (option.starts_with("--generate-test-profile=")) {
213 test_profile_ = option.substr(strlen("--generate-test-profile=")).ToString();
214 } else if (option.starts_with("--generate-test-profile-num-dex=")) {
215 ParseUintOption(option,
216 "--generate-test-profile-num-dex",
217 &test_profile_num_dex_,
218 Usage);
219 } else if (option.starts_with("--generate-test-profile-method-ratio")) {
220 ParseUintOption(option,
221 "--generate-test-profile-method-ratio",
222 &test_profile_method_ratio_,
223 Usage);
224 } else if (option.starts_with("--generate-test-profile-class-ratio")) {
225 ParseUintOption(option,
226 "--generate-test-profile-class-ratio",
227 &test_profile_class_ratio_,
228 Usage);
Jeff Haof0a31f82017-03-27 15:50:37 -0700229 } else if (option.starts_with("--generate-test-profile-seed=")) {
230 ParseUintOption(option, "--generate-test-profile-seed", &test_profile_seed_, Usage);
Calin Juravle2e2db782016-02-23 12:00:03 +0000231 } else {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700232 Usage("Unknown argument '%s'", option.data());
Calin Juravle2e2db782016-02-23 12:00:03 +0000233 }
234 }
235
David Sehr153da0e2017-02-15 08:54:51 -0800236 // Validate global consistency between file/fd options.
Calin Juravle2e2db782016-02-23 12:00:03 +0000237 if (!profile_files_.empty() && !profile_files_fd_.empty()) {
238 Usage("Profile files should not be specified with both --profile-file-fd and --profile-file");
239 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700240 if (!reference_profile_file_.empty() && FdIsValid(reference_profile_file_fd_)) {
241 Usage("Reference profile should not be specified with both "
242 "--reference-profile-file-fd and --reference-profile-file");
243 }
David Sehr153da0e2017-02-15 08:54:51 -0800244 if (!apk_files_.empty() && !apks_fd_.empty()) {
245 Usage("APK files should not be specified with both --apk-fd and --apk");
Calin Juravle2e2db782016-02-23 12:00:03 +0000246 }
247 }
248
249 ProfileAssistant::ProcessingResult ProcessProfiles() {
David Sehr153da0e2017-02-15 08:54:51 -0800250 // Validate that at least one profile file was passed, as well as a reference profile.
251 if (profile_files_.empty() && profile_files_fd_.empty()) {
252 Usage("No profile files specified.");
253 }
254 if (reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
255 Usage("No reference profile file specified.");
256 }
257 if ((!profile_files_.empty() && FdIsValid(reference_profile_file_fd_)) ||
258 (!profile_files_fd_.empty() && !FdIsValid(reference_profile_file_fd_))) {
259 Usage("Options --profile-file-fd and --reference-profile-file-fd "
260 "should only be used together");
261 }
Calin Juravle2e2db782016-02-23 12:00:03 +0000262 ProfileAssistant::ProcessingResult result;
263 if (profile_files_.empty()) {
264 // The file doesn't need to be flushed here (ProcessProfiles will do it)
265 // so don't check the usage.
266 File file(reference_profile_file_fd_, false);
267 result = ProfileAssistant::ProcessProfiles(profile_files_fd_, reference_profile_file_fd_);
268 CloseAllFds(profile_files_fd_, "profile_files_fd_");
269 } else {
270 result = ProfileAssistant::ProcessProfiles(profile_files_, reference_profile_file_);
271 }
272 return result;
273 }
274
David Sehr7c80f2d2017-02-07 16:47:58 -0800275 void OpenApkFilesFromLocations(std::vector<std::unique_ptr<const DexFile>>* dex_files) {
276 bool use_apk_fd_list = !apks_fd_.empty();
277 if (use_apk_fd_list) {
David Sehr153da0e2017-02-15 08:54:51 -0800278 // Get the APKs from the collection of FDs.
David Sehr7c80f2d2017-02-07 16:47:58 -0800279 CHECK_EQ(dex_locations_.size(), apks_fd_.size());
David Sehr153da0e2017-02-15 08:54:51 -0800280 } else if (!apk_files_.empty()) {
281 // Get the APKs from the collection of filenames.
David Sehr7c80f2d2017-02-07 16:47:58 -0800282 CHECK_EQ(dex_locations_.size(), apk_files_.size());
David Sehr153da0e2017-02-15 08:54:51 -0800283 } else {
284 // No APKs were specified.
285 CHECK(dex_locations_.empty());
286 return;
David Sehr7c80f2d2017-02-07 16:47:58 -0800287 }
288 static constexpr bool kVerifyChecksum = true;
289 for (size_t i = 0; i < dex_locations_.size(); ++i) {
290 std::string error_msg;
291 std::vector<std::unique_ptr<const DexFile>> dex_files_for_location;
292 if (use_apk_fd_list) {
293 if (DexFile::OpenZip(apks_fd_[i],
294 dex_locations_[i],
295 kVerifyChecksum,
296 &error_msg,
297 &dex_files_for_location)) {
298 } else {
299 LOG(WARNING) << "OpenZip failed for '" << dex_locations_[i] << "' " << error_msg;
300 continue;
301 }
302 } else {
303 if (DexFile::Open(apk_files_[i].c_str(),
304 dex_locations_[i],
305 kVerifyChecksum,
306 &error_msg,
307 &dex_files_for_location)) {
308 } else {
309 LOG(WARNING) << "Open failed for '" << dex_locations_[i] << "' " << error_msg;
310 continue;
311 }
312 }
313 for (std::unique_ptr<const DexFile>& dex_file : dex_files_for_location) {
314 dex_files->emplace_back(std::move(dex_file));
315 }
316 }
317 }
318
David Sehrb18991b2017-02-08 20:58:10 -0800319 int DumpOneProfile(const std::string& banner,
320 const std::string& filename,
321 int fd,
322 const std::vector<std::unique_ptr<const DexFile>>* dex_files,
323 std::string* dump) {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700324 if (!filename.empty()) {
325 fd = open(filename.c_str(), O_RDWR);
326 if (fd < 0) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800327 LOG(ERROR) << "Cannot open " << filename << strerror(errno);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700328 return -1;
329 }
Calin Juravle876f3502016-03-24 16:16:34 +0000330 }
331 ProfileCompilationInfo info;
332 if (!info.Load(fd)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800333 LOG(ERROR) << "Cannot load profile info from fd=" << fd << "\n";
Calin Juravle876f3502016-03-24 16:16:34 +0000334 return -1;
335 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700336 std::string this_dump = banner + "\n" + info.DumpInfo(dex_files) + "\n";
337 *dump += this_dump;
338 if (close(fd) < 0) {
339 PLOG(WARNING) << "Failed to close descriptor";
340 }
341 return 0;
342 }
343
344 int DumpProfileInfo() {
David Sehr153da0e2017-02-15 08:54:51 -0800345 // Validate that at least one profile file or reference was specified.
346 if (profile_files_.empty() && profile_files_fd_.empty() &&
347 reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
348 Usage("No profile files or reference profile specified.");
349 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700350 static const char* kEmptyString = "";
David Sehr546d24f2016-06-02 10:46:19 -0700351 static const char* kOrdinaryProfile = "=== profile ===";
352 static const char* kReferenceProfile = "=== reference profile ===";
353
354 // Open apk/zip files and and read dex files.
355 MemMap::Init(); // for ZipArchive::OpenFromFd
David Sehrb18991b2017-02-08 20:58:10 -0800356 std::vector<std::unique_ptr<const DexFile>> dex_files;
David Sehr7c80f2d2017-02-07 16:47:58 -0800357 OpenApkFilesFromLocations(&dex_files);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700358 std::string dump;
David Sehr4fcdd6d2016-05-24 14:52:31 -0700359 // Dump individual profile files.
360 if (!profile_files_fd_.empty()) {
361 for (int profile_file_fd : profile_files_fd_) {
David Sehr546d24f2016-06-02 10:46:19 -0700362 int ret = DumpOneProfile(kOrdinaryProfile,
363 kEmptyString,
364 profile_file_fd,
365 &dex_files,
366 &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700367 if (ret != 0) {
368 return ret;
369 }
370 }
371 }
372 if (!profile_files_.empty()) {
373 for (const std::string& profile_file : profile_files_) {
David Sehr546d24f2016-06-02 10:46:19 -0700374 int ret = DumpOneProfile(kOrdinaryProfile, profile_file, kInvalidFd, &dex_files, &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700375 if (ret != 0) {
376 return ret;
377 }
378 }
379 }
380 // Dump reference profile file.
381 if (FdIsValid(reference_profile_file_fd_)) {
David Sehr546d24f2016-06-02 10:46:19 -0700382 int ret = DumpOneProfile(kReferenceProfile,
383 kEmptyString,
384 reference_profile_file_fd_,
385 &dex_files,
386 &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700387 if (ret != 0) {
388 return ret;
389 }
390 }
391 if (!reference_profile_file_.empty()) {
David Sehr546d24f2016-06-02 10:46:19 -0700392 int ret = DumpOneProfile(kReferenceProfile,
393 reference_profile_file_,
394 kInvalidFd,
395 &dex_files,
David Sehr4fcdd6d2016-05-24 14:52:31 -0700396 &dump);
397 if (ret != 0) {
398 return ret;
399 }
400 }
401 if (!FdIsValid(dump_output_to_fd_)) {
402 std::cout << dump;
403 } else {
404 unix_file::FdFile out_fd(dump_output_to_fd_, false /*check_usage*/);
405 if (!out_fd.WriteFully(dump.c_str(), dump.length())) {
406 return -1;
407 }
408 }
Calin Juravle876f3502016-03-24 16:16:34 +0000409 return 0;
410 }
411
412 bool ShouldOnlyDumpProfile() {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700413 return dump_only_;
Calin Juravle876f3502016-03-24 16:16:34 +0000414 }
415
Mathieu Chartier34067262017-04-06 13:55:46 -0700416 bool GetClassNamesAndMethods(int fd,
417 std::vector<std::unique_ptr<const DexFile>>* dex_files,
418 std::set<std::string>* out_lines) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800419 ProfileCompilationInfo profile_info;
420 if (!profile_info.Load(fd)) {
421 LOG(ERROR) << "Cannot load profile info";
422 return false;
423 }
Mathieu Chartier34067262017-04-06 13:55:46 -0700424 for (const std::unique_ptr<const DexFile>& dex_file : *dex_files) {
425 std::set<dex::TypeIndex> class_types;
426 ProfileCompilationInfo::MethodMap methods;
427 if (profile_info.GetClassesAndMethods(dex_file.get(), &class_types, &methods)) {
428 for (const dex::TypeIndex& type_index : class_types) {
429 const DexFile::TypeId& type_id = dex_file->GetTypeId(type_index);
430 out_lines->insert(std::string(dex_file->GetTypeDescriptor(type_id)));
431 }
432 for (const auto& pair : methods) {
433 // TODO: Process inline caches.
434 const uint16_t dex_method_idx = pair.first;
435 const DexFile::MethodId& id = dex_file->GetMethodId(dex_method_idx);
436 std::string signature_string(dex_file->GetMethodSignature(id).ToString());
437 std::string type_string(dex_file->GetTypeDescriptor(dex_file->GetTypeId(id.class_idx_)));
438 std::string method_name(dex_file->GetMethodName(id));
439 out_lines->insert(type_string + kMethodSep + method_name + signature_string);
440 }
441 }
442 }
David Sehr7c80f2d2017-02-07 16:47:58 -0800443 return true;
444 }
445
Mathieu Chartier34067262017-04-06 13:55:46 -0700446 bool GetClassNamesAndMethods(const std::string& profile_file,
447 std::vector<std::unique_ptr<const DexFile>>* dex_files,
448 std::set<std::string>* out_lines) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800449 int fd = open(profile_file.c_str(), O_RDONLY);
450 if (!FdIsValid(fd)) {
451 LOG(ERROR) << "Cannot open " << profile_file << strerror(errno);
452 return false;
453 }
Mathieu Chartier34067262017-04-06 13:55:46 -0700454 if (!GetClassNamesAndMethods(fd, dex_files, out_lines)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800455 return false;
456 }
457 if (close(fd) < 0) {
458 PLOG(WARNING) << "Failed to close descriptor";
459 }
460 return true;
461 }
462
463 int DumpClasses() {
David Sehr153da0e2017-02-15 08:54:51 -0800464 // Validate that at least one profile file or reference was specified.
465 if (profile_files_.empty() && profile_files_fd_.empty() &&
466 reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
467 Usage("No profile files or reference profile specified.");
468 }
David Sehr7c80f2d2017-02-07 16:47:58 -0800469 // Open apk/zip files and and read dex files.
470 MemMap::Init(); // for ZipArchive::OpenFromFd
471 // Open the dex files to get the names for classes.
472 std::vector<std::unique_ptr<const DexFile>> dex_files;
473 OpenApkFilesFromLocations(&dex_files);
474 // Build a vector of class names from individual profile files.
475 std::set<std::string> class_names;
476 if (!profile_files_fd_.empty()) {
477 for (int profile_file_fd : profile_files_fd_) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700478 if (!GetClassNamesAndMethods(profile_file_fd, &dex_files, &class_names)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800479 return -1;
480 }
481 }
482 }
483 if (!profile_files_.empty()) {
484 for (const std::string& profile_file : profile_files_) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700485 if (!GetClassNamesAndMethods(profile_file, &dex_files, &class_names)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800486 return -1;
487 }
488 }
489 }
490 // Concatenate class names from reference profile file.
491 if (FdIsValid(reference_profile_file_fd_)) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700492 if (!GetClassNamesAndMethods(reference_profile_file_fd_, &dex_files, &class_names)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800493 return -1;
494 }
495 }
496 if (!reference_profile_file_.empty()) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700497 if (!GetClassNamesAndMethods(reference_profile_file_, &dex_files, &class_names)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800498 return -1;
499 }
500 }
501 // Dump the class names.
502 std::string dump;
503 for (const std::string& class_name : class_names) {
504 dump += class_name + std::string("\n");
505 }
506 if (!FdIsValid(dump_output_to_fd_)) {
507 std::cout << dump;
508 } else {
509 unix_file::FdFile out_fd(dump_output_to_fd_, false /*check_usage*/);
510 if (!out_fd.WriteFully(dump.c_str(), dump.length())) {
511 return -1;
512 }
513 }
514 return 0;
515 }
516
Mathieu Chartier34067262017-04-06 13:55:46 -0700517 bool ShouldOnlyDumpClassesAndMethods() {
518 return dump_classes_and_methods_;
David Sehr7c80f2d2017-02-07 16:47:58 -0800519 }
520
521 // Read lines from the given file, dropping comments and empty lines. Post-process each line with
522 // the given function.
523 template <typename T>
524 static T* ReadCommentedInputFromFile(
525 const char* input_filename, std::function<std::string(const char*)>* process) {
526 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
527 if (input_file.get() == nullptr) {
528 LOG(ERROR) << "Failed to open input file " << input_filename;
529 return nullptr;
530 }
531 std::unique_ptr<T> result(
532 ReadCommentedInputStream<T>(*input_file, process));
533 input_file->close();
534 return result.release();
535 }
536
537 // Read lines from the given stream, dropping comments and empty lines. Post-process each line
538 // with the given function.
539 template <typename T>
540 static T* ReadCommentedInputStream(
541 std::istream& in_stream,
542 std::function<std::string(const char*)>* process) {
543 std::unique_ptr<T> output(new T());
544 while (in_stream.good()) {
545 std::string dot;
546 std::getline(in_stream, dot);
547 if (android::base::StartsWith(dot, "#") || dot.empty()) {
548 continue;
549 }
550 if (process != nullptr) {
551 std::string descriptor((*process)(dot.c_str()));
552 output->insert(output->end(), descriptor);
553 } else {
554 output->insert(output->end(), dot);
555 }
556 }
557 return output.release();
558 }
559
Calin Juravlee0ac1152017-02-13 19:03:47 -0800560 // Find class klass_descriptor in the given dex_files and store its reference
561 // in the out parameter class_ref.
562 // Return true if the definition of the class was found in any of the dex_files.
563 bool FindClass(const std::vector<std::unique_ptr<const DexFile>>& dex_files,
564 const std::string& klass_descriptor,
565 /*out*/ProfileMethodInfo::ProfileClassReference* class_ref) {
566 for (const std::unique_ptr<const DexFile>& dex_file_ptr : dex_files) {
567 const DexFile* dex_file = dex_file_ptr.get();
568 const DexFile::TypeId* type_id = dex_file->FindTypeId(klass_descriptor.c_str());
569 if (type_id == nullptr) {
570 continue;
571 }
572 dex::TypeIndex type_index = dex_file->GetIndexForTypeId(*type_id);
573 if (dex_file->FindClassDef(type_index) == nullptr) {
574 // Class is only referenced in the current dex file but not defined in it.
575 continue;
576 }
577 class_ref->dex_file = dex_file;
578 class_ref->type_index = type_index;
579 return true;
580 }
581 return false;
582 }
583
Mathieu Chartier34067262017-04-06 13:55:46 -0700584 // Find the method specified by method_spec in the class class_ref.
585 uint32_t FindMethodIndex(const ProfileMethodInfo::ProfileClassReference& class_ref,
586 const std::string& method_spec) {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800587 std::vector<std::string> name_and_signature;
588 Split(method_spec, kProfileParsingFirstCharInSignature, &name_and_signature);
589 if (name_and_signature.size() != 2) {
590 LOG(ERROR) << "Invalid method name and signature " << method_spec;
591 }
592 const std::string& name = name_and_signature[0];
593 const std::string& signature = kProfileParsingFirstCharInSignature + name_and_signature[1];
594 const DexFile* dex_file = class_ref.dex_file;
595
596 const DexFile::StringId* name_id = dex_file->FindStringId(name.c_str());
597 if (name_id == nullptr) {
Mathieu Chartier66aae3b2017-05-18 10:38:28 -0700598 LOG(WARNING) << "Could not find name: " << name;
Mathieu Chartier34067262017-04-06 13:55:46 -0700599 return DexFile::kDexNoIndex;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800600 }
601 dex::TypeIndex return_type_idx;
602 std::vector<dex::TypeIndex> param_type_idxs;
603 if (!dex_file->CreateTypeList(signature, &return_type_idx, &param_type_idxs)) {
Mathieu Chartier66aae3b2017-05-18 10:38:28 -0700604 LOG(WARNING) << "Could not create type list" << signature;
Mathieu Chartier34067262017-04-06 13:55:46 -0700605 return DexFile::kDexNoIndex;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800606 }
607 const DexFile::ProtoId* proto_id = dex_file->FindProtoId(return_type_idx, param_type_idxs);
608 if (proto_id == nullptr) {
Mathieu Chartier66aae3b2017-05-18 10:38:28 -0700609 LOG(WARNING) << "Could not find proto_id: " << name;
Mathieu Chartier34067262017-04-06 13:55:46 -0700610 return DexFile::kDexNoIndex;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800611 }
612 const DexFile::MethodId* method_id = dex_file->FindMethodId(
613 dex_file->GetTypeId(class_ref.type_index), *name_id, *proto_id);
614 if (method_id == nullptr) {
Mathieu Chartier66aae3b2017-05-18 10:38:28 -0700615 LOG(WARNING) << "Could not find method_id: " << name;
Mathieu Chartier34067262017-04-06 13:55:46 -0700616 return DexFile::kDexNoIndex;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800617 }
618
Mathieu Chartier34067262017-04-06 13:55:46 -0700619 return dex_file->GetIndexForMethodId(*method_id);
620 }
Calin Juravlee0ac1152017-02-13 19:03:47 -0800621
Mathieu Chartier34067262017-04-06 13:55:46 -0700622 // Given a method, return true if the method has a single INVOKE_VIRTUAL in its byte code.
623 // Upon success it returns true and stores the method index and the invoke dex pc
624 // in the output parameters.
625 // The format of the method spec is "inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;".
626 //
627 // TODO(calin): support INVOKE_INTERFACE and the range variants.
628 bool HasSingleInvoke(const ProfileMethodInfo::ProfileClassReference& class_ref,
629 uint16_t method_index,
630 /*out*/uint32_t* dex_pc) {
631 const DexFile* dex_file = class_ref.dex_file;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800632 uint32_t offset = dex_file->FindCodeItemOffset(
633 *dex_file->FindClassDef(class_ref.type_index),
Mathieu Chartier34067262017-04-06 13:55:46 -0700634 method_index);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800635 const DexFile::CodeItem* code_item = dex_file->GetCodeItem(offset);
636
637 bool found_invoke = false;
638 for (CodeItemIterator it(*code_item); !it.Done(); it.Advance()) {
639 if (it.CurrentInstruction().Opcode() == Instruction::INVOKE_VIRTUAL) {
640 if (found_invoke) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700641 LOG(ERROR) << "Multiple invoke INVOKE_VIRTUAL found: "
642 << dex_file->PrettyMethod(method_index);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800643 return false;
644 }
645 found_invoke = true;
646 *dex_pc = it.CurrentDexPc();
647 }
648 }
649 if (!found_invoke) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700650 LOG(ERROR) << "Could not find any INVOKE_VIRTUAL: " << dex_file->PrettyMethod(method_index);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800651 }
652 return found_invoke;
653 }
654
655 // Process a line defining a class or a method and its inline caches.
656 // Upon success return true and add the class or the method info to profile.
Calin Juravle589e71e2017-03-03 16:05:05 -0800657 // The possible line formats are:
658 // "LJustTheCass;".
Calin Juravlee0ac1152017-02-13 19:03:47 -0800659 // "LTestInline;->inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;".
Calin Juravle589e71e2017-03-03 16:05:05 -0800660 // "LTestInline;->inlineMissingTypes(LSuper;)I+missing_types".
661 // "LTestInline;->inlineNoInlineCaches(LSuper;)I".
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700662 // "LTestInline;->*".
Calin Juravlee0ac1152017-02-13 19:03:47 -0800663 // The method and classes are searched only in the given dex files.
664 bool ProcessLine(const std::vector<std::unique_ptr<const DexFile>>& dex_files,
665 const std::string& line,
666 /*out*/ProfileCompilationInfo* profile) {
667 std::string klass;
668 std::string method_str;
669 size_t method_sep_index = line.find(kMethodSep);
670 if (method_sep_index == std::string::npos) {
671 klass = line;
672 } else {
673 klass = line.substr(0, method_sep_index);
674 method_str = line.substr(method_sep_index + kMethodSep.size());
675 }
676
677 ProfileMethodInfo::ProfileClassReference class_ref;
678 if (!FindClass(dex_files, klass, &class_ref)) {
Mathieu Chartier3f121342017-03-06 11:16:20 -0800679 LOG(WARNING) << "Could not find class: " << klass;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800680 return false;
681 }
682
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700683 if (method_str.empty() || method_str == kClassAllMethods) {
684 // Start by adding the class.
Calin Juravlee0ac1152017-02-13 19:03:47 -0800685 std::set<DexCacheResolvedClasses> resolved_class_set;
686 const DexFile* dex_file = class_ref.dex_file;
687 const auto& dex_resolved_classes = resolved_class_set.emplace(
688 dex_file->GetLocation(),
689 dex_file->GetBaseLocation(),
690 dex_file->GetLocationChecksum());
691 dex_resolved_classes.first->AddClass(class_ref.type_index);
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700692 std::vector<ProfileMethodInfo> methods;
693 if (method_str == kClassAllMethods) {
694 // Add all of the methods.
695 const DexFile::ClassDef* class_def = dex_file->FindClassDef(class_ref.type_index);
696 const uint8_t* class_data = dex_file->GetClassData(*class_def);
697 if (class_data != nullptr) {
698 ClassDataItemIterator it(*dex_file, class_data);
699 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
700 it.Next();
701 }
702 while (it.HasNextDirectMethod() || it.HasNextVirtualMethod()) {
703 if (it.GetMethodCodeItemOffset() != 0) {
704 // Add all of the methods that have code to the profile.
705 const uint32_t method_idx = it.GetMemberIndex();
706 methods.push_back(ProfileMethodInfo(dex_file, method_idx));
707 }
708 it.Next();
709 }
710 }
711 }
712 profile->AddMethodsAndClasses(methods, resolved_class_set);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800713 return true;
714 }
715
716 // Process the method.
717 std::string method_spec;
718 std::vector<std::string> inline_cache_elems;
719
720 std::vector<std::string> method_elems;
Calin Juravle589e71e2017-03-03 16:05:05 -0800721 bool is_missing_types = false;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800722 Split(method_str, kProfileParsingInlineChacheSep, &method_elems);
723 if (method_elems.size() == 2) {
724 method_spec = method_elems[0];
Calin Juravle589e71e2017-03-03 16:05:05 -0800725 is_missing_types = method_elems[1] == kMissingTypesMarker;
726 if (!is_missing_types) {
727 Split(method_elems[1], kProfileParsingTypeSep, &inline_cache_elems);
728 }
Calin Juravlee0ac1152017-02-13 19:03:47 -0800729 } else if (method_elems.size() == 1) {
730 method_spec = method_elems[0];
731 } else {
732 LOG(ERROR) << "Invalid method line: " << line;
733 return false;
734 }
735
Mathieu Chartier34067262017-04-06 13:55:46 -0700736 const uint32_t method_index = FindMethodIndex(class_ref, method_spec);
737 if (method_index == DexFile::kDexNoIndex) {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800738 return false;
739 }
Mathieu Chartier34067262017-04-06 13:55:46 -0700740
741 std::vector<ProfileMethodInfo> pmi;
742 std::vector<ProfileMethodInfo::ProfileInlineCache> inline_caches;
743 if (is_missing_types || !inline_cache_elems.empty()) {
744 uint32_t dex_pc;
745 if (!HasSingleInvoke(class_ref, method_index, &dex_pc)) {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800746 return false;
747 }
Mathieu Chartier34067262017-04-06 13:55:46 -0700748 std::vector<ProfileMethodInfo::ProfileClassReference> classes(inline_cache_elems.size());
749 size_t class_it = 0;
750 for (const std::string& ic_class : inline_cache_elems) {
751 if (!FindClass(dex_files, ic_class, &(classes[class_it++]))) {
752 LOG(ERROR) << "Could not find class: " << ic_class;
753 return false;
754 }
755 }
756 inline_caches.emplace_back(dex_pc, is_missing_types, classes);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800757 }
Calin Juravlee0ac1152017-02-13 19:03:47 -0800758 pmi.emplace_back(class_ref.dex_file, method_index, inline_caches);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800759 profile->AddMethodsAndClasses(pmi, std::set<DexCacheResolvedClasses>());
760 return true;
761 }
762
763 // Creates a profile from a human friendly textual representation.
764 // The expected input format is:
765 // # Classes
766 // Ljava/lang/Comparable;
767 // Ljava/lang/Math;
768 // # Methods with inline caches
769 // LTestInline;->inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;
770 // LTestInline;->noInlineCache(LSuper;)I
David Sehr7c80f2d2017-02-07 16:47:58 -0800771 int CreateProfile() {
David Sehr153da0e2017-02-15 08:54:51 -0800772 // Validate parameters for this command.
773 if (apk_files_.empty() && apks_fd_.empty()) {
774 Usage("APK files must be specified");
775 }
776 if (dex_locations_.empty()) {
777 Usage("DEX locations must be specified");
778 }
779 if (reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
780 Usage("Reference profile must be specified with --reference-profile-file or "
781 "--reference-profile-file-fd");
782 }
783 if (!profile_files_.empty() || !profile_files_fd_.empty()) {
784 Usage("Profile must be specified with --reference-profile-file or "
785 "--reference-profile-file-fd");
786 }
787 // for ZipArchive::OpenFromFd
788 MemMap::Init();
David Sehr7c80f2d2017-02-07 16:47:58 -0800789 // Open the profile output file if needed.
790 int fd = reference_profile_file_fd_;
791 if (!FdIsValid(fd)) {
792 CHECK(!reference_profile_file_.empty());
793 fd = open(reference_profile_file_.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
794 if (fd < 0) {
795 LOG(ERROR) << "Cannot open " << reference_profile_file_ << strerror(errno);
796 return -1;
797 }
798 }
Calin Juravlee0ac1152017-02-13 19:03:47 -0800799 // Read the user-specified list of classes and methods.
David Sehr7c80f2d2017-02-07 16:47:58 -0800800 std::unique_ptr<std::unordered_set<std::string>>
Calin Juravlee0ac1152017-02-13 19:03:47 -0800801 user_lines(ReadCommentedInputFromFile<std::unordered_set<std::string>>(
David Sehr7c80f2d2017-02-07 16:47:58 -0800802 create_profile_from_file_.c_str(), nullptr)); // No post-processing.
Calin Juravlee0ac1152017-02-13 19:03:47 -0800803
804 // Open the dex files to look up classes and methods.
David Sehr7c80f2d2017-02-07 16:47:58 -0800805 std::vector<std::unique_ptr<const DexFile>> dex_files;
806 OpenApkFilesFromLocations(&dex_files);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800807
808 // Process the lines one by one and add the successful ones to the profile.
David Sehr7c80f2d2017-02-07 16:47:58 -0800809 ProfileCompilationInfo info;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800810
811 for (const auto& line : *user_lines) {
812 ProcessLine(dex_files, line, &info);
813 }
814
David Sehr7c80f2d2017-02-07 16:47:58 -0800815 // Write the profile file.
816 CHECK(info.Save(fd));
817 if (close(fd) < 0) {
818 PLOG(WARNING) << "Failed to close descriptor";
819 }
820 return 0;
821 }
822
823 bool ShouldCreateProfile() {
824 return !create_profile_from_file_.empty();
825 }
826
Calin Juravle7bcdb532016-06-07 16:14:47 +0100827 int GenerateTestProfile() {
David Sehr153da0e2017-02-15 08:54:51 -0800828 // Validate parameters for this command.
829 if (test_profile_method_ratio_ > 100) {
830 Usage("Invalid ratio for --generate-test-profile-method-ratio");
831 }
832 if (test_profile_class_ratio_ > 100) {
833 Usage("Invalid ratio for --generate-test-profile-class-ratio");
834 }
Jeff Haof0a31f82017-03-27 15:50:37 -0700835 // If given APK files or DEX locations, check that they're ok.
836 if (!apk_files_.empty() || !apks_fd_.empty() || !dex_locations_.empty()) {
837 if (apk_files_.empty() && apks_fd_.empty()) {
838 Usage("APK files must be specified when passing DEX locations to --generate-test-profile");
839 }
840 if (dex_locations_.empty()) {
841 Usage("DEX locations must be specified when passing APK files to --generate-test-profile");
842 }
843 }
David Sehr153da0e2017-02-15 08:54:51 -0800844 // ShouldGenerateTestProfile confirms !test_profile_.empty().
George Burgess IV71f77262016-10-25 13:08:17 -0700845 int profile_test_fd = open(test_profile_.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
Calin Juravle7bcdb532016-06-07 16:14:47 +0100846 if (profile_test_fd < 0) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800847 LOG(ERROR) << "Cannot open " << test_profile_ << strerror(errno);
Calin Juravle7bcdb532016-06-07 16:14:47 +0100848 return -1;
849 }
Jeff Haof0a31f82017-03-27 15:50:37 -0700850 bool result;
851 if (apk_files_.empty() && apks_fd_.empty() && dex_locations_.empty()) {
852 result = ProfileCompilationInfo::GenerateTestProfile(profile_test_fd,
853 test_profile_num_dex_,
854 test_profile_method_ratio_,
855 test_profile_class_ratio_,
856 test_profile_seed_);
857 } else {
858 // Initialize MemMap for ZipArchive::OpenFromFd.
859 MemMap::Init();
860 // Open the dex files to look up classes and methods.
861 std::vector<std::unique_ptr<const DexFile>> dex_files;
862 OpenApkFilesFromLocations(&dex_files);
863 // Create a random profile file based on the set of dex files.
864 result = ProfileCompilationInfo::GenerateTestProfile(profile_test_fd,
865 dex_files,
866 test_profile_seed_);
867 }
Calin Juravle7bcdb532016-06-07 16:14:47 +0100868 close(profile_test_fd); // ignore close result.
869 return result ? 0 : -1;
870 }
871
872 bool ShouldGenerateTestProfile() {
873 return !test_profile_.empty();
874 }
875
Calin Juravle2e2db782016-02-23 12:00:03 +0000876 private:
877 static void ParseFdForCollection(const StringPiece& option,
878 const char* arg_name,
879 std::vector<int>* fds) {
880 int fd;
881 ParseUintOption(option, arg_name, &fd, Usage);
882 fds->push_back(fd);
883 }
884
885 static void CloseAllFds(const std::vector<int>& fds, const char* descriptor) {
886 for (size_t i = 0; i < fds.size(); i++) {
887 if (close(fds[i]) < 0) {
888 PLOG(WARNING) << "Failed to close descriptor for " << descriptor << " at index " << i;
889 }
890 }
891 }
892
893 void LogCompletionTime() {
David Sehr52683cf2016-05-05 09:02:38 -0700894 static constexpr uint64_t kLogThresholdTime = MsToNs(100); // 100ms
895 uint64_t time_taken = NanoTime() - start_ns_;
David Sehred793012016-05-05 13:39:43 -0700896 if (time_taken > kLogThresholdTime) {
David Sehrd8557182016-05-06 12:29:35 -0700897 LOG(WARNING) << "profman took " << PrettyDuration(time_taken);
David Sehred793012016-05-05 13:39:43 -0700898 }
Calin Juravle2e2db782016-02-23 12:00:03 +0000899 }
900
901 std::vector<std::string> profile_files_;
902 std::vector<int> profile_files_fd_;
David Sehr546d24f2016-06-02 10:46:19 -0700903 std::vector<std::string> dex_locations_;
David Sehr7c80f2d2017-02-07 16:47:58 -0800904 std::vector<std::string> apk_files_;
David Sehr546d24f2016-06-02 10:46:19 -0700905 std::vector<int> apks_fd_;
Calin Juravle2e2db782016-02-23 12:00:03 +0000906 std::string reference_profile_file_;
907 int reference_profile_file_fd_;
David Sehr4fcdd6d2016-05-24 14:52:31 -0700908 bool dump_only_;
Mathieu Chartier34067262017-04-06 13:55:46 -0700909 bool dump_classes_and_methods_;
David Sehr4fcdd6d2016-05-24 14:52:31 -0700910 int dump_output_to_fd_;
Calin Juravle7bcdb532016-06-07 16:14:47 +0100911 std::string test_profile_;
David Sehr7c80f2d2017-02-07 16:47:58 -0800912 std::string create_profile_from_file_;
Calin Juravle7bcdb532016-06-07 16:14:47 +0100913 uint16_t test_profile_num_dex_;
914 uint16_t test_profile_method_ratio_;
915 uint16_t test_profile_class_ratio_;
Jeff Haof0a31f82017-03-27 15:50:37 -0700916 uint32_t test_profile_seed_;
Calin Juravle2e2db782016-02-23 12:00:03 +0000917 uint64_t start_ns_;
918};
919
920// See ProfileAssistant::ProcessingResult for return codes.
921static int profman(int argc, char** argv) {
922 ProfMan profman;
923
924 // Parse arguments. Argument mistakes will lead to exit(EXIT_FAILURE) in UsageError.
925 profman.ParseArgs(argc, argv);
926
Calin Juravle7bcdb532016-06-07 16:14:47 +0100927 if (profman.ShouldGenerateTestProfile()) {
928 return profman.GenerateTestProfile();
929 }
Calin Juravle876f3502016-03-24 16:16:34 +0000930 if (profman.ShouldOnlyDumpProfile()) {
931 return profman.DumpProfileInfo();
932 }
Mathieu Chartier34067262017-04-06 13:55:46 -0700933 if (profman.ShouldOnlyDumpClassesAndMethods()) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800934 return profman.DumpClasses();
935 }
936 if (profman.ShouldCreateProfile()) {
937 return profman.CreateProfile();
938 }
Calin Juravle2e2db782016-02-23 12:00:03 +0000939 // Process profile information and assess if we need to do a profile guided compilation.
940 // This operation involves I/O.
941 return profman.ProcessProfiles();
942}
943
944} // namespace art
945
946int main(int argc, char **argv) {
947 return art::profman(argc, argv);
948}
949