From d1ab8607b70bf0330e69bb81560e7647ac467af3 Mon Sep 17 00:00:00 2001 From: Thomas Klemm Date: Sat, 14 Nov 2026 15:44:36 +0000 Subject: [PATCH] Make the Callgrind profile's summary line match its totals harness/callgrind.rb started instrumentation, zeroed the counters, ran the measured iterations, stopped instrumentation and then dumped. In that order Callgrind writes a wrapped negative summary line into the dump (summary: 18446744073709549767 for fib, totals: 1959670074). callgrind_annotate takes the summary as the program total, so PROGRAM TOTALS is meaningless and every function shows 0.00%, and a script that reads the summary gets a number near 2^64 for every benchmark. Zero before starting and dump before stopping: the summary then equals the totals, with and without --instr-atstart=no. The profile now also counts the dump request itself, about 2K instructions on fib. Zeroing again after stopping keeps the exit dump at zero; its summary still wraps when other threads ran during the measured iterations, because Callgrind's zero request resets only the calling thread. The benchmark name is computed before instrumentation starts, so its regexp match stays out of the profile now that the dump comes first. --- README.md | 10 ++++++---- harness/callgrind.rb | 9 ++++++--- test/callgrind_harness_test.rb | 10 +++++----- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 9e438619..9db91061 100644 --- a/README.md +++ b/README.md @@ -396,10 +396,12 @@ WARMUP_ITRS=10 MIN_BENCH_ITRS=1 HARNESS=callgrind \ ``` Setup and `WARMUP_ITRS` iterations (default: 10) run without instrumentation. -The harness then starts instrumentation, zeros counters, profiles exactly -`MIN_BENCH_ITRS` iterations (default: the benchmark's iteration hint), stops -instrumentation, and dumps stats. Unlike sampling profilers, this harness does -not use `MIN_BENCH_TIME`: even one iteration can be slow under Callgrind. +The harness then zeros counters, starts instrumentation, profiles exactly +`MIN_BENCH_ITRS` iterations (default: the benchmark's iteration hint), dumps +stats, and stops instrumentation. The dump's `summary:` and `totals:` lines +both hold the instruction count of the profiled iterations. Unlike sampling +profilers, this harness does not use `MIN_BENCH_TIME`: even one iteration can +be slow under Callgrind. On a measured-iteration failure it still dumps a partial profile and propagates the exception without writing results. diff --git a/harness/callgrind.rb b/harness/callgrind.rb index d860e81c..df3e0111 100644 --- a/harness/callgrind.rb +++ b/harness/callgrind.rb @@ -42,9 +42,10 @@ def run_benchmark(num_itrs_hint, **, &block) warmup << get_time - start end + name = benchmark_name begin - Valgrind.callgrind_start_instrumentation Valgrind.callgrind_zero_stats + Valgrind.callgrind_start_instrumentation bench_itrs.times do start = get_time block.call @@ -52,10 +53,12 @@ def run_benchmark(num_itrs_hint, **, &block) end ensure # Preserve a partial profile on failure, but never leave instrumentation on. + # Dump before stopping so the summary line matches the totals; zero this thread so its exit dump is empty. + Valgrind.callgrind_dump_stats(name) Valgrind.callgrind_stop_instrumentation - Valgrind.callgrind_dump_stats(benchmark_name) + Valgrind.callgrind_zero_stats end - puts "Callgrind stats dumped for #{benchmark_name} (output controlled by --callgrind-out-file)." + puts "Callgrind stats dumped for #{name} (output controlled by --callgrind-out-file)." return_results(warmup, bench) if ENV['RESULT_JSON_PATH'] end diff --git a/test/callgrind_harness_test.rb b/test/callgrind_harness_test.rb index d60a8600..bdffd136 100644 --- a/test/callgrind_harness_test.rb +++ b/test/callgrind_harness_test.rb @@ -76,18 +76,18 @@ def return_results(warmup, bench) end def test_profiles_hint_iterations_after_warmup - assert_equal ['stop', 'stop', 'work', 'work', 'start', 'zero', - 'work', 'work', 'work', 'stop', 'dump:-e', ['results', 2, 3]], run_harness + assert_equal ['stop', 'stop', 'work', 'work', 'zero', 'start', + 'work', 'work', 'work', 'dump:-e', 'stop', 'zero', ['results', 2, 3]], run_harness end def test_iteration_overrides_and_zero_warmup - assert_equal ['stop', 'stop', 'start', 'zero', 'work', 'stop', 'dump:-e', ['results', 0, 1]], + assert_equal ['stop', 'stop', 'zero', 'start', 'work', 'dump:-e', 'stop', 'zero', ['results', 0, 1]], run_harness({ 'WARMUP_ITRS' => '0', 'MIN_BENCH_ITRS' => '1' }) end def test_failure_stops_and_dumps_without_results - assert_equal ['stop', 'stop', 'work', 'work', 'start', 'zero', 'work', - 'stop', 'dump:-e', 'workload failed'], run_harness({}, fail_at: 3) + assert_equal ['stop', 'stop', 'work', 'work', 'zero', 'start', 'work', + 'dump:-e', 'stop', 'zero', 'workload failed'], run_harness({}, fail_at: 3) end def test_warmup_failure_never_starts_profiling