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