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