Home | History | Annotate | Line # | Download | only in iostat
iostat.c revision 1.35
      1  1.35  drochner /*	$NetBSD: iostat.c,v 1.35 2003/06/11 17:26:32 drochner 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.35  drochner __RCSID("$NetBSD: iostat.c,v 1.35 2003/06/11 17:26:32 drochner 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.22   thorpej #include <sys/sched.h>
     84   1.9   thorpej #include <sys/time.h>
     85   1.6       cgd 
     86   1.6       cgd #include <err.h>
     87   1.6       cgd #include <ctype.h>
     88   1.6       cgd #include <signal.h>
     89   1.1       cgd #include <stdio.h>
     90   1.1       cgd #include <stdlib.h>
     91   1.1       cgd #include <string.h>
     92   1.6       cgd #include <unistd.h>
     93   1.1       cgd 
     94   1.9   thorpej #include "dkstats.h"
     95   1.1       cgd 
     96   1.9   thorpej /* Namelist and memory files. */
     97   1.9   thorpej char	*nlistf, *memf;
     98   1.9   thorpej 
     99   1.9   thorpej int		hz, reps, interval;
    100   1.9   thorpej static int	todo = 0;
    101   1.9   thorpej 
    102   1.9   thorpej #define ISSET(x, a)	((x) & (a))
    103  1.15     lukem #define SHOW_CPU	1<<0
    104  1.15     lukem #define SHOW_TTY	1<<1
    105  1.15     lukem #define SHOW_STATS_1	1<<2
    106  1.15     lukem #define SHOW_STATS_2	1<<3
    107  1.15     lukem #define SHOW_STATS_X	1<<4
    108  1.15     lukem #define SHOW_TOTALS	1<<7
    109  1.15     lukem #define SHOW_STATS_ALL	(SHOW_STATS_1 | SHOW_STATS_2 | SHOW_STATS_X)
    110   1.9   thorpej 
    111  1.23    simonb static void cpustats(void);
    112  1.23    simonb static void disk_stats(double);
    113  1.23    simonb static void disk_stats2(double);
    114  1.23    simonb static void disk_statsx(double);
    115  1.23    simonb static void header(int);
    116  1.23    simonb static void usage(void);
    117  1.23    simonb static void display(void);
    118  1.34     enami static int selectdrives(int, char *[]);
    119  1.23    simonb 
    120  1.34     enami int main(int, char *[]);
    121   1.1       cgd 
    122   1.6       cgd int
    123  1.23    simonb main(int argc, char *argv[])
    124   1.1       cgd {
    125  1.34     enami 	int ch, hdrcnt, ndrives, lines;
    126  1.28   mycroft 	struct timespec	tv;
    127   1.9   thorpej 
    128  1.15     lukem 	while ((ch = getopt(argc, argv, "Cc:dDIM:N:Tw:x")) != -1)
    129  1.31     enami 		switch (ch) {
    130   1.1       cgd 		case 'c':
    131   1.6       cgd 			if ((reps = atoi(optarg)) <= 0)
    132   1.6       cgd 				errx(1, "repetition count <= 0.");
    133   1.1       cgd 			break;
    134   1.9   thorpej 		case 'C':
    135   1.9   thorpej 			todo |= SHOW_CPU;
    136   1.9   thorpej 			break;
    137   1.9   thorpej 		case 'd':
    138  1.15     lukem 			todo &= ~SHOW_STATS_ALL;
    139   1.9   thorpej 			todo |= SHOW_STATS_1;
    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_2;
    144   1.9   thorpej 			break;
    145   1.9   thorpej 		case 'I':
    146   1.9   thorpej 			todo |= SHOW_TOTALS;
    147   1.9   thorpej 			break;
    148   1.1       cgd 		case 'M':
    149   1.6       cgd 			memf = optarg;
    150   1.1       cgd 			break;
    151   1.1       cgd 		case 'N':
    152   1.6       cgd 			nlistf = optarg;
    153   1.1       cgd 			break;
    154   1.9   thorpej 		case 'T':
    155   1.9   thorpej 			todo |= SHOW_TTY;
    156   1.9   thorpej 			break;
    157   1.1       cgd 		case 'w':
    158   1.6       cgd 			if ((interval = atoi(optarg)) <= 0)
    159   1.6       cgd 				errx(1, "interval <= 0.");
    160   1.1       cgd 			break;
    161  1.15     lukem 		case 'x':
    162  1.15     lukem 			todo &= ~SHOW_STATS_ALL;
    163  1.15     lukem 			todo |= SHOW_STATS_X;
    164  1.15     lukem 			break;
    165   1.1       cgd 		case '?':
    166   1.1       cgd 		default:
    167   1.1       cgd 			usage();
    168   1.1       cgd 		}
    169   1.1       cgd 	argc -= optind;
    170   1.1       cgd 	argv += optind;
    171   1.1       cgd 
    172  1.15     lukem 	if (!ISSET(todo, SHOW_CPU | SHOW_TTY | SHOW_STATS_ALL))
    173   1.9   thorpej 		todo |= SHOW_CPU | SHOW_TTY | SHOW_STATS_1;
    174  1.15     lukem 	if (ISSET(todo, SHOW_STATS_X)) {
    175  1.15     lukem 		todo &= ~(SHOW_CPU | SHOW_TTY | SHOW_STATS_ALL);
    176  1.15     lukem 		todo |= SHOW_STATS_X;
    177  1.15     lukem 	}
    178   1.6       cgd 
    179  1.25    simonb 	dkinit(0);
    180   1.9   thorpej 	dkreadstats();
    181  1.34     enami 	ndrives = selectdrives(argc, argv);
    182  1.34     enami 	if (ndrives == 0) {
    183  1.34     enami 		/* No drives are selected.  No need to show disk stats. */
    184  1.34     enami 		todo &= ~SHOW_STATS_ALL;
    185  1.34     enami 		if (todo == 0)
    186  1.34     enami 			errx(1, "no drives");
    187  1.34     enami 	}
    188  1.34     enami 	if (ISSET(todo, SHOW_STATS_X))
    189  1.34     enami 		lines = ndrives;
    190  1.34     enami 	else
    191  1.34     enami 		lines = 1;
    192   1.1       cgd 
    193   1.9   thorpej 	tv.tv_sec = interval;
    194  1.28   mycroft 	tv.tv_nsec = 0;
    195   1.1       cgd 
    196   1.9   thorpej 	/* print a new header on sigcont */
    197   1.9   thorpej 	(void)signal(SIGCONT, header);
    198   1.1       cgd 
    199   1.1       cgd 	for (hdrcnt = 1;;) {
    200  1.34     enami 		if ((hdrcnt -= lines) <= 0) {
    201   1.9   thorpej 			header(0);
    202   1.1       cgd 			hdrcnt = 20;
    203   1.1       cgd 		}
    204   1.9   thorpej 
    205   1.9   thorpej 		if (!ISSET(todo, SHOW_TOTALS))
    206   1.9   thorpej 			dkswap();
    207   1.9   thorpej 		display();
    208   1.1       cgd 
    209   1.1       cgd 		if (reps >= 0 && --reps <= 0)
    210   1.1       cgd 			break;
    211  1.28   mycroft 		nanosleep(&tv, NULL);
    212   1.9   thorpej 		dkreadstats();
    213   1.1       cgd 	}
    214   1.1       cgd 	exit(0);
    215   1.1       cgd }
    216   1.1       cgd 
    217   1.9   thorpej static void
    218  1.23    simonb header(int signo)
    219   1.1       cgd {
    220  1.12     lukem 	int i;
    221   1.1       cgd 
    222  1.29       mrg 					/* Main Headers. */
    223  1.29       mrg 	if (ISSET(todo, SHOW_STATS_X)) {
    224  1.29       mrg 		if (ISSET(todo, SHOW_TOTALS)) {
    225  1.31     enami 			(void)printf(
    226  1.31     enami 			    "device  read KB/t    xfr   time     MB/s");
    227  1.29       mrg 			(void)printf(" write KB/t    xfr   time     MB/s\n");
    228  1.29       mrg 		} else {
    229  1.31     enami 			(void)printf(
    230  1.31     enami 			    "device  read KB/t    r/s   time     MB/s");
    231  1.29       mrg 			(void)printf(" write KB/t    w/s   time     MB/s\n");
    232  1.29       mrg 		}
    233  1.15     lukem 		return;
    234  1.29       mrg 	}
    235  1.15     lukem 
    236   1.9   thorpej 	if (ISSET(todo, SHOW_TTY))
    237   1.9   thorpej 		(void)printf("      tty");
    238   1.9   thorpej 
    239   1.9   thorpej 	if (ISSET(todo, SHOW_STATS_1))
    240  1.15     lukem 		for (i = 0; i < dk_ndrive; i++)
    241  1.15     lukem 			if (cur.dk_select[i])
    242  1.29       mrg 				(void)printf("        %7.7s ", cur.dk_name[i]);
    243   1.9   thorpej 
    244   1.9   thorpej 	if (ISSET(todo, SHOW_STATS_2))
    245  1.15     lukem 		for (i = 0; i < dk_ndrive; i++)
    246  1.15     lukem 			if (cur.dk_select[i])
    247  1.29       mrg 				(void)printf("        %7.7s ", cur.dk_name[i]);
    248   1.9   thorpej 
    249   1.9   thorpej 	if (ISSET(todo, SHOW_CPU))
    250   1.9   thorpej 		(void)printf("            cpu");
    251  1.15     lukem 
    252   1.9   thorpej 	printf("\n");
    253   1.9   thorpej 
    254  1.15     lukem 					/* Sub-Headers. */
    255   1.9   thorpej 	if (ISSET(todo, SHOW_TTY))
    256   1.9   thorpej 		printf(" tin tout");
    257   1.9   thorpej 
    258  1.15     lukem 	if (ISSET(todo, SHOW_STATS_1)) {
    259  1.15     lukem 		for (i = 0; i < dk_ndrive; i++)
    260  1.15     lukem 			if (cur.dk_select[i]) {
    261  1.15     lukem 				if (ISSET(todo, SHOW_TOTALS))
    262  1.15     lukem 					(void)printf("  KB/t xfr MB   ");
    263  1.15     lukem 				else
    264  1.15     lukem 					(void)printf("  KB/t t/s MB/s ");
    265  1.15     lukem 			}
    266  1.15     lukem 	}
    267   1.9   thorpej 
    268   1.9   thorpej 	if (ISSET(todo, SHOW_STATS_2))
    269  1.15     lukem 		for (i = 0; i < dk_ndrive; i++)
    270  1.15     lukem 			if (cur.dk_select[i])
    271  1.34     enami 				(void)printf("   KB  xfr time ");
    272   1.9   thorpej 
    273   1.9   thorpej 	if (ISSET(todo, SHOW_CPU))
    274   1.9   thorpej 		(void)printf(" us ni sy in id");
    275   1.9   thorpej 	printf("\n");
    276   1.1       cgd }
    277   1.1       cgd 
    278   1.9   thorpej static void
    279  1.23    simonb disk_stats(double etime)
    280   1.1       cgd {
    281  1.12     lukem 	int dn;
    282   1.9   thorpej 	double atime, mbps;
    283   1.1       cgd 
    284   1.1       cgd 	for (dn = 0; dn < dk_ndrive; ++dn) {
    285   1.9   thorpej 		if (!cur.dk_select[dn])
    286   1.1       cgd 			continue;
    287  1.15     lukem 					/* average Kbytes per transfer. */
    288  1.29       mrg 		if (cur.dk_rxfer[dn] + cur.dk_wxfer[dn])
    289  1.32     enami 			mbps = ((cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) /
    290  1.32     enami 			    1024.0) / (cur.dk_rxfer[dn] + cur.dk_wxfer[dn]);
    291   1.9   thorpej 		else
    292   1.9   thorpej 			mbps = 0.0;
    293  1.27  sommerfe 		(void)printf(" %5.2f", mbps);
    294   1.9   thorpej 
    295  1.15     lukem 					/* average transfers per second. */
    296  1.31     enami 		(void)printf(" %3.0f",
    297  1.32     enami 		    (cur.dk_rxfer[dn] + cur.dk_wxfer[dn]) / etime);
    298   1.9   thorpej 
    299  1.15     lukem 					/* time busy in disk activity */
    300   1.9   thorpej 		atime = (double)cur.dk_time[dn].tv_sec +
    301  1.31     enami 		    ((double)cur.dk_time[dn].tv_usec / (double)1000000);
    302   1.9   thorpej 
    303  1.15     lukem 					/* Megabytes per second. */
    304   1.9   thorpej 		if (atime != 0.0)
    305  1.29       mrg 			mbps = (cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) /
    306  1.29       mrg 			    (double)(1024 * 1024);
    307  1.27  sommerfe 		else
    308   1.9   thorpej 			mbps = 0;
    309   1.9   thorpej 		(void)printf(" %4.2f ", mbps / etime);
    310   1.1       cgd 	}
    311   1.1       cgd }
    312   1.1       cgd 
    313   1.9   thorpej static void
    314  1.23    simonb disk_stats2(double etime)
    315   1.9   thorpej {
    316  1.12     lukem 	int dn;
    317   1.9   thorpej 	double atime;
    318   1.9   thorpej 
    319   1.9   thorpej 	for (dn = 0; dn < dk_ndrive; ++dn) {
    320   1.9   thorpej 		if (!cur.dk_select[dn])
    321   1.9   thorpej 			continue;
    322   1.9   thorpej 
    323  1.15     lukem 					/* average kbytes per second. */
    324  1.31     enami 		(void)printf(" %4.0f",
    325  1.32     enami 		    (cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) / 1024.0 / etime);
    326   1.9   thorpej 
    327  1.15     lukem 					/* average transfers per second. */
    328  1.31     enami 		(void)printf(" %3.0f",
    329  1.32     enami 		    (cur.dk_rxfer[dn] + cur.dk_wxfer[dn]) / etime);
    330   1.9   thorpej 
    331  1.15     lukem 					/* average time busy in disk activity */
    332   1.9   thorpej 		atime = (double)cur.dk_time[dn].tv_sec +
    333  1.31     enami 		    ((double)cur.dk_time[dn].tv_usec / (double)1000000);
    334   1.9   thorpej 		(void)printf(" %4.2f ", atime / etime);
    335   1.9   thorpej 	}
    336   1.9   thorpej }
    337   1.9   thorpej 
    338   1.9   thorpej static void
    339  1.23    simonb disk_statsx(double etime)
    340  1.15     lukem {
    341  1.15     lukem 	int dn;
    342  1.15     lukem 	double atime, kbps;
    343  1.15     lukem 
    344  1.15     lukem 	for (dn = 0; dn < dk_ndrive; ++dn) {
    345  1.33  christos 		if (!cur.dk_select[dn])
    346  1.33  christos 			continue;
    347  1.33  christos 
    348  1.15     lukem 		(void)printf("%-8.8s", cur.dk_name[dn]);
    349  1.15     lukem 
    350  1.29       mrg 					/* average read Kbytes per transfer */
    351  1.29       mrg 		if (cur.dk_rxfer[dn])
    352  1.31     enami 			kbps = (cur.dk_rbytes[dn] / 1024.0) / cur.dk_rxfer[dn];
    353  1.15     lukem 		else
    354  1.15     lukem 			kbps = 0.0;
    355  1.27  sommerfe 		(void)printf(" %8.2f", kbps);
    356  1.15     lukem 
    357  1.31     enami 					/* average read transfers
    358  1.31     enami 					   (per second) */
    359  1.29       mrg 		(void)printf(" %6.0f", cur.dk_rxfer[dn] / etime);
    360  1.15     lukem 
    361  1.29       mrg 					/* time read busy in disk activity */
    362  1.15     lukem 		atime = (double)cur.dk_time[dn].tv_sec +
    363  1.31     enami 		    ((double)cur.dk_time[dn].tv_usec / (double)1000000);
    364  1.29       mrg 		(void)printf(" %6.2f", atime / etime);
    365  1.15     lukem 
    366  1.31     enami 					/* average read megabytes
    367  1.31     enami 					   (per second) */
    368  1.17     lukem 		(void)printf(" %8.2f",
    369  1.29       mrg 		    cur.dk_rbytes[dn] / (1024.0 * 1024) / etime);
    370  1.29       mrg 
    371  1.15     lukem 
    372  1.29       mrg 					/* average write Kbytes per transfer */
    373  1.29       mrg 		if (cur.dk_wxfer[dn])
    374  1.31     enami 			kbps = (cur.dk_wbytes[dn] / 1024.0) / cur.dk_wxfer[dn];
    375  1.29       mrg 		else
    376  1.29       mrg 			kbps = 0.0;
    377  1.29       mrg 		(void)printf("   %8.2f", kbps);
    378  1.29       mrg 
    379  1.31     enami 					/* average write transfers
    380  1.31     enami 					   (per second) */
    381  1.29       mrg 		(void)printf(" %6.0f", cur.dk_wxfer[dn] / etime);
    382  1.29       mrg 
    383  1.29       mrg 					/* time write busy in disk activity */
    384  1.29       mrg 		atime = (double)cur.dk_time[dn].tv_sec +
    385  1.31     enami 		    ((double)cur.dk_time[dn].tv_usec / (double)1000000);
    386  1.29       mrg 		(void)printf(" %6.2f", atime / etime);
    387  1.29       mrg 
    388  1.31     enami 					/* average write megabytes
    389  1.31     enami 					   (per second) */
    390  1.33  christos 		(void)printf(" %8.2f\n",
    391  1.29       mrg 		    cur.dk_wbytes[dn] / (1024.0 * 1024) / etime);
    392  1.15     lukem 	}
    393  1.15     lukem }
    394  1.15     lukem 
    395  1.15     lukem static void
    396  1.23    simonb cpustats(void)
    397   1.1       cgd {
    398  1.12     lukem 	int state;
    399   1.1       cgd 	double time;
    400   1.1       cgd 
    401   1.1       cgd 	time = 0;
    402   1.1       cgd 	for (state = 0; state < CPUSTATES; ++state)
    403   1.1       cgd 		time += cur.cp_time[state];
    404   1.9   thorpej 	if (!time)
    405   1.9   thorpej 		time = 1.0;
    406  1.15     lukem 			/* States are generally never 100% and can use %3.0f. */
    407   1.1       cgd 	for (state = 0; state < CPUSTATES; ++state)
    408  1.21    simonb 		printf(" %2.0f", 100. * cur.cp_time[state] / time);
    409   1.1       cgd }
    410   1.1       cgd 
    411   1.9   thorpej static void
    412  1.23    simonb usage(void)
    413   1.1       cgd {
    414  1.13       mrg 
    415  1.31     enami 	(void)fprintf(stderr, "usage: iostat [-CdDITx] [-c count] [-M core] "
    416  1.31     enami 	    "[-N system] [-w wait] [drives]\n");
    417   1.1       cgd 	exit(1);
    418   1.9   thorpej }
    419   1.9   thorpej 
    420   1.9   thorpej static void
    421  1.23    simonb display(void)
    422   1.9   thorpej {
    423   1.9   thorpej 	double	etime;
    424   1.9   thorpej 
    425   1.9   thorpej 	/* Sum up the elapsed ticks. */
    426  1.27  sommerfe 	etime = cur.cp_etime;
    427   1.9   thorpej 
    428  1.34     enami 	/*
    429  1.34     enami 	 * If we're showing totals only, then don't divide by the
    430   1.9   thorpej 	 * system time.
    431   1.9   thorpej 	 */
    432   1.9   thorpej 	if (ISSET(todo, SHOW_TOTALS))
    433   1.9   thorpej 		etime = 1.0;
    434   1.9   thorpej 
    435  1.34     enami 	if (ISSET(todo, SHOW_STATS_X)) {
    436  1.34     enami 		disk_statsx(etime);
    437  1.34     enami 		goto out;
    438  1.34     enami 	}
    439  1.34     enami 
    440   1.9   thorpej 	if (ISSET(todo, SHOW_TTY))
    441   1.9   thorpej 		printf("%4.0f %4.0f", cur.tk_nin / etime, cur.tk_nout / etime);
    442  1.27  sommerfe 
    443   1.9   thorpej 	if (ISSET(todo, SHOW_STATS_1))
    444   1.9   thorpej 		disk_stats(etime);
    445   1.9   thorpej 
    446   1.9   thorpej 	if (ISSET(todo, SHOW_STATS_2))
    447   1.9   thorpej 		disk_stats2(etime);
    448  1.15     lukem 
    449   1.9   thorpej 	if (ISSET(todo, SHOW_CPU))
    450   1.9   thorpej 		cpustats();
    451   1.9   thorpej 
    452  1.34     enami 	(void)printf("\n");
    453  1.34     enami 
    454  1.34     enami out:
    455   1.9   thorpej 	(void)fflush(stdout);
    456   1.9   thorpej }
    457   1.9   thorpej 
    458  1.34     enami static int
    459  1.23    simonb selectdrives(int argc, char *argv[])
    460   1.9   thorpej {
    461  1.34     enami 	int	i, maxdrives, ndrives, tried;
    462   1.9   thorpej 
    463   1.9   thorpej 	/*
    464   1.9   thorpej 	 * Choose drives to be displayed.  Priority goes to (in order) drives
    465   1.9   thorpej 	 * supplied as arguments and default drives.  If everything isn't
    466   1.9   thorpej 	 * filled in and there are drives not taken care of, display the first
    467   1.9   thorpej 	 * few that fit.
    468   1.9   thorpej 	 *
    469   1.9   thorpej 	 * The backward compatibility #ifdefs permit the syntax:
    470   1.9   thorpej 	 *	iostat [ drives ] [ interval [ count ] ]
    471   1.9   thorpej 	 */
    472  1.19      tron 
    473   1.9   thorpej #define	BACKWARD_COMPATIBILITY
    474  1.34     enami 	for (tried = ndrives = 0; *argv; ++argv) {
    475  1.34     enami #ifdef BACKWARD_COMPATIBILITY
    476   1.9   thorpej 		if (isdigit(**argv))
    477   1.9   thorpej 			break;
    478   1.9   thorpej #endif
    479  1.34     enami 		tried++;
    480  1.33  christos 		for (i = 0; i < dk_ndrive; i++) {
    481  1.34     enami 			if (strcmp(cur.dk_name[i], *argv))
    482  1.33  christos 				continue;
    483  1.33  christos 			cur.dk_select[i] = 1;
    484  1.33  christos 			++ndrives;
    485  1.33  christos 		}
    486   1.9   thorpej 	}
    487  1.34     enami 
    488  1.34     enami 	if (ndrives == 0 && tried == 0) {
    489  1.34     enami 		/*
    490  1.34     enami 		 * Pick up to 4 (or all if -x is given) drives
    491  1.34     enami 		 * if none specified.
    492  1.34     enami 		 */
    493  1.34     enami 		maxdrives = ISSET(todo, SHOW_STATS_X) ? dk_ndrive : 4;
    494  1.34     enami 		for (i = 0; i < maxdrives; i++) {
    495  1.34     enami 			cur.dk_select[i] = 1;
    496  1.34     enami 			++ndrives;
    497  1.34     enami 			if (!ISSET(todo, SHOW_STATS_X) && ndrives == 4)
    498  1.34     enami 				break;
    499  1.34     enami 		}
    500  1.34     enami 	}
    501  1.34     enami 
    502  1.34     enami #ifdef BACKWARD_COMPATIBILITY
    503   1.9   thorpej 	if (*argv) {
    504   1.9   thorpej 		interval = atoi(*argv);
    505   1.9   thorpej 		if (*++argv)
    506   1.9   thorpej 			reps = atoi(*argv);
    507   1.9   thorpej 	}
    508   1.9   thorpej #endif
    509   1.9   thorpej 
    510   1.9   thorpej 	if (interval) {
    511   1.9   thorpej 		if (!reps)
    512   1.9   thorpej 			reps = -1;
    513   1.9   thorpej 	} else
    514   1.9   thorpej 		if (reps)
    515   1.9   thorpej 			interval = 1;
    516   1.9   thorpej 
    517  1.34     enami 	return (ndrives);
    518   1.1       cgd }
    519