xref: /freebsd/usr.bin/systat/vmstat.c (revision 6966ac055c3b7a39266fb982493330df7a097997)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1983, 1989, 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 
34 __FBSDID("$FreeBSD$");
35 
36 #ifdef lint
37 static const char sccsid[] = "@(#)vmstat.c	8.2 (Berkeley) 1/12/94";
38 #endif
39 
40 /*
41  * Cursed vmstat -- from Robert Elz.
42  */
43 
44 #include <sys/param.h>
45 #include <sys/stat.h>
46 #include <sys/time.h>
47 #include <sys/proc.h>
48 #include <sys/uio.h>
49 #include <sys/namei.h>
50 #include <sys/resource.h>
51 #include <sys/sysctl.h>
52 #include <sys/vmmeter.h>
53 
54 #include <vm/vm_param.h>
55 
56 #include <ctype.h>
57 #include <err.h>
58 #include <errno.h>
59 #include <langinfo.h>
60 #include <libutil.h>
61 #include <nlist.h>
62 #include <paths.h>
63 #include <signal.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <time.h>
67 #include <unistd.h>
68 #include <utmpx.h>
69 #include "systat.h"
70 #include "extern.h"
71 #include "devs.h"
72 
73 static struct Info {
74 	long	time[CPUSTATES];
75 	uint64_t v_swtch;	/* context switches */
76 	uint64_t v_trap;	/* calls to trap */
77 	uint64_t v_syscall;	/* calls to syscall() */
78 	uint64_t v_intr;	/* device interrupts */
79 	uint64_t v_soft;	/* software interrupts */
80 	/*
81 	 * Virtual memory activity.
82 	 */
83 	uint64_t v_vm_faults;	/* number of address memory faults */
84 	uint64_t v_io_faults;	/* page faults requiring I/O */
85 	uint64_t v_cow_faults;	/* number of copy-on-writes */
86 	uint64_t v_zfod;	/* pages zero filled on demand */
87 	uint64_t v_ozfod;	/* optimized zero fill pages */
88 	uint64_t v_swapin;	/* swap pager pageins */
89 	uint64_t v_swapout;	/* swap pager pageouts */
90 	uint64_t v_swappgsin;	/* swap pager pages paged in */
91 	uint64_t v_swappgsout;	/* swap pager pages paged out */
92 	uint64_t v_vnodein;	/* vnode pager pageins */
93 	uint64_t v_vnodeout;	/* vnode pager pageouts */
94 	uint64_t v_vnodepgsin;	/* vnode_pager pages paged in */
95 	uint64_t v_vnodepgsout;	/* vnode pager pages paged out */
96 	uint64_t v_intrans;	/* intransit blocking page faults */
97 	uint64_t v_reactivated;	/* number of pages reactivated by pagedaemon */
98 	uint64_t v_pdwakeups;	/* number of times daemon has awaken from sleep */
99 	uint64_t v_pdpages;	/* number of pages analyzed by daemon */
100 
101 	uint64_t v_dfree;	/* pages freed by daemon */
102 	uint64_t v_pfree;	/* pages freed by exiting processes */
103 	uint64_t v_tfree;	/* total pages freed */
104 	/*
105 	 * Distribution of page usages.
106 	 */
107 	u_int v_page_size;	/* page size in bytes */
108 	u_int v_free_count;	/* number of pages free */
109 	u_int v_wire_count;	/* number of pages wired down */
110 	u_int v_active_count;	/* number of pages active */
111 	u_int v_inactive_count;	/* number of pages inactive */
112 	u_int v_laundry_count;	/* number of pages in laundry queue */
113 	u_long v_kmem_map_size;	/* Current kmem allocation size */
114 	struct	vmtotal Total;
115 	struct	nchstats nchstats;
116 	long	nchcount;
117 	long	*intrcnt;
118 	long	bufspace;
119 	u_long	maxvnodes;
120 	long	numvnodes;
121 	long	freevnodes;
122 	int	numdirtybuffers;
123 } s, s1, s2, z;
124 static u_long kmem_size;
125 static u_int v_page_count;
126 
127 
128 #define	total s.Total
129 #define	nchtotal s.nchstats
130 #define	oldnchtotal s1.nchstats
131 
132 static	enum state { BOOT, TIME, RUN } state = TIME;
133 enum divisor { IEC = 0, SI = HN_DIVISOR_1000 };
134 
135 static void allocinfo(struct Info *);
136 static void copyinfo(struct Info *, struct Info *);
137 static float cputime(int);
138 static void do_putuint64(uint64_t, int, int, int, int);
139 static void getinfo(struct Info *);
140 static void putuint64(uint64_t, 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 (dsinit(MAXDRIVES) != 1)
207 		return(0);
208 
209 	if (nintr == 0) {
210 		if (sysctlbyname("hw.intrcnt", NULL, &sz, NULL, 0) == -1) {
211 			error("sysctl(hw.intrcnt...) failed: %s",
212 			      strerror(errno));
213 			return (0);
214 		}
215 		nintr = sz / sizeof(u_long);
216 		intrloc = calloc(nintr, sizeof (long));
217 		intrname = calloc(nintr, sizeof (char *));
218 		intrnamebuf = sysctl_dynread("hw.intrnames", NULL);
219 		if (intrnamebuf == NULL || intrname == NULL ||
220 		    intrloc == NULL) {
221 			error("Out of memory");
222 			if (intrnamebuf)
223 				free(intrnamebuf);
224 			if (intrname)
225 				free(intrname);
226 			if (intrloc)
227 				free(intrloc);
228 			nintr = 0;
229 			return(0);
230 		}
231 		for (cp = intrnamebuf, i = 0; i < nintr; i++) {
232 			nextcp = cp + strlen(cp) + 1;
233 
234 			/* Discard trailing spaces. */
235 			for (cp1 = nextcp - 1; cp1 > cp && *(cp1 - 1) == ' '; )
236 				*--cp1 = '\0';
237 
238 			/* Convert "irqN: name" to "name irqN". */
239 			if (strncmp(cp, "irq", 3) == 0) {
240 				cp1 = cp + 3;
241 				while (isdigit((u_char)*cp1))
242 					cp1++;
243 				if (cp1 != cp && *cp1 == ':' &&
244 				    *(cp1 + 1) == ' ') {
245 					sz = strlen(cp);
246 					*cp1 = '\0';
247 					cp1 = cp1 + 2;
248 					cp2 = strdup(cp);
249 					bcopy(cp1, cp, sz - (cp1 - cp) + 1);
250 					if (sz <= 10 + 4) {
251 						strcat(cp, " ");
252 						strcat(cp, cp2 + 3);
253 					}
254 					free(cp2);
255 				}
256 			}
257 
258 			/*
259 			 * Convert "name irqN" to "name N" if the former is
260 			 * longer than the field width.
261 			 */
262 			if ((cp1 = strstr(cp, "irq")) != NULL &&
263 			    strlen(cp) > 10)
264 				bcopy(cp1 + 3, cp1, strlen(cp1 + 3) + 1);
265 
266 			intrname[i] = cp;
267 			cp = nextcp;
268 		}
269 		nextintsrow = INTSROW + 2;
270 		allocinfo(&s);
271 		allocinfo(&s1);
272 		allocinfo(&s2);
273 		allocinfo(&z);
274 	}
275 	GETSYSCTL("vm.kmem_size", kmem_size);
276 	GETSYSCTL("vm.stats.vm.v_page_count", v_page_count);
277 	getinfo(&s2);
278 	copyinfo(&s2, &s1);
279 	return(1);
280 }
281 
282 void
283 fetchkre(void)
284 {
285 	time_t now;
286 	struct tm *tp;
287 	static int d_first = -1;
288 
289 	if (d_first < 0)
290 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
291 
292 	time(&now);
293 	tp = localtime(&now);
294 	(void) strftime(buf, sizeof(buf),
295 			d_first ? "%e %b %R" : "%b %e %R", tp);
296 	getinfo(&s);
297 }
298 
299 void
300 labelkre(void)
301 {
302 	int i, j;
303 
304 	clear();
305 	mvprintw(STATROW, STATCOL + 6, "users    Load");
306 	mvprintw(STATROW + 1, STATCOL + 3, "Mem usage:    %%Phy   %%Kmem");
307 	mvprintw(MEMROW, MEMCOL, "Mem: KB    REAL            VIRTUAL");
308 	mvprintw(MEMROW + 1, MEMCOL, "        Tot   Share      Tot    Share");
309 	mvprintw(MEMROW + 2, MEMCOL, "Act");
310 	mvprintw(MEMROW + 3, MEMCOL, "All");
311 
312 	mvprintw(MEMROW + 1, MEMCOL + 41, "Free");
313 
314 	mvprintw(PAGEROW, PAGECOL,     "         VN PAGER   SWAP PAGER");
315 	mvprintw(PAGEROW + 1, PAGECOL, "         in   out     in   out");
316 	mvprintw(PAGEROW + 2, PAGECOL, "count");
317 	mvprintw(PAGEROW + 3, PAGECOL, "pages");
318 
319 	mvprintw(INTSROW, INTSCOL + 1, "Interrupts");
320 	mvprintw(INTSROW + 1, INTSCOL + 6, "total");
321 
322 	mvprintw(VMSTATROW, VMSTATCOL + 9, "ioflt");
323 	mvprintw(VMSTATROW + 1, VMSTATCOL + 9, "cow");
324 	mvprintw(VMSTATROW + 2, VMSTATCOL + 9, "zfod");
325 	mvprintw(VMSTATROW + 3, VMSTATCOL + 9, "ozfod");
326 	mvprintw(VMSTATROW + 4, VMSTATCOL + 9 - 1, "%%ozfod");
327 	mvprintw(VMSTATROW + 5, VMSTATCOL + 9, "daefr");
328 	mvprintw(VMSTATROW + 6, VMSTATCOL + 9, "prcfr");
329 	mvprintw(VMSTATROW + 7, VMSTATCOL + 9, "totfr");
330 	mvprintw(VMSTATROW + 8, VMSTATCOL + 9, "react");
331 	mvprintw(VMSTATROW + 9, VMSTATCOL + 9, "pdwak");
332 	mvprintw(VMSTATROW + 10, VMSTATCOL + 9, "pdpgs");
333 	mvprintw(VMSTATROW + 11, VMSTATCOL + 9, "intrn");
334 	mvprintw(VMSTATROW + 12, VMSTATCOL + 9, "wire");
335 	mvprintw(VMSTATROW + 13, VMSTATCOL + 9, "act");
336 	mvprintw(VMSTATROW + 14, VMSTATCOL + 9, "inact");
337 	mvprintw(VMSTATROW + 15, VMSTATCOL + 9, "laund");
338 	mvprintw(VMSTATROW + 16, VMSTATCOL + 9, "free");
339 	if (LINES - 1 > VMSTATROW + 17)
340 		mvprintw(VMSTATROW + 17, VMSTATCOL + 9, "buf");
341 
342 	mvprintw(GENSTATROW, GENSTATCOL, " Csw  Trp  Sys  Int  Sof  Flt");
343 
344 	mvprintw(GRAPHROW, GRAPHCOL,
345 		"  . %%Sys    . %%Intr   . %%User   . %%Nice   . %%Idle");
346 	mvprintw(PROCSROW, PROCSCOL, "Proc:");
347 	mvprintw(PROCSROW + 1, PROCSCOL, "  r   p   d   s   w");
348 	mvprintw(GRAPHROW + 1, GRAPHCOL,
349 		"|    |    |    |    |    |    |    |    |    |    |");
350 
351 	mvprintw(VNSTATROW, VNSTATCOL + 8, "dtbuf");
352 	mvprintw(VNSTATROW + 1, VNSTATCOL + 8, "maxvn");
353 	mvprintw(VNSTATROW + 2, VNSTATCOL + 8, "numvn");
354 	mvprintw(VNSTATROW + 3, VNSTATCOL + 8, "frevn");
355 
356 	mvprintw(NAMEIROW, NAMEICOL, "Namei     Name-cache   Dir-cache");
357 	mvprintw(NAMEIROW + 1, NAMEICOL,
358 		"   Calls    hits   %%    hits   %%");
359 	dslabel(MAXDRIVES, DISKCOL, DISKROW);
360 
361 	for (i = 0; i < nintr; i++) {
362 		if (intrloc[i] == 0)
363 			continue;
364 		mvprintw(intrloc[i], INTSCOL + 6, "%-10.10s", intrname[i]);
365 	}
366 }
367 
368 #define X(fld)	{t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
369 #define Q(fld)	{t=cur_dev.fld[i]; cur_dev.fld[i]-=last_dev.fld[i]; if(state==TIME) last_dev.fld[i]=t;}
370 #define Y(fld)	{t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
371 #define Z(fld)	{t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
372 	if(state == TIME) s1.nchstats.fld = t;}
373 #define PUTRATE(fld, l, c, w) \
374 do { \
375 	Y(fld); \
376 	putint((int)((float)s.fld/etime + 0.5), l, c, w); \
377 } while (0)
378 #define MAXFAIL 5
379 
380 static	char cpuchar[CPUSTATES] = { '=' , '+', '>', '-', ' ' };
381 static	char cpuorder[CPUSTATES] = { CP_SYS, CP_INTR, CP_USER, CP_NICE,
382 				     CP_IDLE };
383 
384 void
385 showkre(void)
386 {
387 	float f1, f2;
388 	int psiz, inttotal;
389 	int i, l, lc;
390 	static int failcnt = 0;
391 
392 	etime = 0;
393 	for(i = 0; i < CPUSTATES; i++) {
394 		X(time);
395 		Q(cp_time);
396 		etime += s.time[i];
397 	}
398 	if (etime < 5.0) {	/* < 5 ticks - ignore this trash */
399 		if (failcnt++ >= MAXFAIL) {
400 			clear();
401 			mvprintw(2, 10, "The alternate system clock has died!");
402 			mvprintw(3, 10, "Reverting to ``pigs'' display.");
403 			move(CMDLINE, 0);
404 			refresh();
405 			failcnt = 0;
406 			sleep(5);
407 			command("pigs");
408 		}
409 		return;
410 	}
411 	failcnt = 0;
412 	etime /= hertz;
413 	etime /= ncpu;
414 	inttotal = 0;
415 	for (i = 0; i < nintr; i++) {
416 		if (s.intrcnt[i] == 0)
417 			continue;
418 		X(intrcnt);
419 		l = (int)((float)s.intrcnt[i]/etime + 0.5);
420 		inttotal += l;
421 		if (intrloc[i] == 0) {
422 			if (nextintsrow == LINES)
423 				continue;
424 			intrloc[i] = nextintsrow++;
425 			mvprintw(intrloc[i], INTSCOL + 6, "%-10.10s",
426 				intrname[i]);
427 		}
428 		putint(l, intrloc[i], INTSCOL, 5);
429 	}
430 	putint(inttotal, INTSROW + 1, INTSCOL, 5);
431 	Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
432 	Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes); Z(ncs_neghits);
433 	s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
434 	    nchtotal.ncs_miss + nchtotal.ncs_long + nchtotal.ncs_neghits;
435 	if (state == TIME)
436 		s1.nchcount = s.nchcount;
437 
438 	psiz = 0;
439 	f2 = 0.0;
440 	for (lc = 0; lc < CPUSTATES; lc++) {
441 		i = cpuorder[lc];
442 		f1 = cputime(i);
443 		f2 += f1;
444 		l = (int) ((f2 + 1.0) / 2.0) - psiz;
445 		putfloat(f1, GRAPHROW, GRAPHCOL + 10 * lc, 4, 1, 0);
446 		move(GRAPHROW + 2, psiz);
447 		psiz += l;
448 		while (l-- > 0)
449 			addch(cpuchar[lc]);
450 	}
451 
452 	putint(ucount(), STATROW, STATCOL, 5);
453 	putfloat(avenrun[0], STATROW, STATCOL + 20, 5, 2, 0);
454 	putfloat(avenrun[1], STATROW, STATCOL + 26, 5, 2, 0);
455 	putfloat(avenrun[2], STATROW, STATCOL + 32, 5, 2, 0);
456 	mvaddstr(STATROW, STATCOL + 55, buf);
457 #define pgtokb(pg)	((pg) * (s.v_page_size / 1024))
458 	putfloat(100.0 * (v_page_count - total.t_free) / v_page_count,
459 	   STATROW + 1, STATCOL + 15, 2, 0, 1);
460 	putfloat(100.0 * s.v_kmem_map_size / kmem_size,
461 	   STATROW + 1, STATCOL + 22, 2, 0, 1);
462 
463 	putuint64(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 4, 7);
464 	putuint64(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 12, 7);
465 	putuint64(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 20, 8);
466 	putuint64(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 29, 8);
467 	putuint64(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 4, 7);
468 	putuint64(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 12, 7);
469 	putuint64(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 20, 8);
470 	putuint64(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 29, 8);
471 	putuint64(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 38, 7);
472 	putint(total.t_rq - 1, PROCSROW + 2, PROCSCOL, 3);
473 	putint(total.t_pw, PROCSROW + 2, PROCSCOL + 4, 3);
474 	putint(total.t_dw, PROCSROW + 2, PROCSCOL + 8, 3);
475 	putint(total.t_sl, PROCSROW + 2, PROCSCOL + 12, 3);
476 	putint(total.t_sw, PROCSROW + 2, PROCSCOL + 16, 3);
477 	PUTRATE(v_io_faults, VMSTATROW, VMSTATCOL + 2, 8 - 2);
478 	PUTRATE(v_cow_faults, VMSTATROW + 1, VMSTATCOL + 2, 8 - 2);
479 	PUTRATE(v_zfod, VMSTATROW + 2, VMSTATCOL + 2, 8 - 2);
480 	PUTRATE(v_ozfod, VMSTATROW + 3, VMSTATCOL, 8);
481 	putint(s.v_zfod != 0 ? (int)(s.v_ozfod * 100.0 / s.v_zfod) : 0,
482 	    VMSTATROW + 4, VMSTATCOL + 1, 8 - 1);
483 	PUTRATE(v_dfree, VMSTATROW + 5, VMSTATCOL + 2, 8 - 2);
484 	PUTRATE(v_pfree, VMSTATROW + 6, VMSTATCOL + 2, 8 - 2);
485 	PUTRATE(v_tfree, VMSTATROW + 7, VMSTATCOL, 8);
486 	PUTRATE(v_reactivated, VMSTATROW + 8, VMSTATCOL, 8);
487 	PUTRATE(v_pdwakeups, VMSTATROW + 9, VMSTATCOL, 8);
488 	PUTRATE(v_pdpages, VMSTATROW + 10, VMSTATCOL, 8);
489 	PUTRATE(v_intrans, VMSTATROW + 11, VMSTATCOL, 8);
490 	putuint64(pgtokb(s.v_wire_count), VMSTATROW + 12, VMSTATCOL, 8);
491 	putuint64(pgtokb(s.v_active_count), VMSTATROW + 13, VMSTATCOL, 8);
492 	putuint64(pgtokb(s.v_inactive_count), VMSTATROW + 14, VMSTATCOL, 8);
493 	putuint64(pgtokb(s.v_laundry_count), VMSTATROW + 15, VMSTATCOL, 8);
494 	putuint64(pgtokb(s.v_free_count), VMSTATROW + 16, VMSTATCOL, 8);
495 	if (LINES - 1 > VMSTATROW + 17)
496 		putuint64(s.bufspace / 1024, VMSTATROW + 17, VMSTATCOL, 8);
497 	PUTRATE(v_vnodein, PAGEROW + 2, PAGECOL + 6, 5);
498 	PUTRATE(v_vnodeout, PAGEROW + 2, PAGECOL + 12, 5);
499 	PUTRATE(v_swapin, PAGEROW + 2, PAGECOL + 19, 5);
500 	PUTRATE(v_swapout, PAGEROW + 2, PAGECOL + 25, 5);
501 	PUTRATE(v_vnodepgsin, PAGEROW + 3, PAGECOL + 6, 5);
502 	PUTRATE(v_vnodepgsout, PAGEROW + 3, PAGECOL + 12, 5);
503 	PUTRATE(v_swappgsin, PAGEROW + 3, PAGECOL + 19, 5);
504 	PUTRATE(v_swappgsout, PAGEROW + 3, PAGECOL + 25, 5);
505 	PUTRATE(v_swtch, GENSTATROW + 1, GENSTATCOL, 4);
506 	PUTRATE(v_trap, GENSTATROW + 1, GENSTATCOL + 5, 4);
507 	PUTRATE(v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 4);
508 	PUTRATE(v_intr, GENSTATROW + 1, GENSTATCOL + 15, 4);
509 	PUTRATE(v_soft, GENSTATROW + 1, GENSTATCOL + 20, 4);
510 	PUTRATE(v_vm_faults, GENSTATROW + 1, GENSTATCOL + 25, 4);
511 	switch(state) {
512 	case TIME:
513 		dsshow(MAXDRIVES, DISKCOL, DISKROW, &cur_dev, &last_dev);
514 		break;
515 	case RUN:
516 		dsshow(MAXDRIVES, DISKCOL, DISKROW, &cur_dev, &run_dev);
517 		break;
518 	case BOOT:
519 		dsshow(MAXDRIVES, DISKCOL, DISKROW, &cur_dev, NULL);
520 		break;
521 	}
522 	putint(s.numdirtybuffers, VNSTATROW, VNSTATCOL, 7);
523 	putint(s.maxvnodes, VNSTATROW + 1, VNSTATCOL, 7);
524 	putint(s.numvnodes, VNSTATROW + 2, VNSTATCOL, 7);
525 	putint(s.freevnodes, VNSTATROW + 3, VNSTATCOL, 7);
526 	putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 8);
527 	putint((nchtotal.ncs_goodhits + nchtotal.ncs_neghits),
528 	   NAMEIROW + 2, NAMEICOL + 9, 7);
529 #define nz(x)	((x) ? (x) : 1)
530 	putfloat((nchtotal.ncs_goodhits+nchtotal.ncs_neghits) *
531 	   100.0 / nz(s.nchcount),
532 	   NAMEIROW + 2, NAMEICOL + 17, 3, 0, 1);
533 	putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 21, 7);
534 	putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
535 	   NAMEIROW + 2, NAMEICOL + 29, 3, 0, 1);
536 #undef nz
537 }
538 
539 int
540 cmdkre(const char *cmd, const char *args)
541 {
542 	int retval;
543 
544 	if (prefix(cmd, "run")) {
545 		retval = 1;
546 		copyinfo(&s2, &s1);
547 		switch (devstat_getdevs(NULL, &run_dev)) {
548 		case -1:
549 			errx(1, "%s", devstat_errbuf);
550 			break;
551 		case 1:
552 			num_devices = run_dev.dinfo->numdevs;
553 			generation = run_dev.dinfo->generation;
554 			retval = dscmd("refresh", NULL, MAXDRIVES, &cur_dev);
555 			if (retval == 2)
556 				labelkre();
557 			break;
558 		default:
559 			break;
560 		}
561 		state = RUN;
562 		return (retval);
563 	}
564 	if (prefix(cmd, "boot")) {
565 		state = BOOT;
566 		copyinfo(&z, &s1);
567 		return (1);
568 	}
569 	if (prefix(cmd, "time")) {
570 		state = TIME;
571 		return (1);
572 	}
573 	if (prefix(cmd, "zero")) {
574 		retval = 1;
575 		if (state == RUN) {
576 			getinfo(&s1);
577 			switch (devstat_getdevs(NULL, &run_dev)) {
578 			case -1:
579 				errx(1, "%s", devstat_errbuf);
580 				break;
581 			case 1:
582 				num_devices = run_dev.dinfo->numdevs;
583 				generation = run_dev.dinfo->generation;
584 				retval = dscmd("refresh",NULL, MAXDRIVES, &cur_dev);
585 				if (retval == 2)
586 					labelkre();
587 				break;
588 			default:
589 				break;
590 			}
591 		}
592 		return (retval);
593 	}
594 	retval = dscmd(cmd, args, MAXDRIVES, &cur_dev);
595 
596 	if (retval == 2)
597 		labelkre();
598 
599 	return(retval);
600 }
601 
602 /* calculate number of users on the system */
603 static int
604 ucount(void)
605 {
606 	int nusers = 0;
607 	struct utmpx *ut;
608 
609 	setutxent();
610 	while ((ut = getutxent()) != NULL)
611 		if (ut->ut_type == USER_PROCESS)
612 			nusers++;
613 	endutxent();
614 
615 	return (nusers);
616 }
617 
618 static float
619 cputime(int indx)
620 {
621 	double lt;
622 	int i;
623 
624 	lt = 0;
625 	for (i = 0; i < CPUSTATES; i++)
626 		lt += s.time[i];
627 	if (lt == 0.0)
628 		lt = 1.0;
629 	return (s.time[indx] * 100.0 / lt);
630 }
631 
632 void
633 putint(int n, int l, int lc, int w)
634 {
635 
636 	do_putuint64(n, l, lc, w, SI);
637 }
638 
639 static void
640 putuint64(uint64_t n, int l, int lc, int w)
641 {
642 
643 	do_putuint64(n, l, lc, w, IEC);
644 }
645 
646 static void
647 do_putuint64(uint64_t n, int l, int lc, int w, int div)
648 {
649 	int snr;
650 	char b[128];
651 	char buf[128];
652 
653 	move(l, lc);
654 #ifdef DEBUG
655 		while (w-- > 0)
656 			addch('*');
657 		return;
658 #endif
659 	if (n == 0) {
660 		while (w-- > 0)
661 			addch(' ');
662 		return;
663 	}
664 	snr = snprintf(b, sizeof(b), "%*ju", w, (uintmax_t)n);
665 	if (snr != w) {
666 		humanize_number(buf, w, n, "", HN_AUTOSCALE,
667 		    HN_NOSPACE | HN_DECIMAL | div);
668 		snr = snprintf(b, sizeof(b), "%*s", w, buf);
669 	}
670 	if (snr != w) {
671 		while (w-- > 0)
672 			addch('*');
673 		return;
674 	}
675 	addstr(b);
676 }
677 
678 void
679 putfloat(double f, int l, int lc, int w, int d, int nz)
680 {
681 	int snr;
682 	char b[128];
683 
684 	move(l, lc);
685 #ifdef DEBUG
686 		while (--w >= 0)
687 			addch('*');
688 		return;
689 #endif
690 	if (nz && f == 0.0) {
691 		while (--w >= 0)
692 			addch(' ');
693 		return;
694 	}
695 	snr = snprintf(b, sizeof(b), "%*.*f", w, d, f);
696 	if (snr != w)
697 		snr = snprintf(b, sizeof(b), "%*.0f", w, f);
698 	if (snr != w)
699 		snr = snprintf(b, sizeof(b), "%*.0fk", w - 1, f / 1000);
700 	if (snr != w)
701 		snr = snprintf(b, sizeof(b), "%*.0fM", w - 1, f / 1000000);
702 	if (snr != w) {
703 		while (--w >= 0)
704 			addch('*');
705 		return;
706 	}
707 	addstr(b);
708 }
709 
710 void
711 putlongdouble(long double f, int l, int lc, int w, int d, int nz)
712 {
713 	int snr;
714 	char b[128];
715 
716 	move(l, lc);
717 #ifdef DEBUG
718 		while (--w >= 0)
719 			addch('*');
720 		return;
721 #endif
722 	if (nz && f == 0.0) {
723 		while (--w >= 0)
724 			addch(' ');
725 		return;
726 	}
727 	snr = snprintf(b, sizeof(b), "%*.*Lf", w, d, f);
728 	if (snr != w)
729 		snr = snprintf(b, sizeof(b), "%*.0Lf", w, f);
730 	if (snr != w)
731 		snr = snprintf(b, sizeof(b), "%*.0Lfk", w - 1, f / 1000);
732 	if (snr != w)
733 		snr = snprintf(b, sizeof(b), "%*.0LfM", w - 1, f / 1000000);
734 	if (snr != w) {
735 		while (--w >= 0)
736 			addch('*');
737 		return;
738 	}
739 	addstr(b);
740 }
741 
742 static void
743 getinfo(struct Info *ls)
744 {
745 	struct devinfo *tmp_dinfo;
746 	size_t size;
747 	int mib[2];
748 
749 	GETSYSCTL("kern.cp_time", ls->time);
750 	GETSYSCTL("kern.cp_time", cur_dev.cp_time);
751 	GETSYSCTL("vm.stats.sys.v_swtch", ls->v_swtch);
752 	GETSYSCTL("vm.stats.sys.v_trap", ls->v_trap);
753 	GETSYSCTL("vm.stats.sys.v_syscall", ls->v_syscall);
754 	GETSYSCTL("vm.stats.sys.v_intr", ls->v_intr);
755 	GETSYSCTL("vm.stats.sys.v_soft", ls->v_soft);
756 	GETSYSCTL("vm.stats.vm.v_vm_faults", ls->v_vm_faults);
757 	GETSYSCTL("vm.stats.vm.v_io_faults", ls->v_io_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_laundry_count", ls->v_laundry_count);
782 	GETSYSCTL("vfs.bufspace", ls->bufspace);
783 	GETSYSCTL("kern.maxvnodes", ls->maxvnodes);
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("vm.kmem_map_size", ls->v_kmem_map_size);
789 	getsysctl("hw.intrcnt", ls->intrcnt, nintr * sizeof(u_long));
790 
791 	size = sizeof(ls->Total);
792 	mib[0] = CTL_VM;
793 	mib[1] = VM_TOTAL;
794 	if (sysctl(mib, 2, &ls->Total, &size, NULL, 0) < 0) {
795 		error("Can't get kernel info: %s\n", strerror(errno));
796 		bzero(&ls->Total, sizeof(ls->Total));
797 	}
798 	size = sizeof(ncpu);
799 	if (sysctlbyname("hw.ncpu", &ncpu, &size, NULL, 0) < 0 ||
800 	    size != sizeof(ncpu))
801 		ncpu = 1;
802 
803 	tmp_dinfo = last_dev.dinfo;
804 	last_dev.dinfo = cur_dev.dinfo;
805 	cur_dev.dinfo = tmp_dinfo;
806 
807 	last_dev.snap_time = cur_dev.snap_time;
808 	dsgetinfo(&cur_dev);
809 }
810 
811 static void
812 allocinfo(struct Info *ls)
813 {
814 
815 	ls->intrcnt = (long *) calloc(nintr, sizeof(long));
816 	if (ls->intrcnt == NULL)
817 		errx(2, "out of memory");
818 }
819 
820 static void
821 copyinfo(struct Info *from, struct Info *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