The CPU profiler from gperftools can be attached to arbitrary executables using either LD_PRELOAD
or typical dynamic linkage. It can output data in a callgrind-compatible format.
Let's assume that you'd like to profile an executable a.out
. Begin by linking it with -lprofiler
. Afterwards run it with CPUPROFILE
env. variable pointing to a name of a file where profiling data will be stored. Data in the callgrind format can be obtained using pprof
.
CPUPROFILE=a.out.prof ./a.out
pprof --callgrind a.out a.out.prof
What's interesting is the fact that with CPUPROFILE
undefined your executable behaves normally. As a result this profiler can be easily enabled without recompiling or relinking the application.
If for any reason you cannot alter the way the executable is linked you can still profile it by defining LD_PRELOAD
in a following fashion.
LD_PRELOAD=/path/to/libprofiler.so CPUPROFILE=a.out.prof ./a.out