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