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