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