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