xref: /freebsd-13-stable/usr.bin/systat/vmstat.c (revision 0d349c19beda4185496f7a81330e0961a462512d)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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 
33 #ifdef lint
34 static const char sccsid[] = "@(#)vmstat.c	8.2 (Berkeley) 1/12/94";
35 #endif
36 
37 /*
38  * Cursed vmstat -- from Robert Elz.
39  */
40 
41 #include <sys/param.h>
42 #include <sys/stat.h>
43 #include <sys/time.h>
44 #include <sys/proc.h>
45 #include <sys/uio.h>
46 #include <sys/namei.h>
47 #include <sys/resource.h>
48 #include <sys/sysctl.h>
49 #include <sys/vmmeter.h>
50 
51 #include <vm/vm_param.h>
52 
53 #include <ctype.h>
54 #include <err.h>
55 #include <errno.h>
56 #include <langinfo.h>
57 #include <libutil.h>
58 #include <nlist.h>
59 #include <paths.h>
60 #include <signal.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <time.h>
64 #include <unistd.h>
65 #include <utmpx.h>
66 #include "systat.h"
67 #include "extern.h"
68 #include "devs.h"
69 
70 static struct Info {
71 	long	time[CPUSTATES];
72 	uint64_t v_swtch;	/* context switches */
73 	uint64_t v_trap;	/* calls to trap */
74 	uint64_t v_syscall;	/* calls to syscall() */
75 	uint64_t v_intr;	/* device interrupts */
76 	uint64_t v_soft;	/* software interrupts */
77 	/*
78 	 * Virtual memory activity.
79 	 */
80 	uint64_t v_vm_faults;	/* number of address memory faults */
81 	uint64_t v_io_faults;	/* page faults requiring I/O */
82 	uint64_t v_cow_faults;	/* number of copy-on-writes */
83 	uint64_t v_zfod;	/* pages zero filled on demand */
84 	uint64_t v_ozfod;	/* optimized zero fill pages */
85 	uint64_t v_swapin;	/* swap pager pageins */
86 	uint64_t v_swapout;	/* swap pager pageouts */
87 	uint64_t v_swappgsin;	/* swap pager pages paged in */
88 	uint64_t v_swappgsout;	/* swap pager pages paged out */
89 	uint64_t v_vnodein;	/* vnode pager pageins */
90 	uint64_t v_vnodeout;	/* vnode pager pageouts */
91 	uint64_t v_vnodepgsin;	/* vnode_pager pages paged in */
92 	uint64_t v_vnodepgsout;	/* vnode pager pages paged out */
93 	uint64_t v_intrans;	/* intransit blocking page faults */
94 	uint64_t v_reactivated;	/* number of pages reactivated by pagedaemon */
95 	uint64_t v_pdwakeups;	/* number of times daemon has awaken from sleep */
96 	uint64_t v_pdpages;	/* number of pages analyzed by daemon */
97 
98 	uint64_t v_dfree;	/* pages freed by daemon */
99 	uint64_t v_pfree;	/* pages freed by exiting processes */
100 	uint64_t v_tfree;	/* total pages freed */
101 	/*
102 	 * Distribution of page usages.
103 	 */
104 	u_int v_free_count;	/* number of pages free */
105 	u_int v_wire_count;	/* number of pages wired down */
106 	u_int v_active_count;	/* number of pages active */
107 	u_int v_inactive_count;	/* number of pages inactive */
108 	u_int v_laundry_count;	/* number of pages in laundry queue */
109 	u_long v_kmem_map_size;	/* Current kmem allocation size */
110 	struct	vmtotal Total;
111 	struct	nchstats nchstats;
112 	long	nchcount;
113 	long	*intrcnt;
114 	long	bufspace;
115 	u_long	maxvnodes;
116 	long	numvnodes;
117 	long	freevnodes;
118 	int	numdirtybuffers;
119 } s, s1, s2, z;
120 static u_long kmem_size;
121 static u_int v_page_count;
122 
123 
124 #define	total s.Total
125 #define	nchtotal s.nchstats
126 #define	oldnchtotal s1.nchstats
127 
128 static	enum state { BOOT, TIME, RUN } state = TIME;
129 enum divisor { IEC = 0, SI = HN_DIVISOR_1000 };
130 
131 static void allocinfo(struct Info *);
132 static void copyinfo(struct Info *, struct Info *);
133 static float cputime(int);
134 static void do_putuint64(uint64_t, int, int, int, int);
135 static void getinfo(struct Info *);
136 static void putuint64(uint64_t, int, int, int);
137 static int ucount(void);
138 
139 static	int ncpu;
140 static	char buf[26];
141 static	time_t t;
142 static	double etime;
143 static	int nintr;
144 static	long *intrloc;
145 static	char **intrname;
146 static	int nextintsrow;
147 
148 WINDOW *
openkre(void)149 openkre(void)
150 {
151 
152 	return (stdscr);
153 }
154 
155 void
closekre(WINDOW * w)156 closekre(WINDOW *w)
157 {
158 
159 	if (w == NULL)
160 		return;
161 	wclear(w);
162 	wrefresh(w);
163 }
164 
165 /*
166  * These constants define where the major pieces are laid out
167  */
168 #define STATROW		 0	/* uses 1 row and 67 cols */
169 #define STATCOL		 0
170 #define MEMROW		 2	/* uses 4 rows and 45 cols */
171 #define MEMCOL		 0
172 #define PAGEROW		 1	/* uses 4 rows and 30 cols */
173 #define PAGECOL		47
174 #define INTSROW		 5	/* uses all rows to bottom and 16 cols */
175 #define INTSCOL		64
176 #define PROCSROW	 6	/* uses 3 rows and 20 cols */
177 #define PROCSCOL	 0
178 #define GENSTATROW	 7	/* uses 2 rows and 29 cols */
179 #define GENSTATCOL	22
180 #define VMSTATROW	 5	/* uses 17 rows and 12-14 cols */
181 #define VMSTATCOL	52
182 #define GRAPHROW	10	/* uses 3 rows and 49-51 cols */
183 #define GRAPHCOL	 0
184 #define VNSTATROW	13	/* uses 4 rows and 13 columns */
185 #define VNSTATCOL	35
186 #define NAMEIROW	14	/* uses 3 rows and 32 cols */
187 #define NAMEICOL	 0
188 #define DISKROW		18	/* uses 5 rows and 47 cols (for 7 drives) */
189 #define DISKCOL		 0
190 
191 #define	DRIVESPACE	 7	/* max # for space */
192 
193 #define	MAXDRIVES	DRIVESPACE	 /* max # to display */
194 
195 int
initkre(void)196 initkre(void)
197 {
198 	char *cp, *cp1, *cp2, *intrnamebuf, *nextcp;
199 	int i;
200 	size_t sz;
201 
202 	if (dsinit(MAXDRIVES) != 1)
203 		return(0);
204 
205 	if (nintr == 0) {
206 		if (sysctlbyname("hw.intrcnt", NULL, &sz, NULL, 0) == -1) {
207 			error("sysctl(hw.intrcnt...) failed: %s",
208 			      strerror(errno));
209 			return (0);
210 		}
211 		nintr = sz / sizeof(u_long);
212 		intrloc = calloc(nintr, sizeof (long));
213 		intrname = calloc(nintr, sizeof (char *));
214 		intrnamebuf = sysctl_dynread("hw.intrnames", NULL);
215 		if (intrnamebuf == NULL || intrname == NULL ||
216 		    intrloc == NULL) {
217 			error("Out of memory");
218 			if (intrnamebuf)
219 				free(intrnamebuf);
220 			if (intrname)
221 				free(intrname);
222 			if (intrloc)
223 				free(intrloc);
224 			nintr = 0;
225 			return(0);
226 		}
227 		for (cp = intrnamebuf, i = 0; i < nintr; i++) {
228 			nextcp = cp + strlen(cp) + 1;
229 
230 			/* Discard trailing spaces. */
231 			for (cp1 = nextcp - 1; cp1 > cp && *(cp1 - 1) == ' '; )
232 				*--cp1 = '\0';
233 
234 			/* Convert "irqN: name" to "name irqN". */
235 			if (strncmp(cp, "irq", 3) == 0) {
236 				cp1 = cp + 3;
237 				while (isdigit((u_char)*cp1))
238 					cp1++;
239 				if (cp1 != cp && *cp1 == ':' &&
240 				    *(cp1 + 1) == ' ') {
241 					sz = strlen(cp);
242 					*cp1 = '\0';
243 					cp1 = cp1 + 2;
244 					cp2 = strdup(cp);
245 					bcopy(cp1, cp, sz - (cp1 - cp) + 1);
246 					if (sz <= 10 + 4) {
247 						strcat(cp, " ");
248 						strcat(cp, cp2 + 3);
249 					}
250 					free(cp2);
251 				}
252 			}
253 
254 			/*
255 			 * Convert "name irqN" to "name N" if the former is
256 			 * longer than the field width.
257 			 */
258 			if ((cp1 = strstr(cp, "irq")) != NULL &&
259 			    strlen(cp) > 10)
260 				bcopy(cp1 + 3, cp1, strlen(cp1 + 3) + 1);
261 
262 			intrname[i] = cp;
263 			cp = nextcp;
264 		}
265 		nextintsrow = INTSROW + 2;
266 		allocinfo(&s);
267 		allocinfo(&s1);
268 		allocinfo(&s2);
269 		allocinfo(&z);
270 	}
271 	GETSYSCTL("vm.kmem_size", kmem_size);
272 	GETSYSCTL("vm.stats.vm.v_page_count", v_page_count);
273 	getinfo(&s2);
274 	copyinfo(&s2, &s1);
275 	return(1);
276 }
277 
278 void
fetchkre(void)279 fetchkre(void)
280 {
281 	time_t now;
282 	struct tm *tp;
283 	static int d_first = -1;
284 
285 	if (d_first < 0)
286 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
287 
288 	time(&now);
289 	tp = localtime(&now);
290 	(void) strftime(buf, sizeof(buf),
291 			d_first ? "%e %b %T" : "%b %e %T", tp);
292 	getinfo(&s);
293 }
294 
295 void
labelkre(void)296 labelkre(void)
297 {
298 	int i, j;
299 
300 	clear();
301 	mvprintw(STATROW, STATCOL + 6, "users    Load");
302 	mvprintw(STATROW + 1, STATCOL + 3, "Mem usage:    %%Phy   %%Kmem");
303 	mvprintw(MEMROW, MEMCOL, "Mem:      REAL           VIRTUAL");
304 	mvprintw(MEMROW + 1, MEMCOL, "       Tot   Share     Tot    Share");
305 	mvprintw(MEMROW + 2, MEMCOL, "Act");
306 	mvprintw(MEMROW + 3, MEMCOL, "All");
307 
308 	mvprintw(MEMROW + 1, MEMCOL + 40, "Free");
309 
310 	mvprintw(PAGEROW, PAGECOL,     "         VN PAGER   SWAP PAGER");
311 	mvprintw(PAGEROW + 1, PAGECOL, "         in   out     in   out");
312 	mvprintw(PAGEROW + 2, PAGECOL, "count");
313 	mvprintw(PAGEROW + 3, PAGECOL, "pages");
314 
315 	mvprintw(INTSROW, INTSCOL + 1, "Interrupts");
316 	mvprintw(INTSROW + 1, INTSCOL + 6, "total");
317 
318 	mvprintw(VMSTATROW, VMSTATCOL + 6, "ioflt");
319 	mvprintw(VMSTATROW + 1, VMSTATCOL + 6, "cow");
320 	mvprintw(VMSTATROW + 2, VMSTATCOL + 6, "zfod");
321 	mvprintw(VMSTATROW + 3, VMSTATCOL + 6, "ozfod");
322 	mvprintw(VMSTATROW + 4, VMSTATCOL + 6 - 1, "%%ozfod");
323 	mvprintw(VMSTATROW + 5, VMSTATCOL + 6, "daefr");
324 	mvprintw(VMSTATROW + 6, VMSTATCOL + 6, "prcfr");
325 	mvprintw(VMSTATROW + 7, VMSTATCOL + 6, "totfr");
326 	mvprintw(VMSTATROW + 8, VMSTATCOL + 6, "react");
327 	mvprintw(VMSTATROW + 9, VMSTATCOL + 6, "pdwak");
328 	mvprintw(VMSTATROW + 10, VMSTATCOL + 6, "pdpgs");
329 	mvprintw(VMSTATROW + 11, VMSTATCOL + 6, "intrn");
330 	mvprintw(VMSTATROW + 12, VMSTATCOL + 6, "wire");
331 	mvprintw(VMSTATROW + 13, VMSTATCOL + 6, "act");
332 	mvprintw(VMSTATROW + 14, VMSTATCOL + 6, "inact");
333 	mvprintw(VMSTATROW + 15, VMSTATCOL + 6, "laund");
334 	mvprintw(VMSTATROW + 16, VMSTATCOL + 6, "free");
335 	if (LINES - 1 > VMSTATROW + 17)
336 		mvprintw(VMSTATROW + 17, VMSTATCOL + 6, "buf");
337 
338 	mvprintw(GENSTATROW, GENSTATCOL, " Csw  Trp  Sys  Int  Sof  Flt");
339 
340 	mvprintw(GRAPHROW, GRAPHCOL,
341 		"  . %%Sys    . %%Intr   . %%User   . %%Nice   . %%Idle");
342 	mvprintw(PROCSROW, PROCSCOL, "Proc:");
343 	mvprintw(PROCSROW + 1, PROCSCOL, "  r   p   d    s   w");
344 	mvprintw(GRAPHROW + 1, GRAPHCOL,
345 		"|    |    |    |    |    |    |    |    |    |    |");
346 
347 	mvprintw(VNSTATROW, VNSTATCOL + 8, "dtbuf");
348 	mvprintw(VNSTATROW + 1, VNSTATCOL + 8, "maxvn");
349 	mvprintw(VNSTATROW + 2, VNSTATCOL + 8, "numvn");
350 	mvprintw(VNSTATROW + 3, VNSTATCOL + 8, "frevn");
351 
352 	mvprintw(NAMEIROW, NAMEICOL, "Namei     Name-cache   Dir-cache");
353 	mvprintw(NAMEIROW + 1, NAMEICOL,
354 		"   Calls    hits   %%    hits   %%");
355 	dslabel(MAXDRIVES, DISKCOL, DISKROW);
356 
357 	for (i = 0; i < nintr; i++) {
358 		if (intrloc[i] == 0)
359 			continue;
360 		mvprintw(intrloc[i], INTSCOL + 6, "%-10.10s", intrname[i]);
361 	}
362 }
363 
364 #define X(fld)	{t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
365 #define Q(fld)	{t=cur_dev.fld[i]; cur_dev.fld[i]-=last_dev.fld[i]; if(state==TIME) last_dev.fld[i]=t;}
366 #define Y(fld)	{t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
367 #define Z(fld)	{t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
368 	if(state == TIME) s1.nchstats.fld = t;}
369 #define PUTRATE(fld, l, c, w) \
370 do { \
371 	Y(fld); \
372 	sysputwuint64(wnd, l, c, w, (s.fld/etime + 0.5), 0); \
373 } while (0)
374 #define MAXFAIL 5
375 
376 static	char cpuchar[CPUSTATES] = { '=' , '+', '>', '-', ' ' };
377 static	char cpuorder[CPUSTATES] = { CP_SYS, CP_INTR, CP_USER, CP_NICE,
378 				     CP_IDLE };
379 
380 void
showkre(void)381 showkre(void)
382 {
383 	float f1, f2;
384 	int psiz, inttotal;
385 	int i, l, lc;
386 	static int failcnt = 0;
387 
388 	etime = 0;
389 	for(i = 0; i < CPUSTATES; i++) {
390 		X(time);
391 		Q(cp_time);
392 		etime += s.time[i];
393 	}
394 	if (etime < 5.0) {	/* < 5 ticks - ignore this trash */
395 		if (failcnt++ >= MAXFAIL) {
396 			clear();
397 			mvprintw(2, 10, "The alternate system clock has died!");
398 			mvprintw(3, 10, "Reverting to ``pigs'' display.");
399 			move(CMDLINE, 0);
400 			refresh();
401 			failcnt = 0;
402 			sleep(5);
403 			command("pigs");
404 		}
405 		return;
406 	}
407 	failcnt = 0;
408 	etime /= hertz;
409 	etime /= ncpu;
410 	inttotal = 0;
411 	for (i = 0; i < nintr; i++) {
412 		if (s.intrcnt[i] == 0)
413 			continue;
414 		X(intrcnt);
415 		l = (int)((float)s.intrcnt[i]/etime + 0.5);
416 		inttotal += l;
417 		if (intrloc[i] == 0) {
418 			if (nextintsrow == LINES)
419 				continue;
420 			intrloc[i] = nextintsrow++;
421 			mvprintw(intrloc[i], INTSCOL + 6, "%-10.10s",
422 				intrname[i]);
423 		}
424 		putint(l, intrloc[i], INTSCOL, 5);
425 	}
426 	putint(inttotal, INTSROW + 1, INTSCOL, 5);
427 	Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
428 	Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes); Z(ncs_neghits);
429 	s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
430 	    nchtotal.ncs_miss + nchtotal.ncs_long + nchtotal.ncs_neghits;
431 	if (state == TIME)
432 		s1.nchcount = s.nchcount;
433 
434 	psiz = 0;
435 	f2 = 0.0;
436 	for (lc = 0; lc < CPUSTATES; lc++) {
437 		i = cpuorder[lc];
438 		f1 = cputime(i);
439 		f2 += f1;
440 		l = (int) ((f2 + 1.0) / 2.0) - psiz;
441 		putfloat(f1, GRAPHROW, GRAPHCOL + 10 * lc, 4, 1, 0);
442 		move(GRAPHROW + 2, psiz);
443 		psiz += l;
444 		while (l-- > 0)
445 			addch(cpuchar[lc]);
446 	}
447 
448 	putint(ucount(), STATROW, STATCOL, 5);
449 	putfloat(avenrun[0], STATROW, STATCOL + 20, 5, 2, 0);
450 	putfloat(avenrun[1], STATROW, STATCOL + 26, 5, 2, 0);
451 	putfloat(avenrun[2], STATROW, STATCOL + 32, 5, 2, 0);
452 	mvaddstr(STATROW, STATCOL + 55, buf);
453 	putfloat(100.0 * (v_page_count - total.t_free) / v_page_count,
454 	   STATROW + 1, STATCOL + 15, 2, 0, 1);
455 	putfloat(100.0 * s.v_kmem_map_size / kmem_size,
456 	   STATROW + 1, STATCOL + 22, 2, 0, 1);
457 
458 	sysputpage(wnd, MEMROW + 2, MEMCOL + 4, 6, total.t_arm, 0);
459 	sysputpage(wnd, MEMROW + 2, MEMCOL + 12, 6, total.t_armshr, 0);
460 	sysputpage(wnd, MEMROW + 2, MEMCOL + 20, 6, total.t_avm, 0);
461 	sysputpage(wnd, MEMROW + 2, MEMCOL + 29, 6, total.t_avmshr, 0);
462 	sysputpage(wnd, MEMROW + 3, MEMCOL + 4, 6, total.t_rm, 0);
463 	sysputpage(wnd, MEMROW + 3, MEMCOL + 12, 6, total.t_rmshr, 0);
464 	sysputpage(wnd, MEMROW + 3, MEMCOL + 20, 6, total.t_vm, 0);
465 	sysputpage(wnd, MEMROW + 3, MEMCOL + 29, 6, total.t_vmshr, 0);
466 	sysputpage(wnd, MEMROW + 2, MEMCOL + 38, 6, total.t_free, 0);
467 	putint(total.t_rq - 1, PROCSROW + 2, PROCSCOL, 3);
468 	putint(total.t_pw, PROCSROW + 2, PROCSCOL + 4, 3);
469 	putint(total.t_dw, PROCSROW + 2, PROCSCOL + 8, 3);
470 	putint(total.t_sl, PROCSROW + 2, PROCSCOL + 12, 4);
471 	putint(total.t_sw, PROCSROW + 2, PROCSCOL + 17, 3);
472 	PUTRATE(v_io_faults, VMSTATROW, VMSTATCOL, 5);
473 	PUTRATE(v_cow_faults, VMSTATROW + 1, VMSTATCOL, 5);
474 	PUTRATE(v_zfod, VMSTATROW + 2, VMSTATCOL, 5);
475 	PUTRATE(v_ozfod, VMSTATROW + 3, VMSTATCOL, 5);
476 	putint(s.v_zfod != 0 ? (int)(s.v_ozfod * 100.0 / s.v_zfod) : 0,
477 	    VMSTATROW + 4, VMSTATCOL, 5);
478 	PUTRATE(v_dfree, VMSTATROW + 5, VMSTATCOL, 5);
479 	PUTRATE(v_pfree, VMSTATROW + 6, VMSTATCOL, 5);
480 	PUTRATE(v_tfree, VMSTATROW + 7, VMSTATCOL, 5);
481 	PUTRATE(v_reactivated, VMSTATROW + 8, VMSTATCOL, 5);
482 	PUTRATE(v_pdwakeups, VMSTATROW + 9, VMSTATCOL, 5);
483 	PUTRATE(v_pdpages, VMSTATROW + 10, VMSTATCOL, 5);
484 	PUTRATE(v_intrans, VMSTATROW + 11, VMSTATCOL, 5);
485 	sysputpage(wnd, VMSTATROW + 12, VMSTATCOL, 5, s.v_wire_count, 0);
486 	sysputpage(wnd, VMSTATROW + 13, VMSTATCOL, 5, s.v_active_count, 0);
487 	sysputpage(wnd, VMSTATROW + 14, VMSTATCOL, 5, s.v_inactive_count, 0);
488 	sysputpage(wnd, VMSTATROW + 15, VMSTATCOL, 5, s.v_laundry_count, 0);
489 	sysputpage(wnd, VMSTATROW + 16, VMSTATCOL, 5, s.v_free_count, 0);
490 	if (LINES - 1 > VMSTATROW + 17)
491 		sysputuint64(wnd, VMSTATROW + 17, VMSTATCOL, 5, s.bufspace, 0);
492 	PUTRATE(v_vnodein, PAGEROW + 2, PAGECOL + 6, 5);
493 	PUTRATE(v_vnodeout, PAGEROW + 2, PAGECOL + 12, 5);
494 	PUTRATE(v_swapin, PAGEROW + 2, PAGECOL + 19, 5);
495 	PUTRATE(v_swapout, PAGEROW + 2, PAGECOL + 25, 5);
496 	PUTRATE(v_vnodepgsin, PAGEROW + 3, PAGECOL + 6, 5);
497 	PUTRATE(v_vnodepgsout, PAGEROW + 3, PAGECOL + 12, 5);
498 	PUTRATE(v_swappgsin, PAGEROW + 3, PAGECOL + 19, 5);
499 	PUTRATE(v_swappgsout, PAGEROW + 3, PAGECOL + 25, 5);
500 	PUTRATE(v_swtch, GENSTATROW + 1, GENSTATCOL, 4);
501 	PUTRATE(v_trap, GENSTATROW + 1, GENSTATCOL + 5, 4);
502 	PUTRATE(v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 4);
503 	PUTRATE(v_intr, GENSTATROW + 1, GENSTATCOL + 15, 4);
504 	PUTRATE(v_soft, GENSTATROW + 1, GENSTATCOL + 20, 4);
505 	PUTRATE(v_vm_faults, GENSTATROW + 1, GENSTATCOL + 25, 4);
506 	switch(state) {
507 	case TIME:
508 		dsshow(MAXDRIVES, DISKCOL, DISKROW, &cur_dev, &last_dev);
509 		break;
510 	case RUN:
511 		dsshow(MAXDRIVES, DISKCOL, DISKROW, &cur_dev, &run_dev);
512 		break;
513 	case BOOT:
514 		dsshow(MAXDRIVES, DISKCOL, DISKROW, &cur_dev, NULL);
515 		break;
516 	}
517 	putint(s.numdirtybuffers, VNSTATROW, VNSTATCOL, 7);
518 	putint(s.maxvnodes, VNSTATROW + 1, VNSTATCOL, 7);
519 	putint(s.numvnodes, VNSTATROW + 2, VNSTATCOL, 7);
520 	putint(s.freevnodes, VNSTATROW + 3, VNSTATCOL, 7);
521 	putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 8);
522 	putint((nchtotal.ncs_goodhits + nchtotal.ncs_neghits),
523 	   NAMEIROW + 2, NAMEICOL + 9, 7);
524 #define nz(x)	((x) ? (x) : 1)
525 	putfloat((nchtotal.ncs_goodhits+nchtotal.ncs_neghits) *
526 	   100.0 / nz(s.nchcount),
527 	   NAMEIROW + 2, NAMEICOL + 17, 3, 0, 1);
528 	putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 21, 7);
529 	putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
530 	   NAMEIROW + 2, NAMEICOL + 29, 3, 0, 1);
531 #undef nz
532 }
533 
534 int
cmdkre(const char * cmd,const char * args)535 cmdkre(const char *cmd, const char *args)
536 {
537 	int retval;
538 
539 	if (prefix(cmd, "run")) {
540 		retval = 1;
541 		copyinfo(&s2, &s1);
542 		switch (devstat_getdevs(NULL, &run_dev)) {
543 		case -1:
544 			errx(1, "%s", devstat_errbuf);
545 			break;
546 		case 1:
547 			num_devices = run_dev.dinfo->numdevs;
548 			generation = run_dev.dinfo->generation;
549 			retval = dscmd("refresh", NULL, MAXDRIVES, &cur_dev);
550 			if (retval == 2)
551 				labelkre();
552 			break;
553 		default:
554 			break;
555 		}
556 		state = RUN;
557 		return (retval);
558 	}
559 	if (prefix(cmd, "boot")) {
560 		state = BOOT;
561 		copyinfo(&z, &s1);
562 		return (1);
563 	}
564 	if (prefix(cmd, "time")) {
565 		state = TIME;
566 		return (1);
567 	}
568 	if (prefix(cmd, "zero")) {
569 		retval = 1;
570 		if (state == RUN) {
571 			getinfo(&s1);
572 			switch (devstat_getdevs(NULL, &run_dev)) {
573 			case -1:
574 				errx(1, "%s", devstat_errbuf);
575 				break;
576 			case 1:
577 				num_devices = run_dev.dinfo->numdevs;
578 				generation = run_dev.dinfo->generation;
579 				retval = dscmd("refresh",NULL, MAXDRIVES, &cur_dev);
580 				if (retval == 2)
581 					labelkre();
582 				break;
583 			default:
584 				break;
585 			}
586 		}
587 		return (retval);
588 	}
589 	retval = dscmd(cmd, args, MAXDRIVES, &cur_dev);
590 
591 	if (retval == 2)
592 		labelkre();
593 
594 	return(retval);
595 }
596 
597 /* calculate number of users on the system */
598 static int
ucount(void)599 ucount(void)
600 {
601 	int nusers = 0;
602 	struct utmpx *ut;
603 
604 	setutxent();
605 	while ((ut = getutxent()) != NULL)
606 		if (ut->ut_type == USER_PROCESS)
607 			nusers++;
608 	endutxent();
609 
610 	return (nusers);
611 }
612 
613 static float
cputime(int indx)614 cputime(int indx)
615 {
616 	double lt;
617 	int i;
618 
619 	lt = 0;
620 	for (i = 0; i < CPUSTATES; i++)
621 		lt += s.time[i];
622 	if (lt == 0.0)
623 		lt = 1.0;
624 	return (s.time[indx] * 100.0 / lt);
625 }
626 
627 void
putint(int n,int l,int lc,int w)628 putint(int n, int l, int lc, int w)
629 {
630 
631 	do_putuint64(n, l, lc, w, SI);
632 }
633 
634 static void
putuint64(uint64_t n,int l,int lc,int w)635 putuint64(uint64_t n, int l, int lc, int w)
636 {
637 
638 	do_putuint64(n, l, lc, w, IEC);
639 }
640 
641 static void
do_putuint64(uint64_t n,int l,int lc,int w,int div)642 do_putuint64(uint64_t n, int l, int lc, int w, int div)
643 {
644 	int snr;
645 	char b[128];
646 	char buf[128];
647 
648 	move(l, lc);
649 #ifdef DEBUG
650 		while (w-- > 0)
651 			addch('*');
652 		return;
653 #endif
654 	if (n == 0) {
655 		while (w-- > 0)
656 			addch(' ');
657 		return;
658 	}
659 	snr = snprintf(b, sizeof(b), "%*ju", w, (uintmax_t)n);
660 	if (snr != w) {
661 		humanize_number(buf, w, n, "", HN_AUTOSCALE,
662 		    HN_NOSPACE | HN_DECIMAL | div);
663 		snr = snprintf(b, sizeof(b), "%*s", w, buf);
664 	}
665 	if (snr != w) {
666 		while (w-- > 0)
667 			addch('*');
668 		return;
669 	}
670 	addstr(b);
671 }
672 
673 void
putfloat(double f,int l,int lc,int w,int d,int nz)674 putfloat(double f, int l, int lc, int w, int d, int nz)
675 {
676 	int snr;
677 	char b[128];
678 
679 	move(l, lc);
680 #ifdef DEBUG
681 		while (--w >= 0)
682 			addch('*');
683 		return;
684 #endif
685 	if (nz && f == 0.0) {
686 		while (--w >= 0)
687 			addch(' ');
688 		return;
689 	}
690 	snr = snprintf(b, sizeof(b), "%*.*f", w, d, f);
691 	if (snr != w)
692 		snr = snprintf(b, sizeof(b), "%*.0f", w, f);
693 	if (snr != w)
694 		snr = snprintf(b, sizeof(b), "%*.0fk", w - 1, f / 1000);
695 	if (snr != w)
696 		snr = snprintf(b, sizeof(b), "%*.0fM", w - 1, f / 1000000);
697 	if (snr != w) {
698 		while (--w >= 0)
699 			addch('*');
700 		return;
701 	}
702 	addstr(b);
703 }
704 
705 void
putlongdouble(long double f,int l,int lc,int w,int d,int nz)706 putlongdouble(long double f, int l, int lc, int w, int d, int nz)
707 {
708 	int snr;
709 	char b[128];
710 
711 	move(l, lc);
712 #ifdef DEBUG
713 		while (--w >= 0)
714 			addch('*');
715 		return;
716 #endif
717 	if (nz && f == 0.0) {
718 		while (--w >= 0)
719 			addch(' ');
720 		return;
721 	}
722 	snr = snprintf(b, sizeof(b), "%*.*Lf", w, d, f);
723 	if (snr != w)
724 		snr = snprintf(b, sizeof(b), "%*.0Lf", w, f);
725 	if (snr != w)
726 		snr = snprintf(b, sizeof(b), "%*.0Lfk", w - 1, f / 1000);
727 	if (snr != w)
728 		snr = snprintf(b, sizeof(b), "%*.0LfM", w - 1, f / 1000000);
729 	if (snr != w) {
730 		while (--w >= 0)
731 			addch('*');
732 		return;
733 	}
734 	addstr(b);
735 }
736 
737 static void
getinfo(struct Info * ls)738 getinfo(struct Info *ls)
739 {
740 	struct devinfo *tmp_dinfo;
741 	size_t size;
742 	int mib[2];
743 
744 	GETSYSCTL("kern.cp_time", ls->time);
745 	GETSYSCTL("kern.cp_time", cur_dev.cp_time);
746 	GETSYSCTL("vm.stats.sys.v_swtch", ls->v_swtch);
747 	GETSYSCTL("vm.stats.sys.v_trap", ls->v_trap);
748 	GETSYSCTL("vm.stats.sys.v_syscall", ls->v_syscall);
749 	GETSYSCTL("vm.stats.sys.v_intr", ls->v_intr);
750 	GETSYSCTL("vm.stats.sys.v_soft", ls->v_soft);
751 	GETSYSCTL("vm.stats.vm.v_vm_faults", ls->v_vm_faults);
752 	GETSYSCTL("vm.stats.vm.v_io_faults", ls->v_io_faults);
753 	GETSYSCTL("vm.stats.vm.v_cow_faults", ls->v_cow_faults);
754 	GETSYSCTL("vm.stats.vm.v_zfod", ls->v_zfod);
755 	GETSYSCTL("vm.stats.vm.v_ozfod", ls->v_ozfod);
756 	GETSYSCTL("vm.stats.vm.v_swapin", ls->v_swapin);
757 	GETSYSCTL("vm.stats.vm.v_swapout", ls->v_swapout);
758 	GETSYSCTL("vm.stats.vm.v_swappgsin", ls->v_swappgsin);
759 	GETSYSCTL("vm.stats.vm.v_swappgsout", ls->v_swappgsout);
760 	GETSYSCTL("vm.stats.vm.v_vnodein", ls->v_vnodein);
761 	GETSYSCTL("vm.stats.vm.v_vnodeout", ls->v_vnodeout);
762 	GETSYSCTL("vm.stats.vm.v_vnodepgsin", ls->v_vnodepgsin);
763 	GETSYSCTL("vm.stats.vm.v_vnodepgsout", ls->v_vnodepgsout);
764 	GETSYSCTL("vm.stats.vm.v_intrans", ls->v_intrans);
765 	GETSYSCTL("vm.stats.vm.v_reactivated", ls->v_reactivated);
766 	GETSYSCTL("vm.stats.vm.v_pdwakeups", ls->v_pdwakeups);
767 	GETSYSCTL("vm.stats.vm.v_pdpages", ls->v_pdpages);
768 	GETSYSCTL("vm.stats.vm.v_dfree", ls->v_dfree);
769 	GETSYSCTL("vm.stats.vm.v_pfree", ls->v_pfree);
770 	GETSYSCTL("vm.stats.vm.v_tfree", ls->v_tfree);
771 	GETSYSCTL("vm.stats.vm.v_free_count", ls->v_free_count);
772 	GETSYSCTL("vm.stats.vm.v_wire_count", ls->v_wire_count);
773 	GETSYSCTL("vm.stats.vm.v_active_count", ls->v_active_count);
774 	GETSYSCTL("vm.stats.vm.v_inactive_count", ls->v_inactive_count);
775 	GETSYSCTL("vm.stats.vm.v_laundry_count", ls->v_laundry_count);
776 	GETSYSCTL("vfs.bufspace", ls->bufspace);
777 	GETSYSCTL("kern.maxvnodes", ls->maxvnodes);
778 	GETSYSCTL("vfs.numvnodes", ls->numvnodes);
779 	GETSYSCTL("vfs.freevnodes", ls->freevnodes);
780 	GETSYSCTL("vfs.cache.nchstats", ls->nchstats);
781 	GETSYSCTL("vfs.numdirtybuffers", ls->numdirtybuffers);
782 	GETSYSCTL("vm.kmem_map_size", ls->v_kmem_map_size);
783 	getsysctl("hw.intrcnt", ls->intrcnt, nintr * sizeof(u_long));
784 
785 	size = sizeof(ls->Total);
786 	mib[0] = CTL_VM;
787 	mib[1] = VM_TOTAL;
788 	if (sysctl(mib, 2, &ls->Total, &size, NULL, 0) < 0) {
789 		error("Can't get kernel info: %s\n", strerror(errno));
790 		bzero(&ls->Total, sizeof(ls->Total));
791 	}
792 	size = sizeof(ncpu);
793 	if (sysctlbyname("hw.ncpu", &ncpu, &size, NULL, 0) < 0 ||
794 	    size != sizeof(ncpu))
795 		ncpu = 1;
796 
797 	tmp_dinfo = last_dev.dinfo;
798 	last_dev.dinfo = cur_dev.dinfo;
799 	cur_dev.dinfo = tmp_dinfo;
800 
801 	last_dev.snap_time = cur_dev.snap_time;
802 	dsgetinfo(&cur_dev);
803 }
804 
805 static void
allocinfo(struct Info * ls)806 allocinfo(struct Info *ls)
807 {
808 
809 	ls->intrcnt = (long *) calloc(nintr, sizeof(long));
810 	if (ls->intrcnt == NULL)
811 		errx(2, "out of memory");
812 }
813 
814 static void
copyinfo(struct Info * from,struct Info * to)815 copyinfo(struct Info *from, struct Info *to)
816 {
817 	long *intrcnt;
818 
819 	/*
820 	 * time, wds, seek, and xfer are malloc'd so we have to
821 	 * save the pointers before the structure copy and then
822 	 * copy by hand.
823 	 */
824 	intrcnt = to->intrcnt;
825 	*to = *from;
826 
827 	bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
828 }
829