Software Profiling with gperftools

Metin Cakircali

1 min read

gperftools (formerly Google Performance Tools) is a collection of software performance analysis tools. The pprof tool is used to visualize the profiling data. The version included in the collection is long deprecated. The latest pprof (re-written in Go language) can be found here.

Installation

On UNIX based systems, the gperftools can be installed using HomeBrew:

brew install gperftools

The visualization of profiling data pprof, the Go language is needed and can be:

brew install go

Then, to build and install pprof via go:

go install github.com/google/pprof@latest

which will install binaries at $GOPATH/bin ($HOME/go/bin).

CPU Profiler

The three main steps are: linking, running, and analyzing the output. For detailed documentation, refer here.

Linking

There are two ways to linking the executable to the profiler; link-time and run-time. For the link-time, just pass -lprofiler option to the linker. For the run-time linking (which is not recommended), set the LD_PRELOAD=/path/to/libprofiler.so environment variable.

Running

Run the application with the CPUPROFILE=/path/to/output/prof.out environment. For example:

CPUPROFILE=prof.out /myapp/bin/test

Analyzing

To analyze the profile outputs (see prof.out):

# top entries (text)
pprof -top /myapp/bin/test prof.out

# visualize graph (svg)
pprof -web /myapp/bin/test prof.out