Home | History | Annotate | Line # | Download | only in systat
iostat.c revision 1.10
      1  1.10      mrg /*	$NetBSD: iostat.c,v 1.10 1998/07/12 05:59:00 mrg 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.1      jtc  * 3. All advertising materials mentioning features or use of this software
     16   1.1      jtc  *    must display the following acknowledgement:
     17   1.1      jtc  *	This product includes software developed by the University of
     18   1.1      jtc  *	California, Berkeley and its contributors.
     19   1.1      jtc  * 4. Neither the name of the University nor the names of its contributors
     20   1.1      jtc  *    may be used to endorse or promote products derived from this software
     21   1.1      jtc  *    without specific prior written permission.
     22   1.1      jtc  *
     23   1.1      jtc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1      jtc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1      jtc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1      jtc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1      jtc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1      jtc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1      jtc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1      jtc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1      jtc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1      jtc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1      jtc  * SUCH DAMAGE.
     34   1.1      jtc  */
     35   1.1      jtc 
     36   1.8      mrg #include <sys/cdefs.h>
     37   1.1      jtc #ifndef lint
     38   1.2      jtc #if 0
     39   1.1      jtc static char sccsid[] = "@(#)iostat.c	8.1 (Berkeley) 6/6/93";
     40   1.2      jtc #endif
     41  1.10      mrg __RCSID("$NetBSD: iostat.c,v 1.10 1998/07/12 05:59:00 mrg Exp $");
     42   1.1      jtc #endif not lint
     43   1.1      jtc 
     44   1.1      jtc #include <sys/param.h>
     45   1.1      jtc #include <sys/dkstat.h>
     46   1.1      jtc #include <sys/buf.h>
     47   1.5  thorpej #include <sys/time.h>
     48   1.1      jtc 
     49   1.1      jtc #include <string.h>
     50   1.1      jtc #include <stdlib.h>
     51   1.1      jtc #include <nlist.h>
     52   1.1      jtc #include <paths.h>
     53   1.1      jtc #include "systat.h"
     54   1.1      jtc #include "extern.h"
     55   1.1      jtc 
     56   1.5  thorpej #include "dkstats.h"
     57   1.5  thorpej extern struct _disk	cur;
     58   1.1      jtc 
     59   1.1      jtc static  int linesperregion;
     60   1.1      jtc static  double etime;
     61   1.1      jtc static  int numbers = 0;		/* default display bar graphs */
     62   1.5  thorpej static  int secs = 0;			/* default seconds shown */
     63   1.1      jtc 
     64   1.1      jtc static int barlabels __P((int));
     65   1.1      jtc static void histogram __P((double, int, double));
     66   1.1      jtc static int numlabels __P((int));
     67   1.1      jtc static int stats __P((int, int, int));
     68   1.1      jtc static void stat1 __P((int, int));
     69   1.1      jtc 
     70   1.1      jtc 
     71   1.1      jtc WINDOW *
     72   1.1      jtc openiostat()
     73   1.1      jtc {
     74   1.8      mrg 
     75   1.1      jtc 	return (subwin(stdscr, LINES-1-5, 0, 5, 0));
     76   1.1      jtc }
     77   1.1      jtc 
     78   1.1      jtc void
     79   1.1      jtc closeiostat(w)
     80   1.1      jtc 	WINDOW *w;
     81   1.1      jtc {
     82   1.8      mrg 
     83   1.1      jtc 	if (w == NULL)
     84   1.1      jtc 		return;
     85   1.1      jtc 	wclear(w);
     86   1.1      jtc 	wrefresh(w);
     87   1.1      jtc 	delwin(w);
     88   1.1      jtc }
     89   1.1      jtc 
     90   1.1      jtc int
     91   1.1      jtc initiostat()
     92   1.1      jtc {
     93   1.8      mrg 
     94   1.5  thorpej 	dkinit(1);
     95   1.5  thorpej 	dkreadstats();
     96   1.8      mrg 	return(1);
     97   1.1      jtc }
     98   1.1      jtc 
     99   1.1      jtc void
    100   1.1      jtc fetchiostat()
    101   1.1      jtc {
    102   1.8      mrg 
    103   1.5  thorpej 	if (dk_ndrive == 0)
    104   1.1      jtc 		return;
    105   1.5  thorpej 	dkreadstats();
    106   1.1      jtc }
    107   1.1      jtc 
    108   1.1      jtc #define	INSET	10
    109   1.1      jtc 
    110   1.1      jtc void
    111   1.1      jtc labeliostat()
    112   1.1      jtc {
    113   1.1      jtc 	int row;
    114   1.1      jtc 
    115   1.5  thorpej 	if (dk_ndrive == 0) {
    116   1.5  thorpej 		error("No drives defined.");
    117   1.1      jtc 		return;
    118   1.1      jtc 	}
    119   1.1      jtc 	row = 0;
    120   1.1      jtc 	wmove(wnd, row, 0); wclrtobot(wnd);
    121   1.1      jtc 	mvwaddstr(wnd, row++, INSET,
    122   1.1      jtc 	    "/0   /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
    123   1.1      jtc 	mvwaddstr(wnd, row++, 0, "cpu  user|");
    124   1.1      jtc 	mvwaddstr(wnd, row++, 0, "     nice|");
    125   1.1      jtc 	mvwaddstr(wnd, row++, 0, "   system|");
    126   1.3  mycroft 	mvwaddstr(wnd, row++, 0, "interrupt|");
    127   1.1      jtc 	mvwaddstr(wnd, row++, 0, "     idle|");
    128   1.1      jtc 	if (numbers)
    129   1.1      jtc 		row = numlabels(row + 1);
    130   1.1      jtc 	else
    131   1.1      jtc 		row = barlabels(row + 1);
    132   1.1      jtc }
    133   1.1      jtc 
    134   1.1      jtc static int
    135   1.1      jtc numlabels(row)
    136   1.1      jtc 	int row;
    137   1.1      jtc {
    138   1.1      jtc 	int i, col, regions, ndrives;
    139   1.1      jtc 
    140   1.1      jtc #define COLWIDTH	14
    141   1.7      jtc #define DRIVESPERLINE	((getmaxx(wnd) - INSET) / COLWIDTH)
    142   1.1      jtc 	for (ndrives = 0, i = 0; i < dk_ndrive; i++)
    143   1.5  thorpej 		if (cur.dk_select[i])
    144   1.1      jtc 			ndrives++;
    145   1.1      jtc 	regions = howmany(ndrives, DRIVESPERLINE);
    146   1.1      jtc 	/*
    147   1.1      jtc 	 * Deduct -regions for blank line after each scrolling region.
    148   1.1      jtc 	 */
    149   1.7      jtc 	linesperregion = (getmaxy(wnd) - row - regions) / regions;
    150   1.1      jtc 	/*
    151   1.1      jtc 	 * Minimum region contains space for two
    152   1.1      jtc 	 * label lines and one line of statistics.
    153   1.1      jtc 	 */
    154   1.1      jtc 	if (linesperregion < 3)
    155   1.1      jtc 		linesperregion = 3;
    156   1.1      jtc 	col = 0;
    157   1.1      jtc 	for (i = 0; i < dk_ndrive; i++)
    158   1.5  thorpej 		if (cur.dk_select[i] /*&& cur.dk_bytes[i] != 0.0*/) {
    159   1.7      jtc 			if (col + COLWIDTH >= getmaxx(wnd) - INSET) {
    160   1.1      jtc 				col = 0, row += linesperregion + 1;
    161   1.7      jtc 				if (row > getmaxy(wnd) - (linesperregion + 1))
    162   1.1      jtc 					break;
    163   1.1      jtc 			}
    164   1.5  thorpej 			mvwaddstr(wnd, row, col + 4, cur.dk_name[i]);
    165   1.6   scottr 			mvwaddstr(wnd, row + 1, col, "KBps tps  sec");
    166   1.1      jtc 			col += COLWIDTH;
    167   1.1      jtc 		}
    168   1.1      jtc 	if (col)
    169   1.1      jtc 		row += linesperregion + 1;
    170   1.1      jtc 	return (row);
    171   1.1      jtc }
    172   1.1      jtc 
    173   1.1      jtc static int
    174   1.1      jtc barlabels(row)
    175   1.1      jtc 	int row;
    176   1.1      jtc {
    177   1.1      jtc 	int i;
    178   1.1      jtc 
    179   1.1      jtc 	mvwaddstr(wnd, row++, INSET,
    180   1.5  thorpej 	    "/0   /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
    181   1.5  thorpej 	linesperregion = 2 + secs;
    182   1.1      jtc 	for (i = 0; i < dk_ndrive; i++)
    183   1.5  thorpej 		if (cur.dk_select[i] /*&& cur.dk_bytes[i] != 0.0*/) {
    184   1.7      jtc 			if (row > getmaxy(wnd) - linesperregion)
    185   1.1      jtc 				break;
    186   1.6   scottr 			mvwprintw(wnd, row++, 0, "%3.3s  KBps|", cur.dk_name[i]);
    187   1.1      jtc 			mvwaddstr(wnd, row++, 0, "      tps|");
    188   1.5  thorpej 			if (secs)
    189   1.5  thorpej 				mvwaddstr(wnd, row++, 0, "     msec|");
    190   1.1      jtc 		}
    191   1.1      jtc 	return (row);
    192   1.1      jtc }
    193   1.1      jtc 
    194   1.1      jtc 
    195   1.1      jtc void
    196   1.1      jtc showiostat()
    197   1.1      jtc {
    198   1.9    lukem 	int i, row, col;
    199   1.1      jtc 
    200   1.5  thorpej 	if (dk_ndrive == 0)
    201   1.1      jtc 		return;
    202   1.5  thorpej 	dkswap();
    203   1.5  thorpej 
    204   1.1      jtc 	etime = 0;
    205   1.1      jtc 	for(i = 0; i < CPUSTATES; i++) {
    206   1.5  thorpej 		etime += cur.cp_time[i];
    207   1.1      jtc 	}
    208   1.1      jtc 	if (etime == 0.0)
    209   1.1      jtc 		etime = 1.0;
    210   1.1      jtc 	etime /= (float) hz;
    211   1.1      jtc 	row = 1;
    212   1.1      jtc 
    213   1.1      jtc 	/*
    214   1.3  mycroft 	 * Interrupt CPU state not calculated yet.
    215   1.1      jtc 	 */
    216   1.3  mycroft 	for (i = 0; i < CPUSTATES; i++)
    217   1.1      jtc 		stat1(row++, i);
    218   1.1      jtc 	if (!numbers) {
    219   1.1      jtc 		row += 2;
    220   1.1      jtc 		for (i = 0; i < dk_ndrive; i++)
    221   1.5  thorpej 			if (cur.dk_select[i] /*&& cur.dk_bytes[i] != 0.0*/) {
    222   1.7      jtc 				if (row > getmaxy(wnd) - linesperregion)
    223   1.1      jtc 					break;
    224   1.1      jtc 				row = stats(row, INSET, i);
    225   1.1      jtc 			}
    226   1.1      jtc 		return;
    227   1.1      jtc 	}
    228   1.1      jtc 	col = 0;
    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 	for (i = 0; i < dk_ndrive; i++)
    234   1.5  thorpej 		if (cur.dk_select[i] /*&& cur.dk_bytes[i] != 0.0*/) {
    235   1.7      jtc 			if (col + COLWIDTH >= getmaxx(wnd)) {
    236   1.1      jtc 				col = 0, row += linesperregion + 1;
    237   1.7      jtc 				if (row > getmaxy(wnd) - (linesperregion + 1))
    238   1.1      jtc 					break;
    239   1.1      jtc 				wmove(wnd, row + linesperregion, 0);
    240   1.1      jtc 				wdeleteln(wnd);
    241   1.1      jtc 				wmove(wnd, row + 3, 0);
    242   1.1      jtc 				winsertln(wnd);
    243   1.1      jtc 			}
    244   1.1      jtc 			(void) stats(row + 3, col, i);
    245   1.1      jtc 			col += COLWIDTH;
    246   1.1      jtc 		}
    247   1.1      jtc }
    248   1.1      jtc 
    249   1.1      jtc static int
    250   1.1      jtc stats(row, col, dn)
    251   1.1      jtc 	int row, col, dn;
    252   1.1      jtc {
    253   1.5  thorpej 	double atime, words;
    254   1.5  thorpej 
    255   1.5  thorpej 	/* time busy in disk activity */
    256   1.5  thorpej 	atime = (double)cur.dk_time[dn].tv_sec +
    257   1.5  thorpej 		((double)cur.dk_time[dn].tv_usec / (double)1000000);
    258   1.1      jtc 
    259   1.5  thorpej 	words = cur.dk_bytes[dn] / 1024.0;	/* # of K transferred */
    260   1.1      jtc 	if (numbers) {
    261   1.6   scottr 		mvwprintw(wnd, row, col, " %3.0f%4.0f%5.1f",
    262   1.5  thorpej 		    words / etime, cur.dk_xfer[dn] / etime, atime / etime);
    263   1.1      jtc 		return (row);
    264   1.1      jtc 	}
    265   1.1      jtc 	wmove(wnd, row++, col);
    266   1.5  thorpej 	histogram(words / etime, 50, 0.5);
    267   1.1      jtc 	wmove(wnd, row++, col);
    268   1.5  thorpej 	histogram(cur.dk_xfer[dn] / etime, 50, 0.5);
    269   1.5  thorpej 	if (secs) {
    270   1.1      jtc 		wmove(wnd, row++, col);
    271   1.5  thorpej 		atime *= 1000;	/* In milliseconds */
    272   1.5  thorpej 		histogram(atime / etime, 50, 0.5);
    273   1.1      jtc 	}
    274   1.1      jtc 	return (row);
    275   1.1      jtc }
    276   1.1      jtc 
    277   1.1      jtc static void
    278   1.1      jtc stat1(row, o)
    279   1.1      jtc 	int row, o;
    280   1.1      jtc {
    281   1.9    lukem 	int i;
    282   1.1      jtc 	double time;
    283   1.1      jtc 
    284   1.1      jtc 	time = 0;
    285   1.1      jtc 	for (i = 0; i < CPUSTATES; i++)
    286   1.5  thorpej 		time += cur.cp_time[i];
    287   1.1      jtc 	if (time == 0.0)
    288   1.1      jtc 		time = 1.0;
    289   1.1      jtc 	wmove(wnd, row, INSET);
    290   1.1      jtc #define CPUSCALE	0.5
    291   1.5  thorpej 	histogram(100.0 * cur.cp_time[o] / time, 50, CPUSCALE);
    292   1.1      jtc }
    293   1.1      jtc 
    294   1.1      jtc static void
    295   1.1      jtc histogram(val, colwidth, scale)
    296   1.1      jtc 	double val;
    297   1.1      jtc 	int colwidth;
    298   1.1      jtc 	double scale;
    299   1.1      jtc {
    300   1.1      jtc 	char buf[10];
    301   1.9    lukem 	int k;
    302   1.9    lukem 	int v = (int)(val * scale) + 0.5;
    303   1.1      jtc 
    304   1.1      jtc 	k = MIN(v, colwidth);
    305   1.1      jtc 	if (v > colwidth) {
    306  1.10      mrg 		snprintf(buf, sizeof buf, "%4.1f", val);
    307   1.1      jtc 		k -= strlen(buf);
    308   1.1      jtc 		while (k--)
    309   1.1      jtc 			waddch(wnd, 'X');
    310   1.1      jtc 		waddstr(wnd, buf);
    311   1.5  thorpej 		wclrtoeol(wnd);
    312   1.1      jtc 		return;
    313   1.1      jtc 	}
    314   1.1      jtc 	while (k--)
    315   1.1      jtc 		waddch(wnd, 'X');
    316   1.1      jtc 	wclrtoeol(wnd);
    317   1.1      jtc }
    318   1.1      jtc 
    319   1.1      jtc int
    320   1.1      jtc cmdiostat(cmd, args)
    321   1.1      jtc 	char *cmd, *args;
    322   1.1      jtc {
    323   1.1      jtc 
    324   1.5  thorpej 	if (prefix(cmd, "secs"))
    325   1.5  thorpej 		secs = !secs;
    326   1.1      jtc 	else if (prefix(cmd, "numbers"))
    327   1.1      jtc 		numbers = 1;
    328   1.1      jtc 	else if (prefix(cmd, "bars"))
    329   1.1      jtc 		numbers = 0;
    330   1.1      jtc 	else if (!dkcmd(cmd, args))
    331   1.1      jtc 		return (0);
    332   1.1      jtc 	wclear(wnd);
    333   1.1      jtc 	labeliostat();
    334   1.1      jtc 	refresh();
    335   1.1      jtc 	return (1);
    336   1.1      jtc }
    337