Skip to content

Commit 068af5b

Browse files
authored
[lld][BP] Print total size of startup symbols (#145106)
A good proxy to estimate the number of page faults during startup is the total size of startup functions. Assuming profiles are up-to-date, we can measure this total size pretty easily. Note that if profile data is old, this number could be wrong.
1 parent daa2a58 commit 068af5b

File tree

3 files changed

+46
-30
lines changed

3 files changed

+46
-30
lines changed

lld/include/lld/Common/BPSectionOrdererBase.inc

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -292,45 +292,54 @@ auto BPOrderer<D>::computeOrder(
292292
bp.run(nodesForDataCompression);
293293
}
294294

295-
unsigned numStartupSections = 0;
296-
unsigned numCodeCompressionSections = 0;
297-
unsigned numDuplicateCodeSections = 0;
298-
unsigned numDataCompressionSections = 0;
299-
unsigned numDuplicateDataSections = 0;
295+
unsigned numStartupSections = 0, startupSize = 0;
296+
unsigned numCodeCompressionSections = 0, codeCompressionSize = 0;
297+
unsigned numDuplicateCodeSections = 0, duplicateCodeSize = 0;
298+
unsigned numDataCompressionSections = 0, dataCompressionSize = 0;
299+
unsigned numDuplicateDataSections = 0, duplicateDataSize = 0;
300300
SetVector<const Section *> orderedSections;
301301
// Order startup functions,
302302
for (auto &node : nodesForStartup) {
303303
const auto *isec = sections[node.Id];
304-
if (orderedSections.insert(isec))
304+
if (orderedSections.insert(isec)) {
305+
startupSize += D::getSize(*isec);
305306
++numStartupSections;
307+
}
306308
}
307309
// then functions for compression,
308310
for (auto &node : nodesForFunctionCompression) {
309311
const auto *isec = sections[node.Id];
310-
if (orderedSections.insert(isec))
312+
if (orderedSections.insert(isec)) {
313+
codeCompressionSize += D::getSize(*isec);
311314
++numCodeCompressionSections;
312-
315+
}
313316
auto It = duplicateSectionIdxs.find(node.Id);
314317
if (It == duplicateSectionIdxs.end())
315318
continue;
316319
for (auto dupSecIdx : It->getSecond()) {
317320
const auto *dupIsec = sections[dupSecIdx];
318-
if (orderedSections.insert(dupIsec))
321+
if (orderedSections.insert(dupIsec)) {
322+
duplicateCodeSize += D::getSize(*isec);
319323
++numDuplicateCodeSections;
324+
}
320325
}
321326
}
322327
// then data for compression.
323328
for (auto &node : nodesForDataCompression) {
324329
const auto *isec = sections[node.Id];
325-
if (orderedSections.insert(isec))
330+
if (orderedSections.insert(isec)) {
331+
dataCompressionSize += D::getSize(*isec);
326332
++numDataCompressionSections;
333+
}
327334
auto It = duplicateSectionIdxs.find(node.Id);
328335
if (It == duplicateSectionIdxs.end())
329336
continue;
330337
for (auto dupSecIdx : It->getSecond()) {
331338
const auto *dupIsec = sections[dupSecIdx];
332-
if (orderedSections.insert(dupIsec))
339+
if (orderedSections.insert(dupIsec)) {
340+
duplicateDataSize += D::getSize(*isec);
333341
++numDuplicateDataSections;
342+
}
334343
}
335344
}
336345

@@ -339,14 +348,21 @@ auto BPOrderer<D>::computeOrder(
339348
numStartupSections + numCodeCompressionSections +
340349
numDuplicateCodeSections + numDataCompressionSections +
341350
numDuplicateDataSections;
342-
dbgs()
343-
<< "Ordered " << numTotalOrderedSections
344-
<< " sections using balanced partitioning:\n Functions for startup: "
345-
<< numStartupSections
346-
<< "\n Functions for compression: " << numCodeCompressionSections
347-
<< "\n Duplicate functions: " << numDuplicateCodeSections
348-
<< "\n Data for compression: " << numDataCompressionSections
349-
<< "\n Duplicate data: " << numDuplicateDataSections << "\n";
351+
unsigned totalOrderedSize = startupSize + codeCompressionSize +
352+
duplicateCodeSize + dataCompressionSize +
353+
duplicateDataSize;
354+
dbgs() << "Ordered " << numTotalOrderedSections << " sections ("
355+
<< totalOrderedSize << " bytes) using balanced partitioning:\n";
356+
dbgs() << " Functions for startup: " << numStartupSections << " ("
357+
<< startupSize << " bytes)\n";
358+
dbgs() << " Functions for compression: " << numCodeCompressionSections
359+
<< " (" << codeCompressionSize << " bytes)\n";
360+
dbgs() << " Duplicate functions: " << numDuplicateCodeSections << " ("
361+
<< duplicateCodeSize << " bytes)\n";
362+
dbgs() << " Data for compression: " << numDataCompressionSections << " ("
363+
<< dataCompressionSize << " bytes)\n";
364+
dbgs() << " Duplicate data: " << numDuplicateDataSections << " ("
365+
<< duplicateDataSize << " bytes)\n";
350366

351367
if (!profilePath.empty()) {
352368
// Evaluate this function order for startup

lld/test/ELF/bp-section-orderer.s

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# RUN: llvm-profdata merge a.proftext -o a.profdata
2222
# RUN: ld.lld a.o --irpgo-profile=a.profdata --bp-startup-sort=function --verbose-bp-section-orderer --icf=all --gc-sections 2>&1 | FileCheck %s --check-prefix=STARTUP-FUNC-ORDER
2323

24-
# STARTUP-FUNC-ORDER: Ordered 3 sections using balanced partitioning
24+
# STARTUP-FUNC-ORDER: Ordered 3 sections ([[#]] bytes) using balanced partitioning
2525
# STARTUP-FUNC-ORDER: Total area under the page fault curve: 3.
2626

2727
# RUN: ld.lld -o out.s a.o --irpgo-profile=a.profdata --bp-startup-sort=function
@@ -49,10 +49,10 @@
4949
# RUN: llvm-nm -jn out.cbs | tr '\n' , | FileCheck %s --check-prefix=CBOTH-STARTUP
5050
# CBOTH-STARTUP: s5,s3,s4,s2,s1,A,B,C,F,E,D,merged1,merged2,_start,d4,d1,d3,d2,{{$}}
5151

52-
# BP-COMPRESSION-FUNC: Ordered 9 sections using balanced partitioning
53-
# BP-COMPRESSION-ICF-FUNC: Ordered 8 sections using balanced partitioning
54-
# BP-COMPRESSION-DATA: Ordered 9 sections using balanced partitioning
55-
# BP-COMPRESSION-BOTH: Ordered 18 sections using balanced partitioning
52+
# BP-COMPRESSION-FUNC: Ordered 9 sections ([[#]] bytes) using balanced partitioning
53+
# BP-COMPRESSION-ICF-FUNC: Ordered 8 sections ([[#]] bytes) using balanced partitioning
54+
# BP-COMPRESSION-DATA: Ordered 9 sections ([[#]] bytes) using balanced partitioning
55+
# BP-COMPRESSION-BOTH: Ordered 18 sections ([[#]] bytes) using balanced partitioning
5656

5757
#--- a.proftext
5858
:ir

lld/test/MachO/bp-section-orderer.s

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
# RUN: %lld -arch arm64 -lSystem -e _main -o %t/a.out %t/a.o --irpgo-profile %t/a.profdata --bp-startup-sort=function --verbose-bp-section-orderer 2>&1 | FileCheck %s --check-prefix=STARTUP
1111
# RUN: %lld -arch arm64 -lSystem -e _main -o %t/a.out %t/a.o --irpgo-profile=%t/a.profdata --bp-startup-sort=function --verbose-bp-section-orderer --icf=all --bp-compression-sort=none 2>&1 | FileCheck %s --check-prefix=STARTUP-ICF
12-
# STARTUP: Ordered 5 sections using balanced partitioning
13-
# STARTUP-ICF: Ordered 4 sections using balanced partitioning
12+
# STARTUP: Ordered 5 sections ([[#]] bytes) using balanced partitioning
13+
# STARTUP-ICF: Ordered 4 sections ([[#]] bytes) using balanced partitioning
1414

1515
# Check that orderfiles take precedence over BP
1616
# RUN: %no-fatal-warnings-lld -arch arm64 -lSystem -e _main -o - %t/a.o -order_file %t/a.orderfile --irpgo-profile-sort=%t/a.profdata | llvm-nm --numeric-sort --format=just-symbols - | FileCheck %s --check-prefix=ORDERFILE
@@ -50,10 +50,10 @@
5050
# RUN: %lld -arch arm64 -lSystem -e _main -o %t/a.out %t/a.o --verbose-bp-section-orderer --bp-compression-sort=both 2>&1 | FileCheck %s --check-prefix=COMPRESSION-BOTH
5151
# RUN: %lld -arch arm64 -lSystem -e _main -o %t/a.out %t/a.o --verbose-bp-section-orderer --bp-compression-sort=both --irpgo-profile=%t/a.profdata --bp-startup-sort=function 2>&1 | FileCheck %s --check-prefix=COMPRESSION-BOTH
5252

53-
# COMPRESSION-FUNC: Ordered 9 sections using balanced partitioning
54-
# COMPRESSION-ICF-FUNC: Ordered 7 sections using balanced partitioning
55-
# COMPRESSION-DATA: Ordered 7 sections using balanced partitioning
56-
# COMPRESSION-BOTH: Ordered 16 sections using balanced partitioning
53+
# COMPRESSION-FUNC: Ordered 9 sections ([[#]] bytes) using balanced partitioning
54+
# COMPRESSION-ICF-FUNC: Ordered 7 sections ([[#]] bytes) using balanced partitioning
55+
# COMPRESSION-DATA: Ordered 7 sections ([[#]] bytes) using balanced partitioning
56+
# COMPRESSION-BOTH: Ordered 16 sections ([[#]] bytes) using balanced partitioning
5757

5858
#--- a.s
5959
.text

0 commit comments

Comments
 (0)