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