Learning outcomes
- Generate and analyze CPU flamegraphs using perf
- Identify off-CPU blocking latency bottlenecks
Mental model
A Flamegraph is a visual representation of sampled stack traces. The X-axis spans the full sample population (wider boxes indicate functions consuming more CPU time), while the Y-axis shows call stack depth from top to bottom.
Theory
- On-CPU Profiling: Samples active CPU instruction pointers at regular intervals (e.g. 99 Hertz). Identifies CPU-bound algorithm bottlenecks.
- Off-CPU Profiling: Traces time spent by threads sleeping, blocked on disk I/O, waiting for locks, or waiting on network sockets.
perf(Linux Performance Counters): The standard kernel profiling utility for capturing hardware performance counters and stack traces.
# Capture CPU stack traces across all CPUs at 99Hz for 30 seconds
perf record -F 99 -a -g -- sleep 30
# Collapse stack traces and generate Flamegraph SVG
perf script | stackcollapse-perf.pl | flamegraph.pl > cpu_flamegraph.svg
# Profile Off-CPU blocking time using eBPF offcputime
offcputime-bpfcc -df 30 > offcpu.stacks
flamegraph.pl --color=io offcpu.stacks > offcpu_flamegraph.svg
Alternatives and trade-offs
- Instrumented Micro-benchmarks: High precision for isolated functions; distorts runtime behavior and cannot capture kernel locks.
perfFlamegraph Sampling: Low overhead (under 1% CPU impact); provides immediate visual identification of hot code paths across user and kernel space.
Failure modes and misconceptions
- Missing Frame Pointers (
-fomit-frame-pointer): Compiling C/C++ applications with-fomit-frame-pointerdestroys stack frame pointers, resulting in broken, truncated flamegraph stacks ([unknown]). Always compile with-fno-omit-frame-pointer. - Ignoring Off-CPU Latency: Focusing exclusively on CPU Flamegraphs fails to reveal why an application is slow when threads spend 95% of their time sleeping on database I/O or mutex contention. Use Off-CPU flamegraphs for I/O bottlenecks.
Decision scenario
Generate CPU and Off-CPU Flamegraphs using perf and eBPF tools to visually diagnose application performance bottlenecks in production.
Learning outcomes
- Sample kernel and application stack traces using
perf. - Interpret Flamegraph X-axis (sample width) and Y-axis (call stack depth).
- Generate Off-CPU Flamegraphs to isolate I/O blocking and lock contention.
Trade-offs
Flamegraph sampling provides clear visual identification of system bottlenecks, but requires compiling binaries with frame pointers (-fno-omit-frame-pointer).
Evidence assessment
Theory and decision mastery
Decision scenario
You are designing a high-concurrency production system requiring reliable execution of flamegraphs performance profiling under heavy traffic load.
Which architectural decision ensures maximum resilience, scalability, and system stability?
Primary sources
- Linux Kernel Kernel.org Official Architecture & Systems Documentation — Linux Kernel Organization, verified 2026-07-23