本页目录
perf 深度使用
覆盖: perf record/report/annotate → 火焰图 → perf stat → PEBS → perf probe → 性能分析方法论 适用: Linux perf_events (kernel 2.6.31+)
perf stat: CPU 性能计数器
# 计数型: 整个程序执行期间的硬件事件
# 输出:
# task-clock: # 实际 CPU 时间
# cycles: # CPU 周期 (可被频率缩放影响, 用 cycles:u 限制用户态)
# instructions: # 执行的指令数
# IPC: # instructions/cycles (>1 = 超标量利用率好)
# branches: # 分支指令
# branch-misses: # 分支预测失败
# cache-misses: # 缓存失效
# context-switches: # 上下文切换
# page-faults: # 页故障
# 关键指标:
# IPC < 0.5: 严重 stalled (cache miss, branch miss, data dependency)
# IPC > 2: 超标量高度利用
# branch-miss rate > 5%: 分支预测问题
# cache-miss rate > 10%: 数据局部性问题
# 指定事件:
perf record: 采样
# 采样 (默认 cycles, 采样率 4000 Hz):
# 报告:
# Annotate (混合源码+汇编+采样):
# 实时 top:
火焰图
# 1. 采样:
# 2. 生成火焰图 (Brendan Gregg 工具):
# 或一行:
| |
perf probe: 动态探测
# 在任意用户态函数加探测点:
# 在任意行加探测点:
# 记录:
# 查看已加探测点:
# 删除:
PEBS: 精确采样
# 默认采样: 中断后读取指令指针 (可能不精确, skid)
# PEBS (Precise Event-Based Sampling): 硬件记录精确 IP
系统级分析
# 全系统采样 (需要 root):
# 按进程过滤:
# 按 CPU 过滤:
# CPU 利用率分解 (topdown):
# frontend bound / backend bound / bad speculation / retiring
性能分析方法论
参考
- 文档: https://perf.wiki.kernel.org, Brendan Gregg's perf examples
- 工具: https://github.com/brendangregg/FlameGraph
- 书籍: "Systems Performance" (Brendan Gregg), "BPF Performance Tools"
关键词: perf, PEBS, IPC, cache-misses, flame graph, perf probe, topdown, branch prediction