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