blob: f6b145aa760428d31b80af565fcc8f8ed44f8dcf [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
Calin Juravle876f3502016-03-24 16:16:34 +0000219 bool has_profiles = !profile_files_.empty() || !profile_files_fd_.empty();
220 bool has_reference_profile = !reference_profile_file_.empty() ||
David Sehr4fcdd6d2016-05-24 14:52:31 -0700221 FdIsValid(reference_profile_file_fd_);
Calin Juravle876f3502016-03-24 16:16:34 +0000222
Calin Juravle7bcdb532016-06-07 16:14:47 +0100223 if (!test_profile_.empty()) {
224 if (test_profile_method_ratio_ > 100) {
225 Usage("Invalid ratio for --generate-test-profile-method-ratio");
226 }
227 if (test_profile_class_ratio_ > 100) {
228 Usage("Invalid ratio for --generate-test-profile-class-ratio");
229 }
230 return;
231 }
David Sehr7c80f2d2017-02-07 16:47:58 -0800232 if (!apk_files_.empty() && !apks_fd_.empty()) {
233 Usage("APK files should not be specified with both --apk-fd and --apk");
234 }
235 if (!create_profile_from_file_.empty()) {
236 if (apk_files_.empty() && apks_fd_.empty()) {
237 Usage("APK files must be specified");
238 }
239 if (dex_locations_.empty()) {
240 Usage("DEX locations must be specified");
241 }
242 if (reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
243 Usage("Reference profile must be specified with --reference-profile-file or "
244 "--reference-profile-file-fd");
245 }
246 if (has_profiles) {
247 Usage("Profile must be specified with --reference-profile-file or "
248 "--reference-profile-file-fd");
249 }
250 return;
251 }
252 // --dump-only and --dump-classes may be specified with only --reference-profiles present.
253 if (!dump_only_ && !dump_classes_ && !has_profiles) {
Calin Juravle2e2db782016-02-23 12:00:03 +0000254 Usage("No profile files specified.");
255 }
256 if (!profile_files_.empty() && !profile_files_fd_.empty()) {
257 Usage("Profile files should not be specified with both --profile-file-fd and --profile-file");
258 }
David Sehr7c80f2d2017-02-07 16:47:58 -0800259 if (!dump_only_ && !dump_classes_ && !has_reference_profile) {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700260 Usage("No reference profile file specified.");
Calin Juravle2e2db782016-02-23 12:00:03 +0000261 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700262 if (!reference_profile_file_.empty() && FdIsValid(reference_profile_file_fd_)) {
263 Usage("Reference profile should not be specified with both "
264 "--reference-profile-file-fd and --reference-profile-file");
265 }
266 if ((!profile_files_.empty() && FdIsValid(reference_profile_file_fd_)) ||
267 (!dump_only_ && !profile_files_fd_.empty() && !FdIsValid(reference_profile_file_fd_))) {
268 Usage("Options --profile-file-fd and --reference-profile-file-fd "
269 "should only be used together");
Calin Juravle2e2db782016-02-23 12:00:03 +0000270 }
271 }
272
273 ProfileAssistant::ProcessingResult ProcessProfiles() {
274 ProfileAssistant::ProcessingResult result;
275 if (profile_files_.empty()) {
276 // The file doesn't need to be flushed here (ProcessProfiles will do it)
277 // so don't check the usage.
278 File file(reference_profile_file_fd_, false);
279 result = ProfileAssistant::ProcessProfiles(profile_files_fd_, reference_profile_file_fd_);
280 CloseAllFds(profile_files_fd_, "profile_files_fd_");
281 } else {
282 result = ProfileAssistant::ProcessProfiles(profile_files_, reference_profile_file_);
283 }
284 return result;
285 }
286
David Sehr7c80f2d2017-02-07 16:47:58 -0800287 void OpenApkFilesFromLocations(std::vector<std::unique_ptr<const DexFile>>* dex_files) {
288 bool use_apk_fd_list = !apks_fd_.empty();
289 if (use_apk_fd_list) {
290 CHECK(apk_files_.empty());
291 CHECK_EQ(dex_locations_.size(), apks_fd_.size());
292 } else {
293 CHECK_EQ(dex_locations_.size(), apk_files_.size());
294 CHECK(!apk_files_.empty());
295 }
296 static constexpr bool kVerifyChecksum = true;
297 for (size_t i = 0; i < dex_locations_.size(); ++i) {
298 std::string error_msg;
299 std::vector<std::unique_ptr<const DexFile>> dex_files_for_location;
300 if (use_apk_fd_list) {
301 if (DexFile::OpenZip(apks_fd_[i],
302 dex_locations_[i],
303 kVerifyChecksum,
304 &error_msg,
305 &dex_files_for_location)) {
306 } else {
307 LOG(WARNING) << "OpenZip failed for '" << dex_locations_[i] << "' " << error_msg;
308 continue;
309 }
310 } else {
311 if (DexFile::Open(apk_files_[i].c_str(),
312 dex_locations_[i],
313 kVerifyChecksum,
314 &error_msg,
315 &dex_files_for_location)) {
316 } else {
317 LOG(WARNING) << "Open failed for '" << dex_locations_[i] << "' " << error_msg;
318 continue;
319 }
320 }
321 for (std::unique_ptr<const DexFile>& dex_file : dex_files_for_location) {
322 dex_files->emplace_back(std::move(dex_file));
323 }
324 }
325 }
326
David Sehrb18991b2017-02-08 20:58:10 -0800327 int DumpOneProfile(const std::string& banner,
328 const std::string& filename,
329 int fd,
330 const std::vector<std::unique_ptr<const DexFile>>* dex_files,
331 std::string* dump) {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700332 if (!filename.empty()) {
333 fd = open(filename.c_str(), O_RDWR);
334 if (fd < 0) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800335 LOG(ERROR) << "Cannot open " << filename << strerror(errno);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700336 return -1;
337 }
Calin Juravle876f3502016-03-24 16:16:34 +0000338 }
339 ProfileCompilationInfo info;
340 if (!info.Load(fd)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800341 LOG(ERROR) << "Cannot load profile info from fd=" << fd << "\n";
Calin Juravle876f3502016-03-24 16:16:34 +0000342 return -1;
343 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700344 std::string this_dump = banner + "\n" + info.DumpInfo(dex_files) + "\n";
345 *dump += this_dump;
346 if (close(fd) < 0) {
347 PLOG(WARNING) << "Failed to close descriptor";
348 }
349 return 0;
350 }
351
352 int DumpProfileInfo() {
353 static const char* kEmptyString = "";
David Sehr546d24f2016-06-02 10:46:19 -0700354 static const char* kOrdinaryProfile = "=== profile ===";
355 static const char* kReferenceProfile = "=== reference profile ===";
356
357 // Open apk/zip files and and read dex files.
358 MemMap::Init(); // for ZipArchive::OpenFromFd
David Sehrb18991b2017-02-08 20:58:10 -0800359 std::vector<std::unique_ptr<const DexFile>> dex_files;
David Sehr7c80f2d2017-02-07 16:47:58 -0800360 OpenApkFilesFromLocations(&dex_files);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700361 std::string dump;
David Sehr4fcdd6d2016-05-24 14:52:31 -0700362 // Dump individual profile files.
363 if (!profile_files_fd_.empty()) {
364 for (int profile_file_fd : profile_files_fd_) {
David Sehr546d24f2016-06-02 10:46:19 -0700365 int ret = DumpOneProfile(kOrdinaryProfile,
366 kEmptyString,
367 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 }
375 if (!profile_files_.empty()) {
376 for (const std::string& profile_file : profile_files_) {
David Sehr546d24f2016-06-02 10:46:19 -0700377 int ret = DumpOneProfile(kOrdinaryProfile, profile_file, kInvalidFd, &dex_files, &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700378 if (ret != 0) {
379 return ret;
380 }
381 }
382 }
383 // Dump reference profile file.
384 if (FdIsValid(reference_profile_file_fd_)) {
David Sehr546d24f2016-06-02 10:46:19 -0700385 int ret = DumpOneProfile(kReferenceProfile,
386 kEmptyString,
387 reference_profile_file_fd_,
388 &dex_files,
389 &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700390 if (ret != 0) {
391 return ret;
392 }
393 }
394 if (!reference_profile_file_.empty()) {
David Sehr546d24f2016-06-02 10:46:19 -0700395 int ret = DumpOneProfile(kReferenceProfile,
396 reference_profile_file_,
397 kInvalidFd,
398 &dex_files,
David Sehr4fcdd6d2016-05-24 14:52:31 -0700399 &dump);
400 if (ret != 0) {
401 return ret;
402 }
403 }
404 if (!FdIsValid(dump_output_to_fd_)) {
405 std::cout << dump;
406 } else {
407 unix_file::FdFile out_fd(dump_output_to_fd_, false /*check_usage*/);
408 if (!out_fd.WriteFully(dump.c_str(), dump.length())) {
409 return -1;
410 }
411 }
Calin Juravle876f3502016-03-24 16:16:34 +0000412 return 0;
413 }
414
415 bool ShouldOnlyDumpProfile() {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700416 return dump_only_;
Calin Juravle876f3502016-03-24 16:16:34 +0000417 }
418
David Sehr7c80f2d2017-02-07 16:47:58 -0800419 bool GetClassNames(int fd,
420 std::vector<std::unique_ptr<const DexFile>>* dex_files,
421 std::set<std::string>* class_names) {
422 ProfileCompilationInfo profile_info;
423 if (!profile_info.Load(fd)) {
424 LOG(ERROR) << "Cannot load profile info";
425 return false;
426 }
427 profile_info.GetClassNames(dex_files, class_names);
428 return true;
429 }
430
431 bool GetClassNames(std::string profile_file,
432 std::vector<std::unique_ptr<const DexFile>>* dex_files,
433 std::set<std::string>* class_names) {
434 int fd = open(profile_file.c_str(), O_RDONLY);
435 if (!FdIsValid(fd)) {
436 LOG(ERROR) << "Cannot open " << profile_file << strerror(errno);
437 return false;
438 }
439 if (!GetClassNames(fd, dex_files, class_names)) {
440 return false;
441 }
442 if (close(fd) < 0) {
443 PLOG(WARNING) << "Failed to close descriptor";
444 }
445 return true;
446 }
447
448 int DumpClasses() {
449 // Open apk/zip files and and read dex files.
450 MemMap::Init(); // for ZipArchive::OpenFromFd
451 // Open the dex files to get the names for classes.
452 std::vector<std::unique_ptr<const DexFile>> dex_files;
453 OpenApkFilesFromLocations(&dex_files);
454 // Build a vector of class names from individual profile files.
455 std::set<std::string> class_names;
456 if (!profile_files_fd_.empty()) {
457 for (int profile_file_fd : profile_files_fd_) {
458 if (!GetClassNames(profile_file_fd, &dex_files, &class_names)) {
459 return -1;
460 }
461 }
462 }
463 if (!profile_files_.empty()) {
464 for (const std::string& profile_file : profile_files_) {
465 if (!GetClassNames(profile_file, &dex_files, &class_names)) {
466 return -1;
467 }
468 }
469 }
470 // Concatenate class names from reference profile file.
471 if (FdIsValid(reference_profile_file_fd_)) {
472 if (!GetClassNames(reference_profile_file_fd_, &dex_files, &class_names)) {
473 return -1;
474 }
475 }
476 if (!reference_profile_file_.empty()) {
477 if (!GetClassNames(reference_profile_file_, &dex_files, &class_names)) {
478 return -1;
479 }
480 }
481 // Dump the class names.
482 std::string dump;
483 for (const std::string& class_name : class_names) {
484 dump += class_name + std::string("\n");
485 }
486 if (!FdIsValid(dump_output_to_fd_)) {
487 std::cout << dump;
488 } else {
489 unix_file::FdFile out_fd(dump_output_to_fd_, false /*check_usage*/);
490 if (!out_fd.WriteFully(dump.c_str(), dump.length())) {
491 return -1;
492 }
493 }
494 return 0;
495 }
496
497 bool ShouldOnlyDumpClasses() {
498 return dump_classes_;
499 }
500
501 // Read lines from the given file, dropping comments and empty lines. Post-process each line with
502 // the given function.
503 template <typename T>
504 static T* ReadCommentedInputFromFile(
505 const char* input_filename, std::function<std::string(const char*)>* process) {
506 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
507 if (input_file.get() == nullptr) {
508 LOG(ERROR) << "Failed to open input file " << input_filename;
509 return nullptr;
510 }
511 std::unique_ptr<T> result(
512 ReadCommentedInputStream<T>(*input_file, process));
513 input_file->close();
514 return result.release();
515 }
516
517 // Read lines from the given stream, dropping comments and empty lines. Post-process each line
518 // with the given function.
519 template <typename T>
520 static T* ReadCommentedInputStream(
521 std::istream& in_stream,
522 std::function<std::string(const char*)>* process) {
523 std::unique_ptr<T> output(new T());
524 while (in_stream.good()) {
525 std::string dot;
526 std::getline(in_stream, dot);
527 if (android::base::StartsWith(dot, "#") || dot.empty()) {
528 continue;
529 }
530 if (process != nullptr) {
531 std::string descriptor((*process)(dot.c_str()));
532 output->insert(output->end(), descriptor);
533 } else {
534 output->insert(output->end(), dot);
535 }
536 }
537 return output.release();
538 }
539
540 int CreateProfile() {
541 MemMap::Init(); // for ZipArchive::OpenFromFd
542 // Open the profile output file if needed.
543 int fd = reference_profile_file_fd_;
544 if (!FdIsValid(fd)) {
545 CHECK(!reference_profile_file_.empty());
546 fd = open(reference_profile_file_.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
547 if (fd < 0) {
548 LOG(ERROR) << "Cannot open " << reference_profile_file_ << strerror(errno);
549 return -1;
550 }
551 }
552 // Read the user-specified list of classes (dot notation rather than descriptors).
553 std::unique_ptr<std::unordered_set<std::string>>
554 user_class_list(ReadCommentedInputFromFile<std::unordered_set<std::string>>(
555 create_profile_from_file_.c_str(), nullptr)); // No post-processing.
556 std::unordered_set<std::string> matched_user_classes;
557 // Open the dex files to look up class names.
558 std::vector<std::unique_ptr<const DexFile>> dex_files;
559 OpenApkFilesFromLocations(&dex_files);
560 // Iterate over the dex files looking for class names in the input stream.
561 std::set<DexCacheResolvedClasses> resolved_class_set;
562 for (auto& dex_file : dex_files) {
563 // Compute the set of classes to be added for this dex file first. This
564 // avoids creating an entry in the profile information for dex files that
565 // contribute no classes.
566 std::unordered_set<dex::TypeIndex> classes_to_be_added;
567 for (const auto& klass : *user_class_list) {
568 std::string descriptor = DotToDescriptor(klass.c_str());
569 const DexFile::TypeId* type_id = dex_file->FindTypeId(descriptor.c_str());
570 if (type_id == nullptr) {
571 continue;
572 }
573 classes_to_be_added.insert(dex_file->GetIndexForTypeId(*type_id));
574 matched_user_classes.insert(klass);
575 }
576 if (classes_to_be_added.empty()) {
577 continue;
578 }
579 // Insert the DexCacheResolved Classes into the set expected for
580 // AddMethodsAndClasses.
581 std::set<DexCacheResolvedClasses>::iterator dex_resolved_classes =
582 resolved_class_set.emplace(dex_file->GetLocation(),
583 dex_file->GetBaseLocation(),
584 dex_file->GetLocationChecksum()).first;
585 dex_resolved_classes->AddClasses(classes_to_be_added.begin(), classes_to_be_added.end());
586 }
587 // Warn the user if we didn't find matches for every class.
588 for (const auto& klass : *user_class_list) {
589 if (matched_user_classes.find(klass) == matched_user_classes.end()) {
590 LOG(WARNING) << "requested class '" << klass << "' was not matched in any dex file";
591 }
592 }
593 // Generate the profile data structure.
594 ProfileCompilationInfo info;
595 std::vector<MethodReference> methods; // No methods for now.
596 info.AddMethodsAndClasses(methods, resolved_class_set);
597 // Write the profile file.
598 CHECK(info.Save(fd));
599 if (close(fd) < 0) {
600 PLOG(WARNING) << "Failed to close descriptor";
601 }
602 return 0;
603 }
604
605 bool ShouldCreateProfile() {
606 return !create_profile_from_file_.empty();
607 }
608
Calin Juravle7bcdb532016-06-07 16:14:47 +0100609 int GenerateTestProfile() {
George Burgess IV71f77262016-10-25 13:08:17 -0700610 int profile_test_fd = open(test_profile_.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
Calin Juravle7bcdb532016-06-07 16:14:47 +0100611 if (profile_test_fd < 0) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800612 LOG(ERROR) << "Cannot open " << test_profile_ << strerror(errno);
Calin Juravle7bcdb532016-06-07 16:14:47 +0100613 return -1;
614 }
615
616 bool result = ProfileCompilationInfo::GenerateTestProfile(profile_test_fd,
David Sehr7c80f2d2017-02-07 16:47:58 -0800617 test_profile_num_dex_,
618 test_profile_method_ratio_,
619 test_profile_class_ratio_);
Calin Juravle7bcdb532016-06-07 16:14:47 +0100620 close(profile_test_fd); // ignore close result.
621 return result ? 0 : -1;
622 }
623
624 bool ShouldGenerateTestProfile() {
625 return !test_profile_.empty();
626 }
627
Calin Juravle2e2db782016-02-23 12:00:03 +0000628 private:
629 static void ParseFdForCollection(const StringPiece& option,
630 const char* arg_name,
631 std::vector<int>* fds) {
632 int fd;
633 ParseUintOption(option, arg_name, &fd, Usage);
634 fds->push_back(fd);
635 }
636
637 static void CloseAllFds(const std::vector<int>& fds, const char* descriptor) {
638 for (size_t i = 0; i < fds.size(); i++) {
639 if (close(fds[i]) < 0) {
640 PLOG(WARNING) << "Failed to close descriptor for " << descriptor << " at index " << i;
641 }
642 }
643 }
644
645 void LogCompletionTime() {
David Sehr52683cf2016-05-05 09:02:38 -0700646 static constexpr uint64_t kLogThresholdTime = MsToNs(100); // 100ms
647 uint64_t time_taken = NanoTime() - start_ns_;
David Sehred793012016-05-05 13:39:43 -0700648 if (time_taken > kLogThresholdTime) {
David Sehrd8557182016-05-06 12:29:35 -0700649 LOG(WARNING) << "profman took " << PrettyDuration(time_taken);
David Sehred793012016-05-05 13:39:43 -0700650 }
Calin Juravle2e2db782016-02-23 12:00:03 +0000651 }
652
653 std::vector<std::string> profile_files_;
654 std::vector<int> profile_files_fd_;
David Sehr546d24f2016-06-02 10:46:19 -0700655 std::vector<std::string> dex_locations_;
David Sehr7c80f2d2017-02-07 16:47:58 -0800656 std::vector<std::string> apk_files_;
David Sehr546d24f2016-06-02 10:46:19 -0700657 std::vector<int> apks_fd_;
Calin Juravle2e2db782016-02-23 12:00:03 +0000658 std::string reference_profile_file_;
659 int reference_profile_file_fd_;
David Sehr4fcdd6d2016-05-24 14:52:31 -0700660 bool dump_only_;
David Sehr7c80f2d2017-02-07 16:47:58 -0800661 bool dump_classes_;
David Sehr4fcdd6d2016-05-24 14:52:31 -0700662 int dump_output_to_fd_;
Calin Juravle7bcdb532016-06-07 16:14:47 +0100663 std::string test_profile_;
David Sehr7c80f2d2017-02-07 16:47:58 -0800664 std::string create_profile_from_file_;
Calin Juravle7bcdb532016-06-07 16:14:47 +0100665 uint16_t test_profile_num_dex_;
666 uint16_t test_profile_method_ratio_;
667 uint16_t test_profile_class_ratio_;
Calin Juravle2e2db782016-02-23 12:00:03 +0000668 uint64_t start_ns_;
669};
670
671// See ProfileAssistant::ProcessingResult for return codes.
672static int profman(int argc, char** argv) {
673 ProfMan profman;
674
675 // Parse arguments. Argument mistakes will lead to exit(EXIT_FAILURE) in UsageError.
676 profman.ParseArgs(argc, argv);
677
Calin Juravle7bcdb532016-06-07 16:14:47 +0100678 if (profman.ShouldGenerateTestProfile()) {
679 return profman.GenerateTestProfile();
680 }
Calin Juravle876f3502016-03-24 16:16:34 +0000681 if (profman.ShouldOnlyDumpProfile()) {
682 return profman.DumpProfileInfo();
683 }
David Sehr7c80f2d2017-02-07 16:47:58 -0800684 if (profman.ShouldOnlyDumpClasses()) {
685 return profman.DumpClasses();
686 }
687 if (profman.ShouldCreateProfile()) {
688 return profman.CreateProfile();
689 }
Calin Juravle2e2db782016-02-23 12:00:03 +0000690 // Process profile information and assess if we need to do a profile guided compilation.
691 // This operation involves I/O.
692 return profman.ProcessProfiles();
693}
694
695} // namespace art
696
697int main(int argc, char **argv) {
698 return art::profman(argc, argv);
699}
700