2 * Copyright (c) 1980, 1986, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1986, 1991, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)vmstat.c 8.1 (Berkeley) 6/6/93";
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
49 #include <sys/param.h>
53 #include <sys/namei.h>
54 #include <sys/malloc.h>
55 #include <sys/signal.h>
56 #include <sys/fcntl.h>
57 #include <sys/ioctl.h>
58 #include <sys/resource.h>
59 #include <sys/sysctl.h>
60 #include <sys/vmmeter.h>
62 #include <vm/vm_param.h>
81 static char da[] = "da";
83 static struct nlist namelist[] = {
96 #define X_EINTRNAMES 6
102 #define X_KMEMSTATS 9
103 { "_kmemstatistics" },
104 #define X_KMEMZONES 10
107 #define X_DEFICIT XXX
122 static struct statinfo cur, last;
123 static int num_devices, maxshowdevs;
124 static long generation;
125 static struct device_selection *dev_select;
126 static int num_selected;
127 static struct devstat_match *matches;
128 static int num_matches = 0;
129 static int num_devices_specified, num_selections;
130 static long select_generation;
131 static char **specified_devices;
132 static devstat_select_mode select_mode;
134 static struct vmmeter sum, osum;
136 static int winlines = 20;
144 #define FORKSTAT 0x01
145 #define INTRSTAT 0x02
148 #define TIMESTAT 0x10
150 #define ZMEMSTAT 0x40
152 static void cpustats(void);
153 static void pcpustats(int, u_long, int);
154 static void devstats(void);
155 static void doforkst(void);
156 static void dointr(void);
157 static void dosum(void);
158 static void dovmstat(unsigned int, int);
159 static void domemstat_malloc(void);
160 static void domemstat_zone(void);
161 static void kread(int, void *, size_t);
162 static void kreado(int, void *, size_t, size_t);
163 static char *kgetstr(const char *);
164 static void needhdr(int);
165 static void printhdr(int, u_long);
166 static void usage(void);
168 static long pct(long, long);
169 static long getuptime(void);
171 static char **getdrivedata(char **);
174 main(int argc, char *argv[])
177 unsigned int interval;
180 char errbuf[_POSIX2_LINE_MAX];
182 memf = nlistf = NULL;
183 interval = reps = todo = 0;
186 while ((c = getopt(argc, argv, "ac:fhHiM:mN:n:Pp:stw:z")) != -1) {
220 maxshowdevs = atoi(optarg);
222 errx(1, "number of devices %d is < 0",
226 if (devstat_buildmatch(optarg, &matches, &num_matches) != 0)
227 errx(1, "%s", devstat_errbuf);
236 errx(EX_USAGE, "sorry, -t is not (re)implemented yet");
240 interval = atoi(optarg);
257 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
259 errx(1, "kvm_openfiles: %s", errbuf);
262 if (kd != NULL && (c = kvm_nlist(kd, namelist)) != 0) {
264 warnx("undefined symbols:");
266 c < (int)(sizeof(namelist)/sizeof(namelist[0]));
268 if (namelist[c].n_type == 0)
269 (void)fprintf(stderr, " %s",
271 (void)fputc('\n', stderr);
273 warnx("kvm_nlist: %s", kvm_geterr(kd));
277 errx(1, "Cannot use -P with crash dumps");
280 struct winsize winsize;
283 * Make sure that the userland devstat version matches the
284 * kernel devstat version. If not, exit and print a
285 * message informing the user of his mistake.
287 if (devstat_checkversion(NULL) < 0)
288 errx(1, "%s", devstat_errbuf);
291 argv = getdrivedata(argv);
293 (void) ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize);
294 if (winsize.ws_row > 0)
295 winlines = winsize.ws_row;
299 #define BACKWARD_COMPATIBILITY
300 #ifdef BACKWARD_COMPATIBILITY
302 interval = atoi(*argv);
329 dovmstat(interval, reps);
334 mysysctl(const char *name, void *oldp, size_t *oldlenp,
335 void *newp, size_t newlen)
339 error = sysctlbyname(name, oldp, oldlenp, newp, newlen);
340 if (error != 0 && errno != ENOMEM)
341 err(1, "sysctl(%s)", name);
346 getdrivedata(char **argv)
348 if ((num_devices = devstat_getnumdevs(NULL)) < 0)
349 errx(1, "%s", devstat_errbuf);
351 cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
352 last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
353 bzero(cur.dinfo, sizeof(struct devinfo));
354 bzero(last.dinfo, sizeof(struct devinfo));
356 if (devstat_getdevs(NULL, &cur) == -1)
357 errx(1, "%s", devstat_errbuf);
359 num_devices = cur.dinfo->numdevs;
360 generation = cur.dinfo->generation;
362 specified_devices = (char **)malloc(sizeof(char *));
363 for (num_devices_specified = 0; *argv; ++argv) {
366 num_devices_specified++;
367 specified_devices = (char **)realloc(specified_devices,
369 num_devices_specified);
370 specified_devices[num_devices_specified - 1] = *argv;
374 if (nflag == 0 && maxshowdevs < num_devices_specified)
375 maxshowdevs = num_devices_specified;
378 * People are generally only interested in disk statistics when
379 * they're running vmstat. So, that's what we're going to give
380 * them if they don't specify anything by default. We'll also give
381 * them any other random devices in the system so that we get to
382 * maxshowdevs devices, if that many devices exist. If the user
383 * specifies devices on the command line, either through a pattern
384 * match or by naming them explicitly, we will give the user only
387 if ((num_devices_specified == 0) && (num_matches == 0)) {
388 if (devstat_buildmatch(da, &matches, &num_matches) != 0)
389 errx(1, "%s", devstat_errbuf);
391 select_mode = DS_SELECT_ADD;
393 select_mode = DS_SELECT_ONLY;
396 * At this point, selectdevs will almost surely indicate that the
397 * device list has changed, so we don't look for return values of 0
398 * or 1. If we get back -1, though, there is an error.
400 if (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
401 &select_generation, generation, cur.dinfo->devices,
402 num_devices, matches, num_matches, specified_devices,
403 num_devices_specified, select_mode,
404 maxshowdevs, 0) == -1)
405 errx(1, "%s", devstat_errbuf);
416 (void)clock_gettime(CLOCK_MONOTONIC, &sp);
418 if (uptime <= 0 || uptime > 60*60*24*365*10)
419 errx(1, "time makes no sense; namelist must be wrong");
425 fill_vmmeter(struct vmmeter *vmmp)
428 kread(X_SUM, vmmp, sizeof(*vmmp));
430 size_t size = sizeof(unsigned int);
431 #define GET_VM_STATS(cat, name) \
432 mysysctl("vm.stats." #cat "." #name, &vmmp->name, &size, NULL, 0)
434 GET_VM_STATS(sys, v_swtch);
435 GET_VM_STATS(sys, v_trap);
436 GET_VM_STATS(sys, v_syscall);
437 GET_VM_STATS(sys, v_intr);
438 GET_VM_STATS(sys, v_soft);
441 GET_VM_STATS(vm, v_vm_faults);
442 GET_VM_STATS(vm, v_cow_faults);
443 GET_VM_STATS(vm, v_cow_optim);
444 GET_VM_STATS(vm, v_zfod);
445 GET_VM_STATS(vm, v_ozfod);
446 GET_VM_STATS(vm, v_swapin);
447 GET_VM_STATS(vm, v_swapout);
448 GET_VM_STATS(vm, v_swappgsin);
449 GET_VM_STATS(vm, v_swappgsout);
450 GET_VM_STATS(vm, v_vnodein);
451 GET_VM_STATS(vm, v_vnodeout);
452 GET_VM_STATS(vm, v_vnodepgsin);
453 GET_VM_STATS(vm, v_vnodepgsout);
454 GET_VM_STATS(vm, v_intrans);
455 GET_VM_STATS(vm, v_reactivated);
456 GET_VM_STATS(vm, v_pdwakeups);
457 GET_VM_STATS(vm, v_pdpages);
458 GET_VM_STATS(vm, v_tcached);
459 GET_VM_STATS(vm, v_dfree);
460 GET_VM_STATS(vm, v_pfree);
461 GET_VM_STATS(vm, v_tfree);
462 GET_VM_STATS(vm, v_page_size);
463 GET_VM_STATS(vm, v_page_count);
464 GET_VM_STATS(vm, v_free_reserved);
465 GET_VM_STATS(vm, v_free_target);
466 GET_VM_STATS(vm, v_free_min);
467 GET_VM_STATS(vm, v_free_count);
468 GET_VM_STATS(vm, v_wire_count);
469 GET_VM_STATS(vm, v_active_count);
470 GET_VM_STATS(vm, v_inactive_target);
471 GET_VM_STATS(vm, v_inactive_count);
472 GET_VM_STATS(vm, v_cache_count);
473 GET_VM_STATS(vm, v_cache_min);
474 GET_VM_STATS(vm, v_cache_max);
475 GET_VM_STATS(vm, v_pageout_free_min);
476 GET_VM_STATS(vm, v_interrupt_free_min);
477 /*GET_VM_STATS(vm, v_free_severe);*/
478 GET_VM_STATS(vm, v_forks);
479 GET_VM_STATS(vm, v_vforks);
480 GET_VM_STATS(vm, v_rforks);
481 GET_VM_STATS(vm, v_kthreads);
482 GET_VM_STATS(vm, v_forkpages);
483 GET_VM_STATS(vm, v_vforkpages);
484 GET_VM_STATS(vm, v_rforkpages);
485 GET_VM_STATS(vm, v_kthreadpages);
491 fill_vmtotal(struct vmtotal *vmtp)
495 errx(1, "not implemented");
497 size_t size = sizeof(*vmtp);
498 mysysctl("vm.vmtotal", vmtp, &size, NULL, 0);
499 if (size != sizeof(*vmtp))
500 errx(1, "vm.total size mismatch");
504 /* Determine how many cpu columns, and what index they are in kern.cp_times */
506 getcpuinfo(u_long *maskp, int *maxidp)
518 errx(1, "not implemented");
521 size = sizeof(maxcpu);
522 mysysctl("kern.smp.maxcpus", &maxcpu, &size, NULL, 0);
523 if (size != sizeof(maxcpu))
524 errx(1, "sysctl kern.smp.maxcpus");
525 size = sizeof(long) * maxcpu * CPUSTATES;
526 times = malloc(size);
528 err(1, "malloc %zd bytes", size);
529 mysysctl("kern.cp_times", times, &size, NULL, 0);
530 maxid = (size / CPUSTATES / sizeof(long)) - 1;
531 for (i = 0; i <= maxid; i++) {
533 for (j = 0; empty && j < CPUSTATES; j++) {
534 if (times[i * CPUSTATES + j] != 0)
551 prthuman(u_int64_t val, int size)
556 if (size < 5 || size > 9)
558 flags = HN_B | HN_NOSPACE | HN_DECIMAL;
559 humanize_number(buf, size, val, "", HN_AUTOSCALE, flags);
560 printf("%*s", size, buf);
563 static int hz, hdrcnt;
565 static long *cur_cp_times;
566 static long *last_cp_times;
567 static size_t size_cp_times;
570 dovmstat(unsigned int interval, int reps)
572 struct vmtotal total;
573 time_t uptime, halfuptime;
574 struct devinfo *tmp_dinfo;
579 uptime = getuptime();
580 halfuptime = uptime / 2;
581 (void)signal(SIGCONT, needhdr);
584 if (namelist[X_STATHZ].n_type != 0 &&
585 namelist[X_STATHZ].n_value != 0)
586 kread(X_STATHZ, &hz, sizeof(hz));
588 kread(X_HZ, &hz, sizeof(hz));
590 struct clockinfo clockrate;
592 size = sizeof(clockrate);
593 mysysctl("kern.clockrate", &clockrate, &size, NULL, 0);
594 if (size != sizeof(clockrate))
595 errx(1, "clockrate size mismatch");
600 ncpus = getcpuinfo(&cpumask, &maxid);
601 size_cp_times = sizeof(long) * (maxid + 1) * CPUSTATES;
602 cur_cp_times = malloc(size_cp_times);
603 last_cp_times = malloc(size_cp_times);
604 bzero(cur_cp_times, size_cp_times);
605 bzero(last_cp_times, size_cp_times);
609 printhdr(ncpus, cpumask);
611 if (kvm_getcptime(kd, cur.cp_time) < 0)
612 errx(1, "kvm_getcptime: %s", kvm_geterr(kd));
614 size = sizeof(cur.cp_time);
615 mysysctl("kern.cp_time", &cur.cp_time, &size, NULL, 0);
616 if (size != sizeof(cur.cp_time))
617 errx(1, "cp_time size mismatch");
620 size = size_cp_times;
621 mysysctl("kern.cp_times", cur_cp_times, &size, NULL, 0);
622 if (size != size_cp_times)
623 errx(1, "cp_times mismatch");
626 tmp_dinfo = last.dinfo;
627 last.dinfo = cur.dinfo;
628 cur.dinfo = tmp_dinfo;
629 last.snap_time = cur.snap_time;
632 * Here what we want to do is refresh our device stats.
633 * getdevs() returns 1 when the device list has changed.
634 * If the device list has changed, we want to go through
635 * the selection process again, in case a device that we
636 * were previously displaying has gone away.
638 switch (devstat_getdevs(NULL, &cur)) {
640 errx(1, "%s", devstat_errbuf);
645 num_devices = cur.dinfo->numdevs;
646 generation = cur.dinfo->generation;
648 retval = devstat_selectdevs(&dev_select, &num_selected,
649 &num_selections, &select_generation,
650 generation, cur.dinfo->devices,
651 num_devices, matches, num_matches,
653 num_devices_specified, select_mode,
657 errx(1, "%s", devstat_errbuf);
660 printhdr(ncpus, cpumask);
671 fill_vmtotal(&total);
672 (void)printf("%2d %1d %1d",
673 total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
674 #define vmstat_pgtok(a) ((a) * (sum.v_page_size >> 10))
675 #define rate(x) (((x) + halfuptime) / uptime) /* round */
678 prthuman(total.t_avm * (u_int64_t)sum.v_page_size, 7);
680 prthuman(total.t_free * (u_int64_t)sum.v_page_size, 6);
683 printf(" %7d ", vmstat_pgtok(total.t_avm));
684 printf(" %6d ", vmstat_pgtok(total.t_free));
686 (void)printf("%5lu ",
687 (unsigned long)rate(sum.v_vm_faults - osum.v_vm_faults));
688 (void)printf("%3lu ",
689 (unsigned long)rate(sum.v_reactivated - osum.v_reactivated));
690 (void)printf("%3lu ",
691 (unsigned long)rate(sum.v_swapin + sum.v_vnodein -
692 (osum.v_swapin + osum.v_vnodein)));
693 (void)printf("%3lu ",
694 (unsigned long)rate(sum.v_swapout + sum.v_vnodeout -
695 (osum.v_swapout + osum.v_vnodeout)));
696 (void)printf("%5lu ",
697 (unsigned long)rate(sum.v_tfree - osum.v_tfree));
698 (void)printf("%3lu ",
699 (unsigned long)rate(sum.v_pdpages - osum.v_pdpages));
701 (void)printf("%4lu %4lu %4lu",
702 (unsigned long)rate(sum.v_intr - osum.v_intr),
703 (unsigned long)rate(sum.v_syscall - osum.v_syscall),
704 (unsigned long)rate(sum.v_swtch - osum.v_swtch));
706 pcpustats(ncpus, cpumask, maxid);
710 (void)fflush(stdout);
711 if (reps >= 0 && --reps <= 0)
716 * We round upward to avoid losing low-frequency events
717 * (i.e., >= 1 per interval but < 1 per second).
720 halfuptime = (uptime + 1) / 2;
723 (void)sleep(interval);
728 printhdr(int ncpus, u_long cpumask)
732 num_shown = (num_selected < maxshowdevs) ? num_selected : maxshowdevs;
733 (void)printf(" procs memory page%*s", 19, "");
735 (void)printf(" disks %*s", num_shown * 4 - 7, "");
736 else if (num_shown == 1)
737 (void)printf("disk");
738 (void)printf(" faults ");
740 for (i = 0; i < ncpus; i++) {
741 if (cpumask & (1ul << i))
742 printf("cpu%-2d ", i);
747 (void)printf(" r b w avm fre flt re pi po fr sr ");
748 for (i = 0; i < num_devices; i++)
749 if ((dev_select[i].selected)
750 && (dev_select[i].selected <= maxshowdevs))
751 (void)printf("%c%c%d ", dev_select[i].device_name[0],
752 dev_select[i].device_name[1],
753 dev_select[i].unit_number);
754 (void)printf(" in sy cs");
756 for (i = 0; i < ncpus; i++)
760 printf(" us sy id\n");
761 hdrcnt = winlines - 2;
765 * Force a header to be prepended to the next output.
768 needhdr(int dummy __unused)
778 unsigned int pgintime, rectime;
780 kread(X_REC, &rectime, sizeof(rectime));
781 kread(X_PGIN, &pgintime, sizeof(pgintime));
782 kread(X_SUM, &sum, sizeof(sum));
783 (void)printf("%u reclaims, %u total time (usec)\n",
784 sum.v_pgrec, rectime);
785 (void)printf("average: %u usec / reclaim\n", rectime / sum.v_pgrec);
787 (void)printf("%u page ins, %u total time (msec)\n",
788 sum.v_pgin, pgintime / 10);
789 (void)printf("average: %8.1f msec / page in\n",
790 pgintime / (sum.v_pgin * 10.0));
795 pct(long top, long bot)
801 ans = (quad_t)top * 100 / bot;
805 #define PCT(top, bot) pct((long)(top), (long)(bot))
810 struct nchstats lnchstats;
814 (void)printf("%9u cpu context switches\n", sum.v_swtch);
815 (void)printf("%9u device interrupts\n", sum.v_intr);
816 (void)printf("%9u software interrupts\n", sum.v_soft);
817 (void)printf("%9u traps\n", sum.v_trap);
818 (void)printf("%9u system calls\n", sum.v_syscall);
819 (void)printf("%9u kernel threads created\n", sum.v_kthreads);
820 (void)printf("%9u fork() calls\n", sum.v_forks);
821 (void)printf("%9u vfork() calls\n", sum.v_vforks);
822 (void)printf("%9u rfork() calls\n", sum.v_rforks);
823 (void)printf("%9u swap pager pageins\n", sum.v_swapin);
824 (void)printf("%9u swap pager pages paged in\n", sum.v_swappgsin);
825 (void)printf("%9u swap pager pageouts\n", sum.v_swapout);
826 (void)printf("%9u swap pager pages paged out\n", sum.v_swappgsout);
827 (void)printf("%9u vnode pager pageins\n", sum.v_vnodein);
828 (void)printf("%9u vnode pager pages paged in\n", sum.v_vnodepgsin);
829 (void)printf("%9u vnode pager pageouts\n", sum.v_vnodeout);
830 (void)printf("%9u vnode pager pages paged out\n", sum.v_vnodepgsout);
831 (void)printf("%9u page daemon wakeups\n", sum.v_pdwakeups);
832 (void)printf("%9u pages examined by the page daemon\n", sum.v_pdpages);
833 (void)printf("%9u pages reactivated\n", sum.v_reactivated);
834 (void)printf("%9u copy-on-write faults\n", sum.v_cow_faults);
835 (void)printf("%9u copy-on-write optimized faults\n", sum.v_cow_optim);
836 (void)printf("%9u zero fill pages zeroed\n", sum.v_zfod);
837 (void)printf("%9u zero fill pages prezeroed\n", sum.v_ozfod);
838 (void)printf("%9u intransit blocking page faults\n", sum.v_intrans);
839 (void)printf("%9u total VM faults taken\n", sum.v_vm_faults);
840 (void)printf("%9u pages affected by kernel thread creation\n", sum.v_kthreadpages);
841 (void)printf("%9u pages affected by fork()\n", sum.v_forkpages);
842 (void)printf("%9u pages affected by vfork()\n", sum.v_vforkpages);
843 (void)printf("%9u pages affected by rfork()\n", sum.v_rforkpages);
844 (void)printf("%9u pages cached\n", sum.v_tcached);
845 (void)printf("%9u pages freed\n", sum.v_tfree);
846 (void)printf("%9u pages freed by daemon\n", sum.v_dfree);
847 (void)printf("%9u pages freed by exiting processes\n", sum.v_pfree);
848 (void)printf("%9u pages active\n", sum.v_active_count);
849 (void)printf("%9u pages inactive\n", sum.v_inactive_count);
850 (void)printf("%9u pages in VM cache\n", sum.v_cache_count);
851 (void)printf("%9u pages wired down\n", sum.v_wire_count);
852 (void)printf("%9u pages free\n", sum.v_free_count);
853 (void)printf("%9u bytes per page\n", sum.v_page_size);
855 kread(X_NCHSTATS, &lnchstats, sizeof(lnchstats));
857 size_t size = sizeof(lnchstats);
858 mysysctl("vfs.cache.nchstats", &lnchstats, &size, NULL, 0);
859 if (size != sizeof(lnchstats))
860 errx(1, "vfs.cache.nchstats size mismatch");
862 nchtotal = lnchstats.ncs_goodhits + lnchstats.ncs_neghits +
863 lnchstats.ncs_badhits + lnchstats.ncs_falsehits +
864 lnchstats.ncs_miss + lnchstats.ncs_long;
865 (void)printf("%9ld total name lookups\n", nchtotal);
867 "%9s cache hits (%ld%% pos + %ld%% neg) system %ld%% per-directory\n",
868 "", PCT(lnchstats.ncs_goodhits, nchtotal),
869 PCT(lnchstats.ncs_neghits, nchtotal),
870 PCT(lnchstats.ncs_pass2, nchtotal));
871 (void)printf("%9s deletions %ld%%, falsehits %ld%%, toolong %ld%%\n", "",
872 PCT(lnchstats.ncs_badhits, nchtotal),
873 PCT(lnchstats.ncs_falsehits, nchtotal),
874 PCT(lnchstats.ncs_long, nchtotal));
881 (void)printf("%u forks, %u pages, average %.2f\n",
882 sum.v_forks, sum.v_forkpages,
883 sum.v_forks == 0 ? 0.0 :
884 (double)sum.v_forkpages / sum.v_forks);
885 (void)printf("%u vforks, %u pages, average %.2f\n",
886 sum.v_vforks, sum.v_vforkpages,
887 sum.v_vforks == 0 ? 0.0 :
888 (double)sum.v_vforkpages / sum.v_vforks);
889 (void)printf("%u rforks, %u pages, average %.2f\n",
890 sum.v_rforks, sum.v_rforkpages,
891 sum.v_rforks == 0 ? 0.0 :
892 (double)sum.v_rforkpages / sum.v_rforks);
899 long double transfers_per_second;
900 long double busy_seconds;
903 for (state = 0; state < CPUSTATES; ++state) {
904 tmp = cur.cp_time[state];
905 cur.cp_time[state] -= last.cp_time[state];
906 last.cp_time[state] = tmp;
909 busy_seconds = cur.snap_time - last.snap_time;
911 for (dn = 0; dn < num_devices; dn++) {
914 if ((dev_select[dn].selected == 0)
915 || (dev_select[dn].selected > maxshowdevs))
918 di = dev_select[dn].position;
920 if (devstat_compute_statistics(&cur.dinfo->devices[di],
921 &last.dinfo->devices[di], busy_seconds,
922 DSM_TRANSFERS_PER_SECOND, &transfers_per_second,
924 errx(1, "%s", devstat_errbuf);
926 (void)printf("%3.0Lf ", transfers_per_second);
931 percent(double pct, int *over)
936 l = snprintf(buf, sizeof(buf), "%.0f", pct);
937 if (l == 1 && *over) {
953 for (state = 0; state < CPUSTATES; ++state)
954 total += cur.cp_time[state];
956 lpct = 100.0 / total;
961 percent((cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * lpct, &over);
963 percent((cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * lpct, &over);
965 percent(cur.cp_time[CP_IDLE] * lpct, &over);
969 pcpustats(int ncpus, u_long cpumask, int maxid)
976 /* devstats does this for cp_time */
977 for (i = 0; i <= maxid; i++) {
978 if ((cpumask & (1ul << i)) == 0)
980 for (state = 0; state < CPUSTATES; ++state) {
981 tmp = cur_cp_times[i * CPUSTATES + state];
982 cur_cp_times[i * CPUSTATES + state] -= last_cp_times[i * CPUSTATES + state];
983 last_cp_times[i * CPUSTATES + state] = tmp;
988 for (i = 0; i <= maxid; i++) {
989 if ((cpumask & (1ul << i)) == 0)
992 for (state = 0; state < CPUSTATES; ++state)
993 total += cur_cp_times[i * CPUSTATES + state];
995 lpct = 100.0 / total;
999 percent((cur_cp_times[i * CPUSTATES + CP_USER] +
1000 cur_cp_times[i * CPUSTATES + CP_NICE]) * lpct, &over);
1002 percent((cur_cp_times[i * CPUSTATES + CP_SYS] +
1003 cur_cp_times[i * CPUSTATES + CP_INTR]) * lpct, &over);
1005 percent(cur_cp_times[i * CPUSTATES + CP_IDLE] * lpct, &over);
1012 unsigned long *intrcnt, uptime;
1014 size_t clen, inamlen, intrcntlen, istrnamlen;
1015 unsigned int i, nintr;
1016 char *intrname, *tintrname;
1018 uptime = getuptime();
1020 intrcntlen = namelist[X_EINTRCNT].n_value -
1021 namelist[X_INTRCNT].n_value;
1022 inamlen = namelist[X_EINTRNAMES].n_value -
1023 namelist[X_INTRNAMES].n_value;
1024 if ((intrcnt = malloc(intrcntlen)) == NULL ||
1025 (intrname = malloc(inamlen)) == NULL)
1027 kread(X_INTRCNT, intrcnt, intrcntlen);
1028 kread(X_INTRNAMES, intrname, inamlen);
1030 for (intrcnt = NULL, intrcntlen = 1024; ; intrcntlen *= 2) {
1031 if ((intrcnt = reallocf(intrcnt, intrcntlen)) == NULL)
1032 err(1, "reallocf()");
1033 if (mysysctl("hw.intrcnt",
1034 intrcnt, &intrcntlen, NULL, 0) == 0)
1037 for (intrname = NULL, inamlen = 1024; ; inamlen *= 2) {
1038 if ((intrname = reallocf(intrname, inamlen)) == NULL)
1039 err(1, "reallocf()");
1040 if (mysysctl("hw.intrnames",
1041 intrname, &inamlen, NULL, 0) == 0)
1045 nintr = intrcntlen / sizeof(unsigned long);
1046 tintrname = intrname;
1047 istrnamlen = strlen("interrupt");
1048 for (i = 0; i < nintr; i++) {
1049 clen = strlen(tintrname);
1050 if (clen > istrnamlen)
1052 tintrname += clen + 1;
1054 (void)printf("%-*s %20s %10s\n", istrnamlen, "interrupt", "total",
1057 for (i = 0; i < nintr; i++) {
1058 if (intrname[0] != '\0' && (*intrcnt != 0 || aflag))
1059 (void)printf("%-*s %20lu %10lu\n", istrnamlen, intrname,
1060 *intrcnt, *intrcnt / uptime);
1061 intrname += strlen(intrname) + 1;
1062 inttotal += *intrcnt++;
1064 (void)printf("%-*s %20llu %10llu\n", istrnamlen, "Total",
1065 (long long)inttotal, (long long)(inttotal / uptime));
1069 domemstat_malloc(void)
1071 struct memory_type_list *mtlp;
1072 struct memory_type *mtp;
1073 int error, first, i;
1075 mtlp = memstat_mtl_alloc();
1077 warn("memstat_mtl_alloc");
1081 if (memstat_sysctl_malloc(mtlp, 0) < 0) {
1082 warnx("memstat_sysctl_malloc: %s",
1083 memstat_strerror(memstat_mtl_geterror(mtlp)));
1087 if (memstat_kvm_malloc(mtlp, kd) < 0) {
1088 error = memstat_mtl_geterror(mtlp);
1089 if (error == MEMSTAT_ERROR_KVM)
1090 warnx("memstat_kvm_malloc: %s",
1093 warnx("memstat_kvm_malloc: %s",
1094 memstat_strerror(error));
1097 printf("%13s %5s %6s %7s %8s Size(s)\n", "Type", "InUse", "MemUse",
1098 "HighUse", "Requests");
1099 for (mtp = memstat_mtl_first(mtlp); mtp != NULL;
1100 mtp = memstat_mtl_next(mtp)) {
1101 if (memstat_get_numallocs(mtp) == 0 &&
1102 memstat_get_count(mtp) == 0)
1104 printf("%13s %5lld %5lldK %7s %8lld ",
1105 memstat_get_name(mtp), memstat_get_count(mtp),
1106 ((int64_t)memstat_get_bytes(mtp) + 1023) / 1024, "-",
1107 memstat_get_numallocs(mtp));
1109 for (i = 0; i < 32; i++) {
1110 if (memstat_get_sizemask(mtp) & (1 << i)) {
1113 printf("%d", 1 << (i + 4));
1119 memstat_mtl_free(mtlp);
1123 domemstat_zone(void)
1125 struct memory_type_list *mtlp;
1126 struct memory_type *mtp;
1127 char name[MEMTYPE_MAXNAME + 1];
1130 mtlp = memstat_mtl_alloc();
1132 warn("memstat_mtl_alloc");
1136 if (memstat_sysctl_uma(mtlp, 0) < 0) {
1137 warnx("memstat_sysctl_uma: %s",
1138 memstat_strerror(memstat_mtl_geterror(mtlp)));
1142 if (memstat_kvm_uma(mtlp, kd) < 0) {
1143 error = memstat_mtl_geterror(mtlp);
1144 if (error == MEMSTAT_ERROR_KVM)
1145 warnx("memstat_kvm_uma: %s",
1148 warnx("memstat_kvm_uma: %s",
1149 memstat_strerror(error));
1152 printf("%-20s %8s %8s %8s %8s %8s %8s\n\n", "ITEM", "SIZE",
1153 "LIMIT", "USED", "FREE", "REQUESTS", "FAILURES");
1154 for (mtp = memstat_mtl_first(mtlp); mtp != NULL;
1155 mtp = memstat_mtl_next(mtp)) {
1156 strlcpy(name, memstat_get_name(mtp), MEMTYPE_MAXNAME);
1158 printf("%-20s %8llu, %8llu, %8llu, %8llu, %8llu, %8llu\n", name,
1159 memstat_get_size(mtp), memstat_get_countlimit(mtp),
1160 memstat_get_count(mtp), memstat_get_free(mtp),
1161 memstat_get_numallocs(mtp), memstat_get_failures(mtp));
1163 memstat_mtl_free(mtlp);
1168 * kread reads something from the kernel, given its nlist index.
1171 kreado(int nlx, void *addr, size_t size, size_t offset)
1175 if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
1176 sym = namelist[nlx].n_name;
1179 errx(1, "symbol %s not defined", sym);
1181 if ((size_t)kvm_read(kd, namelist[nlx].n_value + offset, addr,
1183 sym = namelist[nlx].n_name;
1186 errx(1, "%s: %s", sym, kvm_geterr(kd));
1191 kread(int nlx, void *addr, size_t size)
1193 kreado(nlx, addr, size, 0);
1197 kgetstr(const char *strp)
1199 int n = 0, size = 1;
1203 if (size == n + 1) {
1204 ret = realloc(ret, size);
1206 err(1, "%s: realloc", __func__);
1209 if (kvm_read(kd, (u_long)strp + n, &ret[n], 1) != 1)
1210 errx(1, "%s: %s", __func__, kvm_geterr(kd));
1211 } while (ret[n++] != '\0');
1218 (void)fprintf(stderr, "%s%s",
1219 "usage: vmstat [-afHhimPsz] [-c count] [-M core [-N system]] [-w wait]\n",
1220 " [-n devs] [-p type,if,pass] [disks]\n");