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