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