From e88b44e49c27a648112033d8eed7630b25225d5e Mon Sep 17 00:00:00 2001 From: Wenlei He Date: Sat, 2 May 2020 08:34:10 -0700 Subject: [PATCH] [llvm-profdata] Support -detailed-summary for Sample Profile Summary: Add -detailed-summary support for sample profile dump to match that of instrumentation profile. Reviewers: wmi, davidxl, hoyFB Subscribers: llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D79291 --- include/llvm/IR/ProfileSummary.h | 3 +++ lib/IR/ProfileSummary.cpp | 18 +++++++++++++++ test/tools/llvm-profdata/general.proftext | 2 +- test/tools/llvm-profdata/sample-summary.test | 24 ++++++++++++++++++++ tools/llvm-profdata/llvm-profdata.cpp | 20 ++++++++-------- 5 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 test/tools/llvm-profdata/sample-summary.test diff --git a/include/llvm/IR/ProfileSummary.h b/include/llvm/IR/ProfileSummary.h index b105f8dd55a..425ee2f1c16 100644 --- a/include/llvm/IR/ProfileSummary.h +++ b/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 @@ public: 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/lib/IR/ProfileSummary.cpp b/lib/IR/ProfileSummary.cpp index d6c3c5035dc..c823b8cca3a 100644 --- a/lib/IR/ProfileSummary.cpp +++ b/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/test/tools/llvm-profdata/general.proftext b/test/tools/llvm-profdata/general.proftext index 6669cd6f12d..2dfb8e8b34d 100644 --- a/test/tools/llvm-profdata/general.proftext +++ b/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/test/tools/llvm-profdata/sample-summary.test b/test/tools/llvm-profdata/sample-summary.test new file mode 100644 index 00000000000..3326c9bb298 --- /dev/null +++ b/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/tools/llvm-profdata/llvm-profdata.cpp b/tools/llvm-profdata/llvm-profdata.cpp index 482bd1c842d..6d2f636b35e 100644 --- a/tools/llvm-profdata/llvm-profdata.cpp +++ b/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[]) {