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