xref: /openbsd/usr.bin/systat/vmstat.c (revision db3296cf)
1 /*	$OpenBSD: vmstat.c,v 1.41 2003/06/03 02:56:17 millert Exp $	*/
2 /*	$NetBSD: vmstat.c,v 1.5 1996/05/10 23:16:40 thorpej Exp $	*/
3 
4 /*-
5  * Copyright (c) 1983, 1989, 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)vmstat.c	8.2 (Berkeley) 1/12/94";
36 #endif
37 static char rcsid[] = "$OpenBSD: vmstat.c,v 1.41 2003/06/03 02:56:17 millert Exp $";
38 #endif /* not lint */
39 
40 /*
41  * Cursed vmstat -- from Robert Elz.
42  */
43 
44 #include <sys/param.h>
45 #include <sys/dkstat.h>
46 #include <sys/buf.h>
47 #include <sys/stat.h>
48 #include <sys/time.h>
49 #include <sys/user.h>
50 #include <sys/proc.h>
51 #include <sys/namei.h>
52 #include <sys/sysctl.h>
53 
54 #include <uvm/uvm_extern.h>
55 
56 #include <ctype.h>
57 #include <err.h>
58 #include <nlist.h>
59 #include <paths.h>
60 #include <signal.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <utmp.h>
64 #include <unistd.h>
65 
66 #if defined(__i386__)
67 #define	_KERNEL
68 #include <machine/psl.h>
69 #undef _KERNEL
70 #endif
71 
72 #include "systat.h"
73 #include "extern.h"
74 
75 static struct Info {
76 	long	time[CPUSTATES];
77 	struct	uvmexp uvmexp;
78 	struct	vmtotal Total;
79 	struct	nchstats nchstats;
80 	long	nchcount;
81 	int	*intrcnt;
82 } s, s1, s2, z;
83 
84 #include "dkstats.h"
85 extern struct _disk	cur;
86 
87 #define	cnt s.Cnt
88 #define oldcnt s1.Cnt
89 #define	total s.Total
90 #define	nchtotal s.nchstats
91 #define	oldnchtotal s1.nchstats
92 
93 static	enum state { BOOT, TIME, RUN } state = TIME;
94 
95 static void allocinfo(struct Info *);
96 static void copyinfo(struct Info *, struct Info *);
97 static float cputime(int);
98 static void dinfo(int, int);
99 static void getinfo(struct Info *, enum state);
100 static void putint(int, int, int, int);
101 static void putfloat(double, int, int, int, int, int);
102 static int ucount(void);
103 
104 static	int ut;
105 static	char buf[26];
106 static	time_t t;
107 static	double etime;
108 static	float hertz;
109 static	int nintr;
110 static	long *intrloc;
111 static	char **intrname;
112 static	int nextintsrow;
113 
114 struct	utmp utmp;
115 
116 WINDOW *
117 openkre(void)
118 {
119 
120 	ut = open(_PATH_UTMP, O_RDONLY);
121 	if (ut < 0)
122 		error("No utmp");
123 	return (stdscr);
124 }
125 
126 void
127 closekre(WINDOW *w)
128 {
129 
130 	(void) close(ut);
131 	if (w == NULL)
132 		return;
133 	wclear(w);
134 	wrefresh(w);
135 }
136 
137 
138 static struct nlist namelist[] = {
139 #define	X_INTRNAMES	0		/* no sysctl */
140 	{ "_intrnames" },
141 #define	X_EINTRNAMES	1		/* no sysctl */
142 	{ "_eintrnames" },
143 #define	X_INTRCNT	2		/* no sysctl */
144 	{ "_intrcnt" },
145 #define	X_EINTRCNT	3		/* no sysctl */
146 	{ "_eintrcnt" },
147 #if defined(__i386__)
148 #define	X_INTRHAND	4		/* no sysctl */
149 	{ "_intrhand" },
150 #endif
151 	{ "" },
152 };
153 
154 /*
155  * These constants define where the major pieces are laid out
156  */
157 #define STATROW		 0	/* uses 1 row and 68 cols */
158 #define STATCOL		 2
159 #define MEMROW		 2	/* uses 4 rows and 31 cols */
160 #define MEMCOL		 0
161 #define PAGEROW		 2	/* uses 4 rows and 26 cols */
162 #define PAGECOL		37
163 #define INTSROW		 2	/* uses all rows to bottom and 17 cols */
164 #define INTSCOL		63
165 #define PROCSROW	 7	/* uses 2 rows and 20 cols */
166 #define PROCSCOL	 0
167 #define GENSTATROW	 7	/* uses 2 rows and 35 cols */
168 #define GENSTATCOL	16
169 #define VMSTATROW	 7	/* uses 17 rows and 12 cols */
170 #define VMSTATCOL	48
171 #define GRAPHROW	10	/* uses 3 rows and 51 cols */
172 #define GRAPHCOL	 0
173 #define NAMEIROW	14	/* uses 3 rows and 49 cols */
174 #define NAMEICOL	 0
175 #define DISKROW		18	/* uses 5 rows and 50 cols (for 9 drives) */
176 #define DISKCOL		 0
177 
178 #define	DRIVESPACE	45	/* max space for drives */
179 
180 int
181 initkre(void)
182 {
183 	char *intrnamebuf, *cp;
184 	int i, ret;
185 
186 	if (namelist[0].n_type == 0) {
187 		if ((ret = kvm_nlist(kd, namelist)) == -1)
188 			errx(1, "%s", kvm_geterr(kd));
189 		else if (ret)
190 			nlisterr(namelist);
191 		if (namelist[0].n_type == 0) {
192 			error("No namelist");
193 			return(0);
194 		}
195 	}
196 	hertz = stathz ? stathz : hz;
197 	if (!dkinit(1))
198 		return(0);
199 	if (nintr == 0) {
200 #if defined(__i386__)
201 		struct intrhand *intrhand[16], *ihp, ih;
202 		char iname[16];
203 		int namelen, n;
204 
205 		NREAD(X_INTRHAND, intrhand, sizeof(intrhand));
206 		for (namelen = 0, i = 0; i < 16; i++) {
207 			ihp = intrhand[i];
208 			while (ihp) {
209 				nintr++;
210 				KREAD(ihp, &ih, sizeof(ih));
211 				KREAD(ih.ih_what, iname, 16);
212 				namelen += strlen(iname) + 1;
213 				ihp = ih.ih_next;
214 			}
215 		}
216 		intrloc = calloc(nintr, sizeof (long));
217 		intrname = calloc(nintr, sizeof (char *));
218 		cp = intrnamebuf = malloc(namelen);
219 		for (i = 0, n = 0; i < 16; i++) {
220 			ihp = intrhand[i];
221 			while (ihp) {
222 				KREAD(ihp, &ih, sizeof(ih));
223 				KREAD(ih.ih_what, iname, 16);
224 				intrname[n++] = cp;
225 				strlcpy(cp, iname, intrnamebuf + namelen - cp);
226 				cp += strlen(iname) + 1;
227 				ihp = ih.ih_next;
228 			}
229 		}
230 #else
231 		nintr = (namelist[X_EINTRCNT].n_value -
232 		    namelist[X_INTRCNT].n_value) / sizeof (int);
233 		intrloc = calloc(nintr, sizeof (long));
234 		intrname = calloc(nintr, sizeof (long));
235 		intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value -
236 		    namelist[X_INTRNAMES].n_value);
237 		if (intrnamebuf == 0 || intrname == 0 || intrloc == 0) {
238 			error("Out of memory\n");
239 			if (intrnamebuf)
240 				free(intrnamebuf);
241 			if (intrname)
242 				free(intrname);
243 			if (intrloc)
244 				free(intrloc);
245 			nintr = 0;
246 			return(0);
247 		}
248 		NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) -
249 		    NVAL(X_INTRNAMES));
250 		for (cp = intrnamebuf, i = 0; i < nintr; i++) {
251 			intrname[i] = cp;
252 			cp += strlen(cp) + 1;
253 		}
254 #endif
255 		nextintsrow = INTSROW + 2;
256 		allocinfo(&s);
257 		allocinfo(&s1);
258 		allocinfo(&s2);
259 		allocinfo(&z);
260 	}
261 	getinfo(&s2, RUN);
262 	copyinfo(&s2, &s1);
263 	return(1);
264 }
265 
266 void
267 fetchkre(void)
268 {
269 	time_t now;
270 
271 	time(&now);
272 	strlcpy(buf, ctime(&now), sizeof buf);
273 	getinfo(&s, state);
274 }
275 
276 void
277 labelkre(void)
278 {
279 	int i, j, l;
280 
281 	clear();
282 	mvprintw(STATROW, STATCOL + 4, "users    Load");
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 
293 	mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
294 	mvprintw(INTSROW + 1, INTSCOL + 9, "total");
295 
296 	mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "forks");
297 	mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "fkppw");
298 	mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "fksvm");
299 	mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "pwait");
300 	mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "relck");
301 	mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "rlkok");
302 	mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "noram");
303 	mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "ndcpy");
304 	mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "fltcp");
305 	mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "zfod");
306 	mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "cow");
307 	mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "fmin");
308 	mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "ftarg");
309 	mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "itarg");
310 	mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "wired");
311 	mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "pdfre");
312 	if (LINES - 1 > VMSTATROW + 16)
313 		mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "pdscn");
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  d  s  w");
320 	mvprintw(GRAPHROW + 1, GRAPHCOL,
321 	    "|    |    |    |    |    |    |    |    |    |    |");
322 
323 	mvprintw(NAMEIROW, NAMEICOL,
324 	    "Namei         Sys-cache    Proc-cache    No-cache");
325 	mvprintw(NAMEIROW + 1, NAMEICOL,
326 	    "    Calls     hits    %%    hits     %%    miss   %%");
327 	mvprintw(DISKROW, DISKCOL, "Discs");
328 	mvprintw(DISKROW + 1, DISKCOL, "seeks");
329 	mvprintw(DISKROW + 2, DISKCOL, "xfers");
330 	mvprintw(DISKROW + 3, DISKCOL, "Kbyte");
331 	mvprintw(DISKROW + 4, DISKCOL, "  sec");
332 	for (i = 0, j = 0; i < cur.dk_ndrive && j < DRIVESPACE; i++)
333 		if (cur.dk_select[i] && (j + strlen(dr_name[i])) < DRIVESPACE) {
334 			l = MAX(4, strlen(dr_name[i]));
335 			mvprintw(DISKROW, DISKCOL + 5 + j,
336 			    " %*s", l, dr_name[i]);
337 			j += 1 + l;
338 		}
339 	for (i = 0; i < nintr; i++) {
340 		if (intrloc[i] == 0)
341 			continue;
342 		mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", intrname[i]);
343 	}
344 }
345 
346 #define X(fld)	{t=s.fld[i]; s.fld[i]-=s1.fld[i]; if (state==TIME) s1.fld[i]=t;}
347 #define Y(fld)	{t = s.fld; s.fld -= s1.fld; if (state == TIME) s1.fld = t;}
348 #define Z(fld)	{t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
349 	if (state == TIME) s1.nchstats.fld = t;}
350 #define PUTRATE(fld, l, c, w) \
351 	Y(fld); \
352 	putint((int)((float)s.fld/etime + 0.5), l, c, w)
353 #define MAXFAIL 5
354 
355 static	char cpuchar[CPUSTATES] = { '=' , '>', '-', ' ' };
356 static	char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_IDLE };
357 
358 void
359 showkre(void)
360 {
361 	float f1, f2;
362 	int psiz, inttotal;
363 	int i, l, c;
364 	static int failcnt = 0;
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 	/*
435 	 * The above code does not account for time in the CP_INTR state.
436 	 * Thus the total may be less than 100%.  If the total is less than
437 	 * the previous total old data may be left on the graph.  The graph
438 	 * assumes one character position for every 2 percentage points for
439 	 * a total of 50 positions.  Ensure all positions have been filled.
440 	 */
441 	while ( psiz++ <= 50 )
442 		addch(' ');
443 
444 	putint(ucount(), STATROW, STATCOL, 3);
445 	putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
446 	putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
447 	putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
448 	mvaddstr(STATROW, STATCOL + 53, buf);
449 #define pgtokb(pg)	((pg) * (s.uvmexp.pagesize / 1024))
450 
451 	putint(pgtokb(s.uvmexp.active), MEMROW + 2, MEMCOL + 6, 7);
452 	putint(pgtokb(s.uvmexp.active + s.uvmexp.swpginuse),    /* XXX */
453 	    MEMROW + 2, MEMCOL + 16, 7);
454 	putint(pgtokb(s.uvmexp.npages - s.uvmexp.free), MEMROW + 3, MEMCOL + 6, 7);
455 	putint(pgtokb(s.uvmexp.npages - s.uvmexp.free + s.uvmexp.swpginuse),
456 	    MEMROW + 3, MEMCOL + 16, 7);
457 	putint(pgtokb(s.uvmexp.free), MEMROW + 2, MEMCOL + 24, 7);
458 	putint(pgtokb(s.uvmexp.free + s.uvmexp.swpages - s.uvmexp.swpginuse),
459 	    MEMROW + 3, MEMCOL + 24, 7);
460 	putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
461 
462 	putint(total.t_dw, PROCSROW + 1, PROCSCOL + 6, 3);
463 	putint(total.t_sl, PROCSROW + 1, PROCSCOL + 9, 3);
464 	putint(total.t_sw, PROCSROW + 1, PROCSCOL + 12, 3);
465 	PUTRATE(uvmexp.forks, VMSTATROW + 0, VMSTATCOL + 3, 6);
466 	PUTRATE(uvmexp.forks_ppwait, VMSTATROW + 1, VMSTATCOL + 3, 6);
467 	PUTRATE(uvmexp.forks_sharevm, VMSTATROW + 2, VMSTATCOL + 3, 6);
468 	PUTRATE(uvmexp.fltpgwait, VMSTATROW + 3, VMSTATCOL + 4, 5);
469 	PUTRATE(uvmexp.fltrelck, VMSTATROW + 4, VMSTATCOL + 3, 6);
470 	PUTRATE(uvmexp.fltrelckok, VMSTATROW + 5, VMSTATCOL + 3, 6);
471 	PUTRATE(uvmexp.fltnoram, VMSTATROW + 6, VMSTATCOL + 3, 6);
472 	PUTRATE(uvmexp.fltamcopy, VMSTATROW + 7, VMSTATCOL + 3, 6);
473 	PUTRATE(uvmexp.flt_prcopy, VMSTATROW + 8, VMSTATCOL + 3, 6);
474 	PUTRATE(uvmexp.flt_przero, VMSTATROW + 9, VMSTATCOL + 3, 6);
475 	PUTRATE(uvmexp.flt_acow, VMSTATROW + 10, VMSTATCOL, 9);
476 	putint(s.uvmexp.freemin, VMSTATROW + 11, VMSTATCOL, 9);
477 	putint(s.uvmexp.freetarg, VMSTATROW + 12, VMSTATCOL, 9);
478 	putint(s.uvmexp.inactarg, VMSTATROW + 13, VMSTATCOL, 9);
479 	putint(s.uvmexp.wired, VMSTATROW + 14, VMSTATCOL, 9);
480 	PUTRATE(uvmexp.pdfreed, VMSTATROW + 15, VMSTATCOL, 9);
481 	if (LINES - 1 > VMSTATROW + 16)
482 		PUTRATE(uvmexp.pdscans, VMSTATROW + 16, VMSTATCOL, 9);
483 
484 	PUTRATE(uvmexp.pageins, PAGEROW + 2, PAGECOL + 5, 5);
485 	PUTRATE(uvmexp.pdpageouts, PAGEROW + 2, PAGECOL + 10, 5);
486 	PUTRATE(uvmexp.swapins, PAGEROW + 2, PAGECOL + 15, 5);
487 	PUTRATE(uvmexp.swapouts, PAGEROW + 2, PAGECOL + 20, 5);
488 	PUTRATE(uvmexp.pgswapin, PAGEROW + 3, PAGECOL + 5, 5);
489 	PUTRATE(uvmexp.pgswapout, PAGEROW + 3, PAGECOL + 10, 5);
490 
491 	PUTRATE(uvmexp.swtch, GENSTATROW + 1, GENSTATCOL, 6);
492 	PUTRATE(uvmexp.traps, GENSTATROW + 1, GENSTATCOL + 6, 6);
493 	PUTRATE(uvmexp.syscalls, GENSTATROW + 1, GENSTATCOL + 12, 6);
494 	PUTRATE(uvmexp.intrs, GENSTATROW + 1, GENSTATCOL + 18, 6);
495 	PUTRATE(uvmexp.softs, GENSTATROW + 1, GENSTATCOL + 24, 6);
496 	PUTRATE(uvmexp.faults, GENSTATROW + 1, GENSTATCOL + 30, 5);
497 	mvprintw(DISKROW, DISKCOL + 5, "                              ");
498 	for (i = 0, c = 0; i < cur.dk_ndrive && c < DRIVESPACE; i++)
499 		if (cur.dk_select[i] && (c + strlen(dr_name[i])) < DRIVESPACE) {
500 			l = MAX(4, strlen(dr_name[i]));
501 			mvprintw(DISKROW, DISKCOL + 5 + c,
502 			    " %*s", l, dr_name[i]);
503 			c += 1 + l;
504 			dinfo(i, c);
505 		}
506 	/* and pad the DRIVESPACE */
507 	l = DRIVESPACE - c;
508 	for (i = 0; i < 5; i++)
509 		mvprintw(DISKROW + i, DISKCOL + 5 + c, "%*s", l, "");
510 
511 	putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
512 	putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 10, 8);
513 #define nz(x)	((x) ? (x) : 1)
514 	putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount),
515 	    NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
516 	putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 24, 7);
517 	putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
518 	    NAMEIROW + 2, NAMEICOL + 33, 4, 0, 1);
519 	putint(nchtotal.ncs_miss - nchtotal.ncs_pass2,
520 	   NAMEIROW + 2, NAMEICOL + 38, 7);
521 	putfloat((nchtotal.ncs_miss - nchtotal.ncs_pass2) *
522 	    100.0 / nz(s.nchcount), NAMEIROW + 2, NAMEICOL + 45, 4, 0, 1);
523 #undef nz
524 }
525 
526 int
527 cmdkre(char *cmd, char *args)
528 {
529 
530 	if (prefix(cmd, "run")) {
531 		copyinfo(&s2, &s1);
532 		state = RUN;
533 		return (1);
534 	}
535 	if (prefix(cmd, "boot")) {
536 		state = BOOT;
537 		copyinfo(&z, &s1);
538 		return (1);
539 	}
540 	if (prefix(cmd, "time")) {
541 		state = TIME;
542 		return (1);
543 	}
544 	if (prefix(cmd, "zero")) {
545 		if (state == RUN)
546 			getinfo(&s1, RUN);
547 		return (1);
548 	}
549 	return (dkcmd(cmd, args));
550 }
551 
552 /* calculate number of users on the system */
553 static int
554 ucount(void)
555 {
556 	int nusers = 0;
557 
558 	if (ut < 0)
559 		return (0);
560 	while (read(ut, &utmp, sizeof(utmp)))
561 		if (utmp.ut_name[0] != '\0')
562 			nusers++;
563 
564 	lseek(ut, 0, SEEK_SET);
565 	return (nusers);
566 }
567 
568 static float
569 cputime(int indx)
570 {
571 	double t;
572 	int i;
573 
574 	t = 0;
575 	for (i = 0; i < CPUSTATES; i++)
576 		t += s.time[i];
577 	if (t == 0.0)
578 		t = 1.0;
579 	return (s.time[indx] * 100.0 / t);
580 }
581 
582 static void
583 putint(int n, int l, int c, int w)
584 {
585 	char b[128];
586 
587 	move(l, c);
588 	if (n == 0) {
589 		while (w-- > 0)
590 			addch(' ');
591 		return;
592 	}
593 	snprintf(b, sizeof b, "%*d", w, n);
594 	if (strlen(b) > w) {
595 		while (w-- > 0)
596 			addch('*');
597 		return;
598 	}
599 	addstr(b);
600 }
601 
602 static void
603 putfloat(double f, int l, int c, int w, int d, int nz)
604 {
605 	char b[128];
606 
607 	move(l, c);
608 	if (nz && f == 0.0) {
609 		while (--w >= 0)
610 			addch(' ');
611 		return;
612 	}
613 	snprintf(b, sizeof b, "%*.*f", w, d, f);
614 	if (strlen(b) > w) {
615 		while (--w >= 0)
616 			addch('*');
617 		return;
618 	}
619 	addstr(b);
620 }
621 
622 static void
623 getinfo(struct Info *s, enum state st)
624 {
625 	static int cp_time_mib[] = { CTL_KERN, KERN_CPTIME };
626 	static int nchstats_mib[2] = { CTL_KERN, KERN_NCHSTATS };
627 	static int uvmexp_mib[2] = { CTL_VM, VM_UVMEXP };
628 	static int vmtotal_mib[2] = { CTL_VM, VM_METER };
629 	size_t size;
630 #if defined(__i386__)
631 	struct intrhand *intrhand[16], *ihp, ih;
632 	int i, n;
633 #endif
634 
635 	dkreadstats();
636 #if defined(__i386__)
637 	NREAD(X_INTRHAND, intrhand, sizeof(intrhand));
638 	for (i = 0, n = 0; i < 16; i++) {
639 		ihp = intrhand[i];
640 		while (ihp) {
641 			KREAD(ihp, &ih, sizeof(ih));
642 			s->intrcnt[n++] = ih.ih_count;
643 			ihp = ih.ih_next;
644 		}
645 	}
646 #else
647 	NREAD(X_INTRCNT, s->intrcnt, nintr * sizeof(int));
648 #endif
649 	size = sizeof(s->time);
650 	if (sysctl(cp_time_mib, 2, &s->time, &size, NULL, 0) < 0) {
651 		error("Can't get KERN_CPTIME: %s\n", strerror(errno));
652 		bzero(&s->time, sizeof(s->time));
653 	}
654 
655 	size = sizeof(s->nchstats);
656 	if (sysctl(nchstats_mib, 2, &s->nchstats, &size, NULL, 0) < 0) {
657 		error("Can't get KERN_NCHSTATS: %s\n", strerror(errno));
658 		bzero(&s->nchstats, sizeof(s->nchstats));
659 	}
660 
661 	size = sizeof(s->uvmexp);
662 	if (sysctl(uvmexp_mib, 2, &s->uvmexp, &size, NULL, 0) < 0) {
663 		error("Can't get VM_UVMEXP: %s\n", strerror(errno));
664 		bzero(&s->uvmexp, sizeof(s->uvmexp));
665 	}
666 
667 	size = sizeof(s->Total);
668 	if (sysctl(vmtotal_mib, 2, &s->Total, &size, NULL, 0) < 0) {
669 		error("Can't get VM_METER: %s\n", strerror(errno));
670 		bzero(&s->Total, sizeof(s->Total));
671 	}
672 }
673 
674 static void
675 allocinfo(struct Info *s)
676 {
677 
678 	s->intrcnt = (int *) malloc(nintr * sizeof(int));
679 	if (s->intrcnt == NULL)
680 		errx(2, "out of memory");
681 }
682 
683 static void
684 copyinfo(struct Info *from, struct Info *to)
685 {
686 	int *intrcnt;
687 
688 	intrcnt = to->intrcnt;
689 	*to = *from;
690 	bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
691 }
692 
693 static void
694 dinfo(int dn, int c)
695 {
696 	double words, atime;
697 
698 	c += DISKCOL;
699 
700 	/* time busy in disk activity */
701 	atime = (double)cur.dk_time[dn].tv_sec +
702 	    ((double)cur.dk_time[dn].tv_usec / (double)1000000);
703 
704 	words = cur.dk_bytes[dn] / 1024.0;	/* # of K transferred */
705 
706 	putint((int)((float)cur.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5);
707 	putint((int)((float)cur.dk_xfer[dn]/etime+0.5), DISKROW + 2, c, 5);
708 	putint((int)(words/etime + 0.5), DISKROW + 3, c, 5);
709 	putfloat(atime/etime, DISKROW + 4, c, 5, 1, 1);
710 }
711