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,86 @@ 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; + // Sum of all counts. + uint64_t TotalCount; + // Number of blocks + uint32_t NumBlocks; + +public: + ProfileSummary() : TotalCount(0), NumBlocks(0) {} + void AddCount(uint64_t Count); + std::vector + GetProfileSummary(std::vector Cutoffs); + uint32_t GetNumBlocks() { return NumBlocks; } + uint64_t GetTotalCount() { return TotalCount; } +}; + +// This is called when a count is seen in the profile. +void ProfileSummary::AddCount(uint64_t Count) { + TotalCount += Count; + NumBlocks++; + if (CountFrequencies.count(Count)) + CountFrequencies[Count] += 1; + else + CountFrequencies[Count] = 1; +} + +// The argument to this method is a vector of cutoff percentages and the return +// value is a vector of (Cutoff, MinBlockCount, NumBlocks) triplets. +std::vector +ProfileSummary::GetProfileSummary(std::vector Cutoffs) { + std::vector Summary; + if (Cutoffs.empty()) + return Summary; + auto Iter = Cutoffs.begin(); + auto End = Cutoffs.end(); + + std::sort(Cutoffs.begin(), Cutoffs.end()); + uint32_t NumBlocks = 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; + NumBlocks += 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, NumBlocks}; + Summary.push_back(PSE); + Iter++; + if (Iter == End) { + Done = true; + break; + } + } + if (Done) + break; + } + return Summary; +} + static void exitWithError(const Twine &Message, StringRef Whence = "", StringRef Hint = "") { errs() << "error: "; @@ -249,10 +329,13 @@ } 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); + ProfileSummary PS; if (std::error_code EC = ReaderOrErr.getError()) exitWithErrorCode(EC, Filename); @@ -278,6 +361,7 @@ MaxFunctionCount = Func.Counts[0]; for (size_t I = 1, E = Func.Counts.size(); I < E; ++I) { + PS.AddCount(Func.Counts[I]); if (Func.Counts[I] > MaxBlockCount) MaxBlockCount = Func.Counts[I]; } @@ -335,6 +419,20 @@ OS << "Total functions: " << TotalFunctions << "\n"; OS << "Maximum function count: " << MaxFunctionCount << "\n"; OS << "Maximum internal block count: " << MaxBlockCount << "\n"; + + if (ShowDetailedSummary) { + std::vector Cutoffs(DetailedSummaryCutoffs); + if (Cutoffs.empty()) + Cutoffs = {80, 90, 95, 99, 99.9, 99.99, 99.999}; + OS << "Detailed summary:\n"; + OS << "Total number of blocks: " << PS.GetNumBlocks() << "\n"; + OS << "Total count: " << PS.GetTotalCount() << "\n"; + for (auto Entry : PS.GetProfileSummary(Cutoffs)) { + 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 +468,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 +501,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,