Skip to content

Using perf for Linux Performance Analysis, Part 2

Follow-up perf workflows for interpreting samples and investigating bottlenecks.

This English edition keeps the original illustrations and reproducible command or configuration examples.

Reference diagram 1

Reference diagram 2

Reference diagram 3

Reference diagram 4

Commands and configuration

sudo apt-get install linux-tools-common linux-tools-generic linux-tools-$(uname -r)
CONFIG_PERF_EVENTS=y
perf <command> [options] [arguments]
#
perf stat
perf list
# 硬件事件
perf list hw
# 软件事件
perf list sw
perf probe [<options>] <probe-point> [<probe-point> ...]

# 例子:添加一个探针,在do_sys_open入口插入一个
perf probe do_sys_open
# 带参数的探针
perf probe 'do_sys_open filename'

# 查看已经添加的探针
perf probe -l

# 删除探针
perf probe -d do_sys_open

# 使用探针进行性能分析
# 例子:在系统运行 10 秒钟内记录 do_sys_open 函数的调用,并生成一份性能报告
perf record -e probe:do_sys_open -aR sleep 10
perf report
Usage: perf stat [<options>] [<command>]

# 命令perf stat -p <pid>可以采集某一个进程的运行信息
perf stat -p 344

# 例子: zero文件读取输入写到空设备中,连续写888888个block:
perf stat -B dd if=/dev/zero of=/dev/null count=888888
perf stat 

# 例子:用1000统计频率,统计一个sleep 5秒过程中
perf record -a -F 1000 sleep 1
perf report
Usage: perf annotate [<options>]

# 注释汇编代码
perf annotate -i perf.data
perf top [<options>]


# 监控指定进程的性能事件
perf top -p 12345

# 监控指定用户的性能事件
perf top -u john

# 监控特定事件
perf top -e cycles

Practical notes

Test each change on a recoverable target, keep logs from the boot or failure path, and record the exact kernel and userspace versions used in the result.