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