How to Compile Perf with all Features on?

Want perf with all the features. Here's how to compile it yourself with all features enabled.

How to Compile Perf with all Features on?
Photo by Todd Quackenbush / Unsplash

The perf profile is included in most Linux distributions. You can find it easily in the repositories. It comes with basic features enabled, which are perfectly suitable for most scenarios.

Sometimes you need it with all the bells and whistles on. For instance, I needed to use a Python script to analyze the function execution times on a system that did not provide perf with Python support. The only way was to recompile perf from the source.

Luckily, compiling perf from the sources is fairly straightforward.

All of the examples are run within Ubuntu 22.04  (ubuntu:22.04) docker image.

The source

First, we need to get the source code for perf. It is distributed with the kernel so you can get it from https://mirrors.edge.kernel.org/pub/linux/kernel/.

Don't be intimated by the kernel. Unsurprisingly, it is very well-maintained and does not require any fancy tools to build.

The safest way is to match the version number to the kernel version of the system under test. If you are only after the latest features, then go with the latest version.

Extract the source package and see what is required

I opted in using the latest kernel version 6.0.9. The next task is to fetch and extract the sources.

Fetch the sources

curl -o linux-source.tar.gz https://mirrors.edge.kernel.org/pub/linux/kernel/v6.x/linux-6.0.9.tar.gz

Extract the sources

tar xvf linux-source.tar.gz

Move to the perf tool base directory

cd linux-6.0.9/tools/perf

Compile perf

If you have not yet installed basic tools for building C applications, then run the following minimal setup to get the tools to build perf

sudo apt install make gcc flex bison

Now, you can simply run make to build perf

make

The build configuration will print out the supported features. Notice how we are missing a lot of things. This is because our minimal environment does not provide most of the libraries required for these features.

I'll go through one-by-one the features and required library to enable the support

Feature Required package
dwarf libdwarf-dev libdw-dev
dwarf_getlocations libdwarf-dev libdw-dev
glibc enabled by default
libbfd binutils-dev
libbfd-buildid binutils-dev
libcap libcap-dev
libelf libelf-dev
libnuma libnuma-dev
numa_num_possible_cpus libnuma-dev
libperl libperl-dev
libpython python2 python2-dev python-setuptools
libcrypto libssl-dev
libunwind libunwind-dev
libdw-dwarf-unwind libdwarf-dev libunwind-dev
zlib zlib1g-dev
lzma liblzma-dev
get_cpuid enabled by default
bpf enabled by default
libaio libaio-dev
libzstd libzstd-dev

Install everything in a single command

sudo apt install libdwarf-dev libdw-dev binutils-dev libcap-dev libelf-dev libnuma-dev libperf-dev python2 python2-dev python-setuptools libssl-dev libunwind-dev libdwarf-dev libunwindw zlib1g-dev liblzma-dev libaio-dev

Running make again should show you all the features enabled

Summary

Getting perf for your favorite distribution is easy. Just fetch it from the repo. Getting all the features requires some work. But as you can see it's not that hard. As long as you are not intimated by the kernel sources, rebuilding the tools is actually quite a straightforward process.