目录
一、概述
1、perf 作用
perf 是一个性能分析工具(基于 Linux 内核提供的性能事件 perf_event
口),用于对 Linux 系统进行性能调优和性能分析。它可以通过收集硬件性能计数器、跟踪系统事件和采样程序调用栈等方式来提供详细的性能统计信息。
perf 依赖事件进行统计,这里的事件是通过采样机制,并不是 clock 级别的统计;根据使用 perf 工具的不同按测量事件的类型进行统计。
2、常用的工具集
除了 perf 命令本身,还有一些常用的工具集可以与 perf 搭配使用来进行更深入的性能分析和调优。以下是一些常用的 perf 工具集:
perf stat
:用于收集和显示性能计数器统计信息,可以通过perf stat
命令来监测进程或命令的整体性能指标,如指令数、缓存命中率、分支预测错误等。perf record
:用于采集程序执行期间的硬件性能计数器数据、事件和调用栈信息,并将其保存到数据文件中。可以使用perf record
命令来启动采样,并通过perf report
命令来分析采样数据。perf report
:用于分析通过perf record
收集的性能采样数据,并生成性能分析报告。可以使用perf report
命令来查看调用栈信息、函数耗时和性能热点等。perf top
:用于实时监测进程的性能指标,并显示当前的性能热点。可以使用perf top
命令来查看 CPU 使用情况、函数执行次数和事件计数等。perf annotate
:用于以源代码的方式显示采样数据和调用栈信息,并标注每个源代码行的性能指标。可以使用perf annotate
命令来查看性能热点和优化建议。perf diff
:用于比较和分析两个不同版本的程序的性能差异。可以使用perf diff
命令来对比两个 perf 数据文件,并生成性能差异报告。perf probe
:用于动态添加和移除性能探针,以收集特定代码路径的性能数据。可以使用perf probe
命令来添加探针,并通过perf record
和perf report
命令来收集和分析探针数据。
还有一些针对性性能检查工具:如针对锁的 lock;针对调度的 sched;针对 slab 分配器性能 kmem;自定义检查点 probe 等。可以通过命令:perf
或 perf -h
来查看:
[projectsauron]:~/$ perf -h ## 或 perf
usage: perf [--version] [--help] [OPTIONS] COMMAND [ARGS]
The most commonly used perf commands are:
annotate Read perf.data (created by perf record) and display annotated code
archive Create archive with object files with build-ids found in perf.data file
bench General framework for benchmark suites
buildid-cache Manage build-id cache.
buildid-list List the buildids in a perf.data file
c2c Shared Data C2C/HITM Analyzer.
config Get and set variables in a configuration file.
daemon Run record sessions on background
data Data file related processing
diff Read perf.data files and display the differential profile
evlist List the event names in a perf.data file
ftrace simple wrapper for kernel's ftrace functionality
inject Filter to augment the events stream with additional information
iostat Show I/O performance metrics
kallsyms Searches running kernel for symbols
kmem Tool to trace/measure kernel memory properties
kvm Tool to trace/measure kvm guest os
list List all symbolic event types
lock Analyze lock events
mem Profile memory accesses
record Run a command and record its profile into perf.data
report Read perf.data (created by perf record) and display the profile
sched Tool to trace/measure scheduler properties (latencies)
script Read perf.data (created by perf record) and display trace output
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