Installing eBPF tools, bcc and ply on CentOS 7

Linux performance analysis is a vast and complicated topic, but due to recent advancements in tooling it is becoming significantly easier to get to the root of many performance issues that plague production environments around the world. Brendan Gregg has a page dedicated to this subject. Here I cover how to get up and running on CentOS 7 so you can practice those tools before your servers are on fire. Observing your applications while they are working nominally will also help you notice differences in behavior once something breaks.

Set up sudo if you have not done so already, my instructions assume you did. Update the system and install development and required/optional tools:

sudo yum install -y epel-release
sudo yum update -y
sudo yum groupinstall -y "Development tools"
sudo yum install -y elfutils-libelf-devel iperf cmake3

CentOS 7 comes with bison 2.7, but to compile ply we need bison 3:
cd
mkdir build
cd build
curl -OL https://ftp.gnu.org/gnu/bison/bison-3.0.tar.xz
tar -xf bison-3.0.tar.xz
cd bison-3.0
./configure
make
sudo make install
sudo yum remove -y bison

CentOS 7 comes with 3.x Linux kernel, to use all of eBPF, bcc and ply we need at least 4.9. Thankfully Elrepo offers up to date kernel packages.

sudo rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
curl -LO https://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm
sudo yum localinstall -y elrepo-release-7.0-2.el7.elrepo.noarch.rpm
sudo yum --enablerepo=elrepo-kernel install -y kernel-ml kernel-ml-devel

We need to install kernel-ml-devel to be able to run bcc tools once they are installed. We also need kernel headers but sadly the package for it in elrepo conflicts with the one in base repo. We need to download it and use the headers for our build without installing them:

cd ~/build
sudo yum install -y yum-utils
yumdownloader --enablerepo=elrepo-kernel kernel-ml-headers
rpm2cpio kernel-ml-headers*.rpm | cpio -idmv

The headers will end up in usr/include under the current directory. Lets make the mainline the default kernel on boot:

sudo grub2-set-default 0

Now reboot, and make sure we are booting our freshly installed 4.x series kernel. With this out of the way lets install LLVM, clang, ply and bcc. Here I am installing LLVM 3.9 but you can try 4.0 as well if you are feeling lucky.

cd ~/build
git clone https://github.com/iovisor/ply.git
cd ply
./autogen.sh
 export CFLAGS=-I${HOME}/build/usr/include
./configure
make
sudo make install

cd ..
curl -LO http://releases.llvm.org/3.9.1/cfe-3.9.1.src.tar.xz
curl -LO http://releases.llvm.org/3.9.1/llvm-3.9.1.src.tar.xz
tar -xf cfe-3.9.1.src.tar.xz
tar -xf llvm-3.9.1.src.tar.xz
mkdir clang-build
mkdir llvm-build

cd llvm-build
cmake3 -G "Unix Makefiles" -DLLVM_TARGETS_TO_BUILD="BPF;X86" \
  -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr ../llvm-3.9.1.src
make
sudo make install

cd ../clang-build
cmake3 -G "Unix Makefiles" -DLLVM_TARGETS_TO_BUILD="BPF;X86" \
  -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr ../cfe-3.9.1.src
make
sudo make install

cd ..
git clone https://github.com/iovisor/bcc.git
mkdir bcc-build
cd bcc-build
cmake3 -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/usr ../bcc
make
sudo make install

ply should now be on your PATH and bcc tools installed to /usr/share/bcc/tools.