简介
perf是linux下一种常用的分析工具,主要可以测量CPU PMU、tracepoints、kprobes和uprobes等,
常见的两种使用模式分别是计数和采样,以下几个命令比较常用:
perf stat:计数模式,主要是计数events
perf top:实时采样,同步输出热点数据
perf record:采样热点数据,默认保存到perf.data供分析,可以通过-o参数更改文件名
perf report:解析perf.data数据,并输出热点,可以通过-i参数更改文件名
常见事件
perf list 可以查看所有events,主要分为以下几大类:
1. hw or hardware to list hardware events such as cache-misses, etc.
2. sw or software to list software events such as context switches, etc.
3. cache or hwcache to list hardware cache events such as L1-dcache-loads, etc.
4. tracepoint to list all tracepoint events, alternatively use subsys_glob:event_glob to filter by tracepoint subsystems such as sched, block, etc.
5. pmu to print the kernel supplied PMU events.
6. sdt to list all Statically Defined Tracepoint events.
可以通过指定events的类型来过滤,如 perf list hw
计数模式
针对命令进行计数,Ctrl C结束运行
perf stat <command>
输出更多计数的细节,最多可以-ddd
perf stat -d <command>
针对进程pid计数,只执行10秒
perf stat -p <PID> -- sleep 10
采样模式
针对进程采样,Ctrl C结束运行
perf record <command>
针对进程pid采样
perf record -p <PID>
增加函数堆栈的记录
perf record -p <PID> -g
只采样10秒
perf record -p <PID> -- sleep 10
解析输出perf.data的内容
perf report
针对云主机的用法
perf kvm
以上主要是一些基础用法,更多的细节需要查阅perf的manual