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" |
| 36 | #include "utils.h" |
| 37 | #include "profile_assistant.h" |
| 38 | |
| 39 | namespace art { |
| 40 | |
| 41 | static int original_argc; |
| 42 | static char** original_argv; |
| 43 | |
| 44 | static std::string CommandLine() { |
| 45 | std::vector<std::string> command; |
| 46 | for (int i = 0; i < original_argc; ++i) { |
| 47 | command.push_back(original_argv[i]); |
| 48 | } |
| 49 | return Join(command, ' '); |
| 50 | } |
| 51 | |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame^] | 52 | static constexpr int kInvalidFd = -1; |
| 53 | |
| 54 | static bool FdIsValid(int fd) { |
| 55 | return fd != kInvalidFd; |
| 56 | } |
| 57 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 58 | static void UsageErrorV(const char* fmt, va_list ap) { |
| 59 | std::string error; |
| 60 | StringAppendV(&error, fmt, ap); |
| 61 | LOG(ERROR) << error; |
| 62 | } |
| 63 | |
| 64 | static void UsageError(const char* fmt, ...) { |
| 65 | va_list ap; |
| 66 | va_start(ap, fmt); |
| 67 | UsageErrorV(fmt, ap); |
| 68 | va_end(ap); |
| 69 | } |
| 70 | |
| 71 | NO_RETURN static void Usage(const char *fmt, ...) { |
| 72 | va_list ap; |
| 73 | va_start(ap, fmt); |
| 74 | UsageErrorV(fmt, ap); |
| 75 | va_end(ap); |
| 76 | |
| 77 | UsageError("Command: %s", CommandLine().c_str()); |
| 78 | UsageError("Usage: profman [options]..."); |
| 79 | UsageError(""); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame^] | 80 | UsageError(" --dump-only: dumps the content of the specified profile files"); |
| 81 | UsageError(" to standard output (default) in a human readable form."); |
| 82 | UsageError(""); |
| 83 | UsageError(" --dump-output-to-fd=<number>: redirects --dump-info-for output to a file"); |
| 84 | UsageError(" descriptor."); |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 85 | UsageError(""); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 86 | UsageError(" --profile-file=<filename>: specify profiler output file to use for compilation."); |
| 87 | UsageError(" Can be specified multiple time, in which case the data from the different"); |
| 88 | UsageError(" profiles will be aggregated."); |
| 89 | UsageError(""); |
| 90 | UsageError(" --profile-file-fd=<number>: same as --profile-file but accepts a file descriptor."); |
| 91 | UsageError(" Cannot be used together with --profile-file."); |
| 92 | UsageError(""); |
| 93 | UsageError(" --reference-profile-file=<filename>: specify a reference profile."); |
| 94 | UsageError(" The data in this file will be compared with the data obtained by merging"); |
| 95 | UsageError(" all the files specified with --profile-file or --profile-file-fd."); |
| 96 | UsageError(" If the exit code is EXIT_COMPILE then all --profile-file will be merged into"); |
| 97 | UsageError(" --reference-profile-file. "); |
| 98 | UsageError(""); |
| 99 | UsageError(" --reference-profile-file-fd=<number>: same as --reference-profile-file but"); |
| 100 | UsageError(" accepts a file descriptor. Cannot be used together with"); |
| 101 | UsageError(" --reference-profile-file."); |
| 102 | UsageError(""); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame^] | 103 | UsageError(" --code-location=<string>: location string to use with corresponding"); |
| 104 | UsageError(" code-fd to find dex files"); |
| 105 | UsageError(""); |
| 106 | UsageError(" --code-location-fd=<number>: file descriptor containing an open APK to"); |
| 107 | UsageError(" search for dex files"); |
| 108 | UsageError(""); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 109 | |
| 110 | exit(EXIT_FAILURE); |
| 111 | } |
| 112 | |
| 113 | class ProfMan FINAL { |
| 114 | public: |
| 115 | ProfMan() : |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame^] | 116 | reference_profile_file_fd_(kInvalidFd), |
| 117 | dump_only_(false), |
| 118 | dump_output_to_fd_(kInvalidFd), |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 119 | start_ns_(NanoTime()) {} |
| 120 | |
| 121 | ~ProfMan() { |
| 122 | LogCompletionTime(); |
| 123 | } |
| 124 | |
| 125 | void ParseArgs(int argc, char **argv) { |
| 126 | original_argc = argc; |
| 127 | original_argv = argv; |
| 128 | |
| 129 | InitLogging(argv); |
| 130 | |
| 131 | // Skip over the command name. |
| 132 | argv++; |
| 133 | argc--; |
| 134 | |
| 135 | if (argc == 0) { |
| 136 | Usage("No arguments specified"); |
| 137 | } |
| 138 | |
| 139 | for (int i = 0; i < argc; ++i) { |
| 140 | const StringPiece option(argv[i]); |
| 141 | const bool log_options = false; |
| 142 | if (log_options) { |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 143 | LOG(INFO) << "profman: option[" << i << "]=" << argv[i]; |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 144 | } |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame^] | 145 | if (option == "--dump-only") { |
| 146 | dump_only_ = true; |
| 147 | } else if (option.starts_with("--dump-output-to-fd=")) { |
| 148 | ParseUintOption(option, "--dump-output-to-fd", &dump_output_to_fd_, Usage); |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 149 | } else if (option.starts_with("--profile-file=")) { |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 150 | profile_files_.push_back(option.substr(strlen("--profile-file=")).ToString()); |
| 151 | } else if (option.starts_with("--profile-file-fd=")) { |
| 152 | ParseFdForCollection(option, "--profile-file-fd", &profile_files_fd_); |
| 153 | } else if (option.starts_with("--reference-profile-file=")) { |
| 154 | reference_profile_file_ = option.substr(strlen("--reference-profile-file=")).ToString(); |
| 155 | } else if (option.starts_with("--reference-profile-file-fd=")) { |
| 156 | ParseUintOption(option, "--reference-profile-file-fd", &reference_profile_file_fd_, Usage); |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame^] | 157 | } else if (option.starts_with("--code-location=")) { |
| 158 | code_locations_.push_back(option.substr(strlen("--code-location=")).ToString()); |
| 159 | } else if (option.starts_with("--code-location-fd=")) { |
| 160 | ParseFdForCollection(option, "--code-location-fd", &code_locations_fd_); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 161 | } else { |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame^] | 162 | Usage("Unknown argument '%s'", option.data()); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 166 | bool has_profiles = !profile_files_.empty() || !profile_files_fd_.empty(); |
| 167 | bool has_reference_profile = !reference_profile_file_.empty() || |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame^] | 168 | FdIsValid(reference_profile_file_fd_); |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 169 | |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame^] | 170 | // --dump-only may be specified with only --reference-profiles present. |
| 171 | if (!dump_only_ && !has_profiles) { |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 172 | Usage("No profile files specified."); |
| 173 | } |
| 174 | if (!profile_files_.empty() && !profile_files_fd_.empty()) { |
| 175 | Usage("Profile files should not be specified with both --profile-file-fd and --profile-file"); |
| 176 | } |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame^] | 177 | if (!dump_only_ && !has_reference_profile) { |
| 178 | Usage("No reference profile file specified."); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 179 | } |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame^] | 180 | if (!reference_profile_file_.empty() && FdIsValid(reference_profile_file_fd_)) { |
| 181 | Usage("Reference profile should not be specified with both " |
| 182 | "--reference-profile-file-fd and --reference-profile-file"); |
| 183 | } |
| 184 | if ((!profile_files_.empty() && FdIsValid(reference_profile_file_fd_)) || |
| 185 | (!dump_only_ && !profile_files_fd_.empty() && !FdIsValid(reference_profile_file_fd_))) { |
| 186 | Usage("Options --profile-file-fd and --reference-profile-file-fd " |
| 187 | "should only be used together"); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| 191 | ProfileAssistant::ProcessingResult ProcessProfiles() { |
| 192 | ProfileAssistant::ProcessingResult result; |
| 193 | if (profile_files_.empty()) { |
| 194 | // The file doesn't need to be flushed here (ProcessProfiles will do it) |
| 195 | // so don't check the usage. |
| 196 | File file(reference_profile_file_fd_, false); |
| 197 | result = ProfileAssistant::ProcessProfiles(profile_files_fd_, reference_profile_file_fd_); |
| 198 | CloseAllFds(profile_files_fd_, "profile_files_fd_"); |
| 199 | } else { |
| 200 | result = ProfileAssistant::ProcessProfiles(profile_files_, reference_profile_file_); |
| 201 | } |
| 202 | return result; |
| 203 | } |
| 204 | |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame^] | 205 | int DumpOneProfile(const std::string& banner, const std::string& filename, int fd, |
| 206 | const std::vector<const DexFile*>* dex_files, std::string* dump) { |
| 207 | if (!filename.empty()) { |
| 208 | fd = open(filename.c_str(), O_RDWR); |
| 209 | if (fd < 0) { |
| 210 | std::cerr << "Cannot open " << filename << strerror(errno); |
| 211 | return -1; |
| 212 | } |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 213 | } |
| 214 | ProfileCompilationInfo info; |
| 215 | if (!info.Load(fd)) { |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame^] | 216 | std::cerr << "Cannot load profile info from fd=" << fd << "\n"; |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 217 | return -1; |
| 218 | } |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame^] | 219 | std::string this_dump = banner + "\n" + info.DumpInfo(dex_files) + "\n"; |
| 220 | *dump += this_dump; |
| 221 | if (close(fd) < 0) { |
| 222 | PLOG(WARNING) << "Failed to close descriptor"; |
| 223 | } |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | int DumpProfileInfo() { |
| 228 | static const char* kEmptyString = ""; |
| 229 | static const char *kOrdinaryProfile = "=== profile ==="; |
| 230 | static const char *kReferenceProfile = "=== reference profile ==="; |
| 231 | const std::vector<const DexFile*>* dex_files = nullptr; |
| 232 | std::string dump; |
| 233 | |
| 234 | // TODO(sehr): open apk/zip files and and read dex files. |
| 235 | |
| 236 | // Dump individual profile files. |
| 237 | if (!profile_files_fd_.empty()) { |
| 238 | for (int profile_file_fd : profile_files_fd_) { |
| 239 | int ret = DumpOneProfile(kOrdinaryProfile, kEmptyString, profile_file_fd, dex_files, &dump); |
| 240 | if (ret != 0) { |
| 241 | return ret; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | if (!profile_files_.empty()) { |
| 246 | for (const std::string& profile_file : profile_files_) { |
| 247 | int ret = DumpOneProfile(kOrdinaryProfile, profile_file, kInvalidFd, dex_files, &dump); |
| 248 | if (ret != 0) { |
| 249 | return ret; |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | // Dump reference profile file. |
| 254 | if (FdIsValid(reference_profile_file_fd_)) { |
| 255 | int ret = DumpOneProfile(kReferenceProfile, kEmptyString, reference_profile_file_fd_, |
| 256 | dex_files, &dump); |
| 257 | if (ret != 0) { |
| 258 | return ret; |
| 259 | } |
| 260 | } |
| 261 | if (!reference_profile_file_.empty()) { |
| 262 | int ret = DumpOneProfile(kReferenceProfile, reference_profile_file_, kInvalidFd, dex_files, |
| 263 | &dump); |
| 264 | if (ret != 0) { |
| 265 | return ret; |
| 266 | } |
| 267 | } |
| 268 | if (!FdIsValid(dump_output_to_fd_)) { |
| 269 | std::cout << dump; |
| 270 | } else { |
| 271 | unix_file::FdFile out_fd(dump_output_to_fd_, false /*check_usage*/); |
| 272 | if (!out_fd.WriteFully(dump.c_str(), dump.length())) { |
| 273 | return -1; |
| 274 | } |
| 275 | } |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | bool ShouldOnlyDumpProfile() { |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame^] | 280 | return dump_only_; |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 283 | private: |
| 284 | static void ParseFdForCollection(const StringPiece& option, |
| 285 | const char* arg_name, |
| 286 | std::vector<int>* fds) { |
| 287 | int fd; |
| 288 | ParseUintOption(option, arg_name, &fd, Usage); |
| 289 | fds->push_back(fd); |
| 290 | } |
| 291 | |
| 292 | static void CloseAllFds(const std::vector<int>& fds, const char* descriptor) { |
| 293 | for (size_t i = 0; i < fds.size(); i++) { |
| 294 | if (close(fds[i]) < 0) { |
| 295 | PLOG(WARNING) << "Failed to close descriptor for " << descriptor << " at index " << i; |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | void LogCompletionTime() { |
David Sehr | 52683cf | 2016-05-05 09:02:38 -0700 | [diff] [blame] | 301 | static constexpr uint64_t kLogThresholdTime = MsToNs(100); // 100ms |
| 302 | uint64_t time_taken = NanoTime() - start_ns_; |
David Sehr | ed79301 | 2016-05-05 13:39:43 -0700 | [diff] [blame] | 303 | if (time_taken > kLogThresholdTime) { |
David Sehr | d855718 | 2016-05-06 12:29:35 -0700 | [diff] [blame] | 304 | LOG(WARNING) << "profman took " << PrettyDuration(time_taken); |
David Sehr | ed79301 | 2016-05-05 13:39:43 -0700 | [diff] [blame] | 305 | } |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | std::vector<std::string> profile_files_; |
| 309 | std::vector<int> profile_files_fd_; |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame^] | 310 | std::vector<std::string> code_locations_; |
| 311 | std::vector<int> code_locations_fd_; |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 312 | std::string reference_profile_file_; |
| 313 | int reference_profile_file_fd_; |
David Sehr | 4fcdd6d | 2016-05-24 14:52:31 -0700 | [diff] [blame^] | 314 | bool dump_only_; |
| 315 | int dump_output_to_fd_; |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 316 | uint64_t start_ns_; |
| 317 | }; |
| 318 | |
| 319 | // See ProfileAssistant::ProcessingResult for return codes. |
| 320 | static int profman(int argc, char** argv) { |
| 321 | ProfMan profman; |
| 322 | |
| 323 | // Parse arguments. Argument mistakes will lead to exit(EXIT_FAILURE) in UsageError. |
| 324 | profman.ParseArgs(argc, argv); |
| 325 | |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 326 | if (profman.ShouldOnlyDumpProfile()) { |
| 327 | return profman.DumpProfileInfo(); |
| 328 | } |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 329 | // Process profile information and assess if we need to do a profile guided compilation. |
| 330 | // This operation involves I/O. |
| 331 | return profman.ProcessProfiles(); |
| 332 | } |
| 333 | |
| 334 | } // namespace art |
| 335 | |
| 336 | int main(int argc, char **argv) { |
| 337 | return art::profman(argc, argv); |
| 338 | } |
| 339 | |