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,647FansLike
360FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

How to install CMake on Ubuntu

CMake is a cross-platform free and open-source software tool designed to build, test, and package the software. CMake uses a simple platform and compiler-independent configuration files to control the software compilation process.

How to install Lightworks on Ubuntu

Even though Linux may not get a native installer of video editing software like Adobe Premiere or Final Cut Pro, that doesn't mean there are no industry standards tools available. Lightworks is non-linear editing (NLE) video mastering app for Windows, Linux, and macOS. Installing it on Ubuntu is simple due to deb package availability.

How to install DaVinci Resolve on Fedora

Davinci Resolve is a professional application used for color correction, video editing, visual effects, and motion graphics. It is one of the extensively used software by movie industries located in Hollywood.

The 10 Best Programming Languages for Hacking

One of the significant entities we have in Cyber Security is Ethical Hacking (ETH). It is the process of detecting and finding flaws or vulnerabilities in a system that a hacker would exploit.

5 Ways to Open a Terminal in Ubuntu

Even though Ubuntu supports many applications with amazing Graphical User Interfaces (GUI), there are always reasons why users prefer using the Terminal to perform different tasks.

How to install Wine on Fedora Workstation

Linux distributions are becoming more and more popular every day, and Fedora Workstation is not left behind. This popularity brings forth the need to run Windows applications on Linux distros like Fedora. Windows has quite some excellent Software that is not available for Linux.

MUST READ

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.
Elementary OS 5.1 Hera has received a point release with a handful of new features and bug fixes, and we will be reviewing the significant changes in this article. For those new to elementary OS, this Ubuntu-based Linux distribution uses their inhouse built Pantheon desktop environment and AppCenter.

Manjaro ‘Gellivara’ XFCE Edition (17.0.5) in 10 Screenshots

Want to take a quick virtual tour Manjaro XFCE edition instead of downloading GBs worth of ISO image and then making a Live USB of it? We will make it easy for you. Here are a series of screenshots of the important aspects of Manjaro Linux in XFCE edition. This is a light-weight edition, and aims to be fast and low on system resources. You will be amazed on how it is still visually appealing and user friendly.

Installing multiple Linux distros on a USB drive

A bootable Linux USB drive, which also referred to as a Live USB drive, is a drive containing files that enable a computer to boot into a specific Linux distro. It is most commonly used to test-drive various Linux distros before installing on the computer for use as a daily driver.

5 ways to check if a Port is open on a remote Linux PC

There is an ample number of ways to check for any open ports on a remote Linux PC. Knowing open ports on a Linux machine helps system administrators to connect to the remote PC for troubleshooting system and cloud server issues.

Top 20 must-have apps for your Ubuntu PC

OK, this one is going to be a long one, so grab a cup of coffee and scroll through the best apps that we think are must-have for your Ubuntu PC. We have hand-picked each one of these considering the most common categories that suit an average Linux user.