xref: /netbsd-src/usr.bin/systat/vmstat.c (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /*	$NetBSD: vmstat.c,v 1.61 2005/08/07 12:32:38 blymn Exp $	*/
2 
3 /*-
4  * Copyright (c) 1983, 1989, 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)vmstat.c	8.2 (Berkeley) 1/12/94";
36 #endif
37 __RCSID("$NetBSD: vmstat.c,v 1.61 2005/08/07 12:32:38 blymn Exp $");
38 #endif /* not lint */
39 
40 /*
41  * Cursed vmstat -- from Robert Elz.
42  */
43 
44 #include <sys/param.h>
45 #include <sys/user.h>
46 #include <sys/namei.h>
47 #include <sys/sysctl.h>
48 #include <sys/device.h>
49 
50 #include <uvm/uvm_extern.h>
51 
52 #include <stdlib.h>
53 #include <string.h>
54 #include <util.h>
55 
56 #include "systat.h"
57 #include "extern.h"
58 #include "dkstats.h"
59 #include "tpstats.h"
60 #include "utmpentry.h"
61 
62 static struct Info {
63 	struct	uvmexp_sysctl uvmexp;
64 	struct	vmtotal Total;
65 	struct	nchstats nchstats;
66 	long	nchcount;
67 	long	*intrcnt;
68 	u_int64_t	*evcnt;
69 } s, s1, s2, z;
70 
71 #define	cnt s.Cnt
72 #define oldcnt s1.Cnt
73 #define	total s.Total
74 #define	nchtotal s.nchstats
75 #define	oldnchtotal s1.nchstats
76 
77 static	enum state { BOOT, TIME, RUN } state = TIME;
78 
79 static void allocinfo(struct Info *);
80 static void copyinfo(struct Info *, struct Info *);
81 static float cputime(int);
82 static void dinfo(int, int, int);
83 static void tinfo(int, int, int);
84 static void getinfo(struct Info *, enum state);
85 static void putint(int, int, int, int);
86 static void putfloat(double, int, int, int, int, int);
87 static int ucount(void);
88 
89 static	char buf[26];
90 static	u_int64_t temp;
91 static	double etime;
92 static	float hertz;
93 static	int nintr;
94 static	long *intrloc;
95 static	char **intrname;
96 static	int nextintsrow;
97 static	int disk_horiz = 1;
98 
99 WINDOW *
100 openvmstat(void)
101 {
102 	return (stdscr);
103 }
104 
105 void
106 closevmstat(WINDOW *w)
107 {
108 
109 	if (w == NULL)
110 		return;
111 	wclear(w);
112 	wrefresh(w);
113 }
114 
115 
116 static struct nlist namelist[] = {
117 #define	X_NCHSTATS	0
118 	{ "_nchstats" },
119 #define	X_INTRNAMES	1
120 	{ "_intrnames" },
121 #define	X_EINTRNAMES	2
122 	{ "_eintrnames" },
123 #define	X_INTRCNT	3
124 	{ "_intrcnt" },
125 #define	X_EINTRCNT	4
126 	{ "_eintrcnt" },
127 #define	X_ALLEVENTS	5
128 	{ "_allevents" },
129 	{ NULL }
130 };
131 
132 /*
133  * These constants define where the major pieces are laid out
134  */
135 #define STATROW		 0	/* uses 1 row and 68 cols */
136 #define STATCOL		 2
137 #define MEMROW		 9	/* uses 4 rows and 31 cols */
138 #define MEMCOL		 0
139 #define PAGEROW		 2	/* uses 4 rows and 26 cols */
140 #define PAGECOL		54
141 #define INTSROW		 9	/* uses all rows to bottom and 17 cols */
142 #define INTSCOL		40
143 #define INTSCOLEND	(VMSTATCOL - 0)
144 #define PROCSROW	 2	/* uses 2 rows and 20 cols */
145 #define PROCSCOL	 0
146 #define GENSTATROW	 2	/* uses 2 rows and 30 cols */
147 #define GENSTATCOL	17
148 #define VMSTATROW	 7	/* uses 17 rows and 15 cols */
149 #define VMSTATCOL	64
150 #define GRAPHROW	 5	/* uses 3 rows and 51 cols */
151 #define GRAPHCOL	 0
152 #define NAMEIROW	14	/* uses 3 rows and 38 cols */
153 #define NAMEICOL	 0
154 #define DISKROW		18	/* uses 5 rows and 50 cols (for 9 drives) */
155 #define DISKCOL		 0
156 #define DISKCOLWIDTH	 6
157 #define DISKCOLEND	INTSCOL
158 
159 typedef struct intr_evcnt intr_evcnt_t;
160 struct intr_evcnt {
161 	char		*ie_group;
162 	char		*ie_name;
163 	u_int64_t	*ie_count;	/* kernel address... */
164 	int		ie_loc;		/* screen row */
165 } *ie_head;
166 int nevcnt;
167 
168 static void
169 get_interrupt_events(void)
170 {
171 	struct evcntlist allevents;
172 	struct evcnt evcnt, *evptr;
173 	intr_evcnt_t *ie;
174 	intr_evcnt_t *n;
175 
176 	if (!NREAD(X_ALLEVENTS, &allevents, sizeof allevents))
177 		return;
178 	evptr = TAILQ_FIRST(&allevents);
179 	for (; evptr != NULL; evptr = TAILQ_NEXT(&evcnt, ev_list)) {
180 		if (!KREAD(evptr, &evcnt, sizeof evcnt))
181 			return;
182 		if (evcnt.ev_type != EVCNT_TYPE_INTR)
183 			continue;
184 		n = realloc(ie_head, sizeof *ie * (nevcnt + 1));
185 		if (n == NULL) {
186 			error("realloc failed");
187 			die(0);
188 		}
189 		ie_head = n;
190 		ie = ie_head + nevcnt;
191 		ie->ie_group = malloc(evcnt.ev_grouplen + 1);
192 		ie->ie_name = malloc(evcnt.ev_namelen + 1);
193 		if (ie->ie_group == NULL || ie->ie_name == NULL)
194 			return;
195 		if (!KREAD(evcnt.ev_group, ie->ie_group, evcnt.ev_grouplen + 1))
196 			return;
197 		if (!KREAD(evcnt.ev_name, ie->ie_name, evcnt.ev_namelen + 1))
198 			return;
199 		ie->ie_count = &evptr->ev_count;
200 		ie->ie_loc = 0;
201 		nevcnt++;
202 	}
203 }
204 
205 int
206 initvmstat(void)
207 {
208 	char *intrnamebuf, *cp;
209 	int i;
210 
211 	if (namelist[0].n_type == 0) {
212 		if (kvm_nlist(kd, namelist) &&
213 		    (namelist[X_NCHSTATS].n_type == 0 ||
214 		     namelist[X_ALLEVENTS].n_type == 0)) {
215 			nlisterr(namelist);
216 			return(0);
217 		}
218 		if (namelist[0].n_type == 0) {
219 			error("No namelist");
220 			return(0);
221 		}
222 	}
223 	hertz = stathz ? stathz : hz;
224 	if (!dkinit(1))
225 		return(0);
226 	if (!tpinit(1))
227 		return(0);
228 
229 	/* Old style interrupt counts - deprecated */
230 	nintr = (namelist[X_EINTRCNT].n_value -
231 		namelist[X_INTRCNT].n_value) / sizeof (long);
232 	if (nintr) {
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 == NULL || 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 	}
255 
256 	/* event counter interrupt counts */
257 	get_interrupt_events();
258 
259 	nextintsrow = INTSROW + 1;
260 	allocinfo(&s);
261 	allocinfo(&s1);
262 	allocinfo(&s2);
263 	allocinfo(&z);
264 
265 	getinfo(&s2, RUN);
266 	copyinfo(&s2, &s1);
267 	return(1);
268 }
269 
270 void
271 fetchvmstat(void)
272 {
273 	time_t now;
274 
275 	time(&now);
276 	strlcpy(buf, ctime(&now), sizeof(buf));
277 	buf[19] = '\0';
278 	getinfo(&s, state);
279 }
280 
281 static void
282 print_ie_title(int i)
283 {
284 	int width, name_width, group_width;
285 
286 	width = INTSCOLEND - (INTSCOL + 9);
287 	if (width <= 0)
288 		return;
289 
290 	move(ie_head[i].ie_loc, INTSCOL + 9);
291 	group_width = strlen(ie_head[i].ie_group);
292 	name_width = strlen(ie_head[i].ie_name);
293 	width -= group_width + 1 + name_width;
294 	if (width < 0) {
295 		/*
296 		 * Screen to narrow for full strings
297 		 * This is all rather horrid, in some cases there are a lot
298 		 * of events in the same group, and in others the event
299 		 * name is "intr".  There are also names which need 7 or 8
300 		 * columns before they become meaningful.
301 		 * This is a bad compromise.
302 		 */
303 		width = -width;
304 		group_width -= (width + 1) / 2;
305 		name_width -= width / 2;
306 		/* some have the 'useful' name "intr", display their group */
307 		if (strcasecmp(ie_head[i].ie_name, "intr") == 0) {
308 			 group_width += name_width + 1;
309 			 name_width = 0;
310 		} else {
311 			if (group_width <= 3 || name_width < 0) {
312 				/* don't display group */
313 				name_width += group_width + 1;
314 				group_width = 0;
315 			}
316 		}
317 	}
318 
319 	if (group_width != 0) {
320 		printw("%-.*s", group_width, ie_head[i].ie_group);
321 		if (name_width != 0)
322 			printw(" ");
323 	}
324 	if (name_width != 0)
325 		printw("%-.*s", name_width, ie_head[i].ie_name);
326 }
327 
328 void
329 labelvmstat(void)
330 {
331 	int i;
332 
333 	clear();
334 	mvprintw(STATROW, STATCOL + 4, "users    Load");
335 	mvprintw(MEMROW, MEMCOL,     "           memory totals (in kB)");
336 	mvprintw(MEMROW + 1, MEMCOL, "          real  virtual     free");
337 	mvprintw(MEMROW + 2, MEMCOL, "Active");
338 	mvprintw(MEMROW + 3, MEMCOL, "All");
339 
340 	mvprintw(PAGEROW, PAGECOL, "        PAGING   SWAPPING ");
341 	mvprintw(PAGEROW + 1, PAGECOL, "        in  out   in  out ");
342 	mvprintw(PAGEROW + 2, PAGECOL, "ops");
343 	mvprintw(PAGEROW + 3, PAGECOL, "pages");
344 
345 	mvprintw(INTSROW, INTSCOL + 9, "Interrupts");
346 
347 	mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "forks");
348 	mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "fkppw");
349 	mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "fksvm");
350 	mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "pwait");
351 	mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "relck");
352 	mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "rlkok");
353 	mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "noram");
354 	mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "ndcpy");
355 	mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "fltcp");
356 	mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "zfod");
357 	mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "cow");
358 	mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "fmin");
359 	mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "ftarg");
360 	mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "itarg");
361 	mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "wired");
362 	mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "pdfre");
363 	if (LINES - 1 > VMSTATROW + 16)
364 		mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "pdscn");
365 
366 	mvprintw(GENSTATROW, GENSTATCOL, "  Csw   Trp   Sys  Int  Sof   Flt");
367 
368 	mvprintw(GRAPHROW, GRAPHCOL,
369 		"    . %% Sy    . %% Us    . %% Ni    . %% In    . %% Id");
370 	mvprintw(PROCSROW, PROCSCOL, "Proc:r  d  s  w");
371 	mvprintw(GRAPHROW + 1, GRAPHCOL,
372 		"|    |    |    |    |    |    |    |    |    |    |");
373 
374 	mvprintw(NAMEIROW, NAMEICOL, "Namei         Sys-cache     Proc-cache");
375 	mvprintw(NAMEIROW + 1, NAMEICOL,
376 		"    Calls     hits    %%     hits     %%");
377 	mvprintw(DISKROW, DISKCOL, "Disks:");
378 	if (disk_horiz) {
379 		mvprintw(DISKROW + 1, DISKCOL + 1, "seeks");
380 		mvprintw(DISKROW + 2, DISKCOL + 1, "xfers");
381 		mvprintw(DISKROW + 3, DISKCOL + 1, "bytes");
382 		mvprintw(DISKROW + 4, DISKCOL + 1, "%%busy");
383 	} else {
384 		mvprintw(DISKROW, DISKCOL + 1 + 1 * DISKCOLWIDTH, "seeks");
385 		mvprintw(DISKROW, DISKCOL + 1 + 2 * DISKCOLWIDTH, "xfers");
386 		mvprintw(DISKROW, DISKCOL + 1 + 3 * DISKCOLWIDTH, "bytes");
387 		mvprintw(DISKROW, DISKCOL + 1 + 4 * DISKCOLWIDTH, "%%busy");
388 	}
389 	for (i = 0; i < nintr; i++) {
390 		if (intrloc[i] == 0)
391 			continue;
392 		mvprintw(intrloc[i], INTSCOL + 9, "%-.*s",
393 			INTSCOLEND - (INTSCOL + 9), intrname[i]);
394 	}
395 	for (i = 0; i < nevcnt; i++) {
396 		if (ie_head[i].ie_loc == 0)
397 			continue;
398 		print_ie_title(i);
399 	}
400 }
401 
402 #define X(fld)	{temp=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=temp;}
403 #define Y(fld)	{temp = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = temp;}
404 #define Z(fld)	{temp = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
405 	if(state == TIME) s1.nchstats.fld = temp;}
406 #define PUTRATE(fld, l, c, w) {Y(fld); putint((int)((float)s.fld/etime + 0.5), l, c, w);}
407 #define MAXFAIL 5
408 
409 static	char cpuchar[CPUSTATES] = { '=' , '>', '-', '%', ' ' };
410 static	char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_INTR, CP_IDLE };
411 
412 void
413 showvmstat(void)
414 {
415 	float f1, f2;
416 	int psiz, inttotal;
417 	int i, l, r, c;
418 	static int failcnt = 0;
419 	static int relabel = 0;
420 	static int last_disks = 0;
421 	static char pigs[] = "pigs";
422 
423 	if (relabel) {
424 		labelvmstat();
425 		relabel = 0;
426 	}
427 
428 	if (state == TIME) {
429 		dkswap();
430 		tpswap();
431 		etime = cur.cp_etime;
432 		/* < 5 ticks - ignore this trash */
433 		if ((etime * hertz) < 1.0) {
434 			if (failcnt++ > MAXFAIL)
435 				return;
436 			clear();
437 			mvprintw(2, 10, "The alternate system clock has died!");
438 			mvprintw(3, 10, "Reverting to ``pigs'' display.");
439 			move(CMDLINE, 0);
440 			refresh();
441 			failcnt = 0;
442 			sleep(5);
443 			command(pigs);
444 			return;
445 		}
446 	} else
447 		etime = 1.0;
448 
449 	failcnt = 0;
450 	inttotal = 0;
451 	for (i = 0; i < nintr; i++) {
452 		if (s.intrcnt[i] == 0)
453 			continue;
454 		if (intrloc[i] == 0) {
455 			if (nextintsrow == LINES)
456 				continue;
457 			intrloc[i] = nextintsrow++;
458 			mvprintw(intrloc[i], INTSCOL + 9, "%-.*s",
459 				INTSCOLEND - (INTSCOL + 9), intrname[i]);
460 		}
461 		X(intrcnt);
462 		l = (int)((float)s.intrcnt[i]/etime + 0.5);
463 		inttotal += l;
464 		putint(l, intrloc[i], INTSCOL, 8);
465 	}
466 	for (i = 0; i < nevcnt; i++) {
467 		if (s.evcnt[i] == 0)
468 			continue;
469 		if (ie_head[i].ie_loc == 0) {
470 			if (nextintsrow == LINES)
471 				continue;
472 			ie_head[i].ie_loc = nextintsrow++;
473 			print_ie_title(i);
474 		}
475 		X(evcnt);
476 		l = (int)((float)s.evcnt[i]/etime + 0.5);
477 		inttotal += l;
478 		putint(l, ie_head[i].ie_loc, INTSCOL, 8);
479 	}
480 	putint(inttotal, INTSROW, INTSCOL, 8);
481 	Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
482 	Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes);
483 	s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
484 	    nchtotal.ncs_miss + nchtotal.ncs_long;
485 	if (state == TIME)
486 		s1.nchcount = s.nchcount;
487 
488 	psiz = 0;
489 	f2 = 0.0;
490 
491 	/*
492 	 * Last CPU state not calculated yet.
493 	 */
494 	for (c = 0; c < CPUSTATES; c++) {
495 		i = cpuorder[c];
496 		f1 = cputime(i);
497 		f2 += f1;
498 		l = (int) ((f2 + 1.0) / 2.0) - psiz;
499 		if (c == 0)
500 			putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0);
501 		else
502 			putfloat(f1, GRAPHROW, GRAPHCOL + 10 * c + 1, 5, 1, 0);
503 		mvhline(GRAPHROW + 2, psiz, cpuchar[c], l);
504 		psiz += l;
505 	}
506 
507 	putint(ucount(), STATROW, STATCOL, 3);
508 	putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
509 	putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
510 	putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
511 	mvaddstr(STATROW, STATCOL + 53, buf);
512 #define pgtokb(pg)	((pg) * (s.uvmexp.pagesize / 1024))
513 
514 	putint(pgtokb(s.uvmexp.active), MEMROW + 2, MEMCOL + 6, 8);
515 	putint(pgtokb(s.uvmexp.active + s.uvmexp.swpginuse),	/* XXX */
516 	    MEMROW + 2, MEMCOL + 15, 8);
517 	putint(pgtokb(s.uvmexp.npages - s.uvmexp.free), MEMROW + 3, MEMCOL + 6, 8);
518 	putint(pgtokb(s.uvmexp.npages - s.uvmexp.free + s.uvmexp.swpginuse),
519 	    MEMROW + 3, MEMCOL + 15, 8);
520 	putint(pgtokb(s.uvmexp.free), MEMROW + 2, MEMCOL + 24, 8);
521 	putint(pgtokb(s.uvmexp.free + s.uvmexp.swpages - s.uvmexp.swpginuse),
522 	    MEMROW + 3, MEMCOL + 24, 8);
523 	putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
524 	putint(total.t_dw, PROCSROW + 1, PROCSCOL + 6, 3);
525 	putint(total.t_sl, PROCSROW + 1, PROCSCOL + 9, 3);
526 	putint(total.t_sw, PROCSROW + 1, PROCSCOL + 12, 3);
527 	PUTRATE(uvmexp.forks, VMSTATROW + 0, VMSTATCOL + 3, 6);
528 	PUTRATE(uvmexp.forks_ppwait, VMSTATROW + 1, VMSTATCOL + 3, 6);
529 	PUTRATE(uvmexp.forks_sharevm, VMSTATROW + 2, VMSTATCOL + 3, 6);
530 	PUTRATE(uvmexp.fltpgwait, VMSTATROW + 3, VMSTATCOL + 4, 5);
531 	PUTRATE(uvmexp.fltrelck, VMSTATROW + 4, VMSTATCOL + 3, 6);
532 	PUTRATE(uvmexp.fltrelckok, VMSTATROW + 5, VMSTATCOL + 3, 6);
533 	PUTRATE(uvmexp.fltnoram, VMSTATROW + 6, VMSTATCOL + 3, 6);
534 	PUTRATE(uvmexp.fltamcopy, VMSTATROW + 7, VMSTATCOL + 3, 6);
535 	PUTRATE(uvmexp.flt_prcopy, VMSTATROW + 8, VMSTATCOL + 3, 6);
536 	PUTRATE(uvmexp.flt_przero, VMSTATROW + 9, VMSTATCOL + 3, 6);
537 	PUTRATE(uvmexp.flt_acow, VMSTATROW + 10, VMSTATCOL, 9);
538 	putint(s.uvmexp.freemin, VMSTATROW + 11, VMSTATCOL, 9);
539 	putint(s.uvmexp.freetarg, VMSTATROW + 12, VMSTATCOL, 9);
540 	putint(s.uvmexp.inactarg, VMSTATROW + 13, VMSTATCOL, 9);
541 	putint(s.uvmexp.wired, VMSTATROW + 14, VMSTATCOL, 9);
542 	PUTRATE(uvmexp.pdfreed, VMSTATROW + 15, VMSTATCOL, 9);
543 	if (LINES - 1 > VMSTATROW + 16)
544 		PUTRATE(uvmexp.pdscans, VMSTATROW + 16, VMSTATCOL, 9);
545 
546 	PUTRATE(uvmexp.pageins, PAGEROW + 2, PAGECOL + 5, 5);
547 	PUTRATE(uvmexp.pdpageouts, PAGEROW + 2, PAGECOL + 10, 5);
548 	PUTRATE(uvmexp.swapins, PAGEROW + 2, PAGECOL + 15, 5);
549 	PUTRATE(uvmexp.swapouts, PAGEROW + 2, PAGECOL + 20, 5);
550 	PUTRATE(uvmexp.pgswapin, PAGEROW + 3, PAGECOL + 5, 5);
551 	PUTRATE(uvmexp.pgswapout, PAGEROW + 3, PAGECOL + 10, 5);
552 
553 	PUTRATE(uvmexp.swtch, GENSTATROW + 1, GENSTATCOL, 5);
554 	PUTRATE(uvmexp.traps, GENSTATROW + 1, GENSTATCOL + 5, 6);
555 	PUTRATE(uvmexp.syscalls, GENSTATROW + 1, GENSTATCOL + 11, 6);
556 	PUTRATE(uvmexp.intrs, GENSTATROW + 1, GENSTATCOL + 17, 5);
557 	PUTRATE(uvmexp.softs, GENSTATROW + 1, GENSTATCOL + 22, 5);
558 	PUTRATE(uvmexp.faults, GENSTATROW + 1, GENSTATCOL + 27, 6);
559 	for (l = 0, i = 0, r = DISKROW, c = DISKCOL;
560 	     i < (dk_ndrive + tp_ndrive); i++) {
561 		if (i < dk_ndrive) {
562 			if (!dk_select[i])
563 				continue;
564 		} else {
565 			if (!tp_select[i - dk_ndrive])
566 				continue;
567 		}
568 
569 		if (disk_horiz)
570 			c += DISKCOLWIDTH;
571 		else
572 			r++;
573 		if (c + DISKCOLWIDTH > DISKCOLEND) {
574 			if (disk_horiz && LINES - 1 - DISKROW >
575 			    (DISKCOLEND - DISKCOL) / DISKCOLWIDTH) {
576 				disk_horiz = 0;
577 				relabel = 1;
578 			}
579 			break;
580 		}
581 		if (r >= LINES - 1) {
582 			if (!disk_horiz && LINES - 1 - DISKROW <
583 			    (DISKCOLEND - DISKCOL) / DISKCOLWIDTH) {
584 				disk_horiz = 1;
585 				relabel = 1;
586 			}
587 			break;
588 		}
589 		l++;
590 
591 		if (i < dk_ndrive)
592 			dinfo(i, r, c);
593 		else
594 			tinfo(i - dk_ndrive, r, c);
595 	}
596 	/* blank out if we lost any disks */
597 	for (i = l; i < last_disks; i++) {
598 		int j;
599 		if (disk_horiz)
600 			c += DISKCOLWIDTH;
601 		else
602 			r++;
603 		for (j = 0; j < 5; j++) {
604 			if (disk_horiz)
605 				mvprintw(r+j, c, "%*s", DISKCOLWIDTH, "");
606 			else
607 				mvprintw(r, c+j*DISKCOLWIDTH, "%*s", DISKCOLWIDTH, "");
608 		}
609 	}
610 	last_disks = l;
611 	putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
612 	putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 9, 9);
613 #define nz(x)	((x) ? (x) : 1)
614 	putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount),
615 	   NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
616 	putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
617 	putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
618 	   NAMEIROW + 2, NAMEICOL + 34, 4, 0, 1);
619 #undef nz
620 }
621 
622 void
623 vmstat_boot(char *args)
624 {
625 	copyinfo(&z, &s1);
626 	state = BOOT;
627 }
628 
629 void
630 vmstat_run(char *args)
631 {
632 	copyinfo(&s1, &s2);
633 	state = RUN;
634 }
635 
636 void
637 vmstat_time(char *args)
638 {
639 	state = TIME;
640 }
641 
642 void
643 vmstat_zero(char *args)
644 {
645 	if (state == RUN)
646 		getinfo(&s1, RUN);
647 }
648 
649 /* calculate number of users on the system */
650 static int
651 ucount(void)
652 {
653 	static int onusers = -1;
654 	static struct utmpentry *oehead = NULL;
655 	int nusers = 0;
656 	struct utmpentry *ehead;
657 
658 	nusers = getutentries(NULL, &ehead);
659 	if (oehead != ehead) {
660 		freeutentries(oehead);
661 		oehead = ehead;
662 	}
663 
664 	if (nusers != onusers) {
665 		if (nusers == 1)
666 			mvprintw(STATROW, STATCOL + 8, " ");
667 		else
668 			mvprintw(STATROW, STATCOL + 8, "s");
669 	}
670 	onusers = nusers;
671 	return (nusers);
672 }
673 
674 static float
675 cputime(int indx)
676 {
677 	double t;
678 	int i;
679 
680 	t = 0;
681 	for (i = 0; i < CPUSTATES; i++)
682 		t += cur.cp_time[i];
683 	if (t == 0.0)
684 		t = 1.0;
685 	return (cur.cp_time[indx] * 100.0 / t);
686 }
687 
688 static void
689 puthumanint(u_int64_t n, int l, int c, int w)
690 {
691 	char b[128];
692 
693 	if (move(l, c) != OK)
694 		return;
695 	if (n == 0) {
696 		hline(' ', w);
697 		return;
698 	}
699 	if (humanize_number(b, w, n, "", HN_AUTOSCALE, HN_NOSPACE) == -1 ) {
700 		hline('*', w);
701 		return;
702 	}
703 	hline(' ', w - strlen(b));
704 	mvaddstr(l, c + w - strlen(b), b);
705 }
706 
707 static void
708 putint(int n, int l, int c, int w)
709 {
710 	char b[128];
711 
712 	if (move(l, c) != OK)
713 		return;
714 	if (n == 0) {
715 		hline(' ', w);
716 		return;
717 	}
718 	(void)snprintf(b, sizeof b, "%*d", w, n);
719 	if (strlen(b) > w) {
720 		hline('*', w);
721 		return;
722 	}
723 	addstr(b);
724 }
725 
726 static void
727 putfloat(double f, int l, int c, int w, int d, int nz)
728 {
729 	char b[128];
730 
731 	if (move(l, c) != OK)
732 		return;
733 	if (nz && f == 0.0) {
734 		hline(' ', w);
735 		return;
736 	}
737 	(void)snprintf(b, sizeof b, "%*.*f", w, d, f);
738 	if (strlen(b) > w) {
739 		hline('*', w);
740 		return;
741 	}
742 	addstr(b);
743 }
744 
745 static void
746 getinfo(struct Info *stats, enum state st)
747 {
748 	int mib[2];
749 	size_t size;
750 	int i;
751 
752 	dkreadstats();
753 	tpreadstats();
754 	NREAD(X_NCHSTATS, &stats->nchstats, sizeof stats->nchstats);
755 	if (nintr)
756 		NREAD(X_INTRCNT, stats->intrcnt, nintr * LONG);
757 	for (i = 0; i < nevcnt; i++)
758 		KREAD(ie_head[i].ie_count, &stats->evcnt[i],
759 		      sizeof stats->evcnt[i]);
760 	size = sizeof(stats->uvmexp);
761 	mib[0] = CTL_VM;
762 	mib[1] = VM_UVMEXP2;
763 	if (sysctl(mib, 2, &stats->uvmexp, &size, NULL, 0) < 0) {
764 		error("can't get uvmexp: %s\n", strerror(errno));
765 		memset(&stats->uvmexp, 0, sizeof(stats->uvmexp));
766 	}
767 	size = sizeof(stats->Total);
768 	mib[0] = CTL_VM;
769 	mib[1] = VM_METER;
770 	if (sysctl(mib, 2, &stats->Total, &size, NULL, 0) < 0) {
771 		error("Can't get kernel info: %s\n", strerror(errno));
772 		memset(&stats->Total, 0, sizeof(stats->Total));
773 	}
774 }
775 
776 static void
777 allocinfo(struct Info *stats)
778 {
779 
780 	if (nintr &&
781 	    (stats->intrcnt = calloc(nintr, sizeof(long))) == NULL) {
782 		error("calloc failed");
783 		die(0);
784 	}
785 	if ((stats->evcnt = calloc(nevcnt, sizeof(u_int64_t))) == NULL) {
786 		error("calloc failed");
787 		die(0);
788 	}
789 }
790 
791 static void
792 copyinfo(struct Info *from, struct Info *to)
793 {
794 	long *intrcnt;
795 	u_int64_t *evcnt;
796 
797 	intrcnt = to->intrcnt;
798 	evcnt = to->evcnt;
799 	*to = *from;
800 	memmove(to->intrcnt = intrcnt, from->intrcnt, nintr * sizeof *intrcnt);
801 	memmove(to->evcnt = evcnt, from->evcnt, nevcnt * sizeof *evcnt);
802 }
803 
804 static void
805 dinfo(int dn, int r, int c)
806 {
807 	double atime;
808 #define ADV if (disk_horiz) r++; else c += DISKCOLWIDTH
809 
810 	mvprintw(r, c, "%*.*s", DISKCOLWIDTH, DISKCOLWIDTH, dr_name[dn]);
811 	ADV;
812 
813 	putint((int)(cur.dk_seek[dn]/etime+0.5), r, c, DISKCOLWIDTH);
814 	ADV;
815 	putint((int)((cur.dk_rxfer[dn]+cur.dk_wxfer[dn])/etime+0.5),
816 	    r, c, DISKCOLWIDTH);
817 	ADV;
818 	puthumanint((cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) / etime + 0.5,
819 		    r, c, DISKCOLWIDTH);
820 	ADV;
821 
822 	/* time busy in disk activity */
823 	atime = cur.dk_time[dn].tv_sec + cur.dk_time[dn].tv_usec / 1000000.0;
824 	atime = atime * 100.0 / etime;
825 	if (atime >= 100)
826 		putint(100, r, c, DISKCOLWIDTH);
827 	else
828 		putfloat(atime, r, c, DISKCOLWIDTH, 1, 1);
829 }
830 
831 static void
832 tinfo(int dn, int r, int c)
833 {
834 	double atime;
835 
836 	mvprintw(r, c, "%*.*s", DISKCOLWIDTH, DISKCOLWIDTH, tp_name[dn]);
837 	ADV;
838 	ADV; /* skip over the seeks column - not relevant for tape drives */
839 
840 	putint((int)((cur_tape.rxfer[dn]+cur_tape.wxfer[dn])/etime+0.5),
841 	    r, c, DISKCOLWIDTH);
842 	ADV;
843 	puthumanint((cur_tape.rbytes[dn] + cur_tape.wbytes[dn]) / etime + 0.5,
844 		    r, c, DISKCOLWIDTH);
845 	ADV;
846 
847 	/* time busy in disk activity */
848 	atime = cur_tape.time[dn].tv_sec + cur_tape.time[dn].tv_usec / 1000000.0;
849 	atime = atime * 100.0 / etime;
850 	if (atime >= 100)
851 		putint(100, r, c, DISKCOLWIDTH);
852 	else
853 		putfloat(atime, r, c, DISKCOLWIDTH, 1, 1);
854 #undef ADV
855 }
856 
857 
858