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"
37 enum ProfileFormat
{ PF_None
= 0, PF_Text
, PF_Binary
, PF_GCC
};
39 static void warn(Twine Message
, std::string Whence
= "",
40 std::string Hint
= "") {
43 errs() << Whence
<< ": ";
44 errs() << Message
<< "\n";
46 WithColor::note() << Hint
<< "\n";
49 static void exitWithError(Twine Message
, std::string Whence
= "",
50 std::string Hint
= "") {
53 errs() << Whence
<< ": ";
54 errs() << Message
<< "\n";
56 WithColor::note() << Hint
<< "\n";
60 static void exitWithError(Error E
, StringRef Whence
= "") {
61 if (E
.isA
<InstrProfError
>()) {
62 handleAllErrors(std::move(E
), [&](const InstrProfError
&IPE
) {
63 instrprof_error instrError
= IPE
.get();
65 if (instrError
== instrprof_error::unrecognized_format
) {
66 // Hint for common error of forgetting -sample for sample profiles.
67 Hint
= "Perhaps you forgot to use the -sample option?";
69 exitWithError(IPE
.message(), Whence
, Hint
);
73 exitWithError(toString(std::move(E
)), Whence
);
76 static void exitWithErrorCode(std::error_code EC
, StringRef Whence
= "") {
77 exitWithError(EC
.message(), Whence
);
81 enum ProfileKinds
{ instr
, sample
};
84 static void handleMergeWriterError(Error E
, StringRef WhenceFile
= "",
85 StringRef WhenceFunction
= "",
86 bool ShowHint
= true) {
87 if (!WhenceFile
.empty())
88 errs() << WhenceFile
<< ": ";
89 if (!WhenceFunction
.empty())
90 errs() << WhenceFunction
<< ": ";
92 auto IPE
= instrprof_error::success
;
93 E
= handleErrors(std::move(E
),
94 [&IPE
](std::unique_ptr
<InstrProfError
> E
) -> Error
{
96 return Error(std::move(E
));
98 errs() << toString(std::move(E
)) << "\n";
102 if (IPE
!= instrprof_error::success
) {
104 case instrprof_error::hash_mismatch
:
105 case instrprof_error::count_mismatch
:
106 case instrprof_error::value_site_count_mismatch
:
107 Hint
= "Make sure that all profile data to be merged is generated "
108 "from the same binary.";
116 errs() << Hint
<< "\n";
120 struct WeightedFile
{
121 std::string Filename
;
124 typedef SmallVector
<WeightedFile
, 5> WeightedFileVector
;
126 /// Keep track of merged data and reported errors.
127 struct WriterContext
{
129 InstrProfWriter Writer
;
131 std::string ErrWhence
;
133 SmallSet
<instrprof_error
, 4> &WriterErrorCodes
;
135 WriterContext(bool IsSparse
, std::mutex
&ErrLock
,
136 SmallSet
<instrprof_error
, 4> &WriterErrorCodes
)
137 : Lock(), Writer(IsSparse
), Err(Error::success()), ErrWhence(""),
138 ErrLock(ErrLock
), WriterErrorCodes(WriterErrorCodes
) {}
141 /// Determine whether an error is fatal for profile merging.
142 static bool isFatalError(instrprof_error IPE
) {
146 case instrprof_error::success
:
147 case instrprof_error::eof
:
148 case instrprof_error::unknown_function
:
149 case instrprof_error::hash_mismatch
:
150 case instrprof_error::count_mismatch
:
151 case instrprof_error::counter_overflow
:
152 case instrprof_error::value_site_count_mismatch
:
157 /// Load an input into a writer context.
158 static void loadInput(const WeightedFile
&Input
, WriterContext
*WC
) {
159 std::unique_lock
<std::mutex
> CtxGuard
{WC
->Lock
};
161 // If there's a pending hard error, don't do more work.
165 // Copy the filename, because llvm::ThreadPool copied the input "const
166 // WeightedFile &" by value, making a reference to the filename within it
167 // invalid outside of this packaged task.
168 WC
->ErrWhence
= Input
.Filename
;
170 auto ReaderOrErr
= InstrProfReader::create(Input
.Filename
);
171 if (Error E
= ReaderOrErr
.takeError()) {
172 // Skip the empty profiles by returning sliently.
173 instrprof_error IPE
= InstrProfError::take(std::move(E
));
174 if (IPE
!= instrprof_error::empty_raw_profile
)
175 WC
->Err
= make_error
<InstrProfError
>(IPE
);
179 auto Reader
= std::move(ReaderOrErr
.get());
180 bool IsIRProfile
= Reader
->isIRLevelProfile();
181 if (WC
->Writer
.setIsIRLevelProfile(IsIRProfile
)) {
182 WC
->Err
= make_error
<StringError
>(
183 "Merge IR generated profile with Clang generated profile.",
188 for (auto &I
: *Reader
) {
189 const StringRef FuncName
= I
.Name
;
190 bool Reported
= false;
191 WC
->Writer
.addRecord(std::move(I
), Input
.Weight
, [&](Error E
) {
193 consumeError(std::move(E
));
197 // Only show hint the first time an error occurs.
198 instrprof_error IPE
= InstrProfError::take(std::move(E
));
199 std::unique_lock
<std::mutex
> ErrGuard
{WC
->ErrLock
};
200 bool firstTime
= WC
->WriterErrorCodes
.insert(IPE
).second
;
201 handleMergeWriterError(make_error
<InstrProfError
>(IPE
), Input
.Filename
,
202 FuncName
, firstTime
);
205 if (Reader
->hasError()) {
206 if (Error E
= Reader
->getError()) {
207 instrprof_error IPE
= InstrProfError::take(std::move(E
));
208 if (isFatalError(IPE
))
209 WC
->Err
= make_error
<InstrProfError
>(IPE
);
214 /// Merge the \p Src writer context into \p Dst.
215 static void mergeWriterContexts(WriterContext
*Dst
, WriterContext
*Src
) {
216 // If we've already seen a hard error, continuing with the merge would
218 if (Dst
->Err
|| Src
->Err
)
221 bool Reported
= false;
222 Dst
->Writer
.mergeRecordsFromWriter(std::move(Src
->Writer
), [&](Error E
) {
224 consumeError(std::move(E
));
228 Dst
->Err
= std::move(E
);
232 static void mergeInstrProfile(const WeightedFileVector
&Inputs
,
233 StringRef OutputFilename
,
234 ProfileFormat OutputFormat
, bool OutputSparse
,
235 unsigned NumThreads
) {
236 if (OutputFilename
.compare("-") == 0)
237 exitWithError("Cannot write indexed profdata format to stdout.");
239 if (OutputFormat
!= PF_Binary
&& OutputFormat
!= PF_Text
)
240 exitWithError("Unknown format is specified.");
243 raw_fd_ostream
Output(OutputFilename
.data(), EC
, sys::fs::F_None
);
245 exitWithErrorCode(EC
, OutputFilename
);
247 std::mutex ErrorLock
;
248 SmallSet
<instrprof_error
, 4> WriterErrorCodes
;
250 // If NumThreads is not specified, auto-detect a good default.
253 std::min(hardware_concurrency(), unsigned((Inputs
.size() + 1) / 2));
255 // Initialize the writer contexts.
256 SmallVector
<std::unique_ptr
<WriterContext
>, 4> Contexts
;
257 for (unsigned I
= 0; I
< NumThreads
; ++I
)
258 Contexts
.emplace_back(llvm::make_unique
<WriterContext
>(
259 OutputSparse
, ErrorLock
, WriterErrorCodes
));
261 if (NumThreads
== 1) {
262 for (const auto &Input
: Inputs
)
263 loadInput(Input
, Contexts
[0].get());
265 ThreadPool
Pool(NumThreads
);
267 // Load the inputs in parallel (N/NumThreads serial steps).
269 for (const auto &Input
: Inputs
) {
270 Pool
.async(loadInput
, Input
, Contexts
[Ctx
].get());
271 Ctx
= (Ctx
+ 1) % NumThreads
;
275 // Merge the writer contexts together (~ lg(NumThreads) serial steps).
276 unsigned Mid
= Contexts
.size() / 2;
277 unsigned End
= Contexts
.size();
278 assert(Mid
> 0 && "Expected more than one context");
280 for (unsigned I
= 0; I
< Mid
; ++I
)
281 Pool
.async(mergeWriterContexts
, Contexts
[I
].get(),
282 Contexts
[I
+ Mid
].get());
285 Pool
.async(mergeWriterContexts
, Contexts
[0].get(),
286 Contexts
[End
- 1].get());
294 // Handle deferred hard errors encountered during merging.
295 for (std::unique_ptr
<WriterContext
> &WC
: Contexts
) {
298 if (!WC
->Err
.isA
<InstrProfError
>())
299 exitWithError(std::move(WC
->Err
), WC
->ErrWhence
);
301 instrprof_error IPE
= InstrProfError::take(std::move(WC
->Err
));
302 if (isFatalError(IPE
))
303 exitWithError(make_error
<InstrProfError
>(IPE
), WC
->ErrWhence
);
305 warn(toString(make_error
<InstrProfError
>(IPE
)),
309 InstrProfWriter
&Writer
= Contexts
[0]->Writer
;
310 if (OutputFormat
== PF_Text
) {
311 if (Error E
= Writer
.writeText(Output
))
312 exitWithError(std::move(E
));
314 Writer
.write(Output
);
318 static sampleprof::SampleProfileFormat FormatMap
[] = {
319 sampleprof::SPF_None
, sampleprof::SPF_Text
, sampleprof::SPF_Binary
,
320 sampleprof::SPF_GCC
};
322 static void mergeSampleProfile(const WeightedFileVector
&Inputs
,
323 StringRef OutputFilename
,
324 ProfileFormat OutputFormat
) {
325 using namespace sampleprof
;
327 SampleProfileWriter::create(OutputFilename
, FormatMap
[OutputFormat
]);
328 if (std::error_code EC
= WriterOrErr
.getError())
329 exitWithErrorCode(EC
, OutputFilename
);
331 auto Writer
= std::move(WriterOrErr
.get());
332 StringMap
<FunctionSamples
> ProfileMap
;
333 SmallVector
<std::unique_ptr
<sampleprof::SampleProfileReader
>, 5> Readers
;
335 for (const auto &Input
: Inputs
) {
336 auto ReaderOrErr
= SampleProfileReader::create(Input
.Filename
, Context
);
337 if (std::error_code EC
= ReaderOrErr
.getError())
338 exitWithErrorCode(EC
, Input
.Filename
);
340 // We need to keep the readers around until after all the files are
341 // read so that we do not lose the function names stored in each
342 // reader's memory. The function names are needed to write out the
343 // merged profile map.
344 Readers
.push_back(std::move(ReaderOrErr
.get()));
345 const auto Reader
= Readers
.back().get();
346 if (std::error_code EC
= Reader
->read())
347 exitWithErrorCode(EC
, Input
.Filename
);
349 StringMap
<FunctionSamples
> &Profiles
= Reader
->getProfiles();
350 for (StringMap
<FunctionSamples
>::iterator I
= Profiles
.begin(),
353 StringRef FName
= I
->first();
354 FunctionSamples
&Samples
= I
->second
;
355 sampleprof_error Result
= ProfileMap
[FName
].merge(Samples
, Input
.Weight
);
356 if (Result
!= sampleprof_error::success
) {
357 std::error_code EC
= make_error_code(Result
);
358 handleMergeWriterError(errorCodeToError(EC
), Input
.Filename
, FName
);
362 Writer
->write(ProfileMap
);
365 static WeightedFile
parseWeightedFile(const StringRef
&WeightedFilename
) {
366 StringRef WeightStr
, FileName
;
367 std::tie(WeightStr
, FileName
) = WeightedFilename
.split(',');
370 if (WeightStr
.getAsInteger(10, Weight
) || Weight
< 1)
371 exitWithError("Input weight must be a positive integer.");
373 return {FileName
, Weight
};
376 static std::unique_ptr
<MemoryBuffer
>
377 getInputFilenamesFileBuf(const StringRef
&InputFilenamesFile
) {
378 if (InputFilenamesFile
== "")
381 auto BufOrError
= MemoryBuffer::getFileOrSTDIN(InputFilenamesFile
);
383 exitWithErrorCode(BufOrError
.getError(), InputFilenamesFile
);
385 return std::move(*BufOrError
);
388 static void addWeightedInput(WeightedFileVector
&WNI
, const WeightedFile
&WF
) {
389 StringRef Filename
= WF
.Filename
;
390 uint64_t Weight
= WF
.Weight
;
392 // If it's STDIN just pass it on.
393 if (Filename
== "-") {
394 WNI
.push_back({Filename
, Weight
});
398 llvm::sys::fs::file_status Status
;
399 llvm::sys::fs::status(Filename
, Status
);
400 if (!llvm::sys::fs::exists(Status
))
401 exitWithErrorCode(make_error_code(errc::no_such_file_or_directory
),
403 // If it's a source file, collect it.
404 if (llvm::sys::fs::is_regular_file(Status
)) {
405 WNI
.push_back({Filename
, Weight
});
409 if (llvm::sys::fs::is_directory(Status
)) {
411 for (llvm::sys::fs::recursive_directory_iterator
F(Filename
, EC
), E
;
412 F
!= E
&& !EC
; F
.increment(EC
)) {
413 if (llvm::sys::fs::is_regular_file(F
->path())) {
414 addWeightedInput(WNI
, {F
->path(), Weight
});
418 exitWithErrorCode(EC
, Filename
);
422 static void parseInputFilenamesFile(MemoryBuffer
*Buffer
,
423 WeightedFileVector
&WFV
) {
427 SmallVector
<StringRef
, 8> Entries
;
428 StringRef Data
= Buffer
->getBuffer();
429 Data
.split(Entries
, '\n', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
430 for (const StringRef
&FileWeightEntry
: Entries
) {
431 StringRef SanitizedEntry
= FileWeightEntry
.trim(" \t\v\f\r");
433 if (SanitizedEntry
.startswith("#"))
435 // If there's no comma, it's an unweighted profile.
436 else if (SanitizedEntry
.find(',') == StringRef::npos
)
437 addWeightedInput(WFV
, {SanitizedEntry
, 1});
439 addWeightedInput(WFV
, parseWeightedFile(SanitizedEntry
));
443 static int merge_main(int argc
, const char *argv
[]) {
444 cl::list
<std::string
> InputFilenames(cl::Positional
,
445 cl::desc("<filename...>"));
446 cl::list
<std::string
> WeightedInputFilenames("weighted-input",
447 cl::desc("<weight>,<filename>"));
448 cl::opt
<std::string
> InputFilenamesFile(
449 "input-files", cl::init(""),
450 cl::desc("Path to file containing newline-separated "
451 "[<weight>,]<filename> entries"));
452 cl::alias
InputFilenamesFileA("f", cl::desc("Alias for --input-files"),
453 cl::aliasopt(InputFilenamesFile
));
454 cl::opt
<bool> DumpInputFileList(
455 "dump-input-file-list", cl::init(false), cl::Hidden
,
456 cl::desc("Dump the list of input files and their weights, then exit"));
457 cl::opt
<std::string
> OutputFilename("output", cl::value_desc("output"),
458 cl::init("-"), cl::Required
,
459 cl::desc("Output file"));
460 cl::alias
OutputFilenameA("o", cl::desc("Alias for --output"),
461 cl::aliasopt(OutputFilename
));
462 cl::opt
<ProfileKinds
> ProfileKind(
463 cl::desc("Profile kind:"), cl::init(instr
),
464 cl::values(clEnumVal(instr
, "Instrumentation profile (default)"),
465 clEnumVal(sample
, "Sample profile")));
466 cl::opt
<ProfileFormat
> OutputFormat(
467 cl::desc("Format of output profile"), cl::init(PF_Binary
),
468 cl::values(clEnumValN(PF_Binary
, "binary", "Binary encoding (default)"),
469 clEnumValN(PF_Text
, "text", "Text encoding"),
470 clEnumValN(PF_GCC
, "gcc",
471 "GCC encoding (only meaningful for -sample)")));
472 cl::opt
<bool> OutputSparse("sparse", cl::init(false),
473 cl::desc("Generate a sparse profile (only meaningful for -instr)"));
474 cl::opt
<unsigned> NumThreads(
475 "num-threads", cl::init(0),
476 cl::desc("Number of merge threads to use (default: autodetect)"));
477 cl::alias
NumThreadsA("j", cl::desc("Alias for --num-threads"),
478 cl::aliasopt(NumThreads
));
480 cl::ParseCommandLineOptions(argc
, argv
, "LLVM profile data merger\n");
482 WeightedFileVector WeightedInputs
;
483 for (StringRef Filename
: InputFilenames
)
484 addWeightedInput(WeightedInputs
, {Filename
, 1});
485 for (StringRef WeightedFilename
: WeightedInputFilenames
)
486 addWeightedInput(WeightedInputs
, parseWeightedFile(WeightedFilename
));
488 // Make sure that the file buffer stays alive for the duration of the
489 // weighted input vector's lifetime.
490 auto Buffer
= getInputFilenamesFileBuf(InputFilenamesFile
);
491 parseInputFilenamesFile(Buffer
.get(), WeightedInputs
);
493 if (WeightedInputs
.empty())
494 exitWithError("No input files specified. See " +
495 sys::path::filename(argv
[0]) + " -help");
497 if (DumpInputFileList
) {
498 for (auto &WF
: WeightedInputs
)
499 outs() << WF
.Weight
<< "," << WF
.Filename
<< "\n";
503 if (ProfileKind
== instr
)
504 mergeInstrProfile(WeightedInputs
, OutputFilename
, OutputFormat
,
505 OutputSparse
, NumThreads
);
507 mergeSampleProfile(WeightedInputs
, OutputFilename
, OutputFormat
);
512 typedef struct ValueSitesStats
{
514 : TotalNumValueSites(0), TotalNumValueSitesWithValueProfile(0),
516 uint64_t TotalNumValueSites
;
517 uint64_t TotalNumValueSitesWithValueProfile
;
518 uint64_t TotalNumValues
;
519 std::vector
<unsigned> ValueSitesHistogram
;
522 static void traverseAllValueSites(const InstrProfRecord
&Func
, uint32_t VK
,
523 ValueSitesStats
&Stats
, raw_fd_ostream
&OS
,
524 InstrProfSymtab
*Symtab
) {
525 uint32_t NS
= Func
.getNumValueSites(VK
);
526 Stats
.TotalNumValueSites
+= NS
;
527 for (size_t I
= 0; I
< NS
; ++I
) {
528 uint32_t NV
= Func
.getNumValueDataForSite(VK
, I
);
529 std::unique_ptr
<InstrProfValueData
[]> VD
= Func
.getValueForSite(VK
, I
);
530 Stats
.TotalNumValues
+= NV
;
532 Stats
.TotalNumValueSitesWithValueProfile
++;
533 if (NV
> Stats
.ValueSitesHistogram
.size())
534 Stats
.ValueSitesHistogram
.resize(NV
, 0);
535 Stats
.ValueSitesHistogram
[NV
- 1]++;
537 for (uint32_t V
= 0; V
< NV
; V
++) {
538 OS
<< "\t[ " << I
<< ", ";
539 if (Symtab
== nullptr)
542 OS
<< Symtab
->getFuncName(VD
[V
].Value
);
543 OS
<< ", " << VD
[V
].Count
<< " ]\n";
548 static void showValueSitesStats(raw_fd_ostream
&OS
, uint32_t VK
,
549 ValueSitesStats
&Stats
) {
550 OS
<< " Total number of sites: " << Stats
.TotalNumValueSites
<< "\n";
551 OS
<< " Total number of sites with values: "
552 << Stats
.TotalNumValueSitesWithValueProfile
<< "\n";
553 OS
<< " Total number of profiled values: " << Stats
.TotalNumValues
<< "\n";
555 OS
<< " Value sites histogram:\n\tNumTargets, SiteCount\n";
556 for (unsigned I
= 0; I
< Stats
.ValueSitesHistogram
.size(); I
++) {
557 if (Stats
.ValueSitesHistogram
[I
] > 0)
558 OS
<< "\t" << I
+ 1 << ", " << Stats
.ValueSitesHistogram
[I
] << "\n";
562 static int showInstrProfile(const std::string
&Filename
, bool ShowCounts
,
563 uint32_t TopN
, bool ShowIndirectCallTargets
,
564 bool ShowMemOPSizes
, bool ShowDetailedSummary
,
565 std::vector
<uint32_t> DetailedSummaryCutoffs
,
566 bool ShowAllFunctions
,
567 const std::string
&ShowFunction
, bool TextFormat
,
568 raw_fd_ostream
&OS
) {
569 auto ReaderOrErr
= InstrProfReader::create(Filename
);
570 std::vector
<uint32_t> Cutoffs
= std::move(DetailedSummaryCutoffs
);
571 if (ShowDetailedSummary
&& Cutoffs
.empty()) {
572 Cutoffs
= {800000, 900000, 950000, 990000, 999000, 999900, 999990};
574 InstrProfSummaryBuilder
Builder(std::move(Cutoffs
));
575 if (Error E
= ReaderOrErr
.takeError())
576 exitWithError(std::move(E
), Filename
);
578 auto Reader
= std::move(ReaderOrErr
.get());
579 bool IsIRInstr
= Reader
->isIRLevelProfile();
580 size_t ShownFunctions
= 0;
581 int NumVPKind
= IPVK_Last
- IPVK_First
+ 1;
582 std::vector
<ValueSitesStats
> VPStats(NumVPKind
);
584 auto MinCmp
= [](const std::pair
<std::string
, uint64_t> &v1
,
585 const std::pair
<std::string
, uint64_t> &v2
) {
586 return v1
.second
> v2
.second
;
589 std::priority_queue
<std::pair
<std::string
, uint64_t>,
590 std::vector
<std::pair
<std::string
, uint64_t>>,
592 HottestFuncs(MinCmp
);
594 for (const auto &Func
: *Reader
) {
596 ShowAllFunctions
|| (!ShowFunction
.empty() &&
597 Func
.Name
.find(ShowFunction
) != Func
.Name
.npos
);
599 bool doTextFormatDump
= (Show
&& ShowCounts
&& TextFormat
);
601 if (doTextFormatDump
) {
602 InstrProfSymtab
&Symtab
= Reader
->getSymtab();
603 InstrProfWriter::writeRecordInText(Func
.Name
, Func
.Hash
, Func
, Symtab
,
608 assert(Func
.Counts
.size() > 0 && "function missing entry counter");
609 Builder
.addRecord(Func
);
612 uint64_t FuncMax
= 0;
613 for (size_t I
= 0, E
= Func
.Counts
.size(); I
< E
; ++I
)
614 FuncMax
= std::max(FuncMax
, Func
.Counts
[I
]);
616 if (HottestFuncs
.size() == TopN
) {
617 if (HottestFuncs
.top().second
< FuncMax
) {
619 HottestFuncs
.emplace(std::make_pair(std::string(Func
.Name
), FuncMax
));
622 HottestFuncs
.emplace(std::make_pair(std::string(Func
.Name
), FuncMax
));
632 OS
<< " " << Func
.Name
<< ":\n"
633 << " Hash: " << format("0x%016" PRIx64
, Func
.Hash
) << "\n"
634 << " Counters: " << Func
.Counts
.size() << "\n";
636 OS
<< " Function count: " << Func
.Counts
[0] << "\n";
638 if (ShowIndirectCallTargets
)
639 OS
<< " Indirect Call Site Count: "
640 << Func
.getNumValueSites(IPVK_IndirectCallTarget
) << "\n";
642 uint32_t NumMemOPCalls
= Func
.getNumValueSites(IPVK_MemOPSize
);
643 if (ShowMemOPSizes
&& NumMemOPCalls
> 0)
644 OS
<< " Number of Memory Intrinsics Calls: " << NumMemOPCalls
648 OS
<< " Block counts: [";
649 size_t Start
= (IsIRInstr
? 0 : 1);
650 for (size_t I
= Start
, E
= Func
.Counts
.size(); I
< E
; ++I
) {
651 OS
<< (I
== Start
? "" : ", ") << Func
.Counts
[I
];
656 if (ShowIndirectCallTargets
) {
657 OS
<< " Indirect Target Results:\n";
658 traverseAllValueSites(Func
, IPVK_IndirectCallTarget
,
659 VPStats
[IPVK_IndirectCallTarget
], OS
,
660 &(Reader
->getSymtab()));
663 if (ShowMemOPSizes
&& NumMemOPCalls
> 0) {
664 OS
<< " Memory Intrinsic Size Results:\n";
665 traverseAllValueSites(Func
, IPVK_MemOPSize
, VPStats
[IPVK_MemOPSize
], OS
,
670 if (Reader
->hasError())
671 exitWithError(Reader
->getError(), Filename
);
673 if (ShowCounts
&& TextFormat
)
675 std::unique_ptr
<ProfileSummary
> PS(Builder
.getSummary());
676 OS
<< "Instrumentation level: "
677 << (Reader
->isIRLevelProfile() ? "IR" : "Front-end") << "\n";
678 if (ShowAllFunctions
|| !ShowFunction
.empty())
679 OS
<< "Functions shown: " << ShownFunctions
<< "\n";
680 OS
<< "Total functions: " << PS
->getNumFunctions() << "\n";
681 OS
<< "Maximum function count: " << PS
->getMaxFunctionCount() << "\n";
682 OS
<< "Maximum internal block count: " << PS
->getMaxInternalCount() << "\n";
685 std::vector
<std::pair
<std::string
, uint64_t>> SortedHottestFuncs
;
686 while (!HottestFuncs
.empty()) {
687 SortedHottestFuncs
.emplace_back(HottestFuncs
.top());
691 << " functions with the largest internal block counts: \n";
692 for (auto &hotfunc
: llvm::reverse(SortedHottestFuncs
))
693 OS
<< " " << hotfunc
.first
<< ", max count = " << hotfunc
.second
<< "\n";
696 if (ShownFunctions
&& ShowIndirectCallTargets
) {
697 OS
<< "Statistics for indirect call sites profile:\n";
698 showValueSitesStats(OS
, IPVK_IndirectCallTarget
,
699 VPStats
[IPVK_IndirectCallTarget
]);
702 if (ShownFunctions
&& ShowMemOPSizes
) {
703 OS
<< "Statistics for memory intrinsic calls sizes profile:\n";
704 showValueSitesStats(OS
, IPVK_MemOPSize
, VPStats
[IPVK_MemOPSize
]);
707 if (ShowDetailedSummary
) {
708 OS
<< "Detailed summary:\n";
709 OS
<< "Total number of blocks: " << PS
->getNumCounts() << "\n";
710 OS
<< "Total count: " << PS
->getTotalCount() << "\n";
711 for (auto Entry
: PS
->getDetailedSummary()) {
712 OS
<< Entry
.NumCounts
<< " blocks with count >= " << Entry
.MinCount
714 << format("%0.6g", (float)Entry
.Cutoff
/ ProfileSummary::Scale
* 100)
715 << " percentage of the total counts.\n";
721 static int showSampleProfile(const std::string
&Filename
, bool ShowCounts
,
722 bool ShowAllFunctions
,
723 const std::string
&ShowFunction
,
724 raw_fd_ostream
&OS
) {
725 using namespace sampleprof
;
727 auto ReaderOrErr
= SampleProfileReader::create(Filename
, Context
);
728 if (std::error_code EC
= ReaderOrErr
.getError())
729 exitWithErrorCode(EC
, Filename
);
731 auto Reader
= std::move(ReaderOrErr
.get());
732 if (std::error_code EC
= Reader
->read())
733 exitWithErrorCode(EC
, Filename
);
735 if (ShowAllFunctions
|| ShowFunction
.empty())
738 Reader
->dumpFunctionProfile(ShowFunction
, OS
);
743 static int show_main(int argc
, const char *argv
[]) {
744 cl::opt
<std::string
> Filename(cl::Positional
, cl::Required
,
745 cl::desc("<profdata-file>"));
747 cl::opt
<bool> ShowCounts("counts", cl::init(false),
748 cl::desc("Show counter values for shown functions"));
749 cl::opt
<bool> TextFormat(
750 "text", cl::init(false),
751 cl::desc("Show instr profile data in text dump format"));
752 cl::opt
<bool> ShowIndirectCallTargets(
753 "ic-targets", cl::init(false),
754 cl::desc("Show indirect call site target values for shown functions"));
755 cl::opt
<bool> ShowMemOPSizes(
756 "memop-sizes", cl::init(false),
757 cl::desc("Show the profiled sizes of the memory intrinsic calls "
758 "for shown functions"));
759 cl::opt
<bool> ShowDetailedSummary("detailed-summary", cl::init(false),
760 cl::desc("Show detailed profile summary"));
761 cl::list
<uint32_t> DetailedSummaryCutoffs(
762 cl::CommaSeparated
, "detailed-summary-cutoffs",
764 "Cutoff percentages (times 10000) for generating detailed summary"),
765 cl::value_desc("800000,901000,999999"));
766 cl::opt
<bool> ShowAllFunctions("all-functions", cl::init(false),
767 cl::desc("Details for every function"));
768 cl::opt
<std::string
> ShowFunction("function",
769 cl::desc("Details for matching functions"));
771 cl::opt
<std::string
> OutputFilename("output", cl::value_desc("output"),
772 cl::init("-"), cl::desc("Output file"));
773 cl::alias
OutputFilenameA("o", cl::desc("Alias for --output"),
774 cl::aliasopt(OutputFilename
));
775 cl::opt
<ProfileKinds
> ProfileKind(
776 cl::desc("Profile kind:"), cl::init(instr
),
777 cl::values(clEnumVal(instr
, "Instrumentation profile (default)"),
778 clEnumVal(sample
, "Sample profile")));
779 cl::opt
<uint32_t> TopNFunctions(
781 cl::desc("Show the list of functions with the largest internal counts"));
783 cl::ParseCommandLineOptions(argc
, argv
, "LLVM profile data summary\n");
785 if (OutputFilename
.empty())
786 OutputFilename
= "-";
789 raw_fd_ostream
OS(OutputFilename
.data(), EC
, sys::fs::F_Text
);
791 exitWithErrorCode(EC
, OutputFilename
);
793 if (ShowAllFunctions
&& !ShowFunction
.empty())
794 WithColor::warning() << "-function argument ignored: showing all functions\n";
796 std::vector
<uint32_t> Cutoffs(DetailedSummaryCutoffs
.begin(),
797 DetailedSummaryCutoffs
.end());
798 if (ProfileKind
== instr
)
799 return showInstrProfile(Filename
, ShowCounts
, TopNFunctions
,
800 ShowIndirectCallTargets
, ShowMemOPSizes
,
801 ShowDetailedSummary
, DetailedSummaryCutoffs
,
802 ShowAllFunctions
, ShowFunction
, TextFormat
, OS
);
804 return showSampleProfile(Filename
, ShowCounts
, ShowAllFunctions
,
808 int main(int argc
, const char *argv
[]) {
809 InitLLVM
X(argc
, argv
);
811 StringRef
ProgName(sys::path::filename(argv
[0]));
813 int (*func
)(int, const char *[]) = nullptr;
815 if (strcmp(argv
[1], "merge") == 0)
817 else if (strcmp(argv
[1], "show") == 0)
821 std::string
Invocation(ProgName
.str() + " " + argv
[1]);
822 argv
[1] = Invocation
.c_str();
823 return func(argc
- 1, argv
+ 1);
826 if (strcmp(argv
[1], "-h") == 0 || strcmp(argv
[1], "-help") == 0 ||
827 strcmp(argv
[1], "--help") == 0) {
829 errs() << "OVERVIEW: LLVM profile data tools\n\n"
830 << "USAGE: " << ProgName
<< " <command> [args...]\n"
831 << "USAGE: " << ProgName
<< " <command> -help\n\n"
832 << "See each individual command --help for more details.\n"
833 << "Available commands: merge, show\n";
839 errs() << ProgName
<< ": No command specified!\n";
841 errs() << ProgName
<< ": Unknown command!\n";
843 errs() << "USAGE: " << ProgName
<< " <merge|show> [args...]\n";