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