Home | History | Annotate | Line # | Download | only in iostat
iostat.c revision 1.14
      1 /*	$NetBSD: iostat.c,v 1.14 1998/07/19 17:48:15 drochner 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.14 1998/07/19 17:48:15 drochner Exp $");
     79 #endif
     80 #endif /* not lint */
     81 
     82 #include <sys/types.h>
     83 #include <sys/dkstat.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 /* Defined in dkstats.c */
     97 extern struct _disk cur;
     98 extern int  	dk_ndrive;
     99 
    100 /* Namelist and memory files. */
    101 char	*nlistf, *memf;
    102 
    103 int		hz, reps, interval;
    104 static int	todo = 0;
    105 
    106 #define ISSET(x, a)	((x) & (a))
    107 #define SHOW_CPU	0x0001
    108 #define SHOW_TTY	0x0002
    109 #define SHOW_STATS_1	0x0004
    110 #define SHOW_STATS_2	0x0008
    111 #define SHOW_TOTALS	0x0080
    112 
    113 static void cpustats __P((void));
    114 static void disk_stats __P((double));
    115 static void disk_stats2 __P((double));
    116 static void header __P((int));
    117 static void usage __P((void));
    118 static void display __P((void));
    119 static void selectdrives __P((int, char **));
    120 
    121 void dkswap __P((void));
    122 void dkreadstats __P((void));
    123 int dkinit __P((int, gid_t));
    124 int main __P((int, char **));
    125 
    126 int
    127 main(argc, argv)
    128 	int argc;
    129 	char *argv[];
    130 {
    131 	int ch, hdrcnt;
    132 	struct timeval	tv;
    133 	gid_t egid = getegid();
    134 	setegid(getgid());
    135 
    136 	while ((ch = getopt(argc, argv, "Cc:dDIM:N:Tw:")) != -1)
    137 		switch(ch) {
    138 		case 'c':
    139 			if ((reps = atoi(optarg)) <= 0)
    140 				errx(1, "repetition count <= 0.");
    141 			break;
    142 		case 'C':
    143 			todo |= SHOW_CPU;
    144 			break;
    145 		case 'd':
    146 			todo |= SHOW_STATS_1;
    147 			break;
    148 		case 'D':
    149 			todo |= SHOW_STATS_2;
    150 			break;
    151 		case 'I':
    152 			todo |= SHOW_TOTALS;
    153 			break;
    154 		case 'M':
    155 			memf = optarg;
    156 			break;
    157 		case 'N':
    158 			nlistf = optarg;
    159 			break;
    160 		case 'T':
    161 			todo |= SHOW_TTY;
    162 			break;
    163 		case 'w':
    164 			if ((interval = atoi(optarg)) <= 0)
    165 				errx(1, "interval <= 0.");
    166 			break;
    167 		case '?':
    168 		default:
    169 			usage();
    170 		}
    171 	argc -= optind;
    172 	argv += optind;
    173 
    174 	if (!ISSET(todo, SHOW_CPU | SHOW_TTY | SHOW_STATS_1 | SHOW_STATS_2))
    175 		todo |= SHOW_CPU | SHOW_TTY | SHOW_STATS_1;
    176 
    177 	/*
    178 	 * Discard setgid privileges if not the running kernel so that bad
    179 	 * guys can't print interesting stuff from kernel memory.
    180 	 */
    181 	if (nlistf != NULL || memf != NULL)
    182 		setgid(getgid());
    183 
    184 	dkinit(0, egid);
    185 	dkreadstats();
    186 	selectdrives(argc, argv);
    187 
    188 	tv.tv_sec = interval;
    189 	tv.tv_usec = 0;
    190 
    191 	/* print a new header on sigcont */
    192 	(void)signal(SIGCONT, header);
    193 
    194 	for (hdrcnt = 1;;) {
    195 		if (!--hdrcnt) {
    196 			header(0);
    197 			hdrcnt = 20;
    198 		}
    199 
    200 		if (!ISSET(todo, SHOW_TOTALS))
    201 			dkswap();
    202 		display();
    203 
    204 		if (reps >= 0 && --reps <= 0)
    205 			break;
    206 		select(0, NULL, NULL, NULL, &tv);
    207 		dkreadstats();
    208 	}
    209 	exit(0);
    210 }
    211 
    212 static void
    213 header(signo)
    214 	int signo;
    215 {
    216 	int i;
    217 
    218 	/* Main Headers. */
    219 	if (ISSET(todo, SHOW_TTY))
    220 		(void)printf("      tty");
    221 
    222 	if (ISSET(todo, SHOW_STATS_1))
    223 	for (i = 0; i < dk_ndrive; i++)
    224 		if (cur.dk_select[i])
    225 			(void)printf("            %3.3s ", cur.dk_name[i]);
    226 
    227 	if (ISSET(todo, SHOW_STATS_2))
    228 	for (i = 0; i < dk_ndrive; i++)
    229 		if (cur.dk_select[i])
    230 			(void)printf("           %3.3s ", cur.dk_name[i]);
    231 
    232 	if (ISSET(todo, SHOW_CPU))
    233 		(void)printf("            cpu");
    234 	printf("\n");
    235 
    236 	/* Sub-Headers. */
    237 	if (ISSET(todo, SHOW_TTY))
    238 		printf(" tin tout");
    239 
    240 	if (ISSET(todo, SHOW_STATS_1))
    241 	for (i = 0; i < dk_ndrive; i++)
    242 		if (cur.dk_select[i])
    243 			if (ISSET(todo, SHOW_TOTALS))
    244 				(void)printf("  KB/t xfr MB   ");
    245 			else
    246 				(void)printf("  KB/t t/s MB/s ");
    247 
    248 	if (ISSET(todo, SHOW_STATS_2))
    249 	for (i = 0; i < dk_ndrive; i++)
    250 		if (cur.dk_select[i])
    251 			(void)printf("   KB xfr time ");
    252 
    253 	if (ISSET(todo, SHOW_CPU))
    254 		(void)printf(" us ni sy in id");
    255 	printf("\n");
    256 }
    257 
    258 static void
    259 disk_stats(etime)
    260 	double etime;
    261 {
    262 	int dn;
    263 	double atime, mbps;
    264 
    265 	for (dn = 0; dn < dk_ndrive; ++dn) {
    266 		if (!cur.dk_select[dn])
    267 			continue;
    268 
    269 		/* average Kbytes per transfer. */
    270 		if (cur.dk_xfer[dn])
    271 			mbps = (cur.dk_bytes[dn] / (1024.0)) / cur.dk_xfer[dn];
    272 		else
    273 			mbps = 0.0;
    274 		(void)printf(" %5.2f", mbps);
    275 
    276 		/* average transfers per second. */
    277 		(void)printf(" %3.0f", cur.dk_xfer[dn] / etime);
    278 
    279 		/* time busy in disk activity */
    280 		atime = (double)cur.dk_time[dn].tv_sec +
    281 			((double)cur.dk_time[dn].tv_usec / (double)1000000);
    282 
    283 		/* Megabytes per second. */
    284 		if (atime != 0.0)
    285 			mbps = cur.dk_bytes[dn] / (double)(1024 * 1024);
    286 		else
    287 			mbps = 0;
    288 		(void)printf(" %4.2f ", mbps / etime);
    289 	}
    290 }
    291 
    292 static void
    293 disk_stats2(etime)
    294 	double etime;
    295 {
    296 	int dn;
    297 	double atime;
    298 
    299 	for (dn = 0; dn < dk_ndrive; ++dn) {
    300 		if (!cur.dk_select[dn])
    301 			continue;
    302 
    303 		/* average kbytes per second. */
    304 		(void)printf(" %4.0f", cur.dk_bytes[dn] / (1024.0) / etime);
    305 
    306 		/* average transfers per second. */
    307 		(void)printf(" %3.0f", cur.dk_xfer[dn] / etime);
    308 
    309 		/* average time busy in disk activity. */
    310 		atime = (double)cur.dk_time[dn].tv_sec +
    311 			((double)cur.dk_time[dn].tv_usec / (double)1000000);
    312 		(void)printf(" %4.2f ", atime / etime);
    313 	}
    314 }
    315 
    316 static void
    317 cpustats()
    318 {
    319 	int state;
    320 	double time;
    321 
    322 	time = 0;
    323 	for (state = 0; state < CPUSTATES; ++state)
    324 		time += cur.cp_time[state];
    325 	if (!time)
    326 		time = 1.0;
    327 	/* States are generally never 100% and can use %3.0f. */
    328 	for (state = 0; state < CPUSTATES; ++state)
    329 		printf("%3.0f", 100. * cur.cp_time[state] / time);
    330 }
    331 
    332 static void
    333 usage()
    334 {
    335 
    336 	(void)fprintf(stderr,
    337 "usage: iostat [-CdDIT] [-c count] [-M core] [-N system] [-w wait] [drives]\n");
    338 	exit(1);
    339 }
    340 
    341 static void
    342 display()
    343 {
    344 	int	i;
    345 	double	etime;
    346 
    347 	/* Sum up the elapsed ticks. */
    348 	etime = 0.0;
    349 	for (i = 0; i < CPUSTATES; i++) {
    350 		etime += cur.cp_time[i];
    351 	}
    352 	if (etime == 0.0)
    353 		etime = 1.0;
    354 	/* Convert to seconds. */
    355 	etime /= (float)hz;
    356 
    357 	/* If we're showing totals only, then don't divide by the
    358 	 * system time.
    359 	 */
    360 	if (ISSET(todo, SHOW_TOTALS))
    361 		etime = 1.0;
    362 
    363 	if (ISSET(todo, SHOW_TTY))
    364 		printf("%4.0f %4.0f", cur.tk_nin / etime, cur.tk_nout / etime);
    365 
    366 	if (ISSET(todo, SHOW_STATS_1))
    367 		disk_stats(etime);
    368 
    369 	if (ISSET(todo, SHOW_STATS_2))
    370 		disk_stats2(etime);
    371 
    372 	if (ISSET(todo, SHOW_CPU))
    373 		cpustats();
    374 
    375 	(void)printf("\n");
    376 	(void)fflush(stdout);
    377 }
    378 
    379 static void
    380 selectdrives(argc, argv)
    381 	int	argc;
    382 	char	*argv[];
    383 {
    384 	int	i, ndrives;
    385 
    386 	/*
    387 	 * Choose drives to be displayed.  Priority goes to (in order) drives
    388 	 * supplied as arguments and default drives.  If everything isn't
    389 	 * filled in and there are drives not taken care of, display the first
    390 	 * few that fit.
    391 	 *
    392 	 * The backward compatibility #ifdefs permit the syntax:
    393 	 *	iostat [ drives ] [ interval [ count ] ]
    394 	 */
    395 #define	BACKWARD_COMPATIBILITY
    396 	for (ndrives = 0; *argv; ++argv) {
    397 #ifdef	BACKWARD_COMPATIBILITY
    398 		if (isdigit(**argv))
    399 			break;
    400 #endif
    401 		for (i = 0; i < dk_ndrive; i++) {
    402 			if (strcmp(cur.dk_name[i], *argv))
    403 				continue;
    404 			cur.dk_select[i] = 1;
    405 			++ndrives;
    406 		}
    407 	}
    408 #ifdef	BACKWARD_COMPATIBILITY
    409 	if (*argv) {
    410 		interval = atoi(*argv);
    411 		if (*++argv)
    412 			reps = atoi(*argv);
    413 	}
    414 #endif
    415 
    416 	if (interval) {
    417 		if (!reps)
    418 			reps = -1;
    419 	} else
    420 		if (reps)
    421 			interval = 1;
    422 
    423 	/* Pick up to 4 drives if none specified. */
    424 	if (ndrives == 0)
    425 		for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
    426 			if (cur.dk_select[i])
    427 				continue;
    428 			cur.dk_select[i] = 1;
    429 			++ndrives;
    430 		}
    431 }
    432