Home | History | Annotate | Line # | Download | only in iostat
iostat.c revision 1.69
      1  1.69       kre /*	$NetBSD: iostat.c,v 1.69 2022/06/18 11:33:13 kre 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.69       kre __RCSID("$NetBSD: iostat.c,v 1.69 2022/06/18 11:33:13 kre 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.69       kre static int *order, ordersize;
    103  1.69       kre 
    104  1.69       kre static char Line_Marker[] = "________________________________________________";
    105  1.69       kre 
    106  1.54  jakllsch #define	MAX(a,b)	(((a)>(b))?(a):(b))
    107  1.69       kre #define	MIN(a,b)	(((a)<(b))?(a):(b))
    108  1.54  jakllsch 
    109  1.36    simonb #define	ISSET(x, a)	((x) & (a))
    110  1.51  christos #define	SHOW_CPU	(1<<0)
    111  1.51  christos #define	SHOW_TTY	(1<<1)
    112  1.51  christos #define	SHOW_STATS_1	(1<<2)
    113  1.51  christos #define	SHOW_STATS_2	(1<<3)
    114  1.51  christos #define	SHOW_STATS_X	(1<<4)
    115  1.64   mlelstv #define	SHOW_STATS_Y	(1<<5)
    116  1.69       kre #define	SHOW_UPDATES	(1<<6)
    117  1.51  christos #define	SHOW_TOTALS	(1<<7)
    118  1.69       kre #define	SHOW_NEW_TOTALS	(1<<8)
    119  1.69       kre #define	SUPPRESS_ZERO	(1<<9)
    120  1.69       kre 
    121  1.69       kre #define	SHOW_STATS_ALL	(SHOW_STATS_1 | SHOW_STATS_2 |	\
    122  1.69       kre 			    SHOW_STATS_X | SHOW_STATS_Y)
    123  1.69       kre 
    124  1.69       kre /*
    125  1.69       kre  * Decide how many screen columns each output statistic is given
    126  1.69       kre  * (these are determined empirically ("looks good to me") and likely
    127  1.69       kre  * will require changes from time to time as technology advances).
    128  1.69       kre  *
    129  1.69       kre  * The odd "+ N" at the end of the summary (total width of stat) definition
    130  1.69       kre  * allows for the gaps between the columns, and is (#data cols - 1).
    131  1.69       kre  * So, tty stats have "in" and "out", 2 columns, so there is 1 extra space,
    132  1.69       kre  * whereas the cpu stats have 5 columns, so 4 extra spaces (etc).
    133  1.69       kre  */
    134  1.69       kre #define	LAYOUT_TTY_IN	4	/* tty input in last interval */
    135  1.69       kre #define	LAYOUT_TTY_TIN	7	/* tty input forever */
    136  1.69       kre #define	LAYOUT_TTY_OUT	5	/* tty output in last interval */
    137  1.69       kre #define	LAYOUT_TTY_TOUT	10	/* tty output forever */
    138  1.69       kre #define	LAYOUT_TTY	(((todo & SHOW_TOTALS)				     \
    139  1.69       kre 				? (LAYOUT_TTY_TIN + LAYOUT_TTY_TOUT)	     \
    140  1.69       kre 				: (LAYOUT_TTY_IN + LAYOUT_TTY_OUT)) + 1)
    141  1.69       kre #define	LAYOUT_TTY_GAP	0		/* always starts at left margin */
    142  1.69       kre 
    143  1.69       kre #define	LAYOUT_CPU_USER	2
    144  1.69       kre #define	LAYOUT_CPU_NICE	2
    145  1.69       kre #define	LAYOUT_CPU_SYS	2
    146  1.69       kre #define	LAYOUT_CPU_INT	2
    147  1.69       kre #define	LAYOUT_CPU_IDLE	3
    148  1.69       kre #define	LAYOUT_CPU	(LAYOUT_CPU_USER + LAYOUT_CPU_NICE + LAYOUT_CPU_SYS + \
    149  1.69       kre 			    LAYOUT_CPU_INT + LAYOUT_CPU_IDLE + 4)
    150  1.69       kre #define	LAYOUT_CPU_GAP	2
    151  1.69       kre 
    152  1.69       kre 			/* used for:       w/o TOTALS  w TOTALS	*/
    153  1.69       kre #define	LAYOUT_DRIVE_1_XSIZE	5	/*	KB/t	KB/t	*/
    154  1.69       kre #define	LAYOUT_DRIVE_1_RATE	6	/*	t/s		*/
    155  1.69       kre #define	LAYOUT_DRIVE_1_XFER	10	/*		xfr	*/
    156  1.69       kre #define	LAYOUT_DRIVE_1_SPEED	5	/*	MB/s		*/
    157  1.69       kre #define	LAYOUT_DRIVE_1_VOLUME	8	/*		MB	*/
    158  1.69       kre #define	LAYOUT_DRIVE_1_INCR	5	/*		(inc)	*/
    159  1.69       kre 
    160  1.69       kre #define	LAYOUT_DRIVE_2_XSIZE	7	/*	KB		*/
    161  1.69       kre #define	LAYOUT_DRIVE_2_VOLUME	11	/*		KB	*/
    162  1.69       kre #define	LAYOUT_DRIVE_2_XFR	7	/*	xfr		*/
    163  1.69       kre #define	LAYOUT_DRIVE_2_TXFR	10	/*		xfr	*/
    164  1.69       kre #define	LAYOUT_DRIVE_2_INCR	5	/*		(inc)	*/
    165  1.69       kre #define	LAYOUT_DRIVE_2_TBUSY	9	/*		time	*/
    166  1.69       kre #define	LAYOUT_DRIVE_2_BUSY	5	/*	time		*/
    167  1.69       kre 
    168  1.69       kre #define	LAYOUT_DRIVE_1	(LAYOUT_DRIVE_1_XSIZE + ((todo & SHOW_TOTALS) ?	       \
    169  1.69       kre 			    (LAYOUT_DRIVE_1_XFER + LAYOUT_DRIVE_1_VOLUME +     \
    170  1.69       kre 			    ((todo&SHOW_UPDATES)? 2*LAYOUT_DRIVE_1_INCR+2 :0)) \
    171  1.69       kre 			  : (LAYOUT_DRIVE_1_RATE + LAYOUT_DRIVE_1_SPEED)) + 3)
    172  1.69       kre #define	LAYOUT_DRIVE_2	(((todo & SHOW_TOTALS) ? (LAYOUT_DRIVE_2_VOLUME +      \
    173  1.69       kre 			    LAYOUT_DRIVE_2_TXFR + LAYOUT_DRIVE_2_TBUSY +       \
    174  1.69       kre 			    ((todo&SHOW_UPDATES)? 2*LAYOUT_DRIVE_2_INCR+2 : 0))\
    175  1.69       kre 			  : (LAYOUT_DRIVE_2_XSIZE + LAYOUT_DRIVE_2_XFR +       \
    176  1.69       kre 			     LAYOUT_DRIVE_2_BUSY)) + 3)
    177  1.69       kre 
    178  1.69       kre #define	LAYOUT_DRIVE_GAP 0	/* Gap included in column, always present */
    179  1.69       kre 
    180  1.69       kre /* TODO: X & Y stats layouts */
    181   1.9   thorpej 
    182  1.23    simonb static void cpustats(void);
    183  1.65   mlelstv static double drive_time(double, int);
    184  1.69       kre static void drive_stats(int, double);
    185  1.69       kre static void drive_stats2(int, double);
    186  1.69       kre static void drive_statsx(int, double);
    187  1.69       kre static void drive_statsy(int, double);
    188  1.64   mlelstv static void drive_statsy_io(double, double, double);
    189  1.64   mlelstv static void drive_statsy_q(double, double, double, double, double, double);
    190  1.39       mrg static void sig_header(int);
    191  1.39       mrg static volatile int do_header;
    192  1.69       kre static void header(int);
    193  1.60     joerg __dead static void usage(void);
    194  1.69       kre static void display(int);
    195  1.68       kre static int selectdrives(int, char *[], int);
    196  1.23    simonb 
    197   1.6       cgd int
    198  1.23    simonb main(int argc, char *argv[])
    199   1.1       cgd {
    200  1.66       mrg 	int ch, hdrcnt, hdroffset, ndrives, lines;
    201  1.28   mycroft 	struct timespec	tv;
    202  1.37    simonb 	struct ttysize ts;
    203  1.69       kre 	long width = -1, height = -1;
    204  1.69       kre 	char *ep;
    205   1.9   thorpej 
    206  1.69       kre #if 0		/* -i and -u are not currently (sanely) implementable */
    207  1.69       kre 	while ((ch = getopt(argc, argv, "Cc:dDH:iITuw:W:xyz")) != -1)
    208  1.69       kre #else
    209  1.69       kre 	while ((ch = getopt(argc, argv, "Cc:dDH:ITw:W:xyz")) != -1)
    210  1.69       kre #endif
    211  1.31     enami 		switch (ch) {
    212   1.1       cgd 		case 'c':
    213   1.6       cgd 			if ((reps = atoi(optarg)) <= 0)
    214   1.6       cgd 				errx(1, "repetition count <= 0.");
    215   1.1       cgd 			break;
    216   1.9   thorpej 		case 'C':
    217   1.9   thorpej 			todo |= SHOW_CPU;
    218   1.9   thorpej 			break;
    219   1.9   thorpej 		case 'd':
    220  1.15     lukem 			todo &= ~SHOW_STATS_ALL;
    221   1.9   thorpej 			todo |= SHOW_STATS_1;
    222   1.9   thorpej 			break;
    223   1.9   thorpej 		case 'D':
    224  1.15     lukem 			todo &= ~SHOW_STATS_ALL;
    225   1.9   thorpej 			todo |= SHOW_STATS_2;
    226   1.9   thorpej 			break;
    227  1.69       kre 		case 'H':
    228  1.69       kre 			height = strtol(optarg, &ep, 10);
    229  1.69       kre 			if (height < 0 || *ep != '\0')
    230  1.69       kre 				errx(1, "bad height (-H) value.");
    231  1.69       kre 			height += 2;	/* magic, but needed to be sane */
    232  1.69       kre 			break;
    233  1.69       kre #if 0
    234  1.69       kre 		case 'i':
    235  1.69       kre 			todo |= SHOW_TOTALS | SHOW_NEW_TOTALS;
    236  1.69       kre 			break;
    237  1.69       kre #endif
    238   1.9   thorpej 		case 'I':
    239   1.9   thorpej 			todo |= SHOW_TOTALS;
    240   1.9   thorpej 			break;
    241   1.9   thorpej 		case 'T':
    242   1.9   thorpej 			todo |= SHOW_TTY;
    243   1.9   thorpej 			break;
    244  1.69       kre #if 0
    245  1.69       kre 		case 'u':
    246  1.69       kre 			todo |= SHOW_UPDATES;
    247  1.69       kre 			break;
    248  1.69       kre #endif
    249   1.1       cgd 		case 'w':
    250   1.6       cgd 			if ((interval = atoi(optarg)) <= 0)
    251   1.6       cgd 				errx(1, "interval <= 0.");
    252   1.1       cgd 			break;
    253  1.69       kre 		case 'W':
    254  1.69       kre 			width = strtol(optarg, &ep, 10);
    255  1.69       kre 			if (width < 0 || *ep != '\0')
    256  1.69       kre 				errx(1, "bad width (-W) value.");
    257  1.69       kre 			break;
    258  1.15     lukem 		case 'x':
    259  1.15     lukem 			todo &= ~SHOW_STATS_ALL;
    260  1.15     lukem 			todo |= SHOW_STATS_X;
    261  1.15     lukem 			break;
    262  1.64   mlelstv 		case 'y':
    263  1.64   mlelstv 			todo &= ~SHOW_STATS_ALL;
    264  1.64   mlelstv 			todo |= SHOW_STATS_Y;
    265  1.64   mlelstv 			break;
    266  1.69       kre 		case 'z':
    267  1.69       kre 			todo |= SUPPRESS_ZERO;
    268  1.69       kre 			break;
    269   1.1       cgd 		case '?':
    270   1.1       cgd 		default:
    271   1.1       cgd 			usage();
    272   1.1       cgd 		}
    273   1.1       cgd 	argc -= optind;
    274   1.1       cgd 	argv += optind;
    275   1.1       cgd 
    276  1.15     lukem 	if (!ISSET(todo, SHOW_CPU | SHOW_TTY | SHOW_STATS_ALL))
    277   1.9   thorpej 		todo |= SHOW_CPU | SHOW_TTY | SHOW_STATS_1;
    278  1.15     lukem 	if (ISSET(todo, SHOW_STATS_X)) {
    279  1.15     lukem 		todo &= ~(SHOW_CPU | SHOW_TTY | SHOW_STATS_ALL);
    280  1.15     lukem 		todo |= SHOW_STATS_X;
    281  1.15     lukem 	}
    282  1.64   mlelstv 	if (ISSET(todo, SHOW_STATS_Y)) {
    283  1.64   mlelstv 		todo &= ~(SHOW_CPU | SHOW_TTY | SHOW_STATS_ALL | SHOW_TOTALS);
    284  1.64   mlelstv 		todo |= SHOW_STATS_Y;
    285  1.64   mlelstv 	}
    286   1.6       cgd 
    287  1.38    simonb 	if (ioctl(STDOUT_FILENO, TIOCGSIZE, &ts) != -1) {
    288  1.38    simonb 		if (ts.ts_lines)
    289  1.38    simonb 			winlines = ts.ts_lines;
    290  1.38    simonb 		if (ts.ts_cols)
    291  1.38    simonb 			wincols = ts.ts_cols;
    292  1.38    simonb 	}
    293  1.38    simonb 
    294  1.69       kre 	if (height == -1) {
    295  1.69       kre 		char *lns = getenv("LINES");
    296  1.69       kre 
    297  1.69       kre 		if (lns == NULL || (height = strtol(lns, &ep, 10)) < 0 ||
    298  1.69       kre 		    *ep != '\0')
    299  1.69       kre 			height = winlines;
    300  1.69       kre 	}
    301  1.69       kre 	winlines = height;
    302  1.69       kre 
    303  1.69       kre 	if (width == -1) {
    304  1.69       kre 		char *cols = getenv("COLUMNS");
    305  1.69       kre 
    306  1.69       kre 		if (cols == NULL || (width = strtol(cols, &ep, 10)) < 0 ||
    307  1.69       kre 		    *ep != '\0')
    308  1.69       kre 			width = wincols;
    309  1.69       kre 	}
    310  1.69       kre 	defdrives = width;
    311  1.69       kre 	if (defdrives == 0) {
    312  1.69       kre 		defdrives = 5000;	/* anything absurdly big */
    313  1.69       kre 	} else {
    314  1.69       kre 		if (ISSET(todo, SHOW_CPU))
    315  1.69       kre 			defdrives -= LAYOUT_CPU + LAYOUT_CPU_GAP;
    316  1.69       kre 		if (ISSET(todo, SHOW_TTY))
    317  1.69       kre 			defdrives -= LAYOUT_TTY + LAYOUT_TTY_GAP;
    318  1.69       kre 		if (ISSET(todo, SHOW_STATS_2))
    319  1.69       kre 			defdrives /= LAYOUT_DRIVE_2 + LAYOUT_DRIVE_GAP;
    320  1.69       kre 		else
    321  1.69       kre 			defdrives /= LAYOUT_DRIVE_1 + LAYOUT_DRIVE_GAP;
    322  1.69       kre 	}
    323  1.38    simonb 
    324  1.49     blymn 	drvinit(0);
    325  1.48       dsl 	cpureadstats();
    326  1.49     blymn 	drvreadstats();
    327  1.69       kre 	ordersize = 0;
    328  1.68       kre 	ndrives = selectdrives(argc, argv, 1);
    329  1.34     enami 	if (ndrives == 0) {
    330  1.49     blymn 		/* No drives are selected.  No need to show drive stats. */
    331  1.34     enami 		todo &= ~SHOW_STATS_ALL;
    332  1.34     enami 		if (todo == 0)
    333  1.34     enami 			errx(1, "no drives");
    334  1.34     enami 	}
    335   1.9   thorpej 	tv.tv_sec = interval;
    336  1.28   mycroft 	tv.tv_nsec = 0;
    337   1.1       cgd 
    338   1.9   thorpej 	/* print a new header on sigcont */
    339  1.39       mrg 	(void)signal(SIGCONT, sig_header);
    340  1.69       kre 	do_header = 1;
    341   1.1       cgd 
    342   1.1       cgd 	for (hdrcnt = 1;;) {
    343  1.67   mlelstv 		if (ISSET(todo, SHOW_STATS_X | SHOW_STATS_Y)) {
    344  1.67   mlelstv 			lines = ndrives;
    345  1.67   mlelstv 			hdroffset = 3;
    346  1.67   mlelstv 		} else {
    347  1.67   mlelstv 			lines = 1;
    348  1.67   mlelstv 			hdroffset = 4;
    349  1.67   mlelstv 		}
    350  1.67   mlelstv 
    351  1.69       kre 		if (do_header || (winlines != 0 && (hdrcnt -= lines) <= 0)) {
    352  1.39       mrg 			do_header = 0;
    353  1.69       kre 			header(ndrives);
    354  1.66       mrg 			hdrcnt = winlines - hdroffset;
    355   1.1       cgd 		}
    356   1.9   thorpej 
    357  1.69       kre 		if (!ISSET(todo, SHOW_TOTALS) || ISSET(todo, SHOW_NEW_TOTALS)) {
    358  1.48       dsl 			cpuswap();
    359  1.49     blymn 			drvswap();
    360  1.50    simonb 			tkswap();
    361  1.69       kre 			todo &= ~SHOW_NEW_TOTALS;
    362  1.45     blymn 		}
    363  1.49     blymn 
    364  1.69       kre 		display(ndrives);
    365   1.1       cgd 
    366   1.1       cgd 		if (reps >= 0 && --reps <= 0)
    367   1.1       cgd 			break;
    368  1.28   mycroft 		nanosleep(&tv, NULL);
    369  1.48       dsl 		cpureadstats();
    370  1.49     blymn 		drvreadstats();
    371  1.67   mlelstv 
    372  1.68       kre 		ndrives = selectdrives(argc, argv, 0);
    373   1.1       cgd 	}
    374   1.1       cgd 	exit(0);
    375   1.1       cgd }
    376   1.1       cgd 
    377   1.9   thorpej static void
    378  1.39       mrg sig_header(int signo)
    379  1.39       mrg {
    380  1.39       mrg 	do_header = 1;
    381  1.39       mrg }
    382  1.39       mrg 
    383  1.39       mrg static void
    384  1.69       kre header(int ndrives)
    385   1.1       cgd {
    386  1.69       kre 	int i;
    387   1.1       cgd 
    388  1.29       mrg 					/* Main Headers. */
    389  1.29       mrg 	if (ISSET(todo, SHOW_STATS_X)) {
    390  1.29       mrg 		if (ISSET(todo, SHOW_TOTALS)) {
    391  1.31     enami 			(void)printf(
    392  1.42       dbj 			    "device  read KB/t    xfr   time     MB  ");
    393  1.42       dbj 			(void)printf(" write KB/t    xfr   time     MB\n");
    394  1.29       mrg 		} else {
    395  1.31     enami 			(void)printf(
    396  1.31     enami 			    "device  read KB/t    r/s   time     MB/s");
    397  1.29       mrg 			(void)printf(" write KB/t    w/s   time     MB/s\n");
    398  1.29       mrg 		}
    399  1.15     lukem 		return;
    400  1.29       mrg 	}
    401  1.15     lukem 
    402  1.64   mlelstv 	if (ISSET(todo, SHOW_STATS_Y)) {
    403  1.64   mlelstv 		(void)printf("device  read KB/t    r/s     MB/s write KB/t    w/s     MB/s");
    404  1.64   mlelstv 		(void)printf("   wait   actv  wsvc_t  asvc_t  wtime   time");
    405  1.64   mlelstv 		(void)printf("\n");
    406  1.64   mlelstv 		return;
    407  1.64   mlelstv 	}
    408  1.64   mlelstv 
    409   1.9   thorpej 	if (ISSET(todo, SHOW_TTY))
    410  1.69       kre 		(void)printf("%*s", LAYOUT_TTY_GAP + LAYOUT_TTY, "tty");
    411   1.9   thorpej 
    412  1.45     blymn 	if (ISSET(todo, SHOW_STATS_1)) {
    413  1.69       kre 		for (i = 0; i < ndrives; i++) {
    414  1.69       kre 			char *dname = cur.name[order[i]];
    415  1.69       kre 			int dnlen = (int)strlen(dname);
    416  1.69       kre 
    417  1.69       kre 			printf(" ");	/* always a 1 column gap */
    418  1.69       kre 			if (dnlen < LAYOUT_DRIVE_1 - 6)
    419  1.69       kre 				printf("|%-*.*s ",
    420  1.69       kre 				    (LAYOUT_DRIVE_1 - 1 - dnlen - 1) / 2 - 1,
    421  1.69       kre 				    (LAYOUT_DRIVE_1 - 1 - dnlen - 1) / 2 - 1,
    422  1.69       kre 				    Line_Marker);
    423  1.69       kre 			printf("%*.*s", ((dnlen >= LAYOUT_DRIVE_1 - 6) ?
    424  1.69       kre 			    MIN(MAX((LAYOUT_DRIVE_1 - dnlen) / 2, 0),
    425  1.69       kre 				LAYOUT_DRIVE_1) : 0),
    426  1.69       kre 			    LAYOUT_DRIVE_1, dname);
    427  1.69       kre 			if (dnlen < LAYOUT_DRIVE_1 - 6)
    428  1.69       kre 				printf(" %*.*s|",
    429  1.69       kre 				    (LAYOUT_DRIVE_1 - 1 - dnlen - 2) / 2 - 1,
    430  1.69       kre 				    (LAYOUT_DRIVE_1 - 1 - dnlen - 2) / 2 - 1,
    431  1.69       kre 				    Line_Marker);
    432  1.69       kre 		}
    433  1.45     blymn 	}
    434   1.9   thorpej 
    435  1.45     blymn 	if (ISSET(todo, SHOW_STATS_2)) {
    436  1.69       kre 		for (i = 0; i < ndrives; i++) {
    437  1.69       kre 			char *dname = cur.name[order[i]];
    438  1.69       kre 			int dnlen = (int)strlen(dname);
    439  1.69       kre 
    440  1.69       kre 			printf(" ");	/* always a 1 column gap */
    441  1.69       kre 			if (dnlen < LAYOUT_DRIVE_2 - 6)
    442  1.69       kre 				printf("|%-*.*s ",
    443  1.69       kre 				    (LAYOUT_DRIVE_2 - 1 - dnlen - 1) / 2 - 1,
    444  1.69       kre 				    (LAYOUT_DRIVE_2 - 1 - dnlen - 1) / 2 - 1,
    445  1.69       kre 				    Line_Marker);
    446  1.69       kre 			printf("%*.*s", ((dnlen >= LAYOUT_DRIVE_1 - 6) ?
    447  1.69       kre 			    MIN(MAX((LAYOUT_DRIVE_2 - dnlen) / 2, 0),
    448  1.69       kre 				LAYOUT_DRIVE_2) : 0),
    449  1.69       kre 			    LAYOUT_DRIVE_1, dname);
    450  1.69       kre 			if (dnlen < LAYOUT_DRIVE_2 - 6)
    451  1.69       kre 				printf(" %*.*s|",
    452  1.69       kre 				    (LAYOUT_DRIVE_2 - 1 - dnlen - 2) / 2 - 1,
    453  1.69       kre 				    (LAYOUT_DRIVE_2 - 1 - dnlen - 2) / 2 - 1,
    454  1.69       kre 				    Line_Marker);
    455  1.69       kre 		}
    456  1.45     blymn 	}
    457  1.49     blymn 
    458   1.9   thorpej 	if (ISSET(todo, SHOW_CPU))
    459  1.69       kre 		(void)printf("%*s", LAYOUT_CPU + LAYOUT_CPU_GAP, "CPU");
    460  1.15     lukem 
    461   1.9   thorpej 	printf("\n");
    462   1.9   thorpej 
    463  1.15     lukem 					/* Sub-Headers. */
    464  1.69       kre 	if (ISSET(todo, SHOW_TTY)) {
    465  1.69       kre 		printf("%*s %*s",
    466  1.69       kre 		   ((todo&SHOW_TOTALS)?LAYOUT_TTY_TIN:LAYOUT_TTY_IN), "tin",
    467  1.69       kre 		   ((todo&SHOW_TOTALS)?LAYOUT_TTY_TOUT:LAYOUT_TTY_OUT), "tout");
    468  1.69       kre 	}
    469   1.9   thorpej 
    470  1.15     lukem 	if (ISSET(todo, SHOW_STATS_1)) {
    471  1.69       kre 		for (i = 0; i < ndrives; i++) {
    472  1.69       kre 			if (ISSET(todo, SHOW_TOTALS)) {
    473  1.69       kre 				(void)printf(" %*s %*s %*s",
    474  1.69       kre 				    LAYOUT_DRIVE_1_XFER, "xfr",
    475  1.69       kre 				    LAYOUT_DRIVE_1_XSIZE, "KB/t",
    476  1.69       kre 				    LAYOUT_DRIVE_1_VOLUME, "MB");
    477  1.69       kre 			} else {
    478  1.69       kre 				(void)printf(" %*s %*s %*s",
    479  1.69       kre 				    LAYOUT_DRIVE_1_RATE, "t/s",
    480  1.69       kre 				    LAYOUT_DRIVE_1_XSIZE, "KB/t",
    481  1.69       kre 				    LAYOUT_DRIVE_1_SPEED, "MB/s");
    482  1.45     blymn 			}
    483  1.69       kre 		}
    484  1.15     lukem 	}
    485   1.9   thorpej 
    486  1.45     blymn 	if (ISSET(todo, SHOW_STATS_2)) {
    487  1.69       kre 		for (i = 0; i < ndrives; i++) {
    488  1.69       kre 			if (ISSET(todo, SHOW_TOTALS)) {
    489  1.69       kre 				(void)printf(" %*s %*s %*s",
    490  1.69       kre 				    LAYOUT_DRIVE_2_TXFR, "xfr",
    491  1.69       kre 				    LAYOUT_DRIVE_2_VOLUME, "KB",
    492  1.69       kre 				    LAYOUT_DRIVE_2_TBUSY, "time");
    493  1.69       kre 			} else {
    494  1.69       kre 				(void)printf(" %*s %*s %*s",
    495  1.69       kre 				    LAYOUT_DRIVE_2_XFR, "xfr",
    496  1.69       kre 				    LAYOUT_DRIVE_2_XSIZE, "KB",
    497  1.69       kre 				    LAYOUT_DRIVE_2_BUSY, "time");
    498  1.69       kre 			}
    499  1.69       kre 		}
    500  1.45     blymn 	}
    501   1.9   thorpej 
    502  1.69       kre 	/* should do this properly, but it is such a simple case... */
    503   1.9   thorpej 	if (ISSET(todo, SHOW_CPU))
    504  1.69       kre 		(void)printf("  us ni sy in  id");
    505   1.9   thorpej 	printf("\n");
    506   1.1       cgd }
    507   1.1       cgd 
    508  1.65   mlelstv static double
    509  1.65   mlelstv drive_time(double etime, int dn)
    510  1.65   mlelstv {
    511  1.65   mlelstv 	if (ISSET(todo, SHOW_TOTALS))
    512  1.65   mlelstv 		return etime;
    513  1.65   mlelstv 
    514  1.65   mlelstv 	if (cur.timestamp[dn].tv_sec || cur.timestamp[dn].tv_usec) {
    515  1.65   mlelstv 		etime = (double)cur.timestamp[dn].tv_sec +
    516  1.65   mlelstv 		    ((double)cur.timestamp[dn].tv_usec / (double)1000000);
    517  1.65   mlelstv 	}
    518  1.65   mlelstv 
    519  1.65   mlelstv 	return etime;
    520  1.65   mlelstv }
    521  1.65   mlelstv 
    522   1.9   thorpej static void
    523  1.69       kre drive_stats(int ndrives, double etime)
    524  1.45     blymn {
    525  1.69       kre 	int drive;
    526  1.65   mlelstv 	double atime, dtime, mbps;
    527  1.69       kre 	int c1, c2, c3;
    528  1.69       kre 
    529  1.69       kre 	if (ISSET(todo, SHOW_TOTALS)) {
    530  1.69       kre 		c1 = LAYOUT_DRIVE_1_XFER;
    531  1.69       kre 		c2 = LAYOUT_DRIVE_1_XSIZE;
    532  1.69       kre 		c3 = LAYOUT_DRIVE_1_VOLUME;
    533  1.69       kre 	} else {
    534  1.69       kre 		c1 = LAYOUT_DRIVE_1_RATE;
    535  1.69       kre 		c2 = LAYOUT_DRIVE_1_XSIZE;
    536  1.69       kre 		c3 = LAYOUT_DRIVE_1_SPEED;
    537  1.69       kre 	}
    538  1.45     blymn 
    539  1.69       kre 	for (drive = 0; drive < ndrives; ++drive) {
    540  1.69       kre 		int dn = order[drive];
    541  1.69       kre 
    542  1.69       kre 		if (!cur.select[dn])	/* should be impossible */
    543  1.45     blymn 			continue;
    544  1.65   mlelstv 
    545  1.69       kre 		if (todo & SUPPRESS_ZERO) {
    546  1.69       kre 			if (cur.rxfer[dn] == 0 &&
    547  1.69       kre 			    cur.wxfer[dn] == 0 &&
    548  1.69       kre 			    cur.rbytes[dn] == 0 &&
    549  1.69       kre 			    cur.wbytes[dn] == 0) {
    550  1.69       kre 				printf("%*s", c1 + 1 + c2 + 1 + c3 + 1, "");
    551  1.69       kre 				continue;
    552  1.69       kre 			}
    553  1.69       kre 		}
    554  1.69       kre 
    555  1.65   mlelstv 		dtime = drive_time(etime, dn);
    556  1.65   mlelstv 
    557  1.69       kre 					/* average transfers per second. */
    558  1.69       kre 		(void)printf(" %*.0f", c1,
    559  1.69       kre 		    (cur.rxfer[dn] + cur.wxfer[dn]) / dtime);
    560  1.69       kre 
    561  1.45     blymn 					/* average Kbytes per transfer. */
    562  1.49     blymn 		if (cur.rxfer[dn] + cur.wxfer[dn])
    563  1.49     blymn 			mbps = ((cur.rbytes[dn] + cur.wbytes[dn]) /
    564  1.49     blymn 			    1024.0) / (cur.rxfer[dn] + cur.wxfer[dn]);
    565  1.45     blymn 		else
    566  1.45     blymn 			mbps = 0.0;
    567  1.69       kre 		(void)printf(" %*.*f", c2,
    568  1.59     enami 		    MAX(0, 3 - (int)floor(log10(fmax(1.0, mbps)))), mbps);
    569  1.45     blymn 
    570  1.49     blymn 					/* time busy in drive activity */
    571  1.49     blymn 		atime = (double)cur.time[dn].tv_sec +
    572  1.49     blymn 		    ((double)cur.time[dn].tv_usec / (double)1000000);
    573  1.45     blymn 
    574  1.45     blymn 					/* Megabytes per second. */
    575  1.45     blymn 		if (atime != 0.0)
    576  1.49     blymn 			mbps = (cur.rbytes[dn] + cur.wbytes[dn]) /
    577  1.45     blymn 			    (double)(1024 * 1024);
    578  1.45     blymn 		else
    579  1.45     blymn 			mbps = 0;
    580  1.65   mlelstv 		mbps /= dtime;
    581  1.69       kre 		(void)printf(" %*.*f", c3,
    582  1.59     enami 		    MAX(0, 3 - (int)floor(log10(fmax(1.0, mbps)))), mbps);
    583  1.45     blymn 	}
    584  1.45     blymn }
    585  1.45     blymn 
    586  1.45     blymn static void
    587  1.69       kre drive_stats2(int ndrives, double etime)
    588  1.45     blymn {
    589  1.69       kre 	int drive;
    590  1.65   mlelstv 	double atime, dtime;
    591  1.69       kre 	int c1, c2, c3;
    592  1.69       kre 
    593  1.69       kre 	if (ISSET(todo, SHOW_TOTALS)) {
    594  1.69       kre 		c1 = LAYOUT_DRIVE_2_TXFR;
    595  1.69       kre 		c2 = LAYOUT_DRIVE_2_VOLUME;
    596  1.69       kre 		c3 = LAYOUT_DRIVE_2_TBUSY;
    597  1.69       kre 	} else {
    598  1.69       kre 		c1 = LAYOUT_DRIVE_2_XFR;
    599  1.69       kre 		c2 = LAYOUT_DRIVE_2_XSIZE;
    600  1.69       kre 		c3 = LAYOUT_DRIVE_2_BUSY;
    601  1.69       kre 	}
    602  1.45     blymn 
    603  1.69       kre 	for (drive = 0; drive < ndrives; ++drive) {
    604  1.69       kre 		int dn = order[drive];
    605  1.69       kre 
    606  1.69       kre 		if (!cur.select[dn])		/* should be impossible */
    607  1.45     blymn 			continue;
    608  1.45     blymn 
    609  1.69       kre 		if (todo & SUPPRESS_ZERO) {
    610  1.69       kre 			if (cur.rxfer[dn] == 0 &&
    611  1.69       kre 			    cur.wxfer[dn] == 0 &&
    612  1.69       kre 			    cur.rbytes[dn] == 0 &&
    613  1.69       kre 			    cur.wbytes[dn] == 0) {
    614  1.69       kre 				printf("%*s", c1 + 1 + c2 + 1 + c3 + 1, "");
    615  1.69       kre 				continue;
    616  1.69       kre 			}
    617  1.69       kre 		}
    618  1.69       kre 
    619  1.65   mlelstv 		dtime = drive_time(etime, dn);
    620  1.65   mlelstv 
    621  1.69       kre 					/* average transfers per second. */
    622  1.69       kre 		(void)printf(" %*.0f", c1,
    623  1.69       kre 		    (cur.rxfer[dn] + cur.wxfer[dn]) / dtime);
    624  1.69       kre 
    625  1.45     blymn 					/* average kbytes per second. */
    626  1.69       kre 		(void)printf(" %*.0f", c2,
    627  1.65   mlelstv 		    (cur.rbytes[dn] + cur.wbytes[dn]) / 1024.0 / dtime);
    628  1.45     blymn 
    629  1.69       kre 					/* average time busy in dn activity */
    630  1.49     blymn 		atime = (double)cur.time[dn].tv_sec +
    631  1.49     blymn 		    ((double)cur.time[dn].tv_usec / (double)1000000);
    632  1.69       kre 		(void)printf(" %*.2f", c3, atime / dtime);
    633  1.45     blymn 	}
    634  1.45     blymn }
    635  1.45     blymn 
    636  1.45     blymn static void
    637  1.69       kre drive_statsx(int ndrives, double etime)
    638  1.45     blymn {
    639  1.69       kre 	int dn, drive;
    640  1.65   mlelstv 	double atime, dtime, kbps;
    641  1.45     blymn 
    642  1.69       kre 	for (drive = 0; drive < ndrives; ++drive) {
    643  1.69       kre 		dn = order[drive];
    644  1.69       kre 
    645  1.69       kre 		if (!cur.select[dn])	/* impossible */
    646  1.45     blymn 			continue;
    647  1.45     blymn 
    648  1.69       kre 		(void)printf("%-8.8s", cur.name[dn]);
    649  1.69       kre 
    650  1.69       kre 		if (todo & SUPPRESS_ZERO) {
    651  1.69       kre 			if (cur.rbytes[dn] == 0 && cur.rxfer[dn] == 0 &&
    652  1.69       kre 			    cur.wbytes[dn] == 0 && cur.wxfer[dn] == 0) {
    653  1.69       kre 				printf("\n");
    654  1.69       kre 				continue;
    655  1.69       kre 			}
    656  1.69       kre 		}
    657  1.69       kre 
    658  1.65   mlelstv 		dtime = drive_time(etime, dn);
    659  1.65   mlelstv 
    660  1.45     blymn 					/* average read Kbytes per transfer */
    661  1.49     blymn 		if (cur.rxfer[dn])
    662  1.49     blymn 			kbps = (cur.rbytes[dn] / 1024.0) / cur.rxfer[dn];
    663  1.45     blymn 		else
    664  1.45     blymn 			kbps = 0.0;
    665  1.45     blymn 		(void)printf(" %8.2f", kbps);
    666  1.45     blymn 
    667  1.45     blymn 					/* average read transfers
    668  1.45     blymn 					   (per second) */
    669  1.65   mlelstv 		(void)printf(" %6.0f", cur.rxfer[dn] / dtime);
    670  1.45     blymn 
    671  1.49     blymn 					/* time read busy in drive activity */
    672  1.49     blymn 		atime = (double)cur.time[dn].tv_sec +
    673  1.49     blymn 		    ((double)cur.time[dn].tv_usec / (double)1000000);
    674  1.65   mlelstv 		(void)printf(" %6.2f", atime / dtime);
    675  1.45     blymn 
    676  1.45     blymn 					/* average read megabytes
    677  1.45     blymn 					   (per second) */
    678  1.45     blymn 		(void)printf(" %8.2f",
    679  1.65   mlelstv 		    cur.rbytes[dn] / (1024.0 * 1024) / dtime);
    680  1.45     blymn 
    681  1.45     blymn 
    682  1.45     blymn 					/* average write Kbytes per transfer */
    683  1.49     blymn 		if (cur.wxfer[dn])
    684  1.49     blymn 			kbps = (cur.wbytes[dn] / 1024.0) / cur.wxfer[dn];
    685  1.45     blymn 		else
    686  1.45     blymn 			kbps = 0.0;
    687  1.45     blymn 		(void)printf("   %8.2f", kbps);
    688  1.45     blymn 
    689  1.45     blymn 					/* average write transfers
    690  1.45     blymn 					   (per second) */
    691  1.65   mlelstv 		(void)printf(" %6.0f", cur.wxfer[dn] / dtime);
    692  1.45     blymn 
    693  1.49     blymn 					/* time write busy in drive activity */
    694  1.49     blymn 		atime = (double)cur.time[dn].tv_sec +
    695  1.49     blymn 		    ((double)cur.time[dn].tv_usec / (double)1000000);
    696  1.65   mlelstv 		(void)printf(" %6.2f", atime / dtime);
    697  1.45     blymn 
    698  1.45     blymn 					/* average write megabytes
    699  1.45     blymn 					   (per second) */
    700  1.45     blymn 		(void)printf(" %8.2f\n",
    701  1.65   mlelstv 		    cur.wbytes[dn] / (1024.0 * 1024) / dtime);
    702  1.45     blymn 	}
    703  1.45     blymn }
    704  1.45     blymn 
    705  1.45     blymn static void
    706  1.64   mlelstv drive_statsy_io(double elapsed, double count, double volume)
    707  1.64   mlelstv {
    708  1.64   mlelstv 	double kbps;
    709  1.64   mlelstv 
    710  1.64   mlelstv 	/* average Kbytes per transfer */
    711  1.64   mlelstv 	if (count)
    712  1.64   mlelstv 		kbps = (volume / 1024.0) / count;
    713  1.64   mlelstv 	else
    714  1.64   mlelstv 		kbps = 0.0;
    715  1.64   mlelstv 	(void)printf(" %8.2f", kbps);
    716  1.64   mlelstv 
    717  1.64   mlelstv 	/* average transfers (per second) */
    718  1.64   mlelstv 	(void)printf(" %6.0f", count / elapsed);
    719  1.64   mlelstv 
    720  1.64   mlelstv 	/* average megabytes (per second) */
    721  1.64   mlelstv 	(void)printf(" %8.2f", volume / (1024.0 * 1024) / elapsed);
    722  1.64   mlelstv }
    723  1.64   mlelstv 
    724  1.64   mlelstv static void
    725  1.64   mlelstv drive_statsy_q(double elapsed, double busy, double wait, double busysum, double waitsum, double count)
    726  1.64   mlelstv {
    727  1.64   mlelstv 	/* average wait queue length */
    728  1.64   mlelstv 	(void)printf(" %6.1f", waitsum / elapsed);
    729  1.64   mlelstv 
    730  1.64   mlelstv 	/* average busy queue length */
    731  1.64   mlelstv 	(void)printf(" %6.1f", busysum / elapsed);
    732  1.64   mlelstv 
    733  1.64   mlelstv 	/* average wait time */
    734  1.64   mlelstv 	(void)printf(" %7.2f", count > 0 ? waitsum / count * 1000.0 : 0.0);
    735  1.64   mlelstv 
    736  1.64   mlelstv 	/* average service time */
    737  1.64   mlelstv 	(void)printf(" %7.2f", count > 0 ? busysum / count * 1000.0 : 0.0);
    738  1.64   mlelstv 
    739  1.64   mlelstv 	/* time waiting for drive activity */
    740  1.64   mlelstv 	(void)printf(" %6.2f", wait / elapsed);
    741  1.64   mlelstv 
    742  1.64   mlelstv 	/* time busy in drive activity */
    743  1.64   mlelstv 	(void)printf(" %6.2f", busy / elapsed);
    744  1.64   mlelstv }
    745  1.64   mlelstv 
    746  1.64   mlelstv static void
    747  1.69       kre drive_statsy(int ndrives, double etime)
    748  1.64   mlelstv {
    749  1.69       kre 	int drive, dn;
    750  1.65   mlelstv 	double atime, await, abusysum, awaitsum, dtime;
    751  1.64   mlelstv 
    752  1.69       kre 	for (drive = 0; drive < ndrives; ++drive) {
    753  1.69       kre 		dn = order[drive];
    754  1.69       kre 		if (!cur.select[dn])	/* impossible */
    755  1.64   mlelstv 			continue;
    756  1.64   mlelstv 
    757  1.69       kre 		(void)printf("%-8.8s", cur.name[dn]);
    758  1.69       kre 
    759  1.69       kre 		if (todo & SUPPRESS_ZERO) {
    760  1.69       kre 			if (cur.rbytes[dn] == 0 && cur.rxfer[dn] == 0 &&
    761  1.69       kre 			    cur.wbytes[dn] == 0 && cur.wxfer[dn] == 0) {
    762  1.69       kre 				printf("\n");
    763  1.69       kre 				continue;
    764  1.69       kre 			}
    765  1.69       kre 		}
    766  1.69       kre 
    767  1.65   mlelstv 		dtime = drive_time(etime, dn);
    768  1.65   mlelstv 
    769  1.64   mlelstv 		atime = (double)cur.time[dn].tv_sec +
    770  1.64   mlelstv 		    ((double)cur.time[dn].tv_usec / (double)1000000);
    771  1.64   mlelstv 		await = (double)cur.wait[dn].tv_sec +
    772  1.64   mlelstv 		    ((double)cur.wait[dn].tv_usec / (double)1000000);
    773  1.64   mlelstv 		abusysum = (double)cur.busysum[dn].tv_sec +
    774  1.64   mlelstv 		    ((double)cur.busysum[dn].tv_usec / (double)1000000);
    775  1.64   mlelstv 		awaitsum = (double)cur.waitsum[dn].tv_sec +
    776  1.64   mlelstv 		    ((double)cur.waitsum[dn].tv_usec / (double)1000000);
    777  1.64   mlelstv 
    778  1.65   mlelstv 		drive_statsy_io(dtime, cur.rxfer[dn], cur.rbytes[dn]);
    779  1.64   mlelstv 		(void)printf("  ");
    780  1.65   mlelstv 		drive_statsy_io(dtime, cur.wxfer[dn], cur.wbytes[dn]);
    781  1.65   mlelstv 		drive_statsy_q(dtime, atime, await, abusysum, awaitsum, cur.rxfer[dn]+cur.wxfer[dn]);
    782  1.64   mlelstv 
    783  1.64   mlelstv 		(void)printf("\n");
    784  1.64   mlelstv 	}
    785  1.64   mlelstv }
    786  1.64   mlelstv 
    787  1.64   mlelstv static void
    788  1.23    simonb cpustats(void)
    789   1.1       cgd {
    790  1.12     lukem 	int state;
    791  1.53     lukem 	double ttime;
    792   1.1       cgd 
    793  1.69       kre 	static int cwidth[CPUSTATES] = {
    794  1.69       kre 		LAYOUT_CPU_USER,
    795  1.69       kre 		LAYOUT_CPU_NICE,
    796  1.69       kre 		LAYOUT_CPU_SYS,
    797  1.69       kre 		LAYOUT_CPU_INT,
    798  1.69       kre 		LAYOUT_CPU_IDLE
    799  1.69       kre 	};
    800  1.69       kre 
    801  1.53     lukem 	ttime = 0;
    802   1.1       cgd 	for (state = 0; state < CPUSTATES; ++state)
    803  1.53     lukem 		ttime += cur.cp_time[state];
    804  1.53     lukem 	if (!ttime)
    805  1.53     lukem 		ttime = 1.0;
    806  1.69       kre 
    807  1.69       kre 	printf("%*s", LAYOUT_CPU_GAP - 1, "");	/* the 1 is the next space */
    808  1.69       kre 	for (state = 0; state < CPUSTATES; ++state) {
    809  1.69       kre 		if ((todo & SUPPRESS_ZERO) && cur.cp_time[state] == 0) {
    810  1.69       kre 			printf(" %*s", cwidth[state], "");
    811  1.69       kre 			continue;
    812  1.69       kre 		}
    813  1.69       kre 		printf(" %*.0f", cwidth[state],
    814  1.69       kre 		    100. * cur.cp_time[state] / ttime);
    815  1.69       kre 	}
    816   1.1       cgd }
    817   1.1       cgd 
    818   1.9   thorpej static void
    819  1.23    simonb usage(void)
    820   1.1       cgd {
    821  1.13       mrg 
    822  1.69       kre 	(void)fprintf(stderr, "usage: iostat [-CdDITxyz] [-c count] "
    823  1.69       kre 	    "[-H height] [-W width] [-w wait] [drives]\n");
    824   1.1       cgd 	exit(1);
    825   1.9   thorpej }
    826   1.9   thorpej 
    827   1.9   thorpej static void
    828  1.69       kre display(int ndrives)
    829   1.9   thorpej {
    830   1.9   thorpej 	double	etime;
    831   1.9   thorpej 
    832   1.9   thorpej 	/* Sum up the elapsed ticks. */
    833  1.27  sommerfe 	etime = cur.cp_etime;
    834   1.9   thorpej 
    835  1.34     enami 	/*
    836  1.34     enami 	 * If we're showing totals only, then don't divide by the
    837   1.9   thorpej 	 * system time.
    838   1.9   thorpej 	 */
    839   1.9   thorpej 	if (ISSET(todo, SHOW_TOTALS))
    840   1.9   thorpej 		etime = 1.0;
    841   1.9   thorpej 
    842  1.34     enami 	if (ISSET(todo, SHOW_STATS_X)) {
    843  1.69       kre 		drive_statsx(ndrives, etime);
    844  1.34     enami 		goto out;
    845  1.34     enami 	}
    846  1.34     enami 
    847  1.64   mlelstv 	if (ISSET(todo, SHOW_STATS_Y)) {
    848  1.69       kre 		drive_statsy(ndrives, etime);
    849  1.64   mlelstv 		goto out;
    850  1.64   mlelstv 	}
    851  1.64   mlelstv 
    852   1.9   thorpej 	if (ISSET(todo, SHOW_TTY))
    853  1.69       kre 		printf("%*.0f %*.0f",
    854  1.69       kre 		    ((todo & SHOW_TOTALS) ? LAYOUT_TTY_TIN : LAYOUT_TTY_IN),
    855  1.69       kre 		    cur.tk_nin / etime,
    856  1.69       kre 		    ((todo & SHOW_TOTALS) ? LAYOUT_TTY_TOUT : LAYOUT_TTY_OUT),
    857  1.69       kre 		    cur.tk_nout / etime);
    858  1.27  sommerfe 
    859  1.45     blymn 	if (ISSET(todo, SHOW_STATS_1)) {
    860  1.69       kre 		drive_stats(ndrives, etime);
    861  1.45     blymn 	}
    862  1.49     blymn 
    863   1.9   thorpej 
    864  1.45     blymn 	if (ISSET(todo, SHOW_STATS_2)) {
    865  1.69       kre 		drive_stats2(ndrives, etime);
    866  1.45     blymn 	}
    867  1.49     blymn 
    868  1.15     lukem 
    869   1.9   thorpej 	if (ISSET(todo, SHOW_CPU))
    870   1.9   thorpej 		cpustats();
    871   1.9   thorpej 
    872  1.34     enami 	(void)printf("\n");
    873  1.34     enami 
    874  1.34     enami out:
    875   1.9   thorpej 	(void)fflush(stdout);
    876   1.9   thorpej }
    877   1.9   thorpej 
    878  1.34     enami static int
    879  1.68       kre selectdrives(int argc, char *argv[], int first)
    880   1.9   thorpej {
    881  1.34     enami 	int	i, maxdrives, ndrives, tried;
    882   1.9   thorpej 
    883   1.9   thorpej 	/*
    884   1.9   thorpej 	 * Choose drives to be displayed.  Priority goes to (in order) drives
    885   1.9   thorpej 	 * supplied as arguments and default drives.  If everything isn't
    886   1.9   thorpej 	 * filled in and there are drives not taken care of, display the first
    887   1.9   thorpej 	 * few that fit.
    888   1.9   thorpej 	 *
    889   1.9   thorpej 	 * The backward compatibility #ifdefs permit the syntax:
    890   1.9   thorpej 	 *	iostat [ drives ] [ interval [ count ] ]
    891   1.9   thorpej 	 */
    892  1.19      tron 
    893   1.9   thorpej #define	BACKWARD_COMPATIBILITY
    894  1.34     enami 	for (tried = ndrives = 0; *argv; ++argv) {
    895  1.34     enami #ifdef BACKWARD_COMPATIBILITY
    896  1.43       dsl 		if (isdigit((unsigned char)**argv))
    897   1.9   thorpej 			break;
    898   1.9   thorpej #endif
    899  1.34     enami 		tried++;
    900  1.53     lukem 		for (i = 0; i < (int)ndrive; i++) {
    901  1.62       mrg 			if (fnmatch(*argv, cur.name[i], 0))
    902  1.33  christos 				continue;
    903  1.49     blymn 			cur.select[i] = 1;
    904  1.69       kre 			if (ordersize <= ndrives) {
    905  1.69       kre 				int *new = realloc(order,
    906  1.69       kre 				    (ordersize + 8) * sizeof *order);
    907  1.69       kre 				if (new == NULL)
    908  1.69       kre 					break;
    909  1.69       kre 				ordersize += 8;
    910  1.69       kre 				order = new;
    911  1.69       kre 			}
    912  1.69       kre 			order[ndrives++] = i;
    913  1.45     blymn 		}
    914  1.49     blymn 
    915   1.9   thorpej 	}
    916  1.34     enami 
    917  1.34     enami 	if (ndrives == 0 && tried == 0) {
    918  1.34     enami 		/*
    919  1.38    simonb 		 * Pick up to defdrives (or all if -x is given) drives
    920  1.34     enami 		 * if none specified.
    921  1.34     enami 		 */
    922  1.64   mlelstv 		maxdrives = (ISSET(todo, SHOW_STATS_X | SHOW_STATS_Y) ||
    923  1.53     lukem 			     (int)ndrive < defdrives)
    924  1.53     lukem 			? (int)(ndrive) : defdrives;
    925  1.69       kre 		ordersize = maxdrives;
    926  1.69       kre 		free(order);
    927  1.69       kre 		order = calloc(ordersize, sizeof *order);
    928  1.69       kre 		if (order == NULL)
    929  1.69       kre 			errx(1, "Insufficient memory");
    930  1.34     enami 		for (i = 0; i < maxdrives; i++) {
    931  1.49     blymn 			cur.select[i] = 1;
    932  1.69       kre 			order[i] = i;
    933  1.49     blymn 
    934  1.34     enami 			++ndrives;
    935  1.64   mlelstv 			if (!ISSET(todo, SHOW_STATS_X | SHOW_STATS_Y) &&
    936  1.64   mlelstv 			    ndrives == defdrives)
    937  1.34     enami 				break;
    938  1.34     enami 		}
    939  1.34     enami 	}
    940  1.34     enami 
    941  1.34     enami #ifdef BACKWARD_COMPATIBILITY
    942  1.68       kre 	if (first && *argv) {
    943   1.9   thorpej 		interval = atoi(*argv);
    944   1.9   thorpej 		if (*++argv)
    945   1.9   thorpej 			reps = atoi(*argv);
    946   1.9   thorpej 	}
    947   1.9   thorpej #endif
    948   1.9   thorpej 
    949   1.9   thorpej 	if (interval) {
    950   1.9   thorpej 		if (!reps)
    951   1.9   thorpej 			reps = -1;
    952   1.9   thorpej 	} else
    953   1.9   thorpej 		if (reps)
    954   1.9   thorpej 			interval = 1;
    955   1.9   thorpej 
    956  1.34     enami 	return (ndrives);
    957   1.1       cgd }
    958