summaryrefslogtreecommitdiffstats
path: root/cddl/contrib/dtracetoolkit/Disk/iopending
blob: ef9d4da7a625407f3b34dcab25d05f26fbd34c36 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/ksh
#
# iopending - Print a plot for the number of pending disk I/O events.
#             Written using DTrace (Solaris 10 3/05).
#
# This is measuring disk events that have made it past system caches.
# By plotting a distribution graph of the number of pending events, the
# "serialness" or "parallelness" of disk behaviour can be distinguished.
#
# $Id: iopending 3 2007-08-01 10:50:08Z brendan $
#
# USAGE:	iopending [-c] [-d device] [-f filename] 
#		          [-m mount_point] [interval [count]]
#
#		-c		# clear the screen
#		-d device	# instance name to snoop (eg, dad0)
#		-f filename	# full pathname of file to snoop
#		-m mount_point	# this FS only (will skip raw events)
#	eg,
#		iopending   	# default output, 5 second intervals
#		iopending 1  	# 1 second samples
#		iopending -c	# clear the screen
#		iopending 5 12	# print 12 x 5 second samples
# 	
# FIELDS:
#		value		number of pending events, 0 == idle
#		count		number of samples @ 1000 Hz
#		load		1 min load average
#		disk_r		total disk read Kbytes for sample
#		disk_w		total disk write Kbytes for sample
# 
# SEE ALSO: iosnoop, iotop
#
# IDEA: Dr Rex di Bona (Sydney, Australia)
#
# COPYRIGHT: Copyright (c) 2005, 2006 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
#
# Author: Brendan Gregg  [Sydney, Australia]
#
# 01-Nov-2005	Brendan Gregg	Created this.
# 20-Apr-2006	   "      "	Last update.
#


##############################
# --- Process Arguments ---
#

### default variables
opt_device=0; opt_file=0; opt_mount=0; opt_clear=0; 
opt_def=1; filter=0; device=.; filename=.; mount=.
interval=5; count=-1

### process options
while getopts cd:f:hm: name
do
	case $name in
	c)	opt_clear=1 ;;
	d)	opt_device=1; device=$OPTARG ;;
	f)	opt_file=1; filename=$OPTARG ;;
	m)	opt_mount=1; mount=$OPTARG ;;
	h|?)	cat <<-END >&2
		USAGE: iopending [-c] [-d device] [-f filename]
		                 [-m mount_point] [interval [count]]
 
		                -c              # clear the screen
		                -d device       # instance name to snoop 
		                -f filename     # snoop this file only
		                -m mount_point  # this FS only 
		   eg,
		        iopending         # default output, 5 second samples
		        iopending 1       # 1 second samples
		        iopending -m /    # snoop events on filesystem / only
		        iopending 5 12    # print 12 x 5 second samples
		END
		exit 1
	esac
done

shift $(( $OPTIND - 1 ))

### option logic
if [[ "$1" > 0 ]]; then
        interval=$1; shift
fi
if [[ "$1" > 0 ]]; then
        count=$1; shift
fi
if (( opt_device || opt_mount || opt_file )); then
	filter=1
fi
if (( opt_clear )); then
        clearstr=`clear`
else
        clearstr=.
fi



#################################
# --- Main Program, DTrace ---
#
/usr/sbin/dtrace -n '
 /*
  * Command line arguments
  */
 inline int OPT_def 	= '$opt_def';
 inline int OPT_clear 	= '$opt_clear';
 inline int OPT_device 	= '$opt_device';
 inline int OPT_mount 	= '$opt_mount';
 inline int OPT_file 	= '$opt_file';
 inline int INTERVAL 	= '$interval';
 inline int COUNTER 	= '$count';
 inline int FILTER 	= '$filter';
 inline string DEVICE 	= "'$device'";
 inline string FILENAME = "'$filename'";
 inline string MOUNT 	= "'$mount'";
 inline string CLEAR 	= "'$clearstr'";

 inline int MAX_PENDING = 32;	/* max pending value */
 
 #pragma D option quiet

 /*
  * Print header
  */
 dtrace:::BEGIN 
 {
        /* starting values */
        counts = COUNTER;
        secs = INTERVAL;
        disk_r = 0;
        disk_w = 0;
        pending = 0;

        printf("Tracing... Please wait.\n");
 }

 /*
  * Check event is being traced
  */
 io:genunix::start,
 io:genunix::done 
 { 
	/* default is to trace unless filtering, */
	this->ok = FILTER ? 0 : 1;

	/* check each filter, */
	(OPT_device == 1 && DEVICE == args[1]->dev_statname)? this->ok = 1 : 1;
	(OPT_file == 1 && FILENAME == args[2]->fi_pathname) ? this->ok = 1 : 1;
	(OPT_mount == 1 && MOUNT == args[2]->fi_mount)  ? this->ok = 1 : 1;
 }

 /*
  * Store entry details
  */
 io:genunix::start
 /this->ok/
 {
	/* track bytes */
	disk_r += args[0]->b_flags & B_READ ? args[0]->b_bcount : 0;
	disk_w += args[0]->b_flags & B_READ ? 0 : args[0]->b_bcount;

	/* increase event pending count */
	pending++;
 }

 /*
  * Process and Print completion
  */
 io:genunix::done
 /this->ok/
 {
	/* decrease event pending count */
	pending--;
 }

 /*
  * Prevent pending from underflowing
  * this can happen if this program is started during disk events.
  */
 io:genunix::done
 /pending < 0/
 {
	pending = 0;
 }

 /*
  * Timer
  */
 profile:::tick-1sec
 {
	secs--;
 }

 profile:::profile-1000hz
 {
	@out = lquantize(pending, 0, MAX_PENDING, 1);
 }

 /*
  * Print Report
  */
 profile:::tick-1sec
 /secs == 0/
 {
	/* fetch 1 min load average */
	this->load1a  = `hp_avenrun[0] / 65536;
	this->load1b  = ((`hp_avenrun[0] % 65536) * 100) / 65536;

	/* convert counters to Kbytes */
	disk_r /= 1024;
	disk_w /= 1024;

	/* print status */
	OPT_clear ? printf("%s", CLEAR) : 1;
	printf("%Y,  load: %d.%02d,  disk_r: %6d KB,  disk_w: %6d KB",
	    walltimestamp, this->load1a, this->load1b, disk_r, disk_w);

	/* print output */
	printa(@out);

	/* clear data */
	trunc(@out);
	disk_r = 0;
	disk_w = 0;
	secs = INTERVAL;
	counts--;
 }

 /*
  * End of program
  */
 profile:::tick-1sec
 /counts == 0/
 {
	exit(0);
 }

 /*
  * Cleanup for Ctrl-C
  */
 dtrace:::END
 {
	trunc(@out);
 }
'
OpenPOWER on IntegriCloud