Home | History | Annotate | Line # | Download | only in iostat
iostat.c revision 1.40
      1 /*	$NetBSD: iostat.c,v 1.40 2003/08/07 11:25:22 agc 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. Neither the name of the University nor the names of its contributors
     48  *    may be used to endorse or promote products derived from this software
     49  *    without specific prior written permission.
     50  *
     51  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     52  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     53  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     54  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     55  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     56  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     57  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     59  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     60  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     61  * SUCH DAMAGE.
     62  */
     63 
     64 #include <sys/cdefs.h>
     65 #ifndef lint
     66 __COPYRIGHT("@(#) Copyright (c) 1986, 1991, 1993\n\
     67         The Regents of the University of California.  All rights reserved.\n");
     68 #endif /* not lint */
     69 
     70 #ifndef lint
     71 #if 0
     72 static char sccsid[] = "@(#)iostat.c	8.3 (Berkeley) 4/28/95";
     73 #else
     74 __RCSID("$NetBSD: iostat.c,v 1.40 2003/08/07 11:25:22 agc Exp $");
     75 #endif
     76 #endif /* not lint */
     77 
     78 #include <sys/types.h>
     79 #include <sys/ioctl.h>
     80 #include <sys/sched.h>
     81 #include <sys/time.h>
     82 
     83 #include <err.h>
     84 #include <ctype.h>
     85 #include <signal.h>
     86 #include <stdio.h>
     87 #include <stdlib.h>
     88 #include <string.h>
     89 #include <unistd.h>
     90 
     91 #include "dkstats.h"
     92 
     93 /* Namelist and memory files. */
     94 char	*nlistf, *memf;
     95 
     96 int		hz, reps, interval;
     97 static int	todo = 0;
     98 static int	defdrives;
     99 static int	winlines = 20;
    100 static int	wincols = 80;
    101 
    102 #define	ISSET(x, a)	((x) & (a))
    103 #define	SHOW_CPU	1<<0
    104 #define	SHOW_TTY	1<<1
    105 #define	SHOW_STATS_1	1<<2
    106 #define	SHOW_STATS_2	1<<3
    107 #define	SHOW_STATS_X	1<<4
    108 #define	SHOW_TOTALS	1<<7
    109 #define	SHOW_STATS_ALL	(SHOW_STATS_1 | SHOW_STATS_2 | SHOW_STATS_X)
    110 
    111 static void cpustats(void);
    112 static void disk_stats(double);
    113 static void disk_stats2(double);
    114 static void disk_statsx(double);
    115 static void sig_header(int);
    116 static volatile int do_header;
    117 static void header(void);
    118 static void usage(void);
    119 static void display(void);
    120 static int selectdrives(int, char *[]);
    121 
    122 int main(int, char *[]);
    123 
    124 int
    125 main(int argc, char *argv[])
    126 {
    127 	int ch, hdrcnt, ndrives, lines;
    128 	struct timespec	tv;
    129 	struct ttysize ts;
    130 
    131 	while ((ch = getopt(argc, argv, "Cc:dDIM:N:Tw:x")) != -1)
    132 		switch (ch) {
    133 		case 'c':
    134 			if ((reps = atoi(optarg)) <= 0)
    135 				errx(1, "repetition count <= 0.");
    136 			break;
    137 		case 'C':
    138 			todo |= SHOW_CPU;
    139 			break;
    140 		case 'd':
    141 			todo &= ~SHOW_STATS_ALL;
    142 			todo |= SHOW_STATS_1;
    143 			break;
    144 		case 'D':
    145 			todo &= ~SHOW_STATS_ALL;
    146 			todo |= SHOW_STATS_2;
    147 			break;
    148 		case 'I':
    149 			todo |= SHOW_TOTALS;
    150 			break;
    151 		case 'M':
    152 			memf = optarg;
    153 			break;
    154 		case 'N':
    155 			nlistf = optarg;
    156 			break;
    157 		case 'T':
    158 			todo |= SHOW_TTY;
    159 			break;
    160 		case 'w':
    161 			if ((interval = atoi(optarg)) <= 0)
    162 				errx(1, "interval <= 0.");
    163 			break;
    164 		case 'x':
    165 			todo &= ~SHOW_STATS_ALL;
    166 			todo |= SHOW_STATS_X;
    167 			break;
    168 		case '?':
    169 		default:
    170 			usage();
    171 		}
    172 	argc -= optind;
    173 	argv += optind;
    174 
    175 	if (!ISSET(todo, SHOW_CPU | SHOW_TTY | SHOW_STATS_ALL))
    176 		todo |= SHOW_CPU | SHOW_TTY | SHOW_STATS_1;
    177 	if (ISSET(todo, SHOW_STATS_X)) {
    178 		todo &= ~(SHOW_CPU | SHOW_TTY | SHOW_STATS_ALL);
    179 		todo |= SHOW_STATS_X;
    180 	}
    181 
    182 	if (ioctl(STDOUT_FILENO, TIOCGSIZE, &ts) != -1) {
    183 		if (ts.ts_lines)
    184 			winlines = ts.ts_lines;
    185 		if (ts.ts_cols)
    186 			wincols = ts.ts_cols;
    187 	}
    188 
    189 	defdrives = wincols;
    190 	if (ISSET(todo, SHOW_CPU))
    191 		defdrives -= 16;	/* XXX magic number */
    192 	if (ISSET(todo, SHOW_TTY))
    193 		defdrives -= 9;		/* XXX magic number */
    194 	defdrives /= 18;		/* XXX magic number */
    195 
    196 	dkinit(0);
    197 	dkreadstats();
    198 	ndrives = selectdrives(argc, argv);
    199 	if (ndrives == 0) {
    200 		/* No drives are selected.  No need to show disk stats. */
    201 		todo &= ~SHOW_STATS_ALL;
    202 		if (todo == 0)
    203 			errx(1, "no drives");
    204 	}
    205 	if (ISSET(todo, SHOW_STATS_X))
    206 		lines = ndrives;
    207 	else
    208 		lines = 1;
    209 
    210 	tv.tv_sec = interval;
    211 	tv.tv_nsec = 0;
    212 
    213 	/* print a new header on sigcont */
    214 	(void)signal(SIGCONT, sig_header);
    215 
    216 	for (hdrcnt = 1;;) {
    217 		if (do_header || ((hdrcnt -= lines) <= 0)) {
    218 			do_header = 0;
    219 			header();
    220 			if ((hdrcnt -= lines) <= 0)
    221 				hdrcnt = winlines - 4;
    222 		}
    223 
    224 		if (!ISSET(todo, SHOW_TOTALS))
    225 			dkswap();
    226 		display();
    227 
    228 		if (reps >= 0 && --reps <= 0)
    229 			break;
    230 		nanosleep(&tv, NULL);
    231 		dkreadstats();
    232 	}
    233 	exit(0);
    234 }
    235 
    236 static void
    237 sig_header(int signo)
    238 {
    239 	do_header = 1;
    240 }
    241 
    242 static void
    243 header()
    244 {
    245 	int i;
    246 
    247 					/* Main Headers. */
    248 	if (ISSET(todo, SHOW_STATS_X)) {
    249 		if (ISSET(todo, SHOW_TOTALS)) {
    250 			(void)printf(
    251 			    "device  read KB/t    xfr   time     MB/s");
    252 			(void)printf(" write KB/t    xfr   time     MB/s\n");
    253 		} else {
    254 			(void)printf(
    255 			    "device  read KB/t    r/s   time     MB/s");
    256 			(void)printf(" write KB/t    w/s   time     MB/s\n");
    257 		}
    258 		return;
    259 	}
    260 
    261 	if (ISSET(todo, SHOW_TTY))
    262 		(void)printf("      tty");
    263 
    264 	if (ISSET(todo, SHOW_STATS_1))
    265 		for (i = 0; i < dk_ndrive; i++)
    266 			if (cur.dk_select[i])
    267 				(void)printf("        %9.9s ", cur.dk_name[i]);
    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("        %9.9s ", cur.dk_name[i]);
    273 
    274 	if (ISSET(todo, SHOW_CPU))
    275 		(void)printf("            cpu");
    276 
    277 	printf("\n");
    278 
    279 					/* Sub-Headers. */
    280 	if (ISSET(todo, SHOW_TTY))
    281 		printf(" tin tout");
    282 
    283 	if (ISSET(todo, SHOW_STATS_1)) {
    284 		for (i = 0; i < dk_ndrive; i++)
    285 			if (cur.dk_select[i]) {
    286 				if (ISSET(todo, SHOW_TOTALS))
    287 					(void)printf("  KB/t  xfr  MB   ");
    288 				else
    289 					(void)printf("  KB/t  t/s  MB/s ");
    290 			}
    291 	}
    292 
    293 	if (ISSET(todo, SHOW_STATS_2))
    294 		for (i = 0; i < dk_ndrive; i++)
    295 			if (cur.dk_select[i])
    296 				(void)printf("    KB   xfr time ");
    297 
    298 	if (ISSET(todo, SHOW_CPU))
    299 		(void)printf(" us ni sy in id");
    300 	printf("\n");
    301 }
    302 
    303 static void
    304 disk_stats(double etime)
    305 {
    306 	int dn;
    307 	double atime, mbps;
    308 
    309 	for (dn = 0; dn < dk_ndrive; ++dn) {
    310 		if (!cur.dk_select[dn])
    311 			continue;
    312 					/* average Kbytes per transfer. */
    313 		if (cur.dk_rxfer[dn] + cur.dk_wxfer[dn])
    314 			mbps = ((cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) /
    315 			    1024.0) / (cur.dk_rxfer[dn] + cur.dk_wxfer[dn]);
    316 		else
    317 			mbps = 0.0;
    318 		(void)printf(" %5.2f", mbps);
    319 
    320 					/* average transfers per second. */
    321 		(void)printf(" %4.0f",
    322 		    (cur.dk_rxfer[dn] + cur.dk_wxfer[dn]) / etime);
    323 
    324 					/* time busy in disk activity */
    325 		atime = (double)cur.dk_time[dn].tv_sec +
    326 		    ((double)cur.dk_time[dn].tv_usec / (double)1000000);
    327 
    328 					/* Megabytes per second. */
    329 		if (atime != 0.0)
    330 			mbps = (cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) /
    331 			    (double)(1024 * 1024);
    332 		else
    333 			mbps = 0;
    334 		(void)printf(" %5.2f ", mbps / etime);
    335 	}
    336 }
    337 
    338 static void
    339 disk_stats2(double etime)
    340 {
    341 	int dn;
    342 	double atime;
    343 
    344 	for (dn = 0; dn < dk_ndrive; ++dn) {
    345 		if (!cur.dk_select[dn])
    346 			continue;
    347 
    348 					/* average kbytes per second. */
    349 		(void)printf(" %5.0f",
    350 		    (cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) / 1024.0 / etime);
    351 
    352 					/* average transfers per second. */
    353 		(void)printf(" %5.0f",
    354 		    (cur.dk_rxfer[dn] + cur.dk_wxfer[dn]) / etime);
    355 
    356 					/* average time busy in disk activity */
    357 		atime = (double)cur.dk_time[dn].tv_sec +
    358 		    ((double)cur.dk_time[dn].tv_usec / (double)1000000);
    359 		(void)printf(" %4.2f ", atime / etime);
    360 	}
    361 }
    362 
    363 static void
    364 disk_statsx(double etime)
    365 {
    366 	int dn;
    367 	double atime, kbps;
    368 
    369 	for (dn = 0; dn < dk_ndrive; ++dn) {
    370 		if (!cur.dk_select[dn])
    371 			continue;
    372 
    373 		(void)printf("%-8.8s", cur.dk_name[dn]);
    374 
    375 					/* average read Kbytes per transfer */
    376 		if (cur.dk_rxfer[dn])
    377 			kbps = (cur.dk_rbytes[dn] / 1024.0) / cur.dk_rxfer[dn];
    378 		else
    379 			kbps = 0.0;
    380 		(void)printf(" %8.2f", kbps);
    381 
    382 					/* average read transfers
    383 					   (per second) */
    384 		(void)printf(" %6.0f", cur.dk_rxfer[dn] / etime);
    385 
    386 					/* time read busy in disk activity */
    387 		atime = (double)cur.dk_time[dn].tv_sec +
    388 		    ((double)cur.dk_time[dn].tv_usec / (double)1000000);
    389 		(void)printf(" %6.2f", atime / etime);
    390 
    391 					/* average read megabytes
    392 					   (per second) */
    393 		(void)printf(" %8.2f",
    394 		    cur.dk_rbytes[dn] / (1024.0 * 1024) / etime);
    395 
    396 
    397 					/* average write Kbytes per transfer */
    398 		if (cur.dk_wxfer[dn])
    399 			kbps = (cur.dk_wbytes[dn] / 1024.0) / cur.dk_wxfer[dn];
    400 		else
    401 			kbps = 0.0;
    402 		(void)printf("   %8.2f", kbps);
    403 
    404 					/* average write transfers
    405 					   (per second) */
    406 		(void)printf(" %6.0f", cur.dk_wxfer[dn] / etime);
    407 
    408 					/* time write busy in disk activity */
    409 		atime = (double)cur.dk_time[dn].tv_sec +
    410 		    ((double)cur.dk_time[dn].tv_usec / (double)1000000);
    411 		(void)printf(" %6.2f", atime / etime);
    412 
    413 					/* average write megabytes
    414 					   (per second) */
    415 		(void)printf(" %8.2f\n",
    416 		    cur.dk_wbytes[dn] / (1024.0 * 1024) / etime);
    417 	}
    418 }
    419 
    420 static void
    421 cpustats(void)
    422 {
    423 	int state;
    424 	double time;
    425 
    426 	time = 0;
    427 	for (state = 0; state < CPUSTATES; ++state)
    428 		time += cur.cp_time[state];
    429 	if (!time)
    430 		time = 1.0;
    431 			/* States are generally never 100% and can use %3.0f. */
    432 	for (state = 0; state < CPUSTATES; ++state)
    433 		printf(" %2.0f", 100. * cur.cp_time[state] / time);
    434 }
    435 
    436 static void
    437 usage(void)
    438 {
    439 
    440 	(void)fprintf(stderr, "usage: iostat [-CdDITx] [-c count] [-M core] "
    441 	    "[-N system] [-w wait] [drives]\n");
    442 	exit(1);
    443 }
    444 
    445 static void
    446 display(void)
    447 {
    448 	double	etime;
    449 
    450 	/* Sum up the elapsed ticks. */
    451 	etime = cur.cp_etime;
    452 
    453 	/*
    454 	 * If we're showing totals only, then don't divide by the
    455 	 * system time.
    456 	 */
    457 	if (ISSET(todo, SHOW_TOTALS))
    458 		etime = 1.0;
    459 
    460 	if (ISSET(todo, SHOW_STATS_X)) {
    461 		disk_statsx(etime);
    462 		goto out;
    463 	}
    464 
    465 	if (ISSET(todo, SHOW_TTY))
    466 		printf("%4.0f %4.0f", cur.tk_nin / etime, cur.tk_nout / etime);
    467 
    468 	if (ISSET(todo, SHOW_STATS_1))
    469 		disk_stats(etime);
    470 
    471 	if (ISSET(todo, SHOW_STATS_2))
    472 		disk_stats2(etime);
    473 
    474 	if (ISSET(todo, SHOW_CPU))
    475 		cpustats();
    476 
    477 	(void)printf("\n");
    478 
    479 out:
    480 	(void)fflush(stdout);
    481 }
    482 
    483 static int
    484 selectdrives(int argc, char *argv[])
    485 {
    486 	int	i, maxdrives, ndrives, tried;
    487 
    488 	/*
    489 	 * Choose drives to be displayed.  Priority goes to (in order) drives
    490 	 * supplied as arguments and default drives.  If everything isn't
    491 	 * filled in and there are drives not taken care of, display the first
    492 	 * few that fit.
    493 	 *
    494 	 * The backward compatibility #ifdefs permit the syntax:
    495 	 *	iostat [ drives ] [ interval [ count ] ]
    496 	 */
    497 
    498 #define	BACKWARD_COMPATIBILITY
    499 	for (tried = ndrives = 0; *argv; ++argv) {
    500 #ifdef BACKWARD_COMPATIBILITY
    501 		if (isdigit(**argv))
    502 			break;
    503 #endif
    504 		tried++;
    505 		for (i = 0; i < dk_ndrive; i++) {
    506 			if (strcmp(cur.dk_name[i], *argv))
    507 				continue;
    508 			cur.dk_select[i] = 1;
    509 			++ndrives;
    510 		}
    511 	}
    512 
    513 	if (ndrives == 0 && tried == 0) {
    514 		/*
    515 		 * Pick up to defdrives (or all if -x is given) drives
    516 		 * if none specified.
    517 		 */
    518 		maxdrives = (ISSET(todo, SHOW_STATS_X) ||
    519 			     dk_ndrive < defdrives) ? dk_ndrive : defdrives;
    520 		for (i = 0; i < maxdrives; i++) {
    521 			cur.dk_select[i] = 1;
    522 			++ndrives;
    523 			if (!ISSET(todo, SHOW_STATS_X) && ndrives == defdrives)
    524 				break;
    525 		}
    526 	}
    527 
    528 #ifdef BACKWARD_COMPATIBILITY
    529 	if (*argv) {
    530 		interval = atoi(*argv);
    531 		if (*++argv)
    532 			reps = atoi(*argv);
    533 	}
    534 #endif
    535 
    536 	if (interval) {
    537 		if (!reps)
    538 			reps = -1;
    539 	} else
    540 		if (reps)
    541 			interval = 1;
    542 
    543 	return (ndrives);
    544 }
    545