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