xref: /netbsd/usr.bin/systat/vmstat.c (revision 1183faee)
1 /*	$NetBSD: vmstat.c,v 1.92 2023/03/28 00:00:30 kre 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.92 2023/03/28 00:00:30 kre Exp $");
38 #endif /* not lint */
39 
40 /*
41  * Cursed vmstat -- from Robert Elz.
42  */
43 
44 #include <sys/param.h>
45 #include <sys/uio.h>
46 #include <sys/namei.h>
47 #include <sys/sysctl.h>
48 #include <sys/evcnt.h>
49 
50 #include <uvm/uvm_extern.h>
51 
52 #include <errno.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <util.h>
56 
57 #include "systat.h"
58 #include "extern.h"
59 #include "drvstats.h"
60 #include "utmpentry.h"
61 #include "vmstat.h"
62 
63 static struct Info {
64 	struct	uvmexp_sysctl uvmexp;
65 	struct	vmtotal Total;
66 	struct	nchstats nchstats;
67 	long	nchcount;
68 	long	*intrcnt;
69 	u_int64_t	*evcnt;
70 } s, s1, s2, z;
71 
72 enum display_mode display_mode = TIME;
73 
74 static void allocinfo(struct Info *);
75 static void copyinfo(struct Info *, struct Info *);
76 static float cputime(int);
77 static void dinfo(int, int, int);
78 static void getinfo(struct Info *);
79 static int ucount(void);
80 
81 static	char buf[26];
82 static	u_int64_t temp;
83 static	int nintr;
84 static	long *intrloc;
85 static	char **intrname;
86 static	int nextintsrow;
87 static	int disk_horiz = 1;
88 static	u_int nbuf;
89 
90 WINDOW *
openvmstat(void)91 openvmstat(void)
92 {
93 	return (stdscr);
94 }
95 
96 void
closevmstat(WINDOW * w)97 closevmstat(WINDOW *w)
98 {
99 
100 	if (w == NULL)
101 		return;
102 	wclear(w);
103 	wrefresh(w);
104 }
105 
106 
107 static struct nlist namelist[] = {
108 #define	X_INTRNAMES	0
109 	{ .n_name = "_intrnames" },
110 #define	X_EINTRNAMES	1
111 	{ .n_name = "_eintrnames" },
112 #define	X_INTRCNT	2
113 	{ .n_name = "_intrcnt" },
114 #define	X_EINTRCNT	3
115 	{ .n_name = "_eintrcnt" },
116 #define	X_ALLEVENTS	4
117 	{ .n_name = "_allevents" },
118 	{ .n_name = NULL }
119 };
120 
121 /*
122  * These constants define where the major pieces are laid out
123  */
124 #define STATROW		 0	/* uses 1 row and 68 cols */
125 #define STATCOL		 2
126 #define MEMROW		 9	/* uses 5 rows and 31 cols */
127 #define MEMCOL		 0
128 #define PAGEROW		 2	/* uses 4 rows and 26 cols */
129 #define PAGECOL		54
130 #define INTSROW		 9	/* uses all rows to bottom and 17 cols */
131 #define INTSCOL		40
132 #define INTSCOLEND	(VMSTATCOL - 0)
133 #define PROCSROW	 2	/* uses 2 rows and 20 cols */
134 #define PROCSCOL	 0
135 #define GENSTATROW	 2	/* uses 2 rows and 30 cols */
136 #define GENSTATCOL	17
137 #define VMSTATROW	 7	/* uses 17 rows and 15 cols */
138 #define VMSTATCOL	64
139 #define GRAPHROW	 5	/* uses 3 rows and 51 cols */
140 #define GRAPHCOL	 0
141 #define NAMEIROW	15	/* uses 3 rows and 38 cols (must be MEMROW + 5 + 1) */
142 #define NAMEICOL	 0
143 #define DISKROW		19	/* uses 5 rows and 50 cols (for 9 drives) */
144 #define DISKCOL		 0
145 #define DISKCOLWIDTH	 8
146 #define DISKCOLEND	INTSCOL
147 
148 typedef struct intr_evcnt intr_evcnt_t;
149 struct intr_evcnt {
150 	char		*ie_group;
151 	char		*ie_name;
152 	u_int64_t	*ie_count;	/* kernel address... */
153 	int		ie_loc;		/* screen row */
154 } *ie_head;
155 int nevcnt;
156 
157 static void
get_interrupt_events(void)158 get_interrupt_events(void)
159 {
160 	struct evcntlist allevents;
161 	struct evcnt evcnt, *evptr;
162 	intr_evcnt_t *ie;
163 
164 	if (!NREAD(X_ALLEVENTS, &allevents, sizeof allevents))
165 		return;
166 	evptr = TAILQ_FIRST(&allevents);
167 	for (; evptr != NULL; evptr = TAILQ_NEXT(&evcnt, ev_list)) {
168 		if (!KREAD(evptr, &evcnt, sizeof evcnt))
169 			return;
170 		if (evcnt.ev_type != EVCNT_TYPE_INTR)
171 			continue;
172 		if (reallocarr(&ie_head, nevcnt + 1, sizeof(*ie)) != 0) {
173 			error("realloc failed");
174 			die(0);
175 		}
176 		ie = ie_head + nevcnt;
177 		ie->ie_group = malloc(evcnt.ev_grouplen + 1);
178 		ie->ie_name = malloc(evcnt.ev_namelen + 1);
179 		if (ie->ie_group == NULL || ie->ie_name == NULL)
180 			return;
181 		if (!KREAD(evcnt.ev_group, ie->ie_group, evcnt.ev_grouplen + 1))
182 			return;
183 		if (!KREAD(evcnt.ev_name, ie->ie_name, evcnt.ev_namelen + 1))
184 			return;
185 		ie->ie_count = &evptr->ev_count;
186 		ie->ie_loc = 0;
187 		nevcnt++;
188 	}
189 }
190 
191 int
initvmstat(void)192 initvmstat(void)
193 {
194 	static char *intrnamebuf;
195 	char *cp;
196 	int i;
197 
198 	if (intrnamebuf)
199 		free(intrnamebuf);
200 	if (intrname)
201 		free(intrname);
202 	if (intrloc)
203 		free(intrloc);
204 
205 	if (namelist[0].n_type == 0) {
206 		if (kvm_nlist(kd, namelist) &&
207 		    namelist[X_ALLEVENTS].n_type == 0) {
208 			nlisterr(namelist);
209 			return(0);
210 		}
211 	}
212 	hertz = stathz ? stathz : hz;
213 	if (!drvinit(1))
214 		return(0);
215 
216 	/* Old style interrupt counts - deprecated */
217 	nintr = (namelist[X_EINTRCNT].n_value -
218 		namelist[X_INTRCNT].n_value) / sizeof (long);
219 	if (nintr) {
220 		intrloc = calloc(nintr, sizeof (long));
221 		intrname = calloc(nintr, sizeof (long));
222 		intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value -
223 				     namelist[X_INTRNAMES].n_value);
224 		if (intrnamebuf == NULL || intrname == 0 || intrloc == 0) {
225 			error("Out of memory\n");
226 			nintr = 0;
227 			return(0);
228 		}
229 		NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) -
230 		      NVAL(X_INTRNAMES));
231 		for (cp = intrnamebuf, i = 0; i < nintr; i++) {
232 			intrname[i] = cp;
233 			cp += strlen(cp) + 1;
234 		}
235 	}
236 
237 	/* event counter interrupt counts */
238 	get_interrupt_events();
239 
240 	nextintsrow = INTSROW + 1;
241 	allocinfo(&s);
242 	allocinfo(&s1);
243 	allocinfo(&s2);
244 	allocinfo(&z);
245 
246 	getinfo(&s2);
247 	copyinfo(&s2, &s1);
248 	return(1);
249 }
250 
251 void
fetchvmstat(void)252 fetchvmstat(void)
253 {
254 	time_t now;
255 
256 	time(&now);
257 	strlcpy(buf, ctime(&now), sizeof(buf));
258 	buf[19] = '\0';
259 	getinfo(&s);
260 }
261 
262 static void
print_ie_title(int i)263 print_ie_title(int i)
264 {
265 	int width, name_width, group_width;
266 
267 	width = INTSCOLEND - (INTSCOL + 9);
268 	if (width <= 0)
269 		return;
270 
271 	move(ie_head[i].ie_loc, INTSCOL + 9);
272 	group_width = strlen(ie_head[i].ie_group);
273 	name_width = strlen(ie_head[i].ie_name);
274 	width -= group_width + 1 + name_width;
275 	if (width < 0) {
276 		/*
277 		 * Screen to narrow for full strings
278 		 * This is all rather horrid, in some cases there are a lot
279 		 * of events in the same group, and in others the event
280 		 * name is "intr".  There are also names which need 7 or 8
281 		 * columns before they become meaningful.
282 		 * This is a bad compromise.
283 		 */
284 		width = -width;
285 		group_width -= (width + 1) / 2;
286 		name_width -= width / 2;
287 		/* some have the 'useful' name "intr", display their group */
288 		if (strcasecmp(ie_head[i].ie_name, "intr") == 0) {
289 			 group_width += name_width + 1;
290 			 name_width = 0;
291 		} else {
292 			if (group_width <= 3 || name_width < 0) {
293 				/* don't display group */
294 				name_width += group_width + 1;
295 				group_width = 0;
296 			}
297 		}
298 	}
299 
300 	if (group_width != 0) {
301 		printw("%-.*s", group_width, ie_head[i].ie_group);
302 		if (name_width != 0)
303 			printw(" ");
304 	}
305 	if (name_width != 0)
306 		printw("%-.*s", name_width, ie_head[i].ie_name);
307 }
308 
309 void
labelvmstat_top(void)310 labelvmstat_top(void)
311 {
312 
313 	clear();
314 
315 	mvprintw(STATROW, STATCOL + 4, "users    Load");
316 
317 	mvprintw(GENSTATROW, GENSTATCOL, "   Csw  Traps SysCal  Intr   Soft  Fault");
318 
319 	mvprintw(GRAPHROW, GRAPHCOL,
320 		"    . %% Sy    . %% Us    . %% Ni    . %% In    . %% Id");
321 	mvprintw(PROCSROW, PROCSCOL, "Proc:r  d  s");
322 	mvprintw(GRAPHROW + 1, GRAPHCOL,
323 		"|    |    |    |    |    |    |    |    |    |    |");
324 
325 	mvprintw(PAGEROW, PAGECOL + 8, "PAGING   SWAPPING ");
326 	mvprintw(PAGEROW + 1, PAGECOL, "        in  out   in  out ");
327 	mvprintw(PAGEROW + 2, PAGECOL, "  ops                     ");
328 	mvprintw(PAGEROW + 3, PAGECOL, "pages                     ");
329 }
330 
331 void
labelvmstat(void)332 labelvmstat(void)
333 {
334 	int i;
335 
336 	/* Top few lines first */
337 
338 	labelvmstat_top();
339 
340 	/* Left hand column */
341 
342 	mvprintw(MEMROW + 0, MEMCOL, "Anon                 %%   zero         ");
343 	mvprintw(MEMROW + 1, MEMCOL, "Exec                 %%   wired        ");
344 	mvprintw(MEMROW + 2, MEMCOL, "File                 %%   inact        ");
345 	mvprintw(MEMROW + 3, MEMCOL, "Meta                 %%   bufs         ");
346 	mvprintw(MEMROW + 4, MEMCOL, " (kB)        real   swaponly      free");
347 	mvprintw(MEMROW + 5, MEMCOL, "Active                                ");
348 
349 	mvprintw(NAMEIROW, NAMEICOL, "Namei         Sys-cache     Proc-cache");
350 	mvprintw(NAMEIROW + 1, NAMEICOL,
351 		"    Calls     hits    %%     hits     %%");
352 
353 	mvprintw(DISKROW, DISKCOL, "%*s", DISKCOLWIDTH, "Disks:");
354 	if (disk_horiz) {
355 		mvprintw(DISKROW + 1, DISKCOL + 1, "seeks");
356 		mvprintw(DISKROW + 2, DISKCOL + 1, "xfers");
357 		mvprintw(DISKROW + 3, DISKCOL + 1, "bytes");
358 		mvprintw(DISKROW + 4, DISKCOL + 1, "%%busy");
359 	} else {
360 		mvprintw(DISKROW, DISKCOL + 1 * DISKCOLWIDTH, "%*s", DISKCOLWIDTH, "seeks");
361 		mvprintw(DISKROW, DISKCOL + 2 * DISKCOLWIDTH, "%*s", DISKCOLWIDTH, "xfers");
362 		mvprintw(DISKROW, DISKCOL + 3 * DISKCOLWIDTH, "%*s", DISKCOLWIDTH, "bytes");
363 		mvprintw(DISKROW, DISKCOL + 4 * DISKCOLWIDTH, "%*s", DISKCOLWIDTH, "%busy");
364 	}
365 
366 	/* Middle column */
367 
368 	mvprintw(INTSROW, INTSCOL + 9, "Interrupts");
369 	for (i = 0; i < nintr; i++) {
370 		if (intrloc[i] == 0)
371 			continue;
372 		mvprintw(intrloc[i], INTSCOL + 9, "%-.*s",
373 			INTSCOLEND - (INTSCOL + 9), intrname[i]);
374 	}
375 	for (i = 0; i < nevcnt; i++) {
376 		if (ie_head[i].ie_loc == 0)
377 			continue;
378 		print_ie_title(i);
379 	}
380 
381 	/* Right hand column */
382 
383 	mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "forks");
384 	mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "fkppw");
385 	mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "fksvm");
386 	mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "pwait");
387 	mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "relck");
388 	mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "rlkok");
389 	mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "noram");
390 	mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "ndcpy");
391 	mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "fltcp");
392 	mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "zfod");
393 	mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "cow");
394 	mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "fmin");
395 	mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "ftarg");
396 	mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "itarg");
397 	mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "flnan");
398 	mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "pdfre");
399 
400 	if (LINES - 1 > VMSTATROW + 16)
401 		mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "pdscn");
402 }
403 
404 #define X(s, s1, fld)	{temp = (s).fld[i]; (s).fld[i] -= (s1).fld[i]; \
405 			if (display_mode == TIME) (s1).fld[i] = temp;}
406 #define Z(s, s1, fld)	{temp = (s).nchstats.fld; \
407 			(s).nchstats.fld -= (s1).nchstats.fld; \
408 			if (display_mode == TIME) (s1).nchstats.fld = temp;}
409 #define PUTRATE(s, s1, fld, l, c, w) \
410 			{temp = (s).fld; (s).fld -= (s1).fld; \
411 			if (display_mode == TIME) (s1).fld = temp; \
412 			putint((int)((float)(s).fld/etime + 0.5), l, c, w);}
413 
414 static	char cpuchar[CPUSTATES] = { '=' , '>', '-', '%', ' ' };
415 static	char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_INTR, CP_IDLE };
416 
417 void
show_vmstat_top(vmtotal_t * Total,uvmexp_sysctl_t * uvm,uvmexp_sysctl_t * uvm1)418 show_vmstat_top(vmtotal_t *Total, uvmexp_sysctl_t *uvm, uvmexp_sysctl_t *uvm1)
419 {
420 	float f1, f2;
421 	int psiz;
422 	int i, l, c;
423 	struct {
424 		struct uvmexp_sysctl *uvmexp;
425 	} us, us1;
426 
427 	us.uvmexp = uvm;
428 	us1.uvmexp = uvm1;
429 
430 	putint(ucount(), STATROW, STATCOL, 3);
431 	putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
432 	putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
433 	putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
434 	mvaddstr(STATROW, STATCOL + 53, buf);
435 
436 	putint(Total->t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
437 	putint(Total->t_dw, PROCSROW + 1, PROCSCOL + 6, 3);
438 	putint(Total->t_sl, PROCSROW + 1, PROCSCOL + 9, 3);
439 
440 	PUTRATE(us, us1, uvmexp->swtch, GENSTATROW + 1, GENSTATCOL - 1, 7);
441 	PUTRATE(us, us1, uvmexp->traps, GENSTATROW + 1, GENSTATCOL + 7, 6);
442 	PUTRATE(us, us1, uvmexp->syscalls, GENSTATROW + 1, GENSTATCOL + 14, 6);
443 	PUTRATE(us, us1, uvmexp->intrs, GENSTATROW + 1, GENSTATCOL + 21, 5);
444 	PUTRATE(us, us1, uvmexp->softs, GENSTATROW + 1, GENSTATCOL + 27, 6);
445 	PUTRATE(us, us1, uvmexp->faults, GENSTATROW + 1, GENSTATCOL + 34, 6);
446 
447 	/*
448 	 * XXX it sure would be nice if this did what top(1) does and showed
449 	 * the utilization of each CPU on a separate line, though perhaps IFF
450 	 * the screen is tall enough
451 	 */
452 	/* Last CPU state not calculated yet. */
453 	for (f2 = 0.0, psiz = 0, c = 0; c < CPUSTATES; c++) {
454 		i = cpuorder[c];
455 		f1 = cputime(i);
456 		f2 += f1;
457 		l = (int) ((f2 + 1.0) / 2.0) - psiz;
458 		if (c == 0)
459 			putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0);
460 		else
461 			putfloat(f1, GRAPHROW, GRAPHCOL + 10 * c + 1, 5, 1, 0);
462 		mvhline(GRAPHROW + 2, psiz, cpuchar[c], l);
463 		psiz += l;
464 	}
465 
466 	PUTRATE(us, us1, uvmexp->pageins, PAGEROW + 2, PAGECOL + 5, 5);
467 	PUTRATE(us, us1, uvmexp->pdpageouts, PAGEROW + 2, PAGECOL + 10, 5);
468 	PUTRATE(us, us1, uvmexp->pgswapin, PAGEROW + 3, PAGECOL + 5, 5);
469 	PUTRATE(us, us1, uvmexp->pgswapout, PAGEROW + 3, PAGECOL + 10, 5);
470 }
471 
472 void
showvmstat(void)473 showvmstat(void)
474 {
475 	int inttotal;
476 	int i, l, r, c;
477 	static int failcnt = 0;
478 	static int relabel = 0;
479 	static int last_disks = 0;
480 	static u_long bufmem;
481 	int mib[6];
482 	size_t size;
483 
484 	if (relabel) {
485 		labelvmstat();
486 		relabel = 0;
487 	}
488 
489 	cpuswap();
490 	if (display_mode == TIME) {
491 		drvswap();
492 		if (toofast(&failcnt))
493 			return;
494 	} else
495 		etime = 1.0;
496 
497 	show_vmstat_top(&s.Total, &s.uvmexp, &s1.uvmexp);
498 
499 	/* Memory totals */
500 #define pgtokb(pg)	((pg) * (s.uvmexp.pagesize / 1024))
501 
502 	putint(pgtokb(s.uvmexp.anonpages),				MEMROW + 0, MEMCOL + 7, 10);
503 	putint((s.uvmexp.anonpages * 100 + 0.5) / s.uvmexp.npages,	MEMROW + 0, MEMCOL + 17, 4);
504 
505 	putint(pgtokb(s.uvmexp.zeropages),				MEMROW + 0, MEMCOL + 30, 8);
506 
507 	putint(pgtokb(s.uvmexp.execpages),				MEMROW + 1, MEMCOL + 7, 10);
508 	putint((s.uvmexp.execpages * 100 + 0.5) / s.uvmexp.npages,	MEMROW + 1, MEMCOL + 17, 4);
509 
510 	putint(pgtokb(s.uvmexp.wired),					MEMROW + 1, MEMCOL + 30, 8);
511 
512 	putint(pgtokb(s.uvmexp.filepages),				MEMROW + 2, MEMCOL + 7, 10);
513 	putint((s.uvmexp.filepages * 100 + 0.5) / s.uvmexp.npages,	MEMROW + 2, MEMCOL + 17, 4);
514 
515 	putint(pgtokb(s.uvmexp.inactive),				MEMROW + 2, MEMCOL + 30, 8);
516 
517 	/* Get total size of metadata buffers */
518 	size = sizeof(bufmem);
519 	if (sysctlbyname("vm.bufmem", &bufmem, &size, NULL, 0) < 0) {
520 		error("can't get buffers size: %s\n", strerror(errno));
521 		return;
522 	}
523 
524 	/* Get number of metadata buffers */
525 	size = 0;
526 	mib[0] = CTL_KERN;
527 	mib[1] = KERN_BUF;
528 	mib[2] = KERN_BUF_ALL;
529 	mib[3] = KERN_BUF_ALL;
530 	mib[4] = (int)sizeof(struct buf_sysctl);
531 	mib[5] = INT_MAX; /* we want them all */
532 	if (sysctl(mib, 6, NULL, &size, NULL, 0) < 0) {
533 		error("can't get buffers size: %s\n", strerror(errno));
534 		return;
535 	}
536 	if (size == 0) {
537 		error("buffers size is zero: %s\n", strerror(errno));
538 		return;
539 	}
540 	nbuf = size / sizeof(struct buf_sysctl);
541 	nbuf -= KERN_BUFSLOP;
542 
543 	putint((int) (bufmem / 1024),		MEMROW + 3, MEMCOL + 5, 12);
544 	putint((int) ((bufmem * 100) + 0.5) / s.uvmexp.pagesize / s.uvmexp.npages,
545 						MEMROW + 3, MEMCOL + 17, 4);
546 	putint(nbuf,				MEMROW + 3, MEMCOL + 30, 8);
547 
548 	putint(pgtokb(s.uvmexp.active),		MEMROW + 5, MEMCOL + 7, 10);
549 	putint(pgtokb(s.uvmexp.swpgonly),	MEMROW + 5, MEMCOL + 18, 10);
550 	putint(pgtokb(s.uvmexp.free),		MEMROW + 5, MEMCOL + 28, 10);
551 
552 #undef pgtokb
553 
554 	/* Namei cache */
555 	Z(s, s1, ncs_goodhits); Z(s, s1, ncs_badhits); Z(s, s1, ncs_miss);
556 	Z(s, s1, ncs_long); Z(s, s1, ncs_pass2); Z(s, s1, ncs_2passes);
557 	s.nchcount = s.nchstats.ncs_goodhits + s.nchstats.ncs_badhits +
558 	    s.nchstats.ncs_miss + s.nchstats.ncs_long +
559 	    s.nchstats.ncs_pass2 + s.nchstats.ncs_2passes;
560 	if (display_mode == TIME)
561 		s1.nchcount = s.nchcount;
562 
563 	putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
564 	putint(s.nchstats.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 9, 9);
565 #define nz(x)	((x) ? (x) : 1)
566 	putfloat(s.nchstats.ncs_goodhits * 100.0 / nz(s.nchcount),
567 	   NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
568 	putint(s.nchstats.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
569 	putfloat(s.nchstats.ncs_pass2 * 100.0 / nz(s.nchcount),
570 	   NAMEIROW + 2, NAMEICOL + 34, 4, 0, 1);
571 #undef nz
572 
573 	/* Disks */
574 	for (l = 0, i = 0, r = DISKROW, c = DISKCOL;
575 	     i < (int)ndrive; i++) {
576 		if (!drv_select[i])
577 			continue;
578 
579 		if (disk_horiz)
580 			c += DISKCOLWIDTH;
581 		else
582 			r++;
583 		if (c + DISKCOLWIDTH > DISKCOLEND) {
584 			if (disk_horiz && LINES - 1 - DISKROW >
585 			    (DISKCOLEND - DISKCOL) / DISKCOLWIDTH) {
586 				disk_horiz = 0;
587 				relabel = 1;
588 			}
589 			break;
590 		}
591 		if (r >= LINES - 1) {
592 			if (!disk_horiz && LINES - 1 - DISKROW <
593 			    (DISKCOLEND - DISKCOL) / DISKCOLWIDTH) {
594 				disk_horiz = 1;
595 				relabel = 1;
596 			}
597 			break;
598 		}
599 		l++;
600 
601 		dinfo(i, r, c);
602 	}
603 	/* blank out if we lost any disks */
604 	for (i = l; i < last_disks; i++) {
605 		int j;
606 		if (disk_horiz)
607 			c += DISKCOLWIDTH;
608 		else
609 			r++;
610 		for (j = 0; j < 5; j++) {
611 			if (disk_horiz)
612 				mvprintw(r+j, c, "%*s", DISKCOLWIDTH, "");
613 			else
614 				mvprintw(r, c+j*DISKCOLWIDTH, "%*s", DISKCOLWIDTH, "");
615 		}
616 	}
617 	last_disks = l;
618 
619 	/* Interrupts */
620 	failcnt = 0;
621 	inttotal = 0;
622 	for (i = 0; i < nintr; i++) {
623 		if (s.intrcnt[i] == 0)
624 			continue;
625 		if (intrloc[i] == 0) {
626 			if (nextintsrow == LINES)
627 				continue;
628 			intrloc[i] = nextintsrow++;
629 			mvprintw(intrloc[i], INTSCOL + 9, "%-.*s",
630 				INTSCOLEND - (INTSCOL + 9), intrname[i]);
631 		}
632 		X(s, s1, intrcnt);
633 		l = (int)((float)s.intrcnt[i]/etime + 0.5);
634 		inttotal += l;
635 		putint(l, intrloc[i], INTSCOL, 8);
636 	}
637 
638 	for (i = 0; i < nevcnt; i++) {
639 		if (s.evcnt[i] == 0)
640 			continue;
641 		if (ie_head[i].ie_loc == 0) {
642 			if (nextintsrow == LINES)
643 				continue;
644 			ie_head[i].ie_loc = nextintsrow++;
645 			print_ie_title(i);
646 		}
647 		X(s, s1, evcnt);
648 		l = (int)((float)s.evcnt[i]/etime + 0.5);
649 		inttotal += l;
650 		putint(l, ie_head[i].ie_loc, INTSCOL, 8);
651 	}
652 	putint(inttotal, INTSROW, INTSCOL, 8);
653 
654 	PUTRATE(s, s1, uvmexp.forks, VMSTATROW + 0, VMSTATCOL + 3, 6);
655 	PUTRATE(s, s1, uvmexp.forks_ppwait, VMSTATROW + 1, VMSTATCOL + 3, 6);
656 	PUTRATE(s, s1, uvmexp.forks_sharevm, VMSTATROW + 2, VMSTATCOL + 3, 6);
657 	PUTRATE(s, s1, uvmexp.fltpgwait, VMSTATROW + 3, VMSTATCOL + 4, 5);
658 	PUTRATE(s, s1, uvmexp.fltrelck, VMSTATROW + 4, VMSTATCOL + 3, 6);
659 	PUTRATE(s, s1, uvmexp.fltrelckok, VMSTATROW + 5, VMSTATCOL + 3, 6);
660 	PUTRATE(s, s1, uvmexp.fltnoram, VMSTATROW + 6, VMSTATCOL + 3, 6);
661 	PUTRATE(s, s1, uvmexp.fltamcopy, VMSTATROW + 7, VMSTATCOL + 3, 6);
662 	PUTRATE(s, s1, uvmexp.flt_prcopy, VMSTATROW + 8, VMSTATCOL + 3, 6);
663 	PUTRATE(s, s1, uvmexp.flt_przero, VMSTATROW + 9, VMSTATCOL + 3, 6);
664 	PUTRATE(s, s1, uvmexp.flt_acow, VMSTATROW + 10, VMSTATCOL, 9);
665 	putint(s.uvmexp.freemin, VMSTATROW + 11, VMSTATCOL, 9);
666 	putint(s.uvmexp.freetarg, VMSTATROW + 12, VMSTATCOL, 9);
667 	putint(s.uvmexp.inactarg, VMSTATROW + 13, VMSTATCOL, 9);
668 	putint(s.uvmexp.fltnoanon, VMSTATROW + 14, VMSTATCOL, 9);
669 	PUTRATE(s, s1, uvmexp.pdfreed, VMSTATROW + 15, VMSTATCOL, 9);
670 	if (LINES - 1 > VMSTATROW + 16)
671 		PUTRATE(s, s1, uvmexp.pdscans, VMSTATROW + 16, VMSTATCOL, 9);
672 
673 }
674 
675 void
vmstat_boot(char * args)676 vmstat_boot(char *args)
677 {
678 	copyinfo(&z, &s1);
679 	display_mode = BOOT;
680 }
681 
682 void
vmstat_run(char * args)683 vmstat_run(char *args)
684 {
685 	copyinfo(&s1, &s2);
686 	display_mode = RUN;
687 }
688 
689 void
vmstat_time(char * args)690 vmstat_time(char *args)
691 {
692 	display_mode = TIME;
693 }
694 
695 void
vmstat_zero(char * args)696 vmstat_zero(char *args)
697 {
698 	if (display_mode == RUN)
699 		getinfo(&s1);
700 }
701 
702 /* calculate number of users on the system */
703 static int
ucount(void)704 ucount(void)
705 {
706 	int nusers = 0;
707 	struct utmpentry *ehead;
708 
709 	nusers = getutentries(NULL, &ehead);
710 
711 	if (nusers == 1)
712 		mvprintw(STATROW, STATCOL + 8, " ");
713 	else
714 		mvprintw(STATROW, STATCOL + 8, "s");
715 
716 	return (nusers);
717 }
718 
719 static float
cputime(int indx)720 cputime(int indx)
721 {
722 	double t;
723 	int i;
724 
725 	t = 0;
726 	for (i = 0; i < CPUSTATES; i++)
727 		t += cur.cp_time[i];
728 	if (t == 0.0)
729 		t = 1.0;
730 	return (cur.cp_time[indx] * 100.0 / t);
731 }
732 
733 void
puthumanint_scale(u_int64_t n,int l,int c,int w,int scale)734 puthumanint_scale(u_int64_t n, int l, int c, int w, int scale)
735 {
736 	char b[128];
737 
738 	if (move(l, c) != OK)
739 		return;
740 	if (n == 0 && !showzero) {
741 		hline(' ', w);
742 		return;
743 	}
744 	if (humanize_number(b, w, n, "", scale, HN_NOSPACE) == -1 ) {
745 		hline('*', w);
746 		return;
747 	}
748 	printw("%*s", w, b);
749 }
750 
751 void
puthumanint_sticky(u_int64_t n,int l,int c,int w,int * scale)752 puthumanint_sticky(u_int64_t n, int l, int c, int w, int *scale)
753 {
754 	char b[128];
755 	int sc;
756 
757 	sc = humanize_number(b, w, n, "", HN_GETSCALE, HN_NOSPACE);
758 	if (sc > *scale)
759 		*scale = sc;
760 	else
761 		sc = *scale;
762 
763 	puthumanint_scale(n, l, c, w, sc);
764 }
765 
766 void
puthumanint(u_int64_t n,int l,int c,int w)767 puthumanint(u_int64_t n, int l, int c, int w)
768 {
769 
770 	puthumanint_scale(n, l, c, w, HN_AUTOSCALE);
771 }
772 
773 void
putint(int n,int l,int c,int w)774 putint(int n, int l, int c, int w)
775 {
776 	char b[128];
777 
778 	if (move(l, c) != OK)
779 		return;
780 	if (n == 0 && !showzero) {
781 		hline(' ', w);
782 		return;
783 	}
784 	(void)snprintf(b, sizeof b, "%*d", w, n);
785 	if ((int)strlen(b) > w) {
786 		if (display_mode == TIME)
787 			hline('*', w);
788 		else
789 			puthumanint(n, l, c, w);
790 		return;
791 	}
792 	addstr(b);
793 }
794 
795 void
putfloat(double f,int l,int c,int w,int d,int nz)796 putfloat(double f, int l, int c, int w, int d, int nz)
797 {
798 	char b[128];
799 
800 	if (move(l, c) != OK)
801 		return;
802 	if (nz && f == 0.0 && !showzero) {
803 		hline(' ', w);
804 		return;
805 	}
806 	(void)snprintf(b, sizeof b, "%*.*f", w, d, f);
807 	if ((int)strlen(b) > w) {
808 		hline('*', w);
809 		return;
810 	}
811 	addstr(b);
812 }
813 
814 static void
getinfo(struct Info * stats)815 getinfo(struct Info *stats)
816 {
817 	int mib[2];
818 	size_t size;
819 	int i;
820 
821 	cpureadstats();
822 	drvreadstats();
823 	size = sizeof(stats->nchstats);
824 	if (sysctlbyname("vfs.namecache_stats", &stats->nchstats, &size,
825 	    NULL, 0) < 0) {
826 		error("can't get namecache statistics: %s\n", strerror(errno));
827 		memset(&stats->nchstats, 0, sizeof(stats->nchstats));
828 	}
829 	if (nintr)
830 		NREAD(X_INTRCNT, stats->intrcnt, nintr * sizeof(long));
831 	for (i = 0; i < nevcnt; i++)
832 		KREAD(ie_head[i].ie_count, &stats->evcnt[i],
833 		      sizeof stats->evcnt[i]);
834 	size = sizeof(stats->uvmexp);
835 	mib[0] = CTL_VM;
836 	mib[1] = VM_UVMEXP2;
837 	if (sysctl(mib, 2, &stats->uvmexp, &size, NULL, 0) < 0) {
838 		error("can't get uvmexp: %s\n", strerror(errno));
839 		memset(&stats->uvmexp, 0, sizeof(stats->uvmexp));
840 	}
841 	size = sizeof(stats->Total);
842 	mib[0] = CTL_VM;
843 	mib[1] = VM_METER;
844 	if (sysctl(mib, 2, &stats->Total, &size, NULL, 0) < 0) {
845 		error("Can't get kernel info: %s\n", strerror(errno));
846 		memset(&stats->Total, 0, sizeof(stats->Total));
847 	}
848 }
849 
850 static void
allocinfo(struct Info * stats)851 allocinfo(struct Info *stats)
852 {
853 
854 	if (nintr &&
855 	    (stats->intrcnt = calloc(nintr, sizeof(long))) == NULL) {
856 		error("calloc failed");
857 		die(0);
858 	}
859 	if ((stats->evcnt = calloc(nevcnt, sizeof(u_int64_t))) == NULL) {
860 		error("calloc failed");
861 		die(0);
862 	}
863 }
864 
865 static void
copyinfo(struct Info * from,struct Info * to)866 copyinfo(struct Info *from, struct Info *to)
867 {
868 	long *intrcnt;
869 	u_int64_t *evcnt;
870 
871 	intrcnt = to->intrcnt;
872 	evcnt = to->evcnt;
873 	*to = *from;
874 	memmove(to->intrcnt = intrcnt, from->intrcnt, nintr * sizeof *intrcnt);
875 	memmove(to->evcnt = evcnt, from->evcnt, nevcnt * sizeof *evcnt);
876 }
877 
878 static void
dinfo(int dn,int r,int c)879 dinfo(int dn, int r, int c)
880 {
881 	double atime, dtime;
882 #define ADV if (disk_horiz) r++; else c += DISKCOLWIDTH
883 
884 	/* elapsed time for disk stats */
885 	dtime = etime;
886 	if (cur.timestamp[dn].tv_sec || cur.timestamp[dn].tv_usec) {
887 		dtime = (double)cur.timestamp[dn].tv_sec +
888 			((double)cur.timestamp[dn].tv_usec / (double)1000000);
889 	}
890 
891 	mvprintw(r, c, "%*.*s", DISKCOLWIDTH, DISKCOLWIDTH, dr_name[dn]);
892 	ADV;
893 
894 	putint((int)(cur.seek[dn]/dtime+0.5), r, c, DISKCOLWIDTH);
895 	ADV;
896 	putint((int)((cur.rxfer[dn]+cur.wxfer[dn])/dtime+0.5),
897 	    r, c, DISKCOLWIDTH);
898 	ADV;
899 	puthumanint_sticky((cur.rbytes[dn] + cur.wbytes[dn]) / dtime + 0.5,
900 		    r, c, DISKCOLWIDTH, &cur.scale[dn]);
901 	ADV;
902 
903 	/* time busy in disk activity */
904 	atime = cur.time[dn].tv_sec + cur.time[dn].tv_usec / 1000000.0;
905 	atime = atime * 100.0 / dtime;
906 	if (atime >= 100)
907 		putint(100, r, c, DISKCOLWIDTH);
908 	else
909 		putfloat(atime, r, c, DISKCOLWIDTH, 1, 1);
910 }
911