← All writing

Reading a flame graph without fooling yourself

6 min readprofilinggo

Flame graphs are the fastest way I know to get a feel for where a service spends CPU. They are also very easy to misread. These are the questions I ask before I believe what I'm looking at.

1. What was the process doing while I sampled?

A 30-second profile of an idle service shows you the runtime's background work, not your hot path. I either profile under a realistic load test or grab the profile from production during a busy window:

go tool pprof -http=:8080 \
  http://localhost:6060/debug/pprof/profile?seconds=30

If the total sample count is tiny, stop there and collect a better profile.

2. Width is share of samples, not latency

A wide frame means the function was on the stack in a large fraction of CPU samples. It says nothing about wall-clock time spent waiting on the network or a lock. For that you want the block and mutex profiles, or a trace.

3. Look for plateaus, not towers

Tall, narrow towers are usually deep but cheap call chains. The interesting part is a wide flat top — a leaf function that burns CPU itself. Common culprits I keep finding:

  • runtime.mallocgc — allocation pressure; check the alloc profile next.
  • encoding/json reflection on a hot request path.
  • regexp compiled inside a loop instead of once at package level.

4. Compare, don't stare

One profile tells you where time goes. Two profiles tell you what changed. pprof -diff_base old.pb.gz new.pb.gz is the single most useful flag when a deploy got slower and nobody knows why.

If the fix doesn't move the number you measured, it wasn't the fix.

That's the whole checklist. It's boring, and it keeps me from spending an afternoon optimizing a function that was 2% of the problem.