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 warn(StringRef Prefix, Twine Message, std::string Whence = "", 41 std::string Hint = "") { 42 errs() << Prefix; 43 if (!Whence.empty()) 44 errs() << Whence << ": "; 45 errs() << Message << "\n"; 46 if (!Hint.empty()) 47 errs() << Hint << "\n"; 48 } 49 50 static void exitWithError(Twine Message, std::string Whence = "", 51 std::string Hint = "") { 52 warn("error: ", Message, Whence, Hint); 53 ::exit(1); 54 } 55 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(); 60 StringRef Hint = ""; 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?"; 64 } 65 exitWithError(IPE.message(), Whence, Hint); 66 }); 67 } 68 69 exitWithError(toString(std::move(E)), Whence); 70 } 71 72 static void exitWithErrorCode(std::error_code EC, StringRef Whence = "") { 73 exitWithError(EC.message(), Whence); 74 } 75 76 namespace { 77 enum ProfileKinds { instr, sample }; 78 } 79 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 << ": "; 87 88 auto IPE = instrprof_error::success; 89 E = handleErrors(std::move(E), 90 [&IPE](std::unique_ptr<InstrProfError> E) -> Error { 91 IPE = E->get(); 92 return Error(std::move(E)); 93 }); 94 errs() << toString(std::move(E)) << "\n"; 95 96 if (ShowHint) { 97 StringRef Hint = ""; 98 if (IPE != instrprof_error::success) { 99 switch (IPE) { 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."; 105 break; 106 default: 107 break; 108 } 109 } 110 111 if (!Hint.empty()) 112 errs() << Hint << "\n"; 113 } 114 } 115 116 struct WeightedFile { 117 std::string Filename; 118 uint64_t Weight; 119 }; 120 typedef SmallVector<WeightedFile, 5> WeightedFileVector; 121 122 /// Keep track of merged data and reported errors. 123 struct WriterContext { 124 std::mutex Lock; 125 InstrProfWriter Writer; 126 Error Err; 127 std::string ErrWhence; 128 std::mutex &ErrLock; 129 SmallSet<instrprof_error, 4> &WriterErrorCodes; 130 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) {} 135 }; 136 137 /// Determine whether an error is fatal for profile merging. 138 static bool isFatalError(instrprof_error IPE) { 139 switch (IPE) { 140 default: 141 return true; 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: 149 return false; 150 } 151 } 152 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}; 156 157 // If there's a pending hard error, don't do more work. 158 if (WC->Err) 159 return; 160 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; 165 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); 172 return; 173 } 174 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.", 180 std::error_code()); 181 return; 182 } 183 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) { 188 if (Reported) { 189 consumeError(std::move(E)); 190 return; 191 } 192 Reported = true; 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); 199 }); 200 } 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); 206 } 207 } 208 } 209 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 213 // clobber it. 214 if (Dst->Err || Src->Err) 215 return; 216 217 bool Reported = false; 218 Dst->Writer.mergeRecordsFromWriter(std::move(Src->Writer), [&](Error E) { 219 if (Reported) { 220 consumeError(std::move(E)); 221 return; 222 } 223 Reported = true; 224 Dst->Err = std::move(E); 225 }); 226 } 227 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."); 234 235 if (OutputFormat != PF_Binary && OutputFormat != PF_Text) 236 exitWithError("Unknown format is specified."); 237 238 std::error_code EC; 239 raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None); 240 if (EC) 241 exitWithErrorCode(EC, OutputFilename); 242 243 std::mutex ErrorLock; 244 SmallSet<instrprof_error, 4> WriterErrorCodes; 245 246 // If NumThreads is not specified, auto-detect a good default. 247 if (NumThreads == 0) 248 NumThreads = 249 std::min(hardware_concurrency(), unsigned((Inputs.size() + 1) / 2)); 250 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)); 256 257 if (NumThreads == 1) { 258 for (const auto &Input : Inputs) 259 loadInput(Input, Contexts[0].get()); 260 } else { 261 ThreadPool Pool(NumThreads); 262 263 // Load the inputs in parallel (N/NumThreads serial steps). 264 unsigned Ctx = 0; 265 for (const auto &Input : Inputs) { 266 Pool.async(loadInput, Input, Contexts[Ctx].get()); 267 Ctx = (Ctx + 1) % NumThreads; 268 } 269 Pool.wait(); 270 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"); 275 do { 276 for (unsigned I = 0; I < Mid; ++I) 277 Pool.async(mergeWriterContexts, Contexts[I].get(), 278 Contexts[I + Mid].get()); 279 Pool.wait(); 280 if (End & 1) { 281 Pool.async(mergeWriterContexts, Contexts[0].get(), 282 Contexts[End - 1].get()); 283 Pool.wait(); 284 } 285 End = Mid; 286 Mid /= 2; 287 } while (Mid > 0); 288 } 289 290 // Handle deferred hard errors encountered during merging. 291 for (std::unique_ptr<WriterContext> &WC : Contexts) { 292 if (!WC->Err) 293 continue; 294 if (!WC->Err.isA<InstrProfError>()) 295 exitWithError(std::move(WC->Err), WC->ErrWhence); 296 297 instrprof_error IPE = InstrProfError::take(std::move(WC->Err)); 298 if (isFatalError(IPE)) 299 exitWithError(make_error<InstrProfError>(IPE), WC->ErrWhence); 300 else 301 warn("warning: ", toString(make_error<InstrProfError>(IPE)), 302 WC->ErrWhence); 303 } 304 305 InstrProfWriter &Writer = Contexts[0]->Writer; 306 if (OutputFormat == PF_Text) { 307 if (Error E = Writer.writeText(Output)) 308 exitWithError(std::move(E)); 309 } else { 310 Writer.write(Output); 311 } 312 } 313 314 static sampleprof::SampleProfileFormat FormatMap[] = { 315 sampleprof::SPF_None, sampleprof::SPF_Text, sampleprof::SPF_Binary, 316 sampleprof::SPF_GCC}; 317 318 static void mergeSampleProfile(const WeightedFileVector &Inputs, 319 StringRef OutputFilename, 320 ProfileFormat OutputFormat) { 321 using namespace sampleprof; 322 auto WriterOrErr = 323 SampleProfileWriter::create(OutputFilename, FormatMap[OutputFormat]); 324 if (std::error_code EC = WriterOrErr.getError()) 325 exitWithErrorCode(EC, OutputFilename); 326 327 auto Writer = std::move(WriterOrErr.get()); 328 StringMap<FunctionSamples> ProfileMap; 329 SmallVector<std::unique_ptr<sampleprof::SampleProfileReader>, 5> Readers; 330 LLVMContext Context; 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); 335 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); 344 345 StringMap<FunctionSamples> &Profiles = Reader->getProfiles(); 346 for (StringMap<FunctionSamples>::iterator I = Profiles.begin(), 347 E = Profiles.end(); 348 I != E; ++I) { 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); 355 } 356 } 357 } 358 Writer->write(ProfileMap); 359 } 360 361 static WeightedFile parseWeightedFile(const StringRef &WeightedFilename) { 362 StringRef WeightStr, FileName; 363 std::tie(WeightStr, FileName) = WeightedFilename.split(','); 364 365 uint64_t Weight; 366 if (WeightStr.getAsInteger(10, Weight) || Weight < 1) 367 exitWithError("Input weight must be a positive integer."); 368 369 return {FileName, Weight}; 370 } 371 372 static std::unique_ptr<MemoryBuffer> 373 getInputFilenamesFileBuf(const StringRef &InputFilenamesFile) { 374 if (InputFilenamesFile == "") 375 return {}; 376 377 auto BufOrError = MemoryBuffer::getFileOrSTDIN(InputFilenamesFile); 378 if (!BufOrError) 379 exitWithErrorCode(BufOrError.getError(), InputFilenamesFile); 380 381 return std::move(*BufOrError); 382 } 383 384 static void addWeightedInput(WeightedFileVector &WNI, const WeightedFile &WF) { 385 StringRef Filename = WF.Filename; 386 uint64_t Weight = WF.Weight; 387 388 // If it's STDIN just pass it on. 389 if (Filename == "-") { 390 WNI.push_back({Filename, Weight}); 391 return; 392 } 393 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), 398 Filename); 399 // If it's a source file, collect it. 400 if (llvm::sys::fs::is_regular_file(Status)) { 401 WNI.push_back({Filename, Weight}); 402 return; 403 } 404 405 if (llvm::sys::fs::is_directory(Status)) { 406 std::error_code EC; 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}); 411 } 412 } 413 if (EC) 414 exitWithErrorCode(EC, Filename); 415 } 416 } 417 418 static void parseInputFilenamesFile(MemoryBuffer *Buffer, 419 WeightedFileVector &WFV) { 420 if (!Buffer) 421 return; 422 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"); 428 // Skip comments. 429 if (SanitizedEntry.startswith("#")) 430 continue; 431 // If there's no comma, it's an unweighted profile. 432 else if (SanitizedEntry.find(',') == StringRef::npos) 433 addWeightedInput(WFV, {SanitizedEntry, 1}); 434 else 435 addWeightedInput(WFV, parseWeightedFile(SanitizedEntry)); 436 } 437 } 438 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)); 475 476 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n"); 477 478 WeightedFileVector WeightedInputs; 479 for (StringRef Filename : InputFilenames) 480 addWeightedInput(WeightedInputs, {Filename, 1}); 481 for (StringRef WeightedFilename : WeightedInputFilenames) 482 addWeightedInput(WeightedInputs, parseWeightedFile(WeightedFilename)); 483 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); 488 489 if (WeightedInputs.empty()) 490 exitWithError("No input files specified. See " + 491 sys::path::filename(argv[0]) + " -help"); 492 493 if (DumpInputFileList) { 494 for (auto &WF : WeightedInputs) 495 outs() << WF.Weight << "," << WF.Filename << "\n"; 496 return 0; 497 } 498 499 if (ProfileKind == instr) 500 mergeInstrProfile(WeightedInputs, OutputFilename, OutputFormat, 501 OutputSparse, NumThreads); 502 else 503 mergeSampleProfile(WeightedInputs, OutputFilename, OutputFormat); 504 505 return 0; 506 } 507 508 typedef struct ValueSitesStats { 509 ValueSitesStats() 510 : TotalNumValueSites(0), TotalNumValueSitesWithValueProfile(0), 511 TotalNumValues(0) {} 512 uint64_t TotalNumValueSites; 513 uint64_t TotalNumValueSitesWithValueProfile; 514 uint64_t TotalNumValues; 515 std::vector<unsigned> ValueSitesHistogram; 516 } ValueSitesStats; 517 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; 527 if (NV) { 528 Stats.TotalNumValueSitesWithValueProfile++; 529 if (NV > Stats.ValueSitesHistogram.size()) 530 Stats.ValueSitesHistogram.resize(NV, 0); 531 Stats.ValueSitesHistogram[NV - 1]++; 532 } 533 for (uint32_t V = 0; V < NV; V++) { 534 OS << "\t[ " << I << ", "; 535 if (Symtab == nullptr) 536 OS << VD[V].Value; 537 else 538 OS << Symtab->getFuncName(VD[V].Value); 539 OS << ", " << VD[V].Count << " ]\n"; 540 } 541 } 542 } 543 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"; 550 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"; 555 } 556 } 557 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}; 569 } 570 InstrProfSummaryBuilder Builder(std::move(Cutoffs)); 571 if (Error E = ReaderOrErr.takeError()) 572 exitWithError(std::move(E), Filename); 573 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); 579 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; 583 }; 584 585 std::priority_queue<std::pair<std::string, uint64_t>, 586 std::vector<std::pair<std::string, uint64_t>>, 587 decltype(MinCmp)> 588 HottestFuncs(MinCmp); 589 590 for (const auto &Func : *Reader) { 591 bool Show = 592 ShowAllFunctions || (!ShowFunction.empty() && 593 Func.Name.find(ShowFunction) != Func.Name.npos); 594 595 bool doTextFormatDump = (Show && ShowCounts && TextFormat); 596 597 if (doTextFormatDump) { 598 InstrProfSymtab &Symtab = Reader->getSymtab(); 599 InstrProfWriter::writeRecordInText(Func.Name, Func.Hash, Func, Symtab, 600 OS); 601 continue; 602 } 603 604 assert(Func.Counts.size() > 0 && "function missing entry counter"); 605 Builder.addRecord(Func); 606 607 if (TopN) { 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]); 611 612 if (HottestFuncs.size() == TopN) { 613 if (HottestFuncs.top().second < FuncMax) { 614 HottestFuncs.pop(); 615 HottestFuncs.emplace(std::make_pair(std::string(Func.Name), FuncMax)); 616 } 617 } else 618 HottestFuncs.emplace(std::make_pair(std::string(Func.Name), FuncMax)); 619 } 620 621 if (Show) { 622 623 if (!ShownFunctions) 624 OS << "Counters:\n"; 625 626 ++ShownFunctions; 627 628 OS << " " << Func.Name << ":\n" 629 << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n" 630 << " Counters: " << Func.Counts.size() << "\n"; 631 if (!IsIRInstr) 632 OS << " Function count: " << Func.Counts[0] << "\n"; 633 634 if (ShowIndirectCallTargets) 635 OS << " Indirect Call Site Count: " 636 << Func.getNumValueSites(IPVK_IndirectCallTarget) << "\n"; 637 638 uint32_t NumMemOPCalls = Func.getNumValueSites(IPVK_MemOPSize); 639 if (ShowMemOPSizes && NumMemOPCalls > 0) 640 OS << " Number of Memory Intrinsics Calls: " << NumMemOPCalls 641 << "\n"; 642 643 if (ShowCounts) { 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]; 648 } 649 OS << "]\n"; 650 } 651 652 if (ShowIndirectCallTargets) { 653 OS << " Indirect Target Results:\n"; 654 traverseAllValueSites(Func, IPVK_IndirectCallTarget, 655 VPStats[IPVK_IndirectCallTarget], OS, 656 &(Reader->getSymtab())); 657 } 658 659 if (ShowMemOPSizes && NumMemOPCalls > 0) { 660 OS << " Memory Intrinsic Size Results:\n"; 661 traverseAllValueSites(Func, IPVK_MemOPSize, VPStats[IPVK_MemOPSize], OS, 662 nullptr); 663 } 664 } 665 } 666 if (Reader->hasError()) 667 exitWithError(Reader->getError(), Filename); 668 669 if (ShowCounts && TextFormat) 670 return 0; 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"; 679 680 if (TopN) { 681 std::vector<std::pair<std::string, uint64_t>> SortedHottestFuncs; 682 while (!HottestFuncs.empty()) { 683 SortedHottestFuncs.emplace_back(HottestFuncs.top()); 684 HottestFuncs.pop(); 685 } 686 OS << "Top " << TopN 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"; 690 } 691 692 if (ShownFunctions && ShowIndirectCallTargets) { 693 OS << "Statistics for indirect call sites profile:\n"; 694 showValueSitesStats(OS, IPVK_IndirectCallTarget, 695 VPStats[IPVK_IndirectCallTarget]); 696 } 697 698 if (ShownFunctions && ShowMemOPSizes) { 699 OS << "Statistics for memory intrinsic calls sizes profile:\n"; 700 showValueSitesStats(OS, IPVK_MemOPSize, VPStats[IPVK_MemOPSize]); 701 } 702 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 709 << " account for " 710 << format("%0.6g", (float)Entry.Cutoff / ProfileSummary::Scale * 100) 711 << " percentage of the total counts.\n"; 712 } 713 } 714 return 0; 715 } 716 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; 722 LLVMContext Context; 723 auto ReaderOrErr = SampleProfileReader::create(Filename, Context); 724 if (std::error_code EC = ReaderOrErr.getError()) 725 exitWithErrorCode(EC, Filename); 726 727 auto Reader = std::move(ReaderOrErr.get()); 728 if (std::error_code EC = Reader->read()) 729 exitWithErrorCode(EC, Filename); 730 731 if (ShowAllFunctions || ShowFunction.empty()) 732 Reader->dump(OS); 733 else 734 Reader->dumpFunctionProfile(ShowFunction, OS); 735 736 return 0; 737 } 738 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>")); 742 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", 759 cl::desc( 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")); 766 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( 776 "topn", cl::init(0), 777 cl::desc("Show the list of functions with the largest internal counts")); 778 779 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n"); 780 781 if (OutputFilename.empty()) 782 OutputFilename = "-"; 783 784 std::error_code EC; 785 raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text); 786 if (EC) 787 exitWithErrorCode(EC, OutputFilename); 788 789 if (ShowAllFunctions && !ShowFunction.empty()) 790 errs() << "warning: -function argument ignored: showing all functions\n"; 791 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); 799 else 800 return showSampleProfile(Filename, ShowCounts, ShowAllFunctions, 801 ShowFunction, OS); 802 } 803 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. 809 810 StringRef ProgName(sys::path::filename(argv[0])); 811 if (argc > 1) { 812 int (*func)(int, const char *[]) = nullptr; 813 814 if (strcmp(argv[1], "merge") == 0) 815 func = merge_main; 816 else if (strcmp(argv[1], "show") == 0) 817 func = show_main; 818 819 if (func) { 820 std::string Invocation(ProgName.str() + " " + argv[1]); 821 argv[1] = Invocation.c_str(); 822 return func(argc - 1, argv + 1); 823 } 824 825 if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-help") == 0 || 826 strcmp(argv[1], "--help") == 0) { 827 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"; 833 return 0; 834 } 835 } 836 837 if (argc < 2) 838 errs() << ProgName << ": No command specified!\n"; 839 else 840 errs() << ProgName << ": Unknown command!\n"; 841 842 errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n"; 843 return 1; 844 } 845