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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1980, 1986, 1991, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)vmstat.c 8.1 (Berkeley) 6/6/93";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
48 #include <sys/namei.h>
49 #include <sys/malloc.h>
50 #include <sys/signal.h>
51 #include <sys/fcntl.h>
52 #include <sys/ioctl.h>
53 #include <sys/resource.h>
54 #include <sys/sysctl.h>
57 #include <sys/vmmeter.h>
60 #include <vm/vm_param.h>
80 static char da[] = "da";
82 static struct nlist namelist[] = {
93 #define X_SINTRNAMES 5
100 #define X_DEFICIT XXX
115 static struct statinfo cur, last;
116 static int num_devices, maxshowdevs;
117 static long generation;
118 static struct device_selection *dev_select;
119 static int num_selected;
120 static struct devstat_match *matches;
121 static int num_matches = 0;
122 static int num_devices_specified, num_selections;
123 static long select_generation;
124 static char **specified_devices;
125 static devstat_select_mode select_mode;
127 static struct vmmeter sum, osum;
129 #define VMSTAT_DEFAULT_LINES 20 /* Default number of `winlines'. */
130 volatile sig_atomic_t wresized; /* Tty resized, when non-zero. */
131 static int winlines = VMSTAT_DEFAULT_LINES; /* Current number of tty rows. */
140 #define FORKSTAT 0x01
141 #define INTRSTAT 0x02
144 #define TIMESTAT 0x10
146 #define ZMEMSTAT 0x40
149 static void cpustats(void);
150 static void pcpustats(int, u_long, int);
151 static void devstats(void);
152 static void doforkst(void);
153 static void dointr(unsigned int, int);
154 static void doobjstat(void);
155 static void dosum(void);
156 static void dovmstat(unsigned int, int);
157 static void domemstat_malloc(void);
158 static void domemstat_zone(void);
159 static void kread(int, void *, size_t);
160 static void kreado(int, void *, size_t, size_t);
161 static char *kgetstr(const char *);
162 static void needhdr(int);
163 static void needresize(int);
164 static void doresize(void);
165 static void printhdr(int, u_long);
166 static void usage(void);
168 static long pct(long, long);
169 static long long getuptime(void);
171 static char **getdrivedata(char **);
174 main(int argc, char *argv[])
177 unsigned int interval;
181 char errbuf[_POSIX2_LINE_MAX];
183 memf = nlistf = NULL;
184 interval = reps = todo = 0;
187 while ((c = getopt(argc, argv, "ac:fhHiM:mN:n:oPp:stw:z")) != -1) {
221 maxshowdevs = atoi(optarg);
223 errx(1, "number of devices %d is < 0",
230 if (devstat_buildmatch(optarg, &matches, &num_matches) != 0)
231 errx(1, "%s", devstat_errbuf);
240 errx(EX_USAGE, "sorry, -t is not (re)implemented yet");
244 /* Convert to milliseconds. */
263 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
265 errx(1, "kvm_openfiles: %s", errbuf);
269 if (kd != NULL && (c = kvm_nlist(kd, namelist)) != 0) {
272 * 'cnt' was renamed to 'vm_cnt'. If 'vm_cnt' is not
273 * found try looking up older 'cnt' symbol.
275 if (namelist[X_SUM].n_type == 0 &&
276 strcmp(namelist[X_SUM].n_name, "_vm_cnt") == 0) {
277 namelist[X_SUM].n_name = "_cnt";
280 warnx("undefined symbols:");
282 c < (int)(sizeof(namelist)/sizeof(namelist[0]));
284 if (namelist[c].n_type == 0)
285 (void)fprintf(stderr, " %s",
287 (void)fputc('\n', stderr);
289 warnx("kvm_nlist: %s", kvm_geterr(kd));
293 errx(1, "Cannot use -P with crash dumps");
297 * Make sure that the userland devstat version matches the
298 * kernel devstat version. If not, exit and print a
299 * message informing the user of his mistake.
301 if (devstat_checkversion(NULL) < 0)
302 errx(1, "%s", devstat_errbuf);
305 argv = getdrivedata(argv);
336 dointr(interval, reps);
338 dovmstat(interval, reps);
343 mysysctl(const char *name, void *oldp, size_t *oldlenp,
344 void *newp, size_t newlen)
348 error = sysctlbyname(name, oldp, oldlenp, newp, newlen);
349 if (error != 0 && errno != ENOMEM)
350 err(1, "sysctl(%s)", name);
355 getdrivedata(char **argv)
357 if ((num_devices = devstat_getnumdevs(NULL)) < 0)
358 errx(1, "%s", devstat_errbuf);
360 cur.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo));
361 last.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo));
363 if (devstat_getdevs(NULL, &cur) == -1)
364 errx(1, "%s", devstat_errbuf);
366 num_devices = cur.dinfo->numdevs;
367 generation = cur.dinfo->generation;
369 specified_devices = (char **)malloc(sizeof(char *));
370 for (num_devices_specified = 0; *argv; ++argv) {
373 num_devices_specified++;
374 specified_devices = (char **)realloc(specified_devices,
376 num_devices_specified);
377 specified_devices[num_devices_specified - 1] = *argv;
381 if (nflag == 0 && maxshowdevs < num_devices_specified)
382 maxshowdevs = num_devices_specified;
385 * People are generally only interested in disk statistics when
386 * they're running vmstat. So, that's what we're going to give
387 * them if they don't specify anything by default. We'll also give
388 * them any other random devices in the system so that we get to
389 * maxshowdevs devices, if that many devices exist. If the user
390 * specifies devices on the command line, either through a pattern
391 * match or by naming them explicitly, we will give the user only
394 if ((num_devices_specified == 0) && (num_matches == 0)) {
395 if (devstat_buildmatch(da, &matches, &num_matches) != 0)
396 errx(1, "%s", devstat_errbuf);
398 select_mode = DS_SELECT_ADD;
400 select_mode = DS_SELECT_ONLY;
403 * At this point, selectdevs will almost surely indicate that the
404 * device list has changed, so we don't look for return values of 0
405 * or 1. If we get back -1, though, there is an error.
407 if (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
408 &select_generation, generation, cur.dinfo->devices,
409 num_devices, matches, num_matches, specified_devices,
410 num_devices_specified, select_mode,
411 maxshowdevs, 0) == -1)
412 errx(1, "%s", devstat_errbuf);
417 /* Return system uptime in nanoseconds */
423 (void)clock_gettime(CLOCK_UPTIME, &sp);
425 return((long long)sp.tv_sec * 1000000000LL + sp.tv_nsec);
429 fill_pcpu(struct pcpu ***pcpup, int* maxcpup)
440 maxcpu = kvm_getmaxcpu(kd);
442 errx(1, "kvm_getmaxcpu: %s", kvm_geterr(kd));
444 pcpu = calloc(maxcpu, sizeof(struct pcpu *));
448 for (i = 0; i < maxcpu; i++) {
449 pcpu[i] = kvm_getpcpu(kd, i);
450 if (pcpu[i] == (struct pcpu *)-1)
451 errx(1, "kvm_getpcpu: %s", kvm_geterr(kd));
459 free_pcpu(struct pcpu **pcpu, int maxcpu)
463 for (i = 0; i < maxcpu; i++)
469 fill_vmmeter(struct vmmeter *vmmp)
475 kread(X_SUM, vmmp, sizeof(*vmmp));
476 fill_pcpu(&pcpu, &maxcpu);
477 for (i = 0; i < maxcpu; i++) {
480 #define ADD_FROM_PCPU(i, name) \
481 vmmp->name += pcpu[i]->pc_cnt.name
482 ADD_FROM_PCPU(i, v_swtch);
483 ADD_FROM_PCPU(i, v_trap);
484 ADD_FROM_PCPU(i, v_syscall);
485 ADD_FROM_PCPU(i, v_intr);
486 ADD_FROM_PCPU(i, v_soft);
487 ADD_FROM_PCPU(i, v_vm_faults);
488 ADD_FROM_PCPU(i, v_io_faults);
489 ADD_FROM_PCPU(i, v_cow_faults);
490 ADD_FROM_PCPU(i, v_cow_optim);
491 ADD_FROM_PCPU(i, v_zfod);
492 ADD_FROM_PCPU(i, v_ozfod);
493 ADD_FROM_PCPU(i, v_swapin);
494 ADD_FROM_PCPU(i, v_swapout);
495 ADD_FROM_PCPU(i, v_swappgsin);
496 ADD_FROM_PCPU(i, v_swappgsout);
497 ADD_FROM_PCPU(i, v_vnodein);
498 ADD_FROM_PCPU(i, v_vnodeout);
499 ADD_FROM_PCPU(i, v_vnodepgsin);
500 ADD_FROM_PCPU(i, v_vnodepgsout);
501 ADD_FROM_PCPU(i, v_intrans);
502 ADD_FROM_PCPU(i, v_tfree);
503 ADD_FROM_PCPU(i, v_forks);
504 ADD_FROM_PCPU(i, v_vforks);
505 ADD_FROM_PCPU(i, v_rforks);
506 ADD_FROM_PCPU(i, v_kthreads);
507 ADD_FROM_PCPU(i, v_forkpages);
508 ADD_FROM_PCPU(i, v_vforkpages);
509 ADD_FROM_PCPU(i, v_rforkpages);
510 ADD_FROM_PCPU(i, v_kthreadpages);
513 free_pcpu(pcpu, maxcpu);
515 size_t size = sizeof(unsigned int);
516 #define GET_VM_STATS(cat, name) \
517 mysysctl("vm.stats." #cat "." #name, &vmmp->name, &size, NULL, 0)
519 GET_VM_STATS(sys, v_swtch);
520 GET_VM_STATS(sys, v_trap);
521 GET_VM_STATS(sys, v_syscall);
522 GET_VM_STATS(sys, v_intr);
523 GET_VM_STATS(sys, v_soft);
526 GET_VM_STATS(vm, v_vm_faults);
527 GET_VM_STATS(vm, v_io_faults);
528 GET_VM_STATS(vm, v_cow_faults);
529 GET_VM_STATS(vm, v_cow_optim);
530 GET_VM_STATS(vm, v_zfod);
531 GET_VM_STATS(vm, v_ozfod);
532 GET_VM_STATS(vm, v_swapin);
533 GET_VM_STATS(vm, v_swapout);
534 GET_VM_STATS(vm, v_swappgsin);
535 GET_VM_STATS(vm, v_swappgsout);
536 GET_VM_STATS(vm, v_vnodein);
537 GET_VM_STATS(vm, v_vnodeout);
538 GET_VM_STATS(vm, v_vnodepgsin);
539 GET_VM_STATS(vm, v_vnodepgsout);
540 GET_VM_STATS(vm, v_intrans);
541 GET_VM_STATS(vm, v_reactivated);
542 GET_VM_STATS(vm, v_pdwakeups);
543 GET_VM_STATS(vm, v_pdpages);
544 GET_VM_STATS(vm, v_tcached);
545 GET_VM_STATS(vm, v_dfree);
546 GET_VM_STATS(vm, v_pfree);
547 GET_VM_STATS(vm, v_tfree);
548 GET_VM_STATS(vm, v_page_size);
549 GET_VM_STATS(vm, v_page_count);
550 GET_VM_STATS(vm, v_free_reserved);
551 GET_VM_STATS(vm, v_free_target);
552 GET_VM_STATS(vm, v_free_min);
553 GET_VM_STATS(vm, v_free_count);
554 GET_VM_STATS(vm, v_wire_count);
555 GET_VM_STATS(vm, v_active_count);
556 GET_VM_STATS(vm, v_inactive_target);
557 GET_VM_STATS(vm, v_inactive_count);
558 GET_VM_STATS(vm, v_cache_count);
559 GET_VM_STATS(vm, v_pageout_free_min);
560 GET_VM_STATS(vm, v_interrupt_free_min);
561 /*GET_VM_STATS(vm, v_free_severe);*/
562 GET_VM_STATS(vm, v_forks);
563 GET_VM_STATS(vm, v_vforks);
564 GET_VM_STATS(vm, v_rforks);
565 GET_VM_STATS(vm, v_kthreads);
566 GET_VM_STATS(vm, v_forkpages);
567 GET_VM_STATS(vm, v_vforkpages);
568 GET_VM_STATS(vm, v_rforkpages);
569 GET_VM_STATS(vm, v_kthreadpages);
575 fill_vmtotal(struct vmtotal *vmtp)
579 errx(1, "not implemented");
581 size_t size = sizeof(*vmtp);
582 mysysctl("vm.vmtotal", vmtp, &size, NULL, 0);
583 if (size != sizeof(*vmtp))
584 errx(1, "vm.total size mismatch");
588 /* Determine how many cpu columns, and what index they are in kern.cp_times */
590 getcpuinfo(u_long *maskp, int *maxidp)
602 errx(1, "not implemented");
605 size = sizeof(maxcpu);
606 mysysctl("kern.smp.maxcpus", &maxcpu, &size, NULL, 0);
607 if (size != sizeof(maxcpu))
608 errx(1, "sysctl kern.smp.maxcpus");
609 size = sizeof(long) * maxcpu * CPUSTATES;
610 times = malloc(size);
612 err(1, "malloc %zd bytes", size);
613 mysysctl("kern.cp_times", times, &size, NULL, 0);
614 maxid = (size / CPUSTATES / sizeof(long)) - 1;
615 for (i = 0; i <= maxid; i++) {
617 for (j = 0; empty && j < CPUSTATES; j++) {
618 if (times[i * CPUSTATES + j] != 0)
635 prthuman(u_int64_t val, int size)
640 if (size < 5 || size > 9)
642 flags = HN_B | HN_NOSPACE | HN_DECIMAL;
643 humanize_number(buf, size, val, "", HN_AUTOSCALE, flags);
644 printf("%*s", size, buf);
647 static int hz, hdrcnt;
649 static long *cur_cp_times;
650 static long *last_cp_times;
651 static size_t size_cp_times;
654 dovmstat(unsigned int interval, int reps)
656 struct vmtotal total;
657 time_t uptime, halfuptime;
658 struct devinfo *tmp_dinfo;
664 uptime = getuptime() / 1000000000LL;
665 halfuptime = uptime / 2;
671 * If the user stops the program (control-Z) and then resumes it,
672 * print out the header again.
674 (void)signal(SIGCONT, needhdr);
677 * If our standard output is a tty, then install a SIGWINCH handler
678 * and set wresized so that our first iteration through the main
679 * vmstat loop will peek at the terminal's current rows to find out
680 * how many lines can fit in a screenful of output.
682 if (isatty(fileno(stdout)) != 0) {
684 (void)signal(SIGWINCH, needresize);
687 winlines = VMSTAT_DEFAULT_LINES;
691 if (namelist[X_STATHZ].n_type != 0 &&
692 namelist[X_STATHZ].n_value != 0)
693 kread(X_STATHZ, &hz, sizeof(hz));
695 kread(X_HZ, &hz, sizeof(hz));
697 struct clockinfo clockrate;
699 size = sizeof(clockrate);
700 mysysctl("kern.clockrate", &clockrate, &size, NULL, 0);
701 if (size != sizeof(clockrate))
702 errx(1, "clockrate size mismatch");
707 ncpus = getcpuinfo(&cpumask, &maxid);
708 size_cp_times = sizeof(long) * (maxid + 1) * CPUSTATES;
709 cur_cp_times = calloc(1, size_cp_times);
710 last_cp_times = calloc(1, size_cp_times);
714 printhdr(maxid, cpumask);
716 if (kvm_getcptime(kd, cur.cp_time) < 0)
717 errx(1, "kvm_getcptime: %s", kvm_geterr(kd));
719 size = sizeof(cur.cp_time);
720 mysysctl("kern.cp_time", &cur.cp_time, &size, NULL, 0);
721 if (size != sizeof(cur.cp_time))
722 errx(1, "cp_time size mismatch");
725 size = size_cp_times;
726 mysysctl("kern.cp_times", cur_cp_times, &size, NULL, 0);
727 if (size != size_cp_times)
728 errx(1, "cp_times mismatch");
731 tmp_dinfo = last.dinfo;
732 last.dinfo = cur.dinfo;
733 cur.dinfo = tmp_dinfo;
734 last.snap_time = cur.snap_time;
737 * Here what we want to do is refresh our device stats.
738 * getdevs() returns 1 when the device list has changed.
739 * If the device list has changed, we want to go through
740 * the selection process again, in case a device that we
741 * were previously displaying has gone away.
743 switch (devstat_getdevs(NULL, &cur)) {
745 errx(1, "%s", devstat_errbuf);
750 num_devices = cur.dinfo->numdevs;
751 generation = cur.dinfo->generation;
753 retval = devstat_selectdevs(&dev_select, &num_selected,
754 &num_selections, &select_generation,
755 generation, cur.dinfo->devices,
756 num_devices, matches, num_matches,
758 num_devices_specified, select_mode,
762 errx(1, "%s", devstat_errbuf);
765 printhdr(maxid, cpumask);
776 fill_vmtotal(&total);
777 (void)printf("%1d %1d %1d",
778 total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
779 #define vmstat_pgtok(a) ((a) * (sum.v_page_size >> 10))
780 #define rate(x) (((x) * rate_adj + halfuptime) / uptime) /* round */
783 prthuman(total.t_avm * (u_int64_t)sum.v_page_size, 5);
785 prthuman(total.t_free * (u_int64_t)sum.v_page_size, 5);
787 (void)printf("%5lu ",
788 (unsigned long)rate(sum.v_vm_faults -
791 printf(" %7d", vmstat_pgtok(total.t_avm));
792 printf(" %7d ", vmstat_pgtok(total.t_free));
793 (void)printf("%4lu ",
794 (unsigned long)rate(sum.v_vm_faults -
797 (void)printf("%3lu ",
798 (unsigned long)rate(sum.v_reactivated - osum.v_reactivated));
799 (void)printf("%3lu ",
800 (unsigned long)rate(sum.v_swapin + sum.v_vnodein -
801 (osum.v_swapin + osum.v_vnodein)));
802 (void)printf("%3lu ",
803 (unsigned long)rate(sum.v_swapout + sum.v_vnodeout -
804 (osum.v_swapout + osum.v_vnodeout)));
805 (void)printf("%5lu ",
806 (unsigned long)rate(sum.v_tfree - osum.v_tfree));
807 (void)printf("%4lu ",
808 (unsigned long)rate(sum.v_pdpages - osum.v_pdpages));
810 (void)printf("%4lu %5lu %5lu",
811 (unsigned long)rate(sum.v_intr - osum.v_intr),
812 (unsigned long)rate(sum.v_syscall - osum.v_syscall),
813 (unsigned long)rate(sum.v_swtch - osum.v_swtch));
815 pcpustats(ncpus, cpumask, maxid);
819 (void)fflush(stdout);
820 if (reps >= 0 && --reps <= 0)
826 * We round upward to avoid losing low-frequency events
827 * (i.e., >= 1 per interval but < 1 per millisecond).
830 halfuptime = (uptime + 1) / 2;
833 (void)usleep(interval * 1000);
838 printhdr(int maxid, u_long cpumask)
842 num_shown = (num_selected < maxshowdevs) ? num_selected : maxshowdevs;
844 (void)printf("procs memory page%*s ", 19, "");
846 (void)printf("procs memory page%*s ", 19, "");
849 (void)printf(" disks %*s", num_shown * 4 - 7, "");
850 else if (num_shown == 1)
851 (void)printf(" disk");
852 (void)printf(" faults ");
854 for (i = 0; i <= maxid; i++) {
855 if (cpumask & (1ul << i))
856 printf(" cpu%d ", i);
862 (void)printf("r b w avm fre flt re pi po fr sr ");
864 (void)printf("r b w avm fre flt re pi po fr sr ");
866 for (i = 0; i < num_devices; i++)
867 if ((dev_select[i].selected)
868 && (dev_select[i].selected <= maxshowdevs))
869 (void)printf("%c%c%d ", dev_select[i].device_name[0],
870 dev_select[i].device_name[1],
871 dev_select[i].unit_number);
872 (void)printf(" in sy cs");
874 for (i = 0; i <= maxid; i++) {
875 if (cpumask & (1ul << i))
880 printf(" us sy id\n");
887 * Force a header to be prepended to the next output.
890 needhdr(int dummy __unused)
897 * When the terminal is resized, force an update of the maximum number of rows
898 * printed between each header repetition. Then force a new header to be
899 * prepended to the next output.
902 needresize(int signo)
910 * Update the global `winlines' count of terminal rows.
919 status = ioctl(fileno(stdout), TIOCGWINSZ, &w);
920 if (status == -1 && errno == EINTR)
922 else if (status == -1)
925 winlines = w.ws_row - 3;
927 winlines = VMSTAT_DEFAULT_LINES;
932 * Inhibit doresize() calls until we are rescheduled by SIGWINCH.
941 unsigned int pgintime, rectime;
943 kread(X_REC, &rectime, sizeof(rectime));
944 kread(X_PGIN, &pgintime, sizeof(pgintime));
945 kread(X_SUM, &sum, sizeof(sum));
946 (void)printf("%u reclaims, %u total time (usec)\n",
947 sum.v_pgrec, rectime);
948 (void)printf("average: %u usec / reclaim\n", rectime / sum.v_pgrec);
950 (void)printf("%u page ins, %u total time (msec)\n",
951 sum.v_pgin, pgintime / 10);
952 (void)printf("average: %8.1f msec / page in\n",
953 pgintime / (sum.v_pgin * 10.0));
958 pct(long top, long bot)
964 ans = (quad_t)top * 100 / bot;
968 #define PCT(top, bot) pct((long)(top), (long)(bot))
973 struct nchstats lnchstats;
977 (void)printf("%9u cpu context switches\n", sum.v_swtch);
978 (void)printf("%9u device interrupts\n", sum.v_intr);
979 (void)printf("%9u software interrupts\n", sum.v_soft);
980 (void)printf("%9u traps\n", sum.v_trap);
981 (void)printf("%9u system calls\n", sum.v_syscall);
982 (void)printf("%9u kernel threads created\n", sum.v_kthreads);
983 (void)printf("%9u fork() calls\n", sum.v_forks);
984 (void)printf("%9u vfork() calls\n", sum.v_vforks);
985 (void)printf("%9u rfork() calls\n", sum.v_rforks);
986 (void)printf("%9u swap pager pageins\n", sum.v_swapin);
987 (void)printf("%9u swap pager pages paged in\n", sum.v_swappgsin);
988 (void)printf("%9u swap pager pageouts\n", sum.v_swapout);
989 (void)printf("%9u swap pager pages paged out\n", sum.v_swappgsout);
990 (void)printf("%9u vnode pager pageins\n", sum.v_vnodein);
991 (void)printf("%9u vnode pager pages paged in\n", sum.v_vnodepgsin);
992 (void)printf("%9u vnode pager pageouts\n", sum.v_vnodeout);
993 (void)printf("%9u vnode pager pages paged out\n", sum.v_vnodepgsout);
994 (void)printf("%9u page daemon wakeups\n", sum.v_pdwakeups);
995 (void)printf("%9u pages examined by the page daemon\n", sum.v_pdpages);
996 (void)printf("%9u pages reactivated\n", sum.v_reactivated);
997 (void)printf("%9u copy-on-write faults\n", sum.v_cow_faults);
998 (void)printf("%9u copy-on-write optimized faults\n", sum.v_cow_optim);
999 (void)printf("%9u zero fill pages zeroed\n", sum.v_zfod);
1000 (void)printf("%9u zero fill pages prezeroed\n", sum.v_ozfod);
1001 (void)printf("%9u intransit blocking page faults\n", sum.v_intrans);
1002 (void)printf("%9u total VM faults taken\n", sum.v_vm_faults);
1003 (void)printf("%9u page faults requiring I/O\n", sum.v_io_faults);
1004 (void)printf("%9u pages affected by kernel thread creation\n",
1005 sum.v_kthreadpages);
1006 (void)printf("%9u pages affected by fork()\n", sum.v_forkpages);
1007 (void)printf("%9u pages affected by vfork()\n", sum.v_vforkpages);
1008 (void)printf("%9u pages affected by rfork()\n", sum.v_rforkpages);
1009 (void)printf("%9u pages cached\n", sum.v_tcached);
1010 (void)printf("%9u pages freed\n", sum.v_tfree);
1011 (void)printf("%9u pages freed by daemon\n", sum.v_dfree);
1012 (void)printf("%9u pages freed by exiting processes\n", sum.v_pfree);
1013 (void)printf("%9u pages active\n", sum.v_active_count);
1014 (void)printf("%9u pages inactive\n", sum.v_inactive_count);
1015 (void)printf("%9u pages in VM cache\n", sum.v_cache_count);
1016 (void)printf("%9u pages wired down\n", sum.v_wire_count);
1017 (void)printf("%9u pages free\n", sum.v_free_count);
1018 (void)printf("%9u bytes per page\n", sum.v_page_size);
1020 kread(X_NCHSTATS, &lnchstats, sizeof(lnchstats));
1022 size_t size = sizeof(lnchstats);
1023 mysysctl("vfs.cache.nchstats", &lnchstats, &size, NULL, 0);
1024 if (size != sizeof(lnchstats))
1025 errx(1, "vfs.cache.nchstats size mismatch");
1027 nchtotal = lnchstats.ncs_goodhits + lnchstats.ncs_neghits +
1028 lnchstats.ncs_badhits + lnchstats.ncs_falsehits +
1029 lnchstats.ncs_miss + lnchstats.ncs_long;
1030 (void)printf("%9ld total name lookups\n", nchtotal);
1032 "%9s cache hits (%ld%% pos + %ld%% neg) system %ld%% per-directory\n",
1033 "", PCT(lnchstats.ncs_goodhits, nchtotal),
1034 PCT(lnchstats.ncs_neghits, nchtotal),
1035 PCT(lnchstats.ncs_pass2, nchtotal));
1036 (void)printf("%9s deletions %ld%%, falsehits %ld%%, toolong %ld%%\n", "",
1037 PCT(lnchstats.ncs_badhits, nchtotal),
1038 PCT(lnchstats.ncs_falsehits, nchtotal),
1039 PCT(lnchstats.ncs_long, nchtotal));
1046 (void)printf("%u forks, %u pages, average %.2f\n",
1047 sum.v_forks, sum.v_forkpages,
1048 sum.v_forks == 0 ? 0.0 :
1049 (double)sum.v_forkpages / sum.v_forks);
1050 (void)printf("%u vforks, %u pages, average %.2f\n",
1051 sum.v_vforks, sum.v_vforkpages,
1052 sum.v_vforks == 0 ? 0.0 :
1053 (double)sum.v_vforkpages / sum.v_vforks);
1054 (void)printf("%u rforks, %u pages, average %.2f\n",
1055 sum.v_rforks, sum.v_rforkpages,
1056 sum.v_rforks == 0 ? 0.0 :
1057 (double)sum.v_rforkpages / sum.v_rforks);
1064 long double transfers_per_second;
1065 long double busy_seconds;
1068 for (state = 0; state < CPUSTATES; ++state) {
1069 tmp = cur.cp_time[state];
1070 cur.cp_time[state] -= last.cp_time[state];
1071 last.cp_time[state] = tmp;
1074 busy_seconds = cur.snap_time - last.snap_time;
1076 for (dn = 0; dn < num_devices; dn++) {
1079 if ((dev_select[dn].selected == 0)
1080 || (dev_select[dn].selected > maxshowdevs))
1083 di = dev_select[dn].position;
1085 if (devstat_compute_statistics(&cur.dinfo->devices[di],
1086 &last.dinfo->devices[di], busy_seconds,
1087 DSM_TRANSFERS_PER_SECOND, &transfers_per_second,
1089 errx(1, "%s", devstat_errbuf);
1091 (void)printf("%3.0Lf ", transfers_per_second);
1096 percent(double pct, int *over)
1101 l = snprintf(buf, sizeof(buf), "%.0f", pct);
1102 if (l == 1 && *over) {
1118 for (state = 0; state < CPUSTATES; ++state)
1119 total += cur.cp_time[state];
1121 lpct = 100.0 / total;
1126 percent((cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * lpct, &over);
1128 percent((cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * lpct, &over);
1130 percent(cur.cp_time[CP_IDLE] * lpct, &over);
1134 pcpustats(int ncpus, u_long cpumask, int maxid)
1141 /* devstats does this for cp_time */
1142 for (i = 0; i <= maxid; i++) {
1143 if ((cpumask & (1ul << i)) == 0)
1145 for (state = 0; state < CPUSTATES; ++state) {
1146 tmp = cur_cp_times[i * CPUSTATES + state];
1147 cur_cp_times[i * CPUSTATES + state] -= last_cp_times[i *
1149 last_cp_times[i * CPUSTATES + state] = tmp;
1154 for (i = 0; i <= maxid; i++) {
1155 if ((cpumask & (1ul << i)) == 0)
1158 for (state = 0; state < CPUSTATES; ++state)
1159 total += cur_cp_times[i * CPUSTATES + state];
1161 lpct = 100.0 / total;
1165 percent((cur_cp_times[i * CPUSTATES + CP_USER] +
1166 cur_cp_times[i * CPUSTATES + CP_NICE]) * lpct, &over);
1168 percent((cur_cp_times[i * CPUSTATES + CP_SYS] +
1169 cur_cp_times[i * CPUSTATES + CP_INTR]) * lpct, &over);
1171 percent(cur_cp_times[i * CPUSTATES + CP_IDLE] * lpct, &over);
1176 read_intrcnts(unsigned long **intrcnts)
1181 kread(X_SINTRCNT, &intrcntlen, sizeof(intrcntlen));
1182 if ((*intrcnts = malloc(intrcntlen)) == NULL)
1184 kread(X_INTRCNT, *intrcnts, intrcntlen);
1186 for (*intrcnts = NULL, intrcntlen = 1024; ; intrcntlen *= 2) {
1187 *intrcnts = reallocf(*intrcnts, intrcntlen);
1188 if (*intrcnts == NULL)
1189 err(1, "reallocf()");
1190 if (mysysctl("hw.intrcnt",
1191 *intrcnts, &intrcntlen, NULL, 0) == 0)
1196 return (intrcntlen / sizeof(unsigned long));
1200 print_intrcnts(unsigned long *intrcnts, unsigned long *old_intrcnts,
1201 char *intrnames, unsigned int nintr,
1202 size_t istrnamlen, long long period_ms)
1204 unsigned long *intrcnt, *old_intrcnt;
1205 uint64_t inttotal, old_inttotal, total_count, total_rate;
1211 intrname = intrnames;
1212 for (i = 0, intrcnt=intrcnts, old_intrcnt=old_intrcnts; i < nintr; i++) {
1213 if (intrname[0] != '\0' && (*intrcnt != 0 || aflag)) {
1214 unsigned long count, rate;
1216 count = *intrcnt - *old_intrcnt;
1217 rate = (count * 1000 + period_ms / 2) / period_ms;
1218 (void)printf("%-*s %20lu %10lu\n", (int)istrnamlen,
1219 intrname, count, rate);
1221 intrname += strlen(intrname) + 1;
1222 inttotal += *intrcnt++;
1223 old_inttotal += *old_intrcnt++;
1225 total_count = inttotal - old_inttotal;
1226 total_rate = (total_count * 1000 + period_ms / 2) / period_ms;
1227 (void)printf("%-*s %20" PRIu64 " %10" PRIu64 "\n", (int)istrnamlen,
1228 "Total", total_count, total_rate);
1232 dointr(unsigned int interval, int reps)
1234 unsigned long *intrcnts;
1235 long long uptime, period_ms;
1236 unsigned long *old_intrcnts = NULL;
1237 size_t clen, inamlen, istrnamlen;
1238 char *intrnames, *intrname;
1240 uptime = getuptime();
1242 /* Get the names of each interrupt source */
1244 kread(X_SINTRNAMES, &inamlen, sizeof(inamlen));
1245 if ((intrnames = malloc(inamlen)) == NULL)
1247 kread(X_INTRNAMES, intrnames, inamlen);
1249 for (intrnames = NULL, inamlen = 1024; ; inamlen *= 2) {
1250 if ((intrnames = reallocf(intrnames, inamlen)) == NULL)
1251 err(1, "reallocf()");
1252 if (mysysctl("hw.intrnames",
1253 intrnames, &inamlen, NULL, 0) == 0)
1258 /* Determine the length of the longest interrupt name */
1259 intrname = intrnames;
1260 istrnamlen = strlen("interrupt");
1261 while(*intrname != '\0') {
1262 clen = strlen(intrname);
1263 if (clen > istrnamlen)
1265 intrname += strlen(intrname) + 1;
1267 (void)printf("%-*s %20s %10s\n", (int)istrnamlen, "interrupt", "total",
1271 * Loop reps times printing differential interrupt counts. If reps is
1272 * zero, then run just once, printing total counts
1274 period_ms = uptime / 1000000;
1277 long long old_uptime;
1279 nintr = read_intrcnts(&intrcnts);
1281 * Initialize old_intrcnts to 0 for the first pass, so
1282 * print_intrcnts will print total interrupts since boot
1284 if (old_intrcnts == NULL) {
1285 old_intrcnts = calloc(nintr, sizeof(unsigned long));
1286 if (old_intrcnts == NULL)
1290 print_intrcnts(intrcnts, old_intrcnts, intrnames, nintr,
1291 istrnamlen, period_ms);
1294 old_intrcnts = intrcnts;
1295 if (reps >= 0 && --reps <= 0)
1297 usleep(interval * 1000);
1298 old_uptime = uptime;
1299 uptime = getuptime();
1300 period_ms = (uptime - old_uptime) / 1000000;
1305 domemstat_malloc(void)
1307 struct memory_type_list *mtlp;
1308 struct memory_type *mtp;
1309 int error, first, i;
1311 mtlp = memstat_mtl_alloc();
1313 warn("memstat_mtl_alloc");
1317 if (memstat_sysctl_malloc(mtlp, 0) < 0) {
1318 warnx("memstat_sysctl_malloc: %s",
1319 memstat_strerror(memstat_mtl_geterror(mtlp)));
1323 if (memstat_kvm_malloc(mtlp, kd) < 0) {
1324 error = memstat_mtl_geterror(mtlp);
1325 if (error == MEMSTAT_ERROR_KVM)
1326 warnx("memstat_kvm_malloc: %s",
1329 warnx("memstat_kvm_malloc: %s",
1330 memstat_strerror(error));
1333 printf("%13s %5s %6s %7s %8s Size(s)\n", "Type", "InUse", "MemUse",
1334 "HighUse", "Requests");
1335 for (mtp = memstat_mtl_first(mtlp); mtp != NULL;
1336 mtp = memstat_mtl_next(mtp)) {
1337 if (memstat_get_numallocs(mtp) == 0 &&
1338 memstat_get_count(mtp) == 0)
1340 printf("%13s %5" PRIu64 " %5" PRIu64 "K %7s %8" PRIu64 " ",
1341 memstat_get_name(mtp), memstat_get_count(mtp),
1342 (memstat_get_bytes(mtp) + 1023) / 1024, "-",
1343 memstat_get_numallocs(mtp));
1345 for (i = 0; i < 32; i++) {
1346 if (memstat_get_sizemask(mtp) & (1 << i)) {
1349 printf("%d", 1 << (i + 4));
1355 memstat_mtl_free(mtlp);
1359 domemstat_zone(void)
1361 struct memory_type_list *mtlp;
1362 struct memory_type *mtp;
1363 char name[MEMTYPE_MAXNAME + 1];
1366 mtlp = memstat_mtl_alloc();
1368 warn("memstat_mtl_alloc");
1372 if (memstat_sysctl_uma(mtlp, 0) < 0) {
1373 warnx("memstat_sysctl_uma: %s",
1374 memstat_strerror(memstat_mtl_geterror(mtlp)));
1378 if (memstat_kvm_uma(mtlp, kd) < 0) {
1379 error = memstat_mtl_geterror(mtlp);
1380 if (error == MEMSTAT_ERROR_KVM)
1381 warnx("memstat_kvm_uma: %s",
1384 warnx("memstat_kvm_uma: %s",
1385 memstat_strerror(error));
1388 printf("%-20s %6s %6s %8s %8s %8s %4s %4s\n\n", "ITEM", "SIZE",
1389 "LIMIT", "USED", "FREE", "REQ", "FAIL", "SLEEP");
1390 for (mtp = memstat_mtl_first(mtlp); mtp != NULL;
1391 mtp = memstat_mtl_next(mtp)) {
1392 strlcpy(name, memstat_get_name(mtp), MEMTYPE_MAXNAME);
1394 printf("%-20s %6" PRIu64 ", %6" PRIu64 ",%8" PRIu64 ",%8" PRIu64
1395 ",%8" PRIu64 ",%4" PRIu64 ",%4" PRIu64 "\n", name,
1396 memstat_get_size(mtp), memstat_get_countlimit(mtp),
1397 memstat_get_count(mtp), memstat_get_free(mtp),
1398 memstat_get_numallocs(mtp), memstat_get_failures(mtp),
1399 memstat_get_sleeps(mtp));
1401 memstat_mtl_free(mtlp);
1406 display_object(struct kinfo_vmobject *kvo)
1410 printf("%5jd ", (uintmax_t)kvo->kvo_resident);
1411 printf("%5jd ", (uintmax_t)kvo->kvo_active);
1412 printf("%5jd ", (uintmax_t)kvo->kvo_inactive);
1413 printf("%3d ", kvo->kvo_ref_count);
1414 printf("%3d ", kvo->kvo_shadow_count);
1415 switch (kvo->kvo_memattr) {
1416 #ifdef VM_MEMATTR_UNCACHEABLE
1417 case VM_MEMATTR_UNCACHEABLE:
1421 #ifdef VM_MEMATTR_WRITE_COMBINING
1422 case VM_MEMATTR_WRITE_COMBINING:
1426 #ifdef VM_MEMATTR_WRITE_THROUGH
1427 case VM_MEMATTR_WRITE_THROUGH:
1431 #ifdef VM_MEMATTR_WRITE_PROTECTED
1432 case VM_MEMATTR_WRITE_PROTECTED:
1436 #ifdef VM_MEMATTR_WRITE_BACK
1437 case VM_MEMATTR_WRITE_BACK:
1441 #ifdef VM_MEMATTR_WEAK_UNCACHEABLE
1442 case VM_MEMATTR_WEAK_UNCACHEABLE:
1446 #ifdef VM_MEMATTR_WB_WA
1447 case VM_MEMATTR_WB_WA:
1451 #ifdef VM_MEMATTR_NOCACHE
1452 case VM_MEMATTR_NOCACHE:
1456 #ifdef VM_MEMATTR_DEVICE
1457 case VM_MEMATTR_DEVICE:
1461 #ifdef VM_MEMATTR_CACHEABLE
1462 case VM_MEMATTR_CACHEABLE:
1466 #ifdef VM_MEMATTR_PREFETCHABLE
1467 case VM_MEMATTR_PREFETCHABLE:
1475 printf("%-3s ", str);
1476 switch (kvo->kvo_type) {
1477 case KVME_TYPE_NONE:
1480 case KVME_TYPE_DEFAULT:
1483 case KVME_TYPE_VNODE:
1486 case KVME_TYPE_SWAP:
1489 case KVME_TYPE_DEVICE:
1492 case KVME_TYPE_PHYS:
1495 case KVME_TYPE_DEAD:
1501 case KVME_TYPE_UNKNOWN:
1506 printf("%-2s ", str);
1507 printf("%-s\n", kvo->kvo_path);
1513 struct kinfo_vmobject *kvo;
1516 kvo = kinfo_getvmobject(&cnt);
1518 warn("Failed to fetch VM object list");
1521 printf("%5s %5s %5s %3s %3s %3s %2s %s\n", "RES", "ACT", "INACT",
1522 "REF", "SHD", "CM", "TP", "PATH");
1523 for (i = 0; i < cnt; i++)
1524 display_object(&kvo[i]);
1529 * kread reads something from the kernel, given its nlist index.
1532 kreado(int nlx, void *addr, size_t size, size_t offset)
1536 if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
1537 sym = namelist[nlx].n_name;
1540 errx(1, "symbol %s not defined", sym);
1542 if ((size_t)kvm_read(kd, namelist[nlx].n_value + offset, addr,
1544 sym = namelist[nlx].n_name;
1547 errx(1, "%s: %s", sym, kvm_geterr(kd));
1552 kread(int nlx, void *addr, size_t size)
1554 kreado(nlx, addr, size, 0);
1558 kgetstr(const char *strp)
1560 int n = 0, size = 1;
1564 if (size == n + 1) {
1565 ret = realloc(ret, size);
1567 err(1, "%s: realloc", __func__);
1570 if (kvm_read(kd, (u_long)strp + n, &ret[n], 1) != 1)
1571 errx(1, "%s: %s", __func__, kvm_geterr(kd));
1572 } while (ret[n++] != '\0');
1579 (void)fprintf(stderr, "%s%s",
1580 "usage: vmstat [-afHhimoPsz] [-M core [-N system]] [-c count] [-n devs]\n",
1581 " [-p type,if,pass] [-w wait] [disks] [wait [count]]\n");