vmstat.c revision 111002
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 111002 2003-02-16 14:13:23Z phk $");
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/sysctl.h>
53#include <sys/vmmeter.h>
54
55#include <vm/vm_param.h>
56
57#include <ctype.h>
58#include <err.h>
59#include <errno.h>
60#include <langinfo.h>
61#include <nlist.h>
62#include <paths.h>
63#include <signal.h>
64#include <stdlib.h>
65#include <string.h>
66#include <time.h>
67#include <unistd.h>
68#include <utmp.h>
69#include <devstat.h>
70#include "systat.h"
71#include "extern.h"
72#include "devs.h"
73
74static struct Info {
75	long	time[CPUSTATES];
76	u_int v_swtch;		/* context switches */
77	u_int v_trap;		/* calls to trap */
78	u_int v_syscall;	/* calls to syscall() */
79	u_int v_intr;		/* device interrupts */
80	u_int v_soft;		/* software interrupts */
81	/*
82	 * Virtual memory activity.
83	 */
84	u_int v_vm_faults;	/* number of address memory faults */
85	u_int v_cow_faults;	/* number of copy-on-writes */
86	u_int v_zfod;		/* pages zero filled on demand */
87	u_int v_ozfod;		/* optimized zero fill pages */
88	u_int v_swapin;		/* swap pager pageins */
89	u_int v_swapout;	/* swap pager pageouts */
90	u_int v_swappgsin;	/* swap pager pages paged in */
91	u_int v_swappgsout;	/* swap pager pages paged out */
92	u_int v_vnodein;	/* vnode pager pageins */
93	u_int v_vnodeout;	/* vnode pager pageouts */
94	u_int v_vnodepgsin;	/* vnode_pager pages paged in */
95	u_int v_vnodepgsout;	/* vnode pager pages paged out */
96	u_int v_intrans;	/* intransit blocking page faults */
97	u_int v_reactivated;	/* number of pages reactivated from free list */
98	u_int v_pdwakeups;	/* number of times daemon has awaken from sleep */
99	u_int v_pdpages;	/* number of pages analyzed by daemon */
100
101	u_int v_dfree;		/* pages freed by daemon */
102	u_int v_pfree;		/* pages freed by exiting processes */
103	u_int v_tfree;		/* total pages freed */
104	/*
105	 * Distribution of page usages.
106	 */
107	u_int v_page_size;	/* page size in bytes */
108	u_int v_free_count;	/* number of pages free */
109	u_int v_wire_count;	/* number of pages wired down */
110	u_int v_active_count;	/* number of pages active */
111	u_int v_inactive_count;	/* number of pages inactive */
112	u_int v_cache_count;	/* number of pages on buffer cache queue */
113	struct	vmtotal Total;
114	struct	nchstats nchstats;
115	long	nchcount;
116	long	*intrcnt;
117	int	bufspace;
118	int	desiredvnodes;
119	long	numvnodes;
120	long	freevnodes;
121	int	numdirtybuffers;
122} s, s1, s2, z;
123
124struct statinfo cur, last, run;
125
126#define	total s.Total
127#define	nchtotal s.nchstats
128#define	oldnchtotal s1.nchstats
129
130static	enum state { BOOT, TIME, RUN } state = TIME;
131
132static void allocinfo(struct Info *);
133static void copyinfo(struct Info *, struct Info *);
134static float cputime(int);
135static void dinfo(int, int, struct statinfo *, struct statinfo *);
136static void getinfo(struct Info *);
137static void putint(int, int, int, int);
138static void putfloat(double, int, int, int, int, int);
139static void putlongdouble(long double, int, int, int, int, int);
140static int ucount(void);
141
142static	int ncpu;
143static	int ut;
144static	char buf[26];
145static	time_t t;
146static	double etime;
147static	int nintr;
148static	long *intrloc;
149static	char **intrname;
150static	int nextintsrow;
151static  int extended_vm_stats;
152
153struct	utmp utmp;
154
155
156WINDOW *
157openkre()
158{
159
160	ut = open(_PATH_UTMP, O_RDONLY);
161	if (ut < 0)
162		error("No utmp");
163	return (stdscr);
164}
165
166void
167closekre(w)
168	WINDOW *w;
169{
170
171	(void) close(ut);
172	if (w == NULL)
173		return;
174	wclear(w);
175	wrefresh(w);
176}
177
178/*
179 * These constants define where the major pieces are laid out
180 */
181#define STATROW		 0	/* uses 1 row and 68 cols */
182#define STATCOL		 2
183#define MEMROW		 2	/* uses 4 rows and 31 cols */
184#define MEMCOL		 0
185#define PAGEROW		 2	/* uses 4 rows and 26 cols */
186#define PAGECOL		46
187#define INTSROW		 6	/* uses all rows to bottom and 17 cols */
188#define INTSCOL		61
189#define PROCSROW	 7	/* uses 2 rows and 20 cols */
190#define PROCSCOL	 0
191#define GENSTATROW	 7	/* uses 2 rows and 30 cols */
192#define GENSTATCOL	20
193#define VMSTATROW	 6	/* uses 17 rows and 12 cols */
194#define VMSTATCOL	48
195#define GRAPHROW	10	/* uses 3 rows and 51 cols */
196#define GRAPHCOL	 0
197#define NAMEIROW	14	/* uses 3 rows and 38 cols */
198#define NAMEICOL	 0
199#define DISKROW		18	/* uses 5 rows and 50 cols (for 9 drives) */
200#define DISKCOL		 0
201
202#define	DRIVESPACE	 7	/* max # for space */
203
204#define	MAXDRIVES	DRIVESPACE	 /* max # to display */
205
206int
207initkre()
208{
209	char *intrnamebuf, *cp;
210	int i;
211	size_t sz;
212
213	if ((num_devices = devstat_getnumdevs(NULL)) < 0) {
214		warnx("%s", devstat_errbuf);
215		return(0);
216	}
217
218	cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
219	last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
220	run.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
221	bzero(cur.dinfo, sizeof(struct devinfo));
222	bzero(last.dinfo, sizeof(struct devinfo));
223	bzero(run.dinfo, sizeof(struct devinfo));
224
225	if (dsinit(MAXDRIVES, &cur, &last, &run) != 1)
226		return(0);
227
228	if (nintr == 0) {
229		if (sysctlbyname("hw.intrcnt", NULL, &sz, NULL, 0) == -1) {
230			error("sysctl(hw.intrcnt...) failed: %s",
231			      strerror(errno));
232			return (0);
233		}
234		nintr = sz / sizeof(u_long);
235		intrloc = calloc(nintr, sizeof (long));
236		intrname = calloc(nintr, sizeof (char *));
237		intrnamebuf = sysctl_dynread("hw.intrnames", NULL);
238		if (intrnamebuf == NULL || intrname == NULL ||
239		    intrloc == NULL) {
240			error("Out of memory");
241			if (intrnamebuf)
242				free(intrnamebuf);
243			if (intrname)
244				free(intrname);
245			if (intrloc)
246				free(intrloc);
247			nintr = 0;
248			return(0);
249		}
250		for (cp = intrnamebuf, i = 0; i < nintr; i++) {
251			intrname[i] = cp;
252			cp += strlen(cp) + 1;
253		}
254		nextintsrow = INTSROW + 2;
255		allocinfo(&s);
256		allocinfo(&s1);
257		allocinfo(&s2);
258		allocinfo(&z);
259	}
260	getinfo(&s2);
261	copyinfo(&s2, &s1);
262	return(1);
263}
264
265void
266fetchkre()
267{
268	time_t now;
269	struct tm *tp;
270	static int d_first = -1;
271
272	if (d_first < 0)
273		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
274
275	time(&now);
276	tp = localtime(&now);
277	(void) strftime(buf, sizeof(buf),
278			d_first ? "%e %b %R" : "%b %e %R", tp);
279	getinfo(&s);
280}
281
282void
283labelkre()
284{
285	int i, j;
286
287	clear();
288	mvprintw(STATROW, STATCOL + 4, "users    Load");
289	mvprintw(MEMROW, MEMCOL, "Mem:KB    REAL            VIRTUAL");
290	mvprintw(MEMROW + 1, MEMCOL, "        Tot   Share      Tot    Share");
291	mvprintw(MEMROW + 2, MEMCOL, "Act");
292	mvprintw(MEMROW + 3, MEMCOL, "All");
293
294	mvprintw(MEMROW + 1, MEMCOL + 41, "Free");
295
296	mvprintw(PAGEROW, PAGECOL,     "        VN PAGER  SWAP PAGER ");
297	mvprintw(PAGEROW + 1, PAGECOL, "        in  out     in  out ");
298	mvprintw(PAGEROW + 2, PAGECOL, "count");
299	mvprintw(PAGEROW + 3, PAGECOL, "pages");
300
301	mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
302	mvprintw(INTSROW + 1, INTSCOL + 9, "total");
303
304	mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "cow");
305	mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "wire");
306	mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "act");
307	mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "inact");
308	mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "cache");
309	mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "free");
310	mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "daefr");
311	mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "prcfr");
312	mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "react");
313	mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "pdwake");
314	mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "pdpgs");
315	mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "intrn");
316	mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "buf");
317	mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "dirtybuf");
318
319	mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "desiredvnodes");
320	mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "numvnodes");
321	mvprintw(VMSTATROW + 17, VMSTATCOL + 10, "freevnodes");
322
323	mvprintw(GENSTATROW, GENSTATCOL, "  Csw  Trp  Sys  Int  Sof  Flt");
324
325	mvprintw(GRAPHROW, GRAPHCOL,
326		"  . %%Sys    . %%Intr   . %%User   . %%Nice   . %%Idle");
327	mvprintw(PROCSROW, PROCSCOL, "Proc:r  p  d  s  w");
328	mvprintw(GRAPHROW + 1, GRAPHCOL,
329		"|    |    |    |    |    |    |    |    |    |    |");
330
331	mvprintw(NAMEIROW, NAMEICOL, "Namei         Name-cache    Dir-cache");
332	mvprintw(NAMEIROW + 1, NAMEICOL,
333		"    Calls     hits    %%     hits    %%");
334	mvprintw(DISKROW, DISKCOL, "Disks");
335	mvprintw(DISKROW + 1, DISKCOL, "KB/t");
336	mvprintw(DISKROW + 2, DISKCOL, "tps");
337	mvprintw(DISKROW + 3, DISKCOL, "MB/s");
338	mvprintw(DISKROW + 4, DISKCOL, "%% busy");
339	/*
340	 * For now, we don't support a fourth disk statistic.  So there's
341	 * no point in providing a label for it.  If someone can think of a
342	 * fourth useful disk statistic, there is room to add it.
343	 */
344	/* mvprintw(DISKROW + 4, DISKCOL, " msps"); */
345	j = 0;
346	for (i = 0; i < num_devices && j < MAXDRIVES; i++)
347		if (dev_select[i].selected) {
348			char tmpstr[80];
349			sprintf(tmpstr, "%s%d", dev_select[i].device_name,
350				dev_select[i].unit_number);
351			mvprintw(DISKROW, DISKCOL + 5 + 6 * j,
352				" %5.5s", tmpstr);
353			j++;
354		}
355
356	if (j <= 4) {
357		/*
358		 * room for extended VM stats
359		 */
360		mvprintw(VMSTATROW + 11, VMSTATCOL - 6, "zfod");
361		mvprintw(VMSTATROW + 12, VMSTATCOL - 6, "ofod");
362		mvprintw(VMSTATROW + 13, VMSTATCOL - 6, "%%slo-z");
363		mvprintw(VMSTATROW + 14, VMSTATCOL - 6, "tfree");
364		extended_vm_stats = 1;
365	} else {
366		extended_vm_stats = 0;
367		mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "zfod");
368	}
369
370	for (i = 0; i < nintr; i++) {
371		if (intrloc[i] == 0)
372			continue;
373		mvprintw(intrloc[i], INTSCOL + 9, "%-10.10s", intrname[i]);
374	}
375}
376
377#define X(fld)	{t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
378#define Q(fld)	{t=cur.fld[i]; cur.fld[i]-=last.fld[i]; if(state==TIME) last.fld[i]=t;}
379#define Y(fld)	{t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
380#define Z(fld)	{t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
381	if(state == TIME) s1.nchstats.fld = t;}
382#define PUTRATE(fld, l, c, w) \
383	Y(fld); \
384	putint((int)((float)s.fld/etime + 0.5), l, c, w)
385#define MAXFAIL 5
386
387static	char cpuchar[CPUSTATES] = { '=' , '+', '>', '-', ' ' };
388static	char cpuorder[CPUSTATES] = { CP_SYS, CP_INTR, CP_USER, CP_NICE,
389				     CP_IDLE };
390
391void
392showkre()
393{
394	float f1, f2;
395	int psiz, inttotal;
396	int i, j, k, l, lc;
397	static int failcnt = 0;
398	char intrbuffer[10];
399
400	etime = 0;
401	for(i = 0; i < CPUSTATES; i++) {
402		X(time);
403		Q(cp_time);
404		etime += s.time[i];
405	}
406	if (etime < 5.0) {	/* < 5 ticks - ignore this trash */
407		if (failcnt++ >= MAXFAIL) {
408			clear();
409			mvprintw(2, 10, "The alternate system clock has died!");
410			mvprintw(3, 10, "Reverting to ``pigs'' display.");
411			move(CMDLINE, 0);
412			refresh();
413			failcnt = 0;
414			sleep(5);
415			command("pigs");
416		}
417		return;
418	}
419	failcnt = 0;
420	etime /= hertz;
421	etime /= ncpu;
422	inttotal = 0;
423	for (i = 0; i < nintr; i++) {
424		if (s.intrcnt[i] == 0)
425			continue;
426		if (intrloc[i] == 0) {
427			if (nextintsrow == LINES)
428				continue;
429			intrloc[i] = nextintsrow++;
430			k = 0;
431			for (j = 0; j < (int)sizeof(intrbuffer); j++) {
432				if (strncmp(&intrname[i][j], "irq", 3) == 0)
433					j += 3;
434				intrbuffer[k++] = intrname[i][j];
435			}
436			intrbuffer[k] = '\0';
437			mvprintw(intrloc[i], INTSCOL + 9, "%-10.10s",
438				intrbuffer);
439		}
440		X(intrcnt);
441		l = (int)((float)s.intrcnt[i]/etime + 0.5);
442		inttotal += l;
443		putint(l, intrloc[i], INTSCOL + 2, 6);
444	}
445	putint(inttotal, INTSROW + 1, INTSCOL + 2, 6);
446	Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
447	Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes); Z(ncs_neghits);
448	s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
449	    nchtotal.ncs_miss + nchtotal.ncs_long + nchtotal.ncs_neghits;
450	if (state == TIME)
451		s1.nchcount = s.nchcount;
452
453	psiz = 0;
454	f2 = 0.0;
455	for (lc = 0; lc < CPUSTATES; lc++) {
456		i = cpuorder[lc];
457		f1 = cputime(i);
458		f2 += f1;
459		l = (int) ((f2 + 1.0) / 2.0) - psiz;
460		if (f1 > 99.9)
461			f1 = 99.9;	/* no room to display 100.0 */
462		putfloat(f1, GRAPHROW, GRAPHCOL + 10 * lc, 4, 1, 0);
463		move(GRAPHROW + 2, psiz);
464		psiz += l;
465		while (l-- > 0)
466			addch(cpuchar[lc]);
467	}
468
469	putint(ucount(), STATROW, STATCOL, 3);
470	putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
471	putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
472	putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
473	mvaddstr(STATROW, STATCOL + 53, buf);
474#define pgtokb(pg)	((pg) * s.v_page_size / 1024)
475	putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 3, 8);
476	putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 11, 8);
477	putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 19, 9);
478	putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 28, 9);
479	putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 3, 8);
480	putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 11, 8);
481	putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 19, 9);
482	putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 28, 9);
483	putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 37, 8);
484	putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
485	putint(total.t_pw, PROCSROW + 1, PROCSCOL + 6, 3);
486	putint(total.t_dw, PROCSROW + 1, PROCSCOL + 9, 3);
487	putint(total.t_sl, PROCSROW + 1, PROCSCOL + 12, 3);
488	putint(total.t_sw, PROCSROW + 1, PROCSCOL + 15, 3);
489	if (extended_vm_stats == 0) {
490		PUTRATE(v_zfod, VMSTATROW + 0, VMSTATCOL + 4, 5);
491	}
492	PUTRATE(v_cow_faults, VMSTATROW + 1, VMSTATCOL + 3, 6);
493	putint(pgtokb(s.v_wire_count), VMSTATROW + 2, VMSTATCOL, 9);
494	putint(pgtokb(s.v_active_count), VMSTATROW + 3, VMSTATCOL, 9);
495	putint(pgtokb(s.v_inactive_count), VMSTATROW + 4, VMSTATCOL, 9);
496	putint(pgtokb(s.v_cache_count), VMSTATROW + 5, VMSTATCOL, 9);
497	putint(pgtokb(s.v_free_count), VMSTATROW + 6, VMSTATCOL, 9);
498	PUTRATE(v_dfree, VMSTATROW + 7, VMSTATCOL, 9);
499	PUTRATE(v_pfree, VMSTATROW + 8, VMSTATCOL, 9);
500	PUTRATE(v_reactivated, VMSTATROW + 9, VMSTATCOL, 9);
501	PUTRATE(v_pdwakeups, VMSTATROW + 10, VMSTATCOL, 9);
502	PUTRATE(v_pdpages, VMSTATROW + 11, VMSTATCOL, 9);
503	PUTRATE(v_intrans, VMSTATROW + 12, VMSTATCOL, 9);
504
505	if (extended_vm_stats) {
506	    PUTRATE(v_zfod, VMSTATROW + 11, VMSTATCOL - 16, 9);
507	    PUTRATE(v_ozfod, VMSTATROW + 12, VMSTATCOL - 16, 9);
508	    putint(
509		((s.v_ozfod < s.v_zfod) ?
510		    s.v_ozfod * 100 / s.v_zfod :
511		    0
512		),
513		VMSTATROW + 13,
514		VMSTATCOL - 16,
515		9
516	    );
517	    PUTRATE(v_tfree, VMSTATROW + 14, VMSTATCOL - 16, 9);
518	}
519
520	putint(s.bufspace/1024, VMSTATROW + 13, VMSTATCOL, 9);
521	putint(s.numdirtybuffers, VMSTATROW + 14, VMSTATCOL, 9);
522	putint(s.desiredvnodes, VMSTATROW + 15, VMSTATCOL, 9);
523	putint(s.numvnodes, VMSTATROW + 16, VMSTATCOL, 9);
524	putint(s.freevnodes, VMSTATROW + 17, VMSTATCOL, 9);
525	PUTRATE(v_vnodein, PAGEROW + 2, PAGECOL + 5, 5);
526	PUTRATE(v_vnodeout, PAGEROW + 2, PAGECOL + 10, 5);
527	PUTRATE(v_swapin, PAGEROW + 2, PAGECOL + 17, 5);
528	PUTRATE(v_swapout, PAGEROW + 2, PAGECOL + 22, 5);
529	PUTRATE(v_vnodepgsin, PAGEROW + 3, PAGECOL + 5, 5);
530	PUTRATE(v_vnodepgsout, PAGEROW + 3, PAGECOL + 10, 5);
531	PUTRATE(v_swappgsin, PAGEROW + 3, PAGECOL + 17, 5);
532	PUTRATE(v_swappgsout, PAGEROW + 3, PAGECOL + 22, 5);
533	PUTRATE(v_swtch, GENSTATROW + 1, GENSTATCOL, 5);
534	PUTRATE(v_trap, GENSTATROW + 1, GENSTATCOL + 5, 5);
535	PUTRATE(v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 5);
536	PUTRATE(v_intr, GENSTATROW + 1, GENSTATCOL + 15, 5);
537	PUTRATE(v_soft, GENSTATROW + 1, GENSTATCOL + 20, 5);
538	PUTRATE(v_vm_faults, GENSTATROW + 1, GENSTATCOL + 25, 5);
539	mvprintw(DISKROW, DISKCOL + 5, "                              ");
540	for (i = 0, lc = 0; i < num_devices && lc < MAXDRIVES; i++)
541		if (dev_select[i].selected) {
542			char tmpstr[80];
543			sprintf(tmpstr, "%s%d", dev_select[i].device_name,
544				dev_select[i].unit_number);
545			mvprintw(DISKROW, DISKCOL + 5 + 6 * lc,
546				" %5.5s", tmpstr);
547			switch(state) {
548			case TIME:
549				dinfo(i, ++lc, &cur, &last);
550				break;
551			case RUN:
552				dinfo(i, ++lc, &cur, &run);
553				break;
554			case BOOT:
555				dinfo(i, ++lc, &cur, NULL);
556				break;
557			}
558		}
559	putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
560	putint((nchtotal.ncs_goodhits + nchtotal.ncs_neghits),
561	   NAMEIROW + 2, NAMEICOL + 9, 9);
562#define nz(x)	((x) ? (x) : 1)
563	putfloat((nchtotal.ncs_goodhits+nchtotal.ncs_neghits) *
564	   100.0 / nz(s.nchcount),
565	   NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
566	putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
567	putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
568	   NAMEIROW + 2, NAMEICOL + 33, 4, 0, 1);
569#undef nz
570}
571
572int
573cmdkre(cmd, args)
574	const char *cmd, *args;
575{
576	int retval;
577
578	if (prefix(cmd, "run")) {
579		retval = 1;
580		copyinfo(&s2, &s1);
581		switch (devstat_getdevs(NULL, &run)) {
582		case -1:
583			errx(1, "%s", devstat_errbuf);
584			break;
585		case 1:
586			num_devices = run.dinfo->numdevs;
587			generation = run.dinfo->generation;
588			retval = dscmd("refresh", NULL, MAXDRIVES, &cur);
589			if (retval == 2)
590				labelkre();
591			break;
592		default:
593			break;
594		}
595		state = RUN;
596		return (retval);
597	}
598	if (prefix(cmd, "boot")) {
599		state = BOOT;
600		copyinfo(&z, &s1);
601		return (1);
602	}
603	if (prefix(cmd, "time")) {
604		state = TIME;
605		return (1);
606	}
607	if (prefix(cmd, "zero")) {
608		retval = 1;
609		if (state == RUN) {
610			getinfo(&s1);
611			switch (devstat_getdevs(NULL, &run)) {
612			case -1:
613				errx(1, "%s", devstat_errbuf);
614				break;
615			case 1:
616				num_devices = run.dinfo->numdevs;
617				generation = run.dinfo->generation;
618				retval = dscmd("refresh",NULL, MAXDRIVES, &cur);
619				if (retval == 2)
620					labelkre();
621				break;
622			default:
623				break;
624			}
625		}
626		return (retval);
627	}
628	retval = dscmd(cmd, args, MAXDRIVES, &cur);
629
630	if (retval == 2)
631		labelkre();
632
633	return(retval);
634}
635
636/* calculate number of users on the system */
637static int
638ucount()
639{
640	int nusers = 0;
641
642	if (ut < 0)
643		return (0);
644	while (read(ut, &utmp, sizeof(utmp)))
645		if (utmp.ut_name[0] != '\0')
646			nusers++;
647
648	lseek(ut, 0L, L_SET);
649	return (nusers);
650}
651
652static float
653cputime(indx)
654	int indx;
655{
656	double lt;
657	int i;
658
659	lt = 0;
660	for (i = 0; i < CPUSTATES; i++)
661		lt += s.time[i];
662	if (lt == 0.0)
663		lt = 1.0;
664	return (s.time[indx] * 100.0 / lt);
665}
666
667static void
668putint(n, l, lc, w)
669	int n, l, lc, w;
670{
671	char b[128];
672
673	move(l, lc);
674	if (n == 0) {
675		while (w-- > 0)
676			addch(' ');
677		return;
678	}
679	snprintf(b, sizeof(b), "%*d", w, n);
680	if ((int)strlen(b) > w) {
681		while (w-- > 0)
682			addch('*');
683		return;
684	}
685	addstr(b);
686}
687
688static void
689putfloat(f, l, lc, w, d, nz)
690	double f;
691	int l, lc, w, d, nz;
692{
693	char b[128];
694
695	move(l, lc);
696	if (nz && f == 0.0) {
697		while (--w >= 0)
698			addch(' ');
699		return;
700	}
701	snprintf(b, sizeof(b), "%*.*f", w, d, f);
702	if ((int)strlen(b) > w)
703		snprintf(b, sizeof(b), "%*.0f", w, f);
704	if ((int)strlen(b) > w) {
705		while (--w >= 0)
706			addch('*');
707		return;
708	}
709	addstr(b);
710}
711
712static void
713putlongdouble(f, l, lc, w, d, nz)
714	long double f;
715	int l, lc, w, d, nz;
716{
717	char b[128];
718
719	move(l, lc);
720	if (nz && f == 0.0) {
721		while (--w >= 0)
722			addch(' ');
723		return;
724	}
725	sprintf(b, "%*.*Lf", w, d, f);
726	if ((int)strlen(b) > w)
727		sprintf(b, "%*.0Lf", w, f);
728	if ((int)strlen(b) > w) {
729		while (--w >= 0)
730			addch('*');
731		return;
732	}
733	addstr(b);
734}
735
736static void
737getinfo(ls)
738	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.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_cow_faults", ls->v_cow_faults);
753	GETSYSCTL("vm.stats.vm.v_zfod", ls->v_zfod);
754	GETSYSCTL("vm.stats.vm.v_ozfod", ls->v_ozfod);
755	GETSYSCTL("vm.stats.vm.v_swapin", ls->v_swapin);
756	GETSYSCTL("vm.stats.vm.v_swapout", ls->v_swapout);
757	GETSYSCTL("vm.stats.vm.v_swappgsin", ls->v_swappgsin);
758	GETSYSCTL("vm.stats.vm.v_swappgsout", ls->v_swappgsout);
759	GETSYSCTL("vm.stats.vm.v_vnodein", ls->v_vnodein);
760	GETSYSCTL("vm.stats.vm.v_vnodeout", ls->v_vnodeout);
761	GETSYSCTL("vm.stats.vm.v_vnodepgsin", ls->v_vnodepgsin);
762	GETSYSCTL("vm.stats.vm.v_vnodepgsout", ls->v_vnodepgsout);
763	GETSYSCTL("vm.stats.vm.v_intrans", ls->v_intrans);
764	GETSYSCTL("vm.stats.vm.v_reactivated", ls->v_reactivated);
765	GETSYSCTL("vm.stats.vm.v_pdwakeups", ls->v_pdwakeups);
766	GETSYSCTL("vm.stats.vm.v_pdpages", ls->v_pdpages);
767	GETSYSCTL("vm.stats.vm.v_dfree", ls->v_dfree);
768	GETSYSCTL("vm.stats.vm.v_pfree", ls->v_pfree);
769	GETSYSCTL("vm.stats.vm.v_tfree", ls->v_tfree);
770	GETSYSCTL("vm.stats.vm.v_page_size", ls->v_page_size);
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_cache_count", ls->v_cache_count);
776	GETSYSCTL("vfs.bufspace", ls->bufspace);
777	GETSYSCTL("kern.maxvnodes", ls->desiredvnodes);
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("hw.intrcnt", ls->intrcnt, nintr * sizeof(u_long));
783
784	size = sizeof(ls->Total);
785	mib[0] = CTL_VM;
786	mib[1] = VM_TOTAL;
787	if (sysctl(mib, 2, &ls->Total, &size, NULL, 0) < 0) {
788		error("Can't get kernel info: %s\n", strerror(errno));
789		bzero(&ls->Total, sizeof(ls->Total));
790	}
791	size = sizeof(ncpu);
792	if (sysctlbyname("hw.ncpu", &ncpu, &size, NULL, 0) < 0 ||
793	    size != sizeof(ncpu))
794		ncpu = 1;
795
796	tmp_dinfo = last.dinfo;
797	last.dinfo = cur.dinfo;
798	cur.dinfo = tmp_dinfo;
799
800	last.busy_time = cur.busy_time;
801	switch (devstat_getdevs(NULL, &cur)) {
802	case -1:
803		errx(1, "%s", devstat_errbuf);
804		break;
805	case 1:
806		num_devices = cur.dinfo->numdevs;
807		generation = cur.dinfo->generation;
808		cmdkre("refresh", NULL);
809		break;
810	default:
811		break;
812	}
813}
814
815static void
816allocinfo(ls)
817	struct Info *ls;
818{
819
820	ls->intrcnt = (long *) calloc(nintr, sizeof(long));
821	if (ls->intrcnt == NULL)
822		errx(2, "out of memory");
823}
824
825static void
826copyinfo(from, to)
827	struct Info *from, *to;
828{
829	long *intrcnt;
830
831	/*
832	 * time, wds, seek, and xfer are malloc'd so we have to
833	 * save the pointers before the structure copy and then
834	 * copy by hand.
835	 */
836	intrcnt = to->intrcnt;
837	*to = *from;
838
839	bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
840}
841
842static void
843dinfo(dn, lc, now, then)
844	int dn, lc;
845	struct statinfo *now, *then;
846{
847	long double transfers_per_second;
848	long double kb_per_transfer, mb_per_second;
849	long double elapsed_time, device_busy;
850	int di;
851
852	di = dev_select[dn].position;
853
854	elapsed_time = devstat_compute_etime(now->busy_time,
855	    then ? then->busy_time : now->dinfo->devices[di].dev_creation_time);
856
857	device_busy =  devstat_compute_etime(now->dinfo->devices[di].busy_time,
858	    then ? then->dinfo->devices[di].busy_time :
859	    now->dinfo->devices[di].dev_creation_time);
860
861	if (devstat_compute_statistics(&now->dinfo->devices[di], then ?
862	    &then->dinfo->devices[di] : NULL, elapsed_time,
863	    DSM_KB_PER_TRANSFER, &kb_per_transfer, DSM_TRANSFERS_PER_SECOND,
864	    &transfers_per_second, DSM_MB_PER_SECOND, &mb_per_second,
865	    DSM_NONE) != 0)
866		errx(1, "%s", devstat_errbuf);
867
868	if ((device_busy == 0) && (transfers_per_second > 5))
869		/* the device has been 100% busy, fake it because
870		 * as long as the device is 100% busy the busy_time
871		 * field in the devstat struct is not updated */
872		device_busy = elapsed_time;
873	if (device_busy > elapsed_time)
874		/* this normally happens after one or more periods
875		 * where the device has been 100% busy, correct it */
876		device_busy = elapsed_time;
877
878	lc = DISKCOL + lc * 6;
879	putlongdouble(kb_per_transfer, DISKROW + 1, lc, 5, 2, 0);
880	putlongdouble(transfers_per_second, DISKROW + 2, lc, 5, 0, 0);
881	putlongdouble(mb_per_second, DISKROW + 3, lc, 5, 2, 0);
882	putlongdouble(device_busy * 100 / elapsed_time, DISKROW + 4, lc, 5, 0, 0);
883}
884