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