Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux perf command

Linux perf command

Linux Command Reference Linux Command Reference


perf is a performance analysis tool set for Linux systems, short for Performance Event Counters. It is based on the Linux kernel's perf_events subsystem and can provide performance analysis capabilities at both the hardware and software levels.

The main functions of perf include:


perf basic syntax

The basic syntax of the perf command is:

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

Common subcommands include:


perf common subcommands in detail

perf stat

Counts various hardware and software events during command execution.

perf stat [options] command [command-options]

Common options:

Example:

Example

Count the execution of the ls command

perf

stat

ls

Monitor a specified process

perf

stat

-p

1234

Monitor specific events

perf

stat

-e

cycles,instructions,cache-misses

ls

perf record

Record performance data to a file (perf.data by default).

perf record [options] command [command-options]

Common options:

Example:

Example

Record the execution of the ls command

perf record

ls

Record process 1234 at a frequency of 99Hz

perf record

-F

99

-p

1234

-g

perf report

Analyze the data recorded by perf record.

perf report [options]

Common options:

Example:

Example

Analyze the default perf.data file

perf report

Analyze a specified file and output in text mode

perf report

-i

perf.data.old

--stdio

perf top

Display the functions that consume the most resources in the system in real time.

perf top [options]

Common options:

Example:

Example

Monitor system performance in real time

perf top

Monitor specific events

perf top

-e

cache-misses

perf list

List all monitorable events.

perf list [hw|sw|cache|tracepoint|pmu|event_glob]

Example:

Example

List all events

perf list

List hardware cache events

perf list cache


perf event types

perf can monitor many types of events:

Event type Description Example
Hardware CPU hardware events cycles, instructions
Software kernel software events context-switches, page-faults
Cache cache-related events cache-references, cache-misses
Tracepoints kernel static tracepoints syscalls, block, sched
PMU processor-specific events (vendor specific)
Breakpoints breakpoint events mem:[:access]

perf practical application examples

1. Analyze program performance bottlenecks

Example

Record program execution

perf record

-g

.

/

my_program

Analyze the results

perf report

-g

2. Find CPU hot-spot functions

perf top -p $(pidof my_program)

3. Compare the performance difference between two runs

Example

First run

perf record

-o

perf.data.1 .

/

my_program input1

Second run

perf record

-o

perf.data.2 .

/

my_program input2

Compare differences

perf

diff

perf.data.1 perf.data.2

4. Analyze system calls

Example

List available system call tracepoints

perf list

'syscalls:*'

Trace the open system call

perf

stat

-e

'syscalls:sys_enter_open'

-a

sleep

10


perf usage tips

  1. Reducing overhead: for long-running performance analysis, appropriately lower the sampling frequency (e.g., -F 99)

  2. Symbol resolution: ensure debug symbols are available, by installing a debug package or compiling the program with -g

  3. Flame graph generation: combine with the FlameGraph tool to generate intuitive performance analysis graphs

  4. Multi-core analysis: use the -a option to monitor all CPUs, or -C to specify a particular CPU

  5. User/kernel space separation: use the -k and -u options to analyze kernel and user space separately


Common Problem Solutions

  1. Permission issues:

    echo -1 > /proc/sys/kernel/perf_event_paranoid

    or run perf with sudo

  2. Missing symbol information:

  3. Ensure the program is compiled with the -g option
  4. Install the debug symbol package
  5. - Ensure the program is compiled with the -g option - Install the debug symbol package
    1. Sampling data is too large:

    2. Reduce the sampling frequency
    3. Shorten the sampling time
    4. Use --no-call-graph to reduce call graph information
    5. - Reduce the sampling frequency - Shorten the sampling time - Use --no-call-graph to reduce call graph information
      1. Cannot resolve high-level languages such as Java/Python:

      2. Requires perf tool support for the specific language
      3. Consider using language-specific analysis tools
      4. - Requires perf tool support for the specific language - Consider using language-specific analysis tools

        Advanced learning resources

        1. Official documentation: the tools/perf/Documentation directory in the Linux kernel source

        2. Book recommendations:

        3. "Systems Performance: Enterprise and the Cloud"
        4. "Systems Performance: Enterprise and the Cloud"
        5. - Systems Performance: Enterprise and the Cloud - Systems Performance: Enterprise and the Cloud (Chinese edition)
          1. Online resources:

          2. Brendan Gregg's blog and tool set
          3. perf-tools tool set
          4. - Brendan Gregg's blog and tool set - perf-tools tool set
            1. Related tools:

            2. FlameGraph: visualize performance data
            3. bpftrace: more flexible dynamic tracing
            4. - FlameGraph: visualize performance data - bpftrace: more flexible dynamic tracing

              By mastering the perf command, you can gain deep insight into Linux system performance characteristics, quickly locate performance bottlenecks, and optimize applications and system configuration.


              Linux Command Reference Linux Command Reference