perf is a performance analysis tool for Linux. Provides profiling, tracing, and performance monitoring capabilities using hardware performance counters and kernel tracing.
Basic Usage
- perf stat command - Count events
- perf record command - Record performance data
- perf report - Analyze recorded data
- perf top - Real-time profiling
- perf list - List available events
perf stat
- perf stat command - Count events for command
- perf stat -a - System-wide statistics
- perf stat -e event - Count specific event
- perf stat -p PID - Attach to process
- perf stat -r N - Run N times and average
- perf stat -d - Detailed statistics
- perf stat -I interval - Interval output
perf record
- perf record command - Record command execution
- perf record -a - System-wide recording
- perf record -p PID - Record process
- perf record -g - Record call graphs
- perf record -e event - Record specific event
- perf record -F freq - Sampling frequency
- perf record -o file - Output file
- perf record -s - Per-thread statistics
perf report
- perf report - Analyze perf.data
- perf report -i file - Input file
- perf report -g - Show call graph
- perf report -s symbol - Sort by symbol
- perf report -n - Show overhead
- perf report --stdio - Text output
- perf report --tui - TUI interface
perf top
- perf top - Real-time profiling
- perf top -a - System-wide
- perf top -p PID - Specific process
- perf top -e event - Specific event
- perf top -g - Show call graph
- perf top -K - Hide kernel symbols
- perf top -U - Hide user symbols
Common Examples
Count Events
perf stat ls
Count performance events for command.
System-Wide Stats
perf stat -a sleep 5
System-wide statistics.
Record Performance
perf record -g ./program
Record with call graph.
Real-Time Profiling
perf top
Interactive performance monitor.
List Events
perf list
Show available events.
Analyze Recorded Data
perf report
View performance report.
Record Process
perf record -p 1234
Record specific process.
Specific Event
perf stat -e cache-misses command
Count cache misses.
Tips
- Use perf stat for quick measurements
- Use perf record for detailed analysis
- Use -g for call graph analysis
- Use perf top for real-time monitoring
- Check perf list for available events
- May require root for system-wide profiling
- Essential for performance optimization
- Powerful tool for kernel and application profiling