2 * Copyright (c) 1999 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
40 ************************************************************************
48 #include <mach/mach.h>
50 vm_statistics_data_t vm_stat
, last
;
55 vm_size_t pageSize
= 4096; /* set to 4k default */
60 void pstat(char *str
, natural_t n
);
61 void print_stats(void);
62 void get_stats(struct vm_statistics
*stat
);
65 main(int argc
, char *argv
[])
75 if (sscanf(argv
[1], "%d", &delay
) != 1)
81 myHost
= mach_host_self();
83 if(host_page_size(mach_host_self(), &pageSize
) != KERN_SUCCESS
) {
84 fprintf(stderr
, "%s: failed to get pagesize; defaulting to 4K.\n", pgmname
);
103 fprintf(stderr
, "usage: %s [ repeat-interval ]\n", pgmname
);
111 printf("Mach Virtual Memory Statistics: ");
112 printf("(page size of %d bytes, cache hits %u%%)\n",
114 printf("%6s %6s %4s %4s %8s %8s %8s %8s %8s %8s\n",
125 bzero(&last
, sizeof(last
));
133 printf("Mach Virtual Memory Statistics: (page size of %d bytes)\n",
136 pstat("Pages free:", vm_stat
.free_count
);
137 pstat("Pages active:", vm_stat
.active_count
);
138 pstat("Pages inactive:", vm_stat
.inactive_count
);
139 pstat("Pages wired down:", vm_stat
.wire_count
);
140 pstat("\"Translation faults\":", vm_stat
.faults
);
141 pstat("Pages copy-on-write:", vm_stat
.cow_faults
);
142 pstat("Pages zero filled:", vm_stat
.zero_fill_count
);
143 pstat("Pages reactivated:", vm_stat
.reactivations
);
144 pstat("Pageins:", vm_stat
.pageins
);
145 pstat("Pageouts:", vm_stat
.pageouts
);
146 printf("Object cache: %u hits of %u lookups (%u%% hit rate)\n",
147 vm_stat
.hits
, vm_stat
.lookups
, percent
);
151 pstat(char *str
, natural_t n
)
153 printf("%-25s %10u.\n", str
, n
);
159 static int count
= 0;
168 printf("%6u %6u %4u %4u %8u %8u %8u %8u %8u %8u\n",
170 vm_stat
.active_count
,
171 vm_stat
.inactive_count
,
173 vm_stat
.faults
- last
.faults
,
174 vm_stat
.cow_faults
- last
.cow_faults
,
175 vm_stat
.zero_fill_count
- last
.zero_fill_count
,
176 vm_stat
.reactivations
- last
.reactivations
,
177 vm_stat
.pageins
- last
.pageins
,
178 vm_stat
.pageouts
- last
.pageouts
);
183 get_stats(struct vm_statistics
*stat
)
185 unsigned int count
= HOST_VM_INFO_COUNT
;
186 if (host_statistics(myHost
, HOST_VM_INFO
, (host_info_t
)stat
, &count
) != KERN_SUCCESS
) {
187 fprintf(stderr
, "%s: failed to get statistics.\n", pgmname
);
190 if (stat
->lookups
== 0)
194 * We have limited precision with the 32-bit natural_t fields
195 * in the vm_statistics structure. There's nothing we can do
196 * about counter overflows, but we can avoid percentage
197 * calculation overflows by doing the computation in floating
198 * point arithmetic ...
200 percent
= (natural_t
)(((double)stat
->hits
*100)/stat
->lookups
);