Installing and Using Perf in Ubuntu and CentOS

Perf a performance counter for Linux that you can use to gather data about performance of your system. Let's learn how to install it and use it.

Performance is one of the most complicated parts of systems administration.  Whenever you are trying to investigate the source of bad performance or the reason for the high workload, you need the right tools to do so.

Perf is one of those tools. It’s a performance counter for Linux that you can use to gather data about performance of your system.

We will see how to install it on Ubuntu and Centos and will do a small walkthrough of the basic command usage.

Installing Perf on Ubuntu

Perf is part of the package ‘linux-tools’ in Ubuntu and it is installed like any other package using apt. Just make sure you have sources updated.

sudo apt update

First, install package linux-tools-common:

sudo apt install linux-tools-common

Perf
Linux Tools Common Installation

Next, you need to specific package for your running kernel. To get the exact version of your kernel, use the following command:

uname -r

You will get the exact kernel version.

uname
Getting kernel version in Linux

You can install the specific linux-tools package or if there is no specific version of the package for your kernel, you can use the linux-tools-generic package. But this might not work as expected. As a rule of thumb, perf won’t work or won’t work as intended, if the linux-tools package does not match the running (very important! the running, not the installed) version of Linux kernel.

Installing Perf on CentOS

As usual, package names vary from CentOS to Ubuntu but the procedure is basically the same, first update package information:

yum update

There is a straight package that will trigger all dependencies in CentOS.

yum install perf

Centos install summary
Perf installation summary in CentOS

Basic usage of Perf Command

Let us see some basic commands on how to use perf. The most basic usage of perf is, of course, help page, with “–help” flag to see command summary

perf help result
Perf Usage Summary

Depending on your system configuration, some commands of perf might be limited by security policies. To disable those policies just use:

echo 0 > /proc/sys/kernel/perf_event_paranoid

There are two big groups of commands: commands that are run over live system and commands that are run over a performance record.

Commands over a live system

perf list [category]

The above command lists all the available event types in the system. This is a long list and depending on the system configuration, it will be wrapped by less/more. If you provide a category value, only events on this category will be shown.

Available categories are: hw|sw|cache|tracepoint|pmu|event_glob

Perf list output
Available software events in Perf

perf bench <category> <benchmark>

The above command makes several benchmarks on the system. They are split amongst different categories (sched, mem, numa, futex, all) and each category has a different list of benchmarks.

If you want to check all memory speed tests, use the below command:

perf bench mem all

Perf bench result
Perf memory benchmark result

perf stat <command>

The above command will return the performance stats. It will output something like this

Perf Stat Result
Results of perf stat execution

perf top

This will display a live analysis of system profile, like top, but more fine-tuned.

Perf Top Result
Example screen of perf top output

Here starts the second group of commands.

Commands over a Performance record

You can record a performance profile of any command you execute using the following command line:

perf record [options]

result of perf record
Result of perf record for ls -laR

This will produce a raw performance registers record. How deep this record is, will depend on the installation of your system. You might need debug or dev packages to get all the profiling data you might get. There are also a lot of flags that will allow us to control what will be profiled, how it will be saved and how often it will be profiled.

Once we have the performance record, there are several commands to act with this data, the easiest one is as follows:

perf report -i <perf file> [options]

perf report result
Result of perf report on ls -laR command

This will generate a report of the performance file. We can select how to split or sort the data or which data we want in the report.

perf diff <datafile 1> <datafile 2> …

This will allow us to compare two executions, for example, if we want to know how the program is running after changing parameters compared with base execution.

perf lock -I <perf file>

This will analyze all lock events in a previously recorded performance file.

perf mem -t <Type> [ record <command> | report ]

This will record or report from a record, profiling on memory access of the type selected.

perf timechart

This will make a time chart of the execution of previously recorded profiling.

Bar chart of perf execution
Partial output of perf time chart

With all these tools in our hand, you can start checking what’s going on in your server, you can start doing profiles of executions to test different configurations and you will be armed with a vast set of tools to get control of the performance on your system.

Divya Kiran Kumar
I'm the Editor of FOSS Linux. I worked as a Software Engineer before taking up blogging as my full-time job. I enjoy using Linux, and can't imagine anything else for my PC. Apart from writing for FOSS Linux, I enjoy reading non-fictional books. Sapiens was my favorite last read. I hope you enjoy reading and using this blog to enhance your Linux experience! Have a great day ahead!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,279FansLike
386FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

MUST READ

Buyers who wish to go for a machine that is based on Linux often show interest in Chromebooks due to the form factor and extended battery life capabilities. Although ChromeOS power these machines, users can still miss out on a more genuine Linux experience. For those who happen to agree, the new Lemur Pro by System76 might get some heads turning.
Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.

The 10 Best Linux Network Monitoring Tools

Having total control over your network is essential to prevent programs from overusing your network resources and slowing down the overall performance. This is why you should install a network monitoring tool on your system, giving you a visual overview of everything that's happening on your network. Networking Tools are like swiss-knife for the system administrators for troubleshooting system issues.

5 Things to do when your Linux system GUI freezes

All operating systems freeze at some point and, Linux distributions are no exceptions. In this post, we are going to look at five methods you can apply to your frozen system before thinking of plugging out the power cable or holding down the power button to power off the PC.

Ubuntu MATE 20.04 LTS Review: Refinement at its Best

Ubuntu MATE 20.04 LTS was released a week ago after two years of development. Official updates and security patches will be provided until April 2025. I have installed it on my test laptop for a spin and here are my observations based on almost a week usage.

Pop!_OS 20.04 Review: Professional Linux Distribution Ever Made

Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.