1235368Sgnn#!/usr/sbin/dtrace -Zs
2235368Sgnn/*
3235368Sgnn * rb_calltime.d - measure Ruby elapsed times for types of operation.
4235368Sgnn *                 Written for the Ruby DTrace provider.
5235368Sgnn *
6235368Sgnn * $Id: rb_calltime.d 41 2007-09-17 02:20:10Z brendan $
7235368Sgnn *
8235368Sgnn * This traces Ruby activity from all programs running on the system with
9235368Sgnn * Ruby provider support.
10235368Sgnn *
11235368Sgnn * USAGE: rb_calltime.d 	# hit Ctrl-C to end
12235368Sgnn *
13235368Sgnn * FIELDS:
14235368Sgnn *		FILE		Filename of the Ruby program
15235368Sgnn *		TYPE		Type of call (method/obj-new/gc/total)
16235368Sgnn *		NAME		Name of call
17235368Sgnn *		TOTAL		Total elapsed time for calls (us)
18235368Sgnn *
19235368Sgnn * Filename and method names are printed if available.
20235368Sgnn *
21235368Sgnn * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
22235368Sgnn *
23235368Sgnn * CDDL HEADER START
24235368Sgnn *
25235368Sgnn *  The contents of this file are subject to the terms of the
26235368Sgnn *  Common Development and Distribution License, Version 1.0 only
27235368Sgnn *  (the "License").  You may not use this file except in compliance
28235368Sgnn *  with the License.
29235368Sgnn *
30235368Sgnn *  You can obtain a copy of the license at Docs/cddl1.txt
31235368Sgnn *  or http://www.opensolaris.org/os/licensing.
32235368Sgnn *  See the License for the specific language governing permissions
33235368Sgnn *  and limitations under the License.
34235368Sgnn *
35235368Sgnn * CDDL HEADER END
36235368Sgnn *
37235368Sgnn * 09-Sep-2007	Brendan Gregg	Created this.
38235368Sgnn */
39235368Sgnn
40235368Sgnn#pragma D option quiet
41235368Sgnn
42235368Sgnndtrace:::BEGIN
43235368Sgnn{
44235368Sgnn	printf("Tracing... Hit Ctrl-C to end.\n");
45235368Sgnn}
46235368Sgnn
47235368Sgnnruby*:::function-entry
48235368Sgnn{
49235368Sgnn	self->depth++;
50235368Sgnn	self->exclude[self->depth] = 0;
51235368Sgnn	self->function[self->depth] = timestamp;
52235368Sgnn}
53235368Sgnn
54235368Sgnnruby*:::function-return
55235368Sgnn/self->function[self->depth]/
56235368Sgnn{
57235368Sgnn	this->elapsed_incl = timestamp - self->function[self->depth];
58235368Sgnn	this->elapsed_excl = this->elapsed_incl - self->exclude[self->depth];
59235368Sgnn	self->function[self->depth] = 0;
60235368Sgnn	self->exclude[self->depth] = 0;
61235368Sgnn	this->file = basename(copyinstr(arg2));
62235368Sgnn	this->name = strjoin(strjoin(copyinstr(arg0), "::"), copyinstr(arg1));
63235368Sgnn
64235368Sgnn	@num[this->file, "func", this->name] = count();
65235368Sgnn	@num["-", "total", "-"] = count();
66235368Sgnn	@types_incl[this->file, "func", this->name] = sum(this->elapsed_incl);
67235368Sgnn	@types_excl[this->file, "func", this->name] = sum(this->elapsed_excl);
68235368Sgnn	@types_excl["-", "total", "-"] = sum(this->elapsed_excl);
69235368Sgnn
70235368Sgnn	self->depth--;
71235368Sgnn	self->exclude[self->depth] += this->elapsed_incl;
72235368Sgnn}
73235368Sgnn
74235368Sgnnruby*:::object-create-start
75235368Sgnn{
76235368Sgnn	self->object = timestamp;
77235368Sgnn}
78235368Sgnn
79235368Sgnnruby*:::object-create-done
80235368Sgnn/self->object/
81235368Sgnn{
82235368Sgnn	this->elapsed = timestamp - self->object;
83235368Sgnn	self->object = 0;
84235368Sgnn	this->file = basename(copyinstr(arg1));
85235368Sgnn	this->file = this->file != NULL ? this->file : ".";
86235368Sgnn	this->name = copyinstr(arg0);
87235368Sgnn
88235368Sgnn	@num[this->file, "obj-new", this->name] = count();
89235368Sgnn	@types[this->file, "obj-new", this->name] = sum(this->elapsed);
90235368Sgnn
91235368Sgnn	self->exclude[self->depth] += this->elapsed;
92235368Sgnn}
93235368Sgnn
94235368Sgnnruby*:::gc-begin
95235368Sgnn{
96235368Sgnn	self->gc = timestamp;
97235368Sgnn}
98235368Sgnn
99235368Sgnnruby*:::gc-end
100235368Sgnn/self->gc/
101235368Sgnn{
102235368Sgnn	this->elapsed = timestamp - self->gc;
103235368Sgnn	self->gc = 0;
104235368Sgnn	@num[".", "gc", "-"] = count();
105235368Sgnn	@types[".", "gc", "-"] = sum(this->elapsed);
106235368Sgnn	self->exclude[self->depth] += this->elapsed;
107235368Sgnn}
108235368Sgnn
109235368Sgnndtrace:::END
110235368Sgnn{
111235368Sgnn	printf("\nCount,\n");
112235368Sgnn	printf("   %-20s %-10s %-32s %8s\n", "FILE", "TYPE", "NAME", "COUNT");
113235368Sgnn	printa("   %-20s %-10s %-32s %@8d\n", @num);
114235368Sgnn
115235368Sgnn	normalize(@types, 1000);
116235368Sgnn	printf("\nElapsed times (us),\n");
117235368Sgnn	printf("   %-20s %-10s %-32s %8s\n", "FILE", "TYPE", "NAME", "TOTAL");
118235368Sgnn	printa("   %-20s %-10s %-32s %@8d\n", @types);
119235368Sgnn
120235368Sgnn	normalize(@types_excl, 1000);
121235368Sgnn	printf("\nExclusive function elapsed times (us),\n");
122235368Sgnn	printf("   %-20s %-10s %-32s %8s\n", "FILE", "TYPE", "NAME", "TOTAL");
123235368Sgnn	printa("   %-20s %-10s %-32s %@8d\n", @types_excl);
124235368Sgnn
125235368Sgnn	normalize(@types_incl, 1000);
126235368Sgnn	printf("\nInclusive function elapsed times (us),\n");
127235368Sgnn	printf("   %-20s %-10s %-32s %8s\n", "FILE", "TYPE", "NAME", "TOTAL");
128235368Sgnn	printa("   %-20s %-10s %-32s %@8d\n", @types_incl);
129235368Sgnn}
130