How use callgrind to profiling only a certain period of program execution?

superb picture superb · Mar 8, 2010 · Viewed 12.3k times · Source

I want to use valgrind to do some profiling, since it does not need re-build the program. (the program I want to profile is already build with “-g")

But valgrind(callgrind) is quite slow ... so here's what I to do:

  1. start the server ( I want to profile that server)
  2. kind of attach to that server
  3. before I do some operation on server, start collect profile data
  4. after the operation is done, end collecting profile data
  5. analyze the profiling data.

I can do this kind of thing using sun studio on Solaris. (using dbx ). I just want to know is it possible to do the same thing using valgrind(callgrind)?

Thanks

Answer

Doomsday picture Doomsday · Jul 21, 2010

You should look at callgrind documentation, and read about callgrind_control.

  1. Launch your app : valgrind --tool=callgrind --instr-atstart=no your_server.x
  2. See 1.
  3. start collect profile data: callgrind_control -i on
  4. end collect profile data: callgrind_control -i off
  5. Analyze data with kcachegrind or callgrind_annotate/cg_annotate