Linux perf: How to Use the Command and Profiler

Introduction

Before we start talking about Linux perf: how to use command and profiler, let's briefly understand-What is Linux Perf?

The Linux perf tool is a simple command-line software that may be used to profile and monitor CPU performance on Linux systems. Although the command is simple, it provides detailed information that can be used to analyze CPUs.

It provides a comprehensive set of features to analyze the behavior of your applications and optimize their performance. There are numerous sub-commands in this command for gathering, tracking, and examining CPU event data.

In this tutorial, we will explore how to effectively use the perf command and profiler on Linux. We will also address a few FAQs on Linux perf, how to use command and profiler.

Prerequisites

  • Access to the terminal.
  • Access to either root or a user who has sudo privileges.
  • Text editor like nano or Vi/Vim.

How to Install perf

On Linux systems, the perf programme is not preloaded. Depending on the Linux distribution, the installation varies:

  • Ubuntu/Debian
sudo apt install linux-tools-$(uname -r) linux-tools-generic
  • RHEL/CentOS
sudo yum install perf
  • Fedora
sudo dnf install perf

Check the installation by using:

perf --version

The version number is printed in the output, confirming a successful installation.

Allow Regular Users to Use perf

By default, the perf command needs sudo privileges. Follow these steps to make perf accessible to common users:

1) Change to the root user:

sudo su -

2) Execute the following command:

echo 0 > /proc/sys/kernel/perf_event_paranoid

Regular users are able to access the perf utility with this command during the current session.

3) Return to the regular user by using:

exit

Follow these steps to keep the modifications in place:

1) Change the sysctl config file:

sudo nano /etc/sysctl.conf

2) Include the following in the file:

kernel.perf_event_paranoid = 0

3) Save the modifications, then exit nano.

Linux perf Command Syntax

The syntax for the perf command is:

perf <options> subcommand <options/arguments>

The perf tool is similar to git. It serves as a user interface for a variety of sub commands and tasks. The list of available sub commands is displayed when the command is executed without any arguments or options.

Linux perf Sub commands

The following table lists the most popular perf sub commands:

Subcommand Description
annotate Reads perf.data and displays code with annotations.
list Lists all measurable events.
stat Retrieves performance statistics.
record Records samples into perf.data.
report Reads perf.data and shows the profile.
script Reads perf.data and shows trace output.
top Profiling tool.

Sub commands offer extra options. Run the following command to view the options for each sub command:

perf <subcommand> -h

A small help window for the particular sub command is shown in the output.

Linux perf Command Examples

The usual approach when using the perf command to profile a CPU is to use:

1. perf list to find events.

2. perf stat to count the events.

3. perf record to record events to a file.

4. perf report to browse the recorded file.

5. perf script to dump events after processing.

Depending on the system and resources accessible locally, the outcomes change.

1. List of the Available Events

List every measurable event using the list sub command of perf:

sudo perf list

Regardless of event type, the output lists all supported events. The command outputs a more condensed list without sudo. Use the -e tag and the event name from the first column when referring to events in other sub commands.

To restrict the list by event name (first column) or event type (second column), add the filter option after the command. For instance, you can display only hardware events with:

sudo perf list hardware

Or alternatively:

sudo perf list hw

The output displays the filtered result based on the specified parameter or parameters.

2. View CPU Real-Time System Profile

Use the top sub command to see the CPU profile live:

sudo perf top

Similar to the Linux top command, the command shows real-time samples of many functions. The following three columns are printed in the output, starting from the left:

  1. CPU usage expressed as a percentage and linked to a function.
  2. The programme or library that makes use of the function.
  3. The name of the symbol and function, with [k] denoting kernel space and [.] denoting user space.

All active CPUs are tracked by perf top by default. Other choices include:

  • Monitoring all CPUs (including idle) (-a).
  • Monitoring specific CPUs (-C).
  • Controlling the sampling frequency (-F).

Press h while in browsing mode to see more options.

Press q to leave the profiler and go back to the terminal.

3. View CPU Performance Statistics with perf

Run the following command to display CPU performance data for all typical CPU-wide hardware and software events.

sudo perf stat -a sleep 5

A thorough report for the complete system, gathered over five seconds, is displayed in the output. Without sleep 5, the system measures until CTRL+C termination.

4. View CPU Performance for a Command

Run the following command to check CPU performance data for a specific command:

sudo perf stat <command>

For instance, use the following to look for the ls command:

sudo perf stat ls

At the end of the output, a command's total execution time is displayed as time elapsed.

5. View CPU Performance for a Process

Use the -p tag and the process ID (PID) to associate CPU performance information with a particular running process:

sudo perf -p <PID> sleep 5

The output gathers and presents performance data for the specified process.

6. Count Event System Calls by Type

Run the following command to count system calls made by the Linux kernel by type:

sudo perf stat -e 'syscalls:sys_enter_*' -a sleep 5

The output shows the total number of system-wide calls and their count after five seconds.

7. Record CPU Cycles

An instance of hardware is a CPU cycle. Use the record sub command and the event name with the -e tag to log CPU cycles:

sudo perf record -e cycles sleep 10

The data is saved by the recording into a perf.data file. The file's size and the number of data samples it contains are printed in the output.

8. View Performance Results

Run the following command to view the perf.data file's performance results:

sudo perf report

The command aids in reading the perf.data file and displays all the events and information gathered. Press CTRL+C to close the viewer.

9. Modify Sample Output Format

Run this command to see the example output in standard output format.

sudo perf report --stdio

Other changes include showing the sample number for each event (-n) and showing only certain columns (--sort column name>). For instance:

sudo perf report -n --sort comm,symbol --stdio

The sample number, command, and symbol information are added as a column to the output.

10. Display Trace Output

For a complete list of events from perf.data, use the script subcommand. For instance:

sudo perf script

The perf.data details are displayed in time order in the output. Use the script subcommand to process data thereafter.

11. Display Trace Header

Run the following command to see all the events from perf.data along with additional trace header details:

sudo perf script --header

The output displays the file's header details, including the command that fetched the data, the trace's start and end times, and CPU information. The events list follows the header information.

12. Dump Raw Data

Use the -D option to dump the perf.data file's raw data as hex:

sudo perf script -D

The outcome is the ASCII-formatted raw event trace information. The option is useful for event debugging.

13. Annotate Data

Use the annotate sub command to annotate data and subsequently disassemble:

sudo perf annotate --stdio -v

The -v option produces detailed output. The output displays the events' disassembled components and source code.

FAQs: Using the perf Command and Profiler on Linux

How do I install the perf command on Linux? 

The perf command is part of the linux-tools-common package. Use the package manager specific to your Linux distribution to install it. For example, on Ubuntu, you can use apt with the command sudo apt install linux-tools-common.

How do I use the perf command to gather CPU performance information? 

To gather CPU performance information, use the perf stat command, followed by the executable or command you want to profile. For example, perf stat ./my_program will provide statistics about CPU cycles, cache misses, and other relevant metrics.

Can I use the perf command to profile software or applications? 

Yes, the perf command can be used as a software profiler. To profile an application, use the perf record command followed by the executable or command you want to profile.

How do I analyze the collected profiling data using the perf command? 

To analyze the collected data, use the perf report command. It will display a detailed summary of the collected performance data, including CPU usage, function call graph, and other useful information.

Can I specify events or hardware counters to monitor with the perf command?

Yes, you can use the -e option followed by specific events or hardware counters you want to monitor with the perf command. For example, perf stat -e cache-misses ./my_program will monitor cache misses while running my_program.

Can I generate a flame graph using the perf command? 

Yes, you can use the perf command in combination with other tools like FlameGraph to generate flame graphs. Flame graphs visually represent collected profiling data, showing the relative execution time of functions.

Can I use the perf command for kernel-related performance analysis? 

Yes, the perf command can be used to analyze kernel-related performance issues. It provides insights into kernel functions and events, allowing you to analyze kernel performance and identify bottlenecks.

Conclusion

You have a basic understanding of how to use the Linux perf command and several of its primary sub-commands after going through the examples in this tutorial. To access the entire manual for the performance analysis tool and its sub-commands, use the man command.