2

I am trying to figure out how does program profiling work. I am using Valgrind. My first question is:

What does the cost of a function mean for Valgrind? Is is time?

From what I read, it seems that Valgrind runs the program on a virtual machine that is supposed to mirror a "generic computer". It then counts events occuring in this machine. But how does it compute the cost of a function from this data? Can times smaller than 1 millisecond be measured on a standard desktop PC?

enter image description here

Edit:

Please what does the 1 dimensional number "cost" mean in the output of callgrind?

Martin Drozdik
  • 12,742
  • 22
  • 81
  • 146

2 Answers2

7

What does the cost of a function mean for Valgrind? Is is time?

Assuming you're referring to the Cachegrind and Callgrind tools, they don't measure "cost"; they measure specific statistics gathered from running the program on a simulated processor including:

  • Cache reads and misses
  • Conditional and indirect branches executed and mispredicted

For full details, see the documentation.

Other profilers measure the elapsed time for each function, or use sampling to determine which functions the program spends the most time in.

Can times smaller than 1 millisecond be measured on a standard desktop PC?

Yes, most modern computers have timers with an accuracy of less than a microsecond, and modern Intel processors allow you to count CPU cycles. But Valgrind doesn't measure elapsed time, since that's meaningless on its simulated machine.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Thank you! So if I understand correctly, in the case of the output in my post, the profiler measures the number of instructions executed, but is not concerned with the cost of the instruction. Is this so? – Martin Drozdik Jan 24 '13 at 17:06
  • 1
    @MartinDrozdik: Indeed; it looks like your output program uses the word "cost" to mean "number of events", where the event type is given in the first column. So 1531843733 instructions were read during execution of that function. – Mike Seymour Jan 24 '13 at 17:08
  • 1
    The documentation for Callgrind says that the cost is "event counts (data reads, cache misses, etc.)". Self cost are the costs incurred only in that function; inclusive costs include all costs that your function call. ([documentation](http://valgrind.org/docs/manual/cl-manual.html#cl-manual.functionality)) – MonkeyWithDarts Jan 24 '13 at 17:08
1

Yes, time can be measured, less than 1 ms on a standard desktop PC(if you mean that has Intel processors). There is an assembly instruction called rtdsc that does this.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78