Skip to content

Make the Callgrind profile's summary line match its totals - #579

Open
thomasklemm wants to merge 1 commit into
ruby:mainfrom
thomasklemm:thomasklemm/callgrind-summary-matches-totals
Open

thomasklemm wants to merge 1 commit into
ruby:mainfrom
thomasklemm:thomasklemm/callgrind-summary-matches-totals

Conversation

@thomasklemm

Copy link
Copy Markdown
Contributor

harness/callgrind.rb starts instrumentation, zeroes the counters, runs the measured iterations, stops instrumentation and then dumps. In that order Callgrind writes a wrapped-around negative summary: line into the dump. fib, --warmup=1 --bench=1, Ruby 4.0.9, Valgrind 3.24.0:

summary: totals:
main, interpreter 18446744073709549767 1959670074
this PR, interpreter 1959671789 1959671789
main, YJIT 18446744073709549440 454676097
this PR, YJIT 454677958 454677958

callgrind_annotate takes the summary as the program total, so on main it prints 18,446,744,073,709,549,767 (100.0%) PROGRAM TOTALS and every function at 0.00%. A script that reads summary:, as zjit_callgrind.rb in ruby/ruby#19851 does, gets a number near 2^64 for every benchmark.

This zeroes before starting and dumps before stopping. The summary then equals the totals, with and without --instr-atstart=no. The profile now also counts the dump request itself: +1,715 instructions (interpreter) and +1,861 (YJIT) against main's totals on fib. Zeroing again after stopping keeps the dump at exit at 0, as on main, unless other threads ran during the measured iterations: 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.

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.
thomasklemm added a commit to thomasklemm/lowrail that referenced this pull request Nov 14, 2026
@tekknolagi

Copy link
Copy Markdown
Contributor

We probably want to use zero_stats instead of start/stop; so leave it on but zero immediately before block.call

@tekknolagi

Copy link
Copy Markdown
Contributor

I am in the middle of another PR that measures callgrind instruction counts per iteration instead of time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants