Skip to content

Lesson 4 of 13 · 18m

perf record and Call Graphs

`perf record -F 99` samples stacks, but only a deliberate `--call-graph fp|dwarf|lbr` choice gets you the callers.

perf record does not instrument your binary. It takes samples: at a chosen frequency the kernel interrupts the target, captures the instruction pointer and whatever call chain it can see, and appends the record to perf.data. -F 99 is the convention — not because 99 is magic, but because sampling at 100 or 1000 Hz lands in lockstep with kernel timer frequencies and aliases your data against periodic work in the kernel and in your own process. Statistical profilers are honest about what they are: every entry is "this stack was executing when sampled," nothing more.

The call-graph problem

A flat profile — leaf functions only — tells you memcpy and cmp are hot and nothing about why. The caller chain is the expensive part, and perf offers three ways to get it:

  • -g / --call-graph fp: frame pointers. Cheap, reliable — if the binary was compiled with them. GCC's -fomit-frame-pointer has been the default at -O1 and above for decades, so most distro binaries lacked them for years. Recent Fedora and Ubuntu re-enabled frame pointers by default because profiling matters; verify your own binaries rather than assuming.
  • --call-graph dwarf: perf dumps the user stack per sample (8192 bytes by default, up to 65528) and unwinds it offline against DWARF .debug_frame info. Works on binaries built -fomit-frame-pointer, costs more CPU and disk per sample, and needs valid unwind info — hand-written assembly and -ffreestanding code won't unwind.
  • --call-graph lbr: Intel's Last Branch Record hardware keeps recent branch targets, and perf reconstructs a partial stack from them. Near-zero overhead per sample, but depth is the LBR's depth — 8 to 32 entries depending on the CPU — so deep chains get truncated.

Pick the tradeoff on purpose: fp when you control the build, dwarf when you don't, lbr when overhead is the constraint and stacks are shallow.

Running it

perf record -F 99 -g -p 31337 -- sleep 30
perf report --stdio

Use -a for the whole box instead of -p. Symbols need to exist: a stripped binary produces a histogram of hex addresses; distro -dbgsym/-dbg packages or debuginfod fill them in. If perf refuses with a permissions error, that is kernel.perf_event_paranoid — hardened distros now ship it as high as 4 — and CAP_PERFMON (kernel 5.8+) grants profiling without full CAP_SYS_ADMIN.

Reading the report

perf report columns earn a skim: Overhead is the sample share, Command the process, Shared Object the library or binary owning the symbol — a hot [kernel] or libc row means the work is inside the library, not beside it. perf top -p <pid> gives the same view live. For instruction-level blame, perf annotate <symbol> interleaves source and assembly weighted by samples, and on DWARF-graph captures perf report --children folds callees into callers so wide frames surface instead of hiding under their children. When stacks still truncate, -m 256 enlarges the per-CPU mmap buffer — dwarf-mode drops show up as [unknown] chains when it fills.

What the samples can't tell you

Sampling misses short-lived work. A function that runs 200 microseconds appears in roughly 2% of samples at 99 Hz — invisible unless it runs often. Tail latency lives below the sampling floor. Answer tail questions with off-CPU tools or tracing, not with more Hz: raising frequency buys finer statistics at proportional overhead and never fixes the blind spot.

The practice

  • perf record -F 99 -g with an explicit call-graph choice; never trust the default.
  • Build with -fno-omit-frame-pointer on code you ship — a few percent of CPU for permanent diagnosability.
  • Ship or serve debug symbols; a profile of hex addresses is a profile of nothing.
  • Treat the profile as a map of samples. Confirm a hot path before rewriting it.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.