xref: /dragonfly/usr.bin/systat/vmstat.c (revision 0ca59c34)
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. 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 /*
31  * Cursed vmstat -- from Robert Elz.
32  */
33 
34 #include <sys/user.h>
35 #include <sys/param.h>
36 #include <sys/stat.h>
37 #include <sys/time.h>
38 #include <sys/uio.h>
39 #include <sys/namei.h>
40 #include <sys/sysctl.h>
41 #include <sys/vmmeter.h>
42 
43 #include <vm/vm_param.h>
44 
45 #include <ctype.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <kinfo.h>
49 #include <langinfo.h>
50 #include <nlist.h>
51 #include <paths.h>
52 #include <signal.h>
53 #include <stddef.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <time.h>
57 #include <unistd.h>
58 #include "utmpentry.h"
59 #include <devstat.h>
60 #include "systat.h"
61 #include "extern.h"
62 #include "devs.h"
63 
64 static struct Info {
65 	struct kinfo_cputime cp_time;
66 	struct	vmmeter Vmm;
67 	struct	vmtotal Total;
68 	struct  vmstats Vms;
69 	struct	nchstats nchstats;
70 	long	nchcount;
71 	long	nchpathcount;
72 	long	*intrcnt;
73 	long	bufspace;
74 	int	desiredvnodes;
75 	int	cachedvnodes;
76 	int	inactivevnodes;
77 	int	activevnodes;
78 	long	dirtybufspace;
79 } s, s1, s2, z;
80 
81 struct kinfo_cputime cp_time, old_cp_time;
82 struct statinfo cur, last, run;
83 
84 #define	vmm s.Vmm
85 #define	vms s.Vms
86 #define oldvmm s1.Vmm
87 #define oldvms s1.Vms
88 #define	total s.Total
89 #define	nchtotal s.nchstats
90 #define	oldnchtotal s1.nchstats
91 
92 static	enum state { BOOT, TIME, RUN } state = TIME;
93 
94 static void allocinfo(struct Info *);
95 static void copyinfo(struct Info *, struct Info *);
96 static void dinfo(int, int, struct statinfo *, struct statinfo *);
97 static void getinfo(struct Info *);
98 static void put64(int64_t, int, int, int, int);
99 static void putfloat(double, int, int, int, int, int);
100 static void putlongdouble(long double, int, int, int, int, int);
101 static void putlongdoublez(long double, int, int, int, int, int);
102 static int ucount(void);
103 
104 static	int ncpu;
105 static	char buf[26];
106 static	time_t t;
107 static	double etime;
108 static	int nintr;
109 static	long *intrloc;
110 static	char **intrname;
111 static	int nextintsrow;
112 static  int extended_vm_stats;
113 
114 
115 
116 WINDOW *
117 openkre(void)
118 {
119 
120 	return (stdscr);
121 }
122 
123 void
124 closekre(WINDOW *w)
125 {
126 
127 	if (w == NULL)
128 		return;
129 	wclear(w);
130 	wrefresh(w);
131 }
132 
133 
134 static struct nlist namelist[] = {
135 #define	X_BUFFERSPACE	0
136 	{ .n_name = "_bufspace" },
137 #define	X_NCHSTATS	1
138 	{ .n_name = "_nchstats" },
139 #define	X_DESIREDVNODES	2
140 	{ .n_name = "_desiredvnodes" },
141 #define	X_CACHEDVNODES	3
142 	{ .n_name = "_cachedvnodes" },
143 #define	X_INACTIVEVNODES 4
144 	{ .n_name = "_inactivevnodes" },
145 #define	X_ACTIVEVNODES	5
146 	{ .n_name = "_activevnodes" },
147 #define X_NUMDIRTYBUFFERS 6
148 	{ .n_name = "_dirtybufspace" },
149 	{ .n_name = "" },
150 };
151 
152 /*
153  * These constants define where the major pieces are laid out
154  */
155 #define STATROW		 0	/* uses 1 row and 68 cols */
156 #define STATCOL		 2
157 #define MEMROW		 2	/* uses 4 rows and 31 cols */
158 #define MEMCOL		 0
159 #define PAGEROW		 2	/* uses 4 rows and 26 cols */
160 #define PAGECOL		46
161 #define INTSROW		 6	/* uses all rows to bottom and 17 cols */
162 #define INTSCOL		61
163 #define PROCSROW	 7	/* uses 2 rows and 20 cols */
164 #define PROCSCOL	 0
165 #define GENSTATROW	 7	/* uses 2 rows and 30 cols */
166 #define GENSTATCOL	16
167 #define VMSTATROW	 6	/* uses 17 rows and 12 cols */
168 #define VMSTATCOL	50
169 #define GRAPHROW	10	/* uses 3 rows and 51 cols */
170 #define GRAPHCOL	 0
171 #define NAMEIROW	14	/* uses 3 rows and 38 cols */
172 #define NAMEICOL	 0
173 #define EXECROW		14	/* uses 2 rows and 5 cols */
174 #define EXECCOL		38
175 #define DISKROW		17	/* uses 6 rows and 50 cols (for 9 drives) */
176 #define DISKCOL		 0
177 
178 #define	DRIVESPACE	 7	/* max # for space */
179 
180 #define	MAXDRIVES	DRIVESPACE	 /* max # to display */
181 
182 int
183 initkre(void)
184 {
185 	char *intrnamebuf;
186 	size_t bytes;
187 	size_t b;
188 	size_t i;
189 
190 	if (namelist[0].n_type == 0) {
191 		if (kvm_nlist(kd, namelist)) {
192 			nlisterr(namelist);
193 			return(0);
194 		}
195 		if (namelist[0].n_type == 0) {
196 			error("No namelist");
197 			return(0);
198 		}
199 	}
200 
201 	if ((num_devices = getnumdevs()) < 0) {
202 		warnx("%s", devstat_errbuf);
203 		return(0);
204 	}
205 
206 	cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
207 	last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
208 	run.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
209 	bzero(cur.dinfo, sizeof(struct devinfo));
210 	bzero(last.dinfo, sizeof(struct devinfo));
211 	bzero(run.dinfo, sizeof(struct devinfo));
212 
213 	if (dsinit(MAXDRIVES, &cur, &last, &run) != 1)
214 		return(0);
215 
216 	if (nintr == 0) {
217 		if (sysctlbyname("hw.intrnames", NULL, &bytes, NULL, 0) == 0) {
218 			intrnamebuf = malloc(bytes);
219 			sysctlbyname("hw.intrnames", intrnamebuf, &bytes,
220 					NULL, 0);
221 			for (i = 0; i < bytes; ++i) {
222 				if (intrnamebuf[i] == 0)
223 					++nintr;
224 			}
225 			intrname = malloc(nintr * sizeof(char *));
226 			intrloc = malloc(nintr * sizeof(*intrloc));
227 			nintr = 0;
228 			for (b = i = 0; i < bytes; ++i) {
229 				if (intrnamebuf[i] == 0) {
230 					intrname[nintr] = intrnamebuf + b;
231 					intrloc[nintr] = 0;
232 					b = i + 1;
233 					++nintr;
234 				}
235 			}
236 		}
237 		nextintsrow = INTSROW + 2;
238 		allocinfo(&s);
239 		allocinfo(&s1);
240 		allocinfo(&s2);
241 		allocinfo(&z);
242 	}
243 	getinfo(&s2);
244 	copyinfo(&s2, &s1);
245 	return(1);
246 }
247 
248 void
249 fetchkre(void)
250 {
251 	time_t now;
252 	struct tm *tp;
253 	static int d_first = -1;
254 
255 	if (d_first < 0)
256 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
257 
258 	time(&now);
259 	tp = localtime(&now);
260 	(void) strftime(buf, sizeof(buf),
261 			d_first ? "%e %b %R" : "%b %e %R", tp);
262 	getinfo(&s);
263 }
264 
265 void
266 labelkre(void)
267 {
268 	int i, j;
269 
270 	clear();
271 	mvprintw(STATROW, STATCOL + 4, "users    Load");
272 	mvprintw(MEMROW, MEMCOL, "Mem:      REAL            VIRTUAL");
273 	mvprintw(MEMROW + 1, MEMCOL, "       Tot  Share     Tot  Share");
274 	mvprintw(MEMROW + 2, MEMCOL, "Act");
275 	mvprintw(MEMROW + 3, MEMCOL, "All");
276 
277 	mvprintw(MEMROW + 1, MEMCOL + 36, "Free");
278 
279 	mvprintw(PAGEROW, PAGECOL,     "        VN PAGER  SWAP PAGER ");
280 	mvprintw(PAGEROW + 1, PAGECOL, "        in  out     in  out ");
281 	mvprintw(PAGEROW + 2, PAGECOL, "count");
282 	mvprintw(PAGEROW + 3, PAGECOL, "pages");
283 
284 	mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
285 	mvprintw(INTSROW + 1, INTSCOL + 9, "total");
286 
287 	mvprintw(VMSTATROW + 1, VMSTATCOL + 8, "cow");
288 	mvprintw(VMSTATROW + 2, VMSTATCOL + 8, "wire");
289 	mvprintw(VMSTATROW + 3, VMSTATCOL + 8, "act");
290 	mvprintw(VMSTATROW + 4, VMSTATCOL + 8, "inact");
291 	mvprintw(VMSTATROW + 5, VMSTATCOL + 8, "cache");
292 	mvprintw(VMSTATROW + 6, VMSTATCOL + 8, "free");
293 	mvprintw(VMSTATROW + 7, VMSTATCOL + 8, "daefr");
294 	mvprintw(VMSTATROW + 8, VMSTATCOL + 8, "prcfr");
295 	mvprintw(VMSTATROW + 9, VMSTATCOL + 8, "react");
296 	mvprintw(VMSTATROW + 10, VMSTATCOL + 8, "pdwake");
297 	mvprintw(VMSTATROW + 11, VMSTATCOL + 8, "pdpgs");
298 	mvprintw(VMSTATROW + 12, VMSTATCOL + 8, "intrn");
299 	mvprintw(VMSTATROW + 13, VMSTATCOL + 8, "buf");
300 	mvprintw(VMSTATROW + 14, VMSTATCOL + 8, "dirtybuf");
301 
302 	mvprintw(VMSTATROW + 15, VMSTATCOL + 8, "activ-vp");
303 	mvprintw(VMSTATROW + 16, VMSTATCOL + 8, "cachd-vp");
304 	mvprintw(VMSTATROW + 17, VMSTATCOL + 8, "inact-vp");
305 
306 	mvprintw(GENSTATROW, GENSTATCOL, "  Csw  Trp  Sys  Int  Sof  Flt");
307 
308 	mvprintw(GRAPHROW, GRAPHCOL,
309 		"  . %%Sys    . %%Intr   . %%User   . %%Nice   . %%Idle");
310 	mvprintw(PROCSROW, PROCSCOL, "   r   p   d   s");
311 	mvprintw(GRAPHROW + 1, GRAPHCOL,
312 		"|    |    |    |    |    |    |    |    |    |    |");
313 
314 	mvprintw(NAMEIROW, NAMEICOL, "Path-lookups   hits   %%    Components");
315 	mvprintw(EXECROW, EXECCOL, "Execs");
316 	mvprintw(DISKROW, DISKCOL, "Disks");
317 	mvprintw(DISKROW + 1, DISKCOL, "KB/t");
318 	mvprintw(DISKROW + 2, DISKCOL, "tpr/s");
319 	mvprintw(DISKROW + 3, DISKCOL, "MBr/s");
320 	mvprintw(DISKROW + 4, DISKCOL, "tpw/s");
321 	mvprintw(DISKROW + 5, DISKCOL, "MBw/s");
322 	mvprintw(DISKROW + 6, DISKCOL, "%% busy");
323 	/*
324 	 * For now, we don't support a fourth disk statistic.  So there's
325 	 * no point in providing a label for it.  If someone can think of a
326 	 * fourth useful disk statistic, there is room to add it.
327 	 */
328 	j = 0;
329 	for (i = 0; i < num_devices && j < MAXDRIVES; i++)
330 		if (dev_select[i].selected) {
331 			char tmpstr[80];
332 			sprintf(tmpstr, "%s%d", dev_select[i].device_name,
333 				dev_select[i].unit_number);
334 			mvprintw(DISKROW, DISKCOL + 5 + 6 * j,
335 				" %5.5s", tmpstr);
336 			j++;
337 		}
338 
339 	if (j <= 4) {
340 		/*
341 		 * room for extended VM stats
342 		 */
343 		mvprintw(VMSTATROW + 11, VMSTATCOL - 6, "zfod");
344 		mvprintw(VMSTATROW + 12, VMSTATCOL - 6, "ozfod");
345 		mvprintw(VMSTATROW + 13, VMSTATCOL - 6, "%%sloz");
346 		mvprintw(VMSTATROW + 14, VMSTATCOL - 6, "tfree");
347 		extended_vm_stats = 1;
348 	} else {
349 		extended_vm_stats = 0;
350 		mvprintw(VMSTATROW + 0, VMSTATCOL + 8, "zfod");
351 	}
352 
353 	for (i = 0; i < nintr; i++) {
354 		if (intrloc[i] == 0)
355 			continue;
356 		mvprintw(intrloc[i], INTSCOL + 9, "%-10.10s", intrname[i]);
357 	}
358 }
359 
360 #define CP_UPDATE(fld)	do {	\
361 	uint64_t lt;		\
362 	lt=s.fld;		\
363 	s.fld-=s1.fld;		\
364 	if(state==TIME)		\
365 		s1.fld=lt;	\
366 	lt=fld;			\
367 	fld-=old_##fld;		\
368 	if(state==TIME)		\
369 		old_##fld=lt;	\
370 	etime += s.fld;		\
371 } while(0)
372 #define X(fld)	{t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
373 #define Y(fld)	{t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
374 #define Z(fld)	{t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
375 	if(state == TIME) s1.nchstats.fld = t;}
376 #define PUTRATE(fld, l, c, w) \
377 	Y(fld); \
378 	put64((int64_t)((float)s.fld/etime + 0.5), l, c, w, 'D')
379 #define MAXFAIL 5
380 
381 #define CPUSTATES 5
382 static	const char cpuchar[5] = { '=' , '+', '>', '-', ' ' };
383 
384 static	const size_t cpuoffsets[] = {
385 	offsetof(struct kinfo_cputime, cp_sys),
386 	offsetof(struct kinfo_cputime, cp_intr),
387 	offsetof(struct kinfo_cputime, cp_user),
388 	offsetof(struct kinfo_cputime, cp_nice),
389 	offsetof(struct kinfo_cputime, cp_idle)
390 };
391 
392 void
393 showkre(void)
394 {
395 	float f1, f2;
396 	int psiz;
397 	int i, lc;
398 	long inttotal;
399 	long l;
400 	static int failcnt = 0;
401 	double total_time;
402 
403 	etime = 0;
404 	CP_UPDATE(cp_time.cp_user);
405 	CP_UPDATE(cp_time.cp_nice);
406 	CP_UPDATE(cp_time.cp_sys);
407 	CP_UPDATE(cp_time.cp_intr);
408 	CP_UPDATE(cp_time.cp_idle);
409 
410 	total_time = etime;
411 	if (total_time == 0.0)
412 		total_time = 1.0;
413 
414 	if (etime < 100000.0) {	/* < 100ms ignore this trash */
415 		if (failcnt++ >= MAXFAIL) {
416 			clear();
417 			mvprintw(2, 10, "The alternate system clock has died!");
418 			mvprintw(3, 10, "Reverting to ``pigs'' display.");
419 			move(CMDLINE, 0);
420 			refresh();
421 			failcnt = 0;
422 			sleep(5);
423 			command("pigs");
424 		}
425 		return;
426 	}
427 	failcnt = 0;
428 	etime /= 1000000.0;
429 	etime /= ncpu;
430 	if (etime == 0)
431 		etime = 1;
432 	inttotal = 0;
433 	for (i = 0; i < nintr; i++) {
434 		if (s.intrcnt[i] == 0)
435 			continue;
436 		if (intrloc[i] == 0) {
437 			if (nextintsrow == LINES)
438 				continue;
439 			intrloc[i] = nextintsrow++;
440 			mvprintw(intrloc[i], INTSCOL + 9, "%-10.10s",
441 				intrname[i]);
442 		}
443 		X(intrcnt);
444 		l = (long)((float)s.intrcnt[i]/etime + 0.5);
445 		inttotal += l;
446 		put64(l, intrloc[i], INTSCOL + 2, 6, 'D');
447 	}
448 	put64(inttotal, INTSROW + 1, INTSCOL + 2, 6, 'D');
449 	Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
450 	Z(ncs_longhits); Z(ncs_longmiss); Z(ncs_neghits);
451 	s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
452 	    nchtotal.ncs_miss + nchtotal.ncs_neghits;
453 	s.nchpathcount = nchtotal.ncs_longhits + nchtotal.ncs_longmiss;
454 	if (state == TIME) {
455 		s1.nchcount = s.nchcount;
456 		s1.nchpathcount = s.nchpathcount;
457 	}
458 
459 	psiz = 0;
460 	f2 = 0.0;
461 	for (lc = 0; lc < CPUSTATES; lc++) {
462 		uint64_t val = *(uint64_t *)(((uint8_t *)&s.cp_time) +
463 		    cpuoffsets[lc]);
464 		f1 = 100.0 * val / total_time;
465 		f2 += f1;
466 		l = (int) ((f2 + 1.0) / 2.0) - psiz;
467 		if (f1 > 99.9)
468 			f1 = 99.9;	/* no room to display 100.0 */
469 		putfloat(f1, GRAPHROW, GRAPHCOL + 10 * lc, 4, 1, 0);
470 		move(GRAPHROW + 2, psiz);
471 		psiz += l;
472 		while (l-- > 0)
473 			addch(cpuchar[lc]);
474 	}
475 
476 	put64(ucount(), STATROW, STATCOL, 3, 'D');
477 	putfloat(avenrun[0], STATROW, STATCOL + 18, 6, 2, 0);
478 	putfloat(avenrun[1], STATROW, STATCOL + 25, 6, 2, 0);
479 	putfloat(avenrun[2], STATROW, STATCOL + 32, 6, 2, 0);
480 	mvaddstr(STATROW, STATCOL + 53, buf);
481 #define pgtokb(pg) (int64_t)((intmax_t)(pg) * vms.v_page_size / 1024)
482 #define pgtomb(pg) (int64_t)((intmax_t)(pg) * vms.v_page_size / (1024 * 1024))
483 #define pgtob(pg)  (int64_t)((intmax_t)(pg) * vms.v_page_size)
484 	put64(pgtob(total.t_arm), MEMROW + 2, MEMCOL + 4, 6, 0);
485 	put64(pgtob(total.t_armshr), MEMROW + 2, MEMCOL + 11, 6, 0);
486 	put64(pgtob(total.t_avm), MEMROW + 2, MEMCOL + 19, 6, 0);
487 	put64(pgtob(total.t_avmshr), MEMROW + 2, MEMCOL + 26, 6, 0);
488 	put64(pgtob(total.t_rm), MEMROW + 3, MEMCOL + 4, 6, 0);
489 	put64(pgtob(total.t_rmshr), MEMROW + 3, MEMCOL + 11, 6, 0);
490 	put64(pgtob(total.t_vm), MEMROW + 3, MEMCOL + 19, 6, 0);
491 	put64(pgtob(total.t_vmshr), MEMROW + 3, MEMCOL + 26, 6, 0);
492 	put64(pgtob(total.t_free), MEMROW + 2, MEMCOL + 34, 6, 0);
493 	put64(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 0, 4, 'D');
494 	put64(total.t_pw, PROCSROW + 1, PROCSCOL + 4, 4, 'D');
495 	put64(total.t_dw, PROCSROW + 1, PROCSCOL + 8, 4, 'D');
496 	put64(total.t_sl, PROCSROW + 1, PROCSCOL + 12, 4, 'D');
497 	/*put64(total.t_sw, PROCSROW + 1, PROCSCOL + 12, 3, 'D');*/
498 	if (extended_vm_stats == 0) {
499 		PUTRATE(Vmm.v_zfod, VMSTATROW + 0, VMSTATCOL, 7);
500 	}
501 	PUTRATE(Vmm.v_cow_faults, VMSTATROW + 1, VMSTATCOL, 7);
502 	put64(pgtob(vms.v_wire_count), VMSTATROW + 2, VMSTATCOL, 7, 0);
503 	put64(pgtob(vms.v_active_count), VMSTATROW + 3, VMSTATCOL, 7, 0);
504 	put64(pgtob(vms.v_inactive_count), VMSTATROW + 4, VMSTATCOL, 7, 0);
505 	put64(pgtob(vms.v_cache_count), VMSTATROW + 5, VMSTATCOL, 7, 0);
506 	put64(pgtob(vms.v_free_count), VMSTATROW + 6, VMSTATCOL, 7, 0);
507 	PUTRATE(Vmm.v_dfree, VMSTATROW + 7, VMSTATCOL, 7);
508 	PUTRATE(Vmm.v_pfree, VMSTATROW + 8, VMSTATCOL, 7);
509 	PUTRATE(Vmm.v_reactivated, VMSTATROW + 9, VMSTATCOL, 7);
510 	PUTRATE(Vmm.v_pdwakeups, VMSTATROW + 10, VMSTATCOL, 7);
511 	PUTRATE(Vmm.v_pdpages, VMSTATROW + 11, VMSTATCOL, 7);
512 	PUTRATE(Vmm.v_intrans, VMSTATROW + 12, VMSTATCOL, 7);
513 
514 	if (extended_vm_stats) {
515 		PUTRATE(Vmm.v_zfod, VMSTATROW + 11, VMSTATCOL - 16, 9);
516 		PUTRATE(Vmm.v_ozfod, VMSTATROW + 12, VMSTATCOL - 16, 9);
517 #define nz(x)	((x) ? (x) : 1)
518 		put64((s.Vmm.v_zfod - s.Vmm.v_ozfod) * 100 / nz(s.Vmm.v_zfod),
519 		    VMSTATROW + 13, VMSTATCOL - 16, 9, 'D');
520 #undef nz
521 		PUTRATE(Vmm.v_tfree, VMSTATROW + 14, VMSTATCOL - 16, 9);
522 	}
523 
524 	put64(s.bufspace, VMSTATROW + 13, VMSTATCOL, 7, 0);
525 	put64(s.dirtybufspace/1024, VMSTATROW + 14, VMSTATCOL, 7, 'k');
526 	put64(s.activevnodes, VMSTATROW + 15, VMSTATCOL, 7, 'D');
527 	put64(s.cachedvnodes, VMSTATROW + 16, VMSTATCOL, 7, 'D');
528 	put64(s.inactivevnodes, VMSTATROW + 17, VMSTATCOL, 7, 'D');
529 	PUTRATE(Vmm.v_vnodein, PAGEROW + 2, PAGECOL + 6, 4);
530 	PUTRATE(Vmm.v_vnodeout, PAGEROW + 2, PAGECOL + 11, 4);
531 	PUTRATE(Vmm.v_swapin, PAGEROW + 2, PAGECOL + 18, 4);
532 	PUTRATE(Vmm.v_swapout, PAGEROW + 2, PAGECOL + 23, 4);
533 	PUTRATE(Vmm.v_vnodepgsin, PAGEROW + 3, PAGECOL + 6, 4);
534 	PUTRATE(Vmm.v_vnodepgsout, PAGEROW + 3, PAGECOL + 11, 4);
535 	PUTRATE(Vmm.v_swappgsin, PAGEROW + 3, PAGECOL + 18, 4);
536 	PUTRATE(Vmm.v_swappgsout, PAGEROW + 3, PAGECOL + 23, 4);
537 	PUTRATE(Vmm.v_swtch, GENSTATROW + 1, GENSTATCOL + 1, 4);
538 	PUTRATE(Vmm.v_trap, GENSTATROW + 1, GENSTATCOL + 6, 4);
539 	PUTRATE(Vmm.v_syscall, GENSTATROW + 1, GENSTATCOL + 11, 4);
540 	PUTRATE(Vmm.v_intr, GENSTATROW + 1, GENSTATCOL + 16, 4);
541 	PUTRATE(Vmm.v_soft, GENSTATROW + 1, GENSTATCOL + 21, 4);
542 	PUTRATE(Vmm.v_vm_faults, GENSTATROW + 1, GENSTATCOL + 26, 4);
543 	mvprintw(DISKROW, DISKCOL + 5, "                              ");
544 	for (i = 0, lc = 0; i < num_devices && lc < MAXDRIVES; i++)
545 		if (dev_select[i].selected) {
546 			char tmpstr[80];
547 			sprintf(tmpstr, "%s%d", dev_select[i].device_name,
548 				dev_select[i].unit_number);
549 			mvprintw(DISKROW, DISKCOL + 5 + 6 * lc,
550 				" %5.5s", tmpstr);
551 			switch(state) {
552 			case TIME:
553 				dinfo(i, ++lc, &cur, &last);
554 				break;
555 			case RUN:
556 				dinfo(i, ++lc, &cur, &run);
557 				break;
558 			case BOOT:
559 				dinfo(i, ++lc, &cur, NULL);
560 				break;
561 			}
562 		}
563 #define nz(x)	((x) ? (x) : 1)
564 	put64(s.nchpathcount, NAMEIROW + 1, NAMEICOL + 6, 6, 'D');
565 	PUTRATE(Vmm.v_exec, EXECROW + 1, EXECCOL, 5);
566 	put64(nchtotal.ncs_longhits, NAMEIROW + 1, NAMEICOL + 13, 6, 'D');
567 	putfloat(nchtotal.ncs_longhits * 100.0 / nz(s.nchpathcount),
568 	    NAMEIROW + 1, NAMEICOL + 19, 4, 0, 0);
569 
570 	putfloat((double)s.nchcount / nz(s.nchpathcount),
571 	    NAMEIROW + 1, NAMEICOL + 27, 5, 2, 1);
572 #undef nz
573 }
574 
575 int
576 cmdkre(const char *cmd, char *args)
577 {
578 	int retval;
579 
580 	if (prefix(cmd, "run")) {
581 		retval = 1;
582 		copyinfo(&s2, &s1);
583 		switch (getdevs(&run)) {
584 		case -1:
585 			errx(1, "%s", devstat_errbuf);
586 			break;
587 		case 1:
588 			num_devices = run.dinfo->numdevs;
589 			generation = run.dinfo->generation;
590 			retval = dscmd("refresh", NULL, MAXDRIVES, &cur);
591 			if (retval == 2)
592 				labelkre();
593 			break;
594 		default:
595 			break;
596 		}
597 		state = RUN;
598 		return (retval);
599 	}
600 	if (prefix(cmd, "boot")) {
601 		state = BOOT;
602 		copyinfo(&z, &s1);
603 		return (1);
604 	}
605 	if (prefix(cmd, "time")) {
606 		state = TIME;
607 		return (1);
608 	}
609 	if (prefix(cmd, "zero")) {
610 		retval = 1;
611 		if (state == RUN) {
612 			getinfo(&s1);
613 			switch (getdevs(&run)) {
614 			case -1:
615 				errx(1, "%s", devstat_errbuf);
616 				break;
617 			case 1:
618 				num_devices = run.dinfo->numdevs;
619 				generation = run.dinfo->generation;
620 				retval = dscmd("refresh",NULL, MAXDRIVES, &cur);
621 				if (retval == 2)
622 					labelkre();
623 				break;
624 			default:
625 				break;
626 			}
627 		}
628 		return (retval);
629 	}
630 	retval = dscmd(cmd, args, MAXDRIVES, &cur);
631 
632 	if (retval == 2)
633 		labelkre();
634 
635 	return(retval);
636 }
637 
638 /* calculate number of users on the system */
639 static int
640 ucount(void)
641 {
642 	struct utmpentry *ep;
643 	int nusers = 0;
644 
645 	getutentries(NULL, &ep);
646 	for (; ep; ep = ep->next)
647 		nusers++;
648 
649 	return (nusers);
650 }
651 
652 static void
653 put64(intmax_t n, int l, int lc, int w, int type)
654 {
655 	char b[128];
656 	int isneg;
657 	int i;
658 	int64_t d;
659 	int64_t u;
660 
661 	move(l, lc);
662 	if (n == 0) {
663 		while (w-- > 0)
664 			addch(' ');
665 		return;
666 	}
667 	if (type == 0 || type == 'D')
668 		snprintf(b, sizeof(b), "%*jd", w, n);
669 	else
670 		snprintf(b, sizeof(b), "%*jd%c", w - 1, n, type);
671 	if (strlen(b) <= (size_t)w) {
672 		addstr(b);
673 		return;
674 	}
675 
676 	if (type == 'D')
677 		u = 1000;
678 	else
679 		u = 1024;
680 	if (n < 0) {
681 		n = -n;
682 		isneg = 1;
683 	} else {
684 		isneg = 0;
685 	}
686 
687 	for (d = 1; n / d >= 1000; d *= u) {
688 		switch(type) {
689 		case 'D':
690 		case 0:
691 			type = 'k';
692 			break;
693 		case 'k':
694 			type = 'M';
695 			break;
696 		case 'M':
697 			type = 'G';
698 			break;
699 		case 'G':
700 			type = 'T';
701 			break;
702 		case 'T':
703 			type = 'X';
704 			break;
705 		default:
706 			type = '?';
707 			break;
708 		}
709 	}
710 
711 	i = w - isneg;
712 	if (n / d >= 100)
713 		i -= 3;
714 	else if (n / d >= 10)
715 		i -= 2;
716 	else
717 		i -= 1;
718 	if (i > 4) {
719 		snprintf(b + 64, sizeof(b) - 64, "%jd.%03jd%c",
720 			 n / d, n / (d / 1000) % 1000, type);
721 	} else if (i > 3) {
722 		snprintf(b + 64, sizeof(b) - 64, "%jd.%02jd%c",
723 			 n / d, n / (d / 100) % 100, type);
724 	} else if (i > 2) {
725 		snprintf(b + 64, sizeof(b) - 64, "%jd.%01jd%c",
726 			 n / d, n / (d / 10) % 10, type);
727 	} else {
728 		snprintf(b + 64, sizeof(b) - 64, "%jd%c",
729 			 n / d, type);
730 	}
731 	w -= strlen(b + 64);
732 	i = 64;
733 	if (isneg) {
734 		b[--i] = '-';
735 		--w;
736 	}
737 	while (w > 0) {
738 		--w;
739 		b[--i] = ' ';
740 	}
741 	addstr(b + i);
742 }
743 
744 static void
745 putfloat(double f, int l, int lc, int w, int d, int nz)
746 {
747 	char b[128];
748 
749 	move(l, lc);
750 	if (nz && f == 0.0) {
751 		while (--w >= 0)
752 			addch(' ');
753 		return;
754 	}
755 	snprintf(b, sizeof(b), "%*.*f", w, d, f);
756 	if (strlen(b) > (size_t)w)
757 		snprintf(b, sizeof(b), "%*.0f", w, f);
758 	if (strlen(b) > (size_t)w) {
759 		while (--w >= 0)
760 			addch('*');
761 		return;
762 	}
763 	addstr(b);
764 }
765 
766 static void
767 putlongdouble(long double f, int l, int lc, int w, int d, int nz)
768 {
769 	char b[128];
770 
771 	move(l, lc);
772 	if (nz && f == 0.0) {
773 		while (--w >= 0)
774 			addch(' ');
775 		return;
776 	}
777 	sprintf(b, "%*.*Lf", w, d, f);
778 	if (strlen(b) > (size_t)w)
779 		sprintf(b, "%*.0Lf", w, f);
780 	if (strlen(b) > (size_t)w) {
781 		while (--w >= 0)
782 			addch('*');
783 		return;
784 	}
785 	addstr(b);
786 }
787 
788 static void
789 putlongdoublez(long double f, int l, int lc, int w, int d, int nz)
790 {
791 	char b[128];
792 
793 	if (f == 0.0) {
794 		move(l, lc);
795 		sprintf(b, "%*.*s", w, w, "");
796 		addstr(b);
797 	} else {
798 		putlongdouble(f, l, lc, w, d, nz);
799 	}
800 }
801 
802 static void
803 getinfo(struct Info *ls)
804 {
805 	struct devinfo *tmp_dinfo;
806 	struct nchstats *nch_tmp;
807 	size_t size;
808 	size_t vms_size = sizeof(ls->Vms);
809 	size_t vmm_size = sizeof(ls->Vmm);
810 	size_t nch_size = sizeof(ls->nchstats) * SMP_MAXCPU;
811 
812         if (sysctlbyname("vm.vmstats", &ls->Vms, &vms_size, NULL, 0)) {
813                 perror("sysctlbyname: vm.vmstats");
814                 exit(1);
815         }
816         if (sysctlbyname("vm.vmmeter", &ls->Vmm, &vmm_size, NULL, 0)) {
817                 perror("sysctlbyname: vm.vmstats");
818                 exit(1);
819         }
820 
821 	if (kinfo_get_sched_cputime(&ls->cp_time))
822 		err(1, "kinfo_get_sched_cputime");
823 	if (kinfo_get_sched_cputime(&cp_time))
824 		err(1, "kinfo_get_sched_cputime");
825 	NREAD(X_BUFFERSPACE, &ls->bufspace, sizeof(ls->bufspace));
826 	NREAD(X_DESIREDVNODES, &ls->desiredvnodes, sizeof(ls->desiredvnodes));
827 	NREAD(X_CACHEDVNODES, &ls->cachedvnodes, sizeof(ls->cachedvnodes));
828 	NREAD(X_INACTIVEVNODES, &ls->inactivevnodes,
829 						sizeof(ls->inactivevnodes));
830 	NREAD(X_ACTIVEVNODES, &ls->activevnodes, sizeof(ls->activevnodes));
831 	NREAD(X_NUMDIRTYBUFFERS, &ls->dirtybufspace, sizeof(ls->dirtybufspace));
832 
833 	if (nintr) {
834 		size = nintr * sizeof(ls->intrcnt[0]);
835 		sysctlbyname("hw.intrcnt_all", ls->intrcnt, &size, NULL, 0);
836 	}
837 	size = sizeof(ls->Total);
838 	if (sysctlbyname("vm.vmtotal", &ls->Total, &size, NULL, 0) < 0) {
839 		error("Can't get kernel info: %s\n", strerror(errno));
840 		bzero(&ls->Total, sizeof(ls->Total));
841 	}
842 
843 	if ((nch_tmp = malloc(nch_size)) == NULL) {
844 		perror("malloc");
845 		exit(1);
846 	} else {
847 		if (sysctlbyname("vfs.cache.nchstats", nch_tmp, &nch_size, NULL, 0)) {
848 			perror("sysctlbyname vfs.cache.nchstats");
849 			free(nch_tmp);
850 			exit(1);
851 		} else {
852 			if ((nch_tmp = realloc(nch_tmp, nch_size)) == NULL) {
853 				perror("realloc");
854 				exit(1);
855 			}
856 		}
857 	}
858 
859 	if (kinfo_get_cpus(&ncpu))
860 		err(1, "kinfo_get_cpus");
861 	kvm_nch_cpuagg(nch_tmp, &ls->nchstats, ncpu);
862 	free(nch_tmp);
863 
864 	tmp_dinfo = last.dinfo;
865 	last.dinfo = cur.dinfo;
866 	cur.dinfo = tmp_dinfo;
867 
868 	last.busy_time = cur.busy_time;
869 	switch (getdevs(&cur)) {
870 	case -1:
871 		errx(1, "%s", devstat_errbuf);
872 		break;
873 	case 1:
874 		num_devices = cur.dinfo->numdevs;
875 		generation = cur.dinfo->generation;
876 		cmdkre("refresh", NULL);
877 		break;
878 	default:
879 		break;
880 	}
881 }
882 
883 static void
884 allocinfo(struct Info *ls)
885 {
886 	ls->intrcnt = (long *) calloc(nintr, sizeof(long));
887 	if (ls->intrcnt == NULL)
888 		errx(2, "out of memory");
889 }
890 
891 static void
892 copyinfo(struct Info *from, struct Info *to)
893 {
894 	long *intrcnt;
895 
896 	/*
897 	 * time, wds, seek, and xfer are malloc'd so we have to
898 	 * save the pointers before the structure copy and then
899 	 * copy by hand.
900 	 */
901 	intrcnt = to->intrcnt;
902 	*to = *from;
903 
904 	bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
905 }
906 
907 static void
908 dinfo(int dn, int lc, struct statinfo *now, struct statinfo *then)
909 {
910 	long double kb_per_transfer;
911 	long double transfers_per_secondr;
912 	long double transfers_per_secondw;
913 	long double mb_per_secondr;
914 	long double mb_per_secondw;
915 	long double elapsed_time, device_busy;
916 	int di;
917 
918 	di = dev_select[dn].position;
919 
920 	elapsed_time = compute_etime(now->busy_time, then ?
921 				     then->busy_time :
922 				     now->dinfo->devices[di].dev_creation_time);
923 
924 	device_busy =  compute_etime(now->dinfo->devices[di].busy_time, then ?
925 				     then->dinfo->devices[di].busy_time :
926 				     now->dinfo->devices[di].dev_creation_time);
927 
928 	if (compute_stats(
929 			  &now->dinfo->devices[di],
930 			  (then ? &then->dinfo->devices[di] : NULL),
931 			  elapsed_time,
932 			  NULL, NULL, NULL,
933 			  &kb_per_transfer,
934 			  NULL,
935 			  NULL,
936 			  NULL, NULL) != 0)
937 		errx(1, "%s", devstat_errbuf);
938 
939 	if (compute_stats_read(
940 			  &now->dinfo->devices[di],
941 			  (then ? &then->dinfo->devices[di] : NULL),
942 			  elapsed_time,
943 			  NULL, NULL, NULL,
944 			  NULL,
945 			  &transfers_per_secondr,
946 			  &mb_per_secondr,
947 			  NULL, NULL) != 0)
948 		errx(1, "%s", devstat_errbuf);
949 
950 	if (compute_stats_write(
951 			  &now->dinfo->devices[di],
952 			  (then ? &then->dinfo->devices[di] : NULL),
953 			  elapsed_time,
954 			  NULL, NULL, NULL,
955 			  NULL,
956 			  &transfers_per_secondw,
957 			  &mb_per_secondw,
958 			  NULL, NULL) != 0)
959 		errx(1, "%s", devstat_errbuf);
960 
961 #if 0
962 	/*
963 	 * Remove this hack, it no longer works properly and will
964 	 * report 100% busy in situations where the device is able
965 	 * to respond to the requests faster than the busy counter's
966 	 * granularity.
967 	 */
968 	if ((device_busy == 0) &&
969 	    (transfers_per_secondr > 5 || transfers_per_secondw > 5)) {
970 		/* the device has been 100% busy, fake it because
971 		 * as long as the device is 100% busy the busy_time
972 		 * field in the devstat struct is not updated */
973 		device_busy = elapsed_time;
974 	}
975 #endif
976 	if (device_busy > elapsed_time) {
977 		/* this normally happens after one or more periods
978 		 * where the device has been 100% busy, correct it */
979 		device_busy = elapsed_time;
980 	}
981 
982 	lc = DISKCOL + lc * 6;
983 	putlongdoublez(kb_per_transfer, DISKROW + 1, lc, 5, 2, 0);
984 	putlongdoublez(transfers_per_secondr, DISKROW + 2, lc, 5, 0, 0);
985 	putlongdoublez(mb_per_secondr, DISKROW + 3, lc, 5, 2, 0);
986 	putlongdoublez(transfers_per_secondw, DISKROW + 4, lc, 5, 0, 0);
987 	putlongdoublez(mb_per_secondw, DISKROW + 5, lc, 5, 2, 0);
988 	putlongdouble(device_busy * 100 / elapsed_time,
989 				      DISKROW + 6, lc, 5, 0, 0);
990 }
991