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