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