perf Command

The perf command is a powerful performance analysis tool for Linux. It provides a rich set of functionalities to profile CPU usage, analyze system calls, track events, and more, helping developers and system administrators optimize application and kernel performance.

Syntax

perf <command> [<options>]

Description

The perf tool is part of the Linux kernel source tree and is designed to provide a low-overhead way to collect and analyze performance data. It can be used to understand where CPU cycles are being spent, identify bottlenecks, and gain insights into system behavior.

Common uses include:

  • Profiling CPU usage of applications and the kernel.
  • Analyzing system calls and their performance.
  • Tracking hardware and software events.
  • Generating flame graphs for visual performance analysis.

Common Commands and Options

Command/Option Description
record Record performance data
report Display performance data
stat Run a command and gather performance statistics
list List available events
top System-wide performance monitoring in real-time
probe Define new dynamic tracepoints
-e <event> Select event(s) to sample
-F <freq> Sampling frequency
-g Record callgraph
-p <pid> Profile a specific PID

Examples

Record CPU cycles for a command

perf record -e cycles ls

Records CPU cycles for the ls command.

Report performance data from a previous record

perf report

Analyzes the perf.data file (created by perf record) and displays a performance report.

Get system-wide statistics

perf stat sleep 5

Runs the sleep 5 command and displays performance statistics for its execution.

List all available performance events

perf list

Lists all hardware and software events that can be profiled with perf.

Record call graph for a running process

perf record -g -p <PID>

Records a call graph for the specified process ID (PID), useful for identifying function-level bottlenecks.

See also