|home| |posts| |projects| |cv| |bookmarks| |github|

Cpu Profiling With Gperftools

In this post I'll go through how you can get a CPU profile for C/C++ programs using the gperftools CPU profiler an view it with pprof.

Install necessary tools

gperftools

On 64-bit systems, install libunwind, see here why.

sudo apt install libunwind-dev

Now you can install gperftools.

Download the latest release from here and unpack it.

wget https://github.com/gperftools/gperftools/releases/download/gperftools-2.9.1/gperftools-2.9.1.tar.gz
tar xf gperftools-2.9.1.tar.gz

Then configure, build and install it.

./configure
make -j $(nproc)
sudo make install
sudo ldconfig

pprof

install Go.

Install Graphviz(for graph view):

sudo apt install graphviz

Then install pprof.

go get -u github.com/google/pprof

Generate CPU profile

compile code with debug symbols

activate CPU profiling

This allow you to trigger profiling by sending a signal to the process.

These functions are declared in <gperftools/profiler.h>. ProfilerStart() will take the profile-filename as an argument.

Example

Analyze CPU profile