Home | History | Annotate | Line # | Download | only in systat
vmstat.c revision 1.17
      1 /*	$NetBSD: vmstat.c,v 1.17 1998/12/19 14:34:40 drochner 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.17 1998/12/19 14:34:40 drochner 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 	extern gid_t egid;
    206 
    207 	if (namelist[0].n_type == 0) {
    208 		if (kvm_nlist(kd, namelist)) {
    209 			nlisterr(namelist);
    210 			return(0);
    211 		}
    212 		if (namelist[0].n_type == 0) {
    213 			error("No namelist");
    214 			return(0);
    215 		}
    216 	}
    217 	hertz = stathz ? stathz : hz;
    218 	if (! dkinit(1, egid))
    219 		return(0);
    220 	if (dk_ndrive && !once) {
    221 #define	allocate(e, t) \
    222 	s./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
    223 	s1./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
    224 	s2./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
    225 	z./**/e = (t *)calloc(dk_ndrive, sizeof (t));
    226 		once = 1;
    227 #undef allocate
    228 	}
    229 	if (nintr == 0) {
    230 		nintr = (namelist[X_EINTRCNT].n_value -
    231 			namelist[X_INTRCNT].n_value) / sizeof (long);
    232 		intrloc = calloc(nintr, sizeof (long));
    233 		intrname = calloc(nintr, sizeof (long));
    234 		intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value -
    235 			namelist[X_INTRNAMES].n_value);
    236 		if (intrnamebuf == 0 || intrname == 0 || intrloc == 0) {
    237 			error("Out of memory\n");
    238 			if (intrnamebuf)
    239 				free(intrnamebuf);
    240 			if (intrname)
    241 				free(intrname);
    242 			if (intrloc)
    243 				free(intrloc);
    244 			nintr = 0;
    245 			return(0);
    246 		}
    247 		NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) -
    248 			NVAL(X_INTRNAMES));
    249 		for (cp = intrnamebuf, i = 0; i < nintr; i++) {
    250 			intrname[i] = cp;
    251 			cp += strlen(cp) + 1;
    252 		}
    253 		nextintsrow = INTSROW + 2;
    254 		allocinfo(&s);
    255 		allocinfo(&s1);
    256 		allocinfo(&s2);
    257 		allocinfo(&z);
    258 	}
    259 	getinfo(&s2, RUN);
    260 	copyinfo(&s2, &s1);
    261 	return(1);
    262 }
    263 
    264 void
    265 fetchkre()
    266 {
    267 	time_t now;
    268 
    269 	time(&now);
    270 	strcpy(buf, ctime(&now));
    271 	buf[16] = '\0';
    272 	getinfo(&s, state);
    273 }
    274 
    275 void
    276 labelkre()
    277 {
    278 	int i, j;
    279 
    280 	clear();
    281 	mvprintw(STATROW, STATCOL + 4, "users    Load");
    282 #if defined(UVM)
    283 	mvprintw(MEMROW, MEMCOL,     "          memory totals (in KB)");
    284 	mvprintw(MEMROW + 1, MEMCOL, "         real   virtual    free");
    285 	mvprintw(MEMROW + 2, MEMCOL, "Active");
    286 	mvprintw(MEMROW + 3, MEMCOL, "All");
    287 
    288 	mvprintw(PAGEROW, PAGECOL, "        PAGING   SWAPPING ");
    289 	mvprintw(PAGEROW + 1, PAGECOL, "        in  out   in  out ");
    290 	mvprintw(PAGEROW + 2, PAGECOL, "ops");
    291 	mvprintw(PAGEROW + 3, PAGECOL, "pages");
    292 #else
    293 	mvprintw(MEMROW, MEMCOL, "Mem:KB  REAL        VIRTUAL");
    294 	mvprintw(MEMROW + 1, MEMCOL, "      Tot Share    Tot  Share");
    295 	mvprintw(MEMROW + 2, MEMCOL, "Act");
    296 	mvprintw(MEMROW + 3, MEMCOL, "All");
    297 
    298 	mvprintw(MEMROW + 1, MEMCOL + 31, "Free");
    299 
    300 	mvprintw(PAGEROW, PAGECOL,     "        PAGING   SWAPPING ");
    301 	mvprintw(PAGEROW + 1, PAGECOL, "        in  out   in  out ");
    302 	mvprintw(PAGEROW + 2, PAGECOL, "count");
    303 	mvprintw(PAGEROW + 3, PAGECOL, "pages");
    304 #endif
    305 
    306 	mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
    307 	mvprintw(INTSROW + 1, INTSCOL + 9, "total");
    308 
    309 #if defined(UVM)
    310 	mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "forks");
    311 	mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "fkppw");
    312 	mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "fksvm");
    313 	mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "pwait");
    314 	mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "relck");
    315 	mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "rlkok");
    316 	mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "noram");
    317 	mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "ndcpy");
    318 	mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "fltcp");
    319 	mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "zfod");
    320 	mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "cow");
    321 	mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "fmin");
    322 	mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "ftarg");
    323 	mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "itarg");
    324 	mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "wired");
    325 	mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "pdfre");
    326 	if (LINES - 1 > VMSTATROW + 16)
    327 		mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "pdscn");
    328 #else
    329 	mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "cow");
    330 	mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "objlk");
    331 	mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "objht");
    332 	mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "zfod");
    333 	mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "nzfod");
    334 	mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "%%zfod");
    335 	mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "kern");
    336 	mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "wire");
    337 	mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "act");
    338 	mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "inact");
    339 	mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "free");
    340 	mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "daefr");
    341 	mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "prcfr");
    342 	mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "react");
    343 	mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "scan");
    344 	mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "hdrev");
    345 	if (LINES - 1 > VMSTATROW + 16)
    346 		mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "intrn");
    347 #endif
    348 
    349 	mvprintw(GENSTATROW, GENSTATCOL, "  Csw  Trp  Sys  Int  Sof  Flt");
    350 
    351 	mvprintw(GRAPHROW, GRAPHCOL,
    352 		"    . %% Sys    . %% User    . %% Nice    . %% Idle");
    353 #if defined(UVM)
    354 	mvprintw(PROCSROW, PROCSCOL, "Proc:r  d  s  w");
    355 #else
    356 	mvprintw(PROCSROW, PROCSCOL, "Proc:r  d  s  w  p");
    357 #endif
    358 	mvprintw(GRAPHROW + 1, GRAPHCOL,
    359 		"|    |    |    |    |    |    |    |    |    |    |");
    360 
    361 	mvprintw(NAMEIROW, NAMEICOL, "Namei         Sys-cache     Proc-cache");
    362 	mvprintw(NAMEIROW + 1, NAMEICOL,
    363 		"    Calls     hits    %%     hits     %%");
    364 	mvprintw(DISKROW, DISKCOL, "Discs");
    365 	mvprintw(DISKROW + 1, DISKCOL, "seeks");
    366 	mvprintw(DISKROW + 2, DISKCOL, "xfers");
    367 	mvprintw(DISKROW + 3, DISKCOL, "Kbyte");
    368 	mvprintw(DISKROW + 4, DISKCOL, "  sec");
    369 	j = 0;
    370 	for (i = 0; i < dk_ndrive && j < MAXDRIVES; i++)
    371 		if (dk_select[i]) {
    372 			mvprintw(DISKROW, DISKCOL + 5 + 5 * j,
    373 				"  %3.3s", dr_name[j]);
    374 			j++;
    375 		}
    376 	for (i = 0; i < nintr; i++) {
    377 		if (intrloc[i] == 0)
    378 			continue;
    379 		mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", intrname[i]);
    380 	}
    381 }
    382 
    383 #define X(fld)	{t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
    384 #define Y(fld)	{t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
    385 #define Z(fld)	{t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
    386 	if(state == TIME) s1.nchstats.fld = t;}
    387 #define PUTRATE(fld, l, c, w) {Y(fld); putint((int)((float)s.fld/etime + 0.5), l, c, w);}
    388 #define MAXFAIL 5
    389 
    390 static	char cpuchar[CPUSTATES] = { '=' , '>', '-', ' ' };
    391 static	char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_IDLE };
    392 
    393 void
    394 showkre()
    395 {
    396 	float f1, f2;
    397 	int psiz, inttotal;
    398 	int i, l, c;
    399 	static int failcnt = 0;
    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 
    562 	PUTRATE(Cnt.v_pageins, PAGEROW + 2, PAGECOL + 5, 5);
    563 	PUTRATE(Cnt.v_pageouts, PAGEROW + 2, PAGECOL + 10, 5);
    564 	PUTRATE(Cnt.v_swpin, PAGEROW + 2, PAGECOL + 15, 5);	/* - */
    565 	PUTRATE(Cnt.v_swpout, PAGEROW + 2, PAGECOL + 20, 5);	/* - */
    566 	PUTRATE(Cnt.v_pgpgin, PAGEROW + 3, PAGECOL + 5, 5);	/* ? */
    567 	PUTRATE(Cnt.v_pgpgout, PAGEROW + 3, PAGECOL + 10, 5);	/* ? */
    568 	PUTRATE(Cnt.v_pswpin, PAGEROW + 3, PAGECOL + 15, 5);	/* - */
    569 	PUTRATE(Cnt.v_pswpout, PAGEROW + 3, PAGECOL + 20, 5);	/* - */
    570 
    571 	PUTRATE(Cnt.v_swtch, GENSTATROW + 1, GENSTATCOL, 5);
    572 	PUTRATE(Cnt.v_trap, GENSTATROW + 1, GENSTATCOL + 5, 5);
    573 	PUTRATE(Cnt.v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 5);
    574 	PUTRATE(Cnt.v_intr, GENSTATROW + 1, GENSTATCOL + 15, 5);
    575 	PUTRATE(Cnt.v_soft, GENSTATROW + 1, GENSTATCOL + 20, 5);
    576 	PUTRATE(Cnt.v_faults, GENSTATROW + 1, GENSTATCOL + 25, 5);
    577 #endif
    578 	mvprintw(DISKROW, DISKCOL + 5, "                              ");
    579 	for (i = 0, c = 0; i < dk_ndrive && c < MAXDRIVES; i++)
    580 		if (dk_select[i]) {
    581 			mvprintw(DISKROW, DISKCOL + 5 + 5 * c,
    582 				"  %3.3s", dr_name[i]);
    583 			dinfo(i, ++c);
    584 		}
    585 	putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
    586 	putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 9, 9);
    587 #define nz(x)	((x) ? (x) : 1)
    588 	putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount),
    589 	   NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
    590 	putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
    591 	putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
    592 	   NAMEIROW + 2, NAMEICOL + 34, 4, 0, 1);
    593 #undef nz
    594 }
    595 
    596 int
    597 cmdkre(cmd, args)
    598 	char *cmd, *args;
    599 {
    600 
    601 	if (prefix(cmd, "run")) {
    602 		copyinfo(&s2, &s1);
    603 		state = RUN;
    604 		return (1);
    605 	}
    606 	if (prefix(cmd, "boot")) {
    607 		state = BOOT;
    608 		copyinfo(&z, &s1);
    609 		return (1);
    610 	}
    611 	if (prefix(cmd, "time")) {
    612 		state = TIME;
    613 		return (1);
    614 	}
    615 	if (prefix(cmd, "zero")) {
    616 		if (state == RUN)
    617 			getinfo(&s1, RUN);
    618 		return (1);
    619 	}
    620 	return (dkcmd(cmd, args));
    621 }
    622 
    623 /* calculate number of users on the system */
    624 static int
    625 ucount()
    626 {
    627 	int nusers = 0;
    628 
    629 	if (ut < 0)
    630 		return (0);
    631 	while (read(ut, &utmp, sizeof(utmp)))
    632 		if (utmp.ut_name[0] != '\0')
    633 			nusers++;
    634 
    635 	lseek(ut, (off_t)0, SEEK_SET);
    636 	return (nusers);
    637 }
    638 
    639 static float
    640 cputime(indx)
    641 	int indx;
    642 {
    643 	double t;
    644 	int i;
    645 
    646 	t = 0;
    647 	for (i = 0; i < CPUSTATES; i++)
    648 		t += s.time[i];
    649 	if (t == 0.0)
    650 		t = 1.0;
    651 	return (s.time[indx] * 100.0 / t);
    652 }
    653 
    654 static void
    655 putint(n, l, c, w)
    656 	int n, l, c, w;
    657 {
    658 	char b[128];
    659 
    660 	move(l, c);
    661 	if (n == 0) {
    662 		while (w-- > 0)
    663 			addch(' ');
    664 		return;
    665 	}
    666 	(void)snprintf(b, sizeof b, "%*d", w, n);
    667 	if (strlen(b) > w) {
    668 		while (w-- > 0)
    669 			addch('*');
    670 		return;
    671 	}
    672 	addstr(b);
    673 }
    674 
    675 static void
    676 putfloat(f, l, c, w, d, nz)
    677 	double f;
    678 	int l, c, w, d, nz;
    679 {
    680 	char b[128];
    681 
    682 	move(l, c);
    683 	if (nz && f == 0.0) {
    684 		while (--w >= 0)
    685 			addch(' ');
    686 		return;
    687 	}
    688 	(void)snprintf(b, sizeof b, "%*.*f", w, d, f);
    689 	if (strlen(b) > w) {
    690 		while (--w >= 0)
    691 			addch('*');
    692 		return;
    693 	}
    694 	addstr(b);
    695 }
    696 
    697 static void
    698 getinfo(s, st)
    699 	struct Info *s;
    700 	enum state st;
    701 {
    702 	int mib[2];
    703 	size_t size;
    704 	extern int errno;
    705 
    706 	dkreadstats();
    707 	NREAD(X_CPTIME, s->time, sizeof s->time);
    708 	NREAD(X_NCHSTATS, &s->nchstats, sizeof s->nchstats);
    709 	NREAD(X_INTRCNT, s->intrcnt, nintr * LONG);
    710 #if defined(UVM)
    711 	size = sizeof(s->uvmexp);
    712 	mib[0] = CTL_VM;
    713 	mib[1] = VM_UVMEXP;
    714 	if (sysctl(mib, 2, &s->uvmexp, &size, NULL, 0) < 0) {
    715 		error("can't get uvmexp: %s\n", strerror(errno));
    716 		memset(&s->uvmexp, 0, sizeof(s->uvmexp));
    717 	}
    718 #else
    719 	NREAD(X_CNT, &s->Cnt, sizeof s->Cnt);
    720 #endif
    721 	size = sizeof(s->Total);
    722 	mib[0] = CTL_VM;
    723 	mib[1] = VM_METER;
    724 	if (sysctl(mib, 2, &s->Total, &size, NULL, 0) < 0) {
    725 		error("Can't get kernel info: %s\n", strerror(errno));
    726 		memset(&s->Total, 0, sizeof(s->Total));
    727 	}
    728 }
    729 
    730 static void
    731 allocinfo(s)
    732 	struct Info *s;
    733 {
    734 
    735 	s->intrcnt = (long *) malloc(nintr * sizeof(long));
    736 	if (s->intrcnt == NULL)
    737 		errx(2, "out of memory");
    738 }
    739 
    740 static void
    741 copyinfo(from, to)
    742 	struct Info *from, *to;
    743 {
    744 	long *intrcnt;
    745 
    746 	intrcnt = to->intrcnt;
    747 	*to = *from;
    748 	memmove(to->intrcnt = intrcnt, from->intrcnt, nintr * sizeof (int));
    749 }
    750 
    751 static void
    752 dinfo(dn, c)
    753 	int dn, c;
    754 {
    755 	double words, atime;
    756 
    757 	c = DISKCOL + c * 5;
    758 
    759 	/* time busy in disk activity */
    760 	atime = (double)cur.dk_time[dn].tv_sec +
    761 		((double)cur.dk_time[dn].tv_usec / (double)1000000);
    762 
    763 	words = cur.dk_bytes[dn] / 1024.0;	/* # of K transferred */
    764 
    765 	putint((int)((float)cur.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5);
    766 	putint((int)((float)cur.dk_xfer[dn]/etime+0.5), DISKROW + 2, c, 5);
    767 	putint((int)(words/etime + 0.5), DISKROW + 3, c, 5);
    768 	putfloat(atime/etime, DISKROW + 4, c, 5, 1, 1);
    769 }
    770