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