llvm-profdata.cpp 30 KB

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