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