Home | History | Annotate | Line # | Download | only in systat
vmstat.c revision 1.12
      1 /*	$NetBSD: vmstat.c,v 1.12 1998/02/09 14:14:43 mrg 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 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)vmstat.c	8.2 (Berkeley) 1/12/94";
     40 #endif
     41 __RCSID("$NetBSD: vmstat.c,v 1.12 1998/02/09 14:14:43 mrg Exp $");
     42 #endif /* not lint */
     43 
     44 /*
     45  * Cursed vmstat -- from Robert Elz.
     46  */
     47 
     48 #include <sys/param.h>
     49 #include <sys/dkstat.h>
     50 #include <sys/buf.h>
     51 #include <sys/stat.h>
     52 #include <sys/time.h>
     53 #include <sys/user.h>
     54 #include <sys/proc.h>
     55 #include <sys/namei.h>
     56 #include <sys/sysctl.h>
     57 #include <vm/vm.h>
     58 
     59 #if defined(UVM)
     60 #include <uvm/uvm_extern.h>
     61 #endif
     62 
     63 #include <ctype.h>
     64 #include <err.h>
     65 #include <nlist.h>
     66 #include <paths.h>
     67 #include <signal.h>
     68 #include <stdlib.h>
     69 #include <string.h>
     70 #include <utmp.h>
     71 #include <unistd.h>
     72 
     73 #include "systat.h"
     74 #include "extern.h"
     75 
     76 static struct Info {
     77 	long	time[CPUSTATES];
     78 #if defined(UVM)
     79 	struct	uvmexp uvmexp;
     80 #else
     81 	struct	vmmeter Cnt;
     82 #endif
     83 	struct	vmtotal Total;
     84 	struct	nchstats nchstats;
     85 	long	nchcount;
     86 	long	*intrcnt;
     87 } s, s1, s2, z;
     88 
     89 #include "dkstats.h"
     90 extern struct _disk	cur;
     91 
     92 
     93 #define	cnt s.Cnt
     94 #define oldcnt s1.Cnt
     95 #define	total s.Total
     96 #define	nchtotal s.nchstats
     97 #define	oldnchtotal s1.nchstats
     98 
     99 static	enum state { BOOT, TIME, RUN } state = TIME;
    100 
    101 static void allocinfo __P((struct Info *));
    102 static void copyinfo __P((struct Info *, struct Info *));
    103 static float cputime __P((int));
    104 static void dinfo __P((int, int));
    105 static void getinfo __P((struct Info *, enum state));
    106 static void putint __P((int, int, int, int));
    107 static void putfloat __P((double, int, int, int, int, int));
    108 static int ucount __P((void));
    109 
    110 static	int ut;
    111 static	char buf[26];
    112 static	time_t t;
    113 static	double etime;
    114 static	float hertz;
    115 static	int nintr;
    116 static	long *intrloc;
    117 static	char **intrname;
    118 static	int nextintsrow;
    119 
    120 struct	utmp utmp;
    121 
    122 WINDOW *
    123 openkre()
    124 {
    125 
    126 	ut = open(_PATH_UTMP, O_RDONLY);
    127 	if (ut < 0)
    128 		error("No utmp");
    129 	return (stdscr);
    130 }
    131 
    132 void
    133 closekre(w)
    134 	WINDOW *w;
    135 {
    136 
    137 	(void) close(ut);
    138 	if (w == NULL)
    139 		return;
    140 	wclear(w);
    141 	wrefresh(w);
    142 }
    143 
    144 
    145 static struct nlist namelist[] = {
    146 #define X_CPTIME	0
    147 	{ "_cp_time" },
    148 #define X_TOTAL		1
    149 	{ "_total" },
    150 #define	X_NCHSTATS	2
    151 	{ "_nchstats" },
    152 #define	X_INTRNAMES	3
    153 	{ "_intrnames" },
    154 #define	X_EINTRNAMES	4
    155 	{ "_eintrnames" },
    156 #define	X_INTRCNT	5
    157 	{ "_intrcnt" },
    158 #define	X_EINTRCNT	6
    159 	{ "_eintrcnt" },
    160 #if !defined(UVM)
    161 #define X_CNT		7
    162 	{ "_cnt" },
    163 #endif
    164 	{ "" },
    165 };
    166 
    167 /*
    168  * These constants define where the major pieces are laid out
    169  */
    170 #define STATROW		 0	/* uses 1 row and 68 cols */
    171 #define STATCOL		 2
    172 #define MEMROW		 2	/* uses 4 rows and 31 cols */
    173 #define MEMCOL		 0
    174 #define PAGEROW		 2	/* uses 4 rows and 26 cols */
    175 #define PAGECOL		36
    176 #define INTSROW		 2	/* uses all rows to bottom and 17 cols */
    177 #define INTSCOL		63
    178 #define PROCSROW	 7	/* uses 2 rows and 20 cols */
    179 #define PROCSCOL	 0
    180 #define GENSTATROW	 7	/* uses 2 rows and 30 cols */
    181 #define GENSTATCOL	20
    182 #define VMSTATROW	 7	/* uses 17 rows and 12 cols */
    183 #define VMSTATCOL	48
    184 #define GRAPHROW	10	/* uses 3 rows and 51 cols */
    185 #define GRAPHCOL	 0
    186 #define NAMEIROW	14	/* uses 3 rows and 38 cols */
    187 #define NAMEICOL	 0
    188 #define DISKROW		18	/* uses 5 rows and 50 cols (for 9 drives) */
    189 #define DISKCOL		 0
    190 
    191 #define	DRIVESPACE	 9	/* max # for space */
    192 
    193 #if DK_NDRIVE > DRIVESPACE
    194 #define	MAXDRIVES	DRIVESPACE	 /* max # to display */
    195 #else
    196 #define	MAXDRIVES	DK_NDRIVE	 /* max # to display */
    197 #endif
    198 
    199 int
    200 initkre()
    201 {
    202 	char *intrnamebuf, *cp;
    203 	int i;
    204 	static int once = 0;
    205 
    206 	if (namelist[0].n_type == 0) {
    207 		if (kvm_nlist(kd, namelist)) {
    208 			nlisterr(namelist);
    209 			return(0);
    210 		}
    211 		if (namelist[0].n_type == 0) {
    212 			error("No namelist");
    213 			return(0);
    214 		}
    215 	}
    216 	hertz = stathz ? stathz : hz;
    217 	if (! dkinit(1))
    218 		return(0);
    219 	if (dk_ndrive && !once) {
    220 #define	allocate(e, t) \
    221 	s./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
    222 	s1./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
    223 	s2./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
    224 	z./**/e = (t *)calloc(dk_ndrive, sizeof (t));
    225 		once = 1;
    226 #undef allocate
    227 	}
    228 	if (nintr == 0) {
    229 		nintr = (namelist[X_EINTRCNT].n_value -
    230 			namelist[X_INTRCNT].n_value) / sizeof (long);
    231 		intrloc = calloc(nintr, sizeof (long));
    232 		intrname = calloc(nintr, sizeof (long));
    233 		intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value -
    234 			namelist[X_INTRNAMES].n_value);
    235 		if (intrnamebuf == 0 || intrname == 0 || intrloc == 0) {
    236 			error("Out of memory\n");
    237 			if (intrnamebuf)
    238 				free(intrnamebuf);
    239 			if (intrname)
    240 				free(intrname);
    241 			if (intrloc)
    242 				free(intrloc);
    243 			nintr = 0;
    244 			return(0);
    245 		}
    246 		NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) -
    247 			NVAL(X_INTRNAMES));
    248 		for (cp = intrnamebuf, i = 0; i < nintr; i++) {
    249 			intrname[i] = cp;
    250 			cp += strlen(cp) + 1;
    251 		}
    252 		nextintsrow = INTSROW + 2;
    253 		allocinfo(&s);
    254 		allocinfo(&s1);
    255 		allocinfo(&s2);
    256 		allocinfo(&z);
    257 	}
    258 	getinfo(&s2, RUN);
    259 	copyinfo(&s2, &s1);
    260 	return(1);
    261 }
    262 
    263 void
    264 fetchkre()
    265 {
    266 	time_t now;
    267 
    268 	time(&now);
    269 	strcpy(buf, ctime(&now));
    270 	buf[16] = '\0';
    271 	getinfo(&s, state);
    272 }
    273 
    274 void
    275 labelkre()
    276 {
    277 	int i, j;
    278 
    279 	clear();
    280 	mvprintw(STATROW, STATCOL + 4, "users    Load");
    281 	mvprintw(MEMROW, MEMCOL, "Mem:KB  REAL        VIRTUAL");
    282 	mvprintw(MEMROW + 1, MEMCOL, "      Tot Share    Tot  Share");
    283 	mvprintw(MEMROW + 2, MEMCOL, "Act");
    284 	mvprintw(MEMROW + 3, MEMCOL, "All");
    285 
    286 	mvprintw(MEMROW + 1, MEMCOL + 31, "Free");
    287 
    288 	mvprintw(PAGEROW, PAGECOL,     "        PAGING   SWAPPING ");
    289 	mvprintw(PAGEROW + 1, PAGECOL, "        in  out   in  out ");
    290 	mvprintw(PAGEROW + 2, PAGECOL, "count");
    291 	mvprintw(PAGEROW + 3, PAGECOL, "pages");
    292 
    293 	mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
    294 	mvprintw(INTSROW + 1, INTSCOL + 9, "total");
    295 
    296 #if defined(UVM)
    297 	mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "forks");
    298 	mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "fkppw");
    299 	mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "fksvm");
    300 	mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "pwait");
    301 	mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "relck");
    302 	mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "rlkok");
    303 	mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "noram");
    304 	mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "ndcpy");
    305 	mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "fltcp");
    306 	mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "zfod");
    307 	mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "cow");
    308 	mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "fmin");
    309 	mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "ftarg");
    310 	mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "itarg");
    311 	mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "wired");
    312 	mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "pdfre");
    313 	if (LINES - 1 > VMSTATROW + 16)
    314 		mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "pdscn");
    315 #else
    316 	mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "cow");
    317 	mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "objlk");
    318 	mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "objht");
    319 	mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "zfod");
    320 	mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "nzfod");
    321 	mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "%%zfod");
    322 	mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "kern");
    323 	mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "wire");
    324 	mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "act");
    325 	mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "inact");
    326 	mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "free");
    327 	mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "daefr");
    328 	mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "prcfr");
    329 	mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "react");
    330 	mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "scan");
    331 	mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "hdrev");
    332 	if (LINES - 1 > VMSTATROW + 16)
    333 		mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "intrn");
    334 #endif
    335 
    336 	mvprintw(GENSTATROW, GENSTATCOL, "  Csw  Trp  Sys  Int  Sof  Flt");
    337 
    338 	mvprintw(GRAPHROW, GRAPHCOL,
    339 		"    . %% Sys    . %% User    . %% Nice    . %% Idle");
    340 	mvprintw(PROCSROW, PROCSCOL, "Proc:r  p  d  s  w");
    341 	mvprintw(GRAPHROW + 1, GRAPHCOL,
    342 		"|    |    |    |    |    |    |    |    |    |    |");
    343 
    344 	mvprintw(NAMEIROW, NAMEICOL, "Namei         Sys-cache     Proc-cache");
    345 	mvprintw(NAMEIROW + 1, NAMEICOL,
    346 		"    Calls     hits    %%     hits     %%");
    347 	mvprintw(DISKROW, DISKCOL, "Discs");
    348 	mvprintw(DISKROW + 1, DISKCOL, "seeks");
    349 	mvprintw(DISKROW + 2, DISKCOL, "xfers");
    350 	mvprintw(DISKROW + 3, DISKCOL, "Kbyte");
    351 	mvprintw(DISKROW + 4, DISKCOL, "  sec");
    352 	j = 0;
    353 	for (i = 0; i < dk_ndrive && j < MAXDRIVES; i++)
    354 		if (dk_select[i]) {
    355 			mvprintw(DISKROW, DISKCOL + 5 + 5 * j,
    356 				"  %3.3s", dr_name[j]);
    357 			j++;
    358 		}
    359 	for (i = 0; i < nintr; i++) {
    360 		if (intrloc[i] == 0)
    361 			continue;
    362 		mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", intrname[i]);
    363 	}
    364 }
    365 
    366 #define X(fld)	{t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
    367 #define Y(fld)	{t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
    368 #define Z(fld)	{t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
    369 	if(state == TIME) s1.nchstats.fld = t;}
    370 #define PUTRATE(fld, l, c, w) Y(fld); putint((int)((float)s.fld/etime + 0.5), l, c, w)
    371 #define MAXFAIL 5
    372 
    373 static	char cpuchar[CPUSTATES] = { '=' , '>', '-', ' ' };
    374 static	char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_IDLE };
    375 
    376 void
    377 showkre()
    378 {
    379 	float f1, f2;
    380 	int psiz, inttotal;
    381 	int i, l, c;
    382 	static int failcnt = 0;
    383 
    384 
    385 	if (state == TIME)
    386 		dkswap();
    387 	etime = 0;
    388 	for(i = 0; i < CPUSTATES; i++) {
    389 		X(time);
    390 		etime += s.time[i];
    391 	}
    392 	if (etime < 5.0) {	/* < 5 ticks - ignore this trash */
    393 		if (failcnt++ >= MAXFAIL) {
    394 			clear();
    395 			mvprintw(2, 10, "The alternate system clock has died!");
    396 			mvprintw(3, 10, "Reverting to ``pigs'' display.");
    397 			move(CMDLINE, 0);
    398 			refresh();
    399 			failcnt = 0;
    400 			sleep(5);
    401 			command("pigs");
    402 		}
    403 		return;
    404 	}
    405 	failcnt = 0;
    406 	etime /= hertz;
    407 	inttotal = 0;
    408 	for (i = 0; i < nintr; i++) {
    409 		if (s.intrcnt[i] == 0)
    410 			continue;
    411 		if (intrloc[i] == 0) {
    412 			if (nextintsrow == LINES)
    413 				continue;
    414 			intrloc[i] = nextintsrow++;
    415 			mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s",
    416 				intrname[i]);
    417 		}
    418 		X(intrcnt);
    419 		l = (int)((float)s.intrcnt[i]/etime + 0.5);
    420 		inttotal += l;
    421 		putint(l, intrloc[i], INTSCOL, 8);
    422 	}
    423 	putint(inttotal, INTSROW + 1, INTSCOL, 8);
    424 	Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
    425 	Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes);
    426 	s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
    427 	    nchtotal.ncs_miss + nchtotal.ncs_long;
    428 	if (state == TIME)
    429 		s1.nchcount = s.nchcount;
    430 
    431 	psiz = 0;
    432 	f2 = 0.0;
    433 
    434 	/*
    435 	 * Last CPU state not calculated yet.
    436 	 */
    437 	for (c = 0; c < CPUSTATES - 1; c++) {
    438 		i = cpuorder[c];
    439 		f1 = cputime(i);
    440 		f2 += f1;
    441 		l = (int) ((f2 + 1.0) / 2.0) - psiz;
    442 		if (c == 0)
    443 			putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0);
    444 		else
    445 			putfloat(f1, GRAPHROW, GRAPHCOL + 12 * c,
    446 				5, 1, 0);
    447 		move(GRAPHROW + 2, psiz);
    448 		psiz += l;
    449 		while (l-- > 0)
    450 			addch(cpuchar[c]);
    451 	}
    452 
    453 	putint(ucount(), STATROW, STATCOL, 3);
    454 	putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
    455 	putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
    456 	putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
    457 	mvaddstr(STATROW, STATCOL + 53, buf);
    458 #if defined(UVM)
    459 #define pgtokb(pg)	((pg) * s.uvmexp.pagesize / 1024)
    460 #else
    461 #define pgtokb(pg)	((pg) * cnt.v_page_size / 1024)
    462 #endif
    463 
    464 #if defined(UVM)
    465 	putint(pgtokb(s.uvmexp.active), MEMROW + 2, MEMCOL + 3, 6);
    466 	putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 9, 6); /* XXX */
    467 	putint(pgtokb(s.uvmexp.active + s.uvmexp.swpginuse),	/* XXX */
    468 	    MEMROW + 2, MEMCOL + 15, 7);
    469 	putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 22, 7);/* XXX */
    470 	putint(pgtokb(s.uvmexp.npages - s.uvmexp.free), MEMROW + 3, MEMCOL + 3, 6);
    471 	putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 9, 6); /* XXX */
    472 	putint(pgtokb(s.uvmexp.npages - s.uvmexp.free + s.uvmexp.swpginuse),
    473 	    MEMROW + 3, MEMCOL + 15, 7);
    474 	putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 22, 7); /* XXX */
    475 	putint(pgtokb(s.uvmexp.free), MEMROW + 2, MEMCOL + 29, 6);
    476 	putint(pgtokb(s.uvmexp.free + s.uvmexp.swpages - s.uvmexp.swpginuse),
    477 	    MEMROW + 3, MEMCOL + 29, 6);
    478 #else
    479 	putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 3, 6);
    480 	putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 9, 6);
    481 	putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 15, 7);
    482 	putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 22, 7);
    483 	putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 3, 6);
    484 	putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 9, 6);
    485 	putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 15, 7);
    486 	putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 22, 7);
    487 	putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 29, 6);
    488 #endif
    489 	putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
    490 	putint(total.t_pw, PROCSROW + 1, PROCSCOL + 6, 3);
    491 	putint(total.t_dw, PROCSROW + 1, PROCSCOL + 9, 3);
    492 	putint(total.t_sl, PROCSROW + 1, PROCSCOL + 12, 3);
    493 	putint(total.t_sw, PROCSROW + 1, PROCSCOL + 15, 3);
    494 #if defined(UVM)
    495 	PUTRATE(uvmexp.forks, VMSTATROW + 0, VMSTATCOL + 3, 6);
    496 	PUTRATE(uvmexp.forks_ppwait, VMSTATROW + 1, VMSTATCOL + 3, 6);
    497 	PUTRATE(uvmexp.forks_sharevm, VMSTATROW + 2, VMSTATCOL + 3, 6);
    498 	PUTRATE(uvmexp.fltpgwait, VMSTATROW + 3, VMSTATCOL + 4, 5);
    499 	PUTRATE(uvmexp.fltrelck, VMSTATROW + 4, VMSTATCOL + 3, 6);
    500 	PUTRATE(uvmexp.fltrelckok, VMSTATROW + 5, VMSTATCOL + 3, 6);
    501 	PUTRATE(uvmexp.fltnoram, VMSTATROW + 6, VMSTATCOL + 3, 6);
    502 	PUTRATE(uvmexp.fltamcopy, VMSTATROW + 7, VMSTATCOL + 3, 6);
    503 	PUTRATE(uvmexp.flt_prcopy, VMSTATROW + 8, VMSTATCOL + 3, 6);
    504 	PUTRATE(uvmexp.flt_przero, VMSTATROW + 9, VMSTATCOL + 3, 6);
    505 	PUTRATE(uvmexp.flt_acow, VMSTATROW + 10, VMSTATCOL, 9);
    506 	putint(s.uvmexp.freemin, VMSTATROW + 11, VMSTATCOL, 9);
    507 	putint(s.uvmexp.freetarg, VMSTATROW + 12, VMSTATCOL, 9);
    508 	putint(s.uvmexp.inactarg, VMSTATROW + 13, VMSTATCOL, 9);
    509 	putint(s.uvmexp.wired, VMSTATROW + 14, VMSTATCOL, 9);
    510 	PUTRATE(uvmexp.pdfreed, VMSTATROW + 15, VMSTATCOL, 9);
    511 	if (LINES - 1 > VMSTATROW + 16)
    512 		PUTRATE(uvmexp.pdscans, VMSTATROW + 16, VMSTATCOL, 9);
    513 
    514 	PUTRATE(uvmexp.pageins, PAGEROW + 2, PAGECOL + 5, 5);
    515 	PUTRATE(uvmexp.pdpageouts, PAGEROW + 2, PAGECOL + 10, 5);
    516 	PUTRATE(uvmexp.swapins, PAGEROW + 2, PAGECOL + 15, 5);
    517 	PUTRATE(uvmexp.swapouts, PAGEROW + 2, PAGECOL + 20, 5);
    518 	PUTRATE(uvmexp.pgswapin, PAGEROW + 3, PAGECOL + 5, 5);
    519 	PUTRATE(uvmexp.pgswapout, PAGEROW + 3, PAGECOL + 10, 5);
    520 
    521 	PUTRATE(uvmexp.swtch, GENSTATROW + 1, GENSTATCOL, 5);
    522 	PUTRATE(uvmexp.traps, GENSTATROW + 1, GENSTATCOL + 5, 5);
    523 	PUTRATE(uvmexp.syscalls, GENSTATROW + 1, GENSTATCOL + 10, 5);
    524 	PUTRATE(uvmexp.intrs, GENSTATROW + 1, GENSTATCOL + 15, 5);
    525 	PUTRATE(uvmexp.softs, GENSTATROW + 1, GENSTATCOL + 20, 5);
    526 	PUTRATE(uvmexp.faults, GENSTATROW + 1, GENSTATCOL + 25, 5);
    527 #else
    528 	PUTRATE(Cnt.v_cow_faults, VMSTATROW + 0, VMSTATCOL + 3, 6);
    529 	PUTRATE(Cnt.v_lookups, VMSTATROW + 1, VMSTATCOL + 3, 6);
    530 	PUTRATE(Cnt.v_hits, VMSTATROW + 2, VMSTATCOL + 3, 6);
    531 	PUTRATE(Cnt.v_zfod, VMSTATROW + 3, VMSTATCOL + 4, 5);
    532 	PUTRATE(Cnt.v_nzfod, VMSTATROW + 4, VMSTATCOL + 3, 6);
    533 	putfloat(cnt.v_nzfod == 0 ? 0.0 : (100.0 * cnt.v_zfod / cnt.v_nzfod),
    534 		 VMSTATROW + 5, VMSTATCOL + 2, 7, 2, 1);
    535 	putint(pgtokb(cnt.v_kernel_pages), VMSTATROW + 6, VMSTATCOL, 9);
    536 	putint(pgtokb(cnt.v_wire_count), VMSTATROW + 7, VMSTATCOL, 9);
    537 	putint(pgtokb(cnt.v_active_count), VMSTATROW + 8, VMSTATCOL, 9);
    538 	putint(pgtokb(cnt.v_inactive_count), VMSTATROW + 9, VMSTATCOL, 9);
    539 	putint(pgtokb(cnt.v_free_count), VMSTATROW + 10, VMSTATCOL, 9);
    540 	PUTRATE(Cnt.v_dfree, VMSTATROW + 11, VMSTATCOL, 9);
    541 	PUTRATE(Cnt.v_pfree, VMSTATROW + 12, VMSTATCOL, 9);
    542 	PUTRATE(Cnt.v_reactivated, VMSTATROW + 13, VMSTATCOL, 9);
    543 	PUTRATE(Cnt.v_scan, VMSTATROW + 14, VMSTATCOL, 9);
    544 	PUTRATE(Cnt.v_rev, VMSTATROW + 15, VMSTATCOL, 9);
    545 	if (LINES - 1 > VMSTATROW + 16)
    546 		PUTRATE(Cnt.v_intrans, VMSTATROW + 16, VMSTATCOL, 9);
    547 	PUTRATE(Cnt.v_pageins, PAGEROW + 2, PAGECOL + 5, 5);
    548 	PUTRATE(Cnt.v_pageouts, PAGEROW + 2, PAGECOL + 10, 5);
    549 	PUTRATE(Cnt.v_swpin, PAGEROW + 2, PAGECOL + 15, 5);	/* - */
    550 	PUTRATE(Cnt.v_swpout, PAGEROW + 2, PAGECOL + 20, 5);	/* - */
    551 	PUTRATE(Cnt.v_pgpgin, PAGEROW + 3, PAGECOL + 5, 5);	/* ? */
    552 	PUTRATE(Cnt.v_pgpgout, PAGEROW + 3, PAGECOL + 10, 5);	/* ? */
    553 	PUTRATE(Cnt.v_pswpin, PAGEROW + 3, PAGECOL + 15, 5);	/* - */
    554 	PUTRATE(Cnt.v_pswpout, PAGEROW + 3, PAGECOL + 20, 5);	/* - */
    555 	PUTRATE(Cnt.v_swtch, GENSTATROW + 1, GENSTATCOL, 5);
    556 	PUTRATE(Cnt.v_trap, GENSTATROW + 1, GENSTATCOL + 5, 5);
    557 	PUTRATE(Cnt.v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 5);
    558 	PUTRATE(Cnt.v_intr, GENSTATROW + 1, GENSTATCOL + 15, 5);
    559 	PUTRATE(Cnt.v_soft, GENSTATROW + 1, GENSTATCOL + 20, 5);
    560 	PUTRATE(Cnt.v_faults, GENSTATROW + 1, GENSTATCOL + 25, 5);
    561 #endif
    562 	mvprintw(DISKROW, DISKCOL + 5, "                              ");
    563 	for (i = 0, c = 0; i < dk_ndrive && c < MAXDRIVES; i++)
    564 		if (dk_select[i]) {
    565 			mvprintw(DISKROW, DISKCOL + 5 + 5 * c,
    566 				"  %3.3s", dr_name[i]);
    567 			dinfo(i, ++c);
    568 		}
    569 	putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
    570 	putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 9, 9);
    571 #define nz(x)	((x) ? (x) : 1)
    572 	putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount),
    573 	   NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
    574 	putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
    575 	putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
    576 	   NAMEIROW + 2, NAMEICOL + 34, 4, 0, 1);
    577 #undef nz
    578 }
    579 
    580 int
    581 cmdkre(cmd, args)
    582 	char *cmd, *args;
    583 {
    584 
    585 	if (prefix(cmd, "run")) {
    586 		copyinfo(&s2, &s1);
    587 		state = RUN;
    588 		return (1);
    589 	}
    590 	if (prefix(cmd, "boot")) {
    591 		state = BOOT;
    592 		copyinfo(&z, &s1);
    593 		return (1);
    594 	}
    595 	if (prefix(cmd, "time")) {
    596 		state = TIME;
    597 		return (1);
    598 	}
    599 	if (prefix(cmd, "zero")) {
    600 		if (state == RUN)
    601 			getinfo(&s1, RUN);
    602 		return (1);
    603 	}
    604 	return (dkcmd(cmd, args));
    605 }
    606 
    607 /* calculate number of users on the system */
    608 static int
    609 ucount()
    610 {
    611 	int nusers = 0;
    612 
    613 	if (ut < 0)
    614 		return (0);
    615 	while (read(ut, &utmp, sizeof(utmp)))
    616 		if (utmp.ut_name[0] != '\0')
    617 			nusers++;
    618 
    619 	lseek(ut, (off_t)0, SEEK_SET);
    620 	return (nusers);
    621 }
    622 
    623 static float
    624 cputime(indx)
    625 	int indx;
    626 {
    627 	double t;
    628 	int i;
    629 
    630 	t = 0;
    631 	for (i = 0; i < CPUSTATES; i++)
    632 		t += s.time[i];
    633 	if (t == 0.0)
    634 		t = 1.0;
    635 	return (s.time[indx] * 100.0 / t);
    636 }
    637 
    638 static void
    639 putint(n, l, c, w)
    640 	int n, l, c, w;
    641 {
    642 	char b[128];
    643 
    644 	move(l, c);
    645 	if (n == 0) {
    646 		while (w-- > 0)
    647 			addch(' ');
    648 		return;
    649 	}
    650 	sprintf(b, "%*d", w, n);
    651 	if (strlen(b) > w) {
    652 		while (w-- > 0)
    653 			addch('*');
    654 		return;
    655 	}
    656 	addstr(b);
    657 }
    658 
    659 static void
    660 putfloat(f, l, c, w, d, nz)
    661 	double f;
    662 	int l, c, w, d, nz;
    663 {
    664 	char b[128];
    665 
    666 	move(l, c);
    667 	if (nz && f == 0.0) {
    668 		while (--w >= 0)
    669 			addch(' ');
    670 		return;
    671 	}
    672 	sprintf(b, "%*.*f", w, d, f);
    673 	if (strlen(b) > w) {
    674 		while (--w >= 0)
    675 			addch('*');
    676 		return;
    677 	}
    678 	addstr(b);
    679 }
    680 
    681 static void
    682 getinfo(s, st)
    683 	struct Info *s;
    684 	enum state st;
    685 {
    686 	int mib[2];
    687 	size_t size;
    688 	extern int errno;
    689 
    690 	dkreadstats();
    691 	NREAD(X_CPTIME, s->time, sizeof s->time);
    692 	NREAD(X_NCHSTATS, &s->nchstats, sizeof s->nchstats);
    693 	NREAD(X_INTRCNT, s->intrcnt, nintr * LONG);
    694 #if defined(UVM)
    695 	size = sizeof(s->uvmexp);
    696 	mib[0] = CTL_VM;
    697 	mib[1] = VM_UVMEXP;
    698 	if (sysctl(mib, 2, &s->uvmexp, &size, NULL, 0) < 0) {
    699 		error("can't get uvmexp: %s\n", strerror(errno));
    700 		memset(&s->uvmexp, 0, sizeof(s->uvmexp));
    701 	}
    702 #else
    703 	NREAD(X_CNT, &s->Cnt, sizeof s->Cnt);
    704 #endif
    705 	size = sizeof(s->Total);
    706 	mib[0] = CTL_VM;
    707 	mib[1] = VM_METER;
    708 	if (sysctl(mib, 2, &s->Total, &size, NULL, 0) < 0) {
    709 		error("Can't get kernel info: %s\n", strerror(errno));
    710 		memset(&s->Total, 0, sizeof(s->Total));
    711 	}
    712 }
    713 
    714 static void
    715 allocinfo(s)
    716 	struct Info *s;
    717 {
    718 
    719 	s->intrcnt = (long *) malloc(nintr * sizeof(long));
    720 	if (s->intrcnt == NULL)
    721 		errx(2, "out of memory");
    722 }
    723 
    724 static void
    725 copyinfo(from, to)
    726 	struct Info *from, *to;
    727 {
    728 	long *intrcnt;
    729 
    730 	intrcnt = to->intrcnt;
    731 	*to = *from;
    732 	memmove(to->intrcnt = intrcnt, from->intrcnt, nintr * sizeof (int));
    733 }
    734 
    735 static void
    736 dinfo(dn, c)
    737 	int dn, c;
    738 {
    739 	double words, atime;
    740 
    741 	c = DISKCOL + c * 5;
    742 
    743 	/* time busy in disk activity */
    744 	atime = (double)cur.dk_time[dn].tv_sec +
    745 		((double)cur.dk_time[dn].tv_usec / (double)1000000);
    746 
    747 	words = cur.dk_bytes[dn] / 1024.0;	/* # of K transferred */
    748 
    749 	putint((int)((float)cur.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5);
    750 	putint((int)((float)cur.dk_xfer[dn]/etime+0.5), DISKROW + 2, c, 5);
    751 	putint((int)(words/etime + 0.5), DISKROW + 3, c, 5);
    752 	putfloat(atime/etime, DISKROW + 4, c, 5, 1, 1);
    753 }
    754