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 ************************************************************************
51 #include <mach/mach.h>
53 vm_statistics64_data_t vm_stat
, last
;
58 vm_size_t pageSize
= 4096; /* set to 4k default */
62 void sspstat(char *str
, uint64_t n
);
64 void print_stats(void);
65 void get_stats(vm_statistics64_t stat
);
67 void pstat(uint64_t n
, int width
);
70 main(int argc
, char *argv
[])
80 if (sscanf(argv
[1], "%d", &delay
) != 1)
86 myHost
= mach_host_self();
88 if(host_page_size(mach_host_self(), &pageSize
) != KERN_SUCCESS
) {
89 fprintf(stderr
, "%s: failed to get pagesize; defaulting to 4K.\n", pgmname
);
108 fprintf(stderr
, "usage: %s [ repeat-interval ]\n", pgmname
);
117 printf("Mach Virtual Memory Statistics: (page size of %d bytes)\n",
120 sspstat("Pages free:", (uint64_t) (vm_stat
.free_count
- vm_stat
.speculative_count
));
121 sspstat("Pages active:", (uint64_t) (vm_stat
.active_count
));
122 sspstat("Pages inactive:", (uint64_t) (vm_stat
.inactive_count
));
123 sspstat("Pages speculative:", (uint64_t) (vm_stat
.speculative_count
));
124 sspstat("Pages wired down:", (uint64_t) (vm_stat
.wire_count
));
125 sspstat("\"Translation faults\":", (uint64_t) (vm_stat
.faults
));
126 sspstat("Pages copy-on-write:", (uint64_t) (vm_stat
.cow_faults
));
127 sspstat("Pages zero filled:", (uint64_t) (vm_stat
.zero_fill_count
));
128 sspstat("Pages reactivated:", (uint64_t) (vm_stat
.reactivations
));
129 sspstat("Pageins:", (uint64_t) (vm_stat
.pageins
));
130 sspstat("Pageouts:", (uint64_t) (vm_stat
.pageouts
));
131 #if defined(__ppc__) /* vm_statistics are still 32-bit on ppc */
132 printf("Object cache: %u hits of %u lookups (%u%% hit rate)\n",
134 printf("Object cache: %llu hits of %llu lookups (%u%% hit rate)\n",
136 vm_stat
.hits
, vm_stat
.lookups
, percent
);
141 sspstat(char *str
, uint64_t n
)
143 printf("%-25s %16llu.\n", str
, n
);
150 printf("Mach Virtual Memory Statistics: ");
151 printf("(page size of %d bytes, cache hits %u%%)\n",
152 (int) (pageSize
), percent
);
153 printf("%6s %6s %6s %8s %6s %8s %8s %8s %8s %8s %8s\n",
165 bzero(&last
, sizeof(last
));
171 static int count
= 0;
180 pstat((uint64_t) (vm_stat
.free_count
- vm_stat
.speculative_count
), 6);
181 pstat((uint64_t) (vm_stat
.active_count
), 6);
182 pstat((uint64_t) (vm_stat
.speculative_count
), 6);
183 pstat((uint64_t) (vm_stat
.inactive_count
), 8);
184 pstat((uint64_t) (vm_stat
.wire_count
), 6);
185 pstat((uint64_t) (vm_stat
.faults
- last
.faults
), 8);
186 pstat((uint64_t) (vm_stat
.cow_faults
- last
.cow_faults
), 8);
187 pstat((uint64_t) (vm_stat
.zero_fill_count
- last
.zero_fill_count
), 8);
188 pstat((uint64_t) (vm_stat
.reactivations
- last
.reactivations
), 8);
189 pstat((uint64_t) (vm_stat
.pageins
- last
.pageins
), 8);
190 pstat((uint64_t) (vm_stat
.pageouts
- last
.pageouts
), 8);
196 pstat(uint64_t n
, int width
)
199 if (width
>= sizeof(buf
)) {
200 width
= sizeof(buf
) -1;
203 unsigned long long nb
= n
* (unsigned long long)pageSize
;
205 /* Now that we have the speculative field, there is really not enough
206 space, but we were actually overflowing three or four fields before
207 anyway. So any field that overflows we drop some insignifigant
208 digets and slap on the appropriate suffix
210 int w
= snprintf(buf
, sizeof(buf
), "%*llu", width
, n
);
212 w
= snprintf(buf
, sizeof(buf
), "%*lluK", width
-1, n
/ 1000);
214 w
= snprintf(buf
, sizeof(buf
), "%*lluM", width
-1, n
/ 1000000);
216 w
= snprintf(buf
, sizeof(buf
), "%*lluG", width
-1, n
/ 1000000000);
225 get_stats(vm_statistics64_t stat
)
227 unsigned int count
= HOST_VM_INFO64_COUNT
;
228 if (host_statistics64(myHost
, HOST_VM_INFO64
, (host_info64_t
)stat
, &count
) != KERN_SUCCESS
) {
229 fprintf(stderr
, "%s: failed to get statistics.\n", pgmname
);
232 if (stat
->lookups
== 0)
236 * We have limited precision with the 32-bit natural_t fields
237 * in the vm_statistics structure. There's nothing we can do
238 * about counter overflows, but we can avoid percentage
239 * calculation overflows by doing the computation in floating
240 * point arithmetic ...
242 percent
= (natural_t
)(((double)stat
->hits
*100)/stat
->lookups
);