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