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 https://github.com/gperftools/gperftools
cd gperftools
git tag -l # check the latest version
git checkout gperftools-2.7
./autogen.sh
./configure
make
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 github.com/google/pprof

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
CPUPROFILE=my_program.prof ./my_program

The profiling result will be written to my_program.prof.

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 libprofiler.so is called from the program ( my_program), then it will skip libprofiler.so library while linking.


To view profiling result

  • As text: pprof -top my_program.prof
  • As web view (SVG format): pprof -top my_program.prof
  • Interactive mode: pprof my_program.prof
  • Web: pprof -http=localhost:9001 my_program.prof (9001 is an arbitrary port)
Buy Me A Coffee