Skip to content

Commit 0289ca0

Browse files
authored
[BOLT] Print heatmap from perf2bolt (#139194)
Add perf2bolt `--heatmap` option to produce heatmaps during profile aggregation. Distinguish exclusive mode (`llvm-bolt-heatmap`) and optional mode (`perf2bolt --heatmap`), which impacts perf.data handling: exclusive mode covers all addresses, whereas optional mode consumes attached profile only covering function addresses. Test Plan: updated per2bolt tests: - pre-aggregated-perf.test: pre-aggregated data, - bolt-address-translation-yaml.test: pre-aggregated + BOLTed input, - perf_test.test: no-LBR perf data.
1 parent 7f4febd commit 0289ca0

File tree

10 files changed

+73
-41
lines changed

10 files changed

+73
-41
lines changed

bolt/include/bolt/Utils/CommandLineOpts.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@
1717

1818
namespace opts {
1919

20-
extern bool HeatmapMode;
20+
enum HeatmapModeKind {
21+
HM_None = 0,
22+
HM_Exclusive, // llvm-bolt-heatmap
23+
HM_Optional // perf2bolt --heatmap
24+
};
25+
26+
extern HeatmapModeKind HeatmapMode;
2127
extern bool BinaryAnalysisMode;
2228

2329
extern llvm::cl::OptionCategory BoltCategory;
@@ -45,6 +51,7 @@ extern llvm::cl::opt<unsigned> HeatmapBlock;
4551
extern llvm::cl::opt<unsigned long long> HeatmapMaxAddress;
4652
extern llvm::cl::opt<unsigned long long> HeatmapMinAddress;
4753
extern llvm::cl::opt<bool> HeatmapPrintMappings;
54+
extern llvm::cl::opt<std::string> HeatmapOutput;
4855
extern llvm::cl::opt<bool> HotData;
4956
extern llvm::cl::opt<bool> HotFunctionsAtEnd;
5057
extern llvm::cl::opt<bool> HotText;

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ extern cl::opt<bool> UpdateDebugSections;
6666
extern cl::opt<unsigned> Verbosity;
6767

6868
extern bool BinaryAnalysisMode;
69-
extern bool HeatmapMode;
69+
extern HeatmapModeKind HeatmapMode;
7070
extern bool processAllFunctions();
7171

7272
static cl::opt<bool> CheckEncoding(

bolt/lib/Profile/DataAggregator.cpp

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ void DataAggregator::findPerfExecutable() {
164164
void DataAggregator::start() {
165165
outs() << "PERF2BOLT: Starting data aggregation job for " << Filename << "\n";
166166

167+
// Turn on heatmap building if requested by --heatmap flag.
168+
if (!opts::HeatmapMode && opts::HeatmapOutput.getNumOccurrences())
169+
opts::HeatmapMode = opts::HeatmapModeKind::HM_Optional;
170+
167171
// Don't launch perf for pre-aggregated files or when perf input is specified
168172
// by the user.
169173
if (opts::ReadPreAggregated || !opts::ReadPerfEvents.empty())
@@ -502,24 +506,25 @@ Error DataAggregator::preprocessProfile(BinaryContext &BC) {
502506
errs() << "PERF2BOLT: failed to parse samples\n";
503507

504508
// Special handling for memory events
505-
if (prepareToParse("mem events", MemEventsPPI, MemEventsErrorCallback))
506-
return Error::success();
507-
508-
if (const std::error_code EC = parseMemEvents())
509-
errs() << "PERF2BOLT: failed to parse memory events: " << EC.message()
510-
<< '\n';
509+
if (!prepareToParse("mem events", MemEventsPPI, MemEventsErrorCallback))
510+
if (const std::error_code EC = parseMemEvents())
511+
errs() << "PERF2BOLT: failed to parse memory events: " << EC.message()
512+
<< '\n';
511513

512514
deleteTempFiles();
513515

514516
heatmap:
515-
if (opts::HeatmapMode) {
516-
if (std::error_code EC = printLBRHeatMap()) {
517-
errs() << "ERROR: failed to print heat map: " << EC.message() << '\n';
518-
exit(1);
519-
}
520-
exit(0);
521-
}
522-
return Error::success();
517+
if (!opts::HeatmapMode)
518+
return Error::success();
519+
520+
if (std::error_code EC = printLBRHeatMap())
521+
return errorCodeToError(EC);
522+
523+
if (opts::HeatmapMode == opts::HeatmapModeKind::HM_Optional)
524+
return Error::success();
525+
526+
assert(opts::HeatmapMode == opts::HeatmapModeKind::HM_Exclusive);
527+
exit(0);
523528
}
524529

525530
Error DataAggregator::readProfile(BinaryContext &BC) {
@@ -1351,15 +1356,14 @@ std::error_code DataAggregator::printLBRHeatMap() {
13511356
exit(1);
13521357
}
13531358

1354-
HM.print(opts::OutputFilename);
1355-
if (opts::OutputFilename == "-")
1356-
HM.printCDF(opts::OutputFilename);
1357-
else
1358-
HM.printCDF(opts::OutputFilename + ".csv");
1359-
if (opts::OutputFilename == "-")
1360-
HM.printSectionHotness(opts::OutputFilename);
1361-
else
1362-
HM.printSectionHotness(opts::OutputFilename + "-section-hotness.csv");
1359+
HM.print(opts::HeatmapOutput);
1360+
if (opts::HeatmapOutput == "-") {
1361+
HM.printCDF(opts::HeatmapOutput);
1362+
HM.printSectionHotness(opts::HeatmapOutput);
1363+
} else {
1364+
HM.printCDF(opts::HeatmapOutput + ".csv");
1365+
HM.printSectionHotness(opts::HeatmapOutput + "-section-hotness.csv");
1366+
}
13631367

13641368
return std::error_code();
13651369
}
@@ -1386,7 +1390,7 @@ void DataAggregator::parseLBRSample(const PerfBranchSample &Sample,
13861390
const uint64_t TraceTo = NextLBR->From;
13871391
const BinaryFunction *TraceBF =
13881392
getBinaryFunctionContainingAddress(TraceFrom);
1389-
if (opts::HeatmapMode) {
1393+
if (opts::HeatmapMode == opts::HeatmapModeKind::HM_Exclusive) {
13901394
FTInfo &Info = FallthroughLBRs[Trace(TraceFrom, TraceTo)];
13911395
++Info.InternCount;
13921396
} else if (TraceBF && TraceBF->containsAddress(TraceTo)) {
@@ -1424,7 +1428,7 @@ void DataAggregator::parseLBRSample(const PerfBranchSample &Sample,
14241428
NextLBR = &LBR;
14251429

14261430
// Record branches outside binary functions for heatmap.
1427-
if (opts::HeatmapMode) {
1431+
if (opts::HeatmapMode == opts::HeatmapModeKind::HM_Exclusive) {
14281432
TakenBranchInfo &Info = BranchLBRs[Trace(LBR.From, LBR.To)];
14291433
++Info.TakenCount;
14301434
continue;
@@ -1439,7 +1443,8 @@ void DataAggregator::parseLBRSample(const PerfBranchSample &Sample,
14391443
}
14401444
// Record LBR addresses not covered by fallthroughs (bottom-of-stack source
14411445
// and top-of-stack target) as basic samples for heatmap.
1442-
if (opts::HeatmapMode && !Sample.LBR.empty()) {
1446+
if (opts::HeatmapMode == opts::HeatmapModeKind::HM_Exclusive &&
1447+
!Sample.LBR.empty()) {
14431448
++BasicSamples[Sample.LBR.front().To];
14441449
++BasicSamples[Sample.LBR.back().From];
14451450
}

bolt/lib/Profile/Heatmap.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,11 @@ void Heatmap::printSectionHotness(raw_ostream &OS) const {
305305

306306
uint64_t UnmappedHotness = 0;
307307
auto RecordUnmappedBucket = [&](uint64_t Address, uint64_t Frequency) {
308-
errs() << "Couldn't map the address bucket [0x" << Twine::utohexstr(Address)
309-
<< ", 0x" << Twine::utohexstr(Address + BucketSize)
310-
<< "] containing " << Frequency
311-
<< " samples to a text section in the binary.";
308+
if (opts::Verbosity >= 1)
309+
errs() << "Couldn't map the address bucket [0x"
310+
<< Twine::utohexstr(Address) << ", 0x"
311+
<< Twine::utohexstr(Address + BucketSize) << "] containing "
312+
<< Frequency << " samples to a text section in the binary.";
312313
UnmappedHotness += Frequency;
313314
};
314315

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,7 +1453,8 @@ void RewriteInstance::updateRtFiniReloc() {
14531453
}
14541454

14551455
void RewriteInstance::registerFragments() {
1456-
if (!BC->HasSplitFunctions || opts::HeatmapMode)
1456+
if (!BC->HasSplitFunctions ||
1457+
opts::HeatmapMode == opts::HeatmapModeKind::HM_Exclusive)
14571458
return;
14581459

14591460
// Process fragments with ambiguous parents separately as they are typically a
@@ -1998,7 +1999,7 @@ Error RewriteInstance::readSpecialSections() {
19981999
BC->getUniqueSectionByName(BoltAddressTranslation::SECTION_NAME)) {
19992000
BC->HasBATSection = true;
20002001
// Do not read BAT when plotting a heatmap
2001-
if (!opts::HeatmapMode) {
2002+
if (opts::HeatmapMode != opts::HeatmapModeKind::HM_Exclusive) {
20022003
if (std::error_code EC = BAT->parse(BC->outs(), BATSec->getContents())) {
20032004
BC->errs() << "BOLT-ERROR: failed to parse BOLT address translation "
20042005
"table.\n";
@@ -2037,7 +2038,7 @@ Error RewriteInstance::readSpecialSections() {
20372038
}
20382039

20392040
// Force non-relocation mode for heatmap generation
2040-
if (opts::HeatmapMode)
2041+
if (opts::HeatmapMode == opts::HeatmapModeKind::HM_Exclusive)
20412042
BC->HasRelocations = false;
20422043

20432044
if (BC->HasRelocations)

bolt/lib/Utils/CommandLineOpts.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const char *BoltRevision =
2828

2929
namespace opts {
3030

31-
bool HeatmapMode = false;
31+
HeatmapModeKind HeatmapMode = HM_None;
3232
bool BinaryAnalysisMode = false;
3333

3434
cl::OptionCategory BoltCategory("BOLT generic options");
@@ -124,6 +124,10 @@ cl::opt<bool> HeatmapPrintMappings(
124124
"sections (default false)"),
125125
cl::Optional, cl::cat(HeatmapCategory));
126126

127+
cl::opt<std::string> HeatmapOutput("heatmap",
128+
cl::desc("print heatmap to a given file"),
129+
cl::Optional, cl::cat(HeatmapCategory));
130+
127131
cl::opt<bool> HotData("hot-data",
128132
cl::desc("hot data symbols support (relocation mode)"),
129133
cl::cat(BoltCategory));

bolt/test/X86/bolt-address-translation-yaml.test

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ ORDER-YAML-CHECK-NEXT: calls: [ { off: 0x26, fid: [[#]], cnt: 20 } ]
2828
ORDER-YAML-CHECK-NEXT: succ: [ { bid: 5, cnt: 7 }
2929
## Large profile test
3030
RUN: perf2bolt %t.out --pa -p %p/Inputs/blarge_new_bat.preagg.txt -w %t.yaml -o %t.fdata \
31-
RUN: 2>&1 | FileCheck --check-prefix READ-BAT-CHECK %s
31+
RUN: --heatmap %t.hm 2>&1 | FileCheck --check-prefix READ-BAT-CHECK %s
3232
RUN: FileCheck --input-file %t.yaml --check-prefix YAML-BAT-CHECK %s
33+
RUN: FileCheck --input-file %t.hm-section-hotness.csv --check-prefix CHECK-HM %s
3334
## Check that YAML converted from fdata matches YAML created directly with BAT.
3435
RUN: llvm-bolt %t.exe -data %t.fdata -w %t.yaml-fdata -o /dev/null \
3536
RUN: 2>&1 | FileCheck --check-prefix READ-BAT-FDATA-CHECK %s
@@ -46,8 +47,10 @@ WRITE-BAT-CHECK: BOLT-INFO: BAT section size (bytes): 404
4647
READ-BAT-CHECK-NOT: BOLT-ERROR: unable to save profile in YAML format for input file processed by BOLT
4748
READ-BAT-CHECK: BOLT-INFO: Parsed 5 BAT entries
4849
READ-BAT-CHECK: PERF2BOLT: read 79 aggregated LBR entries
50+
READ-BAT-CHECK: HEATMAP: building heat map
4951
READ-BAT-CHECK: BOLT-INFO: 5 out of 21 functions in the binary (23.8%) have non-empty execution profile
5052
READ-BAT-FDATA-CHECK: BOLT-INFO: 5 out of 16 functions in the binary (31.2%) have non-empty execution profile
53+
CHECK-HM: .text, 0x800000, 0x8002cc, 38.7595, 91.6667, 0.3553
5154

5255
YAML-BAT-CHECK: functions:
5356
# Function not covered by BAT - has insns in basic block

bolt/test/X86/pre-aggregated-perf.test

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ REQUIRES: system-linux
1111

1212
RUN: yaml2obj %p/Inputs/blarge.yaml &> %t.exe
1313
RUN: perf2bolt %t.exe -o %t --pa -p %p/Inputs/pre-aggregated.txt -w %t.new \
14-
RUN: --show-density \
14+
RUN: --show-density --heatmap %t.hm \
1515
RUN: --profile-density-threshold=9 --profile-density-cutoff-hot=970000 \
1616
RUN: --profile-use-dfs | FileCheck %s --check-prefix=CHECK-P2B
17+
RUN: FileCheck --input-file %t.hm-section-hotness.csv --check-prefix=CHECK-HM %s
1718

19+
CHECK-P2B: HEATMAP: building heat map
1820
CHECK-P2B: BOLT-INFO: 4 out of 7 functions in the binary (57.1%) have non-empty execution profile
1921
CHECK-P2B: BOLT-INFO: Functions with density >= 21.7 account for 97.00% total sample counts.
22+
CHECK-HM: .text, 0x400680, 0x401232, 100.0000, 4.2553, 0.0426
2023

2124
RUN: perf2bolt %t.exe -o %t --pa -p %p/Inputs/pre-aggregated.txt -w %t.new \
2225
RUN: --show-density \

bolt/test/perf2bolt/perf_test.test

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@ REQUIRES: system-linux, perf
44

55
RUN: %clang %S/Inputs/perf_test.c -fuse-ld=lld -Wl,--script=%S/Inputs/perf_test.lds -o %t
66
RUN: perf record -Fmax -e cycles:u -o %t2 -- %t
7-
RUN: perf2bolt %t -p=%t2 -o %t3 -nl -ignore-build-id --show-density 2>&1 | FileCheck %s
7+
RUN: perf2bolt %t -p=%t2 -o %t3 -nl -ignore-build-id --show-density \
8+
RUN: --heatmap %t.hm 2>&1 | FileCheck %s
9+
RUN: FileCheck %s --input-file %t.hm-section-hotness.csv --check-prefix=CHECK-HM
810

911
CHECK-NOT: PERF2BOLT-ERROR
1012
CHECK-NOT: !! WARNING !! This high mismatch ratio indicates the input binary is probably not the same binary used during profiling collection.
13+
CHECK: HEATMAP: building heat map
1114
CHECK: BOLT-INFO: Functions with density >= {{.*}} account for 99.00% total sample counts.
1215

1316
RUN: %clang %S/Inputs/perf_test.c -no-pie -fuse-ld=lld -o %t4
1417
RUN: perf record -Fmax -e cycles:u -o %t5 -- %t4
15-
RUN: perf2bolt %t4 -p=%t5 -o %t6 -nl -ignore-build-id --show-density 2>&1 | FileCheck %s
18+
RUN: perf2bolt %t4 -p=%t5 -o %t6 -nl -ignore-build-id --show-density \
19+
RUN: --heatmap %t.hm2 2>&1 | FileCheck %s
20+
RUN: FileCheck %s --input-file %t.hm2-section-hotness.csv --check-prefix=CHECK-HM
21+
22+
CHECK-HM: .text

bolt/tools/heatmap/heatmap.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,15 @@ int main(int argc, char **argv) {
6666
exit(1);
6767
}
6868

69-
opts::HeatmapMode = true;
69+
opts::HeatmapMode = opts::HM_Exclusive;
7070
opts::AggregateOnly = true;
7171
if (!sys::fs::exists(opts::InputFilename))
7272
report_error(opts::InputFilename, errc::no_such_file_or_directory);
7373

7474
// Output to stdout by default
7575
if (opts::OutputFilename.empty())
7676
opts::OutputFilename = "-";
77+
opts::HeatmapOutput.assign(opts::OutputFilename);
7778

7879
// Initialize targets and assembly printers/parsers.
7980
#define BOLT_TARGET(target) \

0 commit comments

Comments
 (0)