vmstat.c revision 1.18 1 /*
2 * Copyright (c) 1980, 1986, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 static char copyright[] =
36 "@(#) Copyright (c) 1980, 1986, 1991, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)vmstat.c 8.1 (Berkeley) 6/6/93";*/
42 static char *rcsid = "$Id: vmstat.c,v 1.18 1995/03/14 07:35:49 pk Exp $";
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/time.h>
47 #include <sys/proc.h>
48 #include <sys/user.h>
49 #include <sys/dkstat.h>
50 #include <sys/buf.h>
51 #include <sys/namei.h>
52 #include <sys/malloc.h>
53 #include <sys/signal.h>
54 #include <sys/fcntl.h>
55 #include <sys/ioctl.h>
56 #include <sys/sysctl.h>
57 #include <sys/device.h>
58 #include <vm/vm.h>
59 #include <time.h>
60 #include <nlist.h>
61 #include <kvm.h>
62 #include <errno.h>
63 #include <unistd.h>
64 #include <stdio.h>
65 #include <ctype.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <paths.h>
69 #include <limits.h>
70
71 #define NEWVM /* XXX till old has been updated or purged */
72 struct nlist namelist[] = {
73 #define X_CPTIME 0
74 { "_cp_time" },
75 #define X_DK_NDRIVE 1
76 { "_dk_ndrive" },
77 #define X_SUM 2
78 { "_cnt" },
79 #define X_BOOTTIME 3
80 { "_boottime" },
81 #define X_DKXFER 4
82 { "_dk_xfer" },
83 #define X_HZ 5
84 { "_hz" },
85 #define X_STATHZ 6
86 { "_stathz" },
87 #define X_NCHSTATS 7
88 { "_nchstats" },
89 #define X_INTRNAMES 8
90 { "_intrnames" },
91 #define X_EINTRNAMES 9
92 { "_eintrnames" },
93 #define X_INTRCNT 10
94 { "_intrcnt" },
95 #define X_EINTRCNT 11
96 { "_eintrcnt" },
97 #define X_KMEMSTAT 12
98 { "_kmemstats" },
99 #define X_KMEMBUCKETS 13
100 { "_bucket" },
101 #define X_ALLEVENTS 14
102 { "_allevents" },
103 #ifdef notdef
104 #define X_DEFICIT 15
105 { "_deficit" },
106 #define X_FORKSTAT 16
107 { "_forkstat" },
108 #define X_REC 17
109 { "_rectime" },
110 #define X_PGIN 18
111 { "_pgintime" },
112 #define X_XSTATS 19
113 { "_xstats" },
114 #define X_END 20
115 #else
116 #define X_END 15
117 #endif
118 #if defined(hp300) || defined(luna68k)
119 #define X_HPDINIT (X_END)
120 { "_hp_dinit" },
121 #endif
122 #ifdef mips
123 #define X_SCSI_DINIT (X_END)
124 { "_scsi_dinit" },
125 #endif
126 #ifdef tahoe
127 #define X_VBDINIT (X_END)
128 { "_vbdinit" },
129 #define X_CKEYSTATS (X_END+1)
130 { "_ckeystats" },
131 #define X_DKEYSTATS (X_END+2)
132 { "_dkeystats" },
133 #endif
134 #ifdef vax
135 #define X_MBDINIT (X_END)
136 { "_mbdinit" },
137 #define X_UBDINIT (X_END+1)
138 { "_ubdinit" },
139 #endif
140 { "" },
141 };
142
143 struct _disk {
144 long time[CPUSTATES];
145 long *xfer;
146 } cur, last;
147
148 struct vmmeter sum, osum;
149 char **dr_name;
150 int *dr_select, dk_ndrive, ndrives;
151
152 int winlines = 20;
153
154 kvm_t *kd;
155
156 #define FORKSTAT 0x01
157 #define INTRSTAT 0x02
158 #define MEMSTAT 0x04
159 #define SUMSTAT 0x08
160 #define TIMESTAT 0x10
161 #define VMSTAT 0x20
162
163 #include "names.c" /* disk names -- machine dependent */
164
165 void cpustats(), dkstats(), dointr(), domem(), dosum();
166 void dovmstat(), kread(), usage();
167 #ifdef notdef
168 void dotimes(), doforkst();
169 #endif
170
171 main(argc, argv)
172 register int argc;
173 register char **argv;
174 {
175 extern int optind;
176 extern char *optarg;
177 register int c, todo;
178 u_int interval;
179 int reps;
180 char *memf, *nlistf;
181 char errbuf[_POSIX2_LINE_MAX];
182
183 memf = nlistf = NULL;
184 interval = reps = todo = 0;
185 while ((c = getopt(argc, argv, "c:fiM:mN:stw:")) != EOF) {
186 switch (c) {
187 case 'c':
188 reps = atoi(optarg);
189 break;
190 #ifndef notdef
191 case 'f':
192 todo |= FORKSTAT;
193 break;
194 #endif
195 case 'i':
196 todo |= INTRSTAT;
197 break;
198 case 'M':
199 memf = optarg;
200 break;
201 case 'm':
202 todo |= MEMSTAT;
203 break;
204 case 'N':
205 nlistf = optarg;
206 break;
207 case 's':
208 todo |= SUMSTAT;
209 break;
210 #ifndef notdef
211 case 't':
212 todo |= TIMESTAT;
213 break;
214 #endif
215 case 'w':
216 interval = atoi(optarg);
217 break;
218 case '?':
219 default:
220 usage();
221 }
222 }
223 argc -= optind;
224 argv += optind;
225
226 if (todo == 0)
227 todo = VMSTAT;
228
229 /*
230 * Discard setgid privileges if not the running kernel so that bad
231 * guys can't print interesting stuff from kernel memory.
232 */
233 if (nlistf != NULL || memf != NULL)
234 setgid(getgid());
235
236 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
237 if (kd == 0) {
238 (void)fprintf(stderr,
239 "vmstat: kvm_openfiles: %s\n", errbuf);
240 exit(1);
241 }
242
243 if ((c = kvm_nlist(kd, namelist)) != 0) {
244 if (c > 0) {
245 (void)fprintf(stderr,
246 "vmstat: undefined symbols:");
247 for (c = 0;
248 c < sizeof(namelist)/sizeof(namelist[0]); c++)
249 if (namelist[c].n_type == 0)
250 fprintf(stderr, " %s",
251 namelist[c].n_name);
252 (void)fputc('\n', stderr);
253 } else
254 (void)fprintf(stderr, "vmstat: kvm_nlist: %s\n",
255 kvm_geterr(kd));
256 exit(1);
257 }
258
259 if (todo & VMSTAT) {
260 char **getdrivedata();
261 struct winsize winsize;
262
263 argv = getdrivedata(argv);
264 winsize.ws_row = 0;
265 (void) ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize);
266 if (winsize.ws_row > 0)
267 winlines = winsize.ws_row;
268
269 }
270
271 #define BACKWARD_COMPATIBILITY
272 #ifdef BACKWARD_COMPATIBILITY
273 if (*argv) {
274 interval = atoi(*argv);
275 if (*++argv)
276 reps = atoi(*argv);
277 }
278 #endif
279
280 if (interval) {
281 if (!reps)
282 reps = -1;
283 } else if (reps)
284 interval = 1;
285
286 #ifdef notdef
287 if (todo & FORKSTAT)
288 doforkst();
289 #endif
290 if (todo & MEMSTAT)
291 domem();
292 if (todo & SUMSTAT)
293 dosum();
294 #ifdef notdef
295 if (todo & TIMESTAT)
296 dotimes();
297 #endif
298 if (todo & INTRSTAT)
299 dointr();
300 if (todo & VMSTAT)
301 dovmstat(interval, reps);
302 exit(0);
303 }
304
305 char **
306 getdrivedata(argv)
307 char **argv;
308 {
309 register int i;
310 register char **cp;
311 char buf[30];
312
313 kread(X_DK_NDRIVE, &dk_ndrive, sizeof(dk_ndrive));
314 if (dk_ndrive <= 0) {
315 (void)fprintf(stderr, "vmstat: dk_ndrive %d\n", dk_ndrive);
316 exit(1);
317 }
318 dr_select = calloc((size_t)dk_ndrive, sizeof(int));
319 dr_name = calloc((size_t)dk_ndrive, sizeof(char *));
320 for (i = 0; i < dk_ndrive; i++)
321 dr_name[i] = NULL;
322 cur.xfer = calloc((size_t)dk_ndrive, sizeof(long));
323 last.xfer = calloc((size_t)dk_ndrive, sizeof(long));
324 if (!read_names())
325 exit (1);
326 for (i = 0; i < dk_ndrive; i++)
327 if (dr_name[i] == NULL) {
328 (void)sprintf(buf, "??%d", i);
329 dr_name[i] = strdup(buf);
330 }
331
332 /*
333 * Choose drives to be displayed. Priority goes to (in order) drives
334 * supplied as arguments, default drives. If everything isn't filled
335 * in and there are drives not taken care of, display the first few
336 * that fit.
337 */
338 #define BACKWARD_COMPATIBILITY
339 for (ndrives = 0; *argv; ++argv) {
340 #ifdef BACKWARD_COMPATIBILITY
341 if (isdigit(**argv))
342 break;
343 #endif
344 for (i = 0; i < dk_ndrive; i++) {
345 if (strcmp(dr_name[i], *argv))
346 continue;
347 dr_select[i] = 1;
348 ++ndrives;
349 break;
350 }
351 }
352 for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
353 if (dr_select[i])
354 continue;
355 for (cp = defdrives; *cp; cp++)
356 if (strcmp(dr_name[i], *cp) == 0) {
357 dr_select[i] = 1;
358 ++ndrives;
359 break;
360 }
361 }
362 for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
363 if (dr_select[i])
364 continue;
365 dr_select[i] = 1;
366 ++ndrives;
367 }
368 return(argv);
369 }
370
371 long
372 getuptime()
373 {
374 static time_t now, boottime;
375 time_t uptime;
376
377 if (boottime == 0)
378 kread(X_BOOTTIME, &boottime, sizeof(boottime));
379 (void)time(&now);
380 uptime = now - boottime;
381 if (uptime <= 0 || uptime > 60*60*24*365*10) {
382 (void)fprintf(stderr,
383 "vmstat: time makes no sense; namelist must be wrong.\n");
384 exit(1);
385 }
386 return(uptime);
387 }
388
389 int hz, hdrcnt;
390
391 void
392 dovmstat(interval, reps)
393 u_int interval;
394 int reps;
395 {
396 struct vmtotal total;
397 time_t uptime, halfuptime;
398 void needhdr();
399 int mib[2];
400 size_t size;
401
402 uptime = getuptime();
403 halfuptime = uptime / 2;
404 (void)signal(SIGCONT, needhdr);
405
406 if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0)
407 kread(X_STATHZ, &hz, sizeof(hz));
408 if (!hz)
409 kread(X_HZ, &hz, sizeof(hz));
410
411 for (hdrcnt = 1;;) {
412 if (!--hdrcnt)
413 printhdr();
414 kread(X_CPTIME, cur.time, sizeof(cur.time));
415 kread(X_DKXFER, cur.xfer, sizeof(*cur.xfer) * dk_ndrive);
416 kread(X_SUM, &sum, sizeof(sum));
417 size = sizeof(total);
418 mib[0] = CTL_VM;
419 mib[1] = VM_METER;
420 if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) {
421 printf("Can't get kerninfo: %s\n", strerror(errno));
422 bzero(&total, sizeof(total));
423 }
424 (void)printf("%2d%2d%2d",
425 total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
426 #define pgtok(a) ((a) * sum.v_page_size >> 10)
427 #define rate(x) (((x) + halfuptime) / uptime) /* round */
428 (void)printf("%6ld%6ld ",
429 pgtok(total.t_avm), pgtok(total.t_free));
430 #ifdef NEWVM
431 (void)printf("%4lu ", rate(sum.v_faults - osum.v_faults));
432 (void)printf("%3lu ",
433 rate(sum.v_reactivated - osum.v_reactivated));
434 (void)printf("%3lu ", rate(sum.v_pageins - osum.v_pageins));
435 (void)printf("%3lu %3lu ",
436 rate(sum.v_pageouts - osum.v_pageouts), 0);
437 #else
438 (void)printf("%3lu %2lu ",
439 rate(sum.v_pgrec - (sum.v_xsfrec+sum.v_xifrec) -
440 (osum.v_pgrec - (osum.v_xsfrec+osum.v_xifrec))),
441 rate(sum.v_xsfrec + sum.v_xifrec -
442 osum.v_xsfrec - osum.v_xifrec));
443 (void)printf("%3lu ",
444 rate(pgtok(sum.v_pgpgin - osum.v_pgpgin)));
445 (void)printf("%3lu %3lu ",
446 rate(pgtok(sum.v_pgpgout - osum.v_pgpgout)),
447 rate(pgtok(sum.v_dfree - osum.v_dfree)));
448 (void)printf("%3d ", pgtok(deficit));
449 #endif
450 (void)printf("%3lu ", rate(sum.v_scan - osum.v_scan));
451 dkstats();
452 (void)printf("%4lu %4lu %3lu ",
453 rate(sum.v_intr - osum.v_intr),
454 rate(sum.v_syscall - osum.v_syscall),
455 rate(sum.v_swtch - osum.v_swtch));
456 cpustats();
457 (void)printf("\n");
458 (void)fflush(stdout);
459 if (reps >= 0 && --reps <= 0)
460 break;
461 osum = sum;
462 uptime = interval;
463 /*
464 * We round upward to avoid losing low-frequency events
465 * (i.e., >= 1 per interval but < 1 per second).
466 */
467 halfuptime = (uptime + 1) / 2;
468 (void)sleep(interval);
469 }
470 }
471
472 printhdr()
473 {
474 register int i;
475
476 (void)printf(" procs memory page%*s", 20, "");
477 if (ndrives > 1)
478 (void)printf("disks %*s faults cpu\n",
479 ndrives * 3 - 6, "");
480 else
481 (void)printf("%*s faults cpu\n", ndrives * 3, "");
482 #ifndef NEWVM
483 (void)printf(" r b w avm fre re at pi po fr de sr ");
484 #else
485 (void)printf(" r b w avm fre flt re pi po fr sr ");
486 #endif
487 for (i = 0; i < dk_ndrive; i++)
488 if (dr_select[i])
489 (void)printf("%c%c ", dr_name[i][0],
490 dr_name[i][strlen(dr_name[i]) - 1]);
491 (void)printf(" in sy cs us sy id\n");
492 hdrcnt = winlines - 2;
493 }
494
495 /*
496 * Force a header to be prepended to the next output.
497 */
498 void
499 needhdr()
500 {
501
502 hdrcnt = 1;
503 }
504
505 #ifdef notdef
506 void
507 dotimes()
508 {
509 u_int pgintime, rectime;
510
511 kread(X_REC, &rectime, sizeof(rectime));
512 kread(X_PGIN, &pgintime, sizeof(pgintime));
513 kread(X_SUM, &sum, sizeof(sum));
514 (void)printf("%u reclaims, %u total time (usec)\n",
515 sum.v_pgrec, rectime);
516 (void)printf("average: %u usec / reclaim\n", rectime / sum.v_pgrec);
517 (void)printf("\n");
518 (void)printf("%u page ins, %u total time (msec)\n",
519 sum.v_pgin, pgintime / 10);
520 (void)printf("average: %8.1f msec / page in\n",
521 pgintime / (sum.v_pgin * 10.0));
522 }
523 #endif
524
525 pct(top, bot)
526 long top, bot;
527 {
528 long ans;
529
530 if (bot == 0)
531 return(0);
532 ans = (quad_t)top * 100 / bot;
533 return (ans);
534 }
535
536 #define PCT(top, bot) pct((long)(top), (long)(bot))
537
538 #if defined(tahoe)
539 #include <machine/cpu.h>
540 #endif
541
542 void
543 dosum()
544 {
545 struct nchstats nchstats;
546 #ifndef NEWVM
547 struct xstats xstats;
548 #endif
549 long nchtotal;
550 #if defined(tahoe)
551 struct keystats keystats;
552 #endif
553
554 kread(X_SUM, &sum, sizeof(sum));
555 (void)printf("%9u cpu context switches\n", sum.v_swtch);
556 (void)printf("%9u device interrupts\n", sum.v_intr);
557 (void)printf("%9u software interrupts\n", sum.v_soft);
558 #ifdef vax
559 (void)printf("%9u pseudo-dma dz interrupts\n", sum.v_pdma);
560 #endif
561 (void)printf("%9u traps\n", sum.v_trap);
562 (void)printf("%9u system calls\n", sum.v_syscall);
563 (void)printf("%9u total faults taken\n", sum.v_faults);
564 (void)printf("%9u swap ins\n", sum.v_swpin);
565 (void)printf("%9u swap outs\n", sum.v_swpout);
566 (void)printf("%9u pages swapped in\n", sum.v_pswpin / CLSIZE);
567 (void)printf("%9u pages swapped out\n", sum.v_pswpout / CLSIZE);
568 (void)printf("%9u page ins\n", sum.v_pageins);
569 (void)printf("%9u page outs\n", sum.v_pageouts);
570 (void)printf("%9u pages paged in\n", sum.v_pgpgin);
571 (void)printf("%9u pages paged out\n", sum.v_pgpgout);
572 (void)printf("%9u pages reactivated\n", sum.v_reactivated);
573 (void)printf("%9u intransit blocking page faults\n", sum.v_intrans);
574 (void)printf("%9u zero fill pages created\n", sum.v_nzfod / CLSIZE);
575 (void)printf("%9u zero fill page faults\n", sum.v_zfod / CLSIZE);
576 (void)printf("%9u pages examined by the clock daemon\n", sum.v_scan);
577 (void)printf("%9u revolutions of the clock hand\n", sum.v_rev);
578 #ifdef NEWVM
579 (void)printf("%9u VM object cache lookups\n", sum.v_lookups);
580 (void)printf("%9u VM object hits\n", sum.v_hits);
581 (void)printf("%9u total VM faults taken\n", sum.v_vm_faults);
582 (void)printf("%9u copy-on-write faults\n", sum.v_cow_faults);
583 (void)printf("%9u pages freed by daemon\n", sum.v_dfree);
584 (void)printf("%9u pages freed by exiting processes\n", sum.v_pfree);
585 (void)printf("%9u pages free\n", sum.v_free_count);
586 (void)printf("%9u pages wired down\n", sum.v_wire_count);
587 (void)printf("%9u pages active\n", sum.v_active_count);
588 (void)printf("%9u pages inactive\n", sum.v_inactive_count);
589 (void)printf("%9u bytes per page\n", sum.v_page_size);
590 #else
591 (void)printf("%9u sequential process pages freed\n", sum.v_seqfree);
592 (void)printf("%9u total reclaims (%d%% fast)\n", sum.v_pgrec,
593 PCT(sum.v_fastpgrec, sum.v_pgrec));
594 (void)printf("%9u reclaims from free list\n", sum.v_pgfrec);
595 (void)printf("%9u executable fill pages created\n",
596 sum.v_nexfod / CLSIZE);
597 (void)printf("%9u executable fill page faults\n",
598 sum.v_exfod / CLSIZE);
599 (void)printf("%9u swap text pages found in free list\n",
600 sum.v_xsfrec);
601 (void)printf("%9u inode text pages found in free list\n",
602 sum.v_xifrec);
603 (void)printf("%9u file fill pages created\n", sum.v_nvrfod / CLSIZE);
604 (void)printf("%9u file fill page faults\n", sum.v_vrfod / CLSIZE);
605 (void)printf("%9u pages freed by the clock daemon\n",
606 sum.v_dfree / CLSIZE);
607 #endif
608 kread(X_NCHSTATS, &nchstats, sizeof(nchstats));
609 nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
610 nchstats.ncs_badhits + nchstats.ncs_falsehits +
611 nchstats.ncs_miss + nchstats.ncs_long;
612 (void)printf("%9ld total name lookups\n", nchtotal);
613 (void)printf(
614 "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n",
615 "", PCT(nchstats.ncs_goodhits, nchtotal),
616 PCT(nchstats.ncs_neghits, nchtotal),
617 PCT(nchstats.ncs_pass2, nchtotal));
618 (void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
619 PCT(nchstats.ncs_badhits, nchtotal),
620 PCT(nchstats.ncs_falsehits, nchtotal),
621 PCT(nchstats.ncs_long, nchtotal));
622 #ifndef NEWVM
623 kread(X_XSTATS, &xstats, sizeof(xstats));
624 (void)printf("%9lu total calls to xalloc (cache hits %d%%)\n",
625 xstats.alloc, PCT(xstats.alloc_cachehit, xstats.alloc));
626 (void)printf("%9s sticky %lu flushed %lu unused %lu\n", "",
627 xstats.alloc_inuse, xstats.alloc_cacheflush, xstats.alloc_unused);
628 (void)printf("%9lu total calls to xfree", xstats.free);
629 (void)printf(" (sticky %lu cached %lu swapped %lu)\n",
630 xstats.free_inuse, xstats.free_cache, xstats.free_cacheswap);
631 #endif
632 #if defined(tahoe)
633 kread(X_CKEYSTATS, &keystats, sizeof(keystats));
634 (void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n",
635 keystats.ks_allocs, "code cache keys allocated",
636 PCT(keystats.ks_allocfree, keystats.ks_allocs),
637 PCT(keystats.ks_norefs, keystats.ks_allocs),
638 PCT(keystats.ks_taken, keystats.ks_allocs),
639 PCT(keystats.ks_shared, keystats.ks_allocs));
640 kread(X_DKEYSTATS, &keystats, sizeof(keystats));
641 (void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n",
642 keystats.ks_allocs, "data cache keys allocated",
643 PCT(keystats.ks_allocfree, keystats.ks_allocs),
644 PCT(keystats.ks_norefs, keystats.ks_allocs),
645 PCT(keystats.ks_taken, keystats.ks_allocs),
646 PCT(keystats.ks_shared, keystats.ks_allocs));
647 #endif
648 }
649
650 #ifdef notdef
651 void
652 doforkst()
653 {
654 struct forkstat fks;
655
656 kread(X_FORKSTAT, &fks, sizeof(struct forkstat));
657 (void)printf("%d forks, %d pages, average %.2f\n",
658 fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork);
659 (void)printf("%d vforks, %d pages, average %.2f\n",
660 fks.cntvfork, fks.sizvfork, (double)fks.sizvfork / fks.cntvfork);
661 }
662 #endif
663
664 void
665 dkstats()
666 {
667 register int dn, state;
668 double etime;
669 long tmp;
670
671 for (dn = 0; dn < dk_ndrive; ++dn) {
672 tmp = cur.xfer[dn];
673 cur.xfer[dn] -= last.xfer[dn];
674 last.xfer[dn] = tmp;
675 }
676 etime = 0;
677 for (state = 0; state < CPUSTATES; ++state) {
678 tmp = cur.time[state];
679 cur.time[state] -= last.time[state];
680 last.time[state] = tmp;
681 etime += cur.time[state];
682 }
683 if (etime == 0)
684 etime = 1;
685 etime /= hz;
686 for (dn = 0; dn < dk_ndrive; ++dn) {
687 if (!dr_select[dn])
688 continue;
689 (void)printf("%2.0f ", cur.xfer[dn] / etime);
690 }
691 }
692
693 void
694 cpustats()
695 {
696 register int state;
697 double pct, total;
698
699 total = 0;
700 for (state = 0; state < CPUSTATES; ++state)
701 total += cur.time[state];
702 if (total)
703 pct = 100 / total;
704 else
705 pct = 0;
706 (void)printf("%2.0f ", (cur.time[CP_USER] + cur.time[CP_NICE]) * pct);
707 (void)printf("%2.0f ", (cur.time[CP_SYS] + cur.time[CP_INTR]) * pct);
708 (void)printf("%2.0f", cur.time[CP_IDLE] * pct);
709 }
710
711 void
712 dointr()
713 {
714 register long *intrcnt, inttotal, uptime;
715 register int nintr, inamlen;
716 register char *intrname;
717 struct evcnt evcnt, *allevents;
718 struct device dev;
719
720 uptime = getuptime();
721 nintr = namelist[X_EINTRCNT].n_value - namelist[X_INTRCNT].n_value;
722 inamlen =
723 namelist[X_EINTRNAMES].n_value - namelist[X_INTRNAMES].n_value;
724 intrcnt = malloc((size_t)nintr);
725 intrname = malloc((size_t)inamlen);
726 if (intrcnt == NULL || intrname == NULL) {
727 (void)fprintf(stderr, "vmstat: %s.\n", strerror(errno));
728 exit(1);
729 }
730 kread(X_INTRCNT, intrcnt, (size_t)nintr);
731 kread(X_INTRNAMES, intrname, (size_t)inamlen);
732 (void)printf("interrupt total rate\n");
733 inttotal = 0;
734 nintr /= sizeof(long);
735 while (--nintr >= 0) {
736 if (*intrcnt)
737 (void)printf("%-12s %8ld %8ld\n", intrname,
738 *intrcnt, *intrcnt / uptime);
739 intrname += strlen(intrname) + 1;
740 inttotal += *intrcnt++;
741 }
742 kread(X_ALLEVENTS, &allevents, sizeof allevents);
743 while (allevents) {
744 if (kvm_read(kd, (int)allevents, (void *)&evcnt,
745 sizeof evcnt) != sizeof evcnt) {
746 (void)fprintf(stderr, "vmstat: event chain trashed\n",
747 kvm_geterr(kd));
748 exit(1);
749 }
750 if (strcmp(evcnt.ev_name, "intr") == 0) {
751 if (kvm_read(kd, (int)evcnt.ev_dev, (void *)&dev,
752 sizeof dev) != sizeof dev) {
753 (void)fprintf(stderr, "vmstat: event chain trashed\n",
754 kvm_geterr(kd));
755 exit(1);
756 }
757 if (evcnt.ev_count)
758 (void)printf("%-12s %8ld %8ld\n", dev.dv_xname,
759 evcnt.ev_count, evcnt.ev_count / uptime);
760 }
761 allevents = evcnt.ev_next;
762 }
763 (void)printf("Total %8ld %8ld\n", inttotal, inttotal / uptime);
764 }
765
766 /*
767 * These names are defined in <sys/malloc.h>.
768 */
769 char *kmemnames[] = INITKMEMNAMES;
770
771 void
772 domem()
773 {
774 register struct kmembuckets *kp;
775 register struct kmemstats *ks;
776 register int i, j;
777 int len, size, first;
778 long totuse = 0, totfree = 0, totreq = 0;
779 char *name;
780 struct kmemstats kmemstats[M_LAST];
781 struct kmembuckets buckets[MINBUCKET + 16];
782
783 kread(X_KMEMBUCKETS, buckets, sizeof(buckets));
784 (void)printf("Memory statistics by bucket size\n");
785 (void)printf(
786 " Size In Use Free Requests HighWater Couldfree\n");
787 for (i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16; i++, kp++) {
788 if (kp->kb_calls == 0)
789 continue;
790 size = 1 << i;
791 (void)printf("%8d %8ld %6ld %10ld %7ld %10ld\n", size,
792 kp->kb_total - kp->kb_totalfree,
793 kp->kb_totalfree, kp->kb_calls,
794 kp->kb_highwat, kp->kb_couldfree);
795 totfree += size * kp->kb_totalfree;
796 }
797
798 kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats));
799 (void)printf("\nMemory usage type by bucket size\n");
800 (void)printf(" Size Type(s)\n");
801 kp = &buckets[MINBUCKET];
802 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) {
803 if (kp->kb_calls == 0)
804 continue;
805 first = 1;
806 len = 8;
807 for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
808 if (ks->ks_calls == 0)
809 continue;
810 if ((ks->ks_size & j) == 0)
811 continue;
812 name = kmemnames[i] ? kmemnames[i] : "undefined";
813 len += 2 + strlen(name);
814 if (first)
815 printf("%8d %s", j, name);
816 else
817 printf(",");
818 if (len >= 80) {
819 printf("\n\t ");
820 len = 10 + strlen(name);
821 }
822 if (!first)
823 printf(" %s", name);
824 first = 0;
825 }
826 printf("\n");
827 }
828
829 (void)printf(
830 "\nMemory statistics by type Type Kern\n");
831 (void)printf(
832 " Type InUse MemUse HighUse Limit Requests Limit Limit Size(s)\n");
833 for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
834 if (ks->ks_calls == 0)
835 continue;
836 (void)printf("%12s%6ld%6ldK%7ldK%6ldK%9ld%5u%6u",
837 kmemnames[i] ? kmemnames[i] : "undefined",
838 ks->ks_inuse, (ks->ks_memuse + 1023) / 1024,
839 (ks->ks_maxused + 1023) / 1024,
840 (ks->ks_limit + 1023) / 1024, ks->ks_calls,
841 ks->ks_limblocks, ks->ks_mapblocks);
842 first = 1;
843 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
844 if ((ks->ks_size & j) == 0)
845 continue;
846 if (first)
847 printf(" %d", j);
848 else
849 printf(",%d", j);
850 first = 0;
851 }
852 printf("\n");
853 totuse += ks->ks_memuse;
854 totreq += ks->ks_calls;
855 }
856 (void)printf("\nMemory Totals: In Use Free Requests\n");
857 (void)printf(" %7ldK %6ldK %8ld\n",
858 (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
859 }
860
861 /*
862 * kread reads something from the kernel, given its nlist index.
863 */
864 void
865 kread(nlx, addr, size)
866 int nlx;
867 void *addr;
868 size_t size;
869 {
870 char *sym;
871
872 if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
873 sym = namelist[nlx].n_name;
874 if (*sym == '_')
875 ++sym;
876 (void)fprintf(stderr,
877 "vmstat: symbol %s not defined\n", sym);
878 exit(1);
879 }
880 if (kvm_read(kd, namelist[nlx].n_value, addr, size) != size) {
881 sym = namelist[nlx].n_name;
882 if (*sym == '_')
883 ++sym;
884 (void)fprintf(stderr, "vmstat: %s: %s\n", sym, kvm_geterr(kd));
885 exit(1);
886 }
887 }
888
889 void
890 usage()
891 {
892 (void)fprintf(stderr,
893 #ifndef NEWVM
894 "usage: vmstat [-fimst] [-c count] [-M core] \
895 [-N system] [-w wait] [disks]\n");
896 #else
897 "usage: vmstat [-ims] [-c count] [-M core] \
898 [-N system] [-w wait] [disks]\n");
899 #endif
900 exit(1);
901 }
902
903