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