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