blob: 0b2d172726cfef674076d6e3936a8cd1dfbe0c48 [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
Calin Juravle876f3502016-03-24 16:16:34 +000024#include <iostream>
Calin Juravle2e2db782016-02-23 12:00:03 +000025#include <string>
26#include <vector>
27
Andreas Gampe9186ced2016-12-12 14:28:21 -080028#include "android-base/strings.h"
29
Calin Juravle2e2db782016-02-23 12:00:03 +000030#include "base/dumpable.h"
31#include "base/scoped_flock.h"
32#include "base/stringpiece.h"
33#include "base/stringprintf.h"
34#include "base/time_utils.h"
35#include "base/unix_file/fd_file.h"
David Sehr4fcdd6d2016-05-24 14:52:31 -070036#include "dex_file.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000037#include "jit/offline_profiling_info.h"
David Sehrf57589f2016-10-17 10:09:33 -070038#include "runtime.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000039#include "utils.h"
David Sehr546d24f2016-06-02 10:46:19 -070040#include "zip_archive.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000041#include "profile_assistant.h"
42
43namespace art {
44
45static int original_argc;
46static char** original_argv;
47
48static std::string CommandLine() {
49 std::vector<std::string> command;
50 for (int i = 0; i < original_argc; ++i) {
51 command.push_back(original_argv[i]);
52 }
Andreas Gampe9186ced2016-12-12 14:28:21 -080053 return android::base::Join(command, ' ');
Calin Juravle2e2db782016-02-23 12:00:03 +000054}
55
David Sehr4fcdd6d2016-05-24 14:52:31 -070056static constexpr int kInvalidFd = -1;
57
58static bool FdIsValid(int fd) {
59 return fd != kInvalidFd;
60}
61
Calin Juravle2e2db782016-02-23 12:00:03 +000062static void UsageErrorV(const char* fmt, va_list ap) {
63 std::string error;
64 StringAppendV(&error, fmt, ap);
65 LOG(ERROR) << error;
66}
67
68static void UsageError(const char* fmt, ...) {
69 va_list ap;
70 va_start(ap, fmt);
71 UsageErrorV(fmt, ap);
72 va_end(ap);
73}
74
75NO_RETURN static void Usage(const char *fmt, ...) {
76 va_list ap;
77 va_start(ap, fmt);
78 UsageErrorV(fmt, ap);
79 va_end(ap);
80
81 UsageError("Command: %s", CommandLine().c_str());
82 UsageError("Usage: profman [options]...");
83 UsageError("");
David Sehr4fcdd6d2016-05-24 14:52:31 -070084 UsageError(" --dump-only: dumps the content of the specified profile files");
85 UsageError(" to standard output (default) in a human readable form.");
86 UsageError("");
87 UsageError(" --dump-output-to-fd=<number>: redirects --dump-info-for output to a file");
88 UsageError(" descriptor.");
Calin Juravle876f3502016-03-24 16:16:34 +000089 UsageError("");
Calin Juravle2e2db782016-02-23 12:00:03 +000090 UsageError(" --profile-file=<filename>: specify profiler output file to use for compilation.");
91 UsageError(" Can be specified multiple time, in which case the data from the different");
92 UsageError(" profiles will be aggregated.");
93 UsageError("");
94 UsageError(" --profile-file-fd=<number>: same as --profile-file but accepts a file descriptor.");
95 UsageError(" Cannot be used together with --profile-file.");
96 UsageError("");
97 UsageError(" --reference-profile-file=<filename>: specify a reference profile.");
98 UsageError(" The data in this file will be compared with the data obtained by merging");
99 UsageError(" all the files specified with --profile-file or --profile-file-fd.");
100 UsageError(" If the exit code is EXIT_COMPILE then all --profile-file will be merged into");
101 UsageError(" --reference-profile-file. ");
102 UsageError("");
103 UsageError(" --reference-profile-file-fd=<number>: same as --reference-profile-file but");
104 UsageError(" accepts a file descriptor. Cannot be used together with");
105 UsageError(" --reference-profile-file.");
Calin Juravle7bcdb532016-06-07 16:14:47 +0100106 UsageError(" --generate-test-profile=<filename>: generates a random profile file for testing.");
107 UsageError(" --generate-test-profile-num-dex=<number>: number of dex files that should be");
108 UsageError(" included in the generated profile. Defaults to 20.");
109 UsageError(" --generate-test-profile-method-ratio=<number>: the percentage from the maximum");
110 UsageError(" number of methods that should be generated. Defaults to 5.");
111 UsageError(" --generate-test-profile-class-ratio=<number>: the percentage from the maximum");
112 UsageError(" number of classes that should be generated. Defaults to 5.");
113 UsageError("");
Calin Juravle2e2db782016-02-23 12:00:03 +0000114 UsageError("");
David Sehr546d24f2016-06-02 10:46:19 -0700115 UsageError(" --dex-location=<string>: location string to use with corresponding");
116 UsageError(" apk-fd to find dex files");
David Sehr4fcdd6d2016-05-24 14:52:31 -0700117 UsageError("");
David Sehr546d24f2016-06-02 10:46:19 -0700118 UsageError(" --apk-fd=<number>: file descriptor containing an open APK to");
David Sehr4fcdd6d2016-05-24 14:52:31 -0700119 UsageError(" search for dex files");
120 UsageError("");
Calin Juravle2e2db782016-02-23 12:00:03 +0000121
122 exit(EXIT_FAILURE);
123}
124
Calin Juravle7bcdb532016-06-07 16:14:47 +0100125// Note: make sure you update the Usage if you change these values.
126static constexpr uint16_t kDefaultTestProfileNumDex = 20;
127static constexpr uint16_t kDefaultTestProfileMethodRatio = 5;
128static constexpr uint16_t kDefaultTestProfileClassRatio = 5;
129
Calin Juravle2e2db782016-02-23 12:00:03 +0000130class ProfMan FINAL {
131 public:
132 ProfMan() :
David Sehr4fcdd6d2016-05-24 14:52:31 -0700133 reference_profile_file_fd_(kInvalidFd),
134 dump_only_(false),
135 dump_output_to_fd_(kInvalidFd),
Calin Juravle7bcdb532016-06-07 16:14:47 +0100136 test_profile_num_dex_(kDefaultTestProfileNumDex),
137 test_profile_method_ratio_(kDefaultTestProfileMethodRatio),
138 test_profile_class_ratio_(kDefaultTestProfileClassRatio),
Calin Juravle2e2db782016-02-23 12:00:03 +0000139 start_ns_(NanoTime()) {}
140
141 ~ProfMan() {
142 LogCompletionTime();
143 }
144
145 void ParseArgs(int argc, char **argv) {
146 original_argc = argc;
147 original_argv = argv;
148
David Sehrf57589f2016-10-17 10:09:33 -0700149 InitLogging(argv, Runtime::Aborter);
Calin Juravle2e2db782016-02-23 12:00:03 +0000150
151 // Skip over the command name.
152 argv++;
153 argc--;
154
155 if (argc == 0) {
156 Usage("No arguments specified");
157 }
158
159 for (int i = 0; i < argc; ++i) {
160 const StringPiece option(argv[i]);
161 const bool log_options = false;
162 if (log_options) {
Calin Juravle876f3502016-03-24 16:16:34 +0000163 LOG(INFO) << "profman: option[" << i << "]=" << argv[i];
Calin Juravle2e2db782016-02-23 12:00:03 +0000164 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700165 if (option == "--dump-only") {
166 dump_only_ = true;
167 } else if (option.starts_with("--dump-output-to-fd=")) {
168 ParseUintOption(option, "--dump-output-to-fd", &dump_output_to_fd_, Usage);
Calin Juravle876f3502016-03-24 16:16:34 +0000169 } else if (option.starts_with("--profile-file=")) {
Calin Juravle2e2db782016-02-23 12:00:03 +0000170 profile_files_.push_back(option.substr(strlen("--profile-file=")).ToString());
171 } else if (option.starts_with("--profile-file-fd=")) {
172 ParseFdForCollection(option, "--profile-file-fd", &profile_files_fd_);
173 } else if (option.starts_with("--reference-profile-file=")) {
174 reference_profile_file_ = option.substr(strlen("--reference-profile-file=")).ToString();
175 } else if (option.starts_with("--reference-profile-file-fd=")) {
176 ParseUintOption(option, "--reference-profile-file-fd", &reference_profile_file_fd_, Usage);
David Sehr546d24f2016-06-02 10:46:19 -0700177 } else if (option.starts_with("--dex-location=")) {
178 dex_locations_.push_back(option.substr(strlen("--dex-location=")).ToString());
179 } else if (option.starts_with("--apk-fd=")) {
180 ParseFdForCollection(option, "--apk-fd", &apks_fd_);
Calin Juravle7bcdb532016-06-07 16:14:47 +0100181 } else if (option.starts_with("--generate-test-profile=")) {
182 test_profile_ = option.substr(strlen("--generate-test-profile=")).ToString();
183 } else if (option.starts_with("--generate-test-profile-num-dex=")) {
184 ParseUintOption(option,
185 "--generate-test-profile-num-dex",
186 &test_profile_num_dex_,
187 Usage);
188 } else if (option.starts_with("--generate-test-profile-method-ratio")) {
189 ParseUintOption(option,
190 "--generate-test-profile-method-ratio",
191 &test_profile_method_ratio_,
192 Usage);
193 } else if (option.starts_with("--generate-test-profile-class-ratio")) {
194 ParseUintOption(option,
195 "--generate-test-profile-class-ratio",
196 &test_profile_class_ratio_,
197 Usage);
Calin Juravle2e2db782016-02-23 12:00:03 +0000198 } else {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700199 Usage("Unknown argument '%s'", option.data());
Calin Juravle2e2db782016-02-23 12:00:03 +0000200 }
201 }
202
Calin Juravle876f3502016-03-24 16:16:34 +0000203 bool has_profiles = !profile_files_.empty() || !profile_files_fd_.empty();
204 bool has_reference_profile = !reference_profile_file_.empty() ||
David Sehr4fcdd6d2016-05-24 14:52:31 -0700205 FdIsValid(reference_profile_file_fd_);
Calin Juravle876f3502016-03-24 16:16:34 +0000206
Calin Juravle7bcdb532016-06-07 16:14:47 +0100207 if (!test_profile_.empty()) {
208 if (test_profile_method_ratio_ > 100) {
209 Usage("Invalid ratio for --generate-test-profile-method-ratio");
210 }
211 if (test_profile_class_ratio_ > 100) {
212 Usage("Invalid ratio for --generate-test-profile-class-ratio");
213 }
214 return;
215 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700216 // --dump-only may be specified with only --reference-profiles present.
217 if (!dump_only_ && !has_profiles) {
Calin Juravle2e2db782016-02-23 12:00:03 +0000218 Usage("No profile files specified.");
219 }
220 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 (!dump_only_ && !has_reference_profile) {
224 Usage("No reference profile file specified.");
Calin Juravle2e2db782016-02-23 12:00:03 +0000225 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700226 if (!reference_profile_file_.empty() && FdIsValid(reference_profile_file_fd_)) {
227 Usage("Reference profile should not be specified with both "
228 "--reference-profile-file-fd and --reference-profile-file");
229 }
230 if ((!profile_files_.empty() && FdIsValid(reference_profile_file_fd_)) ||
231 (!dump_only_ && !profile_files_fd_.empty() && !FdIsValid(reference_profile_file_fd_))) {
232 Usage("Options --profile-file-fd and --reference-profile-file-fd "
233 "should only be used together");
Calin Juravle2e2db782016-02-23 12:00:03 +0000234 }
235 }
236
237 ProfileAssistant::ProcessingResult ProcessProfiles() {
238 ProfileAssistant::ProcessingResult result;
239 if (profile_files_.empty()) {
240 // The file doesn't need to be flushed here (ProcessProfiles will do it)
241 // so don't check the usage.
242 File file(reference_profile_file_fd_, false);
243 result = ProfileAssistant::ProcessProfiles(profile_files_fd_, reference_profile_file_fd_);
244 CloseAllFds(profile_files_fd_, "profile_files_fd_");
245 } else {
246 result = ProfileAssistant::ProcessProfiles(profile_files_, reference_profile_file_);
247 }
248 return result;
249 }
250
David Sehr4fcdd6d2016-05-24 14:52:31 -0700251 int DumpOneProfile(const std::string& banner, const std::string& filename, int fd,
252 const std::vector<const DexFile*>* dex_files, std::string* dump) {
253 if (!filename.empty()) {
254 fd = open(filename.c_str(), O_RDWR);
255 if (fd < 0) {
256 std::cerr << "Cannot open " << filename << strerror(errno);
257 return -1;
258 }
Calin Juravle876f3502016-03-24 16:16:34 +0000259 }
260 ProfileCompilationInfo info;
261 if (!info.Load(fd)) {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700262 std::cerr << "Cannot load profile info from fd=" << fd << "\n";
Calin Juravle876f3502016-03-24 16:16:34 +0000263 return -1;
264 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700265 std::string this_dump = banner + "\n" + info.DumpInfo(dex_files) + "\n";
266 *dump += this_dump;
267 if (close(fd) < 0) {
268 PLOG(WARNING) << "Failed to close descriptor";
269 }
270 return 0;
271 }
272
273 int DumpProfileInfo() {
274 static const char* kEmptyString = "";
David Sehr546d24f2016-06-02 10:46:19 -0700275 static const char* kOrdinaryProfile = "=== profile ===";
276 static const char* kReferenceProfile = "=== reference profile ===";
277
278 // Open apk/zip files and and read dex files.
279 MemMap::Init(); // for ZipArchive::OpenFromFd
280 std::vector<const DexFile*> dex_files;
281 assert(dex_locations_.size() == apks_fd_.size());
Aart Bik37d6a3b2016-06-21 18:30:10 -0700282 static constexpr bool kVerifyChecksum = true;
David Sehr546d24f2016-06-02 10:46:19 -0700283 for (size_t i = 0; i < dex_locations_.size(); ++i) {
284 std::string error_msg;
285 std::vector<std::unique_ptr<const DexFile>> dex_files_for_location;
David Sehr733ddb22016-09-19 15:02:18 -0700286 if (DexFile::OpenZip(apks_fd_[i],
287 dex_locations_[i],
288 kVerifyChecksum,
289 &error_msg,
290 &dex_files_for_location)) {
David Sehr546d24f2016-06-02 10:46:19 -0700291 } else {
292 LOG(WARNING) << "OpenFromZip failed for '" << dex_locations_[i] << "' " << error_msg;
293 continue;
294 }
295 for (std::unique_ptr<const DexFile>& dex_file : dex_files_for_location) {
296 dex_files.push_back(dex_file.release());
297 }
298 }
299
David Sehr4fcdd6d2016-05-24 14:52:31 -0700300 std::string dump;
David Sehr4fcdd6d2016-05-24 14:52:31 -0700301 // Dump individual profile files.
302 if (!profile_files_fd_.empty()) {
303 for (int profile_file_fd : profile_files_fd_) {
David Sehr546d24f2016-06-02 10:46:19 -0700304 int ret = DumpOneProfile(kOrdinaryProfile,
305 kEmptyString,
306 profile_file_fd,
307 &dex_files,
308 &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700309 if (ret != 0) {
310 return ret;
311 }
312 }
313 }
314 if (!profile_files_.empty()) {
315 for (const std::string& profile_file : profile_files_) {
David Sehr546d24f2016-06-02 10:46:19 -0700316 int ret = DumpOneProfile(kOrdinaryProfile, profile_file, kInvalidFd, &dex_files, &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700317 if (ret != 0) {
318 return ret;
319 }
320 }
321 }
322 // Dump reference profile file.
323 if (FdIsValid(reference_profile_file_fd_)) {
David Sehr546d24f2016-06-02 10:46:19 -0700324 int ret = DumpOneProfile(kReferenceProfile,
325 kEmptyString,
326 reference_profile_file_fd_,
327 &dex_files,
328 &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700329 if (ret != 0) {
330 return ret;
331 }
332 }
333 if (!reference_profile_file_.empty()) {
David Sehr546d24f2016-06-02 10:46:19 -0700334 int ret = DumpOneProfile(kReferenceProfile,
335 reference_profile_file_,
336 kInvalidFd,
337 &dex_files,
David Sehr4fcdd6d2016-05-24 14:52:31 -0700338 &dump);
339 if (ret != 0) {
340 return ret;
341 }
342 }
343 if (!FdIsValid(dump_output_to_fd_)) {
344 std::cout << dump;
345 } else {
346 unix_file::FdFile out_fd(dump_output_to_fd_, false /*check_usage*/);
347 if (!out_fd.WriteFully(dump.c_str(), dump.length())) {
348 return -1;
349 }
350 }
Calin Juravle876f3502016-03-24 16:16:34 +0000351 return 0;
352 }
353
354 bool ShouldOnlyDumpProfile() {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700355 return dump_only_;
Calin Juravle876f3502016-03-24 16:16:34 +0000356 }
357
Calin Juravle7bcdb532016-06-07 16:14:47 +0100358 int GenerateTestProfile() {
George Burgess IV71f77262016-10-25 13:08:17 -0700359 int profile_test_fd = open(test_profile_.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
Calin Juravle7bcdb532016-06-07 16:14:47 +0100360 if (profile_test_fd < 0) {
361 std::cerr << "Cannot open " << test_profile_ << strerror(errno);
362 return -1;
363 }
364
365 bool result = ProfileCompilationInfo::GenerateTestProfile(profile_test_fd,
366 test_profile_num_dex_,
367 test_profile_method_ratio_,
368 test_profile_class_ratio_);
369 close(profile_test_fd); // ignore close result.
370 return result ? 0 : -1;
371 }
372
373 bool ShouldGenerateTestProfile() {
374 return !test_profile_.empty();
375 }
376
Calin Juravle2e2db782016-02-23 12:00:03 +0000377 private:
378 static void ParseFdForCollection(const StringPiece& option,
379 const char* arg_name,
380 std::vector<int>* fds) {
381 int fd;
382 ParseUintOption(option, arg_name, &fd, Usage);
383 fds->push_back(fd);
384 }
385
386 static void CloseAllFds(const std::vector<int>& fds, const char* descriptor) {
387 for (size_t i = 0; i < fds.size(); i++) {
388 if (close(fds[i]) < 0) {
389 PLOG(WARNING) << "Failed to close descriptor for " << descriptor << " at index " << i;
390 }
391 }
392 }
393
394 void LogCompletionTime() {
David Sehr52683cf2016-05-05 09:02:38 -0700395 static constexpr uint64_t kLogThresholdTime = MsToNs(100); // 100ms
396 uint64_t time_taken = NanoTime() - start_ns_;
David Sehred793012016-05-05 13:39:43 -0700397 if (time_taken > kLogThresholdTime) {
David Sehrd8557182016-05-06 12:29:35 -0700398 LOG(WARNING) << "profman took " << PrettyDuration(time_taken);
David Sehred793012016-05-05 13:39:43 -0700399 }
Calin Juravle2e2db782016-02-23 12:00:03 +0000400 }
401
402 std::vector<std::string> profile_files_;
403 std::vector<int> profile_files_fd_;
David Sehr546d24f2016-06-02 10:46:19 -0700404 std::vector<std::string> dex_locations_;
405 std::vector<int> apks_fd_;
Calin Juravle2e2db782016-02-23 12:00:03 +0000406 std::string reference_profile_file_;
407 int reference_profile_file_fd_;
David Sehr4fcdd6d2016-05-24 14:52:31 -0700408 bool dump_only_;
409 int dump_output_to_fd_;
Calin Juravle7bcdb532016-06-07 16:14:47 +0100410 std::string test_profile_;
411 uint16_t test_profile_num_dex_;
412 uint16_t test_profile_method_ratio_;
413 uint16_t test_profile_class_ratio_;
Calin Juravle2e2db782016-02-23 12:00:03 +0000414 uint64_t start_ns_;
415};
416
417// See ProfileAssistant::ProcessingResult for return codes.
418static int profman(int argc, char** argv) {
419 ProfMan profman;
420
421 // Parse arguments. Argument mistakes will lead to exit(EXIT_FAILURE) in UsageError.
422 profman.ParseArgs(argc, argv);
423
Calin Juravle7bcdb532016-06-07 16:14:47 +0100424 if (profman.ShouldGenerateTestProfile()) {
425 return profman.GenerateTestProfile();
426 }
Calin Juravle876f3502016-03-24 16:16:34 +0000427 if (profman.ShouldOnlyDumpProfile()) {
428 return profman.DumpProfileInfo();
429 }
Calin Juravle2e2db782016-02-23 12:00:03 +0000430 // Process profile information and assess if we need to do a profile guided compilation.
431 // This operation involves I/O.
432 return profman.ProcessProfiles();
433}
434
435} // namespace art
436
437int main(int argc, char **argv) {
438 return art::profman(argc, argv);
439}
440