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