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