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.
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 --prefix=your_install_path
make -j $(nproc) install
Install Graphviz(for graph view):
sudo apt install graphviz
Then install pprof.
go get -u github.com/google/pprof
LD_PRELOAD=your_install_path/lib/libprofiler.so
by defining the CPUPROFILE
environment variable
by defining the CPUPROFILE
environment variable and the the CPUPROFILESIGNAL
environment variable
This allow you to trigger profiling by sending a signal to the process.
ProfilerStart()
and ProfilerStop()
.These functions are declared in <gperftools/profiler.h>
.
ProfilerStart()
will take the profile-filename as an argument.
LD_PRELOAD=libprofiler.so CPUPROFILE=cpu.prof ./my_program
LD_PRELOAD=libprofiler.so CPUPROFILE=cpu.prof CPUPROFILESIGNAL=12 ./my_program
kill -12 my_program
on the command line
pprof my_program cpu.prof
GUI in browser(view it as a graph, flame graph etc)
pprof -http=:8080 my_program cpu.prof