Profiling C/C++ with google-perftools
Contents
Install libunwind
wget https://github.com/libunwind/libunwind/releases/download/v1.3.0/libunwind-1.3.0.tar.gz --no-check-certificate
tar -xvf libunwind-1.3.0.tar.gz
cd libunwind-1.3.0
./configure
make && make installInstall gperftools
git clone https://github.com/gperftools/gperftools
cd gperftools/
sh autogen.sh
./configure
make && make install
ln -s /usr/local/lib/libprofiler.so.0 /usr/lib64/libprofiler.so.0For the symlink, 64-bit systems use /usr/lib64/, 32-bit systems /usr/lib/.
Install graphviz
graphviz is needed for graphical reports:
yum -y install graphvizInstrument the code
Add the link flags at build time:
-L/usr/local/lib -lprofilerThen bracket the region you want to measure:
ProfilerStart("MyProfile");
// ... code under test ...
ProfilerStop();These calls can live in the source, or be toggled at runtime through gdb call.
Generate a report
When the process exits, a MyProfile file appears in the working directory. Convert it to SVG with pprof:
pprof --svg ./myapp MyProfile > profile.svgpprof supports several output formats — run it without arguments to see the list. SVG was good enough here; just open it in a browser.