perf

systemLinux
The perf command is one of the most frequently used commands in Linux/Unix-like operating systems. perf Performance analysis tool for Linux

Quick Reference

Command Name:

perf

Category:

system

Platform:

Linux

Basic Usage:

perf [options] [arguments]

Common Use Cases

    Syntax

    perf [--version] [--help] [OPTIONS] COMMAND [ARGS]

    Options

    Option Description
    -v, --verbose Be more verbose
    -a, --all-cpus System-wide collection from all CPUs
    -p, --pid=PID Profile events on existing process ID
    -e, --event=EVENT Event selector. Use 'perf list' to list available events
    -g, --call-graph Enable call-graph (stack chain/backtrace) recording
    -F, --freq=FREQ Profile at this frequency
    -o, --output=FILE Output file name
    -i, --input=FILE Input file name

    Common Commands:

    Command Description
    list List all symbolic event types
    stat Run a command and gather performance counter statistics
    record Run a command and record its profile into perf.data
    report Read perf.data (created by perf record) and display the profile
    annotate Read perf.data and display annotated code
    top System profiling tool
    script Read perf.data and display trace output
    bench Run different benchmarks for core operations
    diff Compare two perf.data files
    sched Tool to trace/measure scheduler properties

    Examples

    How to Use These Examples

    The examples below show common ways to use the perf command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    perf list
    List available events.
    perf stat ls
    Run a command and gather performance counter statistics.
    perf record -a sleep 5
    Record system-wide performance data for 5 seconds. # Advanced Examples Advanced perf top Display real-time performance monitoring of the system. perf record -g ./myprogram Record call graph (stack chain/backtrace) information. perf report Display performance report from a previous perf record run. perf stat -e cycles,instructions,cache-misses ./myprogram Count specific hardware events for a program. perf record -F 99 -a -g -- sleep 30 Sample CPU stack traces at 99 Hz for 30 seconds. perf script Read and display trace output from perf record. perf annotate --stdio Show annotated code with performance statistics.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    Perf (also known as perf_events or perf tools) is a powerful performance analysis tool for Linux systems. It's integrated into the Linux kernel and provides a comprehensive framework for analyzing system performance through a wide range of hardware and software events. Originally developed as a simple tool for accessing performance counters in Linux, perf has evolved into a sophisticated performance profiling system that allows users to collect and analyze various performance metrics, identify bottlenecks, and optimize both applications and the system as a whole. Key features of perf include: 1. Hardware Events Monitoring: Perf can measure CPU performance counters that track hardware events such as CPU cycles, instructions executed, cache misses, branch mispredictions, and more. These counters provide direct insight into how efficiently the hardware is being utilized. 2. Software Events Tracing: Beyond hardware events, perf can monitor software events like context switches, minor/major page faults, and CPU migrations, which are crucial for understanding system behavior. 3. Dynamic Tracing: Through integration with kernel tracepoints, kprobes (kernel dynamic tracing), and uprobes (user-space dynamic tracing), perf can instrument specific code paths in both the kernel and user applications. 4. Call Graph Analysis: With call graph recording capability, perf can capture and visualize function call chains, helping developers understand application flow and identify performance bottlenecks in complex codebases. 5. System-wide or Process-specific Profiling: Perf can monitor the entire system or focus on specific processes, providing flexibility in analysis scope. 6. Statistical Sampling: Instead of counting every event (which would cause excessive overhead), perf uses statistical sampling to estimate event frequencies with minimal impact on system performance. 7. Real-time Monitoring: Tools like 'perf top' provide real-time insights into what functions are consuming CPU resources. Common use cases for perf include: - Identifying CPU-intensive functions in applications - Analyzing cache behavior and memory access patterns - Detecting lock contention in multithreaded applications - Measuring system call overhead - Examining kernel and interrupt processing time - Investigating scheduler behavior and CPU migrations - Profiling I/O operations and their impact on performance - Benchmarking system and application performance The perf tool is particularly valuable because it combines low-level hardware insights with high-level software analysis capabilities. This makes it suitable for a wide range of users, from application developers to kernel engineers and system administrators. Perhaps most importantly, perf has minimal overhead when compared to many other profiling tools, making it suitable for use in production environments. Its integration with the Linux kernel means it can provide accurate and detailed information without requiring special builds or significant modifications to the system or applications being analyzed. To use perf effectively, users should have a good understanding of system architecture, how applications interact with the operating system, and the basics of performance analysis methodology. The tool is most powerful when combined with knowledge of the specific system and application being profiled.

    Related Commands

    These commands are frequently used alongside perf or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the perf command works in different scenarios.

    $ perf
    View All Commands