Course contents · Lesson 16 of 22

How to Read a Flame Graph: A Beginner's Guide

Profiles · Lesson 16 of 22 · 10 min Engineer

You ran a profiler on a slow service and it handed you a flame graph: a wall of orange and red boxes stacked like a city skyline. It looks busy, but it answers one question very well, which is where the CPU time went. Once you know what the width, the height and the left-to-right order mean, you can find the expensive function in under a minute.

Where the boxes come from

A flame graph is a picture of many sampled call stacks merged together, invented by performance engineer Brendan Gregg in 2011. As the profiling lesson showed, a sampling profiler copies the call stack about 100 times a second. Each function in a stack is a frame, and in a flame graph each frame becomes a box.

Merging is the key step. Stacks that start with the same functions share the same boxes, and each box gets wider by one unit for every sample it appears in. Three samples, two of them a → b → c and one a → d, become a box a three units wide, with b two wide and d one wide resting on it.

Three sampled stacks a-b-c, a-b-c and a-d merge into one flame graph where a is 3 samples wide, b and c are 2 wide and d is 1 wide, with the folded text a;b;c 2 and a;d 1
Merging throws away the order of the samples and keeps only how often each path appeared, which is exactly what width shows.

Tools store the merged data as folded stacks: one line per unique stack, frames separated by semicolons from the bottom up, then the sample count. These lines come from the real checkout profile in the last lesson, trimmed to start at main.main and stop four frames deep:

main.main;main.handleCheckout;main.applyDiscount 246
main.main;main.handleCheckout;main.parseJSON;encoding/json.Unmarshal 9
main.main;main.handleCheckout;main.parseJSON;encoding/json.Valid 596
main.main;main.handleCheckout;main.renderReceipt 5
main.main;main.handleCheckout;main.renderReceipt;strings.(*Builder).WriteString 40

Each sample is 10 ms of CPU, so 246 samples is 2.46 s spent in applyDiscount itself. On Linux, as root, you can build the same picture for any process with perf and Gregg's FlameGraph scripts. The -F 99 flag samples 99 times a second and -g records call stacks:

$ perf record -F 99 -g -p 4321 -- sleep 30
$ perf script | ./stackcollapse-perf.pl > out.folded
$ ./flamegraph.pl out.folded > flame.svg

Open flame.svg in a browser and you can hover and click to zoom. For a Go profile, go tool pprof -http=:8080 cpu.pprof opens a local web page with the same view under its View menu.

The axes: width, height and an x-axis that is not time

Annotated flame graph of the checkout program: root at the bottom, then runtime.main, main.main and main.handleCheckout at 97.5%, then applyDiscount 26.8%, parseJSON 65.8% and renderReceipt 4.9%, with JSON library frames above parseJSON and two hot plateaus marked
Two plateaus explain most of the CPU: the JSON scanner above parseJSON and the loop inside applyDiscount, even though root is the widest box.

Width is share of samples. A box's width is the fraction of all samples that contained that function anywhere in the stack. In a CPU flame graph that is the share of CPU time. In a memory flame graph built from a heap profile it is the share of bytes allocated or in use. parseJSON is 65.8% wide because it was on the stack in 65.8% of samples.

Height is stack depth. The bottom box is the entry point (here root, which stands for all samples), and every box sits on the function that called it. The boxes along the top edge are the functions that were actually running when the samples were taken. A tall tower only means a deep chain of calls, not a slow one.

Left to right is not time. Gregg's original tool sorts the boxes on each level alphabetically so identical frames merge into one; other tools sort by size. Either way the order means nothing. In the code, parseJSON runs before applyDiscount, yet it is drawn to the right of it.

Chrome DevTools adds a twist. Its Performance panel draws a flame chart, where the x-axis is time and stacks hang downward from the caller. Same boxes, different meaning: a flame chart shows what happened when, while a flame graph throws time away to show totals.

Self time versus total time

Total time is a box's full width: the function plus everything it called. Self time is only the part of the box with nothing on top of it, the time when that function itself was running. pprof calls these cum and flat.

That gives you two shapes to recognise. A wide box with little or nothing above it is a hot spot: applyDiscount has no children, so all of its 26.8% is self time. A wide box completely covered by children is just a caller: main.handleCheckout is 97.5% wide but does almost no work itself. Making a caller faster means changing what it calls, or how often.

One function can also appear in several towers when it is called from different places. A flame graph shows each call path separately, so a logging function might be a thin sliver in twenty towers. A bottom-up view, which merges every appearance of a function into one row, shows its real combined cost.

Reading one, step by step

Flowchart: open the right flame graph, scan the top edge, read the name and self share, walk down to the callers, then if there is one wide plateau fix that function, otherwise look for a shared caller or use a bottom-up view
Start at the top edge, where the work happens, and walk down only to learn who asked for it.

Apply the steps to the checkout graph. The widest plateau on the top edge is ConsumeSimpleString at 37.5% self, a function inside Go's JSON library. You cannot rewrite the library, so walk down: it sits on the JSON decoder, then json.Valid, then parseJSON. The decision is in your code: parseJSON validates the whole order payload on every request, and doing that less often removes most of the cost.

The second plateau, applyDiscount at 26.8%, is your own loop and can be fixed directly. After any change, profile again and compare widths, because a fix that looks obvious in the code can turn out to save nothing.

Colours, icicles and common shapes

Colours usually mean nothing. The original tool picks random warm colours so neighbouring boxes are easy to tell apart. Some tools colour by package or language, and a differential flame graph uses colour to show which functions grew or shrank between two profiles. Check the legend before you read meaning into red.

Icicle graphs are flame graphs upside down, with the root at the top and callees hanging below. Many tools, including Grafana's flame graph panel, draw them this way. The reading rules stay the same: width is share, depth is the call chain, and the hot spots are now along the bottom edge of each icicle.

Three shapes come up again and again:

  • One wide tower with a broad plateau: a single hot function. This is the easy case.
  • Many thin towers of similar width: death by a thousand cuts, with the cost spread across many functions. Look for a shared caller or a shared helper, such as serialisation or logging, in a bottom-up view.
  • Runtime frames such as Go's runtime.mallocgc (allocating) or gcBgMarkWorker (garbage collection) mean memory pressure, so open a heap profile next. Lock frames such as runtime.futex or pthread_mutex_lock show only the CPU spent on locks; time spent asleep waiting is off-CPU and needs a lock or off-CPU profile.

Check yourself

In a flame graph, box A is to the left of box B on the same level. Did A run first?

No: left-to-right order is alphabetical or by size, so it says nothing about when each function ran.

A box is 40% wide and its children cover 39% of that width. Where should you look?

At the children: the function has only 1% self time, so the cost is in what it calls.

A flame graph has dozens of thin towers and no wide plateau. What does that suggest?

The cost is spread across many functions, so look for a shared caller or helper in a bottom-up view rather than one hot spot.