1235368Sgnn#!/usr/sbin/dtrace -Zs
2235368Sgnn/*
3235368Sgnn * sh_calldist.d - measure Bourne shell elapsed times for types of operation.
4235368Sgnn *                 Written for the sh DTrace provider.
5235368Sgnn *
6235368Sgnn * $Id: sh_calldist.d 28 2007-09-13 10:49:37Z brendan $
7235368Sgnn *
8235368Sgnn * This traces shell activity from all Bourne shells on the system that are
9235368Sgnn * running with sh provider support.
10235368Sgnn *
11235368Sgnn * USAGE: sh_calldist.d 	# hit Ctrl-C to end
12235368Sgnn *
13235368Sgnn * This script prints distribution plots of elapsed time for shell
14235368Sgnn * operations. Use sh_calltime.d for summary reports.
15235368Sgnn *
16235368Sgnn * FIELDS:
17235368Sgnn *		1		Filename of the shell or shellscript
18235368Sgnn *		2		Type of call (func/builtin/cmd)
19235368Sgnn *		3		Name of call
20235368Sgnn *
21235368Sgnn * Filename and call 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
49235368Sgnnsh*:::function-entry
50235368Sgnn{
51235368Sgnn	self->depth++;
52235368Sgnn	self->exclude[self->depth] = 0;
53235368Sgnn	self->function[self->depth] = timestamp;
54235368Sgnn}
55235368Sgnn
56235368Sgnnsh*:::function-return
57235368Sgnn/self->function[self->depth]/
58235368Sgnn{
59235368Sgnn	this->elapsed_incl = timestamp - self->function[self->depth];
60235368Sgnn	this->elapsed_excl = this->elapsed_incl - self->exclude[self->depth];
61235368Sgnn	self->function[self->depth] = 0;
62235368Sgnn	self->exclude[self->depth] = 0;
63235368Sgnn	this->file = basename(copyinstr(arg0));
64235368Sgnn	this->name = copyinstr(arg1);
65235368Sgnn
66235368Sgnn	@types_incl[this->file, "func", this->name] =
67235368Sgnn	    quantize(this->elapsed_incl / 1000);
68235368Sgnn	@types_excl[this->file, "func", this->name] =
69235368Sgnn	    quantize(this->elapsed_excl / 1000);
70235368Sgnn
71235368Sgnn	self->depth--;
72235368Sgnn	self->exclude[self->depth] += this->elapsed_incl;
73235368Sgnn}
74235368Sgnn
75235368Sgnnsh*:::builtin-entry
76235368Sgnn{
77235368Sgnn	self->builtin = timestamp;
78235368Sgnn}
79235368Sgnn
80235368Sgnnsh*:::builtin-return
81235368Sgnn/self->builtin/
82235368Sgnn{
83235368Sgnn	this->elapsed = timestamp - self->builtin;
84235368Sgnn	self->builtin = 0;
85235368Sgnn
86235368Sgnn	@types[basename(copyinstr(arg0)), "builtin", copyinstr(arg1)] =
87235368Sgnn	    quantize(this->elapsed / 1000);
88235368Sgnn
89235368Sgnn	self->exclude[self->depth] += this->elapsed;
90235368Sgnn}
91235368Sgnn
92235368Sgnnsh*:::command-entry
93235368Sgnn{
94235368Sgnn	self->command = timestamp;
95235368Sgnn}
96235368Sgnn
97235368Sgnnsh*:::command-return
98235368Sgnn/self->command/
99235368Sgnn{
100235368Sgnn	this->elapsed = timestamp - self->command;
101235368Sgnn	self->command = 0;
102235368Sgnn
103235368Sgnn	@types[basename(copyinstr(arg0)), "cmd", copyinstr(arg1)] =
104235368Sgnn	    quantize(this->elapsed / 1000);
105235368Sgnn
106235368Sgnn	self->exclude[self->depth] += this->elapsed;
107235368Sgnn}
108235368Sgnn
109235368Sgnndtrace:::END
110235368Sgnn{
111235368Sgnn	printf("Elapsed times (us),\n\n");
112235368Sgnn	printa("   %s, %s, %s %@d\n", @types);
113235368Sgnn
114235368Sgnn	printf("Exclusive function elapsed times (us),\n\n");
115235368Sgnn	printa("   %s, %s, %s %@d\n", @types_excl);
116235368Sgnn
117235368Sgnn	printf("Inclusive function elapsed times (us),\n\n");
118235368Sgnn	printa("   %s, %s, %s %@d\n", @types_incl);
119235368Sgnn}
120