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