Flamegraphs

mohitmishra786/low-level-dev-skills/skills/profilers/flamegraphs

作者 mohitmishra786bdc58472fa9f無授權條款253 個星標收錄於 2026年10月9日更新於 2026年10月9日儲存庫3 個月前更新

Flamegraph generation and interpretation skill. Use when converting perf, Valgrind Callgrind, or other profiler output into SVG flamegraphs using Brendan Gregg's FlameGraph tools, or when reading flamegraphs to identify performance bottlenecks. Activates on queries about flamegraphs, stackcollapse, flamegraph.svg, identifying hot frames, wide vs tall frames, or performance visualisation.

AI 產生的概覽

指導如何從效能分析工具輸出產生 SVG 火焰圖,並解讀火焰圖以找出效能瓶頸。

功能
此技能引導代理使用 Brendan Gregg 的 FlameGraph 工具,完成從效能分析資料到 SVG 火焰圖的流程,涵蓋 perf、Valgrind Callgrind、Go pprof、DTrace 與 async-profiler 等輸入。它也說明如何閱讀火焰圖,包括寬框架與高框架的意義、前後對比的差異火焰圖,以及 flamegraph.pl 的各種選項。它產出的是指令步驟與解讀指引,本身不產生檔案。
適用情境
適用於將 perf、Callgrind 或其他效能分析工具輸出轉換為 SVG 火焰圖,或閱讀既有火焰圖以找出熱點框架。也適合用差異火焰圖比較最佳化前後的效能剖析結果。
執行需求
需要 FlameGraph 工具指令碼(stackcollapse-perf.pl、flamegraph.pl、difffolded.pl 等),以及對應的效能分析工具,例如 perf、Valgrind Callgrind、Go pprof、DTrace 或 async-profiler。此技能本身不附帶指令碼,也不需要憑證,但複製 FlameGraph 儲存庫需要網路存取。

Flamegraphs

Purpose

Guide agents through the pipeline from profiler data to SVG flamegraph, and teach interpretation of flamegraphs to drive concrete optimisation decisions.

Triggers

  • "How do I generate a flamegraph from perf data?"
  • "How do I read a flamegraph?"
  • "The flamegraph shows a wide frame — what does that mean?"
  • "How do I generate a flamegraph from Callgrind?"
  • "I want to compare two flamegraphs (before/after)"

Workflow

1. Install FlameGraph tools

bash
git clone https://github.com/brendangregg/FlameGraph# No install needed; scripts are in the repoexport PATH=$PATH:/path/to/FlameGraph

2. perf → flamegraph (most common path)

bash
# Step 1: recordperf record -F 999 -g -o perf.data ./prog
# Step 2: generate script outputperf script -i perf.data > out.perf
# Step 3: collapse stacksstackcollapse-perf.pl out.perf > out.folded
# Step 4: generate SVGflamegraph.pl out.folded > flamegraph.svg
# Step 5: viewxdg-open flamegraph.svg     # Linuxopen flamegraph.svg          # macOS

One-liner:

bash
perf record -F 999 -g ./prog && perf script | stackcollapse-perf.pl | flamegraph.pl > fg.svg

3. Differential flamegraph (before/after)

bash
# Collect two profilesperf record -g -o before.data ./prog_oldperf record -g -o after.data ./prog_new
# Collapseperf script -i before.data | stackcollapse-perf.pl > before.foldedperf script -i after.data  | stackcollapse-perf.pl > after.folded
# Diff (red = regressed, blue = improved)difffolded.pl before.folded after.folded | flamegraph.pl > diff.svg

4. Callgrind → flamegraph

bash
valgrind --tool=callgrind --callgrind-out-file=cg.out ./progstackcollapse-callgrind.pl cg.out | flamegraph.pl > fg.svg

5. Other profiler inputs

bash
# Go pprofgo tool pprof -raw -output=prof.txt progstackcollapse-go.pl prof.txt | flamegraph.pl > fg.svg
# DTracedtrace -x ustackframes=100 -n 'profile-99 /execname=="prog"/ { @[ustack()] = count(); }' \  -o out.stacks sleep 10stackcollapse.pl out.stacks | flamegraph.pl > fg.svg
# Java (async-profiler)async-profiler -d 30 -f out.collapsed PIDflamegraph.pl out.collapsed > fg.svg

6. Reading flamegraphs

A flamegraph is a call-stack visualisation:

  • X axis: time on CPU (not time sequence) — wider = more time
  • Y axis: call stack depth — taller = deeper call chain
  • Color: random (no significance) — unless using differential mode

What to look for:

PatternMeaningAction
Wide frame near bottomFunction itself is hotOptimise that function
Wide frame with tall narrow towersCalling many different calleesHot dispatch; reduce call overhead
Very tall stack with wide baseDeep recursionCheck recursion depth; consider iterative approach
Plateau at the topLeaf function with no calleesThis leaf is the actual hotspot
Many narrow identical stacksMany threads doing the same workConsider parallelism or batching

Identifying the actionable hotspot:

  1. Find the widest top frame (a frame with no or narrow children above it)
  2. That is where CPU time is actually spent
  3. Trace down to understand what called it and why

Differential flamegraph:

  • Red frames: more time in new profile (regression)
  • Blue frames: less time in new profile (improvement)
  • Frames only in one profile appear solid colored

7. flamegraph.pl options

bash
flamegraph.pl --title "My App" \              --subtitle "Release build, workload X" \              --width 1600 \              --height 16 \              --minwidth 0.5 \              --colors java \              out.folded > fg.svg
OptionEffect
--titleSVG title
--widthWidth in pixels
--heightFrame height in pixels
--minwidthOmit frames < N% (reduces clutter)
--colorsPalette: hot (default), mem, io, java, js, perl, red, green, blue
--invertedIcicle chart (roots at top)
--reverseReverse stacks
--cpConsistent palette (same frame = same color across SVGs)

References

For tool installation, stackcollapse scripts, and palette options, see references/tools.md [blocked].

Related skills

  • Use skills/profilers/linux-perf to collect perf data
  • Use skills/profilers/valgrind to collect Callgrind data
  • Use skills/compilers/clang for LLVM PGO from sampling profiles

來源與署名

來源:mohitmishra786/low-level-dev-skills位於skills/profilers/flamegraphs提交bdc5847

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架