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