1 /** $MirOS: src/usr.bin/vmstat/vmstat.c,v 1.4 2005/11/23 20:38:25 tg Exp $ */
2 /* $NetBSD: vmstat.c,v 1.29.4.1 1996/06/05 00:21:05 cgd Exp $ */
3 /* $OpenBSD: vmstat.c,v 1.96 2005/07/04 01:54:10 djm Exp $ */
4
5 /*
6 * Copyright (c) 1980, 1986, 1991, 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. 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 #include <sys/param.h>
41 #include <sys/time.h>
42 #include <sys/proc.h>
43 #include <sys/user.h>
44 #include <sys/dkstat.h>
45 #include <sys/buf.h>
46 #include <sys/namei.h>
47 #include <sys/malloc.h>
48 #include <sys/fcntl.h>
49 #include <sys/ioctl.h>
50 #include <sys/sysctl.h>
51 #include <sys/device.h>
52 #include <sys/pool.h>
53 #include <time.h>
54 #include <nlist.h>
55 #include <kvm.h>
56 #include <err.h>
57 #include <errno.h>
58 #include <unistd.h>
59 #include <signal.h>
60 #include <stdio.h>
61 #include <ctype.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <paths.h>
65 #include <limits.h>
66 #include "dkstats.h"
67
68 #include <uvm/uvm_object.h>
69 #include <uvm/uvm_extern.h>
70
71 __SCCSID("@(#)vmstat.c 8.1 (Berkeley) 6/6/93");
72 __RCSID("$MirOS: src/usr.bin/vmstat/vmstat.c,v 1.4 2005/11/23 20:38:25 tg Exp $");
73
74 struct nlist namelist[] = {
75 #define X_UVMEXP 0 /* sysctl */
76 { "_uvmexp" },
77 #define X_BOOTTIME 1 /* sysctl */
78 { "_boottime" },
79 #define X_NCHSTATS 2 /* sysctl */
80 { "_nchstats" },
81 #define X_KMEMSTAT 3 /* sysctl */
82 { "_kmemstats" },
83 #define X_KMEMBUCKETS 4 /* sysctl */
84 { "_bucket" },
85 #define X_FORKSTAT 5 /* sysctl */
86 { "_forkstat" },
87 #define X_NSELCOLL 6 /* sysctl */
88 { "_nselcoll" },
89 #define X_POOLHEAD 7 /* sysctl */
90 { "_pool_head" },
91 #define X_END 8
92 { "" },
93 };
94
95 /* Objects defined in dkstats.c */
96 extern struct _disk cur, last;
97 extern char **dr_name;
98 extern int *dk_select, dk_ndrive;
99
100 struct uvmexp uvmexp, ouvmexp;
101 int ndrives;
102
103 int winlines = 20;
104
105 kvm_t *kd;
106
107 #define FORKSTAT 0x01
108 #define INTRSTAT 0x02
109 #define MEMSTAT 0x04
110 #define SUMSTAT 0x08
111 #define TIMESTAT 0x10
112 #define VMSTAT 0x20
113
114 void cpustats(void);
115 void dkstats(void);
116 void dointr(void);
117 void domem(void);
118 void dopool(void);
119 void dosum(void);
120 void dovmstat(u_int, int);
121 void kread(int, void *, size_t);
122 void usage(void);
123 void dotimes(void);
124 void doforkst(void);
125 void printhdr(void);
126
127 char **choosedrives(char **);
128
129 /* Namelist and memory file names. */
130 char *nlistf, *memf;
131
132 extern char *__progname;
133
134 int verbose = 0;
135 int zflag = 0;
136
137 int ncpu;
138
139 int
main(int argc,char * argv[])140 main(int argc, char *argv[])
141 {
142 extern int optind;
143 extern char *optarg;
144 int mib[2];
145 size_t size;
146 int c, todo;
147 u_int interval;
148 int reps;
149 char errbuf[_POSIX2_LINE_MAX];
150 gid_t gid;
151
152 interval = reps = todo = 0;
153 while ((c = getopt(argc, argv, "c:fiM:mN:stw:vz")) != -1) {
154 switch (c) {
155 case 'c':
156 reps = atoi(optarg);
157 break;
158 case 'f':
159 todo |= FORKSTAT;
160 break;
161 case 'i':
162 todo |= INTRSTAT;
163 break;
164 case 'M':
165 memf = optarg;
166 break;
167 case 'm':
168 todo |= MEMSTAT;
169 break;
170 case 'N':
171 nlistf = optarg;
172 break;
173 case 's':
174 todo |= SUMSTAT;
175 break;
176 case 't':
177 todo |= TIMESTAT;
178 break;
179 case 'w':
180 interval = atoi(optarg);
181 break;
182 case 'v':
183 verbose = 1;
184 break;
185 case 'z':
186 zflag = 1;
187 break;
188 case '?':
189 default:
190 usage();
191 }
192 }
193 argc -= optind;
194 argv += optind;
195
196 if (todo == 0)
197 todo = VMSTAT;
198
199 gid = getgid();
200 if (nlistf != NULL || memf != NULL) {
201 if (setresgid(gid, gid, gid) == -1)
202 err(1, "setresgid");
203 }
204
205 /*
206 * Discard setgid privileges if not the running kernel so that bad
207 * guys can't print interesting stuff from kernel memory.
208 */
209 #if notyet
210 if (nlistf != NULL || memf != NULL) {
211 #endif
212 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
213 if (kd == 0)
214 errx(1, "kvm_openfiles: %s", errbuf);
215
216 if (nlistf == NULL && memf == NULL)
217 if (setresgid(gid, gid, gid) == -1)
218 err(1, "setresgid");
219
220 if ((c = kvm_nlist(kd, namelist)) != 0) {
221
222 if (c > 0) {
223 (void)fprintf(stderr,
224 "%s: undefined symbols:", __progname);
225 for (c = 0;
226 c < sizeof(namelist)/sizeof(namelist[0]);
227 c++)
228 if (namelist[c].n_type == 0)
229 fprintf(stderr, " %s",
230 namelist[c].n_name);
231 (void)fputc('\n', stderr);
232 exit(1);
233 } else
234 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
235 }
236 #ifdef notyet
237 } else if (setresgid(gid, gid, gid) == -1)
238 err(1, "setresgid");
239 #endif /* notyet */
240
241 mib[0] = CTL_HW;
242 mib[1] = HW_NCPU;
243 size = sizeof(ncpu);
244 (void) sysctl(mib, 2, &ncpu, &size, NULL, 0);
245
246 if (todo & VMSTAT) {
247 struct winsize winsize;
248
249 dkinit(0); /* Initialize disk stats, no disks selected. */
250 argv = choosedrives(argv); /* Select disks. */
251 winsize.ws_row = 0;
252 (void) ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize);
253 if (winsize.ws_row > 0)
254 winlines = winsize.ws_row;
255
256 }
257
258 #define BACKWARD_COMPATIBILITY
259 #ifdef BACKWARD_COMPATIBILITY
260 if (*argv) {
261 interval = atoi(*argv);
262 if (*++argv)
263 reps = atoi(*argv);
264 }
265 #endif
266
267 if (interval) {
268 if (!reps)
269 reps = -1;
270 } else if (reps)
271 interval = 1;
272
273 if (todo & FORKSTAT)
274 doforkst();
275 if (todo & MEMSTAT) {
276 domem();
277 dopool();
278 }
279 if (todo & SUMSTAT)
280 dosum();
281 if (todo & TIMESTAT)
282 dotimes();
283 if (todo & INTRSTAT)
284 dointr();
285 if (todo & VMSTAT)
286 dovmstat(interval, reps);
287 exit(0);
288 }
289
290 char **
choosedrives(char ** argv)291 choosedrives(char **argv)
292 {
293 int i;
294
295 /*
296 * Choose drives to be displayed. Priority goes to (in order) drives
297 * supplied as arguments, default drives. If everything isn't filled
298 * in and there are drives not taken care of, display the first few
299 * that fit.
300 */
301 #define BACKWARD_COMPATIBILITY
302 for (ndrives = 0; *argv; ++argv) {
303 #ifdef BACKWARD_COMPATIBILITY
304 if (isdigit(**argv))
305 break;
306 #endif
307 for (i = 0; i < dk_ndrive; i++) {
308 if (strcmp(dr_name[i], *argv))
309 continue;
310 dk_select[i] = 1;
311 ++ndrives;
312 break;
313 }
314 }
315 for (i = 0; i < dk_ndrive && ndrives < 2; i++) {
316 if (dk_select[i])
317 continue;
318 dk_select[i] = 1;
319 ++ndrives;
320 }
321 return(argv);
322 }
323
324 time_t
getuptime(void)325 getuptime(void)
326 {
327 static time_t now;
328 static struct timeval boottime;
329 time_t uptime;
330 int mib[2];
331 size_t size;
332
333 if (boottime.tv_sec == 0) {
334 if (nlistf == NULL && memf == NULL) {
335 size = sizeof(boottime);
336 mib[0] = CTL_KERN;
337 mib[1] = KERN_BOOTTIME;
338 if (sysctl(mib, 2, &boottime, &size, NULL, 0) < 0) {
339 warn("could not get kern.boottime");
340 bzero(&boottime, sizeof(boottime));
341 }
342 } else {
343 kread(X_BOOTTIME, &boottime, sizeof(boottime));
344 }
345 }
346 (void)time(&now);
347 uptime = now - boottime.tv_sec;
348 if (uptime <= 0 || uptime > 60*60*24*365*10)
349 errx(1, "time makes no sense; namelist must be wrong");
350
351 return(uptime);
352 }
353
354 int hz, hdrcnt;
355
356 void
dovmstat(u_int interval,int reps)357 dovmstat(u_int interval, int reps)
358 {
359 struct vmtotal total;
360 time_t uptime, halfuptime;
361 void needhdr(int);
362 int mib[2];
363 struct clockinfo clkinfo;
364 size_t size;
365
366 uptime = getuptime();
367 halfuptime = uptime / 2;
368 (void)signal(SIGCONT, needhdr);
369
370 mib[0] = CTL_KERN;
371 mib[1] = KERN_CLOCKRATE;
372 size = sizeof(clkinfo);
373 if (sysctl(mib, 2, &clkinfo, &size, NULL, 0) < 0) {
374 warn("could not read kern.clockrate");
375 return;
376 }
377 hz = clkinfo.stathz;
378
379 for (hdrcnt = 1;;) {
380 /* Read new disk statistics */
381 dkreadstats();
382 if (!--hdrcnt || last.dk_ndrive != cur.dk_ndrive)
383 printhdr();
384 if (nlistf == NULL && memf == NULL) {
385 size = sizeof(struct uvmexp);
386 mib[0] = CTL_VM;
387 mib[1] = VM_UVMEXP;
388 if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
389 warn("could not get vm.uvmexp");
390 bzero(&uvmexp, sizeof(struct uvmexp));
391 }
392 } else {
393 kread(X_UVMEXP, &uvmexp, sizeof(struct uvmexp));
394 }
395 size = sizeof(total);
396 mib[0] = CTL_VM;
397 mib[1] = VM_METER;
398 if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) {
399 warn("could not read vm.vmmeter");
400 bzero(&total, sizeof(total));
401 }
402 (void)printf("%2u%2u%2u",
403 total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
404 #define rate(x) (((x) + halfuptime) / uptime) /* round */
405 #define pgtok(a) ((a) * ((int)uvmexp.pagesize >> 10))
406 (void)printf("%7u%7u ",
407 pgtok(total.t_avm), pgtok(total.t_free));
408 (void)printf("%5u ", (unsigned)rate(uvmexp.faults - ouvmexp.faults));
409 (void)printf("%3u ", (unsigned)rate(uvmexp.pdreact - ouvmexp.pdreact));
410 (void)printf("%3u ", (unsigned)rate(uvmexp.pageins - ouvmexp.pageins));
411 (void)printf("%3u %3u ",
412 (unsigned)rate(uvmexp.pdpageouts - ouvmexp.pdpageouts), 0);
413 (void)printf("%3u ", (unsigned)rate(uvmexp.pdscans - ouvmexp.pdscans));
414 dkstats();
415 (void)printf("%4u %5u %4u ",
416 (unsigned)rate(uvmexp.intrs - ouvmexp.intrs),
417 (unsigned)rate(uvmexp.syscalls - ouvmexp.syscalls),
418 (unsigned)rate(uvmexp.swtch - ouvmexp.swtch));
419 cpustats();
420 (void)printf("\n");
421 (void)fflush(stdout);
422 if (reps >= 0 && --reps <= 0)
423 break;
424 ouvmexp = uvmexp;
425 uptime = interval;
426 /*
427 * We round upward to avoid losing low-frequency events
428 * (i.e., >= 1 per interval but < 1 per second).
429 */
430 halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2;
431 (void)sleep(interval);
432 }
433 }
434
435 void
printhdr(void)436 printhdr(void)
437 {
438 int i;
439
440 (void)printf(" procs memory page%*s", 20, "");
441 if (ndrives > 0)
442 (void)printf("%s %*straps cpu\n",
443 ((ndrives > 1) ? "disks" : "disk"),
444 ((ndrives > 1) ? ndrives * 4 - 4 : 0), "");
445 else
446 (void)printf("%*s traps cpu\n",
447 ndrives * 3, "");
448
449 (void)printf(" r b w avm fre flt re pi po fr sr ");
450 for (i = 0; i < dk_ndrive; i++)
451 if (dk_select[i])
452 (void)printf("%c%c%c ", dr_name[i][0],
453 dr_name[i][1],
454 dr_name[i][strlen(dr_name[i]) - 1]);
455 (void)printf(" int sys cs us sy id\n");
456 hdrcnt = winlines - 2;
457 }
458
459 /*
460 * Force a header to be prepended to the next output.
461 */
462 /* ARGSUSED */
463 void
needhdr(int signo)464 needhdr(int signo)
465 {
466
467 hdrcnt = 1;
468 }
469
470 void
dotimes(void)471 dotimes(void)
472 {
473 u_int pgintime, rectime;
474 int mib[2];
475 size_t size;
476
477 /* XXX Why are these set to 0 ? This doesn't look right. */
478 pgintime = 0;
479 rectime = 0;
480
481 if (nlistf == NULL && memf == NULL) {
482 size = sizeof(struct uvmexp);
483 mib[0] = CTL_VM;
484 mib[1] = VM_UVMEXP;
485 if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
486 warn("could not read vm.uvmexp");
487 bzero(&uvmexp, sizeof(struct uvmexp));
488 }
489 } else {
490 kread(X_UVMEXP, &uvmexp, sizeof(struct uvmexp));
491 }
492
493 (void)printf("%u reactivates, %u total time (usec)\n",
494 uvmexp.pdreact, rectime);
495 if (uvmexp.pdreact != 0)
496 (void)printf("average: %u usec / reclaim\n",
497 rectime / uvmexp.pdreact);
498 (void)printf("\n");
499 (void)printf("%u page ins, %u total time (msec)\n",
500 uvmexp.pageins, pgintime / 10);
501 if (uvmexp.pageins != 0)
502 (void)printf("average: %8.1f msec / page in\n",
503 pgintime / (uvmexp.pageins * 10.0));
504 }
505
506 int
pct(long top,long bot)507 pct(long top, long bot)
508 {
509 long ans;
510
511 if (bot == 0)
512 return(0);
513 ans = (quad_t)top * 100 / bot;
514 return (ans);
515 }
516
517 #define PCT(top, bot) pct((long)(top), (long)(bot))
518
519 void
dosum(void)520 dosum(void)
521 {
522 struct nchstats nchstats;
523 long nchtotal;
524 size_t size;
525 int mib[2], nselcoll;
526
527 if (nlistf == NULL && memf == NULL) {
528 size = sizeof(struct uvmexp);
529 mib[0] = CTL_VM;
530 mib[1] = VM_UVMEXP;
531 if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
532 warn("could not read vm.uvmexp");
533 bzero(&uvmexp, sizeof(struct uvmexp));
534 }
535 } else {
536 kread(X_UVMEXP, &uvmexp, sizeof(struct uvmexp));
537 }
538
539 /* vm_page constants */
540 (void)printf("%11u bytes per page\n", uvmexp.pagesize);
541
542 /* vm_page counters */
543 (void)printf("%11u pages managed\n", uvmexp.npages);
544 (void)printf("%11u pages free\n", uvmexp.free);
545 (void)printf("%11u pages active\n", uvmexp.active);
546 (void)printf("%11u pages inactive\n", uvmexp.inactive);
547 (void)printf("%11u pages being paged out\n", uvmexp.paging);
548 (void)printf("%11u pages wired\n", uvmexp.wired);
549 (void)printf("%11u pages zeroed\n", uvmexp.zeropages);
550 (void)printf("%11u pages reserved for pagedaemon\n",
551 uvmexp.reserve_pagedaemon);
552 (void)printf("%11u pages reserved for kernel\n",
553 uvmexp.reserve_kernel);
554
555 /* swap */
556 (void)printf("%11u swap pages\n", uvmexp.swpages);
557 (void)printf("%11u swap pages in use\n", uvmexp.swpginuse);
558 (void)printf("%11u total anon's in system\n", uvmexp.nanon);
559 (void)printf("%11u free anon's\n", uvmexp.nfreeanon);
560
561 /* stat counters */
562 (void)printf("%11u page faults\n", uvmexp.faults);
563 (void)printf("%11u traps\n", uvmexp.traps);
564 (void)printf("%11u interrupts\n", uvmexp.intrs);
565 (void)printf("%11u cpu context switches\n", uvmexp.swtch);
566 #if 0 /* XXX */
567 (void)printf("%11u fpu context switches\n", uvmexp.fpswtch);
568 #endif
569 (void)printf("%11u software interrupts\n", uvmexp.softs);
570 (void)printf("%11u syscalls\n", uvmexp.syscalls);
571 (void)printf("%11u pagein operations\n", uvmexp.pageins);
572 (void)printf("%11u swap ins\n", uvmexp.swapins);
573 (void)printf("%11u swap outs\n", uvmexp.swapouts);
574 (void)printf("%11u forks\n", uvmexp.forks);
575 (void)printf("%11u forks where vmspace is shared\n",
576 uvmexp.forks_sharevm);
577
578 /* daemon counters */
579 (void)printf("%11u number of times the pagedaemon woke up\n",
580 uvmexp.pdwoke);
581 (void)printf("%11u revolutions of the clock hand\n", uvmexp.pdrevs);
582 (void)printf("%11u pages freed by pagedaemon\n", uvmexp.pdfreed);
583 (void)printf("%11u pages scanned by pagedaemon\n", uvmexp.pdscans);
584 (void)printf("%11u pages reactivated by pagedaemon\n", uvmexp.pdreact);
585 (void)printf("%11u busy pages found by pagedaemon\n", uvmexp.pdbusy);
586
587 if (nlistf == NULL && memf == NULL) {
588 size = sizeof(nchstats);
589 mib[0] = CTL_KERN;
590 mib[1] = KERN_NCHSTATS;
591 if (sysctl(mib, 2, &nchstats, &size, NULL, 0) < 0) {
592 warn("could not read kern.nchstats");
593 bzero(&nchstats, sizeof(nchstats));
594 }
595 } else {
596 kread(X_NCHSTATS, &nchstats, sizeof(nchstats));
597 }
598
599 nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
600 nchstats.ncs_badhits + nchstats.ncs_falsehits +
601 nchstats.ncs_miss + nchstats.ncs_long;
602 (void)printf("%11ld total name lookups\n", nchtotal);
603 (void)printf("%11s cache hits (%d%% pos + %d%% neg) system %d%% "
604 "per-directory\n",
605 "", PCT(nchstats.ncs_goodhits, nchtotal),
606 PCT(nchstats.ncs_neghits, nchtotal),
607 PCT(nchstats.ncs_pass2, nchtotal));
608 (void)printf("%11s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
609 PCT(nchstats.ncs_badhits, nchtotal),
610 PCT(nchstats.ncs_falsehits, nchtotal),
611 PCT(nchstats.ncs_long, nchtotal));
612
613 if (nlistf == NULL && memf == NULL) {
614 size = sizeof(nselcoll);
615 mib[0] = CTL_KERN;
616 mib[1] = KERN_NSELCOLL;
617 if (sysctl(mib, 2, &nselcoll, &size, NULL, 0) < 0) {
618 warn("could not read kern.nselcoll");
619 nselcoll = 0;
620 }
621 } else {
622 kread(X_NSELCOLL, &nselcoll, sizeof(nselcoll));
623 }
624 (void)printf("%11d select collisions\n", nselcoll);
625 }
626
627 void
doforkst(void)628 doforkst(void)
629 {
630 struct forkstat fks;
631 size_t size;
632 int mib[2];
633
634 if (nlistf == NULL && memf == NULL) {
635 size = sizeof(struct forkstat);
636 mib[0] = CTL_KERN;
637 mib[1] = KERN_FORKSTAT;
638 if (sysctl(mib, 2, &fks, &size, NULL, 0) < 0) {
639 warn("could not read kern.forkstat");
640 bzero(&fks, sizeof(struct forkstat));
641 }
642 } else {
643 kread(X_FORKSTAT, &fks, sizeof(struct forkstat));
644 }
645
646 (void)printf("%d forks, %d pages, average %.2f\n",
647 fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork);
648 (void)printf("%d vforks, %d pages, average %.2f\n",
649 fks.cntvfork, fks.sizvfork,
650 (double)fks.sizvfork / (fks.cntvfork ? fks.cntvfork : 1));
651 (void)printf("%d rforks, %d pages, average %.2f\n",
652 fks.cntrfork, fks.sizrfork,
653 (double)fks.sizrfork / (fks.cntrfork ? fks.cntrfork : 1));
654 (void)printf("%d kthread creations, %d pages, average %.2f\n",
655 fks.cntkthread, fks.sizkthread,
656 (double)fks.sizkthread / (fks.cntkthread ? fks.cntkthread : 1));
657 }
658
659 void
dkstats(void)660 dkstats(void)
661 {
662 int dn, state;
663 double etime;
664
665 /* Calculate disk stat deltas. */
666 dkswap();
667 etime = 0;
668 for (state = 0; state < CPUSTATES; ++state) {
669 etime += cur.cp_time[state];
670 }
671 if (etime == 0)
672 etime = 1;
673 etime /= hz;
674 etime /= ncpu;
675 for (dn = 0; dn < dk_ndrive; ++dn) {
676 if (!dk_select[dn])
677 continue;
678 (void)printf("%3.0f ",
679 (cur.dk_rxfer[dn] + cur.dk_rxfer[dn]) / etime);
680 }
681 }
682
683 void
cpustats(void)684 cpustats(void)
685 {
686 int state;
687 double pct, total;
688
689 total = 0;
690 for (state = 0; state < CPUSTATES; ++state)
691 total += cur.cp_time[state];
692 if (total)
693 pct = 100 / total;
694 else
695 pct = 0;
696 (void)printf("%2.0f ", (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pct);
697 (void)printf("%2.0f ", (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pct);
698 (void)printf("%2.0f", cur.cp_time[CP_IDLE] * pct);
699 }
700
701 void
dointr(void)702 dointr(void)
703 {
704 struct device dev;
705
706 time_t uptime;
707 u_int64_t inttotal;
708 int nintr;
709 char intrname[128];
710 int mib[4];
711 size_t siz;
712 int i;
713
714 if (nlistf != NULL || memf != NULL) {
715 errx(1,
716 "interrupt statistics are only available on live kernels");
717 }
718
719 uptime = getuptime();
720
721 mib[0] = CTL_KERN;
722 mib[1] = KERN_INTRCNT;
723 mib[2] = KERN_INTRCNT_NUM;
724 siz = sizeof(nintr);
725 if (sysctl(mib, 3, &nintr, &siz, NULL, 0) < 0) {
726 warnx("could not read kern.intrcnt.nintrcnt");
727 return;
728 }
729
730 (void)printf("%-16s %20s %8s\n", "interrupt", "total", "rate");
731
732 inttotal = 0;
733 for (i = 0; i < nintr; i++) {
734 char name[128];
735 u_quad_t cnt;
736 int vector;
737
738 mib[0] = CTL_KERN;
739 mib[1] = KERN_INTRCNT;
740 mib[2] = KERN_INTRCNT_NAME;
741 mib[3] = i;
742 siz = sizeof(name);
743 if (sysctl(mib, 4, name, &siz, NULL, 0) < 0) {
744 warnx("could not read kern.intrcnt.name.%d", i);
745 return;
746 }
747
748 #ifdef KERN_INTRCNT_VECTOR
749 mib[0] = CTL_KERN;
750 mib[1] = KERN_INTRCNT;
751 mib[2] = KERN_INTRCNT_VECTOR;
752 mib[3] = i;
753 siz = sizeof(vector);
754 if (sysctl(mib, 4, &vector, &siz, NULL, 0) < 0) {
755 strlcpy(intrname, name, sizeof(intrname));
756 } else {
757 snprintf(intrname, sizeof(intrname), "irq%d/%s",
758 vector, name);
759 }
760 #endif
761
762 mib[0] = CTL_KERN;
763 mib[1] = KERN_INTRCNT;
764 mib[2] = KERN_INTRCNT_CNT;
765 mib[3] = i;
766 siz = sizeof(cnt);
767 if (sysctl(mib, 4, &cnt, &siz, NULL, 0) < 0) {
768 warnx("could not read kern.intrcnt.cnt.%d", i);
769 return;
770 }
771
772 if (cnt || zflag)
773 (void)printf("%-16.16s %20llu %8llu\n", intrname,
774 cnt, cnt / uptime);
775 inttotal += cnt;
776 }
777
778 (void)printf("%-16s %20llu %8llu\n", "Total", inttotal,
779 inttotal / uptime);
780 }
781
782 /*
783 * These names are defined in <sys/malloc.h>.
784 */
785 char *kmemnames[] = INITKMEMNAMES;
786
787 void
domem(void)788 domem(void)
789 {
790 struct kmembuckets *kp;
791 struct kmemstats *ks;
792 int i, j;
793 int len, size, first;
794 u_long totuse = 0, totfree = 0;
795 quad_t totreq = 0;
796 char *name;
797 struct kmemstats kmemstats[M_LAST];
798 struct kmembuckets buckets[MINBUCKET + 16];
799 int mib[4];
800 size_t siz;
801 char buf[BUFSIZ], *bufp, *ap;
802
803 if (memf == NULL && nlistf == NULL) {
804 mib[0] = CTL_KERN;
805 mib[1] = KERN_MALLOCSTATS;
806 mib[2] = KERN_MALLOC_BUCKETS;
807 siz = sizeof(buf);
808 if (sysctl(mib, 3, buf, &siz, NULL, 0) < 0) {
809 warnx("could not read kern.malloc.buckets");
810 return;
811 }
812
813 bufp = buf;
814 mib[2] = KERN_MALLOC_BUCKET;
815 siz = sizeof(struct kmembuckets);
816 i = 0;
817 while ((ap = strsep(&bufp, ",")) != NULL) {
818 mib[3] = atoi(ap);
819
820 if (sysctl(mib, 4, &buckets[MINBUCKET + i], &siz,
821 NULL, 0) < 0) {
822 warn("could not read kern.malloc.bucket.%d", mib[3]);
823 return;
824 }
825 i++;
826 }
827 } else {
828 kread(X_KMEMBUCKETS, buckets, sizeof(buckets));
829 }
830
831 for (first = 1, i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16;
832 i++, kp++) {
833 if (kp->kb_calls == 0 && !verbose)
834 continue;
835 if (first) {
836 (void)printf("Memory statistics by bucket size\n");
837 (void)printf(
838 " Size In Use Free Requests HighWater Couldfree\n");
839 first = 0;
840 }
841 size = 1 << i;
842 (void)printf("%8d %8llu %6llu %18llu %7llu %10llu\n", size,
843 (unsigned long long)(kp->kb_total - kp->kb_totalfree),
844 (unsigned long long)kp->kb_totalfree,
845 (unsigned long long)kp->kb_calls,
846 (unsigned long long)kp->kb_highwat,
847 (unsigned long long)kp->kb_couldfree);
848 totfree += size * kp->kb_totalfree;
849 }
850
851 /*
852 * If kmem statistics are not being gathered by the kernel,
853 * first will still be 1.
854 */
855 if (first) {
856 printf(
857 "Kmem statistics are not being gathered by the kernel.\n");
858 return;
859 }
860
861 if (memf == NULL && nlistf == NULL) {
862 bzero(kmemstats, sizeof(kmemstats));
863 for (i = 0; i < M_LAST; i++) {
864 mib[0] = CTL_KERN;
865 mib[1] = KERN_MALLOCSTATS;
866 mib[2] = KERN_MALLOC_KMEMSTATS;
867 mib[3] = i;
868 siz = sizeof(struct kmemstats);
869
870 /*
871 * Skip errors -- these are presumed to be unallocated
872 * entries.
873 */
874 if (sysctl(mib, 4, &kmemstats[i], &siz, NULL, 0) < 0)
875 continue;
876 }
877 } else {
878 kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats));
879 }
880
881 (void)printf("\nMemory usage type by bucket size\n");
882 (void)printf(" Size Type(s)\n");
883 kp = &buckets[MINBUCKET];
884 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) {
885 if (kp->kb_calls == 0)
886 continue;
887 first = 1;
888 len = 8;
889 for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
890 if (ks->ks_calls == 0)
891 continue;
892 if ((ks->ks_size & j) == 0)
893 continue;
894 name = kmemnames[i] ? kmemnames[i] : "undefined";
895 len += 2 + strlen(name);
896 if (first)
897 printf("%8d %s", j, name);
898 else
899 printf(",");
900 if (len >= 80) {
901 printf("\n\t ");
902 len = 10 + strlen(name);
903 }
904 if (!first)
905 printf(" %s", name);
906 first = 0;
907 }
908 printf("\n");
909 }
910
911 (void)printf(
912 "\nMemory statistics by type Type Kern\n");
913 (void)printf(
914 " Type InUse MemUse HighUse Limit Requests Limit Limit Size(s)\n");
915 for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
916 if (ks->ks_calls == 0)
917 continue;
918 (void)printf("%14s%6ld%6ldK%7ldK%6ldK%9ld%5u%6u",
919 kmemnames[i] ? kmemnames[i] : "undefined",
920 ks->ks_inuse, (ks->ks_memuse + 1023) / 1024,
921 (ks->ks_maxused + 1023) / 1024,
922 (ks->ks_limit + 1023) / 1024, ks->ks_calls,
923 ks->ks_limblocks, ks->ks_mapblocks);
924 first = 1;
925 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
926 if ((ks->ks_size & j) == 0)
927 continue;
928 if (first)
929 printf(" %d", j);
930 else
931 printf(",%d", j);
932 first = 0;
933 }
934 printf("\n");
935 totuse += ks->ks_memuse;
936 totreq += ks->ks_calls;
937 }
938 (void)printf("\nMemory Totals: In Use Free Requests\n");
939 (void)printf(" %7luK %6luK %8qu\n",
940 (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
941 }
942
943 static void
print_pool(struct pool * pp,char * name)944 print_pool(struct pool *pp, char *name)
945 {
946 static int first = 1;
947 int ovflw;
948 char maxp[32];
949
950 if (first) {
951 (void)printf("Memory resource pool statistics\n");
952 (void)printf(
953 "%-11s%5s%9s%5s%9s%6s%6s%6s%6s%6s%6s%5s\n",
954 "Name",
955 "Size",
956 "Requests",
957 "Fail",
958 "Releases",
959 "Pgreq",
960 "Pgrel",
961 "Npage",
962 "Hiwat",
963 "Minpg",
964 "Maxpg",
965 "Idle");
966 first = 0;
967 }
968
969 /* Skip unused pools unless verbose output. */
970 if (pp->pr_nget == 0 && !verbose)
971 return;
972
973 if (pp->pr_maxpages == UINT_MAX)
974 snprintf(maxp, sizeof maxp, "inf");
975 else
976 snprintf(maxp, sizeof maxp, "%u", pp->pr_maxpages);
977 /*
978 * Print single word. `ovflow' is number of characters didn't fit
979 * on the last word. `fmt' is a format string to print this word.
980 * It must contain asterisk for field width. `width' is a width
981 * occupied by this word. `fixed' is a number of constant chars in
982 * `fmt'. `val' is a value to be printed using format string `fmt'.
983 */
984 #define PRWORD(ovflw, fmt, width, fixed, val) do { \
985 (ovflw) += printf((fmt), \
986 (width) - (fixed) - (ovflw) > 0 ? \
987 (width) - (fixed) - (ovflw) : 0, \
988 (val)) - (width); \
989 if ((ovflw) < 0) \
990 (ovflw) = 0; \
991 } while (/* CONSTCOND */0)
992
993 ovflw = 0;
994 PRWORD(ovflw, "%-*s", 11, 0, name);
995 PRWORD(ovflw, " %*u", 5, 1, pp->pr_size);
996 PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nget);
997 PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nfail);
998 PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nput);
999 PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagealloc);
1000 PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagefree);
1001 PRWORD(ovflw, " %*d", 6, 1, pp->pr_npages);
1002 PRWORD(ovflw, " %*d", 6, 1, pp->pr_hiwat);
1003 PRWORD(ovflw, " %*d", 6, 1, pp->pr_minpages);
1004 PRWORD(ovflw, " %*s", 6, 1, maxp);
1005 PRWORD(ovflw, " %*lu\n", 5, 1, pp->pr_nidle);
1006 }
1007
1008 static void dopool_kvm(void);
1009 static void dopool_sysctl(void);
1010
1011 void
dopool(void)1012 dopool(void)
1013 {
1014 if (nlistf == NULL && memf == NULL)
1015 dopool_sysctl();
1016 else
1017 dopool_kvm();
1018 }
1019
1020 void
dopool_sysctl(void)1021 dopool_sysctl(void)
1022 {
1023 long total = 0, inuse = 0;
1024 struct pool pool;
1025 size_t size;
1026 int mib[4];
1027 int npools, i;
1028
1029 mib[0] = CTL_KERN;
1030 mib[1] = KERN_POOL;
1031 mib[2] = KERN_POOL_NPOOLS;
1032 size = sizeof(npools);
1033 if (sysctl(mib, 3, &npools, &size, NULL, 0) < 0) {
1034 warn("can't figure out number of pools in kernel");
1035 return;
1036 }
1037
1038 for (i = 1; npools; i++) {
1039 char name[32];
1040
1041 mib[0] = CTL_KERN;
1042 mib[1] = KERN_POOL;
1043 mib[2] = KERN_POOL_POOL;
1044 mib[3] = i;
1045 size = sizeof(struct pool);
1046 if (sysctl(mib, 4, &pool, &size, NULL, 0) < 0) {
1047 if (errno == ENOENT)
1048 continue;
1049 warn("error getting pool");
1050 return;
1051 }
1052 npools--;
1053 mib[2] = KERN_POOL_NAME;
1054 size = sizeof(name);
1055 if (sysctl(mib, 4, &name, &size, NULL, 0) < 0) {
1056 warn("error getting pool name");
1057 return;
1058 }
1059 print_pool(&pool, name);
1060
1061 inuse += (pool.pr_nget - pool.pr_nput) * pool.pr_size;
1062 total += pool.pr_npages * getpagesize(); /* XXX */
1063 }
1064
1065 inuse /= 1024;
1066 total /= 1024;
1067 printf("\nIn use %ldK, total allocated %ldK; utilization %.1f%%\n",
1068 inuse, total, (double)(100 * inuse) / total);
1069 }
1070
1071 void
dopool_kvm(void)1072 dopool_kvm(void)
1073 {
1074 long addr;
1075 long total = 0, inuse = 0;
1076 TAILQ_HEAD(,pool) pool_head;
1077 struct pool pool, *pp = &pool;
1078
1079 kread(X_POOLHEAD, &pool_head, sizeof(pool_head));
1080 addr = (long)TAILQ_FIRST(&pool_head);
1081
1082 while (addr != 0) {
1083 char name[32];
1084
1085 if (kvm_read(kd, addr, (void *)pp, sizeof *pp) != sizeof *pp) {
1086 (void)fprintf(stderr,
1087 "vmstat: pool chain trashed: %s\n",
1088 kvm_geterr(kd));
1089 exit(1);
1090 }
1091 if (kvm_read(kd, (long)pp->pr_wchan, name, sizeof name) < 0) {
1092 (void)fprintf(stderr,
1093 "vmstat: pool name trashed: %s\n",
1094 kvm_geterr(kd));
1095 exit(1);
1096 }
1097
1098 name[31] = '\0';
1099
1100 print_pool(pp, name);
1101
1102 inuse += (pp->pr_nget - pp->pr_nput) * pp->pr_size;
1103 total += pp->pr_npages * getpagesize(); /* XXX */
1104
1105 addr = (long)TAILQ_NEXT(pp, pr_poollist);
1106 }
1107
1108 inuse /= 1024;
1109 total /= 1024;
1110 printf("\nIn use %ldK, total allocated %ldK; utilization %.1f%%\n",
1111 inuse, total, (double)(100 * inuse) / total);
1112 }
1113
1114 /*
1115 * kread reads something from the kernel, given its nlist index.
1116 */
1117 void
kread(int nlx,void * addr,size_t size)1118 kread(int nlx, void *addr, size_t size)
1119 {
1120 char *sym;
1121
1122 if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
1123 sym = namelist[nlx].n_name;
1124 if (*sym == '_')
1125 ++sym;
1126 errx(1, "symbol %s not defined", sym);
1127 }
1128 if (kvm_read(kd, namelist[nlx].n_value, addr, size) != size) {
1129 sym = namelist[nlx].n_name;
1130 if (*sym == '_')
1131 ++sym;
1132 errx(1, "%s: %s", sym, kvm_geterr(kd));
1133 }
1134 }
1135
1136 void
usage(void)1137 usage(void)
1138 {
1139 (void)fprintf(stderr, "usage: %s [-fimstvz] [-c count] [-M core] "
1140 "[-N system] [-w wait] [disks]\n", __progname);
1141 exit(1);
1142 }
1143