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