mcpField
L
linux-perf

linux-perf

github.com/mohitmishra786开发269766 次安装18 周前更新
代码仓库

关于 linux-perf

这是一份面向 Agent 的 Linux `perf` 指南,用于分析 CPU 使用和硬件计数器,涵盖采样、热点分析、实时剖析和生成火焰图,并说明常见符号与权限问题。

主要功能

  1. 01测量 CPU 和硬件计数器
  2. 02使用 perf record 采样
  3. 03通过 perf report 分析剖析数据
  4. 04用 perf annotate 检查热点指令
  5. 05使用 perf top 实时剖析
  6. 06从 perf 数据生成火焰图

适用场景

  1. 01定位占用 CPU 较多的函数
  2. 02测量缓存未命中和指令吞吐量
  3. 03检查运行中进程的热点
  4. 04将剖析数据转换为火焰图

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/mohitmishra786/low-level-dev-skills --skill linux-perf

安装后在 Claude Code 中输入/linux-perf确认已加载。

附带文件references/events.md

详细介绍

Linux perf

用途

指导 Agent 使用 perf 进行 CPU 性能分析:采样、硬件计数器测量、热点识别,以及与火焰图生成工具集成。

触发条件

  • “哪个函数消耗的 CPU 最多?”
  • “如何测量缓存未命中数 / IPC?”
  • “如何使用 perf 查找热点?”
  • “如何根据 perf 数据生成火焰图?”
  • “perf 显示了 [unknown] 或 [kernel] 帧”

工作流

1. 前置条件
# Install
sudo apt install linux-perf    # Debian/Ubuntu (version-matched)
sudo dnf install perf          # Fedora/RHEL

# Check permissions
# By default perf requires root or paranoid level ≤ 1
cat /proc/sys/kernel/perf_event_paranoid
# 2 = only CPU stats (not kernel), 1 = user+kernel, 0 = all, -1 = no restrictions

# Temporarily lower (session only)
sudo sysctl -w kernel.perf_event_paranoid=1

# Persistent
echo 'kernel.perf_event_paranoid=1' | sudo tee /etc/sysctl.d/99-perf.conf
sudo sysctl -p /etc/sysctl.d/99-perf.conf

使用调试符号编译目标,以获取有用的调用帧数据:

gcc -g -O2 -fno-omit-frame-pointer -o prog main.c
# -fno-omit-frame-pointer: essential for frame-pointer-based unwinding
# Alternative: compile with DWARF CFI and use --call-graph=dwarf
2. perf stat — 快速计数器
# Basic hardware counters
perf stat ./prog

# With specific events
perf stat -e cache-misses,cache-references,instructions,cycles,branch-misses ./prog

# Wall-clock comparison: N runs
perf stat -r 5 ./prog

# Attach to existing process
perf stat -p 12345 sleep 10

解读 perf stat 输出:

  • IPC(每周期指令数)< 1.0:受内存限制或流水线停顿
  • 缓存未命中率 > 5%:存在明显的缓存压力
  • 分支未命中率 > 5%:分支预测器遇到困难
3. perf record — 采样
# Default: sample at 1000 Hz (cycles event)
perf record -g ./prog

# Specify frequency
perf record -F 999 -g ./prog

# Specific event
perf record -e cache-misses -g ./prog

# Attach to running process
perf record -F 999 -g -p 12345 sleep 30

# Off-CPU profiling (time spent waiting)
perf record -e sched:sched_switch -ag sleep 10

# DWARF call graphs (better for binaries without frame pointers)
perf record -F 999 --call-graph=dwarf ./prog

# Save to named file
perf record -o myapp.perf.data -g ./prog
4. perf report — 交互式分析
perf report                          # reads perf.data
perf report -i myapp.perf.data
perf report --no-children            # self time only (not cumulative)
perf report --sort comm,dso,sym      # sort by fields
perf report --stdio                  # non-interactive text output

TUI 中的导航操作:

  • Enter — 展开符号
  • a — 注释(显示带命中次数的汇编代码)
  • s — 显示源代码(需要调试信息)
  • d — 按 DSO(库)筛选
  • t — 按线程筛选
  • ? — 帮助
5. perf annotate — 热点指令
# Show assembly with hit percentages
perf annotate sym_name

# From report: press 'a' on a symbol
# Or directly:
perf annotate -i perf.data --symbol=hot_function --stdio

mov 或 vmovdqa 的命中次数较高,说明该加载操作可能发生了缓存未命中。

6. perf top — 实时性能分析
# Live top, like 'top' but for functions
sudo perf top -g

# Filter by process
sudo perf top -p 12345
7. 输入火焰图工具
# Generate perf script output
perf script > out.perf

# Use Brendan Gregg's FlameGraph tools
git clone https://github.com/brendangregg/FlameGraph
./FlameGraph/stackcollapse-perf.pl out.perf > out.folded
./FlameGraph/flamegraph.pl out.folded > flamegraph.svg

# Open flamegraph.svg in browser

有关火焰图的阅读和结果解读,请参阅 skills/profilers/flamegraphs。

8. 常见问题
问题原因解决方法
Permission deniedperf_event_paranoid 值过高降低 paranoid 级别,或使用 sudo 运行
[unknown] 帧缺少帧指针或调试信息使用 -fno-omit-frame-pointer 重新编译,或使用 --call-graph=dwarf
到处都是 [kernel]内核符号不可见使用 sudo perf record;安装 linux-image-$(uname -r)-dbgsym
No kallsyms内核符号不可用`echo 0
短程序的报告为空程序退出得太快使用 -F 9999,或对更长的工作负载进行检测
DWARF 展开速度慢DWARF 调用栈过大使用 --call-graph dwarf,512 限制
9. 实用事件
# List all available events
perf list

# Common hardware events
cycles
instructions
cache-references
cache-misses
branch-instructions
branch-misses
stalled-cycles-frontend
stalled-cycles-backend

# Software events
context-switches
cpu-migrations
page-faults

# Tracepoints (requires root)
sched:sched_switch
syscalls:sys_enter_read

有关计数器参考和解读指南,请参阅 references/events.md。

相关技能

  • 使用 skills/profilers/flamegraphs 生成并阅读 SVG 火焰图
  • 使用 skills/profilers/valgrind 进行缓存模拟和内存性能分析
  • 使用 skills/compilers/gcc 或 skills/compilers/clang 根据 perf 数据进行 PGO(AutoFDO)

评论

支持 Markdown