Skip to content

Commit eee532c

Browse files
committed
Recommit [SampleFDO] Expose an interface to return the size of a section
or the size of the profile for profile in ExtBinary format. Fix a test failure on Mac. [SampleFDO] Expose an interface to return the size of a section or the size of the profile for profile in ExtBinary format. Sometimes we want to limit the size of the profile by stripping some functions with low sample count or by stripping some function names with small text size from profile symbol list. That requires the profile reader to have the interfaces returning the size of a section or the size of total profile. The patch add those interfaces. At the same time, add some dump facility to show the size of each section. Differential revision: https://reviews.llvm.org/D67726 llvm-svn: 372478
1 parent 63f6066 commit eee532c

File tree

5 files changed

+85
-2
lines changed

5 files changed

+85
-2
lines changed

llvm/include/llvm/ProfileData/SampleProf.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,22 @@ enum SecType {
125125
SecLBRProfile = SecFuncProfileFirst
126126
};
127127

128+
static inline std::string getSecName(SecType Type) {
129+
switch (Type) {
130+
case SecInValid:
131+
return "InvalidSection";
132+
case SecProfSummary:
133+
return "ProfileSummarySection";
134+
case SecNameTable:
135+
return "NameTableSection";
136+
case SecProfileSymbolList:
137+
return "ProfileSymbolListSection";
138+
case SecLBRProfile:
139+
return "LBRProfileSection";
140+
}
141+
llvm_unreachable("A SecType has no name for output");
142+
}
143+
128144
// Entry type of section header table used by SampleProfileExtBinaryBaseReader
129145
// and SampleProfileExtBinaryBaseWriter.
130146
struct SecHdrTableEntry {

llvm/include/llvm/ProfileData/SampleProfReader.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ class SampleProfileReader {
333333
/// It includes all the names that have samples either in outline instance
334334
/// or inline instance.
335335
virtual std::vector<StringRef> *getNameTable() { return nullptr; }
336+
virtual bool dumpSectionInfo(raw_ostream &OS = dbgs()) { return false; };
336337

337338
protected:
338339
/// Map every function to its associated profile.
@@ -504,6 +505,12 @@ class SampleProfileReaderExtBinaryBase : public SampleProfileReaderBinary {
504505

505506
/// Read sample profiles in extensible format from the associated file.
506507
std::error_code read() override;
508+
509+
/// Get the total size of all \p Type sections.
510+
uint64_t getSectionSize(SecType Type);
511+
/// Get the total size of header and all sections.
512+
uint64_t getFileSize();
513+
virtual bool dumpSectionInfo(raw_ostream &OS = dbgs()) override;
507514
};
508515

509516
class SampleProfileReaderExtBinary : public SampleProfileReaderExtBinaryBase {

llvm/lib/ProfileData/SampleProfReader.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,36 @@ std::error_code SampleProfileReaderExtBinaryBase::readHeader() {
667667
return sampleprof_error::success;
668668
}
669669

670+
uint64_t SampleProfileReaderExtBinaryBase::getSectionSize(SecType Type) {
671+
for (auto &Entry : SecHdrTable) {
672+
if (Entry.Type == Type)
673+
return Entry.Size;
674+
}
675+
return 0;
676+
}
677+
678+
uint64_t SampleProfileReaderExtBinaryBase::getFileSize() {
679+
auto &LastEntry = SecHdrTable.back();
680+
return LastEntry.Offset + LastEntry.Size;
681+
}
682+
683+
bool SampleProfileReaderExtBinaryBase::dumpSectionInfo(raw_ostream &OS) {
684+
uint64_t TotalSecsSize = 0;
685+
for (auto &Entry : SecHdrTable) {
686+
OS << getSecName(Entry.Type) << " - Offset: " << Entry.Offset
687+
<< ", Size: " << Entry.Size << "\n";
688+
TotalSecsSize += getSectionSize(Entry.Type);
689+
}
690+
uint64_t HeaderSize = SecHdrTable.front().Offset;
691+
assert(HeaderSize + TotalSecsSize == getFileSize() &&
692+
"Size of 'header + sections' doesn't match the total size of profile");
693+
694+
OS << "Header Size: " << HeaderSize << "\n";
695+
OS << "Total Sections Size: " << TotalSecsSize << "\n";
696+
OS << "File Size: " << getFileSize() << "\n";
697+
return true;
698+
}
699+
670700
std::error_code SampleProfileReaderBinary::readMagicIdent() {
671701
// Read and check the magic identifier.
672702
auto Magic = readNumber<uint64_t>();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
; RUN: llvm-profdata merge -sample -extbinary -prof-sym-list=%S/Inputs/profile-symbol-list-1.text %S/Inputs/sample-profile.proftext -o %t.1.output
2+
; RUN: ls -l %t.1.output |tr -s ' ' |cut -f5 -d ' ' > %t.txt
3+
; RUN: llvm-profdata show -sample -show-sec-info-only %t.1.output >> %t.txt
4+
; RUN: FileCheck %s --input-file=%t.txt
5+
; Check llvm-profdata shows the correct file size.
6+
; CHECK: [[FILESIZE:.*]]
7+
; CHECK: [[FILESIZE]]

llvm/tools/llvm-profdata/llvm-profdata.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -982,17 +982,34 @@ static int showInstrProfile(const std::string &Filename, bool ShowCounts,
982982
return 0;
983983
}
984984

985+
static void showSectionInfo(sampleprof::SampleProfileReader *Reader,
986+
raw_fd_ostream &OS) {
987+
if (!Reader->dumpSectionInfo(OS)) {
988+
WithColor::warning() << "-show-sec-info-only is only supported for "
989+
<< "sample profile in extbinary format and is "
990+
<< "ignored for other formats.\n";
991+
return;
992+
}
993+
}
994+
985995
static int showSampleProfile(const std::string &Filename, bool ShowCounts,
986996
bool ShowAllFunctions,
987997
const std::string &ShowFunction,
988-
bool ShowProfileSymbolList, raw_fd_ostream &OS) {
998+
bool ShowProfileSymbolList,
999+
bool ShowSectionInfoOnly, raw_fd_ostream &OS) {
9891000
using namespace sampleprof;
9901001
LLVMContext Context;
9911002
auto ReaderOrErr = SampleProfileReader::create(Filename, Context);
9921003
if (std::error_code EC = ReaderOrErr.getError())
9931004
exitWithErrorCode(EC, Filename);
9941005

9951006
auto Reader = std::move(ReaderOrErr.get());
1007+
1008+
if (ShowSectionInfoOnly) {
1009+
showSectionInfo(Reader.get(), OS);
1010+
return 0;
1011+
}
1012+
9961013
if (std::error_code EC = Reader->read())
9971014
exitWithErrorCode(EC, Filename);
9981015

@@ -1062,6 +1079,11 @@ static int show_main(int argc, const char *argv[]) {
10621079
cl::opt<bool> ShowProfileSymbolList(
10631080
"show-prof-sym-list", cl::init(false),
10641081
cl::desc("Show profile symbol list if it exists in the profile. "));
1082+
cl::opt<bool> ShowSectionInfoOnly(
1083+
"show-sec-info-only", cl::init(false),
1084+
cl::desc("Show the information of each section in the sample profile. "
1085+
"The flag is only usable when the sample profile is in "
1086+
"extbinary format"));
10651087

10661088
cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
10671089

@@ -1090,7 +1112,8 @@ static int show_main(int argc, const char *argv[]) {
10901112
OnlyListBelow, ShowFunction, TextFormat, OS);
10911113
else
10921114
return showSampleProfile(Filename, ShowCounts, ShowAllFunctions,
1093-
ShowFunction, ShowProfileSymbolList, OS);
1115+
ShowFunction, ShowProfileSymbolList,
1116+
ShowSectionInfoOnly, OS);
10941117
}
10951118

10961119
int main(int argc, const char *argv[]) {

0 commit comments

Comments
 (0)