MacRuby DoJo (道場)

Using the DTrace

DTrace is a dynamic tracing framework, and you may trace application without changing by DTrace. MacRuby provides some probe for DTrace to trace the methods behavior.

Probe NameCalling timingarg0arg1arg2arg3
method-entryAt the start of the methodClass nameMethod nameFile nameLine number
method-returnAt the end of the methodClass nameMethod nameFile nameLine number
raiseAn exception raisesClass nameFile nameLine number-

Using these probes, you may easily trace the count of method calling or trace the execution time.

Here is DTrace script which trace the total execution time of String methods.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* time.d */

#pragma D option quiet

macruby$target:::method-entry
/ copyinstr(arg0) == "String" /  /* invokes only String methods */
{
  self->starttime = walltimestamp / 1000;
}

macruby$target:::method-return
/ copyinstr(arg0) == "String" /  /* invokes only String methods */
{
  @invoked_time[copyinstr(arg0), copyinstr(arg1)] = sum((walltimestamp / 1000) - self->starttime);
}

END
{
    printf("\n");
    printf("%-10s  %-15s  %s\n", "CLASS", "METHOD", "TOTAL TIME µsec");
    printf("--------------------------------------------------------------------------------\n");
    printa("%-10s  %-15s  %@d\n", @invoked_time);
}

Prepare your application to trace the execution time.

1
2
3
4
5
6
# test.rb

1000.times do
  str = "abc\n" * 1000
  str.split("\n")
end

You may retrieve the result as following.

1
2
3
4
5
6
$ sudo dtrace -qs time.d  -c "macruby test.rb"

CLASS       METHOD           TOTAL TIME µsec
--------------------------------------------------------------------------------
String      *:               6578
String      split:           1275979

Comments