176633Stshiozak/*-
276633Stshiozak * Copyright (c) 1983, 1989, 1992, 1993
376633Stshiozak *	The Regents of the University of California.  All rights reserved.
476633Stshiozak *
576633Stshiozak * Redistribution and use in source and binary forms, with or without
676633Stshiozak * modification, are permitted provided that the following conditions
776633Stshiozak * are met:
876633Stshiozak * 1. Redistributions of source code must retain the above copyright
976633Stshiozak *    notice, this list of conditions and the following disclaimer.
1076633Stshiozak * 2. Redistributions in binary form must reproduce the above copyright
1176633Stshiozak *    notice, this list of conditions and the following disclaimer in the
1276633Stshiozak *    documentation and/or other materials provided with the distribution.
1376633Stshiozak * 4. Neither the name of the University nor the names of its contributors
1476633Stshiozak *    may be used to endorse or promote products derived from this software
1576633Stshiozak *    without specific prior written permission.
1676633Stshiozak *
1776633Stshiozak * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1876633Stshiozak * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1976633Stshiozak * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2076633Stshiozak * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2176633Stshiozak * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2276633Stshiozak * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2376633Stshiozak * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2476633Stshiozak * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2576633Stshiozak * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2676633Stshiozak * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2776633Stshiozak * SUCH DAMAGE.
2876633Stshiozak */
2976633Stshiozak
3076633Stshiozak#include <sys/cdefs.h>
3176633Stshiozak
3276633Stshiozak__FBSDID("$FreeBSD$");
3376633Stshiozak
3476633Stshiozak#ifdef lint
3576633Stshiozakstatic const char sccsid[] = "@(#)vmstat.c	8.2 (Berkeley) 1/12/94";
3676633Stshiozak#endif
3776633Stshiozak
3876633Stshiozak/*
3976633Stshiozak * Cursed vmstat -- from Robert Elz.
4076633Stshiozak */
4176633Stshiozak
4276633Stshiozak#include <sys/param.h>
4376633Stshiozak#include <sys/stat.h>
4476633Stshiozak#include <sys/time.h>
4576633Stshiozak#include <sys/proc.h>
4676633Stshiozak#include <sys/uio.h>
4776633Stshiozak#include <sys/namei.h>
4876633Stshiozak#include <sys/resource.h>
4976633Stshiozak#include <sys/sysctl.h>
5076633Stshiozak#include <sys/vmmeter.h>
5176633Stshiozak
5276633Stshiozak#include <vm/vm_param.h>
5376633Stshiozak
5476633Stshiozak#include <ctype.h>
5576633Stshiozak#include <err.h>
5676633Stshiozak#include <errno.h>
5776633Stshiozak#include <langinfo.h>
5876633Stshiozak#include <nlist.h>
5976633Stshiozak#include <paths.h>
6076633Stshiozak#include <signal.h>
6176633Stshiozak#include <stdlib.h>
6276633Stshiozak#include <string.h>
6379555Sobrien#include <time.h>
6479555Sobrien#include <unistd.h>
6576633Stshiozak#include <utmpx.h>
6676633Stshiozak#include <devstat.h>
6776633Stshiozak#include "systat.h"
6876633Stshiozak#include "extern.h"
6976633Stshiozak#include "devs.h"
7076633Stshiozak
71123257Smarcelstatic struct Info {
72102227Smike	long	time[CPUSTATES];
73114185Skan	u_int v_swtch;		/* context switches */
74133564Stjr	u_int v_trap;		/* calls to trap */
75101776Stjr	u_int v_syscall;	/* calls to syscall() */
76102227Smike	u_int v_intr;		/* device interrupts */
77102227Smike	u_int v_soft;		/* software interrupts */
78102227Smike	/*
79101409Stjr	 * Virtual memory activity.
80101409Stjr	 */
81102227Smike	u_int v_vm_faults;	/* number of address memory faults */
82102227Smike	u_int v_io_faults;	/* page faults requiring I/O */
83102227Smike	u_int v_cow_faults;	/* number of copy-on-writes */
84101409Stjr	u_int v_zfod;		/* pages zero filled on demand */
85101409Stjr	u_int v_ozfod;		/* optimized zero fill pages */
8699640Sobrien	u_int v_swapin;		/* swap pager pageins */
87102227Smike	u_int v_swapout;	/* swap pager pageouts */
88102227Smike	u_int v_swappgsin;	/* swap pager pages paged in */
89102227Smike	u_int v_swappgsout;	/* swap pager pages paged out */
9076633Stshiozak	u_int v_vnodein;	/* vnode pager pageins */
9199640Sobrien	u_int v_vnodeout;	/* vnode pager pageouts */
9276633Stshiozak	u_int v_vnodepgsin;	/* vnode_pager pages paged in */
93102227Smike	u_int v_vnodepgsout;	/* vnode pager pages paged out */
94102227Smike	u_int v_intrans;	/* intransit blocking page faults */
95102227Smike	u_int v_reactivated;	/* number of pages reactivated from free list */
9676633Stshiozak	u_int v_pdwakeups;	/* number of times daemon has awaken from sleep */
9776633Stshiozak	u_int v_pdpages;	/* number of pages analyzed by daemon */
98114185Skan
99114185Skan	u_int v_dfree;		/* pages freed by daemon */
100114185Skan	u_int v_pfree;		/* pages freed by exiting processes */
101114185Skan	u_int v_tfree;		/* total pages freed */
102114185Skan	/*
10376633Stshiozak	 * Distribution of page usages.
10476633Stshiozak	 */
10576633Stshiozak	u_int v_page_size;	/* page size in bytes */
10676633Stshiozak	u_int v_free_count;	/* number of pages free */
107103177Stjr	u_int v_wire_count;	/* number of pages wired down */
108103218Stjr	u_int v_active_count;	/* number of pages active */
109103177Stjr	u_int v_inactive_count;	/* number of pages inactive */
11076633Stshiozak	u_int v_cache_count;	/* number of pages on buffer cache queue */
111101411Stjr	struct	vmtotal Total;
112103177Stjr	struct	nchstats nchstats;
113101776Stjr	long	nchcount;
114103177Stjr	long	*intrcnt;
115103177Stjr	long	bufspace;
116103177Stjr	int	desiredvnodes;
117103177Stjr	long	numvnodes;
118103739Stjr	long	freevnodes;
119103856Stjr	int	numdirtybuffers;
120103177Stjr} s, s1, s2, z;
121101776Stjr
12293032Simpstruct statinfo cur, last, run;
12393032Simp
12493032Simp#define	total s.Total
12593032Simp#define	nchtotal s.nchstats
12693032Simp#define	oldnchtotal s1.nchstats
12793032Simp
128103177Stjrstatic	enum state { BOOT, TIME, RUN } state = TIME;
129101776Stjr
130103739Stjrstatic void allocinfo(struct Info *);
131103739Stjrstatic void copyinfo(struct Info *, struct Info *);
132103856Stjrstatic float cputime(int);
133103177Stjrstatic void dinfo(int, int, struct statinfo *, struct statinfo *);
134103739Stjrstatic void getinfo(struct Info *);
135103739Stjrstatic void putint(int, int, int, int);
136103739Stjrstatic void putfloat(double, int, int, int, int, int);
137103739Stjrstatic void putlongdouble(long double, int, int, int, int, int);
138103739Stjrstatic int ucount(void);
13993032Simp
14093032Simpstatic	int ncpu;
141132565Stjrstatic	char buf[26];
142132565Stjrstatic	time_t t;
143104443Stjrstatic	double etime;
14493032Simpstatic	int nintr;
145132565Stjrstatic	long *intrloc;
146103218Stjrstatic	char **intrname;
147103218Stjrstatic	int nextintsrow;
148132565Stjr
14993032SimpWINDOW *
15093032Simpopenkre(void)
151132565Stjr{
15293032Simp
153132565Stjr	return (stdscr);
154132565Stjr}
15593032Simp
15693032Simpvoid
157132565Stjrclosekre(WINDOW *w)
158132565Stjr{
159132565Stjr
160104443Stjr	if (w == NULL)
161102142Sache		return;
162103339Stjr	wclear(w);
163103067Stjr	wrefresh(w);
164103067Stjr}
165103097Stjr
166103097Stjr/*
167103097Stjr * These constants define where the major pieces are laid out
168132565Stjr */
169132565Stjr#define STATROW		 0	/* uses 1 row and 67 cols */
170101411Stjr#define STATCOL		 0
171101411Stjr#define MEMROW		 2	/* uses 4 rows and 45 cols */
172101411Stjr#define MEMCOL		 0
173103739Stjr#define PAGEROW		 2	/* uses 4 rows and 30 cols */
174103856Stjr#define PAGECOL		47
175102198Stjr#define INTSROW		 6	/* uses all rows to bottom and 16 cols */
176129774Stjr#define INTSCOL		64
177129774Stjr#define PROCSROW	 6	/* uses 3 rows and 19 cols */
178129774Stjr#define PROCSCOL	 0
179129774Stjr#define GENSTATROW	 7	/* uses 2 rows and 29 cols */
180129774Stjr#define GENSTATCOL	21
181129774Stjr#define VMSTATROW	 7	/* uses 17 rows and 12-14 cols */
182129774Stjr#define VMSTATCOL	49	/* actually 50-51 for some fields */
183129774Stjr#define GRAPHROW	10	/* uses 3 rows and 49-51 cols */
184129774Stjr#define GRAPHCOL	 0
185130185Stjr#define VNSTATROW	13	/* uses 4 rows and 13 columns */
186129774Stjr#define VNSTATCOL	35
187129774Stjr#define NAMEIROW	14	/* uses 3 rows and 32 cols */
188106035Stjr#define NAMEICOL	 0
189106035Stjr#define DISKROW		18	/* uses 5 rows and 47 cols (for 7 drives) */
190106035Stjr#define DISKCOL		 0
191106035Stjr
192106035Stjr#define	DRIVESPACE	 7	/* max # for space */
193106035Stjr
194112177Stjr#define	MAXDRIVES	DRIVESPACE	 /* max # to display */
195112177Stjr
196112177Stjrint
197106035Stjrinitkre(void)
198106035Stjr{
199106035Stjr	char *cp, *cp1, *cp2, *intrnamebuf, *nextcp;
200106035Stjr	int i;
201106035Stjr	size_t sz;
202106035Stjr
203106035Stjr	if ((num_devices = devstat_getnumdevs(NULL)) < 0) {
204106035Stjr		warnx("%s", devstat_errbuf);
205106035Stjr		return(0);
206106035Stjr	}
207102198Stjr
208102198Stjr	cur.dinfo = calloc(1, sizeof(struct devinfo));
209102198Stjr	last.dinfo = calloc(1, sizeof(struct devinfo));
210133564Stjr	run.dinfo = calloc(1, sizeof(struct devinfo));
211102198Stjr
212103737Stjr	if (dsinit(MAXDRIVES, &cur, &last, &run) != 1)
213189136Sdas		return(0);
214132497Stjr
215132497Stjr	if (nintr == 0) {
216189365Sdas		if (sysctlbyname("hw.intrcnt", NULL, &sz, NULL, 0) == -1) {
217189365Sdas			error("sysctl(hw.intrcnt...) failed: %s",
218187961Sdas			      strerror(errno));
219189136Sdas			return (0);
220189136Sdas		}
221189136Sdas		nintr = sz / sizeof(u_long);
222132497Stjr		intrloc = calloc(nintr, sizeof (long));
223132497Stjr		intrname = calloc(nintr, sizeof (char *));
224189136Sdas		intrnamebuf = sysctl_dynread("hw.intrnames", NULL);
225189136Sdas		if (intrnamebuf == NULL || intrname == NULL ||
226189136Sdas		    intrloc == NULL) {
227189136Sdas			error("Out of memory");
228103737Stjr			if (intrnamebuf)
229103737Stjr				free(intrnamebuf);
230103737Stjr			if (intrname)
23176633Stshiozak				free(intrname);
23276633Stshiozak			if (intrloc)
23376633Stshiozak				free(intrloc);
234			nintr = 0;
235			return(0);
236		}
237		for (cp = intrnamebuf, i = 0; i < nintr; i++) {
238			nextcp = cp + strlen(cp) + 1;
239
240			/* Discard trailing spaces. */
241			for (cp1 = nextcp - 1; cp1 > cp && *(cp1 - 1) == ' '; )
242				*--cp1 = '\0';
243
244			/* Convert "irqN: name" to "name irqN". */
245			if (strncmp(cp, "irq", 3) == 0) {
246				cp1 = cp + 3;
247				while (isdigit((u_char)*cp1))
248					cp1++;
249				if (cp1 != cp && *cp1 == ':' &&
250				    *(cp1 + 1) == ' ') {
251					sz = strlen(cp);
252					*cp1 = '\0';
253					cp1 = cp1 + 2;
254					cp2 = strdup(cp);
255					bcopy(cp1, cp, sz - (cp1 - cp) + 1);
256					if (sz <= 10 + 4) {
257						strcat(cp, " ");
258						strcat(cp, cp2 + 3);
259					}
260					free(cp2);
261				}
262			}
263
264			/*
265			 * Convert "name irqN" to "name N" if the former is
266			 * longer than the field width.
267			 */
268			if ((cp1 = strstr(cp, "irq")) != NULL &&
269			    strlen(cp) > 10)
270				bcopy(cp1 + 3, cp1, strlen(cp1 + 3) + 1);
271
272			intrname[i] = cp;
273			cp = nextcp;
274		}
275		nextintsrow = INTSROW + 2;
276		allocinfo(&s);
277		allocinfo(&s1);
278		allocinfo(&s2);
279		allocinfo(&z);
280	}
281	getinfo(&s2);
282	copyinfo(&s2, &s1);
283	return(1);
284}
285
286void
287fetchkre(void)
288{
289	time_t now;
290	struct tm *tp;
291	static int d_first = -1;
292
293	if (d_first < 0)
294		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
295
296	time(&now);
297	tp = localtime(&now);
298	(void) strftime(buf, sizeof(buf),
299			d_first ? "%e %b %R" : "%b %e %R", tp);
300	getinfo(&s);
301}
302
303void
304labelkre(void)
305{
306	int i, j;
307
308	clear();
309	mvprintw(STATROW, STATCOL + 6, "users    Load");
310	mvprintw(MEMROW, MEMCOL, "Mem:KB    REAL            VIRTUAL");
311	mvprintw(MEMROW + 1, MEMCOL, "        Tot   Share      Tot    Share");
312	mvprintw(MEMROW + 2, MEMCOL, "Act");
313	mvprintw(MEMROW + 3, MEMCOL, "All");
314
315	mvprintw(MEMROW + 1, MEMCOL + 41, "Free");
316
317	mvprintw(PAGEROW, PAGECOL,     "         VN PAGER   SWAP PAGER");
318	mvprintw(PAGEROW + 1, PAGECOL, "         in   out     in   out");
319	mvprintw(PAGEROW + 2, PAGECOL, "count");
320	mvprintw(PAGEROW + 3, PAGECOL, "pages");
321
322	mvprintw(INTSROW, INTSCOL + 1, "Interrupts");
323	mvprintw(INTSROW + 1, INTSCOL + 6, "total");
324
325	mvprintw(VMSTATROW, VMSTATCOL + 9, "ioflt");
326	mvprintw(VMSTATROW + 1, VMSTATCOL + 9, "cow");
327	mvprintw(VMSTATROW + 2, VMSTATCOL + 9, "zfod");
328	mvprintw(VMSTATROW + 3, VMSTATCOL + 9, "ozfod");
329	mvprintw(VMSTATROW + 4, VMSTATCOL + 9 - 1, "%%ozfod");
330	mvprintw(VMSTATROW + 5, VMSTATCOL + 9, "daefr");
331	mvprintw(VMSTATROW + 6, VMSTATCOL + 9, "prcfr");
332	mvprintw(VMSTATROW + 7, VMSTATCOL + 9, "totfr");
333	mvprintw(VMSTATROW + 8, VMSTATCOL + 9, "react");
334	mvprintw(VMSTATROW + 9, VMSTATCOL + 9, "pdwak");
335	mvprintw(VMSTATROW + 10, VMSTATCOL + 9, "pdpgs");
336	mvprintw(VMSTATROW + 11, VMSTATCOL + 9, "intrn");
337	mvprintw(VMSTATROW + 12, VMSTATCOL + 9, "wire");
338	mvprintw(VMSTATROW + 13, VMSTATCOL + 9, "act");
339	mvprintw(VMSTATROW + 14, VMSTATCOL + 9, "inact");
340	mvprintw(VMSTATROW + 15, VMSTATCOL + 9, "cache");
341	mvprintw(VMSTATROW + 16, VMSTATCOL + 9, "free");
342	if (LINES - 1 > VMSTATROW + 17)
343		mvprintw(VMSTATROW + 17, VMSTATCOL + 9, "buf");
344
345	mvprintw(GENSTATROW, GENSTATCOL, " Csw  Trp  Sys  Int  Sof  Flt");
346
347	mvprintw(GRAPHROW, GRAPHCOL,
348		"  . %%Sys    . %%Intr   . %%User   . %%Nice   . %%Idle");
349	mvprintw(PROCSROW, PROCSCOL, "Proc:");
350	mvprintw(PROCSROW + 1, PROCSCOL, "  r   p   d   s   w");
351	mvprintw(GRAPHROW + 1, GRAPHCOL,
352		"|    |    |    |    |    |    |    |    |    |    |");
353
354	mvprintw(VNSTATROW, VNSTATCOL + 8, "dtbuf");
355	mvprintw(VNSTATROW + 1, VNSTATCOL + 8, "desvn");
356	mvprintw(VNSTATROW + 2, VNSTATCOL + 8, "numvn");
357	mvprintw(VNSTATROW + 3, VNSTATCOL + 8, "frevn");
358
359	mvprintw(NAMEIROW, NAMEICOL, "Namei     Name-cache   Dir-cache");
360	mvprintw(NAMEIROW + 1, NAMEICOL,
361		"   Calls    hits   %%    hits   %%");
362	mvprintw(DISKROW, DISKCOL, "Disks");
363	mvprintw(DISKROW + 1, DISKCOL, "KB/t");
364	mvprintw(DISKROW + 2, DISKCOL, "tps");
365	mvprintw(DISKROW + 3, DISKCOL, "MB/s");
366	mvprintw(DISKROW + 4, DISKCOL, "%%busy");
367	/*
368	 * For now, we don't support a fourth disk statistic.  So there's
369	 * no point in providing a label for it.  If someone can think of a
370	 * fourth useful disk statistic, there is room to add it.
371	 */
372	/* mvprintw(DISKROW + 4, DISKCOL, " msps"); */
373	j = 0;
374	for (i = 0; i < num_devices && j < MAXDRIVES; i++)
375		if (dev_select[i].selected) {
376			char tmpstr[80];
377			sprintf(tmpstr, "%s%d", dev_select[i].device_name,
378				dev_select[i].unit_number);
379			mvprintw(DISKROW, DISKCOL + 5 + 6 * j,
380				" %5.5s", tmpstr);
381			j++;
382		}
383
384	for (i = 0; i < nintr; i++) {
385		if (intrloc[i] == 0)
386			continue;
387		mvprintw(intrloc[i], INTSCOL + 6, "%-10.10s", intrname[i]);
388	}
389}
390
391#define X(fld)	{t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
392#define Q(fld)	{t=cur.fld[i]; cur.fld[i]-=last.fld[i]; if(state==TIME) last.fld[i]=t;}
393#define Y(fld)	{t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
394#define Z(fld)	{t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
395	if(state == TIME) s1.nchstats.fld = t;}
396#define PUTRATE(fld, l, c, w) \
397do { \
398	Y(fld); \
399	putint((int)((float)s.fld/etime + 0.5), l, c, w); \
400} while (0)
401#define MAXFAIL 5
402
403static	char cpuchar[CPUSTATES] = { '=' , '+', '>', '-', ' ' };
404static	char cpuorder[CPUSTATES] = { CP_SYS, CP_INTR, CP_USER, CP_NICE,
405				     CP_IDLE };
406
407void
408showkre(void)
409{
410	float f1, f2;
411	int psiz, inttotal;
412	int i, l, lc;
413	static int failcnt = 0;
414
415	etime = 0;
416	for(i = 0; i < CPUSTATES; i++) {
417		X(time);
418		Q(cp_time);
419		etime += s.time[i];
420	}
421	if (etime < 5.0) {	/* < 5 ticks - ignore this trash */
422		if (failcnt++ >= MAXFAIL) {
423			clear();
424			mvprintw(2, 10, "The alternate system clock has died!");
425			mvprintw(3, 10, "Reverting to ``pigs'' display.");
426			move(CMDLINE, 0);
427			refresh();
428			failcnt = 0;
429			sleep(5);
430			command("pigs");
431		}
432		return;
433	}
434	failcnt = 0;
435	etime /= hertz;
436	etime /= ncpu;
437	inttotal = 0;
438	for (i = 0; i < nintr; i++) {
439		if (s.intrcnt[i] == 0)
440			continue;
441		X(intrcnt);
442		l = (int)((float)s.intrcnt[i]/etime + 0.5);
443		inttotal += l;
444		if (intrloc[i] == 0) {
445			if (nextintsrow == LINES)
446				continue;
447			intrloc[i] = nextintsrow++;
448			mvprintw(intrloc[i], INTSCOL + 6, "%-10.10s",
449				intrname[i]);
450		}
451		putint(l, intrloc[i], INTSCOL, 5);
452	}
453	putint(inttotal, INTSROW + 1, INTSCOL, 5);
454	Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
455	Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes); Z(ncs_neghits);
456	s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
457	    nchtotal.ncs_miss + nchtotal.ncs_long + nchtotal.ncs_neghits;
458	if (state == TIME)
459		s1.nchcount = s.nchcount;
460
461	psiz = 0;
462	f2 = 0.0;
463	for (lc = 0; lc < CPUSTATES; lc++) {
464		i = cpuorder[lc];
465		f1 = cputime(i);
466		f2 += f1;
467		l = (int) ((f2 + 1.0) / 2.0) - psiz;
468		putfloat(f1, GRAPHROW, GRAPHCOL + 10 * lc, 4, 1, 0);
469		move(GRAPHROW + 2, psiz);
470		psiz += l;
471		while (l-- > 0)
472			addch(cpuchar[lc]);
473	}
474
475	putint(ucount(), STATROW, STATCOL, 5);
476	putfloat(avenrun[0], STATROW, STATCOL + 20, 5, 2, 0);
477	putfloat(avenrun[1], STATROW, STATCOL + 26, 5, 2, 0);
478	putfloat(avenrun[2], STATROW, STATCOL + 32, 5, 2, 0);
479	mvaddstr(STATROW, STATCOL + 55, buf);
480#define pgtokb(pg)	((pg) * (s.v_page_size / 1024))
481	putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 4, 7);
482	putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 12, 7);
483	putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 20, 8);
484	putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 29, 8);
485	putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 4, 7);
486	putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 12, 7);
487	putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 20, 8);
488	putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 29, 8);
489	putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 38, 7);
490	putint(total.t_rq - 1, PROCSROW + 2, PROCSCOL, 3);
491	putint(total.t_pw, PROCSROW + 2, PROCSCOL + 4, 3);
492	putint(total.t_dw, PROCSROW + 2, PROCSCOL + 8, 3);
493	putint(total.t_sl, PROCSROW + 2, PROCSCOL + 12, 3);
494	putint(total.t_sw, PROCSROW + 2, PROCSCOL + 16, 3);
495	PUTRATE(v_io_faults, VMSTATROW, VMSTATCOL + 2, 8 - 2);
496	PUTRATE(v_cow_faults, VMSTATROW + 1, VMSTATCOL + 2, 8 - 2);
497	PUTRATE(v_zfod, VMSTATROW + 2, VMSTATCOL + 2, 8 - 2);
498	PUTRATE(v_ozfod, VMSTATROW + 3, VMSTATCOL, 8);
499	putint(s.v_zfod != 0 ? (int)(s.v_ozfod * 100.0 / s.v_zfod) : 0,
500	    VMSTATROW + 4, VMSTATCOL + 1, 8 - 1);
501	PUTRATE(v_dfree, VMSTATROW + 5, VMSTATCOL + 2, 8 - 2);
502	PUTRATE(v_pfree, VMSTATROW + 6, VMSTATCOL + 2, 8 - 2);
503	PUTRATE(v_tfree, VMSTATROW + 7, VMSTATCOL, 8);
504	PUTRATE(v_reactivated, VMSTATROW + 8, VMSTATCOL, 8);
505	PUTRATE(v_pdwakeups, VMSTATROW + 9, VMSTATCOL, 8);
506	PUTRATE(v_pdpages, VMSTATROW + 10, VMSTATCOL, 8);
507	PUTRATE(v_intrans, VMSTATROW + 11, VMSTATCOL, 8);
508	putint(pgtokb(s.v_wire_count), VMSTATROW + 12, VMSTATCOL, 8);
509	putint(pgtokb(s.v_active_count), VMSTATROW + 13, VMSTATCOL, 8);
510	putint(pgtokb(s.v_inactive_count), VMSTATROW + 14, VMSTATCOL, 8);
511	putint(pgtokb(s.v_cache_count), VMSTATROW + 15, VMSTATCOL, 8);
512	putint(pgtokb(s.v_free_count), VMSTATROW + 16, VMSTATCOL, 8);
513	if (LINES - 1 > VMSTATROW + 17)
514		putint(s.bufspace / 1024, VMSTATROW + 17, VMSTATCOL, 8);
515	PUTRATE(v_vnodein, PAGEROW + 2, PAGECOL + 6, 5);
516	PUTRATE(v_vnodeout, PAGEROW + 2, PAGECOL + 12, 5);
517	PUTRATE(v_swapin, PAGEROW + 2, PAGECOL + 19, 5);
518	PUTRATE(v_swapout, PAGEROW + 2, PAGECOL + 25, 5);
519	PUTRATE(v_vnodepgsin, PAGEROW + 3, PAGECOL + 6, 5);
520	PUTRATE(v_vnodepgsout, PAGEROW + 3, PAGECOL + 12, 5);
521	PUTRATE(v_swappgsin, PAGEROW + 3, PAGECOL + 19, 5);
522	PUTRATE(v_swappgsout, PAGEROW + 3, PAGECOL + 25, 5);
523	PUTRATE(v_swtch, GENSTATROW + 1, GENSTATCOL, 4);
524	PUTRATE(v_trap, GENSTATROW + 1, GENSTATCOL + 5, 4);
525	PUTRATE(v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 4);
526	PUTRATE(v_intr, GENSTATROW + 1, GENSTATCOL + 15, 4);
527	PUTRATE(v_soft, GENSTATROW + 1, GENSTATCOL + 20, 4);
528	PUTRATE(v_vm_faults, GENSTATROW + 1, GENSTATCOL + 25, 4);
529	for (i = 0, lc = 0; i < num_devices && lc < MAXDRIVES; i++)
530		if (dev_select[i].selected) {
531			switch(state) {
532			case TIME:
533				dinfo(i, ++lc, &cur, &last);
534				break;
535			case RUN:
536				dinfo(i, ++lc, &cur, &run);
537				break;
538			case BOOT:
539				dinfo(i, ++lc, &cur, NULL);
540				break;
541			}
542		}
543	putint(s.numdirtybuffers, VNSTATROW, VNSTATCOL, 7);
544	putint(s.desiredvnodes, VNSTATROW + 1, VNSTATCOL, 7);
545	putint(s.numvnodes, VNSTATROW + 2, VNSTATCOL, 7);
546	putint(s.freevnodes, VNSTATROW + 3, VNSTATCOL, 7);
547	putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 8);
548	putint((nchtotal.ncs_goodhits + nchtotal.ncs_neghits),
549	   NAMEIROW + 2, NAMEICOL + 9, 7);
550#define nz(x)	((x) ? (x) : 1)
551	putfloat((nchtotal.ncs_goodhits+nchtotal.ncs_neghits) *
552	   100.0 / nz(s.nchcount),
553	   NAMEIROW + 2, NAMEICOL + 17, 3, 0, 1);
554	putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 21, 7);
555	putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
556	   NAMEIROW + 2, NAMEICOL + 29, 3, 0, 1);
557#undef nz
558}
559
560int
561cmdkre(const char *cmd, const char *args)
562{
563	int retval;
564
565	if (prefix(cmd, "run")) {
566		retval = 1;
567		copyinfo(&s2, &s1);
568		switch (devstat_getdevs(NULL, &run)) {
569		case -1:
570			errx(1, "%s", devstat_errbuf);
571			break;
572		case 1:
573			num_devices = run.dinfo->numdevs;
574			generation = run.dinfo->generation;
575			retval = dscmd("refresh", NULL, MAXDRIVES, &cur);
576			if (retval == 2)
577				labelkre();
578			break;
579		default:
580			break;
581		}
582		state = RUN;
583		return (retval);
584	}
585	if (prefix(cmd, "boot")) {
586		state = BOOT;
587		copyinfo(&z, &s1);
588		return (1);
589	}
590	if (prefix(cmd, "time")) {
591		state = TIME;
592		return (1);
593	}
594	if (prefix(cmd, "zero")) {
595		retval = 1;
596		if (state == RUN) {
597			getinfo(&s1);
598			switch (devstat_getdevs(NULL, &run)) {
599			case -1:
600				errx(1, "%s", devstat_errbuf);
601				break;
602			case 1:
603				num_devices = run.dinfo->numdevs;
604				generation = run.dinfo->generation;
605				retval = dscmd("refresh",NULL, MAXDRIVES, &cur);
606				if (retval == 2)
607					labelkre();
608				break;
609			default:
610				break;
611			}
612		}
613		return (retval);
614	}
615	retval = dscmd(cmd, args, MAXDRIVES, &cur);
616
617	if (retval == 2)
618		labelkre();
619
620	return(retval);
621}
622
623/* calculate number of users on the system */
624static int
625ucount(void)
626{
627	int nusers = 0;
628	struct utmpx *ut;
629
630	setutxent();
631	while ((ut = getutxent()) != NULL)
632		if (ut->ut_type == USER_PROCESS)
633			nusers++;
634	endutxent();
635
636	return (nusers);
637}
638
639static float
640cputime(int indx)
641{
642	double lt;
643	int i;
644
645	lt = 0;
646	for (i = 0; i < CPUSTATES; i++)
647		lt += s.time[i];
648	if (lt == 0.0)
649		lt = 1.0;
650	return (s.time[indx] * 100.0 / lt);
651}
652
653static void
654putint(int n, int l, int lc, int w)
655{
656	int snr;
657	char b[128];
658
659	move(l, lc);
660#ifdef DEBUG
661		while (w-- > 0)
662			addch('*');
663		return;
664#endif
665	if (n == 0) {
666		while (w-- > 0)
667			addch(' ');
668		return;
669	}
670	snr = snprintf(b, sizeof(b), "%*d", w, n);
671	if (snr != w)
672		snr = snprintf(b, sizeof(b), "%*dk", w - 1, n / 1000);
673	if (snr != w)
674		snr = snprintf(b, sizeof(b), "%*dM", w - 1, n / 1000000);
675	if (snr != w) {
676		while (w-- > 0)
677			addch('*');
678		return;
679	}
680	addstr(b);
681}
682
683static void
684putfloat(double f, int l, int lc, int w, int d, int nz)
685{
686	int snr;
687	char b[128];
688
689	move(l, lc);
690#ifdef DEBUG
691		while (--w >= 0)
692			addch('*');
693		return;
694#endif
695	if (nz && f == 0.0) {
696		while (--w >= 0)
697			addch(' ');
698		return;
699	}
700	snr = snprintf(b, sizeof(b), "%*.*f", w, d, f);
701	if (snr != w)
702		snr = snprintf(b, sizeof(b), "%*.0f", w, f);
703	if (snr != w)
704		snr = snprintf(b, sizeof(b), "%*.0fk", w - 1, f / 1000);
705	if (snr != w)
706		snr = snprintf(b, sizeof(b), "%*.0fM", w - 1, f / 1000000);
707	if (snr != w) {
708		while (--w >= 0)
709			addch('*');
710		return;
711	}
712	addstr(b);
713}
714
715static void
716putlongdouble(long double f, int l, int lc, int w, int d, int nz)
717{
718	int snr;
719	char b[128];
720
721	move(l, lc);
722#ifdef DEBUG
723		while (--w >= 0)
724			addch('*');
725		return;
726#endif
727	if (nz && f == 0.0) {
728		while (--w >= 0)
729			addch(' ');
730		return;
731	}
732	snr = snprintf(b, sizeof(b), "%*.*Lf", w, d, f);
733	if (snr != w)
734		snr = snprintf(b, sizeof(b), "%*.0Lf", w, f);
735	if (snr != w)
736		snr = snprintf(b, sizeof(b), "%*.0Lfk", w - 1, f / 1000);
737	if (snr != w)
738		snr = snprintf(b, sizeof(b), "%*.0LfM", w - 1, f / 1000000);
739	if (snr != w) {
740		while (--w >= 0)
741			addch('*');
742		return;
743	}
744	addstr(b);
745}
746
747static void
748getinfo(struct Info *ls)
749{
750	struct devinfo *tmp_dinfo;
751	size_t size;
752	int mib[2];
753
754	GETSYSCTL("kern.cp_time", ls->time);
755	GETSYSCTL("kern.cp_time", cur.cp_time);
756	GETSYSCTL("vm.stats.sys.v_swtch", ls->v_swtch);
757	GETSYSCTL("vm.stats.sys.v_trap", ls->v_trap);
758	GETSYSCTL("vm.stats.sys.v_syscall", ls->v_syscall);
759	GETSYSCTL("vm.stats.sys.v_intr", ls->v_intr);
760	GETSYSCTL("vm.stats.sys.v_soft", ls->v_soft);
761	GETSYSCTL("vm.stats.vm.v_vm_faults", ls->v_vm_faults);
762	GETSYSCTL("vm.stats.vm.v_io_faults", ls->v_io_faults);
763	GETSYSCTL("vm.stats.vm.v_cow_faults", ls->v_cow_faults);
764	GETSYSCTL("vm.stats.vm.v_zfod", ls->v_zfod);
765	GETSYSCTL("vm.stats.vm.v_ozfod", ls->v_ozfod);
766	GETSYSCTL("vm.stats.vm.v_swapin", ls->v_swapin);
767	GETSYSCTL("vm.stats.vm.v_swapout", ls->v_swapout);
768	GETSYSCTL("vm.stats.vm.v_swappgsin", ls->v_swappgsin);
769	GETSYSCTL("vm.stats.vm.v_swappgsout", ls->v_swappgsout);
770	GETSYSCTL("vm.stats.vm.v_vnodein", ls->v_vnodein);
771	GETSYSCTL("vm.stats.vm.v_vnodeout", ls->v_vnodeout);
772	GETSYSCTL("vm.stats.vm.v_vnodepgsin", ls->v_vnodepgsin);
773	GETSYSCTL("vm.stats.vm.v_vnodepgsout", ls->v_vnodepgsout);
774	GETSYSCTL("vm.stats.vm.v_intrans", ls->v_intrans);
775	GETSYSCTL("vm.stats.vm.v_reactivated", ls->v_reactivated);
776	GETSYSCTL("vm.stats.vm.v_pdwakeups", ls->v_pdwakeups);
777	GETSYSCTL("vm.stats.vm.v_pdpages", ls->v_pdpages);
778	GETSYSCTL("vm.stats.vm.v_dfree", ls->v_dfree);
779	GETSYSCTL("vm.stats.vm.v_pfree", ls->v_pfree);
780	GETSYSCTL("vm.stats.vm.v_tfree", ls->v_tfree);
781	GETSYSCTL("vm.stats.vm.v_page_size", ls->v_page_size);
782	GETSYSCTL("vm.stats.vm.v_free_count", ls->v_free_count);
783	GETSYSCTL("vm.stats.vm.v_wire_count", ls->v_wire_count);
784	GETSYSCTL("vm.stats.vm.v_active_count", ls->v_active_count);
785	GETSYSCTL("vm.stats.vm.v_inactive_count", ls->v_inactive_count);
786	GETSYSCTL("vm.stats.vm.v_cache_count", ls->v_cache_count);
787	GETSYSCTL("vfs.bufspace", ls->bufspace);
788	GETSYSCTL("kern.maxvnodes", ls->desiredvnodes);
789	GETSYSCTL("vfs.numvnodes", ls->numvnodes);
790	GETSYSCTL("vfs.freevnodes", ls->freevnodes);
791	GETSYSCTL("vfs.cache.nchstats", ls->nchstats);
792	GETSYSCTL("vfs.numdirtybuffers", ls->numdirtybuffers);
793	getsysctl("hw.intrcnt", ls->intrcnt, nintr * sizeof(u_long));
794
795	size = sizeof(ls->Total);
796	mib[0] = CTL_VM;
797	mib[1] = VM_TOTAL;
798	if (sysctl(mib, 2, &ls->Total, &size, NULL, 0) < 0) {
799		error("Can't get kernel info: %s\n", strerror(errno));
800		bzero(&ls->Total, sizeof(ls->Total));
801	}
802	size = sizeof(ncpu);
803	if (sysctlbyname("hw.ncpu", &ncpu, &size, NULL, 0) < 0 ||
804	    size != sizeof(ncpu))
805		ncpu = 1;
806
807	tmp_dinfo = last.dinfo;
808	last.dinfo = cur.dinfo;
809	cur.dinfo = tmp_dinfo;
810
811	last.snap_time = cur.snap_time;
812	switch (devstat_getdevs(NULL, &cur)) {
813	case -1:
814		errx(1, "%s", devstat_errbuf);
815		break;
816	case 1:
817		num_devices = cur.dinfo->numdevs;
818		generation = cur.dinfo->generation;
819		cmdkre("refresh", NULL);
820		break;
821	default:
822		break;
823	}
824}
825
826static void
827allocinfo(struct Info *ls)
828{
829
830	ls->intrcnt = (long *) calloc(nintr, sizeof(long));
831	if (ls->intrcnt == NULL)
832		errx(2, "out of memory");
833}
834
835static void
836copyinfo(struct Info *from, struct Info *to)
837{
838	long *intrcnt;
839
840	/*
841	 * time, wds, seek, and xfer are malloc'd so we have to
842	 * save the pointers before the structure copy and then
843	 * copy by hand.
844	 */
845	intrcnt = to->intrcnt;
846	*to = *from;
847
848	bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
849}
850
851static void
852dinfo(int dn, int lc, struct statinfo *now, struct statinfo *then)
853{
854	long double transfers_per_second;
855	long double kb_per_transfer, mb_per_second;
856	long double elapsed_time, device_busy;
857	int di;
858
859	di = dev_select[dn].position;
860
861	if (then != NULL) {
862		/* Calculate relative to previous sample */
863		elapsed_time = now->snap_time - then->snap_time;
864	} else {
865		/* Calculate relative to device creation */
866		elapsed_time = now->snap_time - devstat_compute_etime(
867		    &now->dinfo->devices[di].creation_time, NULL);
868	}
869
870	if (devstat_compute_statistics(&now->dinfo->devices[di], then ?
871	    &then->dinfo->devices[di] : NULL, elapsed_time,
872	    DSM_KB_PER_TRANSFER, &kb_per_transfer,
873	    DSM_TRANSFERS_PER_SECOND, &transfers_per_second,
874	    DSM_MB_PER_SECOND, &mb_per_second,
875	    DSM_BUSY_PCT, &device_busy,
876	    DSM_NONE) != 0)
877		errx(1, "%s", devstat_errbuf);
878
879	lc = DISKCOL + lc * 6;
880	putlongdouble(kb_per_transfer, DISKROW + 1, lc, 5, 2, 0);
881	putlongdouble(transfers_per_second, DISKROW + 2, lc, 5, 0, 0);
882	putlongdouble(mb_per_second, DISKROW + 3, lc, 5, 2, 0);
883	putlongdouble(device_busy, DISKROW + 4, lc, 5, 0, 0);
884}
885