# Profiling C/C++ with google-perftools


## Install libunwind

```bash
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 install
```

## Install gperftools

```bash
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.0
```

For the symlink, 64-bit systems use `/usr/lib64/`, 32-bit systems `/usr/lib/`.

## Install graphviz

graphviz is needed for graphical reports:

```bash
yum -y install graphviz
```

## Instrument the code

Add the link flags at build time:

```text
-L/usr/local/lib -lprofiler
```

Then bracket the region you want to measure:

```cpp
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:

```bash
pprof --svg ./myapp MyProfile > profile.svg
```

pprof supports several output formats — run it without arguments to see the list. SVG was good enough here; just open it in a browser.

