perf 命令基本介绍perfPerformance Counters for Linux是 Linux 系统中用于性能分析的强大工具套件。它基于内核性能计数器PMC可以分析 CPU 使用率、内存访问、缓存命中率、分支预测等硬件级性能指标是系统性能优化和故障诊断的核心工具。资料合集https://pan.quark.cn/s/6fe3007c3e95、https://pan.quark.cn/s/561de99256a5、https://pan.quark.cn/s/985f55b13d94、https://pan.quark.cn/s/d0fb20abd19a语法perf [--version] [--help] [OPTIONS] COMMAND [ARGS]常用子命令性能分析命令perf stat执行命令并收集性能统计信息perf record记录性能事件数据perf report分析记录的数据并生成报告perf top实时显示系统性能热点perf probe动态添加探测点追踪命令perf trace追踪系统调用和函数调用perf sched分析调度器性能perf lock分析锁性能perf mem分析内存访问性能其他命令perf list列出可用的性能事件perf annotate对代码进行注解分析perf bench运行性能基准测试perf test运行内置测试常用选项通用选项-h, --help显示帮助信息-v, --version显示版本信息-e, --eventEVENT指定要监控的性能事件-p, --pidPID监控指定进程-t, --tidTID监控指定线程-C, --cpuCPU监控指定 CPUperf record 选项-o, --outputFILE输出到指定文件-g, --call-graph记录调用图-F, --freqHZ采样频率-c, --countNUM事件计数阈值perf stat 选项-r, --repeatN重复执行 N 次-n, --null不执行命令只显示统计-a, --all-cpus监控所有 CPU使用示例1. 查看可用的性能事件perf list perf list|grepcache perf list|grepcpu2. 执行命令并收集性能统计perfstatlsperfstat-r3./myprogram perfstat-ecycles,instructions,cache-misses ./myprogram输出示例Performance counter stats for ./myprogram: 123,456,789 cycles:u # 0.500 GHz 234,567,890 instructions:u # 1.90 insn per cycle 1,234,567 cache-misses:u # 0.50% of all cache refs 12,345,678 cache-references:u 0.247839456 seconds time elapsed 0.123456789 seconds user 0.012345678 seconds sys3. 记录性能数据perf record-g./myprogram perf record-F99-g-p1234perf record-ecpu-clock,page-faults-operf.data ./myprogram4. 分析记录的数据perf report perf report-iperf.data perf report --call-graphgraph5. 实时性能分析perftopperftop-p1234perftop-ecycles6. 追踪系统调用perf tracelsperf trace-p1234perf trace-eopen,close,read,write7. 分析内存访问性能perf mem record ./myprogram perf mem report8. 分析调度器性能perf sched record ./myprogram perf sched report perf sched latency9. 添加动态探测点perf probe-x/bin/ls main perf probe-x./myprogram my_function10. 代码注解分析perf annotate perf annotate-smy_function常用性能事件CPU 事件事件名称说明cyclesCPU 周期数instructions指令数branches分支指令数branch-misses分支预测失败数cpu-clockCPU 时钟缓存事件事件名称说明cache-references缓存引用次数cache-misses缓存未命中次数L1-dcache-loadsL1 数据缓存加载L1-dcache-load-missesL1 数据缓存未命中LLC-loads最后一级缓存加载LLC-load-misses最后一级缓存未命中内存事件事件名称说明page-faults缺页错误次数major-faults主缺页错误minor-faults次缺页错误软件事件事件名称说明context-switches上下文切换次数cpu-migrationsCPU 迁移次数page-faults缺页错误task-clock任务占用 CPU 时间实用技巧分析程序热点perf record-g-F99./myprogram perf report--sortdso,symbol定位性能瓶颈perfstat-ecycles,instructions,cache-misses,L1-dcache-load-misses ./myprogram分析多线程程序perf record-g-p$(pgrep-d,myprogram)perf report--threadsseparate生成火焰图# 需要安装 FlameGraph 工具perf record-F99-g./myprogram perf script|./stackcollapse-perf.plout.perf-folded ./flamegraph.pl out.perf-foldedflame.svg分析 Java 程序perf record-g-pjava_pidperf report --map-exec-name分析内核性能perf record-g-a-F99sleep10perf report相关命令perf-tools额外的 perf 工具集strace系统调用跟踪ltrace库函数调用跟踪valgrind内存调试工具gdbGNU 调试器top实时系统监控vmstat虚拟内存统计iostatI/O 统计注意事项权限要求大部分 perf 功能需要 root 权限内核配置需要内核支持性能计数器CONFIG_PERF_EVENTS采样频率过高的采样频率会影响系统性能输出文件perf.data 文件可能很大注意磁盘空间符号解析需要安装调试符号包才能正确解析符号Java 程序需要使用-XX:PreserveFramePointer编译选项安装方法Debian/Ubuntuapt-get install linux-tools-common linux-tools-$(uname -r)CentOS/RHELyum install perf总结perf是 Linux 系统中功能强大的性能分析工具套件。它可以帮助开发者和系统管理员深入分析程序的性能瓶颈包括 CPU 使用率、缓存命中率、内存访问模式等。通过perf的各种子命令可以进行实时性能监控、性能数据记录和分析、代码热点定位等操作是系统性能优化的必备工具。