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