3

I'm looking for a C/C++ library with profiling capabilities such as gprof, or callgrind.

More precisely, I want its output to be equivalent to what callgrind would issue, in order to pass it to third-party tools such as KCacheGrind.

The idea is to be able to design an aspect based on this library and to plug it into several applications we are developing in my team.

jopasserat
  • 5,721
  • 4
  • 31
  • 50
  • we have also considered recompiling `gprof` as a shared library but haven'tfoudnany other initiative going this way – jopasserat Dec 18 '12 at 13:52
  • 2
    You're aware that gprof is just a "display" tool, it doesn't generate/gather the profiling data, right? – Mat Dec 18 '12 at 13:55
  • yeah i'm aware of that. actually, the output that we are interested in is more or less a callgraph, so an output from callgrind would be satisfying too. I'll update my question accordingly. – jopasserat Dec 18 '12 at 14:03
  • 1
    I'm sorry it's still not clear what you're trying to achieve. Are you looking for a library that would produce callgrind-like data from... something? – Mat Dec 18 '12 at 15:05
  • exactly. I want to integrate this library in my apps to profile them when needed. – jopasserat Dec 18 '12 at 15:08
  • Since you're a computer scientist (and have probably been taught that *gprof* is the canonical profiler), and have some basic knowledge of probability and statistics, I hope you're aware of [*these issues*](http://scicomp.stackexchange.com/a/2719/1262), and [*these*](http://stackoverflow.com/a/1779343/23771). – Mike Dunlavey Dec 19 '12 at 02:35
  • I was aware of part of but thank you for these thorough explanations! – jopasserat Dec 24 '12 at 21:01

1 Answers1

3

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
Jan
  • 11,636
  • 38
  • 47