To get the per-call cost of an LD_PRELOAD tracer, subtract two callgrind runs of different length and read self cost
RecipeVerified 7 Oct 2026Holds anywhere
Recipe. When to use it, the steps, and what showed that they work.
When to use#
You want to know what a traced call costs and where it goes, and a single callgrind run is dominated by process and library startup (here 1.4 billion instructions for 60,000 calls).
Steps#
- Write a loop program that does N of each call you care about (pread, open+close, stat) and prints best-of-5 ns per call.
- Run it under
valgrind --tool=callgrind --callgrind-out-file=cg.NwithLD_PRELOADset, once with N=10000 and once with N=20000. - Run
callgrind_annotate cg.Nwithout--inclusive: the wrappers recurse, so inclusive costs come out garbled (thousands of percent, names ending in'2). - Parse the
Ir file:functionrows from both runs, subtract per function and divide by the extra operations. - Compare instructions with wall time from the same session. They move differently: atomics and locks cost cycles but few instructions.
Evidence#
dftracer, arm64 container: the first profile showed about 80 percent of 5,238 instructions per call in glibc snprintf, not in hashing or the clock. After writing JSON lines without snprintf it was 1,849 per call with metadata off. Wall-clock overhead per pread fell from +339 ns to +239 ns, a smaller gain than the instruction count.
Also found by searching
- how do I measure instructions per call of an interposition library
- callgrind inclusive numbers over 100% with recursive wrappers
- profile startup noise drowns the per-call cost in callgrind
- where does the overhead of a tracing library go