diff --git a/llvm/include/llvm/IR/ProfileSummary.h b/llvm/include/llvm/IR/ProfileSummary.h index b105f8dd55a94..425ee2f1c16d2 100644 --- a/llvm/include/llvm/IR/ProfileSummary.h +++ b/llvm/include/llvm/IR/ProfileSummary.h @@ -21,6 +21,7 @@ namespace llvm { class LLVMContext; class Metadata; +class raw_ostream; // The profile summary is one or more (Cutoff, MinCount, NumCounts) triplets. // The semantics of counts depend on the type of profile. For instrumentation @@ -85,6 +86,8 @@ class ProfileSummary { uint64_t getMaxInternalCount() { return MaxInternalCount; } void setPartialProfile(bool PP) { Partial = PP; } bool isPartialProfile() { return Partial; } + void printSummary(raw_ostream &OS); + void printDetailedSummary(raw_ostream &OS); }; } // end namespace llvm diff --git a/llvm/lib/IR/ProfileSummary.cpp b/llvm/lib/IR/ProfileSummary.cpp index d6c3c5035dc85..c823b8cca3ab4 100644 --- a/llvm/lib/IR/ProfileSummary.cpp +++ b/llvm/lib/IR/ProfileSummary.cpp @@ -205,3 +205,21 @@ ProfileSummary *ProfileSummary::getFromMD(Metadata *MD) { MaxCount, MaxInternalCount, MaxFunctionCount, NumCounts, NumFunctions, IsPartialProfile); } + +void ProfileSummary::printSummary(raw_ostream &OS) { + OS << "Total functions: " << NumFunctions << "\n"; + OS << "Maximum function count: " << MaxFunctionCount << "\n"; + OS << "Maximum block count: " << MaxCount << "\n"; + OS << "Total number of blocks: " << NumCounts << "\n"; + OS << "Total count: " << TotalCount << "\n"; +} + +void ProfileSummary::printDetailedSummary(raw_ostream &OS) { + OS << "Detailed summary:\n"; + for (auto Entry : DetailedSummary) { + OS << Entry.NumCounts << " blocks with count >= " << Entry.MinCount + << " account for " + << format("%0.6g", (float)Entry.Cutoff / Scale * 100) + << " percentage of the total counts.\n"; + } +} diff --git a/llvm/test/tools/llvm-profdata/general.proftext b/llvm/test/tools/llvm-profdata/general.proftext index 6669cd6f12dcc..2dfb8e8b34d57 100644 --- a/llvm/test/tools/llvm-profdata/general.proftext +++ b/llvm/test/tools/llvm-profdata/general.proftext @@ -68,9 +68,9 @@ hex_hash # SUMMARY: Maximum internal block count: 1152921504606846976 # RUN: llvm-profdata show --detailed-summary %t.profdata.dense | FileCheck %s -check-prefix=DETAILED-SUMMARY -# DETAILED-SUMMARY: Detailed summary: # DETAILED-SUMMARY: Total number of blocks: 10 # DETAILED-SUMMARY: Total count: 4539628424389557499 +# DETAILED-SUMMARY: Detailed summary: # DETAILED-SUMMARY: 3 blocks with count >= 576460752303423488 account for 80 percentage of the total counts. # DETAILED-SUMMARY: 4 blocks with count >= 288230376151711744 account for 90 percentage of the total counts. # DETAILED-SUMMARY: 4 blocks with count >= 288230376151711744 account for 95 percentage of the total counts. diff --git a/llvm/test/tools/llvm-profdata/sample-summary.test b/llvm/test/tools/llvm-profdata/sample-summary.test new file mode 100644 index 0000000000000..3326c9bb29806 --- /dev/null +++ b/llvm/test/tools/llvm-profdata/sample-summary.test @@ -0,0 +1,24 @@ +; RUN: llvm-profdata show -sample -detailed-summary %S/Inputs/sample-profile.proftext | FileCheck %s + +; CHECK: Total functions: 3 +; CHECK-NEXT: Maximum function count: 1437 +; CHECK-NEXT: Maximum block count: 2080 +; CHECK-NEXT: Total number of blocks: 11 +; CHECK-NEXT: Total count: 12943 +; CHECK-NEXT: Detailed summary: +; CHECK-NEXT: 1 blocks with count >= 2080 account for 1 percentage of the total counts. +; CHECK-NEXT: 1 blocks with count >= 2080 account for 10 percentage of the total counts. +; CHECK-NEXT: 2 blocks with count >= 2064 account for 20 percentage of the total counts. +; CHECK-NEXT: 2 blocks with count >= 2064 account for 30 percentage of the total counts. +; CHECK-NEXT: 3 blocks with count >= 2000 account for 40 percentage of the total counts. +; CHECK-NEXT: 4 blocks with count >= 1437 account for 50 percentage of the total counts. +; CHECK-NEXT: 6 blocks with count >= 1075 account for 60 percentage of the total counts. +; CHECK-NEXT: 6 blocks with count >= 1075 account for 70 percentage of the total counts. +; CHECK-NEXT: 7 blocks with count >= 1000 account for 80 percentage of the total counts. +; CHECK-NEXT: 11 blocks with count >= 534 account for 90 percentage of the total counts. +; CHECK-NEXT: 11 blocks with count >= 534 account for 95 percentage of the total counts. +; CHECK-NEXT: 11 blocks with count >= 534 account for 99 percentage of the total counts. +; CHECK-NEXT: 11 blocks with count >= 534 account for 99.9 percentage of the total counts. +; CHECK-NEXT: 11 blocks with count >= 534 account for 99.99 percentage of the total counts. +; CHECK-NEXT: 11 blocks with count >= 534 account for 99.999 percentage of the total counts. +; CHECK-NEXT: 11 blocks with count >= 534 account for 99.9999 percentage of the total counts. diff --git a/llvm/tools/llvm-profdata/llvm-profdata.cpp b/llvm/tools/llvm-profdata/llvm-profdata.cpp index 482bd1c842d18..6d2f636b35e93 100644 --- a/llvm/tools/llvm-profdata/llvm-profdata.cpp +++ b/llvm/tools/llvm-profdata/llvm-profdata.cpp @@ -1010,15 +1010,9 @@ static int showInstrProfile(const std::string &Filename, bool ShowCounts, } if (ShowDetailedSummary) { - OS << "Detailed summary:\n"; OS << "Total number of blocks: " << PS->getNumCounts() << "\n"; OS << "Total count: " << PS->getTotalCount() << "\n"; - for (auto Entry : PS->getDetailedSummary()) { - OS << Entry.NumCounts << " blocks with count >= " << Entry.MinCount - << " account for " - << format("%0.6g", (float)Entry.Cutoff / ProfileSummary::Scale * 100) - << " percentage of the total counts.\n"; - } + PS->printDetailedSummary(OS); } return 0; } @@ -1034,7 +1028,7 @@ static void showSectionInfo(sampleprof::SampleProfileReader *Reader, } static int showSampleProfile(const std::string &Filename, bool ShowCounts, - bool ShowAllFunctions, + bool ShowAllFunctions, bool ShowDetailedSummary, const std::string &ShowFunction, bool ShowProfileSymbolList, bool ShowSectionInfoOnly, raw_fd_ostream &OS) { @@ -1065,6 +1059,12 @@ static int showSampleProfile(const std::string &Filename, bool ShowCounts, ReaderList->dump(OS); } + if (ShowDetailedSummary) { + auto &PS = Reader->getSummary(); + PS.printSummary(OS); + PS.printDetailedSummary(OS); + } + return 0; } @@ -1153,8 +1153,8 @@ static int show_main(int argc, const char *argv[]) { OnlyListBelow, ShowFunction, TextFormat, OS); else return showSampleProfile(Filename, ShowCounts, ShowAllFunctions, - ShowFunction, ShowProfileSymbolList, - ShowSectionInfoOnly, OS); + ShowDetailedSummary, ShowFunction, + ShowProfileSymbolList, ShowSectionInfoOnly, OS); } int main(int argc, const char *argv[]) {