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