blob: bfef834bd93f57bb46561881c50f96ded88bd758 [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
28#include "base/dumpable.h"
29#include "base/scoped_flock.h"
30#include "base/stringpiece.h"
31#include "base/stringprintf.h"
32#include "base/time_utils.h"
33#include "base/unix_file/fd_file.h"
David Sehr4fcdd6d2016-05-24 14:52:31 -070034#include "dex_file.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000035#include "jit/offline_profiling_info.h"
David Sehrf57589f2016-10-17 10:09:33 -070036#include "runtime.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000037#include "utils.h"
David Sehr546d24f2016-06-02 10:46:19 -070038#include "zip_archive.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000039#include "profile_assistant.h"
40
41namespace art {
42
43static int original_argc;
44static char** original_argv;
45
46static std::string CommandLine() {
47 std::vector<std::string> command;
48 for (int i = 0; i < original_argc; ++i) {
49 command.push_back(original_argv[i]);
50 }
51 return Join(command, ' ');
52}
53
David Sehr4fcdd6d2016-05-24 14:52:31 -070054static constexpr int kInvalidFd = -1;
55
56static bool FdIsValid(int fd) {
57 return fd != kInvalidFd;
58}
59
Calin Juravle2e2db782016-02-23 12:00:03 +000060static void UsageErrorV(const char* fmt, va_list ap) {
61 std::string error;
62 StringAppendV(&error, fmt, ap);
63 LOG(ERROR) << error;
64}
65
66static void UsageError(const char* fmt, ...) {
67 va_list ap;
68 va_start(ap, fmt);
69 UsageErrorV(fmt, ap);
70 va_end(ap);
71}
72
73NO_RETURN static void Usage(const char *fmt, ...) {
74 va_list ap;
75 va_start(ap, fmt);
76 UsageErrorV(fmt, ap);
77 va_end(ap);
78
79 UsageError("Command: %s", CommandLine().c_str());
80 UsageError("Usage: profman [options]...");
81 UsageError("");
David Sehr4fcdd6d2016-05-24 14:52:31 -070082 UsageError(" --dump-only: dumps the content of the specified profile files");
83 UsageError(" to standard output (default) in a human readable form.");
84 UsageError("");
85 UsageError(" --dump-output-to-fd=<number>: redirects --dump-info-for output to a file");
86 UsageError(" descriptor.");
Calin Juravle876f3502016-03-24 16:16:34 +000087 UsageError("");
Calin Juravle2e2db782016-02-23 12:00:03 +000088 UsageError(" --profile-file=<filename>: specify profiler output file to use for compilation.");
89 UsageError(" Can be specified multiple time, in which case the data from the different");
90 UsageError(" profiles will be aggregated.");
91 UsageError("");
92 UsageError(" --profile-file-fd=<number>: same as --profile-file but accepts a file descriptor.");
93 UsageError(" Cannot be used together with --profile-file.");
94 UsageError("");
95 UsageError(" --reference-profile-file=<filename>: specify a reference profile.");
96 UsageError(" The data in this file will be compared with the data obtained by merging");
97 UsageError(" all the files specified with --profile-file or --profile-file-fd.");
98 UsageError(" If the exit code is EXIT_COMPILE then all --profile-file will be merged into");
99 UsageError(" --reference-profile-file. ");
100 UsageError("");
101 UsageError(" --reference-profile-file-fd=<number>: same as --reference-profile-file but");
102 UsageError(" accepts a file descriptor. Cannot be used together with");
103 UsageError(" --reference-profile-file.");
Calin Juravle7bcdb532016-06-07 16:14:47 +0100104 UsageError(" --generate-test-profile=<filename>: generates a random profile file for testing.");
105 UsageError(" --generate-test-profile-num-dex=<number>: number of dex files that should be");
106 UsageError(" included in the generated profile. Defaults to 20.");
107 UsageError(" --generate-test-profile-method-ratio=<number>: the percentage from the maximum");
108 UsageError(" number of methods that should be generated. Defaults to 5.");
109 UsageError(" --generate-test-profile-class-ratio=<number>: the percentage from the maximum");
110 UsageError(" number of classes that should be generated. Defaults to 5.");
111 UsageError("");
Calin Juravle2e2db782016-02-23 12:00:03 +0000112 UsageError("");
David Sehr546d24f2016-06-02 10:46:19 -0700113 UsageError(" --dex-location=<string>: location string to use with corresponding");
114 UsageError(" apk-fd to find dex files");
David Sehr4fcdd6d2016-05-24 14:52:31 -0700115 UsageError("");
David Sehr546d24f2016-06-02 10:46:19 -0700116 UsageError(" --apk-fd=<number>: file descriptor containing an open APK to");
David Sehr4fcdd6d2016-05-24 14:52:31 -0700117 UsageError(" search for dex files");
118 UsageError("");
Calin Juravle2e2db782016-02-23 12:00:03 +0000119
120 exit(EXIT_FAILURE);
121}
122
Calin Juravle7bcdb532016-06-07 16:14:47 +0100123// Note: make sure you update the Usage if you change these values.
124static constexpr uint16_t kDefaultTestProfileNumDex = 20;
125static constexpr uint16_t kDefaultTestProfileMethodRatio = 5;
126static constexpr uint16_t kDefaultTestProfileClassRatio = 5;
127
Calin Juravle2e2db782016-02-23 12:00:03 +0000128class ProfMan FINAL {
129 public:
130 ProfMan() :
David Sehr4fcdd6d2016-05-24 14:52:31 -0700131 reference_profile_file_fd_(kInvalidFd),
132 dump_only_(false),
133 dump_output_to_fd_(kInvalidFd),
Calin Juravle7bcdb532016-06-07 16:14:47 +0100134 test_profile_num_dex_(kDefaultTestProfileNumDex),
135 test_profile_method_ratio_(kDefaultTestProfileMethodRatio),
136 test_profile_class_ratio_(kDefaultTestProfileClassRatio),
Calin Juravle2e2db782016-02-23 12:00:03 +0000137 start_ns_(NanoTime()) {}
138
139 ~ProfMan() {
140 LogCompletionTime();
141 }
142
143 void ParseArgs(int argc, char **argv) {
144 original_argc = argc;
145 original_argv = argv;
146
David Sehrf57589f2016-10-17 10:09:33 -0700147 InitLogging(argv, Runtime::Aborter);
Calin Juravle2e2db782016-02-23 12:00:03 +0000148
149 // Skip over the command name.
150 argv++;
151 argc--;
152
153 if (argc == 0) {
154 Usage("No arguments specified");
155 }
156
157 for (int i = 0; i < argc; ++i) {
158 const StringPiece option(argv[i]);
159 const bool log_options = false;
160 if (log_options) {
Calin Juravle876f3502016-03-24 16:16:34 +0000161 LOG(INFO) << "profman: option[" << i << "]=" << argv[i];
Calin Juravle2e2db782016-02-23 12:00:03 +0000162 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700163 if (option == "--dump-only") {
164 dump_only_ = true;
165 } else if (option.starts_with("--dump-output-to-fd=")) {
166 ParseUintOption(option, "--dump-output-to-fd", &dump_output_to_fd_, Usage);
Calin Juravle876f3502016-03-24 16:16:34 +0000167 } else if (option.starts_with("--profile-file=")) {
Calin Juravle2e2db782016-02-23 12:00:03 +0000168 profile_files_.push_back(option.substr(strlen("--profile-file=")).ToString());
169 } else if (option.starts_with("--profile-file-fd=")) {
170 ParseFdForCollection(option, "--profile-file-fd", &profile_files_fd_);
171 } else if (option.starts_with("--reference-profile-file=")) {
172 reference_profile_file_ = option.substr(strlen("--reference-profile-file=")).ToString();
173 } else if (option.starts_with("--reference-profile-file-fd=")) {
174 ParseUintOption(option, "--reference-profile-file-fd", &reference_profile_file_fd_, Usage);
David Sehr546d24f2016-06-02 10:46:19 -0700175 } else if (option.starts_with("--dex-location=")) {
176 dex_locations_.push_back(option.substr(strlen("--dex-location=")).ToString());
177 } else if (option.starts_with("--apk-fd=")) {
178 ParseFdForCollection(option, "--apk-fd", &apks_fd_);
Calin Juravle7bcdb532016-06-07 16:14:47 +0100179 } else if (option.starts_with("--generate-test-profile=")) {
180 test_profile_ = option.substr(strlen("--generate-test-profile=")).ToString();
181 } else if (option.starts_with("--generate-test-profile-num-dex=")) {
182 ParseUintOption(option,
183 "--generate-test-profile-num-dex",
184 &test_profile_num_dex_,
185 Usage);
186 } else if (option.starts_with("--generate-test-profile-method-ratio")) {
187 ParseUintOption(option,
188 "--generate-test-profile-method-ratio",
189 &test_profile_method_ratio_,
190 Usage);
191 } else if (option.starts_with("--generate-test-profile-class-ratio")) {
192 ParseUintOption(option,
193 "--generate-test-profile-class-ratio",
194 &test_profile_class_ratio_,
195 Usage);
Calin Juravle2e2db782016-02-23 12:00:03 +0000196 } else {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700197 Usage("Unknown argument '%s'", option.data());
Calin Juravle2e2db782016-02-23 12:00:03 +0000198 }
199 }
200
Calin Juravle876f3502016-03-24 16:16:34 +0000201 bool has_profiles = !profile_files_.empty() || !profile_files_fd_.empty();
202 bool has_reference_profile = !reference_profile_file_.empty() ||
David Sehr4fcdd6d2016-05-24 14:52:31 -0700203 FdIsValid(reference_profile_file_fd_);
Calin Juravle876f3502016-03-24 16:16:34 +0000204
Calin Juravle7bcdb532016-06-07 16:14:47 +0100205 if (!test_profile_.empty()) {
206 if (test_profile_method_ratio_ > 100) {
207 Usage("Invalid ratio for --generate-test-profile-method-ratio");
208 }
209 if (test_profile_class_ratio_ > 100) {
210 Usage("Invalid ratio for --generate-test-profile-class-ratio");
211 }
212 return;
213 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700214 // --dump-only may be specified with only --reference-profiles present.
215 if (!dump_only_ && !has_profiles) {
Calin Juravle2e2db782016-02-23 12:00:03 +0000216 Usage("No profile files specified.");
217 }
218 if (!profile_files_.empty() && !profile_files_fd_.empty()) {
219 Usage("Profile files should not be specified with both --profile-file-fd and --profile-file");
220 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700221 if (!dump_only_ && !has_reference_profile) {
222 Usage("No reference profile file specified.");
Calin Juravle2e2db782016-02-23 12:00:03 +0000223 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700224 if (!reference_profile_file_.empty() && FdIsValid(reference_profile_file_fd_)) {
225 Usage("Reference profile should not be specified with both "
226 "--reference-profile-file-fd and --reference-profile-file");
227 }
228 if ((!profile_files_.empty() && FdIsValid(reference_profile_file_fd_)) ||
229 (!dump_only_ && !profile_files_fd_.empty() && !FdIsValid(reference_profile_file_fd_))) {
230 Usage("Options --profile-file-fd and --reference-profile-file-fd "
231 "should only be used together");
Calin Juravle2e2db782016-02-23 12:00:03 +0000232 }
233 }
234
235 ProfileAssistant::ProcessingResult ProcessProfiles() {
236 ProfileAssistant::ProcessingResult result;
237 if (profile_files_.empty()) {
238 // The file doesn't need to be flushed here (ProcessProfiles will do it)
239 // so don't check the usage.
240 File file(reference_profile_file_fd_, false);
241 result = ProfileAssistant::ProcessProfiles(profile_files_fd_, reference_profile_file_fd_);
242 CloseAllFds(profile_files_fd_, "profile_files_fd_");
243 } else {
244 result = ProfileAssistant::ProcessProfiles(profile_files_, reference_profile_file_);
245 }
246 return result;
247 }
248
David Sehr4fcdd6d2016-05-24 14:52:31 -0700249 int DumpOneProfile(const std::string& banner, const std::string& filename, int fd,
250 const std::vector<const DexFile*>* dex_files, std::string* dump) {
251 if (!filename.empty()) {
252 fd = open(filename.c_str(), O_RDWR);
253 if (fd < 0) {
254 std::cerr << "Cannot open " << filename << strerror(errno);
255 return -1;
256 }
Calin Juravle876f3502016-03-24 16:16:34 +0000257 }
258 ProfileCompilationInfo info;
259 if (!info.Load(fd)) {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700260 std::cerr << "Cannot load profile info from fd=" << fd << "\n";
Calin Juravle876f3502016-03-24 16:16:34 +0000261 return -1;
262 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700263 std::string this_dump = banner + "\n" + info.DumpInfo(dex_files) + "\n";
264 *dump += this_dump;
265 if (close(fd) < 0) {
266 PLOG(WARNING) << "Failed to close descriptor";
267 }
268 return 0;
269 }
270
271 int DumpProfileInfo() {
272 static const char* kEmptyString = "";
David Sehr546d24f2016-06-02 10:46:19 -0700273 static const char* kOrdinaryProfile = "=== profile ===";
274 static const char* kReferenceProfile = "=== reference profile ===";
275
276 // Open apk/zip files and and read dex files.
277 MemMap::Init(); // for ZipArchive::OpenFromFd
278 std::vector<const DexFile*> dex_files;
279 assert(dex_locations_.size() == apks_fd_.size());
Aart Bik37d6a3b2016-06-21 18:30:10 -0700280 static constexpr bool kVerifyChecksum = true;
David Sehr546d24f2016-06-02 10:46:19 -0700281 for (size_t i = 0; i < dex_locations_.size(); ++i) {
282 std::string error_msg;
283 std::vector<std::unique_ptr<const DexFile>> dex_files_for_location;
David Sehr733ddb22016-09-19 15:02:18 -0700284 if (DexFile::OpenZip(apks_fd_[i],
285 dex_locations_[i],
286 kVerifyChecksum,
287 &error_msg,
288 &dex_files_for_location)) {
David Sehr546d24f2016-06-02 10:46:19 -0700289 } else {
290 LOG(WARNING) << "OpenFromZip failed for '" << dex_locations_[i] << "' " << error_msg;
291 continue;
292 }
293 for (std::unique_ptr<const DexFile>& dex_file : dex_files_for_location) {
294 dex_files.push_back(dex_file.release());
295 }
296 }
297
David Sehr4fcdd6d2016-05-24 14:52:31 -0700298 std::string dump;
David Sehr4fcdd6d2016-05-24 14:52:31 -0700299 // Dump individual profile files.
300 if (!profile_files_fd_.empty()) {
301 for (int profile_file_fd : profile_files_fd_) {
David Sehr546d24f2016-06-02 10:46:19 -0700302 int ret = DumpOneProfile(kOrdinaryProfile,
303 kEmptyString,
304 profile_file_fd,
305 &dex_files,
306 &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700307 if (ret != 0) {
308 return ret;
309 }
310 }
311 }
312 if (!profile_files_.empty()) {
313 for (const std::string& profile_file : profile_files_) {
David Sehr546d24f2016-06-02 10:46:19 -0700314 int ret = DumpOneProfile(kOrdinaryProfile, profile_file, kInvalidFd, &dex_files, &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700315 if (ret != 0) {
316 return ret;
317 }
318 }
319 }
320 // Dump reference profile file.
321 if (FdIsValid(reference_profile_file_fd_)) {
David Sehr546d24f2016-06-02 10:46:19 -0700322 int ret = DumpOneProfile(kReferenceProfile,
323 kEmptyString,
324 reference_profile_file_fd_,
325 &dex_files,
326 &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700327 if (ret != 0) {
328 return ret;
329 }
330 }
331 if (!reference_profile_file_.empty()) {
David Sehr546d24f2016-06-02 10:46:19 -0700332 int ret = DumpOneProfile(kReferenceProfile,
333 reference_profile_file_,
334 kInvalidFd,
335 &dex_files,
David Sehr4fcdd6d2016-05-24 14:52:31 -0700336 &dump);
337 if (ret != 0) {
338 return ret;
339 }
340 }
341 if (!FdIsValid(dump_output_to_fd_)) {
342 std::cout << dump;
343 } else {
344 unix_file::FdFile out_fd(dump_output_to_fd_, false /*check_usage*/);
345 if (!out_fd.WriteFully(dump.c_str(), dump.length())) {
346 return -1;
347 }
348 }
Calin Juravle876f3502016-03-24 16:16:34 +0000349 return 0;
350 }
351
352 bool ShouldOnlyDumpProfile() {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700353 return dump_only_;
Calin Juravle876f3502016-03-24 16:16:34 +0000354 }
355
Calin Juravle7bcdb532016-06-07 16:14:47 +0100356 int GenerateTestProfile() {
George Burgess IV71f77262016-10-25 13:08:17 -0700357 int profile_test_fd = open(test_profile_.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
Calin Juravle7bcdb532016-06-07 16:14:47 +0100358 if (profile_test_fd < 0) {
359 std::cerr << "Cannot open " << test_profile_ << strerror(errno);
360 return -1;
361 }
362
363 bool result = ProfileCompilationInfo::GenerateTestProfile(profile_test_fd,
364 test_profile_num_dex_,
365 test_profile_method_ratio_,
366 test_profile_class_ratio_);
367 close(profile_test_fd); // ignore close result.
368 return result ? 0 : -1;
369 }
370
371 bool ShouldGenerateTestProfile() {
372 return !test_profile_.empty();
373 }
374
Calin Juravle2e2db782016-02-23 12:00:03 +0000375 private:
376 static void ParseFdForCollection(const StringPiece& option,
377 const char* arg_name,
378 std::vector<int>* fds) {
379 int fd;
380 ParseUintOption(option, arg_name, &fd, Usage);
381 fds->push_back(fd);
382 }
383
384 static void CloseAllFds(const std::vector<int>& fds, const char* descriptor) {
385 for (size_t i = 0; i < fds.size(); i++) {
386 if (close(fds[i]) < 0) {
387 PLOG(WARNING) << "Failed to close descriptor for " << descriptor << " at index " << i;
388 }
389 }
390 }
391
392 void LogCompletionTime() {
David Sehr52683cf2016-05-05 09:02:38 -0700393 static constexpr uint64_t kLogThresholdTime = MsToNs(100); // 100ms
394 uint64_t time_taken = NanoTime() - start_ns_;
David Sehred793012016-05-05 13:39:43 -0700395 if (time_taken > kLogThresholdTime) {
David Sehrd8557182016-05-06 12:29:35 -0700396 LOG(WARNING) << "profman took " << PrettyDuration(time_taken);
David Sehred793012016-05-05 13:39:43 -0700397 }
Calin Juravle2e2db782016-02-23 12:00:03 +0000398 }
399
400 std::vector<std::string> profile_files_;
401 std::vector<int> profile_files_fd_;
David Sehr546d24f2016-06-02 10:46:19 -0700402 std::vector<std::string> dex_locations_;
403 std::vector<int> apks_fd_;
Calin Juravle2e2db782016-02-23 12:00:03 +0000404 std::string reference_profile_file_;
405 int reference_profile_file_fd_;
David Sehr4fcdd6d2016-05-24 14:52:31 -0700406 bool dump_only_;
407 int dump_output_to_fd_;
Calin Juravle7bcdb532016-06-07 16:14:47 +0100408 std::string test_profile_;
409 uint16_t test_profile_num_dex_;
410 uint16_t test_profile_method_ratio_;
411 uint16_t test_profile_class_ratio_;
Calin Juravle2e2db782016-02-23 12:00:03 +0000412 uint64_t start_ns_;
413};
414
415// See ProfileAssistant::ProcessingResult for return codes.
416static int profman(int argc, char** argv) {
417 ProfMan profman;
418
419 // Parse arguments. Argument mistakes will lead to exit(EXIT_FAILURE) in UsageError.
420 profman.ParseArgs(argc, argv);
421
Calin Juravle7bcdb532016-06-07 16:14:47 +0100422 if (profman.ShouldGenerateTestProfile()) {
423 return profman.GenerateTestProfile();
424 }
Calin Juravle876f3502016-03-24 16:16:34 +0000425 if (profman.ShouldOnlyDumpProfile()) {
426 return profman.DumpProfileInfo();
427 }
Calin Juravle2e2db782016-02-23 12:00:03 +0000428 // Process profile information and assess if we need to do a profile guided compilation.
429 // This operation involves I/O.
430 return profman.ProcessProfiles();
431}
432
433} // namespace art
434
435int main(int argc, char **argv) {
436 return art::profman(argc, argv);
437}
438