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