Flame Graph: How to Read One and How to Generate One

Raghav Chamadiya10 min read
On this page

A profiler hands you thousands of stack samples, and nobody reads those as text.

A flame graph is a picture of a sampling profile. Each box is a function, boxes are stacked to show who called whom, and the width of a box is the share of samples in which that function was on the stack. The widest boxes are where the program spends its time. Brendan Gregg invented the format, and it is the fastest way to answer "where is the CPU going?" in a program you did not write.

What does a flame graph show?

A flame graph shows where a program spent its time, summed over many stack samples. The profiler interrupts the program at a fixed rate (often around 100 times a second), records the call stack each time, and the tool merges identical stacks into one tree.

Three rules cover the whole format:

  • X axis: share of samples. A box that is half as wide as the graph was on the stack in half the samples. Boxes are sorted alphabetically within each parent, which is why the x axis is not time.
  • Y axis: stack depth. The bottom is the entry point (main, a thread start). Each box sits on its caller and holds its callees above it.
  • Width is the signal. Color is usually random or by library, and carries no meaning unless the tool documents one.

Here is a small one in text. Read it bottom to top.

text
                  [ hash_row 30 ][ sort 10 ]
      [ parse_line 30 ][ render 10 ] [ load 20 ][ query 10 ]
   [ read_file 30 ]   [ build_report 40 ]  [ handle_request 30 ]
[ main ........................................................ 100 ]

The numbers are sample counts out of 100. main is on every stack. build_report was on 40 stacks, and beneath it render and load account for 30 of those. The topmost boxes, hash_row and sort, are where the program was executing when it was sampled.

How to read a flame graph, step by step

Start at the top edge and work down. Time is spent in the frames with nothing above them, and the frames below only explain how the program got there.

  1. Find the widest box with a flat top. A wide box with narrow children is a function burning its own time. Here parse_line is 30 wide and has nothing above it.
  2. Check what is above wide boxes. build_report is 40 wide, but 30 of that is render and load stacked on it. Its own time is the 10 left over.
  3. Follow the plateau down to its caller. The caller tells you why the function runs this often. A hot hash_row called from a loop in build_report is a different fix than one called once.
  4. Compare siblings by width, not position. read_file sits left of handle_request only because of the alphabet.
  5. Search for a name. Most viewers highlight every box matching a regex and report the total share. That is how you answer "how much of the run is in the JSON library?" across many stacks.
  6. Zoom to a subtree. Click a box and its width becomes 100%. Do that once you know which part of the program matters.

A plateau is a wide box at the top of a stack. A tower is a narrow, deep stack, usually recursion or deep framework layers, and is rarely the problem on its own. Plateaus are what you fix.

Flame graph vs icicle graph vs flame chart

These three get confused, and the confusion changes how you read the picture.

Flame graphIcicle graphFlame chart
OrientationRoot at the bottom, grows upSame data, root at the top, grows downRoot at the top in most tools
X axisShare of samples, sorted alphabeticallySame as a flame graphTime, left to right
Merges identical stacksYesYesNo, one box per call over time
AnswersWhere does the time go overall?Same question, different habitWhat happened when, in this one run?
Typical homeperf, FlameGraph, py-spy, GrafanaSome profile viewersChrome DevTools Performance tab

Scroll the table sideways to see every column.

An icicle graph is a flame graph turned upside down. Same data, same reading rule, different default in some tools. A flame chart keeps time on the x axis, so a function that ran twice shows as two boxes. Use a flame graph to find the cost and a flame chart to see the sequence, such as a long task blocking the UI.

CPU, off-CPU and memory flame graphs

The graph is only as meaningful as the value that was sampled. The same drawing works for several.

  • CPU flame graph. Samples taken while a thread runs on a core. It answers "what is burning CPU?" and says nothing about a thread that is waiting.
  • Off-CPU flame graph. Samples of where threads were blocked: locks, disk, network, sleep. If a request is slow but CPU is idle, this is the one to draw. Linux perf and eBPF tools can record it.
  • Wall-clock flame graph. Samples every thread whether running or waiting. py-spy with --idle and pyinstrument work this way, so waiting shows up as width.
  • Memory (allocation) flame graph. Width is bytes or allocations, by the stack that allocated them. Go's heap profile and async-profiler's allocation mode produce these.

Know which one you have before you read it. A wall-clock graph of a web server will show a wide box for the event loop's idle wait, and that is not a bug.

How to generate a flame graph

The method is the same everywhere: sample the program under a realistic workload, then render. Here is the shortest working command per language. Run it against a workload that does real work, because a profile of a program that printed its usage measures nothing.

LanguageToolCommand
Any native, Linuxperf + FlameGraphsee below
Gopprofgo tool pprof -http=:8080 cpu.pprof
Pythonpy-spypy-spy record -o profile.svg -- python app.py
Node.js--cpu-profnode --cpu-prof app.js
Javaasync-profilerasprof -d 30 -f flame.html <pid>
Rustcargo flamegraphcargo flamegraph --bin <name>

Scroll the table sideways to see every column.

Linux: perf and the FlameGraph scripts

This is the original recipe, and it also answers "how can I convert perf data to a flame graph?". Clone Brendan Gregg's FlameGraph repository, record with perf, fold the stacks, and render.

bash
git clone https://github.com/brendangregg/FlameGraph
perf record -F 99 -g -- ./your-program
perf script | ./FlameGraph/stackcollapse-perf.pl | ./FlameGraph/flamegraph.pl > flame.svg

-F 99 samples 99 times a second, and -g records call stacks. Use -p <pid> instead of a program to attach to a running process. Programs compiled without frame pointers leave gaps in the stacks: rebuild with -fno-omit-frame-pointer or record with --call-graph dwarf. Open flame.svg in a browser: it is interactive.

Go: pprof

Go ships the profiler. Write a CPU profile from a test or benchmark, then open it in the browser UI, which includes a flame graph view.

bash
go test -run='^$' -bench . -cpuprofile cpu.pprof ./...
go tool pprof -http=:8080 cpu.pprof

For a running service, import net/http/pprof and fetch a 30 second profile directly with go tool pprof -http=:8080 "http://localhost:6060/debug/pprof/profile?seconds=30". The Go team documents the endpoints in the net/http/pprof package docs. This same endpoint is what continuous profiling tools poll on a schedule, so a flame graph from last Tuesday is available without anyone attaching a profiler.

Python: py-spy

py-spy samples a Python process from outside, so you do not change the code.

bash
py-spy record -o profile.svg -- python app.py
py-spy record -o profile.svg --pid 12345

The first form launches the program, the second attaches to a running one (this may need elevated privileges on Linux). For a longer treatment of Python profilers and what each one costs, see Python profiling.

Node.js: --cpu-prof

Node writes a V8 profile on exit.

bash
node --cpu-prof app.js

That leaves a .cpuprofile file in the working directory. Load it in the Chrome DevTools Performance panel or a viewer such as 0x, which runs your program with npx 0x app.js and, when you stop it with Ctrl+C, writes an HTML flame graph into a <pid>.0x folder.

Java: async-profiler

async-profiler avoids the safepoint bias of many JVM profilers. A file name ending in .html selects the flame graph output.

bash
asprof -d 30 -f flame.html <pid>

That samples CPU for 30 seconds at the default 100 Hz. The project recommends starting the JVM with -XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints for better accuracy.

Rust: cargo flamegraph

bash
cargo install flamegraph
cargo flamegraph --bin your-bin

Add debug = true under [profile.release] in Cargo.toml so frames have names. On Linux it drives perf underneath, so the perf permissions above apply. Pass program arguments after --. The flamegraph-rs README lists the platform notes.

Common ways to misread a flame graph

Most wrong conclusions come from five habits.

  • Reading left to right as time. It is alphabetical. If you need time, you want a flame chart.
  • Optimizing the widest low box. main, a request handler and a thread pool entry are on almost every stack. They cost nothing themselves. Look at what has nothing above it.
  • Trusting a short profile. A few seconds of samples gives noisy widths for small boxes. Profile long enough that the box you care about has hundreds of samples.
  • Mixing up CPU and wall time. A wide box in a wall-clock profile may be waiting on a socket, not computing.
  • Profiling the wrong workload. A unit test, a benchmark and production traffic exercise different paths. A box that is wide in a test may not exist in production.

Missing symbols look like a different problem: a graph full of hex addresses or [unknown] means the binary has no debug info or frame pointers, not that your code is mysterious.

What a flame graph does not tell you

A flame graph names the function that is hot. It does not tell you who owns that function, what else in the codebase calls it, or whether its shape is a known anti-pattern such as a query inside a loop. You get a name and a width, then you go searching.

That is the gap repowise fills. repowise evidence add reads a profile exactly as the profiler wrote it (pprof, Speedscope, a V8 .cpuprofile, or collapsed stacks) and joins every entry to a function in the index, with the commit and workload it was measured on. repowise never runs your code to get evidence. Your profiler does, and evidence add ingests the file.

bash
repowise evidence setup --format json
repowise evidence add .repowise/app.speedscope.json --workload "app" --class entry_point --commit $(git rev-parse HEAD)
repowise evidence status

evidence setup prints what to run per build unit, evidence add ingests the result, and evidence status shows what is stored and how fresh it is. For Go, the ingest line takes the pprof file go test -cpuprofile wrote. For Node, it takes the .cpuprofile files. py-spy output goes in as Speedscope or collapsed stacks, with --value-kind naming the clock.

Once stored, the hot frames sit next to the call graph and the static performance findings. A function that is wide in the profile and flagged for a database call per loop iteration is a confirmed N+1 query problem, not a guess. A function that is wide and also a code hotspot by churn is where a refactor pays back twice. Agents read the same data through the get_health tool. For how this fits with the rest of code health, see the code quality guide.

FAQ

What does a flame graph do?
A flame graph shows which functions a program spends its time in. It merges thousands of sampled call stacks into one picture where the width of each box is the share of samples that function was on the stack, so the widest boxes at the top of stacks are the hot spots.
How can I convert perf data to a flame graph?
Run perf script on the recorded data, pipe it through stackcollapse-perf.pl to fold the stacks, then through flamegraph.pl to produce an SVG. Both scripts are in Brendan Gregg's FlameGraph repository on GitHub.
How do I generate a flame graph?
Sample the program under a realistic workload with a profiler, then render the samples. Use perf plus the FlameGraph scripts on Linux, go tool pprof -http for Go, py-spy record -o profile.svg for Python, node --cpu-prof for Node.js, async-profiler for Java and cargo flamegraph for Rust.
What is the difference between a flame graph and an icicle graph?
They draw the same merged stacks. A flame graph puts the root at the bottom and grows up, an icicle graph puts the root at the top and grows down. Both use width for sample share and neither has time on the x axis.
What is the difference between a flame graph and a flame chart?
A flame graph merges identical stacks and sorts them alphabetically, so it shows total cost. A flame chart keeps time on the x axis and shows each call when it happened, so it shows sequence. Chrome DevTools draws flame charts.
Is the x axis of a flame graph time?
No. The x axis is the share of samples, and sibling boxes are sorted alphabetically. Only a flame chart puts time on the x axis.