vmstat.c revision 5463
1/*
2 * Copyright (c) 1980, 1986, 1991, 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 copyright[] =
36"@(#) Copyright (c) 1980, 1986, 1991, 1993\n\
37	The Regents of the University of California.  All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41static char sccsid[] = "@(#)vmstat.c	8.1 (Berkeley) 6/6/93";
42#endif /* not lint */
43
44#include <sys/param.h>
45#include <sys/time.h>
46#include <sys/proc.h>
47#include <sys/user.h>
48#include <sys/dkstat.h>
49#include <sys/buf.h>
50#include <sys/namei.h>
51#include <sys/malloc.h>
52#include <sys/signal.h>
53#include <sys/fcntl.h>
54#include <sys/ioctl.h>
55#include <sys/sysctl.h>
56#include <vm/vm.h>
57#include <time.h>
58#include <nlist.h>
59#include <kvm.h>
60#include <errno.h>
61#include <unistd.h>
62#include <stdio.h>
63#include <ctype.h>
64#include <stdlib.h>
65#include <string.h>
66#include <paths.h>
67#include <limits.h>
68
69struct nlist namelist[] = {
70#define	X_CPTIME	0
71	{ "_cp_time" },
72#define	X_DK_NDRIVE	1
73	{ "_dk_ndrive" },
74#define X_SUM		2
75	{ "_cnt" },
76#define	X_BOOTTIME	3
77	{ "_boottime" },
78#define	X_DKXFER	4
79	{ "_dk_xfer" },
80#define X_HZ		5
81	{ "_hz" },
82#define X_STATHZ	6
83	{ "_stathz" },
84#define X_NCHSTATS	7
85	{ "_nchstats" },
86#define	X_INTRNAMES	8
87	{ "_intrnames" },
88#define	X_EINTRNAMES	9
89	{ "_eintrnames" },
90#define	X_INTRCNT	10
91	{ "_intrcnt" },
92#define	X_EINTRCNT	11
93	{ "_eintrcnt" },
94#define	X_KMEMSTAT	12
95	{ "_kmemstats" },
96#define	X_KMEMBUCKETS	13
97	{ "_bucket" },
98#ifdef notdef
99#define	X_DEFICIT	14
100	{ "_deficit" },
101#define	X_FORKSTAT	15
102	{ "_forkstat" },
103#define X_REC		16
104	{ "_rectime" },
105#define X_PGIN		17
106	{ "_pgintime" },
107#define	X_XSTATS	18
108	{ "_xstats" },
109#define X_END		19
110#else
111#define X_END		14
112#endif
113#if defined(hp300) || defined(luna68k)
114#define	X_HPDINIT	(X_END)
115	{ "_hp_dinit" },
116#endif
117#if defined(i386)
118#define X_DK_NAMES	(X_END)
119	{ "_dk_names" },
120#endif
121#ifdef mips
122#define	X_SCSI_DINIT	(X_END)
123	{ "_scsi_dinit" },
124#endif
125#ifdef tahoe
126#define	X_VBDINIT	(X_END)
127	{ "_vbdinit" },
128#define	X_CKEYSTATS	(X_END+1)
129	{ "_ckeystats" },
130#define	X_DKEYSTATS	(X_END+2)
131	{ "_dkeystats" },
132#endif
133#ifdef vax
134#define X_MBDINIT	(X_END)
135	{ "_mbdinit" },
136#define X_UBDINIT	(X_END+1)
137	{ "_ubdinit" },
138#endif
139	{ "" },
140};
141
142struct _disk {
143	long time[CPUSTATES];
144	long *xfer;
145} cur, last;
146
147struct	vmmeter sum, osum;
148char	**dr_name;
149int	*dr_select, dk_ndrive, ndrives;
150
151int	winlines = 20;
152
153kvm_t *kd;
154
155#define	FORKSTAT	0x01
156#define	INTRSTAT	0x02
157#define	MEMSTAT		0x04
158#define	SUMSTAT		0x08
159#define	TIMESTAT	0x10
160#define	VMSTAT		0x20
161
162#include "names.c"			/* disk names -- machine dependent */
163
164void	cpustats(), dkstats(), dointr(), domem(), dosum();
165void	dovmstat(), kread(), usage();
166#ifdef notdef
167void	dotimes(), doforkst();
168#endif
169
170main(argc, argv)
171	register int argc;
172	register char **argv;
173{
174	extern int optind;
175	extern char *optarg;
176	register int c, todo;
177	u_int interval;
178	int reps;
179	char *memf, *nlistf;
180        char errbuf[_POSIX2_LINE_MAX];
181
182	memf = nlistf = NULL;
183	interval = reps = todo = 0;
184	while ((c = getopt(argc, argv, "c:fiM:mN:stw:")) != EOF) {
185		switch (c) {
186		case 'c':
187			reps = atoi(optarg);
188			break;
189#ifndef notdef
190		case 'f':
191			todo |= FORKSTAT;
192			break;
193#endif
194		case 'i':
195			todo |= INTRSTAT;
196			break;
197		case 'M':
198			memf = optarg;
199			break;
200		case 'm':
201			todo |= MEMSTAT;
202			break;
203		case 'N':
204			nlistf = optarg;
205			break;
206		case 's':
207			todo |= SUMSTAT;
208			break;
209#ifndef notdef
210		case 't':
211			todo |= TIMESTAT;
212			break;
213#endif
214		case 'w':
215			interval = atoi(optarg);
216			break;
217		case '?':
218		default:
219			usage();
220		}
221	}
222	argc -= optind;
223	argv += optind;
224
225	if (todo == 0)
226		todo = VMSTAT;
227
228	/*
229	 * Discard setgid privileges if not the running kernel so that bad
230	 * guys can't print interesting stuff from kernel memory.
231	 */
232	if (nlistf != NULL || memf != NULL)
233		setgid(getgid());
234
235        kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
236	if (kd == 0) {
237		(void)fprintf(stderr,
238		    "vmstat: kvm_openfiles: %s\n", errbuf);
239		exit(1);
240	}
241
242	if ((c = kvm_nlist(kd, namelist)) != 0) {
243		if (c > 0) {
244			(void)fprintf(stderr,
245			    "vmstat: undefined symbols:");
246			for (c = 0;
247			    c < sizeof(namelist)/sizeof(namelist[0]); c++)
248				if (namelist[c].n_type == 0)
249					fprintf(stderr, " %s",
250					    namelist[c].n_name);
251			(void)fputc('\n', stderr);
252		} else
253			(void)fprintf(stderr, "vmstat: kvm_nlist: %s\n",
254			    kvm_geterr(kd));
255		exit(1);
256	}
257
258	if (todo & VMSTAT) {
259		char **getdrivedata();
260		struct winsize winsize;
261
262		argv = getdrivedata(argv);
263		winsize.ws_row = 0;
264		(void) ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize);
265		if (winsize.ws_row > 0)
266			winlines = winsize.ws_row;
267
268	}
269
270#define	BACKWARD_COMPATIBILITY
271#ifdef	BACKWARD_COMPATIBILITY
272	if (*argv) {
273		interval = atoi(*argv);
274		if (*++argv)
275			reps = atoi(*argv);
276	}
277#endif
278
279	if (interval) {
280		if (!reps)
281			reps = -1;
282	} else if (reps)
283		interval = 1;
284
285#ifdef notdef
286	if (todo & FORKSTAT)
287		doforkst();
288#endif
289	if (todo & MEMSTAT)
290		domem();
291	if (todo & SUMSTAT)
292		dosum();
293#ifdef notdef
294	if (todo & TIMESTAT)
295		dotimes();
296#endif
297	if (todo & INTRSTAT)
298		dointr();
299	if (todo & VMSTAT)
300		dovmstat(interval, reps);
301	exit(0);
302}
303
304char **
305getdrivedata(argv)
306	char **argv;
307{
308	register int i;
309	register char **cp;
310	char buf[30];
311
312	kread(X_DK_NDRIVE, &dk_ndrive, sizeof(dk_ndrive));
313	if (dk_ndrive < 0) {
314		(void)fprintf(stderr, "vmstat: dk_ndrive %d\n", dk_ndrive);
315		exit(1);
316	}
317	dr_select = calloc((size_t)dk_ndrive, sizeof(int));
318	dr_name = calloc((size_t)dk_ndrive, sizeof(char *));
319	for (i = 0; i < dk_ndrive; i++)
320		dr_name[i] = NULL;
321	cur.xfer = calloc((size_t)dk_ndrive, sizeof(long));
322	last.xfer = calloc((size_t)dk_ndrive, sizeof(long));
323	if (!read_names())
324		exit (1);
325	for (i = 0; i < dk_ndrive; i++)
326		if (dr_name[i] == NULL) {
327			(void)sprintf(buf, "??%d", i);
328			dr_name[i] = strdup(buf);
329		}
330
331	/*
332	 * Choose drives to be displayed.  Priority goes to (in order) drives
333	 * supplied as arguments, default drives.  If everything isn't filled
334	 * in and there are drives not taken care of, display the first few
335	 * that fit.
336	 */
337#define BACKWARD_COMPATIBILITY
338	for (ndrives = 0; *argv; ++argv) {
339#ifdef	BACKWARD_COMPATIBILITY
340		if (isdigit(**argv))
341			break;
342#endif
343		for (i = 0; i < dk_ndrive; i++) {
344			if (strcmp(dr_name[i], *argv))
345				continue;
346			dr_select[i] = 1;
347			++ndrives;
348			break;
349		}
350	}
351	for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
352		if (dr_select[i])
353			continue;
354		for (cp = defdrives; *cp; cp++)
355			if (strcmp(dr_name[i], *cp) == 0) {
356				dr_select[i] = 1;
357				++ndrives;
358				break;
359			}
360	}
361	for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
362		if (dr_select[i])
363			continue;
364		dr_select[i] = 1;
365		++ndrives;
366	}
367	return(argv);
368}
369
370long
371getuptime()
372{
373	static time_t now, boottime;
374	time_t uptime;
375
376	if (boottime == 0)
377		kread(X_BOOTTIME, &boottime, sizeof(boottime));
378	(void)time(&now);
379	uptime = now - boottime;
380	if (uptime <= 0 || uptime > 60*60*24*365*10) {
381		(void)fprintf(stderr,
382		    "vmstat: time makes no sense; namelist must be wrong.\n");
383		exit(1);
384	}
385	return(uptime);
386}
387
388int	hz, hdrcnt;
389
390void
391dovmstat(interval, reps)
392	u_int interval;
393	int reps;
394{
395	struct vmtotal total;
396	time_t uptime, halfuptime;
397	void needhdr();
398	int mib[2], size;
399
400	uptime = getuptime();
401	halfuptime = uptime / 2;
402	(void)signal(SIGCONT, needhdr);
403
404	if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0)
405		kread(X_STATHZ, &hz, sizeof(hz));
406	if (!hz)
407		kread(X_HZ, &hz, sizeof(hz));
408
409	for (hdrcnt = 1;;) {
410		if (!--hdrcnt)
411			printhdr();
412		kread(X_CPTIME, cur.time, sizeof(cur.time));
413		kread(X_DKXFER, cur.xfer, sizeof(*cur.xfer) * dk_ndrive);
414		kread(X_SUM, &sum, sizeof(sum));
415		size = sizeof(total);
416		mib[0] = CTL_VM;
417		mib[1] = VM_METER;
418		if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) {
419			printf("Can't get kerninfo: %s\n", strerror(errno));
420			bzero(&total, sizeof(total));
421		}
422		(void)printf("%2d%2d%2d",
423		    total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
424#define pgtok(a) ((a) * sum.v_page_size >> 10)
425#define	rate(x)	(((x) + halfuptime) / uptime)	/* round */
426		(void)printf("%6ld%6ld ",
427		    pgtok(total.t_avm), pgtok(total.t_free));
428		(void)printf("%4lu ", rate(sum.v_vm_faults - osum.v_vm_faults));
429		(void)printf("%3lu ",
430		    rate(sum.v_reactivated - osum.v_reactivated));
431		(void)printf("%3lu ", rate(sum.v_swapin + sum.v_vnodein -
432		    (osum.v_swapin + osum.v_vnodein)));
433		(void)printf("%3lu ", rate(sum.v_swapout + sum.v_vnodeout -
434		    (osum.v_swapout + osum.v_vnodeout)));
435		(void)printf("%3lu ", rate(sum.v_tfree - osum.v_tfree));
436		(void)printf("%3lu ", rate(sum.v_pdpages - osum.v_pdpages));
437		dkstats();
438		(void)printf("%4lu %4lu %3lu ",
439		    rate(sum.v_intr - osum.v_intr),
440		    rate(sum.v_syscall - osum.v_syscall),
441		    rate(sum.v_swtch - osum.v_swtch));
442		cpustats();
443		(void)printf("\n");
444		(void)fflush(stdout);
445		if (reps >= 0 && --reps <= 0)
446			break;
447		osum = sum;
448		uptime = interval;
449		/*
450		 * We round upward to avoid losing low-frequency events
451		 * (i.e., >= 1 per interval but < 1 per second).
452		 */
453		if (interval != 1)
454			halfuptime = (uptime + 1) / 2;
455		else
456			halfuptime = 0;
457		(void)sleep(interval);
458	}
459}
460
461printhdr()
462{
463	register int i;
464
465	(void)printf(" procs   memory     page%*s", 20, "");
466	if (ndrives > 1)
467		(void)printf("disks %*s  faults      cpu\n",
468		   ndrives * 3 - 6, "");
469	else
470		(void)printf("%*s  faults      cpu\n", ndrives * 3, "");
471	(void)printf(" r b w   avm   fre  flt  re  pi  po  fr  sr ");
472	for (i = 0; i < dk_ndrive; i++)
473		if (dr_select[i])
474			(void)printf("%c%c ", dr_name[i][0],
475			    dr_name[i][strlen(dr_name[i]) - 1]);
476	(void)printf("  in   sy  cs us sy id\n");
477	hdrcnt = winlines - 2;
478}
479
480/*
481 * Force a header to be prepended to the next output.
482 */
483void
484needhdr()
485{
486
487	hdrcnt = 1;
488}
489
490#ifdef notdef
491void
492dotimes()
493{
494	u_int pgintime, rectime;
495
496	kread(X_REC, &rectime, sizeof(rectime));
497	kread(X_PGIN, &pgintime, sizeof(pgintime));
498	kread(X_SUM, &sum, sizeof(sum));
499	(void)printf("%u reclaims, %u total time (usec)\n",
500	    sum.v_pgrec, rectime);
501	(void)printf("average: %u usec / reclaim\n", rectime / sum.v_pgrec);
502	(void)printf("\n");
503	(void)printf("%u page ins, %u total time (msec)\n",
504	    sum.v_pgin, pgintime / 10);
505	(void)printf("average: %8.1f msec / page in\n",
506	    pgintime / (sum.v_pgin * 10.0));
507}
508#endif
509
510pct(top, bot)
511	long top, bot;
512{
513	long ans;
514
515	if (bot == 0)
516		return(0);
517	ans = (quad_t)top * 100 / bot;
518	return (ans);
519}
520
521#define	PCT(top, bot) pct((long)(top), (long)(bot))
522
523#if defined(tahoe)
524#include <machine/cpu.h>
525#endif
526
527void
528dosum()
529{
530	struct nchstats nchstats;
531	long nchtotal;
532#if defined(tahoe)
533	struct keystats keystats;
534#endif
535
536	kread(X_SUM, &sum, sizeof(sum));
537	(void)printf("%9u cpu context switches\n", sum.v_swtch);
538	(void)printf("%9u device interrupts\n", sum.v_intr);
539	(void)printf("%9u software interrupts\n", sum.v_soft);
540#ifdef vax
541	(void)printf("%9u pseudo-dma dz interrupts\n", sum.v_pdma);
542#endif
543	(void)printf("%9u traps\n", sum.v_trap);
544	(void)printf("%9u system calls\n", sum.v_syscall);
545	(void)printf("%9u swap pager pageins\n", sum.v_swapin);
546	(void)printf("%9u swap pager pages paged in\n", sum.v_swappgsin);
547	(void)printf("%9u swap pager pageouts\n", sum.v_swapout);
548	(void)printf("%9u swap pager pages paged out\n", sum.v_swappgsout);
549	(void)printf("%9u vnode pager pageins\n", sum.v_vnodein);
550	(void)printf("%9u vnode pager pages paged in\n", sum.v_vnodepgsin);
551	(void)printf("%9u vnode pager pageouts\n", sum.v_vnodeout);
552	(void)printf("%9u vnode pager pages paged out\n", sum.v_vnodepgsout);
553	(void)printf("%9u VM object cache lookups\n", sum.v_lookups);
554	(void)printf("%9u VM object hits\n", sum.v_hits);
555	(void)printf("%9u page daemon wakeups\n", sum.v_pdwakeups);
556	(void)printf("%9u pages examined by the page daemon\n", sum.v_pdpages);
557	(void)printf("%9u pages reactivated\n", sum.v_reactivated);
558	(void)printf("%9u intransit blocking page faults\n", sum.v_intrans);
559	(void)printf("%9u zero fill pages allocated\n", sum.v_nzfod);
560	(void)printf("%9u zero fill pages zeroed\n", sum.v_zfod);
561	(void)printf("%9u copy-on-write faults\n", sum.v_cow_faults);
562	(void)printf("%9u total VM faults taken\n", sum.v_vm_faults);
563	(void)printf("%9u pages freed\n", sum.v_tfree);
564	(void)printf("%9u pages freed by daemon\n", sum.v_dfree);
565	(void)printf("%9u pages freed by exiting processes\n", sum.v_pfree);
566	(void)printf("%9u pages active\n", sum.v_active_count);
567	(void)printf("%9u pages inactive\n", sum.v_inactive_count);
568	(void)printf("%9u pages in VM cache\n", sum.v_cache_count);
569	(void)printf("%9u pages wired down\n", sum.v_wire_count);
570	(void)printf("%9u pages free\n", sum.v_free_count);
571	(void)printf("%9u bytes per page\n", sum.v_page_size);
572	kread(X_NCHSTATS, &nchstats, sizeof(nchstats));
573	nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
574	    nchstats.ncs_badhits + nchstats.ncs_falsehits +
575	    nchstats.ncs_miss + nchstats.ncs_long;
576	(void)printf("%9ld total name lookups\n", nchtotal);
577	(void)printf(
578	    "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n",
579	    "", PCT(nchstats.ncs_goodhits, nchtotal),
580	    PCT(nchstats.ncs_neghits, nchtotal),
581	    PCT(nchstats.ncs_pass2, nchtotal));
582	(void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
583	    PCT(nchstats.ncs_badhits, nchtotal),
584	    PCT(nchstats.ncs_falsehits, nchtotal),
585	    PCT(nchstats.ncs_long, nchtotal));
586#if defined(tahoe)
587	kread(X_CKEYSTATS, &keystats, sizeof(keystats));
588	(void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n",
589	    keystats.ks_allocs, "code cache keys allocated",
590	    PCT(keystats.ks_allocfree, keystats.ks_allocs),
591	    PCT(keystats.ks_norefs, keystats.ks_allocs),
592	    PCT(keystats.ks_taken, keystats.ks_allocs),
593	    PCT(keystats.ks_shared, keystats.ks_allocs));
594	kread(X_DKEYSTATS, &keystats, sizeof(keystats));
595	(void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n",
596	    keystats.ks_allocs, "data cache keys allocated",
597	    PCT(keystats.ks_allocfree, keystats.ks_allocs),
598	    PCT(keystats.ks_norefs, keystats.ks_allocs),
599	    PCT(keystats.ks_taken, keystats.ks_allocs),
600	    PCT(keystats.ks_shared, keystats.ks_allocs));
601#endif
602}
603
604#ifdef notdef
605void
606doforkst()
607{
608	struct forkstat fks;
609
610	kread(X_FORKSTAT, &fks, sizeof(struct forkstat));
611	(void)printf("%d forks, %d pages, average %.2f\n",
612	    fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork);
613	(void)printf("%d vforks, %d pages, average %.2f\n",
614	    fks.cntvfork, fks.sizvfork, (double)fks.sizvfork / fks.cntvfork);
615}
616#endif
617
618void
619dkstats()
620{
621	register int dn, state;
622	double etime;
623	long tmp;
624
625	for (dn = 0; dn < dk_ndrive; ++dn) {
626		tmp = cur.xfer[dn];
627		cur.xfer[dn] -= last.xfer[dn];
628		last.xfer[dn] = tmp;
629	}
630	etime = 0;
631	for (state = 0; state < CPUSTATES; ++state) {
632		tmp = cur.time[state];
633		cur.time[state] -= last.time[state];
634		last.time[state] = tmp;
635		etime += cur.time[state];
636	}
637	if (etime == 0)
638		etime = 1;
639	etime /= hz;
640	for (dn = 0; dn < dk_ndrive; ++dn) {
641		if (!dr_select[dn])
642			continue;
643		(void)printf("%2.0f ", cur.xfer[dn] / etime);
644	}
645}
646
647void
648cpustats()
649{
650	register int state;
651	double pct, total;
652
653	total = 0;
654	for (state = 0; state < CPUSTATES; ++state)
655		total += cur.time[state];
656	if (total)
657		pct = 100 / total;
658	else
659		pct = 0;
660	(void)printf("%2.0f ", (cur.time[CP_USER] + cur.time[CP_NICE]) * pct);
661	(void)printf("%2.0f ", (cur.time[CP_SYS] + cur.time[CP_INTR]) * pct);
662	(void)printf("%2.0f", cur.time[CP_IDLE] * pct);
663}
664
665void
666dointr()
667{
668	register long *intrcnt, inttotal, uptime;
669	register int nintr, inamlen;
670	register char *intrname;
671
672	uptime = getuptime();
673	nintr = namelist[X_EINTRCNT].n_value - namelist[X_INTRCNT].n_value;
674	inamlen =
675	    namelist[X_EINTRNAMES].n_value - namelist[X_INTRNAMES].n_value;
676	intrcnt = malloc((size_t)nintr);
677	intrname = malloc((size_t)inamlen);
678	if (intrcnt == NULL || intrname == NULL) {
679		(void)fprintf(stderr, "vmstat: %s.\n", strerror(errno));
680		exit(1);
681	}
682	kread(X_INTRCNT, intrcnt, (size_t)nintr);
683	kread(X_INTRNAMES, intrname, (size_t)inamlen);
684	(void)printf("interrupt      total      rate\n");
685	inttotal = 0;
686	nintr /= sizeof(long);
687	while (--nintr >= 0) {
688		if (*intrcnt)
689			(void)printf("%-12s %8ld %8ld\n", intrname,
690			    *intrcnt, *intrcnt / uptime);
691		intrname += strlen(intrname) + 1;
692		inttotal += *intrcnt++;
693	}
694	(void)printf("Total        %8ld %8ld\n", inttotal, inttotal / uptime);
695}
696
697/*
698 * These names are defined in <sys/malloc.h>.
699 */
700char *kmemnames[] = INITKMEMNAMES;
701
702void
703domem()
704{
705	register struct kmembuckets *kp;
706	register struct kmemstats *ks;
707	register int i, j;
708	int len, size, first;
709	long totuse = 0, totfree = 0, totreq = 0;
710	char *name;
711	struct kmemstats kmemstats[M_LAST];
712	struct kmembuckets buckets[MINBUCKET + 16];
713
714	kread(X_KMEMBUCKETS, buckets, sizeof(buckets));
715	(void)printf("Memory statistics by bucket size\n");
716	(void)printf(
717	    "Size   In Use   Free   Requests  HighWater  Couldfree\n");
718	for (i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16; i++, kp++) {
719		if (kp->kb_calls == 0)
720			continue;
721		size = 1 << i;
722		if(size < 1024)
723			(void)printf("%4d",size);
724		else
725			(void)printf("%3dK",size>>10);
726		(void)printf(" %8ld %6ld %10ld %7ld %10ld\n",
727			kp->kb_total - kp->kb_totalfree,
728			kp->kb_totalfree, kp->kb_calls,
729			kp->kb_highwat, kp->kb_couldfree);
730		totfree += size * kp->kb_totalfree;
731	}
732
733	kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats));
734	(void)printf("\nMemory usage type by bucket size\n");
735	(void)printf("Size  Type(s)\n");
736	kp = &buckets[MINBUCKET];
737	for (j =  1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) {
738		if (kp->kb_calls == 0)
739			continue;
740		first = 1;
741		len = 8;
742		for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
743			if (ks->ks_calls == 0)
744				continue;
745			if ((ks->ks_size & j) == 0)
746				continue;
747			name = kmemnames[i] ? kmemnames[i] : "undefined";
748			len += 2 + strlen(name);
749			if (first && j < 1024)
750				printf("%4d  %s", j, name);
751			else if (first)
752				printf("%3dK  %s", j>>10, name);
753			else
754				printf(",");
755			if (len >= 79) {
756				printf("\n\t ");
757				len = 10 + strlen(name);
758			}
759			if (!first)
760				printf(" %s", name);
761			first = 0;
762		}
763		printf("\n");
764	}
765
766	(void)printf(
767	    "\nMemory statistics by type                          Type  Kern\n");
768	(void)printf(
769"        Type  InUse MemUse HighUse  Limit Requests Limit Limit Size(s)\n");
770	for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
771		if (ks->ks_calls == 0)
772			continue;
773		(void)printf("%13s%6ld%6ldK%7ldK%6ldK%9ld%5u%6u",
774		    kmemnames[i] ? kmemnames[i] : "undefined",
775		    ks->ks_inuse, (ks->ks_memuse + 1023) / 1024,
776		    (ks->ks_maxused + 1023) / 1024,
777		    (ks->ks_limit + 1023) / 1024, ks->ks_calls,
778		    ks->ks_limblocks, ks->ks_mapblocks);
779		first = 1;
780		for (j =  1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
781			if ((ks->ks_size & j) == 0)
782				continue;
783			if (first)
784				printf("  ");
785			else
786				printf(",");
787			if(j<1024)
788				printf("%d",j);
789			else
790				printf("%dK",j>>10);
791			first = 0;
792		}
793		printf("\n");
794		totuse += ks->ks_memuse;
795		totreq += ks->ks_calls;
796	}
797	(void)printf("\nMemory Totals:  In Use    Free    Requests\n");
798	(void)printf("              %7ldK %6ldK    %8ld\n",
799	     (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
800}
801
802/*
803 * kread reads something from the kernel, given its nlist index.
804 */
805void
806kread(nlx, addr, size)
807	int nlx;
808	void *addr;
809	size_t size;
810{
811	char *sym;
812
813	if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
814		sym = namelist[nlx].n_name;
815		if (*sym == '_')
816			++sym;
817		(void)fprintf(stderr,
818		    "vmstat: symbol %s not defined\n", sym);
819		exit(1);
820	}
821	if (kvm_read(kd, namelist[nlx].n_value, addr, size) != size) {
822		sym = namelist[nlx].n_name;
823		if (*sym == '_')
824			++sym;
825		(void)fprintf(stderr, "vmstat: %s: %s\n", sym, kvm_geterr(kd));
826		exit(1);
827	}
828}
829
830void
831usage()
832{
833	(void)fprintf(stderr,
834	    "usage: vmstat [-ims] [-c count] [-M core] \
835[-N system] [-w wait] [disks]\n");
836	exit(1);
837}
838