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