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 exitWithError(Error E, StringRef Whence = "") {
51   if (E.isA<InstrProfError>()) {
52     handleAllErrors(std::move(E), [&](const InstrProfError &IPE) {
53       instrprof_error instrError = IPE.get();
54       StringRef Hint = "";
55       if (instrError == instrprof_error::unrecognized_format) {
56         // Hint for common error of forgetting -sample for sample profiles.
57         Hint = "Perhaps you forgot to use the -sample option?";
58       }
59       exitWithError(IPE.message(), Whence, Hint);
60     });
61   }
62 
63   exitWithError(toString(std::move(E)), Whence);
64 }
65 
66 static void exitWithErrorCode(std::error_code EC, StringRef Whence = "") {
67   exitWithError(EC.message(), Whence);
68 }
69 
70 namespace {
71 enum ProfileKinds { instr, sample };
72 }
73 
74 static void handleMergeWriterError(Error E, StringRef WhenceFile = "",
75                                    StringRef WhenceFunction = "",
76                                    bool ShowHint = true) {
77   if (!WhenceFile.empty())
78     errs() << WhenceFile << ": ";
79   if (!WhenceFunction.empty())
80     errs() << WhenceFunction << ": ";
81 
82   auto IPE = instrprof_error::success;
83   E = handleErrors(std::move(E),
84                    [&IPE](std::unique_ptr<InstrProfError> E) -> Error {
85                      IPE = E->get();
86                      return Error(std::move(E));
87                    });
88   errs() << toString(std::move(E)) << "\n";
89 
90   if (ShowHint) {
91     StringRef Hint = "";
92     if (IPE != instrprof_error::success) {
93       switch (IPE) {
94       case instrprof_error::hash_mismatch:
95       case instrprof_error::count_mismatch:
96       case instrprof_error::value_site_count_mismatch:
97         Hint = "Make sure that all profile data to be merged is generated "
98                "from the same binary.";
99         break;
100       default:
101         break;
102       }
103     }
104 
105     if (!Hint.empty())
106       errs() << Hint << "\n";
107   }
108 }
109 
110 struct WeightedFile {
111   StringRef Filename;
112   uint64_t Weight;
113 
114   WeightedFile() {}
115 
116   WeightedFile(StringRef F, uint64_t W) : Filename{F}, Weight{W} {}
117 };
118 typedef SmallVector<WeightedFile, 5> WeightedFileVector;
119 
120 static void mergeInstrProfile(const WeightedFileVector &Inputs,
121                               StringRef OutputFilename,
122                               ProfileFormat OutputFormat, bool OutputSparse) {
123   if (OutputFilename.compare("-") == 0)
124     exitWithError("Cannot write indexed profdata format to stdout.");
125 
126   if (OutputFormat != PF_Binary && OutputFormat != PF_Text)
127     exitWithError("Unknown format is specified.");
128 
129   std::error_code EC;
130   raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None);
131   if (EC)
132     exitWithErrorCode(EC, OutputFilename);
133 
134   InstrProfWriter Writer(OutputSparse);
135   SmallSet<instrprof_error, 4> WriterErrorCodes;
136   for (const auto &Input : Inputs) {
137     auto ReaderOrErr = InstrProfReader::create(Input.Filename);
138     if (Error E = ReaderOrErr.takeError())
139       exitWithError(std::move(E), Input.Filename);
140 
141     auto Reader = std::move(ReaderOrErr.get());
142     bool IsIRProfile = Reader->isIRLevelProfile();
143     if (Writer.setIsIRLevelProfile(IsIRProfile))
144       exitWithError("Merge IR generated profile with Clang generated profile.");
145 
146     for (auto &I : *Reader) {
147       if (Error E = Writer.addRecord(std::move(I), Input.Weight)) {
148         // Only show hint the first time an error occurs.
149         instrprof_error IPE = InstrProfError::take(std::move(E));
150         bool firstTime = WriterErrorCodes.insert(IPE).second;
151         handleMergeWriterError(make_error<InstrProfError>(IPE), Input.Filename,
152                                I.Name, firstTime);
153       }
154     }
155     if (Reader->hasError())
156       exitWithError(Reader->getError(), Input.Filename);
157   }
158   if (OutputFormat == PF_Text)
159     Writer.writeText(Output);
160   else
161     Writer.write(Output);
162 }
163 
164 static sampleprof::SampleProfileFormat FormatMap[] = {
165     sampleprof::SPF_None, sampleprof::SPF_Text, sampleprof::SPF_Binary,
166     sampleprof::SPF_GCC};
167 
168 static void mergeSampleProfile(const WeightedFileVector &Inputs,
169                                StringRef OutputFilename,
170                                ProfileFormat OutputFormat) {
171   using namespace sampleprof;
172   auto WriterOrErr =
173       SampleProfileWriter::create(OutputFilename, FormatMap[OutputFormat]);
174   if (std::error_code EC = WriterOrErr.getError())
175     exitWithErrorCode(EC, OutputFilename);
176 
177   auto Writer = std::move(WriterOrErr.get());
178   StringMap<FunctionSamples> ProfileMap;
179   SmallVector<std::unique_ptr<sampleprof::SampleProfileReader>, 5> Readers;
180   LLVMContext Context;
181   for (const auto &Input : Inputs) {
182     auto ReaderOrErr = SampleProfileReader::create(Input.Filename, Context);
183     if (std::error_code EC = ReaderOrErr.getError())
184       exitWithErrorCode(EC, Input.Filename);
185 
186     // We need to keep the readers around until after all the files are
187     // read so that we do not lose the function names stored in each
188     // reader's memory. The function names are needed to write out the
189     // merged profile map.
190     Readers.push_back(std::move(ReaderOrErr.get()));
191     const auto Reader = Readers.back().get();
192     if (std::error_code EC = Reader->read())
193       exitWithErrorCode(EC, Input.Filename);
194 
195     StringMap<FunctionSamples> &Profiles = Reader->getProfiles();
196     for (StringMap<FunctionSamples>::iterator I = Profiles.begin(),
197                                               E = Profiles.end();
198          I != E; ++I) {
199       StringRef FName = I->first();
200       FunctionSamples &Samples = I->second;
201       sampleprof_error Result = ProfileMap[FName].merge(Samples, Input.Weight);
202       if (Result != sampleprof_error::success) {
203         std::error_code EC = make_error_code(Result);
204         handleMergeWriterError(errorCodeToError(EC), Input.Filename, FName);
205       }
206     }
207   }
208   Writer->write(ProfileMap);
209 }
210 
211 static WeightedFile parseWeightedFile(const StringRef &WeightedFilename) {
212   StringRef WeightStr, FileName;
213   std::tie(WeightStr, FileName) = WeightedFilename.split(',');
214 
215   uint64_t Weight;
216   if (WeightStr.getAsInteger(10, Weight) || Weight < 1)
217     exitWithError("Input weight must be a positive integer.");
218 
219   if (!sys::fs::exists(FileName))
220     exitWithErrorCode(make_error_code(errc::no_such_file_or_directory),
221                       FileName);
222 
223   return WeightedFile(FileName, Weight);
224 }
225 
226 static std::unique_ptr<MemoryBuffer>
227 getInputFilenamesFileBuf(const StringRef &InputFilenamesFile) {
228   if (InputFilenamesFile == "")
229     return {};
230 
231   auto BufOrError = MemoryBuffer::getFileOrSTDIN(InputFilenamesFile);
232   if (!BufOrError)
233     exitWithErrorCode(BufOrError.getError(), InputFilenamesFile);
234 
235   return std::move(*BufOrError);
236 }
237 
238 static void parseInputFilenamesFile(MemoryBuffer *Buffer,
239                                     WeightedFileVector &WFV) {
240   if (!Buffer)
241     return;
242 
243   SmallVector<StringRef, 8> Entries;
244   StringRef Data = Buffer->getBuffer();
245   Data.split(Entries, '\n', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
246   for (const StringRef &FileWeightEntry : Entries) {
247     StringRef SanitizedEntry = FileWeightEntry.trim(" \t\v\f\r");
248     // Skip comments.
249     if (SanitizedEntry.startswith("#"))
250       continue;
251     // If there's no comma, it's an unweighted profile.
252     else if (SanitizedEntry.find(',') == StringRef::npos)
253       WFV.emplace_back(SanitizedEntry, 1);
254     else
255       WFV.emplace_back(parseWeightedFile(SanitizedEntry));
256   }
257 }
258 
259 static int merge_main(int argc, const char *argv[]) {
260   cl::list<std::string> InputFilenames(cl::Positional,
261                                        cl::desc("<filename...>"));
262   cl::list<std::string> WeightedInputFilenames("weighted-input",
263                                                cl::desc("<weight>,<filename>"));
264   cl::opt<std::string> InputFilenamesFile(
265       "input-files", cl::init(""),
266       cl::desc("Path to file containing newline-separated "
267                "[<weight>,]<filename> entries"));
268   cl::alias InputFilenamesFileA("f", cl::desc("Alias for --input-files"),
269                                 cl::aliasopt(InputFilenamesFile));
270   cl::opt<bool> DumpInputFileList(
271       "dump-input-file-list", cl::init(false), cl::Hidden,
272       cl::desc("Dump the list of input files and their weights, then exit"));
273   cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
274                                       cl::init("-"), cl::Required,
275                                       cl::desc("Output file"));
276   cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
277                             cl::aliasopt(OutputFilename));
278   cl::opt<ProfileKinds> ProfileKind(
279       cl::desc("Profile kind:"), cl::init(instr),
280       cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
281                  clEnumVal(sample, "Sample profile"), clEnumValEnd));
282   cl::opt<ProfileFormat> OutputFormat(
283       cl::desc("Format of output profile"), cl::init(PF_Binary),
284       cl::values(clEnumValN(PF_Binary, "binary", "Binary encoding (default)"),
285                  clEnumValN(PF_Text, "text", "Text encoding"),
286                  clEnumValN(PF_GCC, "gcc",
287                             "GCC encoding (only meaningful for -sample)"),
288                  clEnumValEnd));
289   cl::opt<bool> OutputSparse("sparse", cl::init(false),
290       cl::desc("Generate a sparse profile (only meaningful for -instr)"));
291 
292   cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
293 
294   WeightedFileVector WeightedInputs;
295   for (StringRef Filename : InputFilenames)
296     WeightedInputs.emplace_back(Filename, 1);
297   for (StringRef WeightedFilename : WeightedInputFilenames)
298     WeightedInputs.emplace_back(parseWeightedFile(WeightedFilename));
299 
300   // Make sure that the file buffer stays alive for the duration of the
301   // weighted input vector's lifetime.
302   auto Buffer = getInputFilenamesFileBuf(InputFilenamesFile);
303   parseInputFilenamesFile(Buffer.get(), WeightedInputs);
304 
305   if (WeightedInputs.empty())
306     exitWithError("No input files specified. See " +
307                   sys::path::filename(argv[0]) + " -help");
308 
309   if (DumpInputFileList) {
310     for (auto &WF : WeightedInputs)
311       outs() << WF.Weight << "," << WF.Filename << "\n";
312     return 0;
313   }
314 
315   if (ProfileKind == instr)
316     mergeInstrProfile(WeightedInputs, OutputFilename, OutputFormat,
317                       OutputSparse);
318   else
319     mergeSampleProfile(WeightedInputs, OutputFilename, OutputFormat);
320 
321   return 0;
322 }
323 
324 static int showInstrProfile(std::string Filename, bool ShowCounts,
325                             bool ShowIndirectCallTargets,
326                             bool ShowDetailedSummary,
327                             std::vector<uint32_t> DetailedSummaryCutoffs,
328                             bool ShowAllFunctions, std::string ShowFunction,
329                             bool TextFormat, raw_fd_ostream &OS) {
330   auto ReaderOrErr = InstrProfReader::create(Filename);
331   std::vector<uint32_t> Cutoffs(DetailedSummaryCutoffs);
332   if (ShowDetailedSummary && DetailedSummaryCutoffs.empty()) {
333     Cutoffs = {800000, 900000, 950000, 990000, 999000, 999900, 999990};
334   }
335   InstrProfSummaryBuilder Builder(Cutoffs);
336   if (Error E = ReaderOrErr.takeError())
337     exitWithError(std::move(E), Filename);
338 
339   auto Reader = std::move(ReaderOrErr.get());
340   bool IsIRInstr = Reader->isIRLevelProfile();
341   size_t ShownFunctions = 0;
342   uint64_t TotalNumValueSites = 0;
343   uint64_t TotalNumValueSitesWithValueProfile = 0;
344   uint64_t TotalNumValues = 0;
345   for (const auto &Func : *Reader) {
346     bool Show =
347         ShowAllFunctions || (!ShowFunction.empty() &&
348                              Func.Name.find(ShowFunction) != Func.Name.npos);
349 
350     bool doTextFormatDump = (Show && ShowCounts && TextFormat);
351 
352     if (doTextFormatDump) {
353       InstrProfSymtab &Symtab = Reader->getSymtab();
354       InstrProfWriter::writeRecordInText(Func, Symtab, OS);
355       continue;
356     }
357 
358     assert(Func.Counts.size() > 0 && "function missing entry counter");
359     Builder.addRecord(Func);
360 
361     if (Show) {
362 
363       if (!ShownFunctions)
364         OS << "Counters:\n";
365 
366       ++ShownFunctions;
367 
368       OS << "  " << Func.Name << ":\n"
369          << "    Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n"
370          << "    Counters: " << Func.Counts.size() << "\n";
371       if (!IsIRInstr)
372         OS << "    Function count: " << Func.Counts[0] << "\n";
373 
374       if (ShowIndirectCallTargets)
375         OS << "    Indirect Call Site Count: "
376            << Func.getNumValueSites(IPVK_IndirectCallTarget) << "\n";
377 
378       if (ShowCounts) {
379         OS << "    Block counts: [";
380         size_t Start = (IsIRInstr ? 0 : 1);
381         for (size_t I = Start, E = Func.Counts.size(); I < E; ++I) {
382           OS << (I == Start ? "" : ", ") << Func.Counts[I];
383         }
384         OS << "]\n";
385       }
386 
387       if (ShowIndirectCallTargets) {
388         InstrProfSymtab &Symtab = Reader->getSymtab();
389         uint32_t NS = Func.getNumValueSites(IPVK_IndirectCallTarget);
390         OS << "    Indirect Target Results: \n";
391         TotalNumValueSites += NS;
392         for (size_t I = 0; I < NS; ++I) {
393           uint32_t NV = Func.getNumValueDataForSite(IPVK_IndirectCallTarget, I);
394           std::unique_ptr<InstrProfValueData[]> VD =
395               Func.getValueForSite(IPVK_IndirectCallTarget, I);
396           TotalNumValues += NV;
397           if (NV)
398             TotalNumValueSitesWithValueProfile++;
399           for (uint32_t V = 0; V < NV; V++) {
400             OS << "\t[ " << I << ", ";
401             OS << Symtab.getFuncName(VD[V].Value) << ", " << VD[V].Count
402                << " ]\n";
403           }
404         }
405       }
406     }
407   }
408   if (Reader->hasError())
409     exitWithError(Reader->getError(), Filename);
410 
411   if (ShowCounts && TextFormat)
412     return 0;
413   std::unique_ptr<ProfileSummary> PS(Builder.getSummary());
414   if (ShowAllFunctions || !ShowFunction.empty())
415     OS << "Functions shown: " << ShownFunctions << "\n";
416   OS << "Total functions: " << PS->getNumFunctions() << "\n";
417   OS << "Maximum function count: " << PS->getMaxFunctionCount() << "\n";
418   OS << "Maximum internal block count: " << PS->getMaxInternalCount() << "\n";
419   if (ShownFunctions && ShowIndirectCallTargets) {
420     OS << "Total Number of Indirect Call Sites : " << TotalNumValueSites
421        << "\n";
422     OS << "Total Number of Sites With Values : "
423        << TotalNumValueSitesWithValueProfile << "\n";
424     OS << "Total Number of Profiled Values : " << TotalNumValues << "\n";
425   }
426 
427   if (ShowDetailedSummary) {
428     OS << "Detailed summary:\n";
429     OS << "Total number of blocks: " << PS->getNumCounts() << "\n";
430     OS << "Total count: " << PS->getTotalCount() << "\n";
431     for (auto Entry : PS->getDetailedSummary()) {
432       OS << Entry.NumCounts << " blocks with count >= " << Entry.MinCount
433          << " account for "
434          << format("%0.6g", (float)Entry.Cutoff / ProfileSummary::Scale * 100)
435          << " percentage of the total counts.\n";
436     }
437   }
438   return 0;
439 }
440 
441 static int showSampleProfile(std::string Filename, bool ShowCounts,
442                              bool ShowAllFunctions, std::string ShowFunction,
443                              raw_fd_ostream &OS) {
444   using namespace sampleprof;
445   LLVMContext Context;
446   auto ReaderOrErr = SampleProfileReader::create(Filename, Context);
447   if (std::error_code EC = ReaderOrErr.getError())
448     exitWithErrorCode(EC, Filename);
449 
450   auto Reader = std::move(ReaderOrErr.get());
451   if (std::error_code EC = Reader->read())
452     exitWithErrorCode(EC, Filename);
453 
454   if (ShowAllFunctions || ShowFunction.empty())
455     Reader->dump(OS);
456   else
457     Reader->dumpFunctionProfile(ShowFunction, OS);
458 
459   return 0;
460 }
461 
462 static int show_main(int argc, const char *argv[]) {
463   cl::opt<std::string> Filename(cl::Positional, cl::Required,
464                                 cl::desc("<profdata-file>"));
465 
466   cl::opt<bool> ShowCounts("counts", cl::init(false),
467                            cl::desc("Show counter values for shown functions"));
468   cl::opt<bool> TextFormat(
469       "text", cl::init(false),
470       cl::desc("Show instr profile data in text dump format"));
471   cl::opt<bool> ShowIndirectCallTargets(
472       "ic-targets", cl::init(false),
473       cl::desc("Show indirect call site target values for shown functions"));
474   cl::opt<bool> ShowDetailedSummary("detailed-summary", cl::init(false),
475                                     cl::desc("Show detailed profile summary"));
476   cl::list<uint32_t> DetailedSummaryCutoffs(
477       cl::CommaSeparated, "detailed-summary-cutoffs",
478       cl::desc(
479           "Cutoff percentages (times 10000) for generating detailed summary"),
480       cl::value_desc("800000,901000,999999"));
481   cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false),
482                                  cl::desc("Details for every function"));
483   cl::opt<std::string> ShowFunction("function",
484                                     cl::desc("Details for matching functions"));
485 
486   cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
487                                       cl::init("-"), cl::desc("Output file"));
488   cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
489                             cl::aliasopt(OutputFilename));
490   cl::opt<ProfileKinds> ProfileKind(
491       cl::desc("Profile kind:"), cl::init(instr),
492       cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
493                  clEnumVal(sample, "Sample profile"), clEnumValEnd));
494 
495   cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
496 
497   if (OutputFilename.empty())
498     OutputFilename = "-";
499 
500   std::error_code EC;
501   raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text);
502   if (EC)
503     exitWithErrorCode(EC, OutputFilename);
504 
505   if (ShowAllFunctions && !ShowFunction.empty())
506     errs() << "warning: -function argument ignored: showing all functions\n";
507 
508   std::vector<uint32_t> Cutoffs(DetailedSummaryCutoffs.begin(),
509                                 DetailedSummaryCutoffs.end());
510   if (ProfileKind == instr)
511     return showInstrProfile(Filename, ShowCounts, ShowIndirectCallTargets,
512                             ShowDetailedSummary, DetailedSummaryCutoffs,
513                             ShowAllFunctions, ShowFunction, TextFormat, OS);
514   else
515     return showSampleProfile(Filename, ShowCounts, ShowAllFunctions,
516                              ShowFunction, OS);
517 }
518 
519 int main(int argc, const char *argv[]) {
520   // Print a stack trace if we signal out.
521   sys::PrintStackTraceOnErrorSignal();
522   PrettyStackTraceProgram X(argc, argv);
523   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
524 
525   StringRef ProgName(sys::path::filename(argv[0]));
526   if (argc > 1) {
527     int (*func)(int, const char *[]) = nullptr;
528 
529     if (strcmp(argv[1], "merge") == 0)
530       func = merge_main;
531     else if (strcmp(argv[1], "show") == 0)
532       func = show_main;
533 
534     if (func) {
535       std::string Invocation(ProgName.str() + " " + argv[1]);
536       argv[1] = Invocation.c_str();
537       return func(argc - 1, argv + 1);
538     }
539 
540     if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-help") == 0 ||
541         strcmp(argv[1], "--help") == 0) {
542 
543       errs() << "OVERVIEW: LLVM profile data tools\n\n"
544              << "USAGE: " << ProgName << " <command> [args...]\n"
545              << "USAGE: " << ProgName << " <command> -help\n\n"
546              << "Available commands: merge, show\n";
547       return 0;
548     }
549   }
550 
551   if (argc < 2)
552     errs() << ProgName << ": No command specified!\n";
553   else
554     errs() << ProgName << ": Unknown command!\n";
555 
556   errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";
557   return 1;
558 }
559