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