I'd like to introduce you to ITAppleEventCenter, our new facility for sending and...
[ITFoundation.git] / ITVirtualMemoryInfo.m
1 #import "ITVirtualMemoryInfo.h"
2
3 @interface ITVirtualMemoryInfo (Private)
4 - (BOOL)refreshStats;
5 @end
6
7 @implementation ITVirtualMemoryInfo
8
9 - (id)init
10 {
11     if ( ( self = [super init] ) ) {
12         if ([self refreshStats:&stat] == NO) {
13             return nil;
14         }
15     }
16     return self;
17 }
18
19 - (int)pageSize
20 {
21     int pageSize = 0;
22
23     if ( host_page_size(mach_host_self(), &pageSize) != KERN_SUCCESS ) {
24         NSLog(@"Failed to get page size, defaulting to 4096/4k");
25         pageSize = DEFAULT_PAGE_SIZE;
26     }
27     
28     return pageSize;
29 }
30
31 - (int)freePages
32 {
33     [self refreshStats:&stat];
34     return stat.free_count;
35 }
36
37 - (int)activePages
38 {
39     [self refreshStats:&stat];
40     return stat.active_count;
41 }
42
43 - (int)inactivePages
44 {
45     [self refreshStats:&stat];
46     return stat.inactive_count;
47 }
48
49 - (int)wiredPages
50 {
51     [self refreshStats:&stat];
52     return stat.wire_count;
53 }
54
55 - (int)faults
56 {
57     [self refreshStats:&stat];
58     return stat.faults;
59 }
60
61 - (int)copyOnWritePages
62 {
63     [self refreshStats:&stat];
64     return stat.cow_faults;
65 }
66
67 - (int)zeroFilledPages
68 {
69     [self refreshStats:&stat];
70     return stat.zero_fill_count;
71 }
72
73 - (int)reactivatedPages
74 {
75     [self refreshStats:&stat];
76     return stat.reactivations;
77 }
78
79 - (int)pageins
80 {
81     [self refreshStats:&stat];
82     return stat.pageins;
83 }
84
85 - (int)pageouts
86 {
87     [self refreshStats:&stat];
88     return stat.pageouts;
89 }
90
91 - (int)hits
92 {
93     [self refreshStats:&stat];
94     return stat.hits;
95 }
96
97 - (int)lookups
98 {
99     [self refreshStats:&stat];
100     return stat.lookups;
101 }
102
103 - (int)hitratePercentage
104 {
105     [self refreshStats:&stat];
106     if ( stat.lookups == 0 ) {
107         return 0;
108     } else {
109         return ( ( stat.hits * 100 ) / stat.lookups );
110     }
111 }
112
113 - (BOOL)refreshStats:(struct vm_statistics *)myStat
114 {
115     bzero(&myStat,sizeof(myStat));
116     mach_port_t myHost = mach_host_self();
117     int count = HOST_VM_INFO_COUNT;
118     NSLog(@"%i",count);
119     int returned = host_statistics(myHost, HOST_VM_INFO, myStat, &count);
120     if ( returned != KERN_SUCCESS ) {
121         NSLog(@"Failed to get Statistics in -refreshStats method of ITVirtualMemoryInfo");
122         NSLog(@"%s",strerror(returned));
123         return NO;
124     } else {
125         return YES;
126     }
127 }
128
129 @end