We use cookies to understand how the site is used and to display ads. Analytics and advertising only run after you accept. You can change your choice anytime. Privacy policy

Skip to content
devvkit
$devvkit learn --librarie perf-(linux)-guide

perf (Linux) Guide

[linux][profiling][cpu][tracing]
Performance & Profiling
Install
sudo apt install linux-tools-common linux-tools-$(uname -r)
# Or from kernel source: tools/perf/
# macOS: use Instruments.app or xctrace instead

perf is the built-in Linux profiler that accesses hardware performance counters (PMCs) via the perf_event_open syscall. It can profile CPU cycles, cache misses, branch mispredictions, page faults, and context switches with minimal overhead.

The most common workflow: `perf record` samples the call stack at 1kHz (adjust with `-F`), then `perf report` shows the hottest code paths as a flame-graph-friendly call tree. Use `perf top` for real-time CPU hotspot viewing.

perf trace is a strace alternative using tracepoints. perf stat aggregates hardware counters. For Java/Python/Node, use `--call-graph dwarf` for frame-pointer-unwound stacks. GUI: FlameGraph scripts by Brendan Gregg (`perf script | stackcollapse-perf.pl | flamegraph.pl`).

Stats

CPU stats· Aggregate event counts.
perf stat ./myapp
perf stat -e cycles,instructions,cache-misses,cache-references,branches,branch-misses sleep 5
perf stat -e context-switches,cpu-migrations,page-faults -p $(pgrep myapp)
perf stat with multiplexing· Many counters at once.
perf stat -d ./myapp          # Default detail (-d = -e instructions,cycles,cache-misses)
perf stat -dd ./myapp         # More detail
perf stat -ddd ./myapp        # Maximum detail
perf stat -M IPC ./myapp      # Instructions-per-cycle metric

Sampling

Sample CPU· Record call stacks.
perf record -F 99 -a -g -- sleep 10    # 99 Hz, all CPUs, call-graph
perf record -F 99 -p $(pgrep nginx) -g --sleep 30
perf record -F 999 -e cache-misses -a -g -- sleep 5
perf top· Real-time CPU view.
perf top                    # Live, like htop for CPU
perf top -p $(pgrep mysqld)  # Single process
perf top -e cache-misses     # Show cache miss hotspots
perf top -F 999 -g           # With call chains

Reporting

Report hottest· Interactive TUI report.
perf report                  # TUI: navigate with arrows, Enter to zoom
perf report --stdio          # text dump
perf report --sort=dso,symbol # by shared library
perf annotate --stdio        # annotated source + assembly

Tracing

perf trace· Syscall tracing.
perf trace -s ./myapp                # Syscall summary
perf trace -e openat,read,write -p 1234
perf trace --duration 100              # Show syscalls > 100ms

Analysis Tricks

Flame graph· Generate SVG flame graph.
perf script > out.perf
# Download FlameGraph:
# git clone https://github.com/brendangregg/FlameGraph
./stackcollapse-perf.pl < out.perf | ./flamegraph.pl > flame.svg
# Open flame.svg in browser: search for hotspots
Compare before/after· Regression detection.
perf stat -r 10 -e cycles,instructions ./myapp-v1 > v1.log
perf stat -r 10 -e cycles,instructions ./myapp-v2 > v2.log
paste v1.log v2.log | awk '{print $1, $2, ($2-$1)/$1*100 "% change"}'
Profiling with debug symbols· Get symbol names.
# Compile with -fno-omit-frame-pointer
# Install debug symbols:
sudo apt install myapp-dbg
# Or set perf map for JIT (Node, Python):
export PERF_RECORD_JIT=1
perf record -F 99 -g node --perf-basic-prof app.js