Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 1 | /* |
| 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 Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 17 | #include "errno.h" |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <sys/file.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <unistd.h> |
| 23 | |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 24 | #include <iostream> |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 25 | #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 Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 34 | #include "dex_file.h" |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 35 | #include "jit/offline_profiling_info.h" |
David Sehr | f57589f | 2016-10-17 10:09:33 -0700 | [diff] [blame] | 36 | #include "runtime.h" |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 37 | #include "utils.h" |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 38 | #include "zip_archive.h" |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 39 | #include "profile_assistant.h" |
| 40 | |
| 41 | namespace art { |
| 42 | |
| 43 | static int original_argc; |
| 44 | static char** original_argv; |
| 45 | |
| 46 | static 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 Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 54 | static constexpr int kInvalidFd = -1; |
| 55 | |
| 56 | static bool FdIsValid(int fd) { |
| 57 | return fd != kInvalidFd; |
| 58 | } |
| 59 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 60 | static void UsageErrorV(const char* fmt, va_list ap) { |
| 61 | std::string error; |
| 62 | StringAppendV(&error, fmt, ap); |
| 63 | LOG(ERROR) << error; |
| 64 | } |
| 65 | |
| 66 | static void UsageError(const char* fmt, ...) { |
| 67 | va_list ap; |
| 68 | va_start(ap, fmt); |
| 69 | UsageErrorV(fmt, ap); |
| 70 | va_end(ap); |
| 71 | } |
| 72 | |
| 73 | NO_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 Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 82 | 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 Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 87 | UsageError(""); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 88 | 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 Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 104 | 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 Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 112 | UsageError(""); |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 113 | UsageError(" --dex-location=<string>: location string to use with corresponding"); |
| 114 | UsageError(" apk-fd to find dex files"); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 115 | UsageError(""); |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 116 | UsageError(" --apk-fd=<number>: file descriptor containing an open APK to"); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 117 | UsageError(" search for dex files"); |
| 118 | UsageError(""); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 119 | |
| 120 | exit(EXIT_FAILURE); |
| 121 | } |
| 122 | |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 123 | // Note: make sure you update the Usage if you change these values. |
| 124 | static constexpr uint16_t kDefaultTestProfileNumDex = 20; |
| 125 | static constexpr uint16_t kDefaultTestProfileMethodRatio = 5; |
| 126 | static constexpr uint16_t kDefaultTestProfileClassRatio = 5; |
| 127 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 128 | class ProfMan FINAL { |
| 129 | public: |
| 130 | ProfMan() : |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 131 | reference_profile_file_fd_(kInvalidFd), |
| 132 | dump_only_(false), |
| 133 | dump_output_to_fd_(kInvalidFd), |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 134 | test_profile_num_dex_(kDefaultTestProfileNumDex), |
| 135 | test_profile_method_ratio_(kDefaultTestProfileMethodRatio), |
| 136 | test_profile_class_ratio_(kDefaultTestProfileClassRatio), |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 137 | 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 Sehr | f57589f | 2016-10-17 10:09:33 -0700 | [diff] [blame] | 147 | InitLogging(argv, Runtime::Aborter); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 148 | |
| 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 Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 161 | LOG(INFO) << "profman: option[" << i << "]=" << argv[i]; |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 162 | } |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 163 | 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 Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 167 | } else if (option.starts_with("--profile-file=")) { |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 168 | 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 Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 175 | } 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 Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 179 | } 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 Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 196 | } else { |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 197 | Usage("Unknown argument '%s'", option.data()); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 201 | bool has_profiles = !profile_files_.empty() || !profile_files_fd_.empty(); |
| 202 | bool has_reference_profile = !reference_profile_file_.empty() || |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 203 | FdIsValid(reference_profile_file_fd_); |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 204 | |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 205 | 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 Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 214 | // --dump-only may be specified with only --reference-profiles present. |
| 215 | if (!dump_only_ && !has_profiles) { |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 216 | 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 Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 221 | if (!dump_only_ && !has_reference_profile) { |
| 222 | Usage("No reference profile file specified."); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 223 | } |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 224 | 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 Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 232 | } |
| 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 Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 249 | 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 Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 257 | } |
| 258 | ProfileCompilationInfo info; |
| 259 | if (!info.Load(fd)) { |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 260 | std::cerr << "Cannot load profile info from fd=" << fd << "\n"; |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 261 | return -1; |
| 262 | } |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 263 | 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 Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 273 | 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 Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 280 | static constexpr bool kVerifyChecksum = true; |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 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; |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 284 | if (DexFile::OpenZip(apks_fd_[i], |
| 285 | dex_locations_[i], |
| 286 | kVerifyChecksum, |
| 287 | &error_msg, |
| 288 | &dex_files_for_location)) { |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 289 | } 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 Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 298 | std::string dump; |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 299 | // Dump individual profile files. |
| 300 | if (!profile_files_fd_.empty()) { |
| 301 | for (int profile_file_fd : profile_files_fd_) { |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 302 | int ret = DumpOneProfile(kOrdinaryProfile, |
| 303 | kEmptyString, |
| 304 | profile_file_fd, |
| 305 | &dex_files, |
| 306 | &dump); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 307 | if (ret != 0) { |
| 308 | return ret; |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | if (!profile_files_.empty()) { |
| 313 | for (const std::string& profile_file : profile_files_) { |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 314 | int ret = DumpOneProfile(kOrdinaryProfile, profile_file, kInvalidFd, &dex_files, &dump); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 315 | if (ret != 0) { |
| 316 | return ret; |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | // Dump reference profile file. |
| 321 | if (FdIsValid(reference_profile_file_fd_)) { |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 322 | int ret = DumpOneProfile(kReferenceProfile, |
| 323 | kEmptyString, |
| 324 | reference_profile_file_fd_, |
| 325 | &dex_files, |
| 326 | &dump); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 327 | if (ret != 0) { |
| 328 | return ret; |
| 329 | } |
| 330 | } |
| 331 | if (!reference_profile_file_.empty()) { |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 332 | int ret = DumpOneProfile(kReferenceProfile, |
| 333 | reference_profile_file_, |
| 334 | kInvalidFd, |
| 335 | &dex_files, |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 336 | &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 Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | bool ShouldOnlyDumpProfile() { |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 353 | return dump_only_; |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 356 | int GenerateTestProfile() { |
George Burgess IV | 71f7726 | 2016-10-25 13:08:17 -0700 | [diff] [blame^] | 357 | int profile_test_fd = open(test_profile_.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644); |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 358 | 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 Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 375 | 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 Sehr | 52683cf | 2016-05-05 09:02:38 -0700 | [diff] [blame] | 393 | static constexpr uint64_t kLogThresholdTime = MsToNs(100); // 100ms |
| 394 | uint64_t time_taken = NanoTime() - start_ns_; |
David Sehr | ed79301 | 2016-05-05 13:39:43 -0700 | [diff] [blame] | 395 | if (time_taken > kLogThresholdTime) { |
David Sehr | d855718 | 2016-05-06 12:29:35 -0700 | [diff] [blame] | 396 | LOG(WARNING) << "profman took " << PrettyDuration(time_taken); |
David Sehr | ed79301 | 2016-05-05 13:39:43 -0700 | [diff] [blame] | 397 | } |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | std::vector<std::string> profile_files_; |
| 401 | std::vector<int> profile_files_fd_; |
David Sehr | 546d24f | 2016-06-02 10:46:19 -0700 | [diff] [blame] | 402 | std::vector<std::string> dex_locations_; |
| 403 | std::vector<int> apks_fd_; |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 404 | std::string reference_profile_file_; |
| 405 | int reference_profile_file_fd_; |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame] | 406 | bool dump_only_; |
| 407 | int dump_output_to_fd_; |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 408 | 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 Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 412 | uint64_t start_ns_; |
| 413 | }; |
| 414 | |
| 415 | // See ProfileAssistant::ProcessingResult for return codes. |
| 416 | static 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 Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 422 | if (profman.ShouldGenerateTestProfile()) { |
| 423 | return profman.GenerateTestProfile(); |
| 424 | } |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 425 | if (profman.ShouldOnlyDumpProfile()) { |
| 426 | return profman.DumpProfileInfo(); |
| 427 | } |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 428 | // 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 | |
| 435 | int main(int argc, char **argv) { |
| 436 | return art::profman(argc, argv); |
| 437 | } |
| 438 | |