I would like to have a record of how much cpu time each function is using. The caveat here is that I only want to record the time used by the function itself, not the cpu time used by functions that are called by the function and which are also traced by my script. e.g. if both function foo and bar are traced by my script and the total cpu time for function foo is 2000, but function foo calls function bar three times, which costs 500 cpu time each, then I would like to see the following result:
function cputime call count
foo 500 1
bar 1500 3
Right now I have the following dtrace script to get the total cpu time per function, but I don't have any leads yet on how to change it such that I get the cpu time results as described above. (Note call count and output formatting is not yet in the script, but those are easy to add once I have the cpu time info that I am after.)
#!/usr/sbin/dtrace -s
pid$1:$2::entry
{
++self->call_depth[probefunc];
self->start[probefunc, self->call_depth[probefunc]] = timestamp;
self->vstart[probefunc, self->call_depth[probefunc]] = vtimestamp;
}
pid$1:$2::return
/self->start[probefunc, self->call_depth[probefunc]]/
{
@function_walltime[probefunc] = sum(timestamp - self->start[probefunc, self->call_depth[probefunc]]);
self->start[probefunc, self->call_depth[probefunc]] = 0;
@function_cputime[probefunc] = sum(vtimestamp - self->vstart[probefunc, self->call_depth[probefunc]]);
self->vstart[probefunc, self->call_depth[probefunc]] = 0;
--self->call_depth[probefunc];
}