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