perf工具简单入门
perf (Performance analysis tools for Linux)是Linux的一款性能分析工具,能够进行函数级和指令级的热点查找,可以用来分析程序中热点函数的CPU占用率,从而定位性能瓶颈。
原理
Linux性能计数器是一个基于内核的子系统,它提供一个性能分析框架,比如硬件(CPU、PMU(Performance Monitoring Unit))功能和软件(软件计数器、tracepoint)功能。通过perf,应用程序可以利用PMU、tracepoint和内核中的计数器来进行性能统计。
通过perf,应用程序可以利用PMU、tracepoint和内核中的计数器来进行性能统计。它不但可以分析制定应用程序的性能问题(per thread),也可以用来分析内核的性能问题,当然也可以同时分析应用程序和内核,从而全面理解应用程序中的性能瓶颈。
使用perf,可以分析程序运行期间发生的硬件事件,比如instructions retired、processor clock cycles等;也可以分析软件时间,比如page fault和进程切换。
编译
如果需要手动编译perf,需要下载linux 内核源码编译,perf工具的源码路径在tools/perf/下。
$ git clone https://github.com/torvalds/linux --depth=1
$ cd linux/tools/perf/
$ make
使用
$ perf --help
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
kvm Tool to trace/measure kvm guest os
list List all symbolic event types
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
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.
top System profiling tool.
version display the version of perf binary
probe Define new dynamic tracepoints
trace strace inspired tool
kmem Tool to trace/measure kernel memory properties
kwork Tool to trace/measure kernel work properties (latencies)
lock Analyze lock events
sched Tool to trace/measure scheduler properties (latencies)
timechart Tool to visualize total system behavior during a workload
See 'perf help COMMAND' for more information on a specific command.
perf使用perf <子命令>这种模式, 上面列出的都是perf支持的子命令,如果不知道这些子命令需要哪些参数,一般可以通过perf <子命令> help命令查看。
监控整个系统的性能
# perf top --call-graph fractal

单个程序的性能分析
# perf stat ./workspace/hello

对程序进行更细粒度的分析
#生成perf.data记录文件
# perf record -a -g ./workspace/hello
#解析记录
# perf report --call-graph none
# perf report -i perf.data

对进程进行分析
#记录进程pid为217836的性能数据
# perf record -p 217836
#解析数据
# perf report -i perf.data
生成svg图片
# perf timechart record ./hello
#将本目录下的.data文件生成 svg
# perf timechart
火焰图
# perf record -g -F 99 ./hello
# perf script -i perf.data > out.perf
# git clone https://github.com/brendangregg/FlameGraph.git
# ./FlameGraph/stackcollapse-perf.pl out.perf > out.floded
# ./FlameGraph/flamegraph.pl out.floded > hello.svg
生成的svg图片就是火焰图。
参考
https://github.com/torvalds/linux/tree/master/tools/perf
perf (Linux)
perf: Linux profiling with performance counters
Linux Performance
perf Examples
Flame Graphs
在Linux下做性能分析
系统级性能分析工具perf的介绍与使用

浙公网安备 33010602011771号