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 
14 #include "llvm/ADT/SmallSet.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/ProfileData/InstrProfReader.h"
19 #include "llvm/ProfileData/InstrProfWriter.h"
20 #include "llvm/ProfileData/ProfileCommon.h"
21 #include "llvm/ProfileData/SampleProfReader.h"
22 #include "llvm/ProfileData/SampleProfWriter.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Errc.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/Format.h"
27 #include "llvm/Support/ManagedStatic.h"
28 #include "llvm/Support/MemoryBuffer.h"
29 #include "llvm/Support/Path.h"
30 #include "llvm/Support/PrettyStackTrace.h"
31 #include "llvm/Support/Signals.h"
32 #include "llvm/Support/raw_ostream.h"
33 #include <algorithm>
34 
35 using namespace llvm;
36 
37 enum ProfileFormat { PF_None = 0, PF_Text, PF_Binary, PF_GCC };
38 
39 static void exitWithError(const Twine &Message, StringRef Whence = "",
40                           StringRef Hint = "") {
41   errs() << "error: ";
42   if (!Whence.empty())
43     errs() << Whence << ": ";
44   errs() << Message << "\n";
45   if (!Hint.empty())
46     errs() << Hint << "\n";
47   ::exit(1);
48 }
49 
50 static void exitWithErrorCode(const std::error_code &Error,
51                               StringRef Whence = "") {
52   if (Error.category() == instrprof_category()) {
53     instrprof_error instrError = static_cast<instrprof_error>(Error.value());
54     if (instrError == instrprof_error::unrecognized_format) {
55       // Hint for common error of forgetting -sample for sample profiles.
56       exitWithError(Error.message(), Whence,
57                     "Perhaps you forgot to use the -sample option?");
58     }
59   }
60   exitWithError(Error.message(), Whence);
61 }
62 
63 namespace {
64 enum ProfileKinds { instr, sample };
65 }
66 
67 static void handleMergeWriterError(std::error_code &Error,
68                                    StringRef WhenceFile = "",
69                                    StringRef WhenceFunction = "",
70                                    bool ShowHint = true) {
71   if (!WhenceFile.empty())
72     errs() << WhenceFile << ": ";
73   if (!WhenceFunction.empty())
74     errs() << WhenceFunction << ": ";
75   errs() << Error.message() << "\n";
76 
77   if (ShowHint) {
78     StringRef Hint = "";
79     if (Error.category() == instrprof_category()) {
80       instrprof_error instrError = static_cast<instrprof_error>(Error.value());
81       switch (instrError) {
82       case instrprof_error::hash_mismatch:
83       case instrprof_error::count_mismatch:
84       case instrprof_error::value_site_count_mismatch:
85         Hint = "Make sure that all profile data to be merged is generated "
86                "from the same binary.";
87         break;
88       default:
89         break;
90       }
91     }
92 
93     if (!Hint.empty())
94       errs() << Hint << "\n";
95   }
96 }
97 
98 struct WeightedFile {
99   StringRef Filename;
100   uint64_t Weight;
101 
102   WeightedFile() {}
103 
104   WeightedFile(StringRef F, uint64_t W) : Filename{F}, Weight{W} {}
105 };
106 typedef SmallVector<WeightedFile, 5> WeightedFileVector;
107 
108 static void mergeInstrProfile(const WeightedFileVector &Inputs,
109                               StringRef OutputFilename,
110                               ProfileFormat OutputFormat, bool OutputSparse) {
111   if (OutputFilename.compare("-") == 0)
112     exitWithError("Cannot write indexed profdata format to stdout.");
113 
114   if (OutputFormat != PF_Binary && OutputFormat != PF_Text)
115     exitWithError("Unknown format is specified.");
116 
117   std::error_code EC;
118   raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None);
119   if (EC)
120     exitWithErrorCode(EC, OutputFilename);
121 
122   InstrProfWriter Writer(OutputSparse);
123   SmallSet<std::error_code, 4> WriterErrorCodes;
124   for (const auto &Input : Inputs) {
125     auto ReaderOrErr = InstrProfReader::create(Input.Filename);
126     if (std::error_code ec = ReaderOrErr.getError())
127       exitWithErrorCode(ec, Input.Filename);
128 
129     auto Reader = std::move(ReaderOrErr.get());
130     bool IsIRProfile = Reader->isIRLevelProfile();
131     if (Writer.setIsIRLevelProfile(IsIRProfile))
132       exitWithError("Merge IR generated profile with Clang generated profile.");
133 
134     for (auto &I : *Reader) {
135       if (std::error_code EC = Writer.addRecord(std::move(I), Input.Weight)) {
136         // Only show hint the first time an error occurs.
137         bool firstTime = WriterErrorCodes.insert(EC).second;
138         handleMergeWriterError(EC, Input.Filename, I.Name, firstTime);
139       }
140     }
141     if (Reader->hasError())
142       exitWithErrorCode(Reader->getError(), Input.Filename);
143   }
144   if (OutputFormat == PF_Text)
145     Writer.writeText(Output);
146   else
147     Writer.write(Output);
148 }
149 
150 static sampleprof::SampleProfileFormat FormatMap[] = {
151     sampleprof::SPF_None, sampleprof::SPF_Text, sampleprof::SPF_Binary,
152     sampleprof::SPF_GCC};
153 
154 static void mergeSampleProfile(const WeightedFileVector &Inputs,
155                                StringRef OutputFilename,
156                                ProfileFormat OutputFormat) {
157   using namespace sampleprof;
158   auto WriterOrErr =
159       SampleProfileWriter::create(OutputFilename, FormatMap[OutputFormat]);
160   if (std::error_code EC = WriterOrErr.getError())
161     exitWithErrorCode(EC, OutputFilename);
162 
163   auto Writer = std::move(WriterOrErr.get());
164   StringMap<FunctionSamples> ProfileMap;
165   SmallVector<std::unique_ptr<sampleprof::SampleProfileReader>, 5> Readers;
166   LLVMContext Context;
167   for (const auto &Input : Inputs) {
168     auto ReaderOrErr = SampleProfileReader::create(Input.Filename, Context);
169     if (std::error_code EC = ReaderOrErr.getError())
170       exitWithErrorCode(EC, Input.Filename);
171 
172     // We need to keep the readers around until after all the files are
173     // read so that we do not lose the function names stored in each
174     // reader's memory. The function names are needed to write out the
175     // merged profile map.
176     Readers.push_back(std::move(ReaderOrErr.get()));
177     const auto Reader = Readers.back().get();
178     if (std::error_code EC = Reader->read())
179       exitWithErrorCode(EC, Input.Filename);
180 
181     StringMap<FunctionSamples> &Profiles = Reader->getProfiles();
182     for (StringMap<FunctionSamples>::iterator I = Profiles.begin(),
183                                               E = Profiles.end();
184          I != E; ++I) {
185       StringRef FName = I->first();
186       FunctionSamples &Samples = I->second;
187       sampleprof_error Result = ProfileMap[FName].merge(Samples, Input.Weight);
188       if (Result != sampleprof_error::success) {
189         std::error_code EC = make_error_code(Result);
190         handleMergeWriterError(EC, Input.Filename, FName);
191       }
192     }
193   }
194   Writer->write(ProfileMap);
195 }
196 
197 static WeightedFile parseWeightedFile(const StringRef &WeightedFilename) {
198   StringRef WeightStr, FileName;
199   std::tie(WeightStr, FileName) = WeightedFilename.split(',');
200 
201   uint64_t Weight;
202   if (WeightStr.getAsInteger(10, Weight) || Weight < 1)
203     exitWithError("Input weight must be a positive integer.");
204 
205   if (!sys::fs::exists(FileName))
206     exitWithErrorCode(make_error_code(errc::no_such_file_or_directory),
207                       FileName);
208 
209   return WeightedFile(FileName, Weight);
210 }
211 
212 static int merge_main(int argc, const char *argv[]) {
213   cl::list<std::string> InputFilenames(cl::Positional,
214                                        cl::desc("<filename...>"));
215   cl::list<std::string> WeightedInputFilenames("weighted-input",
216                                                cl::desc("<weight>,<filename>"));
217   cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
218                                       cl::init("-"), cl::Required,
219                                       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::opt<ProfileFormat> OutputFormat(
228       cl::desc("Format of output profile"), cl::init(PF_Binary),
229       cl::values(clEnumValN(PF_Binary, "binary", "Binary encoding (default)"),
230                  clEnumValN(PF_Text, "text", "Text encoding"),
231                  clEnumValN(PF_GCC, "gcc",
232                             "GCC encoding (only meaningful for -sample)"),
233                  clEnumValEnd));
234 
235   cl::opt<bool> OutputSparse("sparse", cl::init(false),
236       cl::desc("Generate a sparse profile (only meaningful for -instr)"));
237 
238   cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
239 
240   if (InputFilenames.empty() && WeightedInputFilenames.empty())
241     exitWithError("No input files specified. See " +
242                   sys::path::filename(argv[0]) + " -help");
243 
244   WeightedFileVector WeightedInputs;
245   for (StringRef Filename : InputFilenames)
246     WeightedInputs.push_back(WeightedFile(Filename, 1));
247   for (StringRef WeightedFilename : WeightedInputFilenames)
248     WeightedInputs.push_back(parseWeightedFile(WeightedFilename));
249 
250   if (ProfileKind == instr)
251     mergeInstrProfile(WeightedInputs, OutputFilename, OutputFormat,
252                       OutputSparse);
253   else
254     mergeSampleProfile(WeightedInputs, OutputFilename, OutputFormat);
255 
256   return 0;
257 }
258 
259 static int showInstrProfile(std::string Filename, bool ShowCounts,
260                             bool ShowIndirectCallTargets,
261                             bool ShowDetailedSummary,
262                             std::vector<uint32_t> DetailedSummaryCutoffs,
263                             bool ShowAllFunctions, std::string ShowFunction,
264                             bool TextFormat, raw_fd_ostream &OS) {
265   auto ReaderOrErr = InstrProfReader::create(Filename);
266   std::vector<uint32_t> Cutoffs(DetailedSummaryCutoffs);
267   if (ShowDetailedSummary && DetailedSummaryCutoffs.empty()) {
268     Cutoffs = {800000, 900000, 950000, 990000, 999000, 999900, 999990};
269   }
270   InstrProfSummary PS(Cutoffs);
271   if (std::error_code EC = ReaderOrErr.getError())
272     exitWithErrorCode(EC, Filename);
273 
274   auto Reader = std::move(ReaderOrErr.get());
275   bool IsIRInstr = Reader->isIRLevelProfile();
276   size_t ShownFunctions = 0;
277   for (const auto &Func : *Reader) {
278     bool Show =
279         ShowAllFunctions || (!ShowFunction.empty() &&
280                              Func.Name.find(ShowFunction) != Func.Name.npos);
281 
282     bool doTextFormatDump = (Show && ShowCounts && TextFormat);
283 
284     if (doTextFormatDump) {
285       InstrProfSymtab &Symtab = Reader->getSymtab();
286       InstrProfWriter::writeRecordInText(Func, Symtab, OS);
287       continue;
288     }
289 
290     assert(Func.Counts.size() > 0 && "function missing entry counter");
291     PS.addRecord(Func);
292 
293     if (Show) {
294 
295       if (!ShownFunctions)
296         OS << "Counters:\n";
297 
298       ++ShownFunctions;
299 
300       OS << "  " << Func.Name << ":\n"
301          << "    Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n"
302          << "    Counters: " << Func.Counts.size() << "\n";
303       if (!IsIRInstr)
304         OS << "    Function count: " << Func.Counts[0] << "\n";
305 
306       if (ShowIndirectCallTargets)
307         OS << "    Indirect Call Site Count: "
308            << Func.getNumValueSites(IPVK_IndirectCallTarget) << "\n";
309 
310       if (ShowCounts) {
311         OS << "    Block counts: [";
312         size_t Start = (IsIRInstr ? 0 : 1);
313         for (size_t I = Start, E = Func.Counts.size(); I < E; ++I) {
314           OS << (I == Start ? "" : ", ") << Func.Counts[I];
315         }
316         OS << "]\n";
317       }
318 
319       if (ShowIndirectCallTargets) {
320         InstrProfSymtab &Symtab = Reader->getSymtab();
321         uint32_t NS = Func.getNumValueSites(IPVK_IndirectCallTarget);
322         OS << "    Indirect Target Results: \n";
323         for (size_t I = 0; I < NS; ++I) {
324           uint32_t NV = Func.getNumValueDataForSite(IPVK_IndirectCallTarget, I);
325           std::unique_ptr<InstrProfValueData[]> VD =
326               Func.getValueForSite(IPVK_IndirectCallTarget, I);
327           for (uint32_t V = 0; V < NV; V++) {
328             OS << "\t[ " << I << ", ";
329             OS << Symtab.getFuncName(VD[V].Value) << ", " << VD[V].Count
330                << " ]\n";
331           }
332         }
333       }
334     }
335   }
336 
337   if (Reader->hasError())
338     exitWithErrorCode(Reader->getError(), Filename);
339 
340   if (ShowCounts && TextFormat)
341     return 0;
342 
343   if (ShowAllFunctions || !ShowFunction.empty())
344     OS << "Functions shown: " << ShownFunctions << "\n";
345   OS << "Total functions: " << PS.getNumFunctions() << "\n";
346   OS << "Maximum function count: " << PS.getMaxFunctionCount() << "\n";
347   OS << "Maximum internal block count: " << PS.getMaxInternalBlockCount() << "\n";
348 
349   if (ShowDetailedSummary) {
350     OS << "Detailed summary:\n";
351     OS << "Total number of blocks: " << PS.getNumBlocks() << "\n";
352     OS << "Total count: " << PS.getTotalCount() << "\n";
353     for (auto Entry : PS.getDetailedSummary()) {
354       OS << Entry.NumCounts << " blocks with count >= " << Entry.MinCount
355          << " account for "
356          << format("%0.6g", (float)Entry.Cutoff / ProfileSummary::Scale * 100)
357          << " percentage of the total counts.\n";
358     }
359   }
360   return 0;
361 }
362 
363 static int showSampleProfile(std::string Filename, bool ShowCounts,
364                              bool ShowAllFunctions, std::string ShowFunction,
365                              raw_fd_ostream &OS) {
366   using namespace sampleprof;
367   LLVMContext Context;
368   auto ReaderOrErr = SampleProfileReader::create(Filename, Context);
369   if (std::error_code EC = ReaderOrErr.getError())
370     exitWithErrorCode(EC, Filename);
371 
372   auto Reader = std::move(ReaderOrErr.get());
373   if (std::error_code EC = Reader->read())
374     exitWithErrorCode(EC, Filename);
375 
376   if (ShowAllFunctions || ShowFunction.empty())
377     Reader->dump(OS);
378   else
379     Reader->dumpFunctionProfile(ShowFunction, OS);
380 
381   return 0;
382 }
383 
384 static int show_main(int argc, const char *argv[]) {
385   cl::opt<std::string> Filename(cl::Positional, cl::Required,
386                                 cl::desc("<profdata-file>"));
387 
388   cl::opt<bool> ShowCounts("counts", cl::init(false),
389                            cl::desc("Show counter values for shown functions"));
390   cl::opt<bool> TextFormat(
391       "text", cl::init(false),
392       cl::desc("Show instr profile data in text dump format"));
393   cl::opt<bool> ShowIndirectCallTargets(
394       "ic-targets", cl::init(false),
395       cl::desc("Show indirect call site target values for shown functions"));
396   cl::opt<bool> ShowDetailedSummary("detailed-summary", cl::init(false),
397                                     cl::desc("Show detailed profile summary"));
398   cl::list<uint32_t> DetailedSummaryCutoffs(
399       cl::CommaSeparated, "detailed-summary-cutoffs",
400       cl::desc(
401           "Cutoff percentages (times 10000) for generating detailed summary"),
402       cl::value_desc("800000,901000,999999"));
403   cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false),
404                                  cl::desc("Details for every function"));
405   cl::opt<std::string> ShowFunction("function",
406                                     cl::desc("Details for matching functions"));
407 
408   cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
409                                       cl::init("-"), cl::desc("Output file"));
410   cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
411                             cl::aliasopt(OutputFilename));
412   cl::opt<ProfileKinds> ProfileKind(
413       cl::desc("Profile kind:"), cl::init(instr),
414       cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
415                  clEnumVal(sample, "Sample profile"), clEnumValEnd));
416 
417   cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
418 
419   if (OutputFilename.empty())
420     OutputFilename = "-";
421 
422   std::error_code EC;
423   raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text);
424   if (EC)
425     exitWithErrorCode(EC, OutputFilename);
426 
427   if (ShowAllFunctions && !ShowFunction.empty())
428     errs() << "warning: -function argument ignored: showing all functions\n";
429 
430   std::vector<uint32_t> Cutoffs(DetailedSummaryCutoffs.begin(),
431                                 DetailedSummaryCutoffs.end());
432   if (ProfileKind == instr)
433     return showInstrProfile(Filename, ShowCounts, ShowIndirectCallTargets,
434                             ShowDetailedSummary, DetailedSummaryCutoffs,
435                             ShowAllFunctions, ShowFunction, TextFormat, OS);
436   else
437     return showSampleProfile(Filename, ShowCounts, ShowAllFunctions,
438                              ShowFunction, OS);
439 }
440 
441 int main(int argc, const char *argv[]) {
442   // Print a stack trace if we signal out.
443   sys::PrintStackTraceOnErrorSignal();
444   PrettyStackTraceProgram X(argc, argv);
445   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
446 
447   StringRef ProgName(sys::path::filename(argv[0]));
448   if (argc > 1) {
449     int (*func)(int, const char *[]) = nullptr;
450 
451     if (strcmp(argv[1], "merge") == 0)
452       func = merge_main;
453     else if (strcmp(argv[1], "show") == 0)
454       func = show_main;
455 
456     if (func) {
457       std::string Invocation(ProgName.str() + " " + argv[1]);
458       argv[1] = Invocation.c_str();
459       return func(argc - 1, argv + 1);
460     }
461 
462     if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-help") == 0 ||
463         strcmp(argv[1], "--help") == 0) {
464 
465       errs() << "OVERVIEW: LLVM profile data tools\n\n"
466              << "USAGE: " << ProgName << " <command> [args...]\n"
467              << "USAGE: " << ProgName << " <command> -help\n\n"
468              << "Available commands: merge, show\n";
469       return 0;
470     }
471   }
472 
473   if (argc < 2)
474     errs() << ProgName << ": No command specified!\n";
475   else
476     errs() << ProgName << ": Unknown command!\n";
477 
478   errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";
479   return 1;
480 }
481