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