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/InitLLVM.h"
28 #include "llvm/Support/MemoryBuffer.h"
29 #include "llvm/Support/Path.h"
30 #include "llvm/Support/WithColor.h"
31 #include "llvm/Support/ThreadPool.h"
32 #include "llvm/Support/raw_ostream.h"
45 static void warn(Twine Message, std::string Whence = "",
46 std::string Hint = "") {
49 errs() << Whence << ": ";
50 errs() << Message << "\n";
52 WithColor::note() << Hint << "\n";
55 static void exitWithError(Twine Message, std::string Whence = "",
56 std::string Hint = "") {
59 errs() << Whence << ": ";
60 errs() << Message << "\n";
62 WithColor::note() << Hint << "\n";
66 static void exitWithError(Error E, StringRef Whence = "") {
67 if (E.isA<InstrProfError>()) {
68 handleAllErrors(std::move(E), [&](const InstrProfError &IPE) {
69 instrprof_error instrError = IPE.get();
71 if (instrError == instrprof_error::unrecognized_format) {
72 // Hint for common error of forgetting -sample for sample profiles.
73 Hint = "Perhaps you forgot to use the -sample option?";
75 exitWithError(IPE.message(), Whence, Hint);
79 exitWithError(toString(std::move(E)), Whence);
82 static void exitWithErrorCode(std::error_code EC, StringRef Whence = "") {
83 exitWithError(EC.message(), Whence);
87 enum ProfileKinds { instr, sample };
90 static void handleMergeWriterError(Error E, StringRef WhenceFile = "",
91 StringRef WhenceFunction = "",
92 bool ShowHint = true) {
93 if (!WhenceFile.empty())
94 errs() << WhenceFile << ": ";
95 if (!WhenceFunction.empty())
96 errs() << WhenceFunction << ": ";
98 auto IPE = instrprof_error::success;
99 E = handleErrors(std::move(E),
100 [&IPE](std::unique_ptr<InstrProfError> E) -> Error {
102 return Error(std::move(E));
104 errs() << toString(std::move(E)) << "\n";
108 if (IPE != instrprof_error::success) {
110 case instrprof_error::hash_mismatch:
111 case instrprof_error::count_mismatch:
112 case instrprof_error::value_site_count_mismatch:
113 Hint = "Make sure that all profile data to be merged is generated "
114 "from the same binary.";
122 errs() << Hint << "\n";
127 /// A remapper from original symbol names to new symbol names based on a file
128 /// containing a list of mappings from old name to new name.
129 class SymbolRemapper {
130 std::unique_ptr<MemoryBuffer> File;
131 DenseMap<StringRef, StringRef> RemappingTable;
134 /// Build a SymbolRemapper from a file containing a list of old/new symbols.
135 static std::unique_ptr<SymbolRemapper> create(StringRef InputFile) {
136 auto BufOrError = MemoryBuffer::getFileOrSTDIN(InputFile);
138 exitWithErrorCode(BufOrError.getError(), InputFile);
140 auto Remapper = llvm::make_unique<SymbolRemapper>();
141 Remapper->File = std::move(BufOrError.get());
143 for (line_iterator LineIt(*Remapper->File, /*SkipBlanks=*/true, '#');
144 !LineIt.is_at_eof(); ++LineIt) {
145 std::pair<StringRef, StringRef> Parts = LineIt->split(' ');
146 if (Parts.first.empty() || Parts.second.empty() ||
147 Parts.second.count(' ')) {
148 exitWithError("unexpected line in remapping file",
149 (InputFile + ":" + Twine(LineIt.line_number())).str(),
150 "expected 'old_symbol new_symbol'");
152 Remapper->RemappingTable.insert(Parts);
157 /// Attempt to map the given old symbol into a new symbol.
159 /// \return The new symbol, or \p Name if no such symbol was found.
160 StringRef operator()(StringRef Name) {
161 StringRef New = RemappingTable.lookup(Name);
162 return New.empty() ? Name : New;
167 struct WeightedFile {
168 std::string Filename;
171 typedef SmallVector<WeightedFile, 5> WeightedFileVector;
173 /// Keep track of merged data and reported errors.
174 struct WriterContext {
176 InstrProfWriter Writer;
178 std::string ErrWhence;
180 SmallSet<instrprof_error, 4> &WriterErrorCodes;
182 WriterContext(bool IsSparse, std::mutex &ErrLock,
183 SmallSet<instrprof_error, 4> &WriterErrorCodes)
184 : Lock(), Writer(IsSparse), Err(Error::success()), ErrWhence(""),
185 ErrLock(ErrLock), WriterErrorCodes(WriterErrorCodes) {}
188 /// Determine whether an error is fatal for profile merging.
189 static bool isFatalError(instrprof_error IPE) {
193 case instrprof_error::success:
194 case instrprof_error::eof:
195 case instrprof_error::unknown_function:
196 case instrprof_error::hash_mismatch:
197 case instrprof_error::count_mismatch:
198 case instrprof_error::counter_overflow:
199 case instrprof_error::value_site_count_mismatch:
204 /// Load an input into a writer context.
205 static void loadInput(const WeightedFile &Input, SymbolRemapper *Remapper,
207 std::unique_lock<std::mutex> CtxGuard{WC->Lock};
209 // If there's a pending hard error, don't do more work.
213 // Copy the filename, because llvm::ThreadPool copied the input "const
214 // WeightedFile &" by value, making a reference to the filename within it
215 // invalid outside of this packaged task.
216 WC->ErrWhence = Input.Filename;
218 auto ReaderOrErr = InstrProfReader::create(Input.Filename);
219 if (Error E = ReaderOrErr.takeError()) {
220 // Skip the empty profiles by returning sliently.
221 instrprof_error IPE = InstrProfError::take(std::move(E));
222 if (IPE != instrprof_error::empty_raw_profile)
223 WC->Err = make_error<InstrProfError>(IPE);
227 auto Reader = std::move(ReaderOrErr.get());
228 bool IsIRProfile = Reader->isIRLevelProfile();
229 if (WC->Writer.setIsIRLevelProfile(IsIRProfile)) {
230 WC->Err = make_error<StringError>(
231 "Merge IR generated profile with Clang generated profile.",
236 for (auto &I : *Reader) {
238 I.Name = (*Remapper)(I.Name);
239 const StringRef FuncName = I.Name;
240 bool Reported = false;
241 WC->Writer.addRecord(std::move(I), Input.Weight, [&](Error E) {
243 consumeError(std::move(E));
247 // Only show hint the first time an error occurs.
248 instrprof_error IPE = InstrProfError::take(std::move(E));
249 std::unique_lock<std::mutex> ErrGuard{WC->ErrLock};
250 bool firstTime = WC->WriterErrorCodes.insert(IPE).second;
251 handleMergeWriterError(make_error<InstrProfError>(IPE), Input.Filename,
252 FuncName, firstTime);
255 if (Reader->hasError()) {
256 if (Error E = Reader->getError()) {
257 instrprof_error IPE = InstrProfError::take(std::move(E));
258 if (isFatalError(IPE))
259 WC->Err = make_error<InstrProfError>(IPE);
264 /// Merge the \p Src writer context into \p Dst.
265 static void mergeWriterContexts(WriterContext *Dst, WriterContext *Src) {
266 // If we've already seen a hard error, continuing with the merge would
268 if (Dst->Err || Src->Err)
271 bool Reported = false;
272 Dst->Writer.mergeRecordsFromWriter(std::move(Src->Writer), [&](Error E) {
274 consumeError(std::move(E));
278 Dst->Err = std::move(E);
282 static void mergeInstrProfile(const WeightedFileVector &Inputs,
283 SymbolRemapper *Remapper,
284 StringRef OutputFilename,
285 ProfileFormat OutputFormat, bool OutputSparse,
286 unsigned NumThreads) {
287 if (OutputFilename.compare("-") == 0)
288 exitWithError("Cannot write indexed profdata format to stdout.");
290 if (OutputFormat != PF_Binary && OutputFormat != PF_Compact_Binary &&
291 OutputFormat != PF_Text)
292 exitWithError("Unknown format is specified.");
295 raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None);
297 exitWithErrorCode(EC, OutputFilename);
299 std::mutex ErrorLock;
300 SmallSet<instrprof_error, 4> WriterErrorCodes;
302 // If NumThreads is not specified, auto-detect a good default.
305 std::min(hardware_concurrency(), unsigned((Inputs.size() + 1) / 2));
307 // Initialize the writer contexts.
308 SmallVector<std::unique_ptr<WriterContext>, 4> Contexts;
309 for (unsigned I = 0; I < NumThreads; ++I)
310 Contexts.emplace_back(llvm::make_unique<WriterContext>(
311 OutputSparse, ErrorLock, WriterErrorCodes));
313 if (NumThreads == 1) {
314 for (const auto &Input : Inputs)
315 loadInput(Input, Remapper, Contexts[0].get());
317 ThreadPool Pool(NumThreads);
319 // Load the inputs in parallel (N/NumThreads serial steps).
321 for (const auto &Input : Inputs) {
322 Pool.async(loadInput, Input, Remapper, Contexts[Ctx].get());
323 Ctx = (Ctx + 1) % NumThreads;
327 // Merge the writer contexts together (~ lg(NumThreads) serial steps).
328 unsigned Mid = Contexts.size() / 2;
329 unsigned End = Contexts.size();
330 assert(Mid > 0 && "Expected more than one context");
332 for (unsigned I = 0; I < Mid; ++I)
333 Pool.async(mergeWriterContexts, Contexts[I].get(),
334 Contexts[I + Mid].get());
337 Pool.async(mergeWriterContexts, Contexts[0].get(),
338 Contexts[End - 1].get());
346 // Handle deferred hard errors encountered during merging.
347 for (std::unique_ptr<WriterContext> &WC : Contexts) {
350 if (!WC->Err.isA<InstrProfError>())
351 exitWithError(std::move(WC->Err), WC->ErrWhence);
353 instrprof_error IPE = InstrProfError::take(std::move(WC->Err));
354 if (isFatalError(IPE))
355 exitWithError(make_error<InstrProfError>(IPE), WC->ErrWhence);
357 warn(toString(make_error<InstrProfError>(IPE)),
361 InstrProfWriter &Writer = Contexts[0]->Writer;
362 if (OutputFormat == PF_Text) {
363 if (Error E = Writer.writeText(Output))
364 exitWithError(std::move(E));
366 Writer.write(Output);
370 /// Make a copy of the given function samples with all symbol names remapped
371 /// by the provided symbol remapper.
372 static sampleprof::FunctionSamples
373 remapSamples(const sampleprof::FunctionSamples &Samples,
374 SymbolRemapper &Remapper, sampleprof_error &Error) {
375 sampleprof::FunctionSamples Result;
376 Result.setName(Remapper(Samples.getName()));
377 Result.addTotalSamples(Samples.getTotalSamples());
378 Result.addHeadSamples(Samples.getHeadSamples());
379 for (const auto &BodySample : Samples.getBodySamples()) {
380 Result.addBodySamples(BodySample.first.LineOffset,
381 BodySample.first.Discriminator,
382 BodySample.second.getSamples());
383 for (const auto &Target : BodySample.second.getCallTargets()) {
384 Result.addCalledTargetSamples(BodySample.first.LineOffset,
385 BodySample.first.Discriminator,
386 Remapper(Target.first()), Target.second);
389 for (const auto &CallsiteSamples : Samples.getCallsiteSamples()) {
390 sampleprof::FunctionSamplesMap &Target =
391 Result.functionSamplesAt(CallsiteSamples.first);
392 for (const auto &Callsite : CallsiteSamples.second) {
393 sampleprof::FunctionSamples Remapped =
394 remapSamples(Callsite.second, Remapper, Error);
395 MergeResult(Error, Target[Remapped.getName()].merge(Remapped));
401 static sampleprof::SampleProfileFormat FormatMap[] = {
402 sampleprof::SPF_None, sampleprof::SPF_Text, sampleprof::SPF_Compact_Binary,
403 sampleprof::SPF_GCC, sampleprof::SPF_Binary};
405 static void mergeSampleProfile(const WeightedFileVector &Inputs,
406 SymbolRemapper *Remapper,
407 StringRef OutputFilename,
408 ProfileFormat OutputFormat) {
409 using namespace sampleprof;
411 SampleProfileWriter::create(OutputFilename, FormatMap[OutputFormat]);
412 if (std::error_code EC = WriterOrErr.getError())
413 exitWithErrorCode(EC, OutputFilename);
415 auto Writer = std::move(WriterOrErr.get());
416 StringMap<FunctionSamples> ProfileMap;
417 SmallVector<std::unique_ptr<sampleprof::SampleProfileReader>, 5> Readers;
419 for (const auto &Input : Inputs) {
420 auto ReaderOrErr = SampleProfileReader::create(Input.Filename, Context);
421 if (std::error_code EC = ReaderOrErr.getError())
422 exitWithErrorCode(EC, Input.Filename);
424 // We need to keep the readers around until after all the files are
425 // read so that we do not lose the function names stored in each
426 // reader's memory. The function names are needed to write out the
427 // merged profile map.
428 Readers.push_back(std::move(ReaderOrErr.get()));
429 const auto Reader = Readers.back().get();
430 if (std::error_code EC = Reader->read())
431 exitWithErrorCode(EC, Input.Filename);
433 StringMap<FunctionSamples> &Profiles = Reader->getProfiles();
434 for (StringMap<FunctionSamples>::iterator I = Profiles.begin(),
437 sampleprof_error Result = sampleprof_error::success;
438 FunctionSamples Remapped =
439 Remapper ? remapSamples(I->second, *Remapper, Result)
441 FunctionSamples &Samples = Remapper ? Remapped : I->second;
442 StringRef FName = Samples.getName();
443 MergeResult(Result, ProfileMap[FName].merge(Samples, Input.Weight));
444 if (Result != sampleprof_error::success) {
445 std::error_code EC = make_error_code(Result);
446 handleMergeWriterError(errorCodeToError(EC), Input.Filename, FName);
450 Writer->write(ProfileMap);
453 static WeightedFile parseWeightedFile(const StringRef &WeightedFilename) {
454 StringRef WeightStr, FileName;
455 std::tie(WeightStr, FileName) = WeightedFilename.split(',');
458 if (WeightStr.getAsInteger(10, Weight) || Weight < 1)
459 exitWithError("Input weight must be a positive integer.");
461 return {FileName, Weight};
464 static std::unique_ptr<MemoryBuffer>
465 getInputFilenamesFileBuf(const StringRef &InputFilenamesFile) {
466 if (InputFilenamesFile == "")
469 auto BufOrError = MemoryBuffer::getFileOrSTDIN(InputFilenamesFile);
471 exitWithErrorCode(BufOrError.getError(), InputFilenamesFile);
473 return std::move(*BufOrError);
476 static void addWeightedInput(WeightedFileVector &WNI, const WeightedFile &WF) {
477 StringRef Filename = WF.Filename;
478 uint64_t Weight = WF.Weight;
480 // If it's STDIN just pass it on.
481 if (Filename == "-") {
482 WNI.push_back({Filename, Weight});
486 llvm::sys::fs::file_status Status;
487 llvm::sys::fs::status(Filename, Status);
488 if (!llvm::sys::fs::exists(Status))
489 exitWithErrorCode(make_error_code(errc::no_such_file_or_directory),
491 // If it's a source file, collect it.
492 if (llvm::sys::fs::is_regular_file(Status)) {
493 WNI.push_back({Filename, Weight});
497 if (llvm::sys::fs::is_directory(Status)) {
499 for (llvm::sys::fs::recursive_directory_iterator F(Filename, EC), E;
500 F != E && !EC; F.increment(EC)) {
501 if (llvm::sys::fs::is_regular_file(F->path())) {
502 addWeightedInput(WNI, {F->path(), Weight});
506 exitWithErrorCode(EC, Filename);
510 static void parseInputFilenamesFile(MemoryBuffer *Buffer,
511 WeightedFileVector &WFV) {
515 SmallVector<StringRef, 8> Entries;
516 StringRef Data = Buffer->getBuffer();
517 Data.split(Entries, '\n', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
518 for (const StringRef &FileWeightEntry : Entries) {
519 StringRef SanitizedEntry = FileWeightEntry.trim(" \t\v\f\r");
521 if (SanitizedEntry.startswith("#"))
523 // If there's no comma, it's an unweighted profile.
524 else if (SanitizedEntry.find(',') == StringRef::npos)
525 addWeightedInput(WFV, {SanitizedEntry, 1});
527 addWeightedInput(WFV, parseWeightedFile(SanitizedEntry));
531 static int merge_main(int argc, const char *argv[]) {
532 cl::list<std::string> InputFilenames(cl::Positional,
533 cl::desc("<filename...>"));
534 cl::list<std::string> WeightedInputFilenames("weighted-input",
535 cl::desc("<weight>,<filename>"));
536 cl::opt<std::string> InputFilenamesFile(
537 "input-files", cl::init(""),
538 cl::desc("Path to file containing newline-separated "
539 "[<weight>,]<filename> entries"));
540 cl::alias InputFilenamesFileA("f", cl::desc("Alias for --input-files"),
541 cl::aliasopt(InputFilenamesFile));
542 cl::opt<bool> DumpInputFileList(
543 "dump-input-file-list", cl::init(false), cl::Hidden,
544 cl::desc("Dump the list of input files and their weights, then exit"));
545 cl::opt<std::string> RemappingFile("remapping-file", cl::value_desc("file"),
546 cl::desc("Symbol remapping file"));
547 cl::alias RemappingFileA("r", cl::desc("Alias for --remapping-file"),
548 cl::aliasopt(RemappingFile));
549 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
550 cl::init("-"), cl::Required,
551 cl::desc("Output file"));
552 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
553 cl::aliasopt(OutputFilename));
554 cl::opt<ProfileKinds> ProfileKind(
555 cl::desc("Profile kind:"), cl::init(instr),
556 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
557 clEnumVal(sample, "Sample profile")));
558 cl::opt<ProfileFormat> OutputFormat(
559 cl::desc("Format of output profile"), cl::init(PF_Binary),
560 cl::values(clEnumValN(PF_Binary, "binary", "Binary encoding (default)"),
561 clEnumValN(PF_Compact_Binary, "compbinary",
562 "Compact binary encoding"),
563 clEnumValN(PF_Text, "text", "Text encoding"),
564 clEnumValN(PF_GCC, "gcc",
565 "GCC encoding (only meaningful for -sample)")));
566 cl::opt<bool> OutputSparse("sparse", cl::init(false),
567 cl::desc("Generate a sparse profile (only meaningful for -instr)"));
568 cl::opt<unsigned> NumThreads(
569 "num-threads", cl::init(0),
570 cl::desc("Number of merge threads to use (default: autodetect)"));
571 cl::alias NumThreadsA("j", cl::desc("Alias for --num-threads"),
572 cl::aliasopt(NumThreads));
574 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
576 WeightedFileVector WeightedInputs;
577 for (StringRef Filename : InputFilenames)
578 addWeightedInput(WeightedInputs, {Filename, 1});
579 for (StringRef WeightedFilename : WeightedInputFilenames)
580 addWeightedInput(WeightedInputs, parseWeightedFile(WeightedFilename));
582 // Make sure that the file buffer stays alive for the duration of the
583 // weighted input vector's lifetime.
584 auto Buffer = getInputFilenamesFileBuf(InputFilenamesFile);
585 parseInputFilenamesFile(Buffer.get(), WeightedInputs);
587 if (WeightedInputs.empty())
588 exitWithError("No input files specified. See " +
589 sys::path::filename(argv[0]) + " -help");
591 if (DumpInputFileList) {
592 for (auto &WF : WeightedInputs)
593 outs() << WF.Weight << "," << WF.Filename << "\n";
597 std::unique_ptr<SymbolRemapper> Remapper;
598 if (!RemappingFile.empty())
599 Remapper = SymbolRemapper::create(RemappingFile);
601 if (ProfileKind == instr)
602 mergeInstrProfile(WeightedInputs, Remapper.get(), OutputFilename,
603 OutputFormat, OutputSparse, NumThreads);
605 mergeSampleProfile(WeightedInputs, Remapper.get(), OutputFilename,
611 typedef struct ValueSitesStats {
613 : TotalNumValueSites(0), TotalNumValueSitesWithValueProfile(0),
615 uint64_t TotalNumValueSites;
616 uint64_t TotalNumValueSitesWithValueProfile;
617 uint64_t TotalNumValues;
618 std::vector<unsigned> ValueSitesHistogram;
621 static void traverseAllValueSites(const InstrProfRecord &Func, uint32_t VK,
622 ValueSitesStats &Stats, raw_fd_ostream &OS,
623 InstrProfSymtab *Symtab) {
624 uint32_t NS = Func.getNumValueSites(VK);
625 Stats.TotalNumValueSites += NS;
626 for (size_t I = 0; I < NS; ++I) {
627 uint32_t NV = Func.getNumValueDataForSite(VK, I);
628 std::unique_ptr<InstrProfValueData[]> VD = Func.getValueForSite(VK, I);
629 Stats.TotalNumValues += NV;
631 Stats.TotalNumValueSitesWithValueProfile++;
632 if (NV > Stats.ValueSitesHistogram.size())
633 Stats.ValueSitesHistogram.resize(NV, 0);
634 Stats.ValueSitesHistogram[NV - 1]++;
637 uint64_t SiteSum = 0;
638 for (uint32_t V = 0; V < NV; V++)
639 SiteSum += VD[V].Count;
643 for (uint32_t V = 0; V < NV; V++) {
644 OS << "\t[ " << format("%2u", I) << ", ";
645 if (Symtab == nullptr)
646 OS << format("%4u", VD[V].Value);
648 OS << Symtab->getFuncName(VD[V].Value);
649 OS << ", " << format("%10" PRId64, VD[V].Count) << " ] ("
650 << format("%.2f%%", (VD[V].Count * 100.0 / SiteSum)) << ")\n";
655 static void showValueSitesStats(raw_fd_ostream &OS, uint32_t VK,
656 ValueSitesStats &Stats) {
657 OS << " Total number of sites: " << Stats.TotalNumValueSites << "\n";
658 OS << " Total number of sites with values: "
659 << Stats.TotalNumValueSitesWithValueProfile << "\n";
660 OS << " Total number of profiled values: " << Stats.TotalNumValues << "\n";
662 OS << " Value sites histogram:\n\tNumTargets, SiteCount\n";
663 for (unsigned I = 0; I < Stats.ValueSitesHistogram.size(); I++) {
664 if (Stats.ValueSitesHistogram[I] > 0)
665 OS << "\t" << I + 1 << ", " << Stats.ValueSitesHistogram[I] << "\n";
669 static int showInstrProfile(const std::string &Filename, bool ShowCounts,
670 uint32_t TopN, bool ShowIndirectCallTargets,
671 bool ShowMemOPSizes, bool ShowDetailedSummary,
672 std::vector<uint32_t> DetailedSummaryCutoffs,
673 bool ShowAllFunctions, uint64_t ValueCutoff,
674 bool OnlyListBelow, const std::string &ShowFunction,
675 bool TextFormat, raw_fd_ostream &OS) {
676 auto ReaderOrErr = InstrProfReader::create(Filename);
677 std::vector<uint32_t> Cutoffs = std::move(DetailedSummaryCutoffs);
678 if (ShowDetailedSummary && Cutoffs.empty()) {
679 Cutoffs = {800000, 900000, 950000, 990000, 999000, 999900, 999990};
681 InstrProfSummaryBuilder Builder(std::move(Cutoffs));
682 if (Error E = ReaderOrErr.takeError())
683 exitWithError(std::move(E), Filename);
685 auto Reader = std::move(ReaderOrErr.get());
686 bool IsIRInstr = Reader->isIRLevelProfile();
687 size_t ShownFunctions = 0;
688 size_t BelowCutoffFunctions = 0;
689 int NumVPKind = IPVK_Last - IPVK_First + 1;
690 std::vector<ValueSitesStats> VPStats(NumVPKind);
692 auto MinCmp = [](const std::pair<std::string, uint64_t> &v1,
693 const std::pair<std::string, uint64_t> &v2) {
694 return v1.second > v2.second;
697 std::priority_queue<std::pair<std::string, uint64_t>,
698 std::vector<std::pair<std::string, uint64_t>>,
700 HottestFuncs(MinCmp);
702 if (!TextFormat && OnlyListBelow) {
703 OS << "The list of functions with the maximum counter less than "
704 << ValueCutoff << ":\n";
707 // Add marker so that IR-level instrumentation round-trips properly.
708 if (TextFormat && IsIRInstr)
711 for (const auto &Func : *Reader) {
713 ShowAllFunctions || (!ShowFunction.empty() &&
714 Func.Name.find(ShowFunction) != Func.Name.npos);
716 bool doTextFormatDump = (Show && TextFormat);
718 if (doTextFormatDump) {
719 InstrProfSymtab &Symtab = Reader->getSymtab();
720 InstrProfWriter::writeRecordInText(Func.Name, Func.Hash, Func, Symtab,
725 assert(Func.Counts.size() > 0 && "function missing entry counter");
726 Builder.addRecord(Func);
728 uint64_t FuncMax = 0;
729 uint64_t FuncSum = 0;
730 for (size_t I = 0, E = Func.Counts.size(); I < E; ++I) {
731 FuncMax = std::max(FuncMax, Func.Counts[I]);
732 FuncSum += Func.Counts[I];
735 if (FuncMax < ValueCutoff) {
736 ++BelowCutoffFunctions;
738 OS << " " << Func.Name << ": (Max = " << FuncMax
739 << " Sum = " << FuncSum << ")\n";
742 } else if (OnlyListBelow)
746 if (HottestFuncs.size() == TopN) {
747 if (HottestFuncs.top().second < FuncMax) {
749 HottestFuncs.emplace(std::make_pair(std::string(Func.Name), FuncMax));
752 HottestFuncs.emplace(std::make_pair(std::string(Func.Name), FuncMax));
761 OS << " " << Func.Name << ":\n"
762 << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n"
763 << " Counters: " << Func.Counts.size() << "\n";
765 OS << " Function count: " << Func.Counts[0] << "\n";
767 if (ShowIndirectCallTargets)
768 OS << " Indirect Call Site Count: "
769 << Func.getNumValueSites(IPVK_IndirectCallTarget) << "\n";
771 uint32_t NumMemOPCalls = Func.getNumValueSites(IPVK_MemOPSize);
772 if (ShowMemOPSizes && NumMemOPCalls > 0)
773 OS << " Number of Memory Intrinsics Calls: " << NumMemOPCalls
777 OS << " Block counts: [";
778 size_t Start = (IsIRInstr ? 0 : 1);
779 for (size_t I = Start, E = Func.Counts.size(); I < E; ++I) {
780 OS << (I == Start ? "" : ", ") << Func.Counts[I];
785 if (ShowIndirectCallTargets) {
786 OS << " Indirect Target Results:\n";
787 traverseAllValueSites(Func, IPVK_IndirectCallTarget,
788 VPStats[IPVK_IndirectCallTarget], OS,
789 &(Reader->getSymtab()));
792 if (ShowMemOPSizes && NumMemOPCalls > 0) {
793 OS << " Memory Intrinsic Size Results:\n";
794 traverseAllValueSites(Func, IPVK_MemOPSize, VPStats[IPVK_MemOPSize], OS,
799 if (Reader->hasError())
800 exitWithError(Reader->getError(), Filename);
804 std::unique_ptr<ProfileSummary> PS(Builder.getSummary());
805 OS << "Instrumentation level: "
806 << (Reader->isIRLevelProfile() ? "IR" : "Front-end") << "\n";
807 if (ShowAllFunctions || !ShowFunction.empty())
808 OS << "Functions shown: " << ShownFunctions << "\n";
809 OS << "Total functions: " << PS->getNumFunctions() << "\n";
810 if (ValueCutoff > 0) {
811 OS << "Number of functions with maximum count (< " << ValueCutoff
812 << "): " << BelowCutoffFunctions << "\n";
813 OS << "Number of functions with maximum count (>= " << ValueCutoff
814 << "): " << PS->getNumFunctions() - BelowCutoffFunctions << "\n";
816 OS << "Maximum function count: " << PS->getMaxFunctionCount() << "\n";
817 OS << "Maximum internal block count: " << PS->getMaxInternalCount() << "\n";
820 std::vector<std::pair<std::string, uint64_t>> SortedHottestFuncs;
821 while (!HottestFuncs.empty()) {
822 SortedHottestFuncs.emplace_back(HottestFuncs.top());
826 << " functions with the largest internal block counts: \n";
827 for (auto &hotfunc : llvm::reverse(SortedHottestFuncs))
828 OS << " " << hotfunc.first << ", max count = " << hotfunc.second << "\n";
831 if (ShownFunctions && ShowIndirectCallTargets) {
832 OS << "Statistics for indirect call sites profile:\n";
833 showValueSitesStats(OS, IPVK_IndirectCallTarget,
834 VPStats[IPVK_IndirectCallTarget]);
837 if (ShownFunctions && ShowMemOPSizes) {
838 OS << "Statistics for memory intrinsic calls sizes profile:\n";
839 showValueSitesStats(OS, IPVK_MemOPSize, VPStats[IPVK_MemOPSize]);
842 if (ShowDetailedSummary) {
843 OS << "Detailed summary:\n";
844 OS << "Total number of blocks: " << PS->getNumCounts() << "\n";
845 OS << "Total count: " << PS->getTotalCount() << "\n";
846 for (auto Entry : PS->getDetailedSummary()) {
847 OS << Entry.NumCounts << " blocks with count >= " << Entry.MinCount
849 << format("%0.6g", (float)Entry.Cutoff / ProfileSummary::Scale * 100)
850 << " percentage of the total counts.\n";
856 static int showSampleProfile(const std::string &Filename, bool ShowCounts,
857 bool ShowAllFunctions,
858 const std::string &ShowFunction,
859 raw_fd_ostream &OS) {
860 using namespace sampleprof;
862 auto ReaderOrErr = SampleProfileReader::create(Filename, Context);
863 if (std::error_code EC = ReaderOrErr.getError())
864 exitWithErrorCode(EC, Filename);
866 auto Reader = std::move(ReaderOrErr.get());
867 if (std::error_code EC = Reader->read())
868 exitWithErrorCode(EC, Filename);
870 if (ShowAllFunctions || ShowFunction.empty())
873 Reader->dumpFunctionProfile(ShowFunction, OS);
878 static int show_main(int argc, const char *argv[]) {
879 cl::opt<std::string> Filename(cl::Positional, cl::Required,
880 cl::desc("<profdata-file>"));
882 cl::opt<bool> ShowCounts("counts", cl::init(false),
883 cl::desc("Show counter values for shown functions"));
884 cl::opt<bool> TextFormat(
885 "text", cl::init(false),
886 cl::desc("Show instr profile data in text dump format"));
887 cl::opt<bool> ShowIndirectCallTargets(
888 "ic-targets", cl::init(false),
889 cl::desc("Show indirect call site target values for shown functions"));
890 cl::opt<bool> ShowMemOPSizes(
891 "memop-sizes", cl::init(false),
892 cl::desc("Show the profiled sizes of the memory intrinsic calls "
893 "for shown functions"));
894 cl::opt<bool> ShowDetailedSummary("detailed-summary", cl::init(false),
895 cl::desc("Show detailed profile summary"));
896 cl::list<uint32_t> DetailedSummaryCutoffs(
897 cl::CommaSeparated, "detailed-summary-cutoffs",
899 "Cutoff percentages (times 10000) for generating detailed summary"),
900 cl::value_desc("800000,901000,999999"));
901 cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false),
902 cl::desc("Details for every function"));
903 cl::opt<std::string> ShowFunction("function",
904 cl::desc("Details for matching functions"));
906 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
907 cl::init("-"), cl::desc("Output file"));
908 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
909 cl::aliasopt(OutputFilename));
910 cl::opt<ProfileKinds> ProfileKind(
911 cl::desc("Profile kind:"), cl::init(instr),
912 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
913 clEnumVal(sample, "Sample profile")));
914 cl::opt<uint32_t> TopNFunctions(
916 cl::desc("Show the list of functions with the largest internal counts"));
917 cl::opt<uint32_t> ValueCutoff(
918 "value-cutoff", cl::init(0),
919 cl::desc("Set the count value cutoff. Functions with the maximum count "
920 "less than this value will not be printed out. (Default is 0)"));
921 cl::opt<bool> OnlyListBelow(
922 "list-below-cutoff", cl::init(false),
923 cl::desc("Only output names of functions whose max count values are "
924 "below the cutoff value"));
925 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
927 if (OutputFilename.empty())
928 OutputFilename = "-";
931 raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text);
933 exitWithErrorCode(EC, OutputFilename);
935 if (ShowAllFunctions && !ShowFunction.empty())
936 WithColor::warning() << "-function argument ignored: showing all functions\n";
938 std::vector<uint32_t> Cutoffs(DetailedSummaryCutoffs.begin(),
939 DetailedSummaryCutoffs.end());
940 if (ProfileKind == instr)
941 return showInstrProfile(Filename, ShowCounts, TopNFunctions,
942 ShowIndirectCallTargets, ShowMemOPSizes,
943 ShowDetailedSummary, DetailedSummaryCutoffs,
944 ShowAllFunctions, ValueCutoff, OnlyListBelow,
945 ShowFunction, TextFormat, OS);
947 return showSampleProfile(Filename, ShowCounts, ShowAllFunctions,
951 int main(int argc, const char *argv[]) {
952 InitLLVM X(argc, argv);
954 StringRef ProgName(sys::path::filename(argv[0]));
956 int (*func)(int, const char *[]) = nullptr;
958 if (strcmp(argv[1], "merge") == 0)
960 else if (strcmp(argv[1], "show") == 0)
964 std::string Invocation(ProgName.str() + " " + argv[1]);
965 argv[1] = Invocation.c_str();
966 return func(argc - 1, argv + 1);
969 if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-help") == 0 ||
970 strcmp(argv[1], "--help") == 0) {
972 errs() << "OVERVIEW: LLVM profile data tools\n\n"
973 << "USAGE: " << ProgName << " <command> [args...]\n"
974 << "USAGE: " << ProgName << " <command> -help\n\n"
975 << "See each individual command --help for more details.\n"
976 << "Available commands: merge, show\n";
982 errs() << ProgName << ": No command specified!\n";
984 errs() << ProgName << ": Unknown command!\n";
986 errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";