Home | History | Annotate | Line # | Download | only in iostat
iostat.c revision 1.32
      1 /*	$NetBSD: iostat.c,v 1.32 2002/11/02 06:35:30 enami 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.32 2002/11/02 06:35:30 enami 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 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 	selectdrives(argc, argv);
    183 
    184 	tv.tv_sec = interval;
    185 	tv.tv_nsec = 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 		nanosleep(&tv, NULL);
    203 		dkreadstats();
    204 	}
    205 	exit(0);
    206 }
    207 
    208 static void
    209 header(int signo)
    210 {
    211 	int i;
    212 
    213 					/* Main Headers. */
    214 	if (ISSET(todo, SHOW_STATS_X)) {
    215 		if (ISSET(todo, SHOW_TOTALS)) {
    216 			(void)printf(
    217 			    "device  read KB/t    xfr   time     MB/s");
    218 			(void)printf(" write KB/t    xfr   time     MB/s\n");
    219 		} else {
    220 			(void)printf(
    221 			    "device  read KB/t    r/s   time     MB/s");
    222 			(void)printf(" write KB/t    w/s   time     MB/s\n");
    223 		}
    224 		return;
    225 	}
    226 
    227 	if (ISSET(todo, SHOW_TTY))
    228 		(void)printf("      tty");
    229 
    230 	if (ISSET(todo, SHOW_STATS_1))
    231 		for (i = 0; i < dk_ndrive; i++)
    232 			if (cur.dk_select[i])
    233 				(void)printf("        %7.7s ", cur.dk_name[i]);
    234 
    235 	if (ISSET(todo, SHOW_STATS_2))
    236 		for (i = 0; i < dk_ndrive; i++)
    237 			if (cur.dk_select[i])
    238 				(void)printf("        %7.7s ", cur.dk_name[i]);
    239 
    240 	if (ISSET(todo, SHOW_CPU))
    241 		(void)printf("            cpu");
    242 
    243 	printf("\n");
    244 
    245 					/* Sub-Headers. */
    246 	if (ISSET(todo, SHOW_TTY))
    247 		printf(" tin tout");
    248 
    249 	if (ISSET(todo, SHOW_STATS_1)) {
    250 		for (i = 0; i < dk_ndrive; i++)
    251 			if (cur.dk_select[i]) {
    252 				if (ISSET(todo, SHOW_TOTALS))
    253 					(void)printf("  KB/t xfr MB   ");
    254 				else
    255 					(void)printf("  KB/t t/s MB/s ");
    256 			}
    257 	}
    258 
    259 	if (ISSET(todo, SHOW_STATS_2))
    260 		for (i = 0; i < dk_ndrive; i++)
    261 			if (cur.dk_select[i])
    262 				(void)printf("   KB xfr time ");
    263 
    264 	if (ISSET(todo, SHOW_CPU))
    265 		(void)printf(" us ni sy in id");
    266 	printf("\n");
    267 }
    268 
    269 static void
    270 disk_stats(double etime)
    271 {
    272 	int dn;
    273 	double atime, mbps;
    274 
    275 	for (dn = 0; dn < dk_ndrive; ++dn) {
    276 		if (!cur.dk_select[dn])
    277 			continue;
    278 					/* average Kbytes per transfer. */
    279 		if (cur.dk_rxfer[dn] + cur.dk_wxfer[dn])
    280 			mbps = ((cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) /
    281 			    1024.0) / (cur.dk_rxfer[dn] + cur.dk_wxfer[dn]);
    282 		else
    283 			mbps = 0.0;
    284 		(void)printf(" %5.2f", mbps);
    285 
    286 					/* average transfers per second. */
    287 		(void)printf(" %3.0f",
    288 		    (cur.dk_rxfer[dn] + cur.dk_wxfer[dn]) / etime);
    289 
    290 					/* time busy in disk activity */
    291 		atime = (double)cur.dk_time[dn].tv_sec +
    292 		    ((double)cur.dk_time[dn].tv_usec / (double)1000000);
    293 
    294 					/* Megabytes per second. */
    295 		if (atime != 0.0)
    296 			mbps = (cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) /
    297 			    (double)(1024 * 1024);
    298 		else
    299 			mbps = 0;
    300 		(void)printf(" %4.2f ", mbps / etime);
    301 	}
    302 }
    303 
    304 static void
    305 disk_stats2(double etime)
    306 {
    307 	int dn;
    308 	double atime;
    309 
    310 	for (dn = 0; dn < dk_ndrive; ++dn) {
    311 		if (!cur.dk_select[dn])
    312 			continue;
    313 
    314 					/* average kbytes per second. */
    315 		(void)printf(" %4.0f",
    316 		    (cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) / 1024.0 / etime);
    317 
    318 					/* average transfers per second. */
    319 		(void)printf(" %3.0f",
    320 		    (cur.dk_rxfer[dn] + cur.dk_wxfer[dn]) / etime);
    321 
    322 					/* average time busy in disk activity */
    323 		atime = (double)cur.dk_time[dn].tv_sec +
    324 		    ((double)cur.dk_time[dn].tv_usec / (double)1000000);
    325 		(void)printf(" %4.2f ", atime / etime);
    326 	}
    327 }
    328 
    329 static void
    330 disk_statsx(double etime)
    331 {
    332 	int dn;
    333 	double atime, kbps;
    334 
    335 	for (dn = 0; dn < dk_ndrive; ++dn) {
    336 		(void)printf("%-8.8s", cur.dk_name[dn]);
    337 
    338 					/* average read Kbytes per transfer */
    339 		if (cur.dk_rxfer[dn])
    340 			kbps = (cur.dk_rbytes[dn] / 1024.0) / cur.dk_rxfer[dn];
    341 		else
    342 			kbps = 0.0;
    343 		(void)printf(" %8.2f", kbps);
    344 
    345 					/* average read transfers
    346 					   (per second) */
    347 		(void)printf(" %6.0f", cur.dk_rxfer[dn] / etime);
    348 
    349 					/* time read busy in disk activity */
    350 		atime = (double)cur.dk_time[dn].tv_sec +
    351 		    ((double)cur.dk_time[dn].tv_usec / (double)1000000);
    352 		(void)printf(" %6.2f", atime / etime);
    353 
    354 					/* average read megabytes
    355 					   (per second) */
    356 		(void)printf(" %8.2f",
    357 		    cur.dk_rbytes[dn] / (1024.0 * 1024) / etime);
    358 
    359 
    360 					/* average write Kbytes per transfer */
    361 		if (cur.dk_wxfer[dn])
    362 			kbps = (cur.dk_wbytes[dn] / 1024.0) / cur.dk_wxfer[dn];
    363 		else
    364 			kbps = 0.0;
    365 		(void)printf("   %8.2f", kbps);
    366 
    367 					/* average write transfers
    368 					   (per second) */
    369 		(void)printf(" %6.0f", cur.dk_wxfer[dn] / etime);
    370 
    371 					/* time write busy in disk activity */
    372 		atime = (double)cur.dk_time[dn].tv_sec +
    373 		    ((double)cur.dk_time[dn].tv_usec / (double)1000000);
    374 		(void)printf(" %6.2f", atime / etime);
    375 
    376 					/* average write megabytes
    377 					   (per second) */
    378 		(void)printf(" %8.2f",
    379 		    cur.dk_wbytes[dn] / (1024.0 * 1024) / etime);
    380 		/*
    381 		 * Our caller prints a newline; we need one for every line
    382 		 * except the last.
    383 		 */
    384 		if (dn < dk_ndrive -1)
    385 			(void)printf("\n");
    386 	}
    387 }
    388 
    389 static void
    390 cpustats(void)
    391 {
    392 	int state;
    393 	double time;
    394 
    395 	time = 0;
    396 	for (state = 0; state < CPUSTATES; ++state)
    397 		time += cur.cp_time[state];
    398 	if (!time)
    399 		time = 1.0;
    400 			/* States are generally never 100% and can use %3.0f. */
    401 	for (state = 0; state < CPUSTATES; ++state)
    402 		printf(" %2.0f", 100. * cur.cp_time[state] / time);
    403 }
    404 
    405 static void
    406 usage(void)
    407 {
    408 
    409 	(void)fprintf(stderr, "usage: iostat [-CdDITx] [-c count] [-M core] "
    410 	    "[-N system] [-w wait] [drives]\n");
    411 	exit(1);
    412 }
    413 
    414 static void
    415 display(void)
    416 {
    417 	double	etime;
    418 
    419 	/* Sum up the elapsed ticks. */
    420 	etime = cur.cp_etime;
    421 
    422 	/* If we're showing totals only, then don't divide by the
    423 	 * system time.
    424 	 */
    425 	if (ISSET(todo, SHOW_TOTALS))
    426 		etime = 1.0;
    427 
    428 	if (ISSET(todo, SHOW_TTY))
    429 		printf("%4.0f %4.0f", cur.tk_nin / etime, cur.tk_nout / etime);
    430 
    431 	if (ISSET(todo, SHOW_STATS_1))
    432 		disk_stats(etime);
    433 
    434 	if (ISSET(todo, SHOW_STATS_2))
    435 		disk_stats2(etime);
    436 
    437 	if (ISSET(todo, SHOW_STATS_X))
    438 		disk_statsx(etime);
    439 
    440 	if (ISSET(todo, SHOW_CPU))
    441 		cpustats();
    442 
    443 	(void)printf("\n");
    444 	(void)fflush(stdout);
    445 }
    446 
    447 static void
    448 selectdrives(int argc, char *argv[])
    449 {
    450 	int	i, ndrives;
    451 
    452 	/*
    453 	 * Choose drives to be displayed.  Priority goes to (in order) drives
    454 	 * supplied as arguments and default drives.  If everything isn't
    455 	 * filled in and there are drives not taken care of, display the first
    456 	 * few that fit.
    457 	 *
    458 	 * The backward compatibility #ifdefs permit the syntax:
    459 	 *	iostat [ drives ] [ interval [ count ] ]
    460 	 */
    461 	if (ISSET(todo, SHOW_STATS_X)) {
    462 		for (i = 0; i < dk_ndrive; i++)
    463 			cur.dk_select[i] = 1;
    464 	}
    465 
    466 #define	BACKWARD_COMPATIBILITY
    467 	for (ndrives = 0; *argv; ++argv) {
    468 #ifdef	BACKWARD_COMPATIBILITY
    469 		if (isdigit(**argv))
    470 			break;
    471 #endif
    472 		if (!ISSET(todo, SHOW_STATS_X))
    473 			for (i = 0; i < dk_ndrive; i++) {
    474 				if (strcmp(cur.dk_name[i], *argv))
    475 					continue;
    476 				cur.dk_select[i] = 1;
    477 				++ndrives;
    478 			}
    479 	}
    480 #ifdef	BACKWARD_COMPATIBILITY
    481 	if (*argv) {
    482 		interval = atoi(*argv);
    483 		if (*++argv)
    484 			reps = atoi(*argv);
    485 	}
    486 #endif
    487 
    488 	if (interval) {
    489 		if (!reps)
    490 			reps = -1;
    491 	} else
    492 		if (reps)
    493 			interval = 1;
    494 
    495 	/* Pick up to 4 drives if none specified. */
    496 	if (ndrives == 0)
    497 		for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
    498 			if (cur.dk_select[i])
    499 				continue;
    500 			cur.dk_select[i] = 1;
    501 			++ndrives;
    502 		}
    503 }
    504