vmstat.c revision 1.41 1 /* $NetBSD: vmstat.c,v 1.41 1998/02/07 15:42:32 mrg Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1986, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1991, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)vmstat.c 8.2 (Berkeley) 3/1/95";
45 #else
46 __RCSID("$NetBSD: vmstat.c,v 1.41 1998/02/07 15:42:32 mrg Exp $");
47 #endif
48 #endif /* not lint */
49
50 #include <sys/param.h>
51 #include <sys/time.h>
52 #include <sys/proc.h>
53 #include <sys/user.h>
54 #include <sys/dkstat.h>
55 #include <sys/buf.h>
56 #include <sys/namei.h>
57 #include <sys/malloc.h>
58 #include <sys/fcntl.h>
59 #include <sys/ioctl.h>
60 #include <sys/sysctl.h>
61 #include <sys/device.h>
62 #include <vm/vm.h>
63 #include <time.h>
64 #include <nlist.h>
65 #include <kvm.h>
66 #include <errno.h>
67 #include <unistd.h>
68 #include <signal.h>
69 #include <stdio.h>
70 #include <ctype.h>
71 #include <stdlib.h>
72 #include <string.h>
73 #include <paths.h>
74 #include <limits.h>
75 #include "dkstats.h"
76
77 struct nlist namelist[] = {
78 #define X_CPTIME 0
79 { "_cp_time" },
80 #define X_BOOTTIME 1
81 { "_boottime" },
82 #define X_HZ 2
83 { "_hz" },
84 #define X_STATHZ 3
85 { "_stathz" },
86 #define X_NCHSTATS 4
87 { "_nchstats" },
88 #define X_INTRNAMES 5
89 { "_intrnames" },
90 #define X_EINTRNAMES 6
91 { "_eintrnames" },
92 #define X_INTRCNT 7
93 { "_intrcnt" },
94 #define X_EINTRCNT 8
95 { "_eintrcnt" },
96 #define X_KMEMSTAT 9
97 { "_kmemstats" },
98 #define X_KMEMBUCKETS 10
99 { "_bucket" },
100 #define X_ALLEVENTS 11
101 { "_allevents" },
102 #if defined(UVM)
103 #define X_END 12
104 #else
105 #define X_SUM 12
106 { "_cnt" },
107 #define X_END 13
108 #endif
109 #if defined(pc532)
110 #define X_IVT (X_END)
111 { "_ivt" },
112 #endif
113 { "" },
114 };
115
116 /* Objects defined in dkstats.c */
117 extern struct _disk cur;
118 extern char **dr_name;
119 extern int *dk_select, dk_ndrive;
120
121 #if defined(UVM)
122 struct uvmexp uvmexp, ouvmexp;
123 #else
124 struct vmmeter sum, osum;
125 #endif
126 int ndrives;
127
128 int winlines = 20;
129
130 kvm_t *kd;
131
132 #define FORKSTAT 0x01
133 #define INTRSTAT 0x02
134 #define MEMSTAT 0x04
135 #define SUMSTAT 0x08
136 #define VMSTAT 0x20
137
138 void cpustats __P((void));
139 void dkstats __P((void));
140 void dointr __P((void));
141 void domem __P((void));
142 void dosum __P((void));
143 void dovmstat __P((u_int, int));
144 void kread __P((int, void *, size_t));
145 void needhdr __P((int));
146 long getuptime __P((void));
147 void printhdr __P((void));
148 long pct __P((long, long));
149 void usage __P((void));
150 void doforkst __P((void));
151
152 int main __P((int, char **));
153 char **choosedrives __P((char **));
154
155 extern int dkinit __P((int));
156 extern void dkreadstats __P((void));
157 extern void dkswap __P((void));
158
159 /* Namelist and memory file names. */
160 char *nlistf, *memf;
161
162 int
163 main(argc, argv)
164 int argc;
165 char **argv;
166 {
167 extern int optind;
168 extern char *optarg;
169 int c, todo;
170 u_int interval;
171 int reps;
172 char errbuf[_POSIX2_LINE_MAX];
173
174 memf = nlistf = NULL;
175 interval = reps = todo = 0;
176 while ((c = getopt(argc, argv, "c:fiM:mN:stw:")) != -1) {
177 switch (c) {
178 case 'c':
179 reps = atoi(optarg);
180 break;
181 case 'f':
182 todo |= FORKSTAT;
183 break;
184 case 'i':
185 todo |= INTRSTAT;
186 break;
187 case 'M':
188 memf = optarg;
189 break;
190 case 'm':
191 todo |= MEMSTAT;
192 break;
193 case 'N':
194 nlistf = optarg;
195 break;
196 case 's':
197 todo |= SUMSTAT;
198 break;
199 case 'w':
200 interval = atoi(optarg);
201 break;
202 case '?':
203 default:
204 usage();
205 }
206 }
207 argc -= optind;
208 argv += optind;
209
210 if (todo == 0)
211 todo = VMSTAT;
212
213 /*
214 * Discard setgid privileges if not the running kernel so that bad
215 * guys can't print interesting stuff from kernel memory.
216 */
217 if (nlistf != NULL || memf != NULL)
218 setgid(getgid());
219
220 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
221 if (kd == 0) {
222 (void)fprintf(stderr,
223 "vmstat: kvm_openfiles: %s\n", errbuf);
224 exit(1);
225 }
226
227 if ((c = kvm_nlist(kd, namelist)) != 0) {
228 if (c > 0) {
229 (void)fprintf(stderr,
230 "vmstat: undefined symbols:");
231 for (c = 0;
232 c < sizeof(namelist)/sizeof(namelist[0]); c++)
233 if (namelist[c].n_type == 0)
234 fprintf(stderr, " %s",
235 namelist[c].n_name);
236 (void)fputc('\n', stderr);
237 } else
238 (void)fprintf(stderr, "vmstat: kvm_nlist: %s\n",
239 kvm_geterr(kd));
240 exit(1);
241 }
242
243 if (todo & VMSTAT) {
244 struct winsize winsize;
245
246 dkinit(0); /* Initialize disk stats, no disks selected. */
247 argv = choosedrives(argv); /* Select disks. */
248 winsize.ws_row = 0;
249 (void) ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize);
250 if (winsize.ws_row > 0)
251 winlines = winsize.ws_row;
252
253 }
254
255 #define BACKWARD_COMPATIBILITY
256 #ifdef BACKWARD_COMPATIBILITY
257 if (*argv) {
258 interval = atoi(*argv);
259 if (*++argv)
260 reps = atoi(*argv);
261 }
262 #endif
263
264 if (interval) {
265 if (!reps)
266 reps = -1;
267 } else if (reps)
268 interval = 1;
269
270 if (todo & FORKSTAT)
271 doforkst();
272 if (todo & MEMSTAT)
273 domem();
274 if (todo & SUMSTAT)
275 dosum();
276 if (todo & INTRSTAT)
277 dointr();
278 if (todo & VMSTAT)
279 dovmstat(interval, reps);
280 exit(0);
281 }
282
283 char **
284 choosedrives(argv)
285 char **argv;
286 {
287 int i;
288
289 /*
290 * Choose drives to be displayed. Priority goes to (in order) drives
291 * supplied as arguments, default drives. If everything isn't filled
292 * in and there are drives not taken care of, display the first few
293 * that fit.
294 */
295 #define BACKWARD_COMPATIBILITY
296 for (ndrives = 0; *argv; ++argv) {
297 #ifdef BACKWARD_COMPATIBILITY
298 if (isdigit(**argv))
299 break;
300 #endif
301 for (i = 0; i < dk_ndrive; i++) {
302 if (strcmp(dr_name[i], *argv))
303 continue;
304 dk_select[i] = 1;
305 ++ndrives;
306 break;
307 }
308 }
309 for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
310 if (dk_select[i])
311 continue;
312 dk_select[i] = 1;
313 ++ndrives;
314 }
315 return(argv);
316 }
317
318 long
319 getuptime()
320 {
321 static time_t now;
322 static struct timeval boottime;
323 time_t uptime;
324
325 if (boottime.tv_sec == 0)
326 kread(X_BOOTTIME, &boottime, sizeof(boottime));
327 (void)time(&now);
328 uptime = now - boottime.tv_sec;
329 if (uptime <= 0 || uptime > 60*60*24*365*10) {
330 (void)fprintf(stderr,
331 "vmstat: time makes no sense; namelist must be wrong.\n");
332 exit(1);
333 }
334 return(uptime);
335 }
336
337 int hz, hdrcnt;
338
339 void
340 dovmstat(interval, reps)
341 u_int interval;
342 int reps;
343 {
344 struct vmtotal total;
345 time_t uptime, halfuptime;
346 int mib[2];
347 size_t size;
348 int pagesize = getpagesize();
349
350 uptime = getuptime();
351 halfuptime = uptime / 2;
352 (void)signal(SIGCONT, needhdr);
353
354 if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0)
355 kread(X_STATHZ, &hz, sizeof(hz));
356 if (!hz)
357 kread(X_HZ, &hz, sizeof(hz));
358
359 for (hdrcnt = 1;;) {
360 if (!--hdrcnt)
361 printhdr();
362 /* Read new disk statistics */
363 dkreadstats();
364 #if defined(UVM)
365 size = sizeof(uvmexp);
366 mib[0] = CTL_VM;
367 mib[1] = VM_UVMEXP;
368 if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
369 printf("can't get uvmexp: %s\n", strerror(errno));
370 bzero(&uvmexp, sizeof(uvmexp));
371 }
372 #else
373 kread(X_SUM, &sum, sizeof(sum));
374 #endif
375 size = sizeof(total);
376 mib[0] = CTL_VM;
377 mib[1] = VM_METER;
378 if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) {
379 printf("Can't get kerninfo: %s\n", strerror(errno));
380 bzero(&total, sizeof(total));
381 }
382 (void)printf("%2d%2d%2d",
383 total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
384 #define pgtok(a) (long)((a) * (pagesize >> 10))
385 #define rate(x) (u_long)(((x) + halfuptime) / uptime) /* round */
386 (void)printf("%6ld%6ld ",
387 pgtok(total.t_avm), pgtok(total.t_free));
388 #if defined(UVM)
389 /* XXX FILL ME IN! */
390 #else
391 (void)printf("%4lu ", rate(sum.v_faults - osum.v_faults));
392 (void)printf("%3lu ",
393 rate(sum.v_reactivated - osum.v_reactivated));
394 (void)printf("%3lu ", rate(sum.v_pageins - osum.v_pageins));
395 (void)printf("%3lu %3lu ",
396 rate(sum.v_pageouts - osum.v_pageouts), (u_long)0);
397 (void)printf("%3lu ", rate(sum.v_scan - osum.v_scan));
398 dkstats();
399 (void)printf("%4lu %4lu %3lu ",
400 rate(sum.v_intr - osum.v_intr),
401 rate(sum.v_syscall - osum.v_syscall),
402 rate(sum.v_swtch - osum.v_swtch));
403 cpustats();
404 (void)printf("\n");
405 (void)fflush(stdout);
406 if (reps >= 0 && --reps <= 0)
407 break;
408 osum = sum;
409 #endif
410 uptime = interval;
411 /*
412 * We round upward to avoid losing low-frequency events
413 * (i.e., >= 1 per interval but < 1 per second).
414 */
415 halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2;
416 (void)sleep(interval);
417 }
418 }
419
420 void
421 printhdr()
422 {
423 int i;
424
425 (void)printf(" procs memory page%*s", 20, "");
426 if (ndrives > 0)
427 (void)printf("%s %*sfaults cpu\n",
428 ((ndrives > 1) ? "disks" : "disk"),
429 ((ndrives > 1) ? ndrives * 3 - 4 : 0), "");
430 else
431 (void)printf("%*s faults cpu\n",
432 ndrives * 3, "");
433
434 (void)printf(" r b w avm fre flt re pi po fr sr ");
435 for (i = 0; i < dk_ndrive; i++)
436 if (dk_select[i])
437 (void)printf("%c%c ", dr_name[i][0],
438 dr_name[i][strlen(dr_name[i]) - 1]);
439 (void)printf(" in sy cs us sy id\n");
440 hdrcnt = winlines - 2;
441 }
442
443 /*
444 * Force a header to be prepended to the next output.
445 */
446 void
447 needhdr(dummy)
448 int dummy;
449 {
450
451 hdrcnt = 1;
452 }
453
454 long
455 pct(top, bot)
456 long top, bot;
457 {
458 long ans;
459
460 if (bot == 0)
461 return(0);
462 ans = (quad_t)top * 100 / bot;
463 return (ans);
464 }
465
466 #define PCT(top, bot) (int)pct((long)(top), (long)(bot))
467
468 void
469 dosum()
470 {
471 struct nchstats nchstats;
472 long nchtotal;
473
474 #if defined(UVM)
475 int mib[2];
476 size_t size;
477
478 size = sizeof(uvmexp);
479 mib[0] = CTL_VM;
480 mib[1] = VM_UVMEXP;
481 if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
482 printf("can't get uvmexp: %s\n", strerror(errno));
483 bzero(&uvmexp, sizeof(uvmexp));
484 }
485
486 /* XXX fill me in! */
487 #else
488 kread(X_SUM, &sum, sizeof(sum));
489 (void)printf("%9u cpu context switches\n", sum.v_swtch);
490 (void)printf("%9u device interrupts\n", sum.v_intr);
491 (void)printf("%9u software interrupts\n", sum.v_soft);
492 (void)printf("%9u traps\n", sum.v_trap);
493 (void)printf("%9u system calls\n", sum.v_syscall);
494 (void)printf("%9u total faults taken\n", sum.v_faults);
495 (void)printf("%9u swap ins\n", sum.v_swpin);
496 (void)printf("%9u swap outs\n", sum.v_swpout);
497 (void)printf("%9u pages swapped in\n", sum.v_pswpin / CLSIZE);
498 (void)printf("%9u pages swapped out\n", sum.v_pswpout / CLSIZE);
499 (void)printf("%9u page ins\n", sum.v_pageins);
500 (void)printf("%9u page outs\n", sum.v_pageouts);
501 (void)printf("%9u pages paged in\n", sum.v_pgpgin);
502 (void)printf("%9u pages paged out\n", sum.v_pgpgout);
503 (void)printf("%9u pages reactivated\n", sum.v_reactivated);
504 (void)printf("%9u intransit blocking page faults\n", sum.v_intrans);
505 (void)printf("%9u zero fill pages created\n", sum.v_nzfod / CLSIZE);
506 (void)printf("%9u zero fill page faults\n", sum.v_zfod / CLSIZE);
507 (void)printf("%9u pages examined by the clock daemon\n", sum.v_scan);
508 (void)printf("%9u revolutions of the clock hand\n", sum.v_rev);
509 (void)printf("%9u VM object cache lookups\n", sum.v_lookups);
510 (void)printf("%9u VM object hits\n", sum.v_hits);
511 (void)printf("%9u total VM faults taken\n", sum.v_vm_faults);
512 (void)printf("%9u copy-on-write faults\n", sum.v_cow_faults);
513 (void)printf("%9u pages freed by daemon\n", sum.v_dfree);
514 (void)printf("%9u pages freed by exiting processes\n", sum.v_pfree);
515 (void)printf("%9u pages free\n", sum.v_free_count);
516 (void)printf("%9u pages wired down\n", sum.v_wire_count);
517 (void)printf("%9u pages active\n", sum.v_active_count);
518 (void)printf("%9u pages inactive\n", sum.v_inactive_count);
519 (void)printf("%9u bytes per page\n", sum.v_page_size);
520 (void)printf("%9u target inactive pages\n", sum.v_inactive_target);
521 (void)printf("%9u target free pages\n", sum.v_free_target);
522 (void)printf("%9u minimum free pages\n", sum.v_free_min);
523 #endif
524 kread(X_NCHSTATS, &nchstats, sizeof(nchstats));
525 nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
526 nchstats.ncs_badhits + nchstats.ncs_falsehits +
527 nchstats.ncs_miss + nchstats.ncs_long;
528 (void)printf("%9ld total name lookups\n", nchtotal);
529 (void)printf(
530 "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n",
531 "", PCT(nchstats.ncs_goodhits, nchtotal),
532 PCT(nchstats.ncs_neghits, nchtotal),
533 PCT(nchstats.ncs_pass2, nchtotal));
534 (void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
535 PCT(nchstats.ncs_badhits, nchtotal),
536 PCT(nchstats.ncs_falsehits, nchtotal),
537 PCT(nchstats.ncs_long, nchtotal));
538 }
539
540 void
541 doforkst()
542 {
543 #if defined(UVM)
544 int mib[2];
545 size_t size;
546
547 size = sizeof(uvmexp);
548 mib[0] = CTL_VM;
549 mib[1] = VM_UVMEXP;
550 if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
551 printf("can't get uvmexp: %s\n", strerror(errno));
552 bzero(&uvmexp, sizeof(uvmexp));
553 }
554 (void)printf("%u forks total\n", uvmexp.forks);
555 (void)printf("%u forks blocked parent\n", uvmexp.forks_ppwait);
556 (void)printf("%u forks shared address space with parent\n",
557 uvmexp.forks_sharevm);
558 #else
559
560 kread(X_SUM, &sum, sizeof(sum));
561 (void)printf("%u forks total\n", sum.v_forks);
562 (void)printf("%u forks blocked parent\n", sum.v_forks_ppwait);
563 (void)printf("%u forks shared address space with parent\n",
564 sum.v_forks_sharevm);
565 #endif
566 }
567
568 void
569 dkstats()
570 {
571 int dn, state;
572 double etime;
573
574 /* Calculate disk stat deltas. */
575 dkswap();
576 etime = 0;
577 for (state = 0; state < CPUSTATES; ++state) {
578 etime += cur.cp_time[state];
579 }
580 if (etime == 0)
581 etime = 1;
582 etime /= hz;
583 for (dn = 0; dn < dk_ndrive; ++dn) {
584 if (!dk_select[dn])
585 continue;
586 (void)printf("%2.0f ", cur.dk_xfer[dn] / etime);
587 }
588 }
589
590 void
591 cpustats()
592 {
593 int state;
594 double pct, total;
595
596 total = 0;
597 for (state = 0; state < CPUSTATES; ++state)
598 total += cur.cp_time[state];
599 if (total)
600 pct = 100 / total;
601 else
602 pct = 0;
603 (void)printf("%2.0f ", (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pct);
604 (void)printf("%2.0f ", (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pct);
605 (void)printf("%2.0f", cur.cp_time[CP_IDLE] * pct);
606 }
607
608 #if defined(pc532)
609 /* To get struct iv ...*/
610 #define _KERNEL
611 #include <machine/psl.h>
612 #undef _KERNEL
613 void
614 dointr()
615 {
616 long i, j, inttotal, uptime;
617 static char iname[64];
618 struct iv ivt[32], *ivp = ivt;
619
620 iname[63] = '\0';
621 uptime = getuptime();
622 kread(X_IVT, ivp, sizeof(ivt));
623
624 for (i = 0; i < 2; i++) {
625 (void)printf("%sware interrupts:\n", i ? "\nsoft" : "hard");
626 (void)printf("interrupt total rate\n");
627 inttotal = 0;
628 for (j = 0; j < 16; j++, ivp++) {
629 if (ivp->iv_vec && ivp->iv_use && ivp->iv_cnt) {
630 if (kvm_read(kd, (u_long)ivp->iv_use, iname, 63) != 63) {
631 (void)fprintf(stderr, "vmstat: iv_use: %s\n",
632 kvm_geterr(kd));
633 exit(1);
634 }
635 (void)printf("%-12s %8ld %8ld\n", iname,
636 ivp->iv_cnt, ivp->iv_cnt / uptime);
637 inttotal += ivp->iv_cnt;
638 }
639 }
640 (void)printf("Total %8ld %8ld\n",
641 inttotal, inttotal / uptime);
642 }
643 }
644 #else
645 void
646 dointr()
647 {
648 long *intrcnt, inttotal, uptime;
649 int nintr, inamlen;
650 char *intrname;
651 struct evcntlist allevents;
652 struct evcnt evcnt, *evptr;
653 struct device dev;
654
655 uptime = getuptime();
656 nintr = namelist[X_EINTRCNT].n_value - namelist[X_INTRCNT].n_value;
657 inamlen =
658 namelist[X_EINTRNAMES].n_value - namelist[X_INTRNAMES].n_value;
659 intrcnt = malloc((size_t)nintr);
660 intrname = malloc((size_t)inamlen);
661 if (intrcnt == NULL || intrname == NULL) {
662 (void)fprintf(stderr, "vmstat: %s.\n", strerror(errno));
663 exit(1);
664 }
665 kread(X_INTRCNT, intrcnt, (size_t)nintr);
666 kread(X_INTRNAMES, intrname, (size_t)inamlen);
667 (void)printf("interrupt total rate\n");
668 inttotal = 0;
669 nintr /= sizeof(long);
670 while (--nintr >= 0) {
671 if (*intrcnt)
672 (void)printf("%-14s %8ld %8ld\n", intrname,
673 *intrcnt, *intrcnt / uptime);
674 intrname += strlen(intrname) + 1;
675 inttotal += *intrcnt++;
676 }
677 kread(X_ALLEVENTS, &allevents, sizeof allevents);
678 evptr = allevents.tqh_first;
679 while (evptr) {
680 if (kvm_read(kd, (long)evptr, (void *)&evcnt,
681 sizeof evcnt) != sizeof evcnt) {
682 (void)fprintf(stderr, "vmstat: event chain trashed: %s\n",
683 kvm_geterr(kd));
684 exit(1);
685 }
686 if (kvm_read(kd, (long)evcnt.ev_dev, (void *)&dev,
687 sizeof dev) != sizeof dev) {
688 (void)fprintf(stderr, "vmstat: event chain trashed: %s\n",
689 kvm_geterr(kd));
690 exit(1);
691 }
692 if (evcnt.ev_count)
693 (void)printf("%-14s %8ld %8ld\n", dev.dv_xname,
694 (long)evcnt.ev_count, evcnt.ev_count / uptime);
695 inttotal += evcnt.ev_count++;
696
697 evptr = evcnt.ev_list.tqe_next;
698 }
699 (void)printf("Total %8ld %8ld\n", inttotal, inttotal / uptime);
700 }
701 #endif
702
703 /*
704 * These names are defined in <sys/malloc.h>.
705 */
706 char *kmemnames[] = INITKMEMNAMES;
707
708 void
709 domem()
710 {
711 struct kmembuckets *kp;
712 struct kmemstats *ks;
713 int i, j;
714 int len, size, first;
715 long totuse = 0, totfree = 0, totreq = 0;
716 char *name;
717 struct kmemstats kmemstats[M_LAST];
718 struct kmembuckets buckets[MINBUCKET + 16];
719
720 kread(X_KMEMBUCKETS, buckets, sizeof(buckets));
721 for (first = 1, i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16;
722 i++, kp++) {
723 if (kp->kb_calls == 0)
724 continue;
725 if (first) {
726 (void)printf("Memory statistics by bucket size\n");
727 (void)printf(
728 " Size In Use Free Requests HighWater Couldfree\n");
729 first = 0;
730 }
731 size = 1 << i;
732 (void)printf("%8d %8ld %6ld %10ld %7ld %10ld\n", size,
733 kp->kb_total - kp->kb_totalfree,
734 kp->kb_totalfree, kp->kb_calls,
735 kp->kb_highwat, kp->kb_couldfree);
736 totfree += size * kp->kb_totalfree;
737 }
738
739 /*
740 * If kmem statistics are not being gathered by the kernel,
741 * first will still be 1.
742 */
743 if (first) {
744 printf(
745 "Kmem statistics are not being gathered by the kernel.\n");
746 return;
747 }
748
749 kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats));
750 (void)printf("\nMemory usage type by bucket size\n");
751 (void)printf(" Size Type(s)\n");
752 kp = &buckets[MINBUCKET];
753 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) {
754 if (kp->kb_calls == 0)
755 continue;
756 first = 1;
757 len = 8;
758 for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
759 if (ks->ks_calls == 0)
760 continue;
761 if ((ks->ks_size & j) == 0)
762 continue;
763 if (kmemnames[i] == 0) {
764 kmemnames[i] = malloc(10);
765 /* strlen("undef/")+3+1);*/
766 snprintf(kmemnames[i], 10, "undef/%d", i);
767 /* same 10 as above!!! */
768 }
769 name = kmemnames[i];
770 len += 2 + strlen(name);
771 if (first)
772 printf("%8d %s", j, name);
773 else
774 printf(",");
775 if (len >= 80) {
776 printf("\n\t ");
777 len = 10 + strlen(name);
778 }
779 if (!first)
780 printf(" %s", name);
781 first = 0;
782 }
783 printf("\n");
784 }
785
786 (void)printf(
787 "\nMemory statistics by type Type Kern\n");
788 (void)printf(
789 " Type InUse MemUse HighUse Limit Requests Limit Limit Size(s)\n");
790 for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
791 if (ks->ks_calls == 0)
792 continue;
793 (void)printf("%14s%6ld%6ldK%7ldK%6ldK%9ld%5u%6u",
794 kmemnames[i] ? kmemnames[i] : "undefined",
795 ks->ks_inuse, (ks->ks_memuse + 1023) / 1024,
796 (ks->ks_maxused + 1023) / 1024,
797 (ks->ks_limit + 1023) / 1024, ks->ks_calls,
798 ks->ks_limblocks, ks->ks_mapblocks);
799 first = 1;
800 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
801 if ((ks->ks_size & j) == 0)
802 continue;
803 if (first)
804 printf(" %d", j);
805 else
806 printf(",%d", j);
807 first = 0;
808 }
809 printf("\n");
810 totuse += ks->ks_memuse;
811 totreq += ks->ks_calls;
812 }
813 (void)printf("\nMemory Totals: In Use Free Requests\n");
814 (void)printf(" %7ldK %6ldK %8ld\n",
815 (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
816 }
817
818 /*
819 * kread reads something from the kernel, given its nlist index.
820 */
821 void
822 kread(nlx, addr, size)
823 int nlx;
824 void *addr;
825 size_t size;
826 {
827 char *sym;
828
829 if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
830 sym = namelist[nlx].n_name;
831 if (*sym == '_')
832 ++sym;
833 (void)fprintf(stderr,
834 "vmstat: symbol %s not defined\n", sym);
835 exit(1);
836 }
837 if (kvm_read(kd, namelist[nlx].n_value, addr, size) != size) {
838 sym = namelist[nlx].n_name;
839 if (*sym == '_')
840 ++sym;
841 (void)fprintf(stderr, "vmstat: %s: %s\n", sym, kvm_geterr(kd));
842 exit(1);
843 }
844 }
845
846 void
847 usage()
848 {
849 (void)fprintf(stderr,
850 "usage: vmstat [-ims] [-c count] [-M core] \
851 [-N system] [-w wait] [disks]\n");
852 exit(1);
853 }
854
855