Reputation: 169
I need to profile a couple of C codes and get an annotated file with percentage of exeuction time taken by each line or atleast each block(while/if-else/for-functions) etc.
So far I have looked into valgrind(callgrind) and gperf and some other tools. So far what I get is
What I do need however if percentage of execution time not the count and that should be for each line of source code or atleast all blocks(while/if-else/for-functions).
Can someone let me know of a way I can do it ?
Thanks,
Upvotes: 2
Views: 3211
Reputation: 40679
You need something that samples the stack, either on CPU time, or wall-clock time if you want to include I/O. You don't need particularly high frequency of sampling.
Each stack sample is a list of code locations, some of which can be traced to lines in the code.
The inclusive percentage cost of a line of code is just the number of stack samples that contain it, divided by the total number of stack samples (multiplied by 100).
The usefulness of this is it tells you what percentage of total time you could save if you could get rid of that line.
The exclusive percentage is the fraction of samples where that line appears at the end.
If a line of code appears more than once in a stack sample, that's still just one sample containing it. (That takes care of recursion.)
What tools can do this? Maybe oprofile
. Certainly Zoom
.
Upvotes: 0
Reputation: 4762
I believe perf(1)
(part of the linux-tools-common
package in Ubuntu) will get you what you want. It makes use of a kernel-based subsystem called Performance counters for Linux, included in newer kernels. More information can be found here.
Simple usage example below. Make sure to compile with debugging symbols.
$ perf record ./myprogram arg1 arg2
$ perf report
Cachegrind might be worth looking into too.
Upvotes: 1