xref: /netbsd-src/external/cddl/dtracetoolkit/dist/Shell/sh_cpudist.d (revision c29d51755812ace2e87aeefdb06cb2b4dac7087a)
1 #!/usr/sbin/dtrace -Zs
2 /*
3  * sh_cpudist.d - measure Bourne shell on-CPU times for types of operation.
4  *                Written for the sh DTrace provider.
5  *
6  * $Id: sh_cpudist.d,v 1.1.1.1 2015/09/30 22:01:09 christos Exp $
7  *
8  * This traces shell activity from all Bourne shells on the system that are
9  * running with sh provider support.
10  *
11  * USAGE: sh_cpudist.d 		# hit Ctrl-C to end
12  *
13  * This script prints distribution plots of on-CPU time for shell
14  * operations. Use sh_cputime.d for summary reports.
15  *
16  * FIELDS:
17  *		1		Filename of the shell or shellscript
18  *		2		Type of call (func/builtin/cmd)
19  *		3		Name of call
20  *
21  * Filename and call names are printed if available.
22  *
23  * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
24  *
25  * CDDL HEADER START
26  *
27  *  The contents of this file are subject to the terms of the
28  *  Common Development and Distribution License, Version 1.0 only
29  *  (the "License").  You may not use this file except in compliance
30  *  with the License.
31  *
32  *  You can obtain a copy of the license at Docs/cddl1.txt
33  *  or http://www.opensolaris.org/os/licensing.
34  *  See the License for the specific language governing permissions
35  *  and limitations under the License.
36  *
37  * CDDL HEADER END
38  *
39  * 09-Sep-2007	Brendan Gregg	Created this.
40  */
41 
42 #pragma D option quiet
43 
44 dtrace:::BEGIN
45 {
46 	printf("Tracing... Hit Ctrl-C to end.\n");
47 }
48 
49 sh*:::function-entry
50 {
51 	self->depth++;
52 	self->function[self->depth] = vtimestamp;
53 	self->exclude[self->depth] = 0;
54 }
55 
56 sh*:::function-return
57 /self->function[self->depth]/
58 {
59 	this->oncpu_incl = vtimestamp - self->function[self->depth];
60 	this->oncpu_excl = this->oncpu_incl - self->exclude[self->depth];
61 	self->function[self->depth] = 0;
62 	self->exclude[self->depth] = 0;
63 	this->file = basename(copyinstr(arg0));
64 	this->name = copyinstr(arg1);
65 
66 	@types_incl[this->file, "func", this->name] =
67 	    quantize(this->oncpu_incl / 1000);
68 	@types_excl[this->file, "func", this->name] =
69 	    quantize(this->oncpu_excl / 1000);
70 
71 	self->depth--;
72 	self->exclude[self->depth] += this->oncpu_incl;
73 }
74 
75 sh*:::builtin-entry
76 {
77 	self->builtin = vtimestamp;
78 }
79 
80 sh*:::builtin-return
81 /self->builtin/
82 {
83 	this->oncpu = vtimestamp - self->builtin;
84 	self->builtin = 0;
85 
86 	@types[basename(copyinstr(arg0)), "builtin", copyinstr(arg1)] =
87 	    quantize(this->oncpu / 1000);
88 
89 	self->exclude[self->depth] += this->oncpu;
90 }
91 
92 sh*:::command-entry
93 {
94 	incmd[pid] = basename(copyinstr(arg0));
95 	depth[pid] = self->depth;
96 }
97 
98 sh*:::command-return
99 {
100 	incmd[pid] = 0;
101 }
102 
103 proc:::exec-success
104 {
105 	/*
106 	 * Due to thread timing after fork(), this probe can fire before
107 	 * sh*:::command-entry has, which means we can't predicate this
108 	 * exec() away just yet. Store the vtimestamp in case it is needed.
109 	 */
110 	self->command = vtimestamp;
111 }
112 
113 proc:::exit
114 /incmd[ppid] == NULL/
115 {
116 	self->command = 0;
117 }
118 
119 proc:::exit
120 /incmd[ppid] != NULL/
121 {
122 	this->oncpu = vtimestamp - self->command;
123 	self->command = 0;
124 
125 	@types[incmd[ppid], "cmd", execname] = quantize(this->oncpu / 1000);
126 
127 	self->exclude[depth[ppid]] += this->oncpu;
128 	incmd[ppid] = 0;
129 	depth[ppid] = 0;
130 }
131 
132 dtrace:::END
133 {
134 	printf("On-CPU times (us),\n\n");
135 	printa("   %s, %s, %s %@d\n", @types);
136 
137 	printf("Exclusive function on-CPU times (us),\n\n");
138 	printa("   %s, %s, %s %@d\n", @types_excl);
139 
140 	printf("Inclusive function on-CPU times (us),\n\n");
141 	printa("   %s, %s, %s %@d\n", @types_incl);
142 }
143