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