vmstat.c revision 1.201 1 /* $NetBSD: vmstat.c,v 1.201 2014/06/14 01:26:59 joerg 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\
66 The Regents of the University of California. All rights reserved.");
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.201 2014/06/14 01:26:59 joerg Exp $");
74 #endif
75 #endif /* not lint */
76
77 #define __POOL_EXPOSE
78
79 #include <sys/param.h>
80 #include <sys/types.h>
81 #include <sys/mount.h>
82 #include <sys/uio.h>
83
84 #include <sys/buf.h>
85 #include <sys/evcnt.h>
86 #include <sys/ioctl.h>
87 #include <sys/malloc.h>
88 #include <sys/mallocvar.h>
89 #include <sys/namei.h>
90 #include <sys/pool.h>
91 #include <sys/proc.h>
92 #include <sys/sched.h>
93 #include <sys/socket.h>
94 #include <sys/sysctl.h>
95 #include <sys/time.h>
96 #include <sys/queue.h>
97 #include <sys/kernhist.h>
98
99 #include <uvm/uvm_extern.h>
100 #include <uvm/uvm_stat.h>
101
102 #include <net/if.h>
103 #include <netinet/in.h>
104 #include <netinet/in_var.h>
105
106 #include <ufs/ufs/inode.h>
107
108 #include <nfs/rpcv2.h>
109 #include <nfs/nfsproto.h>
110 #include <nfs/nfsnode.h>
111
112 #include <ctype.h>
113 #include <err.h>
114 #include <errno.h>
115 #include <fcntl.h>
116 #include <kvm.h>
117 #include <limits.h>
118 #include <nlist.h>
119 #undef n_hash
120 #include <paths.h>
121 #include <signal.h>
122 #include <stdio.h>
123 #include <stddef.h>
124 #include <stdlib.h>
125 #include <string.h>
126 #include <time.h>
127 #include <unistd.h>
128 #include <util.h>
129
130 #include "drvstats.h"
131
132 /*
133 * All this mess will go away once everything is converted.
134 */
135 #ifdef __HAVE_CPU_DATA_FIRST
136
137 # include <sys/cpu_data.h>
138 struct cpu_info {
139 struct cpu_data ci_data;
140 };
141 #else
142 # include <sys/cpu.h>
143 #endif
144
145 /*
146 * General namelist
147 */
148 struct nlist namelist[] =
149 {
150 #define X_BOOTTIME 0
151 { .n_name = "_boottime" },
152 #define X_HZ 1
153 { .n_name = "_hz" },
154 #define X_STATHZ 2
155 { .n_name = "_stathz" },
156 #define X_NCHSTATS 3
157 { .n_name = "_nchstats" },
158 #define X_ALLEVENTS 4
159 { .n_name = "_allevents" },
160 #define X_POOLHEAD 5
161 { .n_name = "_pool_head" },
162 #define X_UVMEXP 6
163 { .n_name = "_uvmexp" },
164 #define X_TIME_SECOND 7
165 { .n_name = "_time_second" },
166 #define X_TIME 8
167 { .n_name = "_time" },
168 #define X_CPU_INFOS 9
169 { .n_name = "_cpu_infos" },
170 #define X_NL_SIZE 10
171 { .n_name = NULL },
172 };
173
174 /*
175 * Namelist for pre-evcnt interrupt counters.
176 */
177 struct nlist intrnl[] =
178 {
179 #define X_INTRNAMES 0
180 { .n_name = "_intrnames" },
181 #define X_EINTRNAMES 1
182 { .n_name = "_eintrnames" },
183 #define X_INTRCNT 2
184 { .n_name = "_intrcnt" },
185 #define X_EINTRCNT 3
186 { .n_name = "_eintrcnt" },
187 #define X_INTRNL_SIZE 4
188 { .n_name = NULL },
189 };
190
191
192 /*
193 * Namelist for hash statistics
194 */
195 struct nlist hashnl[] =
196 {
197 #define X_NFSNODE 0
198 { .n_name = "_nfsnodehash" },
199 #define X_NFSNODETBL 1
200 { .n_name = "_nfsnodehashtbl" },
201 #define X_IHASH 2
202 { .n_name = "_ihash" },
203 #define X_IHASHTBL 3
204 { .n_name = "_ihashtbl" },
205 #define X_BUFHASH 4
206 { .n_name = "_bufhash" },
207 #define X_BUFHASHTBL 5
208 { .n_name = "_bufhashtbl" },
209 #define X_UIHASH 6
210 { .n_name = "_uihash" },
211 #define X_UIHASHTBL 7
212 { .n_name = "_uihashtbl" },
213 #define X_IFADDRHASH 8
214 { .n_name = "_in_ifaddrhash" },
215 #define X_IFADDRHASHTBL 9
216 { .n_name = "_in_ifaddrhashtbl" },
217 #define X_NCHASH 10
218 { .n_name = "_nchash" },
219 #define X_NCHASHTBL 11
220 { .n_name = "_nchashtbl" },
221 #define X_NCVHASH 12
222 { .n_name = "_ncvhash" },
223 #define X_NCVHASHTBL 13
224 { .n_name = "_ncvhashtbl" },
225 #define X_HASHNL_SIZE 14 /* must be last */
226 { .n_name = NULL },
227 };
228
229 /*
230 * Namelist for kernel histories
231 */
232 struct nlist histnl[] =
233 {
234 { .n_name = "_kern_histories" },
235 #define X_KERN_HISTORIES 0
236 { .n_name = NULL },
237 };
238
239
240 #define KILO 1024
241
242 struct cpu_counter {
243 uint64_t nintr;
244 uint64_t nsyscall;
245 uint64_t nswtch;
246 uint64_t nfault;
247 uint64_t ntrap;
248 uint64_t nsoft;
249 } cpucounter, ocpucounter;
250
251 struct uvmexp_sysctl uvmexp, ouvmexp;
252 int ndrives;
253
254 int winlines = 20;
255
256 kvm_t *kd;
257
258
259 #define FORKSTAT 0x001
260 #define INTRSTAT 0x002
261 #define MEMSTAT 0x004
262 #define SUMSTAT 0x008
263 #define EVCNTSTAT 0x010
264 #define VMSTAT 0x020
265 #define HISTLIST 0x040
266 #define HISTDUMP 0x080
267 #define HASHSTAT 0x100
268 #define HASHLIST 0x200
269 #define VMTOTAL 0x400
270 #define POOLCACHESTAT 0x800
271
272 /*
273 * Print single word. `ovflow' is number of characters didn't fit
274 * on the last word. `fmt' is a format string to print this word.
275 * It must contain asterisk for field width. `width' is a width
276 * occupied by this word. `fixed' is a number of constant chars in
277 * `fmt'. `val' is a value to be printed using format string `fmt'.
278 */
279 #define PRWORD(ovflw, fmt, width, fixed, val) do { \
280 (ovflw) += printf((fmt), \
281 (width) - (fixed) - (ovflw) > 0 ? \
282 (width) - (fixed) - (ovflw) : 0, \
283 (val)) - (width); \
284 if ((ovflw) < 0) \
285 (ovflw) = 0; \
286 } while (/* CONSTCOND */0)
287
288 void cpustats(int *);
289 void cpucounters(struct cpu_counter *);
290 void deref_kptr(const void *, void *, size_t, const char *);
291 void drvstats(int *);
292 void doevcnt(int verbose, int type);
293 void dohashstat(int, int, const char *);
294 void dointr(int verbose);
295 void dopool(int, int);
296 void dopoolcache(int);
297 void dosum(void);
298 void dovmstat(struct timespec *, int);
299 void print_total_hdr(void);
300 void dovmtotal(struct timespec *, int);
301 void kread(struct nlist *, int, void *, size_t);
302 int kreadc(struct nlist *, int, void *, size_t);
303 void needhdr(int);
304 void getnlist(int);
305 long getuptime(void);
306 void printhdr(void);
307 long pct(long, long);
308 __dead static void usage(void);
309 void doforkst(void);
310
311 void hist_traverse(int, const char *);
312 void hist_dodump(struct kern_history *);
313
314 int main(int, char **);
315 char **choosedrives(char **);
316
317 /* Namelist and memory file names. */
318 char *nlistf, *memf;
319
320 /* allow old usage [vmstat 1] */
321 #define BACKWARD_COMPATIBILITY
322
323 static const int clockrate_mib[] = { CTL_KERN, KERN_CLOCKRATE };
324 static const int vmmeter_mib[] = { CTL_VM, VM_METER };
325 static const int uvmexp2_mib[] = { CTL_VM, VM_UVMEXP2 };
326 static const int boottime_mib[] = { CTL_KERN, KERN_BOOTTIME };
327 static char kvm_errbuf[_POSIX2_LINE_MAX];
328
329 int
330 main(int argc, char *argv[])
331 {
332 int c, todo, verbose, wide;
333 struct timespec interval;
334 int reps;
335 gid_t egid = getegid();
336 const char *histname, *hashname;
337
338 histname = hashname = NULL;
339 (void)setegid(getgid());
340 memf = nlistf = NULL;
341 reps = todo = verbose = wide = 0;
342 interval.tv_sec = 0;
343 interval.tv_nsec = 0;
344 while ((c = getopt(argc, argv, "Cc:efh:HilLM:mN:stu:UvWw:")) != -1) {
345 switch (c) {
346 case 'c':
347 reps = atoi(optarg);
348 break;
349 case 'C':
350 todo |= POOLCACHESTAT;
351 break;
352 case 'e':
353 todo |= EVCNTSTAT;
354 break;
355 case 'f':
356 todo |= FORKSTAT;
357 break;
358 case 'h':
359 hashname = optarg;
360 /* FALLTHROUGH */
361 case 'H':
362 todo |= HASHSTAT;
363 break;
364 case 'i':
365 todo |= INTRSTAT;
366 break;
367 case 'l':
368 todo |= HISTLIST;
369 break;
370 case 'L':
371 todo |= HASHLIST;
372 break;
373 case 'M':
374 memf = optarg;
375 break;
376 case 'm':
377 todo |= MEMSTAT;
378 break;
379 case 'N':
380 nlistf = optarg;
381 break;
382 case 's':
383 todo |= SUMSTAT;
384 break;
385 case 't':
386 todo |= VMTOTAL;
387 break;
388 case 'u':
389 histname = optarg;
390 /* FALLTHROUGH */
391 case 'U':
392 todo |= HISTDUMP;
393 break;
394 case 'v':
395 verbose++;
396 break;
397 case 'W':
398 wide++;
399 break;
400 case 'w':
401 interval.tv_sec = atol(optarg);
402 break;
403 case '?':
404 default:
405 usage();
406 }
407 }
408 argc -= optind;
409 argv += optind;
410
411 if (todo == 0)
412 todo = VMSTAT;
413
414 /*
415 * Discard setgid privileges. If not the running kernel, we toss
416 * them away totally so that bad guys can't print interesting stuff
417 * from kernel memory, otherwise switch back to kmem for the
418 * duration of the kvm_openfiles() call.
419 */
420 if (nlistf != NULL || memf != NULL)
421 (void)setgid(getgid());
422 else
423 (void)setegid(egid);
424
425 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, kvm_errbuf);
426 if (kd == NULL) {
427 if (nlistf != NULL || memf != NULL) {
428 errx(1, "kvm_openfiles: %s", kvm_errbuf);
429 }
430 }
431
432 if (nlistf == NULL && memf == NULL)
433 (void)setgid(getgid());
434
435
436 if (todo & VMSTAT) {
437 struct winsize winsize;
438
439 (void)drvinit(0);/* Initialize disk stats, no disks selected. */
440
441 (void)setgid(getgid()); /* don't need privs anymore */
442
443 argv = choosedrives(argv); /* Select disks. */
444 winsize.ws_row = 0;
445 (void)ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize);
446 if (winsize.ws_row > 0)
447 winlines = winsize.ws_row;
448
449 }
450
451 #ifdef BACKWARD_COMPATIBILITY
452 if (*argv) {
453 interval.tv_sec = atol(*argv);
454 if (*++argv)
455 reps = atoi(*argv);
456 }
457 #endif
458
459 if (interval.tv_sec) {
460 if (!reps)
461 reps = -1;
462 } else if (reps)
463 interval.tv_sec = 1;
464
465
466 getnlist(todo);
467 /*
468 * Statistics dumping is incompatible with the default
469 * VMSTAT/dovmstat() output. So perform the interval/reps handling
470 * for it here.
471 */
472 if ((todo & (VMSTAT|VMTOTAL)) == 0) {
473 for (;;) {
474 if (todo & (HISTLIST|HISTDUMP)) {
475 if ((todo & (HISTLIST|HISTDUMP)) ==
476 (HISTLIST|HISTDUMP))
477 errx(1, "you may list or dump,"
478 " but not both!");
479 hist_traverse(todo, histname);
480 (void)putchar('\n');
481 }
482 if (todo & FORKSTAT) {
483 doforkst();
484 (void)putchar('\n');
485 }
486 if (todo & MEMSTAT) {
487 dopool(verbose, wide);
488 (void)putchar('\n');
489 }
490 if (todo & POOLCACHESTAT) {
491 dopoolcache(verbose);
492 (void)putchar('\n');
493 }
494 if (todo & SUMSTAT) {
495 dosum();
496 (void)putchar('\n');
497 }
498 if (todo & INTRSTAT) {
499 dointr(verbose);
500 (void)putchar('\n');
501 }
502 if (todo & EVCNTSTAT) {
503 doevcnt(verbose, EVCNT_TYPE_ANY);
504 (void)putchar('\n');
505 }
506 if (todo & (HASHLIST|HASHSTAT)) {
507 if ((todo & (HASHLIST|HASHSTAT)) ==
508 (HASHLIST|HASHSTAT))
509 errx(1, "you may list or display,"
510 " but not both!");
511 dohashstat(verbose, todo, hashname);
512 (void)putchar('\n');
513 }
514
515 fflush(stdout);
516 if (reps >= 0 && --reps <=0)
517 break;
518 (void)nanosleep(&interval, NULL);
519 }
520 } else {
521 if ((todo & (VMSTAT|VMTOTAL)) == (VMSTAT|VMTOTAL)) {
522 errx(1, "you may not both do vmstat and vmtotal");
523 }
524 if (todo & VMSTAT)
525 dovmstat(&interval, reps);
526 if (todo & VMTOTAL)
527 dovmtotal(&interval, reps);
528 }
529 return 0;
530 }
531
532 void
533 getnlist(int todo)
534 {
535 static int namelist_done = 0;
536 static int done = 0;
537 int c;
538 size_t i;
539
540 if (kd == NULL)
541 errx(1, "kvm_openfiles: %s", kvm_errbuf);
542
543 if (!namelist_done) {
544 namelist_done = 1;
545 if ((c = kvm_nlist(kd, namelist)) != 0) {
546 int doexit = 0;
547 if (c == -1)
548 errx(1, "kvm_nlist: %s %s",
549 "namelist", kvm_geterr(kd));
550 for (i = 0; i < __arraycount(namelist)-1; i++)
551 if (namelist[i].n_type == 0 &&
552 i != X_TIME_SECOND &&
553 i != X_TIME) {
554 if (doexit++ == 0)
555 (void)fprintf(stderr,
556 "%s: undefined symbols:",
557 getprogname());
558 (void)fprintf(stderr, " %s",
559 namelist[i].n_name);
560 }
561 if (doexit) {
562 (void)fputc('\n', stderr);
563 exit(1);
564 }
565 }
566 }
567 if ((todo & (SUMSTAT|INTRSTAT)) && !(done & (SUMSTAT|INTRSTAT))) {
568 done |= SUMSTAT|INTRSTAT;
569 (void) kvm_nlist(kd, intrnl);
570 }
571 if ((todo & (HASHLIST|HASHSTAT)) && !(done & (HASHLIST|HASHSTAT))) {
572 done |= HASHLIST|HASHSTAT;
573 if ((c = kvm_nlist(kd, hashnl)) == -1 || c == X_HASHNL_SIZE)
574 errx(1, "kvm_nlist: %s %s", "hashnl", kvm_geterr(kd));
575 }
576 if ((todo & (HISTLIST|HISTDUMP)) && !(done & (HISTLIST|HISTDUMP))) {
577 done |= HISTLIST|HISTDUMP;
578 if (kvm_nlist(kd, histnl) == -1)
579 errx(1, "kvm_nlist: %s %s", "histnl", kvm_geterr(kd));
580 }
581 }
582
583 char **
584 choosedrives(char **argv)
585 {
586 size_t i;
587
588 /*
589 * Choose drives to be displayed. Priority goes to (in order) drives
590 * supplied as arguments, default drives. If everything isn't filled
591 * in and there are drives not taken care of, display the first few
592 * that fit.
593 */
594 #define BACKWARD_COMPATIBILITY
595 for (ndrives = 0; *argv; ++argv) {
596 #ifdef BACKWARD_COMPATIBILITY
597 if (isdigit((unsigned char)**argv))
598 break;
599 #endif
600 for (i = 0; i < ndrive; i++) {
601 if (strcmp(dr_name[i], *argv))
602 continue;
603 drv_select[i] = 1;
604 ++ndrives;
605 break;
606 }
607 }
608 for (i = 0; i < ndrive && ndrives < 2; i++) {
609 if (drv_select[i])
610 continue;
611 drv_select[i] = 1;
612 ++ndrives;
613 }
614
615 return (argv);
616 }
617
618 long
619 getuptime(void)
620 {
621 static struct timespec boottime;
622 struct timespec now;
623 time_t uptime, nowsec;
624
625 if (memf == NULL) {
626 if (boottime.tv_sec == 0) {
627 size_t buflen = sizeof(boottime);
628 if (sysctl(boottime_mib, __arraycount(boottime_mib),
629 &boottime, &buflen, NULL, 0) == -1)
630 warn("Can't get boottime");
631 }
632 clock_gettime(CLOCK_REALTIME, &now);
633 } else {
634 if (boottime.tv_sec == 0)
635 kread(namelist, X_BOOTTIME, &boottime,
636 sizeof(boottime));
637 if (kreadc(namelist, X_TIME_SECOND, &nowsec, sizeof(nowsec))) {
638 /*
639 * XXX this assignment dance can be removed once
640 * timeval tv_sec is SUS mandated time_t
641 */
642 now.tv_sec = nowsec;
643 now.tv_nsec = 0;
644 } else {
645 kread(namelist, X_TIME, &now, sizeof(now));
646 }
647 }
648 uptime = now.tv_sec - boottime.tv_sec;
649 if (uptime <= 0 || uptime > 60*60*24*365*10)
650 errx(1, "time makes no sense; namelist must be wrong.");
651 return (uptime);
652 }
653
654 int hz, hdrcnt;
655
656 void
657 print_total_hdr(void)
658 {
659
660 (void)printf("procs memory\n");
661 (void)printf("ru dw pw sl");
662 (void)printf(" total-v active-v active-r");
663 (void)printf(" vm-sh avm-sh rm-sh arm-sh free\n");
664 hdrcnt = winlines - 2;
665 }
666
667 void
668 dovmtotal(struct timespec *interval, int reps)
669 {
670 struct vmtotal total;
671 size_t size;
672
673 (void)signal(SIGCONT, needhdr);
674
675 for (hdrcnt = 1;;) {
676 if (!--hdrcnt)
677 print_total_hdr();
678 if (memf != NULL) {
679 warnx("Unable to get vmtotals from crash dump.");
680 (void)memset(&total, 0, sizeof(total));
681 } else {
682 size = sizeof(total);
683 if (sysctl(vmmeter_mib, __arraycount(vmmeter_mib),
684 &total, &size, NULL, 0) == -1) {
685 warn("Can't get vmtotals");
686 (void)memset(&total, 0, sizeof(total));
687 }
688 }
689 (void)printf("%2d ", total.t_rq);
690 (void)printf("%2d ", total.t_dw);
691 (void)printf("%2d ", total.t_pw);
692 (void)printf("%2d ", total.t_sl);
693
694 (void)printf("%9d ", total.t_vm);
695 (void)printf("%9d ", total.t_avm);
696 (void)printf("%9d ", total.t_arm);
697 (void)printf("%5d ", total.t_vmshr);
698 (void)printf("%6d ", total.t_avmshr);
699 (void)printf("%5d ", total.t_rmshr);
700 (void)printf("%6d ", total.t_armshr);
701 (void)printf("%5d", total.t_free);
702
703 (void)putchar('\n');
704
705 (void)fflush(stdout);
706 if (reps >= 0 && --reps <= 0)
707 break;
708
709 (void)nanosleep(interval, NULL);
710 }
711 }
712
713 void
714 dovmstat(struct timespec *interval, int reps)
715 {
716 struct vmtotal total;
717 time_t uptime, halfuptime;
718 size_t size;
719 int pagesize = getpagesize();
720 int ovflw;
721
722 uptime = getuptime();
723 halfuptime = uptime / 2;
724 (void)signal(SIGCONT, needhdr);
725
726 if (memf != NULL) {
727 if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0)
728 kread(namelist, X_STATHZ, &hz, sizeof(hz));
729 if (!hz)
730 kread(namelist, X_HZ, &hz, sizeof(hz));
731 } else {
732 struct clockinfo clockinfo;
733 size = sizeof(clockinfo);
734 if (sysctl(clockrate_mib, 2, &clockinfo, &size, NULL, 0) == -1)
735 err(1, "sysctl kern.clockrate failed");
736 hz = clockinfo.stathz;
737 if (!hz)
738 hz = clockinfo.hz;
739 }
740
741 for (hdrcnt = 1;;) {
742 if (!--hdrcnt)
743 printhdr();
744 /* Read new disk statistics */
745 cpureadstats();
746 drvreadstats();
747 tkreadstats();
748 if (memf != NULL) {
749 struct uvmexp uvmexp_kernel;
750 /*
751 * XXX Can't do this if we're reading a crash
752 * XXX dump because they're lazily-calculated.
753 */
754 warnx("Unable to get vmtotals from crash dump.");
755 (void)memset(&total, 0, sizeof(total));
756 kread(namelist, X_UVMEXP, &uvmexp_kernel, sizeof(uvmexp_kernel));
757 #define COPY(field) uvmexp.field = uvmexp_kernel.field
758 COPY(pdreact);
759 COPY(pageins);
760 COPY(pgswapout);
761 COPY(pdfreed);
762 COPY(pdscans);
763 #undef COPY
764 } else {
765 size = sizeof(total);
766 if (sysctl(vmmeter_mib, __arraycount(vmmeter_mib),
767 &total, &size, NULL, 0) == -1) {
768 warn("Can't get vmtotals");
769 (void)memset(&total, 0, sizeof(total));
770 }
771 size = sizeof(uvmexp);
772 if (sysctl(uvmexp2_mib, __arraycount(uvmexp2_mib), &uvmexp,
773 &size, NULL, 0) == -1)
774 warn("sysctl vm.uvmexp2 failed");
775 }
776 cpucounters(&cpucounter);
777 ovflw = 0;
778 PRWORD(ovflw, " %*d", 2, 1, total.t_rq - 1);
779 PRWORD(ovflw, " %*d", 2, 1, total.t_dw + total.t_pw);
780 #define pgtok(a) (long)((a) * ((uint32_t)pagesize >> 10))
781 #define rate(x) (u_long)(((x) + halfuptime) / uptime) /* round */
782 PRWORD(ovflw, " %*ld", 9, 1, pgtok(total.t_avm));
783 PRWORD(ovflw, " %*ld", 7, 1, pgtok(total.t_free));
784 PRWORD(ovflw, " %*ld", 5, 1,
785 rate(cpucounter.nfault - ocpucounter.nfault));
786 PRWORD(ovflw, " %*ld", 4, 1,
787 rate(uvmexp.pdreact - ouvmexp.pdreact));
788 PRWORD(ovflw, " %*ld", 4, 1,
789 rate(uvmexp.pageins - ouvmexp.pageins));
790 PRWORD(ovflw, " %*ld", 5, 1,
791 rate(uvmexp.pgswapout - ouvmexp.pgswapout));
792 PRWORD(ovflw, " %*ld", 5, 1,
793 rate(uvmexp.pdfreed - ouvmexp.pdfreed));
794 PRWORD(ovflw, " %*ld", 6, 2,
795 rate(uvmexp.pdscans - ouvmexp.pdscans));
796 drvstats(&ovflw);
797 PRWORD(ovflw, " %*ld", 5, 1,
798 rate(cpucounter.nintr - ocpucounter.nintr));
799 PRWORD(ovflw, " %*ld", 5, 1,
800 rate(cpucounter.nsyscall - ocpucounter.nsyscall));
801 PRWORD(ovflw, " %*ld", 4, 1,
802 rate(cpucounter.nswtch - ocpucounter.nswtch));
803 cpustats(&ovflw);
804 (void)putchar('\n');
805 (void)fflush(stdout);
806 if (reps >= 0 && --reps <= 0)
807 break;
808 ouvmexp = uvmexp;
809 ocpucounter = cpucounter;
810 uptime = interval->tv_sec;
811 /*
812 * We round upward to avoid losing low-frequency events
813 * (i.e., >= 1 per interval but < 1 per second).
814 */
815 halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2;
816 (void)nanosleep(interval, NULL);
817 }
818 }
819
820 void
821 printhdr(void)
822 {
823 size_t i;
824
825 (void)printf(" procs memory page%*s", 23, "");
826 if (ndrives > 0)
827 (void)printf("%s %*sfaults cpu\n",
828 ((ndrives > 1) ? "disks" : "disk"),
829 ((ndrives > 1) ? ndrives * 3 - 4 : 0), "");
830 else
831 (void)printf("%*s faults cpu\n",
832 ndrives * 3, "");
833
834 (void)printf(" r b avm fre flt re pi po fr sr ");
835 for (i = 0; i < ndrive; i++)
836 if (drv_select[i])
837 (void)printf("%c%c ", dr_name[i][0],
838 dr_name[i][strlen(dr_name[i]) - 1]);
839 (void)printf(" in sy cs us sy id\n");
840 hdrcnt = winlines - 2;
841 }
842
843 /*
844 * Force a header to be prepended to the next output.
845 */
846 void
847 /*ARGSUSED*/
848 needhdr(int dummy)
849 {
850
851 hdrcnt = 1;
852 }
853
854 long
855 pct(long top, long bot)
856 {
857 long ans;
858
859 if (bot == 0)
860 return (0);
861 ans = (long)((quad_t)top * 100 / bot);
862 return (ans);
863 }
864
865 #define PCT(top, bot) (int)pct((long)(top), (long)(bot))
866
867 void
868 dosum(void)
869 {
870 struct nchstats_sysctl nch_stats;
871 uint64_t nchtotal;
872 size_t ssize;
873 int active_kernel;
874 struct cpu_counter cc;
875
876 /*
877 * The "active" and "inactive" variables
878 * are now estimated by the kernel and sadly
879 * can not easily be dug out of a crash dump.
880 */
881 ssize = sizeof(uvmexp);
882 memset(&uvmexp, 0, ssize);
883 active_kernel = (memf == NULL);
884 if (active_kernel) {
885 /* only on active kernel */
886 if (sysctl(uvmexp2_mib, __arraycount(uvmexp2_mib), &uvmexp,
887 &ssize, NULL, 0) == -1)
888 warn("sysctl vm.uvmexp2 failed");
889 } else {
890 struct uvmexp uvmexp_kernel;
891 kread(namelist, X_UVMEXP, &uvmexp_kernel, sizeof(uvmexp_kernel));
892 #define COPY(field) uvmexp.field = uvmexp_kernel.field
893 COPY(pagesize);
894 COPY(ncolors);
895 COPY(npages);
896 COPY(free);
897 COPY(paging);
898 COPY(wired);
899 COPY(zeropages);
900 COPY(reserve_pagedaemon);
901 COPY(reserve_kernel);
902 COPY(anonpages);
903 COPY(filepages);
904 COPY(execpages);
905 COPY(freemin);
906 COPY(freetarg);
907 COPY(wiredmax);
908 COPY(nswapdev);
909 COPY(swpages);
910 COPY(swpginuse);
911 COPY(nswget);
912 COPY(pageins);
913 COPY(pdpageouts);
914 COPY(pgswapin);
915 COPY(pgswapout);
916 COPY(forks);
917 COPY(forks_ppwait);
918 COPY(forks_sharevm);
919 COPY(pga_zerohit);
920 COPY(pga_zeromiss);
921 COPY(zeroaborts);
922 COPY(colorhit);
923 COPY(colormiss);
924 COPY(cpuhit);
925 COPY(cpumiss);
926 COPY(fltnoram);
927 COPY(fltnoanon);
928 COPY(fltpgwait);
929 COPY(fltpgrele);
930 COPY(fltrelck);
931 COPY(fltrelckok);
932 COPY(fltanget);
933 COPY(fltanretry);
934 COPY(fltamcopy);
935 COPY(fltamcopy);
936 COPY(fltnomap);
937 COPY(fltlget);
938 COPY(fltget);
939 COPY(flt_anon);
940 COPY(flt_acow);
941 COPY(flt_obj);
942 COPY(flt_prcopy);
943 COPY(flt_przero);
944 COPY(pdwoke);
945 COPY(pdrevs);
946 COPY(pdfreed);
947 COPY(pdscans);
948 COPY(pdanscan);
949 COPY(pdobscan);
950 COPY(pdreact);
951 COPY(pdbusy);
952 COPY(pdpending);
953 COPY(pddeact);
954 #undef COPY
955 }
956
957
958 (void)printf("%9" PRIu64 " bytes per page\n", uvmexp.pagesize);
959
960 (void)printf("%9" PRIu64 " page color%s\n",
961 uvmexp.ncolors, uvmexp.ncolors == 1 ? "" : "s");
962
963 (void)printf("%9" PRIu64 " pages managed\n", uvmexp.npages);
964 (void)printf("%9" PRIu64 " pages free\n", uvmexp.free);
965 if (active_kernel) {
966 (void)printf("%9" PRIu64 " pages active\n", uvmexp.active);
967 (void)printf("%9" PRIu64 " pages inactive\n", uvmexp.inactive);
968 }
969 (void)printf("%9" PRIu64 " pages paging\n", uvmexp.paging);
970 (void)printf("%9" PRIu64 " pages wired\n", uvmexp.wired);
971 (void)printf("%9" PRIu64 " zero pages\n", uvmexp.zeropages);
972 (void)printf("%9" PRIu64 " reserve pagedaemon pages\n",
973 uvmexp.reserve_pagedaemon);
974 (void)printf("%9" PRIu64 " reserve kernel pages\n", uvmexp.reserve_kernel);
975 (void)printf("%9" PRIu64 " anonymous pages\n", uvmexp.anonpages);
976 (void)printf("%9" PRIu64 " cached file pages\n", uvmexp.filepages);
977 (void)printf("%9" PRIu64 " cached executable pages\n", uvmexp.execpages);
978
979 (void)printf("%9" PRIu64 " minimum free pages\n", uvmexp.freemin);
980 (void)printf("%9" PRIu64 " target free pages\n", uvmexp.freetarg);
981 (void)printf("%9" PRIu64 " maximum wired pages\n", uvmexp.wiredmax);
982
983 (void)printf("%9" PRIu64 " swap devices\n", uvmexp.nswapdev);
984 (void)printf("%9" PRIu64 " swap pages\n", uvmexp.swpages);
985 (void)printf("%9" PRIu64 " swap pages in use\n", uvmexp.swpginuse);
986 (void)printf("%9" PRIu64 " swap allocations\n", uvmexp.nswget);
987
988 cpucounters(&cc);
989
990 (void)printf("%9" PRIu64 " total faults taken\n", cc.nfault);
991 (void)printf("%9" PRIu64 " traps\n", cc.ntrap);
992 (void)printf("%9" PRIu64 " device interrupts\n", cc.nintr);
993 (void)printf("%9" PRIu64 " CPU context switches\n", cc.nswtch);
994 (void)printf("%9" PRIu64 " software interrupts\n", cc.nsoft);
995 (void)printf("%9" PRIu64 " system calls\n", cc.nsyscall);
996 (void)printf("%9" PRIu64 " pagein requests\n", uvmexp.pageins);
997 (void)printf("%9" PRIu64 " pageout requests\n", uvmexp.pdpageouts);
998 (void)printf("%9" PRIu64 " pages swapped in\n", uvmexp.pgswapin);
999 (void)printf("%9" PRIu64 " pages swapped out\n", uvmexp.pgswapout);
1000 (void)printf("%9" PRIu64 " forks total\n", uvmexp.forks);
1001 (void)printf("%9" PRIu64 " forks blocked parent\n", uvmexp.forks_ppwait);
1002 (void)printf("%9" PRIu64 " forks shared address space with parent\n",
1003 uvmexp.forks_sharevm);
1004 (void)printf("%9" PRIu64 " pagealloc zero wanted and avail\n",
1005 uvmexp.pga_zerohit);
1006 (void)printf("%9" PRIu64 " pagealloc zero wanted and not avail\n",
1007 uvmexp.pga_zeromiss);
1008 (void)printf("%9" PRIu64 " aborts of idle page zeroing\n",
1009 uvmexp.zeroaborts);
1010 (void)printf("%9" PRIu64 " pagealloc desired color avail\n",
1011 uvmexp.colorhit);
1012 (void)printf("%9" PRIu64 " pagealloc desired color not avail\n",
1013 uvmexp.colormiss);
1014 (void)printf("%9" PRIu64 " pagealloc local cpu avail\n",
1015 uvmexp.cpuhit);
1016 (void)printf("%9" PRIu64 " pagealloc local cpu not avail\n",
1017 uvmexp.cpumiss);
1018
1019 (void)printf("%9" PRIu64 " faults with no memory\n", uvmexp.fltnoram);
1020 (void)printf("%9" PRIu64 " faults with no anons\n", uvmexp.fltnoanon);
1021 (void)printf("%9" PRIu64 " faults had to wait on pages\n", uvmexp.fltpgwait);
1022 (void)printf("%9" PRIu64 " faults found released page\n", uvmexp.fltpgrele);
1023 (void)printf("%9" PRIu64 " faults relock (%" PRIu64 " ok)\n", uvmexp.fltrelck,
1024 uvmexp.fltrelckok);
1025 (void)printf("%9" PRIu64 " anon page faults\n", uvmexp.fltanget);
1026 (void)printf("%9" PRIu64 " anon retry faults\n", uvmexp.fltanretry);
1027 (void)printf("%9" PRIu64 " amap copy faults\n", uvmexp.fltamcopy);
1028 (void)printf("%9" PRIu64 " neighbour anon page faults\n", uvmexp.fltnamap);
1029 (void)printf("%9" PRIu64 " neighbour object page faults\n", uvmexp.fltnomap);
1030 (void)printf("%9" PRIu64 " locked pager get faults\n", uvmexp.fltlget);
1031 (void)printf("%9" PRIu64 " unlocked pager get faults\n", uvmexp.fltget);
1032 (void)printf("%9" PRIu64 " anon faults\n", uvmexp.flt_anon);
1033 (void)printf("%9" PRIu64 " anon copy on write faults\n", uvmexp.flt_acow);
1034 (void)printf("%9" PRIu64 " object faults\n", uvmexp.flt_obj);
1035 (void)printf("%9" PRIu64 " promote copy faults\n", uvmexp.flt_prcopy);
1036 (void)printf("%9" PRIu64 " promote zero fill faults\n", uvmexp.flt_przero);
1037
1038 (void)printf("%9" PRIu64 " times daemon wokeup\n",uvmexp.pdwoke);
1039 (void)printf("%9" PRIu64 " revolutions of the clock hand\n", uvmexp.pdrevs);
1040 (void)printf("%9" PRIu64 " pages freed by daemon\n", uvmexp.pdfreed);
1041 (void)printf("%9" PRIu64 " pages scanned by daemon\n", uvmexp.pdscans);
1042 (void)printf("%9" PRIu64 " anonymous pages scanned by daemon\n",
1043 uvmexp.pdanscan);
1044 (void)printf("%9" PRIu64 " object pages scanned by daemon\n", uvmexp.pdobscan);
1045 (void)printf("%9" PRIu64 " pages reactivated\n", uvmexp.pdreact);
1046 (void)printf("%9" PRIu64 " pages found busy by daemon\n", uvmexp.pdbusy);
1047 (void)printf("%9" PRIu64 " total pending pageouts\n", uvmexp.pdpending);
1048 (void)printf("%9" PRIu64 " pages deactivated\n", uvmexp.pddeact);
1049
1050 if (active_kernel) {
1051 ssize = sizeof(nch_stats);
1052 if (sysctlbyname("vfs.namecache_stats", &nch_stats, &ssize,
1053 NULL, 0)) {
1054 warn("vfs.namecache_stats failed");
1055 memset(&nch_stats, 0, sizeof(nch_stats));
1056 }
1057 } else {
1058 struct nchstats nch_stats_kvm;
1059
1060 kread(namelist, X_NCHSTATS, &nch_stats_kvm,
1061 sizeof(nch_stats_kvm));
1062 nch_stats.ncs_goodhits = nch_stats_kvm.ncs_goodhits;
1063 nch_stats.ncs_neghits = nch_stats_kvm.ncs_neghits;
1064 nch_stats.ncs_badhits = nch_stats_kvm.ncs_badhits;
1065 nch_stats.ncs_falsehits = nch_stats_kvm.ncs_falsehits;
1066 nch_stats.ncs_miss = nch_stats_kvm.ncs_miss;
1067 nch_stats.ncs_long = nch_stats_kvm.ncs_long;
1068 nch_stats.ncs_pass2 = nch_stats_kvm.ncs_pass2;
1069 nch_stats.ncs_2passes = nch_stats_kvm.ncs_2passes;
1070 nch_stats.ncs_revhits = nch_stats_kvm.ncs_revhits;
1071 nch_stats.ncs_revmiss = nch_stats_kvm.ncs_revmiss;
1072 }
1073
1074 nchtotal = nch_stats.ncs_goodhits + nch_stats.ncs_neghits +
1075 nch_stats.ncs_badhits + nch_stats.ncs_falsehits +
1076 nch_stats.ncs_miss + nch_stats.ncs_long;
1077 (void)printf("%9" PRIu64 " total name lookups\n", nchtotal);
1078 (void)printf("%9" PRIu64 " good hits\n", nch_stats.ncs_goodhits);
1079 (void)printf("%9" PRIu64 " negative hits\n", nch_stats.ncs_neghits);
1080 (void)printf("%9" PRIu64 " bad hits\n", nch_stats.ncs_badhits);
1081 (void)printf("%9" PRIu64 " false hits\n", nch_stats.ncs_falsehits);
1082 (void)printf("%9" PRIu64 " miss\n", nch_stats.ncs_miss);
1083 (void)printf("%9" PRIu64 " too long\n", nch_stats.ncs_long);
1084 (void)printf("%9" PRIu64 " pass2 hits\n", nch_stats.ncs_pass2);
1085 (void)printf("%9" PRIu64 " 2passes\n", nch_stats.ncs_2passes);
1086 (void)printf(
1087 "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n",
1088 "", PCT(nch_stats.ncs_goodhits, nchtotal),
1089 PCT(nch_stats.ncs_neghits, nchtotal),
1090 PCT(nch_stats.ncs_pass2, nchtotal));
1091 (void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
1092 PCT(nch_stats.ncs_badhits, nchtotal),
1093 PCT(nch_stats.ncs_falsehits, nchtotal),
1094 PCT(nch_stats.ncs_long, nchtotal));
1095 }
1096
1097 void
1098 doforkst(void)
1099 {
1100 if (memf != NULL) {
1101 struct uvmexp uvmexp_kernel;
1102 kread(namelist, X_UVMEXP, &uvmexp_kernel, sizeof(uvmexp_kernel));
1103 #define COPY(field) uvmexp.field = uvmexp_kernel.field
1104 COPY(forks);
1105 COPY(forks_ppwait);
1106 COPY(forks_sharevm);
1107 #undef COPY
1108 } else {
1109 size_t size = sizeof(uvmexp);
1110 if (sysctl(uvmexp2_mib, __arraycount(uvmexp2_mib), &uvmexp,
1111 &size, NULL, 0) == -1)
1112 warn("sysctl vm.uvmexp2 failed");
1113 }
1114
1115 (void)printf("%" PRIu64 " forks total\n", uvmexp.forks);
1116 (void)printf("%" PRIu64 " forks blocked parent\n", uvmexp.forks_ppwait);
1117 (void)printf("%" PRIu64 " forks shared address space with parent\n",
1118 uvmexp.forks_sharevm);
1119 }
1120
1121 void
1122 drvstats(int *ovflwp)
1123 {
1124 size_t dn;
1125 double etime;
1126 int ovflw = *ovflwp;
1127
1128 /* Calculate disk stat deltas. */
1129 cpuswap();
1130 drvswap();
1131 tkswap();
1132 etime = cur.cp_etime;
1133
1134 for (dn = 0; dn < ndrive; ++dn) {
1135 if (!drv_select[dn])
1136 continue;
1137 PRWORD(ovflw, " %*.0f", 3, 1,
1138 (cur.rxfer[dn] + cur.wxfer[dn]) / etime);
1139 }
1140 *ovflwp = ovflw;
1141 }
1142
1143 void
1144 cpucounters(struct cpu_counter *cc)
1145 {
1146 static struct cpu_info **cpu_infos;
1147 static int initialised;
1148 struct cpu_info **slot;
1149
1150 if (memf == NULL) {
1151 cc->nintr = uvmexp.intrs;
1152 cc->nsyscall = uvmexp.syscalls;
1153 cc->nswtch = uvmexp.swtch;
1154 cc->nfault = uvmexp.faults;
1155 cc->ntrap = uvmexp.traps;
1156 cc->nsoft = uvmexp.softs;
1157 return;
1158 }
1159
1160 if (!initialised) {
1161 kread(namelist, X_CPU_INFOS, &cpu_infos, sizeof(cpu_infos));
1162 initialised = 1;
1163 }
1164
1165 slot = cpu_infos;
1166
1167 memset(cc, 0, sizeof(*cc));
1168
1169 for (;;) {
1170 struct cpu_info tci, *ci = NULL;
1171
1172 deref_kptr(slot++, &ci, sizeof(ci), "CPU array trashed");
1173 if (!ci) {
1174 break;
1175 }
1176
1177 if ((size_t)kvm_read(kd, (u_long)ci, &tci, sizeof(tci))
1178 != sizeof(tci)) {
1179 warnx("Can't read cpu info from %p (%s)",
1180 ci, kvm_geterr(kd));
1181 memset(cc, 0, sizeof(*cc));
1182 return;
1183 }
1184 cc->nintr += tci.ci_data.cpu_nintr;
1185 cc->nsyscall += tci.ci_data.cpu_nsyscall;
1186 cc->nswtch = tci.ci_data.cpu_nswtch;
1187 cc->nfault = tci.ci_data.cpu_nfault;
1188 cc->ntrap = tci.ci_data.cpu_ntrap;
1189 cc->nsoft = tci.ci_data.cpu_nsoft;
1190 }
1191 }
1192
1193 void
1194 cpustats(int *ovflwp)
1195 {
1196 int state;
1197 double pcnt, total;
1198 double stat_us, stat_sy, stat_id;
1199 int ovflw = *ovflwp;
1200
1201 total = 0;
1202 for (state = 0; state < CPUSTATES; ++state)
1203 total += cur.cp_time[state];
1204 if (total)
1205 pcnt = 100 / total;
1206 else
1207 pcnt = 0;
1208 stat_us = (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pcnt;
1209 stat_sy = (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pcnt;
1210 stat_id = cur.cp_time[CP_IDLE] * pcnt;
1211 PRWORD(ovflw, " %*.0f", ((stat_sy >= 100) ? 2 : 3), 1, stat_us);
1212 PRWORD(ovflw, " %*.0f", ((stat_us >= 100 || stat_id >= 100) ? 2 : 3), 1,
1213 stat_sy);
1214 PRWORD(ovflw, " %*.0f", 3, 1, stat_id);
1215 *ovflwp = ovflw;
1216 }
1217
1218 void
1219 dointr(int verbose)
1220 {
1221 unsigned long *intrcnt, *ointrcnt;
1222 unsigned long long inttotal, uptime;
1223 int nintr, inamlen;
1224 char *intrname, *ointrname;
1225
1226 inttotal = 0;
1227 uptime = getuptime();
1228 (void)printf("%-34s %16s %8s\n", "interrupt", "total", "rate");
1229 nintr = intrnl[X_EINTRCNT].n_value - intrnl[X_INTRCNT].n_value;
1230 inamlen = intrnl[X_EINTRNAMES].n_value - intrnl[X_INTRNAMES].n_value;
1231 if (nintr != 0 && inamlen != 0) {
1232 ointrcnt = intrcnt = malloc((size_t)nintr);
1233 ointrname = intrname = malloc((size_t)inamlen);
1234 if (intrcnt == NULL || intrname == NULL)
1235 errx(1, "%s", "");
1236 kread(intrnl, X_INTRCNT, intrcnt, (size_t)nintr);
1237 kread(intrnl, X_INTRNAMES, intrname, (size_t)inamlen);
1238 nintr /= sizeof(long);
1239 while (--nintr >= 0) {
1240 if (*intrcnt || verbose)
1241 (void)printf("%-34s %16llu %8llu\n", intrname,
1242 (unsigned long long)*intrcnt,
1243 (unsigned long long)
1244 (*intrcnt / uptime));
1245 intrname += strlen(intrname) + 1;
1246 inttotal += *intrcnt++;
1247 }
1248 free(ointrcnt);
1249 free(ointrname);
1250 }
1251
1252 doevcnt(verbose, EVCNT_TYPE_INTR);
1253 }
1254
1255 void
1256 doevcnt(int verbose, int type)
1257 {
1258 static const char * const evtypes [] = { "misc", "intr", "trap" };
1259 uint64_t counttotal, uptime;
1260 struct evcntlist allevents;
1261 struct evcnt evcnt, *evptr;
1262 char evgroup[EVCNT_STRING_MAX], evname[EVCNT_STRING_MAX];
1263
1264 counttotal = 0;
1265 uptime = getuptime();
1266 if (type == EVCNT_TYPE_ANY)
1267 (void)printf("%-34s %16s %8s %s\n", "event", "total", "rate",
1268 "type");
1269
1270 if (memf == NULL) do {
1271 const int mib[4] = { CTL_KERN, KERN_EVCNT, type,
1272 verbose ? KERN_EVCNT_COUNT_ANY : KERN_EVCNT_COUNT_NONZERO };
1273 size_t buflen = 0;
1274 void *buf = NULL;
1275 const struct evcnt_sysctl *evs, *last_evs;
1276 for (;;) {
1277 size_t newlen;
1278 int error;
1279 if (buflen)
1280 buf = malloc(buflen);
1281 error = sysctl(mib, __arraycount(mib),
1282 buf, &newlen, NULL, 0);
1283 if (error) {
1284 err(1, "kern.evcnt");
1285 if (buf)
1286 free(buf);
1287 return;
1288 }
1289 if (newlen <= buflen) {
1290 buflen = newlen;
1291 break;
1292 }
1293 if (buf)
1294 free(buf);
1295 buflen = newlen;
1296 }
1297 evs = buf;
1298 last_evs = (void *)((char *)buf + buflen);
1299 buflen /= sizeof(uint64_t);
1300 while (evs < last_evs
1301 && buflen >= sizeof(*evs)/sizeof(uint64_t)
1302 && buflen >= evs->ev_len) {
1303 (void)printf(type == EVCNT_TYPE_ANY ?
1304 "%s %s%*s %16"PRIu64" %8"PRIu64" %s\n" :
1305 "%s %s%*s %16"PRIu64" %8"PRIu64"\n",
1306 evs->ev_strings,
1307 evs->ev_strings + evs->ev_grouplen + 1,
1308 34 - (evs->ev_grouplen + 1 + evs->ev_namelen), "",
1309 evs->ev_count,
1310 evs->ev_count / uptime,
1311 (evs->ev_type < __arraycount(evtypes) ?
1312 evtypes[evs->ev_type] : "?"));
1313 buflen -= evs->ev_len;
1314 counttotal += evs->ev_count;
1315 evs = (const void *)((const uint64_t *)evs + evs->ev_len);
1316 }
1317 free(buf);
1318 if (type != EVCNT_TYPE_ANY)
1319 (void)printf("%-34s %16"PRIu64" %8"PRIu64"\n",
1320 "Total", counttotal, counttotal / uptime);
1321 return;
1322 } while (/*CONSTCOND*/ 0);
1323
1324 kread(namelist, X_ALLEVENTS, &allevents, sizeof allevents);
1325 evptr = TAILQ_FIRST(&allevents);
1326 while (evptr) {
1327 deref_kptr(evptr, &evcnt, sizeof(evcnt), "event chain trashed");
1328
1329 evptr = TAILQ_NEXT(&evcnt, ev_list);
1330 if (evcnt.ev_count == 0 && !verbose)
1331 continue;
1332 if (type != EVCNT_TYPE_ANY && evcnt.ev_type != type)
1333 continue;
1334
1335 deref_kptr(evcnt.ev_group, evgroup,
1336 (size_t)evcnt.ev_grouplen + 1, "event chain trashed");
1337 deref_kptr(evcnt.ev_name, evname,
1338 (size_t)evcnt.ev_namelen + 1, "event chain trashed");
1339
1340 (void)printf(type == EVCNT_TYPE_ANY ?
1341 "%s %s%*s %16"PRIu64" %8"PRIu64" %s\n" :
1342 "%s %s%*s %16"PRIu64" %8"PRIu64"\n",
1343 evgroup, evname,
1344 34 - (evcnt.ev_grouplen + 1 + evcnt.ev_namelen), "",
1345 evcnt.ev_count,
1346 (evcnt.ev_count / uptime),
1347 (evcnt.ev_type < __arraycount(evtypes) ?
1348 evtypes[evcnt.ev_type] : "?"));
1349
1350 counttotal += evcnt.ev_count;
1351 }
1352 if (type != EVCNT_TYPE_ANY)
1353 (void)printf("%-34s %16"PRIu64" %8"PRIu64"\n",
1354 "Total", counttotal, counttotal / uptime);
1355 }
1356
1357 static void
1358 dopool_sysctl(int verbose, int wide)
1359 {
1360 uint64_t total, inuse, this_total, this_inuse;
1361 struct {
1362 uint64_t pt_nget;
1363 uint64_t pt_nfail;
1364 uint64_t pt_nput;
1365 uint64_t pt_nout;
1366 uint64_t pt_nitems;
1367 uint64_t pt_npagealloc;
1368 uint64_t pt_npagefree;
1369 uint64_t pt_npages;
1370 } pool_totals;
1371 size_t i, len;
1372 int name_len, ovflw;
1373 struct pool_sysctl *pp, *data;
1374 char in_use[8], avail[8], maxp[32];
1375
1376 data = asysctlbyname("kern.pool", &len);
1377 if (data == NULL)
1378 err(1, "failed to reead kern.pool");
1379
1380 total = inuse = 0;
1381 len /= sizeof(*data);
1382
1383 (void)printf("Memory resource pool statistics\n");
1384 (void)printf(
1385 "%-*s%*s%*s%5s%*s%s%s%*s%*s%6s%s%6s%6s%6s%5s%s%s\n",
1386 wide ? 16 : 11, "Name",
1387 wide ? 6 : 5, "Size",
1388 wide ? 12 : 9, "Requests",
1389 "Fail",
1390 wide ? 12 : 9, "Releases",
1391 wide ? " InUse" : "",
1392 wide ? " Avail" : "",
1393 wide ? 7 : 6, "Pgreq",
1394 wide ? 7 : 6, "Pgrel",
1395 "Npage",
1396 wide ? " PageSz" : "",
1397 "Hiwat",
1398 "Minpg",
1399 "Maxpg",
1400 "Idle",
1401 wide ? " Flags" : "",
1402 wide ? " Util" : "");
1403
1404 name_len = MIN((int)sizeof(pp->pr_wchan), wide ? 16 : 11);
1405 for (i = 0; i < len; ++i) {
1406 pp = &data[i];
1407 if (pp->pr_nget == 0 && !verbose)
1408 continue;
1409 if (pp->pr_maxpages == UINT_MAX)
1410 (void)snprintf(maxp, sizeof(maxp), "inf");
1411 else
1412 (void)snprintf(maxp, sizeof(maxp), "%" PRIu64,
1413 pp->pr_maxpages);
1414 ovflw = 0;
1415 PRWORD(ovflw, "%-*s", name_len, 0, pp->pr_wchan);
1416 PRWORD(ovflw, " %*" PRIu64, wide ? 6 : 5, 1, pp->pr_size);
1417 PRWORD(ovflw, " %*" PRIu64, wide ? 12 : 9, 1, pp->pr_nget);
1418 pool_totals.pt_nget += pp->pr_nget;
1419 PRWORD(ovflw, " %*" PRIu64, 5, 1, pp->pr_nfail);
1420 pool_totals.pt_nfail += pp->pr_nfail;
1421 PRWORD(ovflw, " %*" PRIu64, wide ? 12 : 9, 1, pp->pr_nput);
1422 pool_totals.pt_nput += pp->pr_nput;
1423 if (wide) {
1424 PRWORD(ovflw, " %*" PRIu64, 7, 1, pp->pr_nout);
1425 pool_totals.pt_nout += pp->pr_nout;
1426 }
1427 if (wide) {
1428 PRWORD(ovflw, " %*" PRIu64, 6, 1, pp->pr_nitems);
1429 pool_totals.pt_nitems += pp->pr_nitems;
1430 }
1431 PRWORD(ovflw, " %*" PRIu64, wide ? 7 : 6, 1, pp->pr_npagealloc);
1432 pool_totals.pt_npagealloc += pp->pr_npagealloc;
1433 PRWORD(ovflw, " %*" PRIu64, wide ? 7 : 6, 1, pp->pr_npagefree);
1434 pool_totals.pt_npagefree += pp->pr_npagefree;
1435 PRWORD(ovflw, " %*" PRIu64, 6, 1, pp->pr_npages);
1436 pool_totals.pt_npages += pp->pr_npages;
1437 if (wide)
1438 PRWORD(ovflw, " %*" PRIu64, 7, 1, pp->pr_pagesize);
1439 PRWORD(ovflw, " %*" PRIu64, 6, 1, pp->pr_hiwat);
1440 PRWORD(ovflw, " %*" PRIu64, 6, 1, pp->pr_minpages);
1441 PRWORD(ovflw, " %*s", 6, 1, maxp);
1442 PRWORD(ovflw, " %*" PRIu64, 5, 1, pp->pr_nidle);
1443 if (wide)
1444 PRWORD(ovflw, " 0x%0*" PRIx64, 4, 1,
1445 pp->pr_flags);
1446
1447 this_inuse = pp->pr_nout * pp->pr_size;
1448 this_total = pp->pr_npages * pp->pr_pagesize;
1449 if (pp->pr_flags & PR_RECURSIVE) {
1450 /*
1451 * Don't count in-use memory, since it's part
1452 * of another pool and will be accounted for
1453 * there.
1454 */
1455 total += (this_total - this_inuse);
1456 } else {
1457 inuse += this_inuse;
1458 total += this_total;
1459 }
1460 if (wide) {
1461 if (this_total == 0)
1462 (void)printf(" ---");
1463 else
1464 (void)printf(" %5.1f%%",
1465 (100.0 * this_inuse) / this_total);
1466 }
1467 (void)printf("\n");
1468 }
1469 if (wide) {
1470 snprintf(in_use, sizeof in_use, "%7"PRId64, pool_totals.pt_nout);
1471 snprintf(avail, sizeof avail, "%6"PRId64, pool_totals.pt_nitems);
1472 } else {
1473 in_use[0] = '\0';
1474 avail[0] = '\0';
1475 }
1476 (void)printf(
1477 "%-*s%*s%*"PRId64"%5"PRId64"%*"PRId64"%s%s%*"PRId64"%*"PRId64"%6"PRId64"\n",
1478 wide ? 16 : 11, "Totals",
1479 wide ? 6 : 5, "",
1480 wide ? 12 : 9, pool_totals.pt_nget,
1481 pool_totals.pt_nfail,
1482 wide ? 12 : 9, pool_totals.pt_nput,
1483 in_use,
1484 avail,
1485 wide ? 7 : 6, pool_totals.pt_npagealloc,
1486 wide ? 7 : 6, pool_totals.pt_npagefree,
1487 pool_totals.pt_npages);
1488
1489 inuse /= KILO;
1490 total /= KILO;
1491 (void)printf(
1492 "\nIn use %" PRIu64 "K, "
1493 "total allocated %" PRIu64 "K; utilization %.1f%%\n",
1494 inuse, total, (100.0 * inuse) / total);
1495
1496 free(data);
1497 }
1498
1499 void
1500 dopool(int verbose, int wide)
1501 {
1502 int first, ovflw;
1503 void *addr;
1504 long total, inuse, this_total, this_inuse;
1505 struct {
1506 uint64_t pt_nget;
1507 uint64_t pt_nfail;
1508 uint64_t pt_nput;
1509 uint64_t pt_nout;
1510 uint64_t pt_nitems;
1511 uint64_t pt_npagealloc;
1512 uint64_t pt_npagefree;
1513 uint64_t pt_npages;
1514 } pool_totals;
1515 char in_use[8];
1516 char avail[8];
1517 TAILQ_HEAD(,pool) pool_head;
1518 struct pool pool, *pp = &pool;
1519 struct pool_allocator pa;
1520 char name[32], maxp[32];
1521
1522 if (memf == NULL)
1523 return dopool_sysctl(verbose, wide);
1524
1525 memset(&pool_totals, 0, sizeof pool_totals);
1526 kread(namelist, X_POOLHEAD, &pool_head, sizeof(pool_head));
1527 addr = TAILQ_FIRST(&pool_head);
1528
1529 total = inuse = 0;
1530
1531 for (first = 1; addr != NULL; addr = TAILQ_NEXT(pp, pr_poollist) ) {
1532 deref_kptr(addr, pp, sizeof(*pp), "pool chain trashed");
1533 deref_kptr(pp->pr_alloc, &pa, sizeof(pa),
1534 "pool allocator trashed");
1535 deref_kptr(pp->pr_wchan, name, sizeof(name),
1536 "pool wait channel trashed");
1537 name[sizeof(name)-1] = '\0';
1538
1539 if (first) {
1540 (void)printf("Memory resource pool statistics\n");
1541 (void)printf(
1542 "%-*s%*s%*s%5s%*s%s%s%*s%*s%6s%s%6s%6s%6s%5s%s%s\n",
1543 wide ? 16 : 11, "Name",
1544 wide ? 6 : 5, "Size",
1545 wide ? 12 : 9, "Requests",
1546 "Fail",
1547 wide ? 12 : 9, "Releases",
1548 wide ? " InUse" : "",
1549 wide ? " Avail" : "",
1550 wide ? 7 : 6, "Pgreq",
1551 wide ? 7 : 6, "Pgrel",
1552 "Npage",
1553 wide ? " PageSz" : "",
1554 "Hiwat",
1555 "Minpg",
1556 "Maxpg",
1557 "Idle",
1558 wide ? " Flags" : "",
1559 wide ? " Util" : "");
1560 first = 0;
1561 }
1562 if (pp->pr_nget == 0 && !verbose)
1563 continue;
1564 if (pp->pr_maxpages == UINT_MAX)
1565 (void)snprintf(maxp, sizeof(maxp), "inf");
1566 else
1567 (void)snprintf(maxp, sizeof(maxp), "%u",
1568 pp->pr_maxpages);
1569 ovflw = 0;
1570 PRWORD(ovflw, "%-*s", wide ? 16 : 11, 0, name);
1571 PRWORD(ovflw, " %*u", wide ? 6 : 5, 1, pp->pr_size);
1572 PRWORD(ovflw, " %*lu", wide ? 12 : 9, 1, pp->pr_nget);
1573 pool_totals.pt_nget += pp->pr_nget;
1574 PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nfail);
1575 pool_totals.pt_nfail += pp->pr_nfail;
1576 PRWORD(ovflw, " %*lu", wide ? 12 : 9, 1, pp->pr_nput);
1577 pool_totals.pt_nput += pp->pr_nput;
1578 if (wide) {
1579 PRWORD(ovflw, " %*u", 7, 1, pp->pr_nout);
1580 pool_totals.pt_nout += pp->pr_nout;
1581 }
1582 if (wide) {
1583 PRWORD(ovflw, " %*u", 6, 1, pp->pr_nitems);
1584 pool_totals.pt_nitems += pp->pr_nitems;
1585 }
1586 PRWORD(ovflw, " %*lu", wide ? 7 : 6, 1, pp->pr_npagealloc);
1587 pool_totals.pt_npagealloc += pp->pr_npagealloc;
1588 PRWORD(ovflw, " %*lu", wide ? 7 : 6, 1, pp->pr_npagefree);
1589 pool_totals.pt_npagefree += pp->pr_npagefree;
1590 PRWORD(ovflw, " %*u", 6, 1, pp->pr_npages);
1591 pool_totals.pt_npages += pp->pr_npages;
1592 if (wide)
1593 PRWORD(ovflw, " %*u", 7, 1, pa.pa_pagesz);
1594 PRWORD(ovflw, " %*u", 6, 1, pp->pr_hiwat);
1595 PRWORD(ovflw, " %*u", 6, 1, pp->pr_minpages);
1596 PRWORD(ovflw, " %*s", 6, 1, maxp);
1597 PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nidle);
1598 if (wide)
1599 PRWORD(ovflw, " 0x%0*x", 4, 1,
1600 pp->pr_flags | pp->pr_roflags);
1601
1602 this_inuse = pp->pr_nout * pp->pr_size;
1603 this_total = pp->pr_npages * pa.pa_pagesz;
1604 if (pp->pr_roflags & PR_RECURSIVE) {
1605 /*
1606 * Don't count in-use memory, since it's part
1607 * of another pool and will be accounted for
1608 * there.
1609 */
1610 total += (this_total - this_inuse);
1611 } else {
1612 inuse += this_inuse;
1613 total += this_total;
1614 }
1615 if (wide) {
1616 if (this_total == 0)
1617 (void)printf(" ---");
1618 else
1619 (void)printf(" %5.1f%%",
1620 (100.0 * this_inuse) / this_total);
1621 }
1622 (void)printf("\n");
1623 }
1624 if (wide) {
1625 snprintf(in_use, sizeof in_use, "%7"PRId64, pool_totals.pt_nout);
1626 snprintf(avail, sizeof avail, "%6"PRId64, pool_totals.pt_nitems);
1627 } else {
1628 in_use[0] = '\0';
1629 avail[0] = '\0';
1630 }
1631 (void)printf(
1632 "%-*s%*s%*"PRId64"%5"PRId64"%*"PRId64"%s%s%*"PRId64"%*"PRId64"%6"PRId64"\n",
1633 wide ? 16 : 11, "Totals",
1634 wide ? 6 : 5, "",
1635 wide ? 12 : 9, pool_totals.pt_nget,
1636 pool_totals.pt_nfail,
1637 wide ? 12 : 9, pool_totals.pt_nput,
1638 in_use,
1639 avail,
1640 wide ? 7 : 6, pool_totals.pt_npagealloc,
1641 wide ? 7 : 6, pool_totals.pt_npagefree,
1642 pool_totals.pt_npages);
1643
1644 inuse /= KILO;
1645 total /= KILO;
1646 (void)printf(
1647 "\nIn use %ldK, total allocated %ldK; utilization %.1f%%\n",
1648 inuse, total, (100.0 * inuse) / total);
1649 }
1650
1651 static void
1652 dopoolcache_sysctl(int verbose)
1653 {
1654 struct pool_sysctl *data, *pp;
1655 size_t i, len;
1656 bool first = true;
1657 int ovflw;
1658 uint64_t tot;
1659 float p;
1660
1661 data = asysctlbyname("kern.pool", &len);
1662 if (data == NULL)
1663 err(1, "failed to reead kern.pool");
1664 len /= sizeof(*data);
1665
1666 for (i = 0; i < len; ++i) {
1667 pp = &data[i];
1668 if (pp->pr_cache_meta_size == 0)
1669 continue;
1670
1671 if (pp->pr_cache_nmiss_global == 0 && !verbose)
1672 continue;
1673
1674 if (first) {
1675 (void)printf("Pool cache statistics.\n");
1676 (void)printf("%-*s%*s%*s%*s%*s%*s%*s%*s%*s%*s\n",
1677 12, "Name",
1678 6, "Spin",
1679 6, "GrpSz",
1680 5, "Full",
1681 5, "Emty",
1682 10, "PoolLayer",
1683 11, "CacheLayer",
1684 6, "Hit%",
1685 12, "CpuLayer",
1686 6, "Hit%"
1687 );
1688 first = false;
1689 }
1690
1691 ovflw = 0;
1692 PRWORD(ovflw, "%-*s", MIN((int)sizeof(pp->pr_wchan), 13), 1,
1693 pp->pr_wchan);
1694 PRWORD(ovflw, " %*" PRIu64, 6, 1, pp->pr_cache_ncontended);
1695 PRWORD(ovflw, " %*" PRIu64, 6, 1, pp->pr_cache_meta_size);
1696 PRWORD(ovflw, " %*" PRIu64, 5, 1, pp->pr_cache_nfull);
1697 PRWORD(ovflw, " %*" PRIu64, 5, 1, pp->pr_cache_nempty);
1698 PRWORD(ovflw, " %*" PRIu64, 10, 1, pp->pr_cache_nmiss_global);
1699
1700 tot = pp->pr_cache_nhit_global + pp->pr_cache_nmiss_global;
1701 p = pp->pr_cache_nhit_global * 100.0 / tot;
1702 PRWORD(ovflw, " %*" PRIu64, 11, 1, tot);
1703 PRWORD(ovflw, " %*.1f", 6, 1, p);
1704
1705 tot = pp->pr_cache_nhit_pcpu + pp->pr_cache_nmiss_pcpu;
1706 p = pp->pr_cache_nhit_pcpu * 100.0 / tot;
1707 PRWORD(ovflw, " %*" PRIu64, 12, 1, tot);
1708 PRWORD(ovflw, " %*.1f", 6, 1, p);
1709 printf("\n");
1710 }
1711 }
1712
1713 void
1714 dopoolcache(int verbose)
1715 {
1716 struct pool_cache pool_cache, *pc = &pool_cache;
1717 pool_cache_cpu_t cache_cpu, *cc = &cache_cpu;
1718 TAILQ_HEAD(,pool) pool_head;
1719 struct pool pool, *pp = &pool;
1720 char name[32];
1721 uint64_t cpuhit, cpumiss, tot;
1722 void *addr;
1723 int first, ovflw;
1724 size_t i;
1725 double p;
1726
1727 if (memf == NULL)
1728 return dopoolcache_sysctl(verbose);
1729
1730 kread(namelist, X_POOLHEAD, &pool_head, sizeof(pool_head));
1731 addr = TAILQ_FIRST(&pool_head);
1732
1733 for (first = 1; addr != NULL; addr = TAILQ_NEXT(pp, pr_poollist) ) {
1734 deref_kptr(addr, pp, sizeof(*pp), "pool chain trashed");
1735 if (pp->pr_cache == NULL)
1736 continue;
1737 deref_kptr(pp->pr_wchan, name, sizeof(name),
1738 "pool wait channel trashed");
1739 deref_kptr(pp->pr_cache, pc, sizeof(*pc), "pool cache trashed");
1740 if (pc->pc_misses == 0 && !verbose)
1741 continue;
1742 name[sizeof(name)-1] = '\0';
1743
1744 cpuhit = 0;
1745 cpumiss = 0;
1746 for (i = 0; i < __arraycount(pc->pc_cpus); i++) {
1747 if ((addr = pc->pc_cpus[i]) == NULL)
1748 continue;
1749 deref_kptr(addr, cc, sizeof(*cc),
1750 "pool cache cpu trashed");
1751 cpuhit += cc->cc_hits;
1752 cpumiss += cc->cc_misses;
1753 }
1754
1755 if (first) {
1756 (void)printf("Pool cache statistics.\n");
1757 (void)printf("%-*s%*s%*s%*s%*s%*s%*s%*s%*s%*s\n",
1758 12, "Name",
1759 6, "Spin",
1760 6, "GrpSz",
1761 5, "Full",
1762 5, "Emty",
1763 10, "PoolLayer",
1764 11, "CacheLayer",
1765 6, "Hit%",
1766 12, "CpuLayer",
1767 6, "Hit%"
1768 );
1769 first = 0;
1770 }
1771
1772 ovflw = 0;
1773 PRWORD(ovflw, "%-*s", 13, 1, name);
1774 PRWORD(ovflw, " %*llu", 6, 1, (long long)pc->pc_contended);
1775 PRWORD(ovflw, " %*u", 6, 1, pc->pc_pcgsize);
1776 PRWORD(ovflw, " %*u", 5, 1, pc->pc_nfull);
1777 PRWORD(ovflw, " %*u", 5, 1, pc->pc_nempty);
1778 PRWORD(ovflw, " %*llu", 10, 1, (long long)pc->pc_misses);
1779
1780 tot = pc->pc_hits + pc->pc_misses;
1781 p = pc->pc_hits * 100.0 / (tot);
1782 PRWORD(ovflw, " %*llu", 11, 1, (long long)tot);
1783 PRWORD(ovflw, " %*.1f", 6, 1, p);
1784
1785 tot = cpuhit + cpumiss;
1786 p = cpuhit * 100.0 / (tot);
1787 PRWORD(ovflw, " %*llu", 12, 1, (long long)tot);
1788 PRWORD(ovflw, " %*.1f", 6, 1, p);
1789 printf("\n");
1790 }
1791 }
1792
1793 enum hashtype { /* from <sys/systm.h> */
1794 HASH_LIST,
1795 HASH_TAILQ
1796 };
1797
1798 struct uidinfo { /* XXX: no kernel header file */
1799 LIST_ENTRY(uidinfo) ui_hash;
1800 uid_t ui_uid;
1801 long ui_proccnt;
1802 };
1803
1804 struct kernel_hash {
1805 const char * description; /* description */
1806 int hashsize; /* nlist index for hash size */
1807 int hashtbl; /* nlist index for hash table */
1808 enum hashtype type; /* type of hash table */
1809 size_t offset; /* offset of {LIST,TAILQ}_NEXT */
1810 } khashes[] =
1811 {
1812 {
1813 "buffer hash",
1814 X_BUFHASH, X_BUFHASHTBL,
1815 HASH_LIST, offsetof(struct buf, b_hash)
1816 }, {
1817 "ipv4 address -> interface hash",
1818 X_IFADDRHASH, X_IFADDRHASHTBL,
1819 HASH_LIST, offsetof(struct in_ifaddr, ia_hash),
1820 }, {
1821 "name cache hash",
1822 X_NCHASH, X_NCHASHTBL,
1823 HASH_LIST, offsetof(struct namecache, nc_hash),
1824 }, {
1825 "name cache directory hash",
1826 X_NCVHASH, X_NCVHASHTBL,
1827 HASH_LIST, offsetof(struct namecache, nc_vhash),
1828 }, {
1829 "user info (uid -> used processes) hash",
1830 X_UIHASH, X_UIHASHTBL,
1831 HASH_LIST, offsetof(struct uidinfo, ui_hash),
1832 }, {
1833 NULL, -1, -1, 0, 0,
1834 }
1835 };
1836
1837 void
1838 dohashstat(int verbose, int todo, const char *hashname)
1839 {
1840 LIST_HEAD(, generic) *hashtbl_list;
1841 TAILQ_HEAD(, generic) *hashtbl_tailq;
1842 struct kernel_hash *curhash;
1843 void *hashaddr, *hashbuf, *nhashbuf, *nextaddr;
1844 size_t elemsize, hashbufsize, thissize;
1845 u_long hashsize, i;
1846 int used, items, chain, maxchain;
1847
1848 hashbuf = NULL;
1849 hashbufsize = 0;
1850
1851 if (todo & HASHLIST) {
1852 (void)printf("Supported hashes:\n");
1853 for (curhash = khashes; curhash->description; curhash++) {
1854 if (hashnl[curhash->hashsize].n_value == 0 ||
1855 hashnl[curhash->hashtbl].n_value == 0)
1856 continue;
1857 (void)printf("\t%-16s%s\n",
1858 hashnl[curhash->hashsize].n_name + 1,
1859 curhash->description);
1860 }
1861 return;
1862 }
1863
1864 if (hashname != NULL) {
1865 for (curhash = khashes; curhash->description; curhash++) {
1866 if (strcmp(hashnl[curhash->hashsize].n_name + 1,
1867 hashname) == 0 &&
1868 hashnl[curhash->hashsize].n_value != 0 &&
1869 hashnl[curhash->hashtbl].n_value != 0)
1870 break;
1871 }
1872 if (curhash->description == NULL) {
1873 warnx("%s: no such hash", hashname);
1874 return;
1875 }
1876 }
1877
1878 (void)printf(
1879 "%-16s %8s %8s %8s %8s %8s %8s\n"
1880 "%-16s %8s %8s %8s %8s %8s %8s\n",
1881 "", "total", "used", "util", "num", "average", "maximum",
1882 "hash table", "buckets", "buckets", "%", "items", "chain",
1883 "chain");
1884
1885 for (curhash = khashes; curhash->description; curhash++) {
1886 if (hashnl[curhash->hashsize].n_value == 0 ||
1887 hashnl[curhash->hashtbl].n_value == 0)
1888 continue;
1889 if (hashname != NULL &&
1890 strcmp(hashnl[curhash->hashsize].n_name + 1, hashname))
1891 continue;
1892 elemsize = curhash->type == HASH_LIST ?
1893 sizeof(*hashtbl_list) : sizeof(*hashtbl_tailq);
1894 deref_kptr((void *)hashnl[curhash->hashsize].n_value,
1895 &hashsize, sizeof(hashsize),
1896 hashnl[curhash->hashsize].n_name);
1897 hashsize++;
1898 deref_kptr((void *)hashnl[curhash->hashtbl].n_value,
1899 &hashaddr, sizeof(hashaddr),
1900 hashnl[curhash->hashtbl].n_name);
1901 if (verbose)
1902 (void)printf(
1903 "%s %lu, %s %p, offset %ld, elemsize %llu\n",
1904 hashnl[curhash->hashsize].n_name + 1, hashsize,
1905 hashnl[curhash->hashtbl].n_name + 1, hashaddr,
1906 (long)curhash->offset,
1907 (unsigned long long)elemsize);
1908 thissize = hashsize * elemsize;
1909 if (hashbuf == NULL || thissize > hashbufsize) {
1910 if ((nhashbuf = realloc(hashbuf, thissize)) == NULL)
1911 errx(1, "malloc hashbuf %llu",
1912 (unsigned long long)hashbufsize);
1913 hashbuf = nhashbuf;
1914 hashbufsize = thissize;
1915 }
1916 deref_kptr(hashaddr, hashbuf, thissize,
1917 hashnl[curhash->hashtbl].n_name);
1918 used = 0;
1919 items = maxchain = 0;
1920 if (curhash->type == HASH_LIST) {
1921 hashtbl_list = hashbuf;
1922 hashtbl_tailq = NULL;
1923 } else {
1924 hashtbl_list = NULL;
1925 hashtbl_tailq = hashbuf;
1926 }
1927 for (i = 0; i < hashsize; i++) {
1928 if (curhash->type == HASH_LIST)
1929 nextaddr = LIST_FIRST(&hashtbl_list[i]);
1930 else
1931 nextaddr = TAILQ_FIRST(&hashtbl_tailq[i]);
1932 if (nextaddr == NULL)
1933 continue;
1934 if (verbose)
1935 (void)printf("%5lu: %p\n", i, nextaddr);
1936 used++;
1937 chain = 0;
1938 do {
1939 chain++;
1940 deref_kptr((char *)nextaddr + curhash->offset,
1941 &nextaddr, sizeof(void *),
1942 "hash chain corrupted");
1943 if (verbose > 1)
1944 (void)printf("got nextaddr as %p\n",
1945 nextaddr);
1946 } while (nextaddr != NULL);
1947 items += chain;
1948 if (verbose && chain > 1)
1949 (void)printf("\tchain = %d\n", chain);
1950 if (chain > maxchain)
1951 maxchain = chain;
1952 }
1953 (void)printf("%-16s %8ld %8d %8.2f %8d %8.2f %8d\n",
1954 hashnl[curhash->hashsize].n_name + 1,
1955 hashsize, used, used * 100.0 / hashsize,
1956 items, used ? (double)items / used : 0.0, maxchain);
1957 }
1958 }
1959
1960 /*
1961 * kreadc like kread but returns 1 if sucessful, 0 otherwise
1962 */
1963 int
1964 kreadc(struct nlist *nl, int nlx, void *addr, size_t size)
1965 {
1966 const char *sym;
1967
1968 sym = nl[nlx].n_name;
1969 if (*sym == '_')
1970 ++sym;
1971 if (nl[nlx].n_type == 0 || nl[nlx].n_value == 0)
1972 return 0;
1973 deref_kptr((void *)nl[nlx].n_value, addr, size, sym);
1974 return 1;
1975 }
1976
1977 /*
1978 * kread reads something from the kernel, given its nlist index in namelist[].
1979 */
1980 void
1981 kread(struct nlist *nl, int nlx, void *addr, size_t size)
1982 {
1983 const char *sym;
1984
1985 sym = nl[nlx].n_name;
1986 if (*sym == '_')
1987 ++sym;
1988 if (nl[nlx].n_type == 0 || nl[nlx].n_value == 0)
1989 errx(1, "symbol %s not defined", sym);
1990 deref_kptr((void *)nl[nlx].n_value, addr, size, sym);
1991 }
1992
1993 /*
1994 * Dereference the kernel pointer `kptr' and fill in the local copy
1995 * pointed to by `ptr'. The storage space must be pre-allocated,
1996 * and the size of the copy passed in `len'.
1997 */
1998 void
1999 deref_kptr(const void *kptr, void *ptr, size_t len, const char *msg)
2000 {
2001
2002 if (*msg == '_')
2003 msg++;
2004 if ((size_t)kvm_read(kd, (u_long)kptr, (char *)ptr, len) != len)
2005 errx(1, "kptr %lx: %s: %s", (u_long)kptr, msg, kvm_geterr(kd));
2006 }
2007
2008 /*
2009 * Traverse the kernel history buffers, performing the requested action.
2010 *
2011 * Note, we assume that if we're not listing, we're dumping.
2012 */
2013 void
2014 hist_traverse(int todo, const char *histname)
2015 {
2016 struct kern_history_head histhead;
2017 struct kern_history hist, *histkva;
2018 char *name = NULL;
2019 size_t namelen = 0;
2020
2021 if (histnl[0].n_value == 0) {
2022 warnx("kernel history is not compiled into the kernel.");
2023 return;
2024 }
2025
2026 deref_kptr((void *)histnl[X_KERN_HISTORIES].n_value, &histhead,
2027 sizeof(histhead), histnl[X_KERN_HISTORIES].n_name);
2028
2029 if (histhead.lh_first == NULL) {
2030 warnx("No active kernel history logs.");
2031 return;
2032 }
2033
2034 if (todo & HISTLIST)
2035 (void)printf("Active kernel histories:");
2036
2037 for (histkva = LIST_FIRST(&histhead); histkva != NULL;
2038 histkva = LIST_NEXT(&hist, list)) {
2039 deref_kptr(histkva, &hist, sizeof(hist), "histkva");
2040 if (name == NULL || hist.namelen > namelen) {
2041 if (name != NULL)
2042 free(name);
2043 namelen = hist.namelen;
2044 if ((name = malloc(namelen + 1)) == NULL)
2045 err(1, "malloc history name");
2046 }
2047
2048 deref_kptr(hist.name, name, namelen, "history name");
2049 name[namelen] = '\0';
2050 if (todo & HISTLIST)
2051 (void)printf(" %s", name);
2052 else {
2053 /*
2054 * If we're dumping all histories, do it, else
2055 * check to see if this is the one we want.
2056 */
2057 if (histname == NULL || strcmp(histname, name) == 0) {
2058 if (histname == NULL)
2059 (void)printf(
2060 "\nkernel history `%s':\n", name);
2061 hist_dodump(&hist);
2062 }
2063 }
2064 }
2065
2066 if (todo & HISTLIST)
2067 (void)putchar('\n');
2068
2069 if (name != NULL)
2070 free(name);
2071 }
2072
2073 /*
2074 * Actually dump the history buffer at the specified KVA.
2075 */
2076 void
2077 hist_dodump(struct kern_history *histp)
2078 {
2079 struct kern_history_ent *histents, *e;
2080 size_t histsize;
2081 char *fmt = NULL, *fn = NULL;
2082 size_t fmtlen = 0, fnlen = 0;
2083 unsigned i;
2084
2085 histsize = sizeof(struct kern_history_ent) * histp->n;
2086
2087 if ((histents = malloc(histsize)) == NULL)
2088 err(1, "malloc history entries");
2089
2090 (void)memset(histents, 0, histsize);
2091
2092 deref_kptr(histp->e, histents, histsize, "history entries");
2093 i = histp->f;
2094 do {
2095 e = &histents[i];
2096 if (e->fmt != NULL) {
2097 if (fmt == NULL || e->fmtlen > fmtlen) {
2098 if (fmt != NULL)
2099 free(fmt);
2100 fmtlen = e->fmtlen;
2101 if ((fmt = malloc(fmtlen + 1)) == NULL)
2102 err(1, "malloc printf format");
2103 }
2104 if (fn == NULL || e->fnlen > fnlen) {
2105 if (fn != NULL)
2106 free(fn);
2107 fnlen = e->fnlen;
2108 if ((fn = malloc(fnlen + 1)) == NULL)
2109 err(1, "malloc function name");
2110 }
2111
2112 deref_kptr(e->fmt, fmt, fmtlen, "printf format");
2113 fmt[fmtlen] = '\0';
2114
2115 deref_kptr(e->fn, fn, fnlen, "function name");
2116 fn[fnlen] = '\0';
2117
2118 (void)printf("%06ld.%06ld ", (long int)e->tv.tv_sec,
2119 (long int)e->tv.tv_usec);
2120 (void)printf("%s#%ld: ", fn, e->call);
2121 (void)printf(fmt, e->v[0], e->v[1], e->v[2], e->v[3]);
2122 (void)putchar('\n');
2123 }
2124 i = (i + 1) % histp->n;
2125 } while (i != histp->f);
2126
2127 free(histents);
2128 if (fmt != NULL)
2129 free(fmt);
2130 if (fn != NULL)
2131 free(fn);
2132 }
2133
2134 static void
2135 usage(void)
2136 {
2137
2138 (void)fprintf(stderr,
2139 "usage: %s [-CefHiLlmstUvW] [-c count] [-h hashname] [-M core] [-N system]\n"
2140 "\t\t[-u histname] [-w wait] [disks]\n", getprogname());
2141 exit(1);
2142 }
2143