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