1235368Sgnn#!/usr/sbin/dtrace -Zs
2235368Sgnn/*
3235368Sgnn * sh_stat.d - Bourne shell operation stats using DTrace.
4235368Sgnn *             Written for the sh DTrace provider.
5235368Sgnn *
6235368Sgnn * $Id: sh_stat.d 52 2007-09-24 04:28:01Z brendan $
7235368Sgnn *
8235368Sgnn * This traces activity from all sh processes on the system that are running
9235368Sgnn * with sh provider support.
10235368Sgnn *
11235368Sgnn * USAGE: sh_stat.d [interval [count]]
12235368Sgnn *
13235368Sgnn * FIELDS:
14235368Sgnn *		EXEC/s		Bourne shells executed per second, including
15235368Sgnn *				those without sh provider support
16235368Sgnn *		FUNC/s		Functions called, per second
17235368Sgnn *		BLTIN/s		Builtins called, per second
18235368Sgnn *		SUB-SH/s	Sub-shells called, per second
19235368Sgnn *		CMD/s		External commands called, per second
20235368Sgnn *
21235368Sgnn * The numbers are counts for the interval specified. The default interval
22235368Sgnn * is 1 second.
23235368Sgnn *
24235368Sgnn * If you see a count in "EXECS" but not in the other columns, then sh
25235368Sgnn * scripts may be running without the DTrace sh provider. See Shell/Readme.
26235368Sgnn *
27235368Sgnn * Filename and function names are printed if available.
28235368Sgnn *
29235368Sgnn * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
30235368Sgnn *
31235368Sgnn * CDDL HEADER START
32235368Sgnn *
33235368Sgnn *  The contents of this file are subject to the terms of the
34235368Sgnn *  Common Development and Distribution License, Version 1.0 only
35235368Sgnn *  (the "License").  You may not use this file except in compliance
36235368Sgnn *  with the License.
37235368Sgnn *
38235368Sgnn *  You can obtain a copy of the license at Docs/cddl1.txt
39235368Sgnn *  or http://www.opensolaris.org/os/licensing.
40235368Sgnn *  See the License for the specific language governing permissions
41235368Sgnn *  and limitations under the License.
42235368Sgnn *
43235368Sgnn * CDDL HEADER END
44235368Sgnn *
45235368Sgnn * 09-Sep-2007	Brendan Gregg	Created this.
46235368Sgnn */
47235368Sgnn
48235368Sgnn#pragma D option quiet
49235368Sgnn#pragma D option defaultargs
50235368Sgnn
51235368Sgnninline int SCREEN = 21;
52235368Sgnn
53235368Sgnndtrace:::BEGIN
54235368Sgnn{
55235368Sgnn	execs = funcs = builtins = subs = cmds = 0;
56235368Sgnn	lines = SCREEN + 1;
57235368Sgnn	interval = $1 ? $1 : 1;
58235368Sgnn	counts = $2 ? $2 : -1;
59235368Sgnn	secs = interval;
60235368Sgnn	first = 1;
61235368Sgnn}
62235368Sgnn
63235368Sgnnprofile:::tick-1sec
64235368Sgnn{
65235368Sgnn	secs--;
66235368Sgnn}
67235368Sgnn
68235368Sgnn/*
69235368Sgnn * Print Header
70235368Sgnn */
71235368Sgnndtrace:::BEGIN,
72235368Sgnnprofile:::tick-1sec
73235368Sgnn/first || (secs == 0 && lines > SCREEN)/
74235368Sgnn{
75235368Sgnn	printf("%-20s %8s %8s %8s %8s %8s\n", "TIME", "EXEC/s", "FUNCS/s",
76235368Sgnn	    "BLTINS/s", "SUB-SH/s", "CMD/s");
77235368Sgnn	lines = 0;
78235368Sgnn	first = 0;
79235368Sgnn}
80235368Sgnn
81235368Sgnn/*
82235368Sgnn * Tally Data
83235368Sgnn */
84235368Sgnnproc:::exec-success
85235368Sgnn/execname == "sh"/
86235368Sgnn{
87235368Sgnn	execs++;
88235368Sgnn}
89235368Sgnn
90235368Sgnnsh*:::function-entry
91235368Sgnn{
92235368Sgnn	funcs++;
93235368Sgnn}
94235368Sgnn
95235368Sgnnsh*:::builtin-entry
96235368Sgnn{
97235368Sgnn	builtins++;
98235368Sgnn}
99235368Sgnn
100235368Sgnnsh*:::subshell-entry
101235368Sgnn/arg0 != 0/
102235368Sgnn{
103235368Sgnn	subs++;
104235368Sgnn}
105235368Sgnn
106235368Sgnnsh*:::command-entry
107235368Sgnn{
108235368Sgnn	cmds++;
109235368Sgnn}
110235368Sgnn
111235368Sgnn/*
112235368Sgnn * Print Output
113235368Sgnn */
114235368Sgnnprofile:::tick-1sec
115235368Sgnn/secs == 0/
116235368Sgnn{
117235368Sgnn	printf("%-20Y %8d %8d %8d %8d %8d\n", walltimestamp, execs / interval,
118235368Sgnn	    funcs / interval, builtins / interval, subs / interval,
119235368Sgnn	    cmds / interval);
120235368Sgnn	execs = funcs = builtins = subs = cmds = 0;
121235368Sgnn	secs = interval;
122235368Sgnn	lines++;
123235368Sgnn	counts--;
124235368Sgnn}
125235368Sgnn
126235368Sgnn/*
127235368Sgnn * End
128235368Sgnn */
129235368Sgnnprofile:::tick-1sec
130235368Sgnn/counts == 0/
131235368Sgnn{
132235368Sgnn        exit(0);
133235368Sgnn}
134