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