perf 使用

使用 perf stat 查看计数器:

~# perf stat -p 5211 -- sleep 60

使用 perf record 指定进程进行 cpu 采样:

~# perf record -e cpu-clock -F 1000 -g --call-graph dwarf -p 2290094 -- sleep 60

可以在远程机器上对采集的数据进行打包,并在本地机器使用 hotspot 等工具进行分析:

~# perf archive
~# tar xvf perf.data.tar.bz2 -C ~/.debug
~$ ./hotspot --kallsyms ~/.debug/\[kernel.kallsyms\]/27355047315282aabb5aaa822a6436abd9c48640 --appPath ~/.debug/

perf record 默认会在当前目录下生成 perf.data 的数据文件,可以使用 perf report 加载并显示相关的信息:

~# perf report -g graph

也可以使用如下的脚本转成火焰图:

~# perf script | ./stackcollapse-perf.pl | ./flamegraph.pl > perf-kernel.svg

查看二进制文件提供的函数符号:

~$ perf probe -x /usr/bin/ceph-osd --funcs

使用类似 top 的方式查看系统当前的利用率信息:

~# perf top -a -g --call-graph dwarf

查看指定进程 cpu 使用率:

~# perf top -e cpu-clock --call-graph dwarf -n -p 2187323 -F 999 -K

查看指定线程 cpu 使用率:

~# perf top -e cpu-clock --call-graph dwarf -n -t 1790440 -F 999 -K

perf stat, perf record, perf top 都可以通过 -p 选项指定进程 id,-t 选项指定线程 id,-C 指定 cpu 核,-a 指定所有 cpu 核(但是显然 -p/-t-a/-C 不能混用)。

TIPS

CentOS 所有安装包可执行程序符号表(只有当前最新版本)

http://debuginfo.centos.org

CentOS 所有安装包源代码(包含所有过往版本)

http://vault.centos.org/

参考资料

perf wiki

https://perf.wiki.kernel.org/index.php/Main_Page

PERF tutorial: Finding execution hot spots

http://sandsoftwaresound.net/perf/perf-tutorial-hot-spots/

The Linux perf GUI for performance analysis

https://github.com/KDAB/hotspot

Converts profiling output to a dot graph

https://github.com/jrfonseca/gprof2dot

使用 Perf 优化程序性能

https://docs.deepin.io/?p=735

Linux Perf Tools Tips

http://oliveryang.net/2016/07/linux-perf-tools-tips/

Examples and hands-on labs for Linux tracing tools workshops

https://github.com/goldshtn/linux-tracing-workshop

Perf for User Space Program Analysis

https://events.static.linuxfound.org/sites/events/files/lcjp13_takata.pdf

Dynamic Probes for Linux

https://events.static.linuxfound.org/sites/events/files/slides/LinuxConJapan2015-DynamicProbes.pdf#page=22

Interpreting of perf report

https://stackoverflow.com/questions/48957480/interpreting-of-perf-report

perf Examples

http://www.brendangregg.com/perf.html#FlameGraphs

Finding MySQL Scaling Problems Using perf

https://www.percona.com/community-blog/2020/02/05/finding-mysql-scaling-problems-using-perf/


最后修改于 2019-01-22