Skip to content

Commit 7f4febd

Browse files
authored
[BOLT][heatmap] Compute section utilization and partition score (#139193)
Heatmap groups samples into buckets of configurable size (`--block-size` flag with 64 bytes as the default =X86 cache line size). Buckets are mapped to containing sections; for buckets that cover multiple sections, they are attributed to the first overlapping section. Buckets not mapped to a section are reported as unmapped. Heatmap reports **section hotness** which is a percentage of samples attributed to the section. Define **section utilization** as a percentage of buckets with non-zero samples relative to the total number of section buckets. Also define section **partition score** as a product of section hotness (where total excludes unmapped buckets) and mapped utilization, ranging from 0 to 1 (higher is better). The intended use of new metrics is with **production profile** collected from BOLT-optimized binary. In this case the partition score of .text (hot text if function splitting is enabled) reflects **optimization profile** representativeness and the quality of hot-cold splitting. Partition score of 1 means that all samples fall into hot text, and all buckets (cache lines) in hot text are exercised, equivalent to perfect hot-cold splitting. Test Plan: updated heatmap-preagg.test
1 parent 3b3adef commit 7f4febd

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed

bolt/lib/Profile/Heatmap.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ void Heatmap::printSectionHotness(StringRef FileName) const {
297297
void Heatmap::printSectionHotness(raw_ostream &OS) const {
298298
uint64_t NumTotalCounts = 0;
299299
StringMap<uint64_t> SectionHotness;
300+
StringMap<uint64_t> BucketUtilization;
300301
unsigned TextSectionIndex = 0;
301302

302303
if (TextSections.empty())
@@ -325,23 +326,29 @@ void Heatmap::printSectionHotness(raw_ostream &OS) const {
325326
continue;
326327
}
327328
SectionHotness[TextSections[TextSectionIndex].Name] += KV.second;
329+
++BucketUtilization[TextSections[TextSectionIndex].Name];
328330
}
329331

330332
assert(NumTotalCounts > 0 &&
331333
"total number of heatmap buckets should be greater than 0");
332334

333-
OS << "Section Name, Begin Address, End Address, Percentage Hotness\n";
334-
for (auto &TextSection : TextSections) {
335-
OS << TextSection.Name << ", 0x"
336-
<< Twine::utohexstr(TextSection.BeginAddress) << ", 0x"
337-
<< Twine::utohexstr(TextSection.EndAddress) << ", "
338-
<< format("%.4f",
339-
100.0 * SectionHotness[TextSection.Name] / NumTotalCounts)
340-
<< "\n";
335+
OS << "Section Name, Begin Address, End Address, Percentage Hotness, "
336+
<< "Utilization Pct, Partition Score\n";
337+
const uint64_t MappedCounts = NumTotalCounts - UnmappedHotness;
338+
for (const auto [Name, Begin, End] : TextSections) {
339+
const float Hotness = 1. * SectionHotness[Name] / NumTotalCounts;
340+
const float MappedHotness =
341+
MappedCounts ? 1. * SectionHotness[Name] / MappedCounts : 0;
342+
const uint64_t NumBuckets =
343+
End / BucketSize + !!(End % BucketSize) - Begin / BucketSize;
344+
const float Utilization = 1. * BucketUtilization[Name] / NumBuckets;
345+
const float PartitionScore = MappedHotness * Utilization;
346+
OS << formatv("{0}, {1:x}, {2:x}, {3:f4}, {4:f4}, {5:f4}\n", Name, Begin,
347+
End, 100. * Hotness, 100. * Utilization, PartitionScore);
341348
}
342349
if (UnmappedHotness > 0)
343-
OS << "[unmapped], 0x0, 0x0, "
344-
<< format("%.4f", 100.0 * UnmappedHotness / NumTotalCounts) << "\n";
350+
OS << formatv("[unmapped], 0x0, 0x0, {0:f4}, 0, 0\n",
351+
100.0 * UnmappedHotness / NumTotalCounts);
345352
}
346353
} // namespace bolt
347354
} // namespace llvm

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1453,7 +1453,7 @@ void RewriteInstance::updateRtFiniReloc() {
14531453
}
14541454

14551455
void RewriteInstance::registerFragments() {
1456-
if (!BC->HasSplitFunctions)
1456+
if (!BC->HasSplitFunctions || opts::HeatmapMode)
14571457
return;
14581458

14591459
// Process fragments with ambiguous parents separately as they are typically a

bolt/test/X86/heatmap-preagg.test

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,19 @@ RUN: FileCheck %s --check-prefix CHECK-SEC-HOT-BAT --input-file %t2-section-hotn
1717
CHECK-HEATMAP: PERF2BOLT: read 81 aggregated LBR entries
1818
CHECK-HEATMAP: HEATMAP: invalid traces: 1
1919

20-
CHECK-SEC-HOT: .init, 0x401000, 0x40101b, 16.8545
21-
CHECK-SEC-HOT-NEXT: .plt, 0x401020, 0x4010b0, 4.7583
22-
CHECK-SEC-HOT-NEXT: .text, 0x4010b0, 0x401c25, 78.3872
23-
CHECK-SEC-HOT-NEXT: .fini, 0x401c28, 0x401c35, 0.0000
20+
CHECK-SEC-HOT: Section Name, Begin Address, End Address, Percentage Hotness, Utilization Pct, Partition Score
21+
CHECK-SEC-HOT-NEXT: .init, 0x401000, 0x40101b, 16.8545, 100.0000, 0.1685
22+
CHECK-SEC-HOT-NEXT: .plt, 0x401020, 0x4010b0, 4.7583, 66.6667, 0.0317
23+
CHECK-SEC-HOT-NEXT: .text, 0x4010b0, 0x401c25, 78.3872, 85.1064, 0.6671
24+
CHECK-SEC-HOT-NEXT: .fini, 0x401c28, 0x401c35, 0.0000, 0.0000, 0.0000
2425

2526
CHECK-HEATMAP-BAT: PERF2BOLT: read 79 aggregated LBR entries
2627
CHECK-HEATMAP-BAT: HEATMAP: invalid traces: 2
2728

28-
CHECK-SEC-HOT-BAT: .init, 0x401000, 0x40101b, 17.2888
29-
CHECK-SEC-HOT-BAT-NEXT: .plt, 0x401020, 0x4010b0, 5.6132
30-
CHECK-SEC-HOT-BAT-NEXT: .bolt.org.text, 0x4010b0, 0x401c25, 38.3385
31-
CHECK-SEC-HOT-BAT-NEXT: .fini, 0x401c28, 0x401c35, 0.0000
32-
CHECK-SEC-HOT-BAT-NEXT: .text, 0x800000, 0x8002cc, 38.7595
33-
CHECK-SEC-HOT-BAT-NEXT: .text.cold, 0x800300, 0x800415, 0.0000
29+
CHECK-SEC-HOT-BAT: Section Name, Begin Address, End Address, Percentage Hotness, Utilization Pct, Partition Score
30+
CHECK-SEC-HOT-BAT-NEXT: .init, 0x401000, 0x40101b, 17.2888, 100.0000, 0.1729
31+
CHECK-SEC-HOT-BAT-NEXT: .plt, 0x401020, 0x4010b0, 5.6132, 66.6667, 0.0374
32+
CHECK-SEC-HOT-BAT-NEXT: .bolt.org.text, 0x4010b0, 0x401c25, 38.3385, 51.0638, 0.1958
33+
CHECK-SEC-HOT-BAT-NEXT: .fini, 0x401c28, 0x401c35, 0.0000, 0.0000, 0.0000
34+
CHECK-SEC-HOT-BAT-NEXT: .text, 0x800000, 0x8002cc, 38.7595, 91.6667, 0.3553
35+
CHECK-SEC-HOT-BAT-NEXT: .text.cold, 0x800300, 0x800415, 0.0000, 0.0000, 0.0000

0 commit comments

Comments
 (0)