vmstat.c revision 1.12 1 /*
2 * Copyright (c) 1980, 1986, 1991 The Regents of the University of California.
3 * 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 char copyright[] =
36 "@(#) Copyright (c) 1980, 1986, 1991 The Regents of the University of California.\n\
37 All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)vmstat.c 5.31 (Berkeley) 7/2/91";*/
42 static char rcsid[] = "$Id: vmstat.c,v 1.12 1994/04/15 07:07:27 cgd 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/vmmeter.h>
57 #include <time.h>
58 #include <nlist.h>
59 #include <kvm.h>
60 #include <errno.h>
61 #include <unistd.h>
62 #include <stdio.h>
63 #include <ctype.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <paths.h>
67
68 #define NEWVM /* XXX till old has been updated or purged */
69 struct nlist nl[] = {
70 #define X_CPTIME 0
71 { "_cp_time" },
72 #define X_TOTAL 1
73 { "_total" },
74 #define X_CNT 2
75 { "_cnt" },
76 #define X_BOOTTIME 3
77 { "_boottime" },
78 #define X_DKXFER 4
79 { "_dk_xfer" },
80 #define X_HZ 5
81 { "_hz" },
82 #define X_PHZ 6
83 { "_phz" },
84 #define X_NCHSTATS 7
85 { "_nchstats" },
86 #define X_INTRNAMES 8
87 { "_intrnames" },
88 #define X_EINTRNAMES 9
89 { "_eintrnames" },
90 #define X_INTRCNT 10
91 { "_intrcnt" },
92 #define X_EINTRCNT 11
93 { "_eintrcnt" },
94 #define X_DK_NDRIVE 12
95 { "_dk_ndrive" },
96 #define X_KMEMSTAT 13
97 { "_kmemstats" },
98 #define X_KMEMBUCKETS 14
99 { "_bucket" },
100 #ifdef notdef
101 #define X_DEFICIT 14
102 { "_deficit" },
103 #define X_FORKSTAT 15
104 { "_forkstat" },
105 #define X_REC 16
106 { "_rectime" },
107 #define X_PGIN 17
108 { "_pgintime" },
109 #define X_XSTATS 18
110 { "_xstats" },
111 #define X_END 18
112 #else
113 #define X_END 14
114 #endif
115 #ifdef hp300
116 #define X_HPDINIT (X_END+1)
117 { "_hp_dinit" },
118 #endif
119 #ifdef tahoe
120 #define X_VBDINIT (X_END+1)
121 { "_vbdinit" },
122 #define X_CKEYSTATS (X_END+2)
123 { "_ckeystats" },
124 #define X_DKEYSTATS (X_END+3)
125 { "_dkeystats" },
126 #endif
127 #ifdef vax
128 #define X_MBDINIT (X_END+1)
129 { "_mbdinit" },
130 #define X_UBDINIT (X_END+2)
131 { "_ubdinit" },
132 #endif
133 #ifdef i386
134 #define X_ISADEVTAB (X_END+1)
135 { "_isa_devtab" },
136 #endif i386
137 { "" },
138 };
139
140 struct _disk {
141 long time[CPUSTATES];
142 long *xfer;
143 } cur, last;
144
145 struct vmmeter cnt, ocnt;
146 char *vmunix = _PATH_UNIX;
147 char **dr_name;
148 int *dr_select, dk_ndrive, ndrives;
149 #ifdef notdef
150 /* to make up for statistics that don't get updated */
151 int size, free_count, active_count, inactive, wired;
152 #endif
153
154 int winlines = 20;
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(), docnt();
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 *kmem;
181
182 kmem = NULL;
183 interval = reps = todo = 0;
184 while ((c = getopt(argc, argv, "c:fiM:mN:stw:")) != EOF) {
185 switch (c) {
186 case 'c':
187 reps = atoi(optarg);
188 break;
189 #ifndef notdef
190 case 'f':
191 todo |= FORKSTAT;
192 break;
193 #endif
194 case 'i':
195 todo |= INTRSTAT;
196 break;
197 case 'M':
198 kmem = optarg;
199 break;
200 case 'm':
201 todo |= MEMSTAT;
202 break;
203 case 'N':
204 vmunix = optarg;
205 break;
206 case 's':
207 todo |= SUMSTAT;
208 break;
209 #ifndef notdef
210 case 't':
211 todo |= TIMESTAT;
212 break;
213 #endif
214 case 'w':
215 interval = atoi(optarg);
216 break;
217 case '?':
218 default:
219 usage();
220 }
221 }
222 argc -= optind;
223 argv += optind;
224
225 if (todo == 0)
226 todo = VMSTAT;
227
228 if (kvm_openfiles(vmunix, kmem, NULL) < 0) {
229 (void)fprintf(stderr,
230 "vmstat: kvm_openfiles: %s\n", kvm_geterr());
231 exit(1);
232 }
233
234 if ((c = kvm_nlist(nl)) != 0) {
235 if (c > 0) {
236 (void)fprintf(stderr,
237 "vmstat: undefined symbols in %s:", vmunix);
238 for (c = 0; c < sizeof(nl)/sizeof(nl[0]); c++)
239 if (nl[c].n_type == 0)
240 fprintf(stderr, " %s", nl[c].n_name);
241 (void)fputc('\n', stderr);
242 } else
243 (void)fprintf(stderr, "vmstat: kvm_nlist: %s\n",
244 kvm_geterr());
245 exit(1);
246 }
247
248 if (todo & VMSTAT) {
249 char **getdrivedata();
250 struct winsize winsize;
251
252 argv = getdrivedata(argv);
253 winsize.ws_row = 0;
254 (void) ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize);
255 if (winsize.ws_row > 0)
256 winlines = winsize.ws_row;
257
258 }
259
260 #define BACKWARD_COMPATIBILITY
261 #ifdef BACKWARD_COMPATIBILITY
262 if (*argv) {
263 interval = atoi(*argv);
264 if (*++argv)
265 reps = atoi(*argv);
266 }
267 #endif
268
269 if (interval) {
270 if (!reps)
271 reps = -1;
272 } else if (reps)
273 interval = 1;
274
275 #ifdef notdef
276 if (todo & FORKSTAT)
277 doforkst();
278 #endif
279 if (todo & MEMSTAT)
280 domem();
281 if (todo & SUMSTAT)
282 docnt();
283 #ifdef notdef
284 if (todo & TIMESTAT)
285 dotimes();
286 #endif
287 if (todo & INTRSTAT)
288 dointr();
289 if (todo & VMSTAT)
290 dovmstat(interval, reps);
291 exit(0);
292 }
293
294 char **
295 getdrivedata(argv)
296 char **argv;
297 {
298 register int i;
299 register char **cp;
300 char buf[30];
301
302 kread(X_DK_NDRIVE, &dk_ndrive, sizeof(dk_ndrive));
303 if (dk_ndrive <= 0) {
304 (void)fprintf(stderr, "vmstat: dk_ndrive %d\n", dk_ndrive);
305 exit(1);
306 }
307 dr_select = calloc((size_t)dk_ndrive, sizeof(int));
308 dr_name = calloc((size_t)dk_ndrive, sizeof(char *));
309 for (i = 0; i < dk_ndrive; i++)
310 dr_name[i] = NULL;
311 cur.xfer = calloc((size_t)dk_ndrive, sizeof(long));
312 last.xfer = calloc((size_t)dk_ndrive, sizeof(long));
313 read_names();
314 for (i = 0; i < dk_ndrive; i++)
315 if (dr_name[i] == NULL) {
316 (void)sprintf(buf, "??%d", i);
317 dr_name[i] = strdup(buf);
318 }
319
320 /*
321 * Choose drives to be displayed. Priority goes to (in order) drives
322 * supplied as arguments, default drives. If everything isn't filled
323 * in and there are drives not taken care of, display the first few
324 * that fit.
325 */
326 #define BACKWARD_COMPATIBILITY
327 for (ndrives = 0; *argv; ++argv) {
328 #ifdef BACKWARD_COMPATIBILITY
329 if (isdigit(**argv))
330 break;
331 #endif
332 for (i = 0; i < dk_ndrive; i++) {
333 if (strcmp(dr_name[i], *argv))
334 continue;
335 dr_select[i] = 1;
336 ++ndrives;
337 break;
338 }
339 }
340 for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
341 if (dr_select[i])
342 continue;
343 for (cp = defdrives; *cp; cp++)
344 if (strcmp(dr_name[i], *cp) == 0) {
345 dr_select[i] = 1;
346 ++ndrives;
347 break;
348 }
349 }
350 for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
351 if (dr_select[i])
352 continue;
353 dr_select[i] = 1;
354 ++ndrives;
355 }
356 return(argv);
357 }
358
359 long
360 getuptime()
361 {
362 static time_t now, boottime;
363 time_t uptime;
364
365 if (boottime == 0)
366 kread(X_BOOTTIME, &boottime, sizeof(boottime));
367 (void)time(&now);
368 uptime = now - boottime;
369 if (uptime <= 0 || uptime > 60*60*24*365*10) {
370 (void)fprintf(stderr,
371 "vmstat: time makes no sense; namelist must be wrong.\n");
372 exit(1);
373 }
374 return(uptime);
375 }
376
377 int hz, hdrcnt;
378
379 void
380 dovmstat(interval, reps)
381 u_int interval;
382 int reps;
383 {
384 struct vmtotal total;
385 time_t uptime, halfuptime;
386 void needhdr();
387 #ifndef notdef
388 int deficit;
389 #endif
390
391 uptime = getuptime();
392 halfuptime = uptime / 2;
393 (void)signal(SIGCONT, needhdr);
394
395 if (nl[X_PHZ].n_type != 0 && nl[X_PHZ].n_value != 0)
396 kread(X_PHZ, &hz, sizeof(hz));
397 if (!hz)
398 kread(X_HZ, &hz, sizeof(hz));
399
400 for (hdrcnt = 1;;) {
401 if (!--hdrcnt)
402 printhdr();
403 kread(X_CPTIME, cur.time, sizeof(cur.time));
404 kread(X_DKXFER, cur.xfer, sizeof(*cur.xfer * dk_ndrive));
405 kread(X_TOTAL, &total, sizeof(total));
406 kread(X_CNT, &cnt, sizeof(cnt));
407 #ifdef notdef
408 kread(X_DEFICIT, &deficit, sizeof(deficit));
409 #endif
410 (void)printf("%2d %1d %1d ",
411 total.t_rq, total.t_dw + total.t_pw, total.t_sw);
412 #define pgtok(a) ((a)*NBPG >> 10)
413 #define rate(x) (((x) + halfuptime) / uptime) /* round */
414 (void)printf("%5ld %5ld ",
415 #ifdef __NetBSD__
416 pgtok(cnt.v_active_count), pgtok(cnt.v_free_count));
417 #else
418 pgtok(total.t_avm), pgtok(total.t_free));
419 #endif
420 #ifdef NEWVM
421 (void)printf("%4lu ", rate(cnt.v_vm_faults - ocnt.v_vm_faults));
422 (void)printf("%3lu ",
423 rate(cnt.v_reactivated - ocnt.v_reactivated));
424 (void)printf("%3lu ", rate(cnt.v_pageins - ocnt.v_pageins));
425 (void)printf("%3lu %3lu ",
426 rate(cnt.v_pageouts - ocnt.v_pageouts), 0);
427 #else
428 (void)printf("%3lu %2lu ",
429 rate(cnt.v_pgrec - (cnt.v_xsfrec+cnt.v_xifrec) -
430 (ocnt.v_pgrec - (ocnt.v_xsfrec+ocnt.v_xifrec))),
431 rate(cnt.v_xsfrec + cnt.v_xifrec -
432 ocnt.v_xsfrec - ocnt.v_xifrec));
433 (void)printf("%3lu ",
434 rate(pgtok(cnt.v_pgpgin - ocnt.v_pgpgin)));
435 (void)printf("%3lu %3lu ",
436 rate(pgtok(cnt.v_pgpgout - ocnt.v_pgpgout)),
437 rate(pgtok(cnt.v_dfree - ocnt.v_dfree)));
438 (void)printf("%3d ", pgtok(deficit));
439 #endif
440 (void)printf("%3lu ", rate(cnt.v_scan - ocnt.v_scan));
441 dkstats();
442 (void)printf("%4lu %4lu %3lu ",
443 rate(cnt.v_intr - ocnt.v_intr),
444 rate(cnt.v_syscall - ocnt.v_syscall),
445 rate(cnt.v_swtch - ocnt.v_swtch));
446 cpustats();
447 (void)printf("\n");
448 (void)fflush(stdout);
449 if (reps >= 0 && --reps <= 0)
450 break;
451 ocnt = cnt;
452 ocnt = cnt;
453 uptime = interval;
454 /*
455 * We round upward to avoid losing low-frequency events
456 * (i.e., >= 1 per interval but < 1 per second).
457 */
458 halfuptime = (uptime + 1) / 2;
459 (void)sleep(interval);
460 }
461 }
462
463 printhdr()
464 {
465 register int i;
466
467 (void)printf(" procs memory page%*s", 20, "");
468 if (ndrives > 1)
469 (void)printf("disks %*s faults cpu\n",
470 ndrives * 3 - 6, "");
471 else
472 (void)printf("%*s faults cpu\n", ndrives * 3, "");
473 #ifndef NEWVM
474 (void)printf(" r b w avm fre re at pi po fr de sr ");
475 #else
476 (void)printf(" r b w avm fre flt re pi po fr sr ");
477 #endif
478 for (i = 0; i < dk_ndrive; i++)
479 if (dr_select[i])
480 (void)printf("%c%c ", dr_name[i][0],
481 dr_name[i][strlen(dr_name[i]) - 1]);
482 (void)printf(" in sy cs us sy id\n");
483 hdrcnt = winlines - 2;
484 }
485
486 /*
487 * Force a header to be prepended to the next output.
488 */
489 void
490 needhdr()
491 {
492
493 hdrcnt = 1;
494 }
495
496 #ifdef notdef
497 void
498 dotimes()
499 {
500 u_int pgintime, rectime;
501
502 kread(X_REC, &rectime, sizeof(rectime));
503 kread(X_PGIN, &pgintime, sizeof(pgintime));
504 kread(X_SUM, &cnt, sizeof(cnt));
505 (void)printf("%u reclaims, %u total time (usec)\n",
506 cnt.v_pgrec, rectime);
507 (void)printf("average: %u usec / reclaim\n", rectime / cnt.v_pgrec);
508 (void)printf("\n");
509 (void)printf("%u page ins, %u total time (msec)\n",
510 cnt.v_pgin, pgintime / 10);
511 (void)printf("average: %8.1f msec / page in\n",
512 pgintime / (cnt.v_pgin * 10.0));
513 }
514 #endif
515
516 pct(top, bot)
517 long top, bot;
518 {
519 if (bot == 0)
520 return(0);
521 return((top * 100) / bot);
522 }
523
524 #define PCT(top, bot) pct((long)(top), (long)(bot))
525
526 #if defined(tahoe)
527 #include <machine/cpu.h>
528 #endif
529
530 void
531 docnt()
532 {
533 struct nchstats nchstats;
534 #ifndef NEWVM
535 struct xstats xstats;
536 #endif
537 long nchtotal;
538 #if defined(tahoe)
539 struct keystats keystats;
540 #endif
541
542 #ifdef NEWVM
543 kread(X_CNT, &cnt, sizeof(cnt));
544 #else
545 (void)printf("%9u swap ins\n", cnt.v_swpin);
546 (void)printf("%9u swap outs\n", cnt.v_swpout);
547 (void)printf("%9u pages swapped in\n", cnt.v_pswpin / CLSIZE);
548 (void)printf("%9u pages swapped out\n", cnt.v_pswpout / CLSIZE);
549 (void)printf("%9u total address trans. faults taken\n", cnt.v_faults);
550 (void)printf("%9u page ins\n", cnt.v_pgin);
551 (void)printf("%9u page outs\n", cnt.v_pgout);
552 (void)printf("%9u pages paged in\n", cnt.v_pgpgin);
553 (void)printf("%9u pages paged out\n", cnt.v_pgpgout);
554 (void)printf("%9u sequential process pages freed\n", cnt.v_seqfree);
555 (void)printf("%9u total reclaims (%d%% fast)\n", cnt.v_pgrec,
556 PCT(cnt.v_fastpgrec, cnt.v_pgrec));
557 (void)printf("%9u reclaims from free list\n", cnt.v_pgfrec);
558 (void)printf("%9u intransit blocking page faults\n", cnt.v_intrans);
559 (void)printf("%9u zero fill pages created\n", cnt.v_nzfod / CLSIZE);
560 (void)printf("%9u zero fill page faults\n", cnt.v_zfod / CLSIZE);
561 (void)printf("%9u executable fill pages created\n",
562 cnt.v_nexfod / CLSIZE);
563 (void)printf("%9u executable fill page faults\n",
564 cnt.v_exfod / CLSIZE);
565 (void)printf("%9u swap text pages found in free list\n",
566 cnt.v_xsfrec);
567 (void)printf("%9u inode text pages found in free list\n",
568 cnt.v_xifrec);
569 (void)printf("%9u file fill pages created\n", cnt.v_nvrfod / CLSIZE);
570 (void)printf("%9u file fill page faults\n", cnt.v_vrfod / CLSIZE);
571 (void)printf("%9u pages examined by the clock daemon\n", cnt.v_scan);
572 (void)printf("%9u revolutions of the clock hand\n", cnt.v_rev);
573 (void)printf("%9u pages freed by the clock daemon\n",
574 cnt.v_dfree / CLSIZE);
575 #endif
576 (void)printf("%9u cpu context switches\n", cnt.v_swtch);
577 (void)printf("%9u device interrupts\n", cnt.v_intr);
578 (void)printf("%9u software interrupts\n", cnt.v_soft);
579 #ifdef vax
580 (void)printf("%9u pseudo-dma dz interrupts\n", cnt.v_pdma);
581 #endif
582 (void)printf("%9u traps\n", cnt.v_trap);
583 (void)printf("%9u system calls\n", cnt.v_syscall);
584 #ifdef NEWVM
585 (void)printf("%9u bytes per page\n", cnt.v_page_size);
586 (void)printf("%9u pages free\n", cnt.v_free_count);
587 (void)printf("%9u pages active\n", cnt.v_active_count);
588 (void)printf("%9u pages inactive\n", cnt.v_inactive_count);
589 (void)printf("%9u pages wired down\n", cnt.v_wire_count);
590 /* (void)printf("%9u zero fill pages created\n", cnt.v_nzfod / CLSIZE);*/
591 (void)printf("%9u zero fill page faults\n", cnt.v_zfod / CLSIZE);
592 (void)printf("%9u pages reactivated\n", cnt.v_reactivated);
593 (void)printf("%9u pageins\n", cnt.v_pageins);
594 (void)printf("%9u pageouts\n", cnt.v_pageouts);
595 (void)printf("%9u VM faults\n", cnt.v_vm_faults);
596 (void)printf("%9u copy-on-write faults\n", cnt.v_cow_faults);
597 (void)printf("%9u VM object cache lookups\n", cnt.v_lookups);
598 (void)printf("%9u VM object hits\n", cnt.v_hits);
599 #endif
600
601 kread(X_NCHSTATS, &nchstats, sizeof(nchstats));
602 nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
603 nchstats.ncs_badhits + nchstats.ncs_falsehits +
604 nchstats.ncs_miss + nchstats.ncs_long;
605 (void)printf("%9ld total name lookups\n", nchtotal);
606 (void)printf(
607 "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n",
608 "", PCT(nchstats.ncs_goodhits, nchtotal),
609 PCT(nchstats.ncs_neghits, nchtotal),
610 PCT(nchstats.ncs_pass2, nchtotal));
611 (void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
612 PCT(nchstats.ncs_badhits, nchtotal),
613 PCT(nchstats.ncs_falsehits, nchtotal),
614 PCT(nchstats.ncs_long, nchtotal));
615 #ifndef NEWVM
616 kread(X_XSTATS, &xstats, sizeof(xstats));
617 (void)printf("%9lu total calls to xalloc (cache hits %d%%)\n",
618 xstats.alloc, PCT(xstats.alloc_cachehit, xstats.alloc));
619 (void)printf("%9s sticky %lu flushed %lu unused %lu\n", "",
620 xstats.alloc_inuse, xstats.alloc_cacheflush, xstats.alloc_unused);
621 (void)printf("%9lu total calls to xfree", xstats.free);
622 (void)printf(" (sticky %lu cached %lu swapped %lu)\n",
623 xstats.free_inuse, xstats.free_cache, xstats.free_cacheswap);
624 #endif
625 #if defined(tahoe)
626 kread(X_CKEYSTATS, &keystats, sizeof(keystats));
627 (void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n",
628 keystats.ks_allocs, "code cache keys allocated",
629 PCT(keystats.ks_allocfree, keystats.ks_allocs),
630 PCT(keystats.ks_norefs, keystats.ks_allocs),
631 PCT(keystats.ks_taken, keystats.ks_allocs),
632 PCT(keystats.ks_shared, keystats.ks_allocs));
633 kread(X_DKEYSTATS, &keystats, sizeof(keystats));
634 (void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n",
635 keystats.ks_allocs, "data 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 #endif
641 }
642
643 #ifdef notdef
644 void
645 doforkst()
646 {
647 struct forkstat fks;
648
649 kread(X_FORKSTAT, &fks, sizeof(struct forkstat));
650 (void)printf("%d forks, %d pages, average %.2f\n",
651 fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork);
652 (void)printf("%d vforks, %d pages, average %.2f\n",
653 fks.cntvfork, fks.sizvfork, (double)fks.sizvfork / fks.cntvfork);
654 }
655 #endif
656
657 void
658 dkstats()
659 {
660 register int dn, state;
661 double etime;
662 long tmp;
663
664 for (dn = 0; dn < dk_ndrive; ++dn) {
665 tmp = cur.xfer[dn];
666 cur.xfer[dn] -= last.xfer[dn];
667 last.xfer[dn] = tmp;
668 }
669 etime = 0;
670 for (state = 0; state < CPUSTATES; ++state) {
671 tmp = cur.time[state];
672 cur.time[state] -= last.time[state];
673 last.time[state] = tmp;
674 etime += cur.time[state];
675 }
676 if (etime == 0)
677 etime = 1;
678 etime /= hz;
679 for (dn = 0; dn < dk_ndrive; ++dn) {
680 if (!dr_select[dn])
681 continue;
682 (void)printf("%2.0f ", cur.xfer[dn] / etime);
683 }
684 }
685
686 void
687 cpustats()
688 {
689 register int state;
690 double pct, total;
691
692 total = 0;
693 for (state = 0; state < CPUSTATES; ++state)
694 total += cur.time[state];
695 if (total)
696 pct = 100 / total;
697 else
698 pct = 0;
699 (void)printf("%2.0f ", /* user + nice */
700 (cur.time[0] + cur.time[1]) * pct);
701 (void)printf("%2.0f ", cur.time[2] * pct); /* system */
702 (void)printf("%2.0f", cur.time[3] * pct); /* idle */
703 }
704
705 void
706 dointr()
707 {
708 register unsigned long *intrcnt, inttotal, uptime;
709 register int nintr, inamlen;
710 register char *intrname;
711
712 uptime = getuptime();
713 nintr = nl[X_EINTRCNT].n_value - nl[X_INTRCNT].n_value;
714 inamlen = nl[X_EINTRNAMES].n_value - nl[X_INTRNAMES].n_value;
715 intrcnt = malloc((size_t)nintr);
716 intrname = malloc((size_t)inamlen);
717 if (intrcnt == NULL || intrname == NULL) {
718 (void)fprintf(stderr, "vmstat: %s.\n", strerror(errno));
719 exit(1);
720 }
721 kread(X_INTRCNT, intrcnt, (size_t)nintr);
722 kread(X_INTRNAMES, intrname, (size_t)inamlen);
723 (void)printf("%-12s %10s %8s\n", "interrupt", "count", "rate");
724 inttotal = 0;
725 nintr /= sizeof(long);
726 while (--nintr >= 0) {
727 if (*intrcnt)
728 (void)printf("%-12s %10lu %8lu\n", intrname,
729 *intrcnt, *intrcnt / uptime);
730 intrname += strlen(intrname) + 1;
731 inttotal += *intrcnt++;
732 }
733 (void)printf("%-12s %10lu %8lu\n", "Total", inttotal, inttotal / uptime);
734 }
735
736 /*
737 * These names are defined in <sys/malloc.h>.
738 */
739 char *kmemnames[] = INITKMEMNAMES;
740
741 void
742 domem()
743 {
744 register struct kmembuckets *kp;
745 register struct kmemstats *ks;
746 register int i;
747 int size;
748 long totuse = 0, totfree = 0, totreq = 0, totwaste = 0;
749 struct kmemstats kmemstats[M_LAST + 1];
750 struct kmembuckets buckets[MINBUCKET + 16];
751
752 kread(X_KMEMBUCKETS, buckets, sizeof(buckets));
753 (void)printf("Memory statistics by bucket size\n");
754 (void)printf(
755 " Size In Use Free Requests HighWater Couldfree\n");
756 for (i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16; i++, kp++) {
757 if (kp->kb_calls == 0)
758 continue;
759 size = 1 << i;
760 (void)printf("%8d %8ld %6ld %10ld %7ld %10ld\n", size,
761 kp->kb_total - kp->kb_totalfree,
762 kp->kb_totalfree, kp->kb_calls,
763 kp->kb_highwat, kp->kb_couldfree);
764 totfree += size * kp->kb_totalfree;
765 if (kp->kb_highwat < kp->kb_totalfree)
766 totwaste += (kp->kb_totalfree - kp->kb_highwat) * size;
767 }
768
769 kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats));
770 (void)printf("\nMemory statistics by type\n");
771 (void)printf(
772 " Type In Use MemUse HighUse Limit Requests TypeLimit KernLimit\n");
773 for (i = 0, ks = &kmemstats[0]; i <= M_LAST; i++, ks++) {
774 if (ks->ks_calls == 0)
775 continue;
776 (void)printf("%12s %6ld %7ldK %8ldK %5ldK %8ld %6u %9u\n",
777 kmemnames[i] ? kmemnames[i] : "undefined",
778 ks->ks_inuse, (ks->ks_memuse + 1023) / 1024,
779 (ks->ks_maxused + 1023) / 1024,
780 (ks->ks_limit + 1023) / 1024, ks->ks_calls,
781 ks->ks_limblocks, ks->ks_mapblocks);
782 totuse += ks->ks_memuse;
783 totreq += ks->ks_calls;
784 }
785 (void)printf("\nMemory Totals: In Use Free Wasted Requests\n");
786 (void)printf(" %7ldK %6ldK %6ldK %8ld\n",
787 (totuse + 1023) / 1024, (totfree + 1023) / 1024,
788 (totwaste + 1023) / 1024, totreq);
789 }
790
791 /*
792 * kread reads something from the kernel, given its nlist index.
793 */
794 void
795 kread(nlx, addr, size)
796 int nlx;
797 void *addr;
798 size_t size;
799 {
800 char *sym;
801
802 if (nl[nlx].n_type == 0 || nl[nlx].n_value == 0) {
803 sym = nl[nlx].n_name;
804 if (*sym == '_')
805 ++sym;
806 (void)fprintf(stderr,
807 "vmstat: %s: symbol %s not defined\n", vmunix, sym);
808 exit(1);
809 }
810 if (kvm_read((void *)nl[nlx].n_value, addr, size) != size) {
811 sym = nl[nlx].n_name;
812 if (*sym == '_')
813 ++sym;
814 (void)fprintf(stderr, "vmstat: %s: %s\n", sym, kvm_geterr());
815 exit(1);
816 }
817 }
818
819 void
820 usage()
821 {
822 (void)fprintf(stderr,
823 #ifndef NEWVM
824 "usage: vmstat [-fimst] [-c count] [-M core] \
825 [-N system] [-w wait] [disks]\n");
826 #else
827 "usage: vmstat [-ims] [-c count] [-M core] \
828 [-N system] [-w wait] [disks]\n");
829 #endif
830 exit(1);
831 }
832