How use callgrind to profiling only a certain period of program execution?
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:
- start the server ( I want to profile that server)
- kind of attach to that server
- before I do some operation on server, start collect profile data
- after the operation is done, end collecting profile data
- 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
You should look at callgrind documentation, and read about callgrind_control.
- Launch your app :
valgrind --tool=callgrind --instr-atstart=no your_server.x
- See 1.
- start collect profile data:
callgrind_control -i on
- end collect profile data:
callgrind_control -i off
- Analyze data with kcachegrind or callgrind_annotate/cg_annotate
For profiling only some function you can also find useful CALLGRIND_START_INSTRUMENTATION
and CALLGRIND_STOP_INSTRUMENTATION
from <valgrind/callgrind.h>
header and using callgrind's --instr-atstart=no
option as suggested in Doomsday's answer.
You don't say what OS - I'm assuming Linux - in which case you might want to look at oprofile (free) or Zoom (not free, but you can get an evaluation licence), both of which are sampling profilers and can profile existing code without re-compilation. Zoom is much nicer and easier to use (it has a GUI and some nice additional features), but you probably already have oprofile on your system.
精彩评论