vmstat.c revision 189626
1264790Sbapt/*-
2264790Sbapt * Copyright (c) 1983, 1989, 1992, 1993
3264790Sbapt *	The Regents of the University of California.  All rights reserved.
4264790Sbapt *
5264790Sbapt * Redistribution and use in source and binary forms, with or without
6264790Sbapt * modification, are permitted provided that the following conditions
7264790Sbapt * are met:
8264790Sbapt * 1. Redistributions of source code must retain the above copyright
9264790Sbapt *    notice, this list of conditions and the following disclaimer.
10264790Sbapt * 2. Redistributions in binary form must reproduce the above copyright
11264790Sbapt *    notice, this list of conditions and the following disclaimer in the
12264790Sbapt *    documentation and/or other materials provided with the distribution.
13264790Sbapt * 3. All advertising materials mentioning features or use of this software
14264790Sbapt *    must display the following acknowledgement:
15264790Sbapt *	This product includes software developed by the University of
16264790Sbapt *	California, Berkeley and its contributors.
17264790Sbapt * 4. Neither the name of the University nor the names of its contributors
18264790Sbapt *    may be used to endorse or promote products derived from this software
19264790Sbapt *    without specific prior written permission.
20264790Sbapt *
21264790Sbapt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22264790Sbapt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23264790Sbapt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24264790Sbapt * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25264790Sbapt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26264790Sbapt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27264790Sbapt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28264790Sbapt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29264790Sbapt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30264790Sbapt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31264790Sbapt * SUCH DAMAGE.
32264790Sbapt */
33264790Sbapt
34264790Sbapt#include <sys/cdefs.h>
35264790Sbapt
36264790Sbapt__FBSDID("$FreeBSD: head/usr.bin/systat/vmstat.c 189626 2009-03-10 15:25:19Z jhb $");
37264790Sbapt
38264790Sbapt#ifdef lint
39264790Sbaptstatic const char sccsid[] = "@(#)vmstat.c	8.2 (Berkeley) 1/12/94";
40264790Sbapt#endif
41264790Sbapt
42264790Sbapt/*
43264790Sbapt * Cursed vmstat -- from Robert Elz.
44264790Sbapt */
45264790Sbapt
46264790Sbapt#include <sys/param.h>
47264790Sbapt#include <sys/stat.h>
48264790Sbapt#include <sys/time.h>
49264790Sbapt#include <sys/proc.h>
50264790Sbapt#include <sys/uio.h>
51264790Sbapt#include <sys/namei.h>
52264790Sbapt#include <sys/resource.h>
53264790Sbapt#include <sys/sysctl.h>
54264790Sbapt#include <sys/vmmeter.h>
55264790Sbapt
56264790Sbapt#include <vm/vm_param.h>
57264790Sbapt
58264790Sbapt#include <ctype.h>
59264790Sbapt#include <err.h>
60264790Sbapt#include <errno.h>
61#include <langinfo.h>
62#include <nlist.h>
63#include <paths.h>
64#include <signal.h>
65#include <stdlib.h>
66#include <string.h>
67#include <time.h>
68#include <unistd.h>
69#include <utmp.h>
70#include <devstat.h>
71#include "systat.h"
72#include "extern.h"
73#include "devs.h"
74
75static struct Info {
76	long	time[CPUSTATES];
77	u_int v_swtch;		/* context switches */
78	u_int v_trap;		/* calls to trap */
79	u_int v_syscall;	/* calls to syscall() */
80	u_int v_intr;		/* device interrupts */
81	u_int v_soft;		/* software interrupts */
82	/*
83	 * Virtual memory activity.
84	 */
85	u_int v_vm_faults;	/* number of address memory faults */
86	u_int v_cow_faults;	/* number of copy-on-writes */
87	u_int v_zfod;		/* pages zero filled on demand */
88	u_int v_ozfod;		/* optimized zero fill pages */
89	u_int v_swapin;		/* swap pager pageins */
90	u_int v_swapout;	/* swap pager pageouts */
91	u_int v_swappgsin;	/* swap pager pages paged in */
92	u_int v_swappgsout;	/* swap pager pages paged out */
93	u_int v_vnodein;	/* vnode pager pageins */
94	u_int v_vnodeout;	/* vnode pager pageouts */
95	u_int v_vnodepgsin;	/* vnode_pager pages paged in */
96	u_int v_vnodepgsout;	/* vnode pager pages paged out */
97	u_int v_intrans;	/* intransit blocking page faults */
98	u_int v_reactivated;	/* number of pages reactivated from free list */
99	u_int v_pdwakeups;	/* number of times daemon has awaken from sleep */
100	u_int v_pdpages;	/* number of pages analyzed by daemon */
101
102	u_int v_dfree;		/* pages freed by daemon */
103	u_int v_pfree;		/* pages freed by exiting processes */
104	u_int v_tfree;		/* total pages freed */
105	/*
106	 * Distribution of page usages.
107	 */
108	u_int v_page_size;	/* page size in bytes */
109	u_int v_free_count;	/* number of pages free */
110	u_int v_wire_count;	/* number of pages wired down */
111	u_int v_active_count;	/* number of pages active */
112	u_int v_inactive_count;	/* number of pages inactive */
113	u_int v_cache_count;	/* number of pages on buffer cache queue */
114	struct	vmtotal Total;
115	struct	nchstats nchstats;
116	long	nchcount;
117	long	*intrcnt;
118	long	bufspace;
119	int	desiredvnodes;
120	long	numvnodes;
121	long	freevnodes;
122	int	numdirtybuffers;
123} s, s1, s2, z;
124
125struct statinfo cur, last, run;
126
127#define	total s.Total
128#define	nchtotal s.nchstats
129#define	oldnchtotal s1.nchstats
130
131static	enum state { BOOT, TIME, RUN } state = TIME;
132
133static void allocinfo(struct Info *);
134static void copyinfo(struct Info *, struct Info *);
135static float cputime(int);
136static void dinfo(int, int, struct statinfo *, struct statinfo *);
137static void getinfo(struct Info *);
138static void putint(int, int, int, int);
139static void putfloat(double, int, int, int, int, int);
140static void putlongdouble(long double, int, int, int, int, int);
141static int ucount(void);
142
143static	int ncpu;
144static	int ut;
145static	char buf[26];
146static	time_t t;
147static	double etime;
148static	int nintr;
149static	long *intrloc;
150static	char **intrname;
151static	int nextintsrow;
152
153struct	utmp utmp;
154
155
156WINDOW *
157openkre(void)
158{
159
160	ut = open(_PATH_UTMP, O_RDONLY);
161	if (ut < 0)
162		error("No utmp");
163	return (stdscr);
164}
165
166void
167closekre(WINDOW *w)
168{
169
170	(void) close(ut);
171	if (w == NULL)
172		return;
173	wclear(w);
174	wrefresh(w);
175}
176
177/*
178 * These constants define where the major pieces are laid out
179 */
180#define STATROW		 0	/* uses 1 row and 67 cols */
181#define STATCOL		 0
182#define MEMROW		 2	/* uses 4 rows and 45 cols */
183#define MEMCOL		 0
184#define PAGEROW		 2	/* uses 4 rows and 30 cols */
185#define PAGECOL		47
186#define INTSROW		 6	/* uses all rows to bottom and 16 cols */
187#define INTSCOL		64
188#define PROCSROW	 6	/* uses 3 rows and 19 cols */
189#define PROCSCOL	 0
190#define GENSTATROW	 7	/* uses 2 rows and 29 cols */
191#define GENSTATCOL	21
192#define VMSTATROW	 7	/* uses 17 rows and 12-14 cols */
193#define VMSTATCOL	49	/* actually 50-51 for some fields */
194#define GRAPHROW	10	/* uses 3 rows and 49-51 cols */
195#define GRAPHCOL	 0
196#define VNSTATROW	13	/* uses 4 rows and 13 columns */
197#define VNSTATCOL	35
198#define NAMEIROW	14	/* uses 3 rows and 32 cols */
199#define NAMEICOL	 0
200#define DISKROW		18	/* uses 5 rows and 47 cols (for 7 drives) */
201#define DISKCOL		 0
202
203#define	DRIVESPACE	 7	/* max # for space */
204
205#define	MAXDRIVES	DRIVESPACE	 /* max # to display */
206
207int
208initkre(void)
209{
210	char *cp, *cp1, *cp2, *intrnamebuf, *nextcp;
211	int i;
212	size_t sz;
213
214	if ((num_devices = devstat_getnumdevs(NULL)) < 0) {
215		warnx("%s", devstat_errbuf);
216		return(0);
217	}
218
219	cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
220	last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
221	run.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
222	bzero(cur.dinfo, sizeof(struct devinfo));
223	bzero(last.dinfo, sizeof(struct devinfo));
224	bzero(run.dinfo, sizeof(struct devinfo));
225
226	if (dsinit(MAXDRIVES, &cur, &last, &run) != 1)
227		return(0);
228
229	if (nintr == 0) {
230		if (sysctlbyname("hw.intrcnt", NULL, &sz, NULL, 0) == -1) {
231			error("sysctl(hw.intrcnt...) failed: %s",
232			      strerror(errno));
233			return (0);
234		}
235		nintr = sz / sizeof(u_long);
236		intrloc = calloc(nintr, sizeof (long));
237		intrname = calloc(nintr, sizeof (char *));
238		intrnamebuf = sysctl_dynread("hw.intrnames", NULL);
239		if (intrnamebuf == NULL || intrname == NULL ||
240		    intrloc == NULL) {
241			error("Out of memory");
242			if (intrnamebuf)
243				free(intrnamebuf);
244			if (intrname)
245				free(intrname);
246			if (intrloc)
247				free(intrloc);
248			nintr = 0;
249			return(0);
250		}
251		for (cp = intrnamebuf, i = 0; i < nintr; i++) {
252			nextcp = cp + strlen(cp) + 1;
253
254			/* Discard trailing spaces. */
255			for (cp1 = nextcp - 1; cp1 > cp && *(cp1 - 1) == ' '; )
256				*--cp1 = '\0';
257
258			/* Convert "irqN: name" to "name irqN". */
259			if (strncmp(cp, "irq", 3) == 0) {
260				cp1 = cp + 3;
261				while (isdigit((u_char)*cp1))
262					cp1++;
263				if (cp1 != cp && *cp1 == ':' &&
264				    *(cp1 + 1) == ' ') {
265					*cp1 = '\0';
266					cp1 = cp1 + 2;
267					cp2 = strdup(cp);
268					bcopy(cp1, cp, strlen(cp1) + 1);
269					strcat(cp, " ");
270					strcat(cp, cp2);
271					free(cp2);
272				}
273			}
274
275			/*
276			 * Convert "name irqN" to "name N" if the former is
277			 * longer than the field width.
278			 */
279			if ((cp1 = strstr(cp, "irq")) != NULL &&
280			    strlen(cp) > 10)
281				bcopy(cp1 + 3, cp1, strlen(cp1 + 3) + 1);
282
283			intrname[i] = cp;
284			cp = nextcp;
285		}
286		nextintsrow = INTSROW + 2;
287		allocinfo(&s);
288		allocinfo(&s1);
289		allocinfo(&s2);
290		allocinfo(&z);
291	}
292	getinfo(&s2);
293	copyinfo(&s2, &s1);
294	return(1);
295}
296
297void
298fetchkre(void)
299{
300	time_t now;
301	struct tm *tp;
302	static int d_first = -1;
303
304	if (d_first < 0)
305		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
306
307	time(&now);
308	tp = localtime(&now);
309	(void) strftime(buf, sizeof(buf),
310			d_first ? "%e %b %R" : "%b %e %R", tp);
311	getinfo(&s);
312}
313
314void
315labelkre(void)
316{
317	int i, j;
318
319	clear();
320	mvprintw(STATROW, STATCOL + 6, "users    Load");
321	mvprintw(MEMROW, MEMCOL, "Mem:KB    REAL            VIRTUAL");
322	mvprintw(MEMROW + 1, MEMCOL, "        Tot   Share      Tot    Share");
323	mvprintw(MEMROW + 2, MEMCOL, "Act");
324	mvprintw(MEMROW + 3, MEMCOL, "All");
325
326	mvprintw(MEMROW + 1, MEMCOL + 41, "Free");
327
328	mvprintw(PAGEROW, PAGECOL,     "         VN PAGER   SWAP PAGER");
329	mvprintw(PAGEROW + 1, PAGECOL, "         in   out     in   out");
330	mvprintw(PAGEROW + 2, PAGECOL, "count");
331	mvprintw(PAGEROW + 3, PAGECOL, "pages");
332
333	mvprintw(INTSROW, INTSCOL + 1, "Interrupts");
334	mvprintw(INTSROW + 1, INTSCOL + 6, "total");
335
336	mvprintw(VMSTATROW, VMSTATCOL + 9, "cow");
337	mvprintw(VMSTATROW + 1, VMSTATCOL + 9, "zfod");
338	mvprintw(VMSTATROW + 2, VMSTATCOL + 9, "ozfod");
339	mvprintw(VMSTATROW + 3, VMSTATCOL + 9 - 1, "%%ozfod");
340	mvprintw(VMSTATROW + 4, VMSTATCOL + 9, "daefr");
341	mvprintw(VMSTATROW + 5, VMSTATCOL + 9, "prcfr");
342	mvprintw(VMSTATROW + 6, VMSTATCOL + 9, "totfr");
343	mvprintw(VMSTATROW + 7, VMSTATCOL + 9, "react");
344	mvprintw(VMSTATROW + 8, VMSTATCOL + 9, "pdwak");
345	mvprintw(VMSTATROW + 9, VMSTATCOL + 9, "pdpgs");
346	mvprintw(VMSTATROW + 10, VMSTATCOL + 9, "intrn");
347	mvprintw(VMSTATROW + 11, VMSTATCOL + 9, "wire");
348	mvprintw(VMSTATROW + 12, VMSTATCOL + 9, "act");
349	mvprintw(VMSTATROW + 13, VMSTATCOL + 9, "inact");
350	mvprintw(VMSTATROW + 14, VMSTATCOL + 9, "cache");
351	mvprintw(VMSTATROW + 15, VMSTATCOL + 9, "free");
352	if (LINES - 1 > VMSTATROW + 16)
353		mvprintw(VMSTATROW + 16, VMSTATCOL + 9, "buf");
354
355	mvprintw(GENSTATROW, GENSTATCOL, " Csw  Trp  Sys  Int  Sof  Flt");
356
357	mvprintw(GRAPHROW, GRAPHCOL,
358		"  . %%Sys    . %%Intr   . %%User   . %%Nice   . %%Idle");
359	mvprintw(PROCSROW, PROCSCOL, "Proc:");
360	mvprintw(PROCSROW + 1, PROCSCOL, "  r   p   d   s   w");
361	mvprintw(GRAPHROW + 1, GRAPHCOL,
362		"|    |    |    |    |    |    |    |    |    |    |");
363
364	mvprintw(VNSTATROW, VNSTATCOL + 8, "dtbuf");
365	mvprintw(VNSTATROW + 1, VNSTATCOL + 8, "desvn");
366	mvprintw(VNSTATROW + 2, VNSTATCOL + 8, "numvn");
367	mvprintw(VNSTATROW + 3, VNSTATCOL + 8, "frevn");
368
369	mvprintw(NAMEIROW, NAMEICOL, "Namei     Name-cache   Dir-cache");
370	mvprintw(NAMEIROW + 1, NAMEICOL,
371		"   Calls    hits   %%    hits   %%");
372	mvprintw(DISKROW, DISKCOL, "Disks");
373	mvprintw(DISKROW + 1, DISKCOL, "KB/t");
374	mvprintw(DISKROW + 2, DISKCOL, "tps");
375	mvprintw(DISKROW + 3, DISKCOL, "MB/s");
376	mvprintw(DISKROW + 4, DISKCOL, "%%busy");
377	/*
378	 * For now, we don't support a fourth disk statistic.  So there's
379	 * no point in providing a label for it.  If someone can think of a
380	 * fourth useful disk statistic, there is room to add it.
381	 */
382	/* mvprintw(DISKROW + 4, DISKCOL, " msps"); */
383	j = 0;
384	for (i = 0; i < num_devices && j < MAXDRIVES; i++)
385		if (dev_select[i].selected) {
386			char tmpstr[80];
387			sprintf(tmpstr, "%s%d", dev_select[i].device_name,
388				dev_select[i].unit_number);
389			mvprintw(DISKROW, DISKCOL + 5 + 6 * j,
390				" %5.5s", tmpstr);
391			j++;
392		}
393
394	for (i = 0; i < nintr; i++) {
395		if (intrloc[i] == 0)
396			continue;
397		mvprintw(intrloc[i], INTSCOL + 6, "%-10.10s", intrname[i]);
398	}
399}
400
401#define X(fld)	{t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
402#define Q(fld)	{t=cur.fld[i]; cur.fld[i]-=last.fld[i]; if(state==TIME) last.fld[i]=t;}
403#define Y(fld)	{t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
404#define Z(fld)	{t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
405	if(state == TIME) s1.nchstats.fld = t;}
406#define PUTRATE(fld, l, c, w) \
407do { \
408	Y(fld); \
409	putint((int)((float)s.fld/etime + 0.5), l, c, w); \
410} while (0)
411#define MAXFAIL 5
412
413static	char cpuchar[CPUSTATES] = { '=' , '+', '>', '-', ' ' };
414static	char cpuorder[CPUSTATES] = { CP_SYS, CP_INTR, CP_USER, CP_NICE,
415				     CP_IDLE };
416
417void
418showkre(void)
419{
420	float f1, f2;
421	int psiz, inttotal;
422	int i, l, lc;
423	static int failcnt = 0;
424
425	etime = 0;
426	for(i = 0; i < CPUSTATES; i++) {
427		X(time);
428		Q(cp_time);
429		etime += s.time[i];
430	}
431	if (etime < 5.0) {	/* < 5 ticks - ignore this trash */
432		if (failcnt++ >= MAXFAIL) {
433			clear();
434			mvprintw(2, 10, "The alternate system clock has died!");
435			mvprintw(3, 10, "Reverting to ``pigs'' display.");
436			move(CMDLINE, 0);
437			refresh();
438			failcnt = 0;
439			sleep(5);
440			command("pigs");
441		}
442		return;
443	}
444	failcnt = 0;
445	etime /= hertz;
446	etime /= ncpu;
447	inttotal = 0;
448	for (i = 0; i < nintr; i++) {
449		if (s.intrcnt[i] == 0)
450			continue;
451		if (intrloc[i] == 0) {
452			if (nextintsrow == LINES)
453				continue;
454			intrloc[i] = nextintsrow++;
455			mvprintw(intrloc[i], INTSCOL + 6, "%-10.10s",
456				intrname[i]);
457		}
458		X(intrcnt);
459		l = (int)((float)s.intrcnt[i]/etime + 0.5);
460		inttotal += l;
461		putint(l, intrloc[i], INTSCOL, 5);
462	}
463	putint(inttotal, INTSROW + 1, INTSCOL, 5);
464	Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
465	Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes); Z(ncs_neghits);
466	s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
467	    nchtotal.ncs_miss + nchtotal.ncs_long + nchtotal.ncs_neghits;
468	if (state == TIME)
469		s1.nchcount = s.nchcount;
470
471	psiz = 0;
472	f2 = 0.0;
473	for (lc = 0; lc < CPUSTATES; lc++) {
474		i = cpuorder[lc];
475		f1 = cputime(i);
476		f2 += f1;
477		l = (int) ((f2 + 1.0) / 2.0) - psiz;
478		putfloat(f1, GRAPHROW, GRAPHCOL + 10 * lc, 4, 1, 0);
479		move(GRAPHROW + 2, psiz);
480		psiz += l;
481		while (l-- > 0)
482			addch(cpuchar[lc]);
483	}
484
485	putint(ucount(), STATROW, STATCOL, 5);
486	putfloat(avenrun[0], STATROW, STATCOL + 20, 5, 2, 0);
487	putfloat(avenrun[1], STATROW, STATCOL + 26, 5, 2, 0);
488	putfloat(avenrun[2], STATROW, STATCOL + 32, 5, 2, 0);
489	mvaddstr(STATROW, STATCOL + 55, buf);
490#define pgtokb(pg)	((pg) * (s.v_page_size / 1024))
491	putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 4, 7);
492	putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 12, 7);
493	putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 20, 8);
494	putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 29, 8);
495	putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 4, 7);
496	putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 12, 7);
497	putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 20, 8);
498	putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 29, 8);
499	putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 38, 7);
500	putint(total.t_rq - 1, PROCSROW + 2, PROCSCOL, 3);
501	putint(total.t_pw, PROCSROW + 2, PROCSCOL + 4, 3);
502	putint(total.t_dw, PROCSROW + 2, PROCSCOL + 8, 3);
503	putint(total.t_sl, PROCSROW + 2, PROCSCOL + 12, 3);
504	putint(total.t_sw, PROCSROW + 2, PROCSCOL + 16, 3);
505	PUTRATE(v_cow_faults, VMSTATROW, VMSTATCOL + 2, 8 - 2);
506	PUTRATE(v_zfod, VMSTATROW + 1, VMSTATCOL + 2, 8 - 2);
507	PUTRATE(v_ozfod, VMSTATROW + 2, VMSTATCOL, 8);
508	putint(s.v_zfod != 0 ? (int)(s.v_ozfod * 100.0 / s.v_zfod) : 0,
509	    VMSTATROW + 3, VMSTATCOL + 1, 8 - 1);
510	PUTRATE(v_dfree, VMSTATROW + 4, VMSTATCOL + 2, 8 - 2);
511	PUTRATE(v_pfree, VMSTATROW + 5, VMSTATCOL + 2, 8 - 2);
512	PUTRATE(v_tfree, VMSTATROW + 6, VMSTATCOL, 8);
513	PUTRATE(v_reactivated, VMSTATROW + 7, VMSTATCOL, 8);
514	PUTRATE(v_pdwakeups, VMSTATROW + 8, VMSTATCOL, 8);
515	PUTRATE(v_pdpages, VMSTATROW + 9, VMSTATCOL, 8);
516	PUTRATE(v_intrans, VMSTATROW + 10, VMSTATCOL, 8);
517	putint(pgtokb(s.v_wire_count), VMSTATROW + 11, VMSTATCOL, 8);
518	putint(pgtokb(s.v_active_count), VMSTATROW + 12, VMSTATCOL, 8);
519	putint(pgtokb(s.v_inactive_count), VMSTATROW + 13, VMSTATCOL, 8);
520	putint(pgtokb(s.v_cache_count), VMSTATROW + 14, VMSTATCOL, 8);
521	putint(pgtokb(s.v_free_count), VMSTATROW + 15, VMSTATCOL, 8);
522	if (LINES - 1 > VMSTATROW + 16)
523		putint(s.bufspace / 1024, VMSTATROW + 16, VMSTATCOL, 8);
524	PUTRATE(v_vnodein, PAGEROW + 2, PAGECOL + 6, 5);
525	PUTRATE(v_vnodeout, PAGEROW + 2, PAGECOL + 12, 5);
526	PUTRATE(v_swapin, PAGEROW + 2, PAGECOL + 19, 5);
527	PUTRATE(v_swapout, PAGEROW + 2, PAGECOL + 25, 5);
528	PUTRATE(v_vnodepgsin, PAGEROW + 3, PAGECOL + 6, 5);
529	PUTRATE(v_vnodepgsout, PAGEROW + 3, PAGECOL + 12, 5);
530	PUTRATE(v_swappgsin, PAGEROW + 3, PAGECOL + 19, 5);
531	PUTRATE(v_swappgsout, PAGEROW + 3, PAGECOL + 25, 5);
532	PUTRATE(v_swtch, GENSTATROW + 1, GENSTATCOL, 4);
533	PUTRATE(v_trap, GENSTATROW + 1, GENSTATCOL + 5, 4);
534	PUTRATE(v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 4);
535	PUTRATE(v_intr, GENSTATROW + 1, GENSTATCOL + 15, 4);
536	PUTRATE(v_soft, GENSTATROW + 1, GENSTATCOL + 20, 4);
537	PUTRATE(v_vm_faults, GENSTATROW + 1, GENSTATCOL + 25, 4);
538	for (i = 0, lc = 0; i < num_devices && lc < MAXDRIVES; i++)
539		if (dev_select[i].selected) {
540			switch(state) {
541			case TIME:
542				dinfo(i, ++lc, &cur, &last);
543				break;
544			case RUN:
545				dinfo(i, ++lc, &cur, &run);
546				break;
547			case BOOT:
548				dinfo(i, ++lc, &cur, NULL);
549				break;
550			}
551		}
552	putint(s.numdirtybuffers, VNSTATROW, VNSTATCOL, 7);
553	putint(s.desiredvnodes, VNSTATROW + 1, VNSTATCOL, 7);
554	putint(s.numvnodes, VNSTATROW + 2, VNSTATCOL, 7);
555	putint(s.freevnodes, VNSTATROW + 3, VNSTATCOL, 7);
556	putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 8);
557	putint((nchtotal.ncs_goodhits + nchtotal.ncs_neghits),
558	   NAMEIROW + 2, NAMEICOL + 9, 7);
559#define nz(x)	((x) ? (x) : 1)
560	putfloat((nchtotal.ncs_goodhits+nchtotal.ncs_neghits) *
561	   100.0 / nz(s.nchcount),
562	   NAMEIROW + 2, NAMEICOL + 17, 3, 0, 1);
563	putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 21, 7);
564	putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
565	   NAMEIROW + 2, NAMEICOL + 29, 3, 0, 1);
566#undef nz
567}
568
569int
570cmdkre(const char *cmd, const char *args)
571{
572	int retval;
573
574	if (prefix(cmd, "run")) {
575		retval = 1;
576		copyinfo(&s2, &s1);
577		switch (devstat_getdevs(NULL, &run)) {
578		case -1:
579			errx(1, "%s", devstat_errbuf);
580			break;
581		case 1:
582			num_devices = run.dinfo->numdevs;
583			generation = run.dinfo->generation;
584			retval = dscmd("refresh", NULL, MAXDRIVES, &cur);
585			if (retval == 2)
586				labelkre();
587			break;
588		default:
589			break;
590		}
591		state = RUN;
592		return (retval);
593	}
594	if (prefix(cmd, "boot")) {
595		state = BOOT;
596		copyinfo(&z, &s1);
597		return (1);
598	}
599	if (prefix(cmd, "time")) {
600		state = TIME;
601		return (1);
602	}
603	if (prefix(cmd, "zero")) {
604		retval = 1;
605		if (state == RUN) {
606			getinfo(&s1);
607			switch (devstat_getdevs(NULL, &run)) {
608			case -1:
609				errx(1, "%s", devstat_errbuf);
610				break;
611			case 1:
612				num_devices = run.dinfo->numdevs;
613				generation = run.dinfo->generation;
614				retval = dscmd("refresh",NULL, MAXDRIVES, &cur);
615				if (retval == 2)
616					labelkre();
617				break;
618			default:
619				break;
620			}
621		}
622		return (retval);
623	}
624	retval = dscmd(cmd, args, MAXDRIVES, &cur);
625
626	if (retval == 2)
627		labelkre();
628
629	return(retval);
630}
631
632/* calculate number of users on the system */
633static int
634ucount(void)
635{
636	int nusers = 0;
637
638	if (ut < 0)
639		return (0);
640	while (read(ut, &utmp, sizeof(utmp)))
641		if (utmp.ut_name[0] != '\0')
642			nusers++;
643
644	lseek(ut, 0L, L_SET);
645	return (nusers);
646}
647
648static float
649cputime(int indx)
650{
651	double lt;
652	int i;
653
654	lt = 0;
655	for (i = 0; i < CPUSTATES; i++)
656		lt += s.time[i];
657	if (lt == 0.0)
658		lt = 1.0;
659	return (s.time[indx] * 100.0 / lt);
660}
661
662static void
663putint(int n, int l, int lc, int w)
664{
665	int snr;
666	char b[128];
667
668	move(l, lc);
669#ifdef DEBUG
670		while (w-- > 0)
671			addch('*');
672		return;
673#endif
674	if (n == 0) {
675		while (w-- > 0)
676			addch(' ');
677		return;
678	}
679	snr = snprintf(b, sizeof(b), "%*d", w, n);
680	if (snr != w)
681		snr = snprintf(b, sizeof(b), "%*dk", w - 1, n / 1000);
682	if (snr != w)
683		snr = snprintf(b, sizeof(b), "%*dM", w - 1, n / 1000000);
684	if (snr != w) {
685		while (w-- > 0)
686			addch('*');
687		return;
688	}
689	addstr(b);
690}
691
692static void
693putfloat(double f, int l, int lc, int w, int d, int nz)
694{
695	int snr;
696	char b[128];
697
698	move(l, lc);
699#ifdef DEBUG
700		while (--w >= 0)
701			addch('*');
702		return;
703#endif
704	if (nz && f == 0.0) {
705		while (--w >= 0)
706			addch(' ');
707		return;
708	}
709	snr = snprintf(b, sizeof(b), "%*.*f", w, d, f);
710	if (snr != w)
711		snr = snprintf(b, sizeof(b), "%*.0f", w, f);
712	if (snr != w) {
713		while (--w >= 0)
714			addch('*');
715		return;
716	}
717	addstr(b);
718}
719
720static void
721putlongdouble(long double f, int l, int lc, int w, int d, int nz)
722{
723	int snr;
724	char b[128];
725
726	move(l, lc);
727#ifdef DEBUG
728		while (--w >= 0)
729			addch('*');
730		return;
731#endif
732	if (nz && f == 0.0) {
733		while (--w >= 0)
734			addch(' ');
735		return;
736	}
737	snr = snprintf(b, sizeof(b), "%*.*Lf", w, d, f);
738	if (snr != w)
739		snr = snprintf(b, sizeof(b), "%*.0Lf", w, f);
740	if (snr != w) {
741		while (--w >= 0)
742			addch('*');
743		return;
744	}
745	addstr(b);
746}
747
748static void
749getinfo(struct Info *ls)
750{
751	struct devinfo *tmp_dinfo;
752	size_t size;
753	int mib[2];
754
755	GETSYSCTL("kern.cp_time", ls->time);
756	GETSYSCTL("kern.cp_time", cur.cp_time);
757	GETSYSCTL("vm.stats.sys.v_swtch", ls->v_swtch);
758	GETSYSCTL("vm.stats.sys.v_trap", ls->v_trap);
759	GETSYSCTL("vm.stats.sys.v_syscall", ls->v_syscall);
760	GETSYSCTL("vm.stats.sys.v_intr", ls->v_intr);
761	GETSYSCTL("vm.stats.sys.v_soft", ls->v_soft);
762	GETSYSCTL("vm.stats.vm.v_vm_faults", ls->v_vm_faults);
763	GETSYSCTL("vm.stats.vm.v_cow_faults", ls->v_cow_faults);
764	GETSYSCTL("vm.stats.vm.v_zfod", ls->v_zfod);
765	GETSYSCTL("vm.stats.vm.v_ozfod", ls->v_ozfod);
766	GETSYSCTL("vm.stats.vm.v_swapin", ls->v_swapin);
767	GETSYSCTL("vm.stats.vm.v_swapout", ls->v_swapout);
768	GETSYSCTL("vm.stats.vm.v_swappgsin", ls->v_swappgsin);
769	GETSYSCTL("vm.stats.vm.v_swappgsout", ls->v_swappgsout);
770	GETSYSCTL("vm.stats.vm.v_vnodein", ls->v_vnodein);
771	GETSYSCTL("vm.stats.vm.v_vnodeout", ls->v_vnodeout);
772	GETSYSCTL("vm.stats.vm.v_vnodepgsin", ls->v_vnodepgsin);
773	GETSYSCTL("vm.stats.vm.v_vnodepgsout", ls->v_vnodepgsout);
774	GETSYSCTL("vm.stats.vm.v_intrans", ls->v_intrans);
775	GETSYSCTL("vm.stats.vm.v_reactivated", ls->v_reactivated);
776	GETSYSCTL("vm.stats.vm.v_pdwakeups", ls->v_pdwakeups);
777	GETSYSCTL("vm.stats.vm.v_pdpages", ls->v_pdpages);
778	GETSYSCTL("vm.stats.vm.v_dfree", ls->v_dfree);
779	GETSYSCTL("vm.stats.vm.v_pfree", ls->v_pfree);
780	GETSYSCTL("vm.stats.vm.v_tfree", ls->v_tfree);
781	GETSYSCTL("vm.stats.vm.v_page_size", ls->v_page_size);
782	GETSYSCTL("vm.stats.vm.v_free_count", ls->v_free_count);
783	GETSYSCTL("vm.stats.vm.v_wire_count", ls->v_wire_count);
784	GETSYSCTL("vm.stats.vm.v_active_count", ls->v_active_count);
785	GETSYSCTL("vm.stats.vm.v_inactive_count", ls->v_inactive_count);
786	GETSYSCTL("vm.stats.vm.v_cache_count", ls->v_cache_count);
787	GETSYSCTL("vfs.bufspace", ls->bufspace);
788	GETSYSCTL("kern.maxvnodes", ls->desiredvnodes);
789	GETSYSCTL("vfs.numvnodes", ls->numvnodes);
790	GETSYSCTL("vfs.freevnodes", ls->freevnodes);
791	GETSYSCTL("vfs.cache.nchstats", ls->nchstats);
792	GETSYSCTL("vfs.numdirtybuffers", ls->numdirtybuffers);
793	getsysctl("hw.intrcnt", ls->intrcnt, nintr * sizeof(u_long));
794
795	size = sizeof(ls->Total);
796	mib[0] = CTL_VM;
797	mib[1] = VM_TOTAL;
798	if (sysctl(mib, 2, &ls->Total, &size, NULL, 0) < 0) {
799		error("Can't get kernel info: %s\n", strerror(errno));
800		bzero(&ls->Total, sizeof(ls->Total));
801	}
802	size = sizeof(ncpu);
803	if (sysctlbyname("hw.ncpu", &ncpu, &size, NULL, 0) < 0 ||
804	    size != sizeof(ncpu))
805		ncpu = 1;
806
807	tmp_dinfo = last.dinfo;
808	last.dinfo = cur.dinfo;
809	cur.dinfo = tmp_dinfo;
810
811	last.snap_time = cur.snap_time;
812	switch (devstat_getdevs(NULL, &cur)) {
813	case -1:
814		errx(1, "%s", devstat_errbuf);
815		break;
816	case 1:
817		num_devices = cur.dinfo->numdevs;
818		generation = cur.dinfo->generation;
819		cmdkre("refresh", NULL);
820		break;
821	default:
822		break;
823	}
824}
825
826static void
827allocinfo(struct Info *ls)
828{
829
830	ls->intrcnt = (long *) calloc(nintr, sizeof(long));
831	if (ls->intrcnt == NULL)
832		errx(2, "out of memory");
833}
834
835static void
836copyinfo(struct Info *from, struct Info *to)
837{
838	long *intrcnt;
839
840	/*
841	 * time, wds, seek, and xfer are malloc'd so we have to
842	 * save the pointers before the structure copy and then
843	 * copy by hand.
844	 */
845	intrcnt = to->intrcnt;
846	*to = *from;
847
848	bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
849}
850
851static void
852dinfo(int dn, int lc, struct statinfo *now, struct statinfo *then)
853{
854	long double transfers_per_second;
855	long double kb_per_transfer, mb_per_second;
856	long double elapsed_time, device_busy;
857	int di;
858
859	di = dev_select[dn].position;
860
861	if (then != NULL) {
862		/* Calculate relative to previous sample */
863		elapsed_time = now->snap_time - then->snap_time;
864	} else {
865		/* Calculate relative to device creation */
866	        elapsed_time = now->snap_time - devstat_compute_etime(
867		    &now->dinfo->devices[di].creation_time, NULL);
868	}
869
870	if (devstat_compute_statistics(&now->dinfo->devices[di], then ?
871	    &then->dinfo->devices[di] : NULL, elapsed_time,
872	    DSM_KB_PER_TRANSFER, &kb_per_transfer,
873	    DSM_TRANSFERS_PER_SECOND, &transfers_per_second,
874	    DSM_MB_PER_SECOND, &mb_per_second,
875	    DSM_BUSY_PCT, &device_busy,
876	    DSM_NONE) != 0)
877		errx(1, "%s", devstat_errbuf);
878
879	lc = DISKCOL + lc * 6;
880	putlongdouble(kb_per_transfer, DISKROW + 1, lc, 5, 2, 0);
881	putlongdouble(transfers_per_second, DISKROW + 2, lc, 5, 0, 0);
882	putlongdouble(mb_per_second, DISKROW + 3, lc, 5, 2, 0);
883	putlongdouble(device_busy, DISKROW + 4, lc, 5, 0, 0);
884}
885