Sunday, February 1, 2015

DTracing time spent in routines called by a specific function

Here's a quick DTrace script to average out the time spent in each routine called by a given function:
#!/usr/sbin/dtrace -s

pid$target:a.out:$1:entry
{
        self->trace = 1;
}

pid$target:a.out::entry
/self->trace/

{
        self->ts[probefunc] = timestamp;


pid$target:a.out::return

/self->ts[probefunc] != 0/
{
        @[probefunc] = avg(timestamp - self->ts[probefunc]);
        self->ts[probefunc] = 0;
}

pid$target:a.out:$1:return
/self->trace/
{
        self->trace = 0;
        exit(0);
}
Here's a sample output:
# dtrace -s ./avg_time.d -c ./foo main 
   CPU         ID                    FUNCTION:NAME
     7      67401                      main:return

  bar                                     10085098
  foo                                   1009412287
  main                                  1009420500


This little program has main() calling foo(), which invokes bar() a number of times. The output above shows the two first routines are taking most of the time, as expected.

No comments:

Post a Comment