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