blob: 8f35a76b6dbebd26aeed3ac5a38573bf3502740a [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"
David Sehr4fcdd6d2016-05-24 14:52:31 -070039#include "dex_file.h"
Calin Juravle33083d62017-01-18 15:29:12 -080040#include "jit/profile_compilation_info.h"
David Sehrf57589f2016-10-17 10:09:33 -070041#include "runtime.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000042#include "utils.h"
David Sehr546d24f2016-06-02 10:46:19 -070043#include "zip_archive.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000044#include "profile_assistant.h"
45
46namespace art {
47
48static int original_argc;
49static char** original_argv;
50
51static std::string CommandLine() {
52 std::vector<std::string> command;
53 for (int i = 0; i < original_argc; ++i) {
54 command.push_back(original_argv[i]);
55 }
Andreas Gampe9186ced2016-12-12 14:28:21 -080056 return android::base::Join(command, ' ');
Calin Juravle2e2db782016-02-23 12:00:03 +000057}
58
David Sehr4fcdd6d2016-05-24 14:52:31 -070059static constexpr int kInvalidFd = -1;
60
61static bool FdIsValid(int fd) {
62 return fd != kInvalidFd;
63}
64
Calin Juravle2e2db782016-02-23 12:00:03 +000065static void UsageErrorV(const char* fmt, va_list ap) {
66 std::string error;
Andreas Gampe46ee31b2016-12-14 10:11:49 -080067 android::base::StringAppendV(&error, fmt, ap);
Calin Juravle2e2db782016-02-23 12:00:03 +000068 LOG(ERROR) << error;
69}
70
71static void UsageError(const char* fmt, ...) {
72 va_list ap;
73 va_start(ap, fmt);
74 UsageErrorV(fmt, ap);
75 va_end(ap);
76}
77
78NO_RETURN static void Usage(const char *fmt, ...) {
79 va_list ap;
80 va_start(ap, fmt);
81 UsageErrorV(fmt, ap);
82 va_end(ap);
83
84 UsageError("Command: %s", CommandLine().c_str());
85 UsageError("Usage: profman [options]...");
86 UsageError("");
David Sehr4fcdd6d2016-05-24 14:52:31 -070087 UsageError(" --dump-only: dumps the content of the specified profile files");
88 UsageError(" to standard output (default) in a human readable form.");
89 UsageError("");
David Sehr7c80f2d2017-02-07 16:47:58 -080090 UsageError(" --dump-output-to-fd=<number>: redirects --dump-only output to a file descriptor.");
91 UsageError("");
92 UsageError(" --dump-classes: dumps a sorted list of classes that are in the specified profile");
93 UsageError(" file to standard output (default) in a human readable form.");
Calin Juravle876f3502016-03-24 16:16:34 +000094 UsageError("");
Calin Juravle2e2db782016-02-23 12:00:03 +000095 UsageError(" --profile-file=<filename>: specify profiler output file to use for compilation.");
96 UsageError(" Can be specified multiple time, in which case the data from the different");
97 UsageError(" profiles will be aggregated.");
98 UsageError("");
99 UsageError(" --profile-file-fd=<number>: same as --profile-file but accepts a file descriptor.");
100 UsageError(" Cannot be used together with --profile-file.");
101 UsageError("");
102 UsageError(" --reference-profile-file=<filename>: specify a reference profile.");
103 UsageError(" The data in this file will be compared with the data obtained by merging");
104 UsageError(" all the files specified with --profile-file or --profile-file-fd.");
105 UsageError(" If the exit code is EXIT_COMPILE then all --profile-file will be merged into");
106 UsageError(" --reference-profile-file. ");
107 UsageError("");
108 UsageError(" --reference-profile-file-fd=<number>: same as --reference-profile-file but");
109 UsageError(" accepts a file descriptor. Cannot be used together with");
110 UsageError(" --reference-profile-file.");
David Sehr7c80f2d2017-02-07 16:47:58 -0800111 UsageError("");
Calin Juravle7bcdb532016-06-07 16:14:47 +0100112 UsageError(" --generate-test-profile=<filename>: generates a random profile file for testing.");
113 UsageError(" --generate-test-profile-num-dex=<number>: number of dex files that should be");
114 UsageError(" included in the generated profile. Defaults to 20.");
115 UsageError(" --generate-test-profile-method-ratio=<number>: the percentage from the maximum");
116 UsageError(" number of methods that should be generated. Defaults to 5.");
117 UsageError(" --generate-test-profile-class-ratio=<number>: the percentage from the maximum");
118 UsageError(" number of classes that should be generated. Defaults to 5.");
119 UsageError("");
David Sehr7c80f2d2017-02-07 16:47:58 -0800120 UsageError(" --create-profile-from=<filename>: creates a profile from a list of classes.");
121 UsageError("");
Calin Juravle2e2db782016-02-23 12:00:03 +0000122 UsageError("");
David Sehr546d24f2016-06-02 10:46:19 -0700123 UsageError(" --dex-location=<string>: location string to use with corresponding");
124 UsageError(" apk-fd to find dex files");
David Sehr4fcdd6d2016-05-24 14:52:31 -0700125 UsageError("");
David Sehr546d24f2016-06-02 10:46:19 -0700126 UsageError(" --apk-fd=<number>: file descriptor containing an open APK to");
David Sehr4fcdd6d2016-05-24 14:52:31 -0700127 UsageError(" search for dex files");
David Sehr7c80f2d2017-02-07 16:47:58 -0800128 UsageError(" --apk-=<filename>: an APK to search for dex files");
David Sehr4fcdd6d2016-05-24 14:52:31 -0700129 UsageError("");
Calin Juravle2e2db782016-02-23 12:00:03 +0000130
131 exit(EXIT_FAILURE);
132}
133
Calin Juravle7bcdb532016-06-07 16:14:47 +0100134// Note: make sure you update the Usage if you change these values.
135static constexpr uint16_t kDefaultTestProfileNumDex = 20;
136static constexpr uint16_t kDefaultTestProfileMethodRatio = 5;
137static constexpr uint16_t kDefaultTestProfileClassRatio = 5;
138
Calin Juravle2e2db782016-02-23 12:00:03 +0000139class ProfMan FINAL {
140 public:
141 ProfMan() :
David Sehr4fcdd6d2016-05-24 14:52:31 -0700142 reference_profile_file_fd_(kInvalidFd),
143 dump_only_(false),
David Sehr7c80f2d2017-02-07 16:47:58 -0800144 dump_classes_(false),
David Sehr4fcdd6d2016-05-24 14:52:31 -0700145 dump_output_to_fd_(kInvalidFd),
Calin Juravle7bcdb532016-06-07 16:14:47 +0100146 test_profile_num_dex_(kDefaultTestProfileNumDex),
147 test_profile_method_ratio_(kDefaultTestProfileMethodRatio),
148 test_profile_class_ratio_(kDefaultTestProfileClassRatio),
Calin Juravle2e2db782016-02-23 12:00:03 +0000149 start_ns_(NanoTime()) {}
150
151 ~ProfMan() {
152 LogCompletionTime();
153 }
154
155 void ParseArgs(int argc, char **argv) {
156 original_argc = argc;
157 original_argv = argv;
158
David Sehrf57589f2016-10-17 10:09:33 -0700159 InitLogging(argv, Runtime::Aborter);
Calin Juravle2e2db782016-02-23 12:00:03 +0000160
161 // Skip over the command name.
162 argv++;
163 argc--;
164
165 if (argc == 0) {
166 Usage("No arguments specified");
167 }
168
169 for (int i = 0; i < argc; ++i) {
170 const StringPiece option(argv[i]);
171 const bool log_options = false;
172 if (log_options) {
Calin Juravle876f3502016-03-24 16:16:34 +0000173 LOG(INFO) << "profman: option[" << i << "]=" << argv[i];
Calin Juravle2e2db782016-02-23 12:00:03 +0000174 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700175 if (option == "--dump-only") {
176 dump_only_ = true;
David Sehr7c80f2d2017-02-07 16:47:58 -0800177 } else if (option == "--dump-classes") {
178 dump_classes_ = true;
179 } else if (option.starts_with("--create-profile-from=")) {
180 create_profile_from_file_ = option.substr(strlen("--create-profile-from=")).ToString();
David Sehr4fcdd6d2016-05-24 14:52:31 -0700181 } else if (option.starts_with("--dump-output-to-fd=")) {
182 ParseUintOption(option, "--dump-output-to-fd", &dump_output_to_fd_, Usage);
Calin Juravle876f3502016-03-24 16:16:34 +0000183 } else if (option.starts_with("--profile-file=")) {
Calin Juravle2e2db782016-02-23 12:00:03 +0000184 profile_files_.push_back(option.substr(strlen("--profile-file=")).ToString());
185 } else if (option.starts_with("--profile-file-fd=")) {
186 ParseFdForCollection(option, "--profile-file-fd", &profile_files_fd_);
187 } else if (option.starts_with("--reference-profile-file=")) {
188 reference_profile_file_ = option.substr(strlen("--reference-profile-file=")).ToString();
189 } else if (option.starts_with("--reference-profile-file-fd=")) {
190 ParseUintOption(option, "--reference-profile-file-fd", &reference_profile_file_fd_, Usage);
David Sehr546d24f2016-06-02 10:46:19 -0700191 } else if (option.starts_with("--dex-location=")) {
192 dex_locations_.push_back(option.substr(strlen("--dex-location=")).ToString());
193 } else if (option.starts_with("--apk-fd=")) {
194 ParseFdForCollection(option, "--apk-fd", &apks_fd_);
David Sehr7c80f2d2017-02-07 16:47:58 -0800195 } else if (option.starts_with("--apk=")) {
196 apk_files_.push_back(option.substr(strlen("--apk=")).ToString());
Calin Juravle7bcdb532016-06-07 16:14:47 +0100197 } else if (option.starts_with("--generate-test-profile=")) {
198 test_profile_ = option.substr(strlen("--generate-test-profile=")).ToString();
199 } else if (option.starts_with("--generate-test-profile-num-dex=")) {
200 ParseUintOption(option,
201 "--generate-test-profile-num-dex",
202 &test_profile_num_dex_,
203 Usage);
204 } else if (option.starts_with("--generate-test-profile-method-ratio")) {
205 ParseUintOption(option,
206 "--generate-test-profile-method-ratio",
207 &test_profile_method_ratio_,
208 Usage);
209 } else if (option.starts_with("--generate-test-profile-class-ratio")) {
210 ParseUintOption(option,
211 "--generate-test-profile-class-ratio",
212 &test_profile_class_ratio_,
213 Usage);
Calin Juravle2e2db782016-02-23 12:00:03 +0000214 } else {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700215 Usage("Unknown argument '%s'", option.data());
Calin Juravle2e2db782016-02-23 12:00:03 +0000216 }
217 }
218
David Sehr153da0e2017-02-15 08:54:51 -0800219 // Validate global consistency between file/fd options.
Calin Juravle2e2db782016-02-23 12:00:03 +0000220 if (!profile_files_.empty() && !profile_files_fd_.empty()) {
221 Usage("Profile files should not be specified with both --profile-file-fd and --profile-file");
222 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700223 if (!reference_profile_file_.empty() && FdIsValid(reference_profile_file_fd_)) {
224 Usage("Reference profile should not be specified with both "
225 "--reference-profile-file-fd and --reference-profile-file");
226 }
David Sehr153da0e2017-02-15 08:54:51 -0800227 if (!apk_files_.empty() && !apks_fd_.empty()) {
228 Usage("APK files should not be specified with both --apk-fd and --apk");
Calin Juravle2e2db782016-02-23 12:00:03 +0000229 }
230 }
231
232 ProfileAssistant::ProcessingResult ProcessProfiles() {
David Sehr153da0e2017-02-15 08:54:51 -0800233 // Validate that at least one profile file was passed, as well as a reference profile.
234 if (profile_files_.empty() && profile_files_fd_.empty()) {
235 Usage("No profile files specified.");
236 }
237 if (reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
238 Usage("No reference profile file specified.");
239 }
240 if ((!profile_files_.empty() && FdIsValid(reference_profile_file_fd_)) ||
241 (!profile_files_fd_.empty() && !FdIsValid(reference_profile_file_fd_))) {
242 Usage("Options --profile-file-fd and --reference-profile-file-fd "
243 "should only be used together");
244 }
Calin Juravle2e2db782016-02-23 12:00:03 +0000245 ProfileAssistant::ProcessingResult result;
246 if (profile_files_.empty()) {
247 // The file doesn't need to be flushed here (ProcessProfiles will do it)
248 // so don't check the usage.
249 File file(reference_profile_file_fd_, false);
250 result = ProfileAssistant::ProcessProfiles(profile_files_fd_, reference_profile_file_fd_);
251 CloseAllFds(profile_files_fd_, "profile_files_fd_");
252 } else {
253 result = ProfileAssistant::ProcessProfiles(profile_files_, reference_profile_file_);
254 }
255 return result;
256 }
257
David Sehr7c80f2d2017-02-07 16:47:58 -0800258 void OpenApkFilesFromLocations(std::vector<std::unique_ptr<const DexFile>>* dex_files) {
259 bool use_apk_fd_list = !apks_fd_.empty();
260 if (use_apk_fd_list) {
David Sehr153da0e2017-02-15 08:54:51 -0800261 // Get the APKs from the collection of FDs.
David Sehr7c80f2d2017-02-07 16:47:58 -0800262 CHECK_EQ(dex_locations_.size(), apks_fd_.size());
David Sehr153da0e2017-02-15 08:54:51 -0800263 } else if (!apk_files_.empty()) {
264 // Get the APKs from the collection of filenames.
David Sehr7c80f2d2017-02-07 16:47:58 -0800265 CHECK_EQ(dex_locations_.size(), apk_files_.size());
David Sehr153da0e2017-02-15 08:54:51 -0800266 } else {
267 // No APKs were specified.
268 CHECK(dex_locations_.empty());
269 return;
David Sehr7c80f2d2017-02-07 16:47:58 -0800270 }
271 static constexpr bool kVerifyChecksum = true;
272 for (size_t i = 0; i < dex_locations_.size(); ++i) {
273 std::string error_msg;
274 std::vector<std::unique_ptr<const DexFile>> dex_files_for_location;
275 if (use_apk_fd_list) {
276 if (DexFile::OpenZip(apks_fd_[i],
277 dex_locations_[i],
278 kVerifyChecksum,
279 &error_msg,
280 &dex_files_for_location)) {
281 } else {
282 LOG(WARNING) << "OpenZip failed for '" << dex_locations_[i] << "' " << error_msg;
283 continue;
284 }
285 } else {
286 if (DexFile::Open(apk_files_[i].c_str(),
287 dex_locations_[i],
288 kVerifyChecksum,
289 &error_msg,
290 &dex_files_for_location)) {
291 } else {
292 LOG(WARNING) << "Open failed for '" << dex_locations_[i] << "' " << error_msg;
293 continue;
294 }
295 }
296 for (std::unique_ptr<const DexFile>& dex_file : dex_files_for_location) {
297 dex_files->emplace_back(std::move(dex_file));
298 }
299 }
300 }
301
David Sehrb18991b2017-02-08 20:58:10 -0800302 int DumpOneProfile(const std::string& banner,
303 const std::string& filename,
304 int fd,
305 const std::vector<std::unique_ptr<const DexFile>>* dex_files,
306 std::string* dump) {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700307 if (!filename.empty()) {
308 fd = open(filename.c_str(), O_RDWR);
309 if (fd < 0) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800310 LOG(ERROR) << "Cannot open " << filename << strerror(errno);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700311 return -1;
312 }
Calin Juravle876f3502016-03-24 16:16:34 +0000313 }
314 ProfileCompilationInfo info;
315 if (!info.Load(fd)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800316 LOG(ERROR) << "Cannot load profile info from fd=" << fd << "\n";
Calin Juravle876f3502016-03-24 16:16:34 +0000317 return -1;
318 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700319 std::string this_dump = banner + "\n" + info.DumpInfo(dex_files) + "\n";
320 *dump += this_dump;
321 if (close(fd) < 0) {
322 PLOG(WARNING) << "Failed to close descriptor";
323 }
324 return 0;
325 }
326
327 int DumpProfileInfo() {
David Sehr153da0e2017-02-15 08:54:51 -0800328 // Validate that at least one profile file or reference was specified.
329 if (profile_files_.empty() && profile_files_fd_.empty() &&
330 reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
331 Usage("No profile files or reference profile specified.");
332 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700333 static const char* kEmptyString = "";
David Sehr546d24f2016-06-02 10:46:19 -0700334 static const char* kOrdinaryProfile = "=== profile ===";
335 static const char* kReferenceProfile = "=== reference profile ===";
336
337 // Open apk/zip files and and read dex files.
338 MemMap::Init(); // for ZipArchive::OpenFromFd
David Sehrb18991b2017-02-08 20:58:10 -0800339 std::vector<std::unique_ptr<const DexFile>> dex_files;
David Sehr7c80f2d2017-02-07 16:47:58 -0800340 OpenApkFilesFromLocations(&dex_files);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700341 std::string dump;
David Sehr4fcdd6d2016-05-24 14:52:31 -0700342 // Dump individual profile files.
343 if (!profile_files_fd_.empty()) {
344 for (int profile_file_fd : profile_files_fd_) {
David Sehr546d24f2016-06-02 10:46:19 -0700345 int ret = DumpOneProfile(kOrdinaryProfile,
346 kEmptyString,
347 profile_file_fd,
348 &dex_files,
349 &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700350 if (ret != 0) {
351 return ret;
352 }
353 }
354 }
355 if (!profile_files_.empty()) {
356 for (const std::string& profile_file : profile_files_) {
David Sehr546d24f2016-06-02 10:46:19 -0700357 int ret = DumpOneProfile(kOrdinaryProfile, profile_file, kInvalidFd, &dex_files, &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700358 if (ret != 0) {
359 return ret;
360 }
361 }
362 }
363 // Dump reference profile file.
364 if (FdIsValid(reference_profile_file_fd_)) {
David Sehr546d24f2016-06-02 10:46:19 -0700365 int ret = DumpOneProfile(kReferenceProfile,
366 kEmptyString,
367 reference_profile_file_fd_,
368 &dex_files,
369 &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700370 if (ret != 0) {
371 return ret;
372 }
373 }
374 if (!reference_profile_file_.empty()) {
David Sehr546d24f2016-06-02 10:46:19 -0700375 int ret = DumpOneProfile(kReferenceProfile,
376 reference_profile_file_,
377 kInvalidFd,
378 &dex_files,
David Sehr4fcdd6d2016-05-24 14:52:31 -0700379 &dump);
380 if (ret != 0) {
381 return ret;
382 }
383 }
384 if (!FdIsValid(dump_output_to_fd_)) {
385 std::cout << dump;
386 } else {
387 unix_file::FdFile out_fd(dump_output_to_fd_, false /*check_usage*/);
388 if (!out_fd.WriteFully(dump.c_str(), dump.length())) {
389 return -1;
390 }
391 }
Calin Juravle876f3502016-03-24 16:16:34 +0000392 return 0;
393 }
394
395 bool ShouldOnlyDumpProfile() {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700396 return dump_only_;
Calin Juravle876f3502016-03-24 16:16:34 +0000397 }
398
David Sehr7c80f2d2017-02-07 16:47:58 -0800399 bool GetClassNames(int fd,
400 std::vector<std::unique_ptr<const DexFile>>* dex_files,
401 std::set<std::string>* class_names) {
402 ProfileCompilationInfo profile_info;
403 if (!profile_info.Load(fd)) {
404 LOG(ERROR) << "Cannot load profile info";
405 return false;
406 }
407 profile_info.GetClassNames(dex_files, class_names);
408 return true;
409 }
410
411 bool GetClassNames(std::string profile_file,
412 std::vector<std::unique_ptr<const DexFile>>* dex_files,
413 std::set<std::string>* class_names) {
414 int fd = open(profile_file.c_str(), O_RDONLY);
415 if (!FdIsValid(fd)) {
416 LOG(ERROR) << "Cannot open " << profile_file << strerror(errno);
417 return false;
418 }
419 if (!GetClassNames(fd, dex_files, class_names)) {
420 return false;
421 }
422 if (close(fd) < 0) {
423 PLOG(WARNING) << "Failed to close descriptor";
424 }
425 return true;
426 }
427
428 int DumpClasses() {
David Sehr153da0e2017-02-15 08:54:51 -0800429 // Validate that at least one profile file or reference was specified.
430 if (profile_files_.empty() && profile_files_fd_.empty() &&
431 reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
432 Usage("No profile files or reference profile specified.");
433 }
David Sehr7c80f2d2017-02-07 16:47:58 -0800434 // Open apk/zip files and and read dex files.
435 MemMap::Init(); // for ZipArchive::OpenFromFd
436 // Open the dex files to get the names for classes.
437 std::vector<std::unique_ptr<const DexFile>> dex_files;
438 OpenApkFilesFromLocations(&dex_files);
439 // Build a vector of class names from individual profile files.
440 std::set<std::string> class_names;
441 if (!profile_files_fd_.empty()) {
442 for (int profile_file_fd : profile_files_fd_) {
443 if (!GetClassNames(profile_file_fd, &dex_files, &class_names)) {
444 return -1;
445 }
446 }
447 }
448 if (!profile_files_.empty()) {
449 for (const std::string& profile_file : profile_files_) {
450 if (!GetClassNames(profile_file, &dex_files, &class_names)) {
451 return -1;
452 }
453 }
454 }
455 // Concatenate class names from reference profile file.
456 if (FdIsValid(reference_profile_file_fd_)) {
457 if (!GetClassNames(reference_profile_file_fd_, &dex_files, &class_names)) {
458 return -1;
459 }
460 }
461 if (!reference_profile_file_.empty()) {
462 if (!GetClassNames(reference_profile_file_, &dex_files, &class_names)) {
463 return -1;
464 }
465 }
466 // Dump the class names.
467 std::string dump;
468 for (const std::string& class_name : class_names) {
469 dump += class_name + std::string("\n");
470 }
471 if (!FdIsValid(dump_output_to_fd_)) {
472 std::cout << dump;
473 } else {
474 unix_file::FdFile out_fd(dump_output_to_fd_, false /*check_usage*/);
475 if (!out_fd.WriteFully(dump.c_str(), dump.length())) {
476 return -1;
477 }
478 }
479 return 0;
480 }
481
482 bool ShouldOnlyDumpClasses() {
483 return dump_classes_;
484 }
485
486 // Read lines from the given file, dropping comments and empty lines. Post-process each line with
487 // the given function.
488 template <typename T>
489 static T* ReadCommentedInputFromFile(
490 const char* input_filename, std::function<std::string(const char*)>* process) {
491 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
492 if (input_file.get() == nullptr) {
493 LOG(ERROR) << "Failed to open input file " << input_filename;
494 return nullptr;
495 }
496 std::unique_ptr<T> result(
497 ReadCommentedInputStream<T>(*input_file, process));
498 input_file->close();
499 return result.release();
500 }
501
502 // Read lines from the given stream, dropping comments and empty lines. Post-process each line
503 // with the given function.
504 template <typename T>
505 static T* ReadCommentedInputStream(
506 std::istream& in_stream,
507 std::function<std::string(const char*)>* process) {
508 std::unique_ptr<T> output(new T());
509 while (in_stream.good()) {
510 std::string dot;
511 std::getline(in_stream, dot);
512 if (android::base::StartsWith(dot, "#") || dot.empty()) {
513 continue;
514 }
515 if (process != nullptr) {
516 std::string descriptor((*process)(dot.c_str()));
517 output->insert(output->end(), descriptor);
518 } else {
519 output->insert(output->end(), dot);
520 }
521 }
522 return output.release();
523 }
524
525 int CreateProfile() {
David Sehr153da0e2017-02-15 08:54:51 -0800526 // Validate parameters for this command.
527 if (apk_files_.empty() && apks_fd_.empty()) {
528 Usage("APK files must be specified");
529 }
530 if (dex_locations_.empty()) {
531 Usage("DEX locations must be specified");
532 }
533 if (reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
534 Usage("Reference profile must be specified with --reference-profile-file or "
535 "--reference-profile-file-fd");
536 }
537 if (!profile_files_.empty() || !profile_files_fd_.empty()) {
538 Usage("Profile must be specified with --reference-profile-file or "
539 "--reference-profile-file-fd");
540 }
541 // for ZipArchive::OpenFromFd
542 MemMap::Init();
David Sehr7c80f2d2017-02-07 16:47:58 -0800543 // Open the profile output file if needed.
544 int fd = reference_profile_file_fd_;
545 if (!FdIsValid(fd)) {
546 CHECK(!reference_profile_file_.empty());
547 fd = open(reference_profile_file_.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
548 if (fd < 0) {
549 LOG(ERROR) << "Cannot open " << reference_profile_file_ << strerror(errno);
550 return -1;
551 }
552 }
553 // Read the user-specified list of classes (dot notation rather than descriptors).
554 std::unique_ptr<std::unordered_set<std::string>>
555 user_class_list(ReadCommentedInputFromFile<std::unordered_set<std::string>>(
556 create_profile_from_file_.c_str(), nullptr)); // No post-processing.
557 std::unordered_set<std::string> matched_user_classes;
558 // Open the dex files to look up class names.
559 std::vector<std::unique_ptr<const DexFile>> dex_files;
560 OpenApkFilesFromLocations(&dex_files);
561 // Iterate over the dex files looking for class names in the input stream.
562 std::set<DexCacheResolvedClasses> resolved_class_set;
563 for (auto& dex_file : dex_files) {
564 // Compute the set of classes to be added for this dex file first. This
565 // avoids creating an entry in the profile information for dex files that
566 // contribute no classes.
567 std::unordered_set<dex::TypeIndex> classes_to_be_added;
568 for (const auto& klass : *user_class_list) {
569 std::string descriptor = DotToDescriptor(klass.c_str());
570 const DexFile::TypeId* type_id = dex_file->FindTypeId(descriptor.c_str());
571 if (type_id == nullptr) {
572 continue;
573 }
574 classes_to_be_added.insert(dex_file->GetIndexForTypeId(*type_id));
575 matched_user_classes.insert(klass);
576 }
577 if (classes_to_be_added.empty()) {
578 continue;
579 }
580 // Insert the DexCacheResolved Classes into the set expected for
581 // AddMethodsAndClasses.
582 std::set<DexCacheResolvedClasses>::iterator dex_resolved_classes =
583 resolved_class_set.emplace(dex_file->GetLocation(),
584 dex_file->GetBaseLocation(),
585 dex_file->GetLocationChecksum()).first;
586 dex_resolved_classes->AddClasses(classes_to_be_added.begin(), classes_to_be_added.end());
587 }
588 // Warn the user if we didn't find matches for every class.
589 for (const auto& klass : *user_class_list) {
590 if (matched_user_classes.find(klass) == matched_user_classes.end()) {
591 LOG(WARNING) << "requested class '" << klass << "' was not matched in any dex file";
592 }
593 }
594 // Generate the profile data structure.
595 ProfileCompilationInfo info;
596 std::vector<MethodReference> methods; // No methods for now.
597 info.AddMethodsAndClasses(methods, resolved_class_set);
598 // Write the profile file.
599 CHECK(info.Save(fd));
600 if (close(fd) < 0) {
601 PLOG(WARNING) << "Failed to close descriptor";
602 }
603 return 0;
604 }
605
606 bool ShouldCreateProfile() {
607 return !create_profile_from_file_.empty();
608 }
609
Calin Juravle7bcdb532016-06-07 16:14:47 +0100610 int GenerateTestProfile() {
David Sehr153da0e2017-02-15 08:54:51 -0800611 // Validate parameters for this command.
612 if (test_profile_method_ratio_ > 100) {
613 Usage("Invalid ratio for --generate-test-profile-method-ratio");
614 }
615 if (test_profile_class_ratio_ > 100) {
616 Usage("Invalid ratio for --generate-test-profile-class-ratio");
617 }
618 // ShouldGenerateTestProfile confirms !test_profile_.empty().
George Burgess IV71f77262016-10-25 13:08:17 -0700619 int profile_test_fd = open(test_profile_.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
Calin Juravle7bcdb532016-06-07 16:14:47 +0100620 if (profile_test_fd < 0) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800621 LOG(ERROR) << "Cannot open " << test_profile_ << strerror(errno);
Calin Juravle7bcdb532016-06-07 16:14:47 +0100622 return -1;
623 }
624
625 bool result = ProfileCompilationInfo::GenerateTestProfile(profile_test_fd,
David Sehr7c80f2d2017-02-07 16:47:58 -0800626 test_profile_num_dex_,
627 test_profile_method_ratio_,
628 test_profile_class_ratio_);
Calin Juravle7bcdb532016-06-07 16:14:47 +0100629 close(profile_test_fd); // ignore close result.
630 return result ? 0 : -1;
631 }
632
633 bool ShouldGenerateTestProfile() {
634 return !test_profile_.empty();
635 }
636
Calin Juravle2e2db782016-02-23 12:00:03 +0000637 private:
638 static void ParseFdForCollection(const StringPiece& option,
639 const char* arg_name,
640 std::vector<int>* fds) {
641 int fd;
642 ParseUintOption(option, arg_name, &fd, Usage);
643 fds->push_back(fd);
644 }
645
646 static void CloseAllFds(const std::vector<int>& fds, const char* descriptor) {
647 for (size_t i = 0; i < fds.size(); i++) {
648 if (close(fds[i]) < 0) {
649 PLOG(WARNING) << "Failed to close descriptor for " << descriptor << " at index " << i;
650 }
651 }
652 }
653
654 void LogCompletionTime() {
David Sehr52683cf2016-05-05 09:02:38 -0700655 static constexpr uint64_t kLogThresholdTime = MsToNs(100); // 100ms
656 uint64_t time_taken = NanoTime() - start_ns_;
David Sehred793012016-05-05 13:39:43 -0700657 if (time_taken > kLogThresholdTime) {
David Sehrd8557182016-05-06 12:29:35 -0700658 LOG(WARNING) << "profman took " << PrettyDuration(time_taken);
David Sehred793012016-05-05 13:39:43 -0700659 }
Calin Juravle2e2db782016-02-23 12:00:03 +0000660 }
661
662 std::vector<std::string> profile_files_;
663 std::vector<int> profile_files_fd_;
David Sehr546d24f2016-06-02 10:46:19 -0700664 std::vector<std::string> dex_locations_;
David Sehr7c80f2d2017-02-07 16:47:58 -0800665 std::vector<std::string> apk_files_;
David Sehr546d24f2016-06-02 10:46:19 -0700666 std::vector<int> apks_fd_;
Calin Juravle2e2db782016-02-23 12:00:03 +0000667 std::string reference_profile_file_;
668 int reference_profile_file_fd_;
David Sehr4fcdd6d2016-05-24 14:52:31 -0700669 bool dump_only_;
David Sehr7c80f2d2017-02-07 16:47:58 -0800670 bool dump_classes_;
David Sehr4fcdd6d2016-05-24 14:52:31 -0700671 int dump_output_to_fd_;
Calin Juravle7bcdb532016-06-07 16:14:47 +0100672 std::string test_profile_;
David Sehr7c80f2d2017-02-07 16:47:58 -0800673 std::string create_profile_from_file_;
Calin Juravle7bcdb532016-06-07 16:14:47 +0100674 uint16_t test_profile_num_dex_;
675 uint16_t test_profile_method_ratio_;
676 uint16_t test_profile_class_ratio_;
Calin Juravle2e2db782016-02-23 12:00:03 +0000677 uint64_t start_ns_;
678};
679
680// See ProfileAssistant::ProcessingResult for return codes.
681static int profman(int argc, char** argv) {
682 ProfMan profman;
683
684 // Parse arguments. Argument mistakes will lead to exit(EXIT_FAILURE) in UsageError.
685 profman.ParseArgs(argc, argv);
686
Calin Juravle7bcdb532016-06-07 16:14:47 +0100687 if (profman.ShouldGenerateTestProfile()) {
688 return profman.GenerateTestProfile();
689 }
Calin Juravle876f3502016-03-24 16:16:34 +0000690 if (profman.ShouldOnlyDumpProfile()) {
691 return profman.DumpProfileInfo();
692 }
David Sehr7c80f2d2017-02-07 16:47:58 -0800693 if (profman.ShouldOnlyDumpClasses()) {
694 return profman.DumpClasses();
695 }
696 if (profman.ShouldCreateProfile()) {
697 return profman.CreateProfile();
698 }
Calin Juravle2e2db782016-02-23 12:00:03 +0000699 // Process profile information and assess if we need to do a profile guided compilation.
700 // This operation involves I/O.
701 return profman.ProcessProfiles();
702}
703
704} // namespace art
705
706int main(int argc, char **argv) {
707 return art::profman(argc, argv);
708}
709