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/ThreadPool.h" 33 #include "llvm/Support/raw_ostream.h" 34 #include <algorithm> 35 36 using namespace llvm; 37 38 enum ProfileFormat { PF_None = 0, PF_Text, PF_Binary, PF_GCC }; 39 40 static void exitWithError(const Twine &Message, StringRef Whence = "", 41 StringRef Hint = "") { 42 errs() << "error: "; 43 if (!Whence.empty()) 44 errs() << Whence << ": "; 45 errs() << Message << "\n"; 46 if (!Hint.empty()) 47 errs() << Hint << "\n"; 48 ::exit(1); 49 } 50 51 static void exitWithError(Error E, StringRef Whence = "") { 52 if (E.isA<InstrProfError>()) { 53 handleAllErrors(std::move(E), [&](const InstrProfError &IPE) { 54 instrprof_error instrError = IPE.get(); 55 StringRef Hint = ""; 56 if (instrError == instrprof_error::unrecognized_format) { 57 // Hint for common error of forgetting -sample for sample profiles. 58 Hint = "Perhaps you forgot to use the -sample option?"; 59 } 60 exitWithError(IPE.message(), Whence, Hint); 61 }); 62 } 63 64 exitWithError(toString(std::move(E)), Whence); 65 } 66 67 static void exitWithErrorCode(std::error_code EC, StringRef Whence = "") { 68 exitWithError(EC.message(), Whence); 69 } 70 71 namespace { 72 enum ProfileKinds { instr, sample }; 73 } 74 75 static void handleMergeWriterError(Error E, StringRef WhenceFile = "", 76 StringRef WhenceFunction = "", 77 bool ShowHint = true) { 78 if (!WhenceFile.empty()) 79 errs() << WhenceFile << ": "; 80 if (!WhenceFunction.empty()) 81 errs() << WhenceFunction << ": "; 82 83 auto IPE = instrprof_error::success; 84 E = handleErrors(std::move(E), 85 [&IPE](std::unique_ptr<InstrProfError> E) -> Error { 86 IPE = E->get(); 87 return Error(std::move(E)); 88 }); 89 errs() << toString(std::move(E)) << "\n"; 90 91 if (ShowHint) { 92 StringRef Hint = ""; 93 if (IPE != instrprof_error::success) { 94 switch (IPE) { 95 case instrprof_error::hash_mismatch: 96 case instrprof_error::count_mismatch: 97 case instrprof_error::value_site_count_mismatch: 98 Hint = "Make sure that all profile data to be merged is generated " 99 "from the same binary."; 100 break; 101 default: 102 break; 103 } 104 } 105 106 if (!Hint.empty()) 107 errs() << Hint << "\n"; 108 } 109 } 110 111 struct WeightedFile { 112 std::string Filename; 113 uint64_t Weight; 114 }; 115 typedef SmallVector<WeightedFile, 5> WeightedFileVector; 116 117 /// Keep track of merged data and reported errors. 118 struct WriterContext { 119 std::mutex Lock; 120 InstrProfWriter Writer; 121 Error Err; 122 StringRef ErrWhence; 123 std::mutex &ErrLock; 124 SmallSet<instrprof_error, 4> &WriterErrorCodes; 125 126 WriterContext(bool IsSparse, std::mutex &ErrLock, 127 SmallSet<instrprof_error, 4> &WriterErrorCodes) 128 : Lock(), Writer(IsSparse), Err(Error::success()), ErrWhence(""), 129 ErrLock(ErrLock), WriterErrorCodes(WriterErrorCodes) {} 130 }; 131 132 /// Load an input into a writer context. 133 static void loadInput(const WeightedFile &Input, WriterContext *WC) { 134 std::unique_lock<std::mutex> CtxGuard{WC->Lock}; 135 136 // If there's a pending hard error, don't do more work. 137 if (WC->Err) 138 return; 139 140 WC->ErrWhence = Input.Filename; 141 142 auto ReaderOrErr = InstrProfReader::create(Input.Filename); 143 if (Error E = ReaderOrErr.takeError()) { 144 // Skip the empty profiles by returning sliently. 145 instrprof_error IPE = InstrProfError::take(std::move(E)); 146 if (IPE != instrprof_error::empty_raw_profile) 147 WC->Err = make_error<InstrProfError>(IPE); 148 return; 149 } 150 151 auto Reader = std::move(ReaderOrErr.get()); 152 bool IsIRProfile = Reader->isIRLevelProfile(); 153 if (WC->Writer.setIsIRLevelProfile(IsIRProfile)) { 154 WC->Err = make_error<StringError>( 155 "Merge IR generated profile with Clang generated profile.", 156 std::error_code()); 157 return; 158 } 159 160 for (auto &I : *Reader) { 161 const StringRef FuncName = I.Name; 162 bool Reported = false; 163 WC->Writer.addRecord(std::move(I), Input.Weight, [&](Error E) { 164 if (Reported) { 165 consumeError(std::move(E)); 166 return; 167 } 168 Reported = true; 169 // Only show hint the first time an error occurs. 170 instrprof_error IPE = InstrProfError::take(std::move(E)); 171 std::unique_lock<std::mutex> ErrGuard{WC->ErrLock}; 172 bool firstTime = WC->WriterErrorCodes.insert(IPE).second; 173 handleMergeWriterError(make_error<InstrProfError>(IPE), Input.Filename, 174 FuncName, firstTime); 175 }); 176 } 177 if (Reader->hasError()) 178 WC->Err = Reader->getError(); 179 } 180 181 /// Merge the \p Src writer context into \p Dst. 182 static void mergeWriterContexts(WriterContext *Dst, WriterContext *Src) { 183 bool Reported = false; 184 Dst->Writer.mergeRecordsFromWriter(std::move(Src->Writer), [&](Error E) { 185 if (Reported) { 186 consumeError(std::move(E)); 187 return; 188 } 189 Reported = true; 190 Dst->Err = std::move(E); 191 }); 192 } 193 194 static void mergeInstrProfile(const WeightedFileVector &Inputs, 195 StringRef OutputFilename, 196 ProfileFormat OutputFormat, bool OutputSparse, 197 unsigned NumThreads) { 198 if (OutputFilename.compare("-") == 0) 199 exitWithError("Cannot write indexed profdata format to stdout."); 200 201 if (OutputFormat != PF_Binary && OutputFormat != PF_Text) 202 exitWithError("Unknown format is specified."); 203 204 std::error_code EC; 205 raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None); 206 if (EC) 207 exitWithErrorCode(EC, OutputFilename); 208 209 std::mutex ErrorLock; 210 SmallSet<instrprof_error, 4> WriterErrorCodes; 211 212 // If NumThreads is not specified, auto-detect a good default. 213 if (NumThreads == 0) 214 NumThreads = 215 std::min(hardware_concurrency(), unsigned((Inputs.size() + 1) / 2)); 216 217 // Initialize the writer contexts. 218 SmallVector<std::unique_ptr<WriterContext>, 4> Contexts; 219 for (unsigned I = 0; I < NumThreads; ++I) 220 Contexts.emplace_back(llvm::make_unique<WriterContext>( 221 OutputSparse, ErrorLock, WriterErrorCodes)); 222 223 if (NumThreads == 1) { 224 for (const auto &Input : Inputs) 225 loadInput(Input, Contexts[0].get()); 226 } else { 227 ThreadPool Pool(NumThreads); 228 229 // Load the inputs in parallel (N/NumThreads serial steps). 230 unsigned Ctx = 0; 231 for (const auto &Input : Inputs) { 232 Pool.async(loadInput, Input, Contexts[Ctx].get()); 233 Ctx = (Ctx + 1) % NumThreads; 234 } 235 Pool.wait(); 236 237 // Merge the writer contexts together (~ lg(NumThreads) serial steps). 238 unsigned Mid = Contexts.size() / 2; 239 unsigned End = Contexts.size(); 240 assert(Mid > 0 && "Expected more than one context"); 241 do { 242 for (unsigned I = 0; I < Mid; ++I) 243 Pool.async(mergeWriterContexts, Contexts[I].get(), 244 Contexts[I + Mid].get()); 245 Pool.wait(); 246 if (End & 1) { 247 Pool.async(mergeWriterContexts, Contexts[0].get(), 248 Contexts[End - 1].get()); 249 Pool.wait(); 250 } 251 End = Mid; 252 Mid /= 2; 253 } while (Mid > 0); 254 } 255 256 // Handle deferred hard errors encountered during merging. 257 for (std::unique_ptr<WriterContext> &WC : Contexts) 258 if (WC->Err) 259 exitWithError(std::move(WC->Err), WC->ErrWhence); 260 261 InstrProfWriter &Writer = Contexts[0]->Writer; 262 if (OutputFormat == PF_Text) { 263 if (Error E = Writer.writeText(Output)) 264 exitWithError(std::move(E)); 265 } else { 266 Writer.write(Output); 267 } 268 } 269 270 static sampleprof::SampleProfileFormat FormatMap[] = { 271 sampleprof::SPF_None, sampleprof::SPF_Text, sampleprof::SPF_Binary, 272 sampleprof::SPF_GCC}; 273 274 static void mergeSampleProfile(const WeightedFileVector &Inputs, 275 StringRef OutputFilename, 276 ProfileFormat OutputFormat) { 277 using namespace sampleprof; 278 auto WriterOrErr = 279 SampleProfileWriter::create(OutputFilename, FormatMap[OutputFormat]); 280 if (std::error_code EC = WriterOrErr.getError()) 281 exitWithErrorCode(EC, OutputFilename); 282 283 auto Writer = std::move(WriterOrErr.get()); 284 StringMap<FunctionSamples> ProfileMap; 285 SmallVector<std::unique_ptr<sampleprof::SampleProfileReader>, 5> Readers; 286 LLVMContext Context; 287 for (const auto &Input : Inputs) { 288 auto ReaderOrErr = SampleProfileReader::create(Input.Filename, Context); 289 if (std::error_code EC = ReaderOrErr.getError()) 290 exitWithErrorCode(EC, Input.Filename); 291 292 // We need to keep the readers around until after all the files are 293 // read so that we do not lose the function names stored in each 294 // reader's memory. The function names are needed to write out the 295 // merged profile map. 296 Readers.push_back(std::move(ReaderOrErr.get())); 297 const auto Reader = Readers.back().get(); 298 if (std::error_code EC = Reader->read()) 299 exitWithErrorCode(EC, Input.Filename); 300 301 StringMap<FunctionSamples> &Profiles = Reader->getProfiles(); 302 for (StringMap<FunctionSamples>::iterator I = Profiles.begin(), 303 E = Profiles.end(); 304 I != E; ++I) { 305 StringRef FName = I->first(); 306 FunctionSamples &Samples = I->second; 307 sampleprof_error Result = ProfileMap[FName].merge(Samples, Input.Weight); 308 if (Result != sampleprof_error::success) { 309 std::error_code EC = make_error_code(Result); 310 handleMergeWriterError(errorCodeToError(EC), Input.Filename, FName); 311 } 312 } 313 } 314 Writer->write(ProfileMap); 315 } 316 317 static WeightedFile parseWeightedFile(const StringRef &WeightedFilename) { 318 StringRef WeightStr, FileName; 319 std::tie(WeightStr, FileName) = WeightedFilename.split(','); 320 321 uint64_t Weight; 322 if (WeightStr.getAsInteger(10, Weight) || Weight < 1) 323 exitWithError("Input weight must be a positive integer."); 324 325 return {FileName, Weight}; 326 } 327 328 static std::unique_ptr<MemoryBuffer> 329 getInputFilenamesFileBuf(const StringRef &InputFilenamesFile) { 330 if (InputFilenamesFile == "") 331 return {}; 332 333 auto BufOrError = MemoryBuffer::getFileOrSTDIN(InputFilenamesFile); 334 if (!BufOrError) 335 exitWithErrorCode(BufOrError.getError(), InputFilenamesFile); 336 337 return std::move(*BufOrError); 338 } 339 340 static void addWeightedInput(WeightedFileVector &WNI, const WeightedFile &WF) { 341 StringRef Filename = WF.Filename; 342 uint64_t Weight = WF.Weight; 343 344 // If it's STDIN just pass it on. 345 if (Filename == "-") { 346 WNI.push_back({Filename, Weight}); 347 return; 348 } 349 350 llvm::sys::fs::file_status Status; 351 llvm::sys::fs::status(Filename, Status); 352 if (!llvm::sys::fs::exists(Status)) 353 exitWithErrorCode(make_error_code(errc::no_such_file_or_directory), 354 Filename); 355 // If it's a source file, collect it. 356 if (llvm::sys::fs::is_regular_file(Status)) { 357 WNI.push_back({Filename, Weight}); 358 return; 359 } 360 361 if (llvm::sys::fs::is_directory(Status)) { 362 std::error_code EC; 363 for (llvm::sys::fs::recursive_directory_iterator F(Filename, EC), E; 364 F != E && !EC; F.increment(EC)) { 365 if (llvm::sys::fs::is_regular_file(F->path())) { 366 addWeightedInput(WNI, {F->path(), Weight}); 367 } 368 } 369 if (EC) 370 exitWithErrorCode(EC, Filename); 371 } 372 } 373 374 static void parseInputFilenamesFile(MemoryBuffer *Buffer, 375 WeightedFileVector &WFV) { 376 if (!Buffer) 377 return; 378 379 SmallVector<StringRef, 8> Entries; 380 StringRef Data = Buffer->getBuffer(); 381 Data.split(Entries, '\n', /*MaxSplit=*/-1, /*KeepEmpty=*/false); 382 for (const StringRef &FileWeightEntry : Entries) { 383 StringRef SanitizedEntry = FileWeightEntry.trim(" \t\v\f\r"); 384 // Skip comments. 385 if (SanitizedEntry.startswith("#")) 386 continue; 387 // If there's no comma, it's an unweighted profile. 388 else if (SanitizedEntry.find(',') == StringRef::npos) 389 addWeightedInput(WFV, {SanitizedEntry, 1}); 390 else 391 addWeightedInput(WFV, parseWeightedFile(SanitizedEntry)); 392 } 393 } 394 395 static int merge_main(int argc, const char *argv[]) { 396 cl::list<std::string> InputFilenames(cl::Positional, 397 cl::desc("<filename...>")); 398 cl::list<std::string> WeightedInputFilenames("weighted-input", 399 cl::desc("<weight>,<filename>")); 400 cl::opt<std::string> InputFilenamesFile( 401 "input-files", cl::init(""), 402 cl::desc("Path to file containing newline-separated " 403 "[<weight>,]<filename> entries")); 404 cl::alias InputFilenamesFileA("f", cl::desc("Alias for --input-files"), 405 cl::aliasopt(InputFilenamesFile)); 406 cl::opt<bool> DumpInputFileList( 407 "dump-input-file-list", cl::init(false), cl::Hidden, 408 cl::desc("Dump the list of input files and their weights, then exit")); 409 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"), 410 cl::init("-"), cl::Required, 411 cl::desc("Output file")); 412 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"), 413 cl::aliasopt(OutputFilename)); 414 cl::opt<ProfileKinds> ProfileKind( 415 cl::desc("Profile kind:"), cl::init(instr), 416 cl::values(clEnumVal(instr, "Instrumentation profile (default)"), 417 clEnumVal(sample, "Sample profile"))); 418 cl::opt<ProfileFormat> OutputFormat( 419 cl::desc("Format of output profile"), cl::init(PF_Binary), 420 cl::values(clEnumValN(PF_Binary, "binary", "Binary encoding (default)"), 421 clEnumValN(PF_Text, "text", "Text encoding"), 422 clEnumValN(PF_GCC, "gcc", 423 "GCC encoding (only meaningful for -sample)"))); 424 cl::opt<bool> OutputSparse("sparse", cl::init(false), 425 cl::desc("Generate a sparse profile (only meaningful for -instr)")); 426 cl::opt<unsigned> NumThreads( 427 "num-threads", cl::init(0), 428 cl::desc("Number of merge threads to use (default: autodetect)")); 429 cl::alias NumThreadsA("j", cl::desc("Alias for --num-threads"), 430 cl::aliasopt(NumThreads)); 431 432 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n"); 433 434 WeightedFileVector WeightedInputs; 435 for (StringRef Filename : InputFilenames) 436 addWeightedInput(WeightedInputs, {Filename, 1}); 437 for (StringRef WeightedFilename : WeightedInputFilenames) 438 addWeightedInput(WeightedInputs, parseWeightedFile(WeightedFilename)); 439 440 // Make sure that the file buffer stays alive for the duration of the 441 // weighted input vector's lifetime. 442 auto Buffer = getInputFilenamesFileBuf(InputFilenamesFile); 443 parseInputFilenamesFile(Buffer.get(), WeightedInputs); 444 445 if (WeightedInputs.empty()) 446 exitWithError("No input files specified. See " + 447 sys::path::filename(argv[0]) + " -help"); 448 449 if (DumpInputFileList) { 450 for (auto &WF : WeightedInputs) 451 outs() << WF.Weight << "," << WF.Filename << "\n"; 452 return 0; 453 } 454 455 if (ProfileKind == instr) 456 mergeInstrProfile(WeightedInputs, OutputFilename, OutputFormat, 457 OutputSparse, NumThreads); 458 else 459 mergeSampleProfile(WeightedInputs, OutputFilename, OutputFormat); 460 461 return 0; 462 } 463 464 typedef struct ValueSitesStats { 465 ValueSitesStats() 466 : TotalNumValueSites(0), TotalNumValueSitesWithValueProfile(0), 467 TotalNumValues(0) {} 468 uint64_t TotalNumValueSites; 469 uint64_t TotalNumValueSitesWithValueProfile; 470 uint64_t TotalNumValues; 471 std::vector<unsigned> ValueSitesHistogram; 472 } ValueSitesStats; 473 474 static void traverseAllValueSites(const InstrProfRecord &Func, uint32_t VK, 475 ValueSitesStats &Stats, raw_fd_ostream &OS, 476 InstrProfSymtab *Symtab) { 477 uint32_t NS = Func.getNumValueSites(VK); 478 Stats.TotalNumValueSites += NS; 479 for (size_t I = 0; I < NS; ++I) { 480 uint32_t NV = Func.getNumValueDataForSite(VK, I); 481 std::unique_ptr<InstrProfValueData[]> VD = Func.getValueForSite(VK, I); 482 Stats.TotalNumValues += NV; 483 if (NV) { 484 Stats.TotalNumValueSitesWithValueProfile++; 485 if (NV > Stats.ValueSitesHistogram.size()) 486 Stats.ValueSitesHistogram.resize(NV, 0); 487 Stats.ValueSitesHistogram[NV - 1]++; 488 } 489 for (uint32_t V = 0; V < NV; V++) { 490 OS << "\t[ " << I << ", "; 491 if (Symtab == nullptr) 492 OS << VD[V].Value; 493 else 494 OS << Symtab->getFuncName(VD[V].Value); 495 OS << ", " << VD[V].Count << " ]\n"; 496 } 497 } 498 } 499 500 static void showValueSitesStats(raw_fd_ostream &OS, uint32_t VK, 501 ValueSitesStats &Stats) { 502 OS << " Total number of sites: " << Stats.TotalNumValueSites << "\n"; 503 OS << " Total number of sites with values: " 504 << Stats.TotalNumValueSitesWithValueProfile << "\n"; 505 OS << " Total number of profiled values: " << Stats.TotalNumValues << "\n"; 506 507 OS << " Value sites histogram:\n\tNumTargets, SiteCount\n"; 508 for (unsigned I = 0; I < Stats.ValueSitesHistogram.size(); I++) { 509 if (Stats.ValueSitesHistogram[I] > 0) 510 OS << "\t" << I + 1 << ", " << Stats.ValueSitesHistogram[I] << "\n"; 511 } 512 } 513 514 static int showInstrProfile(const std::string &Filename, bool ShowCounts, 515 uint32_t TopN, bool ShowIndirectCallTargets, 516 bool ShowMemOPSizes, bool ShowDetailedSummary, 517 std::vector<uint32_t> DetailedSummaryCutoffs, 518 bool ShowAllFunctions, 519 const std::string &ShowFunction, bool TextFormat, 520 raw_fd_ostream &OS) { 521 auto ReaderOrErr = InstrProfReader::create(Filename); 522 std::vector<uint32_t> Cutoffs = std::move(DetailedSummaryCutoffs); 523 if (ShowDetailedSummary && Cutoffs.empty()) { 524 Cutoffs = {800000, 900000, 950000, 990000, 999000, 999900, 999990}; 525 } 526 InstrProfSummaryBuilder Builder(std::move(Cutoffs)); 527 if (Error E = ReaderOrErr.takeError()) 528 exitWithError(std::move(E), Filename); 529 530 auto Reader = std::move(ReaderOrErr.get()); 531 bool IsIRInstr = Reader->isIRLevelProfile(); 532 size_t ShownFunctions = 0; 533 int NumVPKind = IPVK_Last - IPVK_First + 1; 534 std::vector<ValueSitesStats> VPStats(NumVPKind); 535 536 auto MinCmp = [](const std::pair<std::string, uint64_t> &v1, 537 const std::pair<std::string, uint64_t> &v2) { 538 return v1.second > v2.second; 539 }; 540 541 std::priority_queue<std::pair<std::string, uint64_t>, 542 std::vector<std::pair<std::string, uint64_t>>, 543 decltype(MinCmp)> 544 HottestFuncs(MinCmp); 545 546 for (const auto &Func : *Reader) { 547 bool Show = 548 ShowAllFunctions || (!ShowFunction.empty() && 549 Func.Name.find(ShowFunction) != Func.Name.npos); 550 551 bool doTextFormatDump = (Show && ShowCounts && TextFormat); 552 553 if (doTextFormatDump) { 554 InstrProfSymtab &Symtab = Reader->getSymtab(); 555 InstrProfWriter::writeRecordInText(Func.Name, Func.Hash, Func, Symtab, 556 OS); 557 continue; 558 } 559 560 assert(Func.Counts.size() > 0 && "function missing entry counter"); 561 Builder.addRecord(Func); 562 563 if (TopN) { 564 uint64_t FuncMax = 0; 565 for (size_t I = 0, E = Func.Counts.size(); I < E; ++I) 566 FuncMax = std::max(FuncMax, Func.Counts[I]); 567 568 if (HottestFuncs.size() == TopN) { 569 if (HottestFuncs.top().second < FuncMax) { 570 HottestFuncs.pop(); 571 HottestFuncs.emplace(std::make_pair(std::string(Func.Name), FuncMax)); 572 } 573 } else 574 HottestFuncs.emplace(std::make_pair(std::string(Func.Name), FuncMax)); 575 } 576 577 if (Show) { 578 579 if (!ShownFunctions) 580 OS << "Counters:\n"; 581 582 ++ShownFunctions; 583 584 OS << " " << Func.Name << ":\n" 585 << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n" 586 << " Counters: " << Func.Counts.size() << "\n"; 587 if (!IsIRInstr) 588 OS << " Function count: " << Func.Counts[0] << "\n"; 589 590 if (ShowIndirectCallTargets) 591 OS << " Indirect Call Site Count: " 592 << Func.getNumValueSites(IPVK_IndirectCallTarget) << "\n"; 593 594 uint32_t NumMemOPCalls = Func.getNumValueSites(IPVK_MemOPSize); 595 if (ShowMemOPSizes && NumMemOPCalls > 0) 596 OS << " Number of Memory Intrinsics Calls: " << NumMemOPCalls 597 << "\n"; 598 599 if (ShowCounts) { 600 OS << " Block counts: ["; 601 size_t Start = (IsIRInstr ? 0 : 1); 602 for (size_t I = Start, E = Func.Counts.size(); I < E; ++I) { 603 OS << (I == Start ? "" : ", ") << Func.Counts[I]; 604 } 605 OS << "]\n"; 606 } 607 608 if (ShowIndirectCallTargets) { 609 OS << " Indirect Target Results:\n"; 610 traverseAllValueSites(Func, IPVK_IndirectCallTarget, 611 VPStats[IPVK_IndirectCallTarget], OS, 612 &(Reader->getSymtab())); 613 } 614 615 if (ShowMemOPSizes && NumMemOPCalls > 0) { 616 OS << " Memory Intrinsic Size Results:\n"; 617 traverseAllValueSites(Func, IPVK_MemOPSize, VPStats[IPVK_MemOPSize], OS, 618 nullptr); 619 } 620 } 621 } 622 if (Reader->hasError()) 623 exitWithError(Reader->getError(), Filename); 624 625 if (ShowCounts && TextFormat) 626 return 0; 627 std::unique_ptr<ProfileSummary> PS(Builder.getSummary()); 628 OS << "Instrumentation level: " 629 << (Reader->isIRLevelProfile() ? "IR" : "Front-end") << "\n"; 630 if (ShowAllFunctions || !ShowFunction.empty()) 631 OS << "Functions shown: " << ShownFunctions << "\n"; 632 OS << "Total functions: " << PS->getNumFunctions() << "\n"; 633 OS << "Maximum function count: " << PS->getMaxFunctionCount() << "\n"; 634 OS << "Maximum internal block count: " << PS->getMaxInternalCount() << "\n"; 635 636 if (TopN) { 637 std::vector<std::pair<std::string, uint64_t>> SortedHottestFuncs; 638 while (!HottestFuncs.empty()) { 639 SortedHottestFuncs.emplace_back(HottestFuncs.top()); 640 HottestFuncs.pop(); 641 } 642 OS << "Top " << TopN 643 << " functions with the largest internal block counts: \n"; 644 for (auto &hotfunc : llvm::reverse(SortedHottestFuncs)) 645 OS << " " << hotfunc.first << ", max count = " << hotfunc.second << "\n"; 646 } 647 648 if (ShownFunctions && ShowIndirectCallTargets) { 649 OS << "Statistics for indirect call sites profile:\n"; 650 showValueSitesStats(OS, IPVK_IndirectCallTarget, 651 VPStats[IPVK_IndirectCallTarget]); 652 } 653 654 if (ShownFunctions && ShowMemOPSizes) { 655 OS << "Statistics for memory intrinsic calls sizes profile:\n"; 656 showValueSitesStats(OS, IPVK_MemOPSize, VPStats[IPVK_MemOPSize]); 657 } 658 659 if (ShowDetailedSummary) { 660 OS << "Detailed summary:\n"; 661 OS << "Total number of blocks: " << PS->getNumCounts() << "\n"; 662 OS << "Total count: " << PS->getTotalCount() << "\n"; 663 for (auto Entry : PS->getDetailedSummary()) { 664 OS << Entry.NumCounts << " blocks with count >= " << Entry.MinCount 665 << " account for " 666 << format("%0.6g", (float)Entry.Cutoff / ProfileSummary::Scale * 100) 667 << " percentage of the total counts.\n"; 668 } 669 } 670 return 0; 671 } 672 673 static int showSampleProfile(const std::string &Filename, bool ShowCounts, 674 bool ShowAllFunctions, 675 const std::string &ShowFunction, 676 raw_fd_ostream &OS) { 677 using namespace sampleprof; 678 LLVMContext Context; 679 auto ReaderOrErr = SampleProfileReader::create(Filename, Context); 680 if (std::error_code EC = ReaderOrErr.getError()) 681 exitWithErrorCode(EC, Filename); 682 683 auto Reader = std::move(ReaderOrErr.get()); 684 if (std::error_code EC = Reader->read()) 685 exitWithErrorCode(EC, Filename); 686 687 if (ShowAllFunctions || ShowFunction.empty()) 688 Reader->dump(OS); 689 else 690 Reader->dumpFunctionProfile(ShowFunction, OS); 691 692 return 0; 693 } 694 695 static int show_main(int argc, const char *argv[]) { 696 cl::opt<std::string> Filename(cl::Positional, cl::Required, 697 cl::desc("<profdata-file>")); 698 699 cl::opt<bool> ShowCounts("counts", cl::init(false), 700 cl::desc("Show counter values for shown functions")); 701 cl::opt<bool> TextFormat( 702 "text", cl::init(false), 703 cl::desc("Show instr profile data in text dump format")); 704 cl::opt<bool> ShowIndirectCallTargets( 705 "ic-targets", cl::init(false), 706 cl::desc("Show indirect call site target values for shown functions")); 707 cl::opt<bool> ShowMemOPSizes( 708 "memop-sizes", cl::init(false), 709 cl::desc("Show the profiled sizes of the memory intrinsic calls " 710 "for shown functions")); 711 cl::opt<bool> ShowDetailedSummary("detailed-summary", cl::init(false), 712 cl::desc("Show detailed profile summary")); 713 cl::list<uint32_t> DetailedSummaryCutoffs( 714 cl::CommaSeparated, "detailed-summary-cutoffs", 715 cl::desc( 716 "Cutoff percentages (times 10000) for generating detailed summary"), 717 cl::value_desc("800000,901000,999999")); 718 cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false), 719 cl::desc("Details for every function")); 720 cl::opt<std::string> ShowFunction("function", 721 cl::desc("Details for matching functions")); 722 723 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"), 724 cl::init("-"), cl::desc("Output file")); 725 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"), 726 cl::aliasopt(OutputFilename)); 727 cl::opt<ProfileKinds> ProfileKind( 728 cl::desc("Profile kind:"), cl::init(instr), 729 cl::values(clEnumVal(instr, "Instrumentation profile (default)"), 730 clEnumVal(sample, "Sample profile"))); 731 cl::opt<uint32_t> TopNFunctions( 732 "topn", cl::init(0), 733 cl::desc("Show the list of functions with the largest internal counts")); 734 735 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n"); 736 737 if (OutputFilename.empty()) 738 OutputFilename = "-"; 739 740 std::error_code EC; 741 raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text); 742 if (EC) 743 exitWithErrorCode(EC, OutputFilename); 744 745 if (ShowAllFunctions && !ShowFunction.empty()) 746 errs() << "warning: -function argument ignored: showing all functions\n"; 747 748 std::vector<uint32_t> Cutoffs(DetailedSummaryCutoffs.begin(), 749 DetailedSummaryCutoffs.end()); 750 if (ProfileKind == instr) 751 return showInstrProfile(Filename, ShowCounts, TopNFunctions, 752 ShowIndirectCallTargets, ShowMemOPSizes, 753 ShowDetailedSummary, DetailedSummaryCutoffs, 754 ShowAllFunctions, ShowFunction, TextFormat, OS); 755 else 756 return showSampleProfile(Filename, ShowCounts, ShowAllFunctions, 757 ShowFunction, OS); 758 } 759 760 int main(int argc, const char *argv[]) { 761 // Print a stack trace if we signal out. 762 sys::PrintStackTraceOnErrorSignal(argv[0]); 763 PrettyStackTraceProgram X(argc, argv); 764 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 765 766 StringRef ProgName(sys::path::filename(argv[0])); 767 if (argc > 1) { 768 int (*func)(int, const char *[]) = nullptr; 769 770 if (strcmp(argv[1], "merge") == 0) 771 func = merge_main; 772 else if (strcmp(argv[1], "show") == 0) 773 func = show_main; 774 775 if (func) { 776 std::string Invocation(ProgName.str() + " " + argv[1]); 777 argv[1] = Invocation.c_str(); 778 return func(argc - 1, argv + 1); 779 } 780 781 if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-help") == 0 || 782 strcmp(argv[1], "--help") == 0) { 783 784 errs() << "OVERVIEW: LLVM profile data tools\n\n" 785 << "USAGE: " << ProgName << " <command> [args...]\n" 786 << "USAGE: " << ProgName << " <command> -help\n\n" 787 << "See each individual command --help for more details.\n" 788 << "Available commands: merge, show\n"; 789 return 0; 790 } 791 } 792 793 if (argc < 2) 794 errs() << ProgName << ": No command specified!\n"; 795 else 796 errs() << ProgName << ": Unknown command!\n"; 797 798 errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n"; 799 return 1; 800 } 801