news 2026/9/6 3:43:18

perf 常用命令速查手册(建议收藏)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
perf 常用命令速查手册(建议收藏)

Linux 内核自带了一个性能分析工具叫 perf。它能做函数级和指令级的热点采样,也能配合 tracepoint 采集系统调用、网络事件、文件系统操作等内核事件。因为代码就在内核源码树里,算得上是 Linux 平台上最顺手的性能工具了。

原理

perf 基于内核的性能计数器子系统,硬件层面利用 CPU 的 PMU(Performance Monitoring Unit),软件层面依赖内核的 tracepoint 和软件计数器。

大致原理是:每隔一个固定时间,CPU 产生一个中断,记下当前跑的是哪个进程、哪个函数,累加对应的计数。多采几次就知道 CPU 时间主要花在了哪里。

整体架构分两层:

  • Perf Tools— 用户态工具集,收集和分析性能数据

  • Perf Event Subsystem— 内核事件子系统,和数据采集协同工作(Linux Hard Lockup Detector 也依赖它)

安装

sudoaptinstalllinux-tools-common linux-tools-generic linux-tools-`uname-r`

常用命令

perf --helpusage: perf[--version][--help][OPTIONS]COMMAND[ARGS]Themostcommonly used perf commands are: annotate Read perf.data and display annotated code archive Create archive with object files with build-ids bench General frameworkforbenchmark suites buildid-cache Manage build-id cache. buildid-list List the buildidsina perf.datafilec2c Shared Data C2C/HITM Analyzer. config Get andsetvariablesina configuration file. data Datafilerelated processingdiffRead perf.data files and display the differential profile evlist List the event namesina perf.datafileftrace simple wrapperforkernel 's ftrace functionality inject Filter to augment the events stream with additional information kallsyms Searches running kernelforsymbols 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 acommandand 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 and display trace outputstatRun acommandand gather performance counter statisticstestRuns sanity tests. timechart Tool to visualize total system behavior during a workloadtopSystem profiling tool. version display the version of perf binary probe Define new dynamic tracepoints tracestraceinspired tool

几个常用命令的简要说明:

|
命令
|
作用
|
| — | — |
|
annotate
|
解析 perf.data,显示带注释的代码
|
|
archive
|
按 build-id 打包被采样的 ELF,方便异地分析
|
|
bench
|
内置的调度器和内存管理 benchmark
|
|
diff
|
对比两个 perf.data 的热点差异
|
|
evlist
|
列出 perf.data 中记录的性能事件
|
|
kmem
|
追踪内核 slab 内存
|
|
kvm
|
追踪 KVM 客户机
|
|
list
|
列出当前系统支持的所有性能事件
|
|
lock
|
分析内核锁争用
|
|
mem
|
分析内存访问
|
|
record
|
采集采样数据并写入文件
|
|
report
|
读取 perf.data 显示热点分析结果
|
|
sched
|
分析调度器延迟
|
|
script
|
读取 perf.data 并输出 trace
|
|
stat
|
运行命令并收集性能概况(CPI、Cache miss 等)
|
|
timechart
|
可视化系统行为
|
|
top
|
实时分析(类似 top)
|
|
trace
|
跟踪系统调用
|
|
probe
|
定义动态探测点
|

常用例子

列出事件

perf list perf list'sched:*'

计数

perfstatcommand# 统计命令的 CPU 计数器perfstat-dcommand# 详细统计perfstat-pPID# 统计指定进程perfstat-asleep5# 全系统 5 秒perfstat-ecycles,instructions,cache-references,cache-misses,bus-cycles-asleep10perfstat-eL1-dcache-loads,L1-dcache-load-misses,L1-dcache-storescommandperfstat-eLLC-loads,LLC-load-misses,LLC-stores,LLC-prefetchescommandperfstat-eraw_syscalls:sys_enter-I1000-a# 每秒系统调用数

采样

perf record-F99command# 99Hz 采样命令perf record-F99-pPID-g--sleep10# 采样 + 调用栈perf record-F99-ag--sleep10# 全系统 99Hz,10 秒perf record-eL1-dcache-load-misses-c10000-ag--sleep5# 按 Cache Miss 采样perf record-ecycles:k-a--sleep5# 只采内核态perf record-ecycles:u-a--sleep5# 只采用户态

实时

perftop-F49perftop-F49-nscomm,dso

静态跟踪

perf record-esched :sched_process_exec-aperf record-econtext-switches-aperf record-e'ext4:*'-o/tmp/perf.data-aperf record-evmscan:mm_vmscan_wakeup_kswapd-ag

动态跟踪

perf probe--addtcp_sendmsg# 添加探测点perf probe-dtcp_sendmsg# 删除perf probe'tcp_sendmsg%return'# 探测返回值perf probe-Vtcp_sendmsg# 查看可用变量perf probe-Ltcp_sendmsg# 查看可用行号perf probe-x/lib64/libc.so.6 malloc# 用户态探测perf probe-l# 列出当前探测点

报告

perf report# TUI 模式perf report-n# 显示采样计数perf report--stdio# 文本输出perf script# 列出所有事件perf script--header-Fcomm,pid,tid,cpu,time,event,ip,sym,dso perf annotate--stdio# 反汇编注解

参考

  • https://zhuanlan.zhihu.com/p/186208907

  • https://zhuanlan.zhihu.com/p/54276509

  • https://www.brendangregg.com/perf.html[#OneLiners](javascript:😉

end

往期推荐

嵌入式Linux必读经典书籍

嵌入式学习路线推荐

一位读者逻辑清晰的提问

机械转行嵌入式成功上岸

一位音视频方向读者秋招上岸的经历

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/6 3:40:57

直播录播系统搭建:OBS录制与FFmpeg批量处理实战

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华
网站建设 2026/9/6 3:38:54

vibe coding实战:从能力边界到工具选型的完整指南

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华
网站建设 2026/9/6 3:34:15

自用串口工具分享

自用串口工具分享 粘土,一个面向嵌入式调试的串口工具,支持 多串口同时连接、自定义常用命令、 自动高亮匹配行、条件触发任务,以及 跨端口联动(在 A 口检测到指定字符串时, 向 B 口发送预置命令)。命令模…

作者头像 李华
网站建设 2026/9/6 3:33:38

Claude Code辅助Django电力数据可视化全栈开发实践

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华
网站建设 2026/9/6 3:32:43

录音怎么免费转MP3?这3个方法超简单

很多人都有这样的困扰 很多珍贵的音频被录在了手机里, 想要分享给朋友、上传到社交平台, 却发觉格式不兼容。常见的像是WAV、M4A这样的录音格式, 文件体积大, 传输速度慢, 压缩的质量差。别担忧, 今天会来聊一聊录音如何免费转成MP3格式。下面会介绍几种实用且便捷的方法, 帮你…

作者头像 李华
网站建设 2026/9/6 3:30:38

Python入门实战路线:从环境配置到爬虫、数据分析与自动化脚本

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华