aboutsummaryrefslogtreecommitdiff
path: root/cddl/contrib/dtracetoolkit/iopattern
blob: e825f9f77fbad717975032854e306442a41f06df (plain) (blame)
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/ksh
#
# iopattern - print disk I/O pattern.
#             Written using DTrace (Solaris 10 3/05).
#
# This prints details on the I/O access pattern for the disks, such as
# percentage of events that were of a random or sequential nature.
# By default totals for all disks are printed.
#
# $Id: iopattern 65 2007-10-04 11:09:40Z brendan $
#
# USAGE:	iopattern [-v] [-d device] [-f filename] [-m mount_point] 
#			  [interval [count]]
#
#		       -v       	# print timestamp, string
#		       -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,
#		iopattern   	# default output, 1 second intervals
#		iopattern 10  	# 10 second samples
#		iopattern 5 12	# print 12 x 5 second samples
#	        iopattern -m /  # snoop events on filesystem / only
# 	
# FIELDS:
#		%RAN  		percentage of events of a random nature
#		%SEQ 	 	percentage of events of a sequential nature
#		COUNT		number of I/O events
#		MIN		minimum I/O event size
#		MAX		maximum I/O event size
#		AVG		average I/O event size
#		KR		total kilobytes read during sample
#		KW		total kilobytes written during sample
#		DEVICE		device name
#		MOUNT		mount point
#		FILE		filename
#		TIME		timestamp, string
# 
# NOTES:
#
#  An event is considered random when the heads seek. This program prints
#  the percentage of events that are random. The size of the seek is not
#  measured - it's either random or not.
#
# SEE ALSO: iosnoop, iotop
# 
# IDEA: Ryan Matteson
#
# COPYRIGHT: Copyright (c) 2005 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]
#
# 25-Jul-2005	Brendan Gregg	Created this.
# 25-Jul-2005	   "      "	Last update.
#


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

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

### process options
while getopts d:f:hm:v name
do
	case $name in
	d)	opt_device=1; device=$OPTARG ;;
	f)	opt_file=1; filename=$OPTARG ;;
	m)	opt_mount=1; mount=$OPTARG ;;
	v)	opt_time=1 ;;
	h|?)	cat <<-END >&2
		USAGE: iopattern [-v] [-d device] [-f filename] [-m mount_point]
		                 [interval [count]]
 
		                -v              # print timestamp
		                -d device       # instance name to snoop 
		                -f filename     # snoop this file only
		                -m mount_point  # this FS only 
		   eg,
		        iopattern         # default output, 1 second samples
		        iopattern 10      # 10 second samples
		        iopattern 5 12    # print 12 x 5 second samples
		        iopattern -m /    # snoop events on filesystem / only
		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


#################################
# --- Main Program, DTrace ---
#
/usr/sbin/dtrace -n '
 /*
  * Command line arguments
  */
 inline int OPT_time 	= '$opt_time';
 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'";
 
 #pragma D option quiet

 int last_loc[string];

 /*
  * Program start
  */
 dtrace:::BEGIN 
 {
        /* starting values */
	diskcnt = 0;
	diskmin = 0;
	diskmax = 0;
	diskran = 0;
	diskr = 0;
	diskw = 0;
        counts = COUNTER;
        secs = INTERVAL;
	LINES = 20;
	line = 0;
	last_event[""] = 0;
 }

 /*
  * Print header
  */
 profile:::tick-1sec
 /line <= 0 /
 {
	/* print optional headers */
	OPT_time   ? printf("%-20s ", "TIME")  : 1;
	OPT_device ? printf("%-9s ", "DEVICE") : 1;
	OPT_mount  ? printf("%-12s ", "MOUNT") : 1;
	OPT_file   ? printf("%-12s ", "FILE") : 1;

	/* print header */
	printf("%4s %4s %6s %6s %6s %6s %6s %6s\n",
	    "%RAN", "%SEQ", "COUNT", "MIN", "MAX", "AVG", "KR", "KW");

	line = LINES;
 }

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

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

 /*
  * Process and Print completion
  */
 io:genunix::done
 /self->ok/
 {
	/*
	 * Save details
	 */
	this->loc = args[0]->b_blkno * 512;
	this->pre = last_loc[args[1]->dev_statname];
	diskr += args[0]->b_flags & B_READ ? args[0]->b_bcount : 0;
	diskw += args[0]->b_flags & B_READ ? 0 : args[0]->b_bcount;
	diskran += this->pre == this->loc ? 0 : 1;
	diskcnt++;
	diskmin = diskmin == 0 ? args[0]->b_bcount :
	    (diskmin > args[0]->b_bcount ? args[0]->b_bcount : diskmin);
	diskmax = diskmax < args[0]->b_bcount ? args[0]->b_bcount : diskmax;

	/* save disk location */
	last_loc[args[1]->dev_statname] = this->loc + args[0]->b_bcount;

	/* cleanup */
	self->ok = 0;
 }

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

 /*
  * Print Output
  */
 profile:::tick-1sec
 /secs == 0/
 {
	/* calculate diskavg */
	diskavg = diskcnt > 0 ? (diskr + diskw) / diskcnt : 0;

	/* convert counters to Kbytes */
	diskr /= 1024;
	diskw /= 1024;

	/* convert to percentages */
	diskran = diskcnt == 0 ? 0 : (diskran * 100) / diskcnt;
	diskseq = diskcnt == 0 ? 0 : 100 - diskran;

	/* print optional fields */
	OPT_time   ? printf("%-20Y ", walltimestamp) : 1;
	OPT_device ? printf("%-9s ", DEVICE) : 1;
	OPT_mount  ? printf("%-12s ", MOUNT) : 1;
	OPT_file   ? printf("%-12s ", FILENAME) : 1;

	/* print data */
	printf("%4d %4d %6d %6d %6d %6d %6d %6d\n",
	    diskran, diskseq, diskcnt, diskmin, diskmax, diskavg,
	    diskr, diskw);

	/* clear data */
	diskmin = 0;
	diskmax = 0;
	diskcnt = 0;
	diskran = 0;
	diskr = 0;
	diskw = 0;

	secs = INTERVAL;
	counts--;
	line--;
 }

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