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