Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Using DTrace

Watson1978 edited this page · 2 revisions

DTrace is a dynamic tracing framework which allows you to probe applications without modifying the application itself. MacRuby provides some probes for DTrace so that method behaviors can be traced.

Probe Name Call timing arg0 arg1 arg2 arg3
method-entry At the start of the method Class name Method name File name Line number
method-return At the end of the method Class name Method name File name Line number
raise An exception raises Class name File name Line number ---

Using these probes, we could easily trace the number of times that a method is called, its execution time, etc..

Example Usage

Given a small script like this:

# test.rb

1000.times do
  str = "abc\n" * 1000

We can create a DTrace script to trace the execution time of string methods:

/* time.d */

#pragma D option quiet

/ copyinstr(arg0) == "String" /  /* invokes only String methods */
    self->starttime = walltimestamp / 1000;

/ copyinstr(arg0) == "String" /  /* invokes only String methods */
    @invoked_time[copyinstr(arg0), copyinstr(arg1)] = sum((walltimestamp / 1000) - self->starttime);

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

And then to perform the tracing, we would run dtrace and launch MacRuby:

$ sudo dtrace -qs time.d  -c "macruby test.rb"

CLASS       METHOD           TOTAL TIME µsec
String      *:               6578
String      split:           1275979
Something went wrong with that request. Please try again.