Home | History | Annotate | Line # | Download | only in iostat
iostat.c revision 1.46
      1  1.46      yamt /*	$NetBSD: iostat.c,v 1.46 2006/01/08 08:47:16 yamt 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.12     lukem __COPYRIGHT("@(#) Copyright (c) 1986, 1991, 1993\n\
     67  1.12     lukem         The Regents of the University of California.  All rights reserved.\n");
     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.46      yamt __RCSID("$NetBSD: iostat.c,v 1.46 2006/01/08 08:47:16 yamt 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.1       cgd 
     91   1.9   thorpej #include "dkstats.h"
     92  1.45     blymn #include "tpstats.h"
     93   1.1       cgd 
     94   1.9   thorpej /* Namelist and memory files. */
     95   1.9   thorpej char	*nlistf, *memf;
     96   1.9   thorpej 
     97   1.9   thorpej int		hz, reps, interval;
     98   1.9   thorpej static int	todo = 0;
     99  1.38    simonb static int	defdrives;
    100  1.37    simonb static int	winlines = 20;
    101  1.38    simonb static int	wincols = 80;
    102   1.9   thorpej 
    103  1.36    simonb #define	ISSET(x, a)	((x) & (a))
    104  1.36    simonb #define	SHOW_CPU	1<<0
    105  1.36    simonb #define	SHOW_TTY	1<<1
    106  1.36    simonb #define	SHOW_STATS_1	1<<2
    107  1.36    simonb #define	SHOW_STATS_2	1<<3
    108  1.36    simonb #define	SHOW_STATS_X	1<<4
    109  1.36    simonb #define	SHOW_TOTALS	1<<7
    110  1.36    simonb #define	SHOW_STATS_ALL	(SHOW_STATS_1 | SHOW_STATS_2 | SHOW_STATS_X)
    111   1.9   thorpej 
    112  1.23    simonb static void cpustats(void);
    113  1.23    simonb static void disk_stats(double);
    114  1.23    simonb static void disk_stats2(double);
    115  1.23    simonb static void disk_statsx(double);
    116  1.39       mrg static void sig_header(int);
    117  1.39       mrg static volatile int do_header;
    118  1.39       mrg static void header(void);
    119  1.23    simonb static void usage(void);
    120  1.23    simonb static void display(void);
    121  1.34     enami static int selectdrives(int, char *[]);
    122  1.23    simonb 
    123  1.34     enami int main(int, char *[]);
    124   1.1       cgd 
    125   1.6       cgd int
    126  1.23    simonb main(int argc, char *argv[])
    127   1.1       cgd {
    128  1.34     enami 	int ch, hdrcnt, ndrives, lines;
    129  1.28   mycroft 	struct timespec	tv;
    130  1.37    simonb 	struct ttysize ts;
    131   1.9   thorpej 
    132  1.15     lukem 	while ((ch = getopt(argc, argv, "Cc:dDIM:N:Tw:x")) != -1)
    133  1.31     enami 		switch (ch) {
    134   1.1       cgd 		case 'c':
    135   1.6       cgd 			if ((reps = atoi(optarg)) <= 0)
    136   1.6       cgd 				errx(1, "repetition count <= 0.");
    137   1.1       cgd 			break;
    138   1.9   thorpej 		case 'C':
    139   1.9   thorpej 			todo |= SHOW_CPU;
    140   1.9   thorpej 			break;
    141   1.9   thorpej 		case 'd':
    142  1.15     lukem 			todo &= ~SHOW_STATS_ALL;
    143   1.9   thorpej 			todo |= SHOW_STATS_1;
    144   1.9   thorpej 			break;
    145   1.9   thorpej 		case 'D':
    146  1.15     lukem 			todo &= ~SHOW_STATS_ALL;
    147   1.9   thorpej 			todo |= SHOW_STATS_2;
    148   1.9   thorpej 			break;
    149   1.9   thorpej 		case 'I':
    150   1.9   thorpej 			todo |= SHOW_TOTALS;
    151   1.9   thorpej 			break;
    152   1.1       cgd 		case 'M':
    153   1.6       cgd 			memf = optarg;
    154   1.1       cgd 			break;
    155   1.1       cgd 		case 'N':
    156   1.6       cgd 			nlistf = optarg;
    157   1.1       cgd 			break;
    158   1.9   thorpej 		case 'T':
    159   1.9   thorpej 			todo |= SHOW_TTY;
    160   1.9   thorpej 			break;
    161   1.1       cgd 		case 'w':
    162   1.6       cgd 			if ((interval = atoi(optarg)) <= 0)
    163   1.6       cgd 				errx(1, "interval <= 0.");
    164   1.1       cgd 			break;
    165  1.15     lukem 		case 'x':
    166  1.15     lukem 			todo &= ~SHOW_STATS_ALL;
    167  1.15     lukem 			todo |= SHOW_STATS_X;
    168  1.15     lukem 			break;
    169   1.1       cgd 		case '?':
    170   1.1       cgd 		default:
    171   1.1       cgd 			usage();
    172   1.1       cgd 		}
    173   1.1       cgd 	argc -= optind;
    174   1.1       cgd 	argv += optind;
    175   1.1       cgd 
    176  1.15     lukem 	if (!ISSET(todo, SHOW_CPU | SHOW_TTY | SHOW_STATS_ALL))
    177   1.9   thorpej 		todo |= SHOW_CPU | SHOW_TTY | SHOW_STATS_1;
    178  1.15     lukem 	if (ISSET(todo, SHOW_STATS_X)) {
    179  1.15     lukem 		todo &= ~(SHOW_CPU | SHOW_TTY | SHOW_STATS_ALL);
    180  1.15     lukem 		todo |= SHOW_STATS_X;
    181  1.15     lukem 	}
    182   1.6       cgd 
    183  1.38    simonb 	if (ioctl(STDOUT_FILENO, TIOCGSIZE, &ts) != -1) {
    184  1.38    simonb 		if (ts.ts_lines)
    185  1.38    simonb 			winlines = ts.ts_lines;
    186  1.38    simonb 		if (ts.ts_cols)
    187  1.38    simonb 			wincols = ts.ts_cols;
    188  1.38    simonb 	}
    189  1.38    simonb 
    190  1.38    simonb 	defdrives = wincols;
    191  1.38    simonb 	if (ISSET(todo, SHOW_CPU))
    192  1.38    simonb 		defdrives -= 16;	/* XXX magic number */
    193  1.38    simonb 	if (ISSET(todo, SHOW_TTY))
    194  1.38    simonb 		defdrives -= 9;		/* XXX magic number */
    195  1.38    simonb 	defdrives /= 18;		/* XXX magic number */
    196  1.38    simonb 
    197  1.25    simonb 	dkinit(0);
    198  1.45     blymn 	tpinit(0);
    199   1.9   thorpej 	dkreadstats();
    200  1.45     blymn 	tpreadstats();
    201  1.34     enami 	ndrives = selectdrives(argc, argv);
    202  1.34     enami 	if (ndrives == 0) {
    203  1.34     enami 		/* No drives are selected.  No need to show disk stats. */
    204  1.34     enami 		todo &= ~SHOW_STATS_ALL;
    205  1.34     enami 		if (todo == 0)
    206  1.34     enami 			errx(1, "no drives");
    207  1.34     enami 	}
    208  1.34     enami 	if (ISSET(todo, SHOW_STATS_X))
    209  1.34     enami 		lines = ndrives;
    210  1.34     enami 	else
    211  1.34     enami 		lines = 1;
    212   1.1       cgd 
    213   1.9   thorpej 	tv.tv_sec = interval;
    214  1.28   mycroft 	tv.tv_nsec = 0;
    215   1.1       cgd 
    216   1.9   thorpej 	/* print a new header on sigcont */
    217  1.39       mrg 	(void)signal(SIGCONT, sig_header);
    218   1.1       cgd 
    219   1.1       cgd 	for (hdrcnt = 1;;) {
    220  1.44       mrg 		if (do_header ||
    221  1.44       mrg 		    ((hdrcnt -= lines) <= 0) ||
    222  1.44       mrg 		    ISSET(todo, SHOW_STATS_X)) {
    223  1.39       mrg 			do_header = 0;
    224  1.39       mrg 			header();
    225  1.46      yamt 			hdrcnt = winlines - 4;
    226   1.1       cgd 		}
    227   1.9   thorpej 
    228  1.45     blymn 		if (!ISSET(todo, SHOW_TOTALS)) {
    229   1.9   thorpej 			dkswap();
    230  1.45     blymn 			tpswap();
    231  1.45     blymn 		}
    232  1.45     blymn 
    233   1.9   thorpej 		display();
    234   1.1       cgd 
    235   1.1       cgd 		if (reps >= 0 && --reps <= 0)
    236   1.1       cgd 			break;
    237  1.28   mycroft 		nanosleep(&tv, NULL);
    238   1.9   thorpej 		dkreadstats();
    239  1.45     blymn 		tpreadstats();
    240   1.1       cgd 	}
    241   1.1       cgd 	exit(0);
    242   1.1       cgd }
    243   1.1       cgd 
    244   1.9   thorpej static void
    245  1.39       mrg sig_header(int signo)
    246  1.39       mrg {
    247  1.39       mrg 	do_header = 1;
    248  1.39       mrg }
    249  1.39       mrg 
    250  1.39       mrg static void
    251  1.39       mrg header()
    252   1.1       cgd {
    253  1.12     lukem 	int i;
    254   1.1       cgd 
    255  1.29       mrg 					/* Main Headers. */
    256  1.29       mrg 	if (ISSET(todo, SHOW_STATS_X)) {
    257  1.29       mrg 		if (ISSET(todo, SHOW_TOTALS)) {
    258  1.31     enami 			(void)printf(
    259  1.42       dbj 			    "device  read KB/t    xfr   time     MB  ");
    260  1.42       dbj 			(void)printf(" write KB/t    xfr   time     MB\n");
    261  1.29       mrg 		} else {
    262  1.31     enami 			(void)printf(
    263  1.31     enami 			    "device  read KB/t    r/s   time     MB/s");
    264  1.29       mrg 			(void)printf(" write KB/t    w/s   time     MB/s\n");
    265  1.29       mrg 		}
    266  1.15     lukem 		return;
    267  1.29       mrg 	}
    268  1.15     lukem 
    269   1.9   thorpej 	if (ISSET(todo, SHOW_TTY))
    270   1.9   thorpej 		(void)printf("      tty");
    271   1.9   thorpej 
    272  1.45     blymn 	if (ISSET(todo, SHOW_STATS_1)) {
    273  1.15     lukem 		for (i = 0; i < dk_ndrive; i++)
    274  1.15     lukem 			if (cur.dk_select[i])
    275  1.36    simonb 				(void)printf("        %9.9s ", cur.dk_name[i]);
    276  1.45     blymn 		for (i = 0; i < tp_ndrive; i++)
    277  1.45     blymn 			if (cur_tape.select[i])
    278  1.45     blymn 				(void)printf("        %9.9s ",
    279  1.45     blymn 					     cur_tape.name[i]);
    280  1.45     blymn 	}
    281   1.9   thorpej 
    282  1.45     blymn 	if (ISSET(todo, SHOW_STATS_2)) {
    283  1.15     lukem 		for (i = 0; i < dk_ndrive; i++)
    284  1.15     lukem 			if (cur.dk_select[i])
    285  1.36    simonb 				(void)printf("        %9.9s ", cur.dk_name[i]);
    286  1.45     blymn 		for (i = 0; i < tp_ndrive; i++)
    287  1.45     blymn 			if (cur_tape.select[i])
    288  1.45     blymn 				(void)printf("        %9.9s ",
    289  1.45     blymn 					     cur_tape.name[i]);
    290  1.45     blymn 	}
    291  1.45     blymn 
    292   1.9   thorpej 	if (ISSET(todo, SHOW_CPU))
    293  1.41       wiz 		(void)printf("            CPU");
    294  1.15     lukem 
    295   1.9   thorpej 	printf("\n");
    296   1.9   thorpej 
    297  1.15     lukem 					/* Sub-Headers. */
    298   1.9   thorpej 	if (ISSET(todo, SHOW_TTY))
    299   1.9   thorpej 		printf(" tin tout");
    300   1.9   thorpej 
    301  1.15     lukem 	if (ISSET(todo, SHOW_STATS_1)) {
    302  1.15     lukem 		for (i = 0; i < dk_ndrive; i++)
    303  1.15     lukem 			if (cur.dk_select[i]) {
    304  1.15     lukem 				if (ISSET(todo, SHOW_TOTALS))
    305  1.36    simonb 					(void)printf("  KB/t  xfr  MB   ");
    306  1.15     lukem 				else
    307  1.36    simonb 					(void)printf("  KB/t  t/s  MB/s ");
    308  1.15     lukem 			}
    309  1.45     blymn 		for (i = 0; i < tp_ndrive; i++)
    310  1.45     blymn 			if (cur_tape.select[i]) {
    311  1.45     blymn 				if (ISSET(todo, SHOW_TOTALS))
    312  1.45     blymn 					(void)printf("  KB/t  xfr  MB   ");
    313  1.45     blymn 				else
    314  1.45     blymn 					(void)printf("  KB/t  t/s  MB/s ");
    315  1.45     blymn 			}
    316  1.15     lukem 	}
    317   1.9   thorpej 
    318  1.45     blymn 	if (ISSET(todo, SHOW_STATS_2)) {
    319  1.15     lukem 		for (i = 0; i < dk_ndrive; i++)
    320  1.15     lukem 			if (cur.dk_select[i])
    321  1.36    simonb 				(void)printf("    KB   xfr time ");
    322  1.45     blymn 		for (i = 0; i < tp_ndrive; i++)
    323  1.45     blymn 			if (cur_tape.select[i])
    324  1.45     blymn 				(void)printf("    KB   xfr time ");
    325  1.45     blymn 	}
    326   1.9   thorpej 
    327   1.9   thorpej 	if (ISSET(todo, SHOW_CPU))
    328   1.9   thorpej 		(void)printf(" us ni sy in id");
    329   1.9   thorpej 	printf("\n");
    330   1.1       cgd }
    331   1.1       cgd 
    332   1.9   thorpej static void
    333  1.23    simonb disk_stats(double etime)
    334   1.1       cgd {
    335  1.12     lukem 	int dn;
    336   1.9   thorpej 	double atime, mbps;
    337   1.1       cgd 
    338   1.1       cgd 	for (dn = 0; dn < dk_ndrive; ++dn) {
    339   1.9   thorpej 		if (!cur.dk_select[dn])
    340   1.1       cgd 			continue;
    341  1.15     lukem 					/* average Kbytes per transfer. */
    342  1.29       mrg 		if (cur.dk_rxfer[dn] + cur.dk_wxfer[dn])
    343  1.32     enami 			mbps = ((cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) /
    344  1.32     enami 			    1024.0) / (cur.dk_rxfer[dn] + cur.dk_wxfer[dn]);
    345   1.9   thorpej 		else
    346   1.9   thorpej 			mbps = 0.0;
    347  1.27  sommerfe 		(void)printf(" %5.2f", mbps);
    348   1.9   thorpej 
    349  1.15     lukem 					/* average transfers per second. */
    350  1.36    simonb 		(void)printf(" %4.0f",
    351  1.32     enami 		    (cur.dk_rxfer[dn] + cur.dk_wxfer[dn]) / etime);
    352   1.9   thorpej 
    353  1.15     lukem 					/* time busy in disk activity */
    354   1.9   thorpej 		atime = (double)cur.dk_time[dn].tv_sec +
    355  1.31     enami 		    ((double)cur.dk_time[dn].tv_usec / (double)1000000);
    356   1.9   thorpej 
    357  1.15     lukem 					/* Megabytes per second. */
    358   1.9   thorpej 		if (atime != 0.0)
    359  1.29       mrg 			mbps = (cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) /
    360  1.29       mrg 			    (double)(1024 * 1024);
    361  1.27  sommerfe 		else
    362   1.9   thorpej 			mbps = 0;
    363  1.36    simonb 		(void)printf(" %5.2f ", mbps / etime);
    364   1.1       cgd 	}
    365   1.1       cgd }
    366   1.1       cgd 
    367   1.9   thorpej static void
    368  1.23    simonb disk_stats2(double etime)
    369   1.9   thorpej {
    370  1.12     lukem 	int dn;
    371   1.9   thorpej 	double atime;
    372   1.9   thorpej 
    373   1.9   thorpej 	for (dn = 0; dn < dk_ndrive; ++dn) {
    374   1.9   thorpej 		if (!cur.dk_select[dn])
    375   1.9   thorpej 			continue;
    376   1.9   thorpej 
    377  1.15     lukem 					/* average kbytes per second. */
    378  1.36    simonb 		(void)printf(" %5.0f",
    379  1.32     enami 		    (cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) / 1024.0 / etime);
    380   1.9   thorpej 
    381  1.15     lukem 					/* average transfers per second. */
    382  1.36    simonb 		(void)printf(" %5.0f",
    383  1.32     enami 		    (cur.dk_rxfer[dn] + cur.dk_wxfer[dn]) / etime);
    384   1.9   thorpej 
    385  1.15     lukem 					/* average time busy in disk activity */
    386   1.9   thorpej 		atime = (double)cur.dk_time[dn].tv_sec +
    387  1.31     enami 		    ((double)cur.dk_time[dn].tv_usec / (double)1000000);
    388   1.9   thorpej 		(void)printf(" %4.2f ", atime / etime);
    389   1.9   thorpej 	}
    390   1.9   thorpej }
    391   1.9   thorpej 
    392   1.9   thorpej static void
    393  1.23    simonb disk_statsx(double etime)
    394  1.15     lukem {
    395  1.15     lukem 	int dn;
    396  1.15     lukem 	double atime, kbps;
    397  1.15     lukem 
    398  1.15     lukem 	for (dn = 0; dn < dk_ndrive; ++dn) {
    399  1.33  christos 		if (!cur.dk_select[dn])
    400  1.33  christos 			continue;
    401  1.33  christos 
    402  1.15     lukem 		(void)printf("%-8.8s", cur.dk_name[dn]);
    403  1.15     lukem 
    404  1.29       mrg 					/* average read Kbytes per transfer */
    405  1.29       mrg 		if (cur.dk_rxfer[dn])
    406  1.31     enami 			kbps = (cur.dk_rbytes[dn] / 1024.0) / cur.dk_rxfer[dn];
    407  1.15     lukem 		else
    408  1.15     lukem 			kbps = 0.0;
    409  1.27  sommerfe 		(void)printf(" %8.2f", kbps);
    410  1.15     lukem 
    411  1.31     enami 					/* average read transfers
    412  1.31     enami 					   (per second) */
    413  1.29       mrg 		(void)printf(" %6.0f", cur.dk_rxfer[dn] / etime);
    414  1.15     lukem 
    415  1.29       mrg 					/* time read busy in disk activity */
    416  1.15     lukem 		atime = (double)cur.dk_time[dn].tv_sec +
    417  1.31     enami 		    ((double)cur.dk_time[dn].tv_usec / (double)1000000);
    418  1.29       mrg 		(void)printf(" %6.2f", atime / etime);
    419  1.15     lukem 
    420  1.31     enami 					/* average read megabytes
    421  1.31     enami 					   (per second) */
    422  1.17     lukem 		(void)printf(" %8.2f",
    423  1.29       mrg 		    cur.dk_rbytes[dn] / (1024.0 * 1024) / etime);
    424  1.29       mrg 
    425  1.15     lukem 
    426  1.29       mrg 					/* average write Kbytes per transfer */
    427  1.29       mrg 		if (cur.dk_wxfer[dn])
    428  1.31     enami 			kbps = (cur.dk_wbytes[dn] / 1024.0) / cur.dk_wxfer[dn];
    429  1.29       mrg 		else
    430  1.29       mrg 			kbps = 0.0;
    431  1.29       mrg 		(void)printf("   %8.2f", kbps);
    432  1.29       mrg 
    433  1.31     enami 					/* average write transfers
    434  1.31     enami 					   (per second) */
    435  1.29       mrg 		(void)printf(" %6.0f", cur.dk_wxfer[dn] / etime);
    436  1.29       mrg 
    437  1.29       mrg 					/* time write busy in disk activity */
    438  1.29       mrg 		atime = (double)cur.dk_time[dn].tv_sec +
    439  1.31     enami 		    ((double)cur.dk_time[dn].tv_usec / (double)1000000);
    440  1.29       mrg 		(void)printf(" %6.2f", atime / etime);
    441  1.29       mrg 
    442  1.31     enami 					/* average write megabytes
    443  1.31     enami 					   (per second) */
    444  1.33  christos 		(void)printf(" %8.2f\n",
    445  1.29       mrg 		    cur.dk_wbytes[dn] / (1024.0 * 1024) / etime);
    446  1.15     lukem 	}
    447  1.15     lukem }
    448  1.15     lukem 
    449  1.15     lukem static void
    450  1.45     blymn tape_stats(double etime)
    451  1.45     blymn {
    452  1.45     blymn 	int dn;
    453  1.45     blymn 	double atime, mbps;
    454  1.45     blymn 
    455  1.45     blymn 	for (dn = 0; dn < tp_ndrive; ++dn) {
    456  1.45     blymn 		if (!cur_tape.select[dn])
    457  1.45     blymn 			continue;
    458  1.45     blymn 					/* average Kbytes per transfer. */
    459  1.45     blymn 		if (cur_tape.rxfer[dn] + cur_tape.wxfer[dn])
    460  1.45     blymn 			mbps = ((cur_tape.rbytes[dn] + cur_tape.wbytes[dn]) /
    461  1.45     blymn 			    1024.0) / (cur_tape.rxfer[dn] + cur_tape.wxfer[dn]);
    462  1.45     blymn 		else
    463  1.45     blymn 			mbps = 0.0;
    464  1.45     blymn 		(void)printf(" %5.2f", mbps);
    465  1.45     blymn 
    466  1.45     blymn 					/* average transfers per second. */
    467  1.45     blymn 		(void)printf(" %4.0f",
    468  1.45     blymn 		    (cur_tape.rxfer[dn] + cur_tape.wxfer[dn]) / etime);
    469  1.45     blymn 
    470  1.45     blymn 					/* time busy in disk activity */
    471  1.45     blymn 		atime = (double)cur_tape.time[dn].tv_sec +
    472  1.45     blymn 		    ((double)cur_tape.time[dn].tv_usec / (double)1000000);
    473  1.45     blymn 
    474  1.45     blymn 					/* Megabytes per second. */
    475  1.45     blymn 		if (atime != 0.0)
    476  1.45     blymn 			mbps = (cur_tape.rbytes[dn] + cur_tape.wbytes[dn]) /
    477  1.45     blymn 			    (double)(1024 * 1024);
    478  1.45     blymn 		else
    479  1.45     blymn 			mbps = 0;
    480  1.45     blymn 		(void)printf(" %5.2f ", mbps / etime);
    481  1.45     blymn 	}
    482  1.45     blymn }
    483  1.45     blymn 
    484  1.45     blymn static void
    485  1.45     blymn tape_stats2(double etime)
    486  1.45     blymn {
    487  1.45     blymn 	int dn;
    488  1.45     blymn 	double atime;
    489  1.45     blymn 
    490  1.45     blymn 	for (dn = 0; dn < tp_ndrive; ++dn) {
    491  1.45     blymn 		if (!cur_tape.select[dn])
    492  1.45     blymn 			continue;
    493  1.45     blymn 
    494  1.45     blymn 					/* average kbytes per second. */
    495  1.45     blymn 		(void)printf(" %5.0f",
    496  1.45     blymn 		    (cur_tape.rbytes[dn] + cur_tape.wbytes[dn]) / 1024.0 / etime);
    497  1.45     blymn 
    498  1.45     blymn 					/* average transfers per second. */
    499  1.45     blymn 		(void)printf(" %5.0f",
    500  1.45     blymn 		    (cur_tape.rxfer[dn] + cur_tape.wxfer[dn]) / etime);
    501  1.45     blymn 
    502  1.45     blymn 					/* average time busy in disk activity */
    503  1.45     blymn 		atime = (double)cur_tape.time[dn].tv_sec +
    504  1.45     blymn 		    ((double)cur_tape.time[dn].tv_usec / (double)1000000);
    505  1.45     blymn 		(void)printf(" %4.2f ", atime / etime);
    506  1.45     blymn 	}
    507  1.45     blymn }
    508  1.45     blymn 
    509  1.45     blymn static void
    510  1.45     blymn tape_statsx(double etime)
    511  1.45     blymn {
    512  1.45     blymn 	int dn;
    513  1.45     blymn 	double atime, kbps;
    514  1.45     blymn 
    515  1.45     blymn 	for (dn = 0; dn < tp_ndrive; ++dn) {
    516  1.45     blymn 		if (!cur_tape.select[dn])
    517  1.45     blymn 			continue;
    518  1.45     blymn 
    519  1.45     blymn 		(void)printf("%-8.8s", cur_tape.name[dn]);
    520  1.45     blymn 
    521  1.45     blymn 					/* average read Kbytes per transfer */
    522  1.45     blymn 		if (cur.dk_rxfer[dn])
    523  1.45     blymn 			kbps = (cur_tape.rbytes[dn] / 1024.0) / cur_tape.rxfer[dn];
    524  1.45     blymn 		else
    525  1.45     blymn 			kbps = 0.0;
    526  1.45     blymn 		(void)printf(" %8.2f", kbps);
    527  1.45     blymn 
    528  1.45     blymn 					/* average read transfers
    529  1.45     blymn 					   (per second) */
    530  1.45     blymn 		(void)printf(" %6.0f", cur_tape.rxfer[dn] / etime);
    531  1.45     blymn 
    532  1.45     blymn 					/* time read busy in disk activity */
    533  1.45     blymn 		atime = (double)cur_tape.time[dn].tv_sec +
    534  1.45     blymn 		    ((double)cur_tape.time[dn].tv_usec / (double)1000000);
    535  1.45     blymn 		(void)printf(" %6.2f", atime / etime);
    536  1.45     blymn 
    537  1.45     blymn 					/* average read megabytes
    538  1.45     blymn 					   (per second) */
    539  1.45     blymn 		(void)printf(" %8.2f",
    540  1.45     blymn 		    cur_tape.rbytes[dn] / (1024.0 * 1024) / etime);
    541  1.45     blymn 
    542  1.45     blymn 
    543  1.45     blymn 					/* average write Kbytes per transfer */
    544  1.45     blymn 		if (cur_tape.wxfer[dn])
    545  1.45     blymn 			kbps = (cur_tape.wbytes[dn] / 1024.0) / cur_tape.wxfer[dn];
    546  1.45     blymn 		else
    547  1.45     blymn 			kbps = 0.0;
    548  1.45     blymn 		(void)printf("   %8.2f", kbps);
    549  1.45     blymn 
    550  1.45     blymn 					/* average write transfers
    551  1.45     blymn 					   (per second) */
    552  1.45     blymn 		(void)printf(" %6.0f", cur_tape.wxfer[dn] / etime);
    553  1.45     blymn 
    554  1.45     blymn 					/* time write busy in disk activity */
    555  1.45     blymn 		atime = (double)cur_tape.time[dn].tv_sec +
    556  1.45     blymn 		    ((double)cur_tape.time[dn].tv_usec / (double)1000000);
    557  1.45     blymn 		(void)printf(" %6.2f", atime / etime);
    558  1.45     blymn 
    559  1.45     blymn 					/* average write megabytes
    560  1.45     blymn 					   (per second) */
    561  1.45     blymn 		(void)printf(" %8.2f\n",
    562  1.45     blymn 		    cur_tape.wbytes[dn] / (1024.0 * 1024) / etime);
    563  1.45     blymn 	}
    564  1.45     blymn }
    565  1.45     blymn 
    566  1.45     blymn static void
    567  1.23    simonb cpustats(void)
    568   1.1       cgd {
    569  1.12     lukem 	int state;
    570   1.1       cgd 	double time;
    571   1.1       cgd 
    572   1.1       cgd 	time = 0;
    573   1.1       cgd 	for (state = 0; state < CPUSTATES; ++state)
    574   1.1       cgd 		time += cur.cp_time[state];
    575   1.9   thorpej 	if (!time)
    576   1.9   thorpej 		time = 1.0;
    577  1.15     lukem 			/* States are generally never 100% and can use %3.0f. */
    578   1.1       cgd 	for (state = 0; state < CPUSTATES; ++state)
    579  1.21    simonb 		printf(" %2.0f", 100. * cur.cp_time[state] / time);
    580   1.1       cgd }
    581   1.1       cgd 
    582   1.9   thorpej static void
    583  1.23    simonb usage(void)
    584   1.1       cgd {
    585  1.13       mrg 
    586  1.31     enami 	(void)fprintf(stderr, "usage: iostat [-CdDITx] [-c count] [-M core] "
    587  1.31     enami 	    "[-N system] [-w wait] [drives]\n");
    588   1.1       cgd 	exit(1);
    589   1.9   thorpej }
    590   1.9   thorpej 
    591   1.9   thorpej static void
    592  1.23    simonb display(void)
    593   1.9   thorpej {
    594   1.9   thorpej 	double	etime;
    595   1.9   thorpej 
    596   1.9   thorpej 	/* Sum up the elapsed ticks. */
    597  1.27  sommerfe 	etime = cur.cp_etime;
    598   1.9   thorpej 
    599  1.34     enami 	/*
    600  1.34     enami 	 * If we're showing totals only, then don't divide by the
    601   1.9   thorpej 	 * system time.
    602   1.9   thorpej 	 */
    603   1.9   thorpej 	if (ISSET(todo, SHOW_TOTALS))
    604   1.9   thorpej 		etime = 1.0;
    605   1.9   thorpej 
    606  1.34     enami 	if (ISSET(todo, SHOW_STATS_X)) {
    607  1.34     enami 		disk_statsx(etime);
    608  1.45     blymn 		tape_statsx(etime);
    609  1.34     enami 		goto out;
    610  1.34     enami 	}
    611  1.34     enami 
    612   1.9   thorpej 	if (ISSET(todo, SHOW_TTY))
    613   1.9   thorpej 		printf("%4.0f %4.0f", cur.tk_nin / etime, cur.tk_nout / etime);
    614  1.27  sommerfe 
    615  1.45     blymn 	if (ISSET(todo, SHOW_STATS_1)) {
    616   1.9   thorpej 		disk_stats(etime);
    617  1.45     blymn 		tape_stats(etime);
    618  1.45     blymn 	}
    619  1.45     blymn 
    620   1.9   thorpej 
    621  1.45     blymn 	if (ISSET(todo, SHOW_STATS_2)) {
    622   1.9   thorpej 		disk_stats2(etime);
    623  1.45     blymn 		tape_stats2(etime);
    624  1.45     blymn 	}
    625  1.45     blymn 
    626  1.15     lukem 
    627   1.9   thorpej 	if (ISSET(todo, SHOW_CPU))
    628   1.9   thorpej 		cpustats();
    629   1.9   thorpej 
    630  1.34     enami 	(void)printf("\n");
    631  1.34     enami 
    632  1.34     enami out:
    633   1.9   thorpej 	(void)fflush(stdout);
    634   1.9   thorpej }
    635   1.9   thorpej 
    636  1.34     enami static int
    637  1.23    simonb selectdrives(int argc, char *argv[])
    638   1.9   thorpej {
    639  1.34     enami 	int	i, maxdrives, ndrives, tried;
    640   1.9   thorpej 
    641   1.9   thorpej 	/*
    642   1.9   thorpej 	 * Choose drives to be displayed.  Priority goes to (in order) drives
    643   1.9   thorpej 	 * supplied as arguments and default drives.  If everything isn't
    644   1.9   thorpej 	 * filled in and there are drives not taken care of, display the first
    645   1.9   thorpej 	 * few that fit.
    646   1.9   thorpej 	 *
    647   1.9   thorpej 	 * The backward compatibility #ifdefs permit the syntax:
    648   1.9   thorpej 	 *	iostat [ drives ] [ interval [ count ] ]
    649   1.9   thorpej 	 */
    650  1.19      tron 
    651   1.9   thorpej #define	BACKWARD_COMPATIBILITY
    652  1.34     enami 	for (tried = ndrives = 0; *argv; ++argv) {
    653  1.34     enami #ifdef BACKWARD_COMPATIBILITY
    654  1.43       dsl 		if (isdigit((unsigned char)**argv))
    655   1.9   thorpej 			break;
    656   1.9   thorpej #endif
    657  1.34     enami 		tried++;
    658  1.33  christos 		for (i = 0; i < dk_ndrive; i++) {
    659  1.34     enami 			if (strcmp(cur.dk_name[i], *argv))
    660  1.33  christos 				continue;
    661  1.33  christos 			cur.dk_select[i] = 1;
    662  1.33  christos 			++ndrives;
    663  1.33  christos 		}
    664  1.45     blymn 
    665  1.45     blymn 		for (i = 0; i < tp_ndrive; i++) {
    666  1.45     blymn 			if (strcmp(cur_tape.name[i], *argv))
    667  1.45     blymn 				continue;
    668  1.45     blymn 			cur_tape.select[i] = 1;
    669  1.45     blymn 			++ndrives;
    670  1.45     blymn 		}
    671   1.9   thorpej 	}
    672  1.34     enami 
    673  1.34     enami 	if (ndrives == 0 && tried == 0) {
    674  1.34     enami 		/*
    675  1.38    simonb 		 * Pick up to defdrives (or all if -x is given) drives
    676  1.34     enami 		 * if none specified.
    677  1.34     enami 		 */
    678  1.39       mrg 		maxdrives = (ISSET(todo, SHOW_STATS_X) ||
    679  1.45     blymn 			     (dk_ndrive + tp_ndrive) < defdrives)
    680  1.45     blymn 			? (dk_ndrive + tp_ndrive) : defdrives;
    681  1.34     enami 		for (i = 0; i < maxdrives; i++) {
    682  1.45     blymn 			if (i >= dk_ndrive) {
    683  1.45     blymn 				cur_tape.select[i - dk_ndrive] = 1;
    684  1.45     blymn 			} else {
    685  1.45     blymn 				cur.dk_select[i] = 1;
    686  1.45     blymn 			}
    687  1.45     blymn 
    688  1.34     enami 			++ndrives;
    689  1.38    simonb 			if (!ISSET(todo, SHOW_STATS_X) && ndrives == defdrives)
    690  1.34     enami 				break;
    691  1.34     enami 		}
    692  1.34     enami 	}
    693  1.34     enami 
    694  1.34     enami #ifdef BACKWARD_COMPATIBILITY
    695   1.9   thorpej 	if (*argv) {
    696   1.9   thorpej 		interval = atoi(*argv);
    697   1.9   thorpej 		if (*++argv)
    698   1.9   thorpej 			reps = atoi(*argv);
    699   1.9   thorpej 	}
    700   1.9   thorpej #endif
    701   1.9   thorpej 
    702   1.9   thorpej 	if (interval) {
    703   1.9   thorpej 		if (!reps)
    704   1.9   thorpej 			reps = -1;
    705   1.9   thorpej 	} else
    706   1.9   thorpej 		if (reps)
    707   1.9   thorpej 			interval = 1;
    708   1.9   thorpej 
    709  1.34     enami 	return (ndrives);
    710   1.1       cgd }
    711