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>
56 #include <sys/vmmeter.h>
59 #include <vm/vm_param.h>
79 static char da[] = "da";
81 static struct nlist namelist[] = {
92 #define X_SINTRNAMES 5
114 static struct statinfo cur, last;
115 static int num_devices, maxshowdevs;
116 static long generation;
117 static struct device_selection *dev_select;
118 static int num_selected;
119 static struct devstat_match *matches;
120 static int num_matches = 0;
121 static int num_devices_specified, num_selections;
122 static long select_generation;
123 static char **specified_devices;
124 static devstat_select_mode select_mode;
126 static struct vmmeter sum, osum;
128 #define VMSTAT_DEFAULT_LINES 20 /* Default number of `winlines'. */
129 volatile sig_atomic_t wresized; /* Tty resized, when non-zero. */
130 static int winlines = VMSTAT_DEFAULT_LINES; /* Current number of tty rows. */
139 #define FORKSTAT 0x01
140 #define INTRSTAT 0x02
143 #define TIMESTAT 0x10
145 #define ZMEMSTAT 0x40
147 static void cpustats(void);
148 static void pcpustats(int, u_long, int);
149 static void devstats(void);
150 static void doforkst(void);
151 static void dointr(void);
152 static void dosum(void);
153 static void dovmstat(unsigned int, int);
154 static void domemstat_malloc(void);
155 static void domemstat_zone(void);
156 static void kread(int, void *, size_t);
157 static void kreado(int, void *, size_t, size_t);
158 static char *kgetstr(const char *);
159 static void needhdr(int);
160 static void needresize(int);
161 static void doresize(void);
162 static void printhdr(int, u_long);
163 static void usage(void);
165 static long pct(long, long);
166 static long getuptime(void);
168 static char **getdrivedata(char **);
171 main(int argc, char *argv[])
174 unsigned int interval;
178 char errbuf[_POSIX2_LINE_MAX];
180 memf = nlistf = NULL;
181 interval = reps = todo = 0;
184 while ((c = getopt(argc, argv, "ac:fhHiM:mN:n:Pp:stw:z")) != -1) {
218 maxshowdevs = atoi(optarg);
220 errx(1, "number of devices %d is < 0",
224 if (devstat_buildmatch(optarg, &matches, &num_matches) != 0)
225 errx(1, "%s", devstat_errbuf);
234 errx(EX_USAGE, "sorry, -t is not (re)implemented yet");
238 /* Convert to milliseconds. */
257 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
259 errx(1, "kvm_openfiles: %s", errbuf);
263 if (kd != NULL && (c = kvm_nlist(kd, namelist)) != 0) {
266 * 'cnt' was renamed to 'vm_cnt'. If 'vm_cnt' is not
267 * found try looking up older 'cnt' symbol.
269 if (namelist[X_SUM].n_type == 0 &&
270 strcmp(namelist[X_SUM].n_name, "_vm_cnt") == 0) {
271 namelist[X_SUM].n_name = "_cnt";
274 warnx("undefined symbols:");
276 c < (int)(sizeof(namelist)/sizeof(namelist[0]));
278 if (namelist[c].n_type == 0)
279 (void)fprintf(stderr, " %s",
281 (void)fputc('\n', stderr);
283 warnx("kvm_nlist: %s", kvm_geterr(kd));
287 errx(1, "Cannot use -P with crash dumps");
291 * Make sure that the userland devstat version matches the
292 * kernel devstat version. If not, exit and print a
293 * message informing the user of his mistake.
295 if (devstat_checkversion(NULL) < 0)
296 errx(1, "%s", devstat_errbuf);
299 argv = getdrivedata(argv);
330 dovmstat(interval, reps);
335 mysysctl(const char *name, void *oldp, size_t *oldlenp,
336 void *newp, size_t newlen)
340 error = sysctlbyname(name, oldp, oldlenp, newp, newlen);
341 if (error != 0 && errno != ENOMEM)
342 err(1, "sysctl(%s)", name);
347 getdrivedata(char **argv)
349 if ((num_devices = devstat_getnumdevs(NULL)) < 0)
350 errx(1, "%s", devstat_errbuf);
352 cur.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo));
353 last.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo));
355 if (devstat_getdevs(NULL, &cur) == -1)
356 errx(1, "%s", devstat_errbuf);
358 num_devices = cur.dinfo->numdevs;
359 generation = cur.dinfo->generation;
361 specified_devices = (char **)malloc(sizeof(char *));
362 for (num_devices_specified = 0; *argv; ++argv) {
365 num_devices_specified++;
366 specified_devices = (char **)realloc(specified_devices,
368 num_devices_specified);
369 specified_devices[num_devices_specified - 1] = *argv;
373 if (nflag == 0 && maxshowdevs < num_devices_specified)
374 maxshowdevs = num_devices_specified;
377 * People are generally only interested in disk statistics when
378 * they're running vmstat. So, that's what we're going to give
379 * them if they don't specify anything by default. We'll also give
380 * them any other random devices in the system so that we get to
381 * maxshowdevs devices, if that many devices exist. If the user
382 * specifies devices on the command line, either through a pattern
383 * match or by naming them explicitly, we will give the user only
386 if ((num_devices_specified == 0) && (num_matches == 0)) {
387 if (devstat_buildmatch(da, &matches, &num_matches) != 0)
388 errx(1, "%s", devstat_errbuf);
390 select_mode = DS_SELECT_ADD;
392 select_mode = DS_SELECT_ONLY;
395 * At this point, selectdevs will almost surely indicate that the
396 * device list has changed, so we don't look for return values of 0
397 * or 1. If we get back -1, though, there is an error.
399 if (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
400 &select_generation, generation, cur.dinfo->devices,
401 num_devices, matches, num_matches, specified_devices,
402 num_devices_specified, select_mode,
403 maxshowdevs, 0) == -1)
404 errx(1, "%s", devstat_errbuf);
414 (void)clock_gettime(CLOCK_MONOTONIC, &sp);
420 fill_pcpu(struct pcpu ***pcpup, int* maxcpup)
431 maxcpu = kvm_getmaxcpu(kd);
433 errx(1, "kvm_getmaxcpu: %s", kvm_geterr(kd));
435 pcpu = calloc(maxcpu, sizeof(struct pcpu *));
439 for (i = 0; i < maxcpu; i++) {
440 pcpu[i] = kvm_getpcpu(kd, i);
441 if (pcpu[i] == (struct pcpu *)-1)
442 errx(1, "kvm_getpcpu: %s", kvm_geterr(kd));
450 free_pcpu(struct pcpu **pcpu, int maxcpu)
454 for (i = 0; i < maxcpu; i++)
460 fill_vmmeter(struct vmmeter *vmmp)
466 kread(X_SUM, vmmp, sizeof(*vmmp));
467 fill_pcpu(&pcpu, &maxcpu);
468 for (i = 0; i < maxcpu; i++) {
471 #define ADD_FROM_PCPU(i, name) \
472 vmmp->name += pcpu[i]->pc_cnt.name
473 ADD_FROM_PCPU(i, v_swtch);
474 ADD_FROM_PCPU(i, v_trap);
475 ADD_FROM_PCPU(i, v_syscall);
476 ADD_FROM_PCPU(i, v_intr);
477 ADD_FROM_PCPU(i, v_soft);
478 ADD_FROM_PCPU(i, v_vm_faults);
479 ADD_FROM_PCPU(i, v_io_faults);
480 ADD_FROM_PCPU(i, v_cow_faults);
481 ADD_FROM_PCPU(i, v_cow_optim);
482 ADD_FROM_PCPU(i, v_zfod);
483 ADD_FROM_PCPU(i, v_ozfod);
484 ADD_FROM_PCPU(i, v_swapin);
485 ADD_FROM_PCPU(i, v_swapout);
486 ADD_FROM_PCPU(i, v_swappgsin);
487 ADD_FROM_PCPU(i, v_swappgsout);
488 ADD_FROM_PCPU(i, v_vnodein);
489 ADD_FROM_PCPU(i, v_vnodeout);
490 ADD_FROM_PCPU(i, v_vnodepgsin);
491 ADD_FROM_PCPU(i, v_vnodepgsout);
492 ADD_FROM_PCPU(i, v_intrans);
493 ADD_FROM_PCPU(i, v_tfree);
494 ADD_FROM_PCPU(i, v_forks);
495 ADD_FROM_PCPU(i, v_vforks);
496 ADD_FROM_PCPU(i, v_rforks);
497 ADD_FROM_PCPU(i, v_kthreads);
498 ADD_FROM_PCPU(i, v_forkpages);
499 ADD_FROM_PCPU(i, v_vforkpages);
500 ADD_FROM_PCPU(i, v_rforkpages);
501 ADD_FROM_PCPU(i, v_kthreadpages);
504 free_pcpu(pcpu, maxcpu);
506 size_t size = sizeof(unsigned int);
507 #define GET_VM_STATS(cat, name) \
508 mysysctl("vm.stats." #cat "." #name, &vmmp->name, &size, NULL, 0)
510 GET_VM_STATS(sys, v_swtch);
511 GET_VM_STATS(sys, v_trap);
512 GET_VM_STATS(sys, v_syscall);
513 GET_VM_STATS(sys, v_intr);
514 GET_VM_STATS(sys, v_soft);
517 GET_VM_STATS(vm, v_vm_faults);
518 GET_VM_STATS(vm, v_io_faults);
519 GET_VM_STATS(vm, v_cow_faults);
520 GET_VM_STATS(vm, v_cow_optim);
521 GET_VM_STATS(vm, v_zfod);
522 GET_VM_STATS(vm, v_ozfod);
523 GET_VM_STATS(vm, v_swapin);
524 GET_VM_STATS(vm, v_swapout);
525 GET_VM_STATS(vm, v_swappgsin);
526 GET_VM_STATS(vm, v_swappgsout);
527 GET_VM_STATS(vm, v_vnodein);
528 GET_VM_STATS(vm, v_vnodeout);
529 GET_VM_STATS(vm, v_vnodepgsin);
530 GET_VM_STATS(vm, v_vnodepgsout);
531 GET_VM_STATS(vm, v_intrans);
532 GET_VM_STATS(vm, v_reactivated);
533 GET_VM_STATS(vm, v_pdwakeups);
534 GET_VM_STATS(vm, v_pdpages);
535 GET_VM_STATS(vm, v_tcached);
536 GET_VM_STATS(vm, v_dfree);
537 GET_VM_STATS(vm, v_pfree);
538 GET_VM_STATS(vm, v_tfree);
539 GET_VM_STATS(vm, v_page_size);
540 GET_VM_STATS(vm, v_page_count);
541 GET_VM_STATS(vm, v_free_reserved);
542 GET_VM_STATS(vm, v_free_target);
543 GET_VM_STATS(vm, v_free_min);
544 GET_VM_STATS(vm, v_free_count);
545 GET_VM_STATS(vm, v_wire_count);
546 GET_VM_STATS(vm, v_active_count);
547 GET_VM_STATS(vm, v_inactive_target);
548 GET_VM_STATS(vm, v_inactive_count);
549 GET_VM_STATS(vm, v_cache_count);
550 GET_VM_STATS(vm, v_cache_min);
551 GET_VM_STATS(vm, v_cache_max);
552 GET_VM_STATS(vm, v_pageout_free_min);
553 GET_VM_STATS(vm, v_interrupt_free_min);
554 /*GET_VM_STATS(vm, v_free_severe);*/
555 GET_VM_STATS(vm, v_forks);
556 GET_VM_STATS(vm, v_vforks);
557 GET_VM_STATS(vm, v_rforks);
558 GET_VM_STATS(vm, v_kthreads);
559 GET_VM_STATS(vm, v_forkpages);
560 GET_VM_STATS(vm, v_vforkpages);
561 GET_VM_STATS(vm, v_rforkpages);
562 GET_VM_STATS(vm, v_kthreadpages);
568 fill_vmtotal(struct vmtotal *vmtp)
572 errx(1, "not implemented");
574 size_t size = sizeof(*vmtp);
575 mysysctl("vm.vmtotal", vmtp, &size, NULL, 0);
576 if (size != sizeof(*vmtp))
577 errx(1, "vm.total size mismatch");
581 /* Determine how many cpu columns, and what index they are in kern.cp_times */
583 getcpuinfo(u_long *maskp, int *maxidp)
595 errx(1, "not implemented");
598 size = sizeof(maxcpu);
599 mysysctl("kern.smp.maxcpus", &maxcpu, &size, NULL, 0);
600 if (size != sizeof(maxcpu))
601 errx(1, "sysctl kern.smp.maxcpus");
602 size = sizeof(long) * maxcpu * CPUSTATES;
603 times = malloc(size);
605 err(1, "malloc %zd bytes", size);
606 mysysctl("kern.cp_times", times, &size, NULL, 0);
607 maxid = (size / CPUSTATES / sizeof(long)) - 1;
608 for (i = 0; i <= maxid; i++) {
610 for (j = 0; empty && j < CPUSTATES; j++) {
611 if (times[i * CPUSTATES + j] != 0)
628 prthuman(u_int64_t val, int size)
633 if (size < 5 || size > 9)
635 flags = HN_B | HN_NOSPACE | HN_DECIMAL;
636 humanize_number(buf, size, val, "", HN_AUTOSCALE, flags);
637 printf("%*s", size, buf);
640 static int hz, hdrcnt;
642 static long *cur_cp_times;
643 static long *last_cp_times;
644 static size_t size_cp_times;
647 dovmstat(unsigned int interval, int reps)
649 struct vmtotal total;
650 time_t uptime, halfuptime;
651 struct devinfo *tmp_dinfo;
657 uptime = getuptime();
658 halfuptime = uptime / 2;
664 * If the user stops the program (control-Z) and then resumes it,
665 * print out the header again.
667 (void)signal(SIGCONT, needhdr);
670 * If our standard output is a tty, then install a SIGWINCH handler
671 * and set wresized so that our first iteration through the main
672 * vmstat loop will peek at the terminal's current rows to find out
673 * how many lines can fit in a screenful of output.
675 if (isatty(fileno(stdout)) != 0) {
677 (void)signal(SIGWINCH, needresize);
680 winlines = VMSTAT_DEFAULT_LINES;
684 if (namelist[X_STATHZ].n_type != 0 &&
685 namelist[X_STATHZ].n_value != 0)
686 kread(X_STATHZ, &hz, sizeof(hz));
688 kread(X_HZ, &hz, sizeof(hz));
690 struct clockinfo clockrate;
692 size = sizeof(clockrate);
693 mysysctl("kern.clockrate", &clockrate, &size, NULL, 0);
694 if (size != sizeof(clockrate))
695 errx(1, "clockrate size mismatch");
700 ncpus = getcpuinfo(&cpumask, &maxid);
701 size_cp_times = sizeof(long) * (maxid + 1) * CPUSTATES;
702 cur_cp_times = calloc(1, size_cp_times);
703 last_cp_times = calloc(1, size_cp_times);
707 printhdr(maxid, cpumask);
709 if (kvm_getcptime(kd, cur.cp_time) < 0)
710 errx(1, "kvm_getcptime: %s", kvm_geterr(kd));
712 size = sizeof(cur.cp_time);
713 mysysctl("kern.cp_time", &cur.cp_time, &size, NULL, 0);
714 if (size != sizeof(cur.cp_time))
715 errx(1, "cp_time size mismatch");
718 size = size_cp_times;
719 mysysctl("kern.cp_times", cur_cp_times, &size, NULL, 0);
720 if (size != size_cp_times)
721 errx(1, "cp_times mismatch");
724 tmp_dinfo = last.dinfo;
725 last.dinfo = cur.dinfo;
726 cur.dinfo = tmp_dinfo;
727 last.snap_time = cur.snap_time;
730 * Here what we want to do is refresh our device stats.
731 * getdevs() returns 1 when the device list has changed.
732 * If the device list has changed, we want to go through
733 * the selection process again, in case a device that we
734 * were previously displaying has gone away.
736 switch (devstat_getdevs(NULL, &cur)) {
738 errx(1, "%s", devstat_errbuf);
743 num_devices = cur.dinfo->numdevs;
744 generation = cur.dinfo->generation;
746 retval = devstat_selectdevs(&dev_select, &num_selected,
747 &num_selections, &select_generation,
748 generation, cur.dinfo->devices,
749 num_devices, matches, num_matches,
751 num_devices_specified, select_mode,
755 errx(1, "%s", devstat_errbuf);
758 printhdr(maxid, cpumask);
769 fill_vmtotal(&total);
770 (void)printf("%2d %1d %1d",
771 total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
772 #define vmstat_pgtok(a) ((a) * (sum.v_page_size >> 10))
773 #define rate(x) (((x) * rate_adj + halfuptime) / uptime) /* round */
776 prthuman(total.t_avm * (u_int64_t)sum.v_page_size, 7);
778 prthuman(total.t_free * (u_int64_t)sum.v_page_size, 6);
781 printf(" %7d ", vmstat_pgtok(total.t_avm));
782 printf(" %6d ", vmstat_pgtok(total.t_free));
784 (void)printf("%5lu ",
785 (unsigned long)rate(sum.v_vm_faults - osum.v_vm_faults));
786 (void)printf("%3lu ",
787 (unsigned long)rate(sum.v_reactivated - osum.v_reactivated));
788 (void)printf("%3lu ",
789 (unsigned long)rate(sum.v_swapin + sum.v_vnodein -
790 (osum.v_swapin + osum.v_vnodein)));
791 (void)printf("%3lu ",
792 (unsigned long)rate(sum.v_swapout + sum.v_vnodeout -
793 (osum.v_swapout + osum.v_vnodeout)));
794 (void)printf("%5lu ",
795 (unsigned long)rate(sum.v_tfree - osum.v_tfree));
796 (void)printf("%3lu ",
797 (unsigned long)rate(sum.v_pdpages - osum.v_pdpages));
799 (void)printf("%4lu %4lu %4lu",
800 (unsigned long)rate(sum.v_intr - osum.v_intr),
801 (unsigned long)rate(sum.v_syscall - osum.v_syscall),
802 (unsigned long)rate(sum.v_swtch - osum.v_swtch));
804 pcpustats(ncpus, cpumask, maxid);
808 (void)fflush(stdout);
809 if (reps >= 0 && --reps <= 0)
815 * We round upward to avoid losing low-frequency events
816 * (i.e., >= 1 per interval but < 1 per millisecond).
819 halfuptime = (uptime + 1) / 2;
822 (void)usleep(interval * 1000);
827 printhdr(int maxid, u_long cpumask)
831 num_shown = (num_selected < maxshowdevs) ? num_selected : maxshowdevs;
832 (void)printf(" procs memory page%*s", 19, "");
834 (void)printf(" disks %*s", num_shown * 4 - 7, "");
835 else if (num_shown == 1)
836 (void)printf("disk");
837 (void)printf(" faults ");
839 for (i = 0; i <= maxid; i++) {
840 if (cpumask & (1ul << i))
841 printf("cpu%-2d ", i);
846 (void)printf(" r b w avm fre flt re pi po fr sr ");
847 for (i = 0; i < num_devices; i++)
848 if ((dev_select[i].selected)
849 && (dev_select[i].selected <= maxshowdevs))
850 (void)printf("%c%c%d ", dev_select[i].device_name[0],
851 dev_select[i].device_name[1],
852 dev_select[i].unit_number);
853 (void)printf(" in sy cs");
855 for (i = 0; i <= maxid; i++) {
856 if (cpumask & (1ul << i))
861 printf(" us sy id\n");
868 * Force a header to be prepended to the next output.
871 needhdr(int dummy __unused)
878 * When the terminal is resized, force an update of the maximum number of rows
879 * printed between each header repetition. Then force a new header to be
880 * prepended to the next output.
883 needresize(int signo)
891 * Update the global `winlines' count of terminal rows.
900 status = ioctl(fileno(stdout), TIOCGWINSZ, &w);
901 if (status == -1 && errno == EINTR)
903 else if (status == -1)
906 winlines = w.ws_row - 3;
908 winlines = VMSTAT_DEFAULT_LINES;
913 * Inhibit doresize() calls until we are rescheduled by SIGWINCH.
922 unsigned int pgintime, rectime;
924 kread(X_REC, &rectime, sizeof(rectime));
925 kread(X_PGIN, &pgintime, sizeof(pgintime));
926 kread(X_SUM, &sum, sizeof(sum));
927 (void)printf("%u reclaims, %u total time (usec)\n",
928 sum.v_pgrec, rectime);
929 (void)printf("average: %u usec / reclaim\n", rectime / sum.v_pgrec);
931 (void)printf("%u page ins, %u total time (msec)\n",
932 sum.v_pgin, pgintime / 10);
933 (void)printf("average: %8.1f msec / page in\n",
934 pgintime / (sum.v_pgin * 10.0));
939 pct(long top, long bot)
945 ans = (quad_t)top * 100 / bot;
949 #define PCT(top, bot) pct((long)(top), (long)(bot))
954 struct nchstats lnchstats;
958 (void)printf("%9u cpu context switches\n", sum.v_swtch);
959 (void)printf("%9u device interrupts\n", sum.v_intr);
960 (void)printf("%9u software interrupts\n", sum.v_soft);
961 (void)printf("%9u traps\n", sum.v_trap);
962 (void)printf("%9u system calls\n", sum.v_syscall);
963 (void)printf("%9u kernel threads created\n", sum.v_kthreads);
964 (void)printf("%9u fork() calls\n", sum.v_forks);
965 (void)printf("%9u vfork() calls\n", sum.v_vforks);
966 (void)printf("%9u rfork() calls\n", sum.v_rforks);
967 (void)printf("%9u swap pager pageins\n", sum.v_swapin);
968 (void)printf("%9u swap pager pages paged in\n", sum.v_swappgsin);
969 (void)printf("%9u swap pager pageouts\n", sum.v_swapout);
970 (void)printf("%9u swap pager pages paged out\n", sum.v_swappgsout);
971 (void)printf("%9u vnode pager pageins\n", sum.v_vnodein);
972 (void)printf("%9u vnode pager pages paged in\n", sum.v_vnodepgsin);
973 (void)printf("%9u vnode pager pageouts\n", sum.v_vnodeout);
974 (void)printf("%9u vnode pager pages paged out\n", sum.v_vnodepgsout);
975 (void)printf("%9u page daemon wakeups\n", sum.v_pdwakeups);
976 (void)printf("%9u pages examined by the page daemon\n", sum.v_pdpages);
977 (void)printf("%9u pages reactivated\n", sum.v_reactivated);
978 (void)printf("%9u copy-on-write faults\n", sum.v_cow_faults);
979 (void)printf("%9u copy-on-write optimized faults\n", sum.v_cow_optim);
980 (void)printf("%9u zero fill pages zeroed\n", sum.v_zfod);
981 (void)printf("%9u zero fill pages prezeroed\n", sum.v_ozfod);
982 (void)printf("%9u intransit blocking page faults\n", sum.v_intrans);
983 (void)printf("%9u total VM faults taken\n", sum.v_vm_faults);
984 (void)printf("%9u page faults requiring I/O\n", sum.v_io_faults);
985 (void)printf("%9u pages affected by kernel thread creation\n", sum.v_kthreadpages);
986 (void)printf("%9u pages affected by fork()\n", sum.v_forkpages);
987 (void)printf("%9u pages affected by vfork()\n", sum.v_vforkpages);
988 (void)printf("%9u pages affected by rfork()\n", sum.v_rforkpages);
989 (void)printf("%9u pages cached\n", sum.v_tcached);
990 (void)printf("%9u pages freed\n", sum.v_tfree);
991 (void)printf("%9u pages freed by daemon\n", sum.v_dfree);
992 (void)printf("%9u pages freed by exiting processes\n", sum.v_pfree);
993 (void)printf("%9u pages active\n", sum.v_active_count);
994 (void)printf("%9u pages inactive\n", sum.v_inactive_count);
995 (void)printf("%9u pages in VM cache\n", sum.v_cache_count);
996 (void)printf("%9u pages wired down\n", sum.v_wire_count);
997 (void)printf("%9u pages free\n", sum.v_free_count);
998 (void)printf("%9u bytes per page\n", sum.v_page_size);
1000 kread(X_NCHSTATS, &lnchstats, sizeof(lnchstats));
1002 size_t size = sizeof(lnchstats);
1003 mysysctl("vfs.cache.nchstats", &lnchstats, &size, NULL, 0);
1004 if (size != sizeof(lnchstats))
1005 errx(1, "vfs.cache.nchstats size mismatch");
1007 nchtotal = lnchstats.ncs_goodhits + lnchstats.ncs_neghits +
1008 lnchstats.ncs_badhits + lnchstats.ncs_falsehits +
1009 lnchstats.ncs_miss + lnchstats.ncs_long;
1010 (void)printf("%9ld total name lookups\n", nchtotal);
1012 "%9s cache hits (%ld%% pos + %ld%% neg) system %ld%% per-directory\n",
1013 "", PCT(lnchstats.ncs_goodhits, nchtotal),
1014 PCT(lnchstats.ncs_neghits, nchtotal),
1015 PCT(lnchstats.ncs_pass2, nchtotal));
1016 (void)printf("%9s deletions %ld%%, falsehits %ld%%, toolong %ld%%\n", "",
1017 PCT(lnchstats.ncs_badhits, nchtotal),
1018 PCT(lnchstats.ncs_falsehits, nchtotal),
1019 PCT(lnchstats.ncs_long, nchtotal));
1026 (void)printf("%u forks, %u pages, average %.2f\n",
1027 sum.v_forks, sum.v_forkpages,
1028 sum.v_forks == 0 ? 0.0 :
1029 (double)sum.v_forkpages / sum.v_forks);
1030 (void)printf("%u vforks, %u pages, average %.2f\n",
1031 sum.v_vforks, sum.v_vforkpages,
1032 sum.v_vforks == 0 ? 0.0 :
1033 (double)sum.v_vforkpages / sum.v_vforks);
1034 (void)printf("%u rforks, %u pages, average %.2f\n",
1035 sum.v_rforks, sum.v_rforkpages,
1036 sum.v_rforks == 0 ? 0.0 :
1037 (double)sum.v_rforkpages / sum.v_rforks);
1044 long double transfers_per_second;
1045 long double busy_seconds;
1048 for (state = 0; state < CPUSTATES; ++state) {
1049 tmp = cur.cp_time[state];
1050 cur.cp_time[state] -= last.cp_time[state];
1051 last.cp_time[state] = tmp;
1054 busy_seconds = cur.snap_time - last.snap_time;
1056 for (dn = 0; dn < num_devices; dn++) {
1059 if ((dev_select[dn].selected == 0)
1060 || (dev_select[dn].selected > maxshowdevs))
1063 di = dev_select[dn].position;
1065 if (devstat_compute_statistics(&cur.dinfo->devices[di],
1066 &last.dinfo->devices[di], busy_seconds,
1067 DSM_TRANSFERS_PER_SECOND, &transfers_per_second,
1069 errx(1, "%s", devstat_errbuf);
1071 (void)printf("%3.0Lf ", transfers_per_second);
1076 percent(double pct, int *over)
1081 l = snprintf(buf, sizeof(buf), "%.0f", pct);
1082 if (l == 1 && *over) {
1098 for (state = 0; state < CPUSTATES; ++state)
1099 total += cur.cp_time[state];
1101 lpct = 100.0 / total;
1106 percent((cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * lpct, &over);
1108 percent((cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * lpct, &over);
1110 percent(cur.cp_time[CP_IDLE] * lpct, &over);
1114 pcpustats(int ncpus, u_long cpumask, int maxid)
1121 /* devstats does this for cp_time */
1122 for (i = 0; i <= maxid; i++) {
1123 if ((cpumask & (1ul << i)) == 0)
1125 for (state = 0; state < CPUSTATES; ++state) {
1126 tmp = cur_cp_times[i * CPUSTATES + state];
1127 cur_cp_times[i * CPUSTATES + state] -= last_cp_times[i * CPUSTATES + state];
1128 last_cp_times[i * CPUSTATES + state] = tmp;
1133 for (i = 0; i <= maxid; i++) {
1134 if ((cpumask & (1ul << i)) == 0)
1137 for (state = 0; state < CPUSTATES; ++state)
1138 total += cur_cp_times[i * CPUSTATES + state];
1140 lpct = 100.0 / total;
1144 percent((cur_cp_times[i * CPUSTATES + CP_USER] +
1145 cur_cp_times[i * CPUSTATES + CP_NICE]) * lpct, &over);
1147 percent((cur_cp_times[i * CPUSTATES + CP_SYS] +
1148 cur_cp_times[i * CPUSTATES + CP_INTR]) * lpct, &over);
1150 percent(cur_cp_times[i * CPUSTATES + CP_IDLE] * lpct, &over);
1157 unsigned long *intrcnt, uptime;
1159 size_t clen, inamlen, intrcntlen, istrnamlen;
1160 unsigned int i, nintr;
1161 char *intrname, *tintrname;
1163 uptime = getuptime();
1165 kread(X_SINTRCNT, &intrcntlen, sizeof(intrcntlen));
1166 kread(X_SINTRNAMES, &inamlen, sizeof(inamlen));
1167 if ((intrcnt = malloc(intrcntlen)) == NULL ||
1168 (intrname = malloc(inamlen)) == NULL)
1170 kread(X_INTRCNT, intrcnt, intrcntlen);
1171 kread(X_INTRNAMES, intrname, inamlen);
1173 for (intrcnt = NULL, intrcntlen = 1024; ; intrcntlen *= 2) {
1174 if ((intrcnt = reallocf(intrcnt, intrcntlen)) == NULL)
1175 err(1, "reallocf()");
1176 if (mysysctl("hw.intrcnt",
1177 intrcnt, &intrcntlen, NULL, 0) == 0)
1180 for (intrname = NULL, inamlen = 1024; ; inamlen *= 2) {
1181 if ((intrname = reallocf(intrname, inamlen)) == NULL)
1182 err(1, "reallocf()");
1183 if (mysysctl("hw.intrnames",
1184 intrname, &inamlen, NULL, 0) == 0)
1188 nintr = intrcntlen / sizeof(unsigned long);
1189 tintrname = intrname;
1190 istrnamlen = strlen("interrupt");
1191 for (i = 0; i < nintr; i++) {
1192 clen = strlen(tintrname);
1193 if (clen > istrnamlen)
1195 tintrname += clen + 1;
1197 (void)printf("%-*s %20s %10s\n", (int)istrnamlen, "interrupt", "total",
1200 for (i = 0; i < nintr; i++) {
1201 if (intrname[0] != '\0' && (*intrcnt != 0 || aflag))
1202 (void)printf("%-*s %20lu %10lu\n", (int)istrnamlen,
1203 intrname, *intrcnt, *intrcnt / uptime);
1204 intrname += strlen(intrname) + 1;
1205 inttotal += *intrcnt++;
1207 (void)printf("%-*s %20" PRIu64 " %10" PRIu64 "\n", (int)istrnamlen,
1208 "Total", inttotal, inttotal / uptime);
1212 domemstat_malloc(void)
1214 struct memory_type_list *mtlp;
1215 struct memory_type *mtp;
1216 int error, first, i;
1218 mtlp = memstat_mtl_alloc();
1220 warn("memstat_mtl_alloc");
1224 if (memstat_sysctl_malloc(mtlp, 0) < 0) {
1225 warnx("memstat_sysctl_malloc: %s",
1226 memstat_strerror(memstat_mtl_geterror(mtlp)));
1230 if (memstat_kvm_malloc(mtlp, kd) < 0) {
1231 error = memstat_mtl_geterror(mtlp);
1232 if (error == MEMSTAT_ERROR_KVM)
1233 warnx("memstat_kvm_malloc: %s",
1236 warnx("memstat_kvm_malloc: %s",
1237 memstat_strerror(error));
1240 printf("%13s %5s %6s %7s %8s Size(s)\n", "Type", "InUse", "MemUse",
1241 "HighUse", "Requests");
1242 for (mtp = memstat_mtl_first(mtlp); mtp != NULL;
1243 mtp = memstat_mtl_next(mtp)) {
1244 if (memstat_get_numallocs(mtp) == 0 &&
1245 memstat_get_count(mtp) == 0)
1247 printf("%13s %5" PRIu64 " %5" PRIu64 "K %7s %8" PRIu64 " ",
1248 memstat_get_name(mtp), memstat_get_count(mtp),
1249 (memstat_get_bytes(mtp) + 1023) / 1024, "-",
1250 memstat_get_numallocs(mtp));
1252 for (i = 0; i < 32; i++) {
1253 if (memstat_get_sizemask(mtp) & (1 << i)) {
1256 printf("%d", 1 << (i + 4));
1262 memstat_mtl_free(mtlp);
1266 domemstat_zone(void)
1268 struct memory_type_list *mtlp;
1269 struct memory_type *mtp;
1270 char name[MEMTYPE_MAXNAME + 1];
1273 mtlp = memstat_mtl_alloc();
1275 warn("memstat_mtl_alloc");
1279 if (memstat_sysctl_uma(mtlp, 0) < 0) {
1280 warnx("memstat_sysctl_uma: %s",
1281 memstat_strerror(memstat_mtl_geterror(mtlp)));
1285 if (memstat_kvm_uma(mtlp, kd) < 0) {
1286 error = memstat_mtl_geterror(mtlp);
1287 if (error == MEMSTAT_ERROR_KVM)
1288 warnx("memstat_kvm_uma: %s",
1291 warnx("memstat_kvm_uma: %s",
1292 memstat_strerror(error));
1295 printf("%-20s %6s %6s %8s %8s %8s %4s %4s\n\n", "ITEM", "SIZE",
1296 "LIMIT", "USED", "FREE", "REQ", "FAIL", "SLEEP");
1297 for (mtp = memstat_mtl_first(mtlp); mtp != NULL;
1298 mtp = memstat_mtl_next(mtp)) {
1299 strlcpy(name, memstat_get_name(mtp), MEMTYPE_MAXNAME);
1301 printf("%-20s %6" PRIu64 ", %6" PRIu64 ",%8" PRIu64 ",%8" PRIu64
1302 ",%8" PRIu64 ",%4" PRIu64 ",%4" PRIu64 "\n", name,
1303 memstat_get_size(mtp), memstat_get_countlimit(mtp),
1304 memstat_get_count(mtp), memstat_get_free(mtp),
1305 memstat_get_numallocs(mtp), memstat_get_failures(mtp),
1306 memstat_get_sleeps(mtp));
1308 memstat_mtl_free(mtlp);
1313 * kread reads something from the kernel, given its nlist index.
1316 kreado(int nlx, void *addr, size_t size, size_t offset)
1320 if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
1321 sym = namelist[nlx].n_name;
1324 errx(1, "symbol %s not defined", sym);
1326 if ((size_t)kvm_read(kd, namelist[nlx].n_value + offset, addr,
1328 sym = namelist[nlx].n_name;
1331 errx(1, "%s: %s", sym, kvm_geterr(kd));
1336 kread(int nlx, void *addr, size_t size)
1338 kreado(nlx, addr, size, 0);
1342 kgetstr(const char *strp)
1344 int n = 0, size = 1;
1348 if (size == n + 1) {
1349 ret = realloc(ret, size);
1351 err(1, "%s: realloc", __func__);
1354 if (kvm_read(kd, (u_long)strp + n, &ret[n], 1) != 1)
1355 errx(1, "%s: %s", __func__, kvm_geterr(kd));
1356 } while (ret[n++] != '\0');
1363 (void)fprintf(stderr, "%s%s",
1364 "usage: vmstat [-afHhimPsz] [-M core [-N system]] [-c count] [-n devs]\n",
1365 " [-p type,if,pass] [-w wait] [disks] [wait [count]]\n");