Home | History | Annotate | Line # | Download | only in iostat
iostat.c revision 1.8
      1 /*	$NetBSD: iostat.c,v 1.8 1995/11/28 20:16:31 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1986, 1991, 1993
      5  *	The Regents of the University of California.  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 by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #ifndef lint
     37 static char copyright[] =
     38 "@(#) Copyright (c) 1986, 1991, 1993\n\
     39 	The Regents of the University of California.  All rights reserved.\n";
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)iostat.c	8.2 (Berkeley) 1/26/94";
     45 #else
     46 static char *rcsid = "$NetBSD: iostat.c,v 1.8 1995/11/28 20:16:31 thorpej Exp $";
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/param.h>
     51 #include <sys/buf.h>
     52 #include <sys/dkstat.h>
     53 
     54 #include <err.h>
     55 #include <ctype.h>
     56 #include <fcntl.h>
     57 #include <kvm.h>
     58 #include <limits.h>
     59 #include <nlist.h>
     60 #include <paths.h>
     61 #include <signal.h>
     62 #include <stdio.h>
     63 #include <stdlib.h>
     64 #include <string.h>
     65 #include <unistd.h>
     66 
     67 struct nlist namelist[] = {
     68 #define	X_DK_TIME	0
     69 	{ "_dk_time" },
     70 #define	X_DK_XFER	1
     71 	{ "_dk_xfer" },
     72 #define	X_DK_WDS	2
     73 	{ "_dk_wds" },
     74 #define	X_TK_NIN	3
     75 	{ "_tk_nin" },
     76 #define	X_TK_NOUT	4
     77 	{ "_tk_nout" },
     78 #define	X_DK_SEEK	5
     79 	{ "_dk_seek" },
     80 #define	X_CP_TIME	6
     81 	{ "_cp_time" },
     82 #define	X_DK_WPMS	7
     83 	{ "_dk_wpms" },
     84 #define	X_HZ		8
     85 	{ "_hz" },
     86 #define	X_STATHZ	9
     87 	{ "_stathz" },
     88 #define	X_DK_NDRIVE	10
     89 	{ "_dk_ndrive" },
     90 #define	X_END		10
     91 #if defined(hp300) || defined(luna68k)
     92 #define	X_HPDINIT	(X_END+1)
     93 	{ "_hp_dinit" },
     94 #endif
     95 #ifdef mips
     96 #define	X_SCSI_DINIT	(X_END+1)
     97 	{ "_scsi_dinit" },
     98 #endif
     99 #ifdef tahoe
    100 #define	X_VBDINIT	(X_END+1)
    101 	{ "_vbdinit" },
    102 #endif
    103 #ifdef vax
    104 	{ "_mbdinit" },
    105 #define X_MBDINIT	(X_END+1)
    106 	{ "_ubdinit" },
    107 #define X_UBDINIT	(X_END+2)
    108 #endif
    109 	{ NULL },
    110 };
    111 
    112 struct _disk {
    113 	long	cp_time[CPUSTATES];
    114 	long	*dk_time;
    115 	long	*dk_wds;
    116 	long	*dk_seek;
    117 	long	*dk_xfer;
    118 	long	tk_nin;
    119 	long	tk_nout;
    120 } cur, last;
    121 
    122 kvm_t	 *kd;
    123 double	  etime;
    124 long	 *dk_wpms;
    125 int	  dk_ndrive, *dr_select, hz, kmemfd, ndrives;
    126 char	**dr_name;
    127 
    128 #define nlread(x, v) \
    129 	kvm_read(kd, namelist[x].n_value, &(v), sizeof(v))
    130 
    131 #include "names.c"				/* XXX */
    132 
    133 void cpustats __P((void));
    134 void dkstats __P((void));
    135 void phdr __P((int));
    136 void usage __P((void));
    137 
    138 int
    139 main(argc, argv)
    140 	int argc;
    141 	char *argv[];
    142 {
    143 	register int i;
    144 	long tmp;
    145 	int ch, hdrcnt, reps, interval, stathz, ndrives;
    146 	char **cp, *memf, *nlistf, buf[30];
    147         char errbuf[_POSIX2_LINE_MAX];
    148 
    149 	interval = reps = 0;
    150 	nlistf = memf = NULL;
    151 	while ((ch = getopt(argc, argv, "c:M:N:w:")) != EOF)
    152 		switch(ch) {
    153 		case 'c':
    154 			if ((reps = atoi(optarg)) <= 0)
    155 				errx(1, "repetition count <= 0.");
    156 			break;
    157 		case 'M':
    158 			memf = optarg;
    159 			break;
    160 		case 'N':
    161 			nlistf = optarg;
    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 	/*
    175 	 * Discard setgid privileges if not the running kernel so that bad
    176 	 * guys can't print interesting stuff from kernel memory.
    177 	 */
    178 	if (nlistf != NULL || memf != NULL)
    179 		setgid(getgid());
    180 
    181         kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
    182 	if (kd == 0)
    183 		errx(1, "kvm_openfiles: %s", errbuf);
    184 	if (kvm_nlist(kd, namelist) == -1)
    185 		errx(1, "kvm_nlist: %s", kvm_geterr(kd));
    186 	if (namelist[X_DK_NDRIVE].n_type == 0)
    187 		errx(1, "dk_ndrive not found in namelist");
    188 	(void)nlread(X_DK_NDRIVE, dk_ndrive);
    189 	if (dk_ndrive <= 0)
    190 		errx(1, "invalid dk_ndrive %d\n", dk_ndrive);
    191 
    192 	cur.dk_time = calloc(dk_ndrive, sizeof(long));
    193 	cur.dk_wds = calloc(dk_ndrive, sizeof(long));
    194 	cur.dk_seek = calloc(dk_ndrive, sizeof(long));
    195 	cur.dk_xfer = calloc(dk_ndrive, sizeof(long));
    196 	last.dk_time = calloc(dk_ndrive, sizeof(long));
    197 	last.dk_wds = calloc(dk_ndrive, sizeof(long));
    198 	last.dk_seek = calloc(dk_ndrive, sizeof(long));
    199 	last.dk_xfer = calloc(dk_ndrive, sizeof(long));
    200 	dr_select = calloc(dk_ndrive, sizeof(int));
    201 	dr_name = calloc(dk_ndrive, sizeof(char *));
    202 	dk_wpms = calloc(dk_ndrive, sizeof(long));
    203 
    204 	for (i = 0; i < dk_ndrive; i++) {
    205 		(void)sprintf(buf, "dk%d", i);
    206 		dr_name[i] = strdup(buf);
    207 	}
    208 	if (!read_names())
    209 		exit(1);
    210 	(void)nlread(X_HZ, hz);
    211 	(void)nlread(X_STATHZ, stathz);
    212 	if (stathz)
    213 		hz = stathz;
    214 	(void)kvm_read(kd, namelist[X_DK_WPMS].n_value, dk_wpms,
    215 		dk_ndrive * sizeof(dk_wpms));
    216 
    217 	/*
    218 	 * Choose drives to be displayed.  Priority goes to (in order) drives
    219 	 * supplied as arguments and default drives.  If everything isn't
    220 	 * filled in and there are drives not taken care of, display the first
    221 	 * few that fit.
    222 	 *
    223 	 * The backward compatibility #ifdefs permit the syntax:
    224 	 *	iostat [ drives ] [ interval [ count ] ]
    225 	 */
    226 #define	BACKWARD_COMPATIBILITY
    227 	for (ndrives = 0; *argv; ++argv) {
    228 #ifdef	BACKWARD_COMPATIBILITY
    229 		if (isdigit(**argv))
    230 			break;
    231 #endif
    232 		for (i = 0; i < dk_ndrive; i++) {
    233 			if (strcmp(dr_name[i], *argv))
    234 				continue;
    235 			dr_select[i] = 1;
    236 			++ndrives;
    237 		}
    238 	}
    239 #ifdef	BACKWARD_COMPATIBILITY
    240 	if (*argv) {
    241 		interval = atoi(*argv);
    242 		if (*++argv)
    243 			reps = atoi(*argv);
    244 	}
    245 #endif
    246 
    247 	if (interval) {
    248 		if (!reps)
    249 			reps = -1;
    250 	} else
    251 		if (reps)
    252 			interval = 1;
    253 
    254 	for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
    255 		if (dr_select[i] || dk_wpms[i] == 0)
    256 			continue;
    257 		for (cp = defdrives; *cp; cp++)
    258 			if (strcmp(dr_name[i], *cp) == 0) {
    259 				dr_select[i] = 1;
    260 				++ndrives;
    261 				break;
    262 			}
    263 	}
    264 	for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
    265 		if (dr_select[i])
    266 			continue;
    267 		dr_select[i] = 1;
    268 		++ndrives;
    269 	}
    270 
    271 	(void)signal(SIGCONT, phdr);
    272 
    273 	for (hdrcnt = 1;;) {
    274 		if (!--hdrcnt) {
    275 			phdr(0);
    276 			hdrcnt = 20;
    277 		}
    278 		(void)kvm_read(kd, namelist[X_DK_TIME].n_value,
    279 		    cur.dk_time, dk_ndrive * sizeof(long));
    280 		(void)kvm_read(kd, namelist[X_DK_XFER].n_value,
    281 		    cur.dk_xfer, dk_ndrive * sizeof(long));
    282 		(void)kvm_read(kd, namelist[X_DK_WDS].n_value,
    283 		    cur.dk_wds, dk_ndrive * sizeof(long));
    284 		(void)kvm_read(kd, namelist[X_DK_SEEK].n_value,
    285 		    cur.dk_seek, dk_ndrive * sizeof(long));
    286 		(void)kvm_read(kd, namelist[X_TK_NIN].n_value,
    287 		    &cur.tk_nin, sizeof(cur.tk_nin));
    288 		(void)kvm_read(kd, namelist[X_TK_NOUT].n_value,
    289 		    &cur.tk_nout, sizeof(cur.tk_nout));
    290 		(void)kvm_read(kd, namelist[X_CP_TIME].n_value,
    291 		    cur.cp_time, sizeof(cur.cp_time));
    292 		for (i = 0; i < dk_ndrive; i++) {
    293 			if (!dr_select[i])
    294 				continue;
    295 #define X(fld)	tmp = cur.fld[i]; cur.fld[i] -= last.fld[i]; last.fld[i] = tmp
    296 			X(dk_xfer);
    297 			X(dk_seek);
    298 			X(dk_wds);
    299 			X(dk_time);
    300 		}
    301 		tmp = cur.tk_nin;
    302 		cur.tk_nin -= last.tk_nin;
    303 		last.tk_nin = tmp;
    304 		tmp = cur.tk_nout;
    305 		cur.tk_nout -= last.tk_nout;
    306 		last.tk_nout = tmp;
    307 		etime = 0;
    308 		for (i = 0; i < CPUSTATES; i++) {
    309 			X(cp_time);
    310 			etime += cur.cp_time[i];
    311 		}
    312 		if (etime == 0.0)
    313 			etime = 1.0;
    314 		etime /= (float)hz;
    315 		(void)printf("%4.0f%5.0f",
    316 		    cur.tk_nin / etime, cur.tk_nout / etime);
    317 		dkstats();
    318 		cpustats();
    319 		(void)printf("\n");
    320 		(void)fflush(stdout);
    321 
    322 		if (reps >= 0 && --reps <= 0)
    323 			break;
    324 		(void)sleep(interval);
    325 	}
    326 	exit(0);
    327 }
    328 
    329 /* ARGUSED */
    330 void
    331 phdr(signo)
    332 	int signo;
    333 {
    334 	register int i;
    335 
    336 	(void)printf("      tty");
    337 	for (i = 0; i < dk_ndrive; i++)
    338 		if (dr_select[i])
    339 			(void)printf("          %3.3s ", dr_name[i]);
    340 	(void)printf("         cpu\n tin tout");
    341 	for (i = 0; i < dk_ndrive; i++)
    342 		if (dr_select[i])
    343 			(void)printf(" sps tps msps ");
    344 	(void)printf(" us ni sy in id\n");
    345 }
    346 
    347 void
    348 dkstats()
    349 {
    350 	register int dn;
    351 	double atime, itime, msps, words, xtime;
    352 
    353 	for (dn = 0; dn < dk_ndrive; ++dn) {
    354 		if (!dr_select[dn])
    355 			continue;
    356 		words = cur.dk_wds[dn] * 32;		/* words xfer'd */
    357 		(void)printf("%4.0f",			/* sectors */
    358 		    words / (DEV_BSIZE / 2) / etime);
    359 
    360 		(void)printf("%4.0f", cur.dk_xfer[dn] / etime);
    361 
    362 		if (dk_wpms[dn] && cur.dk_xfer[dn]) {
    363 			atime = cur.dk_time[dn];	/* ticks disk busy */
    364 			atime /= (float)hz;		/* ticks to seconds */
    365 			xtime = words / dk_wpms[dn];	/* transfer time */
    366 			itime = atime - xtime;		/* time not xfer'ing */
    367 			if (itime < 0)
    368 				msps = 0;
    369 			else
    370 				msps = itime * 1000 / cur.dk_xfer[dn];
    371 		} else
    372 			msps = 0;
    373 		(void)printf("%5.1f ", msps);
    374 	}
    375 }
    376 
    377 void
    378 cpustats()
    379 {
    380 	register int state;
    381 	double time;
    382 
    383 	time = 0;
    384 	for (state = 0; state < CPUSTATES; ++state)
    385 		time += cur.cp_time[state];
    386 	for (state = 0; state < CPUSTATES; ++state)
    387 		(void)printf("%3.0f",
    388 		    100. * cur.cp_time[state] / (time ? time : 1));
    389 }
    390 
    391 void
    392 usage()
    393 {
    394 	(void)fprintf(stderr,
    395 "usage: iostat [-c count] [-M core] [-N system] [-w wait] [drives]\n");
    396 	exit(1);
    397 }
    398