Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  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

like image 300
superb Avatar asked Mar 08 '10 08:03

superb


People also ask

How to use callgrind?

To use Callgrind, you must specify --tool=callgrind on the Valgrind command line or use the supplied script callgrind . Callgrind's cache simulation is based on the Cachegrind tool of the Valgrind package.

How callgrind works?

Overview. Callgrind is a profiling tool that records the call history among functions in a program's run as a call-graph. By default, the collected data consists of the number of instructions executed, their relationship to source lines, the caller/callee relationship between functions, and the numbers of such calls.

What does Callgrind measure?

Callgrind records the count of instructions, not the actual time spent in a function. If you have a program where the bottleneck is file I/O, the costs associated with reading and writing files won't show up in the profile, as those are not CPU-intensive tasks.

What is IR in Callgrind?

Ir: The number of instructions executed in total by the selected function after being called by this caller.


3 Answers

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
like image 53
Doomsday Avatar answered Nov 01 '22 17:11

Doomsday


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.

like image 20
Ruslan Avatar answered Nov 01 '22 16:11

Ruslan


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.

like image 23
Paul R Avatar answered Nov 01 '22 16:11

Paul R