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
33 * @(#) Copyright (c) 1980, 1986, 1991, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)vmstat.c 8.1 (Berkeley) 6/6/93
35 * $FreeBSD: src/usr.bin/vmstat/vmstat.c,v 1.38.2.4 2001/07/31 19:52:41 tmm Exp $
36 * $DragonFly: src/usr.bin/vmstat/vmstat.c,v 1.20 2006/01/28 17:18:48 y0netan1 Exp $
39 #define _KERNEL_STRUCTURES
41 #include <sys/param.h>
45 #include <sys/namei.h>
46 #include <sys/malloc.h>
47 #include <sys/signal.h>
48 #include <sys/fcntl.h>
49 #include <sys/ioctl.h>
50 #include <sys/sysctl.h>
51 #include <sys/vmmeter.h>
53 #include <vm/vm_param.h>
71 static struct nlist namelist
[] = {
73 { "_boottime", 0, 0, 0, 0 },
75 { "_nchstats", 0, 0, 0, 0 },
76 #define X_KMEMSTATISTICS 2
77 { "_kmemstatistics", 0, 0, 0, 0 },
79 { "_zlist", 0, 0, 0, 0 },
82 { "_deficit", 0, 0, 0, 0 },
84 { "_forkstat", 0, 0, 0, 0 },
86 { "_rectime", 0, 0, 0, 0 },
88 { "_pgintime", 0, 0, 0, 0 },
90 { "_xstats", 0, 0, 0, 0 },
98 struct statinfo cur
, last
;
99 int num_devices
, maxshowdevs
;
101 struct device_selection
*dev_select
;
103 struct devstat_match
*matches
;
105 int num_devices_specified
, num_selections
;
106 long select_generation
;
107 char **specified_devices
;
108 devstat_select_mode select_mode
;
110 struct vmmeter vmm
, ovmm
;
111 struct vmstats vms
, ovms
;
118 struct kinfo_cputime cp_time
, old_cp_time
, diff_cp_time
;
120 #define FORKSTAT 0x01
121 #define INTRSTAT 0x02
124 #define TIMESTAT 0x10
126 #define ZMEMSTAT 0x40
128 static void cpustats(void);
129 static void dointr(void);
130 static void domem(void);
131 static void dosum(void);
132 static void dozmem(void);
133 static void dovmstat(u_int
, int);
134 static void kread(int, void *, size_t);
135 static void usage(void);
136 static char **getdrivedata(char **);
137 static long getuptime(void);
138 static void needhdr(int);
139 static long pct(long, long);
142 static void dotimes(void); /* Not implemented */
143 static void doforkst(void);
145 static void printhdr(void);
146 static void devstats(void);
149 main(int argc
, char **argv
)
155 char errbuf
[_POSIX2_LINE_MAX
];
157 memf
= nlistf
= NULL
;
158 interval
= reps
= todo
= 0;
160 while ((c
= getopt(argc
, argv
, "c:fiM:mN:n:p:stw:z")) != -1) {
169 errx(EX_USAGE
, "sorry, -f is not (re)implemented yet");
186 maxshowdevs
= atoi(optarg
);
188 errx(1, "number of devices %d is < 0",
192 if (buildmatch(optarg
, &matches
, &num_matches
) != 0)
193 errx(1, "%s", devstat_errbuf
);
202 errx(EX_USAGE
, "sorry, -t is not (re)implemented yet");
206 interval
= atoi(optarg
);
222 * Discard setgid privileges if not the running kernel so that bad
223 * guys can't print interesting stuff from kernel memory.
225 if (nlistf
!= NULL
|| memf
!= NULL
)
228 kd
= kvm_openfiles(nlistf
, memf
, NULL
, O_RDONLY
, errbuf
);
230 errx(1, "kvm_openfiles: %s", errbuf
);
232 if ((c
= kvm_nlist(kd
, namelist
)) != 0) {
234 warnx("undefined symbols:");
235 for (c
= 0; c
< (int)__arysize(namelist
); c
++)
236 if (namelist
[c
].n_type
== 0)
237 fprintf(stderr
, " %s",
241 warnx("kvm_nlist: %s", kvm_geterr(kd
));
246 struct winsize winsize
;
249 * Make sure that the userland devstat version matches the
250 * kernel devstat version. If not, exit and print a
251 * message informing the user of his mistake.
253 if (checkversion() < 0)
254 errx(1, "%s", devstat_errbuf
);
257 argv
= getdrivedata(argv
);
259 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, (char *)&winsize
);
260 if (winsize
.ws_row
> 0)
261 winlines
= winsize
.ws_row
;
265 #define BACKWARD_COMPATIBILITY
266 #ifdef BACKWARD_COMPATIBILITY
268 interval
= atoi(*argv
);
297 dovmstat(interval
, reps
);
302 getdrivedata(char **argv
)
304 if ((num_devices
= getnumdevs()) < 0)
305 errx(1, "%s", devstat_errbuf
);
307 cur
.dinfo
= (struct devinfo
*)malloc(sizeof(struct devinfo
));
308 last
.dinfo
= (struct devinfo
*)malloc(sizeof(struct devinfo
));
309 bzero(cur
.dinfo
, sizeof(struct devinfo
));
310 bzero(last
.dinfo
, sizeof(struct devinfo
));
312 if (getdevs(&cur
) == -1)
313 errx(1, "%s", devstat_errbuf
);
315 num_devices
= cur
.dinfo
->numdevs
;
316 generation
= cur
.dinfo
->generation
;
318 specified_devices
= (char **)malloc(sizeof(char *));
319 for (num_devices_specified
= 0; *argv
; ++argv
) {
322 num_devices_specified
++;
323 specified_devices
= (char **)realloc(specified_devices
,
325 num_devices_specified
);
326 specified_devices
[num_devices_specified
- 1] = *argv
;
330 if (nflag
== 0 && maxshowdevs
< num_devices_specified
)
331 maxshowdevs
= num_devices_specified
;
334 * People are generally only interested in disk statistics when
335 * they're running vmstat. So, that's what we're going to give
336 * them if they don't specify anything by default. We'll also give
337 * them any other random devices in the system so that we get to
338 * maxshowdevs devices, if that many devices exist. If the user
339 * specifies devices on the command line, either through a pattern
340 * match or by naming them explicitly, we will give the user only
343 if ((num_devices_specified
== 0) && (num_matches
== 0)) {
344 if (buildmatch("da", &matches
, &num_matches
) != 0)
345 errx(1, "%s", devstat_errbuf
);
347 select_mode
= DS_SELECT_ADD
;
349 select_mode
= DS_SELECT_ONLY
;
352 * At this point, selectdevs will almost surely indicate that the
353 * device list has changed, so we don't look for return values of 0
354 * or 1. If we get back -1, though, there is an error.
356 if (selectdevs(&dev_select
, &num_selected
, &num_selections
,
357 &select_generation
, generation
, cur
.dinfo
->devices
,
358 num_devices
, matches
, num_matches
, specified_devices
,
359 num_devices_specified
, select_mode
,
360 maxshowdevs
, 0) == -1)
361 errx(1, "%s", devstat_errbuf
);
369 static time_t now
, boottime
;
373 kread(X_BOOTTIME
, &boottime
, sizeof(boottime
));
375 uptime
= now
- boottime
;
376 if (uptime
<= 0 || uptime
> 60*60*24*365*10)
377 errx(1, "time makes no sense; namelist must be wrong");
384 dovmstat(u_int interval
, int reps
)
386 struct vmtotal total
;
387 time_t uptime
, halfuptime
;
388 struct devinfo
*tmp_dinfo
;
389 int vmm_size
= sizeof(vmm
);
390 int vms_size
= sizeof(vms
);
391 int vmt_size
= sizeof(total
);
393 uptime
= getuptime();
394 halfuptime
= uptime
/ 2;
395 signal(SIGCONT
, needhdr
);
400 if (kinfo_get_sched_cputime(&cp_time
))
401 err(1, "kinfo_get_sched_cputime");
403 tmp_dinfo
= last
.dinfo
;
404 last
.dinfo
= cur
.dinfo
;
405 cur
.dinfo
= tmp_dinfo
;
406 last
.busy_time
= cur
.busy_time
;
409 * Here what we want to do is refresh our device stats.
410 * getdevs() returns 1 when the device list has changed.
411 * If the device list has changed, we want to go through
412 * the selection process again, in case a device that we
413 * were previously displaying has gone away.
415 switch (getdevs(&cur
)) {
417 errx(1, "%s", devstat_errbuf
);
422 num_devices
= cur
.dinfo
->numdevs
;
423 generation
= cur
.dinfo
->generation
;
425 retval
= selectdevs(&dev_select
, &num_selected
,
426 &num_selections
, &select_generation
,
427 generation
, cur
.dinfo
->devices
,
428 num_devices
, matches
, num_matches
,
430 num_devices_specified
, select_mode
,
434 errx(1, "%s", devstat_errbuf
);
447 if (sysctlbyname("vm.vmstats", &vms
, &vms_size
, NULL
, 0)) {
448 perror("sysctlbyname: vm.vmstats");
451 if (sysctlbyname("vm.vmmeter", &vmm
, &vmm_size
, NULL
, 0)) {
452 perror("sysctlbyname: vm.vmmeter");
455 if (sysctlbyname("vm.vmtotal", &total
, &vmt_size
, NULL
, 0)) {
456 perror("sysctlbyname: vm.vmtotal");
459 printf("%2d %1d %1d",
460 total
.t_rq
- 1, total
.t_dw
+ total
.t_pw
, total
.t_sw
);
461 #define vmstat_pgtok(a) ((a) * vms.v_page_size >> 10)
462 #define rate(x) (((x) + halfuptime) / uptime) /* round */
463 printf(" %7ld %6ld ",
464 (long)vmstat_pgtok(total
.t_avm
), (long)vmstat_pgtok(total
.t_free
));
466 (u_long
)rate(vmm
.v_vm_faults
- ovmm
.v_vm_faults
));
468 (u_long
)rate(vmm
.v_reactivated
- ovmm
.v_reactivated
));
470 (u_long
)rate(vmm
.v_swapin
+ vmm
.v_vnodein
-
471 (ovmm
.v_swapin
+ ovmm
.v_vnodein
)));
473 (u_long
)rate(vmm
.v_swapout
+ vmm
.v_vnodeout
-
474 (ovmm
.v_swapout
+ ovmm
.v_vnodeout
)));
476 (u_long
)rate(vmm
.v_tfree
- ovmm
.v_tfree
));
478 (u_long
)rate(vmm
.v_pdpages
- ovmm
.v_pdpages
));
480 printf("%4lu %4lu %3lu ",
481 (u_long
)rate(vmm
.v_intr
- ovmm
.v_intr
),
482 (u_long
)rate(vmm
.v_syscall
- ovmm
.v_syscall
),
483 (u_long
)rate(vmm
.v_swtch
- ovmm
.v_swtch
));
487 if (reps
>= 0 && --reps
<= 0)
492 * We round upward to avoid losing low-frequency events
493 * (i.e., >= 1 per interval but < 1 per second).
496 halfuptime
= (uptime
+ 1) / 2;
508 num_shown
= (num_selected
< maxshowdevs
) ? num_selected
: maxshowdevs
;
509 printf(" procs memory page%*s", 19, "");
511 printf(" disks %*s", num_shown
* 4 - 7, "");
512 else if (num_shown
== 1)
514 printf(" faults cpu\n");
515 printf(" r b w avm fre flt re pi po fr sr ");
516 for (i
= 0; i
< num_devices
; i
++)
517 if ((dev_select
[i
].selected
)
518 && (dev_select
[i
].selected
<= maxshowdevs
))
519 printf("%c%c%d ", dev_select
[i
].device_name
[0],
520 dev_select
[i
].device_name
[1],
521 dev_select
[i
].unit_number
);
522 printf(" in sy cs us sy id\n");
523 hdrcnt
= winlines
- 2;
527 * Force a header to be prepended to the next output.
530 needhdr(__unused
int signo
)
537 pct(long top
, long bot
)
543 ans
= (quad_t
)top
* 100 / bot
;
547 #define PCT(top, bot) pct((long)(top), (long)(bot))
552 struct nchstats
*nch_tmp
, nchstats
;
553 int vms_size
= sizeof(vms
);
554 int vmm_size
= sizeof(vmm
);
557 size_t nch_size
= sizeof(struct nchstats
) * SMP_MAXCPU
;
559 if (sysctlbyname("vm.vmstats", &vms
, &vms_size
, NULL
, 0)) {
560 perror("sysctlbyname: vm.vmstats");
563 if (sysctlbyname("vm.vmmeter", &vmm
, &vmm_size
, NULL
, 0)) {
564 perror("sysctlbyname: vm.vmstats");
567 printf("%9u cpu context switches\n", vmm
.v_swtch
);
568 printf("%9u device interrupts\n", vmm
.v_intr
);
569 printf("%9u software interrupts\n", vmm
.v_soft
);
570 printf("%9u traps\n", vmm
.v_trap
);
571 printf("%9u system calls\n", vmm
.v_syscall
);
572 printf("%9u kernel threads created\n", vmm
.v_kthreads
);
573 printf("%9u fork() calls\n", vmm
.v_forks
);
574 printf("%9u vfork() calls\n", vmm
.v_vforks
);
575 printf("%9u rfork() calls\n", vmm
.v_rforks
);
576 printf("%9u exec() calls\n", vmm
.v_exec
);
577 printf("%9u swap pager pageins\n", vmm
.v_swapin
);
578 printf("%9u swap pager pages paged in\n", vmm
.v_swappgsin
);
579 printf("%9u swap pager pageouts\n", vmm
.v_swapout
);
580 printf("%9u swap pager pages paged out\n", vmm
.v_swappgsout
);
581 printf("%9u vnode pager pageins\n", vmm
.v_vnodein
);
582 printf("%9u vnode pager pages paged in\n", vmm
.v_vnodepgsin
);
583 printf("%9u vnode pager pageouts\n", vmm
.v_vnodeout
);
584 printf("%9u vnode pager pages paged out\n", vmm
.v_vnodepgsout
);
585 printf("%9u page daemon wakeups\n", vmm
.v_pdwakeups
);
586 printf("%9u pages examined by the page daemon\n", vmm
.v_pdpages
);
587 printf("%9u pages reactivated\n", vmm
.v_reactivated
);
588 printf("%9u copy-on-write faults\n", vmm
.v_cow_faults
);
589 printf("%9u copy-on-write optimized faults\n", vmm
.v_cow_optim
);
590 printf("%9u zero fill pages zeroed\n", vmm
.v_zfod
);
591 printf("%9u zero fill pages prezeroed\n", vmm
.v_ozfod
);
592 printf("%9u intransit blocking page faults\n", vmm
.v_intrans
);
593 printf("%9u total VM faults taken\n", vmm
.v_vm_faults
);
594 printf("%9u pages affected by kernel thread creation\n", vmm
.v_kthreadpages
);
595 printf("%9u pages affected by fork()\n", vmm
.v_forkpages
);
596 printf("%9u pages affected by vfork()\n", vmm
.v_vforkpages
);
597 printf("%9u pages affected by rfork()\n", vmm
.v_rforkpages
);
598 printf("%9u pages freed\n", vmm
.v_tfree
);
599 printf("%9u pages freed by daemon\n", vmm
.v_dfree
);
600 printf("%9u pages freed by exiting processes\n", vmm
.v_pfree
);
601 printf("%9u pages active\n", vms
.v_active_count
);
602 printf("%9u pages inactive\n", vms
.v_inactive_count
);
603 printf("%9u pages in VM cache\n", vms
.v_cache_count
);
604 printf("%9u pages wired down\n", vms
.v_wire_count
);
605 printf("%9u pages free\n", vms
.v_free_count
);
606 printf("%9u bytes per page\n", vms
.v_page_size
);
608 if ((nch_tmp
= malloc(nch_size
)) == NULL
) {
612 if (sysctlbyname("vfs.cache.nchstats", nch_tmp
, &nch_size
, NULL
, 0)) {
613 perror("sysctlbyname vfs.cache.nchstats");
617 if ((nch_tmp
= realloc(nch_tmp
, nch_size
)) == NULL
) {
624 cpucnt
= nch_size
/ sizeof(struct nchstats
);
625 kvm_nch_cpuagg(nch_tmp
, &nchstats
, cpucnt
);
627 nchtotal
= nchstats
.ncs_goodhits
+ nchstats
.ncs_neghits
+
628 nchstats
.ncs_badhits
+ nchstats
.ncs_falsehits
+
629 nchstats
.ncs_miss
+ nchstats
.ncs_long
;
630 printf("%9ld total name lookups\n", nchtotal
);
632 "%9s cache hits (%ld%% pos + %ld%% neg) system %ld%% per-directory\n",
633 "", PCT(nchstats
.ncs_goodhits
, nchtotal
),
634 PCT(nchstats
.ncs_neghits
, nchtotal
),
635 PCT(nchstats
.ncs_pass2
, nchtotal
));
636 printf("%9s deletions %ld%%, falsehits %ld%%, toolong %ld%%\n", "",
637 PCT(nchstats
.ncs_badhits
, nchtotal
),
638 PCT(nchstats
.ncs_falsehits
, nchtotal
),
639 PCT(nchstats
.ncs_long
, nchtotal
));
649 kread(X_FORKSTAT
, &fks
, sizeof(struct forkstat
));
650 printf("%d forks, %d pages, average %.2f\n",
651 fks
.cntfork
, fks
.sizfork
, (double)fks
.sizfork
/ fks
.cntfork
);
652 printf("%d vforks, %d pages, average %.2f\n",
653 fks
.cntvfork
, fks
.sizvfork
, (double)fks
.sizvfork
/ fks
.cntvfork
);
661 long double transfers_per_second
;
662 long double busy_seconds
;
664 diff_cp_time
.cp_user
= cp_time
.cp_user
- old_cp_time
.cp_user
;
665 diff_cp_time
.cp_nice
= cp_time
.cp_nice
- old_cp_time
.cp_nice
;
666 diff_cp_time
.cp_sys
= cp_time
.cp_sys
- old_cp_time
.cp_sys
;
667 diff_cp_time
.cp_intr
= cp_time
.cp_intr
- old_cp_time
.cp_intr
;
668 diff_cp_time
.cp_idle
= cp_time
.cp_idle
- old_cp_time
.cp_idle
;
669 old_cp_time
= cp_time
;
671 busy_seconds
= compute_etime(cur
.busy_time
, last
.busy_time
);
673 for (dn
= 0; dn
< num_devices
; dn
++) {
676 if ((dev_select
[dn
].selected
== 0)
677 || (dev_select
[dn
].selected
> maxshowdevs
))
680 di
= dev_select
[dn
].position
;
682 if (compute_stats(&cur
.dinfo
->devices
[di
],
683 &last
.dinfo
->devices
[di
], busy_seconds
,
685 NULL
, &transfers_per_second
, NULL
,
687 errx(1, "%s", devstat_errbuf
);
689 printf("%3.0Lf ", transfers_per_second
);
699 total
= diff_cp_time
.cp_user
+ diff_cp_time
.cp_nice
+
700 diff_cp_time
.cp_sys
+ diff_cp_time
.cp_intr
+ diff_cp_time
.cp_idle
;
703 totusage
= 100.0 / total
;
707 (diff_cp_time
.cp_user
+ diff_cp_time
.cp_nice
) * totusage
);
709 (diff_cp_time
.cp_sys
+ diff_cp_time
.cp_intr
) * totusage
);
711 diff_cp_time
.cp_idle
* totusage
);
717 u_long
*intrcnt
, uptime
;
719 size_t nintr
, inamlen
, i
, size
;
724 uptime
= getuptime();
725 if (sysctlbyname("hw.intrnames", NULL
, &inamlen
, NULL
, 0) != 0)
726 errx(1, "sysctlbyname");
727 intrstr
= malloc(inamlen
);
730 sysctlbyname("hw.intrnames", intrstr
, &inamlen
, NULL
, 0);
731 for (nintr
= 0, i
= 0; i
< inamlen
; ++i
) {
735 intrname
= malloc(nintr
* sizeof(char *));
736 for (i
= 0; i
< nintr
; ++i
) {
737 intrname
[i
] = intrstr
;
738 intrstr
+= strlen(intrstr
) + 1;
741 size
= nintr
* sizeof(*intrcnt
);
742 intrcnt
= calloc(nintr
, sizeof(*intrcnt
));
745 sysctlbyname("hw.intrcnt", intrcnt
, &size
, NULL
, 0);
748 for (i
= 0; i
< nintr
; ++i
) {
749 if (nwidth
< (int)strlen(intrname
[i
]))
750 nwidth
= (int)strlen(intrname
[i
]);
753 printf("interrupt total rate\n");
755 for (i
= 0; i
< nintr
; ++i
) {
756 if (intrcnt
[i
] || strncmp(intrname
[i
], "irq", 3) != 0) {
757 printf("%-*.*s %11lu %10lu\n",
758 nwidth
, nwidth
, intrname
[i
],
759 intrcnt
[i
], intrcnt
[i
] / uptime
);
761 inttotal
+= intrcnt
[i
];
763 printf("%-*.*s %11llu %10llu\n",
764 nwidth
, nwidth
, "Total",
765 inttotal
, inttotal
/ (u_int64_t
) uptime
);
768 #define MAX_KMSTATS 200
776 for (i
= ttl
= 0; i
< SMP_MAXCPU
; ++i
)
784 struct malloc_type
*ks
;
787 long totuse
= 0, totfree
= 0, totreq
= 0;
788 struct malloc_type kmemstats
[MAX_KMSTATS
], *kmsp
;
791 kread(X_KMEMSTATISTICS
, &kmsp
, sizeof(kmsp
));
792 for (nkms
= 0; nkms
< MAX_KMSTATS
&& kmsp
!= NULL
; nkms
++) {
793 if (sizeof(kmemstats
[0]) != kvm_read(kd
, (u_long
)kmsp
,
794 &kmemstats
[nkms
], sizeof(kmemstats
[0])))
795 err(1, "kvm_read(%p)", (void *)kmsp
);
796 if (sizeof(buf
) != kvm_read(kd
,
797 (u_long
)kmemstats
[nkms
].ks_shortdesc
, buf
, sizeof(buf
)))
798 err(1, "kvm_read(%p)",
799 kmemstats
[nkms
].ks_shortdesc
);
800 buf
[sizeof(buf
) - 1] = '\0';
801 kmemstats
[nkms
].ks_shortdesc
= strdup(buf
);
802 kmsp
= kmemstats
[nkms
].ks_next
;
805 warnx("truncated to the first %d memory types", nkms
);
808 "\nMemory statistics by type Type Kern\n");
810 " Type InUse MemUse HighUse Limit Requests Limit Limit Size(s)\n");
811 for (i
= 0, ks
= &kmemstats
[0]; i
< nkms
; i
++, ks
++) {
812 if (ks
->ks_calls
== 0)
814 printf("%13s%6ld%6ldK%7ldK%7ldK%9lld%5u%6u",
816 cpuagg(ks
->ks_inuse
), (cpuagg(ks
->ks_memuse
) + 1023) / 1024,
817 (ks
->ks_maxused
+ 1023) / 1024,
818 (ks
->ks_limit
+ 1023) / 1024, ks
->ks_calls
,
819 ks
->ks_limblocks
, ks
->ks_mapblocks
);
821 for (j
= 1 << MINBUCKET
; j
< 1 << (MINBUCKET
+ 16); j
<<= 1) {
822 if ((ks
->ks_size
& j
) == 0)
835 totuse
+= cpuagg(ks
->ks_memuse
);
836 totreq
+= ks
->ks_calls
;
838 printf("\nMemory Totals: In Use Free Requests\n");
839 printf(" %7ldK %6ldK %8ld\n",
840 (totuse
+ 1023) / 1024, (totfree
+ 1023) / 1024, totreq
);
852 if ((buf
= realloc(buf
, bufsize
)) == NULL
)
854 if (sysctlbyname("vm.zone", buf
, &bufsize
, 0, NULL
) == 0)
860 buf
[bufsize
] = '\0'; /* play it safe */
861 printf("%s\n\n", buf
);
866 * kread reads something from the kernel, given its nlist index.
869 kread(int nlx
, void *addr
, size_t size
)
873 if (namelist
[nlx
].n_type
== 0 || namelist
[nlx
].n_value
== 0) {
874 sym
= namelist
[nlx
].n_name
;
877 errx(1, "symbol %s not defined", sym
);
879 if (kvm_read(kd
, namelist
[nlx
].n_value
, addr
, size
) != (ssize_t
)size
) {
880 sym
= namelist
[nlx
].n_name
;
883 errx(1, "%s: %s", sym
, kvm_geterr(kd
));
890 fprintf(stderr
, "%s%s",
891 "usage: vmstat [-imsz] [-c count] [-M core] [-N system] [-w wait]\n",
892 " [-n devs] [disks]\n");