vmstat.c revision 28149
1/*-
2 * Copyright (c) 1983, 1989, 1992, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35#if 0
36static char sccsid[] = "@(#)vmstat.c	8.2 (Berkeley) 1/12/94";
37#endif
38static const char rcsid[] =
39	"$Id$";
40#endif /* not lint */
41
42/*
43 * Cursed vmstat -- from Robert Elz.
44 */
45
46#include <sys/param.h>
47#include <sys/dkstat.h>
48#include <sys/buf.h>
49#include <sys/stat.h>
50#include <sys/time.h>
51#include <sys/proc.h>
52#include <sys/uio.h>
53#include <sys/namei.h>
54#include <sys/sysctl.h>
55#include <sys/vmmeter.h>
56
57#include <vm/vm_param.h>
58
59#include <ctype.h>
60#include <err.h>
61#include <nlist.h>
62#include <paths.h>
63#include <signal.h>
64#include <stdlib.h>
65#include <string.h>
66#include <time.h>
67#include <unistd.h>
68#include <utmp.h>
69#include "systat.h"
70#include "extern.h"
71
72static struct Info {
73	long	time[CPUSTATES];
74	struct	vmmeter Cnt;
75	struct	vmtotal Total;
76	long	*dk_time;
77	long	*dk_wds;
78	long	*dk_seek;
79	long	*dk_xfer;
80	int	dk_busy;
81	struct	nchstats nchstats;
82	long	nchcount;
83	long	*intrcnt;
84	int		bufspace;
85} s, s1, s2, z;
86
87#define	cnt s.Cnt
88#define oldcnt s1.Cnt
89#define	total s.Total
90#define	nchtotal s.nchstats
91#define	oldnchtotal s1.nchstats
92
93static	enum state { BOOT, TIME, RUN } state = TIME;
94
95static void allocinfo __P((struct Info *));
96static void copyinfo __P((struct Info *, struct Info *));
97static float cputime __P((int));
98static void dinfo __P((int, int));
99static void getinfo __P((struct Info *, enum state));
100static void putint __P((int, int, int, int));
101static void putfloat __P((double, int, int, int, int, int));
102static int ucount __P((void));
103
104static	int ut;
105static	char buf[26];
106static	time_t t;
107static	double etime;
108static	int nintr;
109static	long *intrloc;
110static	char **intrname;
111static	int nextintsrow;
112
113struct	utmp utmp;
114
115
116WINDOW *
117openkre()
118{
119
120	ut = open(_PATH_UTMP, O_RDONLY);
121	if (ut < 0)
122		error("No utmp");
123	return (stdscr);
124}
125
126void
127closekre(w)
128	WINDOW *w;
129{
130
131	(void) close(ut);
132	if (w == NULL)
133		return;
134	wclear(w);
135	wrefresh(w);
136}
137
138
139static struct nlist namelist[] = {
140#define X_CPTIME	0
141	{ "_cp_time" },
142#define X_CNT		1
143	{ "_cnt" },
144#define	X_BUFFERSPACE	2
145	{ "_bufspace" },
146#define	X_DK_BUSY	3
147	{ "_dk_busy" },
148#define	X_DK_TIME	4
149	{ "_dk_time" },
150#define	X_DK_XFER	5
151	{ "_dk_xfer" },
152#define	X_DK_WDS	6
153	{ "_dk_wds" },
154#define	X_DK_SEEK	7
155	{ "_dk_seek" },
156#define	X_NCHSTATS	8
157	{ "_nchstats" },
158#define	X_INTRNAMES	9
159	{ "_intrnames" },
160#define	X_EINTRNAMES	10
161	{ "_eintrnames" },
162#define	X_INTRCNT	11
163	{ "_intrcnt" },
164#define	X_EINTRCNT	12
165	{ "_eintrcnt" },
166	{ "" },
167};
168
169/*
170 * These constants define where the major pieces are laid out
171 */
172#define STATROW		 0	/* uses 1 row and 68 cols */
173#define STATCOL		 2
174#define MEMROW		 2	/* uses 4 rows and 31 cols */
175#define MEMCOL		 0
176#define PAGEROW		 2	/* uses 4 rows and 26 cols */
177#define PAGECOL		46
178#define INTSROW		 6	/* uses all rows to bottom and 17 cols */
179#define INTSCOL		61
180#define PROCSROW	 7	/* uses 2 rows and 20 cols */
181#define PROCSCOL	 0
182#define GENSTATROW	 7	/* uses 2 rows and 30 cols */
183#define GENSTATCOL	20
184#define VMSTATROW	 6	/* uses 17 rows and 12 cols */
185#define VMSTATCOL	48
186#define GRAPHROW	10	/* uses 3 rows and 51 cols */
187#define GRAPHCOL	 0
188#define NAMEIROW	14	/* uses 3 rows and 38 cols */
189#define NAMEICOL	 0
190#define DISKROW		18	/* uses 5 rows and 50 cols (for 9 drives) */
191#define DISKCOL		 0
192
193#define	DRIVESPACE	 9	/* max # for space */
194
195#if DK_NDRIVE > DRIVESPACE
196#define	MAXDRIVES	DRIVESPACE	 /* max # to display */
197#else
198#define	MAXDRIVES	DK_NDRIVE	 /* max # to display */
199#endif
200
201int
202initkre()
203{
204	char *intrnamebuf, *cp;
205	int i;
206	static int once = 0;
207
208	if (namelist[0].n_type == 0) {
209		if (kvm_nlist(kd, namelist)) {
210			nlisterr(namelist);
211			return(0);
212		}
213		if (namelist[0].n_type == 0) {
214			error("No namelist");
215			return(0);
216		}
217	}
218	if (! dkinit())
219		return(0);
220	if (dk_ndrive && !once) {
221#define	allocate(e, t) \
222    s./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
223    s1./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
224    s2./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
225    z./**/e = (t *)calloc(dk_ndrive, sizeof (t));
226		allocate(dk_time, long);
227		allocate(dk_wds, long);
228		allocate(dk_seek, long);
229		allocate(dk_xfer, long);
230		once = 1;
231#undef allocate
232	}
233	if (nintr == 0) {
234		nintr = (namelist[X_EINTRCNT].n_value -
235			namelist[X_INTRCNT].n_value) / sizeof (long);
236		intrloc = calloc(nintr, sizeof (long));
237		intrname = calloc(nintr, sizeof (long));
238		intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value -
239			namelist[X_INTRNAMES].n_value);
240		if (intrnamebuf == 0 || intrname == 0 || intrloc == 0) {
241			error("Out of memory\n");
242			if (intrnamebuf)
243				free(intrnamebuf);
244			if (intrname)
245				free(intrname);
246			if (intrloc)
247				free(intrloc);
248			nintr = 0;
249			return(0);
250		}
251		NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) -
252			NVAL(X_INTRNAMES));
253		for (cp = intrnamebuf, i = 0; i < nintr; i++) {
254			intrname[i] = cp;
255			cp += strlen(cp) + 1;
256		}
257		nextintsrow = INTSROW + 2;
258		allocinfo(&s);
259		allocinfo(&s1);
260		allocinfo(&s2);
261		allocinfo(&z);
262	}
263	getinfo(&s2, RUN);
264	copyinfo(&s2, &s1);
265	return(1);
266}
267
268void
269fetchkre()
270{
271	time_t now;
272	struct tm *tp;
273
274	time(&now);
275	tp = localtime(&now);
276	(void) strftime(buf, sizeof(buf), "%c", tp);
277	buf[16] = '\0';
278	getinfo(&s, state);
279}
280
281void
282labelkre()
283{
284	register int i, j;
285
286	clear();
287	mvprintw(STATROW, STATCOL + 4, "users    Load");
288	mvprintw(MEMROW, MEMCOL, "Mem:KB    REAL            VIRTUAL");
289	mvprintw(MEMROW + 1, MEMCOL, "        Tot   Share      Tot    Share");
290	mvprintw(MEMROW + 2, MEMCOL, "Act");
291	mvprintw(MEMROW + 3, MEMCOL, "All");
292
293	mvprintw(MEMROW + 1, MEMCOL + 41, "Free");
294
295	mvprintw(PAGEROW, PAGECOL,     "        VN PAGER  SWAP PAGER ");
296	mvprintw(PAGEROW + 1, PAGECOL, "        in  out     in  out ");
297	mvprintw(PAGEROW + 2, PAGECOL, "count");
298	mvprintw(PAGEROW + 3, PAGECOL, "pages");
299
300	mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
301	mvprintw(INTSROW + 1, INTSCOL + 9, "total");
302
303	mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "cow");
304	mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "zfod");
305	mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "wire");
306	mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "act");
307	mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "inact");
308	mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "cache");
309	mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "free");
310	mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "daefr");
311	mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "prcfr");
312	mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "react");
313	mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "pdwake");
314	mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "pdpgs");
315	mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "intrn");
316	mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "buf");
317
318	mvprintw(GENSTATROW, GENSTATCOL, "  Csw  Trp  Sys  Int  Sof  Flt");
319
320	mvprintw(GRAPHROW, GRAPHCOL,
321		"  . %%Sys    . %%Intr   . %%User   . %%Nice   . %%Idle");
322	mvprintw(PROCSROW, PROCSCOL, "Proc:r  p  d  s  w");
323	mvprintw(GRAPHROW + 1, GRAPHCOL,
324		"|    |    |    |    |    |    |    |    |    |    |");
325
326	mvprintw(NAMEIROW, NAMEICOL, "Namei         Name-cache    Dir-cache");
327	mvprintw(NAMEIROW + 1, NAMEICOL,
328		"    Calls     hits    %%     hits    %%");
329	mvprintw(DISKROW, DISKCOL, "Discs");
330	mvprintw(DISKROW + 1, DISKCOL, "seeks");
331	mvprintw(DISKROW + 2, DISKCOL, "xfers");
332	mvprintw(DISKROW + 3, DISKCOL, " blks");
333	mvprintw(DISKROW + 4, DISKCOL, " msps");
334	j = 0;
335	for (i = 0; i < dk_ndrive && j < MAXDRIVES; i++)
336		if (dk_select[i]) {
337			mvprintw(DISKROW, DISKCOL + 5 + 5 * j,
338				" %4.4s", dr_name[j]);
339			j++;
340		}
341	for (i = 0; i < nintr; i++) {
342		if (intrloc[i] == 0)
343			continue;
344		mvprintw(intrloc[i], INTSCOL + 9, "%-10.10s", intrname[i]);
345	}
346}
347
348#define X(fld)	{t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
349#define Y(fld)	{t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
350#define Z(fld)	{t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
351	if(state == TIME) s1.nchstats.fld = t;}
352#define PUTRATE(fld, l, c, w) \
353	Y(fld); \
354	putint((int)((float)s.fld/etime + 0.5), l, c, w)
355#define MAXFAIL 5
356
357static	char cpuchar[CPUSTATES] = { '=' , '+', '>', '-', ' ' };
358static	char cpuorder[CPUSTATES] = { CP_SYS, CP_INTR, CP_USER, CP_NICE,
359				     CP_IDLE };
360
361void
362showkre()
363{
364	float f1, f2;
365	int psiz, inttotal;
366	int i, l, c;
367	static int failcnt = 0;
368
369	for (i = 0; i < dk_ndrive; i++) {
370		X(dk_xfer); X(dk_seek); X(dk_wds); X(dk_time);
371	}
372	etime = 0;
373	for(i = 0; i < CPUSTATES; i++) {
374		X(time);
375		etime += s.time[i];
376	}
377	if (etime < 5.0) {	/* < 5 ticks - ignore this trash */
378		if (failcnt++ >= MAXFAIL) {
379			clear();
380			mvprintw(2, 10, "The alternate system clock has died!");
381			mvprintw(3, 10, "Reverting to ``pigs'' display.");
382			move(CMDLINE, 0);
383			refresh();
384			failcnt = 0;
385			sleep(5);
386			command("pigs");
387		}
388		return;
389	}
390	failcnt = 0;
391	etime /= hertz;
392	inttotal = 0;
393	for (i = 0; i < nintr; i++) {
394		if (s.intrcnt[i] == 0)
395			continue;
396		if (intrloc[i] == 0) {
397			if (nextintsrow == LINES)
398				continue;
399			intrloc[i] = nextintsrow++;
400			mvprintw(intrloc[i], INTSCOL + 9, "%-10.10s",
401				intrname[i]);
402		}
403		X(intrcnt);
404		l = (int)((float)s.intrcnt[i]/etime + 0.5);
405		inttotal += l;
406		putint(l, intrloc[i], INTSCOL + 2, 6);
407	}
408	putint(inttotal, INTSROW + 1, INTSCOL + 2, 6);
409	Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
410	Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes); Z(ncs_neghits);
411	s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
412	    nchtotal.ncs_miss + nchtotal.ncs_long + nchtotal.ncs_neghits;
413	if (state == TIME)
414		s1.nchcount = s.nchcount;
415
416	psiz = 0;
417	f2 = 0.0;
418	for (c = 0; c < CPUSTATES; c++) {
419		i = cpuorder[c];
420		f1 = cputime(i);
421		f2 += f1;
422		l = (int) ((f2 + 1.0) / 2.0) - psiz;
423		if (f1 > 99.9)
424			f1 = 99.9;	/* no room to display 100.0 */
425		putfloat(f1, GRAPHROW, GRAPHCOL + 10 * c, 4, 1, 0);
426		move(GRAPHROW + 2, psiz);
427		psiz += l;
428		while (l-- > 0)
429			addch(cpuchar[c]);
430	}
431
432	putint(ucount(), STATROW, STATCOL, 3);
433	putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
434	putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
435	putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
436	mvaddstr(STATROW, STATCOL + 53, buf);
437#define pgtokb(pg)	((pg) * cnt.v_page_size / 1024)
438	putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 3, 8);
439	putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 11, 8);
440	putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 19, 9);
441	putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 28, 9);
442	putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 3, 8);
443	putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 11, 8);
444	putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 19, 9);
445	putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 28, 9);
446	putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 37, 8);
447	putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
448	putint(total.t_pw, PROCSROW + 1, PROCSCOL + 6, 3);
449	putint(total.t_dw, PROCSROW + 1, PROCSCOL + 9, 3);
450	putint(total.t_sl, PROCSROW + 1, PROCSCOL + 12, 3);
451	putint(total.t_sw, PROCSROW + 1, PROCSCOL + 15, 3);
452	PUTRATE(Cnt.v_cow_faults, VMSTATROW + 0, VMSTATCOL + 3, 6);
453	PUTRATE(Cnt.v_zfod, VMSTATROW + 1, VMSTATCOL + 4, 5);
454	putint(pgtokb(cnt.v_wire_count), VMSTATROW + 2, VMSTATCOL, 9);
455	putint(pgtokb(cnt.v_active_count), VMSTATROW + 3, VMSTATCOL, 9);
456	putint(pgtokb(cnt.v_inactive_count), VMSTATROW + 4, VMSTATCOL, 9);
457	putint(pgtokb(cnt.v_cache_count), VMSTATROW + 5, VMSTATCOL, 9);
458	putint(pgtokb(cnt.v_free_count), VMSTATROW + 6, VMSTATCOL, 9);
459	PUTRATE(Cnt.v_dfree, VMSTATROW + 7, VMSTATCOL, 9);
460	PUTRATE(Cnt.v_pfree, VMSTATROW + 8, VMSTATCOL, 9);
461	PUTRATE(Cnt.v_reactivated, VMSTATROW + 9, VMSTATCOL, 9);
462	PUTRATE(Cnt.v_pdwakeups, VMSTATROW + 10, VMSTATCOL, 9);
463	PUTRATE(Cnt.v_pdpages, VMSTATROW + 11, VMSTATCOL, 9);
464	PUTRATE(Cnt.v_intrans, VMSTATROW + 12, VMSTATCOL, 9);
465	putint(s.bufspace/1024, VMSTATROW + 13, VMSTATCOL, 9);
466	PUTRATE(Cnt.v_vnodein, PAGEROW + 2, PAGECOL + 5, 5);
467	PUTRATE(Cnt.v_vnodeout, PAGEROW + 2, PAGECOL + 10, 5);
468	PUTRATE(Cnt.v_swapin, PAGEROW + 2, PAGECOL + 17, 5);
469	PUTRATE(Cnt.v_swapout, PAGEROW + 2, PAGECOL + 22, 5);
470	PUTRATE(Cnt.v_vnodepgsin, PAGEROW + 3, PAGECOL + 5, 5);
471	PUTRATE(Cnt.v_vnodepgsout, PAGEROW + 3, PAGECOL + 10, 5);
472	PUTRATE(Cnt.v_swappgsin, PAGEROW + 3, PAGECOL + 17, 5);
473	PUTRATE(Cnt.v_swappgsout, PAGEROW + 3, PAGECOL + 22, 5);
474	PUTRATE(Cnt.v_swtch, GENSTATROW + 1, GENSTATCOL, 5);
475	PUTRATE(Cnt.v_trap, GENSTATROW + 1, GENSTATCOL + 5, 5);
476	PUTRATE(Cnt.v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 5);
477	PUTRATE(Cnt.v_intr, GENSTATROW + 1, GENSTATCOL + 15, 5);
478	PUTRATE(Cnt.v_soft, GENSTATROW + 1, GENSTATCOL + 20, 5);
479	PUTRATE(Cnt.v_vm_faults, GENSTATROW + 1, GENSTATCOL + 25, 5);
480	mvprintw(DISKROW, DISKCOL + 5, "                              ");
481	for (i = 0, c = 0; i < dk_ndrive && c < MAXDRIVES; i++)
482		if (dk_select[i]) {
483			mvprintw(DISKROW, DISKCOL + 5 + 5 * c,
484				" %4.4s", dr_name[i]);
485			dinfo(i, ++c);
486		}
487	putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
488	putint((nchtotal.ncs_goodhits + nchtotal.ncs_neghits),
489	   NAMEIROW + 2, NAMEICOL + 9, 9);
490#define nz(x)	((x) ? (x) : 1)
491	putfloat((nchtotal.ncs_goodhits+nchtotal.ncs_neghits) *
492	   100.0 / nz(s.nchcount),
493	   NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
494	putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
495	putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
496	   NAMEIROW + 2, NAMEICOL + 33, 4, 0, 1);
497#undef nz
498}
499
500int
501cmdkre(cmd, args)
502	char *cmd, *args;
503{
504
505	if (prefix(cmd, "run")) {
506		copyinfo(&s2, &s1);
507		state = RUN;
508		return (1);
509	}
510	if (prefix(cmd, "boot")) {
511		state = BOOT;
512		copyinfo(&z, &s1);
513		return (1);
514	}
515	if (prefix(cmd, "time")) {
516		state = TIME;
517		return (1);
518	}
519	if (prefix(cmd, "zero")) {
520		if (state == RUN)
521			getinfo(&s1, RUN);
522		return (1);
523	}
524	return (dkcmd(cmd, args));
525}
526
527/* calculate number of users on the system */
528static int
529ucount()
530{
531	register int nusers = 0;
532
533	if (ut < 0)
534		return (0);
535	while (read(ut, &utmp, sizeof(utmp)))
536		if (utmp.ut_name[0] != '\0')
537			nusers++;
538
539	lseek(ut, 0L, L_SET);
540	return (nusers);
541}
542
543static float
544cputime(indx)
545	int indx;
546{
547	double t;
548	register int i;
549
550	t = 0;
551	for (i = 0; i < CPUSTATES; i++)
552		t += s.time[i];
553	if (t == 0.0)
554		t = 1.0;
555	return (s.time[indx] * 100.0 / t);
556}
557
558static void
559putint(n, l, c, w)
560	int n, l, c, w;
561{
562	char b[128];
563
564	move(l, c);
565	if (n == 0) {
566		while (w-- > 0)
567			addch(' ');
568		return;
569	}
570	sprintf(b, "%*d", w, n);
571	if (strlen(b) > w) {
572		while (w-- > 0)
573			addch('*');
574		return;
575	}
576	addstr(b);
577}
578
579static void
580putfloat(f, l, c, w, d, nz)
581	double f;
582	int l, c, w, d, nz;
583{
584	char b[128];
585
586	move(l, c);
587	if (nz && f == 0.0) {
588		while (--w >= 0)
589			addch(' ');
590		return;
591	}
592	sprintf(b, "%*.*f", w, d, f);
593	if (strlen(b) > w) {
594		while (--w >= 0)
595			addch('*');
596		return;
597	}
598	addstr(b);
599}
600
601static void
602getinfo(s, st)
603	struct Info *s;
604	enum state st;
605{
606	int mib[2], size;
607	extern int errno;
608
609	NREAD(X_CPTIME, s->time, sizeof s->time);
610	NREAD(X_CNT, &s->Cnt, sizeof s->Cnt);
611	NREAD(X_BUFFERSPACE, &s->bufspace, LONG);
612	NREAD(X_DK_BUSY, &s->dk_busy, LONG);
613	NREAD(X_DK_TIME, s->dk_time, dk_ndrive * LONG);
614	NREAD(X_DK_XFER, s->dk_xfer, dk_ndrive * LONG);
615	NREAD(X_DK_WDS, s->dk_wds, dk_ndrive * LONG);
616	NREAD(X_DK_SEEK, s->dk_seek, dk_ndrive * LONG);
617	NREAD(X_NCHSTATS, &s->nchstats, sizeof s->nchstats);
618	NREAD(X_INTRCNT, s->intrcnt, nintr * LONG);
619	size = sizeof(s->Total);
620	mib[0] = CTL_VM;
621	mib[1] = VM_METER;
622	if (sysctl(mib, 2, &s->Total, &size, NULL, 0) < 0) {
623		error("Can't get kernel info: %s\n", strerror(errno));
624		bzero(&s->Total, sizeof(s->Total));
625	}
626}
627
628static void
629allocinfo(s)
630	struct Info *s;
631{
632
633	s->intrcnt = (long *) calloc(nintr, sizeof(long));
634	if (s->intrcnt == NULL)
635		errx(2, "out of memory");
636}
637
638static void
639copyinfo(from, to)
640	register struct Info *from, *to;
641{
642	long *time, *wds, *seek, *xfer;
643	long *intrcnt;
644
645	/*
646	 * time, wds, seek, and xfer are malloc'd so we have to
647	 * save the pointers before the structure copy and then
648	 * copy by hand.
649	 */
650	time = to->dk_time; wds = to->dk_wds; seek = to->dk_seek;
651	xfer = to->dk_xfer; intrcnt = to->intrcnt;
652	*to = *from;
653	bcopy(from->dk_time, to->dk_time = time, dk_ndrive * sizeof (long));
654	bcopy(from->dk_wds, to->dk_wds = wds, dk_ndrive * sizeof (long));
655	bcopy(from->dk_seek, to->dk_seek = seek, dk_ndrive * sizeof (long));
656	bcopy(from->dk_xfer, to->dk_xfer = xfer, dk_ndrive * sizeof (long));
657	bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
658}
659
660static void
661dinfo(dn, c)
662	int dn, c;
663{
664	double words, atime, itime, xtime;
665
666	c = DISKCOL + c * 5;
667	atime = s.dk_time[dn];
668	atime /= hertz;
669	words = s.dk_wds[dn]*32.0;	/* number of words transferred */
670	xtime = dk_mspw[dn]*words;	/* transfer time */
671	itime = atime - xtime;		/* time not transferring */
672	if (xtime < 0)
673		itime += xtime, xtime = 0;
674	if (itime < 0)
675		xtime += itime, itime = 0;
676	putint((int)((float)s.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5);
677	putint((int)((float)s.dk_xfer[dn]/etime+0.5), DISKROW + 2, c, 5);
678	putint((int)(words/etime/512.0 + 0.5), DISKROW + 3, c, 5);
679	if (s.dk_seek[dn])
680		putfloat(itime*1000.0/s.dk_seek[dn], DISKROW + 4, c, 5, 1, 1);
681	else
682		putint(0, DISKROW + 4, c, 5);
683}
684