Home | History | Annotate | Line # | Download | only in systat
vmstat.c revision 1.5
      1 /*	$NetBSD: vmstat.c,v 1.5 1996/05/10 23:16:40 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1983, 1989, 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "@(#)vmstat.c	8.2 (Berkeley) 1/12/94";
     39 #endif
     40 static char rcsid[] = "$NetBSD: vmstat.c,v 1.5 1996/05/10 23:16:40 thorpej Exp $";
     41 #endif /* not lint */
     42 
     43 /*
     44  * Cursed vmstat -- from Robert Elz.
     45  */
     46 
     47 #include <sys/param.h>
     48 #include <sys/dkstat.h>
     49 #include <sys/buf.h>
     50 #include <sys/stat.h>
     51 #include <sys/time.h>
     52 #include <sys/user.h>
     53 #include <sys/proc.h>
     54 #include <sys/namei.h>
     55 #include <sys/sysctl.h>
     56 #include <vm/vm.h>
     57 
     58 #include <ctype.h>
     59 #include <err.h>
     60 #include <nlist.h>
     61 #include <paths.h>
     62 #include <signal.h>
     63 #include <stdlib.h>
     64 #include <string.h>
     65 #include <utmp.h>
     66 #include <unistd.h>
     67 
     68 #include "systat.h"
     69 #include "extern.h"
     70 
     71 static struct Info {
     72 	long	time[CPUSTATES];
     73 	struct	vmmeter Cnt;
     74 	struct	vmtotal Total;
     75 	struct	nchstats nchstats;
     76 	long	nchcount;
     77 	long	*intrcnt;
     78 } s, s1, s2, z;
     79 
     80 #include "dkstats.h"
     81 extern struct _disk	cur;
     82 
     83 
     84 #define	cnt s.Cnt
     85 #define oldcnt s1.Cnt
     86 #define	total s.Total
     87 #define	nchtotal s.nchstats
     88 #define	oldnchtotal s1.nchstats
     89 
     90 static	enum state { BOOT, TIME, RUN } state = TIME;
     91 
     92 static void allocinfo __P((struct Info *));
     93 static void copyinfo __P((struct Info *, struct Info *));
     94 static float cputime __P((int));
     95 static void dinfo __P((int, int));
     96 static void getinfo __P((struct Info *, enum state));
     97 static void putint __P((int, int, int, int));
     98 static void putfloat __P((double, int, int, int, int, int));
     99 static int ucount __P((void));
    100 
    101 static	int ut;
    102 static	char buf[26];
    103 static	time_t t;
    104 static	double etime;
    105 static	float hertz;
    106 static	int nintr;
    107 static	long *intrloc;
    108 static	char **intrname;
    109 static	int nextintsrow;
    110 
    111 struct	utmp utmp;
    112 
    113 
    114 WINDOW *
    115 openkre()
    116 {
    117 
    118 	ut = open(_PATH_UTMP, O_RDONLY);
    119 	if (ut < 0)
    120 		error("No utmp");
    121 	return (stdscr);
    122 }
    123 
    124 void
    125 closekre(w)
    126 	WINDOW *w;
    127 {
    128 
    129 	(void) close(ut);
    130 	if (w == NULL)
    131 		return;
    132 	wclear(w);
    133 	wrefresh(w);
    134 }
    135 
    136 
    137 static struct nlist namelist[] = {
    138 #define X_CPTIME	0
    139 	{ "_cp_time" },
    140 #define X_CNT		1
    141 	{ "_cnt" },
    142 #define X_TOTAL		2
    143 	{ "_total" },
    144 #define	X_DK_BUSY	3
    145 	{ "_dk_busy" },
    146 #define	X_DK_TIME	4
    147 	{ "_dk_time" },
    148 #define	X_DK_XFER	5
    149 	{ "_dk_xfer" },
    150 #define	X_DK_WDS	6
    151 	{ "_dk_wds" },
    152 #define	X_DK_SEEK	7
    153 	{ "_dk_seek" },
    154 #define	X_NCHSTATS	8
    155 	{ "_nchstats" },
    156 #define	X_INTRNAMES	9
    157 	{ "_intrnames" },
    158 #define	X_EINTRNAMES	10
    159 	{ "_eintrnames" },
    160 #define	X_INTRCNT	11
    161 	{ "_intrcnt" },
    162 #define	X_EINTRCNT	12
    163 	{ "_eintrcnt" },
    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 	register 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 	mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "objlk");
    298 	mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "objht");
    299 	mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "zfod");
    300 	mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "nzfod");
    301 	mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "%%zfod");
    302 	mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "kern");
    303 	mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "wire");
    304 	mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "act");
    305 	mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "inact");
    306 	mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "free");
    307 	mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "daefr");
    308 	mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "prcfr");
    309 	mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "react");
    310 	mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "scan");
    311 	mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "hdrev");
    312 	if (LINES - 1 > VMSTATROW + 16)
    313 		mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "intrn");
    314 
    315 	mvprintw(GENSTATROW, GENSTATCOL, "  Csw  Trp  Sys  Int  Sof  Flt");
    316 
    317 	mvprintw(GRAPHROW, GRAPHCOL,
    318 		"    . %% Sys    . %% User    . %% Nice    . %% Idle");
    319 	mvprintw(PROCSROW, PROCSCOL, "Proc:r  p  d  s  w");
    320 	mvprintw(GRAPHROW + 1, GRAPHCOL,
    321 		"|    |    |    |    |    |    |    |    |    |    |");
    322 
    323 	mvprintw(NAMEIROW, NAMEICOL, "Namei         Sys-cache     Proc-cache");
    324 	mvprintw(NAMEIROW + 1, NAMEICOL,
    325 		"    Calls     hits    %%     hits     %%");
    326 	mvprintw(DISKROW, DISKCOL, "Discs");
    327 	mvprintw(DISKROW + 1, DISKCOL, "seeks");
    328 	mvprintw(DISKROW + 2, DISKCOL, "xfers");
    329 	mvprintw(DISKROW + 3, DISKCOL, "Kbyte");
    330 	mvprintw(DISKROW + 4, DISKCOL, "  sec");
    331 	j = 0;
    332 	for (i = 0; i < dk_ndrive && j < MAXDRIVES; i++)
    333 		if (dk_select[i]) {
    334 			mvprintw(DISKROW, DISKCOL + 5 + 5 * j,
    335 				"  %3.3s", dr_name[j]);
    336 			j++;
    337 		}
    338 	for (i = 0; i < nintr; i++) {
    339 		if (intrloc[i] == 0)
    340 			continue;
    341 		mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", intrname[i]);
    342 	}
    343 }
    344 
    345 #define X(fld)	{t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
    346 #define Y(fld)	{t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
    347 #define Z(fld)	{t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
    348 	if(state == TIME) s1.nchstats.fld = t;}
    349 #define PUTRATE(fld, l, c, w) \
    350 	Y(fld); \
    351 	putint((int)((float)s.fld/etime + 0.5), l, c, w)
    352 #define MAXFAIL 5
    353 
    354 static	char cpuchar[CPUSTATES] = { '=' , '>', '-', ' ' };
    355 static	char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_IDLE };
    356 
    357 void
    358 showkre()
    359 {
    360 	float f1, f2;
    361 	int psiz, inttotal;
    362 	int i, l, c;
    363 	static int failcnt = 0;
    364 
    365 
    366 	if (state == TIME)
    367 		dkswap();
    368 	etime = 0;
    369 	for(i = 0; i < CPUSTATES; i++) {
    370 		X(time);
    371 		etime += s.time[i];
    372 	}
    373 	if (etime < 5.0) {	/* < 5 ticks - ignore this trash */
    374 		if (failcnt++ >= MAXFAIL) {
    375 			clear();
    376 			mvprintw(2, 10, "The alternate system clock has died!");
    377 			mvprintw(3, 10, "Reverting to ``pigs'' display.");
    378 			move(CMDLINE, 0);
    379 			refresh();
    380 			failcnt = 0;
    381 			sleep(5);
    382 			command("pigs");
    383 		}
    384 		return;
    385 	}
    386 	failcnt = 0;
    387 	etime /= hertz;
    388 	inttotal = 0;
    389 	for (i = 0; i < nintr; i++) {
    390 		if (s.intrcnt[i] == 0)
    391 			continue;
    392 		if (intrloc[i] == 0) {
    393 			if (nextintsrow == LINES)
    394 				continue;
    395 			intrloc[i] = nextintsrow++;
    396 			mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s",
    397 				intrname[i]);
    398 		}
    399 		X(intrcnt);
    400 		l = (int)((float)s.intrcnt[i]/etime + 0.5);
    401 		inttotal += l;
    402 		putint(l, intrloc[i], INTSCOL, 8);
    403 	}
    404 	putint(inttotal, INTSROW + 1, INTSCOL, 8);
    405 	Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
    406 	Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes);
    407 	s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
    408 	    nchtotal.ncs_miss + nchtotal.ncs_long;
    409 	if (state == TIME)
    410 		s1.nchcount = s.nchcount;
    411 
    412 	psiz = 0;
    413 	f2 = 0.0;
    414 
    415 	/*
    416 	 * Last CPU state not calculated yet.
    417 	 */
    418 	for (c = 0; c < CPUSTATES - 1; c++) {
    419 		i = cpuorder[c];
    420 		f1 = cputime(i);
    421 		f2 += f1;
    422 		l = (int) ((f2 + 1.0) / 2.0) - psiz;
    423 		if (c == 0)
    424 			putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0);
    425 		else
    426 			putfloat(f1, GRAPHROW, GRAPHCOL + 12 * c,
    427 				5, 1, 0);
    428 		move(GRAPHROW + 2, psiz);
    429 		psiz += l;
    430 		while (l-- > 0)
    431 			addch(cpuchar[c]);
    432 	}
    433 
    434 	putint(ucount(), STATROW, STATCOL, 3);
    435 	putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
    436 	putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
    437 	putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
    438 	mvaddstr(STATROW, STATCOL + 53, buf);
    439 #define pgtokb(pg)	((pg) * cnt.v_page_size / 1024)
    440 	putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 3, 6);
    441 	putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 9, 6);
    442 	putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 15, 7);
    443 	putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 22, 7);
    444 	putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 3, 6);
    445 	putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 9, 6);
    446 	putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 15, 7);
    447 	putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 22, 7);
    448 	putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 29, 6);
    449 	putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
    450 	putint(total.t_pw, PROCSROW + 1, PROCSCOL + 6, 3);
    451 	putint(total.t_dw, PROCSROW + 1, PROCSCOL + 9, 3);
    452 	putint(total.t_sl, PROCSROW + 1, PROCSCOL + 12, 3);
    453 	putint(total.t_sw, PROCSROW + 1, PROCSCOL + 15, 3);
    454 	PUTRATE(Cnt.v_cow_faults, VMSTATROW + 0, VMSTATCOL + 3, 6);
    455 	PUTRATE(Cnt.v_lookups, VMSTATROW + 1, VMSTATCOL + 3, 6);
    456 	PUTRATE(Cnt.v_hits, VMSTATROW + 2, VMSTATCOL + 3, 6);
    457 	PUTRATE(Cnt.v_zfod, VMSTATROW + 3, VMSTATCOL + 4, 5);
    458 	PUTRATE(Cnt.v_nzfod, VMSTATROW + 4, VMSTATCOL + 3, 6);
    459 	putfloat(cnt.v_nzfod == 0 ? 0.0 : (100.0 * cnt.v_zfod / cnt.v_nzfod),
    460 		 VMSTATROW + 5, VMSTATCOL + 2, 7, 2, 1);
    461 	putint(pgtokb(cnt.v_kernel_pages), VMSTATROW + 6, VMSTATCOL, 9);
    462 	putint(pgtokb(cnt.v_wire_count), VMSTATROW + 7, VMSTATCOL, 9);
    463 	putint(pgtokb(cnt.v_active_count), VMSTATROW + 8, VMSTATCOL, 9);
    464 	putint(pgtokb(cnt.v_inactive_count), VMSTATROW + 9, VMSTATCOL, 9);
    465 	putint(pgtokb(cnt.v_free_count), VMSTATROW + 10, VMSTATCOL, 9);
    466 	PUTRATE(Cnt.v_dfree, VMSTATROW + 11, VMSTATCOL, 9);
    467 	PUTRATE(Cnt.v_pfree, VMSTATROW + 12, VMSTATCOL, 9);
    468 	PUTRATE(Cnt.v_reactivated, VMSTATROW + 13, VMSTATCOL, 9);
    469 	PUTRATE(Cnt.v_scan, VMSTATROW + 14, VMSTATCOL, 9);
    470 	PUTRATE(Cnt.v_rev, VMSTATROW + 15, VMSTATCOL, 9);
    471 	if (LINES - 1 > VMSTATROW + 16)
    472 		PUTRATE(Cnt.v_intrans, VMSTATROW + 16, VMSTATCOL, 9);
    473 	PUTRATE(Cnt.v_pageins, PAGEROW + 2, PAGECOL + 5, 5);
    474 	PUTRATE(Cnt.v_pageouts, PAGEROW + 2, PAGECOL + 10, 5);
    475 	PUTRATE(Cnt.v_swpin, PAGEROW + 2, PAGECOL + 15, 5);	/* - */
    476 	PUTRATE(Cnt.v_swpout, PAGEROW + 2, PAGECOL + 20, 5);	/* - */
    477 	PUTRATE(Cnt.v_pgpgin, PAGEROW + 3, PAGECOL + 5, 5);	/* ? */
    478 	PUTRATE(Cnt.v_pgpgout, PAGEROW + 3, PAGECOL + 10, 5);	/* ? */
    479 	PUTRATE(Cnt.v_pswpin, PAGEROW + 3, PAGECOL + 15, 5);	/* - */
    480 	PUTRATE(Cnt.v_pswpout, PAGEROW + 3, PAGECOL + 20, 5);	/* - */
    481 	PUTRATE(Cnt.v_swtch, GENSTATROW + 1, GENSTATCOL, 5);
    482 	PUTRATE(Cnt.v_trap, GENSTATROW + 1, GENSTATCOL + 5, 5);
    483 	PUTRATE(Cnt.v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 5);
    484 	PUTRATE(Cnt.v_intr, GENSTATROW + 1, GENSTATCOL + 15, 5);
    485 	PUTRATE(Cnt.v_soft, GENSTATROW + 1, GENSTATCOL + 20, 5);
    486 	PUTRATE(Cnt.v_faults, GENSTATROW + 1, GENSTATCOL + 25, 5);
    487 	mvprintw(DISKROW, DISKCOL + 5, "                              ");
    488 	for (i = 0, c = 0; i < dk_ndrive && c < MAXDRIVES; i++)
    489 		if (dk_select[i]) {
    490 			mvprintw(DISKROW, DISKCOL + 5 + 5 * c,
    491 				"  %3.3s", dr_name[i]);
    492 			dinfo(i, ++c);
    493 		}
    494 	putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
    495 	putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 9, 9);
    496 #define nz(x)	((x) ? (x) : 1)
    497 	putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount),
    498 	   NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
    499 	putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
    500 	putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
    501 	   NAMEIROW + 2, NAMEICOL + 34, 4, 0, 1);
    502 #undef nz
    503 }
    504 
    505 int
    506 cmdkre(cmd, args)
    507 	char *cmd, *args;
    508 {
    509 
    510 	if (prefix(cmd, "run")) {
    511 		copyinfo(&s2, &s1);
    512 		state = RUN;
    513 		return (1);
    514 	}
    515 	if (prefix(cmd, "boot")) {
    516 		state = BOOT;
    517 		copyinfo(&z, &s1);
    518 		return (1);
    519 	}
    520 	if (prefix(cmd, "time")) {
    521 		state = TIME;
    522 		return (1);
    523 	}
    524 	if (prefix(cmd, "zero")) {
    525 		if (state == RUN)
    526 			getinfo(&s1, RUN);
    527 		return (1);
    528 	}
    529 	return (dkcmd(cmd, args));
    530 }
    531 
    532 /* calculate number of users on the system */
    533 static int
    534 ucount()
    535 {
    536 	register int nusers = 0;
    537 
    538 	if (ut < 0)
    539 		return (0);
    540 	while (read(ut, &utmp, sizeof(utmp)))
    541 		if (utmp.ut_name[0] != '\0')
    542 			nusers++;
    543 
    544 	lseek(ut, 0L, L_SET);
    545 	return (nusers);
    546 }
    547 
    548 static float
    549 cputime(indx)
    550 	int indx;
    551 {
    552 	double t;
    553 	register int i;
    554 
    555 	t = 0;
    556 	for (i = 0; i < CPUSTATES; i++)
    557 		t += s.time[i];
    558 	if (t == 0.0)
    559 		t = 1.0;
    560 	return (s.time[indx] * 100.0 / t);
    561 }
    562 
    563 static void
    564 putint(n, l, c, w)
    565 	int n, l, c, w;
    566 {
    567 	char b[128];
    568 
    569 	move(l, c);
    570 	if (n == 0) {
    571 		while (w-- > 0)
    572 			addch(' ');
    573 		return;
    574 	}
    575 	sprintf(b, "%*d", w, n);
    576 	if (strlen(b) > w) {
    577 		while (w-- > 0)
    578 			addch('*');
    579 		return;
    580 	}
    581 	addstr(b);
    582 }
    583 
    584 static void
    585 putfloat(f, l, c, w, d, nz)
    586 	double f;
    587 	int l, c, w, d, nz;
    588 {
    589 	char b[128];
    590 
    591 	move(l, c);
    592 	if (nz && f == 0.0) {
    593 		while (--w >= 0)
    594 			addch(' ');
    595 		return;
    596 	}
    597 	sprintf(b, "%*.*f", w, d, f);
    598 	if (strlen(b) > w) {
    599 		while (--w >= 0)
    600 			addch('*');
    601 		return;
    602 	}
    603 	addstr(b);
    604 }
    605 
    606 static void
    607 getinfo(s, st)
    608 	struct Info *s;
    609 	enum state st;
    610 {
    611 	int mib[2];
    612 	size_t size;
    613 	extern int errno;
    614 
    615 	dkreadstats();
    616 	NREAD(X_CPTIME, s->time, sizeof s->time);
    617 	NREAD(X_CNT, &s->Cnt, sizeof s->Cnt);
    618 	NREAD(X_NCHSTATS, &s->nchstats, sizeof s->nchstats);
    619 	NREAD(X_INTRCNT, s->intrcnt, nintr * LONG);
    620 	size = sizeof(s->Total);
    621 	mib[0] = CTL_VM;
    622 	mib[1] = VM_METER;
    623 	if (sysctl(mib, 2, &s->Total, &size, NULL, 0) < 0) {
    624 		error("Can't get kernel info: %s\n", strerror(errno));
    625 		bzero(&s->Total, sizeof(s->Total));
    626 	}
    627 }
    628 
    629 static void
    630 allocinfo(s)
    631 	struct Info *s;
    632 {
    633 
    634 	s->intrcnt = (long *) malloc(nintr * sizeof(long));
    635 	if (s->intrcnt == NULL)
    636 		errx(2, "out of memory");
    637 }
    638 
    639 static void
    640 copyinfo(from, to)
    641 	register struct Info *from, *to;
    642 {
    643 	long *intrcnt;
    644 
    645 	intrcnt = to->intrcnt;
    646 	*to = *from;
    647 	bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
    648 }
    649 
    650 static void
    651 dinfo(dn, c)
    652 	int dn, c;
    653 {
    654 	double words, atime;
    655 
    656 	c = DISKCOL + c * 5;
    657 
    658 	/* time busy in disk activity */
    659 	atime = (double)cur.dk_time[dn].tv_sec +
    660 		((double)cur.dk_time[dn].tv_usec / (double)1000000);
    661 
    662 	words = cur.dk_bytes[dn] / 1024.0;	/* # of K transferred */
    663 
    664 	putint((int)((float)cur.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5);
    665 	putint((int)((float)cur.dk_xfer[dn]/etime+0.5), DISKROW + 2, c, 5);
    666 	putint((int)(words/etime + 0.5), DISKROW + 3, c, 5);
    667 	putfloat(atime/etime, DISKROW + 4, c, 5, 1, 1);
    668 }
    669