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