Skip to content

Commit 3b5a39f

Browse files
committed
ci(bench): pin benchmark code placement
A binary's page-cache placement shifts its speed, so walltime benches run from huge pages and perfcompare re-copies binaries per part.
1 parent 261eb33 commit 3b5a39f

3 files changed

Lines changed: 48 additions & 1 deletion

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Prepare huge-page text
2+
description: >
3+
Build elfremapper and reserve the huge pages it needs. Preloaded into a benchmark, it moves the
4+
executable's code and data onto 2 MiB pages, so their physical placement, which otherwise
5+
differs between copies of the same binary, stops affecting the timings.
6+
outputs:
7+
preload:
8+
description: Library to preload into a benchmark
9+
value: ${{ steps.build.outputs.preload }}
10+
runs:
11+
using: composite
12+
steps:
13+
- id: build
14+
shell: bash
15+
env:
16+
REV: 8e19b343deabe6c9e05d5882e173afa846d3832a # dmitriy-philimonov/elfremapper
17+
run: |
18+
echo 64 | sudo tee /proc/sys/vm/nr_hugepages > /dev/null # A benchmark process takes three.
19+
[ "$APT_UPDATED" ] || sudo apt-get update && echo "APT_UPDATED=1" >> "$GITHUB_ENV"
20+
sudo apt-get install -y --no-install-recommends cmake g++
21+
src="$RUNNER_TEMP/elfremapper"
22+
mkdir -p "$src"
23+
curl -fsSL "https://github.com/dmitriy-philimonov/elfremapper/archive/$REV.tar.gz" | tar -xz -C "$src" --strip-components=1
24+
cmake -S "$src" -B "$src/build" -DCMAKE_BUILD_TYPE=Release -DMAKE_LD_PRELOAD_LIBRARY=1
25+
cmake --build "$src/build"
26+
echo "preload=$(find "$src/build" -name 'libelfremapper.so.*' -type f)" >> "$GITHUB_OUTPUT"

‎.github/scripts/perfcompare.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,19 @@ def units() -> list[Unit]:
421421
return out
422422

423423

424+
def recopy(cfg, cmds):
425+
"""Replace each command's executable with a fresh copy of itself.
426+
427+
Where the page cache happens to hold a binary's code shifts its speed by up to ~1.5%, and that
428+
placement holds for the file's lifetime, so give each part a placement of its own.
429+
"""
430+
for cmd in cmds:
431+
exe = cfg.workspace / cmd.split()[0]
432+
copy = exe.with_name(f"{exe.name}.copy")
433+
shutil.copy2(exe, copy)
434+
copy.replace(exe)
435+
436+
424437
def run_unit(cfg, tmp, unit):
425438
"""Run one unit's comparison; `merge` turns its results into a table row."""
426439

@@ -458,6 +471,7 @@ def command(template, flags, disk=""):
458471
for part, order in enumerate(schedule):
459472
for side in order:
460473
cmds, out_dir = sides[side]
474+
recopy(cfg, cmds)
461475
hyperfine(cfg, *cmds, unit.name, out_dir, runs, part)
462476
# After the timed runs, so recording cannot drift them apart.
463477
perf(cfg, scmd, ccmd, unit.name)

‎.github/workflows/bench.yml‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,20 +188,27 @@ jobs:
188188
with:
189189
mtu: ${{ env.MTU }}
190190

191+
- name: Prepare huge-page text
192+
id: huge-page-text
193+
if: ${{ matrix.benchmark.mode == 'walltime' }}
194+
uses: $/.github/actions/huge-page-text
195+
191196
- name: Enable perf
192197
if: ${{ matrix.benchmark.mode != 'walltime' }}
193198
uses: $/.github/actions/enable-perf
194199

195200
- name: Run cargo bench
196201
env:
197202
BENCH_CPUS: ${{ steps.machine.outputs.bench-cpus }} # Two: the benches serve from a thread.
203+
PRELOAD: ${{ steps.huge-page-text.outputs.preload }}
198204
RECORD_OPT: ${{ matrix.benchmark.mode == 'walltime' && env.PERF_OPT || env.PERF_OPT_GITHUB }}
199205
run: |
200206
# Only the pinned runner has CPUs to pin to.
201207
PIN=()
202208
[ -z "$BENCH_CPUS" ] || PIN=(setarch --addr-no-randomize taskset -c "$BENCH_CPUS")
203209
# `MTU` is part of the benchmark names `--exact` matches; `LC_ALL=C` keeps perf parseable.
204-
bench_exec() { env "MTU=$MTU" "LC_ALL=C" "${PIN[@]}" "$@"; }
210+
# Only the benchmark preloads `PRELOAD`: perf can't symbolize remapped code, so not `perf_exec`.
211+
bench_exec() { env "MTU=$MTU" "LC_ALL=C" "${PIN[@]}" env "LD_PRELOAD=$PRELOAD" "$@"; }
205212
# Only `perf` needs root, for the tracepoints; `sudo` resets NSS's paths.
206213
perf_exec() { sudo env "MTU=$MTU" "LC_ALL=C" "LD_LIBRARY_PATH=$LD_LIBRARY_PATH" "TEST_FIXTURE_DB=$TEST_FIXTURE_DB" "${PIN[@]}" perf "$@"; }
207214
shopt -s extglob globstar nullglob

0 commit comments

Comments
 (0)