vmstat.c revision 1.42 1 /* $NetBSD: vmstat.c,v 1.42 1998/02/07 16:18:14 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.42 1998/02/07 16:18:14 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 (void)printf("%4lu ", rate(uvmexp.faults - ouvmexp.faults));
390 (void)printf("%3lu ", rate(uvmexp.pdreact - ouvmexp.pdreact));
391 (void)printf("%3lu ", rate(uvmexp.pageins - ouvmexp.pageins));
392 (void)printf("%3lu %3lu ",
393 rate(uvmexp.pageouts - ouvmexp.pageouts), (u_long)0); /* XXX */
394 (void)printf("%3lu ", rate(uvmexp.pdscans - ouvmexp.pdscans));
395 dkstats();
396 (void)printf("%4lu %4lu %3lu ",
397 rate(uvmexp.intrs - ouvmexp.intrs),
398 rate(uvmexp.syscalls - ouvmexp.syscalls),
399 rate(uvmexp.swtch - ouvmexp.swtch));
400 cpustats();
401 (void)printf("\n");
402 (void)fflush(stdout);
403 if (reps >= 0 && --reps <= 0)
404 break;
405 ouvmexp = uvmexp;
406 #else
407 (void)printf("%4lu ", rate(sum.v_faults - osum.v_faults));
408 (void)printf("%3lu ",
409 rate(sum.v_reactivated - osum.v_reactivated));
410 (void)printf("%3lu ", rate(sum.v_pageins - osum.v_pageins));
411 (void)printf("%3lu %3lu ",
412 rate(sum.v_pageouts - osum.v_pageouts), (u_long)0);
413 (void)printf("%3lu ", rate(sum.v_scan - osum.v_scan));
414 dkstats();
415 (void)printf("%4lu %4lu %3lu ",
416 rate(sum.v_intr - osum.v_intr),
417 rate(sum.v_syscall - osum.v_syscall),
418 rate(sum.v_swtch - osum.v_swtch));
419 cpustats();
420 (void)printf("\n");
421 (void)fflush(stdout);
422 if (reps >= 0 && --reps <= 0)
423 break;
424 osum = sum;
425 #endif
426 uptime = interval;
427 /*
428 * We round upward to avoid losing low-frequency events
429 * (i.e., >= 1 per interval but < 1 per second).
430 */
431 halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2;
432 (void)sleep(interval);
433 }
434 }
435
436 void
437 printhdr()
438 {
439 int i;
440
441 (void)printf(" procs memory page%*s", 20, "");
442 if (ndrives > 0)
443 (void)printf("%s %*sfaults cpu\n",
444 ((ndrives > 1) ? "disks" : "disk"),
445 ((ndrives > 1) ? ndrives * 3 - 4 : 0), "");
446 else
447 (void)printf("%*s faults cpu\n",
448 ndrives * 3, "");
449
450 (void)printf(" r b w avm fre flt re pi po fr sr ");
451 for (i = 0; i < dk_ndrive; i++)
452 if (dk_select[i])
453 (void)printf("%c%c ", dr_name[i][0],
454 dr_name[i][strlen(dr_name[i]) - 1]);
455 (void)printf(" in sy cs us sy id\n");
456 hdrcnt = winlines - 2;
457 }
458
459 /*
460 * Force a header to be prepended to the next output.
461 */
462 void
463 needhdr(dummy)
464 int dummy;
465 {
466
467 hdrcnt = 1;
468 }
469
470 long
471 pct(top, bot)
472 long top, bot;
473 {
474 long ans;
475
476 if (bot == 0)
477 return(0);
478 ans = (quad_t)top * 100 / bot;
479 return (ans);
480 }
481
482 #define PCT(top, bot) (int)pct((long)(top), (long)(bot))
483
484 void
485 dosum()
486 {
487 struct nchstats nchstats;
488 long nchtotal;
489
490 #if defined(UVM)
491 int mib[2];
492 size_t size;
493
494 size = sizeof(uvmexp);
495 mib[0] = CTL_VM;
496 mib[1] = VM_UVMEXP;
497 if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
498 printf("can't get uvmexp: %s\n", strerror(errno));
499 bzero(&uvmexp, sizeof(uvmexp));
500 }
501
502 /* XXX fill me in! */
503 #else
504 kread(X_SUM, &sum, sizeof(sum));
505 (void)printf("%9u cpu context switches\n", sum.v_swtch);
506 (void)printf("%9u device interrupts\n", sum.v_intr);
507 (void)printf("%9u software interrupts\n", sum.v_soft);
508 (void)printf("%9u traps\n", sum.v_trap);
509 (void)printf("%9u system calls\n", sum.v_syscall);
510 (void)printf("%9u total faults taken\n", sum.v_faults);
511 (void)printf("%9u swap ins\n", sum.v_swpin);
512 (void)printf("%9u swap outs\n", sum.v_swpout);
513 (void)printf("%9u pages swapped in\n", sum.v_pswpin / CLSIZE);
514 (void)printf("%9u pages swapped out\n", sum.v_pswpout / CLSIZE);
515 (void)printf("%9u page ins\n", sum.v_pageins);
516 (void)printf("%9u page outs\n", sum.v_pageouts);
517 (void)printf("%9u pages paged in\n", sum.v_pgpgin);
518 (void)printf("%9u pages paged out\n", sum.v_pgpgout);
519 (void)printf("%9u pages reactivated\n", sum.v_reactivated);
520 (void)printf("%9u intransit blocking page faults\n", sum.v_intrans);
521 (void)printf("%9u zero fill pages created\n", sum.v_nzfod / CLSIZE);
522 (void)printf("%9u zero fill page faults\n", sum.v_zfod / CLSIZE);
523 (void)printf("%9u pages examined by the clock daemon\n", sum.v_scan);
524 (void)printf("%9u revolutions of the clock hand\n", sum.v_rev);
525 (void)printf("%9u VM object cache lookups\n", sum.v_lookups);
526 (void)printf("%9u VM object hits\n", sum.v_hits);
527 (void)printf("%9u total VM faults taken\n", sum.v_vm_faults);
528 (void)printf("%9u copy-on-write faults\n", sum.v_cow_faults);
529 (void)printf("%9u pages freed by daemon\n", sum.v_dfree);
530 (void)printf("%9u pages freed by exiting processes\n", sum.v_pfree);
531 (void)printf("%9u pages free\n", sum.v_free_count);
532 (void)printf("%9u pages wired down\n", sum.v_wire_count);
533 (void)printf("%9u pages active\n", sum.v_active_count);
534 (void)printf("%9u pages inactive\n", sum.v_inactive_count);
535 (void)printf("%9u bytes per page\n", sum.v_page_size);
536 (void)printf("%9u target inactive pages\n", sum.v_inactive_target);
537 (void)printf("%9u target free pages\n", sum.v_free_target);
538 (void)printf("%9u minimum free pages\n", sum.v_free_min);
539 #endif
540 kread(X_NCHSTATS, &nchstats, sizeof(nchstats));
541 nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
542 nchstats.ncs_badhits + nchstats.ncs_falsehits +
543 nchstats.ncs_miss + nchstats.ncs_long;
544 (void)printf("%9ld total name lookups\n", nchtotal);
545 (void)printf(
546 "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n",
547 "", PCT(nchstats.ncs_goodhits, nchtotal),
548 PCT(nchstats.ncs_neghits, nchtotal),
549 PCT(nchstats.ncs_pass2, nchtotal));
550 (void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
551 PCT(nchstats.ncs_badhits, nchtotal),
552 PCT(nchstats.ncs_falsehits, nchtotal),
553 PCT(nchstats.ncs_long, nchtotal));
554 }
555
556 void
557 doforkst()
558 {
559 #if defined(UVM)
560 int mib[2];
561 size_t size;
562
563 size = sizeof(uvmexp);
564 mib[0] = CTL_VM;
565 mib[1] = VM_UVMEXP;
566 if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
567 printf("can't get uvmexp: %s\n", strerror(errno));
568 bzero(&uvmexp, sizeof(uvmexp));
569 }
570 (void)printf("%u forks total\n", uvmexp.forks);
571 (void)printf("%u forks blocked parent\n", uvmexp.forks_ppwait);
572 (void)printf("%u forks shared address space with parent\n",
573 uvmexp.forks_sharevm);
574 #else
575
576 kread(X_SUM, &sum, sizeof(sum));
577 (void)printf("%u forks total\n", sum.v_forks);
578 (void)printf("%u forks blocked parent\n", sum.v_forks_ppwait);
579 (void)printf("%u forks shared address space with parent\n",
580 sum.v_forks_sharevm);
581 #endif
582 }
583
584 void
585 dkstats()
586 {
587 int dn, state;
588 double etime;
589
590 /* Calculate disk stat deltas. */
591 dkswap();
592 etime = 0;
593 for (state = 0; state < CPUSTATES; ++state) {
594 etime += cur.cp_time[state];
595 }
596 if (etime == 0)
597 etime = 1;
598 etime /= hz;
599 for (dn = 0; dn < dk_ndrive; ++dn) {
600 if (!dk_select[dn])
601 continue;
602 (void)printf("%2.0f ", cur.dk_xfer[dn] / etime);
603 }
604 }
605
606 void
607 cpustats()
608 {
609 int state;
610 double pct, total;
611
612 total = 0;
613 for (state = 0; state < CPUSTATES; ++state)
614 total += cur.cp_time[state];
615 if (total)
616 pct = 100 / total;
617 else
618 pct = 0;
619 (void)printf("%2.0f ", (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pct);
620 (void)printf("%2.0f ", (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pct);
621 (void)printf("%2.0f", cur.cp_time[CP_IDLE] * pct);
622 }
623
624 #if defined(pc532)
625 /* To get struct iv ...*/
626 #define _KERNEL
627 #include <machine/psl.h>
628 #undef _KERNEL
629 void
630 dointr()
631 {
632 long i, j, inttotal, uptime;
633 static char iname[64];
634 struct iv ivt[32], *ivp = ivt;
635
636 iname[63] = '\0';
637 uptime = getuptime();
638 kread(X_IVT, ivp, sizeof(ivt));
639
640 for (i = 0; i < 2; i++) {
641 (void)printf("%sware interrupts:\n", i ? "\nsoft" : "hard");
642 (void)printf("interrupt total rate\n");
643 inttotal = 0;
644 for (j = 0; j < 16; j++, ivp++) {
645 if (ivp->iv_vec && ivp->iv_use && ivp->iv_cnt) {
646 if (kvm_read(kd, (u_long)ivp->iv_use, iname, 63) != 63) {
647 (void)fprintf(stderr, "vmstat: iv_use: %s\n",
648 kvm_geterr(kd));
649 exit(1);
650 }
651 (void)printf("%-12s %8ld %8ld\n", iname,
652 ivp->iv_cnt, ivp->iv_cnt / uptime);
653 inttotal += ivp->iv_cnt;
654 }
655 }
656 (void)printf("Total %8ld %8ld\n",
657 inttotal, inttotal / uptime);
658 }
659 }
660 #else
661 void
662 dointr()
663 {
664 long *intrcnt, inttotal, uptime;
665 int nintr, inamlen;
666 char *intrname;
667 struct evcntlist allevents;
668 struct evcnt evcnt, *evptr;
669 struct device dev;
670
671 uptime = getuptime();
672 nintr = namelist[X_EINTRCNT].n_value - namelist[X_INTRCNT].n_value;
673 inamlen =
674 namelist[X_EINTRNAMES].n_value - namelist[X_INTRNAMES].n_value;
675 intrcnt = malloc((size_t)nintr);
676 intrname = malloc((size_t)inamlen);
677 if (intrcnt == NULL || intrname == NULL) {
678 (void)fprintf(stderr, "vmstat: %s.\n", strerror(errno));
679 exit(1);
680 }
681 kread(X_INTRCNT, intrcnt, (size_t)nintr);
682 kread(X_INTRNAMES, intrname, (size_t)inamlen);
683 (void)printf("interrupt total rate\n");
684 inttotal = 0;
685 nintr /= sizeof(long);
686 while (--nintr >= 0) {
687 if (*intrcnt)
688 (void)printf("%-14s %8ld %8ld\n", intrname,
689 *intrcnt, *intrcnt / uptime);
690 intrname += strlen(intrname) + 1;
691 inttotal += *intrcnt++;
692 }
693 kread(X_ALLEVENTS, &allevents, sizeof allevents);
694 evptr = allevents.tqh_first;
695 while (evptr) {
696 if (kvm_read(kd, (long)evptr, (void *)&evcnt,
697 sizeof evcnt) != sizeof evcnt) {
698 (void)fprintf(stderr, "vmstat: event chain trashed: %s\n",
699 kvm_geterr(kd));
700 exit(1);
701 }
702 if (kvm_read(kd, (long)evcnt.ev_dev, (void *)&dev,
703 sizeof dev) != sizeof dev) {
704 (void)fprintf(stderr, "vmstat: event chain trashed: %s\n",
705 kvm_geterr(kd));
706 exit(1);
707 }
708 if (evcnt.ev_count)
709 (void)printf("%-14s %8ld %8ld\n", dev.dv_xname,
710 (long)evcnt.ev_count, evcnt.ev_count / uptime);
711 inttotal += evcnt.ev_count++;
712
713 evptr = evcnt.ev_list.tqe_next;
714 }
715 (void)printf("Total %8ld %8ld\n", inttotal, inttotal / uptime);
716 }
717 #endif
718
719 /*
720 * These names are defined in <sys/malloc.h>.
721 */
722 char *kmemnames[] = INITKMEMNAMES;
723
724 void
725 domem()
726 {
727 struct kmembuckets *kp;
728 struct kmemstats *ks;
729 int i, j;
730 int len, size, first;
731 long totuse = 0, totfree = 0, totreq = 0;
732 char *name;
733 struct kmemstats kmemstats[M_LAST];
734 struct kmembuckets buckets[MINBUCKET + 16];
735
736 kread(X_KMEMBUCKETS, buckets, sizeof(buckets));
737 for (first = 1, i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16;
738 i++, kp++) {
739 if (kp->kb_calls == 0)
740 continue;
741 if (first) {
742 (void)printf("Memory statistics by bucket size\n");
743 (void)printf(
744 " Size In Use Free Requests HighWater Couldfree\n");
745 first = 0;
746 }
747 size = 1 << i;
748 (void)printf("%8d %8ld %6ld %10ld %7ld %10ld\n", size,
749 kp->kb_total - kp->kb_totalfree,
750 kp->kb_totalfree, kp->kb_calls,
751 kp->kb_highwat, kp->kb_couldfree);
752 totfree += size * kp->kb_totalfree;
753 }
754
755 /*
756 * If kmem statistics are not being gathered by the kernel,
757 * first will still be 1.
758 */
759 if (first) {
760 printf(
761 "Kmem statistics are not being gathered by the kernel.\n");
762 return;
763 }
764
765 kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats));
766 (void)printf("\nMemory usage type by bucket size\n");
767 (void)printf(" Size Type(s)\n");
768 kp = &buckets[MINBUCKET];
769 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) {
770 if (kp->kb_calls == 0)
771 continue;
772 first = 1;
773 len = 8;
774 for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
775 if (ks->ks_calls == 0)
776 continue;
777 if ((ks->ks_size & j) == 0)
778 continue;
779 if (kmemnames[i] == 0) {
780 kmemnames[i] = malloc(10);
781 /* strlen("undef/")+3+1);*/
782 snprintf(kmemnames[i], 10, "undef/%d", i);
783 /* same 10 as above!!! */
784 }
785 name = kmemnames[i];
786 len += 2 + strlen(name);
787 if (first)
788 printf("%8d %s", j, name);
789 else
790 printf(",");
791 if (len >= 80) {
792 printf("\n\t ");
793 len = 10 + strlen(name);
794 }
795 if (!first)
796 printf(" %s", name);
797 first = 0;
798 }
799 printf("\n");
800 }
801
802 (void)printf(
803 "\nMemory statistics by type Type Kern\n");
804 (void)printf(
805 " Type InUse MemUse HighUse Limit Requests Limit Limit Size(s)\n");
806 for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
807 if (ks->ks_calls == 0)
808 continue;
809 (void)printf("%14s%6ld%6ldK%7ldK%6ldK%9ld%5u%6u",
810 kmemnames[i] ? kmemnames[i] : "undefined",
811 ks->ks_inuse, (ks->ks_memuse + 1023) / 1024,
812 (ks->ks_maxused + 1023) / 1024,
813 (ks->ks_limit + 1023) / 1024, ks->ks_calls,
814 ks->ks_limblocks, ks->ks_mapblocks);
815 first = 1;
816 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
817 if ((ks->ks_size & j) == 0)
818 continue;
819 if (first)
820 printf(" %d", j);
821 else
822 printf(",%d", j);
823 first = 0;
824 }
825 printf("\n");
826 totuse += ks->ks_memuse;
827 totreq += ks->ks_calls;
828 }
829 (void)printf("\nMemory Totals: In Use Free Requests\n");
830 (void)printf(" %7ldK %6ldK %8ld\n",
831 (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
832 }
833
834 /*
835 * kread reads something from the kernel, given its nlist index.
836 */
837 void
838 kread(nlx, addr, size)
839 int nlx;
840 void *addr;
841 size_t size;
842 {
843 char *sym;
844
845 if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
846 sym = namelist[nlx].n_name;
847 if (*sym == '_')
848 ++sym;
849 (void)fprintf(stderr,
850 "vmstat: symbol %s not defined\n", sym);
851 exit(1);
852 }
853 if (kvm_read(kd, namelist[nlx].n_value, addr, size) != size) {
854 sym = namelist[nlx].n_name;
855 if (*sym == '_')
856 ++sym;
857 (void)fprintf(stderr, "vmstat: %s: %s\n", sym, kvm_geterr(kd));
858 exit(1);
859 }
860 }
861
862 void
863 usage()
864 {
865 (void)fprintf(stderr,
866 "usage: vmstat [-ims] [-c count] [-M core] \
867 [-N system] [-w wait] [disks]\n");
868 exit(1);
869 }
870
871