blob: a99a0ea2da83525e3805e0d5a41e5766bff6e07a [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"
Calin Juravlee0ac1152017-02-13 19:03:47 -080039#include "bytecode_utils.h"
David Sehr4fcdd6d2016-05-24 14:52:31 -070040#include "dex_file.h"
Calin Juravle33083d62017-01-18 15:29:12 -080041#include "jit/profile_compilation_info.h"
David Sehrf57589f2016-10-17 10:09:33 -070042#include "runtime.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000043#include "utils.h"
David Sehr546d24f2016-06-02 10:46:19 -070044#include "zip_archive.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000045#include "profile_assistant.h"
46
47namespace art {
48
49static int original_argc;
50static char** original_argv;
51
52static std::string CommandLine() {
53 std::vector<std::string> command;
54 for (int i = 0; i < original_argc; ++i) {
55 command.push_back(original_argv[i]);
56 }
Andreas Gampe9186ced2016-12-12 14:28:21 -080057 return android::base::Join(command, ' ');
Calin Juravle2e2db782016-02-23 12:00:03 +000058}
59
David Sehr4fcdd6d2016-05-24 14:52:31 -070060static constexpr int kInvalidFd = -1;
61
62static bool FdIsValid(int fd) {
63 return fd != kInvalidFd;
64}
65
Calin Juravle2e2db782016-02-23 12:00:03 +000066static void UsageErrorV(const char* fmt, va_list ap) {
67 std::string error;
Andreas Gampe46ee31b2016-12-14 10:11:49 -080068 android::base::StringAppendV(&error, fmt, ap);
Calin Juravle2e2db782016-02-23 12:00:03 +000069 LOG(ERROR) << error;
70}
71
72static void UsageError(const char* fmt, ...) {
73 va_list ap;
74 va_start(ap, fmt);
75 UsageErrorV(fmt, ap);
76 va_end(ap);
77}
78
79NO_RETURN static void Usage(const char *fmt, ...) {
80 va_list ap;
81 va_start(ap, fmt);
82 UsageErrorV(fmt, ap);
83 va_end(ap);
84
85 UsageError("Command: %s", CommandLine().c_str());
86 UsageError("Usage: profman [options]...");
87 UsageError("");
David Sehr4fcdd6d2016-05-24 14:52:31 -070088 UsageError(" --dump-only: dumps the content of the specified profile files");
89 UsageError(" to standard output (default) in a human readable form.");
90 UsageError("");
David Sehr7c80f2d2017-02-07 16:47:58 -080091 UsageError(" --dump-output-to-fd=<number>: redirects --dump-only output to a file descriptor.");
92 UsageError("");
93 UsageError(" --dump-classes: dumps a sorted list of classes that are in the specified profile");
94 UsageError(" file to standard output (default) in a human readable form.");
Calin Juravle876f3502016-03-24 16:16:34 +000095 UsageError("");
Calin Juravle2e2db782016-02-23 12:00:03 +000096 UsageError(" --profile-file=<filename>: specify profiler output file to use for compilation.");
97 UsageError(" Can be specified multiple time, in which case the data from the different");
98 UsageError(" profiles will be aggregated.");
99 UsageError("");
100 UsageError(" --profile-file-fd=<number>: same as --profile-file but accepts a file descriptor.");
101 UsageError(" Cannot be used together with --profile-file.");
102 UsageError("");
103 UsageError(" --reference-profile-file=<filename>: specify a reference profile.");
104 UsageError(" The data in this file will be compared with the data obtained by merging");
105 UsageError(" all the files specified with --profile-file or --profile-file-fd.");
106 UsageError(" If the exit code is EXIT_COMPILE then all --profile-file will be merged into");
107 UsageError(" --reference-profile-file. ");
108 UsageError("");
109 UsageError(" --reference-profile-file-fd=<number>: same as --reference-profile-file but");
110 UsageError(" accepts a file descriptor. Cannot be used together with");
111 UsageError(" --reference-profile-file.");
David Sehr7c80f2d2017-02-07 16:47:58 -0800112 UsageError("");
Calin Juravle7bcdb532016-06-07 16:14:47 +0100113 UsageError(" --generate-test-profile=<filename>: generates a random profile file for testing.");
114 UsageError(" --generate-test-profile-num-dex=<number>: number of dex files that should be");
115 UsageError(" included in the generated profile. Defaults to 20.");
116 UsageError(" --generate-test-profile-method-ratio=<number>: the percentage from the maximum");
117 UsageError(" number of methods that should be generated. Defaults to 5.");
118 UsageError(" --generate-test-profile-class-ratio=<number>: the percentage from the maximum");
119 UsageError(" number of classes that should be generated. Defaults to 5.");
120 UsageError("");
David Sehr7c80f2d2017-02-07 16:47:58 -0800121 UsageError(" --create-profile-from=<filename>: creates a profile from a list of classes.");
122 UsageError("");
Calin Juravle2e2db782016-02-23 12:00:03 +0000123 UsageError("");
David Sehr546d24f2016-06-02 10:46:19 -0700124 UsageError(" --dex-location=<string>: location string to use with corresponding");
125 UsageError(" apk-fd to find dex files");
David Sehr4fcdd6d2016-05-24 14:52:31 -0700126 UsageError("");
David Sehr546d24f2016-06-02 10:46:19 -0700127 UsageError(" --apk-fd=<number>: file descriptor containing an open APK to");
David Sehr4fcdd6d2016-05-24 14:52:31 -0700128 UsageError(" search for dex files");
David Sehr7c80f2d2017-02-07 16:47:58 -0800129 UsageError(" --apk-=<filename>: an APK to search for dex files");
David Sehr4fcdd6d2016-05-24 14:52:31 -0700130 UsageError("");
Calin Juravle2e2db782016-02-23 12:00:03 +0000131
132 exit(EXIT_FAILURE);
133}
134
Calin Juravle7bcdb532016-06-07 16:14:47 +0100135// Note: make sure you update the Usage if you change these values.
136static constexpr uint16_t kDefaultTestProfileNumDex = 20;
137static constexpr uint16_t kDefaultTestProfileMethodRatio = 5;
138static constexpr uint16_t kDefaultTestProfileClassRatio = 5;
139
Calin Juravlee0ac1152017-02-13 19:03:47 -0800140// Separators used when parsing human friendly representation of profiles.
141static const std::string kMethodSep = "->";
142static constexpr char kProfileParsingInlineChacheSep = '+';
143static constexpr char kProfileParsingTypeSep = ',';
144static constexpr char kProfileParsingFirstCharInSignature = '(';
145
146// TODO(calin): This class has grown too much from its initial design. Split the functionality
147// into smaller, more contained pieces.
Calin Juravle2e2db782016-02-23 12:00:03 +0000148class ProfMan FINAL {
149 public:
150 ProfMan() :
David Sehr4fcdd6d2016-05-24 14:52:31 -0700151 reference_profile_file_fd_(kInvalidFd),
152 dump_only_(false),
David Sehr7c80f2d2017-02-07 16:47:58 -0800153 dump_classes_(false),
David Sehr4fcdd6d2016-05-24 14:52:31 -0700154 dump_output_to_fd_(kInvalidFd),
Calin Juravle7bcdb532016-06-07 16:14:47 +0100155 test_profile_num_dex_(kDefaultTestProfileNumDex),
156 test_profile_method_ratio_(kDefaultTestProfileMethodRatio),
157 test_profile_class_ratio_(kDefaultTestProfileClassRatio),
Calin Juravle2e2db782016-02-23 12:00:03 +0000158 start_ns_(NanoTime()) {}
159
160 ~ProfMan() {
161 LogCompletionTime();
162 }
163
164 void ParseArgs(int argc, char **argv) {
165 original_argc = argc;
166 original_argv = argv;
167
David Sehrf57589f2016-10-17 10:09:33 -0700168 InitLogging(argv, Runtime::Aborter);
Calin Juravle2e2db782016-02-23 12:00:03 +0000169
170 // Skip over the command name.
171 argv++;
172 argc--;
173
174 if (argc == 0) {
175 Usage("No arguments specified");
176 }
177
178 for (int i = 0; i < argc; ++i) {
179 const StringPiece option(argv[i]);
180 const bool log_options = false;
181 if (log_options) {
Calin Juravle876f3502016-03-24 16:16:34 +0000182 LOG(INFO) << "profman: option[" << i << "]=" << argv[i];
Calin Juravle2e2db782016-02-23 12:00:03 +0000183 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700184 if (option == "--dump-only") {
185 dump_only_ = true;
David Sehr7c80f2d2017-02-07 16:47:58 -0800186 } else if (option == "--dump-classes") {
187 dump_classes_ = true;
188 } else if (option.starts_with("--create-profile-from=")) {
189 create_profile_from_file_ = option.substr(strlen("--create-profile-from=")).ToString();
David Sehr4fcdd6d2016-05-24 14:52:31 -0700190 } else if (option.starts_with("--dump-output-to-fd=")) {
191 ParseUintOption(option, "--dump-output-to-fd", &dump_output_to_fd_, Usage);
Calin Juravle876f3502016-03-24 16:16:34 +0000192 } else if (option.starts_with("--profile-file=")) {
Calin Juravle2e2db782016-02-23 12:00:03 +0000193 profile_files_.push_back(option.substr(strlen("--profile-file=")).ToString());
194 } else if (option.starts_with("--profile-file-fd=")) {
195 ParseFdForCollection(option, "--profile-file-fd", &profile_files_fd_);
196 } else if (option.starts_with("--reference-profile-file=")) {
197 reference_profile_file_ = option.substr(strlen("--reference-profile-file=")).ToString();
198 } else if (option.starts_with("--reference-profile-file-fd=")) {
199 ParseUintOption(option, "--reference-profile-file-fd", &reference_profile_file_fd_, Usage);
David Sehr546d24f2016-06-02 10:46:19 -0700200 } else if (option.starts_with("--dex-location=")) {
201 dex_locations_.push_back(option.substr(strlen("--dex-location=")).ToString());
202 } else if (option.starts_with("--apk-fd=")) {
203 ParseFdForCollection(option, "--apk-fd", &apks_fd_);
David Sehr7c80f2d2017-02-07 16:47:58 -0800204 } else if (option.starts_with("--apk=")) {
205 apk_files_.push_back(option.substr(strlen("--apk=")).ToString());
Calin Juravle7bcdb532016-06-07 16:14:47 +0100206 } else if (option.starts_with("--generate-test-profile=")) {
207 test_profile_ = option.substr(strlen("--generate-test-profile=")).ToString();
208 } else if (option.starts_with("--generate-test-profile-num-dex=")) {
209 ParseUintOption(option,
210 "--generate-test-profile-num-dex",
211 &test_profile_num_dex_,
212 Usage);
213 } else if (option.starts_with("--generate-test-profile-method-ratio")) {
214 ParseUintOption(option,
215 "--generate-test-profile-method-ratio",
216 &test_profile_method_ratio_,
217 Usage);
218 } else if (option.starts_with("--generate-test-profile-class-ratio")) {
219 ParseUintOption(option,
220 "--generate-test-profile-class-ratio",
221 &test_profile_class_ratio_,
222 Usage);
Calin Juravle2e2db782016-02-23 12:00:03 +0000223 } else {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700224 Usage("Unknown argument '%s'", option.data());
Calin Juravle2e2db782016-02-23 12:00:03 +0000225 }
226 }
227
David Sehr153da0e2017-02-15 08:54:51 -0800228 // Validate global consistency between file/fd options.
Calin Juravle2e2db782016-02-23 12:00:03 +0000229 if (!profile_files_.empty() && !profile_files_fd_.empty()) {
230 Usage("Profile files should not be specified with both --profile-file-fd and --profile-file");
231 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700232 if (!reference_profile_file_.empty() && FdIsValid(reference_profile_file_fd_)) {
233 Usage("Reference profile should not be specified with both "
234 "--reference-profile-file-fd and --reference-profile-file");
235 }
David Sehr153da0e2017-02-15 08:54:51 -0800236 if (!apk_files_.empty() && !apks_fd_.empty()) {
237 Usage("APK files should not be specified with both --apk-fd and --apk");
Calin Juravle2e2db782016-02-23 12:00:03 +0000238 }
239 }
240
241 ProfileAssistant::ProcessingResult ProcessProfiles() {
David Sehr153da0e2017-02-15 08:54:51 -0800242 // Validate that at least one profile file was passed, as well as a reference profile.
243 if (profile_files_.empty() && profile_files_fd_.empty()) {
244 Usage("No profile files specified.");
245 }
246 if (reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
247 Usage("No reference profile file specified.");
248 }
249 if ((!profile_files_.empty() && FdIsValid(reference_profile_file_fd_)) ||
250 (!profile_files_fd_.empty() && !FdIsValid(reference_profile_file_fd_))) {
251 Usage("Options --profile-file-fd and --reference-profile-file-fd "
252 "should only be used together");
253 }
Calin Juravle2e2db782016-02-23 12:00:03 +0000254 ProfileAssistant::ProcessingResult result;
255 if (profile_files_.empty()) {
256 // The file doesn't need to be flushed here (ProcessProfiles will do it)
257 // so don't check the usage.
258 File file(reference_profile_file_fd_, false);
259 result = ProfileAssistant::ProcessProfiles(profile_files_fd_, reference_profile_file_fd_);
260 CloseAllFds(profile_files_fd_, "profile_files_fd_");
261 } else {
262 result = ProfileAssistant::ProcessProfiles(profile_files_, reference_profile_file_);
263 }
264 return result;
265 }
266
David Sehr7c80f2d2017-02-07 16:47:58 -0800267 void OpenApkFilesFromLocations(std::vector<std::unique_ptr<const DexFile>>* dex_files) {
268 bool use_apk_fd_list = !apks_fd_.empty();
269 if (use_apk_fd_list) {
David Sehr153da0e2017-02-15 08:54:51 -0800270 // Get the APKs from the collection of FDs.
David Sehr7c80f2d2017-02-07 16:47:58 -0800271 CHECK_EQ(dex_locations_.size(), apks_fd_.size());
David Sehr153da0e2017-02-15 08:54:51 -0800272 } else if (!apk_files_.empty()) {
273 // Get the APKs from the collection of filenames.
David Sehr7c80f2d2017-02-07 16:47:58 -0800274 CHECK_EQ(dex_locations_.size(), apk_files_.size());
David Sehr153da0e2017-02-15 08:54:51 -0800275 } else {
276 // No APKs were specified.
277 CHECK(dex_locations_.empty());
278 return;
David Sehr7c80f2d2017-02-07 16:47:58 -0800279 }
280 static constexpr bool kVerifyChecksum = true;
281 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;
284 if (use_apk_fd_list) {
285 if (DexFile::OpenZip(apks_fd_[i],
286 dex_locations_[i],
287 kVerifyChecksum,
288 &error_msg,
289 &dex_files_for_location)) {
290 } else {
291 LOG(WARNING) << "OpenZip failed for '" << dex_locations_[i] << "' " << error_msg;
292 continue;
293 }
294 } else {
295 if (DexFile::Open(apk_files_[i].c_str(),
296 dex_locations_[i],
297 kVerifyChecksum,
298 &error_msg,
299 &dex_files_for_location)) {
300 } else {
301 LOG(WARNING) << "Open failed for '" << dex_locations_[i] << "' " << error_msg;
302 continue;
303 }
304 }
305 for (std::unique_ptr<const DexFile>& dex_file : dex_files_for_location) {
306 dex_files->emplace_back(std::move(dex_file));
307 }
308 }
309 }
310
David Sehrb18991b2017-02-08 20:58:10 -0800311 int DumpOneProfile(const std::string& banner,
312 const std::string& filename,
313 int fd,
314 const std::vector<std::unique_ptr<const DexFile>>* dex_files,
315 std::string* dump) {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700316 if (!filename.empty()) {
317 fd = open(filename.c_str(), O_RDWR);
318 if (fd < 0) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800319 LOG(ERROR) << "Cannot open " << filename << strerror(errno);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700320 return -1;
321 }
Calin Juravle876f3502016-03-24 16:16:34 +0000322 }
323 ProfileCompilationInfo info;
324 if (!info.Load(fd)) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800325 LOG(ERROR) << "Cannot load profile info from fd=" << fd << "\n";
Calin Juravle876f3502016-03-24 16:16:34 +0000326 return -1;
327 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700328 std::string this_dump = banner + "\n" + info.DumpInfo(dex_files) + "\n";
329 *dump += this_dump;
330 if (close(fd) < 0) {
331 PLOG(WARNING) << "Failed to close descriptor";
332 }
333 return 0;
334 }
335
336 int DumpProfileInfo() {
David Sehr153da0e2017-02-15 08:54:51 -0800337 // Validate that at least one profile file or reference was specified.
338 if (profile_files_.empty() && profile_files_fd_.empty() &&
339 reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
340 Usage("No profile files or reference profile specified.");
341 }
David Sehr4fcdd6d2016-05-24 14:52:31 -0700342 static const char* kEmptyString = "";
David Sehr546d24f2016-06-02 10:46:19 -0700343 static const char* kOrdinaryProfile = "=== profile ===";
344 static const char* kReferenceProfile = "=== reference profile ===";
345
346 // Open apk/zip files and and read dex files.
347 MemMap::Init(); // for ZipArchive::OpenFromFd
David Sehrb18991b2017-02-08 20:58:10 -0800348 std::vector<std::unique_ptr<const DexFile>> dex_files;
David Sehr7c80f2d2017-02-07 16:47:58 -0800349 OpenApkFilesFromLocations(&dex_files);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700350 std::string dump;
David Sehr4fcdd6d2016-05-24 14:52:31 -0700351 // Dump individual profile files.
352 if (!profile_files_fd_.empty()) {
353 for (int profile_file_fd : profile_files_fd_) {
David Sehr546d24f2016-06-02 10:46:19 -0700354 int ret = DumpOneProfile(kOrdinaryProfile,
355 kEmptyString,
356 profile_file_fd,
357 &dex_files,
358 &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700359 if (ret != 0) {
360 return ret;
361 }
362 }
363 }
364 if (!profile_files_.empty()) {
365 for (const std::string& profile_file : profile_files_) {
David Sehr546d24f2016-06-02 10:46:19 -0700366 int ret = DumpOneProfile(kOrdinaryProfile, profile_file, kInvalidFd, &dex_files, &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700367 if (ret != 0) {
368 return ret;
369 }
370 }
371 }
372 // Dump reference profile file.
373 if (FdIsValid(reference_profile_file_fd_)) {
David Sehr546d24f2016-06-02 10:46:19 -0700374 int ret = DumpOneProfile(kReferenceProfile,
375 kEmptyString,
376 reference_profile_file_fd_,
377 &dex_files,
378 &dump);
David Sehr4fcdd6d2016-05-24 14:52:31 -0700379 if (ret != 0) {
380 return ret;
381 }
382 }
383 if (!reference_profile_file_.empty()) {
David Sehr546d24f2016-06-02 10:46:19 -0700384 int ret = DumpOneProfile(kReferenceProfile,
385 reference_profile_file_,
386 kInvalidFd,
387 &dex_files,
David Sehr4fcdd6d2016-05-24 14:52:31 -0700388 &dump);
389 if (ret != 0) {
390 return ret;
391 }
392 }
393 if (!FdIsValid(dump_output_to_fd_)) {
394 std::cout << dump;
395 } else {
396 unix_file::FdFile out_fd(dump_output_to_fd_, false /*check_usage*/);
397 if (!out_fd.WriteFully(dump.c_str(), dump.length())) {
398 return -1;
399 }
400 }
Calin Juravle876f3502016-03-24 16:16:34 +0000401 return 0;
402 }
403
404 bool ShouldOnlyDumpProfile() {
David Sehr4fcdd6d2016-05-24 14:52:31 -0700405 return dump_only_;
Calin Juravle876f3502016-03-24 16:16:34 +0000406 }
407
David Sehr7c80f2d2017-02-07 16:47:58 -0800408 bool GetClassNames(int fd,
409 std::vector<std::unique_ptr<const DexFile>>* dex_files,
410 std::set<std::string>* class_names) {
411 ProfileCompilationInfo profile_info;
412 if (!profile_info.Load(fd)) {
413 LOG(ERROR) << "Cannot load profile info";
414 return false;
415 }
416 profile_info.GetClassNames(dex_files, class_names);
417 return true;
418 }
419
420 bool GetClassNames(std::string profile_file,
421 std::vector<std::unique_ptr<const DexFile>>* dex_files,
422 std::set<std::string>* class_names) {
423 int fd = open(profile_file.c_str(), O_RDONLY);
424 if (!FdIsValid(fd)) {
425 LOG(ERROR) << "Cannot open " << profile_file << strerror(errno);
426 return false;
427 }
428 if (!GetClassNames(fd, dex_files, class_names)) {
429 return false;
430 }
431 if (close(fd) < 0) {
432 PLOG(WARNING) << "Failed to close descriptor";
433 }
434 return true;
435 }
436
437 int DumpClasses() {
David Sehr153da0e2017-02-15 08:54:51 -0800438 // Validate that at least one profile file or reference was specified.
439 if (profile_files_.empty() && profile_files_fd_.empty() &&
440 reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
441 Usage("No profile files or reference profile specified.");
442 }
David Sehr7c80f2d2017-02-07 16:47:58 -0800443 // Open apk/zip files and and read dex files.
444 MemMap::Init(); // for ZipArchive::OpenFromFd
445 // Open the dex files to get the names for classes.
446 std::vector<std::unique_ptr<const DexFile>> dex_files;
447 OpenApkFilesFromLocations(&dex_files);
448 // Build a vector of class names from individual profile files.
449 std::set<std::string> class_names;
450 if (!profile_files_fd_.empty()) {
451 for (int profile_file_fd : profile_files_fd_) {
452 if (!GetClassNames(profile_file_fd, &dex_files, &class_names)) {
453 return -1;
454 }
455 }
456 }
457 if (!profile_files_.empty()) {
458 for (const std::string& profile_file : profile_files_) {
459 if (!GetClassNames(profile_file, &dex_files, &class_names)) {
460 return -1;
461 }
462 }
463 }
464 // Concatenate class names from reference profile file.
465 if (FdIsValid(reference_profile_file_fd_)) {
466 if (!GetClassNames(reference_profile_file_fd_, &dex_files, &class_names)) {
467 return -1;
468 }
469 }
470 if (!reference_profile_file_.empty()) {
471 if (!GetClassNames(reference_profile_file_, &dex_files, &class_names)) {
472 return -1;
473 }
474 }
475 // Dump the class names.
476 std::string dump;
477 for (const std::string& class_name : class_names) {
478 dump += class_name + std::string("\n");
479 }
480 if (!FdIsValid(dump_output_to_fd_)) {
481 std::cout << dump;
482 } else {
483 unix_file::FdFile out_fd(dump_output_to_fd_, false /*check_usage*/);
484 if (!out_fd.WriteFully(dump.c_str(), dump.length())) {
485 return -1;
486 }
487 }
488 return 0;
489 }
490
491 bool ShouldOnlyDumpClasses() {
492 return dump_classes_;
493 }
494
495 // Read lines from the given file, dropping comments and empty lines. Post-process each line with
496 // the given function.
497 template <typename T>
498 static T* ReadCommentedInputFromFile(
499 const char* input_filename, std::function<std::string(const char*)>* process) {
500 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
501 if (input_file.get() == nullptr) {
502 LOG(ERROR) << "Failed to open input file " << input_filename;
503 return nullptr;
504 }
505 std::unique_ptr<T> result(
506 ReadCommentedInputStream<T>(*input_file, process));
507 input_file->close();
508 return result.release();
509 }
510
511 // Read lines from the given stream, dropping comments and empty lines. Post-process each line
512 // with the given function.
513 template <typename T>
514 static T* ReadCommentedInputStream(
515 std::istream& in_stream,
516 std::function<std::string(const char*)>* process) {
517 std::unique_ptr<T> output(new T());
518 while (in_stream.good()) {
519 std::string dot;
520 std::getline(in_stream, dot);
521 if (android::base::StartsWith(dot, "#") || dot.empty()) {
522 continue;
523 }
524 if (process != nullptr) {
525 std::string descriptor((*process)(dot.c_str()));
526 output->insert(output->end(), descriptor);
527 } else {
528 output->insert(output->end(), dot);
529 }
530 }
531 return output.release();
532 }
533
Calin Juravlee0ac1152017-02-13 19:03:47 -0800534 // Find class klass_descriptor in the given dex_files and store its reference
535 // in the out parameter class_ref.
536 // Return true if the definition of the class was found in any of the dex_files.
537 bool FindClass(const std::vector<std::unique_ptr<const DexFile>>& dex_files,
538 const std::string& klass_descriptor,
539 /*out*/ProfileMethodInfo::ProfileClassReference* class_ref) {
540 for (const std::unique_ptr<const DexFile>& dex_file_ptr : dex_files) {
541 const DexFile* dex_file = dex_file_ptr.get();
542 const DexFile::TypeId* type_id = dex_file->FindTypeId(klass_descriptor.c_str());
543 if (type_id == nullptr) {
544 continue;
545 }
546 dex::TypeIndex type_index = dex_file->GetIndexForTypeId(*type_id);
547 if (dex_file->FindClassDef(type_index) == nullptr) {
548 // Class is only referenced in the current dex file but not defined in it.
549 continue;
550 }
551 class_ref->dex_file = dex_file;
552 class_ref->type_index = type_index;
553 return true;
554 }
555 return false;
556 }
557
558 // Find the method specified by method_spec in the class class_ref. The method
559 // must have a single INVOKE_VIRTUAL in its byte code.
560 // Upon success it returns true and stores the method index and the invoke dex pc
561 // in the output parameters.
562 // The format of the method spec is "inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;".
563 //
564 // TODO(calin): support INVOKE_INTERFACE and the range variants.
565 bool FindMethodWithSingleInvoke(const ProfileMethodInfo::ProfileClassReference& class_ref,
566 const std::string& method_spec,
567 /*out*/uint16_t* method_index,
568 /*out*/uint32_t* dex_pc) {
569 std::vector<std::string> name_and_signature;
570 Split(method_spec, kProfileParsingFirstCharInSignature, &name_and_signature);
571 if (name_and_signature.size() != 2) {
572 LOG(ERROR) << "Invalid method name and signature " << method_spec;
573 }
574 const std::string& name = name_and_signature[0];
575 const std::string& signature = kProfileParsingFirstCharInSignature + name_and_signature[1];
576 const DexFile* dex_file = class_ref.dex_file;
577
578 const DexFile::StringId* name_id = dex_file->FindStringId(name.c_str());
579 if (name_id == nullptr) {
580 LOG(ERROR) << "Could not find name: " << name;
581 return false;
582 }
583 dex::TypeIndex return_type_idx;
584 std::vector<dex::TypeIndex> param_type_idxs;
585 if (!dex_file->CreateTypeList(signature, &return_type_idx, &param_type_idxs)) {
586 LOG(ERROR) << "Could not create type list" << signature;
587 return false;
588 }
589 const DexFile::ProtoId* proto_id = dex_file->FindProtoId(return_type_idx, param_type_idxs);
590 if (proto_id == nullptr) {
591 LOG(ERROR) << "Could not find proto_id: " << name;
592 return false;
593 }
594 const DexFile::MethodId* method_id = dex_file->FindMethodId(
595 dex_file->GetTypeId(class_ref.type_index), *name_id, *proto_id);
596 if (method_id == nullptr) {
597 LOG(ERROR) << "Could not find method_id: " << name;
598 return false;
599 }
600
601 *method_index = dex_file->GetIndexForMethodId(*method_id);
602
603 uint32_t offset = dex_file->FindCodeItemOffset(
604 *dex_file->FindClassDef(class_ref.type_index),
605 *method_index);
606 const DexFile::CodeItem* code_item = dex_file->GetCodeItem(offset);
607
608 bool found_invoke = false;
609 for (CodeItemIterator it(*code_item); !it.Done(); it.Advance()) {
610 if (it.CurrentInstruction().Opcode() == Instruction::INVOKE_VIRTUAL) {
611 if (found_invoke) {
612 LOG(ERROR) << "Multiple invoke INVOKE_VIRTUAL found: " << name;
613 return false;
614 }
615 found_invoke = true;
616 *dex_pc = it.CurrentDexPc();
617 }
618 }
619 if (!found_invoke) {
620 LOG(ERROR) << "Could not find any INVOKE_VIRTUAL: " << name;
621 }
622 return found_invoke;
623 }
624
625 // Process a line defining a class or a method and its inline caches.
626 // Upon success return true and add the class or the method info to profile.
627 // The format of the method line is:
628 // "LTestInline;->inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;".
629 // The method and classes are searched only in the given dex files.
630 bool ProcessLine(const std::vector<std::unique_ptr<const DexFile>>& dex_files,
631 const std::string& line,
632 /*out*/ProfileCompilationInfo* profile) {
633 std::string klass;
634 std::string method_str;
635 size_t method_sep_index = line.find(kMethodSep);
636 if (method_sep_index == std::string::npos) {
637 klass = line;
638 } else {
639 klass = line.substr(0, method_sep_index);
640 method_str = line.substr(method_sep_index + kMethodSep.size());
641 }
642
643 ProfileMethodInfo::ProfileClassReference class_ref;
644 if (!FindClass(dex_files, klass, &class_ref)) {
645 LOG(ERROR) << "Could not find class: " << klass;
646 return false;
647 }
648
649 if (method_str.empty()) {
650 // No method to add. Just add the class.
651 std::set<DexCacheResolvedClasses> resolved_class_set;
652 const DexFile* dex_file = class_ref.dex_file;
653 const auto& dex_resolved_classes = resolved_class_set.emplace(
654 dex_file->GetLocation(),
655 dex_file->GetBaseLocation(),
656 dex_file->GetLocationChecksum());
657 dex_resolved_classes.first->AddClass(class_ref.type_index);
658 profile->AddMethodsAndClasses(std::vector<ProfileMethodInfo>(), resolved_class_set);
659 return true;
660 }
661
662 // Process the method.
663 std::string method_spec;
664 std::vector<std::string> inline_cache_elems;
665
666 std::vector<std::string> method_elems;
667 Split(method_str, kProfileParsingInlineChacheSep, &method_elems);
668 if (method_elems.size() == 2) {
669 method_spec = method_elems[0];
670 Split(method_elems[1], kProfileParsingTypeSep, &inline_cache_elems);
671 } else if (method_elems.size() == 1) {
672 method_spec = method_elems[0];
673 } else {
674 LOG(ERROR) << "Invalid method line: " << line;
675 return false;
676 }
677
678 uint16_t method_index;
679 uint32_t dex_pc;
680 if (!FindMethodWithSingleInvoke(class_ref, method_spec, &method_index, &dex_pc)) {
681 return false;
682 }
683 std::vector<ProfileMethodInfo::ProfileClassReference> classes(inline_cache_elems.size());
684 size_t class_it = 0;
685 for (const std::string ic_class : inline_cache_elems) {
686 if (!FindClass(dex_files, ic_class, &(classes[class_it++]))) {
687 LOG(ERROR) << "Could not find class: " << ic_class;
688 return false;
689 }
690 }
691 std::vector<ProfileMethodInfo::ProfileInlineCache> inline_caches;
692 inline_caches.emplace_back(dex_pc, classes);
693 std::vector<ProfileMethodInfo> pmi;
694 pmi.emplace_back(class_ref.dex_file, method_index, inline_caches);
695
696 profile->AddMethodsAndClasses(pmi, std::set<DexCacheResolvedClasses>());
697 return true;
698 }
699
700 // Creates a profile from a human friendly textual representation.
701 // The expected input format is:
702 // # Classes
703 // Ljava/lang/Comparable;
704 // Ljava/lang/Math;
705 // # Methods with inline caches
706 // LTestInline;->inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;
707 // LTestInline;->noInlineCache(LSuper;)I
David Sehr7c80f2d2017-02-07 16:47:58 -0800708 int CreateProfile() {
David Sehr153da0e2017-02-15 08:54:51 -0800709 // Validate parameters for this command.
710 if (apk_files_.empty() && apks_fd_.empty()) {
711 Usage("APK files must be specified");
712 }
713 if (dex_locations_.empty()) {
714 Usage("DEX locations must be specified");
715 }
716 if (reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
717 Usage("Reference profile must be specified with --reference-profile-file or "
718 "--reference-profile-file-fd");
719 }
720 if (!profile_files_.empty() || !profile_files_fd_.empty()) {
721 Usage("Profile must be specified with --reference-profile-file or "
722 "--reference-profile-file-fd");
723 }
724 // for ZipArchive::OpenFromFd
725 MemMap::Init();
David Sehr7c80f2d2017-02-07 16:47:58 -0800726 // Open the profile output file if needed.
727 int fd = reference_profile_file_fd_;
728 if (!FdIsValid(fd)) {
729 CHECK(!reference_profile_file_.empty());
730 fd = open(reference_profile_file_.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
731 if (fd < 0) {
732 LOG(ERROR) << "Cannot open " << reference_profile_file_ << strerror(errno);
733 return -1;
734 }
735 }
Calin Juravlee0ac1152017-02-13 19:03:47 -0800736 // Read the user-specified list of classes and methods.
David Sehr7c80f2d2017-02-07 16:47:58 -0800737 std::unique_ptr<std::unordered_set<std::string>>
Calin Juravlee0ac1152017-02-13 19:03:47 -0800738 user_lines(ReadCommentedInputFromFile<std::unordered_set<std::string>>(
David Sehr7c80f2d2017-02-07 16:47:58 -0800739 create_profile_from_file_.c_str(), nullptr)); // No post-processing.
Calin Juravlee0ac1152017-02-13 19:03:47 -0800740
741 // Open the dex files to look up classes and methods.
David Sehr7c80f2d2017-02-07 16:47:58 -0800742 std::vector<std::unique_ptr<const DexFile>> dex_files;
743 OpenApkFilesFromLocations(&dex_files);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800744
745 // Process the lines one by one and add the successful ones to the profile.
David Sehr7c80f2d2017-02-07 16:47:58 -0800746 ProfileCompilationInfo info;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800747
748 for (const auto& line : *user_lines) {
749 ProcessLine(dex_files, line, &info);
750 }
751
David Sehr7c80f2d2017-02-07 16:47:58 -0800752 // Write the profile file.
753 CHECK(info.Save(fd));
754 if (close(fd) < 0) {
755 PLOG(WARNING) << "Failed to close descriptor";
756 }
757 return 0;
758 }
759
760 bool ShouldCreateProfile() {
761 return !create_profile_from_file_.empty();
762 }
763
Calin Juravle7bcdb532016-06-07 16:14:47 +0100764 int GenerateTestProfile() {
David Sehr153da0e2017-02-15 08:54:51 -0800765 // Validate parameters for this command.
766 if (test_profile_method_ratio_ > 100) {
767 Usage("Invalid ratio for --generate-test-profile-method-ratio");
768 }
769 if (test_profile_class_ratio_ > 100) {
770 Usage("Invalid ratio for --generate-test-profile-class-ratio");
771 }
772 // ShouldGenerateTestProfile confirms !test_profile_.empty().
George Burgess IV71f77262016-10-25 13:08:17 -0700773 int profile_test_fd = open(test_profile_.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
Calin Juravle7bcdb532016-06-07 16:14:47 +0100774 if (profile_test_fd < 0) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800775 LOG(ERROR) << "Cannot open " << test_profile_ << strerror(errno);
Calin Juravle7bcdb532016-06-07 16:14:47 +0100776 return -1;
777 }
778
779 bool result = ProfileCompilationInfo::GenerateTestProfile(profile_test_fd,
David Sehr7c80f2d2017-02-07 16:47:58 -0800780 test_profile_num_dex_,
781 test_profile_method_ratio_,
782 test_profile_class_ratio_);
Calin Juravle7bcdb532016-06-07 16:14:47 +0100783 close(profile_test_fd); // ignore close result.
784 return result ? 0 : -1;
785 }
786
787 bool ShouldGenerateTestProfile() {
788 return !test_profile_.empty();
789 }
790
Calin Juravle2e2db782016-02-23 12:00:03 +0000791 private:
792 static void ParseFdForCollection(const StringPiece& option,
793 const char* arg_name,
794 std::vector<int>* fds) {
795 int fd;
796 ParseUintOption(option, arg_name, &fd, Usage);
797 fds->push_back(fd);
798 }
799
800 static void CloseAllFds(const std::vector<int>& fds, const char* descriptor) {
801 for (size_t i = 0; i < fds.size(); i++) {
802 if (close(fds[i]) < 0) {
803 PLOG(WARNING) << "Failed to close descriptor for " << descriptor << " at index " << i;
804 }
805 }
806 }
807
808 void LogCompletionTime() {
David Sehr52683cf2016-05-05 09:02:38 -0700809 static constexpr uint64_t kLogThresholdTime = MsToNs(100); // 100ms
810 uint64_t time_taken = NanoTime() - start_ns_;
David Sehred793012016-05-05 13:39:43 -0700811 if (time_taken > kLogThresholdTime) {
David Sehrd8557182016-05-06 12:29:35 -0700812 LOG(WARNING) << "profman took " << PrettyDuration(time_taken);
David Sehred793012016-05-05 13:39:43 -0700813 }
Calin Juravle2e2db782016-02-23 12:00:03 +0000814 }
815
816 std::vector<std::string> profile_files_;
817 std::vector<int> profile_files_fd_;
David Sehr546d24f2016-06-02 10:46:19 -0700818 std::vector<std::string> dex_locations_;
David Sehr7c80f2d2017-02-07 16:47:58 -0800819 std::vector<std::string> apk_files_;
David Sehr546d24f2016-06-02 10:46:19 -0700820 std::vector<int> apks_fd_;
Calin Juravle2e2db782016-02-23 12:00:03 +0000821 std::string reference_profile_file_;
822 int reference_profile_file_fd_;
David Sehr4fcdd6d2016-05-24 14:52:31 -0700823 bool dump_only_;
David Sehr7c80f2d2017-02-07 16:47:58 -0800824 bool dump_classes_;
David Sehr4fcdd6d2016-05-24 14:52:31 -0700825 int dump_output_to_fd_;
Calin Juravle7bcdb532016-06-07 16:14:47 +0100826 std::string test_profile_;
David Sehr7c80f2d2017-02-07 16:47:58 -0800827 std::string create_profile_from_file_;
Calin Juravle7bcdb532016-06-07 16:14:47 +0100828 uint16_t test_profile_num_dex_;
829 uint16_t test_profile_method_ratio_;
830 uint16_t test_profile_class_ratio_;
Calin Juravle2e2db782016-02-23 12:00:03 +0000831 uint64_t start_ns_;
832};
833
834// See ProfileAssistant::ProcessingResult for return codes.
835static int profman(int argc, char** argv) {
836 ProfMan profman;
837
838 // Parse arguments. Argument mistakes will lead to exit(EXIT_FAILURE) in UsageError.
839 profman.ParseArgs(argc, argv);
840
Calin Juravle7bcdb532016-06-07 16:14:47 +0100841 if (profman.ShouldGenerateTestProfile()) {
842 return profman.GenerateTestProfile();
843 }
Calin Juravle876f3502016-03-24 16:16:34 +0000844 if (profman.ShouldOnlyDumpProfile()) {
845 return profman.DumpProfileInfo();
846 }
David Sehr7c80f2d2017-02-07 16:47:58 -0800847 if (profman.ShouldOnlyDumpClasses()) {
848 return profman.DumpClasses();
849 }
850 if (profman.ShouldCreateProfile()) {
851 return profman.CreateProfile();
852 }
Calin Juravle2e2db782016-02-23 12:00:03 +0000853 // Process profile information and assess if we need to do a profile guided compilation.
854 // This operation involves I/O.
855 return profman.ProcessProfiles();
856}
857
858} // namespace art
859
860int main(int argc, char **argv) {
861 return art::profman(argc, argv);
862}
863