Duncan P. N. Exon Smith | 846a627 | 2014-02-17 23:22:49 +0000 | [diff] [blame] | 1 | //===- llvm-profdata.cpp - LLVM profile data tool -------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // llvm-profdata merges .profdata files. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Duncan P. N. Exon Smith | 846a627 | 2014-02-17 23:22:49 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringRef.h" |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 15 | #include "llvm/ProfileData/InstrProfReader.h" |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 16 | #include "llvm/ProfileData/InstrProfWriter.h" |
Duncan P. N. Exon Smith | 846a627 | 2014-02-17 23:22:49 +0000 | [diff] [blame] | 17 | #include "llvm/Support/CommandLine.h" |
Benjamin Kramer | d59664f | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 18 | #include "llvm/Support/FileSystem.h" |
Justin Bogner | 423380f | 2014-03-23 20:43:50 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Format.h" |
Duncan P. N. Exon Smith | 846a627 | 2014-02-17 23:22:49 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ManagedStatic.h" |
| 21 | #include "llvm/Support/MemoryBuffer.h" |
| 22 | #include "llvm/Support/PrettyStackTrace.h" |
| 23 | #include "llvm/Support/Signals.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | |
| 26 | using namespace llvm; |
| 27 | |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 28 | static void exitWithError(const Twine &Message, StringRef Whence = "") { |
| 29 | errs() << "error: "; |
| 30 | if (!Whence.empty()) |
| 31 | errs() << Whence << ": "; |
| 32 | errs() << Message << "\n"; |
Duncan P. N. Exon Smith | 846a627 | 2014-02-17 23:22:49 +0000 | [diff] [blame] | 33 | ::exit(1); |
| 34 | } |
| 35 | |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 36 | int merge_main(int argc, const char *argv[]) { |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 37 | cl::list<std::string> Inputs(cl::Positional, cl::Required, cl::OneOrMore, |
| 38 | cl::desc("<filenames...>")); |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 39 | |
| 40 | cl::opt<std::string> OutputFilename("output", cl::value_desc("output"), |
| 41 | cl::init("-"), |
| 42 | cl::desc("Output file")); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 43 | cl::alias OutputFilenameA("o", cl::desc("Alias for --output"), cl::Required, |
| 44 | cl::aliasopt(OutputFilename)); |
Justin Bogner | bfee8d4 | 2014-03-12 20:14:17 +0000 | [diff] [blame] | 45 | |
Justin Bogner | ec49f98 | 2014-03-12 22:00:57 +0000 | [diff] [blame] | 46 | cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n"); |
Justin Bogner | bfee8d4 | 2014-03-12 20:14:17 +0000 | [diff] [blame] | 47 | |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 48 | if (OutputFilename.compare("-") == 0) |
| 49 | exitWithError("Cannot write indexed profdata format to stdout."); |
Justin Bogner | ec49f98 | 2014-03-12 22:00:57 +0000 | [diff] [blame] | 50 | |
| 51 | std::string ErrorInfo; |
NAKAMURA Takumi | 4baaa6a | 2014-03-22 05:38:22 +0000 | [diff] [blame] | 52 | raw_fd_ostream Output(OutputFilename.data(), ErrorInfo, sys::fs::F_None); |
Justin Bogner | ec49f98 | 2014-03-12 22:00:57 +0000 | [diff] [blame] | 53 | if (!ErrorInfo.empty()) |
| 54 | exitWithError(ErrorInfo, OutputFilename); |
| 55 | |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 56 | InstrProfWriter Writer; |
| 57 | for (const auto &Filename : Inputs) { |
| 58 | std::unique_ptr<InstrProfReader> Reader; |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame^] | 59 | if (std::error_code ec = InstrProfReader::create(Filename, Reader)) |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 60 | exitWithError(ec.message(), Filename); |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 61 | |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 62 | for (const auto &I : *Reader) |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame^] | 63 | if (std::error_code EC = |
| 64 | Writer.addFunctionCounts(I.Name, I.Hash, I.Counts)) |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 65 | errs() << Filename << ": " << I.Name << ": " << EC.message() << "\n"; |
| 66 | if (Reader->hasError()) |
| 67 | exitWithError(Reader->getError().message(), Filename); |
Justin Bogner | bfee8d4 | 2014-03-12 20:14:17 +0000 | [diff] [blame] | 68 | } |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 69 | Writer.write(Output); |
Justin Bogner | bfee8d4 | 2014-03-12 20:14:17 +0000 | [diff] [blame] | 70 | |
Justin Bogner | ec49f98 | 2014-03-12 22:00:57 +0000 | [diff] [blame] | 71 | return 0; |
Justin Bogner | bfee8d4 | 2014-03-12 20:14:17 +0000 | [diff] [blame] | 72 | } |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 73 | |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 74 | int show_main(int argc, const char *argv[]) { |
| 75 | cl::opt<std::string> Filename(cl::Positional, cl::Required, |
| 76 | cl::desc("<profdata-file>")); |
| 77 | |
| 78 | cl::opt<bool> ShowCounts("counts", cl::init(false), |
| 79 | cl::desc("Show counter values for shown functions")); |
| 80 | cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false), |
| 81 | cl::desc("Details for every function")); |
| 82 | cl::opt<std::string> ShowFunction("function", |
| 83 | cl::desc("Details for matching functions")); |
| 84 | |
| 85 | cl::opt<std::string> OutputFilename("output", cl::value_desc("output"), |
| 86 | cl::init("-"), |
| 87 | cl::desc("Output file")); |
| 88 | cl::alias OutputFilenameA("o", cl::desc("Alias for --output"), |
| 89 | cl::aliasopt(OutputFilename)); |
| 90 | |
| 91 | cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n"); |
| 92 | |
| 93 | std::unique_ptr<InstrProfReader> Reader; |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame^] | 94 | if (std::error_code EC = InstrProfReader::create(Filename, Reader)) |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 95 | exitWithError(EC.message(), Filename); |
| 96 | |
| 97 | if (OutputFilename.empty()) |
| 98 | OutputFilename = "-"; |
| 99 | |
| 100 | std::string ErrorInfo; |
| 101 | raw_fd_ostream OS(OutputFilename.data(), ErrorInfo, sys::fs::F_Text); |
| 102 | if (!ErrorInfo.empty()) |
| 103 | exitWithError(ErrorInfo, OutputFilename); |
| 104 | |
| 105 | if (ShowAllFunctions && !ShowFunction.empty()) |
| 106 | errs() << "warning: -function argument ignored: showing all functions\n"; |
| 107 | |
| 108 | uint64_t MaxFunctionCount = 0, MaxBlockCount = 0; |
| 109 | size_t ShownFunctions = 0, TotalFunctions = 0; |
| 110 | for (const auto &Func : *Reader) { |
| 111 | bool Show = ShowAllFunctions || |
| 112 | (!ShowFunction.empty() && |
| 113 | Func.Name.find(ShowFunction) != Func.Name.npos); |
| 114 | |
| 115 | ++TotalFunctions; |
Justin Bogner | b59d7c7 | 2014-04-25 02:45:33 +0000 | [diff] [blame] | 116 | assert(Func.Counts.size() > 0 && "function missing entry counter"); |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 117 | if (Func.Counts[0] > MaxFunctionCount) |
| 118 | MaxFunctionCount = Func.Counts[0]; |
| 119 | |
| 120 | if (Show) { |
| 121 | if (!ShownFunctions) |
| 122 | OS << "Counters:\n"; |
| 123 | ++ShownFunctions; |
| 124 | |
| 125 | OS << " " << Func.Name << ":\n" |
Justin Bogner | 423380f | 2014-03-23 20:43:50 +0000 | [diff] [blame] | 126 | << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n" |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 127 | << " Counters: " << Func.Counts.size() << "\n" |
| 128 | << " Function count: " << Func.Counts[0] << "\n"; |
| 129 | } |
| 130 | |
| 131 | if (Show && ShowCounts) |
| 132 | OS << " Block counts: ["; |
| 133 | for (size_t I = 1, E = Func.Counts.size(); I < E; ++I) { |
| 134 | if (Func.Counts[I] > MaxBlockCount) |
| 135 | MaxBlockCount = Func.Counts[I]; |
| 136 | if (Show && ShowCounts) |
| 137 | OS << (I == 1 ? "" : ", ") << Func.Counts[I]; |
| 138 | } |
| 139 | if (Show && ShowCounts) |
| 140 | OS << "]\n"; |
| 141 | } |
Justin Bogner | db1225d | 2014-03-23 20:55:53 +0000 | [diff] [blame] | 142 | if (Reader->hasError()) |
| 143 | exitWithError(Reader->getError().message(), Filename); |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 144 | |
| 145 | if (ShowAllFunctions || !ShowFunction.empty()) |
| 146 | OS << "Functions shown: " << ShownFunctions << "\n"; |
| 147 | OS << "Total functions: " << TotalFunctions << "\n"; |
| 148 | OS << "Maximum function count: " << MaxFunctionCount << "\n"; |
| 149 | OS << "Maximum internal block count: " << MaxBlockCount << "\n"; |
| 150 | return 0; |
| 151 | } |
| 152 | |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 153 | int main(int argc, const char *argv[]) { |
| 154 | // Print a stack trace if we signal out. |
| 155 | sys::PrintStackTraceOnErrorSignal(); |
| 156 | PrettyStackTraceProgram X(argc, argv); |
| 157 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 158 | |
| 159 | StringRef ProgName(sys::path::filename(argv[0])); |
| 160 | if (argc > 1) { |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 161 | int (*func)(int, const char *[]) = nullptr; |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 162 | |
| 163 | if (strcmp(argv[1], "merge") == 0) |
| 164 | func = merge_main; |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 165 | else if (strcmp(argv[1], "show") == 0) |
| 166 | func = show_main; |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 167 | |
| 168 | if (func) { |
| 169 | std::string Invocation(ProgName.str() + " " + argv[1]); |
| 170 | argv[1] = Invocation.c_str(); |
| 171 | return func(argc - 1, argv + 1); |
| 172 | } |
| 173 | |
| 174 | if (strcmp(argv[1], "-h") == 0 || |
| 175 | strcmp(argv[1], "-help") == 0 || |
| 176 | strcmp(argv[1], "--help") == 0) { |
| 177 | |
| 178 | errs() << "OVERVIEW: LLVM profile data tools\n\n" |
| 179 | << "USAGE: " << ProgName << " <command> [args...]\n" |
| 180 | << "USAGE: " << ProgName << " <command> -help\n\n" |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 181 | << "Available commands: merge, show\n"; |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | if (argc < 2) |
| 187 | errs() << ProgName << ": No command specified!\n"; |
| 188 | else |
| 189 | errs() << ProgName << ": Unknown command!\n"; |
| 190 | |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 191 | errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n"; |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 192 | return 1; |
| 193 | } |