]> git.saurik.com Git - apple/system_cmds.git/blob - vm_stat.tproj/vm_stat.c
system_cmds-300.tar.gz
[apple/system_cmds.git] / vm_stat.tproj / vm_stat.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * File: vm_stat.c
27 * Author: Avadis Tevanian, Jr.
28 *
29 * Copyright (C) 1986, Avadis Tevanian, Jr.
30 *
31 *
32 * Display Mach VM statistics.
33 *
34 ************************************************************************
35 * HISTORY
36 * 6-Jun-86 Avadis Tevanian, Jr. (avie) at Carnegie-Mellon University
37 * Use official Mach interface.
38 *
39 * 25-mar-99 A.Ramesh at Apple
40 * Ported to MacOS X
41 ************************************************************************
42 */
43
44 #include <stddef.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <stdio.h>
48
49 #include <mach/mach.h>
50
51 vm_statistics_data_t vm_stat, last;
52 natural_t percent;
53 int delay;
54 char *pgmname;
55 mach_port_t myHost;
56 int pageSize = 4096; /* set to 4k default */
57
58 void usage(void);
59 void banner(void);
60 void snapshot(void);
61 void pstat(char *str, natural_t n);
62 void print_stats(void);
63 void get_stats(struct vm_statistics *stat);
64
65 int
66 main(int argc, char *argv[])
67 {
68
69 pgmname = argv[0];
70 delay = 0;
71
72
73 setlinebuf (stdout);
74
75 if (argc == 2) {
76 if (sscanf(argv[1], "%d", &delay) != 1)
77 usage();
78 if (delay < 0)
79 usage();
80 }
81
82 myHost = mach_host_self();
83
84 if(host_page_size(mach_host_self(), &pageSize) != KERN_SUCCESS) {
85 fprintf(stderr, "%s: failed to get pagesize; defaulting to 4K.\n", pgmname);
86 pageSize = 4096;
87 }
88
89 if (delay == 0) {
90 snapshot();
91 }
92 else {
93 while (1) {
94 print_stats();
95 sleep(delay);
96 }
97 }
98 exit(EXIT_SUCCESS);
99 }
100
101 void
102 usage(void)
103 {
104 fprintf(stderr, "usage: %s [ repeat-interval ]\n", pgmname);
105 exit(EXIT_FAILURE);
106 }
107
108 void
109 banner(void)
110 {
111 get_stats(&vm_stat);
112 printf("Mach Virtual Memory Statistics: ");
113 printf("(page size of %d bytes, cache hits %u%%)\n",
114 pageSize, percent);
115 printf("%6s %6s %4s %4s %8s %8s %8s %8s %8s %8s\n",
116 "free",
117 "active",
118 "inac",
119 "wire",
120 "faults",
121 "copy",
122 "zerofill",
123 "reactive",
124 "pageins",
125 "pageout");
126 bzero(&last, sizeof(last));
127 }
128
129 void
130 snapshot(void)
131 {
132
133 get_stats(&vm_stat);
134 printf("Mach Virtual Memory Statistics: (page size of %d bytes)\n",
135 pageSize);
136
137 pstat("Pages free:", vm_stat.free_count);
138 pstat("Pages active:", vm_stat.active_count);
139 pstat("Pages inactive:", vm_stat.inactive_count);
140 pstat("Pages wired down:", vm_stat.wire_count);
141 pstat("\"Translation faults\":", vm_stat.faults);
142 pstat("Pages copy-on-write:", vm_stat.cow_faults);
143 pstat("Pages zero filled:", vm_stat.zero_fill_count);
144 pstat("Pages reactivated:", vm_stat.reactivations);
145 pstat("Pageins:", vm_stat.pageins);
146 pstat("Pageouts:", vm_stat.pageouts);
147 printf("Object cache: %u hits of %u lookups (%u%% hit rate)\n",
148 vm_stat.hits, vm_stat.lookups, percent);
149 }
150
151 void
152 pstat(char *str, natural_t n)
153 {
154 printf("%-25s %10u.\n", str, n);
155 }
156
157 void
158 print_stats(void)
159 {
160 static int count = 0;
161
162 if (count++ == 0)
163 banner();
164
165 if (count > 20)
166 count = 0;
167
168 get_stats(&vm_stat);
169 printf("%6u %6u %4u %4u %8u %8u %8u %8u %8u %8u\n",
170 vm_stat.free_count,
171 vm_stat.active_count,
172 vm_stat.inactive_count,
173 vm_stat.wire_count,
174 vm_stat.faults - last.faults,
175 vm_stat.cow_faults - last.cow_faults,
176 vm_stat.zero_fill_count - last.zero_fill_count,
177 vm_stat.reactivations - last.reactivations,
178 vm_stat.pageins - last.pageins,
179 vm_stat.pageouts - last.pageouts);
180 last = vm_stat;
181 }
182
183 void
184 get_stats(struct vm_statistics *stat)
185 {
186 int count = HOST_VM_INFO_COUNT;
187 if (host_statistics(myHost, HOST_VM_INFO, (host_info_t)stat, &count) != KERN_SUCCESS) {
188 fprintf(stderr, "%s: failed to get statistics.\n", pgmname);
189 exit(EXIT_FAILURE);
190 }
191 if (stat->lookups == 0)
192 percent = 0;
193 else {
194 /*
195 * We have limited precision with the 32-bit natural_t fields
196 * in the vm_statistics structure. There's nothing we can do
197 * about counter overflows, but we can avoid percentage
198 * calculation overflows by doing the computation in floating
199 * point arithmetic ...
200 */
201 percent = (natural_t)(((double)stat->hits*100)/stat->lookups);
202 }
203 }