blob: a5fefa71d4db61c2d1315cf0f321d615e2b2ced7 [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"
36#include "utils.h"
David Sehr546d24f2016-06-02 10:46:19 -070037#include "zip_archive.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000038#include "profile_assistant.h"
39
40namespace art {
41
42static int original_argc;
43static char** original_argv;
44
45static std::string CommandLine() {
46 std::vector<std::string> command;
47 for (int i = 0; i < original_argc; ++i) {
48 command.push_back(original_argv[i]);
49 }
50 return Join(command, ' ');
51}
52
David Sehr4fcdd6d2016-05-24 14:52:31 -070053static constexpr int kInvalidFd = -1;
54
55static bool FdIsValid(int fd) {
56 return fd != kInvalidFd;
57}
58
Calin Juravle2e2db782016-02-23 12:00:03 +000059static void UsageErrorV(const char* fmt, va_list ap) {
60 std::string error;
61 StringAppendV(&error, fmt, ap);
62 LOG(ERROR) << error;
63}
64
65static void UsageError(const char* fmt, ...) {
66 va_list ap;
67 va_start(ap, fmt);
68 UsageErrorV(fmt, ap);
69 va_end(ap);
70}
71
72NO_RETURN static void Usage(const char *fmt, ...) {
73 va_list ap;
74 va_start(ap, fmt);
75 UsageErrorV(fmt, ap);
76 va_end(ap);
77
78 UsageError("Command: %s", CommandLine().c_str());
79 UsageError("Usage: profman [options]...");
80 UsageError("");
David Sehr4fcdd6d2016-05-24 14:52:31 -070081 UsageError(" --dump-only: dumps the content of the specified profile files");
82 UsageError(" to standard output (default) in a human readable form.");
83 UsageError("");
84 UsageError(" --dump-output-to-fd=<number>: redirects --dump-info-for output to a file");
85 UsageError(" descriptor.");
Calin Juravle876f3502016-03-24 16:16:34 +000086 UsageError("");
Calin Juravle2e2db782016-02-23 12:00:03 +000087 UsageError(" --profile-file=<filename>: specify profiler output file to use for compilation.");
88 UsageError(" Can be specified multiple time, in which case the data from the different");
89 UsageError(" profiles will be aggregated.");
90 UsageError("");
91 UsageError(" --profile-file-fd=<number>: same as --profile-file but accepts a file descriptor.");
92 UsageError(" Cannot be used together with --profile-file.");
93 UsageError("");
94 UsageError(" --reference-profile-file=<filename>: specify a reference profile.");
95 UsageError(" The data in this file will be compared with the data obtained by merging");
96 UsageError(" all the files specified with --profile-file or --profile-file-fd.");
97 UsageError(" If the exit code is EXIT_COMPILE then all --profile-file will be merged into");
98 UsageError(" --reference-profile-file. ");
99 UsageError("");
100 UsageError(" --reference-profile-file-fd=<number>: same as --reference-profile-file but");
101 UsageError(" accepts a file descriptor. Cannot be used together with");
102 UsageError(" --reference-profile-file.");
Calin Juravle7bcdb532016-06-07 16:14:47 +0100103 UsageError(" --generate-test-profile=<filename>: generates a random profile file for testing.");
104 UsageError(" --generate-test-profile-num-dex=<number>: number of dex files that should be");
105 UsageError(" included in the generated profile. Defaults to 20.");
106 UsageError(" --generate-test-profile-method-ratio=<number>: the percentage from the maximum");
107 UsageError(" number of methods that should be generated. Defaults to 5.");
108 UsageError(" --generate-test-profile-class-ratio=<number>: the percentage from the maximum");
109 UsageError(" number of classes that should be generated. Defaults to 5.");
110 UsageError("");
Calin Juravle2e2db782016-02-23 12:00:03 +0000111 UsageError("");
David Sehr546d24f2016-06-02 10:46:19 -0700112 UsageError(" --dex-location=<string>: location string to use with corresponding");
113 UsageError(" apk-fd to find dex files");
David Sehr4fcdd6d2016-05-24 14:52:31 -0700114 UsageError("");
David Sehr546d24f2016-06-02 10:46:19 -0700115 UsageError(" --apk-fd=<number>: file descriptor containing an open APK to");
David Sehr4fcdd6d2016-05-24 14:52:31 -0700116 UsageError(" search for dex files");
117 UsageError("");
Calin Juravle2e2db782016-02-23 12:00:03 +0000118
119 exit(EXIT_FAILURE);
120}
121
Calin Juravle7bcdb532016-06-07 16:14:47 +0100122// Note: make sure you update the Usage if you change these values.
123static constexpr uint16_t kDefaultTestProfileNumDex = 20;
124static constexpr uint16_t kDefaultTestProfileMethodRatio = 5;
125static constexpr uint16_t kDefaultTestProfileClassRatio = 5;
126
Calin Juravle2e2db782016-02-23 12:00:03 +0000127class ProfMan FINAL {
128 public:
129 ProfMan() :
David Sehr4fcdd6d2016-05-24 14:52:31 -0700130 reference_profile_file_fd_(kInvalidFd),
131 dump_only_(false),
132 dump_output_to_fd_(kInvalidFd),
Calin Juravle7bcdb532016-06-07 16:14:47 +0100133 test_profile_num_dex_(kDefaultTestProfileNumDex),
134 test_profile_method_ratio_(kDefaultTestProfileMethodRatio),
135 test_profile_class_ratio_(kDefaultTestProfileClassRatio),
Calin Juravle2e2db782016-02-23 12:00:03 +0000136 start_ns_(NanoTime()) {}
137
138 ~ProfMan() {
139 LogCompletionTime();
140 }
141
142 void ParseArgs(int argc, char **argv) {
143 original_argc = argc;
144 original_argv = argv;
145
146 InitLogging(argv);
147
148 // Skip over the command name.
149 argv++;
150 argc--;
151
152 if (argc == 0) {
153 Usage("No arguments specified");
154 }
155
156 for (int i = 0; i < argc; ++i) {
157 const StringPiece option(argv[i]);
158 const bool log_options = false;
159 if (log_options) {
Calin Juravle876f3502016-03-24 16:16:34 +0000160 LOG(INFO) << "profman: option[" << i << "]=" << argv[i];
Calin Juravle2e2db782016-02-23 12:00:03 +0000161 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700162 if (option == "--dump-only") {
163 dump_only_ = true;
164 } else if (option.starts_with("--dump-output-to-fd=")) {
165 ParseUintOption(option, "--dump-output-to-fd", &dump_output_to_fd_, Usage);
Calin Juravle876f3502016-03-24 16:16:34 +0000166 } else if (option.starts_with("--profile-file=")) {
Calin Juravle2e2db782016-02-23 12:00:03 +0000167 profile_files_.push_back(option.substr(strlen("--profile-file=")).ToString());
168 } else if (option.starts_with("--profile-file-fd=")) {
169 ParseFdForCollection(option, "--profile-file-fd", &profile_files_fd_);
170 } else if (option.starts_with("--reference-profile-file=")) {
171 reference_profile_file_ = option.substr(strlen("--reference-profile-file=")).ToString();
172 } else if (option.starts_with("--reference-profile-file-fd=")) {
173 ParseUintOption(option, "--reference-profile-file-fd", &reference_profile_file_fd_, Usage);
David Sehr546d24f2016-06-02 10:46:19 -0700174 } else if (option.starts_with("--dex-location=")) {
175 dex_locations_.push_back(option.substr(strlen("--dex-location=")).ToString());
176 } else if (option.starts_with("--apk-fd=")) {
177 ParseFdForCollection(option, "--apk-fd", &apks_fd_);
Calin Juravle7bcdb532016-06-07 16:14:47 +0100178 } else if (option.starts_with("--generate-test-profile=")) {
179 test_profile_ = option.substr(strlen("--generate-test-profile=")).ToString();
180 } else if (option.starts_with("--generate-test-profile-num-dex=")) {
181 ParseUintOption(option,
182 "--generate-test-profile-num-dex",
183 &test_profile_num_dex_,
184 Usage);
185 } else if (option.starts_with("--generate-test-profile-method-ratio")) {
186 ParseUintOption(option,
187 "--generate-test-profile-method-ratio",
188 &test_profile_method_ratio_,
189 Usage);
190 } else if (option.starts_with("--generate-test-profile-class-ratio")) {
191 ParseUintOption(option,
192 "--generate-test-profile-class-ratio",
193 &test_profile_class_ratio_,
194 Usage);
Calin Juravle2e2db782016-02-23 12:00:03 +0000195 } else {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700196 Usage("Unknown argument '%s'", option.data());
Calin Juravle2e2db782016-02-23 12:00:03 +0000197 }
198 }
199
Calin Juravle876f3502016-03-24 16:16:34 +0000200 bool has_profiles = !profile_files_.empty() || !profile_files_fd_.empty();
201 bool has_reference_profile = !reference_profile_file_.empty() ||
David Sehr4fcdd6d2016-05-24 14:52:31 -0700202 FdIsValid(reference_profile_file_fd_);
Calin Juravle876f3502016-03-24 16:16:34 +0000203
Calin Juravle7bcdb532016-06-07 16:14:47 +0100204 if (!test_profile_.empty()) {
205 if (test_profile_method_ratio_ > 100) {
206 Usage("Invalid ratio for --generate-test-profile-method-ratio");
207 }
208 if (test_profile_class_ratio_ > 100) {
209 Usage("Invalid ratio for --generate-test-profile-class-ratio");
210 }
211 return;
212 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700213 // --dump-only may be specified with only --reference-profiles present.
214 if (!dump_only_ && !has_profiles) {
Calin Juravle2e2db782016-02-23 12:00:03 +0000215 Usage("No profile files specified.");
216 }
217 if (!profile_files_.empty() && !profile_files_fd_.empty()) {
218 Usage("Profile files should not be specified with both --profile-file-fd and --profile-file");
219 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700220 if (!dump_only_ && !has_reference_profile) {
221 Usage("No reference profile file specified.");
Calin Juravle2e2db782016-02-23 12:00:03 +0000222 }
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 }
227 if ((!profile_files_.empty() && FdIsValid(reference_profile_file_fd_)) ||
228 (!dump_only_ && !profile_files_fd_.empty() && !FdIsValid(reference_profile_file_fd_))) {
229 Usage("Options --profile-file-fd and --reference-profile-file-fd "
230 "should only be used together");
Calin Juravle2e2db782016-02-23 12:00:03 +0000231 }
232 }
233
234 ProfileAssistant::ProcessingResult ProcessProfiles() {
235 ProfileAssistant::ProcessingResult result;
236 if (profile_files_.empty()) {
237 // The file doesn't need to be flushed here (ProcessProfiles will do it)
238 // so don't check the usage.
239 File file(reference_profile_file_fd_, false);
240 result = ProfileAssistant::ProcessProfiles(profile_files_fd_, reference_profile_file_fd_);
241 CloseAllFds(profile_files_fd_, "profile_files_fd_");
242 } else {
243 result = ProfileAssistant::ProcessProfiles(profile_files_, reference_profile_file_);
244 }
245 return result;
246 }
247
David Sehr4fcdd6d2016-05-24 14:52:31 -0700248 int DumpOneProfile(const std::string& banner, const std::string& filename, int fd,
249 const std::vector<const DexFile*>* dex_files, std::string* dump) {
250 if (!filename.empty()) {
251 fd = open(filename.c_str(), O_RDWR);
252 if (fd < 0) {
253 std::cerr << "Cannot open " << filename << strerror(errno);
254 return -1;
255 }
Calin Juravle876f3502016-03-24 16:16:34 +0000256 }
257 ProfileCompilationInfo info;
258 if (!info.Load(fd)) {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700259 std::cerr << "Cannot load profile info from fd=" << fd << "\n";
Calin Juravle876f3502016-03-24 16:16:34 +0000260 return -1;
261 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700262 std::string this_dump = banner + "\n" + info.DumpInfo(dex_files) + "\n";
263 *dump += this_dump;
264 if (close(fd) < 0) {
265 PLOG(WARNING) << "Failed to close descriptor";
266 }
267 return 0;
268 }
269
270 int DumpProfileInfo() {
271 static const char* kEmptyString = "";
David Sehr546d24f2016-06-02 10:46:19 -0700272 static const char* kOrdinaryProfile = "=== profile ===";
273 static const char* kReferenceProfile = "=== reference profile ===";
274
275 // Open apk/zip files and and read dex files.
276 MemMap::Init(); // for ZipArchive::OpenFromFd
277 std::vector<const DexFile*> dex_files;
278 assert(dex_locations_.size() == apks_fd_.size());
Aart Bik37d6a3b2016-06-21 18:30:10 -0700279 static constexpr bool kVerifyChecksum = true;
David Sehr546d24f2016-06-02 10:46:19 -0700280 for (size_t i = 0; i < dex_locations_.size(); ++i) {
281 std::string error_msg;
282 std::vector<std::unique_ptr<const DexFile>> dex_files_for_location;
283 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(apks_fd_[i],
284 dex_locations_[i].c_str(),
285 &error_msg));
286 if (zip_archive == nullptr) {
287 LOG(WARNING) << "OpenFromFd failed for '" << dex_locations_[i] << "' " << error_msg;
288 continue;
289 }
290 if (DexFile::OpenFromZip(*zip_archive,
291 dex_locations_[i],
Aart Bik37d6a3b2016-06-21 18:30:10 -0700292 kVerifyChecksum,
David Sehr546d24f2016-06-02 10:46:19 -0700293 &error_msg,
294 &dex_files_for_location)) {
295 } else {
296 LOG(WARNING) << "OpenFromZip failed for '" << dex_locations_[i] << "' " << error_msg;
297 continue;
298 }
299 for (std::unique_ptr<const DexFile>& dex_file : dex_files_for_location) {
300 dex_files.push_back(dex_file.release());
301 }
302 }
303
David Sehr4fcdd6d2016-05-24 14:52:31 -0700304 std::string dump;
David Sehr4fcdd6d2016-05-24 14:52:31 -0700305 // Dump individual profile files.
306 if (!profile_files_fd_.empty()) {
307 for (int profile_file_fd : profile_files_fd_) {
David Sehr546d24f2016-06-02 10:46:19 -0700308 int ret = DumpOneProfile(kOrdinaryProfile,
309 kEmptyString,
310 profile_file_fd,
311 &dex_files,
312 &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700313 if (ret != 0) {
314 return ret;
315 }
316 }
317 }
318 if (!profile_files_.empty()) {
319 for (const std::string& profile_file : profile_files_) {
David Sehr546d24f2016-06-02 10:46:19 -0700320 int ret = DumpOneProfile(kOrdinaryProfile, profile_file, kInvalidFd, &dex_files, &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700321 if (ret != 0) {
322 return ret;
323 }
324 }
325 }
326 // Dump reference profile file.
327 if (FdIsValid(reference_profile_file_fd_)) {
David Sehr546d24f2016-06-02 10:46:19 -0700328 int ret = DumpOneProfile(kReferenceProfile,
329 kEmptyString,
330 reference_profile_file_fd_,
331 &dex_files,
332 &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700333 if (ret != 0) {
334 return ret;
335 }
336 }
337 if (!reference_profile_file_.empty()) {
David Sehr546d24f2016-06-02 10:46:19 -0700338 int ret = DumpOneProfile(kReferenceProfile,
339 reference_profile_file_,
340 kInvalidFd,
341 &dex_files,
David Sehr4fcdd6d2016-05-24 14:52:31 -0700342 &dump);
343 if (ret != 0) {
344 return ret;
345 }
346 }
347 if (!FdIsValid(dump_output_to_fd_)) {
348 std::cout << dump;
349 } else {
350 unix_file::FdFile out_fd(dump_output_to_fd_, false /*check_usage*/);
351 if (!out_fd.WriteFully(dump.c_str(), dump.length())) {
352 return -1;
353 }
354 }
Calin Juravle876f3502016-03-24 16:16:34 +0000355 return 0;
356 }
357
358 bool ShouldOnlyDumpProfile() {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700359 return dump_only_;
Calin Juravle876f3502016-03-24 16:16:34 +0000360 }
361
Calin Juravle7bcdb532016-06-07 16:14:47 +0100362 int GenerateTestProfile() {
363 int profile_test_fd = open(test_profile_.c_str(), O_CREAT | O_TRUNC | O_WRONLY);
364 if (profile_test_fd < 0) {
365 std::cerr << "Cannot open " << test_profile_ << strerror(errno);
366 return -1;
367 }
368
369 bool result = ProfileCompilationInfo::GenerateTestProfile(profile_test_fd,
370 test_profile_num_dex_,
371 test_profile_method_ratio_,
372 test_profile_class_ratio_);
373 close(profile_test_fd); // ignore close result.
374 return result ? 0 : -1;
375 }
376
377 bool ShouldGenerateTestProfile() {
378 return !test_profile_.empty();
379 }
380
Calin Juravle2e2db782016-02-23 12:00:03 +0000381 private:
382 static void ParseFdForCollection(const StringPiece& option,
383 const char* arg_name,
384 std::vector<int>* fds) {
385 int fd;
386 ParseUintOption(option, arg_name, &fd, Usage);
387 fds->push_back(fd);
388 }
389
390 static void CloseAllFds(const std::vector<int>& fds, const char* descriptor) {
391 for (size_t i = 0; i < fds.size(); i++) {
392 if (close(fds[i]) < 0) {
393 PLOG(WARNING) << "Failed to close descriptor for " << descriptor << " at index " << i;
394 }
395 }
396 }
397
398 void LogCompletionTime() {
David Sehr52683cf2016-05-05 09:02:38 -0700399 static constexpr uint64_t kLogThresholdTime = MsToNs(100); // 100ms
400 uint64_t time_taken = NanoTime() - start_ns_;
David Sehred793012016-05-05 13:39:43 -0700401 if (time_taken > kLogThresholdTime) {
David Sehrd8557182016-05-06 12:29:35 -0700402 LOG(WARNING) << "profman took " << PrettyDuration(time_taken);
David Sehred793012016-05-05 13:39:43 -0700403 }
Calin Juravle2e2db782016-02-23 12:00:03 +0000404 }
405
406 std::vector<std::string> profile_files_;
407 std::vector<int> profile_files_fd_;
David Sehr546d24f2016-06-02 10:46:19 -0700408 std::vector<std::string> dex_locations_;
409 std::vector<int> apks_fd_;
Calin Juravle2e2db782016-02-23 12:00:03 +0000410 std::string reference_profile_file_;
411 int reference_profile_file_fd_;
David Sehr4fcdd6d2016-05-24 14:52:31 -0700412 bool dump_only_;
413 int dump_output_to_fd_;
Calin Juravle7bcdb532016-06-07 16:14:47 +0100414 std::string test_profile_;
415 uint16_t test_profile_num_dex_;
416 uint16_t test_profile_method_ratio_;
417 uint16_t test_profile_class_ratio_;
Calin Juravle2e2db782016-02-23 12:00:03 +0000418 uint64_t start_ns_;
419};
420
421// See ProfileAssistant::ProcessingResult for return codes.
422static int profman(int argc, char** argv) {
423 ProfMan profman;
424
425 // Parse arguments. Argument mistakes will lead to exit(EXIT_FAILURE) in UsageError.
426 profman.ParseArgs(argc, argv);
427
Calin Juravle7bcdb532016-06-07 16:14:47 +0100428 if (profman.ShouldGenerateTestProfile()) {
429 return profman.GenerateTestProfile();
430 }
Calin Juravle876f3502016-03-24 16:16:34 +0000431 if (profman.ShouldOnlyDumpProfile()) {
432 return profman.DumpProfileInfo();
433 }
Calin Juravle2e2db782016-02-23 12:00:03 +0000434 // Process profile information and assess if we need to do a profile guided compilation.
435 // This operation involves I/O.
436 return profman.ProcessProfiles();
437}
438
439} // namespace art
440
441int main(int argc, char **argv) {
442 return art::profman(argc, argv);
443}
444