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