vmstat.c revision 1.136 1 /* $NetBSD: vmstat.c,v 1.136 2005/08/07 12:23:20 blymn Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2000, 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation by:
8 * - Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 * - Simon Burge and Luke Mewburn of Wasabi Systems, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41 /*
42 * Copyright (c) 1980, 1986, 1991, 1993
43 * The Regents of the University of California. All rights reserved.
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
47 * are met:
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. Neither the name of the University nor the names of its contributors
54 * may be used to endorse or promote products derived from this software
55 * without specific prior written permission.
56 *
57 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 * SUCH DAMAGE.
68 */
69
70 #include <sys/cdefs.h>
71 #ifndef lint
72 __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1991, 1993\n\
73 The Regents of the University of California. All rights reserved.\n");
74 #endif /* not lint */
75
76 #ifndef lint
77 #if 0
78 static char sccsid[] = "@(#)vmstat.c 8.2 (Berkeley) 3/1/95";
79 #else
80 __RCSID("$NetBSD: vmstat.c,v 1.136 2005/08/07 12:23:20 blymn Exp $");
81 #endif
82 #endif /* not lint */
83
84 #define __POOL_EXPOSE
85
86 #include <sys/param.h>
87 #include <sys/mount.h>
88 #include <sys/uio.h>
89
90 #include <sys/buf.h>
91 #include <sys/device.h>
92 #include <sys/ioctl.h>
93 #include <sys/malloc.h>
94 #include <sys/mallocvar.h>
95 #include <sys/namei.h>
96 #include <sys/pool.h>
97 #include <sys/proc.h>
98 #include <sys/sched.h>
99 #include <sys/socket.h>
100 #include <sys/sysctl.h>
101 #include <sys/time.h>
102 #include <sys/user.h>
103
104 #include <uvm/uvm_extern.h>
105 #include <uvm/uvm_stat.h>
106
107 #include <net/if.h>
108 #include <netinet/in.h>
109 #include <netinet/in_var.h>
110
111 #include <ufs/ufs/inode.h>
112
113 #include <nfs/rpcv2.h>
114 #include <nfs/nfsproto.h>
115 #include <nfs/nfsnode.h>
116
117 #include <ctype.h>
118 #include <err.h>
119 #include <errno.h>
120 #include <fcntl.h>
121 #include <kvm.h>
122 #include <limits.h>
123 #include <nlist.h>
124 #undef n_hash
125 #include <paths.h>
126 #include <signal.h>
127 #include <stdio.h>
128 #include <stddef.h>
129 #include <stdlib.h>
130 #include <string.h>
131 #include <time.h>
132 #include <unistd.h>
133 #include <util.h>
134
135 #include "dkstats.h"
136 #include "tpstats.h"
137
138 /*
139 * General namelist
140 */
141 struct nlist namelist[] =
142 {
143 #define X_BOOTTIME 0
144 { "_boottime" },
145 #define X_HZ 1
146 { "_hz" },
147 #define X_STATHZ 2
148 { "_stathz" },
149 #define X_NCHSTATS 3
150 { "_nchstats" },
151 #define X_KMEMSTAT 4
152 { "_kmemstatistics" },
153 #define X_KMEMBUCKETS 5
154 { "_kmembuckets" },
155 #define X_ALLEVENTS 6
156 { "_allevents" },
157 #define X_POOLHEAD 7
158 { "_pool_head" },
159 #define X_UVMEXP 8
160 { "_uvmexp" },
161 #define X_TIME 9
162 { "_time" },
163 #define X_NL_SIZE 10
164 { NULL },
165 };
166
167 /*
168 * Namelist for pre-evcnt interrupt counters.
169 */
170 struct nlist intrnl[] =
171 {
172 #define X_INTRNAMES 0
173 { "_intrnames" },
174 #define X_EINTRNAMES 1
175 { "_eintrnames" },
176 #define X_INTRCNT 2
177 { "_intrcnt" },
178 #define X_EINTRCNT 3
179 { "_eintrcnt" },
180 #define X_INTRNL_SIZE 4
181 { NULL },
182 };
183
184
185 /*
186 * Namelist for hash statistics
187 */
188 struct nlist hashnl[] =
189 {
190 #define X_NFSNODE 0
191 { "_nfsnodehash" },
192 #define X_NFSNODETBL 1
193 { "_nfsnodehashtbl" },
194 #define X_IHASH 2
195 { "_ihash" },
196 #define X_IHASHTBL 3
197 { "_ihashtbl" },
198 #define X_BUFHASH 4
199 { "_bufhash" },
200 #define X_BUFHASHTBL 5
201 { "_bufhashtbl" },
202 #define X_UIHASH 6
203 { "_uihash" },
204 #define X_UIHASHTBL 7
205 { "_uihashtbl" },
206 #define X_IFADDRHASH 8
207 { "_in_ifaddrhash" },
208 #define X_IFADDRHASHTBL 9
209 { "_in_ifaddrhashtbl" },
210 #define X_NCHASH 10
211 { "_nchash" },
212 #define X_NCHASHTBL 11
213 { "_nchashtbl" },
214 #define X_NCVHASH 12
215 { "_ncvhash" },
216 #define X_NCVHASHTBL 13
217 { "_ncvhashtbl" },
218 #define X_HASHNL_SIZE 14 /* must be last */
219 { NULL },
220 };
221
222 /*
223 * Namelist for UVM histories
224 */
225 struct nlist histnl[] =
226 {
227 { "_uvm_histories" },
228 #define X_UVM_HISTORIES 0
229 { NULL },
230 };
231
232
233
234 struct uvmexp uvmexp, ouvmexp;
235 int ndrives;
236
237 int winlines = 20;
238
239 kvm_t *kd;
240
241 #define FORKSTAT 1<<0
242 #define INTRSTAT 1<<1
243 #define MEMSTAT 1<<2
244 #define SUMSTAT 1<<3
245 #define EVCNTSTAT 1<<4
246 #define VMSTAT 1<<5
247 #define HISTLIST 1<<6
248 #define HISTDUMP 1<<7
249 #define HASHSTAT 1<<8
250 #define HASHLIST 1<<9
251 #define VMTOTAL 1<<10
252
253 void cpustats(void);
254 void deref_kptr(const void *, void *, size_t, const char *);
255 void dkstats(void);
256 void tpstats(void);
257 void doevcnt(int verbose);
258 void dohashstat(int, int, const char *);
259 void dointr(int verbose);
260 void domem(void);
261 void dopool(int, int);
262 void dopoolcache(struct pool *, int);
263 void dosum(void);
264 void dovmstat(struct timespec *, int);
265 void print_total_hdr(void);
266 void dovmtotal(struct timespec *, int);
267 void kread(struct nlist *, int, void *, size_t);
268 void needhdr(int);
269 long getuptime(void);
270 void printhdr(void);
271 long pct(long, long);
272 void usage(void);
273 void doforkst(void);
274
275 void hist_traverse(int, const char *);
276 void hist_dodump(struct uvm_history *);
277
278 int main(int, char **);
279 char **choosedrives(char **);
280
281 /* Namelist and memory file names. */
282 char *nlistf, *memf;
283
284 /* allow old usage [vmstat 1] */
285 #define BACKWARD_COMPATIBILITY
286
287 int
288 main(int argc, char *argv[])
289 {
290 int c, todo, verbose, wide;
291 struct timespec interval;
292 int reps;
293 char errbuf[_POSIX2_LINE_MAX];
294 gid_t egid = getegid();
295 const char *histname, *hashname;
296
297 histname = hashname = NULL;
298 (void)setegid(getgid());
299 memf = nlistf = NULL;
300 reps = todo = verbose = wide = 0;
301 interval.tv_sec = 0;
302 interval.tv_nsec = 0;
303 while ((c = getopt(argc, argv, "c:efh:HilLM:mN:stu:UvWw:")) != -1) {
304 switch (c) {
305 case 'c':
306 reps = atoi(optarg);
307 break;
308 case 'e':
309 todo |= EVCNTSTAT;
310 break;
311 case 'f':
312 todo |= FORKSTAT;
313 break;
314 case 'h':
315 hashname = optarg;
316 /* FALLTHROUGH */
317 case 'H':
318 todo |= HASHSTAT;
319 break;
320 case 'i':
321 todo |= INTRSTAT;
322 break;
323 case 'l':
324 todo |= HISTLIST;
325 break;
326 case 'L':
327 todo |= HASHLIST;
328 break;
329 case 'M':
330 memf = optarg;
331 break;
332 case 'm':
333 todo |= MEMSTAT;
334 break;
335 case 'N':
336 nlistf = optarg;
337 break;
338 case 's':
339 todo |= SUMSTAT;
340 break;
341 case 't':
342 todo |= VMTOTAL;
343 break;
344 case 'u':
345 histname = optarg;
346 /* FALLTHROUGH */
347 case 'U':
348 todo |= HISTDUMP;
349 break;
350 case 'v':
351 verbose++;
352 break;
353 case 'W':
354 wide++;
355 break;
356 case 'w':
357 interval.tv_sec = atol(optarg);
358 break;
359 case '?':
360 default:
361 usage();
362 }
363 }
364 argc -= optind;
365 argv += optind;
366
367 if (todo == 0)
368 todo = VMSTAT;
369
370 /*
371 * Discard setgid privileges. If not the running kernel, we toss
372 * them away totally so that bad guys can't print interesting stuff
373 * from kernel memory, otherwise switch back to kmem for the
374 * duration of the kvm_openfiles() call.
375 */
376 if (nlistf != NULL || memf != NULL)
377 (void)setgid(getgid());
378 else
379 (void)setegid(egid);
380
381 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
382 if (kd == NULL)
383 errx(1, "kvm_openfiles: %s", errbuf);
384
385 if (nlistf == NULL && memf == NULL)
386 (void)setgid(getgid());
387
388 if ((c = kvm_nlist(kd, namelist)) != 0) {
389 if (c == -1)
390 errx(1, "kvm_nlist: %s %s", "namelist", kvm_geterr(kd));
391 (void)fprintf(stderr, "vmstat: undefined symbols:");
392 for (c = 0; c < sizeof(namelist) / sizeof(namelist[0])-1; c++)
393 if (namelist[c].n_type == 0)
394 fprintf(stderr, " %s", namelist[c].n_name);
395 (void)fputc('\n', stderr);
396 exit(1);
397 }
398 if (todo & INTRSTAT)
399 (void) kvm_nlist(kd, intrnl);
400 if ((c = kvm_nlist(kd, hashnl)) == -1 || c == X_HASHNL_SIZE)
401 errx(1, "kvm_nlist: %s %s", "hashnl", kvm_geterr(kd));
402 if (kvm_nlist(kd, histnl) == -1)
403 errx(1, "kvm_nlist: %s %s", "histnl", kvm_geterr(kd));
404
405 if (todo & VMSTAT) {
406 struct winsize winsize;
407
408 dkinit(0); /* Initialize disk stats, no disks selected. */
409 tpinit(0);
410
411 (void)setgid(getgid()); /* don't need privs anymore */
412
413 argv = choosedrives(argv); /* Select disks. */
414 winsize.ws_row = 0;
415 (void)ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize);
416 if (winsize.ws_row > 0)
417 winlines = winsize.ws_row;
418
419 }
420
421 #ifdef BACKWARD_COMPATIBILITY
422 if (*argv) {
423 interval.tv_sec = atol(*argv);
424 if (*++argv)
425 reps = atoi(*argv);
426 }
427 #endif
428
429 if (interval.tv_sec) {
430 if (!reps)
431 reps = -1;
432 } else if (reps)
433 interval.tv_sec = 1;
434
435
436 /*
437 * Statistics dumping is incompatible with the default
438 * VMSTAT/dovmstat() output. So perform the interval/reps handling
439 * for it here.
440 */
441 if ((todo & (VMSTAT|VMTOTAL)) == 0) {
442 for (;;) {
443 if (todo & (HISTLIST|HISTDUMP)) {
444 if ((todo & (HISTLIST|HISTDUMP)) ==
445 (HISTLIST|HISTDUMP))
446 errx(1, "you may list or dump,"
447 " but not both!");
448 hist_traverse(todo, histname);
449 putchar('\n');
450 }
451 if (todo & FORKSTAT) {
452 doforkst();
453 putchar('\n');
454 }
455 if (todo & MEMSTAT) {
456 domem();
457 dopool(verbose, wide);
458 putchar('\n');
459 }
460 if (todo & SUMSTAT) {
461 dosum();
462 putchar('\n');
463 }
464 if (todo & INTRSTAT) {
465 dointr(verbose);
466 putchar('\n');
467 }
468 if (todo & EVCNTSTAT) {
469 doevcnt(verbose);
470 putchar('\n');
471 }
472 if (todo & (HASHLIST|HASHSTAT)) {
473 if ((todo & (HASHLIST|HASHSTAT)) ==
474 (HASHLIST|HASHSTAT))
475 errx(1, "you may list or display,"
476 " but not both!");
477 dohashstat(verbose, todo, hashname);
478 putchar('\n');
479 }
480
481 if (reps >= 0 && --reps <=0)
482 break;
483 nanosleep(&interval, NULL);
484 }
485 } else {
486 if ((todo & (VMSTAT|VMTOTAL)) == (VMSTAT|VMTOTAL)) {
487 errx(1, "you may not both do vmstat and vmtotal");
488 }
489 if (todo & VMSTAT)
490 dovmstat(&interval, reps);
491 if (todo & VMTOTAL)
492 dovmtotal(&interval, reps);
493 }
494 exit(0);
495 }
496
497 char **
498 choosedrives(char **argv)
499 {
500 int i;
501
502 /*
503 * Choose drives to be displayed. Priority goes to (in order) drives
504 * supplied as arguments, default drives. If everything isn't filled
505 * in and there are drives not taken care of, display the first few
506 * that fit.
507 */
508 #define BACKWARD_COMPATIBILITY
509 for (ndrives = 0; *argv; ++argv) {
510 #ifdef BACKWARD_COMPATIBILITY
511 if (isdigit((unsigned char)**argv))
512 break;
513 #endif
514 for (i = 0; i < dk_ndrive; i++) {
515 if (strcmp(dr_name[i], *argv))
516 continue;
517 dk_select[i] = 1;
518 ++ndrives;
519 break;
520 }
521 for (i = 0; i < tp_ndrive; i++) {
522 if (strcmp(tp_name[i], *argv))
523 continue;
524 tp_select[i] = 1;
525 ++ndrives;
526 break;
527 }
528 }
529 for (i = 0; i < dk_ndrive && ndrives < 3; i++) {
530 if (dk_select[i])
531 continue;
532 dk_select[i] = 1;
533 ++ndrives;
534 }
535
536 for (i = 0; i < tp_ndrive && ndrives < 3; i++) {
537 if (tp_select[i])
538 continue;
539 tp_select[i] = 1;
540 ++ndrives;
541 }
542
543 return (argv);
544 }
545
546 long
547 getuptime(void)
548 {
549 static struct timeval boottime;
550 struct timeval now, diff;
551 time_t uptime;
552
553 if (boottime.tv_sec == 0)
554 kread(namelist, X_BOOTTIME, &boottime, sizeof(boottime));
555 kread(namelist, X_TIME, &now, sizeof(now));
556 timersub(&now, &boottime, &diff);
557 uptime = diff.tv_sec;
558 if (uptime <= 0 || uptime > 60*60*24*365*10)
559 errx(1, "time makes no sense; namelist must be wrong.");
560 return (uptime);
561 }
562
563 int hz, hdrcnt;
564
565 void
566 print_total_hdr()
567 {
568
569 (void)printf("procs memory\n");
570 (void)printf("ru dw pw sl sw");
571 (void)printf(" total-v active-v active-r");
572 (void)printf(" vm-sh avm-sh rm-sh arm-sh free\n");
573 hdrcnt = winlines - 2;
574 }
575
576 void
577 dovmtotal(struct timespec *interval, int reps)
578 {
579 struct vmtotal total;
580 int mib[2];
581 size_t size;
582
583 (void)signal(SIGCONT, needhdr);
584
585 for (hdrcnt = 1;;) {
586 if (!--hdrcnt)
587 print_total_hdr();
588 if (memf != NULL) {
589 printf("Unable to get vmtotals from crash dump.\n");
590 memset(&total, 0, sizeof(total));
591 } else {
592 size = sizeof(total);
593 mib[0] = CTL_VM;
594 mib[1] = VM_METER;
595 if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) {
596 printf("Can't get vmtotals: %s\n",
597 strerror(errno));
598 memset(&total, 0, sizeof(total));
599 }
600 }
601 printf("%2d ", total.t_rq);
602 printf("%2d ", total.t_dw);
603 printf("%2d ", total.t_pw);
604 printf("%2d ", total.t_sl);
605 printf("%2d ", total.t_sw);
606
607 printf("%9d ", total.t_vm);
608 printf("%9d ", total.t_avm);
609 printf("%9d ", total.t_arm);
610 printf("%5d ", total.t_vmshr);
611 printf("%6d ", total.t_avmshr);
612 printf("%5d ", total.t_rmshr);
613 printf("%6d ", total.t_armshr);
614 printf("%5d", total.t_free);
615
616 putchar('\n');
617
618 (void)fflush(stdout);
619 if (reps >= 0 && --reps <= 0)
620 break;
621
622 nanosleep(interval, NULL);
623 }
624 }
625
626 void
627 dovmstat(struct timespec *interval, int reps)
628 {
629 struct vmtotal total;
630 time_t uptime, halfuptime;
631 int mib[2];
632 size_t size;
633 int pagesize = getpagesize();
634
635 uptime = getuptime();
636 halfuptime = uptime / 2;
637 (void)signal(SIGCONT, needhdr);
638
639 if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0)
640 kread(namelist, X_STATHZ, &hz, sizeof(hz));
641 if (!hz)
642 kread(namelist, X_HZ, &hz, sizeof(hz));
643
644 for (hdrcnt = 1;;) {
645 if (!--hdrcnt)
646 printhdr();
647 /* Read new disk statistics */
648 dkreadstats();
649 kread(namelist, X_UVMEXP, &uvmexp, sizeof(uvmexp));
650 if (memf != NULL) {
651 /*
652 * XXX Can't do this if we're reading a crash
653 * XXX dump because they're lazily-calculated.
654 */
655 printf("Unable to get vmtotals from crash dump.\n");
656 memset(&total, 0, sizeof(total));
657 } else {
658 size = sizeof(total);
659 mib[0] = CTL_VM;
660 mib[1] = VM_METER;
661 if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) {
662 printf("Can't get vmtotals: %s\n",
663 strerror(errno));
664 memset(&total, 0, sizeof(total));
665 }
666 }
667 (void)printf("%2d %d %d",
668 total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
669 #define pgtok(a) (long)((a) * (pagesize >> 10))
670 #define rate(x) (u_long)(((x) + halfuptime) / uptime) /* round */
671 (void)printf(" %6ld %6ld ",
672 pgtok(total.t_avm), pgtok(total.t_free));
673 (void)printf("%4lu ", rate(uvmexp.faults - ouvmexp.faults));
674 (void)printf("%3lu ", rate(uvmexp.pdreact - ouvmexp.pdreact));
675 (void)printf("%3lu ", rate(uvmexp.pageins - ouvmexp.pageins));
676 (void)printf("%4lu ",
677 rate(uvmexp.pgswapout - ouvmexp.pgswapout));
678 (void)printf("%4lu ", rate(uvmexp.pdfreed - ouvmexp.pdfreed));
679 (void)printf("%4lu ", rate(uvmexp.pdscans - ouvmexp.pdscans));
680 dkstats();
681 tpstats();
682 (void)printf("%4lu %4lu %3lu ",
683 rate(uvmexp.intrs - ouvmexp.intrs),
684 rate(uvmexp.syscalls - ouvmexp.syscalls),
685 rate(uvmexp.swtch - ouvmexp.swtch));
686 cpustats();
687 putchar('\n');
688 (void)fflush(stdout);
689 if (reps >= 0 && --reps <= 0)
690 break;
691 ouvmexp = uvmexp;
692 uptime = interval->tv_sec;
693 /*
694 * We round upward to avoid losing low-frequency events
695 * (i.e., >= 1 per interval but < 1 per second).
696 */
697 halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2;
698 nanosleep(interval, NULL);
699 }
700 }
701
702 void
703 printhdr(void)
704 {
705 int i;
706
707 (void)printf(" procs memory page%*s", 23, "");
708 if (ndrives > 0)
709 (void)printf("%s %*sfaults cpu\n",
710 ((ndrives > 1) ? "disks" : "disk"),
711 ((ndrives > 1) ? ndrives * 3 - 4 : 0), "");
712 else
713 (void)printf("%*s faults cpu\n",
714 ndrives * 3, "");
715
716 (void)printf(" r b w avm fre flt re pi po fr sr ");
717 for (i = 0; i < dk_ndrive; i++)
718 if (dk_select[i])
719 (void)printf("%c%c ", dr_name[i][0],
720 dr_name[i][strlen(dr_name[i]) - 1]);
721 (void)printf(" in sy cs us sy id\n");
722 hdrcnt = winlines - 2;
723 }
724
725 /*
726 * Force a header to be prepended to the next output.
727 */
728 void
729 needhdr(int dummy)
730 {
731
732 hdrcnt = 1;
733 }
734
735 long
736 pct(long top, long bot)
737 {
738 long ans;
739
740 if (bot == 0)
741 return (0);
742 ans = (quad_t)top * 100 / bot;
743 return (ans);
744 }
745
746 #define PCT(top, bot) (int)pct((long)(top), (long)(bot))
747
748 void
749 dosum(void)
750 {
751 struct nchstats nchstats;
752 u_long nchtotal;
753
754 kread(namelist, X_UVMEXP, &uvmexp, sizeof(uvmexp));
755
756 (void)printf("%9u bytes per page\n", uvmexp.pagesize);
757
758 (void)printf("%9u page color%s\n",
759 uvmexp.ncolors, uvmexp.ncolors == 1 ? "" : "s");
760
761 (void)printf("%9u pages managed\n", uvmexp.npages);
762 (void)printf("%9u pages free\n", uvmexp.free);
763 (void)printf("%9u pages active\n", uvmexp.active);
764 (void)printf("%9u pages inactive\n", uvmexp.inactive);
765 (void)printf("%9u pages paging\n", uvmexp.paging);
766 (void)printf("%9u pages wired\n", uvmexp.wired);
767 (void)printf("%9u zero pages\n", uvmexp.zeropages);
768 (void)printf("%9u reserve pagedaemon pages\n",
769 uvmexp.reserve_pagedaemon);
770 (void)printf("%9u reserve kernel pages\n", uvmexp.reserve_kernel);
771 (void)printf("%9u anonymous pages\n", uvmexp.anonpages);
772 (void)printf("%9u cached file pages\n", uvmexp.filepages);
773 (void)printf("%9u cached executable pages\n", uvmexp.execpages);
774
775 (void)printf("%9u minimum free pages\n", uvmexp.freemin);
776 (void)printf("%9u target free pages\n", uvmexp.freetarg);
777 (void)printf("%9u target inactive pages\n", uvmexp.inactarg);
778 (void)printf("%9u maximum wired pages\n", uvmexp.wiredmax);
779
780 (void)printf("%9u swap devices\n", uvmexp.nswapdev);
781 (void)printf("%9u swap pages\n", uvmexp.swpages);
782 (void)printf("%9u swap pages in use\n", uvmexp.swpginuse);
783 (void)printf("%9u swap allocations\n", uvmexp.nswget);
784
785 (void)printf("%9u total faults taken\n", uvmexp.faults);
786 (void)printf("%9u traps\n", uvmexp.traps);
787 (void)printf("%9u device interrupts\n", uvmexp.intrs);
788 (void)printf("%9u CPU context switches\n", uvmexp.swtch);
789 (void)printf("%9u software interrupts\n", uvmexp.softs);
790 (void)printf("%9u system calls\n", uvmexp.syscalls);
791 (void)printf("%9u pagein requests\n", uvmexp.pageins);
792 (void)printf("%9u pageout requests\n", uvmexp.pdpageouts);
793 (void)printf("%9u swap ins\n", uvmexp.swapins);
794 (void)printf("%9u swap outs\n", uvmexp.swapouts);
795 (void)printf("%9u pages swapped in\n", uvmexp.pgswapin);
796 (void)printf("%9u pages swapped out\n", uvmexp.pgswapout);
797 (void)printf("%9u forks total\n", uvmexp.forks);
798 (void)printf("%9u forks blocked parent\n", uvmexp.forks_ppwait);
799 (void)printf("%9u forks shared address space with parent\n",
800 uvmexp.forks_sharevm);
801 (void)printf("%9u pagealloc zero wanted and avail\n",
802 uvmexp.pga_zerohit);
803 (void)printf("%9u pagealloc zero wanted and not avail\n",
804 uvmexp.pga_zeromiss);
805 (void)printf("%9u aborts of idle page zeroing\n",
806 uvmexp.zeroaborts);
807 (void)printf("%9u pagealloc desired color avail\n",
808 uvmexp.colorhit);
809 (void)printf("%9u pagealloc desired color not avail\n",
810 uvmexp.colormiss);
811
812 (void)printf("%9u faults with no memory\n", uvmexp.fltnoram);
813 (void)printf("%9u faults with no anons\n", uvmexp.fltnoanon);
814 (void)printf("%9u faults had to wait on pages\n", uvmexp.fltpgwait);
815 (void)printf("%9u faults found released page\n", uvmexp.fltpgrele);
816 (void)printf("%9u faults relock (%u ok)\n", uvmexp.fltrelck,
817 uvmexp.fltrelckok);
818 (void)printf("%9u anon page faults\n", uvmexp.fltanget);
819 (void)printf("%9u anon retry faults\n", uvmexp.fltanretry);
820 (void)printf("%9u amap copy faults\n", uvmexp.fltamcopy);
821 (void)printf("%9u neighbour anon page faults\n", uvmexp.fltnamap);
822 (void)printf("%9u neighbour object page faults\n", uvmexp.fltnomap);
823 (void)printf("%9u locked pager get faults\n", uvmexp.fltlget);
824 (void)printf("%9u unlocked pager get faults\n", uvmexp.fltget);
825 (void)printf("%9u anon faults\n", uvmexp.flt_anon);
826 (void)printf("%9u anon copy on write faults\n", uvmexp.flt_acow);
827 (void)printf("%9u object faults\n", uvmexp.flt_obj);
828 (void)printf("%9u promote copy faults\n", uvmexp.flt_prcopy);
829 (void)printf("%9u promote zero fill faults\n", uvmexp.flt_przero);
830
831 (void)printf("%9u times daemon wokeup\n",uvmexp.pdwoke);
832 (void)printf("%9u revolutions of the clock hand\n", uvmexp.pdrevs);
833 (void)printf("%9u times daemon attempted swapout\n", uvmexp.pdswout);
834 (void)printf("%9u pages freed by daemon\n", uvmexp.pdfreed);
835 (void)printf("%9u pages scanned by daemon\n", uvmexp.pdscans);
836 (void)printf("%9u anonymous pages scanned by daemon\n",
837 uvmexp.pdanscan);
838 (void)printf("%9u object pages scanned by daemon\n", uvmexp.pdobscan);
839 (void)printf("%9u pages reactivated\n", uvmexp.pdreact);
840 (void)printf("%9u anonymous pages reactivated\n", uvmexp.pdreanon);
841 (void)printf("%9u cached file pages reactivated\n", uvmexp.pdrefile);
842 (void)printf("%9u cached executable pages reactivated\n",
843 uvmexp.pdreexec);
844 (void)printf("%9u pages found busy by daemon\n", uvmexp.pdbusy);
845 (void)printf("%9u total pending pageouts\n", uvmexp.pdpending);
846 (void)printf("%9u pages deactivated\n", uvmexp.pddeact);
847
848 kread(namelist, X_NCHSTATS, &nchstats, sizeof(nchstats));
849 nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
850 nchstats.ncs_badhits + nchstats.ncs_falsehits +
851 nchstats.ncs_miss + nchstats.ncs_long;
852 (void)printf("%9lu total name lookups\n", nchtotal);
853 (void)printf("%9lu good hits\n", nchstats.ncs_goodhits);
854 (void)printf("%9lu negative hits\n", nchstats.ncs_neghits);
855 (void)printf("%9lu bad hits\n", nchstats.ncs_badhits);
856 (void)printf("%9lu false hits\n", nchstats.ncs_falsehits);
857 (void)printf("%9lu miss\n", nchstats.ncs_miss);
858 (void)printf("%9lu too long\n", nchstats.ncs_long);
859 (void)printf("%9lu pass2 hits\n", nchstats.ncs_pass2);
860 (void)printf("%9lu 2passes\n", nchstats.ncs_2passes);
861 (void)printf(
862 "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n",
863 "", PCT(nchstats.ncs_goodhits, nchtotal),
864 PCT(nchstats.ncs_neghits, nchtotal),
865 PCT(nchstats.ncs_pass2, nchtotal));
866 (void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
867 PCT(nchstats.ncs_badhits, nchtotal),
868 PCT(nchstats.ncs_falsehits, nchtotal),
869 PCT(nchstats.ncs_long, nchtotal));
870 }
871
872 void
873 doforkst(void)
874 {
875
876 kread(namelist, X_UVMEXP, &uvmexp, sizeof(uvmexp));
877
878 (void)printf("%u forks total\n", uvmexp.forks);
879 (void)printf("%u forks blocked parent\n", uvmexp.forks_ppwait);
880 (void)printf("%u forks shared address space with parent\n",
881 uvmexp.forks_sharevm);
882 }
883
884 void
885 dkstats(void)
886 {
887 int dn;
888 double etime;
889
890 /* Calculate disk stat deltas. */
891 dkswap();
892 etime = cur.cp_etime;
893
894 for (dn = 0; dn < dk_ndrive; ++dn) {
895 if (!dk_select[dn])
896 continue;
897 (void)printf("%2.0f ",
898 (cur.dk_rxfer[dn] + cur.dk_wxfer[dn]) / etime);
899 }
900 }
901
902 void
903 tpstats(void)
904 {
905 int dn;
906 double etime;
907
908 /* Calculate tape stat deltas. */
909 tpswap();
910 etime = cur.cp_etime;
911
912 for (dn = 0; dn < tp_ndrive; ++dn) {
913 if (!tp_select[dn])
914 continue;
915 (void)printf("%2.0f ",
916 (cur_tape.rxfer[dn] + cur_tape.wxfer[dn]) / etime);
917 }
918 }
919
920 void
921 cpustats(void)
922 {
923 int state;
924 double pcnt, total;
925 double stat_us, stat_sy, stat_id;
926
927 total = 0;
928 for (state = 0; state < CPUSTATES; ++state)
929 total += cur.cp_time[state];
930 if (total)
931 pcnt = 100 / total;
932 else
933 pcnt = 0;
934 stat_us = (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pcnt;
935 stat_sy = (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pcnt;
936 stat_id = cur.cp_time[CP_IDLE] * pcnt;
937 (void)printf("%*.0f ", ((stat_sy >= 100) ? 1 : 2), stat_us);
938 (void)printf("%*.0f ", ((stat_us >= 100 || stat_id >= 100) ? 1 : 2),
939 stat_sy);
940 (void)printf("%2.0f", stat_id);
941 }
942
943 void
944 dointr(int verbose)
945 {
946 unsigned long *intrcnt;
947 unsigned long long inttotal, uptime;
948 int nintr, inamlen;
949 char *intrname;
950 struct evcntlist allevents;
951 struct evcnt evcnt, *evptr;
952 char evgroup[EVCNT_STRING_MAX], evname[EVCNT_STRING_MAX];
953
954 inttotal = 0;
955 uptime = getuptime();
956 (void)printf("%-34s %16s %8s\n", "interrupt", "total", "rate");
957 nintr = intrnl[X_EINTRCNT].n_value - intrnl[X_INTRCNT].n_value;
958 inamlen = intrnl[X_EINTRNAMES].n_value - intrnl[X_INTRNAMES].n_value;
959 if (nintr != 0 && inamlen != 0) {
960 intrcnt = malloc((size_t)nintr);
961 intrname = malloc((size_t)inamlen);
962 if (intrcnt == NULL || intrname == NULL)
963 errx(1, "%s", "");
964 kread(intrnl, X_INTRCNT, intrcnt, (size_t)nintr);
965 kread(intrnl, X_INTRNAMES, intrname, (size_t)inamlen);
966 nintr /= sizeof(long);
967 while (--nintr >= 0) {
968 if (*intrcnt || verbose)
969 (void)printf("%-34s %16llu %8llu\n", intrname,
970 (unsigned long long)*intrcnt,
971 (unsigned long long)
972 (*intrcnt / uptime));
973 intrname += strlen(intrname) + 1;
974 inttotal += *intrcnt++;
975 }
976 free(intrcnt);
977 free(intrname);
978 }
979
980 kread(namelist, X_ALLEVENTS, &allevents, sizeof allevents);
981 evptr = TAILQ_FIRST(&allevents);
982 while (evptr) {
983 deref_kptr(evptr, &evcnt, sizeof(evcnt), "event chain trashed");
984 evptr = TAILQ_NEXT(&evcnt, ev_list);
985 if (evcnt.ev_type != EVCNT_TYPE_INTR)
986 continue;
987
988 if (evcnt.ev_count == 0 && !verbose)
989 continue;
990
991 deref_kptr(evcnt.ev_group, evgroup, evcnt.ev_grouplen + 1,
992 "event chain trashed");
993 deref_kptr(evcnt.ev_name, evname, evcnt.ev_namelen + 1,
994 "event chain trashed");
995
996 (void)printf("%s %s%*s %16llu %8llu\n", evgroup, evname,
997 34 - (evcnt.ev_grouplen + 1 + evcnt.ev_namelen), "",
998 (unsigned long long)evcnt.ev_count,
999 (unsigned long long)(evcnt.ev_count / uptime));
1000
1001 inttotal += evcnt.ev_count++;
1002 }
1003 (void)printf("%-34s %16llu %8llu\n", "Total", inttotal,
1004 (unsigned long long)(inttotal / uptime));
1005 }
1006
1007 void
1008 doevcnt(int verbose)
1009 {
1010 static const char * evtypes [] = { "misc", "intr", "trap" };
1011 unsigned long long uptime;
1012 struct evcntlist allevents;
1013 struct evcnt evcnt, *evptr;
1014 char evgroup[EVCNT_STRING_MAX], evname[EVCNT_STRING_MAX];
1015
1016 /* XXX should print type! */
1017
1018 uptime = getuptime();
1019 (void)printf("%-34s %16s %8s %s\n", "event", "total", "rate", "type");
1020 kread(namelist, X_ALLEVENTS, &allevents, sizeof allevents);
1021 evptr = TAILQ_FIRST(&allevents);
1022 while (evptr) {
1023 deref_kptr(evptr, &evcnt, sizeof(evcnt), "event chain trashed");
1024
1025 evptr = TAILQ_NEXT(&evcnt, ev_list);
1026 if (evcnt.ev_count == 0 && !verbose)
1027 continue;
1028
1029 deref_kptr(evcnt.ev_group, evgroup, evcnt.ev_grouplen + 1,
1030 "event chain trashed");
1031 deref_kptr(evcnt.ev_name, evname, evcnt.ev_namelen + 1,
1032 "event chain trashed");
1033
1034 (void)printf("%s %s%*s %16llu %8llu %s\n", evgroup, evname,
1035 34 - (evcnt.ev_grouplen + 1 + evcnt.ev_namelen), "",
1036 (unsigned long long)evcnt.ev_count,
1037 (unsigned long long)(evcnt.ev_count / uptime),
1038 (evcnt.ev_type < sizeof(evtypes)/sizeof(evtypes[0]) ?
1039 evtypes[evcnt.ev_type] : "?"));
1040 }
1041 }
1042
1043 static char memname[64];
1044
1045 void
1046 domem(void)
1047 {
1048 struct kmembuckets *kp;
1049 struct malloc_type ks, *ksp;
1050 int i, j;
1051 int len, size, first;
1052 long totuse = 0, totfree = 0, totreq = 0;
1053 struct kmembuckets buckets[MINBUCKET + 16];
1054
1055 kread(namelist, X_KMEMBUCKETS, buckets, sizeof(buckets));
1056 for (first = 1, i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16;
1057 i++, kp++) {
1058 if (kp->kb_calls == 0)
1059 continue;
1060 if (first) {
1061 (void)printf("Memory statistics by bucket size\n");
1062 (void)printf(
1063 " Size In Use Free Requests HighWater Couldfree\n");
1064 first = 0;
1065 }
1066 size = 1 << i;
1067 (void)printf("%8d %8ld %6ld %10ld %7ld %10ld\n", size,
1068 kp->kb_total - kp->kb_totalfree,
1069 kp->kb_totalfree, kp->kb_calls,
1070 kp->kb_highwat, kp->kb_couldfree);
1071 totfree += size * kp->kb_totalfree;
1072 }
1073
1074 /*
1075 * If kmem statistics are not being gathered by the kernel,
1076 * first will still be 1.
1077 */
1078 if (first) {
1079 warnx("Kmem statistics are not being gathered by the kernel.");
1080 return;
1081 }
1082
1083 (void)printf("\nMemory usage type by bucket size\n");
1084 (void)printf(" Size Type(s)\n");
1085 kp = &buckets[MINBUCKET];
1086 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) {
1087 if (kp->kb_calls == 0)
1088 continue;
1089 first = 1;
1090 len = 8;
1091 for (kread(namelist, X_KMEMSTAT, &ksp, sizeof(ksp));
1092 ksp != NULL; ksp = ks.ks_next) {
1093 deref_kptr(ksp, &ks, sizeof(ks), "malloc type");
1094 if (ks.ks_calls == 0)
1095 continue;
1096 if ((ks.ks_size & j) == 0)
1097 continue;
1098 deref_kptr(ks.ks_shortdesc, memname,
1099 sizeof(memname), "malloc type name");
1100 len += 2 + strlen(memname);
1101 if (first)
1102 printf("%8d %s", j, memname);
1103 else
1104 printf(",");
1105 if (len >= 80) {
1106 printf("\n\t ");
1107 len = 10 + strlen(memname);
1108 }
1109 if (!first)
1110 printf(" %s", memname);
1111 first = 0;
1112 }
1113 putchar('\n');
1114 }
1115
1116 (void)printf(
1117 "\nMemory statistics by type Type Kern\n");
1118 (void)printf(
1119 " Type InUse MemUse HighUse Limit Requests Limit Limit Size(s)\n");
1120 for (kread(namelist, X_KMEMSTAT, &ksp, sizeof(ksp));
1121 ksp != NULL; ksp = ks.ks_next) {
1122 deref_kptr(ksp, &ks, sizeof(ks), "malloc type");
1123 if (ks.ks_calls == 0)
1124 continue;
1125 deref_kptr(ks.ks_shortdesc, memname,
1126 sizeof(memname), "malloc type name");
1127 (void)printf("%14s%6ld%6ldK%7ldK%6ldK%9ld%5u%6u",
1128 memname,
1129 ks.ks_inuse, (ks.ks_memuse + 1023) / 1024,
1130 (ks.ks_maxused + 1023) / 1024,
1131 (ks.ks_limit + 1023) / 1024, ks.ks_calls,
1132 ks.ks_limblocks, ks.ks_mapblocks);
1133 first = 1;
1134 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
1135 if ((ks.ks_size & j) == 0)
1136 continue;
1137 if (first)
1138 printf(" %d", j);
1139 else
1140 printf(",%d", j);
1141 first = 0;
1142 }
1143 printf("\n");
1144 totuse += ks.ks_memuse;
1145 totreq += ks.ks_calls;
1146 }
1147 (void)printf("\nMemory totals: In Use Free Requests\n");
1148 (void)printf(" %7ldK %6ldK %8ld\n\n",
1149 (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
1150 }
1151
1152 void
1153 dopool(int verbose, int wide)
1154 {
1155 int first, ovflw;
1156 void *addr;
1157 long total, inuse, this_total, this_inuse;
1158 TAILQ_HEAD(,pool) pool_head;
1159 struct pool pool, *pp = &pool;
1160 struct pool_allocator pa;
1161 char name[32], maxp[32];
1162
1163 kread(namelist, X_POOLHEAD, &pool_head, sizeof(pool_head));
1164 addr = TAILQ_FIRST(&pool_head);
1165
1166 total = inuse = 0;
1167
1168 for (first = 1; addr != NULL; addr = TAILQ_NEXT(pp, pr_poollist) ) {
1169 deref_kptr(addr, pp, sizeof(*pp), "pool chain trashed");
1170 deref_kptr(pp->pr_alloc, &pa, sizeof(pa),
1171 "pool allocator trashed");
1172 deref_kptr(pp->pr_wchan, name, sizeof(name),
1173 "pool wait channel trashed");
1174 name[sizeof(name)-1] = '\0';
1175
1176 if (first) {
1177 (void)printf("Memory resource pool statistics\n");
1178 (void)printf(
1179 "%-*s%*s%*s%5s%*s%s%s%*s%*s%6s%s%6s%6s%6s%5s%s%s\n",
1180 wide ? 16 : 11, "Name",
1181 wide ? 6 : 5, "Size",
1182 wide ? 12 : 9, "Requests",
1183 "Fail",
1184 wide ? 12 : 9, "Releases",
1185 wide ? " InUse" : "",
1186 wide ? " Avail" : "",
1187 wide ? 7 : 6, "Pgreq",
1188 wide ? 7 : 6, "Pgrel",
1189 "Npage",
1190 wide ? " PageSz" : "",
1191 "Hiwat",
1192 "Minpg",
1193 "Maxpg",
1194 "Idle",
1195 wide ? " Flags" : "",
1196 wide ? " Util" : "");
1197 first = 0;
1198 }
1199 if (pp->pr_nget == 0 && !verbose)
1200 continue;
1201 if (pp->pr_maxpages == UINT_MAX)
1202 snprintf(maxp, sizeof(maxp), "inf");
1203 else
1204 snprintf(maxp, sizeof(maxp), "%u", pp->pr_maxpages);
1205 /*
1206 * Print single word. `ovflow' is number of characters didn't fit
1207 * on the last word. `fmt' is a format string to print this word.
1208 * It must contain asterisk for field width. `width' is a width
1209 * occupied by this word. `fixed' is a number of constant chars in
1210 * `fmt'. `val' is a value to be printed using format string `fmt'.
1211 */
1212 #define PRWORD(ovflw, fmt, width, fixed, val) do { \
1213 (ovflw) += printf((fmt), \
1214 (width) - (fixed) - (ovflw) > 0 ? \
1215 (width) - (fixed) - (ovflw) : 0, \
1216 (val)) - (width); \
1217 if ((ovflw) < 0) \
1218 (ovflw) = 0; \
1219 } while (/* CONSTCOND */0)
1220 ovflw = 0;
1221 PRWORD(ovflw, "%-*s", wide ? 16 : 11, 0, name);
1222 PRWORD(ovflw, " %*u", wide ? 6 : 5, 1, pp->pr_size);
1223 PRWORD(ovflw, " %*lu", wide ? 12 : 9, 1, pp->pr_nget);
1224 PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nfail);
1225 PRWORD(ovflw, " %*lu", wide ? 12 : 9, 1, pp->pr_nput);
1226 if (wide)
1227 PRWORD(ovflw, " %*u", 6, 1, pp->pr_nout);
1228 if (wide)
1229 PRWORD(ovflw, " %*u", 6, 1, pp->pr_nitems);
1230 PRWORD(ovflw, " %*lu", wide ? 7 : 6, 1, pp->pr_npagealloc);
1231 PRWORD(ovflw, " %*lu", wide ? 7 : 6, 1, pp->pr_npagefree);
1232 PRWORD(ovflw, " %*u", 6, 1, pp->pr_npages);
1233 if (wide)
1234 PRWORD(ovflw, " %*u", 7, 1, pa.pa_pagesz);
1235 PRWORD(ovflw, " %*u", 6, 1, pp->pr_hiwat);
1236 PRWORD(ovflw, " %*u", 6, 1, pp->pr_minpages);
1237 PRWORD(ovflw, " %*s", 6, 1, maxp);
1238 PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nidle);
1239 if (wide)
1240 PRWORD(ovflw, " 0x%0*x", 4, 1,
1241 pp->pr_flags | pp->pr_roflags);
1242
1243 this_inuse = pp->pr_nout * pp->pr_size;
1244 this_total = pp->pr_npages * pa.pa_pagesz;
1245 if (pp->pr_roflags & PR_RECURSIVE) {
1246 /*
1247 * Don't count in-use memory, since it's part
1248 * of another pool and will be accounted for
1249 * there.
1250 */
1251 total += (this_total - this_inuse);
1252 } else {
1253 inuse += this_inuse;
1254 total += this_total;
1255 }
1256 if (wide) {
1257 if (this_total == 0)
1258 printf(" ---");
1259 else
1260 printf(" %5.1f%%",
1261 (100.0 * this_inuse) / this_total);
1262 }
1263 printf("\n");
1264 dopoolcache(pp, verbose);
1265 }
1266
1267 inuse /= 1024;
1268 total /= 1024;
1269 printf("\nIn use %ldK, total allocated %ldK; utilization %.1f%%\n",
1270 inuse, total, (100.0 * inuse) / total);
1271 }
1272
1273 void
1274 dopoolcache(struct pool *pp, int verbose)
1275 {
1276 struct pool_cache pool_cache, *pc = &pool_cache;
1277 struct pool_cache_group pool_cache_group, *pcg = &pool_cache_group;
1278 void *addr, *pcg_addr;
1279 int i;
1280
1281 if (verbose < 1)
1282 return;
1283
1284 for (addr = TAILQ_FIRST(&pp->pr_cachelist); addr != NULL;
1285 addr = TAILQ_NEXT(pc, pc_poollist)) {
1286 deref_kptr(addr, pc, sizeof(*pc), "pool cache trashed");
1287 printf("\tcache %p: allocfrom %p freeto %p\n", addr,
1288 pc->pc_allocfrom, pc->pc_freeto);
1289 printf("\t hits %lu misses %lu ngroups %lu nitems %lu\n",
1290 pc->pc_hits, pc->pc_misses, pc->pc_ngroups, pc->pc_nitems);
1291 if (verbose < 2)
1292 continue;
1293 for (pcg_addr = TAILQ_FIRST(&pc->pc_grouplist);
1294 pcg_addr != NULL; pcg_addr = TAILQ_NEXT(pcg, pcg_list)) {
1295 deref_kptr(pcg_addr, pcg, sizeof(*pcg),
1296 "pool cache group trashed");
1297 printf("\t\tgroup %p: avail %d\n", pcg_addr,
1298 pcg->pcg_avail);
1299 for (i = 0; i < PCG_NOBJECTS; i++) {
1300 if (pcg->pcg_objects[i].pcgo_pa !=
1301 POOL_PADDR_INVALID) {
1302 printf("\t\t\t%p, 0x%llx\n",
1303 pcg->pcg_objects[i].pcgo_va,
1304 (unsigned long long)
1305 pcg->pcg_objects[i].pcgo_pa);
1306 } else {
1307 printf("\t\t\t%p\n",
1308 pcg->pcg_objects[i].pcgo_va);
1309 }
1310 }
1311 }
1312 }
1313
1314 }
1315
1316 enum hashtype { /* from <sys/systm.h> */
1317 HASH_LIST,
1318 HASH_TAILQ
1319 };
1320
1321 struct uidinfo { /* XXX: no kernel header file */
1322 LIST_ENTRY(uidinfo) ui_hash;
1323 uid_t ui_uid;
1324 long ui_proccnt;
1325 };
1326
1327 struct kernel_hash {
1328 const char * description; /* description */
1329 int hashsize; /* nlist index for hash size */
1330 int hashtbl; /* nlist index for hash table */
1331 enum hashtype type; /* type of hash table */
1332 size_t offset; /* offset of {LIST,TAILQ}_NEXT */
1333 } khashes[] =
1334 {
1335 {
1336 "buffer hash",
1337 X_BUFHASH, X_BUFHASHTBL,
1338 HASH_LIST, offsetof(struct buf, b_hash)
1339 }, {
1340 "inode cache (ihash)",
1341 X_IHASH, X_IHASHTBL,
1342 HASH_LIST, offsetof(struct inode, i_hash)
1343 }, {
1344 "ipv4 address -> interface hash",
1345 X_IFADDRHASH, X_IFADDRHASHTBL,
1346 HASH_LIST, offsetof(struct in_ifaddr, ia_hash),
1347 }, {
1348 "name cache hash",
1349 X_NCHASH, X_NCHASHTBL,
1350 HASH_LIST, offsetof(struct namecache, nc_hash),
1351 }, {
1352 "name cache directory hash",
1353 X_NCVHASH, X_NCVHASHTBL,
1354 HASH_LIST, offsetof(struct namecache, nc_vhash),
1355 }, {
1356 "nfs client node cache",
1357 X_NFSNODE, X_NFSNODETBL,
1358 HASH_LIST, offsetof(struct nfsnode, n_hash)
1359 }, {
1360 "user info (uid -> used processes) hash",
1361 X_UIHASH, X_UIHASHTBL,
1362 HASH_LIST, offsetof(struct uidinfo, ui_hash),
1363 }, {
1364 NULL, -1, -1, 0, 0,
1365 }
1366 };
1367
1368 void
1369 dohashstat(int verbose, int todo, const char *hashname)
1370 {
1371 LIST_HEAD(, generic) *hashtbl_list;
1372 TAILQ_HEAD(, generic) *hashtbl_tailq;
1373 struct kernel_hash *curhash;
1374 void *hashaddr, *hashbuf, *nhashbuf, *nextaddr;
1375 size_t elemsize, hashbufsize, thissize;
1376 u_long hashsize;
1377 int i, used, items, chain, maxchain;
1378
1379 hashbuf = NULL;
1380 hashbufsize = 0;
1381
1382 if (todo & HASHLIST) {
1383 printf("Supported hashes:\n");
1384 for (curhash = khashes; curhash->description; curhash++) {
1385 if (hashnl[curhash->hashsize].n_value == 0 ||
1386 hashnl[curhash->hashtbl].n_value == 0)
1387 continue;
1388 printf("\t%-16s%s\n",
1389 hashnl[curhash->hashsize].n_name + 1,
1390 curhash->description);
1391 }
1392 return;
1393 }
1394
1395 if (hashname != NULL) {
1396 for (curhash = khashes; curhash->description; curhash++) {
1397 if (strcmp(hashnl[curhash->hashsize].n_name + 1,
1398 hashname) == 0 &&
1399 hashnl[curhash->hashsize].n_value != 0 &&
1400 hashnl[curhash->hashtbl].n_value != 0)
1401 break;
1402 }
1403 if (curhash->description == NULL) {
1404 warnx("%s: no such hash", hashname);
1405 return;
1406 }
1407 }
1408
1409 printf(
1410 "%-16s %8s %8s %8s %8s %8s %8s\n"
1411 "%-16s %8s %8s %8s %8s %8s %8s\n",
1412 "", "total", "used", "util", "num", "average", "maximum",
1413 "hash table", "buckets", "buckets", "%", "items", "chain",
1414 "chain");
1415
1416 for (curhash = khashes; curhash->description; curhash++) {
1417 if (hashnl[curhash->hashsize].n_value == 0 ||
1418 hashnl[curhash->hashtbl].n_value == 0)
1419 continue;
1420 if (hashname != NULL &&
1421 strcmp(hashnl[curhash->hashsize].n_name + 1, hashname))
1422 continue;
1423 elemsize = curhash->type == HASH_LIST ?
1424 sizeof(*hashtbl_list) : sizeof(*hashtbl_tailq);
1425 deref_kptr((void *)hashnl[curhash->hashsize].n_value,
1426 &hashsize, sizeof(hashsize),
1427 hashnl[curhash->hashsize].n_name);
1428 hashsize++;
1429 deref_kptr((void *)hashnl[curhash->hashtbl].n_value,
1430 &hashaddr, sizeof(hashaddr),
1431 hashnl[curhash->hashtbl].n_name);
1432 if (verbose)
1433 printf("%s %lu, %s %p, offset %ld, elemsize %llu\n",
1434 hashnl[curhash->hashsize].n_name + 1, hashsize,
1435 hashnl[curhash->hashtbl].n_name + 1, hashaddr,
1436 (long)curhash->offset,
1437 (unsigned long long)elemsize);
1438 thissize = hashsize * elemsize;
1439 if (thissize > hashbufsize) {
1440 if ((nhashbuf = realloc(hashbuf, thissize)) == NULL)
1441 errx(1, "malloc hashbuf %llu",
1442 (unsigned long long)hashbufsize);
1443 hashbuf = nhashbuf;
1444 hashbufsize = thissize;
1445 }
1446 deref_kptr(hashaddr, hashbuf, thissize,
1447 hashnl[curhash->hashtbl].n_name);
1448 used = 0;
1449 items = maxchain = 0;
1450 if (curhash->type == HASH_LIST) {
1451 hashtbl_list = hashbuf;
1452 hashtbl_tailq = NULL;
1453 } else {
1454 hashtbl_list = NULL;
1455 hashtbl_tailq = hashbuf;
1456 }
1457 for (i = 0; i < hashsize; i++) {
1458 if (curhash->type == HASH_LIST)
1459 nextaddr = LIST_FIRST(&hashtbl_list[i]);
1460 else
1461 nextaddr = TAILQ_FIRST(&hashtbl_tailq[i]);
1462 if (nextaddr == NULL)
1463 continue;
1464 if (verbose)
1465 printf("%5d: %p\n", i, nextaddr);
1466 used++;
1467 chain = 0;
1468 do {
1469 chain++;
1470 deref_kptr((char *)nextaddr + curhash->offset,
1471 &nextaddr, sizeof(void *),
1472 "hash chain corrupted");
1473 if (verbose > 1)
1474 printf("got nextaddr as %p\n",
1475 nextaddr);
1476 } while (nextaddr != NULL);
1477 items += chain;
1478 if (verbose && chain > 1)
1479 printf("\tchain = %d\n", chain);
1480 if (chain > maxchain)
1481 maxchain = chain;
1482 }
1483 printf("%-16s %8ld %8d %8.2f %8d %8.2f %8d\n",
1484 hashnl[curhash->hashsize].n_name + 1,
1485 hashsize, used, used * 100.0 / hashsize,
1486 items, used ? (double)items / used : 0.0, maxchain);
1487 }
1488 }
1489
1490 /*
1491 * kread reads something from the kernel, given its nlist index in namelist[].
1492 */
1493 void
1494 kread(struct nlist *nl, int nlx, void *addr, size_t size)
1495 {
1496 const char *sym;
1497
1498 sym = nl[nlx].n_name;
1499 if (*sym == '_')
1500 ++sym;
1501 if (nl[nlx].n_type == 0 || nl[nlx].n_value == 0)
1502 errx(1, "symbol %s not defined", sym);
1503 deref_kptr((void *)nl[nlx].n_value, addr, size, sym);
1504 }
1505
1506 /*
1507 * Dereference the kernel pointer `kptr' and fill in the local copy
1508 * pointed to by `ptr'. The storage space must be pre-allocated,
1509 * and the size of the copy passed in `len'.
1510 */
1511 void
1512 deref_kptr(const void *kptr, void *ptr, size_t len, const char *msg)
1513 {
1514
1515 if (*msg == '_')
1516 msg++;
1517 if (kvm_read(kd, (u_long)kptr, (char *)ptr, len) != len)
1518 errx(1, "kptr %lx: %s: %s", (u_long)kptr, msg, kvm_geterr(kd));
1519 }
1520
1521 /*
1522 * Traverse the UVM history buffers, performing the requested action.
1523 *
1524 * Note, we assume that if we're not listing, we're dumping.
1525 */
1526 void
1527 hist_traverse(int todo, const char *histname)
1528 {
1529 struct uvm_history_head histhead;
1530 struct uvm_history hist, *histkva;
1531 char *name = NULL;
1532 size_t namelen = 0;
1533
1534 if (histnl[0].n_value == 0) {
1535 warnx("UVM history is not compiled into the kernel.");
1536 return;
1537 }
1538
1539 deref_kptr((void *)histnl[X_UVM_HISTORIES].n_value, &histhead,
1540 sizeof(histhead), histnl[X_UVM_HISTORIES].n_name);
1541
1542 if (histhead.lh_first == NULL) {
1543 warnx("No active UVM history logs.");
1544 return;
1545 }
1546
1547 if (todo & HISTLIST)
1548 printf("Active UVM histories:");
1549
1550 for (histkva = LIST_FIRST(&histhead); histkva != NULL;
1551 histkva = LIST_NEXT(&hist, list)) {
1552 deref_kptr(histkva, &hist, sizeof(hist), "histkva");
1553 if (hist.namelen > namelen) {
1554 if (name != NULL)
1555 free(name);
1556 namelen = hist.namelen;
1557 if ((name = malloc(namelen + 1)) == NULL)
1558 err(1, "malloc history name");
1559 }
1560
1561 deref_kptr(hist.name, name, namelen, "history name");
1562 name[namelen] = '\0';
1563 if (todo & HISTLIST)
1564 printf(" %s", name);
1565 else {
1566 /*
1567 * If we're dumping all histories, do it, else
1568 * check to see if this is the one we want.
1569 */
1570 if (histname == NULL || strcmp(histname, name) == 0) {
1571 if (histname == NULL)
1572 printf("\nUVM history `%s':\n", name);
1573 hist_dodump(&hist);
1574 }
1575 }
1576 }
1577
1578 if (todo & HISTLIST)
1579 putchar('\n');
1580
1581 if (name != NULL)
1582 free(name);
1583 }
1584
1585 /*
1586 * Actually dump the history buffer at the specified KVA.
1587 */
1588 void
1589 hist_dodump(struct uvm_history *histp)
1590 {
1591 struct uvm_history_ent *histents, *e;
1592 size_t histsize;
1593 char *fmt = NULL, *fn = NULL;
1594 size_t fmtlen = 0, fnlen = 0;
1595 int i;
1596
1597 histsize = sizeof(struct uvm_history_ent) * histp->n;
1598
1599 if ((histents = malloc(histsize)) == NULL)
1600 err(1, "malloc history entries");
1601
1602 memset(histents, 0, histsize);
1603
1604 deref_kptr(histp->e, histents, histsize, "history entries");
1605 i = histp->f;
1606 do {
1607 e = &histents[i];
1608 if (e->fmt != NULL) {
1609 if (e->fmtlen > fmtlen) {
1610 if (fmt != NULL)
1611 free(fmt);
1612 fmtlen = e->fmtlen;
1613 if ((fmt = malloc(fmtlen + 1)) == NULL)
1614 err(1, "malloc printf format");
1615 }
1616 if (e->fnlen > fnlen) {
1617 if (fn != NULL)
1618 free(fn);
1619 fnlen = e->fnlen;
1620 if ((fn = malloc(fnlen + 1)) == NULL)
1621 err(1, "malloc function name");
1622 }
1623
1624 deref_kptr(e->fmt, fmt, fmtlen, "printf format");
1625 fmt[fmtlen] = '\0';
1626
1627 deref_kptr(e->fn, fn, fnlen, "function name");
1628 fn[fnlen] = '\0';
1629
1630 printf("%06ld.%06ld ", (long int)e->tv.tv_sec,
1631 (long int)e->tv.tv_usec);
1632 printf("%s#%ld: ", fn, e->call);
1633 printf(fmt, e->v[0], e->v[1], e->v[2], e->v[3]);
1634 putchar('\n');
1635 }
1636 i = (i + 1) % histp->n;
1637 } while (i != histp->f);
1638
1639 free(histents);
1640 if (fmt != NULL)
1641 free(fmt);
1642 if (fn != NULL)
1643 free(fn);
1644 }
1645
1646 void
1647 usage(void)
1648 {
1649
1650 (void)fprintf(stderr,
1651 "usage: %s [-efHiLlmstUvW] [-c count] [-h hashname] [-M core] [-N system]\n"
1652 "\t\t[-u histname] [-w wait] [disks]\n", getprogname());
1653 exit(1);
1654 }
1655