vmstat.c revision 1.79 1 /* $NetBSD: vmstat.c,v 1.79 2001/04/29 05:56:47 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1980, 1986, 1991, 1993
42 * The Regents of the University of California. All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the University of
55 * California, Berkeley and its contributors.
56 * 4. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 */
72
73 #include <sys/cdefs.h>
74 #ifndef lint
75 __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1991, 1993\n\
76 The Regents of the University of California. All rights reserved.\n");
77 #endif /* not lint */
78
79 #ifndef lint
80 #if 0
81 static char sccsid[] = "@(#)vmstat.c 8.2 (Berkeley) 3/1/95";
82 #else
83 __RCSID("$NetBSD: vmstat.c,v 1.79 2001/04/29 05:56:47 thorpej Exp $");
84 #endif
85 #endif /* not lint */
86
87 #define __POOL_EXPOSE
88
89 #include <sys/param.h>
90 #include <sys/time.h>
91 #include <sys/proc.h>
92 #include <sys/user.h>
93 #include <sys/dkstat.h>
94 #include <sys/buf.h>
95 #include <sys/namei.h>
96 #include <sys/malloc.h>
97 #include <sys/ioctl.h>
98 #include <sys/sched.h>
99 #include <sys/sysctl.h>
100 #include <sys/device.h>
101 #include <sys/pool.h>
102
103 #include <uvm/uvm_extern.h>
104 #include <uvm/uvm_stat.h>
105
106 #include <err.h>
107 #include <fcntl.h>
108 #include <time.h>
109 #include <nlist.h>
110 #include <kvm.h>
111 #include <errno.h>
112 #include <unistd.h>
113 #include <signal.h>
114 #include <stdio.h>
115 #include <ctype.h>
116 #include <stdlib.h>
117 #include <string.h>
118 #include <paths.h>
119 #include <limits.h>
120 #include "dkstats.h"
121
122 struct nlist namelist[] = {
123 #define X_BOOTTIME 0
124 { "_boottime" },
125 #define X_HZ 1
126 { "_hz" },
127 #define X_STATHZ 2
128 { "_stathz" },
129 #define X_NCHSTATS 3
130 { "_nchstats" },
131 #define X_INTRNAMES 4
132 { "_intrnames" },
133 #define X_EINTRNAMES 5
134 { "_eintrnames" },
135 #define X_INTRCNT 6
136 { "_intrcnt" },
137 #define X_EINTRCNT 7
138 { "_eintrcnt" },
139 #define X_KMEMSTAT 8
140 { "_kmemstats" },
141 #define X_KMEMBUCKETS 9
142 { "_bucket" },
143 #define X_ALLEVENTS 10
144 { "_allevents" },
145 #define X_POOLHEAD 11
146 { "_pool_head" },
147 #define X_UVMEXP 12
148 { "_uvmexp" },
149 #define X_END 13
150 #if defined(pc532)
151 #define X_IVT (X_END)
152 { "_ivt" },
153 #endif
154 { "" },
155 };
156
157 struct uvmexp uvmexp, ouvmexp;
158 int ndrives;
159
160 int winlines = 20;
161
162 kvm_t *kd;
163
164 #define FORKSTAT 0x01
165 #define INTRSTAT 0x02
166 #define MEMSTAT 0x04
167 #define SUMSTAT 0x08
168 #define EVCNTSTAT 0x10
169 #define VMSTAT 0x20
170 #define HISTLIST 0x40
171 #define HISTDUMP 0x80
172
173 void cpustats(void);
174 void dkstats(void);
175 void doevcnt(int verbose);
176 void dointr(int verbose);
177 void domem(void);
178 void dopool(void);
179 void dosum(void);
180 void dovmstat(u_int, int);
181 void kread(int, void *, size_t);
182 void needhdr(int);
183 long getuptime(void);
184 void printhdr(void);
185 long pct(long, long);
186 void usage(void);
187 void doforkst(void);
188
189 void hist_traverse(int, const char *);
190 void hist_dodump(struct uvm_history *);
191
192 int main(int, char **);
193 char **choosedrives(char **);
194
195 /* Namelist and memory file names. */
196 char *nlistf, *memf;
197
198 /* allow old usage [vmstat 1] */
199 #define BACKWARD_COMPATIBILITY
200
201 int
202 main(int argc, char *argv[])
203 {
204 int c, todo, verbose;
205 u_int interval;
206 int reps;
207 char errbuf[_POSIX2_LINE_MAX];
208 gid_t egid = getegid();
209 const char *histname = NULL;
210
211 (void)setegid(getgid());
212 memf = nlistf = NULL;
213 interval = reps = todo = verbose = 0;
214 while ((c = getopt(argc, argv, "c:efh:HilM:mN:svw:")) != -1) {
215 switch (c) {
216 case 'c':
217 reps = atoi(optarg);
218 break;
219 case 'e':
220 todo |= EVCNTSTAT;
221 break;
222 case 'f':
223 todo |= FORKSTAT;
224 break;
225 case 'h':
226 histname = optarg;
227 /* FALLTHROUGH */
228 case 'H':
229 todo |= HISTDUMP;
230 break;
231 case 'i':
232 todo |= INTRSTAT;
233 break;
234 case 'l':
235 todo |= HISTLIST;
236 break;
237 case 'M':
238 memf = optarg;
239 break;
240 case 'm':
241 todo |= MEMSTAT;
242 break;
243 case 'N':
244 nlistf = optarg;
245 break;
246 case 's':
247 todo |= SUMSTAT;
248 break;
249 case 'v':
250 verbose = 1;
251 break;
252 case 'w':
253 interval = atoi(optarg);
254 break;
255 case '?':
256 default:
257 usage();
258 }
259 }
260 argc -= optind;
261 argv += optind;
262
263 if (todo == 0)
264 todo = VMSTAT;
265
266 /*
267 * Discard setgid privileges. If not the running kernel, we toss
268 * them away totally so that bad guys can't print interesting stuff
269 * from kernel memory, otherwise switch back to kmem for the
270 * duration of the kvm_openfiles() call.
271 */
272 if (nlistf != NULL || memf != NULL)
273 (void)setgid(getgid());
274 else
275 (void)setegid(egid);
276
277 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
278 if (kd == NULL)
279 errx(1, "kvm_openfiles: %s\n", errbuf);
280
281 if (nlistf == NULL && memf == NULL) {
282 if (todo & VMSTAT)
283 (void)setegid(getgid()); /* XXX: dkinit */
284 else
285 (void)setgid(getgid());
286 }
287
288 if ((c = kvm_nlist(kd, namelist)) != 0) {
289 if (c > 0) {
290 (void)fprintf(stderr,
291 "vmstat: undefined symbols:");
292 for (c = 0;
293 c < sizeof(namelist) / sizeof(namelist[0]); c++)
294 if (namelist[c].n_type == 0)
295 fprintf(stderr, " %s",
296 namelist[c].n_name);
297 (void)fputc('\n', stderr);
298 } else
299 (void)fprintf(stderr, "vmstat: kvm_nlist: %s\n",
300 kvm_geterr(kd));
301 exit(1);
302 }
303
304 if (todo & VMSTAT) {
305 struct winsize winsize;
306
307 dkinit(0, egid); /* Initialize disk stats, no disks selected. */
308
309 (void)setgid(getgid()); /* don't need privs anymore */
310
311 argv = choosedrives(argv); /* Select disks. */
312 winsize.ws_row = 0;
313 (void)ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize);
314 if (winsize.ws_row > 0)
315 winlines = winsize.ws_row;
316
317 }
318
319 #ifdef BACKWARD_COMPATIBILITY
320 if (*argv) {
321 interval = atoi(*argv);
322 if (*++argv)
323 reps = atoi(*argv);
324 }
325 #endif
326
327 if (interval) {
328 if (!reps)
329 reps = -1;
330 } else if (reps)
331 interval = 1;
332
333
334 /*
335 * Statistics dumping is incompatible with the default
336 * VMSTAT/dovmstat() output. So perform the interval/reps handling
337 * for it here.
338 */
339 if ((todo & VMSTAT) == 0)
340 for (;;) {
341 if (todo & (HISTLIST|HISTDUMP)) {
342 if ((todo & (HISTLIST|HISTDUMP)) ==
343 (HISTLIST|HISTDUMP))
344 errx(1, "you may list or dump, but not both!");
345 hist_traverse(todo, histname);
346 }
347 if (todo & FORKSTAT)
348 doforkst();
349 if (todo & MEMSTAT) {
350 domem();
351 dopool();
352 }
353 if (todo & SUMSTAT)
354 dosum();
355 if (todo & INTRSTAT)
356 dointr(verbose);
357 if (todo & EVCNTSTAT)
358 doevcnt(verbose);
359
360 if (reps >= 0 && --reps <=0)
361 break;
362 sleep(interval);
363 puts("");
364 }
365 else
366 dovmstat(interval, reps);
367 exit(0);
368 }
369
370 char **
371 choosedrives(char **argv)
372 {
373 int i;
374
375 /*
376 * Choose drives to be displayed. Priority goes to (in order) drives
377 * supplied as arguments, default drives. If everything isn't filled
378 * in and there are drives not taken care of, display the first few
379 * that fit.
380 */
381 #define BACKWARD_COMPATIBILITY
382 for (ndrives = 0; *argv; ++argv) {
383 #ifdef BACKWARD_COMPATIBILITY
384 if (isdigit(**argv))
385 break;
386 #endif
387 for (i = 0; i < dk_ndrive; i++) {
388 if (strcmp(dr_name[i], *argv))
389 continue;
390 dk_select[i] = 1;
391 ++ndrives;
392 break;
393 }
394 }
395 for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
396 if (dk_select[i])
397 continue;
398 dk_select[i] = 1;
399 ++ndrives;
400 }
401 return (argv);
402 }
403
404 long
405 getuptime(void)
406 {
407 static time_t now;
408 static struct timeval boottime;
409 time_t uptime;
410
411 if (boottime.tv_sec == 0)
412 kread(X_BOOTTIME, &boottime, sizeof(boottime));
413 (void)time(&now);
414 uptime = now - boottime.tv_sec;
415 if (uptime <= 0 || uptime > 60*60*24*365*10) {
416 (void)fprintf(stderr,
417 "vmstat: time makes no sense; namelist must be wrong.\n");
418 exit(1);
419 }
420 return (uptime);
421 }
422
423 int hz, hdrcnt;
424
425 void
426 dovmstat(u_int interval, int reps)
427 {
428 struct vmtotal total;
429 time_t uptime, halfuptime;
430 int mib[2];
431 size_t size;
432 int pagesize = getpagesize();
433
434 uptime = getuptime();
435 halfuptime = uptime / 2;
436 (void)signal(SIGCONT, needhdr);
437
438 if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0)
439 kread(X_STATHZ, &hz, sizeof(hz));
440 if (!hz)
441 kread(X_HZ, &hz, sizeof(hz));
442
443 for (hdrcnt = 1;;) {
444 if (!--hdrcnt)
445 printhdr();
446 /* Read new disk statistics */
447 dkreadstats();
448 kread(X_UVMEXP, &uvmexp, sizeof(uvmexp));
449 if (memf != NULL) {
450 /*
451 * XXX Can't do this if we're reading a crash
452 * XXX dump because they're lazily-calculated.
453 */
454 printf("Unable to get vmtotals from crash dump.\n");
455 memset(&total, 0, sizeof(total));
456 } else {
457 size = sizeof(total);
458 mib[0] = CTL_VM;
459 mib[1] = VM_METER;
460 if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) {
461 printf("Can't get vmtotals: %s\n",
462 strerror(errno));
463 memset(&total, 0, sizeof(total));
464 }
465 }
466 (void)printf("%2d%2d%2d",
467 total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
468 #define pgtok(a) (long)((a) * (pagesize >> 10))
469 #define rate(x) (u_long)(((x) + halfuptime) / uptime) /* round */
470 (void)printf("%6ld%6ld ",
471 pgtok(total.t_avm), pgtok(total.t_free));
472 (void)printf("%4lu ", rate(uvmexp.faults - ouvmexp.faults));
473 (void)printf("%3lu ", rate(uvmexp.pdreact - ouvmexp.pdreact));
474 (void)printf("%3lu ", rate(uvmexp.pageins - ouvmexp.pageins));
475 (void)printf("%4lu ",
476 rate(uvmexp.pgswapout - ouvmexp.pgswapout));
477 (void)printf("%4lu ", rate(uvmexp.pdfreed - ouvmexp.pdfreed));
478 (void)printf("%4lu ", rate(uvmexp.pdscans - ouvmexp.pdscans));
479 dkstats();
480 (void)printf("%4lu %4lu %3lu ",
481 rate(uvmexp.intrs - ouvmexp.intrs),
482 rate(uvmexp.syscalls - ouvmexp.syscalls),
483 rate(uvmexp.swtch - ouvmexp.swtch));
484 cpustats();
485 (void)printf("\n");
486 (void)fflush(stdout);
487 if (reps >= 0 && --reps <= 0)
488 break;
489 ouvmexp = uvmexp;
490 uptime = interval;
491 /*
492 * We round upward to avoid losing low-frequency events
493 * (i.e., >= 1 per interval but < 1 per second).
494 */
495 halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2;
496 (void)sleep(interval);
497 }
498 }
499
500 void
501 printhdr(void)
502 {
503 int i;
504
505 (void)printf(" procs memory page%*s", 23, "");
506 if (ndrives > 0)
507 (void)printf("%s %*sfaults cpu\n",
508 ((ndrives > 1) ? "disks" : "disk"),
509 ((ndrives > 1) ? ndrives * 3 - 4 : 0), "");
510 else
511 (void)printf("%*s faults cpu\n",
512 ndrives * 3, "");
513
514 (void)printf(" r b w avm fre flt re pi po fr sr ");
515 for (i = 0; i < dk_ndrive; i++)
516 if (dk_select[i])
517 (void)printf("%c%c ", dr_name[i][0],
518 dr_name[i][strlen(dr_name[i]) - 1]);
519 (void)printf(" in sy cs us sy id\n");
520 hdrcnt = winlines - 2;
521 }
522
523 /*
524 * Force a header to be prepended to the next output.
525 */
526 void
527 needhdr(int dummy)
528 {
529
530 hdrcnt = 1;
531 }
532
533 long
534 pct(long top, long bot)
535 {
536 long ans;
537
538 if (bot == 0)
539 return (0);
540 ans = (quad_t)top * 100 / bot;
541 return (ans);
542 }
543
544 #define PCT(top, bot) (int)pct((long)(top), (long)(bot))
545
546 void
547 dosum(void)
548 {
549 struct nchstats nchstats;
550 long nchtotal;
551
552 kread(X_UVMEXP, &uvmexp, sizeof(uvmexp));
553
554 (void)printf("%9u bytes per page\n", uvmexp.pagesize);
555
556 (void)printf("%9u pages managed\n", uvmexp.npages);
557 (void)printf("%9u pages free\n", uvmexp.free);
558 (void)printf("%9u pages active\n", uvmexp.active);
559 (void)printf("%9u pages inactive\n", uvmexp.inactive);
560 (void)printf("%9u pages paging\n", uvmexp.paging);
561 (void)printf("%9u pages wired\n", uvmexp.wired);
562 (void)printf("%9u zero pages\n", uvmexp.zeropages);
563 (void)printf("%9u reserve pagedaemon pages\n",
564 uvmexp.reserve_pagedaemon);
565 (void)printf("%9u reserve kernel pages\n", uvmexp.reserve_kernel);
566 (void)printf("%9u anon pager pages\n", uvmexp.anonpages);
567 (void)printf("%9u vnode page cache pages\n", uvmexp.vnodepages);
568 (void)printf("%9u executable pages\n", uvmexp.vtextpages);
569
570 (void)printf("%9u minimum free pages\n", uvmexp.freemin);
571 (void)printf("%9u target free pages\n", uvmexp.freetarg);
572 (void)printf("%9u target inactive pages\n", uvmexp.inactarg);
573 (void)printf("%9u maximum wired pages\n", uvmexp.wiredmax);
574
575 (void)printf("%9u swap devices\n", uvmexp.nswapdev);
576 (void)printf("%9u swap pages\n", uvmexp.swpages);
577 (void)printf("%9u swap pages in use\n", uvmexp.swpginuse);
578 (void)printf("%9u swap allocations\n", uvmexp.nswget);
579 (void)printf("%9u anons\n", uvmexp.nanon);
580 (void)printf("%9u free anons\n", uvmexp.nfreeanon);
581
582 (void)printf("%9u total faults taken\n", uvmexp.faults);
583 (void)printf("%9u traps\n", uvmexp.traps);
584 (void)printf("%9u device interrupts\n", uvmexp.intrs);
585 (void)printf("%9u cpu context switches\n", uvmexp.swtch);
586 (void)printf("%9u software interrupts\n", uvmexp.softs);
587 (void)printf("%9u system calls\n", uvmexp.syscalls);
588 (void)printf("%9u pagein requests\n", uvmexp.pageins);
589 (void)printf("%9u pageout requests\n", uvmexp.pdpageouts);
590 (void)printf("%9u swap ins\n", uvmexp.swapins);
591 (void)printf("%9u swap outs\n", uvmexp.swapouts);
592 (void)printf("%9u pages swapped in\n", uvmexp.pgswapin);
593 (void)printf("%9u pages swapped out\n", uvmexp.pgswapout);
594 (void)printf("%9u forks total\n", uvmexp.forks);
595 (void)printf("%9u forks blocked parent\n", uvmexp.forks_ppwait);
596 (void)printf("%9u forks shared address space with parent\n",
597 uvmexp.forks_sharevm);
598 (void)printf("%9u pagealloc zero wanted and avail\n",
599 uvmexp.pga_zerohit);
600 (void)printf("%9u pagealloc zero wanted and not avail\n",
601 uvmexp.pga_zeromiss);
602 (void)printf("%9u aborts of idle page zeroing\n",
603 uvmexp.zeroaborts);
604 (void)printf("%9u pagealloc desired color avail\n",
605 uvmexp.colorhit);
606 (void)printf("%9u pagealloc desired color not avail\n",
607 uvmexp.colormiss);
608
609 (void)printf("%9u faults with no memory\n", uvmexp.fltnoram);
610 (void)printf("%9u faults with no anons\n", uvmexp.fltnoanon);
611 (void)printf("%9u faults had to wait on pages\n", uvmexp.fltpgwait);
612 (void)printf("%9u faults found released page\n", uvmexp.fltpgrele);
613 (void)printf("%9u faults relock (%u ok)\n", uvmexp.fltrelck,
614 uvmexp.fltrelckok);
615 (void)printf("%9u anon page faults\n", uvmexp.fltanget);
616 (void)printf("%9u anon retry faults\n", uvmexp.fltanretry);
617 (void)printf("%9u amap copy faults\n", uvmexp.fltamcopy);
618 (void)printf("%9u neighbour anon page faults\n", uvmexp.fltnamap);
619 (void)printf("%9u neighbour object page faults\n", uvmexp.fltnomap);
620 (void)printf("%9u locked pager get faults\n", uvmexp.fltlget);
621 (void)printf("%9u unlocked pager get faults\n", uvmexp.fltget);
622 (void)printf("%9u anon faults\n", uvmexp.flt_anon);
623 (void)printf("%9u anon copy on write faults\n", uvmexp.flt_acow);
624 (void)printf("%9u object faults\n", uvmexp.flt_obj);
625 (void)printf("%9u promote copy faults\n", uvmexp.flt_prcopy);
626 (void)printf("%9u promote zero fill faults\n", uvmexp.flt_przero);
627
628 (void)printf("%9u times daemon wokeup\n",uvmexp.pdwoke);
629 (void)printf("%9u revolutions of the clock hand\n", uvmexp.pdrevs);
630 (void)printf("%9u times daemon attempted swapout\n", uvmexp.pdswout);
631 (void)printf("%9u pages freed by daemon\n", uvmexp.pdfreed);
632 (void)printf("%9u pages scanned by daemon\n", uvmexp.pdscans);
633 (void)printf("%9u anonymous pages scanned by daemon\n",
634 uvmexp.pdanscan);
635 (void)printf("%9u object pages scanned by daemon\n", uvmexp.pdobscan);
636 (void)printf("%9u pages reactivated\n", uvmexp.pdreact);
637 (void)printf("%9u pages found busy by daemon\n", uvmexp.pdbusy);
638 (void)printf("%9u total pending pageouts\n", uvmexp.pdpending);
639 (void)printf("%9u pages deactivated\n", uvmexp.pddeact);
640 kread(X_NCHSTATS, &nchstats, sizeof(nchstats));
641 nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
642 nchstats.ncs_badhits + nchstats.ncs_falsehits +
643 nchstats.ncs_miss + nchstats.ncs_long;
644 (void)printf("%9ld total name lookups\n", nchtotal);
645 (void)printf(
646 "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n",
647 "", PCT(nchstats.ncs_goodhits, nchtotal),
648 PCT(nchstats.ncs_neghits, nchtotal),
649 PCT(nchstats.ncs_pass2, nchtotal));
650 (void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
651 PCT(nchstats.ncs_badhits, nchtotal),
652 PCT(nchstats.ncs_falsehits, nchtotal),
653 PCT(nchstats.ncs_long, nchtotal));
654 }
655
656 void
657 doforkst(void)
658 {
659
660 kread(X_UVMEXP, &uvmexp, sizeof(uvmexp));
661
662 (void)printf("%u forks total\n", uvmexp.forks);
663 (void)printf("%u forks blocked parent\n", uvmexp.forks_ppwait);
664 (void)printf("%u forks shared address space with parent\n",
665 uvmexp.forks_sharevm);
666 }
667
668 void
669 dkstats(void)
670 {
671 int dn, state;
672 double etime;
673
674 /* Calculate disk stat deltas. */
675 dkswap();
676 etime = 0;
677 for (state = 0; state < CPUSTATES; ++state) {
678 etime += cur.cp_time[state];
679 }
680 if (etime == 0)
681 etime = 1;
682 etime /= hz;
683 for (dn = 0; dn < dk_ndrive; ++dn) {
684 if (!dk_select[dn])
685 continue;
686 (void)printf("%2.0f ", cur.dk_xfer[dn] / etime);
687 }
688 }
689
690 void
691 cpustats(void)
692 {
693 int state;
694 double pct, total;
695
696 total = 0;
697 for (state = 0; state < CPUSTATES; ++state)
698 total += cur.cp_time[state];
699 if (total)
700 pct = 100 / total;
701 else
702 pct = 0;
703 (void)printf("%2.0f ",
704 (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pct);
705 (void)printf("%2.0f ",
706 (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pct);
707 (void)printf("%2.0f", cur.cp_time[CP_IDLE] * pct);
708 }
709
710 #if defined(pc532)
711 /* To get struct iv ...*/
712 #define _KERNEL
713 #include <machine/psl.h>
714 #undef _KERNEL
715 void
716 dointr(int verbose)
717 {
718 long i, j, inttotal, uptime;
719 static char iname[64];
720 struct iv ivt[32], *ivp = ivt;
721
722 iname[63] = '\0';
723 uptime = getuptime();
724 kread(X_IVT, ivp, sizeof(ivt));
725
726 for (i = 0; i < 2; i++) {
727 (void)printf("%sware interrupts:\n", i ? "\nsoft" : "hard");
728 (void)printf("interrupt total rate\n");
729 inttotal = 0;
730 for (j = 0; j < 16; j++, ivp++) {
731 if (ivp->iv_vec && ivp->iv_use &&
732 (ivp->iv_cnt || verbose)) {
733 if (kvm_read(kd, (u_long)ivp->iv_use, iname,
734 63) != 63) {
735 (void)fprintf(stderr,
736 "vmstat: iv_use: %s\n",
737 kvm_geterr(kd));
738 exit(1);
739 }
740 (void)printf("%-12s %8ld %8ld\n", iname,
741 ivp->iv_cnt, ivp->iv_cnt / uptime);
742 inttotal += ivp->iv_cnt;
743 }
744 }
745 (void)printf("Total %8ld %8ld\n",
746 inttotal, inttotal / uptime);
747 }
748 }
749 #else
750 void
751 dointr(int verbose)
752 {
753 long *intrcnt;
754 long long inttotal, uptime;
755 int nintr, inamlen;
756 char *intrname;
757 struct evcntlist allevents;
758 struct evcnt evcnt, *evptr;
759 char evgroup[EVCNT_STRING_MAX], evname[EVCNT_STRING_MAX];
760
761 uptime = getuptime();
762 nintr = namelist[X_EINTRCNT].n_value - namelist[X_INTRCNT].n_value;
763 inamlen =
764 namelist[X_EINTRNAMES].n_value - namelist[X_INTRNAMES].n_value;
765 intrcnt = malloc((size_t)nintr);
766 intrname = malloc((size_t)inamlen);
767 if (intrcnt == NULL || intrname == NULL) {
768 (void)fprintf(stderr, "vmstat: %s.\n", strerror(errno));
769 exit(1);
770 }
771 kread(X_INTRCNT, intrcnt, (size_t)nintr);
772 kread(X_INTRNAMES, intrname, (size_t)inamlen);
773 (void)printf("%-24s %16s %8s\n", "interrupt", "total", "rate");
774 inttotal = 0;
775 nintr /= sizeof(long);
776 while (--nintr >= 0) {
777 if (*intrcnt || verbose)
778 (void)printf("%-24s %16lld %8lld\n", intrname,
779 (long long)*intrcnt,
780 (long long)(*intrcnt / uptime));
781 intrname += strlen(intrname) + 1;
782 inttotal += *intrcnt++;
783 }
784 kread(X_ALLEVENTS, &allevents, sizeof allevents);
785 evptr = allevents.tqh_first;
786 while (evptr) {
787 if (kvm_read(kd, (long)evptr, &evcnt, sizeof evcnt) !=
788 sizeof(evcnt)) {
789 event_chain_trashed:
790 (void)fprintf(stderr,
791 "vmstat: event chain trashed: %s\n",
792 kvm_geterr(kd));
793 exit(1);
794 }
795
796 evptr = evcnt.ev_list.tqe_next;
797 if (evcnt.ev_type != EVCNT_TYPE_INTR)
798 continue;
799
800 if (evcnt.ev_count == 0 && !verbose)
801 continue;
802
803 if (kvm_read(kd, (long)evcnt.ev_group, evgroup,
804 evcnt.ev_grouplen + 1) != evcnt.ev_grouplen + 1)
805 goto event_chain_trashed;
806 if (kvm_read(kd, (long)evcnt.ev_name, evname,
807 evcnt.ev_namelen + 1) != evcnt.ev_namelen + 1)
808 goto event_chain_trashed;
809
810 (void)printf("%s %s%*s %16lld %8lld\n", evgroup, evname,
811 24 - (evcnt.ev_grouplen + 1 + evcnt.ev_namelen), "",
812 (long long)evcnt.ev_count,
813 (long long)(evcnt.ev_count / uptime));
814
815 inttotal += evcnt.ev_count++;
816 }
817 (void)printf("%-24s %16lld %8lld\n", "Total", inttotal,
818 (long long)(inttotal / uptime));
819 }
820 #endif
821
822 void
823 doevcnt(int verbose)
824 {
825 long long uptime;
826 struct evcntlist allevents;
827 struct evcnt evcnt, *evptr;
828 char evgroup[EVCNT_STRING_MAX], evname[EVCNT_STRING_MAX];
829
830 /* XXX should print type! */
831
832 uptime = getuptime();
833 (void)printf("%-24s %16s %8s %s\n", "event", "total", "rate", "type");
834 kread(X_ALLEVENTS, &allevents, sizeof allevents);
835 evptr = allevents.tqh_first;
836 while (evptr) {
837 if (kvm_read(kd, (long)evptr, &evcnt, sizeof evcnt) !=
838 sizeof(evcnt)) {
839 event_chain_trashed:
840 (void)fprintf(stderr,
841 "vmstat: event chain trashed: %s\n",
842 kvm_geterr(kd));
843 exit(1);
844 }
845
846 evptr = evcnt.ev_list.tqe_next;
847 if (evcnt.ev_count == 0 && !verbose)
848 continue;
849
850 if (kvm_read(kd, (long)evcnt.ev_group, evgroup,
851 evcnt.ev_grouplen + 1) != evcnt.ev_grouplen + 1)
852 goto event_chain_trashed;
853 if (kvm_read(kd, (long)evcnt.ev_name, evname,
854 evcnt.ev_namelen + 1) != evcnt.ev_namelen + 1)
855 goto event_chain_trashed;
856
857 (void)printf("%s %s%*s %16lld %8lld %s\n", evgroup, evname,
858 24 - (evcnt.ev_grouplen + 1 + evcnt.ev_namelen), "",
859 (long long)evcnt.ev_count,
860 (long long)(evcnt.ev_count / uptime),
861 /* XXX do the following with an array lookup XXX */
862 (evcnt.ev_type == EVCNT_TYPE_MISC) ? "misc" :
863 ((evcnt.ev_type == EVCNT_TYPE_INTR) ? "intr" : "?"));
864 }
865 }
866
867 /*
868 * These names are defined in <sys/malloc.h>.
869 */
870 char *kmemnames[] = INITKMEMNAMES;
871
872 void
873 domem(void)
874 {
875 struct kmembuckets *kp;
876 struct kmemstats *ks;
877 int i, j;
878 int len, size, first;
879 long totuse = 0, totfree = 0, totreq = 0;
880 char *name;
881 struct kmemstats kmemstats[M_LAST];
882 struct kmembuckets buckets[MINBUCKET + 16];
883
884 kread(X_KMEMBUCKETS, buckets, sizeof(buckets));
885 for (first = 1, i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16;
886 i++, kp++) {
887 if (kp->kb_calls == 0)
888 continue;
889 if (first) {
890 (void)printf("Memory statistics by bucket size\n");
891 (void)printf(
892 " Size In Use Free Requests HighWater Couldfree\n");
893 first = 0;
894 }
895 size = 1 << i;
896 (void)printf("%8d %8ld %6ld %10ld %7ld %10ld\n", size,
897 kp->kb_total - kp->kb_totalfree,
898 kp->kb_totalfree, kp->kb_calls,
899 kp->kb_highwat, kp->kb_couldfree);
900 totfree += size * kp->kb_totalfree;
901 }
902
903 /*
904 * If kmem statistics are not being gathered by the kernel,
905 * first will still be 1.
906 */
907 if (first) {
908 printf(
909 "Kmem statistics are not being gathered by the kernel.\n");
910 return;
911 }
912
913 kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats));
914 (void)printf("\nMemory usage type by bucket size\n");
915 (void)printf(" Size Type(s)\n");
916 kp = &buckets[MINBUCKET];
917 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) {
918 if (kp->kb_calls == 0)
919 continue;
920 first = 1;
921 len = 8;
922 for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
923 if (ks->ks_calls == 0)
924 continue;
925 if ((ks->ks_size & j) == 0)
926 continue;
927 if (kmemnames[i] == 0) {
928 kmemnames[i] = malloc(10);
929 /* strlen("undef/")+3+1);*/
930 snprintf(kmemnames[i], 10, "undef/%d", i);
931 /* same 10 as above!!! */
932 }
933 name = kmemnames[i];
934 len += 2 + strlen(name);
935 if (first)
936 printf("%8d %s", j, name);
937 else
938 printf(",");
939 if (len >= 80) {
940 printf("\n\t ");
941 len = 10 + strlen(name);
942 }
943 if (!first)
944 printf(" %s", name);
945 first = 0;
946 }
947 printf("\n");
948 }
949
950 (void)printf(
951 "\nMemory statistics by type Type Kern\n");
952 (void)printf(
953 " Type InUse MemUse HighUse Limit Requests Limit Limit Size(s)\n");
954 for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
955 if (ks->ks_calls == 0)
956 continue;
957 (void)printf("%14s%6ld%6ldK%7ldK%6ldK%9ld%5u%6u",
958 kmemnames[i] ? kmemnames[i] : "undefined",
959 ks->ks_inuse, (ks->ks_memuse + 1023) / 1024,
960 (ks->ks_maxused + 1023) / 1024,
961 (ks->ks_limit + 1023) / 1024, ks->ks_calls,
962 ks->ks_limblocks, ks->ks_mapblocks);
963 first = 1;
964 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
965 if ((ks->ks_size & j) == 0)
966 continue;
967 if (first)
968 printf(" %d", j);
969 else
970 printf(",%d", j);
971 first = 0;
972 }
973 printf("\n");
974 totuse += ks->ks_memuse;
975 totreq += ks->ks_calls;
976 }
977 (void)printf("\nMemory Totals: In Use Free Requests\n");
978 (void)printf(" %7ldK %6ldK %8ld\n",
979 (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
980 }
981
982 void
983 dopool(void)
984 {
985 int first, ovflw;
986 long addr;
987 long total = 0, inuse = 0;
988 TAILQ_HEAD(,pool) pool_head;
989 struct pool pool, *pp = &pool;
990
991 kread(X_POOLHEAD, &pool_head, sizeof(pool_head));
992 addr = (long)TAILQ_FIRST(&pool_head);
993
994 for (first = 1; addr != 0; ) {
995 char name[32], maxp[32];
996 if (kvm_read(kd, addr, (void *)pp, sizeof *pp) != sizeof *pp) {
997 (void)fprintf(stderr,
998 "vmstat: pool chain trashed: %s\n",
999 kvm_geterr(kd));
1000 exit(1);
1001 }
1002 if (kvm_read(kd, (long)pp->pr_wchan, name, sizeof name) < 0) {
1003 (void)fprintf(stderr,
1004 "vmstat: pool name trashed: %s\n",
1005 kvm_geterr(kd));
1006 exit(1);
1007 }
1008 name[31] = '\0';
1009
1010 if (first) {
1011 (void)printf("Memory resource pool statistics\n");
1012 (void)printf(
1013 "%-11s%5s%9s%5s%9s%6s%6s%6s%6s%6s%6s%5s\n",
1014 "Name",
1015 "Size",
1016 "Requests",
1017 "Fail",
1018 "Releases",
1019 "Pgreq",
1020 "Pgrel",
1021 "Npage",
1022 "Hiwat",
1023 "Minpg",
1024 "Maxpg",
1025 "Idle");
1026 first = 0;
1027 }
1028 if (pp->pr_maxpages == UINT_MAX)
1029 sprintf(maxp, "inf");
1030 else
1031 sprintf(maxp, "%u", pp->pr_maxpages);
1032 /*
1033 * Print single word. `ovflow' is number of characters didn't fit
1034 * on the last word. `fmt' is a format string to print this word.
1035 * It must contain asterisk for field width. `width' is a width
1036 * occupied by this word. `fixed' is a number of constant chars in
1037 * `fmt'. `val' is a value to be printed using format string `fmt'.
1038 */
1039 #define PRWORD(ovflw, fmt, width, fixed, val) do { \
1040 (ovflw) += printf((fmt), \
1041 (width) - (fixed) - (ovflw) > 0 ? \
1042 (width) - (fixed) - (ovflw) : 0, \
1043 (val)) - (width); \
1044 if ((ovflw) < 0) \
1045 (ovflw) = 0; \
1046 } while (/* CONSTCOND */0)
1047 ovflw = 0;
1048 PRWORD(ovflw, "%-*s", 11, 0, name);
1049 PRWORD(ovflw, " %*u", 5, 1, pp->pr_size);
1050 PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nget);
1051 PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nfail);
1052 PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nput);
1053 PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagealloc);
1054 PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagefree);
1055 PRWORD(ovflw, " %*d", 6, 1, pp->pr_npages);
1056 PRWORD(ovflw, " %*d", 6, 1, pp->pr_hiwat);
1057 PRWORD(ovflw, " %*d", 6, 1, pp->pr_minpages);
1058 PRWORD(ovflw, " %*s", 6, 1, maxp);
1059 PRWORD(ovflw, " %*lu\n", 5, 1, pp->pr_nidle);
1060
1061 inuse += (pp->pr_nget - pp->pr_nput) * pp->pr_size;
1062 total += pp->pr_npages * pp->pr_pagesz;
1063 addr = (long)TAILQ_NEXT(pp, pr_poollist);
1064 }
1065
1066 inuse /= 1024;
1067 total /= 1024;
1068 printf("\nIn use %ldK, total allocated %ldK; utilization %.1f%%\n",
1069 inuse, total, (double)(100 * inuse) / total);
1070 }
1071
1072 /*
1073 * kread reads something from the kernel, given its nlist index.
1074 */
1075 void
1076 kread(int nlx, void *addr, size_t size)
1077 {
1078 const char *sym;
1079
1080 if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
1081 sym = namelist[nlx].n_name;
1082 if (*sym == '_')
1083 ++sym;
1084 (void)fprintf(stderr,
1085 "vmstat: symbol %s not defined\n", sym);
1086 exit(1);
1087 }
1088 if (kvm_read(kd, namelist[nlx].n_value, addr, size) != size) {
1089 sym = namelist[nlx].n_name;
1090 if (*sym == '_')
1091 ++sym;
1092 (void)fprintf(stderr, "vmstat: %s: %s\n", sym, kvm_geterr(kd));
1093 exit(1);
1094 }
1095 }
1096
1097 struct nlist histnl[] = {
1098 { "_uvm_histories" },
1099 #define X_UVM_HISTORIES 0
1100 { NULL },
1101 };
1102
1103 /*
1104 * Traverse the UVM history buffers, performing the requested action.
1105 *
1106 * Note, we assume that if we're not listing, we're dumping.
1107 */
1108 void
1109 hist_traverse(int todo, const char *histname)
1110 {
1111 struct uvm_history_head histhead;
1112 struct uvm_history hist, *histkva;
1113 char *name = NULL;
1114 size_t namelen = 0;
1115
1116 if (kvm_nlist(kd, histnl) != 0) {
1117 printf("UVM history is not compiled into the kernel.\n");
1118 return;
1119 }
1120
1121 if (kvm_read(kd, histnl[X_UVM_HISTORIES].n_value, &histhead,
1122 sizeof(histhead)) != sizeof(histhead)) {
1123 warnx("unable to read %s: %s",
1124 histnl[X_UVM_HISTORIES].n_name, kvm_geterr(kd));
1125 return;
1126 }
1127
1128 if (histhead.lh_first == NULL) {
1129 printf("No active UVM history logs.\n");
1130 return;
1131 }
1132
1133 if (todo & HISTLIST)
1134 printf("Active UVM histories:");
1135
1136 for (histkva = LIST_FIRST(&histhead); histkva != NULL;
1137 histkva = LIST_NEXT(&hist, list)) {
1138 if (kvm_read(kd, (u_long)histkva, &hist, sizeof(hist)) !=
1139 sizeof(hist)) {
1140 warnx("unable to read history at %p: %s",
1141 histkva, kvm_geterr(kd));
1142 goto out;
1143 }
1144
1145 if (hist.namelen > namelen) {
1146 if (name != NULL)
1147 free(name);
1148 namelen = hist.namelen;
1149 if ((name = malloc(namelen + 1)) == NULL)
1150 err(1, "malloc history name");
1151 }
1152
1153 if (kvm_read(kd, (u_long)hist.name, name, namelen) !=
1154 namelen) {
1155 warnx("unable to read history name at %p: %s",
1156 hist.name, kvm_geterr(kd));
1157 goto out;
1158 }
1159 name[namelen] = '\0';
1160 if (todo & HISTLIST)
1161 printf(" %s", name);
1162 else {
1163 /*
1164 * If we're dumping all histories, do it, else
1165 * check to see if this is the one we want.
1166 */
1167 if (histname == NULL || strcmp(histname, name) == 0) {
1168 if (histname == NULL)
1169 printf("\nUVM history `%s':\n", name);
1170 hist_dodump(&hist);
1171 }
1172 }
1173 }
1174
1175 if (todo & HISTLIST)
1176 printf("\n");
1177
1178 out:
1179 if (name != NULL)
1180 free(name);
1181 }
1182
1183 /*
1184 * Actually dump the history buffer at the specified KVA.
1185 */
1186 void
1187 hist_dodump(struct uvm_history *histp)
1188 {
1189 struct uvm_history_ent *histents, *e;
1190 size_t histsize;
1191 char *fmt = NULL, *fn = NULL;
1192 size_t fmtlen = 0, fnlen = 0;
1193 int i;
1194
1195 histsize = sizeof(struct uvm_history_ent) * histp->n;
1196
1197 if ((histents = malloc(histsize)) == NULL)
1198 err(1, "malloc history entries");
1199
1200 memset(histents, 0, histsize);
1201
1202 if (kvm_read(kd, (u_long)histp->e, histents, histsize) != histsize) {
1203 warnx("unable to read history entries at %p: %s",
1204 histp->e, kvm_geterr(kd));
1205 goto out;
1206 }
1207
1208 i = histp->f;
1209 do {
1210 e = &histents[i];
1211 if (e->fmt != NULL) {
1212 if (e->fmtlen > fmtlen) {
1213 if (fmt != NULL)
1214 free(fmt);
1215 fmtlen = e->fmtlen;
1216 if ((fmt = malloc(fmtlen + 1)) == NULL)
1217 err(1, "malloc printf format");
1218 }
1219 if (e->fnlen > fnlen) {
1220 if (fn != NULL)
1221 free(fn);
1222 fnlen = e->fnlen;
1223 if ((fn = malloc(fnlen + 1)) == NULL)
1224 err(1, "malloc function name");
1225 }
1226
1227 if (kvm_read(kd, (u_long)e->fmt, fmt, fmtlen) !=
1228 fmtlen) {
1229 warnx("unable to read printf format "
1230 "at %p: %s", e->fmt, kvm_geterr(kd));
1231 goto out;
1232 }
1233 fmt[fmtlen] = '\0';
1234
1235 if (kvm_read(kd, (u_long)e->fn, fn, fnlen) != fnlen) {
1236 warnx("unable to read function name "
1237 "at %p: %s", e->fn, kvm_geterr(kd));
1238 goto out;
1239 }
1240 fn[fnlen] = '\0';
1241
1242 printf("%06ld.%06ld ", (long int)e->tv.tv_sec,
1243 (long int)e->tv.tv_usec);
1244 printf("%s#%ld: ", fn, e->call);
1245 printf(fmt, e->v[0], e->v[1], e->v[2], e->v[3]);
1246 printf("\n");
1247 }
1248 i = (i + 1) % histp->n;
1249 } while (i != histp->f);
1250
1251 out:
1252 free(histents);
1253 if (fmt != NULL)
1254 free(fmt);
1255 if (fn != NULL)
1256 free(fn);
1257 }
1258
1259 void
1260 usage(void)
1261 {
1262
1263 (void)fprintf(stderr,
1264 "usage: vmstat [-efHilmsv] [-h histname] [-c count] [-M core] "
1265 "[-N system] [-w wait] [disks]\n");
1266 exit(1);
1267 }
1268