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