diff --git a/llvm/test/tools/llvm-profdata/sample-hot-func-list.test b/llvm/test/tools/llvm-profdata/sample-hot-func-list.test --- a/llvm/test/tools/llvm-profdata/sample-hot-func-list.test +++ b/llvm/test/tools/llvm-profdata/sample-hot-func-list.test @@ -12,6 +12,13 @@ ; CHECK-NEXT: 6948 (1.95%) 3507 470 Func5 ; CHECK-NEXT: 1523 (0.43%) 563 169 Func1 +; RUN: llvm-profdata show --sample --hot-func-list --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 --hot-func-list %S/Inputs/cs-sample.proftext | FileCheck %s --match-full-lines --strict-whitespace --check-prefix=CS @@ -20,3 +27,10 @@ ; 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 --hot-func-list --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 --- a/llvm/tools/llvm-profdata/llvm-profdata.cpp +++ b/llvm/tools/llvm-profdata/llvm-profdata.cpp @@ -2304,7 +2304,7 @@ 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 @@ } 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 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 @@ } 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, @@ -2447,7 +2452,7 @@ } if (ShowHotFuncList) - showHotFunctionList(Reader->getProfiles(), Reader->getSummary(), OS); + showHotFunctionList(Reader->getProfiles(), Reader->getSummary(), TopN, OS); return 0; } @@ -2538,10 +2543,10 @@ 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[]) {