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