]> git.saurik.com Git - apple/system_cmds.git/blame - vm_stat.tproj/vm_stat.c
system_cmds-279.tar.gz
[apple/system_cmds.git] / vm_stat.tproj / vm_stat.c
CommitLineData
1815bff5
A
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
c3a08f59
A
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.
1815bff5
A
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,
c3a08f59
A
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.
1815bff5
A
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 <unistd.h>
45#include <stdio.h>
46
47#include <mach/mach.h>
48
49vm_statistics_data_t vm_stat, last;
50int percent;
51int delay;
52char *pgmname;
53mach_port_t myHost;
54int pageSize = 4096; /* set to 4k default */
55
56void usage();
57void banner();
58void snapshot();
59void pstat(char *str, int n);
60void print_stats();
61void get_stats(struct vm_statistics *stat);
62
63int
64main(argc, argv)
65 int argc;
66 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(0);
99}
100
101void
102usage()
103{
104 fprintf(stderr, "usage: %s [ repeat-interval ]\n", pgmname);
105 exit(1);
106}
107
108void
109banner()
110{
111 get_stats(&vm_stat);
112 printf("Mach Virtual Memory Statistics: ");
113 printf("(page size of %d bytes, cache hits %d%%)\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
129void
130snapshot()
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: %d hits of %d lookups (%d%% hit rate)\n",
148 vm_stat.hits, vm_stat.lookups, percent);
149}
150
151void
152pstat(str, n)
153 char *str;
154 int n;
155{
156 printf("%-25s %10d.\n", str, n);
157}
158
159void
160print_stats()
161{
162 static count = 0;
163
164 if (count++ == 0)
165 banner();
166
167 if (count > 20)
168 count = 0;
169
170 get_stats(&vm_stat);
171 printf("%6d %6d %4d %4d %8d %8d %8d %8d %8d %8d\n",
172 vm_stat.free_count,
173 vm_stat.active_count,
174 vm_stat.inactive_count,
175 vm_stat.wire_count,
176 vm_stat.faults - last.faults,
177 vm_stat.cow_faults - last.cow_faults,
178 vm_stat.zero_fill_count - last.zero_fill_count,
179 vm_stat.reactivations - last.reactivations,
180 vm_stat.pageins - last.pageins,
181 vm_stat.pageouts - last.pageouts);
182 last = vm_stat;
183}
184
185void
186get_stats(stat)
187 struct vm_statistics *stat;
188{
189 int count = HOST_VM_INFO_COUNT;
190 if (host_statistics(myHost, HOST_VM_INFO, stat,&count) != KERN_SUCCESS) {
191 fprintf(stderr, "%s: failed to get statistics.\n", pgmname);
192 exit(2);
193 }
194 if (stat->lookups == 0)
195 percent = 0;
196 else
197 percent = (stat->hits*100)/stat->lookups;
198}