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. 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
29 * @(#) Copyright (c) 1980, 1986, 1991, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)vmstat.c 8.1 (Berkeley) 6/6/93
31 * $FreeBSD: src/usr.bin/vmstat/vmstat.c,v 1.38.2.4 2001/07/31 19:52:41 tmm Exp $
35 #include <sys/param.h>
38 #include <sys/namei.h>
39 #include <sys/objcache.h>
40 #include <sys/signal.h>
41 #include <sys/fcntl.h>
42 #include <sys/ioctl.h>
43 #include <sys/sysctl.h>
44 #include <sys/vmmeter.h>
45 #include <sys/interrupt.h>
47 #include <vm/vm_param.h>
48 #include <vm/vm_zone.h>
66 static struct nlist namelist
[] = {
68 { "_boottime", 0, 0, 0, 0 },
70 { "_nchstats", 0, 0, 0, 0 },
71 #define X_KMEMSTATISTICS 2
72 { "_kmemstatistics", 0, 0, 0, 0 },
74 { "_ncpus", 0, 0, 0, 0 },
76 { "_zlist", 0, 0, 0, 0 },
77 #define X_KSLAB_DUMMY 5
78 { "_kslab_dummy", 0, 0, 0, 0 },
81 { "_deficit", 0, 0, 0, 0 },
83 { "_forkstat", 0, 0, 0, 0 },
85 { "_rectime", 0, 0, 0, 0 },
87 { "_pgintime", 0, 0, 0, 0 },
89 { "_xstats", 0, 0, 0, 0 },
97 #define ONEMB (1024L * 1024L)
100 LIST_HEAD(zlist
, vm_zone
);
102 struct statinfo cur
, last
;
103 int num_devices
, maxshowdevs
;
105 struct device_selection
*dev_select
;
107 struct devstat_match
*matches
;
109 int num_devices_specified
, num_selections
;
110 long select_generation
;
111 char **specified_devices
;
112 devstat_select_mode select_mode
;
114 struct vmmeter vmm
, ovmm
;
115 struct vmstats vms
, ovms
;
121 int unformatted_opt
= 0;
127 struct kinfo_cputime cp_time
, old_cp_time
, diff_cp_time
;
129 #define FORKSTAT 0x01
130 #define INTRSTAT 0x02
133 #define TIMESTAT 0x10
135 #define ZMEMSTAT 0x40
138 static void cpustats(void);
139 static void dointr(void);
140 static void domem(void);
141 static void dooc(void);
142 static void dosum(void);
143 static void dozmem(u_int interval
, int reps
);
144 static void dovmstat(u_int
, int);
145 static void kread(int, void *, size_t);
146 static void usage(void);
147 static char **getdrivedata(char **);
148 static long getuptime(void);
149 static void needhdr(int);
150 static long pct(long, long);
153 static void dotimes(void); /* Not implemented */
154 static void doforkst(void);
156 static void printhdr(void);
157 static const char *formatnum(intmax_t value
, int width
, int do10s
);
158 static void devstats(int dooutput
);
161 main(int argc
, char **argv
)
164 u_int interval
; /* milliseconds */
167 char errbuf
[_POSIX2_LINE_MAX
];
169 memf
= nlistf
= NULL
;
170 interval
= reps
= todo
= 0;
172 while ((c
= getopt(argc
, argv
, "bc:fiM:mN:n:op:stuvw:z")) != -1) {
184 errx(EX_USAGE
, "sorry, -f is not (re)implemented yet");
202 maxshowdevs
= atoi(optarg
);
204 errx(1, "number of devices %d is < 0",
211 if (buildmatch(optarg
, &matches
, &num_matches
) != 0)
212 errx(1, "%s", devstat_errbuf
);
221 errx(EX_USAGE
, "sorry, -t is not (re)implemented yet");
231 interval
= (u_int
)(strtod(optarg
, NULL
) * 1000.0);
247 * Discard setgid privileges if not the running kernel so that bad
248 * guys can't print interesting stuff from kernel memory.
250 if (nlistf
!= NULL
|| memf
!= NULL
) {
253 errx(1, "objcache stats can only be gathered on "
254 "the running system");
258 kd
= kvm_openfiles(nlistf
, memf
, NULL
, O_RDONLY
, errbuf
);
260 errx(1, "kvm_openfiles: %s", errbuf
);
262 if ((c
= kvm_nlist(kd
, namelist
)) != 0) {
264 warnx("undefined symbols:");
265 for (c
= 0; c
< (int)NELEM(namelist
); c
++)
266 if (namelist
[c
].n_type
== 0)
267 fprintf(stderr
, " %s",
271 warnx("kvm_nlist: %s", kvm_geterr(kd
));
275 kread(X_NCPUS
, &ncpus
, sizeof(ncpus
));
278 struct winsize winsize
;
281 * Make sure that the userland devstat version matches the
282 * kernel devstat version. If not, exit and print a
283 * message informing the user of his mistake.
285 if (checkversion() < 0)
286 errx(1, "%s", devstat_errbuf
);
289 argv
= getdrivedata(argv
);
291 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, (char *)&winsize
);
292 if (winsize
.ws_row
> 0)
293 winlines
= winsize
.ws_row
;
297 #define BACKWARD_COMPATIBILITY
298 #ifdef BACKWARD_COMPATIBILITY
300 interval
= (u_int
)(strtod(*argv
, NULL
) * 1000.0);
320 dozmem(interval
, reps
);
330 dovmstat(interval
, reps
);
337 getdrivedata(char **argv
)
339 if ((num_devices
= getnumdevs()) < 0)
340 errx(1, "%s", devstat_errbuf
);
342 cur
.dinfo
= (struct devinfo
*)malloc(sizeof(struct devinfo
));
343 last
.dinfo
= (struct devinfo
*)malloc(sizeof(struct devinfo
));
344 bzero(cur
.dinfo
, sizeof(struct devinfo
));
345 bzero(last
.dinfo
, sizeof(struct devinfo
));
347 if (getdevs(&cur
) == -1)
348 errx(1, "%s", devstat_errbuf
);
350 num_devices
= cur
.dinfo
->numdevs
;
351 generation
= cur
.dinfo
->generation
;
353 specified_devices
= (char **)malloc(sizeof(char *));
354 for (num_devices_specified
= 0; *argv
; ++argv
) {
357 num_devices_specified
++;
358 specified_devices
= (char **)realloc(specified_devices
,
360 num_devices_specified
);
361 specified_devices
[num_devices_specified
- 1] = *argv
;
365 if (nflag
== 0 && maxshowdevs
< num_devices_specified
)
366 maxshowdevs
= num_devices_specified
;
369 * People are generally only interested in disk statistics when
370 * they're running vmstat. So, that's what we're going to give
371 * them if they don't specify anything by default. We'll also give
372 * them any other random devices in the system so that we get to
373 * maxshowdevs devices, if that many devices exist. If the user
374 * specifies devices on the command line, either through a pattern
375 * match or by naming them explicitly, we will give the user only
378 if ((num_devices_specified
== 0) && (num_matches
== 0)) {
379 if (buildmatch("da", &matches
, &num_matches
) != 0)
380 errx(1, "%s", devstat_errbuf
);
382 select_mode
= DS_SELECT_ADD
;
384 select_mode
= DS_SELECT_ONLY
;
387 * At this point, selectdevs will almost surely indicate that the
388 * device list has changed, so we don't look for return values of 0
389 * or 1. If we get back -1, though, there is an error.
391 if (selectdevs(&dev_select
, &num_selected
, &num_selections
,
392 &select_generation
, generation
, cur
.dinfo
->devices
,
393 num_devices
, matches
, num_matches
, specified_devices
,
394 num_devices_specified
, select_mode
,
395 maxshowdevs
, 0) == -1)
396 errx(1, "%s", devstat_errbuf
);
406 clock_gettime(CLOCK_UPTIME
, &ts
);
414 dovmstat(u_int interval
, int reps
)
416 struct vmtotal total
;
417 struct devinfo
*tmp_dinfo
;
418 size_t vmm_size
= sizeof(vmm
);
419 size_t vms_size
= sizeof(vms
);
420 size_t vmt_size
= sizeof(total
);
424 signal(SIGCONT
, needhdr
);
431 if (kinfo_get_sched_cputime(&cp_time
))
432 err(1, "kinfo_get_sched_cputime");
434 tmp_dinfo
= last
.dinfo
;
435 last
.dinfo
= cur
.dinfo
;
436 cur
.dinfo
= tmp_dinfo
;
437 last
.busy_time
= cur
.busy_time
;
440 * Here what we want to do is refresh our device stats.
441 * getdevs() returns 1 when the device list has changed.
442 * If the device list has changed, we want to go through
443 * the selection process again, in case a device that we
444 * were previously displaying has gone away.
446 switch (getdevs(&cur
)) {
448 errx(1, "%s", devstat_errbuf
);
453 num_devices
= cur
.dinfo
->numdevs
;
454 generation
= cur
.dinfo
->generation
;
456 retval
= selectdevs(&dev_select
, &num_selected
,
457 &num_selections
, &select_generation
,
458 generation
, cur
.dinfo
->devices
,
459 num_devices
, matches
, num_matches
,
461 num_devices_specified
, select_mode
,
465 errx(1, "%s", devstat_errbuf
);
478 if (sysctlbyname("vm.vmstats", &vms
, &vms_size
, NULL
, 0)) {
479 perror("sysctlbyname: vm.vmstats");
482 if (sysctlbyname("vm.vmmeter", &vmm
, &vmm_size
, NULL
, 0)) {
483 perror("sysctlbyname: vm.vmmeter");
486 if (sysctlbyname("vm.vmtotal", &total
, &vmt_size
, NULL
, 0)) {
487 perror("sysctlbyname: vm.vmtotal");
492 * Be a little inventive so we can squeeze everything into
493 * 80 columns. These days the run queue can trivially be
494 * into the three digits and under heavy paging loads the
495 * blocked (d+p) count can as well.
505 if (total
.t_rq
- 1 < 1000) {
506 snprintf(b1
, sizeof(b1
),
507 "%3ld", total
.t_rq
- 1);
509 if (total
.t_dw
+ total
.t_pw
< 1000) {
510 snprintf(b2
, sizeof(b2
),
511 "%3ld", total
.t_dw
+ total
.t_pw
);
513 if (total
.t_sw
< 10) {
514 snprintf(b3
, sizeof(b3
), "%ld", total
.t_sw
);
516 printf("%s %s %s", b1
, b2
, b3
);
520 (intmax_t)(initial ? (x) : ((intmax_t)(x) * 1000 + interval / 2) \
525 formatnum((int64_t)total
.t_free
*
529 formatnum(rate(vmm
.v_vm_faults
-
533 formatnum(rate((vmm
.v_reactivated
-
534 ovmm
.v_reactivated
) *
538 formatnum(rate((vmm
.v_swappgsin
+
545 formatnum(rate((vmm
.v_swappgsout
+
548 ovmm
.v_vnodepgsout
) *
552 formatnum(rate((vmm
.v_tfree
- ovmm
.v_tfree
) *
553 vms
.v_page_size
), 4, 1));
558 formatnum(rate(vmm
.v_intr
- ovmm
.v_intr
),
561 formatnum(rate(vmm
.v_syscall
-
565 formatnum(rate(vmm
.v_swtch
-
572 if (reps
>= 0 && --reps
<= 0)
575 usleep(interval
* 1000);
582 formatnum(intmax_t value
, int width
, int do10s
)
584 static char buf
[16][64];
594 if (unformatted_opt
) {
597 snprintf(buf
[bi
], sizeof(buf
[bi
]), "%4jd", value
);
600 snprintf(buf
[bi
], sizeof(buf
[bi
]), "%5jd", value
);
603 snprintf(buf
[bi
], sizeof(buf
[bi
]), "%jd", value
);
616 } else if (value
< 10*1024) {
619 } else if (value
< 1000*1024) {
622 } else if (value
< 10*1024*1024) {
624 d
= d
/ (1024 * 1024);
625 } else if (value
< 1000*1024*1024) {
627 d
= d
/ (1024 * 1024);
630 d
= d
/ (1024.0 * 1024.0 * 1024.0);
636 } else if (value
< 10*1024) {
639 } else if (value
< 100*1024 && do10s
) {
642 } else if (value
< 1000*1024) {
645 } else if (value
< 10*1024*1024) {
647 d
= d
/ (1024 * 1024);
648 } else if (value
< 100*1024*1024 && do10s
) {
650 d
= d
/ (1024 * 1024);
651 } else if (value
< 1000*1024*1024) {
653 d
= d
/ (1024 * 1024);
654 } else if (value
< 10LL*1024*1024*1024) {
656 d
= d
/ (1024.0 * 1024.0 * 1024.0);
657 } else if (value
< 100LL*1024*1024*1024 && do10s
) {
659 d
= d
/ (1024.0 * 1024.0 * 1024.0);
660 } else if (value
< 1000LL*1024*1024*1024) {
662 d
= d
/ (1024.0 * 1024.0 * 1024.0);
665 d
= d
/ (1024.0 * 1024.0 * 1024.0 * 1024.0);
669 fprintf(stderr
, "formatnum: unsupported width %d\n", width
);
673 snprintf(buf
[bi
], sizeof(buf
[bi
]), fmt
, d
);
682 num_shown
= (num_selected
< maxshowdevs
) ? num_selected
: maxshowdevs
;
683 printf("--procs-- ---memory-- -------paging------ ");
685 printf("--disks%.*s",
687 "---------------------------------");
688 else if (num_shown
== 1)
690 printf(" -----faults------ ---cpu---\n");
691 printf(" r b w fre flt re pi po fr ");
692 for (i
= 0; i
< num_devices
; i
++)
693 if ((dev_select
[i
].selected
)
694 && (dev_select
[i
].selected
<= maxshowdevs
))
695 printf(" %c%c%d ", dev_select
[i
].device_name
[0],
696 dev_select
[i
].device_name
[1],
697 dev_select
[i
].unit_number
);
698 printf(" int sys ctx us sy id\n");
699 hdrcnt
= winlines
- 2;
703 * Force a header to be prepended to the next output.
706 needhdr(__unused
int signo
)
713 pct(long top
, long bot
)
719 ans
= (quad_t
)top
* 100 / bot
;
723 #define PCT(top, bot) pct((long)(top), (long)(bot))
728 struct nchstats
*nch_tmp
, nchstats
;
729 size_t vms_size
= sizeof(vms
);
730 size_t vmm_size
= sizeof(vmm
);
734 size_t nch_size
= sizeof(struct nchstats
) * SMP_MAXCPU
;
736 if (sysctlbyname("vm.vmstats", &vms
, &vms_size
, NULL
, 0)) {
737 perror("sysctlbyname: vm.vmstats");
740 if (sysctlbyname("vm.vmmeter", &vmm
, &vmm_size
, NULL
, 0)) {
741 perror("sysctlbyname: vm.vmstats");
744 printf("%9u cpu context switches\n", vmm
.v_swtch
);
745 printf("%9u device interrupts\n", vmm
.v_intr
);
746 printf("%9u software interrupts\n", vmm
.v_soft
);
747 printf("%9u traps\n", vmm
.v_trap
);
748 printf("%9u system calls\n", vmm
.v_syscall
);
749 printf("%9u kernel threads created\n", vmm
.v_kthreads
);
750 printf("%9u fork() calls\n", vmm
.v_forks
);
751 printf("%9u vfork() calls\n", vmm
.v_vforks
);
752 printf("%9u rfork() calls\n", vmm
.v_rforks
);
753 printf("%9u exec() calls\n", vmm
.v_exec
);
754 printf("%9u swap pager pageins\n", vmm
.v_swapin
);
755 printf("%9u swap pager pages paged in\n", vmm
.v_swappgsin
);
756 printf("%9u swap pager pageouts\n", vmm
.v_swapout
);
757 printf("%9u swap pager pages paged out\n", vmm
.v_swappgsout
);
758 printf("%9u vnode pager pageins\n", vmm
.v_vnodein
);
759 printf("%9u vnode pager pages paged in\n", vmm
.v_vnodepgsin
);
760 printf("%9u vnode pager pageouts\n", vmm
.v_vnodeout
);
761 printf("%9u vnode pager pages paged out\n", vmm
.v_vnodepgsout
);
762 printf("%9u page daemon wakeups\n", vmm
.v_pdwakeups
);
763 printf("%9u pages examined by the page daemon\n", vmm
.v_pdpages
);
764 printf("%9u pages reactivated\n", vmm
.v_reactivated
);
765 printf("%9u copy-on-write faults\n", vmm
.v_cow_faults
);
766 printf("%9u copy-on-write optimized faults\n", vmm
.v_cow_optim
);
767 printf("%9u zero fill pages zeroed\n", vmm
.v_zfod
);
768 printf("%9u zero fill pages prezeroed\n", vmm
.v_ozfod
);
769 printf("%9u intransit blocking page faults\n", vmm
.v_intrans
);
770 printf("%9u total VM faults taken\n", vmm
.v_vm_faults
);
771 printf("%9u pages affected by kernel thread creation\n", vmm
.v_kthreadpages
);
772 printf("%9u pages affected by fork()\n", vmm
.v_forkpages
);
773 printf("%9u pages affected by vfork()\n", vmm
.v_vforkpages
);
774 printf("%9u pages affected by rfork()\n", vmm
.v_rforkpages
);
775 printf("%9u pages freed\n", vmm
.v_tfree
);
776 printf("%9u pages freed by daemon\n", vmm
.v_dfree
);
777 printf("%9u pages freed by exiting processes\n", vmm
.v_pfree
);
778 printf("%9lu pages active\n", vms
.v_active_count
);
779 printf("%9lu pages inactive\n", vms
.v_inactive_count
);
780 printf("%9lu pages in VM cache\n", vms
.v_cache_count
);
781 printf("%9lu pages wired down\n", vms
.v_wire_count
);
782 printf("%9lu pages free\n", vms
.v_free_count
);
783 printf("%9u bytes per page\n", vms
.v_page_size
);
784 printf("%9u global smp invltlbs\n", vmm
.v_smpinvltlb
);
786 if ((nch_tmp
= malloc(nch_size
)) == NULL
) {
790 if (sysctlbyname("vfs.cache.nchstats", nch_tmp
, &nch_size
, NULL
, 0)) {
791 perror("sysctlbyname vfs.cache.nchstats");
795 if ((nch_tmp
= realloc(nch_tmp
, nch_size
)) == NULL
) {
802 cpucnt
= nch_size
/ sizeof(struct nchstats
);
803 kvm_nch_cpuagg(nch_tmp
, &nchstats
, cpucnt
);
805 nchtotal
= nchstats
.ncs_goodhits
+ nchstats
.ncs_neghits
+
806 nchstats
.ncs_badhits
+ nchstats
.ncs_falsehits
+
808 nchpathtotal
= nchstats
.ncs_longhits
+ nchstats
.ncs_longmiss
;
809 printf("%9ld total path lookups\n", nchpathtotal
);
810 printf("%9ld total component lookups\n", nchtotal
);
812 "%9s cache hits (%ld%% pos + %ld%% neg)\n",
813 "", PCT(nchstats
.ncs_goodhits
, nchtotal
),
814 PCT(nchstats
.ncs_neghits
, nchtotal
));
815 printf("%9s deletions %ld%%, falsehits %ld%%\n", "",
816 PCT(nchstats
.ncs_badhits
, nchtotal
),
817 PCT(nchstats
.ncs_falsehits
, nchtotal
));
827 kread(X_FORKSTAT
, &fks
, sizeof(struct forkstat
));
828 printf("%d forks, %d pages, average %.2f\n",
829 fks
.cntfork
, fks
.sizfork
, (double)fks
.sizfork
/ fks
.cntfork
);
830 printf("%d vforks, %d pages, average %.2f\n",
831 fks
.cntvfork
, fks
.sizvfork
, (double)fks
.sizvfork
/ fks
.cntvfork
);
836 devstats(int dooutput
)
839 long double transfers_per_second
;
840 long double busy_seconds
;
842 diff_cp_time
.cp_user
= cp_time
.cp_user
- old_cp_time
.cp_user
;
843 diff_cp_time
.cp_nice
= cp_time
.cp_nice
- old_cp_time
.cp_nice
;
844 diff_cp_time
.cp_sys
= cp_time
.cp_sys
- old_cp_time
.cp_sys
;
845 diff_cp_time
.cp_intr
= cp_time
.cp_intr
- old_cp_time
.cp_intr
;
846 diff_cp_time
.cp_idle
= cp_time
.cp_idle
- old_cp_time
.cp_idle
;
847 old_cp_time
= cp_time
;
849 busy_seconds
= compute_etime(cur
.busy_time
, last
.busy_time
);
851 for (dn
= 0; dn
< num_devices
; dn
++) {
854 if ((dev_select
[dn
].selected
== 0)
855 || (dev_select
[dn
].selected
> maxshowdevs
))
858 di
= dev_select
[dn
].position
;
860 if (compute_stats(&cur
.dinfo
->devices
[di
],
861 &last
.dinfo
->devices
[di
], busy_seconds
,
863 NULL
, &transfers_per_second
, NULL
,
865 errx(1, "%s", devstat_errbuf
);
868 printf("%s ", formatnum(transfers_per_second
, 4, 0));
878 total
= diff_cp_time
.cp_user
+ diff_cp_time
.cp_nice
+
879 diff_cp_time
.cp_sys
+ diff_cp_time
.cp_intr
+ diff_cp_time
.cp_idle
;
882 totusage
= 100.0 / total
;
886 (diff_cp_time
.cp_user
+ diff_cp_time
.cp_nice
) * totusage
);
888 (diff_cp_time
.cp_sys
+ diff_cp_time
.cp_intr
) * totusage
);
890 diff_cp_time
.cp_idle
* totusage
);
896 u_long
*intrcnt
, uptime
;
898 size_t nintr
, inamlen
, i
, size
;
903 uptime
= getuptime();
904 if (sysctlbyname("hw.intrnames", NULL
, &inamlen
, NULL
, 0) != 0)
905 errx(1, "sysctlbyname");
906 intrstr
= malloc(inamlen
);
909 sysctlbyname("hw.intrnames", intrstr
, &inamlen
, NULL
, 0);
910 for (nintr
= 0, i
= 0; i
< inamlen
; ++i
) {
914 intrname
= malloc(nintr
* sizeof(char *));
915 for (i
= 0; i
< nintr
; ++i
) {
916 intrname
[i
] = intrstr
;
917 intrstr
+= strlen(intrstr
) + 1;
920 size
= nintr
* sizeof(*intrcnt
);
921 intrcnt
= calloc(nintr
, sizeof(*intrcnt
));
924 sysctlbyname("hw.intrcnt", intrcnt
, &size
, NULL
, 0);
927 for (i
= 0; i
< nintr
; ++i
) {
928 if (nwidth
< (int)strlen(intrname
[i
]))
929 nwidth
= (int)strlen(intrname
[i
]);
931 if (verbose
) nwidth
+= 12;
933 printf("%-*.*s %11s %10s\n",
934 nwidth
, nwidth
, "interrupt", "total", "rate");
936 for (i
= 0; i
< nintr
; ++i
) {
938 char *infop
, irqinfo
[72];
940 if ((named
= strncmp(intrname
[i
], "irq", 3)) != 0 ||
949 snprintf(irqinfo
, sizeof(irqinfo
),
951 irq
, cpu
, intrname
[i
]);
953 snprintf(irqinfo
, sizeof(irqinfo
),
954 "irq%-3zd %3zd: ", irq
, cpu
);
958 printf("%-*.*s %11lu %10lu\n",
959 nwidth
, nwidth
, infop
,
960 intrcnt
[i
], intrcnt
[i
] / uptime
);
962 inttotal
+= intrcnt
[i
];
964 printf("%-*.*s %11llu %10llu\n",
965 nwidth
, nwidth
, "Total",
966 (long long)inttotal
, (long long)(inttotal
/ uptime
));
969 #define MAX_KMSTATS 16384
971 enum ksuse
{ KSINUSE
, KSMEMUSE
, KSOBJUSE
, KSCALLS
};
974 cpuagg(const struct malloc_type
*ks
, enum ksuse use
)
983 for (i
= 0; i
< ncpus
; ++i
)
984 ttl
+= ks
->ks_use
[i
].inuse
;
987 for (i
= 0; i
< ncpus
; ++i
)
988 ttl
+= ks
->ks_use
[i
].memuse
;
991 ttl
= (ks
->ks_mgt
.npartial
+ ks
->ks_mgt
.nfull
+ ks
->ks_mgt
.nempty
) *
993 for (i
= 0; i
< ncpus
; ++i
) {
994 struct kmalloc_use
*kuse
= &ks
->ks_use
[i
];
996 if (kuse
->mgt
.active
&&
997 kuse
->mgt
.active
!= (void *)namelist
[X_KSLAB_DUMMY
].n_value
)
999 ttl
+= KMALLOC_SLAB_SIZE
;
1001 if (kuse
->mgt
.alternate
&&
1002 kuse
->mgt
.alternate
!= (void *)namelist
[X_KSLAB_DUMMY
].n_value
)
1004 ttl
+= KMALLOC_SLAB_SIZE
;
1009 for (i
= 0; i
< ncpus
; ++i
)
1010 ttl
+= ks
->ks_use
[i
].calls
;
1017 mcompare(const void *arg1
, const void *arg2
)
1019 const struct malloc_type
*m1
= arg1
;
1020 const struct malloc_type
*m2
= arg2
;
1024 total1
= cpuagg(m1
, KSMEMUSE
) + cpuagg(m1
, KSOBJUSE
);
1025 total2
= cpuagg(m2
, KSMEMUSE
) + cpuagg(m2
, KSOBJUSE
);
1026 if (total1
< total2
)
1028 if (total1
> total2
)
1036 struct malloc_type
*ks
;
1043 struct malloc_type kmemstats
[MAX_KMSTATS
], *kmsp
;
1049 kread(X_KMEMSTATISTICS
, &kmsp
, sizeof(kmsp
));
1050 for (nkms
= 0; nkms
< MAX_KMSTATS
&& kmsp
!= NULL
; nkms
++) {
1051 struct malloc_type
*ss
;
1053 ss
= &kmemstats
[nkms
];
1055 if (sizeof(kmemstats
[0]) != kvm_read(kd
, (u_long
)kmsp
, ss
,
1056 sizeof(kmemstats
[0])))
1058 err(1, "kvm_read(%p)", (void *)kmsp
);
1060 if (sizeof(buf
) != kvm_read(kd
, (u_long
)ss
->ks_shortdesc
,
1063 err(1, "kvm_read(%p)", kmemstats
[nkms
].ks_shortdesc
);
1065 buf
[sizeof(buf
) - 1] = '\0';
1066 ss
->ks_shortdesc
= strdup(buf
);
1072 usebytes
= ncpus
* sizeof(ss
->ks_use
[0]);
1073 use
= malloc(usebytes
);
1074 if (kvm_read(kd
, (u_long
)ss
->ks_use
, use
, usebytes
) !=
1077 err(1, "kvm_read(%p)", ss
->ks_use
);
1084 warnx("truncated to the first %d memory types", nkms
);
1090 qsort(kmemstats
, nkms
, sizeof(struct malloc_type
), mcompare
);
1097 "\nMemory statistics by type\n");
1098 printf("\t Type Count MemUse SlabUse Limit Requests\n");
1099 for (i
= 0, ks
= &kmemstats
[0]; i
< nkms
; i
++, ks
++) {
1106 ks_calls
= cpuagg(ks
, KSCALLS
);
1107 if (ks_calls
== 0 && verbose
== 0)
1110 ks_inuse
= cpuagg(ks
, KSINUSE
);
1111 ks_memuse
= cpuagg(ks
, KSMEMUSE
);
1112 ks_objuse
= cpuagg(ks
, KSOBJUSE
);
1114 snprintf(idbuf
, sizeof(idbuf
), "%s", ks
->ks_shortdesc
);
1115 for (n
= 0; idbuf
[n
]; ++n
) {
1116 if (idbuf
[n
] == ' ')
1120 printf("%19s %s %s %s %s %s\n",
1122 formatnum(ks_inuse
, 5, 1),
1123 formatnum(ks_memuse
, 5, 1),
1124 formatnum(ks_objuse
, 5, 1),
1125 formatnum(ks
->ks_limit
, 5, 1),
1126 formatnum(ks_calls
, 5, 1));
1128 totuse
+= ks_memuse
;
1129 totobj
+= ks_objuse
;
1132 printf("\nMemory Totals: In-Use Slab-Use Requests\n");
1133 printf(" %s %s %s\n",
1134 formatnum(totuse
, 5, 1),
1135 formatnum(totobj
, 5, 1),
1136 formatnum(totreq
, 5, 1));
1142 struct objcache_stats
*stat
, *s
;
1145 if (sysctlbyname("kern.objcache.stats", NULL
, &len
, NULL
, 0) < 0)
1146 errx(1, "objcache stats sysctl failed\n");
1148 /* Add some extra space. */
1149 stat
= malloc(len
+ (8 * sizeof(*stat
)));
1150 if (sysctlbyname("kern.objcache.stats", stat
, &len
, NULL
, 0) < 0)
1151 errx(1, "objcache stats sysctl failed\n");
1154 "\nObjcache statistics by name\n");
1155 printf(" Name Used Cached Limit Requests Allocs Fails Exhausts\n");
1156 for (s
= stat
, count
= 0; count
< len
; ++s
) {
1157 printf("%21s %s %s %s %s %s %s %s\n",
1159 formatnum(s
->oc_used
, 5, 1),
1160 formatnum(s
->oc_cached
, 5, 1),
1161 s
->oc_limit
< OBJCACHE_UNLIMITED
?
1162 formatnum(s
->oc_limit
, 5, 1) : "unlim",
1163 formatnum(s
->oc_requested
, 5, 1),
1164 formatnum(s
->oc_allocated
, 5, 1),
1165 formatnum(s
->oc_failed
, 4, 1),
1166 formatnum(s
->oc_exhausted
, 4, 1));
1168 count
+= sizeof(*s
);
1176 dozmem(u_int interval
, int reps
)
1180 struct vm_zone zone
;
1181 struct vm_zone save
[MAXSAVE
];
1192 bzero(save
, sizeof(save
));
1195 kread(X_ZLIST
, &zlist
, sizeof(zlist
));
1196 kz
= LIST_FIRST(&zlist
);
1200 if (kvm_read(kd
, (intptr_t)kz
, &zone
, sizeof(zone
)) !=
1201 (ssize_t
)sizeof(zone
)) {
1205 zfreecnt_prev
= save
[i
].zfreecnt
;
1206 znalloc_prev
= save
[i
].znalloc
;
1207 for (n
= 0; n
< ncpus
; ++n
) {
1208 zfreecnt_prev
+= save
[i
].zpcpu
[n
].zfreecnt
;
1209 znalloc_prev
+= save
[i
].zpcpu
[n
].znalloc
;
1212 zfreecnt_next
= zone
.zfreecnt
;
1213 znalloc_next
= zone
.znalloc
;
1214 for (n
= 0; n
< ncpus
; ++n
) {
1215 zfreecnt_next
+= zone
.zpcpu
[n
].zfreecnt
;
1216 znalloc_next
+= zone
.zpcpu
[n
].znalloc
;
1220 namesz
= sizeof(name
);
1221 if (kvm_readstr(kd
, (intptr_t)zone
.zname
, name
, &namesz
) == NULL
) {
1225 if (first
&& interval
) {
1227 } else if (zone
.zmax
) {
1228 printf("%-10s %9ld / %-9ld %5ldM used"
1231 (long)(zone
.ztotal
- zfreecnt_next
),
1233 (long)zone
.zpagecount
* 4096 / (1024 * 1024),
1234 (double)(zone
.ztotal
- zfreecnt_next
) *
1235 100.0 / (double)zone
.zmax
);
1237 printf("%-10s %9ld %5ldM used"
1240 (long)(zone
.ztotal
- zfreecnt_next
),
1241 (long)(zone
.ztotal
- zfreecnt_next
) *
1242 zone
.zsize
/ (1024 * 1024));
1245 printf("use=%ld\n", znalloc_next
- znalloc_prev
);
1246 } else if (interval
== 0)
1249 kz
= LIST_NEXT(&zone
, zlink
);
1255 usleep(interval
* 1000);
1263 * kread reads something from the kernel, given its nlist index.
1266 kread(int nlx
, void *addr
, size_t size
)
1270 if (namelist
[nlx
].n_type
== 0 || namelist
[nlx
].n_value
== 0) {
1271 sym
= namelist
[nlx
].n_name
;
1274 errx(1, "symbol %s not defined", sym
);
1276 if (kvm_read(kd
, namelist
[nlx
].n_value
, addr
, size
) != (ssize_t
)size
) {
1277 sym
= namelist
[nlx
].n_name
;
1280 errx(1, "%s: %s", sym
, kvm_geterr(kd
));
1287 fprintf(stderr
, "%s%s",
1288 "usage: vmstat [-imsuvz] [-c count] [-M core] "
1289 "[-N system] [-w wait]\n",
1290 " [-n devs] [disks]\n");