Home | History | Annotate | Line # | Download | only in iostat
iostat.c revision 1.36
      1 /*	$NetBSD: iostat.c,v 1.36 2003/07/02 08:35:48 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.36 2003/07/02 08:35:48 simonb Exp $");
     79 #endif
     80 #endif /* not lint */
     81 
     82 #include <sys/types.h>
     83 #include <sys/sched.h>
     84 #include <sys/time.h>
     85 
     86 #include <err.h>
     87 #include <ctype.h>
     88 #include <signal.h>
     89 #include <stdio.h>
     90 #include <stdlib.h>
     91 #include <string.h>
     92 #include <unistd.h>
     93 
     94 #include "dkstats.h"
     95 
     96 /* Namelist and memory files. */
     97 char	*nlistf, *memf;
     98 
     99 int		hz, reps, interval;
    100 static int	todo = 0;
    101 
    102 #define	DEFDRIVES	3
    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 int selectdrives(int, char *[]);
    120 
    121 int main(int, char *[]);
    122 
    123 int
    124 main(int argc, char *argv[])
    125 {
    126 	int ch, hdrcnt, ndrives, lines;
    127 	struct timespec	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 	ndrives = selectdrives(argc, argv);
    183 	if (ndrives == 0) {
    184 		/* No drives are selected.  No need to show disk stats. */
    185 		todo &= ~SHOW_STATS_ALL;
    186 		if (todo == 0)
    187 			errx(1, "no drives");
    188 	}
    189 	if (ISSET(todo, SHOW_STATS_X))
    190 		lines = ndrives;
    191 	else
    192 		lines = 1;
    193 
    194 	tv.tv_sec = interval;
    195 	tv.tv_nsec = 0;
    196 
    197 	/* print a new header on sigcont */
    198 	(void)signal(SIGCONT, header);
    199 
    200 	for (hdrcnt = 1;;) {
    201 		if ((hdrcnt -= lines) <= 0) {
    202 			header(0);
    203 			hdrcnt = 20;
    204 		}
    205 
    206 		if (!ISSET(todo, SHOW_TOTALS))
    207 			dkswap();
    208 		display();
    209 
    210 		if (reps >= 0 && --reps <= 0)
    211 			break;
    212 		nanosleep(&tv, NULL);
    213 		dkreadstats();
    214 	}
    215 	exit(0);
    216 }
    217 
    218 static void
    219 header(int signo)
    220 {
    221 	int i;
    222 
    223 					/* Main Headers. */
    224 	if (ISSET(todo, SHOW_STATS_X)) {
    225 		if (ISSET(todo, SHOW_TOTALS)) {
    226 			(void)printf(
    227 			    "device  read KB/t    xfr   time     MB/s");
    228 			(void)printf(" write KB/t    xfr   time     MB/s\n");
    229 		} else {
    230 			(void)printf(
    231 			    "device  read KB/t    r/s   time     MB/s");
    232 			(void)printf(" write KB/t    w/s   time     MB/s\n");
    233 		}
    234 		return;
    235 	}
    236 
    237 	if (ISSET(todo, SHOW_TTY))
    238 		(void)printf("      tty");
    239 
    240 	if (ISSET(todo, SHOW_STATS_1))
    241 		for (i = 0; i < dk_ndrive; i++)
    242 			if (cur.dk_select[i])
    243 				(void)printf("        %9.9s ", cur.dk_name[i]);
    244 
    245 	if (ISSET(todo, SHOW_STATS_2))
    246 		for (i = 0; i < dk_ndrive; i++)
    247 			if (cur.dk_select[i])
    248 				(void)printf("        %9.9s ", cur.dk_name[i]);
    249 
    250 	if (ISSET(todo, SHOW_CPU))
    251 		(void)printf("            cpu");
    252 
    253 	printf("\n");
    254 
    255 					/* Sub-Headers. */
    256 	if (ISSET(todo, SHOW_TTY))
    257 		printf(" tin tout");
    258 
    259 	if (ISSET(todo, SHOW_STATS_1)) {
    260 		for (i = 0; i < dk_ndrive; i++)
    261 			if (cur.dk_select[i]) {
    262 				if (ISSET(todo, SHOW_TOTALS))
    263 					(void)printf("  KB/t  xfr  MB   ");
    264 				else
    265 					(void)printf("  KB/t  t/s  MB/s ");
    266 			}
    267 	}
    268 
    269 	if (ISSET(todo, SHOW_STATS_2))
    270 		for (i = 0; i < dk_ndrive; i++)
    271 			if (cur.dk_select[i])
    272 				(void)printf("    KB   xfr time ");
    273 
    274 	if (ISSET(todo, SHOW_CPU))
    275 		(void)printf(" us ni sy in id");
    276 	printf("\n");
    277 }
    278 
    279 static void
    280 disk_stats(double etime)
    281 {
    282 	int dn;
    283 	double atime, mbps;
    284 
    285 	for (dn = 0; dn < dk_ndrive; ++dn) {
    286 		if (!cur.dk_select[dn])
    287 			continue;
    288 					/* average Kbytes per transfer. */
    289 		if (cur.dk_rxfer[dn] + cur.dk_wxfer[dn])
    290 			mbps = ((cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) /
    291 			    1024.0) / (cur.dk_rxfer[dn] + cur.dk_wxfer[dn]);
    292 		else
    293 			mbps = 0.0;
    294 		(void)printf(" %5.2f", mbps);
    295 
    296 					/* average transfers per second. */
    297 		(void)printf(" %4.0f",
    298 		    (cur.dk_rxfer[dn] + cur.dk_wxfer[dn]) / etime);
    299 
    300 					/* time busy in disk activity */
    301 		atime = (double)cur.dk_time[dn].tv_sec +
    302 		    ((double)cur.dk_time[dn].tv_usec / (double)1000000);
    303 
    304 					/* Megabytes per second. */
    305 		if (atime != 0.0)
    306 			mbps = (cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) /
    307 			    (double)(1024 * 1024);
    308 		else
    309 			mbps = 0;
    310 		(void)printf(" %5.2f ", mbps / etime);
    311 	}
    312 }
    313 
    314 static void
    315 disk_stats2(double etime)
    316 {
    317 	int dn;
    318 	double atime;
    319 
    320 	for (dn = 0; dn < dk_ndrive; ++dn) {
    321 		if (!cur.dk_select[dn])
    322 			continue;
    323 
    324 					/* average kbytes per second. */
    325 		(void)printf(" %5.0f",
    326 		    (cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) / 1024.0 / etime);
    327 
    328 					/* average transfers per second. */
    329 		(void)printf(" %5.0f",
    330 		    (cur.dk_rxfer[dn] + cur.dk_wxfer[dn]) / etime);
    331 
    332 					/* average time busy in disk activity */
    333 		atime = (double)cur.dk_time[dn].tv_sec +
    334 		    ((double)cur.dk_time[dn].tv_usec / (double)1000000);
    335 		(void)printf(" %4.2f ", atime / etime);
    336 	}
    337 }
    338 
    339 static void
    340 disk_statsx(double etime)
    341 {
    342 	int dn;
    343 	double atime, kbps;
    344 
    345 	for (dn = 0; dn < dk_ndrive; ++dn) {
    346 		if (!cur.dk_select[dn])
    347 			continue;
    348 
    349 		(void)printf("%-8.8s", cur.dk_name[dn]);
    350 
    351 					/* average read Kbytes per transfer */
    352 		if (cur.dk_rxfer[dn])
    353 			kbps = (cur.dk_rbytes[dn] / 1024.0) / cur.dk_rxfer[dn];
    354 		else
    355 			kbps = 0.0;
    356 		(void)printf(" %8.2f", kbps);
    357 
    358 					/* average read transfers
    359 					   (per second) */
    360 		(void)printf(" %6.0f", cur.dk_rxfer[dn] / etime);
    361 
    362 					/* time read busy in disk activity */
    363 		atime = (double)cur.dk_time[dn].tv_sec +
    364 		    ((double)cur.dk_time[dn].tv_usec / (double)1000000);
    365 		(void)printf(" %6.2f", atime / etime);
    366 
    367 					/* average read megabytes
    368 					   (per second) */
    369 		(void)printf(" %8.2f",
    370 		    cur.dk_rbytes[dn] / (1024.0 * 1024) / etime);
    371 
    372 
    373 					/* average write Kbytes per transfer */
    374 		if (cur.dk_wxfer[dn])
    375 			kbps = (cur.dk_wbytes[dn] / 1024.0) / cur.dk_wxfer[dn];
    376 		else
    377 			kbps = 0.0;
    378 		(void)printf("   %8.2f", kbps);
    379 
    380 					/* average write transfers
    381 					   (per second) */
    382 		(void)printf(" %6.0f", cur.dk_wxfer[dn] / etime);
    383 
    384 					/* time write busy in disk activity */
    385 		atime = (double)cur.dk_time[dn].tv_sec +
    386 		    ((double)cur.dk_time[dn].tv_usec / (double)1000000);
    387 		(void)printf(" %6.2f", atime / etime);
    388 
    389 					/* average write megabytes
    390 					   (per second) */
    391 		(void)printf(" %8.2f\n",
    392 		    cur.dk_wbytes[dn] / (1024.0 * 1024) / etime);
    393 	}
    394 }
    395 
    396 static void
    397 cpustats(void)
    398 {
    399 	int state;
    400 	double time;
    401 
    402 	time = 0;
    403 	for (state = 0; state < CPUSTATES; ++state)
    404 		time += cur.cp_time[state];
    405 	if (!time)
    406 		time = 1.0;
    407 			/* States are generally never 100% and can use %3.0f. */
    408 	for (state = 0; state < CPUSTATES; ++state)
    409 		printf(" %2.0f", 100. * cur.cp_time[state] / time);
    410 }
    411 
    412 static void
    413 usage(void)
    414 {
    415 
    416 	(void)fprintf(stderr, "usage: iostat [-CdDITx] [-c count] [-M core] "
    417 	    "[-N system] [-w wait] [drives]\n");
    418 	exit(1);
    419 }
    420 
    421 static void
    422 display(void)
    423 {
    424 	double	etime;
    425 
    426 	/* Sum up the elapsed ticks. */
    427 	etime = cur.cp_etime;
    428 
    429 	/*
    430 	 * If we're showing totals only, then don't divide by the
    431 	 * system time.
    432 	 */
    433 	if (ISSET(todo, SHOW_TOTALS))
    434 		etime = 1.0;
    435 
    436 	if (ISSET(todo, SHOW_STATS_X)) {
    437 		disk_statsx(etime);
    438 		goto out;
    439 	}
    440 
    441 	if (ISSET(todo, SHOW_TTY))
    442 		printf("%4.0f %4.0f", cur.tk_nin / etime, cur.tk_nout / etime);
    443 
    444 	if (ISSET(todo, SHOW_STATS_1))
    445 		disk_stats(etime);
    446 
    447 	if (ISSET(todo, SHOW_STATS_2))
    448 		disk_stats2(etime);
    449 
    450 	if (ISSET(todo, SHOW_CPU))
    451 		cpustats();
    452 
    453 	(void)printf("\n");
    454 
    455 out:
    456 	(void)fflush(stdout);
    457 }
    458 
    459 static int
    460 selectdrives(int argc, char *argv[])
    461 {
    462 	int	i, maxdrives, ndrives, tried;
    463 
    464 	/*
    465 	 * Choose drives to be displayed.  Priority goes to (in order) drives
    466 	 * supplied as arguments and default drives.  If everything isn't
    467 	 * filled in and there are drives not taken care of, display the first
    468 	 * few that fit.
    469 	 *
    470 	 * The backward compatibility #ifdefs permit the syntax:
    471 	 *	iostat [ drives ] [ interval [ count ] ]
    472 	 */
    473 
    474 #define	BACKWARD_COMPATIBILITY
    475 	for (tried = ndrives = 0; *argv; ++argv) {
    476 #ifdef BACKWARD_COMPATIBILITY
    477 		if (isdigit(**argv))
    478 			break;
    479 #endif
    480 		tried++;
    481 		for (i = 0; i < dk_ndrive; i++) {
    482 			if (strcmp(cur.dk_name[i], *argv))
    483 				continue;
    484 			cur.dk_select[i] = 1;
    485 			++ndrives;
    486 		}
    487 	}
    488 
    489 	if (ndrives == 0 && tried == 0) {
    490 		/*
    491 		 * Pick up to DEFDRIVES (or all if -x is given) drives
    492 		 * if none specified.
    493 		 */
    494 		maxdrives = ISSET(todo, SHOW_STATS_X) ? dk_ndrive : DEFDRIVES;
    495 		for (i = 0; i < maxdrives; i++) {
    496 			cur.dk_select[i] = 1;
    497 			++ndrives;
    498 			if (!ISSET(todo, SHOW_STATS_X) && ndrives == DEFDRIVES)
    499 				break;
    500 		}
    501 	}
    502 
    503 #ifdef BACKWARD_COMPATIBILITY
    504 	if (*argv) {
    505 		interval = atoi(*argv);
    506 		if (*++argv)
    507 			reps = atoi(*argv);
    508 	}
    509 #endif
    510 
    511 	if (interval) {
    512 		if (!reps)
    513 			reps = -1;
    514 	} else
    515 		if (reps)
    516 			interval = 1;
    517 
    518 	return (ndrives);
    519 }
    520