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