Perf - Linux


Overview

perf, also known as Performance Counters for Linux (PCL), is a powerful performance analyzing tool in Linux. Its primary purpose is to provide detailed insights about system and application performance utilizing a wide range of hardware and software event counters. perf is most effective for performance optimization, debugging, and understanding low-level system behavior.

Syntax

The general usage of perf is as follows:

perf [options] subcommand [subcommand-options] [subcommand-args]

Replace subcommand, subcommand-options, and subcommand-args with appropriate values based on the specific task you intend to perform.

Options/Flags

  • -v, –verbose: Increase the verbosity of messages. Useful for debugging.
  • -i, –input: Specify an input file to use for perf analysis instead of monitoring live events. Useful for analyzing past events.
  • –version: Show version information and exit.
  • -h, –help: Display help text and a list of available subcommands.

Each subcommand may have its own set of options and flags.

Examples

1. Listing all available events:

perf list

This command lists all the performance events available on the system.

2. Recording backend cache misses for a specific application:

perf record -e cache-misses -p $(pgrep -u myuser myapplication)

Replace myapplication and myuser with actual process names and user.

3. Generating a performance report:

perf report

This command reads the data recorded by perf record and displays a detailed performance report.

4. Monitoring CPU cycles spent in user and kernel mode:

perf stat -e cycles:k,cycles:u sleep 5

This will monitor the CPU cycles for 5 seconds while the system is in sleep.

Common Issues

  • Permission Denied: Running perf might require higher privileges. Using sudo often resolves this.
  • Event Not Found: Check the correctness of event names with perf list if you encounter event-related errors.
  • High Overhead: Extensive use of perf can lead to performance overhead; restrict event monitoring to necessary periods.

Integration

perf can be integrated with shell scripts to automate performance logging. Here’s an example of a bash script to monitor CPU usage:

#!/bin/bash
perf stat -e cycles -a -- sleep 10 > cpu_cycles.txt

Moreover, perf data can be piped into tools like awk or grep for advanced data processing.

  • gprof: GNU profiler for performance analysis of compiled programs.
  • strace: Monitors system calls and signals received by a process.
  • vmstat: Reports information about processes, memory, paging, block IO, traps, and CPU activity.

For further reading, you can visit the official perf documentation, which provides comprehensive resources and guides.