summaryrefslogtreecommitdiffstats
path: root/cddl/contrib/dtracetoolkit/Shell/sh_wasted.d
blob: e20db8ea6efe8ee2c77af49e8eb7890fe8c345f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/sbin/dtrace -Zs
/*
 * sh_wasted.d - measure Bourne shell elapsed times for "wasted" commands.
 *               Written for the sh DTrace provider.
 *
 * $Id: sh_wasted.d 25 2007-09-12 09:51:58Z brendan $
 *
 * USAGE: sh_wasted.d { -p PID | -c cmd }	# hit Ctrl-C to end
 *
 * This script measures "wasted" commands - those which are called externally
 * but are in fact builtins to the shell. Ever seen a script which calls
 * /usr/bin/echo needlessly? This script measures that cost.
 *
 * FIELDS:
 *		FILE		Filename of the shell or shellscript
 *		NAME		Name of call
 *		TIME		Total elapsed time for calls (us)
 *
 * IDEA: Mike Shapiro
 *
 * Filename and call names are printed if available.
 *
 * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
 *
 * CDDL HEADER START
 *
 *  The contents of this file are subject to the terms of the
 *  Common Development and Distribution License, Version 1.0 only
 *  (the "License").  You may not use this file except in compliance
 *  with the License.
 *
 *  You can obtain a copy of the license at Docs/cddl1.txt
 *  or http://www.opensolaris.org/os/licensing.
 *  See the License for the specific language governing permissions
 *  and limitations under the License.
 *
 * CDDL HEADER END
 *
 * 09-Sep-2007	Brendan Gregg	Created this.
 */

#pragma D option quiet

dtrace:::BEGIN
{
	isbuiltin["echo"] = 1;
	isbuiltin["test"] = 1;
	/* add builtins here */

	printf("Tracing... Hit Ctrl-C to end.\n");
	self->start = timestamp;
}

sh$target:::command-entry
{
	self->command = timestamp;
}

sh$target:::command-return
{
	this->elapsed = timestamp - self->command;
	this->path = copyinstr(arg1);
	this->cmd = basename(this->path);
}

sh$target:::command-return
/self->command && !isbuiltin[this->cmd]/
{
	@types_cmd[basename(copyinstr(arg0)), this->path] = sum(this->elapsed);
	self->command = 0;
}

sh$target:::command-return
/self->command/
{
	@types_wasted[basename(copyinstr(arg0)), this->path] =
	    sum(this->elapsed);
	self->command = 0;
}

proc:::exit
/pid == $target/
{
	exit(0);
}

dtrace:::END
{
	this->elapsed = (timestamp - self->start) / 1000;
	printf("Script duration: %d us\n", this->elapsed);

	normalize(@types_cmd, 1000);
	printf("\nExternal command elapsed times,\n");
	printf("   %-30s %-22s %8s\n", "FILE", "NAME", "TIME(us)");
	printa("   %-30s %-22s %@8d\n", @types_cmd);

	normalize(@types_wasted, 1000);
	printf("\nWasted command elapsed times,\n");
	printf("   %-30s %-22s %8s\n", "FILE", "NAME", "TIME(us)");
	printa("   %-30s %-22s %@8d\n", @types_wasted);
}
OpenPOWER on IntegriCloud