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