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