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" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 15 | #include "llvm/IR/LLVMContext.h" |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 16 | #include "llvm/ProfileData/InstrProfReader.h" |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 17 | #include "llvm/ProfileData/InstrProfWriter.h" |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 18 | #include "llvm/ProfileData/SampleProfReader.h" |
| 19 | #include "llvm/ProfileData/SampleProfWriter.h" |
Duncan P. N. Exon Smith | 846a627 | 2014-02-17 23:22:49 +0000 | [diff] [blame] | 20 | #include "llvm/Support/CommandLine.h" |
Benjamin Kramer | d59664f | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 21 | #include "llvm/Support/FileSystem.h" |
Justin Bogner | 423380f | 2014-03-23 20:43:50 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Format.h" |
Duncan P. N. Exon Smith | 846a627 | 2014-02-17 23:22:49 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ManagedStatic.h" |
| 24 | #include "llvm/Support/MemoryBuffer.h" |
Benjamin Kramer | 16132e6 | 2015-03-23 18:07:13 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Path.h" |
Duncan P. N. Exon Smith | 846a627 | 2014-02-17 23:22:49 +0000 | [diff] [blame] | 26 | #include "llvm/Support/PrettyStackTrace.h" |
| 27 | #include "llvm/Support/Signals.h" |
| 28 | #include "llvm/Support/raw_ostream.h" |
| 29 | |
| 30 | using namespace llvm; |
| 31 | |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 32 | static void exitWithError(const Twine &Message, StringRef Whence = "") { |
| 33 | errs() << "error: "; |
| 34 | if (!Whence.empty()) |
| 35 | errs() << Whence << ": "; |
| 36 | errs() << Message << "\n"; |
Duncan P. N. Exon Smith | 846a627 | 2014-02-17 23:22:49 +0000 | [diff] [blame] | 37 | ::exit(1); |
| 38 | } |
| 39 | |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 40 | enum ProfileKinds { instr, sample }; |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 41 | |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 42 | static void mergeInstrProfile(const cl::list<std::string> &Inputs, |
| 43 | StringRef OutputFilename) { |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 44 | if (OutputFilename.compare("-") == 0) |
| 45 | exitWithError("Cannot write indexed profdata format to stdout."); |
Justin Bogner | ec49f98 | 2014-03-12 22:00:57 +0000 | [diff] [blame] | 46 | |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 47 | std::error_code EC; |
| 48 | raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None); |
| 49 | if (EC) |
| 50 | exitWithError(EC.message(), OutputFilename); |
Justin Bogner | ec49f98 | 2014-03-12 22:00:57 +0000 | [diff] [blame] | 51 | |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 52 | InstrProfWriter Writer; |
| 53 | for (const auto &Filename : Inputs) { |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 54 | auto ReaderOrErr = InstrProfReader::create(Filename); |
| 55 | if (std::error_code ec = ReaderOrErr.getError()) |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 56 | exitWithError(ec.message(), Filename); |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 57 | |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 58 | auto Reader = std::move(ReaderOrErr.get()); |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 59 | for (const auto &I : *Reader) |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 60 | if (std::error_code EC = |
| 61 | Writer.addFunctionCounts(I.Name, I.Hash, I.Counts)) |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 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); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 69 | static void mergeSampleProfile(const cl::list<std::string> &Inputs, |
| 70 | StringRef OutputFilename, |
| 71 | sampleprof::SampleProfileFormat OutputFormat) { |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 72 | using namespace sampleprof; |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 73 | auto WriterOrErr = SampleProfileWriter::create(OutputFilename, OutputFormat); |
| 74 | if (std::error_code EC = WriterOrErr.getError()) |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 75 | exitWithError(EC.message(), OutputFilename); |
| 76 | |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 77 | auto Writer = std::move(WriterOrErr.get()); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 78 | StringMap<FunctionSamples> ProfileMap; |
| 79 | for (const auto &Filename : Inputs) { |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 80 | auto ReaderOrErr = |
| 81 | SampleProfileReader::create(Filename, getGlobalContext()); |
| 82 | if (std::error_code EC = ReaderOrErr.getError()) |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 83 | exitWithError(EC.message(), Filename); |
| 84 | |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 85 | auto Reader = std::move(ReaderOrErr.get()); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 86 | if (std::error_code EC = Reader->read()) |
| 87 | exitWithError(EC.message(), Filename); |
| 88 | |
| 89 | StringMap<FunctionSamples> &Profiles = Reader->getProfiles(); |
| 90 | for (StringMap<FunctionSamples>::iterator I = Profiles.begin(), |
| 91 | E = Profiles.end(); |
| 92 | I != E; ++I) { |
| 93 | StringRef FName = I->first(); |
| 94 | FunctionSamples &Samples = I->second; |
| 95 | ProfileMap[FName].merge(Samples); |
| 96 | } |
| 97 | } |
| 98 | Writer->write(ProfileMap); |
| 99 | } |
| 100 | |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 101 | static int merge_main(int argc, const char *argv[]) { |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 102 | cl::list<std::string> Inputs(cl::Positional, cl::Required, cl::OneOrMore, |
| 103 | cl::desc("<filenames...>")); |
| 104 | |
| 105 | cl::opt<std::string> OutputFilename("output", cl::value_desc("output"), |
| 106 | cl::init("-"), cl::Required, |
| 107 | cl::desc("Output file")); |
| 108 | cl::alias OutputFilenameA("o", cl::desc("Alias for --output"), |
| 109 | cl::aliasopt(OutputFilename)); |
| 110 | cl::opt<ProfileKinds> ProfileKind( |
| 111 | cl::desc("Profile kind:"), cl::init(instr), |
| 112 | cl::values(clEnumVal(instr, "Instrumentation profile (default)"), |
| 113 | clEnumVal(sample, "Sample profile"), clEnumValEnd)); |
| 114 | |
| 115 | cl::opt<sampleprof::SampleProfileFormat> OutputFormat( |
| 116 | cl::desc("Format of output profile (only meaningful with --sample)"), |
| 117 | cl::init(sampleprof::SPF_Binary), |
| 118 | cl::values(clEnumValN(sampleprof::SPF_Binary, "binary", |
| 119 | "Binary encoding (default)"), |
| 120 | clEnumValN(sampleprof::SPF_Text, "text", "Text encoding"), |
| 121 | clEnumValN(sampleprof::SPF_GCC, "gcc", "GCC encoding"), |
| 122 | clEnumValEnd)); |
| 123 | |
| 124 | cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n"); |
| 125 | |
| 126 | if (ProfileKind == instr) |
| 127 | mergeInstrProfile(Inputs, OutputFilename); |
| 128 | else |
| 129 | mergeSampleProfile(Inputs, OutputFilename, OutputFormat); |
Justin Bogner | bfee8d4 | 2014-03-12 20:14:17 +0000 | [diff] [blame] | 130 | |
Justin Bogner | ec49f98 | 2014-03-12 22:00:57 +0000 | [diff] [blame] | 131 | return 0; |
Justin Bogner | bfee8d4 | 2014-03-12 20:14:17 +0000 | [diff] [blame] | 132 | } |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 133 | |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 134 | static int showInstrProfile(std::string Filename, bool ShowCounts, |
| 135 | bool ShowAllFunctions, std::string ShowFunction, |
| 136 | raw_fd_ostream &OS) { |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 137 | auto ReaderOrErr = InstrProfReader::create(Filename); |
| 138 | if (std::error_code EC = ReaderOrErr.getError()) |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 139 | exitWithError(EC.message(), Filename); |
| 140 | |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 141 | auto Reader = std::move(ReaderOrErr.get()); |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 142 | uint64_t MaxFunctionCount = 0, MaxBlockCount = 0; |
| 143 | size_t ShownFunctions = 0, TotalFunctions = 0; |
| 144 | for (const auto &Func : *Reader) { |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 145 | bool Show = |
| 146 | ShowAllFunctions || (!ShowFunction.empty() && |
| 147 | Func.Name.find(ShowFunction) != Func.Name.npos); |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 148 | |
| 149 | ++TotalFunctions; |
Justin Bogner | b59d7c7 | 2014-04-25 02:45:33 +0000 | [diff] [blame] | 150 | assert(Func.Counts.size() > 0 && "function missing entry counter"); |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 151 | if (Func.Counts[0] > MaxFunctionCount) |
| 152 | MaxFunctionCount = Func.Counts[0]; |
| 153 | |
| 154 | if (Show) { |
| 155 | if (!ShownFunctions) |
| 156 | OS << "Counters:\n"; |
| 157 | ++ShownFunctions; |
| 158 | |
| 159 | OS << " " << Func.Name << ":\n" |
Justin Bogner | 423380f | 2014-03-23 20:43:50 +0000 | [diff] [blame] | 160 | << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n" |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 161 | << " Counters: " << Func.Counts.size() << "\n" |
| 162 | << " Function count: " << Func.Counts[0] << "\n"; |
| 163 | } |
| 164 | |
| 165 | if (Show && ShowCounts) |
| 166 | OS << " Block counts: ["; |
| 167 | for (size_t I = 1, E = Func.Counts.size(); I < E; ++I) { |
| 168 | if (Func.Counts[I] > MaxBlockCount) |
| 169 | MaxBlockCount = Func.Counts[I]; |
| 170 | if (Show && ShowCounts) |
| 171 | OS << (I == 1 ? "" : ", ") << Func.Counts[I]; |
| 172 | } |
| 173 | if (Show && ShowCounts) |
| 174 | OS << "]\n"; |
| 175 | } |
Justin Bogner | db1225d | 2014-03-23 20:55:53 +0000 | [diff] [blame] | 176 | if (Reader->hasError()) |
| 177 | exitWithError(Reader->getError().message(), Filename); |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 178 | |
| 179 | if (ShowAllFunctions || !ShowFunction.empty()) |
| 180 | OS << "Functions shown: " << ShownFunctions << "\n"; |
| 181 | OS << "Total functions: " << TotalFunctions << "\n"; |
| 182 | OS << "Maximum function count: " << MaxFunctionCount << "\n"; |
| 183 | OS << "Maximum internal block count: " << MaxBlockCount << "\n"; |
| 184 | return 0; |
| 185 | } |
| 186 | |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 187 | static int showSampleProfile(std::string Filename, bool ShowCounts, |
| 188 | bool ShowAllFunctions, std::string ShowFunction, |
| 189 | raw_fd_ostream &OS) { |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 190 | using namespace sampleprof; |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 191 | auto ReaderOrErr = SampleProfileReader::create(Filename, getGlobalContext()); |
| 192 | if (std::error_code EC = ReaderOrErr.getError()) |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 193 | exitWithError(EC.message(), Filename); |
| 194 | |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 195 | auto Reader = std::move(ReaderOrErr.get()); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 196 | Reader->read(); |
| 197 | if (ShowAllFunctions || ShowFunction.empty()) |
| 198 | Reader->dump(OS); |
| 199 | else |
| 200 | Reader->dumpFunctionProfile(ShowFunction, OS); |
| 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 205 | static int show_main(int argc, const char *argv[]) { |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 206 | cl::opt<std::string> Filename(cl::Positional, cl::Required, |
| 207 | cl::desc("<profdata-file>")); |
| 208 | |
| 209 | cl::opt<bool> ShowCounts("counts", cl::init(false), |
| 210 | cl::desc("Show counter values for shown functions")); |
| 211 | cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false), |
| 212 | cl::desc("Details for every function")); |
| 213 | cl::opt<std::string> ShowFunction("function", |
| 214 | cl::desc("Details for matching functions")); |
| 215 | |
| 216 | cl::opt<std::string> OutputFilename("output", cl::value_desc("output"), |
| 217 | cl::init("-"), cl::desc("Output file")); |
| 218 | cl::alias OutputFilenameA("o", cl::desc("Alias for --output"), |
| 219 | cl::aliasopt(OutputFilename)); |
| 220 | cl::opt<ProfileKinds> ProfileKind( |
| 221 | cl::desc("Profile kind:"), cl::init(instr), |
| 222 | cl::values(clEnumVal(instr, "Instrumentation profile (default)"), |
| 223 | clEnumVal(sample, "Sample profile"), clEnumValEnd)); |
| 224 | |
| 225 | cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n"); |
| 226 | |
| 227 | if (OutputFilename.empty()) |
| 228 | OutputFilename = "-"; |
| 229 | |
| 230 | std::error_code EC; |
| 231 | raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text); |
| 232 | if (EC) |
| 233 | exitWithError(EC.message(), OutputFilename); |
| 234 | |
| 235 | if (ShowAllFunctions && !ShowFunction.empty()) |
| 236 | errs() << "warning: -function argument ignored: showing all functions\n"; |
| 237 | |
| 238 | if (ProfileKind == instr) |
| 239 | return showInstrProfile(Filename, ShowCounts, ShowAllFunctions, |
| 240 | ShowFunction, OS); |
| 241 | else |
| 242 | return showSampleProfile(Filename, ShowCounts, ShowAllFunctions, |
| 243 | ShowFunction, OS); |
| 244 | } |
| 245 | |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 246 | int main(int argc, const char *argv[]) { |
| 247 | // Print a stack trace if we signal out. |
| 248 | sys::PrintStackTraceOnErrorSignal(); |
| 249 | PrettyStackTraceProgram X(argc, argv); |
| 250 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 251 | |
| 252 | StringRef ProgName(sys::path::filename(argv[0])); |
| 253 | if (argc > 1) { |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 254 | int (*func)(int, const char *[]) = nullptr; |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 255 | |
| 256 | if (strcmp(argv[1], "merge") == 0) |
| 257 | func = merge_main; |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 258 | else if (strcmp(argv[1], "show") == 0) |
| 259 | func = show_main; |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 260 | |
| 261 | if (func) { |
| 262 | std::string Invocation(ProgName.str() + " " + argv[1]); |
| 263 | argv[1] = Invocation.c_str(); |
| 264 | return func(argc - 1, argv + 1); |
| 265 | } |
| 266 | |
| 267 | if (strcmp(argv[1], "-h") == 0 || |
| 268 | strcmp(argv[1], "-help") == 0 || |
| 269 | strcmp(argv[1], "--help") == 0) { |
| 270 | |
| 271 | errs() << "OVERVIEW: LLVM profile data tools\n\n" |
| 272 | << "USAGE: " << ProgName << " <command> [args...]\n" |
| 273 | << "USAGE: " << ProgName << " <command> -help\n\n" |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 274 | << "Available commands: merge, show\n"; |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 275 | return 0; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | if (argc < 2) |
| 280 | errs() << ProgName << ": No command specified!\n"; |
| 281 | else |
| 282 | errs() << ProgName << ": Unknown command!\n"; |
| 283 | |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 284 | errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n"; |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 285 | return 1; |
| 286 | } |