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