Measuring performance with the perf kernel tool
Inside Job
The kernel supports performance analysis with built-in tools via the Linux performance counters subsystem. perf is easy to use and offers a detailed view of performance data.
Making systems faster is a core part of business. Optimizing resources in data centers can save energy, space, and costs, and, out in the real world, a faster start-up time can improve the user experience for an entertainment system in the living room or even a car in the driveway.
In complex systems with many components, performance problems can stem from many different causes. On the hardware side, the CPU, RAM, the bus load, the memory system (block I/O), or the network can contribute to performance issues. On the software side, the operating system, the execution environment of the programming language, libraries, or the application itself can create bottlenecks. The fact that these components also often influence each other makes things more difficult. Although the systems work without any problems on their own, their interaction leads to friction.
To identify the root cause in the interaction of all these components requires the observer to take a bird's eye view and also have detailed knowledge of all the components. Not many IT professionals combine these skills, thus making performance optimization an exciting but challenging activity.
Fortunately, several useful tools are available for monitoring performance issues, including well-known utilities such as ps
, stat
, top
, htop
, OProfile [1], SystemTap [2], and so on.
One of the most useful performance monitoring tools is perf
[3], which has been part of the kernel since version 2.6.31. perf
uses the kernel's performance counter subsystem and supports both profiling and tracing.
During profiling, Perf evaluates data from hardware registers that count specific hardware events. Thus, it generates statistics at certain times in order to gain an impression of the system. The accuracy depends on the frequency of measurement.
In tracing, however, perf
logs certain events using trace points. These include software events such as context changes, but also hardware events such as executed instructions, cache requests, and so on. System messages are also included.
Admins and developers should keep in mind that tracing can have a major influence on the system. In general, you need to take into account that the measurements themselves can influence the measurement results.
Installation
The perf
program is part of the Linux kernel and located in the tools/perf
directory. As the output from make tools/help
shows, you can install with make -C perf_install
. You can also simply change the directory to /usr/src/<Linux_version>/tools/perf
and execute make
there.
Because certain perf
functions depend on the Linux application binary interface (ABI), distributions often ship one package per kernel version. Under Ubuntu 18.10, the linux-tools-generic
package always installs the appropriate perf
version.
If you want meaningful output, you also need to install the debug symbols for the programs you wish to example – just as you would with a debugger. The debug symbols are usually stored in separate archives. The packages automatically generated on Debian and Ubuntu 18.10 have an extension of -dbgsym
, and the manually generated packages have an extension of -dbg
.
Workflow
perf
supports more than two dozen subcommands. For an overview, type the command without any parameters or check out the perf
wiki [4]. People who use perf
usually start with perf list
. This command displays the available events (Listing 1), which you can then count with perf stat
. The record
command writes data to the hard disk; report
displays the data for searching. script
supports further evaluation of the data at the end.
Listing 1
Outputting Hardware Events (excerpt)
01 $ sudo perf list hardware 02 03 List of pre-defined events (to be used in -e): 04 05 branch-instructions OR branches [Hardware event] 06 branch-misses [Hardware event] 07 bus-cycles [Hardware event] 08 cache-misses [Hardware event] 09 [...]
Events
The Admin perf list
command also gives an optional argument on request (refer to Listing 1). This argument may be the type of the event, for example hw
or tracepoint
, or a regular expression, as in Listing 2.
Listing 2
Events at Block Level (excerpt)
01 $ sudo perf list 'block:*' 02 03 List of pre-defined events (to be used in -e): 04 05 block:block_bio_backmerge [Tracepoint event] 06 block:block_bio_bounce [Tracepoint event] 07 block:block_bio_complete [Tracepoint event] 08 block:block_bio_frontmerge [Tracepoint event] 09 block:block_bio_queue [Tracepoint event] 10 [...]
Each end of line contains the event type in square brackets. In addition, you normally have to look into the source code to see the exact meaning of the event. You can use the --events
or -e
switch to state the events to be considered.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.