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