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