perf
perf
is a powerful command-line tool for system profiling and performance analysis. It’s part of the Linux kernel and provides detailed information about CPU events, such as cache misses, branch mispredictions, and instruction execution.
Use cases:
- CPU Profiling:
perf
can collect data on CPU usage, including time spent in different functions or libraries. - System Bottleneck Analysis: It helps identify performance bottlenecks by analyzing system-wide metrics like cache misses, page faults, and context switches.
- Event Tracing:
perf
allows you to track specific events, such as disk I/O, network packets, or even custom events.
Special hacks:
- Basic profiling: Run
perf record -g <command>
to collect CPU profiling data for a specific command or process.-g
enables function tracing and stack unwinding.
- Event filtering: Use
perf stat
with the--event
option to focus on specific events, e.g.,perf stat --event=cpu-cycles
. - Sampling: Run
perf stat -c <number>
to take multiple samples at regular intervals (e.g., every 10 milliseconds). - Visualization: Use tools like
perf top
,perf stat
with the-x
option, orsvg-perf-report
for interactive visualizations.
Level of experience required:
perf
is a command suitable for intermediate to advanced Linux users. To get the most out of it, you should have a solid understanding of:
- System architecture: Familiarity with CPU architecture, caching mechanisms, and system call interfaces.
- Linux kernel concepts: Knowledge of Linux kernel internals, including process scheduling, memory management, and I/O subsystems.
If you’re new to Linux, start by exploring basic profiling tools like time
, top
, or htop
. As you gain experience with these tools, you can progress to more advanced techniques using perf
.