xref: /dragonfly/usr.bin/systat/vmstat.c (revision 984263bc)
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 #if 0
36 static char sccsid[] = "@(#)vmstat.c	8.2 (Berkeley) 1/12/94";
37 #endif
38 static const char rcsid[] =
39   "$FreeBSD: src/usr.bin/systat/vmstat.c,v 1.38.2.4 2002/03/12 19:50:23 phantom Exp $";
40 #endif /* not lint */
41 
42 /*
43  * Cursed vmstat -- from Robert Elz.
44  */
45 
46 #include <sys/param.h>
47 #include <sys/stat.h>
48 #include <sys/time.h>
49 #include <sys/proc.h>
50 #include <sys/uio.h>
51 #include <sys/namei.h>
52 #include <sys/sysctl.h>
53 #include <sys/dkstat.h>
54 #include <sys/vmmeter.h>
55 
56 #include <vm/vm_param.h>
57 
58 #include <ctype.h>
59 #include <err.h>
60 #include <errno.h>
61 #include <langinfo.h>
62 #include <nlist.h>
63 #include <paths.h>
64 #include <signal.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <time.h>
68 #include <unistd.h>
69 #include <utmp.h>
70 #include <devstat.h>
71 #include "systat.h"
72 #include "extern.h"
73 #include "devs.h"
74 
75 static struct Info {
76 	long	time[CPUSTATES];
77 	struct	vmmeter Cnt;
78 	struct	vmtotal Total;
79 	struct	nchstats nchstats;
80 	long	nchcount;
81 	long	*intrcnt;
82 	int	bufspace;
83 	int	desiredvnodes;
84 	long	numvnodes;
85 	long	freevnodes;
86 	long	numdirtybuffers;
87 } s, s1, s2, z;
88 
89 struct statinfo cur, last, run;
90 
91 #define	cnt s.Cnt
92 #define oldcnt s1.Cnt
93 #define	total s.Total
94 #define	nchtotal s.nchstats
95 #define	oldnchtotal s1.nchstats
96 
97 static	enum state { BOOT, TIME, RUN } state = TIME;
98 
99 static void allocinfo __P((struct Info *));
100 static void copyinfo __P((struct Info *, struct Info *));
101 static float cputime __P((int));
102 static void dinfo __P((int, int, struct statinfo *, struct statinfo *));
103 static void getinfo __P((struct Info *, enum state));
104 static void putint __P((int, int, int, int));
105 static void putfloat __P((double, int, int, int, int, int));
106 static void putlongdouble __P((long double, int, int, int, int, int));
107 static int ucount __P((void));
108 
109 static	int ncpu;
110 static	int ut;
111 static	char buf[26];
112 static	time_t t;
113 static	double etime;
114 static	int nintr;
115 static	long *intrloc;
116 static	char **intrname;
117 static	int nextintsrow;
118 static  int extended_vm_stats;
119 
120 struct	utmp utmp;
121 
122 
123 WINDOW *
124 openkre()
125 {
126 
127 	ut = open(_PATH_UTMP, O_RDONLY);
128 	if (ut < 0)
129 		error("No utmp");
130 	return (stdscr);
131 }
132 
133 void
134 closekre(w)
135 	WINDOW *w;
136 {
137 
138 	(void) close(ut);
139 	if (w == NULL)
140 		return;
141 	wclear(w);
142 	wrefresh(w);
143 }
144 
145 
146 static struct nlist namelist[] = {
147 #define X_CPTIME	0
148 	{ "_cp_time" },
149 #define X_CNT		1
150 	{ "_cnt" },
151 #define	X_BUFFERSPACE	2
152 	{ "_bufspace" },
153 #define	X_NCHSTATS	3
154 	{ "_nchstats" },
155 #define	X_INTRNAMES	4
156 	{ "_intrnames" },
157 #define	X_EINTRNAMES	5
158 	{ "_eintrnames" },
159 #define	X_INTRCNT	6
160 	{ "_intrcnt" },
161 #define	X_EINTRCNT	7
162 	{ "_eintrcnt" },
163 #define	X_DESIREDVNODES	8
164 	{ "_desiredvnodes" },
165 #define	X_NUMVNODES	9
166 	{ "_numvnodes" },
167 #define	X_FREEVNODES	10
168 	{ "_freevnodes" },
169 #define X_NUMDIRTYBUFFERS 11
170 	{ "_numdirtybuffers" },
171 	{ "" },
172 };
173 
174 /*
175  * These constants define where the major pieces are laid out
176  */
177 #define STATROW		 0	/* uses 1 row and 68 cols */
178 #define STATCOL		 2
179 #define MEMROW		 2	/* uses 4 rows and 31 cols */
180 #define MEMCOL		 0
181 #define PAGEROW		 2	/* uses 4 rows and 26 cols */
182 #define PAGECOL		46
183 #define INTSROW		 6	/* uses all rows to bottom and 17 cols */
184 #define INTSCOL		61
185 #define PROCSROW	 7	/* uses 2 rows and 20 cols */
186 #define PROCSCOL	 0
187 #define GENSTATROW	 7	/* uses 2 rows and 30 cols */
188 #define GENSTATCOL	20
189 #define VMSTATROW	 6	/* uses 17 rows and 12 cols */
190 #define VMSTATCOL	48
191 #define GRAPHROW	10	/* uses 3 rows and 51 cols */
192 #define GRAPHCOL	 0
193 #define NAMEIROW	14	/* uses 3 rows and 38 cols */
194 #define NAMEICOL	 0
195 #define DISKROW		18	/* uses 5 rows and 50 cols (for 9 drives) */
196 #define DISKCOL		 0
197 
198 #define	DRIVESPACE	 7	/* max # for space */
199 
200 #define	MAXDRIVES	DRIVESPACE	 /* max # to display */
201 
202 int
203 initkre()
204 {
205 	char *intrnamebuf, *cp;
206 	int i;
207 
208 	if (namelist[0].n_type == 0) {
209 		if (kvm_nlist(kd, namelist)) {
210 			nlisterr(namelist);
211 			return(0);
212 		}
213 		if (namelist[0].n_type == 0) {
214 			error("No namelist");
215 			return(0);
216 		}
217 	}
218 
219 	if (num_devices = getnumdevs() < 0) {
220 		warnx("%s", devstat_errbuf);
221 		return(0);
222 	}
223 
224 	cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
225 	last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
226 	run.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
227 	bzero(cur.dinfo, sizeof(struct devinfo));
228 	bzero(last.dinfo, sizeof(struct devinfo));
229 	bzero(run.dinfo, sizeof(struct devinfo));
230 
231 	if (dsinit(MAXDRIVES, &cur, &last, &run) != 1)
232 		return(0);
233 
234 	if (nintr == 0) {
235 		nintr = (namelist[X_EINTRCNT].n_value -
236 			namelist[X_INTRCNT].n_value) / sizeof (long);
237 		intrloc = calloc(nintr, sizeof (long));
238 		intrname = calloc(nintr, sizeof (long));
239 		intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value -
240 			namelist[X_INTRNAMES].n_value);
241 		if (intrnamebuf == 0 || intrname == 0 || intrloc == 0) {
242 			error("Out of memory\n");
243 			if (intrnamebuf)
244 				free(intrnamebuf);
245 			if (intrname)
246 				free(intrname);
247 			if (intrloc)
248 				free(intrloc);
249 			nintr = 0;
250 			return(0);
251 		}
252 		NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) -
253 			NVAL(X_INTRNAMES));
254 		for (cp = intrnamebuf, i = 0; i < nintr; i++) {
255 			intrname[i] = cp;
256 			cp += strlen(cp) + 1;
257 		}
258 		nextintsrow = INTSROW + 2;
259 		allocinfo(&s);
260 		allocinfo(&s1);
261 		allocinfo(&s2);
262 		allocinfo(&z);
263 	}
264 	getinfo(&s2, RUN);
265 	copyinfo(&s2, &s1);
266 	return(1);
267 }
268 
269 void
270 fetchkre()
271 {
272 	time_t now;
273 	struct tm *tp;
274 	static int d_first = -1;
275 
276 	if (d_first < 0)
277 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
278 
279 	time(&now);
280 	tp = localtime(&now);
281 	(void) strftime(buf, sizeof(buf),
282 			d_first ? "%e %b %R" : "%b %e %R", tp);
283 	getinfo(&s, state);
284 }
285 
286 void
287 labelkre()
288 {
289 	register int i, j;
290 
291 	clear();
292 	mvprintw(STATROW, STATCOL + 4, "users    Load");
293 	mvprintw(MEMROW, MEMCOL, "Mem:KB    REAL            VIRTUAL");
294 	mvprintw(MEMROW + 1, MEMCOL, "        Tot   Share      Tot    Share");
295 	mvprintw(MEMROW + 2, MEMCOL, "Act");
296 	mvprintw(MEMROW + 3, MEMCOL, "All");
297 
298 	mvprintw(MEMROW + 1, MEMCOL + 41, "Free");
299 
300 	mvprintw(PAGEROW, PAGECOL,     "        VN PAGER  SWAP PAGER ");
301 	mvprintw(PAGEROW + 1, PAGECOL, "        in  out     in  out ");
302 	mvprintw(PAGEROW + 2, PAGECOL, "count");
303 	mvprintw(PAGEROW + 3, PAGECOL, "pages");
304 
305 	mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
306 	mvprintw(INTSROW + 1, INTSCOL + 9, "total");
307 
308 	mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "cow");
309 	mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "wire");
310 	mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "act");
311 	mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "inact");
312 	mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "cache");
313 	mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "free");
314 	mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "daefr");
315 	mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "prcfr");
316 	mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "react");
317 	mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "pdwake");
318 	mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "pdpgs");
319 	mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "intrn");
320 	mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "buf");
321 	mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "dirtybuf");
322 
323 	mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "desiredvnodes");
324 	mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "numvnodes");
325 	mvprintw(VMSTATROW + 17, VMSTATCOL + 10, "freevnodes");
326 
327 	mvprintw(GENSTATROW, GENSTATCOL, "  Csw  Trp  Sys  Int  Sof  Flt");
328 
329 	mvprintw(GRAPHROW, GRAPHCOL,
330 		"  . %%Sys    . %%Intr   . %%User   . %%Nice   . %%Idle");
331 	mvprintw(PROCSROW, PROCSCOL, "Proc:r  p  d  s  w");
332 	mvprintw(GRAPHROW + 1, GRAPHCOL,
333 		"|    |    |    |    |    |    |    |    |    |    |");
334 
335 	mvprintw(NAMEIROW, NAMEICOL, "Namei         Name-cache    Dir-cache");
336 	mvprintw(NAMEIROW + 1, NAMEICOL,
337 		"    Calls     hits    %%     hits    %%");
338 	mvprintw(DISKROW, DISKCOL, "Disks");
339 	mvprintw(DISKROW + 1, DISKCOL, "KB/t");
340 	mvprintw(DISKROW + 2, DISKCOL, "tps");
341 	mvprintw(DISKROW + 3, DISKCOL, "MB/s");
342 	mvprintw(DISKROW + 4, DISKCOL, "%% busy");
343 	/*
344 	 * For now, we don't support a fourth disk statistic.  So there's
345 	 * no point in providing a label for it.  If someone can think of a
346 	 * fourth useful disk statistic, there is room to add it.
347 	 */
348 	/* mvprintw(DISKROW + 4, DISKCOL, " msps"); */
349 	j = 0;
350 	for (i = 0; i < num_devices && j < MAXDRIVES; i++)
351 		if (dev_select[i].selected) {
352 			char tmpstr[80];
353 			sprintf(tmpstr, "%s%d", dev_select[i].device_name,
354 				dev_select[i].unit_number);
355 			mvprintw(DISKROW, DISKCOL + 5 + 6 * j,
356 				" %5.5s", tmpstr);
357 			j++;
358 		}
359 
360 	if (j <= 4) {
361 		/*
362 		 * room for extended VM stats
363 		 */
364 		mvprintw(VMSTATROW + 11, VMSTATCOL - 6, "zfod");
365 		mvprintw(VMSTATROW + 12, VMSTATCOL - 6, "ofod");
366 		mvprintw(VMSTATROW + 13, VMSTATCOL - 6, "%%slo-z");
367 		mvprintw(VMSTATROW + 14, VMSTATCOL - 6, "tfree");
368 		extended_vm_stats = 1;
369 	} else {
370 		extended_vm_stats = 0;
371 		mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "zfod");
372 	}
373 
374 	for (i = 0; i < nintr; i++) {
375 		if (intrloc[i] == 0)
376 			continue;
377 		mvprintw(intrloc[i], INTSCOL + 9, "%-10.10s", intrname[i]);
378 	}
379 }
380 
381 #define X(fld)	{t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
382 #define Q(fld)	{t=cur.fld[i]; cur.fld[i]-=last.fld[i]; if(state==TIME) last.fld[i]=t;}
383 #define Y(fld)	{t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
384 #define Z(fld)	{t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
385 	if(state == TIME) s1.nchstats.fld = t;}
386 #define PUTRATE(fld, l, c, w) \
387 	Y(fld); \
388 	putint((int)((float)s.fld/etime + 0.5), l, c, w)
389 #define MAXFAIL 5
390 
391 static	char cpuchar[CPUSTATES] = { '=' , '+', '>', '-', ' ' };
392 static	char cpuorder[CPUSTATES] = { CP_SYS, CP_INTR, CP_USER, CP_NICE,
393 				     CP_IDLE };
394 
395 void
396 showkre()
397 {
398 	float f1, f2;
399 	int psiz, inttotal;
400 	int i, l, c;
401 	static int failcnt = 0;
402 
403 	etime = 0;
404 	for(i = 0; i < CPUSTATES; i++) {
405 		X(time);
406 		Q(cp_time);
407 		etime += s.time[i];
408 	}
409 	if (etime < 5.0) {	/* < 5 ticks - ignore this trash */
410 		if (failcnt++ >= MAXFAIL) {
411 			clear();
412 			mvprintw(2, 10, "The alternate system clock has died!");
413 			mvprintw(3, 10, "Reverting to ``pigs'' display.");
414 			move(CMDLINE, 0);
415 			refresh();
416 			failcnt = 0;
417 			sleep(5);
418 			command("pigs");
419 		}
420 		return;
421 	}
422 	failcnt = 0;
423 	etime /= hertz;
424 	etime /= ncpu;
425 	inttotal = 0;
426 	for (i = 0; i < nintr; i++) {
427 		if (s.intrcnt[i] == 0)
428 			continue;
429 		if (intrloc[i] == 0) {
430 			if (nextintsrow == LINES)
431 				continue;
432 			intrloc[i] = nextintsrow++;
433 			mvprintw(intrloc[i], INTSCOL + 9, "%-10.10s",
434 				intrname[i]);
435 		}
436 		X(intrcnt);
437 		l = (int)((float)s.intrcnt[i]/etime + 0.5);
438 		inttotal += l;
439 		putint(l, intrloc[i], INTSCOL + 2, 6);
440 	}
441 	putint(inttotal, INTSROW + 1, INTSCOL + 2, 6);
442 	Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
443 	Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes); Z(ncs_neghits);
444 	s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
445 	    nchtotal.ncs_miss + nchtotal.ncs_long + nchtotal.ncs_neghits;
446 	if (state == TIME)
447 		s1.nchcount = s.nchcount;
448 
449 	psiz = 0;
450 	f2 = 0.0;
451 	for (c = 0; c < CPUSTATES; c++) {
452 		i = cpuorder[c];
453 		f1 = cputime(i);
454 		f2 += f1;
455 		l = (int) ((f2 + 1.0) / 2.0) - psiz;
456 		if (f1 > 99.9)
457 			f1 = 99.9;	/* no room to display 100.0 */
458 		putfloat(f1, GRAPHROW, GRAPHCOL + 10 * c, 4, 1, 0);
459 		move(GRAPHROW + 2, psiz);
460 		psiz += l;
461 		while (l-- > 0)
462 			addch(cpuchar[c]);
463 	}
464 
465 	putint(ucount(), STATROW, STATCOL, 3);
466 	putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
467 	putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
468 	putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
469 	mvaddstr(STATROW, STATCOL + 53, buf);
470 #define pgtokb(pg)	((pg) * cnt.v_page_size / 1024)
471 	putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 3, 8);
472 	putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 11, 8);
473 	putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 19, 9);
474 	putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 28, 9);
475 	putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 3, 8);
476 	putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 11, 8);
477 	putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 19, 9);
478 	putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 28, 9);
479 	putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 37, 8);
480 	putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
481 	putint(total.t_pw, PROCSROW + 1, PROCSCOL + 6, 3);
482 	putint(total.t_dw, PROCSROW + 1, PROCSCOL + 9, 3);
483 	putint(total.t_sl, PROCSROW + 1, PROCSCOL + 12, 3);
484 	putint(total.t_sw, PROCSROW + 1, PROCSCOL + 15, 3);
485 	if (extended_vm_stats == 0) {
486 		PUTRATE(Cnt.v_zfod, VMSTATROW + 0, VMSTATCOL + 4, 5);
487 	}
488 	PUTRATE(Cnt.v_cow_faults, VMSTATROW + 1, VMSTATCOL + 3, 6);
489 	putint(pgtokb(cnt.v_wire_count), VMSTATROW + 2, VMSTATCOL, 9);
490 	putint(pgtokb(cnt.v_active_count), VMSTATROW + 3, VMSTATCOL, 9);
491 	putint(pgtokb(cnt.v_inactive_count), VMSTATROW + 4, VMSTATCOL, 9);
492 	putint(pgtokb(cnt.v_cache_count), VMSTATROW + 5, VMSTATCOL, 9);
493 	putint(pgtokb(cnt.v_free_count), VMSTATROW + 6, VMSTATCOL, 9);
494 	PUTRATE(Cnt.v_dfree, VMSTATROW + 7, VMSTATCOL, 9);
495 	PUTRATE(Cnt.v_pfree, VMSTATROW + 8, VMSTATCOL, 9);
496 	PUTRATE(Cnt.v_reactivated, VMSTATROW + 9, VMSTATCOL, 9);
497 	PUTRATE(Cnt.v_pdwakeups, VMSTATROW + 10, VMSTATCOL, 9);
498 	PUTRATE(Cnt.v_pdpages, VMSTATROW + 11, VMSTATCOL, 9);
499 	PUTRATE(Cnt.v_intrans, VMSTATROW + 12, VMSTATCOL, 9);
500 
501 	if (extended_vm_stats) {
502 	    PUTRATE(Cnt.v_zfod, VMSTATROW + 11, VMSTATCOL - 16, 9);
503 	    PUTRATE(Cnt.v_ozfod, VMSTATROW + 12, VMSTATCOL - 16, 9);
504 	    putint(
505 		((s.Cnt.v_ozfod < s.Cnt.v_zfod) ?
506 		    s.Cnt.v_ozfod * 100 / s.Cnt.v_zfod :
507 		    0
508 		),
509 		VMSTATROW + 13,
510 		VMSTATCOL - 16,
511 		9
512 	    );
513 	    PUTRATE(Cnt.v_tfree, VMSTATROW + 14, VMSTATCOL - 16, 9);
514 	}
515 
516 	putint(s.bufspace/1024, VMSTATROW + 13, VMSTATCOL, 9);
517 	putint(s.numdirtybuffers, VMSTATROW + 14, VMSTATCOL, 9);
518 	putint(s.desiredvnodes, VMSTATROW + 15, VMSTATCOL, 9);
519 	putint(s.numvnodes, VMSTATROW + 16, VMSTATCOL, 9);
520 	putint(s.freevnodes, VMSTATROW + 17, VMSTATCOL, 9);
521 	PUTRATE(Cnt.v_vnodein, PAGEROW + 2, PAGECOL + 5, 5);
522 	PUTRATE(Cnt.v_vnodeout, PAGEROW + 2, PAGECOL + 10, 5);
523 	PUTRATE(Cnt.v_swapin, PAGEROW + 2, PAGECOL + 17, 5);
524 	PUTRATE(Cnt.v_swapout, PAGEROW + 2, PAGECOL + 22, 5);
525 	PUTRATE(Cnt.v_vnodepgsin, PAGEROW + 3, PAGECOL + 5, 5);
526 	PUTRATE(Cnt.v_vnodepgsout, PAGEROW + 3, PAGECOL + 10, 5);
527 	PUTRATE(Cnt.v_swappgsin, PAGEROW + 3, PAGECOL + 17, 5);
528 	PUTRATE(Cnt.v_swappgsout, PAGEROW + 3, PAGECOL + 22, 5);
529 	PUTRATE(Cnt.v_swtch, GENSTATROW + 1, GENSTATCOL, 5);
530 	PUTRATE(Cnt.v_trap, GENSTATROW + 1, GENSTATCOL + 5, 5);
531 	PUTRATE(Cnt.v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 5);
532 	PUTRATE(Cnt.v_intr, GENSTATROW + 1, GENSTATCOL + 15, 5);
533 	PUTRATE(Cnt.v_soft, GENSTATROW + 1, GENSTATCOL + 20, 5);
534 	PUTRATE(Cnt.v_vm_faults, GENSTATROW + 1, GENSTATCOL + 25, 5);
535 	mvprintw(DISKROW, DISKCOL + 5, "                              ");
536 	for (i = 0, c = 0; i < num_devices && c < MAXDRIVES; i++)
537 		if (dev_select[i].selected) {
538 			char tmpstr[80];
539 			sprintf(tmpstr, "%s%d", dev_select[i].device_name,
540 				dev_select[i].unit_number);
541 			mvprintw(DISKROW, DISKCOL + 5 + 6 * c,
542 				" %5.5s", tmpstr);
543 			switch(state) {
544 			case TIME:
545 				dinfo(i, ++c, &cur, &last);
546 				break;
547 			case RUN:
548 				dinfo(i, ++c, &cur, &run);
549 				break;
550 			case BOOT:
551 				dinfo(i, ++c, &cur, NULL);
552 				break;
553 			}
554 		}
555 	putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
556 	putint((nchtotal.ncs_goodhits + nchtotal.ncs_neghits),
557 	   NAMEIROW + 2, NAMEICOL + 9, 9);
558 #define nz(x)	((x) ? (x) : 1)
559 	putfloat((nchtotal.ncs_goodhits+nchtotal.ncs_neghits) *
560 	   100.0 / nz(s.nchcount),
561 	   NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
562 	putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
563 	putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
564 	   NAMEIROW + 2, NAMEICOL + 33, 4, 0, 1);
565 #undef nz
566 }
567 
568 int
569 cmdkre(cmd, args)
570 	char *cmd, *args;
571 {
572 	int retval;
573 
574 	if (prefix(cmd, "run")) {
575 		retval = 1;
576 		copyinfo(&s2, &s1);
577 		switch (getdevs(&run)) {
578 		case -1:
579 			errx(1, "%s", devstat_errbuf);
580 			break;
581 		case 1:
582 			num_devices = run.dinfo->numdevs;
583 			generation = run.dinfo->generation;
584 			retval = dscmd("refresh", NULL, MAXDRIVES, &cur);
585 			if (retval == 2)
586 				labelkre();
587 			break;
588 		default:
589 			break;
590 		}
591 		state = RUN;
592 		return (retval);
593 	}
594 	if (prefix(cmd, "boot")) {
595 		state = BOOT;
596 		copyinfo(&z, &s1);
597 		return (1);
598 	}
599 	if (prefix(cmd, "time")) {
600 		state = TIME;
601 		return (1);
602 	}
603 	if (prefix(cmd, "zero")) {
604 		retval = 1;
605 		if (state == RUN) {
606 			getinfo(&s1, RUN);
607 			switch (getdevs(&run)) {
608 			case -1:
609 				errx(1, "%s", devstat_errbuf);
610 				break;
611 			case 1:
612 				num_devices = run.dinfo->numdevs;
613 				generation = run.dinfo->generation;
614 				retval = dscmd("refresh",NULL, MAXDRIVES, &cur);
615 				if (retval == 2)
616 					labelkre();
617 				break;
618 			default:
619 				break;
620 			}
621 		}
622 		return (retval);
623 	}
624 	retval = dscmd(cmd, args, MAXDRIVES, &cur);
625 
626 	if (retval == 2)
627 		labelkre();
628 
629 	return(retval);
630 }
631 
632 /* calculate number of users on the system */
633 static int
634 ucount()
635 {
636 	register int nusers = 0;
637 
638 	if (ut < 0)
639 		return (0);
640 	while (read(ut, &utmp, sizeof(utmp)))
641 		if (utmp.ut_name[0] != '\0')
642 			nusers++;
643 
644 	lseek(ut, 0L, L_SET);
645 	return (nusers);
646 }
647 
648 static float
649 cputime(indx)
650 	int indx;
651 {
652 	double t;
653 	register int i;
654 
655 	t = 0;
656 	for (i = 0; i < CPUSTATES; i++)
657 		t += s.time[i];
658 	if (t == 0.0)
659 		t = 1.0;
660 	return (s.time[indx] * 100.0 / t);
661 }
662 
663 static void
664 putint(n, l, c, w)
665 	int n, l, c, w;
666 {
667 	char b[128];
668 
669 	move(l, c);
670 	if (n == 0) {
671 		while (w-- > 0)
672 			addch(' ');
673 		return;
674 	}
675 	snprintf(b, sizeof(b), "%*d", w, n);
676 	if (strlen(b) > w) {
677 		while (w-- > 0)
678 			addch('*');
679 		return;
680 	}
681 	addstr(b);
682 }
683 
684 static void
685 putfloat(f, l, c, w, d, nz)
686 	double f;
687 	int l, c, w, d, nz;
688 {
689 	char b[128];
690 
691 	move(l, c);
692 	if (nz && f == 0.0) {
693 		while (--w >= 0)
694 			addch(' ');
695 		return;
696 	}
697 	snprintf(b, sizeof(b), "%*.*f", w, d, f);
698 	if (strlen(b) > w)
699 		snprintf(b, sizeof(b), "%*.0f", w, f);
700 	if (strlen(b) > w) {
701 		while (--w >= 0)
702 			addch('*');
703 		return;
704 	}
705 	addstr(b);
706 }
707 
708 static void
709 putlongdouble(f, l, c, w, d, nz)
710 	long double f;
711 	int l, c, w, d, nz;
712 {
713 	char b[128];
714 
715 	move(l, c);
716 	if (nz && f == 0.0) {
717 		while (--w >= 0)
718 			addch(' ');
719 		return;
720 	}
721 	sprintf(b, "%*.*Lf", w, d, f);
722 	if (strlen(b) > w)
723 		sprintf(b, "%*.0Lf", w, f);
724 	if (strlen(b) > w) {
725 		while (--w >= 0)
726 			addch('*');
727 		return;
728 	}
729 	addstr(b);
730 }
731 
732 static void
733 getinfo(s, st)
734 	struct Info *s;
735 	enum state st;
736 {
737 	struct devinfo *tmp_dinfo;
738 	size_t size;
739 	int mib[2];
740 
741 	NREAD(X_CPTIME, s->time, sizeof s->time);
742 	NREAD(X_CPTIME, cur.cp_time, sizeof(cur.cp_time));
743 	NREAD(X_CNT, &s->Cnt, sizeof s->Cnt);
744 	NREAD(X_BUFFERSPACE, &s->bufspace, sizeof(s->bufspace));
745 	NREAD(X_DESIREDVNODES, &s->desiredvnodes, sizeof(s->desiredvnodes));
746 	NREAD(X_NUMVNODES, &s->numvnodes, LONG);
747 	NREAD(X_FREEVNODES, &s->freevnodes, LONG);
748 	NREAD(X_NCHSTATS, &s->nchstats, sizeof s->nchstats);
749 	NREAD(X_INTRCNT, s->intrcnt, nintr * LONG);
750 	NREAD(X_NUMDIRTYBUFFERS, &s->numdirtybuffers, sizeof(s->numdirtybuffers));
751 	size = sizeof(s->Total);
752 	mib[0] = CTL_VM;
753 	mib[1] = VM_METER;
754 	if (sysctl(mib, 2, &s->Total, &size, NULL, 0) < 0) {
755 		error("Can't get kernel info: %s\n", strerror(errno));
756 		bzero(&s->Total, sizeof(s->Total));
757 	}
758 	size = sizeof(ncpu);
759 	if (sysctlbyname("hw.ncpu", &ncpu, &size, NULL, 0) < 0)
760 		ncpu = 1;
761 
762 	tmp_dinfo = last.dinfo;
763 	last.dinfo = cur.dinfo;
764 	cur.dinfo = tmp_dinfo;
765 
766 	last.busy_time = cur.busy_time;
767 	switch (getdevs(&cur)) {
768 	case -1:
769 		errx(1, "%s", devstat_errbuf);
770 		break;
771 	case 1:
772 		num_devices = cur.dinfo->numdevs;
773 		generation = cur.dinfo->generation;
774 		cmdkre("refresh", NULL);
775 		break;
776 	default:
777 		break;
778 	}
779 }
780 
781 static void
782 allocinfo(s)
783 	struct Info *s;
784 {
785 
786 	s->intrcnt = (long *) calloc(nintr, sizeof(long));
787 	if (s->intrcnt == NULL)
788 		errx(2, "out of memory");
789 }
790 
791 static void
792 copyinfo(from, to)
793 	register struct Info *from, *to;
794 {
795 	long *intrcnt;
796 
797 	/*
798 	 * time, wds, seek, and xfer are malloc'd so we have to
799 	 * save the pointers before the structure copy and then
800 	 * copy by hand.
801 	 */
802 	intrcnt = to->intrcnt;
803 	*to = *from;
804 
805 	bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
806 }
807 
808 static void
809 dinfo(dn, c, now, then)
810 	int dn, c;
811 	struct statinfo *now, *then;
812 {
813 	long double transfers_per_second;
814 	long double kb_per_transfer, mb_per_second;
815 	long double elapsed_time, device_busy;
816 	int di;
817 
818 	di = dev_select[dn].position;
819 
820 	elapsed_time = compute_etime(now->busy_time, then ?
821 				     then->busy_time :
822 				     now->dinfo->devices[di].dev_creation_time);
823 
824 	device_busy =  compute_etime(now->dinfo->devices[di].busy_time, then ?
825 				     then->dinfo->devices[di].busy_time :
826 				     now->dinfo->devices[di].dev_creation_time);
827 
828 	if (compute_stats(&now->dinfo->devices[di], then ?
829 			  &then->dinfo->devices[di] : NULL, elapsed_time,
830 			  NULL, NULL, NULL,
831 			  &kb_per_transfer, &transfers_per_second,
832 			  &mb_per_second, NULL, NULL) != 0)
833 		errx(1, "%s", devstat_errbuf);
834 
835 	if ((device_busy == 0) && (transfers_per_second > 5))
836 		/* the device has been 100% busy, fake it because
837 		 * as long as the device is 100% busy the busy_time
838 		 * field in the devstat struct is not updated */
839 		device_busy = elapsed_time;
840 	if (device_busy > elapsed_time)
841 		/* this normally happens after one or more periods
842 		 * where the device has been 100% busy, correct it */
843 		device_busy = elapsed_time;
844 
845 	c = DISKCOL + c * 6;
846 	putlongdouble(kb_per_transfer, DISKROW + 1, c, 5, 2, 0);
847 	putlongdouble(transfers_per_second, DISKROW + 2, c, 5, 0, 0);
848 	putlongdouble(mb_per_second, DISKROW + 3, c, 5, 2, 0);
849 	putlongdouble(device_busy * 100 / elapsed_time, DISKROW + 4, c, 5, 0, 0);
850 }
851