[llvm] ce6ed64 - [llvm-profdata] Extend support of --topn to sample profiles

via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 24 16:42:55 PDT 2021


Author: modimo
Date: 2021-09-24T16:42:46-07:00
New Revision: ce6ed64a6953ad0a88b08665c54bae0c1f0f2c84

URL: https://github.com/llvm/llvm-project/commit/ce6ed64a6953ad0a88b08665c54bae0c1f0f2c84
DIFF: https://github.com/llvm/llvm-project/commit/ce6ed64a6953ad0a88b08665c54bae0c1f0f2c84.diff

LOG: [llvm-profdata] Extend support of --topn to sample profiles

Reviewed By: wenlei

Differential Revision: https://reviews.llvm.org/D110449

Added: 
    

Modified: 
    llvm/test/tools/llvm-profdata/sample-hot-func-list.test
    llvm/tools/llvm-profdata/llvm-profdata.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/test/tools/llvm-profdata/sample-hot-func-list.test b/llvm/test/tools/llvm-profdata/sample-hot-func-list.test
index 7f319e6dc0bb5..91a8d817d55f6 100644
--- a/llvm/test/tools/llvm-profdata/sample-hot-func-list.test
+++ b/llvm/test/tools/llvm-profdata/sample-hot-func-list.test
@@ -12,7 +12,6 @@
 ; CHECK-NEXT: 6948 (1.95%)           3507              470             Func5
 ; CHECK-NEXT: 1523 (0.43%)           563               169             Func1
 
-
 ; RUN: llvm-profdata show --sample --hot-func-list %S/Inputs/cs-sample.proftext | FileCheck %s --match-full-lines --strict-whitespace --check-prefix=CS
 
 ;      CS:2 out of 8 functions with profile (25.00%) are considered hot functions (max sample >= 23324).
@@ -20,3 +19,19 @@
 ; CS-NEXT: Total sample (%)       Max sample        Entry sample    Function name
 ; CS-NEXT: 1467299 (74.52%)       287884            11              main:3 @ _Z5funcAi:1 @ _Z8funcLeafi
 ; CS-NEXT: 500853 (25.44%)        74946             20              main:3.1 @ _Z5funcBi:1 @ _Z8funcLeafi
+
+
+; RUN: llvm-profdata show --sample --topn=2 %S/Inputs/sample-hot-func-list.proftext | FileCheck %s --match-full-lines --strict-whitespace --check-prefix=TOPN
+
+;      TOPN:8 out of 10 functions with profile (80.00%) are considered hot functions (max sample >= 470).
+; TOPN-NEXT:355251 out of 356026 profile counts (99.78%) are from hot functions.
+; TOPN-NEXT: Total sample (%)       Max sample        Entry sample    Function name
+; TOPN-NEXT: 184019 (51.69%)        2300              534             main
+; TOPN-NEXT: 97401 (27.36%)         10640             3035            Func3
+
+; RUN: llvm-profdata show --sample --topn=1 %S/Inputs/cs-sample.proftext | FileCheck %s --match-full-lines --strict-whitespace --check-prefix=CS-TOPN
+
+;      CS-TOPN:2 out of 8 functions with profile (25.00%) are considered hot functions (max sample >= 23324).
+; CS-TOPN-NEXT:1968152 out of 1968919 profile counts (99.96%) are from hot functions.
+; CS-TOPN-NEXT: Total sample (%)       Max sample        Entry sample    Function name
+; CS-TOPN-NEXT: 1467299 (74.52%)       287884            11              main:3 @ _Z5funcAi:1 @ _Z8funcLeafi

diff  --git a/llvm/tools/llvm-profdata/llvm-profdata.cpp b/llvm/tools/llvm-profdata/llvm-profdata.cpp
index 6692c54755af2..a153582ac929d 100644
--- a/llvm/tools/llvm-profdata/llvm-profdata.cpp
+++ b/llvm/tools/llvm-profdata/llvm-profdata.cpp
@@ -2304,7 +2304,7 @@ static void dumpHotFunctionList(const std::vector<std::string> &ColumnTitle,
                                 uint64_t HotFuncCount, uint64_t TotalFuncCount,
                                 uint64_t HotProfCount, uint64_t TotalProfCount,
                                 const std::string &HotFuncMetric,
-                                raw_fd_ostream &OS) {
+                                uint32_t TopNFunctions, raw_fd_ostream &OS) {
   assert(ColumnOffset.size() == ColumnTitle.size() &&
          "ColumnOffset and ColumnTitle should have the same size");
   assert(ColumnTitle.size() >= 4 &&
@@ -2333,7 +2333,10 @@ static void dumpHotFunctionList(const std::vector<std::string> &ColumnTitle,
   }
   FOS << "\n";
 
-  for (const HotFuncInfo &R : PrintValues) {
+  uint32_t Count = 0;
+  for (const auto &R : PrintValues) {
+    if (TopNFunctions && (Count++ == TopNFunctions))
+      break;
     FOS.PadToColumn(ColumnOffset[0]);
     FOS << R.TotalCount << " (" << format("%.2f%%", R.TotalCountPercent) << ")";
     FOS.PadToColumn(ColumnOffset[1]);
@@ -2346,7 +2349,8 @@ static void dumpHotFunctionList(const std::vector<std::string> &ColumnTitle,
 }
 
 static int showHotFunctionList(const sampleprof::SampleProfileMap &Profiles,
-                               ProfileSummary &PS, raw_fd_ostream &OS) {
+                               ProfileSummary &PS, uint32_t TopN,
+                               raw_fd_ostream &OS) {
   using namespace sampleprof;
 
   const uint32_t HotFuncCutoff = 990000;
@@ -2401,13 +2405,14 @@ static int showHotFunctionList(const sampleprof::SampleProfileMap &Profiles,
   }
   dumpHotFunctionList(ColumnTitle, ColumnOffset, PrintValues, HotFuncCount,
                       Profiles.size(), HotFuncSample, ProfileTotalSample,
-                      Metric, OS);
+                      Metric, TopN, OS);
 
   return 0;
 }
 
 static int showSampleProfile(const std::string &Filename, bool ShowCounts,
-                             bool ShowAllFunctions, bool ShowDetailedSummary,
+                             uint32_t TopN, bool ShowAllFunctions,
+                             bool ShowDetailedSummary,
                              const std::string &ShowFunction,
                              bool ShowProfileSymbolList,
                              bool ShowSectionInfoOnly, bool ShowHotFuncList,
@@ -2446,8 +2451,8 @@ static int showSampleProfile(const std::string &Filename, bool ShowCounts,
     PS.printDetailedSummary(OS);
   }
 
-  if (ShowHotFuncList)
-    showHotFunctionList(Reader->getProfiles(), Reader->getSummary(), OS);
+  if (ShowHotFuncList || TopN)
+    showHotFunctionList(Reader->getProfiles(), Reader->getSummary(), TopN, OS);
 
   return 0;
 }
@@ -2538,10 +2543,10 @@ static int show_main(int argc, const char *argv[]) {
         ShowAllFunctions, ShowCS, ValueCutoff, OnlyListBelow, ShowFunction,
         TextFormat, ShowBinaryIds, OS);
   else
-    return showSampleProfile(Filename, ShowCounts, ShowAllFunctions,
-                             ShowDetailedSummary, ShowFunction,
-                             ShowProfileSymbolList, ShowSectionInfoOnly,
-                             ShowHotFuncList, OS);
+    return showSampleProfile(Filename, ShowCounts, TopNFunctions,
+                             ShowAllFunctions, ShowDetailedSummary,
+                             ShowFunction, ShowProfileSymbolList,
+                             ShowSectionInfoOnly, ShowHotFuncList, OS);
 }
 
 int main(int argc, const char *argv[]) {


        


More information about the llvm-commits mailing list