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