perf:具有性能计数器的 Linux 分析
:::tips 本文翻译自 Perf Tutorial (opens new window) 采用机翻后人工修订,阅读请保持警惕 :::
# 1. 简介
Perf 是用于基于 Linux 2.6+ 的系统的探查器工具,可以抽象化 Linux 性能测量中的 CPU 硬件差异,并提供一个简单的命令行界面。 Perf 基于最新版本的 Linux 内核导出的 perf_events 接口。 本文通过示例运行演示了 perf 工具。 在具有内核 2.6.38-8 的通用结果的 Ubuntu 11.04 系统上在带有双核 Intel Core2 T7100 CPU 的 HP 6710b 上运行时获得了输出。 为了提高可读性,某些输出使用省略号([...])进行了缩写。
# 1.1 指令
性能工具提供了丰富的命令集,以收集和分析性能以及跟踪数据。
命令行的用法让人想起 git
,因为有一个通用工具 perf
,它实现了一组命令:stat
,record
,report
,[...]
支持的命令列表:
perf
usage: perf [--version] [--help] COMMAND [ARGS]
The most commonly used perf commands are:
annotate Read perf.data (created by perf record) and display annotated code
读取 perf.data (由 perf record 创建)并显示带注释的代码
archive Create archive with object files with build-ids found in perf.data file
使用在 perf.data 文件中找到的带有构建 ID 的目标文件创建档案
bench General framework for benchmark suites
基准套件的通用框架
buildid-cache Manage build-id cache.
管理内部编号缓存。
buildid-list List the buildids in a perf.data file
在 perf.data 文件中列出构建 ID
diff Read two perf.data files and display the differential profile
读取两个 perf.data 文件并显示差异
inject Filter to augment the events stream with additional information
过滤以增加事件流的附加信息
kmem Tool to trace/measure kernel memory(slab) properties
跟踪/测量内核内存(slab)属性的工具
kvm Tool to trace/measure kvm guest os
跟踪/测量 KVM 客户机系统的工具
list List all symbolic event types
列出所有符号事件类型
lock Analyze lock events
分析锁事件
probe Define new dynamic tracepoints
定义新的动态跟踪点
record Run a command and record its profile into perf.data
运行命令并将其概要记录到 perf.data 中
report Read perf.data (created by perf record) and display the profile
读取 perf.data(由 perf record 创建)并显示配置
sched Tool to trace/measure scheduler properties (latencies)
跟踪/测量调度程序属性(延迟)的工具
script Read perf.data (created by perf record) and display trace output
读取 perf.data(由 perf record 创建)并显示跟踪输出
stat Run a command and gather performance counter statistics
运行命令并收集性能计数器统计信息
test Runs sanity tests.
运行健全性测试。
timechart Tool to visualize total system behavior during a workload
可视化工作负载期间整个系统行为的工具
top System profiling tool.
系统分析工具。
See 'perf help COMMAND' for more information on a specific command.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
某些命令需要内核中的特殊支持,因此可能不可用。
要获取每个命令的选项列表,只需键入命令名称,然后输入 -h
即可:
perf stat -h
usage: perf stat [<options>] [<command>]
-e, --event <event> event selector. use 'perf list' to list available events
事件选择器。使用 'perf list' 列出可用事件
-i, --no-inherit child tasks do not inherit counters
子任务不继承计数器
-p, --pid <n> stat events on existing process id
现有进程 ID 上的统计事件
-t, --tid <n> stat events on existing thread id
现有线程 ID 上的 stat 事件
-a, --all-cpus system-wide collection from all CPUs
所有 CPU 的系统范围内的收集
-c, --scale scale/normalize counters
缩放/归一化计数器
-v, --verbose be more verbose (show counter open errors, etc)
更详细(显示计数器打开错误等)
-r, --repeat <n> repeat command and print average + stddev (max: 100)
重复命令并打印 平均值 + stddev(最大值:100)
-n, --null null run - dont start any counters
空转 - 不启动任何计数器
-B, --big-num print large numbers with thousands' separators
使用数千个分隔符打印大量数字
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 1.2 事件
perf
工具支持一系列可测量的事件。
该工具和底层内核接口可以测量来自不同来源的事件。
例如,某些事件是纯内核计数器,在这种情况下,它们称为软件事件。
示例包括:上下文切换,次要故障。
事件的另一个来源是处理器本身及其性能监视单元(PMU)。 它提供了一个事件列表,用于测量微体系结构事件,例如周期数,指令退出,L1 Cache 未命中等等。 这些事件称为 PMU 硬件事件或简称为硬件事件。 它们随处理器类型和型号的不同而不同。
perf_events 接口还提供了一小组常见的硬件事件标记。 在每个处理器上,这些事件将映射到 CPU 提供的实际事件(如果存在),否则将无法使用该事件。 令人困惑的是,这些也称为硬件事件和硬件缓存事件。
最后,还有跟踪点 (tracepoint events) 事件,这些事件是由内核 ftrace 基础结构实现的。这些仅在 2.6.3x 和更高版本的内核中可用。
要获取受支持事件的列表:
perf list
List of pre-defined events (to be used in -e):
cpu-cycles OR cycles [Hardware event]
instructions [Hardware event]
cache-references [Hardware event]
cache-misses [Hardware event]
branch-instructions OR branches [Hardware event]
branch-misses [Hardware event]
bus-cycles [Hardware event]
cpu-clock [Software event]
task-clock [Software event]
page-faults OR faults [Software event]
minor-faults [Software event]
major-faults [Software event]
context-switches OR cs [Software event]
cpu-migrations OR migrations [Software event]
alignment-faults [Software event]
emulation-faults [Software event]
L1-dcache-loads [Hardware cache event]
L1-dcache-load-misses [Hardware cache event]
L1-dcache-stores [Hardware cache event]
L1-dcache-store-misses [Hardware cache event]
L1-dcache-prefetches [Hardware cache event]
L1-dcache-prefetch-misses [Hardware cache event]
L1-icache-loads [Hardware cache event]
L1-icache-load-misses [Hardware cache event]
L1-icache-prefetches [Hardware cache event]
L1-icache-prefetch-misses [Hardware cache event]
LLC-loads [Hardware cache event]
LLC-load-misses [Hardware cache event]
LLC-stores [Hardware cache event]
LLC-store-misses [Hardware cache event]
LLC-prefetch-misses [Hardware cache event]
dTLB-loads [Hardware cache event]
dTLB-load-misses [Hardware cache event]
dTLB-stores [Hardware cache event]
dTLB-store-misses [Hardware cache event]
dTLB-prefetches [Hardware cache event]
dTLB-prefetch-misses [Hardware cache event]
iTLB-loads [Hardware cache event]
iTLB-load-misses [Hardware cache event]
branch-loads [Hardware cache event]
branch-load-misses [Hardware cache event]
rNNN (see 'perf list --help' on how to encode it) [Raw hardware event descriptor]
mem:<addr>[:access] [Hardware breakpoint]
kvmmmu:kvm_mmu_pagetable_walk [Tracepoint event]
[...]
sched:sched_stat_runtime [Tracepoint event]
sched:sched_pi_setprio [Tracepoint event]
syscalls:sys_enter_socket [Tracepoint event]
syscalls:sys_exit_socket [Tracepoint event]
[...]
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
一个事件可以包含子事件(或单元掩码)。 在某些处理器上,对于某些事件,可能会合并单位掩码并测量何时发生子事件。 最后,事件可以具有修饰符,即过滤器,其可以改变对事件进行计数的时间或方式。
# 硬件事件
PMU 硬件事件是特定于 CPU 的,并由 CPU 供应商记录。
如果将性能工具与 libpfm4 库链接,则可以对事件进行一些简短描述。有关 Intel 和 AMD 处理器的 PMU 硬件事件的列表,请参阅
- Intel PMU event tables: Appendix A of manual here (opens new window)
- AMD PMU event table: section 3.14 of manual here (opens new window)