
Linux Perf
- 427 installs
- 155 repo stars
- Updated June 27, 2026
- mohitmishra786/low-level-dev-skills
linux-perf is an agent skill that guides developers through Linux perf stat, perf record, perf report, and flamegraph export to locate CPU hotspots, cache misses, and branch mispredictions in native binaries.
About
linux-perf is a low-level-dev-skills agent skill for CPU performance analysis with the Linux perf profiler. It walks through nine workflow sections covering prerequisites, paranoid-level sysctl tuning, compiling with -g and -fno-omit-frame-pointer, perf stat hardware counters (cache-misses, IPC, branch-misses), perf record sampling at configurable frequencies, perf report hotspot review, perf annotate disassembly, off-CPU profiling, and flamegraph handoff. The skill explains how to interpret IPC below 1.0, cache-miss rates above 5%, and branch-miss rates above 5% as optimization signals. Developers reach for linux-perf when profiling C/C++/Rust binaries on Linux, diagnosing [unknown] stack frames, or feeding perf.data into flamegraph generators before shipping latency-sensitive services.
- perf record/report flamegraphs
- Hardware counter events
- Kernel and userspace stacks
- Off-CPU and syscall analysis
- Before/after benchmark diffs
Linux Perf by the numbers
- 427 all-time installs (skills.sh)
- +25 installs in the week ending Aug 4, 2026 (Skillselion tracking)
- Ranked #101 of 596 Debugging skills by installs in the Skillselion catalog
- Data as of Aug 4, 2026 (Skillselion catalog sync)
npx skills add https://github.com/mohitmishra786/low-level-dev-skills --skill linux-perfAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 427 |
|---|---|
| repo stars | ★ 155 |
| Last updated | June 27, 2026 |
| Repository | mohitmishra786/low-level-dev-skills ↗ |
How do you profile CPU hotspots with Linux perf?
Sample CPU cycles, cache misses, and syscall hotspots in production-like Linux workloads to prioritize optimizations before shipping performance-critical native code.
Who is it for?
Systems and native-code developers optimizing C, C++, or Rust binaries on Linux who need sampling profiles and hardware counter interpretation before release.
Skip if: Developers profiling macOS or Windows workloads without Linux perf access should skip this skill because commands and kernel paranoid settings are Linux-specific.
When should I use this skill?
A user asks which function consumes the most CPU, how to measure cache misses or IPC, or how to generate a flamegraph from perf record output.
What you get
perf.data capture, perf stat counter report, annotated hotspot function list, and flamegraph input ready for visualization.
- perf.data profile
- perf stat counter summary
- hotspot function report
By the numbers
- Documents 9 numbered perf workflow sections from prerequisites through flamegraph handoff
Files
Linux perf
Purpose
Guide agents through perf for CPU profiling: sampling, hardware counter measurement, hotspot identification, and integration with flamegraph generation.
Triggers
- "Which function is consuming the most CPU?"
- "How do I measure cache misses / IPC?"
- "How do I use
perfto find hotspots?" - "How do I generate a flamegraph from perf data?"
- "perf shows
[unknown]or[kernel]frames"
Workflow
1. Prerequisites
# 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.confCompile the target with debug symbols for useful frame data:
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=dwarf2. perf stat — quick counters
# 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 10Interpret perf stat output:
- IPC (instructions per cycle) < 1.0: memory-bound or stalled pipeline
- cache-miss rate > 5%: significant cache pressure
- branch-miss rate > 5%: branch predictor struggling
3. perf record — sampling
# 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 ./prog4. perf report — interactive analysis
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 outputNavigation in TUI:
Enter— expand a symbola— annotate (show assembly with hit counts)s— show source (needs debug info)d— filter by DSO (library)t— filter by thread?— help
5. perf annotate — hot instructions
# 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 --stdioHigh hit count on a mov or vmovdqa suggests a cache miss at that load.
6. perf top — live profiling
# Live top, like 'top' but for functions
sudo perf top -g
# Filter by process
sudo perf top -p 123457. Feed into flamegraphs
# 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 browserSee skills/profilers/flamegraphs for reading flamegraphs and interpreting results.
8. Common issues
| Problem | Cause | Fix |
|---|---|---|
Permission denied | perf_event_paranoid too high | Lower paranoid level or run with sudo |
[unknown] frames | Missing frame pointers or debug info | Recompile with -fno-omit-frame-pointer or use --call-graph=dwarf |
[kernel] everywhere | Kernel symbols not visible | Use sudo perf record; install linux-image-$(uname -r)-dbgsym |
No kallsyms | Kernel symbols unavailable | `echo 0 |
| Empty report for short program | Program exits too fast | Use -F 9999 or instrument longer workload |
| DWARF unwinding slow | Large DWARF stack | Limit with --call-graph dwarf,512 |
9. Useful events
# 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_readFor a counter reference and interpretation guide, see references/events.md.
Related skills
- Use
skills/profilers/flamegraphsfor SVG flamegraph generation and reading - Use
skills/profilers/valgrindfor cache simulation and memory profiling - Use
skills/compilers/gccorskills/compilers/clangfor PGO from perf data (AutoFDO)
Linux perf Events Reference
Source: <https://perf.wiki.kernel.org/index.php/Main_Page> Source: <https://man7.org/linux/man-pages/man1/perf-stat.1.html>
Table of Contents
1. Hardware events 2. Software events 3. Tracepoints 4. Interpreting metrics
---
Hardware events
These map to hardware PMU counters. Availability depends on CPU.
| Event | Meaning |
|---|---|
cycles | CPU clock cycles |
instructions | Instructions retired |
cache-references | L1D cache accesses |
cache-misses | L1D cache misses |
branch-instructions | Branches executed |
branch-misses | Branch mispredictions |
bus-cycles | Bus cycles |
stalled-cycles-frontend | Cycles stalled fetching instructions |
stalled-cycles-backend | Cycles stalled waiting for execution units |
ref-cycles | Reference (unscaled) cycles |
Raw PMU events (Intel Skylake example)
# L2 misses
perf stat -e r412e ./prog
# LLC (L3) misses
perf stat -e r2b4 ./prog
# DTLB misses
perf stat -e r08085 ./prog
# Memory bandwidth (approximate)
perf stat -e 'cpu/event=0xd1,umask=0x20/u' ./progUse ocperf.py or Intel's PMU tools for named aliases.
---
Software events
These are tracked by the kernel in software, not hardware PMU.
| Event | Meaning |
|---|---|
cpu-clock | CPU clock (software timer) |
task-clock | Time on-CPU for the task |
page-faults | Total page faults (minor + major) |
minor-faults | Minor faults (page in memory, just not mapped) |
major-faults | Major faults (page must be fetched from disk) |
context-switches | Voluntary + involuntary switches |
cpu-migrations | Process moved to different CPU |
alignment-faults | Misaligned memory accesses |
emulation-faults | Emulated instructions |
---
Tracepoints
Requires root (perf_event_paranoid ≤ 0 or sudo).
# List all
perf list tracepoint
# Scheduler
sched:sched_switch
sched:sched_wakeup
sched:sched_process_fork
# Syscalls
syscalls:sys_enter_read
syscalls:sys_exit_read
syscalls:sys_enter_write
# Block I/O
block:block_rq_issue
block:block_rq_complete
# Network
net:netif_receive_skb
net:net_dev_xmit---
Interpreting metrics
| Metric | Formula | Interpretation |
|---|---|---|
| IPC | instructions / cycles | < 1.0: stalled; > 2.0: well-optimised |
| CPI | cycles / instructions | Inverse of IPC |
| Cache miss rate | cache-misses / cache-references | > 10%: significant |
| Branch miss rate | branch-misses / branch-instructions | > 5%: worth examining |
| Frontend stall % | stalled-cycles-frontend / cycles | > 20%: instruction fetch bottleneck |
| Backend stall % | stalled-cycles-backend / cycles | > 20%: execution bottleneck |
Diagnosing bottlenecks
- Low IPC + high backend stalls + high cache misses → memory bandwidth bound; improve data locality
- Low IPC + high frontend stalls → i-cache pressure; split hot/cold code, enable PGO
- High branch-miss rate → unpredictable branches; sort data, profile-guided branch hints
- High page-faults major → thrashing; reduce working set or increase physical memory
Related skills
How it compares
Pick this over generic debugging skills when Linux perf sampling, PMU hardware counters, and flamegraph preparation are the explicit profiling toolchain.
FAQ
What compile flags does linux-perf recommend for useful stack traces?
linux-perf recommends compiling with -g -O2 -fno-omit-frame-pointer so perf can unwind frames reliably. Alternatively, DWARF CFI with --call-graph=dwarf works when frame pointers are omitted.
How does linux-perf interpret low IPC in perf stat output?
linux-perf treats instructions-per-cycle below 1.0 as a memory-bound or pipeline-stall signal. Cache-miss and branch-miss rates above 5% indicate significant optimization pressure worth investigating.
What permissions does perf need on Linux?
linux-perf documents kernel.perf_event_paranoid: level 2 limits kernel visibility, level 1 allows user plus kernel sampling, and level 0 or -1 relaxes restrictions. Root or lowered paranoid is required for full profiles.