vmstat.c revision 1.148 1 /* $NetBSD: vmstat.c,v 1.148 2006/07/21 05:26:53 simonb Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2000, 2001 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 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41 /*
42 * Copyright (c) 1980, 1986, 1991, 1993
43 * The Regents of the University of California. All rights reserved.
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
47 * are met:
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. Neither the name of the University nor the names of its contributors
54 * may be used to endorse or promote products derived from this software
55 * without specific prior written permission.
56 *
57 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 * SUCH DAMAGE.
68 */
69
70 #include <sys/cdefs.h>
71 #ifndef lint
72 __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1991, 1993\n\
73 The Regents of the University of California. All rights reserved.\n");
74 #endif /* not lint */
75
76 #ifndef lint
77 #if 0
78 static char sccsid[] = "@(#)vmstat.c 8.2 (Berkeley) 3/1/95";
79 #else
80 __RCSID("$NetBSD: vmstat.c,v 1.148 2006/07/21 05:26:53 simonb Exp $");
81 #endif
82 #endif /* not lint */
83
84 #define __POOL_EXPOSE
85
86 #include <sys/param.h>
87 #include <sys/mount.h>
88 #include <sys/uio.h>
89
90 #include <sys/buf.h>
91 #include <sys/evcnt.h>
92 #include <sys/ioctl.h>
93 #include <sys/malloc.h>
94 #include <sys/mallocvar.h>
95 #include <sys/namei.h>
96 #include <sys/pool.h>
97 #include <sys/proc.h>
98 #include <sys/sched.h>
99 #include <sys/socket.h>
100 #include <sys/sysctl.h>
101 #include <sys/time.h>
102 #include <sys/user.h>
103
104 #include <uvm/uvm_extern.h>
105 #include <uvm/uvm_stat.h>
106
107 #include <net/if.h>
108 #include <netinet/in.h>
109 #include <netinet/in_var.h>
110
111 #include <ufs/ufs/inode.h>
112
113 #include <nfs/rpcv2.h>
114 #include <nfs/nfsproto.h>
115 #include <nfs/nfsnode.h>
116
117 #include <ctype.h>
118 #include <err.h>
119 #include <errno.h>
120 #include <fcntl.h>
121 #include <kvm.h>
122 #include <limits.h>
123 #include <nlist.h>
124 #undef n_hash
125 #include <paths.h>
126 #include <signal.h>
127 #include <stdio.h>
128 #include <stddef.h>
129 #include <stdlib.h>
130 #include <string.h>
131 #include <time.h>
132 #include <unistd.h>
133 #include <util.h>
134
135 #include "drvstats.h"
136
137 /*
138 * General namelist
139 */
140 struct nlist namelist[] =
141 {
142 #define X_BOOTTIME 0
143 { "_boottime" },
144 #define X_HZ 1
145 { "_hz" },
146 #define X_STATHZ 2
147 { "_stathz" },
148 #define X_NCHSTATS 3
149 { "_nchstats" },
150 #define X_KMEMSTAT 4
151 { "_kmemstatistics" },
152 #define X_KMEMBUCKETS 5
153 { "_kmembuckets" },
154 #define X_ALLEVENTS 6
155 { "_allevents" },
156 #define X_POOLHEAD 7
157 { "_pool_head" },
158 #define X_UVMEXP 8
159 { "_uvmexp" },
160 #define X_TIME_SECOND 9
161 { "_time_second" },
162 #define X_TIME 10
163 { "_time" },
164 #define X_NL_SIZE 11
165 { NULL },
166 };
167
168 /*
169 * Namelist for pre-evcnt interrupt counters.
170 */
171 struct nlist intrnl[] =
172 {
173 #define X_INTRNAMES 0
174 { "_intrnames" },
175 #define X_EINTRNAMES 1
176 { "_eintrnames" },
177 #define X_INTRCNT 2
178 { "_intrcnt" },
179 #define X_EINTRCNT 3
180 { "_eintrcnt" },
181 #define X_INTRNL_SIZE 4
182 { NULL },
183 };
184
185
186 /*
187 * Namelist for hash statistics
188 */
189 struct nlist hashnl[] =
190 {
191 #define X_NFSNODE 0
192 { "_nfsnodehash" },
193 #define X_NFSNODETBL 1
194 { "_nfsnodehashtbl" },
195 #define X_IHASH 2
196 { "_ihash" },
197 #define X_IHASHTBL 3
198 { "_ihashtbl" },
199 #define X_BUFHASH 4
200 { "_bufhash" },
201 #define X_BUFHASHTBL 5
202 { "_bufhashtbl" },
203 #define X_UIHASH 6
204 { "_uihash" },
205 #define X_UIHASHTBL 7
206 { "_uihashtbl" },
207 #define X_IFADDRHASH 8
208 { "_in_ifaddrhash" },
209 #define X_IFADDRHASHTBL 9
210 { "_in_ifaddrhashtbl" },
211 #define X_NCHASH 10
212 { "_nchash" },
213 #define X_NCHASHTBL 11
214 { "_nchashtbl" },
215 #define X_NCVHASH 12
216 { "_ncvhash" },
217 #define X_NCVHASHTBL 13
218 { "_ncvhashtbl" },
219 #define X_HASHNL_SIZE 14 /* must be last */
220 { NULL },
221 };
222
223 /*
224 * Namelist for UVM histories
225 */
226 struct nlist histnl[] =
227 {
228 { "_uvm_histories" },
229 #define X_UVM_HISTORIES 0
230 { NULL },
231 };
232
233
234
235 struct uvmexp uvmexp, ouvmexp;
236 int ndrives;
237
238 int winlines = 20;
239
240 kvm_t *kd;
241
242 #define FORKSTAT 1<<0
243 #define INTRSTAT 1<<1
244 #define MEMSTAT 1<<2
245 #define SUMSTAT 1<<3
246 #define EVCNTSTAT 1<<4
247 #define VMSTAT 1<<5
248 #define HISTLIST 1<<6
249 #define HISTDUMP 1<<7
250 #define HASHSTAT 1<<8
251 #define HASHLIST 1<<9
252 #define VMTOTAL 1<<10
253
254 void cpustats(void);
255 void deref_kptr(const void *, void *, size_t, const char *);
256 void drvstats(void);
257 void doevcnt(int verbose);
258 void dohashstat(int, int, const char *);
259 void dointr(int verbose);
260 void domem(void);
261 void dopool(int, int);
262 void dopoolcache(struct pool *, int);
263 void dosum(void);
264 void dovmstat(struct timespec *, int);
265 void print_total_hdr(void);
266 void dovmtotal(struct timespec *, int);
267 void kread(struct nlist *, int, void *, size_t);
268 int kreadc(struct nlist *, int, void *, size_t);
269 void needhdr(int);
270 long getuptime(void);
271 void printhdr(void);
272 long pct(long, long);
273 void usage(void);
274 void doforkst(void);
275
276 void hist_traverse(int, const char *);
277 void hist_dodump(struct uvm_history *);
278
279 int main(int, char **);
280 char **choosedrives(char **);
281
282 /* Namelist and memory file names. */
283 char *nlistf, *memf;
284
285 /* allow old usage [vmstat 1] */
286 #define BACKWARD_COMPATIBILITY
287
288 int
289 main(int argc, char *argv[])
290 {
291 int c, todo, verbose, wide;
292 struct timespec interval;
293 int reps;
294 char errbuf[_POSIX2_LINE_MAX];
295 gid_t egid = getegid();
296 const char *histname, *hashname;
297
298 histname = hashname = NULL;
299 (void)setegid(getgid());
300 memf = nlistf = NULL;
301 reps = todo = verbose = wide = 0;
302 interval.tv_sec = 0;
303 interval.tv_nsec = 0;
304 while ((c = getopt(argc, argv, "c:efh:HilLM:mN:stu:UvWw:")) != -1) {
305 switch (c) {
306 case 'c':
307 reps = atoi(optarg);
308 break;
309 case 'e':
310 todo |= EVCNTSTAT;
311 break;
312 case 'f':
313 todo |= FORKSTAT;
314 break;
315 case 'h':
316 hashname = optarg;
317 /* FALLTHROUGH */
318 case 'H':
319 todo |= HASHSTAT;
320 break;
321 case 'i':
322 todo |= INTRSTAT;
323 break;
324 case 'l':
325 todo |= HISTLIST;
326 break;
327 case 'L':
328 todo |= HASHLIST;
329 break;
330 case 'M':
331 memf = optarg;
332 break;
333 case 'm':
334 todo |= MEMSTAT;
335 break;
336 case 'N':
337 nlistf = optarg;
338 break;
339 case 's':
340 todo |= SUMSTAT;
341 break;
342 case 't':
343 todo |= VMTOTAL;
344 break;
345 case 'u':
346 histname = optarg;
347 /* FALLTHROUGH */
348 case 'U':
349 todo |= HISTDUMP;
350 break;
351 case 'v':
352 verbose++;
353 break;
354 case 'W':
355 wide++;
356 break;
357 case 'w':
358 interval.tv_sec = atol(optarg);
359 break;
360 case '?':
361 default:
362 usage();
363 }
364 }
365 argc -= optind;
366 argv += optind;
367
368 if (todo == 0)
369 todo = VMSTAT;
370
371 /*
372 * Discard setgid privileges. If not the running kernel, we toss
373 * them away totally so that bad guys can't print interesting stuff
374 * from kernel memory, otherwise switch back to kmem for the
375 * duration of the kvm_openfiles() call.
376 */
377 if (nlistf != NULL || memf != NULL)
378 (void)setgid(getgid());
379 else
380 (void)setegid(egid);
381
382 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
383 if (kd == NULL)
384 errx(1, "kvm_openfiles: %s", errbuf);
385
386 if (nlistf == NULL && memf == NULL)
387 (void)setgid(getgid());
388
389 if ((c = kvm_nlist(kd, namelist)) != 0) {
390 int doexit = 0;
391 if (c == -1)
392 errx(1, "kvm_nlist: %s %s", "namelist", kvm_geterr(kd));
393 for (c = 0; c < sizeof(namelist) / sizeof(namelist[0])-1; c++)
394 if (namelist[c].n_type == 0 &&
395 c != X_TIME_SECOND &&
396 c != X_TIME) {
397 if (doexit++ == 0)
398 (void)fprintf(stderr, "vmstat: undefined symbols:");
399 fprintf(stderr, " %s", namelist[c].n_name);
400 }
401 if (doexit) {
402 (void)fputc('\n', stderr);
403 exit(1);
404 }
405 }
406 if (todo & INTRSTAT)
407 (void) kvm_nlist(kd, intrnl);
408 if ((c = kvm_nlist(kd, hashnl)) == -1 || c == X_HASHNL_SIZE)
409 errx(1, "kvm_nlist: %s %s", "hashnl", kvm_geterr(kd));
410 if (kvm_nlist(kd, histnl) == -1)
411 errx(1, "kvm_nlist: %s %s", "histnl", kvm_geterr(kd));
412
413 if (todo & VMSTAT) {
414 struct winsize winsize;
415
416 drvinit(0); /* Initialize disk stats, no disks selected. */
417
418 (void)setgid(getgid()); /* don't need privs anymore */
419
420 argv = choosedrives(argv); /* Select disks. */
421 winsize.ws_row = 0;
422 (void)ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize);
423 if (winsize.ws_row > 0)
424 winlines = winsize.ws_row;
425
426 }
427
428 #ifdef BACKWARD_COMPATIBILITY
429 if (*argv) {
430 interval.tv_sec = atol(*argv);
431 if (*++argv)
432 reps = atoi(*argv);
433 }
434 #endif
435
436 if (interval.tv_sec) {
437 if (!reps)
438 reps = -1;
439 } else if (reps)
440 interval.tv_sec = 1;
441
442
443 /*
444 * Statistics dumping is incompatible with the default
445 * VMSTAT/dovmstat() output. So perform the interval/reps handling
446 * for it here.
447 */
448 if ((todo & (VMSTAT|VMTOTAL)) == 0) {
449 for (;;) {
450 if (todo & (HISTLIST|HISTDUMP)) {
451 if ((todo & (HISTLIST|HISTDUMP)) ==
452 (HISTLIST|HISTDUMP))
453 errx(1, "you may list or dump,"
454 " but not both!");
455 hist_traverse(todo, histname);
456 putchar('\n');
457 }
458 if (todo & FORKSTAT) {
459 doforkst();
460 putchar('\n');
461 }
462 if (todo & MEMSTAT) {
463 domem();
464 dopool(verbose, wide);
465 putchar('\n');
466 }
467 if (todo & SUMSTAT) {
468 dosum();
469 putchar('\n');
470 }
471 if (todo & INTRSTAT) {
472 dointr(verbose);
473 putchar('\n');
474 }
475 if (todo & EVCNTSTAT) {
476 doevcnt(verbose);
477 putchar('\n');
478 }
479 if (todo & (HASHLIST|HASHSTAT)) {
480 if ((todo & (HASHLIST|HASHSTAT)) ==
481 (HASHLIST|HASHSTAT))
482 errx(1, "you may list or display,"
483 " but not both!");
484 dohashstat(verbose, todo, hashname);
485 putchar('\n');
486 }
487
488 if (reps >= 0 && --reps <=0)
489 break;
490 nanosleep(&interval, NULL);
491 }
492 } else {
493 if ((todo & (VMSTAT|VMTOTAL)) == (VMSTAT|VMTOTAL)) {
494 errx(1, "you may not both do vmstat and vmtotal");
495 }
496 if (todo & VMSTAT)
497 dovmstat(&interval, reps);
498 if (todo & VMTOTAL)
499 dovmtotal(&interval, reps);
500 }
501 exit(0);
502 }
503
504 char **
505 choosedrives(char **argv)
506 {
507 int i;
508
509 /*
510 * Choose drives to be displayed. Priority goes to (in order) drives
511 * supplied as arguments, default drives. If everything isn't filled
512 * in and there are drives not taken care of, display the first few
513 * that fit.
514 */
515 #define BACKWARD_COMPATIBILITY
516 for (ndrives = 0; *argv; ++argv) {
517 #ifdef BACKWARD_COMPATIBILITY
518 if (isdigit((unsigned char)**argv))
519 break;
520 #endif
521 for (i = 0; i < ndrive; i++) {
522 if (strcmp(dr_name[i], *argv))
523 continue;
524 drv_select[i] = 1;
525 ++ndrives;
526 break;
527 }
528 }
529 for (i = 0; i < ndrive && ndrives < 3; i++) {
530 if (drv_select[i])
531 continue;
532 drv_select[i] = 1;
533 ++ndrives;
534 }
535
536 return (argv);
537 }
538
539 long
540 getuptime(void)
541 {
542 static struct timeval boottime;
543 struct timeval now;
544 time_t uptime;
545
546 if (boottime.tv_sec == 0)
547 kread(namelist, X_BOOTTIME, &boottime, sizeof(boottime));
548 if (kreadc(namelist, X_TIME_SECOND, &now.tv_sec, sizeof(now))) {
549 now.tv_usec = 0;
550 } else {
551 kread(namelist, X_TIME, &now, sizeof(now));
552 }
553 uptime = now.tv_sec - boottime.tv_sec;
554 if (uptime <= 0 || uptime > 60*60*24*365*10)
555 errx(1, "time makes no sense; namelist must be wrong.");
556 return (uptime);
557 }
558
559 int hz, hdrcnt;
560
561 void
562 print_total_hdr()
563 {
564
565 (void)printf("procs memory\n");
566 (void)printf("ru dw pw sl sw");
567 (void)printf(" total-v active-v active-r");
568 (void)printf(" vm-sh avm-sh rm-sh arm-sh free\n");
569 hdrcnt = winlines - 2;
570 }
571
572 void
573 dovmtotal(struct timespec *interval, int reps)
574 {
575 struct vmtotal total;
576 int mib[2];
577 size_t size;
578
579 (void)signal(SIGCONT, needhdr);
580
581 for (hdrcnt = 1;;) {
582 if (!--hdrcnt)
583 print_total_hdr();
584 if (memf != NULL) {
585 printf("Unable to get vmtotals from crash dump.\n");
586 memset(&total, 0, sizeof(total));
587 } else {
588 size = sizeof(total);
589 mib[0] = CTL_VM;
590 mib[1] = VM_METER;
591 if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) {
592 printf("Can't get vmtotals: %s\n",
593 strerror(errno));
594 memset(&total, 0, sizeof(total));
595 }
596 }
597 printf("%2d ", total.t_rq);
598 printf("%2d ", total.t_dw);
599 printf("%2d ", total.t_pw);
600 printf("%2d ", total.t_sl);
601 printf("%2d ", total.t_sw);
602
603 printf("%9d ", total.t_vm);
604 printf("%9d ", total.t_avm);
605 printf("%9d ", total.t_arm);
606 printf("%5d ", total.t_vmshr);
607 printf("%6d ", total.t_avmshr);
608 printf("%5d ", total.t_rmshr);
609 printf("%6d ", total.t_armshr);
610 printf("%5d", total.t_free);
611
612 putchar('\n');
613
614 (void)fflush(stdout);
615 if (reps >= 0 && --reps <= 0)
616 break;
617
618 nanosleep(interval, NULL);
619 }
620 }
621
622 void
623 dovmstat(struct timespec *interval, int reps)
624 {
625 struct vmtotal total;
626 time_t uptime, halfuptime;
627 int mib[2];
628 size_t size;
629 int pagesize = getpagesize();
630
631 uptime = getuptime();
632 halfuptime = uptime / 2;
633 (void)signal(SIGCONT, needhdr);
634
635 if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0)
636 kread(namelist, X_STATHZ, &hz, sizeof(hz));
637 if (!hz)
638 kread(namelist, X_HZ, &hz, sizeof(hz));
639
640 for (hdrcnt = 1;;) {
641 if (!--hdrcnt)
642 printhdr();
643 /* Read new disk statistics */
644 cpureadstats();
645 drvreadstats();
646 tkreadstats();
647 kread(namelist, X_UVMEXP, &uvmexp, sizeof(uvmexp));
648 if (memf != NULL) {
649 /*
650 * XXX Can't do this if we're reading a crash
651 * XXX dump because they're lazily-calculated.
652 */
653 printf("Unable to get vmtotals from crash dump.\n");
654 memset(&total, 0, sizeof(total));
655 } else {
656 size = sizeof(total);
657 mib[0] = CTL_VM;
658 mib[1] = VM_METER;
659 if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) {
660 printf("Can't get vmtotals: %s\n",
661 strerror(errno));
662 memset(&total, 0, sizeof(total));
663 }
664 }
665 (void)printf("%2d %d %d",
666 total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
667 #define pgtok(a) (long)((a) * (pagesize >> 10))
668 #define rate(x) (u_long)(((x) + halfuptime) / uptime) /* round */
669 (void)printf(" %6ld %6ld ",
670 pgtok(total.t_avm), pgtok(total.t_free));
671 (void)printf("%4lu ", rate(uvmexp.faults - ouvmexp.faults));
672 (void)printf("%3lu ", rate(uvmexp.pdreact - ouvmexp.pdreact));
673 (void)printf("%3lu ", rate(uvmexp.pageins - ouvmexp.pageins));
674 (void)printf("%4lu ",
675 rate(uvmexp.pgswapout - ouvmexp.pgswapout));
676 (void)printf("%4lu ", rate(uvmexp.pdfreed - ouvmexp.pdfreed));
677 (void)printf("%4lu ", rate(uvmexp.pdscans - ouvmexp.pdscans));
678 drvstats();
679 (void)printf("%4lu %4lu %3lu ",
680 rate(uvmexp.intrs - ouvmexp.intrs),
681 rate(uvmexp.syscalls - ouvmexp.syscalls),
682 rate(uvmexp.swtch - ouvmexp.swtch));
683 cpustats();
684 putchar('\n');
685 (void)fflush(stdout);
686 if (reps >= 0 && --reps <= 0)
687 break;
688 ouvmexp = uvmexp;
689 uptime = interval->tv_sec;
690 /*
691 * We round upward to avoid losing low-frequency events
692 * (i.e., >= 1 per interval but < 1 per second).
693 */
694 halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2;
695 nanosleep(interval, NULL);
696 }
697 }
698
699 void
700 printhdr(void)
701 {
702 int i;
703
704 (void)printf(" procs memory page%*s", 23, "");
705 if (ndrives > 0)
706 (void)printf("%s %*sfaults cpu\n",
707 ((ndrives > 1) ? "disks" : "disk"),
708 ((ndrives > 1) ? ndrives * 3 - 4 : 0), "");
709 else
710 (void)printf("%*s faults cpu\n",
711 ndrives * 3, "");
712
713 (void)printf(" r b w avm fre flt re pi po fr sr ");
714 for (i = 0; i < ndrive; i++)
715 if (drv_select[i])
716 (void)printf("%c%c ", dr_name[i][0],
717 dr_name[i][strlen(dr_name[i]) - 1]);
718 (void)printf(" in sy cs us sy id\n");
719 hdrcnt = winlines - 2;
720 }
721
722 /*
723 * Force a header to be prepended to the next output.
724 */
725 void
726 needhdr(int dummy)
727 {
728
729 hdrcnt = 1;
730 }
731
732 long
733 pct(long top, long bot)
734 {
735 long ans;
736
737 if (bot == 0)
738 return (0);
739 ans = (quad_t)top * 100 / bot;
740 return (ans);
741 }
742
743 #define PCT(top, bot) (int)pct((long)(top), (long)(bot))
744
745 void
746 dosum(void)
747 {
748 struct nchstats nchstats;
749 u_long nchtotal;
750
751 kread(namelist, X_UVMEXP, &uvmexp, sizeof(uvmexp));
752
753 (void)printf("%9u bytes per page\n", uvmexp.pagesize);
754
755 (void)printf("%9u page color%s\n",
756 uvmexp.ncolors, uvmexp.ncolors == 1 ? "" : "s");
757
758 (void)printf("%9u pages managed\n", uvmexp.npages);
759 (void)printf("%9u pages free\n", uvmexp.free);
760 (void)printf("%9u pages active\n", uvmexp.active);
761 (void)printf("%9u pages inactive\n", uvmexp.inactive);
762 (void)printf("%9u pages paging\n", uvmexp.paging);
763 (void)printf("%9u pages wired\n", uvmexp.wired);
764 (void)printf("%9u zero pages\n", uvmexp.zeropages);
765 (void)printf("%9u reserve pagedaemon pages\n",
766 uvmexp.reserve_pagedaemon);
767 (void)printf("%9u reserve kernel pages\n", uvmexp.reserve_kernel);
768 (void)printf("%9u anonymous pages\n", uvmexp.anonpages);
769 (void)printf("%9u cached file pages\n", uvmexp.filepages);
770 (void)printf("%9u cached executable pages\n", uvmexp.execpages);
771
772 (void)printf("%9u minimum free pages\n", uvmexp.freemin);
773 (void)printf("%9u target free pages\n", uvmexp.freetarg);
774 (void)printf("%9u target inactive pages\n", uvmexp.inactarg);
775 (void)printf("%9u maximum wired pages\n", uvmexp.wiredmax);
776
777 (void)printf("%9u swap devices\n", uvmexp.nswapdev);
778 (void)printf("%9u swap pages\n", uvmexp.swpages);
779 (void)printf("%9u swap pages in use\n", uvmexp.swpginuse);
780 (void)printf("%9u swap allocations\n", uvmexp.nswget);
781
782 (void)printf("%9u total faults taken\n", uvmexp.faults);
783 (void)printf("%9u traps\n", uvmexp.traps);
784 (void)printf("%9u device interrupts\n", uvmexp.intrs);
785 (void)printf("%9u CPU context switches\n", uvmexp.swtch);
786 (void)printf("%9u software interrupts\n", uvmexp.softs);
787 (void)printf("%9u system calls\n", uvmexp.syscalls);
788 (void)printf("%9u pagein requests\n", uvmexp.pageins);
789 (void)printf("%9u pageout requests\n", uvmexp.pdpageouts);
790 (void)printf("%9u swap ins\n", uvmexp.swapins);
791 (void)printf("%9u swap outs\n", uvmexp.swapouts);
792 (void)printf("%9u pages swapped in\n", uvmexp.pgswapin);
793 (void)printf("%9u pages swapped out\n", uvmexp.pgswapout);
794 (void)printf("%9u forks total\n", uvmexp.forks);
795 (void)printf("%9u forks blocked parent\n", uvmexp.forks_ppwait);
796 (void)printf("%9u forks shared address space with parent\n",
797 uvmexp.forks_sharevm);
798 (void)printf("%9u pagealloc zero wanted and avail\n",
799 uvmexp.pga_zerohit);
800 (void)printf("%9u pagealloc zero wanted and not avail\n",
801 uvmexp.pga_zeromiss);
802 (void)printf("%9u aborts of idle page zeroing\n",
803 uvmexp.zeroaborts);
804 (void)printf("%9u pagealloc desired color avail\n",
805 uvmexp.colorhit);
806 (void)printf("%9u pagealloc desired color not avail\n",
807 uvmexp.colormiss);
808
809 (void)printf("%9u faults with no memory\n", uvmexp.fltnoram);
810 (void)printf("%9u faults with no anons\n", uvmexp.fltnoanon);
811 (void)printf("%9u faults had to wait on pages\n", uvmexp.fltpgwait);
812 (void)printf("%9u faults found released page\n", uvmexp.fltpgrele);
813 (void)printf("%9u faults relock (%u ok)\n", uvmexp.fltrelck,
814 uvmexp.fltrelckok);
815 (void)printf("%9u anon page faults\n", uvmexp.fltanget);
816 (void)printf("%9u anon retry faults\n", uvmexp.fltanretry);
817 (void)printf("%9u amap copy faults\n", uvmexp.fltamcopy);
818 (void)printf("%9u neighbour anon page faults\n", uvmexp.fltnamap);
819 (void)printf("%9u neighbour object page faults\n", uvmexp.fltnomap);
820 (void)printf("%9u locked pager get faults\n", uvmexp.fltlget);
821 (void)printf("%9u unlocked pager get faults\n", uvmexp.fltget);
822 (void)printf("%9u anon faults\n", uvmexp.flt_anon);
823 (void)printf("%9u anon copy on write faults\n", uvmexp.flt_acow);
824 (void)printf("%9u object faults\n", uvmexp.flt_obj);
825 (void)printf("%9u promote copy faults\n", uvmexp.flt_prcopy);
826 (void)printf("%9u promote zero fill faults\n", uvmexp.flt_przero);
827
828 (void)printf("%9u times daemon wokeup\n",uvmexp.pdwoke);
829 (void)printf("%9u revolutions of the clock hand\n", uvmexp.pdrevs);
830 (void)printf("%9u times daemon attempted swapout\n", uvmexp.pdswout);
831 (void)printf("%9u pages freed by daemon\n", uvmexp.pdfreed);
832 (void)printf("%9u pages scanned by daemon\n", uvmexp.pdscans);
833 (void)printf("%9u anonymous pages scanned by daemon\n",
834 uvmexp.pdanscan);
835 (void)printf("%9u object pages scanned by daemon\n", uvmexp.pdobscan);
836 (void)printf("%9u pages reactivated\n", uvmexp.pdreact);
837 (void)printf("%9u anonymous pages reactivated\n", uvmexp.pdreanon);
838 (void)printf("%9u cached file pages reactivated\n", uvmexp.pdrefile);
839 (void)printf("%9u cached executable pages reactivated\n",
840 uvmexp.pdreexec);
841 (void)printf("%9u pages found busy by daemon\n", uvmexp.pdbusy);
842 (void)printf("%9u total pending pageouts\n", uvmexp.pdpending);
843 (void)printf("%9u pages deactivated\n", uvmexp.pddeact);
844
845 kread(namelist, X_NCHSTATS, &nchstats, sizeof(nchstats));
846 nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
847 nchstats.ncs_badhits + nchstats.ncs_falsehits +
848 nchstats.ncs_miss + nchstats.ncs_long;
849 (void)printf("%9lu total name lookups\n", nchtotal);
850 (void)printf("%9lu good hits\n", nchstats.ncs_goodhits);
851 (void)printf("%9lu negative hits\n", nchstats.ncs_neghits);
852 (void)printf("%9lu bad hits\n", nchstats.ncs_badhits);
853 (void)printf("%9lu false hits\n", nchstats.ncs_falsehits);
854 (void)printf("%9lu miss\n", nchstats.ncs_miss);
855 (void)printf("%9lu too long\n", nchstats.ncs_long);
856 (void)printf("%9lu pass2 hits\n", nchstats.ncs_pass2);
857 (void)printf("%9lu 2passes\n", nchstats.ncs_2passes);
858 (void)printf(
859 "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n",
860 "", PCT(nchstats.ncs_goodhits, nchtotal),
861 PCT(nchstats.ncs_neghits, nchtotal),
862 PCT(nchstats.ncs_pass2, nchtotal));
863 (void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
864 PCT(nchstats.ncs_badhits, nchtotal),
865 PCT(nchstats.ncs_falsehits, nchtotal),
866 PCT(nchstats.ncs_long, nchtotal));
867 }
868
869 void
870 doforkst(void)
871 {
872
873 kread(namelist, X_UVMEXP, &uvmexp, sizeof(uvmexp));
874
875 (void)printf("%u forks total\n", uvmexp.forks);
876 (void)printf("%u forks blocked parent\n", uvmexp.forks_ppwait);
877 (void)printf("%u forks shared address space with parent\n",
878 uvmexp.forks_sharevm);
879 }
880
881 void
882 drvstats(void)
883 {
884 int dn;
885 double etime;
886
887 /* Calculate disk stat deltas. */
888 cpuswap();
889 drvswap();
890 tkswap();
891 etime = cur.cp_etime;
892
893 for (dn = 0; dn < ndrive; ++dn) {
894 if (!drv_select[dn])
895 continue;
896 (void)printf("%2.0f ", (cur.rxfer[dn] + cur.wxfer[dn]) / etime);
897 }
898 }
899
900 void
901 cpustats(void)
902 {
903 int state;
904 double pcnt, total;
905 double stat_us, stat_sy, stat_id;
906
907 total = 0;
908 for (state = 0; state < CPUSTATES; ++state)
909 total += cur.cp_time[state];
910 if (total)
911 pcnt = 100 / total;
912 else
913 pcnt = 0;
914 stat_us = (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pcnt;
915 stat_sy = (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pcnt;
916 stat_id = cur.cp_time[CP_IDLE] * pcnt;
917 (void)printf("%*.0f ", ((stat_sy >= 100) ? 1 : 2), stat_us);
918 (void)printf("%*.0f ", ((stat_us >= 100 || stat_id >= 100) ? 1 : 2),
919 stat_sy);
920 (void)printf("%2.0f", stat_id);
921 }
922
923 void
924 dointr(int verbose)
925 {
926 unsigned long *intrcnt, *ointrcnt;
927 unsigned long long inttotal, uptime;
928 int nintr, inamlen;
929 char *intrname, *ointrname;
930 struct evcntlist allevents;
931 struct evcnt evcnt, *evptr;
932 char evgroup[EVCNT_STRING_MAX], evname[EVCNT_STRING_MAX];
933
934 inttotal = 0;
935 uptime = getuptime();
936 (void)printf("%-34s %16s %8s\n", "interrupt", "total", "rate");
937 nintr = intrnl[X_EINTRCNT].n_value - intrnl[X_INTRCNT].n_value;
938 inamlen = intrnl[X_EINTRNAMES].n_value - intrnl[X_INTRNAMES].n_value;
939 if (nintr != 0 && inamlen != 0) {
940 ointrcnt = intrcnt = malloc((size_t)nintr);
941 ointrname = intrname = malloc((size_t)inamlen);
942 if (intrcnt == NULL || intrname == NULL)
943 errx(1, "%s", "");
944 kread(intrnl, X_INTRCNT, intrcnt, (size_t)nintr);
945 kread(intrnl, X_INTRNAMES, intrname, (size_t)inamlen);
946 nintr /= sizeof(long);
947 while (--nintr >= 0) {
948 if (*intrcnt || verbose)
949 (void)printf("%-34s %16llu %8llu\n", intrname,
950 (unsigned long long)*intrcnt,
951 (unsigned long long)
952 (*intrcnt / uptime));
953 intrname += strlen(intrname) + 1;
954 inttotal += *intrcnt++;
955 }
956 free(ointrcnt);
957 free(ointrname);
958 }
959
960 kread(namelist, X_ALLEVENTS, &allevents, sizeof allevents);
961 evptr = TAILQ_FIRST(&allevents);
962 while (evptr) {
963 deref_kptr(evptr, &evcnt, sizeof(evcnt), "event chain trashed");
964 evptr = TAILQ_NEXT(&evcnt, ev_list);
965 if (evcnt.ev_type != EVCNT_TYPE_INTR)
966 continue;
967
968 if (evcnt.ev_count == 0 && !verbose)
969 continue;
970
971 deref_kptr(evcnt.ev_group, evgroup, evcnt.ev_grouplen + 1,
972 "event chain trashed");
973 deref_kptr(evcnt.ev_name, evname, evcnt.ev_namelen + 1,
974 "event chain trashed");
975
976 (void)printf("%s %s%*s %16llu %8llu\n", evgroup, evname,
977 34 - (evcnt.ev_grouplen + 1 + evcnt.ev_namelen), "",
978 (unsigned long long)evcnt.ev_count,
979 (unsigned long long)(evcnt.ev_count / uptime));
980
981 inttotal += evcnt.ev_count++;
982 }
983 (void)printf("%-34s %16llu %8llu\n", "Total", inttotal,
984 (unsigned long long)(inttotal / uptime));
985 }
986
987 void
988 doevcnt(int verbose)
989 {
990 static const char * evtypes [] = { "misc", "intr", "trap" };
991 unsigned long long uptime;
992 struct evcntlist allevents;
993 struct evcnt evcnt, *evptr;
994 char evgroup[EVCNT_STRING_MAX], evname[EVCNT_STRING_MAX];
995
996 /* XXX should print type! */
997
998 uptime = getuptime();
999 (void)printf("%-34s %16s %8s %s\n", "event", "total", "rate", "type");
1000 kread(namelist, X_ALLEVENTS, &allevents, sizeof allevents);
1001 evptr = TAILQ_FIRST(&allevents);
1002 while (evptr) {
1003 deref_kptr(evptr, &evcnt, sizeof(evcnt), "event chain trashed");
1004
1005 evptr = TAILQ_NEXT(&evcnt, ev_list);
1006 if (evcnt.ev_count == 0 && !verbose)
1007 continue;
1008
1009 deref_kptr(evcnt.ev_group, evgroup, evcnt.ev_grouplen + 1,
1010 "event chain trashed");
1011 deref_kptr(evcnt.ev_name, evname, evcnt.ev_namelen + 1,
1012 "event chain trashed");
1013
1014 (void)printf("%s %s%*s %16llu %8llu %s\n", evgroup, evname,
1015 34 - (evcnt.ev_grouplen + 1 + evcnt.ev_namelen), "",
1016 (unsigned long long)evcnt.ev_count,
1017 (unsigned long long)(evcnt.ev_count / uptime),
1018 (evcnt.ev_type < sizeof(evtypes)/sizeof(evtypes[0]) ?
1019 evtypes[evcnt.ev_type] : "?"));
1020 }
1021 }
1022
1023 static char memname[64];
1024
1025 void
1026 domem(void)
1027 {
1028 struct kmembuckets *kp;
1029 struct malloc_type ks, *ksp;
1030 int i, j;
1031 int len, size, first;
1032 long totuse = 0, totfree = 0, totreq = 0;
1033 struct kmembuckets buckets[MINBUCKET + 16];
1034
1035 kread(namelist, X_KMEMBUCKETS, buckets, sizeof(buckets));
1036 for (first = 1, i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16;
1037 i++, kp++) {
1038 if (kp->kb_calls == 0)
1039 continue;
1040 if (first) {
1041 (void)printf("Memory statistics by bucket size\n");
1042 (void)printf(
1043 " Size In Use Free Requests HighWater Couldfree\n");
1044 first = 0;
1045 }
1046 size = 1 << i;
1047 (void)printf("%8d %8ld %6ld %10ld %7ld %10ld\n", size,
1048 kp->kb_total - kp->kb_totalfree,
1049 kp->kb_totalfree, kp->kb_calls,
1050 kp->kb_highwat, kp->kb_couldfree);
1051 totfree += size * kp->kb_totalfree;
1052 }
1053
1054 /*
1055 * If kmem statistics are not being gathered by the kernel,
1056 * first will still be 1.
1057 */
1058 if (first) {
1059 warnx("Kmem statistics are not being gathered by the kernel.");
1060 return;
1061 }
1062
1063 (void)printf("\nMemory usage type by bucket size\n");
1064 (void)printf(" Size Type(s)\n");
1065 kp = &buckets[MINBUCKET];
1066 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) {
1067 if (kp->kb_calls == 0)
1068 continue;
1069 first = 1;
1070 len = 8;
1071 for (kread(namelist, X_KMEMSTAT, &ksp, sizeof(ksp));
1072 ksp != NULL; ksp = ks.ks_next) {
1073 deref_kptr(ksp, &ks, sizeof(ks), "malloc type");
1074 if (ks.ks_calls == 0)
1075 continue;
1076 if ((ks.ks_size & j) == 0)
1077 continue;
1078 deref_kptr(ks.ks_shortdesc, memname,
1079 sizeof(memname), "malloc type name");
1080 len += 2 + strlen(memname);
1081 if (first)
1082 printf("%8d %s", j, memname);
1083 else
1084 printf(",");
1085 if (len >= 80) {
1086 printf("\n\t ");
1087 len = 10 + strlen(memname);
1088 }
1089 if (!first)
1090 printf(" %s", memname);
1091 first = 0;
1092 }
1093 putchar('\n');
1094 }
1095
1096 (void)printf(
1097 "\nMemory statistics by type Type Kern\n");
1098 (void)printf(
1099 " Type InUse MemUse HighUse Limit Requests Limit Limit Size(s)\n");
1100 for (kread(namelist, X_KMEMSTAT, &ksp, sizeof(ksp));
1101 ksp != NULL; ksp = ks.ks_next) {
1102 deref_kptr(ksp, &ks, sizeof(ks), "malloc type");
1103 if (ks.ks_calls == 0)
1104 continue;
1105 deref_kptr(ks.ks_shortdesc, memname,
1106 sizeof(memname), "malloc type name");
1107 (void)printf("%14s%6ld%6ldK%7ldK%6ldK%10ld%5u%6u",
1108 memname,
1109 ks.ks_inuse, (ks.ks_memuse + 1023) / 1024,
1110 (ks.ks_maxused + 1023) / 1024,
1111 (ks.ks_limit + 1023) / 1024, ks.ks_calls,
1112 ks.ks_limblocks, ks.ks_mapblocks);
1113 first = 1;
1114 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
1115 if ((ks.ks_size & j) == 0)
1116 continue;
1117 if (first)
1118 printf(" %d", j);
1119 else
1120 printf(",%d", j);
1121 first = 0;
1122 }
1123 printf("\n");
1124 totuse += ks.ks_memuse;
1125 totreq += ks.ks_calls;
1126 }
1127 (void)printf("\nMemory totals: In Use Free Requests\n");
1128 (void)printf(" %7ldK %6ldK %8ld\n\n",
1129 (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
1130 }
1131
1132 void
1133 dopool(int verbose, int wide)
1134 {
1135 int first, ovflw;
1136 void *addr;
1137 long total, inuse, this_total, this_inuse;
1138 LIST_HEAD(,pool) pool_head;
1139 struct pool pool, *pp = &pool;
1140 struct pool_allocator pa;
1141 char name[32], maxp[32];
1142
1143 kread(namelist, X_POOLHEAD, &pool_head, sizeof(pool_head));
1144 addr = LIST_FIRST(&pool_head);
1145
1146 total = inuse = 0;
1147
1148 for (first = 1; addr != NULL; addr = LIST_NEXT(pp, pr_poollist) ) {
1149 deref_kptr(addr, pp, sizeof(*pp), "pool chain trashed");
1150 deref_kptr(pp->pr_alloc, &pa, sizeof(pa),
1151 "pool allocator trashed");
1152 deref_kptr(pp->pr_wchan, name, sizeof(name),
1153 "pool wait channel trashed");
1154 name[sizeof(name)-1] = '\0';
1155
1156 if (first) {
1157 (void)printf("Memory resource pool statistics\n");
1158 (void)printf(
1159 "%-*s%*s%*s%5s%*s%s%s%*s%*s%6s%s%6s%6s%6s%5s%s%s\n",
1160 wide ? 16 : 11, "Name",
1161 wide ? 6 : 5, "Size",
1162 wide ? 12 : 9, "Requests",
1163 "Fail",
1164 wide ? 12 : 9, "Releases",
1165 wide ? " InUse" : "",
1166 wide ? " Avail" : "",
1167 wide ? 7 : 6, "Pgreq",
1168 wide ? 7 : 6, "Pgrel",
1169 "Npage",
1170 wide ? " PageSz" : "",
1171 "Hiwat",
1172 "Minpg",
1173 "Maxpg",
1174 "Idle",
1175 wide ? " Flags" : "",
1176 wide ? " Util" : "");
1177 first = 0;
1178 }
1179 if (pp->pr_nget == 0 && !verbose)
1180 continue;
1181 if (pp->pr_maxpages == UINT_MAX)
1182 snprintf(maxp, sizeof(maxp), "inf");
1183 else
1184 snprintf(maxp, sizeof(maxp), "%u", pp->pr_maxpages);
1185 /*
1186 * Print single word. `ovflow' is number of characters didn't fit
1187 * on the last word. `fmt' is a format string to print this word.
1188 * It must contain asterisk for field width. `width' is a width
1189 * occupied by this word. `fixed' is a number of constant chars in
1190 * `fmt'. `val' is a value to be printed using format string `fmt'.
1191 */
1192 #define PRWORD(ovflw, fmt, width, fixed, val) do { \
1193 (ovflw) += printf((fmt), \
1194 (width) - (fixed) - (ovflw) > 0 ? \
1195 (width) - (fixed) - (ovflw) : 0, \
1196 (val)) - (width); \
1197 if ((ovflw) < 0) \
1198 (ovflw) = 0; \
1199 } while (/* CONSTCOND */0)
1200 ovflw = 0;
1201 PRWORD(ovflw, "%-*s", wide ? 16 : 11, 0, name);
1202 PRWORD(ovflw, " %*u", wide ? 6 : 5, 1, pp->pr_size);
1203 PRWORD(ovflw, " %*lu", wide ? 12 : 9, 1, pp->pr_nget);
1204 PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nfail);
1205 PRWORD(ovflw, " %*lu", wide ? 12 : 9, 1, pp->pr_nput);
1206 if (wide)
1207 PRWORD(ovflw, " %*u", 7, 1, pp->pr_nout);
1208 if (wide)
1209 PRWORD(ovflw, " %*u", 6, 1, pp->pr_nitems);
1210 PRWORD(ovflw, " %*lu", wide ? 7 : 6, 1, pp->pr_npagealloc);
1211 PRWORD(ovflw, " %*lu", wide ? 7 : 6, 1, pp->pr_npagefree);
1212 PRWORD(ovflw, " %*u", 6, 1, pp->pr_npages);
1213 if (wide)
1214 PRWORD(ovflw, " %*u", 7, 1, pa.pa_pagesz);
1215 PRWORD(ovflw, " %*u", 6, 1, pp->pr_hiwat);
1216 PRWORD(ovflw, " %*u", 6, 1, pp->pr_minpages);
1217 PRWORD(ovflw, " %*s", 6, 1, maxp);
1218 PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nidle);
1219 if (wide)
1220 PRWORD(ovflw, " 0x%0*x", 4, 1,
1221 pp->pr_flags | pp->pr_roflags);
1222
1223 this_inuse = pp->pr_nout * pp->pr_size;
1224 this_total = pp->pr_npages * pa.pa_pagesz;
1225 if (pp->pr_roflags & PR_RECURSIVE) {
1226 /*
1227 * Don't count in-use memory, since it's part
1228 * of another pool and will be accounted for
1229 * there.
1230 */
1231 total += (this_total - this_inuse);
1232 } else {
1233 inuse += this_inuse;
1234 total += this_total;
1235 }
1236 if (wide) {
1237 if (this_total == 0)
1238 printf(" ---");
1239 else
1240 printf(" %5.1f%%",
1241 (100.0 * this_inuse) / this_total);
1242 }
1243 printf("\n");
1244 dopoolcache(pp, verbose);
1245 }
1246
1247 inuse /= 1024;
1248 total /= 1024;
1249 printf("\nIn use %ldK, total allocated %ldK; utilization %.1f%%\n",
1250 inuse, total, (100.0 * inuse) / total);
1251 }
1252
1253 void
1254 dopoolcache(struct pool *pp, int verbose)
1255 {
1256 struct pool_cache pool_cache, *pc = &pool_cache;
1257 struct pool_cache_group pool_cache_group, *pcg = &pool_cache_group;
1258 void *addr, *pcg_addr;
1259 int i;
1260
1261 if (verbose < 1)
1262 return;
1263
1264 #define PR_GROUPLIST \
1265 deref_kptr(pcg_addr, pcg, sizeof(*pcg), \
1266 "pool cache group trashed"); \
1267 printf("\t\tgroup %p: avail %d\n", pcg_addr, \
1268 pcg->pcg_avail); \
1269 for (i = 0; i < PCG_NOBJECTS; i++) { \
1270 if (pcg->pcg_objects[i].pcgo_pa != \
1271 POOL_PADDR_INVALID) { \
1272 printf("\t\t\t%p, 0x%llx\n", \
1273 pcg->pcg_objects[i].pcgo_va, \
1274 (unsigned long long) \
1275 pcg->pcg_objects[i].pcgo_pa); \
1276 } else { \
1277 printf("\t\t\t%p\n", \
1278 pcg->pcg_objects[i].pcgo_va); \
1279 } \
1280 }
1281
1282 for (addr = LIST_FIRST(&pp->pr_cachelist); addr != NULL;
1283 addr = LIST_NEXT(pc, pc_poollist)) {
1284 deref_kptr(addr, pc, sizeof(*pc), "pool cache trashed");
1285 printf("\t hits %lu misses %lu ngroups %lu nitems %lu\n",
1286 pc->pc_hits, pc->pc_misses, pc->pc_ngroups, pc->pc_nitems);
1287 if (verbose < 2)
1288 continue;
1289 printf("\t full groups:\n");
1290 for (pcg_addr = LIST_FIRST(&pc->pc_fullgroups);
1291 pcg_addr != NULL; pcg_addr = LIST_NEXT(pcg, pcg_list)) {
1292 PR_GROUPLIST;
1293 }
1294 printf("\t partial groups:\n");
1295 for (pcg_addr = LIST_FIRST(&pc->pc_partgroups);
1296 pcg_addr != NULL; pcg_addr = LIST_NEXT(pcg, pcg_list)) {
1297 PR_GROUPLIST;
1298 }
1299 printf("\t empty groups:\n");
1300 for (pcg_addr = LIST_FIRST(&pc->pc_emptygroups);
1301 pcg_addr != NULL; pcg_addr = LIST_NEXT(pcg, pcg_list)) {
1302 PR_GROUPLIST;
1303 }
1304 }
1305
1306 #undef PR_GROUPLIST
1307
1308 }
1309
1310 enum hashtype { /* from <sys/systm.h> */
1311 HASH_LIST,
1312 HASH_TAILQ
1313 };
1314
1315 struct uidinfo { /* XXX: no kernel header file */
1316 LIST_ENTRY(uidinfo) ui_hash;
1317 uid_t ui_uid;
1318 long ui_proccnt;
1319 };
1320
1321 struct kernel_hash {
1322 const char * description; /* description */
1323 int hashsize; /* nlist index for hash size */
1324 int hashtbl; /* nlist index for hash table */
1325 enum hashtype type; /* type of hash table */
1326 size_t offset; /* offset of {LIST,TAILQ}_NEXT */
1327 } khashes[] =
1328 {
1329 {
1330 "buffer hash",
1331 X_BUFHASH, X_BUFHASHTBL,
1332 HASH_LIST, offsetof(struct buf, b_hash)
1333 }, {
1334 "inode cache (ihash)",
1335 X_IHASH, X_IHASHTBL,
1336 HASH_LIST, offsetof(struct inode, i_hash)
1337 }, {
1338 "ipv4 address -> interface hash",
1339 X_IFADDRHASH, X_IFADDRHASHTBL,
1340 HASH_LIST, offsetof(struct in_ifaddr, ia_hash),
1341 }, {
1342 "name cache hash",
1343 X_NCHASH, X_NCHASHTBL,
1344 HASH_LIST, offsetof(struct namecache, nc_hash),
1345 }, {
1346 "name cache directory hash",
1347 X_NCVHASH, X_NCVHASHTBL,
1348 HASH_LIST, offsetof(struct namecache, nc_vhash),
1349 }, {
1350 "nfs client node cache",
1351 X_NFSNODE, X_NFSNODETBL,
1352 HASH_LIST, offsetof(struct nfsnode, n_hash)
1353 }, {
1354 "user info (uid -> used processes) hash",
1355 X_UIHASH, X_UIHASHTBL,
1356 HASH_LIST, offsetof(struct uidinfo, ui_hash),
1357 }, {
1358 NULL, -1, -1, 0, 0,
1359 }
1360 };
1361
1362 void
1363 dohashstat(int verbose, int todo, const char *hashname)
1364 {
1365 LIST_HEAD(, generic) *hashtbl_list;
1366 TAILQ_HEAD(, generic) *hashtbl_tailq;
1367 struct kernel_hash *curhash;
1368 void *hashaddr, *hashbuf, *nhashbuf, *nextaddr;
1369 size_t elemsize, hashbufsize, thissize;
1370 u_long hashsize;
1371 int i, used, items, chain, maxchain;
1372
1373 hashbuf = NULL;
1374 hashbufsize = 0;
1375
1376 if (todo & HASHLIST) {
1377 printf("Supported hashes:\n");
1378 for (curhash = khashes; curhash->description; curhash++) {
1379 if (hashnl[curhash->hashsize].n_value == 0 ||
1380 hashnl[curhash->hashtbl].n_value == 0)
1381 continue;
1382 printf("\t%-16s%s\n",
1383 hashnl[curhash->hashsize].n_name + 1,
1384 curhash->description);
1385 }
1386 return;
1387 }
1388
1389 if (hashname != NULL) {
1390 for (curhash = khashes; curhash->description; curhash++) {
1391 if (strcmp(hashnl[curhash->hashsize].n_name + 1,
1392 hashname) == 0 &&
1393 hashnl[curhash->hashsize].n_value != 0 &&
1394 hashnl[curhash->hashtbl].n_value != 0)
1395 break;
1396 }
1397 if (curhash->description == NULL) {
1398 warnx("%s: no such hash", hashname);
1399 return;
1400 }
1401 }
1402
1403 printf(
1404 "%-16s %8s %8s %8s %8s %8s %8s\n"
1405 "%-16s %8s %8s %8s %8s %8s %8s\n",
1406 "", "total", "used", "util", "num", "average", "maximum",
1407 "hash table", "buckets", "buckets", "%", "items", "chain",
1408 "chain");
1409
1410 for (curhash = khashes; curhash->description; curhash++) {
1411 if (hashnl[curhash->hashsize].n_value == 0 ||
1412 hashnl[curhash->hashtbl].n_value == 0)
1413 continue;
1414 if (hashname != NULL &&
1415 strcmp(hashnl[curhash->hashsize].n_name + 1, hashname))
1416 continue;
1417 elemsize = curhash->type == HASH_LIST ?
1418 sizeof(*hashtbl_list) : sizeof(*hashtbl_tailq);
1419 deref_kptr((void *)hashnl[curhash->hashsize].n_value,
1420 &hashsize, sizeof(hashsize),
1421 hashnl[curhash->hashsize].n_name);
1422 hashsize++;
1423 deref_kptr((void *)hashnl[curhash->hashtbl].n_value,
1424 &hashaddr, sizeof(hashaddr),
1425 hashnl[curhash->hashtbl].n_name);
1426 if (verbose)
1427 printf("%s %lu, %s %p, offset %ld, elemsize %llu\n",
1428 hashnl[curhash->hashsize].n_name + 1, hashsize,
1429 hashnl[curhash->hashtbl].n_name + 1, hashaddr,
1430 (long)curhash->offset,
1431 (unsigned long long)elemsize);
1432 thissize = hashsize * elemsize;
1433 if (hashbuf == NULL || thissize > hashbufsize) {
1434 if ((nhashbuf = realloc(hashbuf, thissize)) == NULL)
1435 errx(1, "malloc hashbuf %llu",
1436 (unsigned long long)hashbufsize);
1437 hashbuf = nhashbuf;
1438 hashbufsize = thissize;
1439 }
1440 deref_kptr(hashaddr, hashbuf, thissize,
1441 hashnl[curhash->hashtbl].n_name);
1442 used = 0;
1443 items = maxchain = 0;
1444 if (curhash->type == HASH_LIST) {
1445 hashtbl_list = hashbuf;
1446 hashtbl_tailq = NULL;
1447 } else {
1448 hashtbl_list = NULL;
1449 hashtbl_tailq = hashbuf;
1450 }
1451 for (i = 0; i < hashsize; i++) {
1452 if (curhash->type == HASH_LIST)
1453 nextaddr = LIST_FIRST(&hashtbl_list[i]);
1454 else
1455 nextaddr = TAILQ_FIRST(&hashtbl_tailq[i]);
1456 if (nextaddr == NULL)
1457 continue;
1458 if (verbose)
1459 printf("%5d: %p\n", i, nextaddr);
1460 used++;
1461 chain = 0;
1462 do {
1463 chain++;
1464 deref_kptr((char *)nextaddr + curhash->offset,
1465 &nextaddr, sizeof(void *),
1466 "hash chain corrupted");
1467 if (verbose > 1)
1468 printf("got nextaddr as %p\n",
1469 nextaddr);
1470 } while (nextaddr != NULL);
1471 items += chain;
1472 if (verbose && chain > 1)
1473 printf("\tchain = %d\n", chain);
1474 if (chain > maxchain)
1475 maxchain = chain;
1476 }
1477 printf("%-16s %8ld %8d %8.2f %8d %8.2f %8d\n",
1478 hashnl[curhash->hashsize].n_name + 1,
1479 hashsize, used, used * 100.0 / hashsize,
1480 items, used ? (double)items / used : 0.0, maxchain);
1481 }
1482 }
1483
1484 /*
1485 * kreadc like kread but returns 1 if sucessful, 0 otherwise
1486 */
1487 int
1488 kreadc(struct nlist *nl, int nlx, void *addr, size_t size)
1489 {
1490 const char *sym;
1491
1492 sym = nl[nlx].n_name;
1493 if (*sym == '_')
1494 ++sym;
1495 if (nl[nlx].n_type == 0 || nl[nlx].n_value == 0)
1496 return 0;
1497 deref_kptr((void *)nl[nlx].n_value, addr, size, sym);
1498 return 1;
1499 }
1500
1501 /*
1502 * kread reads something from the kernel, given its nlist index in namelist[].
1503 */
1504 void
1505 kread(struct nlist *nl, int nlx, void *addr, size_t size)
1506 {
1507 const char *sym;
1508
1509 sym = nl[nlx].n_name;
1510 if (*sym == '_')
1511 ++sym;
1512 if (nl[nlx].n_type == 0 || nl[nlx].n_value == 0)
1513 errx(1, "symbol %s not defined", sym);
1514 deref_kptr((void *)nl[nlx].n_value, addr, size, sym);
1515 }
1516
1517 /*
1518 * Dereference the kernel pointer `kptr' and fill in the local copy
1519 * pointed to by `ptr'. The storage space must be pre-allocated,
1520 * and the size of the copy passed in `len'.
1521 */
1522 void
1523 deref_kptr(const void *kptr, void *ptr, size_t len, const char *msg)
1524 {
1525
1526 if (*msg == '_')
1527 msg++;
1528 if (kvm_read(kd, (u_long)kptr, (char *)ptr, len) != len)
1529 errx(1, "kptr %lx: %s: %s", (u_long)kptr, msg, kvm_geterr(kd));
1530 }
1531
1532 /*
1533 * Traverse the UVM history buffers, performing the requested action.
1534 *
1535 * Note, we assume that if we're not listing, we're dumping.
1536 */
1537 void
1538 hist_traverse(int todo, const char *histname)
1539 {
1540 struct uvm_history_head histhead;
1541 struct uvm_history hist, *histkva;
1542 char *name = NULL;
1543 size_t namelen = 0;
1544
1545 if (histnl[0].n_value == 0) {
1546 warnx("UVM history is not compiled into the kernel.");
1547 return;
1548 }
1549
1550 deref_kptr((void *)histnl[X_UVM_HISTORIES].n_value, &histhead,
1551 sizeof(histhead), histnl[X_UVM_HISTORIES].n_name);
1552
1553 if (histhead.lh_first == NULL) {
1554 warnx("No active UVM history logs.");
1555 return;
1556 }
1557
1558 if (todo & HISTLIST)
1559 printf("Active UVM histories:");
1560
1561 for (histkva = LIST_FIRST(&histhead); histkva != NULL;
1562 histkva = LIST_NEXT(&hist, list)) {
1563 deref_kptr(histkva, &hist, sizeof(hist), "histkva");
1564 if (name == NULL || hist.namelen > namelen) {
1565 if (name != NULL)
1566 free(name);
1567 namelen = hist.namelen;
1568 if ((name = malloc(namelen + 1)) == NULL)
1569 err(1, "malloc history name");
1570 }
1571
1572 deref_kptr(hist.name, name, namelen, "history name");
1573 name[namelen] = '\0';
1574 if (todo & HISTLIST)
1575 printf(" %s", name);
1576 else {
1577 /*
1578 * If we're dumping all histories, do it, else
1579 * check to see if this is the one we want.
1580 */
1581 if (histname == NULL || strcmp(histname, name) == 0) {
1582 if (histname == NULL)
1583 printf("\nUVM history `%s':\n", name);
1584 hist_dodump(&hist);
1585 }
1586 }
1587 }
1588
1589 if (todo & HISTLIST)
1590 putchar('\n');
1591
1592 if (name != NULL)
1593 free(name);
1594 }
1595
1596 /*
1597 * Actually dump the history buffer at the specified KVA.
1598 */
1599 void
1600 hist_dodump(struct uvm_history *histp)
1601 {
1602 struct uvm_history_ent *histents, *e;
1603 size_t histsize;
1604 char *fmt = NULL, *fn = NULL;
1605 size_t fmtlen = 0, fnlen = 0;
1606 int i;
1607
1608 histsize = sizeof(struct uvm_history_ent) * histp->n;
1609
1610 if ((histents = malloc(histsize)) == NULL)
1611 err(1, "malloc history entries");
1612
1613 memset(histents, 0, histsize);
1614
1615 deref_kptr(histp->e, histents, histsize, "history entries");
1616 i = histp->f;
1617 do {
1618 e = &histents[i];
1619 if (e->fmt != NULL) {
1620 if (fmt == NULL || e->fmtlen > fmtlen) {
1621 if (fmt != NULL)
1622 free(fmt);
1623 fmtlen = e->fmtlen;
1624 if ((fmt = malloc(fmtlen + 1)) == NULL)
1625 err(1, "malloc printf format");
1626 }
1627 if (fn == NULL || e->fnlen > fnlen) {
1628 if (fn != NULL)
1629 free(fn);
1630 fnlen = e->fnlen;
1631 if ((fn = malloc(fnlen + 1)) == NULL)
1632 err(1, "malloc function name");
1633 }
1634
1635 deref_kptr(e->fmt, fmt, fmtlen, "printf format");
1636 fmt[fmtlen] = '\0';
1637
1638 deref_kptr(e->fn, fn, fnlen, "function name");
1639 fn[fnlen] = '\0';
1640
1641 printf("%06ld.%06ld ", (long int)e->tv.tv_sec,
1642 (long int)e->tv.tv_usec);
1643 printf("%s#%ld: ", fn, e->call);
1644 printf(fmt, e->v[0], e->v[1], e->v[2], e->v[3]);
1645 putchar('\n');
1646 }
1647 i = (i + 1) % histp->n;
1648 } while (i != histp->f);
1649
1650 free(histents);
1651 if (fmt != NULL)
1652 free(fmt);
1653 if (fn != NULL)
1654 free(fn);
1655 }
1656
1657 void
1658 usage(void)
1659 {
1660
1661 (void)fprintf(stderr,
1662 "usage: %s [-efHiLlmstUvW] [-c count] [-h hashname] [-M core] [-N system]\n"
1663 "\t\t[-u histname] [-w wait] [disks]\n", getprogname());
1664 exit(1);
1665 }
1666