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