xref: /original-bsd/usr.bin/systat/vmstat.c (revision 333da485)
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.2 (Berkeley) 01/12/94";
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 17 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 + 0, VMSTATCOL + 10, "cow");
269 	mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "objlk");
270 	mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "objht");
271 	mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "zfod");
272 	mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "nzfod");
273 	mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "%%zfod");
274 	mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "kern");
275 	mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "wire");
276 	mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "act");
277 	mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "inact");
278 	mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "free");
279 	mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "daefr");
280 	mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "prcfr");
281 	mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "react");
282 	mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "scan");
283 	mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "hdrev");
284 	if (LINES - 1 > VMSTATROW + 16)
285 		mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "intrn");
286 
287 	mvprintw(GENSTATROW, GENSTATCOL, "  Csw  Trp  Sys  Int  Sof  Flt");
288 
289 	mvprintw(GRAPHROW, GRAPHCOL,
290 		"    . %% Sys    . %% User    . %% Nice    . %% Idle");
291 	mvprintw(PROCSROW, PROCSCOL, "Proc:r  p  d  s  w");
292 	mvprintw(GRAPHROW + 1, GRAPHCOL,
293 		"|    |    |    |    |    |    |    |    |    |    |");
294 
295 	mvprintw(NAMEIROW, NAMEICOL, "Namei         Sys-cache     Proc-cache");
296 	mvprintw(NAMEIROW + 1, NAMEICOL,
297 		"    Calls     hits    %%     hits     %%");
298 	mvprintw(DISKROW, DISKCOL, "Discs");
299 	mvprintw(DISKROW + 1, DISKCOL, "seeks");
300 	mvprintw(DISKROW + 2, DISKCOL, "xfers");
301 	mvprintw(DISKROW + 3, DISKCOL, " blks");
302 	mvprintw(DISKROW + 4, DISKCOL, " msps");
303 	j = 0;
304 	for (i = 0; i < dk_ndrive && j < MAXDRIVES; i++)
305 		if (dk_select[i]) {
306 			mvprintw(DISKROW, DISKCOL + 5 + 5 * j,
307 				"  %3.3s", dr_name[j]);
308 			j++;
309 		}
310 	for (i = 0; i < nintr; i++) {
311 		if (intrloc[i] == 0)
312 			continue;
313 		mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", intrname[i]);
314 	}
315 }
316 
317 #define X(fld)	{t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
318 #define Y(fld)	{t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
319 #define Z(fld)	{t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
320 	if(state == TIME) s1.nchstats.fld = t;}
321 #define PUTRATE(fld, l, c, w) \
322 	Y(fld); \
323 	putint((int)((float)s.fld/etime + 0.5), l, c, w)
324 #define MAXFAIL 5
325 
326 static	char cpuchar[CPUSTATES] = { '=' , '>', '-', ' ' };
327 static	char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_IDLE };
328 
329 void
330 showkre()
331 {
332 	float f1, f2;
333 	int psiz, inttotal;
334 	int i, l, c;
335 	static int failcnt = 0;
336 
337 	for (i = 0; i < dk_ndrive; i++) {
338 		X(dk_xfer); X(dk_seek); X(dk_wds); X(dk_time);
339 	}
340 	etime = 0;
341 	for(i = 0; i < CPUSTATES; i++) {
342 		X(time);
343 		etime += s.time[i];
344 	}
345 	if (etime < 5.0) {	/* < 5 ticks - ignore this trash */
346 		if (failcnt++ >= MAXFAIL) {
347 			clear();
348 			mvprintw(2, 10, "The alternate system clock has died!");
349 			mvprintw(3, 10, "Reverting to ``pigs'' display.");
350 			move(CMDLINE, 0);
351 			refresh();
352 			failcnt = 0;
353 			sleep(5);
354 			command("pigs");
355 		}
356 		return;
357 	}
358 	failcnt = 0;
359 	etime /= hertz;
360 	inttotal = 0;
361 	for (i = 0; i < nintr; i++) {
362 		if (s.intrcnt[i] == 0)
363 			continue;
364 		if (intrloc[i] == 0) {
365 			if (nextintsrow == LINES)
366 				continue;
367 			intrloc[i] = nextintsrow++;
368 			mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s",
369 				intrname[i]);
370 		}
371 		X(intrcnt);
372 		l = (int)((float)s.intrcnt[i]/etime + 0.5);
373 		inttotal += l;
374 		putint(l, intrloc[i], INTSCOL, 8);
375 	}
376 	putint(inttotal, INTSROW + 1, INTSCOL, 8);
377 	Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
378 	Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes);
379 	s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
380 	    nchtotal.ncs_miss + nchtotal.ncs_long;
381 	if (state == TIME)
382 		s1.nchcount = s.nchcount;
383 
384 	psiz = 0;
385 	f2 = 0.0;
386 
387 	/*
388 	 * Last CPU state not calculated yet.
389 	 */
390 	for (c = 0; c < CPUSTATES - 1; c++) {
391 		i = cpuorder[c];
392 		f1 = cputime(i);
393 		f2 += f1;
394 		l = (int) ((f2 + 1.0) / 2.0) - psiz;
395 		if (c == 0)
396 			putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0);
397 		else
398 			putfloat(f1, GRAPHROW, GRAPHCOL + 12 * c,
399 				5, 1, 0);
400 		move(GRAPHROW + 2, psiz);
401 		psiz += l;
402 		while (l-- > 0)
403 			addch(cpuchar[c]);
404 	}
405 
406 	putint(ucount(), STATROW, STATCOL, 3);
407 	putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
408 	putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
409 	putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
410 	mvaddstr(STATROW, STATCOL + 53, buf);
411 #define pgtokb(pg)	((pg) * cnt.v_page_size / 1024)
412 	putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 3, 6);
413 	putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 9, 6);
414 	putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 15, 7);
415 	putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 22, 7);
416 	putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 3, 6);
417 	putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 9, 6);
418 	putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 15, 7);
419 	putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 22, 7);
420 	putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 29, 6);
421 	putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
422 	putint(total.t_pw, PROCSROW + 1, PROCSCOL + 6, 3);
423 	putint(total.t_dw, PROCSROW + 1, PROCSCOL + 9, 3);
424 	putint(total.t_sl, PROCSROW + 1, PROCSCOL + 12, 3);
425 	putint(total.t_sw, PROCSROW + 1, PROCSCOL + 15, 3);
426 	PUTRATE(Cnt.v_cow_faults, VMSTATROW + 0, VMSTATCOL + 3, 6);
427 	PUTRATE(Cnt.v_lookups, VMSTATROW + 1, VMSTATCOL + 3, 6);
428 	PUTRATE(Cnt.v_hits, VMSTATROW + 2, VMSTATCOL + 3, 6);
429 	PUTRATE(Cnt.v_zfod, VMSTATROW + 3, VMSTATCOL + 4, 5);
430 	PUTRATE(Cnt.v_nzfod, VMSTATROW + 4, VMSTATCOL + 3, 6);
431 	putfloat(cnt.v_nzfod == 0 ? 0.0 : (100.0 * cnt.v_zfod / cnt.v_nzfod),
432 		 VMSTATROW + 5, VMSTATCOL + 2, 7, 2, 1);
433 	putint(pgtokb(cnt.v_kernel_pages), VMSTATROW + 6, VMSTATCOL, 9);
434 	putint(pgtokb(cnt.v_wire_count), VMSTATROW + 7, VMSTATCOL, 9);
435 	putint(pgtokb(cnt.v_active_count), VMSTATROW + 8, VMSTATCOL, 9);
436 	putint(pgtokb(cnt.v_inactive_count), VMSTATROW + 9, VMSTATCOL, 9);
437 	putint(pgtokb(cnt.v_free_count), VMSTATROW + 10, VMSTATCOL, 9);
438 	PUTRATE(Cnt.v_dfree, VMSTATROW + 11, VMSTATCOL, 9);
439 	PUTRATE(Cnt.v_pfree, VMSTATROW + 12, VMSTATCOL, 9);
440 	PUTRATE(Cnt.v_reactivated, VMSTATROW + 13, VMSTATCOL, 9);
441 	PUTRATE(Cnt.v_scan, VMSTATROW + 14, VMSTATCOL, 9);
442 	PUTRATE(Cnt.v_rev, VMSTATROW + 15, VMSTATCOL, 9);
443 	if (LINES - 1 > VMSTATROW + 16)
444 		PUTRATE(Cnt.v_intrans, VMSTATROW + 16, VMSTATCOL, 9);
445 	PUTRATE(Cnt.v_pageins, PAGEROW + 2, PAGECOL + 5, 5);
446 	PUTRATE(Cnt.v_pageouts, PAGEROW + 2, PAGECOL + 10, 5);
447 	PUTRATE(Cnt.v_swpin, PAGEROW + 2, PAGECOL + 15, 5);	/* - */
448 	PUTRATE(Cnt.v_swpout, PAGEROW + 2, PAGECOL + 20, 5);	/* - */
449 	PUTRATE(Cnt.v_pgpgin, PAGEROW + 3, PAGECOL + 5, 5);	/* ? */
450 	PUTRATE(Cnt.v_pgpgout, PAGEROW + 3, PAGECOL + 10, 5);	/* ? */
451 	PUTRATE(Cnt.v_pswpin, PAGEROW + 3, PAGECOL + 15, 5);	/* - */
452 	PUTRATE(Cnt.v_pswpout, PAGEROW + 3, PAGECOL + 20, 5);	/* - */
453 	PUTRATE(Cnt.v_swtch, GENSTATROW + 1, GENSTATCOL, 5);
454 	PUTRATE(Cnt.v_trap, GENSTATROW + 1, GENSTATCOL + 5, 5);
455 	PUTRATE(Cnt.v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 5);
456 	PUTRATE(Cnt.v_intr, GENSTATROW + 1, GENSTATCOL + 15, 5);
457 	PUTRATE(Cnt.v_soft, GENSTATROW + 1, GENSTATCOL + 20, 5);
458 	PUTRATE(Cnt.v_faults, GENSTATROW + 1, GENSTATCOL + 25, 5);
459 	mvprintw(DISKROW, DISKCOL + 5, "                              ");
460 	for (i = 0, c = 0; i < dk_ndrive && c < MAXDRIVES; i++)
461 		if (dk_select[i]) {
462 			mvprintw(DISKROW, DISKCOL + 5 + 5 * c,
463 				"  %3.3s", dr_name[i]);
464 			dinfo(i, ++c);
465 		}
466 	putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
467 	putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 9, 9);
468 #define nz(x)	((x) ? (x) : 1)
469 	putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount),
470 	   NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
471 	putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
472 	putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
473 	   NAMEIROW + 2, NAMEICOL + 34, 4, 0, 1);
474 #undef nz
475 }
476 
477 int
478 cmdkre(cmd, args)
479 	char *cmd, *args;
480 {
481 
482 	if (prefix(cmd, "run")) {
483 		copyinfo(&s2, &s1);
484 		state = RUN;
485 		return (1);
486 	}
487 	if (prefix(cmd, "boot")) {
488 		state = BOOT;
489 		copyinfo(&z, &s1);
490 		return (1);
491 	}
492 	if (prefix(cmd, "time")) {
493 		state = TIME;
494 		return (1);
495 	}
496 	if (prefix(cmd, "zero")) {
497 		if (state == RUN)
498 			getinfo(&s1, RUN);
499 		return (1);
500 	}
501 	return (dkcmd(cmd, args));
502 }
503 
504 /* calculate number of users on the system */
505 static int
506 ucount()
507 {
508 	register int nusers = 0;
509 
510 	if (ut < 0)
511 		return (0);
512 	while (read(ut, &utmp, sizeof(utmp)))
513 		if (utmp.ut_name[0] != '\0')
514 			nusers++;
515 
516 	lseek(ut, 0L, L_SET);
517 	return (nusers);
518 }
519 
520 static float
521 cputime(indx)
522 	int indx;
523 {
524 	double t;
525 	register int i;
526 
527 	t = 0;
528 	for (i = 0; i < CPUSTATES; i++)
529 		t += s.time[i];
530 	if (t == 0.0)
531 		t = 1.0;
532 	return (s.time[indx] * 100.0 / t);
533 }
534 
535 static void
536 putint(n, l, c, w)
537 	int n, l, c, w;
538 {
539 	char b[128];
540 
541 	move(l, c);
542 	if (n == 0) {
543 		while (w-- > 0)
544 			addch(' ');
545 		return;
546 	}
547 	sprintf(b, "%*d", w, n);
548 	if (strlen(b) > w) {
549 		while (w-- > 0)
550 			addch('*');
551 		return;
552 	}
553 	addstr(b);
554 }
555 
556 static void
557 putfloat(f, l, c, w, d, nz)
558 	double f;
559 	int l, c, w, d, nz;
560 {
561 	char b[128];
562 
563 	move(l, c);
564 	if (nz && f == 0.0) {
565 		while (--w >= 0)
566 			addch(' ');
567 		return;
568 	}
569 	sprintf(b, "%*.*f", w, d, f);
570 	if (strlen(b) > w) {
571 		while (--w >= 0)
572 			addch('*');
573 		return;
574 	}
575 	addstr(b);
576 }
577 
578 static void
579 getinfo(s, st)
580 	struct Info *s;
581 	enum state st;
582 {
583 	int mib[2], size;
584 	extern int errno;
585 
586 	NREAD(X_CPTIME, s->time, sizeof s->time);
587 	NREAD(X_CNT, &s->Cnt, sizeof s->Cnt);
588 	NREAD(X_DK_BUSY, &s->dk_busy, LONG);
589 	NREAD(X_DK_TIME, s->dk_time, dk_ndrive * LONG);
590 	NREAD(X_DK_XFER, s->dk_xfer, dk_ndrive * LONG);
591 	NREAD(X_DK_WDS, s->dk_wds, dk_ndrive * LONG);
592 	NREAD(X_DK_SEEK, s->dk_seek, dk_ndrive * LONG);
593 	NREAD(X_NCHSTATS, &s->nchstats, sizeof s->nchstats);
594 	NREAD(X_INTRCNT, s->intrcnt, nintr * LONG);
595 	size = sizeof(s->Total);
596 	mib[0] = CTL_VM;
597 	mib[1] = VM_METER;
598 	if (sysctl(mib, 2, &s->Total, &size, NULL, 0) < 0) {
599 		error("Can't get kernel info: %s\n", strerror(errno));
600 		bzero(&s->Total, sizeof(s->Total));
601 	}
602 }
603 
604 static void
605 allocinfo(s)
606 	struct Info *s;
607 {
608 
609 	s->intrcnt = (long *) malloc(nintr * sizeof(long));
610 	if (s->intrcnt == NULL) {
611 		fprintf(stderr, "systat: out of memory\n");
612 		exit(2);
613 	}
614 }
615 
616 static void
617 copyinfo(from, to)
618 	register struct Info *from, *to;
619 {
620 	long *time, *wds, *seek, *xfer;
621 	long *intrcnt;
622 
623 	/*
624 	 * time, wds, seek, and xfer are malloc'd so we have to
625 	 * save the pointers before the structure copy and then
626 	 * copy by hand.
627 	 */
628 	time = to->dk_time; wds = to->dk_wds; seek = to->dk_seek;
629 	xfer = to->dk_xfer; intrcnt = to->intrcnt;
630 	*to = *from;
631 	bcopy(from->dk_time, to->dk_time = time, dk_ndrive * sizeof (long));
632 	bcopy(from->dk_wds, to->dk_wds = wds, dk_ndrive * sizeof (long));
633 	bcopy(from->dk_seek, to->dk_seek = seek, dk_ndrive * sizeof (long));
634 	bcopy(from->dk_xfer, to->dk_xfer = xfer, dk_ndrive * sizeof (long));
635 	bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
636 }
637 
638 static void
639 dinfo(dn, c)
640 	int dn, c;
641 {
642 	double words, atime, itime, xtime;
643 
644 	c = DISKCOL + c * 5;
645 	atime = s.dk_time[dn];
646 	atime /= hertz;
647 	words = s.dk_wds[dn]*32.0;	/* number of words transferred */
648 	xtime = dk_mspw[dn]*words;	/* transfer time */
649 	itime = atime - xtime;		/* time not transferring */
650 	if (xtime < 0)
651 		itime += xtime, xtime = 0;
652 	if (itime < 0)
653 		xtime += itime, itime = 0;
654 	putint((int)((float)s.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5);
655 	putint((int)((float)s.dk_xfer[dn]/etime+0.5), DISKROW + 2, c, 5);
656 	putint((int)(words/etime/512.0 + 0.5), DISKROW + 3, c, 5);
657 	if (s.dk_seek[dn])
658 		putfloat(itime*1000.0/s.dk_seek[dn], DISKROW + 4, c, 5, 1, 1);
659 	else
660 		putint(0, DISKROW + 4, c, 5);
661 }
662