Home | History | Annotate | Line # | Download | only in systat
iostat.c revision 1.16
      1  1.16        ad /*	$NetBSD: iostat.c,v 1.16 2000/07/05 11:03:21 ad 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.16        ad __RCSID("$NetBSD: iostat.c,v 1.16 2000/07/05 11:03:21 ad Exp $");
     42   1.1       jtc #endif not lint
     43   1.1       jtc 
     44   1.1       jtc #include <sys/param.h>
     45  1.14   thorpej #include <sys/sched.h>
     46   1.1       jtc #include <sys/dkstat.h>
     47   1.1       jtc #include <sys/buf.h>
     48   1.5   thorpej #include <sys/time.h>
     49   1.1       jtc 
     50   1.1       jtc #include <string.h>
     51   1.1       jtc #include <stdlib.h>
     52   1.1       jtc #include <nlist.h>
     53   1.1       jtc #include <paths.h>
     54   1.1       jtc #include "systat.h"
     55   1.1       jtc #include "extern.h"
     56   1.1       jtc 
     57   1.5   thorpej #include "dkstats.h"
     58   1.5   thorpej extern struct _disk	cur;
     59   1.1       jtc 
     60   1.1       jtc static  int linesperregion;
     61   1.1       jtc static  double etime;
     62   1.1       jtc static  int numbers = 0;		/* default display bar graphs */
     63   1.5   thorpej static  int secs = 0;			/* default seconds shown */
     64   1.1       jtc 
     65  1.16        ad static int barlabels(int);
     66  1.16        ad static void histogram(double, int, double);
     67  1.16        ad static int numlabels(int);
     68  1.16        ad static int stats(int, int, int);
     69  1.16        ad static void stat1(int, int);
     70   1.1       jtc 
     71   1.1       jtc 
     72   1.1       jtc WINDOW *
     73  1.16        ad openiostat(void)
     74   1.1       jtc {
     75   1.8       mrg 
     76   1.1       jtc 	return (subwin(stdscr, LINES-1-5, 0, 5, 0));
     77   1.1       jtc }
     78   1.1       jtc 
     79   1.1       jtc void
     80  1.16        ad closeiostat(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.16        ad initiostat(void)
     92   1.1       jtc {
     93  1.11  drochner 	extern gid_t egid;
     94   1.8       mrg 
     95  1.11  drochner 	dkinit(1, egid);
     96   1.5   thorpej 	dkreadstats();
     97   1.8       mrg 	return(1);
     98   1.1       jtc }
     99   1.1       jtc 
    100   1.1       jtc void
    101  1.16        ad fetchiostat(void)
    102   1.1       jtc {
    103   1.8       mrg 
    104   1.5   thorpej 	if (dk_ndrive == 0)
    105   1.1       jtc 		return;
    106   1.5   thorpej 	dkreadstats();
    107   1.1       jtc }
    108   1.1       jtc 
    109  1.12       mjl #define	INSET	14
    110   1.1       jtc 
    111   1.1       jtc void
    112  1.16        ad labeliostat(void)
    113   1.1       jtc {
    114   1.1       jtc 	int row;
    115   1.1       jtc 
    116   1.5   thorpej 	if (dk_ndrive == 0) {
    117   1.5   thorpej 		error("No drives defined.");
    118   1.1       jtc 		return;
    119   1.1       jtc 	}
    120   1.1       jtc 	row = 0;
    121   1.1       jtc 	wmove(wnd, row, 0); wclrtobot(wnd);
    122   1.1       jtc 	mvwaddstr(wnd, row++, INSET,
    123   1.1       jtc 	    "/0   /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
    124  1.12       mjl 	mvwaddstr(wnd, row++, 0, "    cpu  user|");
    125  1.12       mjl 	mvwaddstr(wnd, row++, 0, "         nice|");
    126  1.12       mjl 	mvwaddstr(wnd, row++, 0, "       system|");
    127  1.12       mjl 	mvwaddstr(wnd, row++, 0, "    interrupt|");
    128  1.12       mjl 	mvwaddstr(wnd, row++, 0, "         idle|");
    129   1.1       jtc 	if (numbers)
    130   1.1       jtc 		row = numlabels(row + 1);
    131   1.1       jtc 	else
    132   1.1       jtc 		row = barlabels(row + 1);
    133   1.1       jtc }
    134   1.1       jtc 
    135   1.1       jtc static int
    136  1.16        ad numlabels(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.16        ad barlabels(int row)
    175   1.1       jtc {
    176   1.1       jtc 	int i;
    177   1.1       jtc 
    178   1.1       jtc 	mvwaddstr(wnd, row++, INSET,
    179   1.5   thorpej 	    "/0   /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
    180   1.5   thorpej 	linesperregion = 2 + secs;
    181   1.1       jtc 	for (i = 0; i < dk_ndrive; i++)
    182   1.5   thorpej 		if (cur.dk_select[i] /*&& cur.dk_bytes[i] != 0.0*/) {
    183   1.7       jtc 			if (row > getmaxy(wnd) - linesperregion)
    184   1.1       jtc 				break;
    185  1.12       mjl 			mvwprintw(wnd, row++, 0, "%7.7s  KBps|", cur.dk_name[i]);
    186  1.12       mjl 			mvwaddstr(wnd, row++, 0, "          tps|");
    187   1.5   thorpej 			if (secs)
    188  1.12       mjl 				mvwaddstr(wnd, row++, 0, "         msec|");
    189   1.1       jtc 		}
    190   1.1       jtc 	return (row);
    191   1.1       jtc }
    192   1.1       jtc 
    193   1.1       jtc void
    194  1.16        ad showiostat(void)
    195   1.1       jtc {
    196   1.9     lukem 	int i, row, col;
    197   1.1       jtc 
    198   1.5   thorpej 	if (dk_ndrive == 0)
    199   1.1       jtc 		return;
    200   1.5   thorpej 	dkswap();
    201   1.5   thorpej 
    202   1.1       jtc 	etime = 0;
    203   1.1       jtc 	for(i = 0; i < CPUSTATES; i++) {
    204   1.5   thorpej 		etime += cur.cp_time[i];
    205   1.1       jtc 	}
    206   1.1       jtc 	if (etime == 0.0)
    207   1.1       jtc 		etime = 1.0;
    208   1.1       jtc 	etime /= (float) hz;
    209   1.1       jtc 	row = 1;
    210   1.1       jtc 
    211   1.1       jtc 	/*
    212   1.3   mycroft 	 * Interrupt CPU state not calculated yet.
    213   1.1       jtc 	 */
    214   1.3   mycroft 	for (i = 0; i < CPUSTATES; i++)
    215   1.1       jtc 		stat1(row++, i);
    216   1.1       jtc 	if (!numbers) {
    217   1.1       jtc 		row += 2;
    218   1.1       jtc 		for (i = 0; i < dk_ndrive; i++)
    219   1.5   thorpej 			if (cur.dk_select[i] /*&& cur.dk_bytes[i] != 0.0*/) {
    220   1.7       jtc 				if (row > getmaxy(wnd) - linesperregion)
    221   1.1       jtc 					break;
    222   1.1       jtc 				row = stats(row, INSET, i);
    223   1.1       jtc 			}
    224   1.1       jtc 		return;
    225   1.1       jtc 	}
    226   1.1       jtc 	col = 0;
    227   1.1       jtc 	wmove(wnd, row + linesperregion, 0);
    228   1.1       jtc 	wdeleteln(wnd);
    229   1.1       jtc 	wmove(wnd, row + 3, 0);
    230   1.1       jtc 	winsertln(wnd);
    231   1.1       jtc 	for (i = 0; i < dk_ndrive; i++)
    232   1.5   thorpej 		if (cur.dk_select[i] /*&& cur.dk_bytes[i] != 0.0*/) {
    233   1.7       jtc 			if (col + COLWIDTH >= getmaxx(wnd)) {
    234   1.1       jtc 				col = 0, row += linesperregion + 1;
    235   1.7       jtc 				if (row > getmaxy(wnd) - (linesperregion + 1))
    236   1.1       jtc 					break;
    237   1.1       jtc 				wmove(wnd, row + linesperregion, 0);
    238   1.1       jtc 				wdeleteln(wnd);
    239   1.1       jtc 				wmove(wnd, row + 3, 0);
    240   1.1       jtc 				winsertln(wnd);
    241   1.1       jtc 			}
    242   1.1       jtc 			(void) stats(row + 3, col, i);
    243   1.1       jtc 			col += COLWIDTH;
    244   1.1       jtc 		}
    245   1.1       jtc }
    246   1.1       jtc 
    247   1.1       jtc static int
    248  1.16        ad stats(int row, int col, int dn)
    249   1.1       jtc {
    250   1.5   thorpej 	double atime, words;
    251   1.5   thorpej 
    252   1.5   thorpej 	/* time busy in disk activity */
    253   1.5   thorpej 	atime = (double)cur.dk_time[dn].tv_sec +
    254   1.5   thorpej 		((double)cur.dk_time[dn].tv_usec / (double)1000000);
    255   1.1       jtc 
    256   1.5   thorpej 	words = cur.dk_bytes[dn] / 1024.0;	/* # of K transferred */
    257   1.1       jtc 	if (numbers) {
    258   1.6    scottr 		mvwprintw(wnd, row, col, " %3.0f%4.0f%5.1f",
    259   1.5   thorpej 		    words / etime, cur.dk_xfer[dn] / etime, atime / etime);
    260   1.1       jtc 		return (row);
    261   1.1       jtc 	}
    262   1.1       jtc 	wmove(wnd, row++, col);
    263   1.5   thorpej 	histogram(words / etime, 50, 0.5);
    264   1.1       jtc 	wmove(wnd, row++, col);
    265   1.5   thorpej 	histogram(cur.dk_xfer[dn] / etime, 50, 0.5);
    266   1.5   thorpej 	if (secs) {
    267   1.1       jtc 		wmove(wnd, row++, col);
    268   1.5   thorpej 		atime *= 1000;	/* In milliseconds */
    269   1.5   thorpej 		histogram(atime / etime, 50, 0.5);
    270   1.1       jtc 	}
    271   1.1       jtc 	return (row);
    272   1.1       jtc }
    273   1.1       jtc 
    274   1.1       jtc static void
    275  1.16        ad stat1(int row, int o)
    276   1.1       jtc {
    277   1.9     lukem 	int i;
    278   1.1       jtc 	double time;
    279   1.1       jtc 
    280   1.1       jtc 	time = 0;
    281   1.1       jtc 	for (i = 0; i < CPUSTATES; i++)
    282   1.5   thorpej 		time += cur.cp_time[i];
    283   1.1       jtc 	if (time == 0.0)
    284   1.1       jtc 		time = 1.0;
    285   1.1       jtc 	wmove(wnd, row, INSET);
    286   1.1       jtc #define CPUSCALE	0.5
    287   1.5   thorpej 	histogram(100.0 * cur.cp_time[o] / time, 50, CPUSCALE);
    288   1.1       jtc }
    289   1.1       jtc 
    290   1.1       jtc static void
    291  1.16        ad histogram(double val, int colwidth, double scale)
    292   1.1       jtc {
    293   1.1       jtc 	char buf[10];
    294   1.9     lukem 	int k;
    295   1.9     lukem 	int v = (int)(val * scale) + 0.5;
    296   1.1       jtc 
    297   1.1       jtc 	k = MIN(v, colwidth);
    298   1.1       jtc 	if (v > colwidth) {
    299  1.10       mrg 		snprintf(buf, sizeof buf, "%4.1f", val);
    300   1.1       jtc 		k -= strlen(buf);
    301   1.1       jtc 		while (k--)
    302   1.1       jtc 			waddch(wnd, 'X');
    303   1.1       jtc 		waddstr(wnd, buf);
    304   1.5   thorpej 		wclrtoeol(wnd);
    305   1.1       jtc 		return;
    306   1.1       jtc 	}
    307   1.1       jtc 	wclrtoeol(wnd);
    308  1.15   mycroft 	whline(wnd, 'X', k);
    309   1.1       jtc }
    310   1.1       jtc 
    311  1.13     jwise void
    312  1.16        ad iostat_bars(char *args)
    313  1.13     jwise {
    314  1.13     jwise 	numbers = 0;
    315  1.13     jwise 	wclear(wnd);
    316  1.13     jwise 	labeliostat();
    317  1.13     jwise 	refresh();
    318  1.13     jwise }
    319  1.13     jwise 
    320  1.13     jwise void
    321  1.16        ad iostat_numbers(char *args)
    322   1.1       jtc {
    323  1.13     jwise 	numbers = 1;
    324  1.13     jwise 	wclear(wnd);
    325  1.13     jwise 	labeliostat();
    326  1.13     jwise 	refresh();
    327  1.13     jwise }
    328   1.1       jtc 
    329  1.13     jwise void
    330  1.16        ad iostat_secs(char *args)
    331  1.13     jwise {
    332  1.13     jwise 	secs = !secs;
    333   1.1       jtc 	wclear(wnd);
    334   1.1       jtc 	labeliostat();
    335   1.1       jtc 	refresh();
    336   1.1       jtc }
    337