xref: /netbsd/usr.sbin/iostat/iostat.c (revision bf9ec67e)
1 /*	$NetBSD: iostat.c,v 1.26 2002/05/22 13:12:27 simonb Exp $	*/
2 
3 /*
4  * Copyright (c) 1996 John M. Vinopal
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed for the NetBSD Project
18  *      by John M. Vinopal.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 /*-
36  * Copyright (c) 1986, 1991, 1993
37  *      The Regents of the University of California.  All rights reserved.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  * 3. All advertising materials mentioning features or use of this software
48  *    must display the following acknowledgement:
49  *      This product includes software developed by the University of
50  *      California, Berkeley and its contributors.
51  * 4. Neither the name of the University nor the names of its contributors
52  *    may be used to endorse or promote products derived from this software
53  *    without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65  * SUCH DAMAGE.
66  */
67 
68 #include <sys/cdefs.h>
69 #ifndef lint
70 __COPYRIGHT("@(#) Copyright (c) 1986, 1991, 1993\n\
71         The Regents of the University of California.  All rights reserved.\n");
72 #endif /* not lint */
73 
74 #ifndef lint
75 #if 0
76 static char sccsid[] = "@(#)iostat.c	8.3 (Berkeley) 4/28/95";
77 #else
78 __RCSID("$NetBSD: iostat.c,v 1.26 2002/05/22 13:12:27 simonb Exp $");
79 #endif
80 #endif /* not lint */
81 
82 #include <sys/types.h>
83 #include <sys/sched.h>
84 #include <sys/dkstat.h>
85 #include <sys/time.h>
86 
87 #include <err.h>
88 #include <ctype.h>
89 #include <signal.h>
90 #include <stdio.h>
91 #include <stdlib.h>
92 #include <string.h>
93 #include <unistd.h>
94 
95 #include "dkstats.h"
96 
97 /* Namelist and memory files. */
98 char	*nlistf, *memf;
99 
100 int		hz, reps, interval;
101 static int	todo = 0;
102 
103 #define ISSET(x, a)	((x) & (a))
104 #define SHOW_CPU	1<<0
105 #define SHOW_TTY	1<<1
106 #define SHOW_STATS_1	1<<2
107 #define SHOW_STATS_2	1<<3
108 #define SHOW_STATS_X	1<<4
109 #define SHOW_TOTALS	1<<7
110 #define SHOW_STATS_ALL	(SHOW_STATS_1 | SHOW_STATS_2 | SHOW_STATS_X)
111 
112 static void cpustats(void);
113 static void disk_stats(double);
114 static void disk_stats2(double);
115 static void disk_statsx(double);
116 static void header(int);
117 static void usage(void);
118 static void display(void);
119 static void selectdrives(int, char **);
120 
121 int main(int, char **);
122 
123 int
124 main(int argc, char *argv[])
125 {
126 	int ch, hdrcnt;
127 	struct timeval	tv;
128 
129 	while ((ch = getopt(argc, argv, "Cc:dDIM:N:Tw:x")) != -1)
130 		switch(ch) {
131 		case 'c':
132 			if ((reps = atoi(optarg)) <= 0)
133 				errx(1, "repetition count <= 0.");
134 			break;
135 		case 'C':
136 			todo |= SHOW_CPU;
137 			break;
138 		case 'd':
139 			todo &= ~SHOW_STATS_ALL;
140 			todo |= SHOW_STATS_1;
141 			break;
142 		case 'D':
143 			todo &= ~SHOW_STATS_ALL;
144 			todo |= SHOW_STATS_2;
145 			break;
146 		case 'I':
147 			todo |= SHOW_TOTALS;
148 			break;
149 		case 'M':
150 			memf = optarg;
151 			break;
152 		case 'N':
153 			nlistf = optarg;
154 			break;
155 		case 'T':
156 			todo |= SHOW_TTY;
157 			break;
158 		case 'w':
159 			if ((interval = atoi(optarg)) <= 0)
160 				errx(1, "interval <= 0.");
161 			break;
162 		case 'x':
163 			todo &= ~SHOW_STATS_ALL;
164 			todo |= SHOW_STATS_X;
165 			break;
166 		case '?':
167 		default:
168 			usage();
169 		}
170 	argc -= optind;
171 	argv += optind;
172 
173 	if (!ISSET(todo, SHOW_CPU | SHOW_TTY | SHOW_STATS_ALL))
174 		todo |= SHOW_CPU | SHOW_TTY | SHOW_STATS_1;
175 	if (ISSET(todo, SHOW_STATS_X)) {
176 		todo &= ~(SHOW_CPU | SHOW_TTY | SHOW_STATS_ALL);
177 		todo |= SHOW_STATS_X;
178 	}
179 
180 	dkinit(0);
181 	dkreadstats();
182 	selectdrives(argc, argv);
183 
184 	tv.tv_sec = interval;
185 	tv.tv_usec = 0;
186 
187 	/* print a new header on sigcont */
188 	(void)signal(SIGCONT, header);
189 
190 	for (hdrcnt = 1;;) {
191 		if (!--hdrcnt) {
192 			header(0);
193 			hdrcnt = 20;
194 		}
195 
196 		if (!ISSET(todo, SHOW_TOTALS))
197 			dkswap();
198 		display();
199 
200 		if (reps >= 0 && --reps <= 0)
201 			break;
202 		select(0, NULL, NULL, NULL, &tv);
203 		dkreadstats();
204 	}
205 	exit(0);
206 }
207 
208 static void
209 header(int signo)
210 {
211 	int i;
212 
213 	if (ISSET(todo, SHOW_STATS_X))
214 		return;
215 
216 					/* Main Headers. */
217 	if (ISSET(todo, SHOW_TTY))
218 		(void)printf("      tty");
219 
220 	if (ISSET(todo, SHOW_STATS_1))
221 		for (i = 0; i < dk_ndrive; i++)
222 			if (cur.dk_select[i])
223 				(void)printf(
224 				    "        %7.7s ", cur.dk_name[i]);
225 
226 	if (ISSET(todo, SHOW_STATS_2))
227 		for (i = 0; i < dk_ndrive; i++)
228 			if (cur.dk_select[i])
229 				(void)printf(
230 				    "       %7.7s ", cur.dk_name[i]);
231 
232 	if (ISSET(todo, SHOW_CPU))
233 		(void)printf("            cpu");
234 
235 	printf("\n");
236 
237 					/* Sub-Headers. */
238 	if (ISSET(todo, SHOW_TTY))
239 		printf(" tin tout");
240 
241 	if (ISSET(todo, SHOW_STATS_1)) {
242 		for (i = 0; i < dk_ndrive; i++)
243 			if (cur.dk_select[i]) {
244 				if (ISSET(todo, SHOW_TOTALS))
245 					(void)printf("  KB/t xfr MB   ");
246 				else
247 					(void)printf("  KB/t t/s MB/s ");
248 			}
249 	}
250 
251 	if (ISSET(todo, SHOW_STATS_2))
252 		for (i = 0; i < dk_ndrive; i++)
253 			if (cur.dk_select[i])
254 				(void)printf("   KB xfr time ");
255 
256 	if (ISSET(todo, SHOW_CPU))
257 		(void)printf(" us ni sy in id");
258 	printf("\n");
259 }
260 
261 static void
262 disk_stats(double etime)
263 {
264 	int dn;
265 	double atime, mbps;
266 
267 	for (dn = 0; dn < dk_ndrive; ++dn) {
268 		if (!cur.dk_select[dn])
269 			continue;
270 
271 					/* average Kbytes per transfer. */
272 		if (cur.dk_xfer[dn])
273 			mbps = (cur.dk_bytes[dn] / (1024.0)) / cur.dk_xfer[dn];
274 		else
275 			mbps = 0.0;
276 		(void)printf(" %5.2f", mbps);
277 
278 					/* average transfers per second. */
279 		(void)printf(" %3.0f", cur.dk_xfer[dn] / etime);
280 
281 					/* time busy in disk activity */
282 		atime = (double)cur.dk_time[dn].tv_sec +
283 			((double)cur.dk_time[dn].tv_usec / (double)1000000);
284 
285 					/* Megabytes per second. */
286 		if (atime != 0.0)
287 			mbps = cur.dk_bytes[dn] / (double)(1024 * 1024);
288 		else
289 			mbps = 0;
290 		(void)printf(" %4.2f ", mbps / etime);
291 	}
292 }
293 
294 static void
295 disk_stats2(double etime)
296 {
297 	int dn;
298 	double atime;
299 
300 	for (dn = 0; dn < dk_ndrive; ++dn) {
301 		if (!cur.dk_select[dn])
302 			continue;
303 
304 					/* average kbytes per second. */
305 		(void)printf(" %4.0f", cur.dk_bytes[dn] / (1024.0) / etime);
306 
307 					/* average transfers per second. */
308 		(void)printf(" %3.0f", cur.dk_xfer[dn] / etime);
309 
310 					/* average time busy in disk activity */
311 		atime = (double)cur.dk_time[dn].tv_sec +
312 			((double)cur.dk_time[dn].tv_usec / (double)1000000);
313 		(void)printf(" %4.2f ", atime / etime);
314 	}
315 }
316 
317 static void
318 disk_statsx(double etime)
319 {
320 	int dn;
321 	double atime, kbps;
322 
323 	if (ISSET(todo, SHOW_TOTALS))
324 		(void)printf("device       KB/t      xfr     time       MB");
325 	else
326 		(void)printf("device       KB/t      t/s     time     MB/s");
327 
328 	for (dn = 0; dn < dk_ndrive; ++dn) {
329 		(void)printf("\n");
330 		(void)printf("%-8.8s", cur.dk_name[dn]);
331 
332 					/* average Kbytes per transfer */
333 		if (cur.dk_xfer[dn])
334 			kbps = (cur.dk_bytes[dn] / (1024.0)) / cur.dk_xfer[dn];
335 		else
336 			kbps = 0.0;
337 		(void)printf(" %8.2f", kbps);
338 
339 					/* average transfers (per second) */
340 		(void)printf(" %8.0f", cur.dk_xfer[dn] / etime);
341 
342 					/* time busy in disk activity */
343 		atime = (double)cur.dk_time[dn].tv_sec +
344 			((double)cur.dk_time[dn].tv_usec / (double)1000000);
345 		(void)printf(" %8.2f", atime / etime);
346 
347 					/* average megabytes (per second) */
348 		(void)printf(" %8.2f",
349 		    cur.dk_bytes[dn] / (1024.0 * 1024) / etime);
350 
351 	}
352 }
353 
354 static void
355 cpustats(void)
356 {
357 	int state;
358 	double time;
359 
360 	time = 0;
361 	for (state = 0; state < CPUSTATES; ++state)
362 		time += cur.cp_time[state];
363 	if (!time)
364 		time = 1.0;
365 			/* States are generally never 100% and can use %3.0f. */
366 	for (state = 0; state < CPUSTATES; ++state)
367 		printf(" %2.0f", 100. * cur.cp_time[state] / time);
368 }
369 
370 static void
371 usage(void)
372 {
373 
374 	(void)fprintf(stderr, "usage: iostat [-CdDITx] [-c count] [-M core] \
375 [-N system] [-w wait] [drives]\n");
376 	exit(1);
377 }
378 
379 static void
380 display(void)
381 {
382 	int	i;
383 	double	etime;
384 
385 	/* Sum up the elapsed ticks. */
386 	etime = 0.0;
387 	for (i = 0; i < CPUSTATES; i++) {
388 		etime += cur.cp_time[i];
389 	}
390 	if (etime == 0.0)
391 		etime = 1.0;
392 					/* Convert to seconds. */
393 	etime /= (float)hz;
394 
395 	/* If we're showing totals only, then don't divide by the
396 	 * system time.
397 	 */
398 	if (ISSET(todo, SHOW_TOTALS))
399 		etime = 1.0;
400 
401 	if (ISSET(todo, SHOW_TTY))
402 		printf("%4.0f %4.0f", cur.tk_nin / etime, cur.tk_nout / etime);
403 
404 	if (ISSET(todo, SHOW_STATS_1))
405 		disk_stats(etime);
406 
407 	if (ISSET(todo, SHOW_STATS_2))
408 		disk_stats2(etime);
409 
410 	if (ISSET(todo, SHOW_STATS_X))
411 		disk_statsx(etime);
412 
413 	if (ISSET(todo, SHOW_CPU))
414 		cpustats();
415 
416 	(void)printf("\n");
417 	(void)fflush(stdout);
418 }
419 
420 static void
421 selectdrives(int argc, char *argv[])
422 {
423 	int	i, ndrives;
424 
425 	/*
426 	 * Choose drives to be displayed.  Priority goes to (in order) drives
427 	 * supplied as arguments and default drives.  If everything isn't
428 	 * filled in and there are drives not taken care of, display the first
429 	 * few that fit.
430 	 *
431 	 * The backward compatibility #ifdefs permit the syntax:
432 	 *	iostat [ drives ] [ interval [ count ] ]
433 	 */
434 	if (ISSET(todo, SHOW_STATS_X)) {
435 		for (i = 0; i < dk_ndrive; i++)
436 			cur.dk_select[i] = 1;
437 	}
438 
439 #define	BACKWARD_COMPATIBILITY
440 	for (ndrives = 0; *argv; ++argv) {
441 #ifdef	BACKWARD_COMPATIBILITY
442 		if (isdigit(**argv))
443 			break;
444 #endif
445 		if (!ISSET(todo, SHOW_STATS_X))
446 			for (i = 0; i < dk_ndrive; i++) {
447 				if (strcmp(cur.dk_name[i], *argv))
448 					continue;
449 				cur.dk_select[i] = 1;
450 				++ndrives;
451 			}
452 	}
453 #ifdef	BACKWARD_COMPATIBILITY
454 	if (*argv) {
455 		interval = atoi(*argv);
456 		if (*++argv)
457 			reps = atoi(*argv);
458 	}
459 #endif
460 
461 	if (interval) {
462 		if (!reps)
463 			reps = -1;
464 	} else
465 		if (reps)
466 			interval = 1;
467 
468 	/* Pick up to 4 drives if none specified. */
469 	if (ndrives == 0)
470 		for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
471 			if (cur.dk_select[i])
472 				continue;
473 			cur.dk_select[i] = 1;
474 			++ndrives;
475 		}
476 }
477