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