xref: /freebsd/usr.bin/systat/vmstat.c (revision 0de89efe5c443f213c7ea28773ef2dc6cf3af2ed)
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.22 1997/08/13 06:45:11 charnier Exp $";
40 #endif /* not lint */
41 
42 /*
43  * Cursed vmstat -- from Robert Elz.
44  */
45 
46 #include <sys/param.h>
47 #include <sys/dkstat.h>
48 #include <sys/buf.h>
49 #include <sys/stat.h>
50 #include <sys/time.h>
51 #include <sys/proc.h>
52 #include <sys/uio.h>
53 #include <sys/namei.h>
54 #include <sys/sysctl.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 "systat.h"
70 #include "extern.h"
71 
72 static struct Info {
73 	long	time[CPUSTATES];
74 	struct	vmmeter Cnt;
75 	struct	vmtotal Total;
76 	long	*dk_time;
77 	long	*dk_wds;
78 	long	*dk_seek;
79 	long	*dk_xfer;
80 	int	dk_busy;
81 	struct	nchstats nchstats;
82 	long	nchcount;
83 	long	*intrcnt;
84 	int	bufspace;
85 	int	desiredvnodes;
86 	int	numvnodes;
87 	int	freevnodes;
88 } s, s1, s2, z;
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 
96 static	enum state { BOOT, TIME, RUN } state = TIME;
97 
98 static void allocinfo __P((struct Info *));
99 static void copyinfo __P((struct Info *, struct Info *));
100 static float cputime __P((int));
101 static void dinfo __P((int, int));
102 static void getinfo __P((struct Info *, enum state));
103 static void putint __P((int, int, int, int));
104 static void putfloat __P((double, int, int, int, int, int));
105 static int ucount __P((void));
106 
107 static	int ut;
108 static	char buf[26];
109 static	time_t t;
110 static	double etime;
111 static	int nintr;
112 static	long *intrloc;
113 static	char **intrname;
114 static	int nextintsrow;
115 
116 struct	utmp utmp;
117 
118 
119 WINDOW *
120 openkre()
121 {
122 
123 	ut = open(_PATH_UTMP, O_RDONLY);
124 	if (ut < 0)
125 		error("No utmp");
126 	return (stdscr);
127 }
128 
129 void
130 closekre(w)
131 	WINDOW *w;
132 {
133 
134 	(void) close(ut);
135 	if (w == NULL)
136 		return;
137 	wclear(w);
138 	wrefresh(w);
139 }
140 
141 
142 static struct nlist namelist[] = {
143 #define X_CPTIME	0
144 	{ "_cp_time" },
145 #define X_CNT		1
146 	{ "_cnt" },
147 #define	X_BUFFERSPACE	2
148 	{ "_bufspace" },
149 #define	X_DK_BUSY	3
150 	{ "_dk_busy" },
151 #define	X_DK_TIME	4
152 	{ "_dk_time" },
153 #define	X_DK_XFER	5
154 	{ "_dk_xfer" },
155 #define	X_DK_WDS	6
156 	{ "_dk_wds" },
157 #define	X_DK_SEEK	7
158 	{ "_dk_seek" },
159 #define	X_NCHSTATS	8
160 	{ "_nchstats" },
161 #define	X_INTRNAMES	9
162 	{ "_intrnames" },
163 #define	X_EINTRNAMES	10
164 	{ "_eintrnames" },
165 #define	X_INTRCNT	11
166 	{ "_intrcnt" },
167 #define	X_EINTRCNT	12
168 	{ "_eintrcnt" },
169 #define	X_DESIREDVNODES	13
170 	{ "_desiredvnodes" },
171 #define	X_NUMVNODES	14
172 	{ "_numvnodes" },
173 #define	X_FREEVNODES	15
174 	{ "_freevnodes" },
175 	{ "" },
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	 9	/* max # for space */
203 
204 #if DK_NDRIVE > DRIVESPACE
205 #define	MAXDRIVES	DRIVESPACE	 /* max # to display */
206 #else
207 #define	MAXDRIVES	DK_NDRIVE	 /* max # to display */
208 #endif
209 
210 int
211 initkre()
212 {
213 	char *intrnamebuf, *cp;
214 	int i;
215 	static int once = 0;
216 
217 	if (namelist[0].n_type == 0) {
218 		if (kvm_nlist(kd, namelist)) {
219 			nlisterr(namelist);
220 			return(0);
221 		}
222 		if (namelist[0].n_type == 0) {
223 			error("No namelist");
224 			return(0);
225 		}
226 	}
227 	if (! dkinit())
228 		return(0);
229 	if (dk_ndrive && !once) {
230 #define	allocate(e, t) \
231     s./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
232     s1./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
233     s2./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
234     z./**/e = (t *)calloc(dk_ndrive, sizeof (t));
235 		allocate(dk_time, long);
236 		allocate(dk_wds, long);
237 		allocate(dk_seek, long);
238 		allocate(dk_xfer, long);
239 		once = 1;
240 #undef allocate
241 	}
242 	if (nintr == 0) {
243 		nintr = (namelist[X_EINTRCNT].n_value -
244 			namelist[X_INTRCNT].n_value) / sizeof (long);
245 		intrloc = calloc(nintr, sizeof (long));
246 		intrname = calloc(nintr, sizeof (long));
247 		intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value -
248 			namelist[X_INTRNAMES].n_value);
249 		if (intrnamebuf == 0 || intrname == 0 || intrloc == 0) {
250 			error("Out of memory\n");
251 			if (intrnamebuf)
252 				free(intrnamebuf);
253 			if (intrname)
254 				free(intrname);
255 			if (intrloc)
256 				free(intrloc);
257 			nintr = 0;
258 			return(0);
259 		}
260 		NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) -
261 			NVAL(X_INTRNAMES));
262 		for (cp = intrnamebuf, i = 0; i < nintr; i++) {
263 			intrname[i] = cp;
264 			cp += strlen(cp) + 1;
265 		}
266 		nextintsrow = INTSROW + 2;
267 		allocinfo(&s);
268 		allocinfo(&s1);
269 		allocinfo(&s2);
270 		allocinfo(&z);
271 	}
272 	getinfo(&s2, RUN);
273 	copyinfo(&s2, &s1);
274 	return(1);
275 }
276 
277 void
278 fetchkre()
279 {
280 	time_t now;
281 	struct tm *tp;
282 
283 	time(&now);
284 	tp = localtime(&now);
285 	(void) strftime(buf, sizeof(buf), "%c", tp);
286 	buf[16] = '\0';
287 	getinfo(&s, state);
288 }
289 
290 void
291 labelkre()
292 {
293 	register int i, j;
294 
295 	clear();
296 	mvprintw(STATROW, STATCOL + 4, "users    Load");
297 	mvprintw(MEMROW, MEMCOL, "Mem:KB    REAL            VIRTUAL");
298 	mvprintw(MEMROW + 1, MEMCOL, "        Tot   Share      Tot    Share");
299 	mvprintw(MEMROW + 2, MEMCOL, "Act");
300 	mvprintw(MEMROW + 3, MEMCOL, "All");
301 
302 	mvprintw(MEMROW + 1, MEMCOL + 41, "Free");
303 
304 	mvprintw(PAGEROW, PAGECOL,     "        VN PAGER  SWAP PAGER ");
305 	mvprintw(PAGEROW + 1, PAGECOL, "        in  out     in  out ");
306 	mvprintw(PAGEROW + 2, PAGECOL, "count");
307 	mvprintw(PAGEROW + 3, PAGECOL, "pages");
308 
309 	mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
310 	mvprintw(INTSROW + 1, INTSCOL + 9, "total");
311 
312 	mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "cow");
313 	mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "zfod");
314 	mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "wire");
315 	mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "act");
316 	mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "inact");
317 	mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "cache");
318 	mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "free");
319 	mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "daefr");
320 	mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "prcfr");
321 	mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "react");
322 	mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "pdwake");
323 	mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "pdpgs");
324 	mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "intrn");
325 	mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "buf");
326 
327 	mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "desiredvnodes");
328 	mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "numvnodes");
329 	mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "freevnodes");
330 
331 	mvprintw(GENSTATROW, GENSTATCOL, "  Csw  Trp  Sys  Int  Sof  Flt");
332 
333 	mvprintw(GRAPHROW, GRAPHCOL,
334 		"  . %%Sys    . %%Intr   . %%User   . %%Nice   . %%Idle");
335 	mvprintw(PROCSROW, PROCSCOL, "Proc:r  p  d  s  w");
336 	mvprintw(GRAPHROW + 1, GRAPHCOL,
337 		"|    |    |    |    |    |    |    |    |    |    |");
338 
339 	mvprintw(NAMEIROW, NAMEICOL, "Namei         Name-cache    Dir-cache");
340 	mvprintw(NAMEIROW + 1, NAMEICOL,
341 		"    Calls     hits    %%     hits    %%");
342 	mvprintw(DISKROW, DISKCOL, "Discs");
343 	mvprintw(DISKROW + 1, DISKCOL, "seeks");
344 	mvprintw(DISKROW + 2, DISKCOL, "xfers");
345 	mvprintw(DISKROW + 3, DISKCOL, " blks");
346 	mvprintw(DISKROW + 4, DISKCOL, " msps");
347 	j = 0;
348 	for (i = 0; i < dk_ndrive && j < MAXDRIVES; i++)
349 		if (dk_select[i]) {
350 			mvprintw(DISKROW, DISKCOL + 5 + 5 * j,
351 				" %4.4s", dr_name[j]);
352 			j++;
353 		}
354 	for (i = 0; i < nintr; i++) {
355 		if (intrloc[i] == 0)
356 			continue;
357 		mvprintw(intrloc[i], INTSCOL + 9, "%-10.10s", intrname[i]);
358 	}
359 }
360 
361 #define X(fld)	{t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
362 #define Y(fld)	{t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
363 #define Z(fld)	{t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
364 	if(state == TIME) s1.nchstats.fld = t;}
365 #define PUTRATE(fld, l, c, w) \
366 	Y(fld); \
367 	putint((int)((float)s.fld/etime + 0.5), l, c, w)
368 #define MAXFAIL 5
369 
370 static	char cpuchar[CPUSTATES] = { '=' , '+', '>', '-', ' ' };
371 static	char cpuorder[CPUSTATES] = { CP_SYS, CP_INTR, CP_USER, CP_NICE,
372 				     CP_IDLE };
373 
374 void
375 showkre()
376 {
377 	float f1, f2;
378 	int psiz, inttotal;
379 	int i, l, c;
380 	static int failcnt = 0;
381 
382 	for (i = 0; i < dk_ndrive; i++) {
383 		X(dk_xfer); X(dk_seek); X(dk_wds); X(dk_time);
384 	}
385 	etime = 0;
386 	for(i = 0; i < CPUSTATES; i++) {
387 		X(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 	inttotal = 0;
406 	for (i = 0; i < nintr; i++) {
407 		if (s.intrcnt[i] == 0)
408 			continue;
409 		if (intrloc[i] == 0) {
410 			if (nextintsrow == LINES)
411 				continue;
412 			intrloc[i] = nextintsrow++;
413 			mvprintw(intrloc[i], INTSCOL + 9, "%-10.10s",
414 				intrname[i]);
415 		}
416 		X(intrcnt);
417 		l = (int)((float)s.intrcnt[i]/etime + 0.5);
418 		inttotal += l;
419 		putint(l, intrloc[i], INTSCOL + 2, 6);
420 	}
421 	putint(inttotal, INTSROW + 1, INTSCOL + 2, 6);
422 	Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
423 	Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes); Z(ncs_neghits);
424 	s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
425 	    nchtotal.ncs_miss + nchtotal.ncs_long + nchtotal.ncs_neghits;
426 	if (state == TIME)
427 		s1.nchcount = s.nchcount;
428 
429 	psiz = 0;
430 	f2 = 0.0;
431 	for (c = 0; c < CPUSTATES; c++) {
432 		i = cpuorder[c];
433 		f1 = cputime(i);
434 		f2 += f1;
435 		l = (int) ((f2 + 1.0) / 2.0) - psiz;
436 		if (f1 > 99.9)
437 			f1 = 99.9;	/* no room to display 100.0 */
438 		putfloat(f1, GRAPHROW, GRAPHCOL + 10 * c, 4, 1, 0);
439 		move(GRAPHROW + 2, psiz);
440 		psiz += l;
441 		while (l-- > 0)
442 			addch(cpuchar[c]);
443 	}
444 
445 	putint(ucount(), STATROW, STATCOL, 3);
446 	putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
447 	putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
448 	putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
449 	mvaddstr(STATROW, STATCOL + 53, buf);
450 #define pgtokb(pg)	((pg) * cnt.v_page_size / 1024)
451 	putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 3, 8);
452 	putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 11, 8);
453 	putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 19, 9);
454 	putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 28, 9);
455 	putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 3, 8);
456 	putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 11, 8);
457 	putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 19, 9);
458 	putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 28, 9);
459 	putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 37, 8);
460 	putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
461 	putint(total.t_pw, PROCSROW + 1, PROCSCOL + 6, 3);
462 	putint(total.t_dw, PROCSROW + 1, PROCSCOL + 9, 3);
463 	putint(total.t_sl, PROCSROW + 1, PROCSCOL + 12, 3);
464 	putint(total.t_sw, PROCSROW + 1, PROCSCOL + 15, 3);
465 	PUTRATE(Cnt.v_cow_faults, VMSTATROW + 0, VMSTATCOL + 3, 6);
466 	PUTRATE(Cnt.v_zfod, VMSTATROW + 1, VMSTATCOL + 4, 5);
467 	putint(pgtokb(cnt.v_wire_count), VMSTATROW + 2, VMSTATCOL, 9);
468 	putint(pgtokb(cnt.v_active_count), VMSTATROW + 3, VMSTATCOL, 9);
469 	putint(pgtokb(cnt.v_inactive_count), VMSTATROW + 4, VMSTATCOL, 9);
470 	putint(pgtokb(cnt.v_cache_count), VMSTATROW + 5, VMSTATCOL, 9);
471 	putint(pgtokb(cnt.v_free_count), VMSTATROW + 6, VMSTATCOL, 9);
472 	PUTRATE(Cnt.v_dfree, VMSTATROW + 7, VMSTATCOL, 9);
473 	PUTRATE(Cnt.v_pfree, VMSTATROW + 8, VMSTATCOL, 9);
474 	PUTRATE(Cnt.v_reactivated, VMSTATROW + 9, VMSTATCOL, 9);
475 	PUTRATE(Cnt.v_pdwakeups, VMSTATROW + 10, VMSTATCOL, 9);
476 	PUTRATE(Cnt.v_pdpages, VMSTATROW + 11, VMSTATCOL, 9);
477 	PUTRATE(Cnt.v_intrans, VMSTATROW + 12, VMSTATCOL, 9);
478 	putint(s.bufspace/1024, VMSTATROW + 13, VMSTATCOL, 9);
479 	putint(s.desiredvnodes, VMSTATROW + 14, VMSTATCOL, 9);
480 	putint(s.numvnodes, VMSTATROW + 15, VMSTATCOL, 9);
481 	putint(s.freevnodes, VMSTATROW + 16, VMSTATCOL, 9);
482 	PUTRATE(Cnt.v_vnodein, PAGEROW + 2, PAGECOL + 5, 5);
483 	PUTRATE(Cnt.v_vnodeout, PAGEROW + 2, PAGECOL + 10, 5);
484 	PUTRATE(Cnt.v_swapin, PAGEROW + 2, PAGECOL + 17, 5);
485 	PUTRATE(Cnt.v_swapout, PAGEROW + 2, PAGECOL + 22, 5);
486 	PUTRATE(Cnt.v_vnodepgsin, PAGEROW + 3, PAGECOL + 5, 5);
487 	PUTRATE(Cnt.v_vnodepgsout, PAGEROW + 3, PAGECOL + 10, 5);
488 	PUTRATE(Cnt.v_swappgsin, PAGEROW + 3, PAGECOL + 17, 5);
489 	PUTRATE(Cnt.v_swappgsout, PAGEROW + 3, PAGECOL + 22, 5);
490 	PUTRATE(Cnt.v_swtch, GENSTATROW + 1, GENSTATCOL, 5);
491 	PUTRATE(Cnt.v_trap, GENSTATROW + 1, GENSTATCOL + 5, 5);
492 	PUTRATE(Cnt.v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 5);
493 	PUTRATE(Cnt.v_intr, GENSTATROW + 1, GENSTATCOL + 15, 5);
494 	PUTRATE(Cnt.v_soft, GENSTATROW + 1, GENSTATCOL + 20, 5);
495 	PUTRATE(Cnt.v_vm_faults, GENSTATROW + 1, GENSTATCOL + 25, 5);
496 	mvprintw(DISKROW, DISKCOL + 5, "                              ");
497 	for (i = 0, c = 0; i < dk_ndrive && c < MAXDRIVES; i++)
498 		if (dk_select[i]) {
499 			mvprintw(DISKROW, DISKCOL + 5 + 5 * c,
500 				" %4.4s", dr_name[i]);
501 			dinfo(i, ++c);
502 		}
503 	putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
504 	putint((nchtotal.ncs_goodhits + nchtotal.ncs_neghits),
505 	   NAMEIROW + 2, NAMEICOL + 9, 9);
506 #define nz(x)	((x) ? (x) : 1)
507 	putfloat((nchtotal.ncs_goodhits+nchtotal.ncs_neghits) *
508 	   100.0 / nz(s.nchcount),
509 	   NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
510 	putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
511 	putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
512 	   NAMEIROW + 2, NAMEICOL + 33, 4, 0, 1);
513 #undef nz
514 }
515 
516 int
517 cmdkre(cmd, args)
518 	char *cmd, *args;
519 {
520 
521 	if (prefix(cmd, "run")) {
522 		copyinfo(&s2, &s1);
523 		state = RUN;
524 		return (1);
525 	}
526 	if (prefix(cmd, "boot")) {
527 		state = BOOT;
528 		copyinfo(&z, &s1);
529 		return (1);
530 	}
531 	if (prefix(cmd, "time")) {
532 		state = TIME;
533 		return (1);
534 	}
535 	if (prefix(cmd, "zero")) {
536 		if (state == RUN)
537 			getinfo(&s1, RUN);
538 		return (1);
539 	}
540 	return (dkcmd(cmd, args));
541 }
542 
543 /* calculate number of users on the system */
544 static int
545 ucount()
546 {
547 	register int nusers = 0;
548 
549 	if (ut < 0)
550 		return (0);
551 	while (read(ut, &utmp, sizeof(utmp)))
552 		if (utmp.ut_name[0] != '\0')
553 			nusers++;
554 
555 	lseek(ut, 0L, L_SET);
556 	return (nusers);
557 }
558 
559 static float
560 cputime(indx)
561 	int indx;
562 {
563 	double t;
564 	register int i;
565 
566 	t = 0;
567 	for (i = 0; i < CPUSTATES; i++)
568 		t += s.time[i];
569 	if (t == 0.0)
570 		t = 1.0;
571 	return (s.time[indx] * 100.0 / t);
572 }
573 
574 static void
575 putint(n, l, c, w)
576 	int n, l, c, w;
577 {
578 	char b[128];
579 
580 	move(l, c);
581 	if (n == 0) {
582 		while (w-- > 0)
583 			addch(' ');
584 		return;
585 	}
586 	sprintf(b, "%*d", w, n);
587 	if (strlen(b) > w) {
588 		while (w-- > 0)
589 			addch('*');
590 		return;
591 	}
592 	addstr(b);
593 }
594 
595 static void
596 putfloat(f, l, c, w, d, nz)
597 	double f;
598 	int l, c, w, d, nz;
599 {
600 	char b[128];
601 
602 	move(l, c);
603 	if (nz && f == 0.0) {
604 		while (--w >= 0)
605 			addch(' ');
606 		return;
607 	}
608 	sprintf(b, "%*.*f", w, d, f);
609 	if (strlen(b) > w) {
610 		while (--w >= 0)
611 			addch('*');
612 		return;
613 	}
614 	addstr(b);
615 }
616 
617 static void
618 getinfo(s, st)
619 	struct Info *s;
620 	enum state st;
621 {
622 	int mib[2], size;
623 	extern int errno;
624 
625 	NREAD(X_CPTIME, s->time, sizeof s->time);
626 	NREAD(X_CNT, &s->Cnt, sizeof s->Cnt);
627 	NREAD(X_BUFFERSPACE, &s->bufspace, LONG);
628 	NREAD(X_DESIREDVNODES, &s->desiredvnodes, LONG);
629 	NREAD(X_NUMVNODES, &s->numvnodes, LONG);
630 	NREAD(X_FREEVNODES, &s->freevnodes, LONG);
631 	NREAD(X_DK_BUSY, &s->dk_busy, LONG);
632 	NREAD(X_DK_TIME, s->dk_time, dk_ndrive * LONG);
633 	NREAD(X_DK_XFER, s->dk_xfer, dk_ndrive * LONG);
634 	NREAD(X_DK_WDS, s->dk_wds, dk_ndrive * LONG);
635 	NREAD(X_DK_SEEK, s->dk_seek, dk_ndrive * LONG);
636 	NREAD(X_NCHSTATS, &s->nchstats, sizeof s->nchstats);
637 	NREAD(X_INTRCNT, s->intrcnt, nintr * LONG);
638 	size = sizeof(s->Total);
639 	mib[0] = CTL_VM;
640 	mib[1] = VM_METER;
641 	if (sysctl(mib, 2, &s->Total, &size, NULL, 0) < 0) {
642 		error("Can't get kernel info: %s\n", strerror(errno));
643 		bzero(&s->Total, sizeof(s->Total));
644 	}
645 }
646 
647 static void
648 allocinfo(s)
649 	struct Info *s;
650 {
651 
652 	s->intrcnt = (long *) calloc(nintr, sizeof(long));
653 	if (s->intrcnt == NULL)
654 		errx(2, "out of memory");
655 }
656 
657 static void
658 copyinfo(from, to)
659 	register struct Info *from, *to;
660 {
661 	long *time, *wds, *seek, *xfer;
662 	long *intrcnt;
663 
664 	/*
665 	 * time, wds, seek, and xfer are malloc'd so we have to
666 	 * save the pointers before the structure copy and then
667 	 * copy by hand.
668 	 */
669 	time = to->dk_time; wds = to->dk_wds; seek = to->dk_seek;
670 	xfer = to->dk_xfer; intrcnt = to->intrcnt;
671 	*to = *from;
672 	bcopy(from->dk_time, to->dk_time = time, dk_ndrive * sizeof (long));
673 	bcopy(from->dk_wds, to->dk_wds = wds, dk_ndrive * sizeof (long));
674 	bcopy(from->dk_seek, to->dk_seek = seek, dk_ndrive * sizeof (long));
675 	bcopy(from->dk_xfer, to->dk_xfer = xfer, dk_ndrive * sizeof (long));
676 	bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
677 }
678 
679 static void
680 dinfo(dn, c)
681 	int dn, c;
682 {
683 	double words, atime, itime, xtime;
684 
685 	c = DISKCOL + c * 5;
686 	atime = s.dk_time[dn];
687 	atime /= hertz;
688 	words = s.dk_wds[dn]*32.0;	/* number of words transferred */
689 	xtime = dk_mspw[dn]*words;	/* transfer time */
690 	itime = atime - xtime;		/* time not transferring */
691 	if (xtime < 0)
692 		itime += xtime, xtime = 0;
693 	if (itime < 0)
694 		xtime += itime, itime = 0;
695 	putint((int)((float)s.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5);
696 	putint((int)((float)s.dk_xfer[dn]/etime+0.5), DISKROW + 2, c, 5);
697 	putint((int)(words/etime/512.0 + 0.5), DISKROW + 3, c, 5);
698 	if (s.dk_seek[dn])
699 		putfloat(itime*1000.0/s.dk_seek[dn], DISKROW + 4, c, 5, 1, 1);
700 	else
701 		putint(0, DISKROW + 4, c, 5);
702 }
703