]> git.saurik.com Git - apple/system_cmds.git/blame - vm_stat.tproj/vm_stat.c
system_cmds-279.6.1.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 *
d904471c
A
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
12 * this file.
1815bff5
A
13 *
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,
d904471c
A
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
1815bff5
A
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24/*
25 * File: vm_stat.c
26 * Author: Avadis Tevanian, Jr.
27 *
28 * Copyright (C) 1986, Avadis Tevanian, Jr.
29 *
30 *
31 * Display Mach VM statistics.
32 *
33 ************************************************************************
34 * HISTORY
35 * 6-Jun-86 Avadis Tevanian, Jr. (avie) at Carnegie-Mellon University
36 * Use official Mach interface.
37 *
38 * 25-mar-99 A.Ramesh at Apple
39 * Ported to MacOS X
40 ************************************************************************
41 */
42
43#include <unistd.h>
44#include <stdio.h>
45
46#include <mach/mach.h>
47
48vm_statistics_data_t vm_stat, last;
49int percent;
50int delay;
51char *pgmname;
52mach_port_t myHost;
53int pageSize = 4096; /* set to 4k default */
54
55void usage();
56void banner();
57void snapshot();
58void pstat(char *str, int n);
59void print_stats();
60void get_stats(struct vm_statistics *stat);
61
62int
63main(argc, argv)
64 int argc;
65 char *argv[];
66{
67
68 pgmname = argv[0];
69 delay = 0;
70
71
72 setlinebuf (stdout);
73
74 if (argc == 2) {
75 if (sscanf(argv[1], "%d", &delay) != 1)
76 usage();
77 if (delay < 0)
78 usage();
79 }
80
81 myHost = mach_host_self();
82
83 if(host_page_size(mach_host_self(), &pageSize) != KERN_SUCCESS) {
84 fprintf(stderr, "%s: failed to get pagesize; defaulting to 4K.\n", pgmname);
85 pageSize = 4096;
86 }
87
88 if (delay == 0) {
89 snapshot();
90 }
91 else {
92 while (1) {
93 print_stats();
94 sleep(delay);
95 }
96 }
97 exit(0);
98}
99
100void
101usage()
102{
103 fprintf(stderr, "usage: %s [ repeat-interval ]\n", pgmname);
104 exit(1);
105}
106
107void
108banner()
109{
110 get_stats(&vm_stat);
111 printf("Mach Virtual Memory Statistics: ");
112 printf("(page size of %d bytes, cache hits %d%%)\n",
113 pageSize, percent);
114 printf("%6s %6s %4s %4s %8s %8s %8s %8s %8s %8s\n",
115 "free",
116 "active",
117 "inac",
118 "wire",
119 "faults",
120 "copy",
121 "zerofill",
122 "reactive",
123 "pageins",
124 "pageout");
125 bzero(&last, sizeof(last));
126}
127
128void
129snapshot()
130{
131
132 get_stats(&vm_stat);
133 printf("Mach Virtual Memory Statistics: (page size of %d bytes)\n",
134 pageSize);
135
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: %d hits of %d lookups (%d%% hit rate)\n",
147 vm_stat.hits, vm_stat.lookups, percent);
148}
149
150void
151pstat(str, n)
152 char *str;
153 int n;
154{
155 printf("%-25s %10d.\n", str, n);
156}
157
158void
159print_stats()
160{
161 static count = 0;
162
163 if (count++ == 0)
164 banner();
165
166 if (count > 20)
167 count = 0;
168
169 get_stats(&vm_stat);
170 printf("%6d %6d %4d %4d %8d %8d %8d %8d %8d %8d\n",
171 vm_stat.free_count,
172 vm_stat.active_count,
173 vm_stat.inactive_count,
174 vm_stat.wire_count,
175 vm_stat.faults - last.faults,
176 vm_stat.cow_faults - last.cow_faults,
177 vm_stat.zero_fill_count - last.zero_fill_count,
178 vm_stat.reactivations - last.reactivations,
179 vm_stat.pageins - last.pageins,
180 vm_stat.pageouts - last.pageouts);
181 last = vm_stat;
182}
183
184void
185get_stats(stat)
186 struct vm_statistics *stat;
187{
188 int count = HOST_VM_INFO_COUNT;
189 if (host_statistics(myHost, HOST_VM_INFO, stat,&count) != KERN_SUCCESS) {
190 fprintf(stderr, "%s: failed to get statistics.\n", pgmname);
191 exit(2);
192 }
193 if (stat->lookups == 0)
194 percent = 0;
195 else
196 percent = (stat->hits*100)/stat->lookups;
197}