1 //===- llvm-profdata.cpp - LLVM profile data tool -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // llvm-profdata merges .profdata files.
12 //===----------------------------------------------------------------------===//
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/ThreadPool.h"
33 #include "llvm/Support/raw_ostream.h"
38 enum ProfileFormat { PF_None = 0, PF_Text, PF_Binary, PF_GCC };
40 static void warn(StringRef Prefix, Twine Message, std::string Whence = "",
41 std::string Hint = "") {
44 errs() << Whence << ": ";
45 errs() << Message << "\n";
47 errs() << Hint << "\n";
50 static void exitWithError(Twine Message, std::string Whence = "",
51 std::string Hint = "") {
52 warn("error: ", Message, Whence, Hint);
56 static void exitWithError(Error E, StringRef Whence = "") {
57 if (E.isA<InstrProfError>()) {
58 handleAllErrors(std::move(E), [&](const InstrProfError &IPE) {
59 instrprof_error instrError = IPE.get();
61 if (instrError == instrprof_error::unrecognized_format) {
62 // Hint for common error of forgetting -sample for sample profiles.
63 Hint = "Perhaps you forgot to use the -sample option?";
65 exitWithError(IPE.message(), Whence, Hint);
69 exitWithError(toString(std::move(E)), Whence);
72 static void exitWithErrorCode(std::error_code EC, StringRef Whence = "") {
73 exitWithError(EC.message(), Whence);
77 enum ProfileKinds { instr, sample };
80 static void handleMergeWriterError(Error E, StringRef WhenceFile = "",
81 StringRef WhenceFunction = "",
82 bool ShowHint = true) {
83 if (!WhenceFile.empty())
84 errs() << WhenceFile << ": ";
85 if (!WhenceFunction.empty())
86 errs() << WhenceFunction << ": ";
88 auto IPE = instrprof_error::success;
89 E = handleErrors(std::move(E),
90 [&IPE](std::unique_ptr<InstrProfError> E) -> Error {
92 return Error(std::move(E));
94 errs() << toString(std::move(E)) << "\n";
98 if (IPE != instrprof_error::success) {
100 case instrprof_error::hash_mismatch:
101 case instrprof_error::count_mismatch:
102 case instrprof_error::value_site_count_mismatch:
103 Hint = "Make sure that all profile data to be merged is generated "
104 "from the same binary.";
112 errs() << Hint << "\n";
116 struct WeightedFile {
117 std::string Filename;
120 typedef SmallVector<WeightedFile, 5> WeightedFileVector;
122 /// Keep track of merged data and reported errors.
123 struct WriterContext {
125 InstrProfWriter Writer;
127 std::string ErrWhence;
129 SmallSet<instrprof_error, 4> &WriterErrorCodes;
131 WriterContext(bool IsSparse, std::mutex &ErrLock,
132 SmallSet<instrprof_error, 4> &WriterErrorCodes)
133 : Lock(), Writer(IsSparse), Err(Error::success()), ErrWhence(""),
134 ErrLock(ErrLock), WriterErrorCodes(WriterErrorCodes) {}
137 /// Determine whether an error is fatal for profile merging.
138 static bool isFatalError(instrprof_error IPE) {
142 case instrprof_error::success:
143 case instrprof_error::eof:
144 case instrprof_error::unknown_function:
145 case instrprof_error::hash_mismatch:
146 case instrprof_error::count_mismatch:
147 case instrprof_error::counter_overflow:
148 case instrprof_error::value_site_count_mismatch:
153 /// Load an input into a writer context.
154 static void loadInput(const WeightedFile &Input, WriterContext *WC) {
155 std::unique_lock<std::mutex> CtxGuard{WC->Lock};
157 // If there's a pending hard error, don't do more work.
161 // Copy the filename, because llvm::ThreadPool copied the input "const
162 // WeightedFile &" by value, making a reference to the filename within it
163 // invalid outside of this packaged task.
164 WC->ErrWhence = Input.Filename;
166 auto ReaderOrErr = InstrProfReader::create(Input.Filename);
167 if (Error E = ReaderOrErr.takeError()) {
168 // Skip the empty profiles by returning sliently.
169 instrprof_error IPE = InstrProfError::take(std::move(E));
170 if (IPE != instrprof_error::empty_raw_profile)
171 WC->Err = make_error<InstrProfError>(IPE);
175 auto Reader = std::move(ReaderOrErr.get());
176 bool IsIRProfile = Reader->isIRLevelProfile();
177 if (WC->Writer.setIsIRLevelProfile(IsIRProfile)) {
178 WC->Err = make_error<StringError>(
179 "Merge IR generated profile with Clang generated profile.",
184 for (auto &I : *Reader) {
185 const StringRef FuncName = I.Name;
186 bool Reported = false;
187 WC->Writer.addRecord(std::move(I), Input.Weight, [&](Error E) {
189 consumeError(std::move(E));
193 // Only show hint the first time an error occurs.
194 instrprof_error IPE = InstrProfError::take(std::move(E));
195 std::unique_lock<std::mutex> ErrGuard{WC->ErrLock};
196 bool firstTime = WC->WriterErrorCodes.insert(IPE).second;
197 handleMergeWriterError(make_error<InstrProfError>(IPE), Input.Filename,
198 FuncName, firstTime);
201 if (Reader->hasError()) {
202 if (Error E = Reader->getError()) {
203 instrprof_error IPE = InstrProfError::take(std::move(E));
204 if (isFatalError(IPE))
205 WC->Err = make_error<InstrProfError>(IPE);
210 /// Merge the \p Src writer context into \p Dst.
211 static void mergeWriterContexts(WriterContext *Dst, WriterContext *Src) {
212 // If we've already seen a hard error, continuing with the merge would
214 if (Dst->Err || Src->Err)
217 bool Reported = false;
218 Dst->Writer.mergeRecordsFromWriter(std::move(Src->Writer), [&](Error E) {
220 consumeError(std::move(E));
224 Dst->Err = std::move(E);
228 static void mergeInstrProfile(const WeightedFileVector &Inputs,
229 StringRef OutputFilename,
230 ProfileFormat OutputFormat, bool OutputSparse,
231 unsigned NumThreads) {
232 if (OutputFilename.compare("-") == 0)
233 exitWithError("Cannot write indexed profdata format to stdout.");
235 if (OutputFormat != PF_Binary && OutputFormat != PF_Text)
236 exitWithError("Unknown format is specified.");
239 raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None);
241 exitWithErrorCode(EC, OutputFilename);
243 std::mutex ErrorLock;
244 SmallSet<instrprof_error, 4> WriterErrorCodes;
246 // If NumThreads is not specified, auto-detect a good default.
249 std::min(hardware_concurrency(), unsigned((Inputs.size() + 1) / 2));
251 // Initialize the writer contexts.
252 SmallVector<std::unique_ptr<WriterContext>, 4> Contexts;
253 for (unsigned I = 0; I < NumThreads; ++I)
254 Contexts.emplace_back(llvm::make_unique<WriterContext>(
255 OutputSparse, ErrorLock, WriterErrorCodes));
257 if (NumThreads == 1) {
258 for (const auto &Input : Inputs)
259 loadInput(Input, Contexts[0].get());
261 ThreadPool Pool(NumThreads);
263 // Load the inputs in parallel (N/NumThreads serial steps).
265 for (const auto &Input : Inputs) {
266 Pool.async(loadInput, Input, Contexts[Ctx].get());
267 Ctx = (Ctx + 1) % NumThreads;
271 // Merge the writer contexts together (~ lg(NumThreads) serial steps).
272 unsigned Mid = Contexts.size() / 2;
273 unsigned End = Contexts.size();
274 assert(Mid > 0 && "Expected more than one context");
276 for (unsigned I = 0; I < Mid; ++I)
277 Pool.async(mergeWriterContexts, Contexts[I].get(),
278 Contexts[I + Mid].get());
281 Pool.async(mergeWriterContexts, Contexts[0].get(),
282 Contexts[End - 1].get());
290 // Handle deferred hard errors encountered during merging.
291 for (std::unique_ptr<WriterContext> &WC : Contexts) {
294 if (!WC->Err.isA<InstrProfError>())
295 exitWithError(std::move(WC->Err), WC->ErrWhence);
297 instrprof_error IPE = InstrProfError::take(std::move(WC->Err));
298 if (isFatalError(IPE))
299 exitWithError(make_error<InstrProfError>(IPE), WC->ErrWhence);
301 warn("warning: ", toString(make_error<InstrProfError>(IPE)),
305 InstrProfWriter &Writer = Contexts[0]->Writer;
306 if (OutputFormat == PF_Text) {
307 if (Error E = Writer.writeText(Output))
308 exitWithError(std::move(E));
310 Writer.write(Output);
314 static sampleprof::SampleProfileFormat FormatMap[] = {
315 sampleprof::SPF_None, sampleprof::SPF_Text, sampleprof::SPF_Binary,
316 sampleprof::SPF_GCC};
318 static void mergeSampleProfile(const WeightedFileVector &Inputs,
319 StringRef OutputFilename,
320 ProfileFormat OutputFormat) {
321 using namespace sampleprof;
323 SampleProfileWriter::create(OutputFilename, FormatMap[OutputFormat]);
324 if (std::error_code EC = WriterOrErr.getError())
325 exitWithErrorCode(EC, OutputFilename);
327 auto Writer = std::move(WriterOrErr.get());
328 StringMap<FunctionSamples> ProfileMap;
329 SmallVector<std::unique_ptr<sampleprof::SampleProfileReader>, 5> Readers;
331 for (const auto &Input : Inputs) {
332 auto ReaderOrErr = SampleProfileReader::create(Input.Filename, Context);
333 if (std::error_code EC = ReaderOrErr.getError())
334 exitWithErrorCode(EC, Input.Filename);
336 // We need to keep the readers around until after all the files are
337 // read so that we do not lose the function names stored in each
338 // reader's memory. The function names are needed to write out the
339 // merged profile map.
340 Readers.push_back(std::move(ReaderOrErr.get()));
341 const auto Reader = Readers.back().get();
342 if (std::error_code EC = Reader->read())
343 exitWithErrorCode(EC, Input.Filename);
345 StringMap<FunctionSamples> &Profiles = Reader->getProfiles();
346 for (StringMap<FunctionSamples>::iterator I = Profiles.begin(),
349 StringRef FName = I->first();
350 FunctionSamples &Samples = I->second;
351 sampleprof_error Result = ProfileMap[FName].merge(Samples, Input.Weight);
352 if (Result != sampleprof_error::success) {
353 std::error_code EC = make_error_code(Result);
354 handleMergeWriterError(errorCodeToError(EC), Input.Filename, FName);
358 Writer->write(ProfileMap);
361 static WeightedFile parseWeightedFile(const StringRef &WeightedFilename) {
362 StringRef WeightStr, FileName;
363 std::tie(WeightStr, FileName) = WeightedFilename.split(',');
366 if (WeightStr.getAsInteger(10, Weight) || Weight < 1)
367 exitWithError("Input weight must be a positive integer.");
369 return {FileName, Weight};
372 static std::unique_ptr<MemoryBuffer>
373 getInputFilenamesFileBuf(const StringRef &InputFilenamesFile) {
374 if (InputFilenamesFile == "")
377 auto BufOrError = MemoryBuffer::getFileOrSTDIN(InputFilenamesFile);
379 exitWithErrorCode(BufOrError.getError(), InputFilenamesFile);
381 return std::move(*BufOrError);
384 static void addWeightedInput(WeightedFileVector &WNI, const WeightedFile &WF) {
385 StringRef Filename = WF.Filename;
386 uint64_t Weight = WF.Weight;
388 // If it's STDIN just pass it on.
389 if (Filename == "-") {
390 WNI.push_back({Filename, Weight});
394 llvm::sys::fs::file_status Status;
395 llvm::sys::fs::status(Filename, Status);
396 if (!llvm::sys::fs::exists(Status))
397 exitWithErrorCode(make_error_code(errc::no_such_file_or_directory),
399 // If it's a source file, collect it.
400 if (llvm::sys::fs::is_regular_file(Status)) {
401 WNI.push_back({Filename, Weight});
405 if (llvm::sys::fs::is_directory(Status)) {
407 for (llvm::sys::fs::recursive_directory_iterator F(Filename, EC), E;
408 F != E && !EC; F.increment(EC)) {
409 if (llvm::sys::fs::is_regular_file(F->path())) {
410 addWeightedInput(WNI, {F->path(), Weight});
414 exitWithErrorCode(EC, Filename);
418 static void parseInputFilenamesFile(MemoryBuffer *Buffer,
419 WeightedFileVector &WFV) {
423 SmallVector<StringRef, 8> Entries;
424 StringRef Data = Buffer->getBuffer();
425 Data.split(Entries, '\n', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
426 for (const StringRef &FileWeightEntry : Entries) {
427 StringRef SanitizedEntry = FileWeightEntry.trim(" \t\v\f\r");
429 if (SanitizedEntry.startswith("#"))
431 // If there's no comma, it's an unweighted profile.
432 else if (SanitizedEntry.find(',') == StringRef::npos)
433 addWeightedInput(WFV, {SanitizedEntry, 1});
435 addWeightedInput(WFV, parseWeightedFile(SanitizedEntry));
439 static int merge_main(int argc, const char *argv[]) {
440 cl::list<std::string> InputFilenames(cl::Positional,
441 cl::desc("<filename...>"));
442 cl::list<std::string> WeightedInputFilenames("weighted-input",
443 cl::desc("<weight>,<filename>"));
444 cl::opt<std::string> InputFilenamesFile(
445 "input-files", cl::init(""),
446 cl::desc("Path to file containing newline-separated "
447 "[<weight>,]<filename> entries"));
448 cl::alias InputFilenamesFileA("f", cl::desc("Alias for --input-files"),
449 cl::aliasopt(InputFilenamesFile));
450 cl::opt<bool> DumpInputFileList(
451 "dump-input-file-list", cl::init(false), cl::Hidden,
452 cl::desc("Dump the list of input files and their weights, then exit"));
453 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
454 cl::init("-"), cl::Required,
455 cl::desc("Output file"));
456 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
457 cl::aliasopt(OutputFilename));
458 cl::opt<ProfileKinds> ProfileKind(
459 cl::desc("Profile kind:"), cl::init(instr),
460 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
461 clEnumVal(sample, "Sample profile")));
462 cl::opt<ProfileFormat> OutputFormat(
463 cl::desc("Format of output profile"), cl::init(PF_Binary),
464 cl::values(clEnumValN(PF_Binary, "binary", "Binary encoding (default)"),
465 clEnumValN(PF_Text, "text", "Text encoding"),
466 clEnumValN(PF_GCC, "gcc",
467 "GCC encoding (only meaningful for -sample)")));
468 cl::opt<bool> OutputSparse("sparse", cl::init(false),
469 cl::desc("Generate a sparse profile (only meaningful for -instr)"));
470 cl::opt<unsigned> NumThreads(
471 "num-threads", cl::init(0),
472 cl::desc("Number of merge threads to use (default: autodetect)"));
473 cl::alias NumThreadsA("j", cl::desc("Alias for --num-threads"),
474 cl::aliasopt(NumThreads));
476 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
478 WeightedFileVector WeightedInputs;
479 for (StringRef Filename : InputFilenames)
480 addWeightedInput(WeightedInputs, {Filename, 1});
481 for (StringRef WeightedFilename : WeightedInputFilenames)
482 addWeightedInput(WeightedInputs, parseWeightedFile(WeightedFilename));
484 // Make sure that the file buffer stays alive for the duration of the
485 // weighted input vector's lifetime.
486 auto Buffer = getInputFilenamesFileBuf(InputFilenamesFile);
487 parseInputFilenamesFile(Buffer.get(), WeightedInputs);
489 if (WeightedInputs.empty())
490 exitWithError("No input files specified. See " +
491 sys::path::filename(argv[0]) + " -help");
493 if (DumpInputFileList) {
494 for (auto &WF : WeightedInputs)
495 outs() << WF.Weight << "," << WF.Filename << "\n";
499 if (ProfileKind == instr)
500 mergeInstrProfile(WeightedInputs, OutputFilename, OutputFormat,
501 OutputSparse, NumThreads);
503 mergeSampleProfile(WeightedInputs, OutputFilename, OutputFormat);
508 typedef struct ValueSitesStats {
510 : TotalNumValueSites(0), TotalNumValueSitesWithValueProfile(0),
512 uint64_t TotalNumValueSites;
513 uint64_t TotalNumValueSitesWithValueProfile;
514 uint64_t TotalNumValues;
515 std::vector<unsigned> ValueSitesHistogram;
518 static void traverseAllValueSites(const InstrProfRecord &Func, uint32_t VK,
519 ValueSitesStats &Stats, raw_fd_ostream &OS,
520 InstrProfSymtab *Symtab) {
521 uint32_t NS = Func.getNumValueSites(VK);
522 Stats.TotalNumValueSites += NS;
523 for (size_t I = 0; I < NS; ++I) {
524 uint32_t NV = Func.getNumValueDataForSite(VK, I);
525 std::unique_ptr<InstrProfValueData[]> VD = Func.getValueForSite(VK, I);
526 Stats.TotalNumValues += NV;
528 Stats.TotalNumValueSitesWithValueProfile++;
529 if (NV > Stats.ValueSitesHistogram.size())
530 Stats.ValueSitesHistogram.resize(NV, 0);
531 Stats.ValueSitesHistogram[NV - 1]++;
533 for (uint32_t V = 0; V < NV; V++) {
534 OS << "\t[ " << I << ", ";
535 if (Symtab == nullptr)
538 OS << Symtab->getFuncName(VD[V].Value);
539 OS << ", " << VD[V].Count << " ]\n";
544 static void showValueSitesStats(raw_fd_ostream &OS, uint32_t VK,
545 ValueSitesStats &Stats) {
546 OS << " Total number of sites: " << Stats.TotalNumValueSites << "\n";
547 OS << " Total number of sites with values: "
548 << Stats.TotalNumValueSitesWithValueProfile << "\n";
549 OS << " Total number of profiled values: " << Stats.TotalNumValues << "\n";
551 OS << " Value sites histogram:\n\tNumTargets, SiteCount\n";
552 for (unsigned I = 0; I < Stats.ValueSitesHistogram.size(); I++) {
553 if (Stats.ValueSitesHistogram[I] > 0)
554 OS << "\t" << I + 1 << ", " << Stats.ValueSitesHistogram[I] << "\n";
558 static int showInstrProfile(const std::string &Filename, bool ShowCounts,
559 uint32_t TopN, bool ShowIndirectCallTargets,
560 bool ShowMemOPSizes, bool ShowDetailedSummary,
561 std::vector<uint32_t> DetailedSummaryCutoffs,
562 bool ShowAllFunctions,
563 const std::string &ShowFunction, bool TextFormat,
564 raw_fd_ostream &OS) {
565 auto ReaderOrErr = InstrProfReader::create(Filename);
566 std::vector<uint32_t> Cutoffs = std::move(DetailedSummaryCutoffs);
567 if (ShowDetailedSummary && Cutoffs.empty()) {
568 Cutoffs = {800000, 900000, 950000, 990000, 999000, 999900, 999990};
570 InstrProfSummaryBuilder Builder(std::move(Cutoffs));
571 if (Error E = ReaderOrErr.takeError())
572 exitWithError(std::move(E), Filename);
574 auto Reader = std::move(ReaderOrErr.get());
575 bool IsIRInstr = Reader->isIRLevelProfile();
576 size_t ShownFunctions = 0;
577 int NumVPKind = IPVK_Last - IPVK_First + 1;
578 std::vector<ValueSitesStats> VPStats(NumVPKind);
580 auto MinCmp = [](const std::pair<std::string, uint64_t> &v1,
581 const std::pair<std::string, uint64_t> &v2) {
582 return v1.second > v2.second;
585 std::priority_queue<std::pair<std::string, uint64_t>,
586 std::vector<std::pair<std::string, uint64_t>>,
588 HottestFuncs(MinCmp);
590 for (const auto &Func : *Reader) {
592 ShowAllFunctions || (!ShowFunction.empty() &&
593 Func.Name.find(ShowFunction) != Func.Name.npos);
595 bool doTextFormatDump = (Show && ShowCounts && TextFormat);
597 if (doTextFormatDump) {
598 InstrProfSymtab &Symtab = Reader->getSymtab();
599 InstrProfWriter::writeRecordInText(Func.Name, Func.Hash, Func, Symtab,
604 assert(Func.Counts.size() > 0 && "function missing entry counter");
605 Builder.addRecord(Func);
608 uint64_t FuncMax = 0;
609 for (size_t I = 0, E = Func.Counts.size(); I < E; ++I)
610 FuncMax = std::max(FuncMax, Func.Counts[I]);
612 if (HottestFuncs.size() == TopN) {
613 if (HottestFuncs.top().second < FuncMax) {
615 HottestFuncs.emplace(std::make_pair(std::string(Func.Name), FuncMax));
618 HottestFuncs.emplace(std::make_pair(std::string(Func.Name), FuncMax));
628 OS << " " << Func.Name << ":\n"
629 << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n"
630 << " Counters: " << Func.Counts.size() << "\n";
632 OS << " Function count: " << Func.Counts[0] << "\n";
634 if (ShowIndirectCallTargets)
635 OS << " Indirect Call Site Count: "
636 << Func.getNumValueSites(IPVK_IndirectCallTarget) << "\n";
638 uint32_t NumMemOPCalls = Func.getNumValueSites(IPVK_MemOPSize);
639 if (ShowMemOPSizes && NumMemOPCalls > 0)
640 OS << " Number of Memory Intrinsics Calls: " << NumMemOPCalls
644 OS << " Block counts: [";
645 size_t Start = (IsIRInstr ? 0 : 1);
646 for (size_t I = Start, E = Func.Counts.size(); I < E; ++I) {
647 OS << (I == Start ? "" : ", ") << Func.Counts[I];
652 if (ShowIndirectCallTargets) {
653 OS << " Indirect Target Results:\n";
654 traverseAllValueSites(Func, IPVK_IndirectCallTarget,
655 VPStats[IPVK_IndirectCallTarget], OS,
656 &(Reader->getSymtab()));
659 if (ShowMemOPSizes && NumMemOPCalls > 0) {
660 OS << " Memory Intrinsic Size Results:\n";
661 traverseAllValueSites(Func, IPVK_MemOPSize, VPStats[IPVK_MemOPSize], OS,
666 if (Reader->hasError())
667 exitWithError(Reader->getError(), Filename);
669 if (ShowCounts && TextFormat)
671 std::unique_ptr<ProfileSummary> PS(Builder.getSummary());
672 OS << "Instrumentation level: "
673 << (Reader->isIRLevelProfile() ? "IR" : "Front-end") << "\n";
674 if (ShowAllFunctions || !ShowFunction.empty())
675 OS << "Functions shown: " << ShownFunctions << "\n";
676 OS << "Total functions: " << PS->getNumFunctions() << "\n";
677 OS << "Maximum function count: " << PS->getMaxFunctionCount() << "\n";
678 OS << "Maximum internal block count: " << PS->getMaxInternalCount() << "\n";
681 std::vector<std::pair<std::string, uint64_t>> SortedHottestFuncs;
682 while (!HottestFuncs.empty()) {
683 SortedHottestFuncs.emplace_back(HottestFuncs.top());
687 << " functions with the largest internal block counts: \n";
688 for (auto &hotfunc : llvm::reverse(SortedHottestFuncs))
689 OS << " " << hotfunc.first << ", max count = " << hotfunc.second << "\n";
692 if (ShownFunctions && ShowIndirectCallTargets) {
693 OS << "Statistics for indirect call sites profile:\n";
694 showValueSitesStats(OS, IPVK_IndirectCallTarget,
695 VPStats[IPVK_IndirectCallTarget]);
698 if (ShownFunctions && ShowMemOPSizes) {
699 OS << "Statistics for memory intrinsic calls sizes profile:\n";
700 showValueSitesStats(OS, IPVK_MemOPSize, VPStats[IPVK_MemOPSize]);
703 if (ShowDetailedSummary) {
704 OS << "Detailed summary:\n";
705 OS << "Total number of blocks: " << PS->getNumCounts() << "\n";
706 OS << "Total count: " << PS->getTotalCount() << "\n";
707 for (auto Entry : PS->getDetailedSummary()) {
708 OS << Entry.NumCounts << " blocks with count >= " << Entry.MinCount
710 << format("%0.6g", (float)Entry.Cutoff / ProfileSummary::Scale * 100)
711 << " percentage of the total counts.\n";
717 static int showSampleProfile(const std::string &Filename, bool ShowCounts,
718 bool ShowAllFunctions,
719 const std::string &ShowFunction,
720 raw_fd_ostream &OS) {
721 using namespace sampleprof;
723 auto ReaderOrErr = SampleProfileReader::create(Filename, Context);
724 if (std::error_code EC = ReaderOrErr.getError())
725 exitWithErrorCode(EC, Filename);
727 auto Reader = std::move(ReaderOrErr.get());
728 if (std::error_code EC = Reader->read())
729 exitWithErrorCode(EC, Filename);
731 if (ShowAllFunctions || ShowFunction.empty())
734 Reader->dumpFunctionProfile(ShowFunction, OS);
739 static int show_main(int argc, const char *argv[]) {
740 cl::opt<std::string> Filename(cl::Positional, cl::Required,
741 cl::desc("<profdata-file>"));
743 cl::opt<bool> ShowCounts("counts", cl::init(false),
744 cl::desc("Show counter values for shown functions"));
745 cl::opt<bool> TextFormat(
746 "text", cl::init(false),
747 cl::desc("Show instr profile data in text dump format"));
748 cl::opt<bool> ShowIndirectCallTargets(
749 "ic-targets", cl::init(false),
750 cl::desc("Show indirect call site target values for shown functions"));
751 cl::opt<bool> ShowMemOPSizes(
752 "memop-sizes", cl::init(false),
753 cl::desc("Show the profiled sizes of the memory intrinsic calls "
754 "for shown functions"));
755 cl::opt<bool> ShowDetailedSummary("detailed-summary", cl::init(false),
756 cl::desc("Show detailed profile summary"));
757 cl::list<uint32_t> DetailedSummaryCutoffs(
758 cl::CommaSeparated, "detailed-summary-cutoffs",
760 "Cutoff percentages (times 10000) for generating detailed summary"),
761 cl::value_desc("800000,901000,999999"));
762 cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false),
763 cl::desc("Details for every function"));
764 cl::opt<std::string> ShowFunction("function",
765 cl::desc("Details for matching functions"));
767 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
768 cl::init("-"), cl::desc("Output file"));
769 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
770 cl::aliasopt(OutputFilename));
771 cl::opt<ProfileKinds> ProfileKind(
772 cl::desc("Profile kind:"), cl::init(instr),
773 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
774 clEnumVal(sample, "Sample profile")));
775 cl::opt<uint32_t> TopNFunctions(
777 cl::desc("Show the list of functions with the largest internal counts"));
779 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
781 if (OutputFilename.empty())
782 OutputFilename = "-";
785 raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text);
787 exitWithErrorCode(EC, OutputFilename);
789 if (ShowAllFunctions && !ShowFunction.empty())
790 errs() << "warning: -function argument ignored: showing all functions\n";
792 std::vector<uint32_t> Cutoffs(DetailedSummaryCutoffs.begin(),
793 DetailedSummaryCutoffs.end());
794 if (ProfileKind == instr)
795 return showInstrProfile(Filename, ShowCounts, TopNFunctions,
796 ShowIndirectCallTargets, ShowMemOPSizes,
797 ShowDetailedSummary, DetailedSummaryCutoffs,
798 ShowAllFunctions, ShowFunction, TextFormat, OS);
800 return showSampleProfile(Filename, ShowCounts, ShowAllFunctions,
804 int main(int argc, const char *argv[]) {
805 // Print a stack trace if we signal out.
806 sys::PrintStackTraceOnErrorSignal(argv[0]);
807 PrettyStackTraceProgram X(argc, argv);
808 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
810 StringRef ProgName(sys::path::filename(argv[0]));
812 int (*func)(int, const char *[]) = nullptr;
814 if (strcmp(argv[1], "merge") == 0)
816 else if (strcmp(argv[1], "show") == 0)
820 std::string Invocation(ProgName.str() + " " + argv[1]);
821 argv[1] = Invocation.c_str();
822 return func(argc - 1, argv + 1);
825 if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-help") == 0 ||
826 strcmp(argv[1], "--help") == 0) {
828 errs() << "OVERVIEW: LLVM profile data tools\n\n"
829 << "USAGE: " << ProgName << " <command> [args...]\n"
830 << "USAGE: " << ProgName << " <command> -help\n\n"
831 << "See each individual command --help for more details.\n"
832 << "Available commands: merge, show\n";
838 errs() << ProgName << ": No command specified!\n";
840 errs() << ProgName << ": Unknown command!\n";
842 errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";