Home | History | Annotate | Line # | Download | only in systat
vmstat.c revision 1.2
      1 /*	$NetBSD: vmstat.c,v 1.2 1995/01/20 08:52:16 jtc Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1983, 1989, 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "@(#)vmstat.c	8.2 (Berkeley) 1/12/94";
     39 #endif
     40 static char rcsid[] = "$NetBSD: vmstat.c,v 1.2 1995/01/20 08:52:16 jtc Exp $";
     41 #endif /* not lint */
     42 
     43 /*
     44  * Cursed vmstat -- from Robert Elz.
     45  */
     46 
     47 #include <sys/param.h>
     48 #include <sys/dkstat.h>
     49 #include <sys/buf.h>
     50 #include <sys/stat.h>
     51 #include <sys/time.h>
     52 #include <sys/user.h>
     53 #include <sys/proc.h>
     54 #include <sys/namei.h>
     55 #include <sys/sysctl.h>
     56 #include <vm/vm.h>
     57 
     58 #include <signal.h>
     59 #include <nlist.h>
     60 #include <ctype.h>
     61 #include <utmp.h>
     62 #include <paths.h>
     63 #include <string.h>
     64 #include <stdlib.h>
     65 #include <unistd.h>
     66 #include "systat.h"
     67 #include "extern.h"
     68 
     69 static struct Info {
     70 	long	time[CPUSTATES];
     71 	struct	vmmeter Cnt;
     72 	struct	vmtotal Total;
     73 	long	*dk_time;
     74 	long	*dk_wds;
     75 	long	*dk_seek;
     76 	long	*dk_xfer;
     77 	int	dk_busy;
     78 	struct	nchstats nchstats;
     79 	long	nchcount;
     80 	long	*intrcnt;
     81 } s, s1, s2, z;
     82 
     83 #define	cnt s.Cnt
     84 #define oldcnt s1.Cnt
     85 #define	total s.Total
     86 #define	nchtotal s.nchstats
     87 #define	oldnchtotal s1.nchstats
     88 
     89 static	enum state { BOOT, TIME, RUN } state = TIME;
     90 
     91 static void allocinfo __P((struct Info *));
     92 static void copyinfo __P((struct Info *, struct Info *));
     93 static float cputime __P((int));
     94 static void dinfo __P((int, int));
     95 static void getinfo __P((struct Info *, enum state));
     96 static void putint __P((int, int, int, int));
     97 static void putfloat __P((double, int, int, int, int, int));
     98 static int ucount __P((void));
     99 
    100 static	int ut;
    101 static	char buf[26];
    102 static	time_t t;
    103 static	double etime;
    104 static	float hertz;
    105 static	int nintr;
    106 static	long *intrloc;
    107 static	char **intrname;
    108 static	int nextintsrow;
    109 
    110 struct	utmp utmp;
    111 
    112 
    113 WINDOW *
    114 openkre()
    115 {
    116 
    117 	ut = open(_PATH_UTMP, O_RDONLY);
    118 	if (ut < 0)
    119 		error("No utmp");
    120 	return (stdscr);
    121 }
    122 
    123 void
    124 closekre(w)
    125 	WINDOW *w;
    126 {
    127 
    128 	(void) close(ut);
    129 	if (w == NULL)
    130 		return;
    131 	wclear(w);
    132 	wrefresh(w);
    133 }
    134 
    135 
    136 static struct nlist namelist[] = {
    137 #define X_CPTIME	0
    138 	{ "_cp_time" },
    139 #define X_CNT		1
    140 	{ "_cnt" },
    141 #define X_TOTAL		2
    142 	{ "_total" },
    143 #define	X_DK_BUSY	3
    144 	{ "_dk_busy" },
    145 #define	X_DK_TIME	4
    146 	{ "_dk_time" },
    147 #define	X_DK_XFER	5
    148 	{ "_dk_xfer" },
    149 #define	X_DK_WDS	6
    150 	{ "_dk_wds" },
    151 #define	X_DK_SEEK	7
    152 	{ "_dk_seek" },
    153 #define	X_NCHSTATS	8
    154 	{ "_nchstats" },
    155 #define	X_INTRNAMES	9
    156 	{ "_intrnames" },
    157 #define	X_EINTRNAMES	10
    158 	{ "_eintrnames" },
    159 #define	X_INTRCNT	11
    160 	{ "_intrcnt" },
    161 #define	X_EINTRCNT	12
    162 	{ "_eintrcnt" },
    163 	{ "" },
    164 };
    165 
    166 /*
    167  * These constants define where the major pieces are laid out
    168  */
    169 #define STATROW		 0	/* uses 1 row and 68 cols */
    170 #define STATCOL		 2
    171 #define MEMROW		 2	/* uses 4 rows and 31 cols */
    172 #define MEMCOL		 0
    173 #define PAGEROW		 2	/* uses 4 rows and 26 cols */
    174 #define PAGECOL		36
    175 #define INTSROW		 2	/* uses all rows to bottom and 17 cols */
    176 #define INTSCOL		63
    177 #define PROCSROW	 7	/* uses 2 rows and 20 cols */
    178 #define PROCSCOL	 0
    179 #define GENSTATROW	 7	/* uses 2 rows and 30 cols */
    180 #define GENSTATCOL	20
    181 #define VMSTATROW	 7	/* uses 17 rows and 12 cols */
    182 #define VMSTATCOL	48
    183 #define GRAPHROW	10	/* uses 3 rows and 51 cols */
    184 #define GRAPHCOL	 0
    185 #define NAMEIROW	14	/* uses 3 rows and 38 cols */
    186 #define NAMEICOL	 0
    187 #define DISKROW		18	/* uses 5 rows and 50 cols (for 9 drives) */
    188 #define DISKCOL		 0
    189 
    190 #define	DRIVESPACE	 9	/* max # for space */
    191 
    192 #if DK_NDRIVE > DRIVESPACE
    193 #define	MAXDRIVES	DRIVESPACE	 /* max # to display */
    194 #else
    195 #define	MAXDRIVES	DK_NDRIVE	 /* max # to display */
    196 #endif
    197 
    198 int
    199 initkre()
    200 {
    201 	char *intrnamebuf, *cp;
    202 	int i;
    203 	static int once = 0;
    204 
    205 	if (namelist[0].n_type == 0) {
    206 		if (kvm_nlist(kd, namelist)) {
    207 			nlisterr(namelist);
    208 			return(0);
    209 		}
    210 		if (namelist[0].n_type == 0) {
    211 			error("No namelist");
    212 			return(0);
    213 		}
    214 	}
    215 	hertz = stathz ? stathz : hz;
    216 	if (! dkinit())
    217 		return(0);
    218 	if (dk_ndrive && !once) {
    219 #define	allocate(e, t) \
    220     s./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
    221     s1./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
    222     s2./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
    223     z./**/e = (t *)calloc(dk_ndrive, sizeof (t));
    224 		allocate(dk_time, long);
    225 		allocate(dk_wds, long);
    226 		allocate(dk_seek, long);
    227 		allocate(dk_xfer, long);
    228 		once = 1;
    229 #undef allocate
    230 	}
    231 	if (nintr == 0) {
    232 		nintr = (namelist[X_EINTRCNT].n_value -
    233 			namelist[X_INTRCNT].n_value) / sizeof (long);
    234 		intrloc = calloc(nintr, sizeof (long));
    235 		intrname = calloc(nintr, sizeof (long));
    236 		intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value -
    237 			namelist[X_INTRNAMES].n_value);
    238 		if (intrnamebuf == 0 || intrname == 0 || intrloc == 0) {
    239 			error("Out of memory\n");
    240 			if (intrnamebuf)
    241 				free(intrnamebuf);
    242 			if (intrname)
    243 				free(intrname);
    244 			if (intrloc)
    245 				free(intrloc);
    246 			nintr = 0;
    247 			return(0);
    248 		}
    249 		NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) -
    250 			NVAL(X_INTRNAMES));
    251 		for (cp = intrnamebuf, i = 0; i < nintr; i++) {
    252 			intrname[i] = cp;
    253 			cp += strlen(cp) + 1;
    254 		}
    255 		nextintsrow = INTSROW + 2;
    256 		allocinfo(&s);
    257 		allocinfo(&s1);
    258 		allocinfo(&s2);
    259 		allocinfo(&z);
    260 	}
    261 	getinfo(&s2, RUN);
    262 	copyinfo(&s2, &s1);
    263 	return(1);
    264 }
    265 
    266 void
    267 fetchkre()
    268 {
    269 	time_t now;
    270 
    271 	time(&now);
    272 	strcpy(buf, ctime(&now));
    273 	buf[16] = '\0';
    274 	getinfo(&s, state);
    275 }
    276 
    277 void
    278 labelkre()
    279 {
    280 	register int i, j;
    281 
    282 	clear();
    283 	mvprintw(STATROW, STATCOL + 4, "users    Load");
    284 	mvprintw(MEMROW, MEMCOL, "Mem:KB  REAL        VIRTUAL");
    285 	mvprintw(MEMROW + 1, MEMCOL, "      Tot Share    Tot  Share");
    286 	mvprintw(MEMROW + 2, MEMCOL, "Act");
    287 	mvprintw(MEMROW + 3, MEMCOL, "All");
    288 
    289 	mvprintw(MEMROW + 1, MEMCOL + 31, "Free");
    290 
    291 	mvprintw(PAGEROW, PAGECOL,     "        PAGING   SWAPPING ");
    292 	mvprintw(PAGEROW + 1, PAGECOL, "        in  out   in  out ");
    293 	mvprintw(PAGEROW + 2, PAGECOL, "count");
    294 	mvprintw(PAGEROW + 3, PAGECOL, "pages");
    295 
    296 	mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
    297 	mvprintw(INTSROW + 1, INTSCOL + 9, "total");
    298 
    299 	mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "cow");
    300 	mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "objlk");
    301 	mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "objht");
    302 	mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "zfod");
    303 	mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "nzfod");
    304 	mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "%%zfod");
    305 	mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "kern");
    306 	mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "wire");
    307 	mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "act");
    308 	mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "inact");
    309 	mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "free");
    310 	mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "daefr");
    311 	mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "prcfr");
    312 	mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "react");
    313 	mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "scan");
    314 	mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "hdrev");
    315 	if (LINES - 1 > VMSTATROW + 16)
    316 		mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "intrn");
    317 
    318 	mvprintw(GENSTATROW, GENSTATCOL, "  Csw  Trp  Sys  Int  Sof  Flt");
    319 
    320 	mvprintw(GRAPHROW, GRAPHCOL,
    321 		"    . %% Sys    . %% User    . %% Nice    . %% Idle");
    322 	mvprintw(PROCSROW, PROCSCOL, "Proc:r  p  d  s  w");
    323 	mvprintw(GRAPHROW + 1, GRAPHCOL,
    324 		"|    |    |    |    |    |    |    |    |    |    |");
    325 
    326 	mvprintw(NAMEIROW, NAMEICOL, "Namei         Sys-cache     Proc-cache");
    327 	mvprintw(NAMEIROW + 1, NAMEICOL,
    328 		"    Calls     hits    %%     hits     %%");
    329 	mvprintw(DISKROW, DISKCOL, "Discs");
    330 	mvprintw(DISKROW + 1, DISKCOL, "seeks");
    331 	mvprintw(DISKROW + 2, DISKCOL, "xfers");
    332 	mvprintw(DISKROW + 3, DISKCOL, " blks");
    333 	mvprintw(DISKROW + 4, DISKCOL, " msps");
    334 	j = 0;
    335 	for (i = 0; i < dk_ndrive && j < MAXDRIVES; i++)
    336 		if (dk_select[i]) {
    337 			mvprintw(DISKROW, DISKCOL + 5 + 5 * j,
    338 				"  %3.3s", dr_name[j]);
    339 			j++;
    340 		}
    341 	for (i = 0; i < nintr; i++) {
    342 		if (intrloc[i] == 0)
    343 			continue;
    344 		mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", intrname[i]);
    345 	}
    346 }
    347 
    348 #define X(fld)	{t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
    349 #define Y(fld)	{t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
    350 #define Z(fld)	{t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
    351 	if(state == TIME) s1.nchstats.fld = t;}
    352 #define PUTRATE(fld, l, c, w) \
    353 	Y(fld); \
    354 	putint((int)((float)s.fld/etime + 0.5), l, c, w)
    355 #define MAXFAIL 5
    356 
    357 static	char cpuchar[CPUSTATES] = { '=' , '>', '-', ' ' };
    358 static	char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_IDLE };
    359 
    360 void
    361 showkre()
    362 {
    363 	float f1, f2;
    364 	int psiz, inttotal;
    365 	int i, l, c;
    366 	static int failcnt = 0;
    367 
    368 	for (i = 0; i < dk_ndrive; i++) {
    369 		X(dk_xfer); X(dk_seek); X(dk_wds); X(dk_time);
    370 	}
    371 	etime = 0;
    372 	for(i = 0; i < CPUSTATES; i++) {
    373 		X(time);
    374 		etime += s.time[i];
    375 	}
    376 	if (etime < 5.0) {	/* < 5 ticks - ignore this trash */
    377 		if (failcnt++ >= MAXFAIL) {
    378 			clear();
    379 			mvprintw(2, 10, "The alternate system clock has died!");
    380 			mvprintw(3, 10, "Reverting to ``pigs'' display.");
    381 			move(CMDLINE, 0);
    382 			refresh();
    383 			failcnt = 0;
    384 			sleep(5);
    385 			command("pigs");
    386 		}
    387 		return;
    388 	}
    389 	failcnt = 0;
    390 	etime /= hertz;
    391 	inttotal = 0;
    392 	for (i = 0; i < nintr; i++) {
    393 		if (s.intrcnt[i] == 0)
    394 			continue;
    395 		if (intrloc[i] == 0) {
    396 			if (nextintsrow == LINES)
    397 				continue;
    398 			intrloc[i] = nextintsrow++;
    399 			mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s",
    400 				intrname[i]);
    401 		}
    402 		X(intrcnt);
    403 		l = (int)((float)s.intrcnt[i]/etime + 0.5);
    404 		inttotal += l;
    405 		putint(l, intrloc[i], INTSCOL, 8);
    406 	}
    407 	putint(inttotal, INTSROW + 1, INTSCOL, 8);
    408 	Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
    409 	Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes);
    410 	s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
    411 	    nchtotal.ncs_miss + nchtotal.ncs_long;
    412 	if (state == TIME)
    413 		s1.nchcount = s.nchcount;
    414 
    415 	psiz = 0;
    416 	f2 = 0.0;
    417 
    418 	/*
    419 	 * Last CPU state not calculated yet.
    420 	 */
    421 	for (c = 0; c < CPUSTATES - 1; c++) {
    422 		i = cpuorder[c];
    423 		f1 = cputime(i);
    424 		f2 += f1;
    425 		l = (int) ((f2 + 1.0) / 2.0) - psiz;
    426 		if (c == 0)
    427 			putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0);
    428 		else
    429 			putfloat(f1, GRAPHROW, GRAPHCOL + 12 * c,
    430 				5, 1, 0);
    431 		move(GRAPHROW + 2, psiz);
    432 		psiz += l;
    433 		while (l-- > 0)
    434 			addch(cpuchar[c]);
    435 	}
    436 
    437 	putint(ucount(), STATROW, STATCOL, 3);
    438 	putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
    439 	putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
    440 	putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
    441 	mvaddstr(STATROW, STATCOL + 53, buf);
    442 #define pgtokb(pg)	((pg) * cnt.v_page_size / 1024)
    443 	putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 3, 6);
    444 	putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 9, 6);
    445 	putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 15, 7);
    446 	putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 22, 7);
    447 	putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 3, 6);
    448 	putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 9, 6);
    449 	putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 15, 7);
    450 	putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 22, 7);
    451 	putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 29, 6);
    452 	putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
    453 	putint(total.t_pw, PROCSROW + 1, PROCSCOL + 6, 3);
    454 	putint(total.t_dw, PROCSROW + 1, PROCSCOL + 9, 3);
    455 	putint(total.t_sl, PROCSROW + 1, PROCSCOL + 12, 3);
    456 	putint(total.t_sw, PROCSROW + 1, PROCSCOL + 15, 3);
    457 	PUTRATE(Cnt.v_cow_faults, VMSTATROW + 0, VMSTATCOL + 3, 6);
    458 	PUTRATE(Cnt.v_lookups, VMSTATROW + 1, VMSTATCOL + 3, 6);
    459 	PUTRATE(Cnt.v_hits, VMSTATROW + 2, VMSTATCOL + 3, 6);
    460 	PUTRATE(Cnt.v_zfod, VMSTATROW + 3, VMSTATCOL + 4, 5);
    461 	PUTRATE(Cnt.v_nzfod, VMSTATROW + 4, VMSTATCOL + 3, 6);
    462 	putfloat(cnt.v_nzfod == 0 ? 0.0 : (100.0 * cnt.v_zfod / cnt.v_nzfod),
    463 		 VMSTATROW + 5, VMSTATCOL + 2, 7, 2, 1);
    464 	putint(pgtokb(cnt.v_kernel_pages), VMSTATROW + 6, VMSTATCOL, 9);
    465 	putint(pgtokb(cnt.v_wire_count), VMSTATROW + 7, VMSTATCOL, 9);
    466 	putint(pgtokb(cnt.v_active_count), VMSTATROW + 8, VMSTATCOL, 9);
    467 	putint(pgtokb(cnt.v_inactive_count), VMSTATROW + 9, VMSTATCOL, 9);
    468 	putint(pgtokb(cnt.v_free_count), VMSTATROW + 10, VMSTATCOL, 9);
    469 	PUTRATE(Cnt.v_dfree, VMSTATROW + 11, VMSTATCOL, 9);
    470 	PUTRATE(Cnt.v_pfree, VMSTATROW + 12, VMSTATCOL, 9);
    471 	PUTRATE(Cnt.v_reactivated, VMSTATROW + 13, VMSTATCOL, 9);
    472 	PUTRATE(Cnt.v_scan, VMSTATROW + 14, VMSTATCOL, 9);
    473 	PUTRATE(Cnt.v_rev, VMSTATROW + 15, VMSTATCOL, 9);
    474 	if (LINES - 1 > VMSTATROW + 16)
    475 		PUTRATE(Cnt.v_intrans, VMSTATROW + 16, VMSTATCOL, 9);
    476 	PUTRATE(Cnt.v_pageins, PAGEROW + 2, PAGECOL + 5, 5);
    477 	PUTRATE(Cnt.v_pageouts, PAGEROW + 2, PAGECOL + 10, 5);
    478 	PUTRATE(Cnt.v_swpin, PAGEROW + 2, PAGECOL + 15, 5);	/* - */
    479 	PUTRATE(Cnt.v_swpout, PAGEROW + 2, PAGECOL + 20, 5);	/* - */
    480 	PUTRATE(Cnt.v_pgpgin, PAGEROW + 3, PAGECOL + 5, 5);	/* ? */
    481 	PUTRATE(Cnt.v_pgpgout, PAGEROW + 3, PAGECOL + 10, 5);	/* ? */
    482 	PUTRATE(Cnt.v_pswpin, PAGEROW + 3, PAGECOL + 15, 5);	/* - */
    483 	PUTRATE(Cnt.v_pswpout, PAGEROW + 3, PAGECOL + 20, 5);	/* - */
    484 	PUTRATE(Cnt.v_swtch, GENSTATROW + 1, GENSTATCOL, 5);
    485 	PUTRATE(Cnt.v_trap, GENSTATROW + 1, GENSTATCOL + 5, 5);
    486 	PUTRATE(Cnt.v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 5);
    487 	PUTRATE(Cnt.v_intr, GENSTATROW + 1, GENSTATCOL + 15, 5);
    488 	PUTRATE(Cnt.v_soft, GENSTATROW + 1, GENSTATCOL + 20, 5);
    489 	PUTRATE(Cnt.v_faults, GENSTATROW + 1, GENSTATCOL + 25, 5);
    490 	mvprintw(DISKROW, DISKCOL + 5, "                              ");
    491 	for (i = 0, c = 0; i < dk_ndrive && c < MAXDRIVES; i++)
    492 		if (dk_select[i]) {
    493 			mvprintw(DISKROW, DISKCOL + 5 + 5 * c,
    494 				"  %3.3s", dr_name[i]);
    495 			dinfo(i, ++c);
    496 		}
    497 	putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
    498 	putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 9, 9);
    499 #define nz(x)	((x) ? (x) : 1)
    500 	putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount),
    501 	   NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
    502 	putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
    503 	putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
    504 	   NAMEIROW + 2, NAMEICOL + 34, 4, 0, 1);
    505 #undef nz
    506 }
    507 
    508 int
    509 cmdkre(cmd, args)
    510 	char *cmd, *args;
    511 {
    512 
    513 	if (prefix(cmd, "run")) {
    514 		copyinfo(&s2, &s1);
    515 		state = RUN;
    516 		return (1);
    517 	}
    518 	if (prefix(cmd, "boot")) {
    519 		state = BOOT;
    520 		copyinfo(&z, &s1);
    521 		return (1);
    522 	}
    523 	if (prefix(cmd, "time")) {
    524 		state = TIME;
    525 		return (1);
    526 	}
    527 	if (prefix(cmd, "zero")) {
    528 		if (state == RUN)
    529 			getinfo(&s1, RUN);
    530 		return (1);
    531 	}
    532 	return (dkcmd(cmd, args));
    533 }
    534 
    535 /* calculate number of users on the system */
    536 static int
    537 ucount()
    538 {
    539 	register int nusers = 0;
    540 
    541 	if (ut < 0)
    542 		return (0);
    543 	while (read(ut, &utmp, sizeof(utmp)))
    544 		if (utmp.ut_name[0] != '\0')
    545 			nusers++;
    546 
    547 	lseek(ut, 0L, L_SET);
    548 	return (nusers);
    549 }
    550 
    551 static float
    552 cputime(indx)
    553 	int indx;
    554 {
    555 	double t;
    556 	register int i;
    557 
    558 	t = 0;
    559 	for (i = 0; i < CPUSTATES; i++)
    560 		t += s.time[i];
    561 	if (t == 0.0)
    562 		t = 1.0;
    563 	return (s.time[indx] * 100.0 / t);
    564 }
    565 
    566 static void
    567 putint(n, l, c, w)
    568 	int n, l, c, w;
    569 {
    570 	char b[128];
    571 
    572 	move(l, c);
    573 	if (n == 0) {
    574 		while (w-- > 0)
    575 			addch(' ');
    576 		return;
    577 	}
    578 	sprintf(b, "%*d", w, n);
    579 	if (strlen(b) > w) {
    580 		while (w-- > 0)
    581 			addch('*');
    582 		return;
    583 	}
    584 	addstr(b);
    585 }
    586 
    587 static void
    588 putfloat(f, l, c, w, d, nz)
    589 	double f;
    590 	int l, c, w, d, nz;
    591 {
    592 	char b[128];
    593 
    594 	move(l, c);
    595 	if (nz && f == 0.0) {
    596 		while (--w >= 0)
    597 			addch(' ');
    598 		return;
    599 	}
    600 	sprintf(b, "%*.*f", w, d, f);
    601 	if (strlen(b) > w) {
    602 		while (--w >= 0)
    603 			addch('*');
    604 		return;
    605 	}
    606 	addstr(b);
    607 }
    608 
    609 static void
    610 getinfo(s, st)
    611 	struct Info *s;
    612 	enum state st;
    613 {
    614 	int mib[2], size;
    615 	extern int errno;
    616 
    617 	NREAD(X_CPTIME, s->time, sizeof s->time);
    618 	NREAD(X_CNT, &s->Cnt, sizeof s->Cnt);
    619 	NREAD(X_DK_BUSY, &s->dk_busy, LONG);
    620 	NREAD(X_DK_TIME, s->dk_time, dk_ndrive * LONG);
    621 	NREAD(X_DK_XFER, s->dk_xfer, dk_ndrive * LONG);
    622 	NREAD(X_DK_WDS, s->dk_wds, dk_ndrive * LONG);
    623 	NREAD(X_DK_SEEK, s->dk_seek, dk_ndrive * LONG);
    624 	NREAD(X_NCHSTATS, &s->nchstats, sizeof s->nchstats);
    625 	NREAD(X_INTRCNT, s->intrcnt, nintr * LONG);
    626 	size = sizeof(s->Total);
    627 	mib[0] = CTL_VM;
    628 	mib[1] = VM_METER;
    629 	if (sysctl(mib, 2, &s->Total, &size, NULL, 0) < 0) {
    630 		error("Can't get kernel info: %s\n", strerror(errno));
    631 		bzero(&s->Total, sizeof(s->Total));
    632 	}
    633 }
    634 
    635 static void
    636 allocinfo(s)
    637 	struct Info *s;
    638 {
    639 
    640 	s->intrcnt = (long *) malloc(nintr * sizeof(long));
    641 	if (s->intrcnt == NULL) {
    642 		fprintf(stderr, "systat: out of memory\n");
    643 		exit(2);
    644 	}
    645 }
    646 
    647 static void
    648 copyinfo(from, to)
    649 	register struct Info *from, *to;
    650 {
    651 	long *time, *wds, *seek, *xfer;
    652 	long *intrcnt;
    653 
    654 	/*
    655 	 * time, wds, seek, and xfer are malloc'd so we have to
    656 	 * save the pointers before the structure copy and then
    657 	 * copy by hand.
    658 	 */
    659 	time = to->dk_time; wds = to->dk_wds; seek = to->dk_seek;
    660 	xfer = to->dk_xfer; intrcnt = to->intrcnt;
    661 	*to = *from;
    662 	bcopy(from->dk_time, to->dk_time = time, dk_ndrive * sizeof (long));
    663 	bcopy(from->dk_wds, to->dk_wds = wds, dk_ndrive * sizeof (long));
    664 	bcopy(from->dk_seek, to->dk_seek = seek, dk_ndrive * sizeof (long));
    665 	bcopy(from->dk_xfer, to->dk_xfer = xfer, dk_ndrive * sizeof (long));
    666 	bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
    667 }
    668 
    669 static void
    670 dinfo(dn, c)
    671 	int dn, c;
    672 {
    673 	double words, atime, itime, xtime;
    674 
    675 	c = DISKCOL + c * 5;
    676 	atime = s.dk_time[dn];
    677 	atime /= hertz;
    678 	words = s.dk_wds[dn]*32.0;	/* number of words transferred */
    679 	xtime = dk_mspw[dn]*words;	/* transfer time */
    680 	itime = atime - xtime;		/* time not transferring */
    681 	if (xtime < 0)
    682 		itime += xtime, xtime = 0;
    683 	if (itime < 0)
    684 		xtime += itime, itime = 0;
    685 	putint((int)((float)s.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5);
    686 	putint((int)((float)s.dk_xfer[dn]/etime+0.5), DISKROW + 2, c, 5);
    687 	putint((int)(words/etime/512.0 + 0.5), DISKROW + 3, c, 5);
    688 	if (s.dk_seek[dn])
    689 		putfloat(itime*1000.0/s.dk_seek[dn], DISKROW + 4, c, 5, 1, 1);
    690 	else
    691 		putint(0, DISKROW + 4, c, 5);
    692 }
    693