gprof

gprof is the profiler that introduced the call graph to everyday performance work. It was described in “gprof: a Call Graph Execution Profiler” by Susan L. Graham, Peter B. Kessler, and Marshall K. McKusick, presented at the SIGPLAN ‘82 Symposium on Compiler Construction (SIGPLAN Notices, volume 17, number 6, June 1982). Its central idea, stated in the paper, is to account for the running time of called routines within the running time of the routines that call them, so that a function’s cost is reported together with how much of that cost flows in from each of its callers.

Mechanically, gprof blends both profiling methods. When a program is compiled with profiling enabled (the -pg flag in the GNU toolchain), the compiler inserts a small piece of code at every function’s entry that records the caller/callee pair, building up exact arc counts of who called whom how many times. Simultaneously, the runtime takes statistical samples of the program counter at a fixed interval to estimate how much time is spent in each routine. Combining the sampled self-times with the measured call-arc counts lets gprof propagate time up the call graph and estimate, for example, how much of a workhorse routine’s cost belongs to each distinct caller.

The output reflects this design directly. The GNU gprof manual documents a flat profile, which ranks functions by their own running time and call counts, and a call graph, which for each function shows its callers, its callees, and the time propagated along each edge. The manual also describes refinements added over the years, including line-by-line profiling and an annotated source listing, plus a treatment of the statistical sampling error inherent in the PC-sampling component.

gprof first shipped as part of Berkeley Unix and became a standard fixture of the 4.2BSD development environment that came out of the Computer Systems Research Group, alongside the C compiler toolchain it instrumented. Today the program lives on in GNU Binutils, maintained at sourceware.org, where it remains compatible with output from gcc-compiled programs. More than four decades after the paper, gprof’s vocabulary of flat profiles and call graphs is still the baseline mental model for reading any profiler, and the tool itself is still installed by default on countless Unix and Linux systems.