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