Google performance tool is an excellent tool for profiling C++ code.

Install libuwind

wget http://download.savannah.gnu.org/releases/libunwind/libunwind-1.0.1.tar.gz
tar xvfz libunwind-1.0.1.tar.gz
cd libunwind-1.0.1
export CFLAGS=-U_FORTIFY_SOURCE
./configure
make
sudo make install

Install Google Performance Tools

wget http://google-perftools.googlecode.com/files/google-perftools-1.8.3.tar.gz
tar xvfz google-perftools-1.8.3.tar.gz
cd google-perftools-1.8.3
./configure
make
sudo make install

Install supporting packages

sudo apt-get install graphviz
sudo apt-get install gv

Include profiling code in C++

Include supporting header file(s)

#include <google/profiler.h>

Start Profiler

Assuming that profiler output is stored in file /tmp/output.pprof. The following code can be placed anywhere in the code. This is the point at which the profiler will start collecting data. It is possible to start profiling at multiple stages into different files.

ProfilerStart("/tmp/output.pprof);

Stop Profiler

It is possible to start profiler at any point of time by placing the following code.

ProfilerStop();

Run Profiling

Compile the code and execute it. Once the run is finished or ProfilerStop is called, profiler output will be available in the output file.

Generate profiling report

Several types of reports can be generated from the output file.

Graphical report

X11 need to be enabled for the graphical report.

pprof -gv --lines /tmp/output.pprof
Advertisements