Index: test/tools/llvm-profdata/general.proftext =================================================================== --- test/tools/llvm-profdata/general.proftext +++ test/tools/llvm-profdata/general.proftext @@ -62,3 +62,18 @@ # SUMMARY: Total functions: 4 # SUMMARY: Maximum function count: 2305843009213693952 # SUMMARY: Maximum internal block count: 1152921504606846976 + +# RUN: llvm-profdata show --detailed-summary %t.profdata | FileCheck %s -check-prefix=DETAILED-SUMMARY +# DETAILED-SUMMARY: Detailed summary: +# DETAILED-SUMMARY: Total number of blocks: 6 +# DETAILED-SUMMARY: Total count: 2233785415175766016 +# DETAILED-SUMMARY: 3 blocks with count >= 288230376151711744 account for 80 percentage of the total counts +# DETAILED-SUMMARY: 3 blocks with count >= 288230376151711744 account for 90 percentage of the total counts +# DETAILED-SUMMARY: 4 blocks with count >= 144115188075855872 account for 95 percentage of the total counts. +# DETAILED-SUMMARY: 5 blocks with count >= 72057594037927936 account for 99 percentage of the total counts. +# DETAILED-SUMMARY: 5 blocks with count >= 72057594037927936 account for 99.9 percentage of the total counts. +# DETAILED-SUMMARY: 5 blocks with count >= 72057594037927936 account for 99.99 percentage of the total counts. +# DETAILED-SUMMARY: 5 blocks with count >= 72057594037927936 account for 99.999 percentage of the total counts. + +# RUN: llvm-profdata show --detailed-summary --detailed-summary-cutoffs=60 %t.profdata | FileCheck %s -check-prefix=DETAILED-SUMMARY-2 +# DETAILED-SUMMARY-2: 2 blocks with count >= 576460752303423488 account for 60 percentage of the total counts. Index: tools/llvm-profdata/llvm-profdata.cpp =================================================================== --- tools/llvm-profdata/llvm-profdata.cpp +++ tools/llvm-profdata/llvm-profdata.cpp @@ -36,6 +36,106 @@ enum ProfileFormat { PF_None = 0, PF_Text, PF_Binary, PF_GCC }; +///// Profile summary computation //// +// The 'show' command displays richer summary of the profile data. The profile +// summary is one or more (Cutoff, MinBlockCount, NumBlocks) triplets. Given a +// Cutoff (expressed as percentage in the range 0-100), we compute the smallest +// Top-N counts such that their sum equals or exceeds Cutoff x (sum of all +// counts). The N in the above Top-N is the NumBlocks of the triplet and the +// smallest count in the Top-N is the MinBlockCount. +struct ProfileSummaryEntry { + float Cutoff; + uint64_t MinBlockCount; + uint64_t NumBlocks; +}; + +class ProfileSummary { + // We keep track of the number of times a count appears in the profile and + // keep the map sorted in the descending order of counts. + std::map> CountFrequencies; + std::vector DetailedSummary; + std::vector DetailedSummaryCutoffs; + // Sum of all counts. + uint64_t TotalCount; + uint64_t MaxBlockCount, MaxFunctionCount; + uint32_t NumBlocks, NumFunctions; + void addCount(uint64_t Count); + void computeDetailedSummary(); + +public: + ProfileSummary(std::vector Cutoffs) + : DetailedSummaryCutoffs(Cutoffs), TotalCount(0), MaxBlockCount(0), + MaxFunctionCount(0), NumBlocks(0), NumFunctions(0) {} + void addRecord(const InstrProfRecord &); + std::vector getDetailedSummary(); + uint32_t getNumBlocks() { return NumBlocks; } + uint64_t getTotalCount() { return TotalCount; } + uint32_t getNumFunctions() { return NumFunctions; } + uint64_t getMaxFunctionCount() { return MaxFunctionCount; } + uint64_t getMaxBlockCount() { return MaxBlockCount; } +}; + +// This is called when a count is seen in the profile. +void ProfileSummary::addCount(uint64_t Count) { + TotalCount += Count; + if (Count > MaxBlockCount) + MaxBlockCount = Count; + NumBlocks++; + CountFrequencies[Count]++; +} + +void ProfileSummary::addRecord(const InstrProfRecord &R) { + NumFunctions++; + if (R.Counts[0] > MaxFunctionCount) + MaxFunctionCount = R.Counts[0]; + + for (size_t I = 1, E = R.Counts.size(); I < E; ++I) + addCount(R.Counts[I]); +} + +// The argument to this method is a vector of cutoff percentages and the return +// value is a vector of (Cutoff, MinBlockCount, NumBlocks) triplets. +void ProfileSummary::computeDetailedSummary() { + if (DetailedSummaryCutoffs.empty()) + return; + auto Iter = DetailedSummaryCutoffs.begin(); + auto End = DetailedSummaryCutoffs.end(); + + std::sort(DetailedSummaryCutoffs.begin(), DetailedSummaryCutoffs.end()); + uint32_t BlocksSeen = 0; + bool Done = false; + uint64_t CurrSum = 0; + // CountFrequencies is sorted in the descending order of keys (counts). + for (auto Entry : CountFrequencies) { + uint64_t Count = Entry.first; + uint32_t Freq = Entry.second; + BlocksSeen += Freq; + // CurrSum contains the sum of all counts we have seen so far. + CurrSum += (Count * Freq); + // *Iter is the desired Cutoff. We check if the current sum equals or + // exceeds Cutoff times total. We might have exceeded more than one cutoffs + // and hence we need the while loop. + while (CurrSum >= (*Iter) / 100 * TotalCount) { + ProfileSummaryEntry PSE = {*Iter, Count, BlocksSeen}; + DetailedSummary.push_back(PSE); + Iter++; + if (Iter == End) { + Done = true; + break; + } + } + if (Done) + break; + } + return; +} + +std::vector ProfileSummary::getDetailedSummary() { + if (!DetailedSummaryCutoffs.empty() && DetailedSummary.empty()) + computeDetailedSummary(); + return DetailedSummary; +} + static void exitWithError(const Twine &Message, StringRef Whence = "", StringRef Hint = "") { errs() << "error: "; @@ -249,16 +349,22 @@ } static int showInstrProfile(std::string Filename, bool ShowCounts, - bool ShowIndirectCallTargets, bool ShowAllFunctions, - std::string ShowFunction, bool TextFormat, - raw_fd_ostream &OS) { + bool ShowIndirectCallTargets, + bool ShowDetailedSummary, + std::vector DetailedSummaryCutoffs, + bool ShowAllFunctions, std::string ShowFunction, + bool TextFormat, raw_fd_ostream &OS) { auto ReaderOrErr = InstrProfReader::create(Filename); + std::vector Cutoffs(DetailedSummaryCutoffs); + if (ShowDetailedSummary && DetailedSummaryCutoffs.empty()) { + Cutoffs = {80, 90, 95, 99, 99.9, 99.99, 99.999}; + } + ProfileSummary PS(Cutoffs); if (std::error_code EC = ReaderOrErr.getError()) exitWithErrorCode(EC, Filename); auto Reader = std::move(ReaderOrErr.get()); - uint64_t MaxFunctionCount = 0, MaxBlockCount = 0; - size_t ShownFunctions = 0, TotalFunctions = 0; + size_t ShownFunctions = 0; for (const auto &Func : *Reader) { bool Show = ShowAllFunctions || (!ShowFunction.empty() && @@ -272,15 +378,8 @@ continue; } - ++TotalFunctions; assert(Func.Counts.size() > 0 && "function missing entry counter"); - if (Func.Counts[0] > MaxFunctionCount) - MaxFunctionCount = Func.Counts[0]; - - for (size_t I = 1, E = Func.Counts.size(); I < E; ++I) { - if (Func.Counts[I] > MaxBlockCount) - MaxBlockCount = Func.Counts[I]; - } + PS.addRecord(Func); if (Show) { @@ -332,9 +431,20 @@ if (ShowAllFunctions || !ShowFunction.empty()) OS << "Functions shown: " << ShownFunctions << "\n"; - OS << "Total functions: " << TotalFunctions << "\n"; - OS << "Maximum function count: " << MaxFunctionCount << "\n"; - OS << "Maximum internal block count: " << MaxBlockCount << "\n"; + OS << "Total functions: " << PS.getNumFunctions() << "\n"; + OS << "Maximum function count: " << PS.getMaxFunctionCount() << "\n"; + OS << "Maximum internal block count: " << PS.getMaxBlockCount() << "\n"; + + if (ShowDetailedSummary) { + OS << "Detailed summary:\n"; + OS << "Total number of blocks: " << PS.getNumBlocks() << "\n"; + OS << "Total count: " << PS.getTotalCount() << "\n"; + for (auto Entry : PS.getDetailedSummary()) { + OS << Entry.NumBlocks << " blocks with count >= " << Entry.MinBlockCount + << " account for " << format("%0.6g", Entry.Cutoff) + << " percentage of the total counts.\n"; + } + } return 0; } @@ -370,6 +480,12 @@ cl::opt ShowIndirectCallTargets( "ic-targets", cl::init(false), cl::desc("Show indirect call site target values for shown functions")); + cl::opt ShowDetailedSummary("detailed-summary", cl::init(false), + cl::desc("Show detailed profile summary")); + cl::list DetailedSummaryCutoffs( + "detailed-summary-cutoffs", + cl::desc("Cutoff percentages for generating detailed summary"), + cl::value_desc("80,90.1,99.99")); cl::opt ShowAllFunctions("all-functions", cl::init(false), cl::desc("Details for every function")); cl::opt ShowFunction("function", @@ -397,8 +513,11 @@ if (ShowAllFunctions && !ShowFunction.empty()) errs() << "warning: -function argument ignored: showing all functions\n"; + std::vector Cutoffs(DetailedSummaryCutoffs.begin(), + DetailedSummaryCutoffs.end()); if (ProfileKind == instr) return showInstrProfile(Filename, ShowCounts, ShowIndirectCallTargets, + ShowDetailedSummary, DetailedSummaryCutoffs, ShowAllFunctions, ShowFunction, TextFormat, OS); else return showSampleProfile(Filename, ShowCounts, ShowAllFunctions,