2 * Copyright (c) 1999-2009 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
26 * Author: Avadis Tevanian, Jr.
28 * Copyright (C) 1986, Avadis Tevanian, Jr.
31 * Display Mach VM statistics.
33 ************************************************************************
35 * 6-Jun-86 Avadis Tevanian, Jr. (avie) at Carnegie-Mellon University
36 * Use official Mach interface.
38 * 25-mar-99 A.Ramesh at Apple
41 * 22-Jan-09 R.Branche at Apple
42 * Changed some fields to 64-bit to alleviate overflows
43 ************************************************************************
52 #include <mach/mach.h>
53 #include <mach/vm_page_size.h>
55 vm_statistics64_data_t vm_stat
, last
;
61 void sspstat(char *str
, uint64_t n
);
63 void print_stats(void);
64 void get_stats(vm_statistics64_t stat
);
66 void pstat(uint64_t n
, int width
);
69 main(int argc
, char *argv
[])
80 while ((c
= getopt (argc
, argv
, "c:")) != -1) {
83 count
= (int)strtol(optarg
, NULL
, 10);
85 warnx("count must be positive");
95 argc
-= optind
; argv
+= optind
;
98 delay
= strtod(argv
[0], NULL
);
101 } else if (argc
> 1) {
105 myHost
= mach_host_self();
111 for (int i
= 1; i
< count
|| count
== 0; i
++ ){
112 usleep((int)(delay
* USEC_PER_SEC
));
122 fprintf(stderr
, "usage: %s [[-c count] interval]\n", pgmname
);
131 printf("Mach Virtual Memory Statistics: (page size of %llu bytes)\n", (mach_vm_size_t
)vm_kernel_page_size
);
133 sspstat("Pages free:", (uint64_t) (vm_stat
.free_count
- vm_stat
.speculative_count
));
134 sspstat("Pages active:", (uint64_t) (vm_stat
.active_count
));
135 sspstat("Pages inactive:", (uint64_t) (vm_stat
.inactive_count
));
136 sspstat("Pages speculative:", (uint64_t) (vm_stat
.speculative_count
));
137 sspstat("Pages throttled:", (uint64_t) (vm_stat
.throttled_count
));
138 sspstat("Pages wired down:", (uint64_t) (vm_stat
.wire_count
));
139 sspstat("Pages purgeable:", (uint64_t) (vm_stat
.purgeable_count
));
140 sspstat("\"Translation faults\":", (uint64_t) (vm_stat
.faults
));
141 sspstat("Pages copy-on-write:", (uint64_t) (vm_stat
.cow_faults
));
142 sspstat("Pages zero filled:", (uint64_t) (vm_stat
.zero_fill_count
));
143 sspstat("Pages reactivated:", (uint64_t) (vm_stat
.reactivations
));
144 sspstat("Pages purged:", (uint64_t) (vm_stat
.purges
));
145 sspstat("File-backed pages:", (uint64_t) (vm_stat
.external_page_count
));
146 sspstat("Anonymous pages:", (uint64_t) (vm_stat
.internal_page_count
));
147 sspstat("Pages stored in compressor:", (uint64_t) (vm_stat
.total_uncompressed_pages_in_compressor
));
148 sspstat("Pages occupied by compressor:", (uint64_t) (vm_stat
.compressor_page_count
));
149 sspstat("Decompressions:", (uint64_t) (vm_stat
.decompressions
));
150 sspstat("Compressions:", (uint64_t) (vm_stat
.compressions
));
151 sspstat("Pageins:", (uint64_t) (vm_stat
.pageins
));
152 sspstat("Pageouts:", (uint64_t) (vm_stat
.pageouts
));
153 sspstat("Swapins:", (uint64_t) (vm_stat
.swapins
));
154 sspstat("Swapouts:", (uint64_t) (vm_stat
.swapouts
));
158 sspstat(char *str
, uint64_t n
)
160 printf("%-30s %16llu.\n", str
, n
);
167 printf("Mach Virtual Memory Statistics: ");
168 printf("(page size of %llu bytes)\n", (mach_vm_size_t
)vm_kernel_page_size
);
169 printf("%8s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s %11s %9s %8s %8s %8s %8s %8s %8s %8s %8s\n",
192 bzero(&last
, sizeof(last
));
198 static int count
= 0;
207 pstat((uint64_t) (vm_stat
.free_count
- vm_stat
.speculative_count
), 8);
208 pstat((uint64_t) (vm_stat
.active_count
), 8);
209 pstat((uint64_t) (vm_stat
.speculative_count
), 8);
210 pstat((uint64_t) (vm_stat
.inactive_count
), 8);
211 pstat((uint64_t) (vm_stat
.throttled_count
), 8);
212 pstat((uint64_t) (vm_stat
.wire_count
), 8);
213 pstat((uint64_t) (vm_stat
.purgeable_count
), 8);
214 pstat((uint64_t) (vm_stat
.faults
- last
.faults
), 8);
215 pstat((uint64_t) (vm_stat
.cow_faults
- last
.cow_faults
), 8);
216 pstat((uint64_t) (vm_stat
.zero_fill_count
- last
.zero_fill_count
), 8);
217 pstat((uint64_t) (vm_stat
.reactivations
- last
.reactivations
), 8);
218 pstat((uint64_t) (vm_stat
.purges
- last
.purges
), 8);
219 pstat((uint64_t) (vm_stat
.external_page_count
), 11);
220 pstat((uint64_t) (vm_stat
.internal_page_count
), 9);
221 pstat((uint64_t) (vm_stat
.total_uncompressed_pages_in_compressor
), 8);
222 pstat((uint64_t) (vm_stat
.compressor_page_count
), 8);
223 pstat((uint64_t) (vm_stat
.decompressions
- last
.decompressions
), 8);
224 pstat((uint64_t) (vm_stat
.compressions
- last
.compressions
), 8);
225 pstat((uint64_t) (vm_stat
.pageins
- last
.pageins
), 8);
226 pstat((uint64_t) (vm_stat
.pageouts
- last
.pageouts
), 8);
227 pstat((uint64_t) (vm_stat
.swapins
- last
.swapins
), 8);
228 pstat((uint64_t) (vm_stat
.swapouts
- last
.swapouts
), 8);
234 pstat(uint64_t n
, int width
)
237 if (width
>= sizeof(buf
)) {
238 width
= sizeof(buf
) -1;
241 /* Now that we have the speculative field, there is really not enough
242 space, but we were actually overflowing three or four fields before
243 anyway. So any field that overflows we drop some insignifigant
244 digets and slap on the appropriate suffix
246 int w
= snprintf(buf
, sizeof(buf
), "%*llu", width
, n
);
248 w
= snprintf(buf
, sizeof(buf
), "%*lluK", width
-1, n
/ 1000);
250 w
= snprintf(buf
, sizeof(buf
), "%*lluM", width
-1, n
/ 1000000);
252 w
= snprintf(buf
, sizeof(buf
), "%*lluG", width
-1, n
/ 1000000000);
261 get_stats(vm_statistics64_t stat
)
263 unsigned int count
= HOST_VM_INFO64_COUNT
;
265 if ((ret
= host_statistics64(myHost
, HOST_VM_INFO64
, (host_info64_t
)stat
, &count
) != KERN_SUCCESS
)) {
266 fprintf(stderr
, "%s: failed to get statistics. error %d\n", pgmname
, ret
);