forked from MacFace/MacFace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HostStatistics.m
executable file
·257 lines (201 loc) · 7.33 KB
/
HostStatistics.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//
// HostStatistic.m
// face
//
// Created by rryu on Fri Apr 05 2002.
// Copyright (c) 2001 __MyCompanyName__. All rights reserved.
//
#import <mach/mach.h>
#import <mach/mach_types.h>
#import "HostStatistics.h"
@implementation HostStatistics
//
// カーネルのバージョン文字列を返す
//
+ (NSString*)kernelVersion
{
kernel_version_t kver;
host_kernel_version(mach_host_self(),kver);
return [NSString stringWithUTF8String:kver];
}
//
// 初期化
// capacity: リングバッファの容量
//
- (id)initWithCapacity:(unsigned)capacity
{
kern_return_t kr;
host_basic_info_data_t basic_info;
mach_msg_type_number_t basic_info_count;
hostPort = mach_host_self();
basic_info_count = HOST_BASIC_INFO_COUNT;
kr = host_info(hostPort, HOST_BASIC_INFO, (host_info_t)&basic_info, &basic_info_count);
processorCount = basic_info.avail_cpus;
kr = host_page_size(hostPort, &pageSize);
memoryHistory = calloc(sizeof(MemoryStats), capacity);
totalProcessorHistory = calloc(sizeof(ProcessorStats), capacity);
processorHistories = calloc(sizeof(ProcessorStats), capacity * processorCount);
bufMaxLen = capacity;
bufHead = 0;
bufTail = 0;
bufLen = 1;
[self setMemoryStats:&memoryHistory[0]];
[self setTotalProcessorTicks:&totalProcessorHistory[0]];
[self setAllProcessorTicks:&processorHistories[0]];
totalProcessorHistory[0].usage.user = 0;
totalProcessorHistory[0].usage.system = 0;
totalProcessorHistory[0].usage.idle = 100.0;
totalProcessorHistory[0].usage.nice = 0;
totalPages = memoryHistory[0].wirePages + memoryHistory[0].activePages + memoryHistory[0].inactivePages + memoryHistory[0].freePages;
minUsedPages = memoryHistory[0].wirePages + memoryHistory[0].activePages;
maxUsedPages = minUsedPages;
return self;
}
//
// 後始末
//
- (void)dealloc
{
free(memoryHistory);
free(totalProcessorHistory);
free(processorHistories);
[super dealloc];
}
- (unsigned long)pageSize { return pageSize; }
- (unsigned long)totalPages { return totalPages; }
- (unsigned long)minUsedPages { return minUsedPages; }
- (unsigned long)maxUsedPages { return maxUsedPages; }
- (int)processorCount { return processorCount; }
- (int)length { return bufLen; }
- (void)setMemoryStats:(MemoryStats*)data
{
kern_return_t kr;
vm_statistics_data_t vm_stat;
mach_msg_type_number_t vm_count = HOST_VM_INFO_COUNT;
kr = host_statistics(hostPort, HOST_VM_INFO, (host_info_t)&vm_stat, &vm_count);
data->freePages = vm_stat.free_count;
data->activePages = vm_stat.active_count;
data->inactivePages = vm_stat.inactive_count;
data->wirePages = vm_stat.wire_count;
data->faults = vm_stat.faults;
data->pageins = vm_stat.pageins;
data->pageouts = vm_stat.pageouts;
}
- (void)setTotalProcessorTicks:(ProcessorStats*)data
{
kern_return_t kr;
host_cpu_load_info_data_t load_info;
mach_msg_type_number_t load_info_count = HOST_CPU_LOAD_INFO_COUNT;
kr = host_statistics(hostPort, HOST_CPU_LOAD_INFO, (host_info_t)&load_info, &load_info_count);
data->ticks.user = load_info.cpu_ticks[CPU_STATE_USER];
data->ticks.system = load_info.cpu_ticks[CPU_STATE_SYSTEM];
data->ticks.idle = load_info.cpu_ticks[CPU_STATE_IDLE];
data->ticks.nice = load_info.cpu_ticks[CPU_STATE_NICE];
}
- (void)setAllProcessorTicks:(ProcessorStats*)data
{
kern_return_t kr;
processor_cpu_load_info_t cpu_load_info;
natural_t cpu_count;
mach_msg_type_number_t info_count;
int i;
kr = host_processor_info(hostPort, PROCESSOR_CPU_LOAD_INFO, &cpu_count, (processor_info_array_t*)&cpu_load_info, &info_count);
if (kr) {
mach_error("host_processor_info error:", kr);
return;
}
for (i = 0; i < processorCount; i++)
{
data[i].ticks.user = cpu_load_info[i].cpu_ticks[CPU_STATE_USER];
data[i].ticks.system = cpu_load_info[i].cpu_ticks[CPU_STATE_SYSTEM];
data[i].ticks.idle = cpu_load_info[i].cpu_ticks[CPU_STATE_IDLE];
data[i].ticks.nice = cpu_load_info[i].cpu_ticks[CPU_STATE_NICE];
}
vm_deallocate(mach_task_self(), (vm_address_t)cpu_load_info, info_count);
}
//
// 履歴の更新
//
- (void)update
{
int newHead;
int newLen;
int newTail;
int user,sys,idle,nice,total;
int usedPages;
MemoryStats *curMemStats, *lastMemStats;
ProcessorStats *curProcStats, *lastProcStats;
int i;
newHead = bufHead;
newTail = bufTail;
newLen = bufLen;
if (newLen < bufMaxLen) newLen++;
if (++newHead >= newLen) newHead = 0;
if (newHead == bufTail) {
if (++newTail >= newLen) newTail = 0;
}
[self setMemoryStats:&memoryHistory[newHead]];
[self setTotalProcessorTicks:&totalProcessorHistory[newHead]];
[self setAllProcessorTicks:&processorHistories[processorCount * newHead]];
curMemStats = &memoryHistory[newHead];
lastMemStats = &memoryHistory[bufHead];
curMemStats->pageinDelta = curMemStats->pageins - lastMemStats->pageins;
curMemStats->pageoutDelta = curMemStats->pageouts - lastMemStats->pageouts;
usedPages = curMemStats->wirePages + curMemStats->activePages;
if( minUsedPages < usedPages) minUsedPages = usedPages;
if( maxUsedPages > usedPages) maxUsedPages = usedPages;
curProcStats = &totalProcessorHistory[newHead];
lastProcStats = &totalProcessorHistory[bufHead];
user = curProcStats->ticks.user - lastProcStats->ticks.user;
sys = curProcStats->ticks.system - lastProcStats->ticks.system;
idle = curProcStats->ticks.idle - lastProcStats->ticks.idle;
nice = curProcStats->ticks.nice - lastProcStats->ticks.nice;
total = user + sys + idle + nice;
if (total > 0) {
curProcStats->usage.user = user * 100.0 / total;
curProcStats->usage.system = sys * 100.0 / total;
curProcStats->usage.idle = idle * 100.0 / total;
curProcStats->usage.nice = nice * 100.0 / total;
} else {
curProcStats->usage = lastProcStats->usage;
}
for (i = 0; i < processorCount; i++) {
curProcStats = &processorHistories[processorCount * newHead + i];
lastProcStats = &processorHistories[processorCount * bufHead + i];
user = curProcStats->ticks.user - lastProcStats->ticks.user;
sys = curProcStats->ticks.system - lastProcStats->ticks.system;
idle = curProcStats->ticks.idle - lastProcStats->ticks.idle;
nice = curProcStats->ticks.nice - lastProcStats->ticks.nice;
total = user + sys + idle + nice;
if (total > 0) {
curProcStats->usage.user = user * 100.0 / total;
curProcStats->usage.system = sys * 100.0 / total;
curProcStats->usage.idle = idle * 100.0 / total;
curProcStats->usage.nice = nice * 100.0 / total;
} else {
curProcStats->usage = lastProcStats->usage;
}
}
bufHead = newHead;
bufTail = newTail;
bufLen = newLen;
}
- (const MemoryStats*)memoryStatsIndexAt:(unsigned)index
{
if (index >= bufLen) return nil;
index = (index <= bufHead) ? bufHead - index : bufLen + bufHead - index;
return &memoryHistory[index];
}
- (const ProcessorStats*)totalProcessorUsageIndexAt:(unsigned)index
{
if (index >= bufLen) return nil;
index = (index <= bufHead) ? bufHead - index : bufLen + bufHead - index;
return &totalProcessorHistory[index];
}
- (const ProcessorStats*)processorOf:(int)number usageIndexAt:(unsigned)index
{
if (index >= bufLen) return nil;
index = (index <= bufHead) ? bufHead - index : bufLen + bufHead - index;
return &processorHistories[processorCount * index + number];
}
@end