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