I tried to test my cpp code using gprof on ubuntu.
But I found some bug.
When gprof calculates execution time, the minimum time unit is 0.01 seconds.
For example, if execution time of my function in my program is 0.001 or even more faster, gprof recognizes as 0 seconds.
Even if i execute my function thousand time, it calculate like this: 0/s + 0/s …. + 0/s = 0/s
but real running time is 1 seconds…
So, I want to know how to modify the minimum time unit or calculate exact execution time.
Please help me :)
And i don’t need any recommendation of other profiling tool
This question is almost a duplicate of inaccuracy in gprof output, but with a minor difference: it looks like it tries to find the performance bottleneck in the wrong place:
Even if i execute my function thousand time, it calculate like this: 0/s + 0/s …. + 0/s = 0/s
This is not how gprof works. Gprof samples the program counter once in T (normally 0.01 seconds). It does not just sum up time measurements, but rather relies on statistics. The chances are pretty low that a program that takes 1.00 CPU is never sampled out of the approximately 100 samples it should get. 80 samples is possible, 120 is possible, 0 is virtually impossible. So your problem lies elsewhere.
Gprof has many limitations, as can be seen at inaccuracy in gprof output. The real problem is either that the time is spent in I/O, has a complicated mutual recursion, in a shared library, or it tries to reuse the same signals that gprof uses to sample the code.
If you still insist on changing the sampling rate, then it seems possible in theory but it is too complicated to be worth it. There have been claims that rewriting the profil()
or monstartup()
functions. You can override them using linker facilities such as LD_PRELOAD. Given the limitations of gprof, this path is not worth while, and I could not see any reference to code that actually did that.
Here is a quote by Nick Clifton on the matter:
So your choices are:
- Alter the profil() function in your OS.
- Write your own monstartup() function and find some other way of generating the time samples.
I have tried to modify the intervals by hacking SIGPROF interval:
void set_interval(double seconds)
{
if (seconds <= 0)
return;
itimerval prev, next;
next.it_value.tv_sec = (uint64_t) seconds;
next.it_value.tv_usec = (uint64_t)(1000000 * (seconds - next.it_value.tv_sec));
next.it_interval = next.it_value;
setitimer(ITIMER_PROF, &next, &prev);
}
On the Linux I have tried, set_interval(0.1)
from main does change the time interval to 1/10 of a second (but reports that wrongly in the gprof output). But running set_interval(0.001)
has no effect on my machine, since the finest granularity is set to 10 ms. Anything below 10ms is increased to 10ms internally. To overcome this limitation, please read 1ms resolution timer under linux recommended way.
This is getting so ridicules, that I strongly suggest you should give this route up and look for a different profiler, or find why gprof does not work for you as it is.