Home | History | Annotate | Line # | Download | only in iostat
iostat.c revision 1.38
      1 /*	$NetBSD: iostat.c,v 1.38 2003/07/02 13:47:57 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.38 2003/07/02 13:47:57 simonb Exp $");
     79 #endif
     80 #endif /* not lint */
     81 
     82 #include <sys/types.h>
     83 #include <sys/ioctl.h>
     84 #include <sys/sched.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 static int	defdrives;
    103 static int	winlines = 20;
    104 static int	wincols = 80;
    105 
    106 #define	ISSET(x, a)	((x) & (a))
    107 #define	SHOW_CPU	1<<0
    108 #define	SHOW_TTY	1<<1
    109 #define	SHOW_STATS_1	1<<2
    110 #define	SHOW_STATS_2	1<<3
    111 #define	SHOW_STATS_X	1<<4
    112 #define	SHOW_TOTALS	1<<7
    113 #define	SHOW_STATS_ALL	(SHOW_STATS_1 | SHOW_STATS_2 | SHOW_STATS_X)
    114 
    115 static void cpustats(void);
    116 static void disk_stats(double);
    117 static void disk_stats2(double);
    118 static void disk_statsx(double);
    119 static void header(int);
    120 static void usage(void);
    121 static void display(void);
    122 static int selectdrives(int, char *[]);
    123 
    124 int main(int, char *[]);
    125 
    126 int
    127 main(int argc, char *argv[])
    128 {
    129 	int ch, hdrcnt, ndrives, lines;
    130 	struct timespec	tv;
    131 	struct ttysize ts;
    132 
    133 	while ((ch = getopt(argc, argv, "Cc:dDIM:N:Tw:x")) != -1)
    134 		switch (ch) {
    135 		case 'c':
    136 			if ((reps = atoi(optarg)) <= 0)
    137 				errx(1, "repetition count <= 0.");
    138 			break;
    139 		case 'C':
    140 			todo |= SHOW_CPU;
    141 			break;
    142 		case 'd':
    143 			todo &= ~SHOW_STATS_ALL;
    144 			todo |= SHOW_STATS_1;
    145 			break;
    146 		case 'D':
    147 			todo &= ~SHOW_STATS_ALL;
    148 			todo |= SHOW_STATS_2;
    149 			break;
    150 		case 'I':
    151 			todo |= SHOW_TOTALS;
    152 			break;
    153 		case 'M':
    154 			memf = optarg;
    155 			break;
    156 		case 'N':
    157 			nlistf = optarg;
    158 			break;
    159 		case 'T':
    160 			todo |= SHOW_TTY;
    161 			break;
    162 		case 'w':
    163 			if ((interval = atoi(optarg)) <= 0)
    164 				errx(1, "interval <= 0.");
    165 			break;
    166 		case 'x':
    167 			todo &= ~SHOW_STATS_ALL;
    168 			todo |= SHOW_STATS_X;
    169 			break;
    170 		case '?':
    171 		default:
    172 			usage();
    173 		}
    174 	argc -= optind;
    175 	argv += optind;
    176 
    177 	if (!ISSET(todo, SHOW_CPU | SHOW_TTY | SHOW_STATS_ALL))
    178 		todo |= SHOW_CPU | SHOW_TTY | SHOW_STATS_1;
    179 	if (ISSET(todo, SHOW_STATS_X)) {
    180 		todo &= ~(SHOW_CPU | SHOW_TTY | SHOW_STATS_ALL);
    181 		todo |= SHOW_STATS_X;
    182 	}
    183 
    184 	if (ioctl(STDOUT_FILENO, TIOCGSIZE, &ts) != -1) {
    185 		if (ts.ts_lines)
    186 			winlines = ts.ts_lines;
    187 		if (ts.ts_cols)
    188 			wincols = ts.ts_cols;
    189 	}
    190 
    191 	defdrives = wincols;
    192 	if (ISSET(todo, SHOW_CPU))
    193 		defdrives -= 16;	/* XXX magic number */
    194 	if (ISSET(todo, SHOW_TTY))
    195 		defdrives -= 9;		/* XXX magic number */
    196 	defdrives /= 18;		/* XXX magic number */
    197 
    198 	dkinit(0);
    199 	dkreadstats();
    200 	ndrives = selectdrives(argc, argv);
    201 	if (ndrives == 0) {
    202 		/* No drives are selected.  No need to show disk stats. */
    203 		todo &= ~SHOW_STATS_ALL;
    204 		if (todo == 0)
    205 			errx(1, "no drives");
    206 	}
    207 	if (ISSET(todo, SHOW_STATS_X))
    208 		lines = ndrives;
    209 	else
    210 		lines = 1;
    211 
    212 	tv.tv_sec = interval;
    213 	tv.tv_nsec = 0;
    214 
    215 	/* print a new header on sigcont */
    216 	(void)signal(SIGCONT, header);
    217 
    218 	for (hdrcnt = 1;;) {
    219 		if ((hdrcnt -= lines) <= 0) {
    220 			header(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 header(int signo)
    238 {
    239 	int i;
    240 
    241 					/* Main Headers. */
    242 	if (ISSET(todo, SHOW_STATS_X)) {
    243 		if (ISSET(todo, SHOW_TOTALS)) {
    244 			(void)printf(
    245 			    "device  read KB/t    xfr   time     MB/s");
    246 			(void)printf(" write KB/t    xfr   time     MB/s\n");
    247 		} else {
    248 			(void)printf(
    249 			    "device  read KB/t    r/s   time     MB/s");
    250 			(void)printf(" write KB/t    w/s   time     MB/s\n");
    251 		}
    252 		return;
    253 	}
    254 
    255 	if (ISSET(todo, SHOW_TTY))
    256 		(void)printf("      tty");
    257 
    258 	if (ISSET(todo, SHOW_STATS_1))
    259 		for (i = 0; i < dk_ndrive; i++)
    260 			if (cur.dk_select[i])
    261 				(void)printf("        %9.9s ", cur.dk_name[i]);
    262 
    263 	if (ISSET(todo, SHOW_STATS_2))
    264 		for (i = 0; i < dk_ndrive; i++)
    265 			if (cur.dk_select[i])
    266 				(void)printf("        %9.9s ", cur.dk_name[i]);
    267 
    268 	if (ISSET(todo, SHOW_CPU))
    269 		(void)printf("            cpu");
    270 
    271 	printf("\n");
    272 
    273 					/* Sub-Headers. */
    274 	if (ISSET(todo, SHOW_TTY))
    275 		printf(" tin tout");
    276 
    277 	if (ISSET(todo, SHOW_STATS_1)) {
    278 		for (i = 0; i < dk_ndrive; i++)
    279 			if (cur.dk_select[i]) {
    280 				if (ISSET(todo, SHOW_TOTALS))
    281 					(void)printf("  KB/t  xfr  MB   ");
    282 				else
    283 					(void)printf("  KB/t  t/s  MB/s ");
    284 			}
    285 	}
    286 
    287 	if (ISSET(todo, SHOW_STATS_2))
    288 		for (i = 0; i < dk_ndrive; i++)
    289 			if (cur.dk_select[i])
    290 				(void)printf("    KB   xfr time ");
    291 
    292 	if (ISSET(todo, SHOW_CPU))
    293 		(void)printf(" us ni sy in id");
    294 	printf("\n");
    295 }
    296 
    297 static void
    298 disk_stats(double etime)
    299 {
    300 	int dn;
    301 	double atime, mbps;
    302 
    303 	for (dn = 0; dn < dk_ndrive; ++dn) {
    304 		if (!cur.dk_select[dn])
    305 			continue;
    306 					/* average Kbytes per transfer. */
    307 		if (cur.dk_rxfer[dn] + cur.dk_wxfer[dn])
    308 			mbps = ((cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) /
    309 			    1024.0) / (cur.dk_rxfer[dn] + cur.dk_wxfer[dn]);
    310 		else
    311 			mbps = 0.0;
    312 		(void)printf(" %5.2f", mbps);
    313 
    314 					/* average transfers per second. */
    315 		(void)printf(" %4.0f",
    316 		    (cur.dk_rxfer[dn] + cur.dk_wxfer[dn]) / etime);
    317 
    318 					/* time busy in disk activity */
    319 		atime = (double)cur.dk_time[dn].tv_sec +
    320 		    ((double)cur.dk_time[dn].tv_usec / (double)1000000);
    321 
    322 					/* Megabytes per second. */
    323 		if (atime != 0.0)
    324 			mbps = (cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) /
    325 			    (double)(1024 * 1024);
    326 		else
    327 			mbps = 0;
    328 		(void)printf(" %5.2f ", mbps / etime);
    329 	}
    330 }
    331 
    332 static void
    333 disk_stats2(double etime)
    334 {
    335 	int dn;
    336 	double atime;
    337 
    338 	for (dn = 0; dn < dk_ndrive; ++dn) {
    339 		if (!cur.dk_select[dn])
    340 			continue;
    341 
    342 					/* average kbytes per second. */
    343 		(void)printf(" %5.0f",
    344 		    (cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) / 1024.0 / etime);
    345 
    346 					/* average transfers per second. */
    347 		(void)printf(" %5.0f",
    348 		    (cur.dk_rxfer[dn] + cur.dk_wxfer[dn]) / etime);
    349 
    350 					/* average time busy in disk activity */
    351 		atime = (double)cur.dk_time[dn].tv_sec +
    352 		    ((double)cur.dk_time[dn].tv_usec / (double)1000000);
    353 		(void)printf(" %4.2f ", atime / etime);
    354 	}
    355 }
    356 
    357 static void
    358 disk_statsx(double etime)
    359 {
    360 	int dn;
    361 	double atime, kbps;
    362 
    363 	for (dn = 0; dn < dk_ndrive; ++dn) {
    364 		if (!cur.dk_select[dn])
    365 			continue;
    366 
    367 		(void)printf("%-8.8s", cur.dk_name[dn]);
    368 
    369 					/* average read Kbytes per transfer */
    370 		if (cur.dk_rxfer[dn])
    371 			kbps = (cur.dk_rbytes[dn] / 1024.0) / cur.dk_rxfer[dn];
    372 		else
    373 			kbps = 0.0;
    374 		(void)printf(" %8.2f", kbps);
    375 
    376 					/* average read transfers
    377 					   (per second) */
    378 		(void)printf(" %6.0f", cur.dk_rxfer[dn] / etime);
    379 
    380 					/* time read busy in disk activity */
    381 		atime = (double)cur.dk_time[dn].tv_sec +
    382 		    ((double)cur.dk_time[dn].tv_usec / (double)1000000);
    383 		(void)printf(" %6.2f", atime / etime);
    384 
    385 					/* average read megabytes
    386 					   (per second) */
    387 		(void)printf(" %8.2f",
    388 		    cur.dk_rbytes[dn] / (1024.0 * 1024) / etime);
    389 
    390 
    391 					/* average write Kbytes per transfer */
    392 		if (cur.dk_wxfer[dn])
    393 			kbps = (cur.dk_wbytes[dn] / 1024.0) / cur.dk_wxfer[dn];
    394 		else
    395 			kbps = 0.0;
    396 		(void)printf("   %8.2f", kbps);
    397 
    398 					/* average write transfers
    399 					   (per second) */
    400 		(void)printf(" %6.0f", cur.dk_wxfer[dn] / etime);
    401 
    402 					/* time write busy in disk activity */
    403 		atime = (double)cur.dk_time[dn].tv_sec +
    404 		    ((double)cur.dk_time[dn].tv_usec / (double)1000000);
    405 		(void)printf(" %6.2f", atime / etime);
    406 
    407 					/* average write megabytes
    408 					   (per second) */
    409 		(void)printf(" %8.2f\n",
    410 		    cur.dk_wbytes[dn] / (1024.0 * 1024) / etime);
    411 	}
    412 }
    413 
    414 static void
    415 cpustats(void)
    416 {
    417 	int state;
    418 	double time;
    419 
    420 	time = 0;
    421 	for (state = 0; state < CPUSTATES; ++state)
    422 		time += cur.cp_time[state];
    423 	if (!time)
    424 		time = 1.0;
    425 			/* States are generally never 100% and can use %3.0f. */
    426 	for (state = 0; state < CPUSTATES; ++state)
    427 		printf(" %2.0f", 100. * cur.cp_time[state] / time);
    428 }
    429 
    430 static void
    431 usage(void)
    432 {
    433 
    434 	(void)fprintf(stderr, "usage: iostat [-CdDITx] [-c count] [-M core] "
    435 	    "[-N system] [-w wait] [drives]\n");
    436 	exit(1);
    437 }
    438 
    439 static void
    440 display(void)
    441 {
    442 	double	etime;
    443 
    444 	/* Sum up the elapsed ticks. */
    445 	etime = cur.cp_etime;
    446 
    447 	/*
    448 	 * If we're showing totals only, then don't divide by the
    449 	 * system time.
    450 	 */
    451 	if (ISSET(todo, SHOW_TOTALS))
    452 		etime = 1.0;
    453 
    454 	if (ISSET(todo, SHOW_STATS_X)) {
    455 		disk_statsx(etime);
    456 		goto out;
    457 	}
    458 
    459 	if (ISSET(todo, SHOW_TTY))
    460 		printf("%4.0f %4.0f", cur.tk_nin / etime, cur.tk_nout / etime);
    461 
    462 	if (ISSET(todo, SHOW_STATS_1))
    463 		disk_stats(etime);
    464 
    465 	if (ISSET(todo, SHOW_STATS_2))
    466 		disk_stats2(etime);
    467 
    468 	if (ISSET(todo, SHOW_CPU))
    469 		cpustats();
    470 
    471 	(void)printf("\n");
    472 
    473 out:
    474 	(void)fflush(stdout);
    475 }
    476 
    477 static int
    478 selectdrives(int argc, char *argv[])
    479 {
    480 	int	i, maxdrives, ndrives, tried;
    481 
    482 	/*
    483 	 * Choose drives to be displayed.  Priority goes to (in order) drives
    484 	 * supplied as arguments and default drives.  If everything isn't
    485 	 * filled in and there are drives not taken care of, display the first
    486 	 * few that fit.
    487 	 *
    488 	 * The backward compatibility #ifdefs permit the syntax:
    489 	 *	iostat [ drives ] [ interval [ count ] ]
    490 	 */
    491 
    492 #define	BACKWARD_COMPATIBILITY
    493 	for (tried = ndrives = 0; *argv; ++argv) {
    494 #ifdef BACKWARD_COMPATIBILITY
    495 		if (isdigit(**argv))
    496 			break;
    497 #endif
    498 		tried++;
    499 		for (i = 0; i < dk_ndrive; i++) {
    500 			if (strcmp(cur.dk_name[i], *argv))
    501 				continue;
    502 			cur.dk_select[i] = 1;
    503 			++ndrives;
    504 		}
    505 	}
    506 
    507 	if (ndrives == 0 && tried == 0) {
    508 		/*
    509 		 * Pick up to defdrives (or all if -x is given) drives
    510 		 * if none specified.
    511 		 */
    512 		maxdrives = ISSET(todo, SHOW_STATS_X) ? dk_ndrive : defdrives;
    513 		for (i = 0; i < maxdrives; i++) {
    514 			cur.dk_select[i] = 1;
    515 			++ndrives;
    516 			if (!ISSET(todo, SHOW_STATS_X) && ndrives == defdrives)
    517 				break;
    518 		}
    519 	}
    520 
    521 #ifdef BACKWARD_COMPATIBILITY
    522 	if (*argv) {
    523 		interval = atoi(*argv);
    524 		if (*++argv)
    525 			reps = atoi(*argv);
    526 	}
    527 #endif
    528 
    529 	if (interval) {
    530 		if (!reps)
    531 			reps = -1;
    532 	} else
    533 		if (reps)
    534 			interval = 1;
    535 
    536 	return (ndrives);
    537 }
    538