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