Profiling with perf and flame graphs

Advanced · 20 min

Find where CPU time goes with Linux perf: sample with perf top and perf record, fix missing symbols and stacks, and read a flame graph.

Before thisTracing system calls with straceShared libraries and building from sourceLinux capabilities

GOTO Conferences · 22 min · from 2:03

Profiling with perf and flame graphs

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by GOTO ConferencesReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A sampling profiler interrupts the CPUs many times a second and records what was running. Functions that appear in many samples are where the time goes, at low overhead.
  • `perf top` shows hot functions live. `perf record -F 99 -a -g -- sleep 30` samples every CPU with call stacks, and `perf report` explores the result.
  • Profiles need symbols and stacks. Install debug symbols, build with frame pointers (default since Fedora 38 and Ubuntu 24.04 for most packages), and give JIT runtimes a symbol map.
  • A flame graph draws all sampled stacks at once. Width is the share of samples, height is stack depth, the top edge is what was on the CPU, and the x axis is sorted alphabetically, not by time.
  • Unprivileged profiling is limited by `kernel.perf_event_paranoid`, default 2, which blocks kernel profiling for users without `CAP_PERFMON`.

CPU-bound and no idea why

The checklist in the first sixty seconds says a process is using 100% of a CPU. strace shows it is barely making system calls. The time is being spent inside the program’s own code, or in the kernel on its behalf, and the question is: in which functions?

Adding timing code everywhere is slow and changes what you measure. A sampling profiler takes another route. Many times a second it interrupts each CPU and writes down what was running, including the chain of calls that led there. After thirty seconds you have thousands of snapshots. Functions that appear in a large share of them are where the time goes. Think of a factory manager who walks the floor every ten minutes and notes what each worker is doing. After a week, the tally shows where the hours went without anyone filling in a timesheet.

perf

perf is the Linux kernel’s own profiler, maintained in the kernel source tree and packaged as linux-tools on Ubuntu or perf on Fedora and RHEL. The everyday commands:

sudo perf top                                  # live view of the hottest functions
sudo perf record -F 99 -a -g -- sleep 30       # sample all CPUs, with stacks, for 30 s
sudo perf report                               # browse perf.data
sudo perf record -F 99 -g -p 1234 -- sleep 30  # one process

-F 99 sets the sampling rate. Gregg’s flame graph page explains odd rates like 99 as a way to avoid sampling “in lockstep with other activity”. -a means all CPUs, and -g records call stacks, which you need for anything beyond a flat list.

Unprivileged use is governed by kernel.perf_event_paranoid. The kernel’s sysctl documentation gives the default as 2, which disallows kernel profiling by users without CAP_PERFMON, the capability from Linux capabilities. Some distributions set it stricter. Running as root, or granting CAP_PERFMON to a profiling account, gets around it.

Symbols and stacks

Two things go wrong in almost every first profile.

Missing symbols. perf shows raw addresses when it cannot map them to function names. For compiled software, install the distribution’s debug symbol packages (-dbgsym on Ubuntu, debuginfo on Fedora and RHEL). For JIT-compiled languages there is no symbol file on disk at all. Gregg’s page lists the fixes: Node.js can write a perf map with --perf_basic_prof, and Java needs an agent or a profiler like async-profiler. The idea behind shared libraries applies here too: perf has to know which file each address came from.

Broken stacks. To walk the call stack cheaply, perf follows the frame pointer register. For years compilers omitted it by default to gain a register, which left profiles with one-frame stacks. Gregg’s 2024 article “The Return of the Frame Pointers” records Fedora 38 and Ubuntu 24.04 turning frame pointers back on for most packages. For your own code, build with -fno-omit-frame-pointer. Otherwise perf record --call-graph dwarf can unwind from debug information, at a much higher cost in data volume.

Flame graphs

perf report gives a tree you can expand, which gets tiring quickly. A flame graph, invented by Gregg, shows every sampled stack in one picture. His page gives the pipeline:

sudo perf record -F 99 -a -g -- sleep 60
sudo perf script | ./stackcollapse-perf.pl > out.perf-folded
./flamegraph.pl out.perf-folded > perf.svg

The two scripts come from his FlameGraph repository. To read the SVG:

  • Each box is a function. The width is the share of samples in which it was on the CPU or was an ancestor of what was. Wide means expensive.
  • The y axis is stack depth. A box’s callers are below it. The top edge is what was actually running.
  • The x axis is not time. Frames are sorted alphabetically so identical stacks merge. Colours carry no meaning in the default palette.

Look for wide plateaus near the top, which is code burning CPU itself, and for wide towers whose top is a function you did not expect, such as a logging library or a regular expression engine. Modern tools such as Firefox Profiler and speedscope can load perf script output directly and draw similar views.

No profiling was run for this lesson. perf needs a Linux kernel and there is none here. Commands are from Gregg’s flame graph page and perf’s documentation, and the perf_event_paranoid default is from the kernel’s sysctl documentation, read on 2026-11-28.

About the video

The embed is a 22-minute clip, 2:03 to 24:22, of Brendan Gregg’s “Visualizing Performance - The Developers’ Guide to Flame Graphs” at YOW! 2022, published by GOTO Conferences in June 2023. The clip covers his flame graph chapters, the implementations, CPU profiling and the live demo. It stops before “Stacks & symbols” at 35:00, which is worth watching from the same video if your graphs look broken. The full talk is about 50 minutes. Gregg created flame graphs, which is why his explanation was preferred over shorter ones. A hands-on perf record and perf report walkthrough from Fastware is in deeper. I judged the talk from its chapters and description, and English from its English caption track.

Where this fits

perf samples where time goes. eBPF lets you attach small programs to almost any event in the kernel and count, time or trace it in place: eBPF and bpftrace.

Check yourself

  1. 01Why sample at 99 Hz rather than 100 Hz?

    To avoid sampling in lockstep with activity that runs on round-number timers, which would bias the profile towards whatever happens to run on those ticks.

  2. 02A flame graph shows a wide box `parse_json` near the top. What does that mean, and what does its position along the x axis mean?

    The CPU spent a large share of the sampled time in `parse_json` or functions it called. Its horizontal position means nothing, because frames are sorted alphabetically to merge identical stacks.

  3. 03`perf report` shows most time in hex addresses like `0x7f3a...` instead of function names. What is wrong?

    perf cannot resolve symbols, either because debug symbols are missing or because the code is JIT-compiled. Install the debuginfo or dbgsym packages, or make the runtime write a perf map.

  4. 04Stacks in your flame graph are only one or two frames deep and the callers are missing. Why?

    The binaries were built without frame pointers, so perf cannot walk the stack. Rebuild with `-fno-omit-frame-pointer`, or record with `--call-graph dwarf` at a higher cost.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark