1#!/usr/sbin/dtrace -CZs
2/*
3 * tcl_calldist.d - measure Tcl elapsed time for different types of operation.
4 *                  Written for the Tcl DTrace provider.
5 *
6 * $Id: tcl_calldist.d 63 2007-10-04 04:34:38Z brendan $
7 *
8 * USAGE: tcl_calldist.d [top]	# hit Ctrl-C to end
9 *    eg,
10 *        tcl_calldist.d	# default, truncate to 10 lines
11 *        tcl_calldist.d 25	# truncate each report section to 25 lines
12 *
13 * This traces activity from all Tcl processes on the system with DTrace
14 * provider support (tcl8.4.16).
15 *
16 * FIELDS:
17 *		1		Process ID
18 *		2		Type of call (proc/cmd/total)
19 *		3		Name of call
20 *
21 * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
22 *
23 * CDDL HEADER START
24 *
25 *  The contents of this file are subject to the terms of the
26 *  Common Development and Distribution License, Version 1.0 only
27 *  (the "License").  You may not use this file except in compliance
28 *  with the License.
29 *
30 *  You can obtain a copy of the license at Docs/cddl1.txt
31 *  or http://www.opensolaris.org/os/licensing.
32 *  See the License for the specific language governing permissions
33 *  and limitations under the License.
34 *
35 * CDDL HEADER END
36 *
37 * 09-Sep-2007	Brendan Gregg	Created this.
38 */
39
40#define TOP	10		/* default output truncation */
41#define B_FALSE	0
42
43#pragma D option quiet
44#pragma D option defaultargs
45
46dtrace:::BEGIN
47{
48	printf("Tracing... Hit Ctrl-C to end.\n");
49	top = $1 != 0 ? $1 : TOP;
50}
51
52tcl*:::proc-entry
53{
54	self->depth++;
55	self->exclude[self->depth] = 0;
56	self->proc[self->depth] = timestamp;
57}
58
59tcl*:::proc-return
60/self->proc[self->depth]/
61{
62	this->elapsed_incl = timestamp - self->proc[self->depth];
63	this->elapsed_excl = this->elapsed_incl - self->exclude[self->depth];
64	self->proc[self->depth] = 0;
65	self->exclude[self->depth] = 0;
66	this->name = copyinstr(arg0);
67
68	@types_incl[pid, "proc", this->name] =
69	    quantize(this->elapsed_incl / 1000);
70	@types_excl[pid, "proc", this->name] =
71	    quantize(this->elapsed_excl / 1000);
72
73	self->depth--;
74	self->exclude[self->depth] += this->elapsed_incl;
75}
76
77tcl*:::cmd-entry
78{
79	self->depth++;
80	self->exclude[self->depth] = 0;
81	self->cmd[self->depth] = timestamp;
82}
83
84tcl*:::cmd-return
85/self->cmd[self->depth]/
86{
87	this->elapsed_incl = timestamp - self->cmd[self->depth];
88	this->elapsed_excl = this->elapsed_incl - self->exclude[self->depth];
89	self->cmd[self->depth] = 0;
90	self->exclude[self->depth] = 0;
91	this->name = copyinstr(arg0);
92
93	@types_incl[pid, "cmd", this->name] =
94	    quantize(this->elapsed_incl / 1000);
95	@types_excl[pid, "cmd", this->name] =
96	    quantize(this->elapsed_excl / 1000);
97
98	self->depth--;
99	self->exclude[self->depth] += this->elapsed_incl;
100}
101
102dtrace:::END
103{
104	trunc(@types_excl, top);
105	printf("\nTop %d exclusive elapsed times (us),\n", top);
106	printa("   PID=%d, %s, %s %@d\n", @types_excl);
107
108	trunc(@types_incl, top);
109	printf("\nTop %d inclusive elapsed times (us),\n", top);
110	printa("   PID=%d, %s, %s %@d\n", @types_incl);
111}
112