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