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