Home | History | Annotate | Line # | Download | only in systat
vmstat.c revision 1.13
      1 /*	$NetBSD: vmstat.c,v 1.13 1998/02/09 15:29:52 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.13 1998/02/09 15:29:52 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 #if defined(UVM)
    282 	mvprintw(MEMROW, MEMCOL,     "          memory totals (in KB)");
    283 	mvprintw(MEMROW + 1, MEMCOL, "         real   virtual    free");
    284 	mvprintw(MEMROW + 2, MEMCOL, "Active");
    285 	mvprintw(MEMROW + 3, MEMCOL, "All");
    286 
    287 	mvprintw(PAGEROW, PAGECOL, "        PAGING   SWAPPING ");
    288 	mvprintw(PAGEROW + 1, PAGECOL, "        in  out   in  out ");
    289 	mvprintw(PAGEROW + 2, PAGECOL, "ops");
    290 	mvprintw(PAGEROW + 3, PAGECOL, "pages");
    291 #else
    292 	mvprintw(MEMROW, MEMCOL, "Mem:KB  REAL        VIRTUAL");
    293 	mvprintw(MEMROW + 1, MEMCOL, "      Tot Share    Tot  Share");
    294 	mvprintw(MEMROW + 2, MEMCOL, "Act");
    295 	mvprintw(MEMROW + 3, MEMCOL, "All");
    296 
    297 	mvprintw(MEMROW + 1, MEMCOL + 31, "Free");
    298 
    299 	mvprintw(PAGEROW, PAGECOL,     "        PAGING   SWAPPING ");
    300 	mvprintw(PAGEROW + 1, PAGECOL, "        in  out   in  out ");
    301 	mvprintw(PAGEROW + 2, PAGECOL, "count");
    302 	mvprintw(PAGEROW + 3, PAGECOL, "pages");
    303 #endif
    304 
    305 	mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
    306 	mvprintw(INTSROW + 1, INTSCOL + 9, "total");
    307 
    308 #if defined(UVM)
    309 	mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "forks");
    310 	mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "fkppw");
    311 	mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "fksvm");
    312 	mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "pwait");
    313 	mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "relck");
    314 	mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "rlkok");
    315 	mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "noram");
    316 	mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "ndcpy");
    317 	mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "fltcp");
    318 	mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "zfod");
    319 	mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "cow");
    320 	mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "fmin");
    321 	mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "ftarg");
    322 	mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "itarg");
    323 	mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "wired");
    324 	mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "pdfre");
    325 	if (LINES - 1 > VMSTATROW + 16)
    326 		mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "pdscn");
    327 #else
    328 	mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "cow");
    329 	mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "objlk");
    330 	mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "objht");
    331 	mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "zfod");
    332 	mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "nzfod");
    333 	mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "%%zfod");
    334 	mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "kern");
    335 	mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "wire");
    336 	mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "act");
    337 	mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "inact");
    338 	mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "free");
    339 	mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "daefr");
    340 	mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "prcfr");
    341 	mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "react");
    342 	mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "scan");
    343 	mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "hdrev");
    344 	if (LINES - 1 > VMSTATROW + 16)
    345 		mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "intrn");
    346 #endif
    347 
    348 	mvprintw(GENSTATROW, GENSTATCOL, "  Csw  Trp  Sys  Int  Sof  Flt");
    349 
    350 	mvprintw(GRAPHROW, GRAPHCOL,
    351 		"    . %% Sys    . %% User    . %% Nice    . %% Idle");
    352 #if defined(UVM)
    353 	mvprintw(PROCSROW, PROCSCOL, "Proc:r  d  s  w");
    354 #else
    355 	mvprintw(PROCSROW, PROCSCOL, "Proc:r  d  s  w  p");
    356 #endif
    357 	mvprintw(GRAPHROW + 1, GRAPHCOL,
    358 		"|    |    |    |    |    |    |    |    |    |    |");
    359 
    360 	mvprintw(NAMEIROW, NAMEICOL, "Namei         Sys-cache     Proc-cache");
    361 	mvprintw(NAMEIROW + 1, NAMEICOL,
    362 		"    Calls     hits    %%     hits     %%");
    363 	mvprintw(DISKROW, DISKCOL, "Discs");
    364 	mvprintw(DISKROW + 1, DISKCOL, "seeks");
    365 	mvprintw(DISKROW + 2, DISKCOL, "xfers");
    366 	mvprintw(DISKROW + 3, DISKCOL, "Kbyte");
    367 	mvprintw(DISKROW + 4, DISKCOL, "  sec");
    368 	j = 0;
    369 	for (i = 0; i < dk_ndrive && j < MAXDRIVES; i++)
    370 		if (dk_select[i]) {
    371 			mvprintw(DISKROW, DISKCOL + 5 + 5 * j,
    372 				"  %3.3s", dr_name[j]);
    373 			j++;
    374 		}
    375 	for (i = 0; i < nintr; i++) {
    376 		if (intrloc[i] == 0)
    377 			continue;
    378 		mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", intrname[i]);
    379 	}
    380 }
    381 
    382 #define X(fld)	{t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
    383 #define Y(fld)	{t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
    384 #define Z(fld)	{t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
    385 	if(state == TIME) s1.nchstats.fld = t;}
    386 #define PUTRATE(fld, l, c, w) Y(fld); putint((int)((float)s.fld/etime + 0.5), l, c, w)
    387 #define MAXFAIL 5
    388 
    389 static	char cpuchar[CPUSTATES] = { '=' , '>', '-', ' ' };
    390 static	char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_IDLE };
    391 
    392 void
    393 showkre()
    394 {
    395 	float f1, f2;
    396 	int psiz, inttotal;
    397 	int i, l, c;
    398 	static int failcnt = 0;
    399 
    400 
    401 	if (state == TIME)
    402 		dkswap();
    403 	etime = 0;
    404 	for(i = 0; i < CPUSTATES; i++) {
    405 		X(time);
    406 		etime += s.time[i];
    407 	}
    408 	if (etime < 5.0) {	/* < 5 ticks - ignore this trash */
    409 		if (failcnt++ >= MAXFAIL) {
    410 			clear();
    411 			mvprintw(2, 10, "The alternate system clock has died!");
    412 			mvprintw(3, 10, "Reverting to ``pigs'' display.");
    413 			move(CMDLINE, 0);
    414 			refresh();
    415 			failcnt = 0;
    416 			sleep(5);
    417 			command("pigs");
    418 		}
    419 		return;
    420 	}
    421 	failcnt = 0;
    422 	etime /= hertz;
    423 	inttotal = 0;
    424 	for (i = 0; i < nintr; i++) {
    425 		if (s.intrcnt[i] == 0)
    426 			continue;
    427 		if (intrloc[i] == 0) {
    428 			if (nextintsrow == LINES)
    429 				continue;
    430 			intrloc[i] = nextintsrow++;
    431 			mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s",
    432 				intrname[i]);
    433 		}
    434 		X(intrcnt);
    435 		l = (int)((float)s.intrcnt[i]/etime + 0.5);
    436 		inttotal += l;
    437 		putint(l, intrloc[i], INTSCOL, 8);
    438 	}
    439 	putint(inttotal, INTSROW + 1, INTSCOL, 8);
    440 	Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
    441 	Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes);
    442 	s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
    443 	    nchtotal.ncs_miss + nchtotal.ncs_long;
    444 	if (state == TIME)
    445 		s1.nchcount = s.nchcount;
    446 
    447 	psiz = 0;
    448 	f2 = 0.0;
    449 
    450 	/*
    451 	 * Last CPU state not calculated yet.
    452 	 */
    453 	for (c = 0; c < CPUSTATES - 1; c++) {
    454 		i = cpuorder[c];
    455 		f1 = cputime(i);
    456 		f2 += f1;
    457 		l = (int) ((f2 + 1.0) / 2.0) - psiz;
    458 		if (c == 0)
    459 			putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0);
    460 		else
    461 			putfloat(f1, GRAPHROW, GRAPHCOL + 12 * c,
    462 				5, 1, 0);
    463 		move(GRAPHROW + 2, psiz);
    464 		psiz += l;
    465 		while (l-- > 0)
    466 			addch(cpuchar[c]);
    467 	}
    468 
    469 	putint(ucount(), STATROW, STATCOL, 3);
    470 	putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
    471 	putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
    472 	putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
    473 	mvaddstr(STATROW, STATCOL + 53, buf);
    474 #if defined(UVM)
    475 #define pgtokb(pg)	((pg) * s.uvmexp.pagesize / 1024)
    476 #else
    477 #define pgtokb(pg)	((pg) * cnt.v_page_size / 1024)
    478 #endif
    479 
    480 #if defined(UVM)
    481 	putint(pgtokb(s.uvmexp.active), MEMROW + 2, MEMCOL + 6, 7);
    482 	putint(pgtokb(s.uvmexp.active + s.uvmexp.swpginuse),	/* XXX */
    483 	    MEMROW + 2, MEMCOL + 16, 7);
    484 	putint(pgtokb(s.uvmexp.npages - s.uvmexp.free), MEMROW + 3, MEMCOL + 6, 7);
    485 	putint(pgtokb(s.uvmexp.npages - s.uvmexp.free + s.uvmexp.swpginuse),
    486 	    MEMROW + 3, MEMCOL + 16, 7);
    487 	putint(pgtokb(s.uvmexp.free), MEMROW + 2, MEMCOL + 24, 7);
    488 	putint(pgtokb(s.uvmexp.free + s.uvmexp.swpages - s.uvmexp.swpginuse),
    489 	    MEMROW + 3, MEMCOL + 24, 7);
    490 #else
    491 	putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 3, 6);
    492 	putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 9, 6);
    493 	putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 15, 7);
    494 	putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 22, 7);
    495 	putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 3, 6);
    496 	putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 9, 6);
    497 	putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 15, 7);
    498 	putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 22, 7);
    499 	putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 29, 6);
    500 #endif
    501 	putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
    502 	putint(total.t_dw, PROCSROW + 1, PROCSCOL + 6, 3);
    503 	putint(total.t_sl, PROCSROW + 1, PROCSCOL + 9, 3);
    504 	putint(total.t_sw, PROCSROW + 1, PROCSCOL + 12, 3);
    505 #if !defined(UVM)
    506 	putint(total.t_pw, PROCSROW + 1, PROCSCOL + 15, 3);
    507 #endif
    508 #if defined(UVM)
    509 	PUTRATE(uvmexp.forks, VMSTATROW + 0, VMSTATCOL + 3, 6);
    510 	PUTRATE(uvmexp.forks_ppwait, VMSTATROW + 1, VMSTATCOL + 3, 6);
    511 	PUTRATE(uvmexp.forks_sharevm, VMSTATROW + 2, VMSTATCOL + 3, 6);
    512 	PUTRATE(uvmexp.fltpgwait, VMSTATROW + 3, VMSTATCOL + 4, 5);
    513 	PUTRATE(uvmexp.fltrelck, VMSTATROW + 4, VMSTATCOL + 3, 6);
    514 	PUTRATE(uvmexp.fltrelckok, VMSTATROW + 5, VMSTATCOL + 3, 6);
    515 	PUTRATE(uvmexp.fltnoram, VMSTATROW + 6, VMSTATCOL + 3, 6);
    516 	PUTRATE(uvmexp.fltamcopy, VMSTATROW + 7, VMSTATCOL + 3, 6);
    517 	PUTRATE(uvmexp.flt_prcopy, VMSTATROW + 8, VMSTATCOL + 3, 6);
    518 	PUTRATE(uvmexp.flt_przero, VMSTATROW + 9, VMSTATCOL + 3, 6);
    519 	PUTRATE(uvmexp.flt_acow, VMSTATROW + 10, VMSTATCOL, 9);
    520 	putint(s.uvmexp.freemin, VMSTATROW + 11, VMSTATCOL, 9);
    521 	putint(s.uvmexp.freetarg, VMSTATROW + 12, VMSTATCOL, 9);
    522 	putint(s.uvmexp.inactarg, VMSTATROW + 13, VMSTATCOL, 9);
    523 	putint(s.uvmexp.wired, VMSTATROW + 14, VMSTATCOL, 9);
    524 	PUTRATE(uvmexp.pdfreed, VMSTATROW + 15, VMSTATCOL, 9);
    525 	if (LINES - 1 > VMSTATROW + 16)
    526 		PUTRATE(uvmexp.pdscans, VMSTATROW + 16, VMSTATCOL, 9);
    527 
    528 	PUTRATE(uvmexp.pageins, PAGEROW + 2, PAGECOL + 5, 5);
    529 	PUTRATE(uvmexp.pdpageouts, PAGEROW + 2, PAGECOL + 10, 5);
    530 	PUTRATE(uvmexp.swapins, PAGEROW + 2, PAGECOL + 15, 5);
    531 	PUTRATE(uvmexp.swapouts, PAGEROW + 2, PAGECOL + 20, 5);
    532 	PUTRATE(uvmexp.pgswapin, PAGEROW + 3, PAGECOL + 5, 5);
    533 	PUTRATE(uvmexp.pgswapout, PAGEROW + 3, PAGECOL + 10, 5);
    534 
    535 	PUTRATE(uvmexp.swtch, GENSTATROW + 1, GENSTATCOL, 5);
    536 	PUTRATE(uvmexp.traps, GENSTATROW + 1, GENSTATCOL + 5, 5);
    537 	PUTRATE(uvmexp.syscalls, GENSTATROW + 1, GENSTATCOL + 10, 5);
    538 	PUTRATE(uvmexp.intrs, GENSTATROW + 1, GENSTATCOL + 15, 5);
    539 	PUTRATE(uvmexp.softs, GENSTATROW + 1, GENSTATCOL + 20, 5);
    540 	PUTRATE(uvmexp.faults, GENSTATROW + 1, GENSTATCOL + 25, 5);
    541 #else
    542 	PUTRATE(Cnt.v_cow_faults, VMSTATROW + 0, VMSTATCOL + 3, 6);
    543 	PUTRATE(Cnt.v_lookups, VMSTATROW + 1, VMSTATCOL + 3, 6);
    544 	PUTRATE(Cnt.v_hits, VMSTATROW + 2, VMSTATCOL + 3, 6);
    545 	PUTRATE(Cnt.v_zfod, VMSTATROW + 3, VMSTATCOL + 4, 5);
    546 	PUTRATE(Cnt.v_nzfod, VMSTATROW + 4, VMSTATCOL + 3, 6);
    547 	putfloat(cnt.v_nzfod == 0 ? 0.0 : (100.0 * cnt.v_zfod / cnt.v_nzfod),
    548 		 VMSTATROW + 5, VMSTATCOL + 2, 7, 2, 1);
    549 	putint(pgtokb(cnt.v_kernel_pages), VMSTATROW + 6, VMSTATCOL, 9);
    550 	putint(pgtokb(cnt.v_wire_count), VMSTATROW + 7, VMSTATCOL, 9);
    551 	putint(pgtokb(cnt.v_active_count), VMSTATROW + 8, VMSTATCOL, 9);
    552 	putint(pgtokb(cnt.v_inactive_count), VMSTATROW + 9, VMSTATCOL, 9);
    553 	putint(pgtokb(cnt.v_free_count), VMSTATROW + 10, VMSTATCOL, 9);
    554 	PUTRATE(Cnt.v_dfree, VMSTATROW + 11, VMSTATCOL, 9);
    555 	PUTRATE(Cnt.v_pfree, VMSTATROW + 12, VMSTATCOL, 9);
    556 	PUTRATE(Cnt.v_reactivated, VMSTATROW + 13, VMSTATCOL, 9);
    557 	PUTRATE(Cnt.v_scan, VMSTATROW + 14, VMSTATCOL, 9);
    558 	PUTRATE(Cnt.v_rev, VMSTATROW + 15, VMSTATCOL, 9);
    559 	if (LINES - 1 > VMSTATROW + 16)
    560 		PUTRATE(Cnt.v_intrans, VMSTATROW + 16, VMSTATCOL, 9);
    561 	PUTRATE(Cnt.v_pageins, PAGEROW + 2, PAGECOL + 5, 5);
    562 	PUTRATE(Cnt.v_pageouts, PAGEROW + 2, PAGECOL + 10, 5);
    563 	PUTRATE(Cnt.v_swpin, PAGEROW + 2, PAGECOL + 15, 5);	/* - */
    564 	PUTRATE(Cnt.v_swpout, PAGEROW + 2, PAGECOL + 20, 5);	/* - */
    565 	PUTRATE(Cnt.v_pgpgin, PAGEROW + 3, PAGECOL + 5, 5);	/* ? */
    566 	PUTRATE(Cnt.v_pgpgout, PAGEROW + 3, PAGECOL + 10, 5);	/* ? */
    567 	PUTRATE(Cnt.v_pswpin, PAGEROW + 3, PAGECOL + 15, 5);	/* - */
    568 	PUTRATE(Cnt.v_pswpout, PAGEROW + 3, PAGECOL + 20, 5);	/* - */
    569 	PUTRATE(Cnt.v_swtch, GENSTATROW + 1, GENSTATCOL, 5);
    570 	PUTRATE(Cnt.v_trap, GENSTATROW + 1, GENSTATCOL + 5, 5);
    571 	PUTRATE(Cnt.v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 5);
    572 	PUTRATE(Cnt.v_intr, GENSTATROW + 1, GENSTATCOL + 15, 5);
    573 	PUTRATE(Cnt.v_soft, GENSTATROW + 1, GENSTATCOL + 20, 5);
    574 	PUTRATE(Cnt.v_faults, GENSTATROW + 1, GENSTATCOL + 25, 5);
    575 #endif
    576 	mvprintw(DISKROW, DISKCOL + 5, "                              ");
    577 	for (i = 0, c = 0; i < dk_ndrive && c < MAXDRIVES; i++)
    578 		if (dk_select[i]) {
    579 			mvprintw(DISKROW, DISKCOL + 5 + 5 * c,
    580 				"  %3.3s", dr_name[i]);
    581 			dinfo(i, ++c);
    582 		}
    583 	putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
    584 	putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 9, 9);
    585 #define nz(x)	((x) ? (x) : 1)
    586 	putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount),
    587 	   NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
    588 	putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
    589 	putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
    590 	   NAMEIROW + 2, NAMEICOL + 34, 4, 0, 1);
    591 #undef nz
    592 }
    593 
    594 int
    595 cmdkre(cmd, args)
    596 	char *cmd, *args;
    597 {
    598 
    599 	if (prefix(cmd, "run")) {
    600 		copyinfo(&s2, &s1);
    601 		state = RUN;
    602 		return (1);
    603 	}
    604 	if (prefix(cmd, "boot")) {
    605 		state = BOOT;
    606 		copyinfo(&z, &s1);
    607 		return (1);
    608 	}
    609 	if (prefix(cmd, "time")) {
    610 		state = TIME;
    611 		return (1);
    612 	}
    613 	if (prefix(cmd, "zero")) {
    614 		if (state == RUN)
    615 			getinfo(&s1, RUN);
    616 		return (1);
    617 	}
    618 	return (dkcmd(cmd, args));
    619 }
    620 
    621 /* calculate number of users on the system */
    622 static int
    623 ucount()
    624 {
    625 	int nusers = 0;
    626 
    627 	if (ut < 0)
    628 		return (0);
    629 	while (read(ut, &utmp, sizeof(utmp)))
    630 		if (utmp.ut_name[0] != '\0')
    631 			nusers++;
    632 
    633 	lseek(ut, (off_t)0, SEEK_SET);
    634 	return (nusers);
    635 }
    636 
    637 static float
    638 cputime(indx)
    639 	int indx;
    640 {
    641 	double t;
    642 	int i;
    643 
    644 	t = 0;
    645 	for (i = 0; i < CPUSTATES; i++)
    646 		t += s.time[i];
    647 	if (t == 0.0)
    648 		t = 1.0;
    649 	return (s.time[indx] * 100.0 / t);
    650 }
    651 
    652 static void
    653 putint(n, l, c, w)
    654 	int n, l, c, w;
    655 {
    656 	char b[128];
    657 
    658 	move(l, c);
    659 	if (n == 0) {
    660 		while (w-- > 0)
    661 			addch(' ');
    662 		return;
    663 	}
    664 	sprintf(b, "%*d", w, n);
    665 	if (strlen(b) > w) {
    666 		while (w-- > 0)
    667 			addch('*');
    668 		return;
    669 	}
    670 	addstr(b);
    671 }
    672 
    673 static void
    674 putfloat(f, l, c, w, d, nz)
    675 	double f;
    676 	int l, c, w, d, nz;
    677 {
    678 	char b[128];
    679 
    680 	move(l, c);
    681 	if (nz && f == 0.0) {
    682 		while (--w >= 0)
    683 			addch(' ');
    684 		return;
    685 	}
    686 	sprintf(b, "%*.*f", w, d, f);
    687 	if (strlen(b) > w) {
    688 		while (--w >= 0)
    689 			addch('*');
    690 		return;
    691 	}
    692 	addstr(b);
    693 }
    694 
    695 static void
    696 getinfo(s, st)
    697 	struct Info *s;
    698 	enum state st;
    699 {
    700 	int mib[2];
    701 	size_t size;
    702 	extern int errno;
    703 
    704 	dkreadstats();
    705 	NREAD(X_CPTIME, s->time, sizeof s->time);
    706 	NREAD(X_NCHSTATS, &s->nchstats, sizeof s->nchstats);
    707 	NREAD(X_INTRCNT, s->intrcnt, nintr * LONG);
    708 #if defined(UVM)
    709 	size = sizeof(s->uvmexp);
    710 	mib[0] = CTL_VM;
    711 	mib[1] = VM_UVMEXP;
    712 	if (sysctl(mib, 2, &s->uvmexp, &size, NULL, 0) < 0) {
    713 		error("can't get uvmexp: %s\n", strerror(errno));
    714 		memset(&s->uvmexp, 0, sizeof(s->uvmexp));
    715 	}
    716 #else
    717 	NREAD(X_CNT, &s->Cnt, sizeof s->Cnt);
    718 #endif
    719 	size = sizeof(s->Total);
    720 	mib[0] = CTL_VM;
    721 	mib[1] = VM_METER;
    722 	if (sysctl(mib, 2, &s->Total, &size, NULL, 0) < 0) {
    723 		error("Can't get kernel info: %s\n", strerror(errno));
    724 		memset(&s->Total, 0, sizeof(s->Total));
    725 	}
    726 }
    727 
    728 static void
    729 allocinfo(s)
    730 	struct Info *s;
    731 {
    732 
    733 	s->intrcnt = (long *) malloc(nintr * sizeof(long));
    734 	if (s->intrcnt == NULL)
    735 		errx(2, "out of memory");
    736 }
    737 
    738 static void
    739 copyinfo(from, to)
    740 	struct Info *from, *to;
    741 {
    742 	long *intrcnt;
    743 
    744 	intrcnt = to->intrcnt;
    745 	*to = *from;
    746 	memmove(to->intrcnt = intrcnt, from->intrcnt, nintr * sizeof (int));
    747 }
    748 
    749 static void
    750 dinfo(dn, c)
    751 	int dn, c;
    752 {
    753 	double words, atime;
    754 
    755 	c = DISKCOL + c * 5;
    756 
    757 	/* time busy in disk activity */
    758 	atime = (double)cur.dk_time[dn].tv_sec +
    759 		((double)cur.dk_time[dn].tv_usec / (double)1000000);
    760 
    761 	words = cur.dk_bytes[dn] / 1024.0;	/* # of K transferred */
    762 
    763 	putint((int)((float)cur.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5);
    764 	putint((int)((float)cur.dk_xfer[dn]/etime+0.5), DISKROW + 2, c, 5);
    765 	putint((int)(words/etime + 0.5), DISKROW + 3, c, 5);
    766 	putfloat(atime/etime, DISKROW + 4, c, 5, 1, 1);
    767 }
    768