1#!/bin/ksh
2##!/usr/bin/ksh
3#
4# iopattern - print disk I/O pattern.
5#             Written using DTrace (Solaris 10 3/05).
6#
7# This prints details on the I/O access pattern for the disks, such as
8# percentage of events that were of a random or sequential nature.
9# By default totals for all disks are printed.
10#
11# 25-Jul-2005, ver 0.70
12#
13# USAGE:	iopattern [-v] [-d device] [-f filename] [-m mount_point] 
14#			  [interval [count]]
15#
16#		       -v       	# print timestamp, string
17#		       -d device	# instance name to snoop (eg, dad0)
18#		       -f filename	# full pathname of file to snoop
19#		       -m mount_point	# this FS only (will skip raw events)
20#  eg,
21#		iopattern   	# default output, 1 second intervals
22#		iopattern 10  	# 10 second samples
23#		iopattern 5 12	# print 12 x 5 second samples
24#	        iopattern -m /  # snoop events on filesystem / only
25# 	
26# FIELDS:
27#		%RAN  		percentage of events of a random nature
28#		%SEQ 	 	percentage of events of a sequential nature
29#		COUNT		number of I/O events
30#		MIN		minimum I/O event size
31#		MAX		maximum I/O event size
32#		AVG		average I/O event size
33#		KR		total kilobytes read during sample
34#		KW		total kilobytes written during sample
35#		DEVICE		device name
36#		MOUNT		mount point
37#		FILE		filename
38#		TIME		timestamp, string
39# 
40# NOTES:
41#
42#  An event is considered random when the heads seek. This program prints
43#  the percentage of events that are random. The size of the seek is not
44#  measured - it's either random or not.
45#
46# SEE ALSO: iosnoop, iotop
47# 
48# IDEA: Ryan Matteson
49#
50# COPYRIGHT: Copyright (c) 2005 Brendan Gregg.
51#
52# CDDL HEADER START
53#
54#  The contents of this file are subject to the terms of the
55#  Common Development and Distribution License, Version 1.0 only
56#  (the "License").  You may not use this file except in compliance
57#  with the License.
58#
59#  You can obtain a copy of the license at Docs/cddl1.txt
60#  or http://www.opensolaris.org/os/licensing.
61#  See the License for the specific language governing permissions
62#  and limitations under the License.
63#
64# CDDL HEADER END
65#
66# Author: Brendan Gregg  [Sydney, Australia]
67#
68# 25-Jul-2005	Brendan Gregg	Created this.
69#
70
71
72##############################
73# --- Process Arguments ---
74#
75
76### default variables
77opt_device=0; opt_file=0; opt_mount=0; opt_time=0
78filter=0; device=.; filename=.; mount=.; interval=1; count=-1
79
80### process options
81while getopts d:f:hm:v name
82do
83	case $name in
84	d)	opt_device=1; device=$OPTARG ;;
85	f)	opt_file=1; filename=$OPTARG ;;
86	m)	opt_mount=1; mount=$OPTARG ;;
87	v)	opt_time=1 ;;
88	h|?)	cat <<-END >&2
89		USAGE: iopattern [-v] [-d device] [-f filename] [-m mount_point]
90		                 [interval [count]]
91 
92		                -v              # print timestamp
93		                -d device       # instance name to snoop 
94		                -f filename     # snoop this file only
95		                -m mount_point  # this FS only 
96		   eg,
97		        iopattern         # default output, 1 second samples
98		        iopattern 10      # 10 second samples
99		        iopattern 5 12    # print 12 x 5 second samples
100		        iopattern -m /    # snoop events on filesystem / only
101		END
102		exit 1
103	esac
104done
105
106shift $(( $OPTIND - 1 ))
107
108### option logic
109if [[ "$1" > 0 ]]; then
110        interval=$1; shift
111fi
112if [[ "$1" > 0 ]]; then
113        count=$1; shift
114fi
115if (( opt_device || opt_mount || opt_file )); then
116	filter=1
117fi
118
119
120#################################
121# --- Main Program, DTrace ---
122#
123/usr/sbin/dtrace -n '
124 /*
125  * Command line arguments
126  */
127 inline int OPT_time 	= '$opt_time';
128 inline int OPT_device 	= '$opt_device';
129 inline int OPT_mount 	= '$opt_mount';
130 inline int OPT_file 	= '$opt_file';
131 inline int INTERVAL 	= '$interval';
132 inline int COUNTER 	= '$count';
133 inline int FILTER 	= '$filter';
134 inline string DEVICE 	= "'$device'";
135 inline string FILENAME = "'$filename'";
136 inline string MOUNT 	= "'$mount'";
137 
138 #pragma D option quiet
139
140 int last_loc[string];
141
142 /*
143  * Program start
144  */
145 dtrace:::BEGIN 
146 {
147        /* starting values */
148	diskcnt = 0;
149	diskmin = 0;
150	diskmax = 0;
151	diskran = 0;
152	diskr = 0;
153	diskw = 0;
154        counts = COUNTER;
155        secs = INTERVAL;
156	LINES = 20;
157	line = 0;
158	last_event[""] = 0;
159 }
160
161 /*
162  * Print header
163  */
164 profile:::tick-1sec
165 /line <= 0 /
166 {
167	/* print optional headers */
168	OPT_time   ? printf("%-20s ", "TIME")  : 1;
169	OPT_device ? printf("%-9s ", "DEVICE") : 1;
170	OPT_mount  ? printf("%-12s ", "MOUNT") : 1;
171	OPT_file   ? printf("%-12s ", "FILE") : 1;
172
173	/* print header */
174	printf("%4s %4s %6s %6s %6s %6s %6s %6s\n",
175	    "%RAN", "%SEQ", "COUNT", "MIN", "MAX", "AVG", "KR", "KW");
176
177	line = LINES;
178 }
179
180 /*
181  * Check event is being traced
182  */
183 io:::done
184 { 
185	/* default is to trace unless filtering */
186	self->ok = FILTER ? 0 : 1;
187
188	/* check each filter */
189	(OPT_device == 1 && DEVICE == args[1]->dev_statname)? self->ok = 1 : 1;
190	(OPT_file == 1 && FILENAME == args[2]->fi_pathname) ? self->ok = 1 : 1;
191	(OPT_mount == 1 && MOUNT == args[2]->fi_mount)  ? self->ok = 1 : 1;
192 }
193
194 /*
195  * Process and Print completion
196  */
197 io:::done
198 /self->ok/
199 {
200	/*
201	 * Save details
202	 */
203	this->loc = args[0]->b_blkno * 512;
204	this->pre = last_loc[args[1]->dev_statname];
205	diskr += args[0]->b_flags & B_READ ? args[0]->b_bcount : 0;
206	diskw += args[0]->b_flags & B_READ ? 0 : args[0]->b_bcount;
207	diskran += this->pre == this->loc ? 0 : 1;
208	diskcnt++;
209	diskmin = diskmin == 0 ? args[0]->b_bcount :
210	    (diskmin > args[0]->b_bcount ? args[0]->b_bcount : diskmin);
211	diskmax = diskmax < args[0]->b_bcount ? args[0]->b_bcount : diskmax;
212
213	/* save disk location */
214	last_loc[args[1]->dev_statname] = this->loc + args[0]->b_bcount;
215
216	/* cleanup */
217	self->ok = 0;
218 }
219
220 /*
221  * Timer
222  */
223 profile:::tick-1sec
224 {
225	secs--;
226 }
227
228 /*
229  * Print Output
230  */
231 profile:::tick-1sec
232 /secs == 0/
233 {
234	/* calculate diskavg */
235	diskavg = diskcnt > 0 ? (diskr + diskw) / diskcnt : 0;
236
237	/* convert counters to Kbytes */
238	diskr /= 1024;
239	diskw /= 1024;
240
241	/* convert to percentages */
242	diskran = diskcnt == 0 ? 0 : (diskran * 100) / diskcnt;
243	diskseq = diskcnt == 0 ? 0 : 100 - diskran;
244
245	/* print optional fields */
246	OPT_time   ? printf("%-20Y ", walltimestamp) : 1;
247	OPT_device ? printf("%-9s ", DEVICE) : 1;
248	OPT_mount  ? printf("%-12s ", MOUNT) : 1;
249	OPT_file   ? printf("%-12s ", FILENAME) : 1;
250
251	/* print data */
252	printf("%4d %4d %6d %6d %6d %6d %6d %6d\n",
253	    diskran, diskseq, diskcnt, diskmin, diskmax, diskavg,
254	    diskr, diskw);
255
256	/* clear data */
257	diskmin = 0;
258	diskmax = 0;
259	diskcnt = 0;
260	diskran = 0;
261	diskr = 0;
262	diskw = 0;
263
264	secs = INTERVAL;
265	counts--;
266	line--;
267 }
268
269 /*
270  * End of program
271  */
272 profile:::tick-1sec
273 /counts == 0/
274 {
275	exit(0);
276 }
277'
278
279