Ray's Knowledge Base
Unlock

Gungraun counts only the bench thread, so work in tokio spawn_blocking jobs counts as zero

PitfallVerified 27 Oct 2026Holds gungraun: 0.20, tokio: 1
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.

Symptom#

A gungraun (Callgrind) library bench of an async function shows a huge drop after file work moved into tokio::task::spawn_blocking: a read bench went from 3,207,691 to 57,309 instructions (-98%), an edit bench from 5,376,043 to 31,019. Benches that already used spawn_blocking (a parallel directory walk) never counted that work.

Cause#

Gungraun sets --toggle-collect=<bench function> and --collect-at-start=no. Callgrind collects in a thread only inside a toggled function, so tokio blocking pool threads collect nothing. A toggle on *BlockingTask*poll* does not help: that poll is inlined.

Fix#

Add a second toggle on tokio's raw poll of a blocking task, which is a real symbol:

const BLOCKING_JOB: &str =
    "--toggle-collect=tokio::runtime::task::raw::poll::<tokio::runtime::blocking::task::BlockingTask*";
main!(
    config = LibraryBenchmarkConfig::default().tool(Callgrind::with_args([BLOCKING_JOB])),
    library_benchmark_groups = tools
);

Build the bench fixtures in setup with std::fs, not tokio::fs: a setup that runs a blocking job is counted too. Find the real symbol names with nm -C <bench binary> | grep task::raw::poll.

Evidence#

With the toggle, the read bench counted 2,758,235 instructions and the edit bench 5,416,606 on Linux. The same bench file on the code before the change gave 3,279,325 and 5,492,322, so the comparison is fair again.

Also found by searching

  • gungraun bench shows almost no instructions for a tool that uses spawn_blocking
  • callgrind counts zero for work on tokio blocking pool threads
  • instruction count dropped 98% after moving file io into spawn_blocking
  • how to count other threads in a gungraun library benchmark