xref: /netbsd-src/usr.bin/systat/vmstat.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: vmstat.c,v 1.33 2000/07/05 11:03:23 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 1983, 1989, 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)vmstat.c	8.2 (Berkeley) 1/12/94";
40 #endif
41 __RCSID("$NetBSD: vmstat.c,v 1.33 2000/07/05 11:03:23 ad Exp $");
42 #endif /* not lint */
43 
44 /*
45  * Cursed vmstat -- from Robert Elz.
46  */
47 
48 #include <sys/param.h>
49 #include <sys/dkstat.h>
50 #include <sys/buf.h>
51 #include <sys/stat.h>
52 #include <sys/time.h>
53 #include <sys/user.h>
54 #include <sys/proc.h>
55 #include <sys/namei.h>
56 #include <sys/sched.h>
57 #include <sys/sysctl.h>
58 
59 #include <uvm/uvm_extern.h>
60 
61 #include <ctype.h>
62 #include <err.h>
63 #include <nlist.h>
64 #include <paths.h>
65 #include <signal.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <utmp.h>
69 #include <unistd.h>
70 
71 #include "systat.h"
72 #include "extern.h"
73 
74 static struct Info {
75 	u_int64_t time[CPUSTATES];
76 	struct	uvmexp uvmexp;
77 	struct	vmtotal Total;
78 	struct	nchstats nchstats;
79 	long	nchcount;
80 	long	*intrcnt;
81 } s, s1, s2, z;
82 
83 #include "dkstats.h"
84 extern struct _disk	cur;
85 
86 
87 #define	cnt s.Cnt
88 #define oldcnt s1.Cnt
89 #define	total s.Total
90 #define	nchtotal s.nchstats
91 #define	oldnchtotal s1.nchstats
92 
93 static	enum state { BOOT, TIME, RUN } state = TIME;
94 
95 static void allocinfo(struct Info *);
96 static void copyinfo(struct Info *, struct Info *);
97 static float cputime(int);
98 static void dinfo(int, int);
99 static void getinfo(struct Info *, enum state);
100 static void putint(int, int, int, int);
101 static void putfloat(double, int, int, int, int, int);
102 static int ucount(void);
103 
104 static	int ut;
105 static	char buf[26];
106 static	u_int64_t t;
107 static	double etime;
108 static	float hertz;
109 static	int nintr;
110 static	long *intrloc;
111 static	char **intrname;
112 static	int nextintsrow;
113 
114 struct	utmp utmp;
115 
116 WINDOW *
117 openvmstat(void)
118 {
119 
120 	ut = open(_PATH_UTMP, O_RDONLY);
121 	if (ut < 0)
122 		error("No utmp");
123 	return (stdscr);
124 }
125 
126 void
127 closevmstat(WINDOW *w)
128 {
129 
130 	(void) close(ut);
131 	if (w == NULL)
132 		return;
133 	wclear(w);
134 	wrefresh(w);
135 }
136 
137 
138 static struct nlist namelist[] = {
139 #define X_TOTAL		0
140 	{ "_total" },
141 #define	X_NCHSTATS	1
142 	{ "_nchstats" },
143 #define	X_INTRNAMES	2
144 	{ "_intrnames" },
145 #define	X_EINTRNAMES	3
146 	{ "_eintrnames" },
147 #define	X_INTRCNT	4
148 	{ "_intrcnt" },
149 #define	X_EINTRCNT	5
150 	{ "_eintrcnt" },
151 	{ "" },
152 };
153 
154 /*
155  * These constants define where the major pieces are laid out
156  */
157 #define STATROW		 0	/* uses 1 row and 68 cols */
158 #define STATCOL		 2
159 #define MEMROW		 2	/* uses 4 rows and 31 cols */
160 #define MEMCOL		 0
161 #define PAGEROW		 2	/* uses 4 rows and 26 cols */
162 #define PAGECOL		36
163 #define INTSROW		 2	/* uses all rows to bottom and 17 cols */
164 #define INTSCOL		63
165 #define PROCSROW	 7	/* uses 2 rows and 20 cols */
166 #define PROCSCOL	 0
167 #define GENSTATROW	 7	/* uses 2 rows and 30 cols */
168 #define GENSTATCOL	20
169 #define VMSTATROW	 7	/* uses 17 rows and 12 cols */
170 #define VMSTATCOL	48
171 #define GRAPHROW	10	/* uses 3 rows and 51 cols */
172 #define GRAPHCOL	 0
173 #define NAMEIROW	14	/* uses 3 rows and 38 cols */
174 #define NAMEICOL	 0
175 #define DISKROW		18	/* uses 5 rows and 50 cols (for 9 drives) */
176 #define DISKCOL		 0
177 
178 #define	DRIVESPACE	 9	/* max # for space */
179 
180 #if DK_NDRIVE > DRIVESPACE
181 #define	MAXDRIVES	DRIVESPACE	 /* max # to display */
182 #else
183 #define	MAXDRIVES	DK_NDRIVE	 /* max # to display */
184 #endif
185 
186 int
187 initvmstat(void)
188 {
189 	char *intrnamebuf, *cp;
190 	int i;
191 	static int once = 0;
192 	extern gid_t egid;
193 
194 	if (namelist[0].n_type == 0) {
195 		if (kvm_nlist(kd, namelist)) {
196 			nlisterr(namelist);
197 			return(0);
198 		}
199 		if (namelist[0].n_type == 0) {
200 			error("No namelist");
201 			return(0);
202 		}
203 	}
204 	hertz = stathz ? stathz : hz;
205 	if (! dkinit(1, egid))
206 		return(0);
207 	if (dk_ndrive && !once) {
208 #define	allocate(e, t) \
209 	s./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
210 	s1./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
211 	s2./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
212 	z./**/e = (t *)calloc(dk_ndrive, sizeof (t));
213 		once = 1;
214 #undef allocate
215 	}
216 	if (nintr == 0) {
217 		nintr = (namelist[X_EINTRCNT].n_value -
218 			namelist[X_INTRCNT].n_value) / sizeof (long);
219 		intrloc = calloc(nintr, sizeof (long));
220 		intrname = calloc(nintr, sizeof (long));
221 		intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value -
222 			namelist[X_INTRNAMES].n_value);
223 		if (intrnamebuf == NULL || intrname == 0 || intrloc == 0) {
224 			error("Out of memory\n");
225 			if (intrnamebuf)
226 				free(intrnamebuf);
227 			if (intrname)
228 				free(intrname);
229 			if (intrloc)
230 				free(intrloc);
231 			nintr = 0;
232 			return(0);
233 		}
234 		NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) -
235 			NVAL(X_INTRNAMES));
236 		for (cp = intrnamebuf, i = 0; i < nintr; i++) {
237 			intrname[i] = cp;
238 			cp += strlen(cp) + 1;
239 		}
240 		nextintsrow = INTSROW + 2;
241 		allocinfo(&s);
242 		allocinfo(&s1);
243 		allocinfo(&s2);
244 		allocinfo(&z);
245 	}
246 	getinfo(&s2, RUN);
247 	copyinfo(&s2, &s1);
248 	return(1);
249 }
250 
251 void
252 fetchvmstat(void)
253 {
254 	time_t now;
255 
256 	time(&now);
257 	strcpy(buf, ctime(&now));
258 	buf[19] = '\0';
259 	getinfo(&s, state);
260 }
261 
262 void
263 labelvmstat(void)
264 {
265 	int i, j;
266 
267 	clear();
268 	mvprintw(STATROW, STATCOL + 4, "users    Load");
269 	mvprintw(MEMROW, MEMCOL,     "          memory totals (in KB)");
270 	mvprintw(MEMROW + 1, MEMCOL, "         real   virtual    free");
271 	mvprintw(MEMROW + 2, MEMCOL, "Active");
272 	mvprintw(MEMROW + 3, MEMCOL, "All");
273 
274 	mvprintw(PAGEROW, PAGECOL, "        PAGING   SWAPPING ");
275 	mvprintw(PAGEROW + 1, PAGECOL, "        in  out   in  out ");
276 	mvprintw(PAGEROW + 2, PAGECOL, "ops");
277 	mvprintw(PAGEROW + 3, PAGECOL, "pages");
278 
279 	mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
280 	mvprintw(INTSROW + 1, INTSCOL + 9, "total");
281 
282 	mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "forks");
283 	mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "fkppw");
284 	mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "fksvm");
285 	mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "pwait");
286 	mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "relck");
287 	mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "rlkok");
288 	mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "noram");
289 	mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "ndcpy");
290 	mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "fltcp");
291 	mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "zfod");
292 	mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "cow");
293 	mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "fmin");
294 	mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "ftarg");
295 	mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "itarg");
296 	mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "wired");
297 	mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "pdfre");
298 	if (LINES - 1 > VMSTATROW + 16)
299 		mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "pdscn");
300 
301 	mvprintw(GENSTATROW, GENSTATCOL, "  Csw  Trp  Sys  Int  Sof  Flt");
302 
303 	mvprintw(GRAPHROW, GRAPHCOL,
304 		"    . %% Sy    . %% Us    . %% Ni    . %% In    . %% Id");
305 	mvprintw(PROCSROW, PROCSCOL, "Proc:r  d  s  w");
306 	mvprintw(GRAPHROW + 1, GRAPHCOL,
307 		"|    |    |    |    |    |    |    |    |    |    |");
308 
309 	mvprintw(NAMEIROW, NAMEICOL, "Namei         Sys-cache     Proc-cache");
310 	mvprintw(NAMEIROW + 1, NAMEICOL,
311 		"    Calls     hits    %%     hits     %%");
312 	mvprintw(DISKROW, DISKCOL, "Discs");
313 	mvprintw(DISKROW + 1, DISKCOL, "seeks");
314 	mvprintw(DISKROW + 2, DISKCOL, "xfers");
315 	mvprintw(DISKROW + 3, DISKCOL, "Kbyte");
316 	mvprintw(DISKROW + 4, DISKCOL, "%%busy");
317 	j = 0;
318 	for (i = 0; i < dk_ndrive && j < MAXDRIVES; i++)
319 		if (dk_select[i]) {
320 			mvprintw(DISKROW, DISKCOL + 5 + 5 * j,
321 				" %4.4s", dr_name[j]);
322 			j++;
323 		}
324 	for (i = 0; i < nintr; i++) {
325 		if (intrloc[i] == 0)
326 			continue;
327 		mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", intrname[i]);
328 	}
329 }
330 
331 #define X(fld)	{t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
332 #define Y(fld)	{t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
333 #define Z(fld)	{t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
334 	if(state == TIME) s1.nchstats.fld = t;}
335 #define PUTRATE(fld, l, c, w) {Y(fld); putint((int)((float)s.fld/etime + 0.5), l, c, w);}
336 #define MAXFAIL 5
337 
338 static	char cpuchar[CPUSTATES] = { '=' , '>', '-', '%', ' ' };
339 static	char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_INTR, CP_IDLE };
340 
341 void
342 showvmstat(void)
343 {
344 	float f1, f2;
345 	int psiz, inttotal;
346 	int i, l, c;
347 	static int failcnt = 0;
348 
349 	if (state == TIME)
350 		dkswap();
351 	etime = 0;
352 	for(i = 0; i < CPUSTATES; i++) {
353 		X(time);
354 		etime += s.time[i];
355 	}
356 	if (etime < 1.0) {	/* < 5 ticks - ignore this trash */
357 		if (failcnt++ >= MAXFAIL) {
358 			clear();
359 			mvprintw(2, 10, "The alternate system clock has died!");
360 			mvprintw(3, 10, "Reverting to ``pigs'' display.");
361 			move(CMDLINE, 0);
362 			refresh();
363 			failcnt = 0;
364 			sleep(5);
365 			command("pigs");
366 		}
367 		return;
368 	}
369 	failcnt = 0;
370 	etime /= hertz;
371 	inttotal = 0;
372 	for (i = 0; i < nintr; i++) {
373 		if (s.intrcnt[i] == 0)
374 			continue;
375 		if (intrloc[i] == 0) {
376 			if (nextintsrow == LINES)
377 				continue;
378 			intrloc[i] = nextintsrow++;
379 			mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s",
380 				intrname[i]);
381 		}
382 		X(intrcnt);
383 		l = (int)((float)s.intrcnt[i]/etime + 0.5);
384 		inttotal += l;
385 		putint(l, intrloc[i], INTSCOL, 8);
386 	}
387 	putint(inttotal, INTSROW + 1, INTSCOL, 8);
388 	Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
389 	Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes);
390 	s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
391 	    nchtotal.ncs_miss + nchtotal.ncs_long;
392 	if (state == TIME)
393 		s1.nchcount = s.nchcount;
394 
395 	psiz = 0;
396 	f2 = 0.0;
397 
398 	/*
399 	 * Last CPU state not calculated yet.
400 	 */
401 	for (c = 0; c < CPUSTATES; c++) {
402 		i = cpuorder[c];
403 		f1 = cputime(i);
404 		f2 += f1;
405 		l = (int) ((f2 + 1.0) / 2.0) - psiz;
406 		if (c == 0)
407 			putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0);
408 		else
409 			putfloat(f1, GRAPHROW, GRAPHCOL + 10 * c + 1, 5, 1, 0);
410 		mvhline(GRAPHROW + 2, psiz, cpuchar[c], l);
411 		psiz += l;
412 	}
413 
414 	putint(ucount(), STATROW, STATCOL, 3);
415 	putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
416 	putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
417 	putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
418 	mvaddstr(STATROW, STATCOL + 53, buf);
419 #define pgtokb(pg)	((pg) * (s.uvmexp.pagesize / 1024))
420 
421 	putint(pgtokb(s.uvmexp.active), MEMROW + 2, MEMCOL + 6, 7);
422 	putint(pgtokb(s.uvmexp.active + s.uvmexp.swpginuse),	/* XXX */
423 	    MEMROW + 2, MEMCOL + 16, 7);
424 	putint(pgtokb(s.uvmexp.npages - s.uvmexp.free), MEMROW + 3, MEMCOL + 6, 7);
425 	putint(pgtokb(s.uvmexp.npages - s.uvmexp.free + s.uvmexp.swpginuse),
426 	    MEMROW + 3, MEMCOL + 16, 7);
427 	putint(pgtokb(s.uvmexp.free), MEMROW + 2, MEMCOL + 24, 7);
428 	putint(pgtokb(s.uvmexp.free + s.uvmexp.swpages - s.uvmexp.swpginuse),
429 	    MEMROW + 3, MEMCOL + 24, 7);
430 	putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
431 	putint(total.t_dw, PROCSROW + 1, PROCSCOL + 6, 3);
432 	putint(total.t_sl, PROCSROW + 1, PROCSCOL + 9, 3);
433 	putint(total.t_sw, PROCSROW + 1, PROCSCOL + 12, 3);
434 	PUTRATE(uvmexp.forks, VMSTATROW + 0, VMSTATCOL + 3, 6);
435 	PUTRATE(uvmexp.forks_ppwait, VMSTATROW + 1, VMSTATCOL + 3, 6);
436 	PUTRATE(uvmexp.forks_sharevm, VMSTATROW + 2, VMSTATCOL + 3, 6);
437 	PUTRATE(uvmexp.fltpgwait, VMSTATROW + 3, VMSTATCOL + 4, 5);
438 	PUTRATE(uvmexp.fltrelck, VMSTATROW + 4, VMSTATCOL + 3, 6);
439 	PUTRATE(uvmexp.fltrelckok, VMSTATROW + 5, VMSTATCOL + 3, 6);
440 	PUTRATE(uvmexp.fltnoram, VMSTATROW + 6, VMSTATCOL + 3, 6);
441 	PUTRATE(uvmexp.fltamcopy, VMSTATROW + 7, VMSTATCOL + 3, 6);
442 	PUTRATE(uvmexp.flt_prcopy, VMSTATROW + 8, VMSTATCOL + 3, 6);
443 	PUTRATE(uvmexp.flt_przero, VMSTATROW + 9, VMSTATCOL + 3, 6);
444 	PUTRATE(uvmexp.flt_acow, VMSTATROW + 10, VMSTATCOL, 9);
445 	putint(s.uvmexp.freemin, VMSTATROW + 11, VMSTATCOL, 9);
446 	putint(s.uvmexp.freetarg, VMSTATROW + 12, VMSTATCOL, 9);
447 	putint(s.uvmexp.inactarg, VMSTATROW + 13, VMSTATCOL, 9);
448 	putint(s.uvmexp.wired, VMSTATROW + 14, VMSTATCOL, 9);
449 	PUTRATE(uvmexp.pdfreed, VMSTATROW + 15, VMSTATCOL, 9);
450 	if (LINES - 1 > VMSTATROW + 16)
451 		PUTRATE(uvmexp.pdscans, VMSTATROW + 16, VMSTATCOL, 9);
452 
453 	PUTRATE(uvmexp.pageins, PAGEROW + 2, PAGECOL + 5, 5);
454 	PUTRATE(uvmexp.pdpageouts, PAGEROW + 2, PAGECOL + 10, 5);
455 	PUTRATE(uvmexp.swapins, PAGEROW + 2, PAGECOL + 15, 5);
456 	PUTRATE(uvmexp.swapouts, PAGEROW + 2, PAGECOL + 20, 5);
457 	PUTRATE(uvmexp.pgswapin, PAGEROW + 3, PAGECOL + 5, 5);
458 	PUTRATE(uvmexp.pgswapout, PAGEROW + 3, PAGECOL + 10, 5);
459 
460 	PUTRATE(uvmexp.swtch, GENSTATROW + 1, GENSTATCOL, 5);
461 	PUTRATE(uvmexp.traps, GENSTATROW + 1, GENSTATCOL + 5, 5);
462 	PUTRATE(uvmexp.syscalls, GENSTATROW + 1, GENSTATCOL + 10, 5);
463 	PUTRATE(uvmexp.intrs, GENSTATROW + 1, GENSTATCOL + 15, 5);
464 	PUTRATE(uvmexp.softs, GENSTATROW + 1, GENSTATCOL + 20, 5);
465 	PUTRATE(uvmexp.faults, GENSTATROW + 1, GENSTATCOL + 25, 5);
466 	mvprintw(DISKROW, DISKCOL + 5, "                              ");
467 	for (i = 0, c = 0; i < dk_ndrive && c < MAXDRIVES; i++)
468 		if (dk_select[i]) {
469 			mvprintw(DISKROW, DISKCOL + 5 + 5 * c,
470 				" %4.4s", dr_name[i]);
471 			dinfo(i, ++c);
472 		}
473 	putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
474 	putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 9, 9);
475 #define nz(x)	((x) ? (x) : 1)
476 	putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount),
477 	   NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
478 	putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
479 	putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
480 	   NAMEIROW + 2, NAMEICOL + 34, 4, 0, 1);
481 #undef nz
482 }
483 
484 void
485 vmstat_boot(char *args)
486 {
487 	copyinfo(&z, &s1);
488 	state = BOOT;
489 }
490 
491 void
492 vmstat_run(char *args)
493 {
494 	copyinfo(&s1, &s2);
495 	state = RUN;
496 }
497 
498 void
499 vmstat_time (args)
500 	char * args;
501 {
502 	state = TIME;
503 }
504 
505 void
506 vmstat_zero(char *args)
507 {
508 	if (state == RUN)
509 		getinfo(&s1, RUN);
510 }
511 
512 /* calculate number of users on the system */
513 static int
514 ucount(void)
515 {
516 	int nusers = 0, onusers = -1;
517 
518 	if (ut < 0)
519 		return (0);
520 	while (read(ut, &utmp, sizeof(utmp)))
521 		if (utmp.ut_name[0] != '\0')
522 			nusers++;
523 
524 	if (nusers != onusers) {
525 		if (nusers == 1)
526 			mvprintw(STATROW, STATCOL + 8, " ");
527 		else
528 			mvprintw(STATROW, STATCOL + 8, "s");
529 	}
530 	lseek(ut, (off_t)0, SEEK_SET);
531 	onusers = nusers;
532 	return (nusers);
533 }
534 
535 static float
536 cputime(int indx)
537 {
538 	double t;
539 	int i;
540 
541 	t = 0;
542 	for (i = 0; i < CPUSTATES; i++)
543 		t += s.time[i];
544 	if (t == 0.0)
545 		t = 1.0;
546 	return (s.time[indx] * 100.0 / t);
547 }
548 
549 static void
550 putint(int n, int l, int c, int w)
551 {
552 	char b[128];
553 
554 	move(l, c);
555 	if (n == 0) {
556 		hline(' ', w);
557 		return;
558 	}
559 	(void)snprintf(b, sizeof b, "%*d", w, n);
560 	if (strlen(b) > w) {
561 		hline('*', w);
562 		return;
563 	}
564 	addstr(b);
565 }
566 
567 static void
568 putfloat(double f, int l, int c, int w, int d, int nz)
569 {
570 	char b[128];
571 
572 	move(l, c);
573 	if (nz && f == 0.0) {
574 		hline(' ', w);
575 		return;
576 	}
577 	(void)snprintf(b, sizeof b, "%*.*f", w, d, f);
578 	if (strlen(b) > w) {
579 		hline('*', w);
580 		return;
581 	}
582 	addstr(b);
583 }
584 
585 static void
586 getinfo(struct Info *s, enum state st)
587 {
588 	int mib[2];
589 	size_t size;
590 
591 	dkreadstats();
592 	(void) fetch_cptime(s->time);
593 	NREAD(X_NCHSTATS, &s->nchstats, sizeof s->nchstats);
594 	NREAD(X_INTRCNT, s->intrcnt, nintr * LONG);
595 	size = sizeof(s->uvmexp);
596 	mib[0] = CTL_VM;
597 	mib[1] = VM_UVMEXP;
598 	if (sysctl(mib, 2, &s->uvmexp, &size, NULL, 0) < 0) {
599 		error("can't get uvmexp: %s\n", strerror(errno));
600 		memset(&s->uvmexp, 0, sizeof(s->uvmexp));
601 	}
602 	size = sizeof(s->Total);
603 	mib[0] = CTL_VM;
604 	mib[1] = VM_METER;
605 	if (sysctl(mib, 2, &s->Total, &size, NULL, 0) < 0) {
606 		error("Can't get kernel info: %s\n", strerror(errno));
607 		memset(&s->Total, 0, sizeof(s->Total));
608 	}
609 }
610 
611 static void
612 allocinfo(struct Info *s)
613 {
614 
615 	if ((s->intrcnt = malloc(nintr * sizeof(long))) == NULL) {
616 		error("malloc failed");
617 		die(0);
618 	}
619 }
620 
621 static void
622 copyinfo(struct Info *from, struct Info *to)
623 {
624 	long *intrcnt;
625 
626 	intrcnt = to->intrcnt;
627 	*to = *from;
628 	memmove(to->intrcnt = intrcnt, from->intrcnt, nintr * sizeof (int));
629 }
630 
631 static void
632 dinfo(int dn, int c)
633 {
634 	double words, atime;
635 
636 	c = DISKCOL + c * 5;
637 
638 	/* time busy in disk activity */
639 	atime = (double)cur.dk_time[dn].tv_sec +
640 		((double)cur.dk_time[dn].tv_usec / (double)1000000);
641 
642 	words = cur.dk_bytes[dn] / 1024.0;	/* # of K transferred */
643 
644 	putint((int)((float)cur.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5);
645 	putint((int)((float)cur.dk_xfer[dn]/etime+0.5), DISKROW + 2, c, 5);
646 	putint((int)(words/etime + 0.5), DISKROW + 3, c, 5);
647 	putfloat(atime*100.0/etime, DISKROW + 4, c, 5, 1, 1);
648 }
649