]> git.saurik.com Git - apple/system_cmds.git/blame - vm_stat.tproj/vm_stat.c
system_cmds-336.20.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 *
2fc1e207
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,
2fc1e207
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
83f6dbe8
A
43#include <stddef.h>
44#include <stdlib.h>
1815bff5
A
45#include <unistd.h>
46#include <stdio.h>
47
48#include <mach/mach.h>
49
50vm_statistics_data_t vm_stat, last;
83f6dbe8 51natural_t percent;
1815bff5
A
52int delay;
53char *pgmname;
54mach_port_t myHost;
55int pageSize = 4096; /* set to 4k default */
56
83f6dbe8
A
57void usage(void);
58void banner(void);
59void snapshot(void);
60void pstat(char *str, natural_t n);
61void print_stats(void);
1815bff5
A
62void get_stats(struct vm_statistics *stat);
63
64int
83f6dbe8 65main(int argc, char *argv[])
1815bff5
A
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 }
83f6dbe8 97 exit(EXIT_SUCCESS);
1815bff5
A
98}
99
100void
83f6dbe8 101usage(void)
1815bff5
A
102{
103 fprintf(stderr, "usage: %s [ repeat-interval ]\n", pgmname);
83f6dbe8 104 exit(EXIT_FAILURE);
1815bff5
A
105}
106
107void
83f6dbe8 108banner(void)
1815bff5
A
109{
110 get_stats(&vm_stat);
111 printf("Mach Virtual Memory Statistics: ");
83f6dbe8 112 printf("(page size of %d bytes, cache hits %u%%)\n",
1815bff5
A
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
83f6dbe8 129snapshot(void)
1815bff5
A
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);
83f6dbe8 146 printf("Object cache: %u hits of %u lookups (%u%% hit rate)\n",
1815bff5
A
147 vm_stat.hits, vm_stat.lookups, percent);
148}
149
150void
83f6dbe8 151pstat(char *str, natural_t n)
1815bff5 152{
83f6dbe8 153 printf("%-25s %10u.\n", str, n);
1815bff5
A
154}
155
156void
83f6dbe8 157print_stats(void)
1815bff5 158{
83f6dbe8 159 static int count = 0;
1815bff5
A
160
161 if (count++ == 0)
162 banner();
163
164 if (count > 20)
165 count = 0;
166
167 get_stats(&vm_stat);
83f6dbe8 168 printf("%6u %6u %4u %4u %8u %8u %8u %8u %8u %8u\n",
1815bff5
A
169 vm_stat.free_count,
170 vm_stat.active_count,
171 vm_stat.inactive_count,
172 vm_stat.wire_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);
179 last = vm_stat;
180}
181
182void
83f6dbe8 183get_stats(struct vm_statistics *stat)
1815bff5
A
184{
185 int count = HOST_VM_INFO_COUNT;
83f6dbe8 186 if (host_statistics(myHost, HOST_VM_INFO, (host_info_t)stat, &count) != KERN_SUCCESS) {
1815bff5 187 fprintf(stderr, "%s: failed to get statistics.\n", pgmname);
83f6dbe8 188 exit(EXIT_FAILURE);
1815bff5
A
189 }
190 if (stat->lookups == 0)
191 percent = 0;
83f6dbe8
A
192 else {
193 /*
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 ...
199 */
200 percent = (natural_t)(((double)stat->hits*100)/stat->lookups);
201 }
1815bff5 202}