How to install and use gperftools

How to install and use gperftools

gperftools is a very handy tool made by google to profile an execution. It is well coupled with pprof.

In general, gperftools generates profile data, pprof is used to view/visualize the profile result.

To install gperftools

git clone
cd gperftools
git tag -l # check the latest version
git checkout gperftools-2.7
sudo make install
ls /usr/local/lib/libprofiler.* # verify the installation
# or
ls .libs

To install pprof. There are official docs for the installation. Here is the summary

# sudo apt install -yq golang
go get -u

The pprof command will be installed in $HOME/go/bin/pprof.

To generate profiling data

gcc -c -g my_program.c
LD_LIBARRY_PATH=/usr/local/lib gcc -Wl,--no-as-needed,-lprofiler,--as-needed my_program.o -o my_program
#verify the linking
LD_LIBRARY_PATH=/usr/local/lib ldd my_program ./my_program

The profiling result will be written to

The reason explaining why we need -Wl,--no-as-needed,-lprofiler,--as-needed instead of just -lprofiler is that with -lprofiler the linker will detect that none of the routine in is called from the program ( my_program), then it will skip library while linking.

To view profiling result

  • As text: pprof -top
  • As web view (SVG format): pprof -top
  • Interactive mode: pprof
  • Web: pprof -http=localhost:9001 (9001 is an arbitrary port)