-
Notifications
You must be signed in to change notification settings - Fork 188
Using DTrace
Watson1978 edited this page Apr 15, 2012
·
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..
Given a small script like this:
# test.rb
1000.times do
str = "abc\n" * 1000
str.split("\n")
endWe can create a DTrace script to trace the execution time of string methods:
/* 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);
}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