Gperftools HowTo guide

Have you heard the news of our Lord and savior: gperftools? Even the README on the official project github site doesn’t tell you it’s true awesomeness.

You can profile any Linux binary without having to link in anything or recompile. Say your program is called ‘a.out’, you can profile it like so:
LD_PRELOAD=/usr/lib/libprofiler.so CPUPROFILE=a.out.prof a.out

Then you make a call to:
google-pprof --callgrind a.out a.out.prof > a.out.callgrind

This will have created a file that you can use ‘kcachegrind’ (a graphical tool) to examine your profiling data.

If you want to only profile for a limited amount of time, you can do:
LD_PRELOAD=/usr/lib/libprofiler.so CPUPROFILE=a.out.prof CPUPROFILESIGNAL=12 a.out

This will wait for a signal 12 (SIGUSR2) which you can send your program via:
pkill --signal 12 a.out

Each time you run the above command will turn profiling on (if it’s off), or turn it off (if it’s on). You’ll need ‘libgoogle-perftools-dev’ installed on your system to have that library to load.

Liked it? Take a second to support me on Patreon!

Leave a Reply