perf on-cpu火焰图

火焰图

火焰图是一种剖析软件运行状态的工具,它能够快速的将频繁执行的代码路径以图式的形式展现给用户。

  • CPU
  • Memory
  • Off-CPU
  • Hot/Cold
  • Differential

on-cpu火焰图可以用于分析cpu是被哪些线程、哪些函数占用的,可以方便的找到热点代码便于后续分析优化。

Flame Graph

1
2
3
4
5
6
7
8
git clone https://github.com/brendangregg/FlameGraph.git
Cloning into 'FlameGraph'...
remote: Enumerating objects: 1285, done.
remote: Counting objects: 100% (708/708), done.
remote: Compressing objects: 100% (147/147), done.
remote: Total 1285 (delta 584), reused 575 (delta 561), pack-reused 577
Receiving objects: 100% (1285/1285), 1.92 MiB | 1.99 MiB/s, done.
Resolving deltas: 100% (761/761), done.

进入此目录

程序

运行待测试程序,使用命令

1
ps -A

记录PID

采样

1
sudo perf record -F 99 -a -g -p 371529

Ctrl+C可以结束采样,在当前目录会自动生成perf.data数据文件。

解析

中间的脚本用于折叠函数

1
perf script | ./FlameGraph/stackcollapse-perf.pl | ./stackcollapse-perf.pl > perf.fold

生成图片

1
./flamegraph.pl perf.fold > perf.svg

火焰图


perf on-cpu火焰图
https://feater.top/linux/intro-of-perf-on-cpu-fire-graph/
作者
JackeyLea
发布于
2024年3月25日
许可协议