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