Home | History | Annotate | Line # | Download | only in systat
iostat.c revision 1.30
      1  1.30       wiz /*	$NetBSD: iostat.c,v 1.30 2004/02/13 11:36:24 wiz Exp $	*/
      2   1.2       jtc 
      3   1.1       jtc /*
      4   1.1       jtc  * Copyright (c) 1980, 1992, 1993
      5   1.1       jtc  *	The Regents of the University of California.  All rights reserved.
      6   1.1       jtc  *
      7   1.1       jtc  * Redistribution and use in source and binary forms, with or without
      8   1.1       jtc  * modification, are permitted provided that the following conditions
      9   1.1       jtc  * are met:
     10   1.1       jtc  * 1. Redistributions of source code must retain the above copyright
     11   1.1       jtc  *    notice, this list of conditions and the following disclaimer.
     12   1.1       jtc  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       jtc  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       jtc  *    documentation and/or other materials provided with the distribution.
     15  1.28       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       jtc  *    may be used to endorse or promote products derived from this software
     17   1.1       jtc  *    without specific prior written permission.
     18   1.1       jtc  *
     19   1.1       jtc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       jtc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       jtc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       jtc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       jtc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       jtc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       jtc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       jtc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       jtc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       jtc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       jtc  * SUCH DAMAGE.
     30   1.1       jtc  */
     31   1.1       jtc 
     32   1.8       mrg #include <sys/cdefs.h>
     33   1.1       jtc #ifndef lint
     34   1.2       jtc #if 0
     35   1.1       jtc static char sccsid[] = "@(#)iostat.c	8.1 (Berkeley) 6/6/93";
     36   1.2       jtc #endif
     37  1.30       wiz __RCSID("$NetBSD: iostat.c,v 1.30 2004/02/13 11:36:24 wiz Exp $");
     38  1.18       cgd #endif /* not lint */
     39   1.1       jtc 
     40   1.1       jtc #include <sys/param.h>
     41   1.1       jtc 
     42   1.1       jtc #include <string.h>
     43  1.17    simonb 
     44   1.1       jtc #include "systat.h"
     45   1.1       jtc #include "extern.h"
     46   1.5   thorpej #include "dkstats.h"
     47   1.1       jtc 
     48   1.1       jtc static  int linesperregion;
     49   1.1       jtc static  double etime;
     50   1.1       jtc static  int numbers = 0;		/* default display bar graphs */
     51   1.5   thorpej static  int secs = 0;			/* default seconds shown */
     52  1.23       mrg static  int read_write = 0;		/* default read/write shown */
     53   1.1       jtc 
     54  1.16        ad static int barlabels(int);
     55  1.16        ad static void histogram(double, int, double);
     56  1.16        ad static int numlabels(int);
     57  1.16        ad static int stats(int, int, int);
     58  1.16        ad static void stat1(int, int);
     59   1.1       jtc 
     60   1.1       jtc 
     61   1.1       jtc WINDOW *
     62  1.16        ad openiostat(void)
     63   1.1       jtc {
     64   1.8       mrg 
     65  1.27       dsl 	return (subwin(stdscr, -1, 0, 5, 0));
     66   1.1       jtc }
     67   1.1       jtc 
     68   1.1       jtc void
     69  1.16        ad closeiostat(WINDOW *w)
     70   1.1       jtc {
     71   1.8       mrg 
     72   1.1       jtc 	if (w == NULL)
     73   1.1       jtc 		return;
     74   1.1       jtc 	wclear(w);
     75   1.1       jtc 	wrefresh(w);
     76   1.1       jtc 	delwin(w);
     77   1.1       jtc }
     78   1.1       jtc 
     79   1.1       jtc int
     80  1.16        ad initiostat(void)
     81   1.1       jtc {
     82   1.8       mrg 
     83  1.19  augustss 	dkinit(1);
     84   1.5   thorpej 	dkreadstats();
     85   1.8       mrg 	return(1);
     86   1.1       jtc }
     87   1.1       jtc 
     88   1.1       jtc void
     89  1.16        ad fetchiostat(void)
     90   1.1       jtc {
     91   1.8       mrg 
     92   1.5   thorpej 	if (dk_ndrive == 0)
     93   1.1       jtc 		return;
     94   1.5   thorpej 	dkreadstats();
     95   1.1       jtc }
     96   1.1       jtc 
     97  1.12       mjl #define	INSET	14
     98   1.1       jtc 
     99   1.1       jtc void
    100  1.16        ad labeliostat(void)
    101   1.1       jtc {
    102   1.1       jtc 	int row;
    103   1.1       jtc 
    104   1.5   thorpej 	if (dk_ndrive == 0) {
    105   1.5   thorpej 		error("No drives defined.");
    106   1.1       jtc 		return;
    107   1.1       jtc 	}
    108   1.1       jtc 	row = 0;
    109   1.1       jtc 	wmove(wnd, row, 0); wclrtobot(wnd);
    110   1.1       jtc 	mvwaddstr(wnd, row++, INSET,
    111   1.1       jtc 	    "/0   /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
    112  1.30       wiz 	mvwaddstr(wnd, row++, 0, "    CPU  user|");
    113  1.12       mjl 	mvwaddstr(wnd, row++, 0, "         nice|");
    114  1.12       mjl 	mvwaddstr(wnd, row++, 0, "       system|");
    115  1.12       mjl 	mvwaddstr(wnd, row++, 0, "    interrupt|");
    116  1.12       mjl 	mvwaddstr(wnd, row++, 0, "         idle|");
    117   1.1       jtc 	if (numbers)
    118   1.1       jtc 		row = numlabels(row + 1);
    119   1.1       jtc 	else
    120   1.1       jtc 		row = barlabels(row + 1);
    121   1.1       jtc }
    122   1.1       jtc 
    123   1.1       jtc static int
    124  1.16        ad numlabels(int row)
    125   1.1       jtc {
    126   1.1       jtc 	int i, col, regions, ndrives;
    127   1.1       jtc 
    128  1.23       mrg #define COLWIDTH	(read_write ? 24 : 14)
    129  1.29       dsl #define DRIVESPERLINE	((getmaxx(wnd) + 1) / COLWIDTH)
    130   1.1       jtc 	for (ndrives = 0, i = 0; i < dk_ndrive; i++)
    131   1.5   thorpej 		if (cur.dk_select[i])
    132   1.1       jtc 			ndrives++;
    133   1.1       jtc 	regions = howmany(ndrives, DRIVESPERLINE);
    134   1.1       jtc 	/*
    135   1.1       jtc 	 * Deduct -regions for blank line after each scrolling region.
    136   1.1       jtc 	 */
    137  1.29       dsl 	linesperregion = (getmaxy(wnd) - row - regions + 1) / regions;
    138   1.1       jtc 	/*
    139   1.1       jtc 	 * Minimum region contains space for two
    140   1.1       jtc 	 * label lines and one line of statistics.
    141   1.1       jtc 	 */
    142   1.1       jtc 	if (linesperregion < 3)
    143   1.1       jtc 		linesperregion = 3;
    144   1.1       jtc 	col = 0;
    145   1.1       jtc 	for (i = 0; i < dk_ndrive; i++)
    146  1.23       mrg 		if (cur.dk_select[i]) {
    147  1.29       dsl 			if (col + COLWIDTH - 1 > getmaxx(wnd)) {
    148   1.1       jtc 				col = 0, row += linesperregion + 1;
    149  1.29       dsl 				if (row > getmaxy(wnd) - (linesperregion))
    150   1.1       jtc 					break;
    151   1.1       jtc 			}
    152  1.23       mrg 			mvwprintw(wnd, row, col + 4, "%s%s",
    153  1.23       mrg 			    cur.dk_name[i], read_write ? "       (write)" : "");
    154  1.23       mrg 			if (read_write)
    155  1.23       mrg 				mvwaddstr(wnd, row + 1, col,
    156  1.23       mrg 				    "kBps r/s  sec kBps w/s");
    157  1.23       mrg 			else
    158  1.23       mrg 				mvwaddstr(wnd, row + 1, col, "kBps tps  sec");
    159   1.1       jtc 			col += COLWIDTH;
    160   1.1       jtc 		}
    161   1.1       jtc 	if (col)
    162   1.1       jtc 		row += linesperregion + 1;
    163   1.1       jtc 	return (row);
    164   1.1       jtc }
    165   1.1       jtc 
    166   1.1       jtc static int
    167  1.16        ad barlabels(int row)
    168   1.1       jtc {
    169   1.1       jtc 	int i;
    170   1.1       jtc 
    171   1.1       jtc 	mvwaddstr(wnd, row++, INSET,
    172   1.5   thorpej 	    "/0   /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
    173  1.23       mrg 	linesperregion = 2 + secs + (read_write ? 2 : 0);
    174   1.1       jtc 	for (i = 0; i < dk_ndrive; i++)
    175  1.23       mrg 		if (cur.dk_select[i]) {
    176   1.7       jtc 			if (row > getmaxy(wnd) - linesperregion)
    177   1.1       jtc 				break;
    178  1.24   thorpej 			mvwprintw(wnd, row++, 0, "%7.7s  kBps|",
    179  1.24   thorpej 			    cur.dk_name[i]);
    180  1.12       mjl 			mvwaddstr(wnd, row++, 0, "          tps|");
    181  1.23       mrg 			if (read_write) {
    182  1.23       mrg 				mvwprintw(wnd, row++, 0, " (write) kBps|");
    183  1.23       mrg 				mvwaddstr(wnd, row++, 0, "          tps|");
    184  1.23       mrg 			}
    185   1.5   thorpej 			if (secs)
    186  1.12       mjl 				mvwaddstr(wnd, row++, 0, "         msec|");
    187   1.1       jtc 		}
    188   1.1       jtc 	return (row);
    189   1.1       jtc }
    190   1.1       jtc 
    191   1.1       jtc void
    192  1.16        ad showiostat(void)
    193   1.1       jtc {
    194   1.9     lukem 	int i, row, col;
    195   1.1       jtc 
    196   1.5   thorpej 	if (dk_ndrive == 0)
    197   1.1       jtc 		return;
    198   1.5   thorpej 	dkswap();
    199   1.5   thorpej 
    200  1.21  sommerfe 	etime = cur.cp_etime;
    201   1.1       jtc 	row = 1;
    202   1.1       jtc 
    203   1.1       jtc 	/*
    204   1.3   mycroft 	 * Interrupt CPU state not calculated yet.
    205  1.21  sommerfe 	 */
    206   1.3   mycroft 	for (i = 0; i < CPUSTATES; i++)
    207   1.1       jtc 		stat1(row++, i);
    208   1.1       jtc 	if (!numbers) {
    209   1.1       jtc 		row += 2;
    210   1.1       jtc 		for (i = 0; i < dk_ndrive; i++)
    211  1.23       mrg 			if (cur.dk_select[i]) {
    212   1.7       jtc 				if (row > getmaxy(wnd) - linesperregion)
    213   1.1       jtc 					break;
    214   1.1       jtc 				row = stats(row, INSET, i);
    215   1.1       jtc 			}
    216   1.1       jtc 		return;
    217   1.1       jtc 	}
    218   1.1       jtc 	col = 0;
    219   1.1       jtc 	wmove(wnd, row + linesperregion, 0);
    220   1.1       jtc 	wdeleteln(wnd);
    221   1.1       jtc 	wmove(wnd, row + 3, 0);
    222   1.1       jtc 	winsertln(wnd);
    223   1.1       jtc 	for (i = 0; i < dk_ndrive; i++)
    224  1.23       mrg 		if (cur.dk_select[i]) {
    225  1.29       dsl 			if (col + COLWIDTH - 1 > getmaxx(wnd)) {
    226   1.1       jtc 				col = 0, row += linesperregion + 1;
    227   1.7       jtc 				if (row > getmaxy(wnd) - (linesperregion + 1))
    228   1.1       jtc 					break;
    229   1.1       jtc 				wmove(wnd, row + linesperregion, 0);
    230   1.1       jtc 				wdeleteln(wnd);
    231   1.1       jtc 				wmove(wnd, row + 3, 0);
    232   1.1       jtc 				winsertln(wnd);
    233   1.1       jtc 			}
    234   1.1       jtc 			(void) stats(row + 3, col, i);
    235   1.1       jtc 			col += COLWIDTH;
    236   1.1       jtc 		}
    237   1.1       jtc }
    238   1.1       jtc 
    239   1.1       jtc static int
    240  1.16        ad stats(int row, int col, int dn)
    241   1.1       jtc {
    242  1.23       mrg 	double atime, rwords, wwords;
    243   1.5   thorpej 
    244   1.5   thorpej 	/* time busy in disk activity */
    245   1.5   thorpej 	atime = (double)cur.dk_time[dn].tv_sec +
    246   1.5   thorpej 		((double)cur.dk_time[dn].tv_usec / (double)1000000);
    247   1.1       jtc 
    248  1.23       mrg 	/* # of k transferred */
    249  1.23       mrg 	rwords = cur.dk_rbytes[dn] / 1024.0;
    250  1.23       mrg 	wwords = cur.dk_wbytes[dn] / 1024.0;
    251   1.1       jtc 	if (numbers) {
    252  1.23       mrg 		if (read_write)
    253  1.25  kristerw 			mvwprintw(wnd, row, col, "%4.0f%4.0f%5.1f %3.0f%4.0f",
    254  1.23       mrg 			    rwords / etime, cur.dk_rxfer[dn] / etime,
    255  1.23       mrg 			    atime / etime,
    256  1.23       mrg 			    wwords / etime, cur.dk_wxfer[dn] / etime);
    257  1.23       mrg 		else
    258  1.25  kristerw 			mvwprintw(wnd, row, col, "%4.0f%4.0f%5.1f",
    259  1.23       mrg 			    (rwords + wwords) / etime,
    260  1.23       mrg 			    (cur.dk_rxfer[dn] + cur.dk_wxfer[dn]) / etime,
    261  1.23       mrg 			    atime / etime);
    262   1.1       jtc 		return (row);
    263   1.1       jtc 	}
    264  1.23       mrg 
    265  1.23       mrg 	if (read_write) {
    266  1.23       mrg 		wmove(wnd, row++, col);
    267  1.23       mrg 		histogram(rwords / etime, 50, 0.5);
    268  1.23       mrg 		wmove(wnd, row++, col);
    269  1.23       mrg 		histogram(cur.dk_rxfer[dn] / etime, 50, 0.5);
    270  1.23       mrg 		wmove(wnd, row++, col);
    271  1.23       mrg 		histogram(wwords / etime, 50, 0.5);
    272  1.23       mrg 		wmove(wnd, row++, col);
    273  1.23       mrg 		histogram(cur.dk_wxfer[dn] / etime, 50, 0.5);
    274  1.23       mrg 	} else {
    275  1.23       mrg 		wmove(wnd, row++, col);
    276  1.23       mrg 		histogram((rwords + wwords) / etime, 50, 0.5);
    277  1.23       mrg 		wmove(wnd, row++, col);
    278  1.23       mrg 		histogram((cur.dk_rxfer[dn] + cur.dk_wxfer[dn]) / etime, 50, 0.5);
    279  1.23       mrg 	}
    280  1.23       mrg 
    281   1.5   thorpej 	if (secs) {
    282   1.1       jtc 		wmove(wnd, row++, col);
    283   1.5   thorpej 		atime *= 1000;	/* In milliseconds */
    284   1.5   thorpej 		histogram(atime / etime, 50, 0.5);
    285   1.1       jtc 	}
    286   1.1       jtc 	return (row);
    287   1.1       jtc }
    288   1.1       jtc 
    289   1.1       jtc static void
    290  1.16        ad stat1(int row, int o)
    291   1.1       jtc {
    292   1.9     lukem 	int i;
    293   1.1       jtc 	double time;
    294   1.1       jtc 
    295   1.1       jtc 	time = 0;
    296   1.1       jtc 	for (i = 0; i < CPUSTATES; i++)
    297   1.5   thorpej 		time += cur.cp_time[i];
    298   1.1       jtc 	if (time == 0.0)
    299   1.1       jtc 		time = 1.0;
    300   1.1       jtc 	wmove(wnd, row, INSET);
    301   1.1       jtc #define CPUSCALE	0.5
    302   1.5   thorpej 	histogram(100.0 * cur.cp_time[o] / time, 50, CPUSCALE);
    303   1.1       jtc }
    304   1.1       jtc 
    305   1.1       jtc static void
    306  1.16        ad histogram(double val, int colwidth, double scale)
    307   1.1       jtc {
    308  1.26       dsl 	int v = (int)(val * scale + 0.5);
    309  1.26       dsl 	int factor = 1;
    310  1.26       dsl 	int y, x;
    311  1.26       dsl 
    312  1.26       dsl 	while (v > colwidth) {
    313  1.26       dsl 		v = (v + 5) / 10;
    314  1.26       dsl 		factor *= 10;
    315   1.1       jtc 	}
    316  1.26       dsl 	getyx(wnd, y, x);
    317   1.1       jtc 	wclrtoeol(wnd);
    318  1.26       dsl 	whline(wnd, 'X', v);
    319  1.26       dsl 	if (factor != 1)
    320  1.26       dsl 		mvwprintw(wnd, y, x + colwidth + 1, "* %d ", factor);
    321   1.1       jtc }
    322   1.1       jtc 
    323  1.13     jwise void
    324  1.16        ad iostat_bars(char *args)
    325  1.13     jwise {
    326  1.13     jwise 	numbers = 0;
    327  1.13     jwise 	wclear(wnd);
    328  1.13     jwise 	labeliostat();
    329  1.13     jwise 	refresh();
    330  1.13     jwise }
    331  1.13     jwise 
    332  1.13     jwise void
    333  1.16        ad iostat_numbers(char *args)
    334   1.1       jtc {
    335  1.13     jwise 	numbers = 1;
    336  1.13     jwise 	wclear(wnd);
    337  1.13     jwise 	labeliostat();
    338  1.13     jwise 	refresh();
    339  1.13     jwise }
    340   1.1       jtc 
    341  1.13     jwise void
    342  1.16        ad iostat_secs(char *args)
    343  1.13     jwise {
    344  1.13     jwise 	secs = !secs;
    345  1.23       mrg 	wclear(wnd);
    346  1.23       mrg 	labeliostat();
    347  1.23       mrg 	refresh();
    348  1.23       mrg }
    349  1.23       mrg 
    350  1.23       mrg void
    351  1.23       mrg iostat_rw(char *args)
    352  1.23       mrg {
    353  1.23       mrg 	read_write = 1;
    354  1.23       mrg 	wclear(wnd);
    355  1.23       mrg 	labeliostat();
    356  1.23       mrg 	refresh();
    357  1.23       mrg }
    358  1.23       mrg 
    359  1.23       mrg void
    360  1.23       mrg iostat_all(char *args)
    361  1.23       mrg {
    362  1.23       mrg 	read_write = 0;
    363   1.1       jtc 	wclear(wnd);
    364   1.1       jtc 	labeliostat();
    365   1.1       jtc 	refresh();
    366   1.1       jtc }
    367