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