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