-
Notifications
You must be signed in to change notification settings - Fork 7
/
cpuhistory-multiproc.patch
362 lines (333 loc) · 11.1 KB
/
cpuhistory-multiproc.patch
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
From 4d2001fee99e46d5dc2099dee89cec68430e7fa6 Mon Sep 17 00:00:00 2001
From: Daniel Sandler <dsandler@river.cs.rice.edu>
Date: Sun, 27 Jan 2008 00:00:32 -0600
Subject: [PATCH] Implement multiple CPU support. Tested on MacBook (Core Duo) only.
---
CPUInfo.h | 11 +++--
CPUInfo.m | 87 ++++++++++++++++++++++----------------
MainController.m | 123 +++++++++++++++++++++++++++++++-----------------------
3 files changed, 127 insertions(+), 94 deletions(-)
diff --git a/CPUInfo.h b/CPUInfo.h
index a96f491..be3ae43 100644
--- a/CPUInfo.h
+++ b/CPUInfo.h
@@ -25,10 +25,10 @@ typedef struct cpudata {
} CPUData, *CPUDataPtr;
@interface CPUInfo : NSObject {
- processor_info_array_t lastProcessorInfo;
+ processor_cpu_load_info_t lastProcessorInfo;
mach_msg_type_number_t numLastProcessorInfo;
unsigned numCPUs;
- CPUDataPtr cpudata;
+ CPUDataPtr *allcpudata;
int size;
int inptr;
int outptr;
@@ -36,10 +36,11 @@ typedef struct cpudata {
- (CPUInfo *)initWithCapacity:(unsigned)numItems;
- (void)refresh;
+- (unsigned)numCPUs;
- (void)startIterate;
-- (BOOL)getNext:(CPUDataPtr)ptr;
-- (void)getCurrent:(CPUDataPtr)ptr;
-- (void)getLast:(CPUDataPtr)ptr;
+- (BOOL)getNext:(CPUDataPtr)ptr forCPU:(unsigned)cpu;
+- (void)getCurrent:(CPUDataPtr)ptr forCPU:(unsigned)cpu;
+- (void)getLast:(CPUDataPtr)ptr forCPU:(unsigned)cpu;
- (int)getSize;
@end
diff --git a/CPUInfo.m b/CPUInfo.m
index 77d935d..ad1a659 100644
--- a/CPUInfo.m
+++ b/CPUInfo.m
@@ -37,35 +37,34 @@
*/
- (CPUInfo *) initWithCapacity:(unsigned)numItems
{
-
+ unsigned i;
+
/*
from Hosey's CPU Usage.app:
*/
//We could get the number of processors the same way that we get the CPU usage info, but that allocates memory.
-/* enum { miblen = 2U };
+ enum { miblen = 2U };
int mib[miblen] = { CTL_HW, HW_NCPU };
size_t sizeOfNumCPUs = sizeof(numCPUs);
int status = sysctl(mib, miblen,
&numCPUs, &sizeOfNumCPUs,
-*/ /*newp*/ // NULL, /*newlen*/ 0U);
-// if(status != 0) {
+ NULL, /*newlen*/ 0U);
+ if(status != 0) {
numCPUs = 1; // TODO we're going to assume one CPU for the moment.
-// NSLog(@"%s error status, assuming one CPU", _cmd);
-// }
-
-
-
-
-
+ NSLog(@"%s error status, assuming one CPU", _cmd);
+ }
self = [super init];
size = numItems;
- cpudata = calloc(numItems, sizeof(CPUData));
- if (cpudata == NULL) {
+ allcpudata = calloc(numCPUs, sizeof(CPUDataPtr));
+ if (allcpudata == NULL) {
NSLog (@"%s Failed to allocate buffer for CPUInfo", _cmd);
return (nil);
}
+ for(i = 0; i < numCPUs; i++) {
+ allcpudata[i] = calloc(numItems, sizeof(CPUData));
+ }
// Set up our data structure ptrs
inptr = 0;
outptr = -1;
@@ -96,11 +95,16 @@
*/
- (void)refresh
{
- processor_info_array_t processorInfo;
- natural_t numProcessors_nobodyCares = 0U;
+ processor_cpu_load_info_t processorInfo;
+ natural_t numProcessors;
mach_msg_type_number_t numProcessorInfo;
+ unsigned i;
- kern_return_t err = host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, (natural_t *)&numProcessors_nobodyCares, (processor_info_array_t *)&processorInfo, (mach_msg_type_number_t *)&numProcessorInfo);
+ kern_return_t err = host_processor_info(mach_host_self(),
+ PROCESSOR_CPU_LOAD_INFO,
+ (natural_t *)&numProcessors,
+ (processor_info_array_t *)&processorInfo,
+ (mach_msg_type_number_t *)&numProcessorInfo);
if(err != KERN_SUCCESS) {
NSLog(@"%s failed to get cpu statistics", _cmd);
}
@@ -108,23 +112,30 @@
/*
TODO make this multicore. First, we're gonna need a multicore machine to test it on.
*/
- // for(unsigned i = 0U; i < numCPUs; ++i)
-
- unsigned int inUse, total, user, sys, nice, idle;
-
- user = processorInfo[CPU_STATE_USER] - lastProcessorInfo[CPU_STATE_USER];
- sys = processorInfo[CPU_STATE_SYSTEM] - lastProcessorInfo[CPU_STATE_SYSTEM];
- nice = processorInfo[CPU_STATE_NICE] - lastProcessorInfo[CPU_STATE_NICE];
- idle = processorInfo[CPU_STATE_IDLE] - lastProcessorInfo[CPU_STATE_IDLE];
- inUse = user + sys + nice;
- total = inUse + idle;
-
- cpudata[inptr].user = (double)user / (double)total;
- cpudata[inptr].sys = (double)sys / (double)total;
- cpudata[inptr].nice = (double)nice / (double)total;
- cpudata[inptr].idle = (double)idle / (double)total;
+ for(i = 0U; i < numCPUs; ++i) {
+ // NB: numCPUs ought to be the same as numProcessors -- otherwise ...?
+ assert(numCPUs == numProcessors);
+ assert(numProcessorInfo == numProcessors * CPU_STATE_MAX);
+
+ CPUDataPtr cpudata = allcpudata[i];
+ unsigned int inUse, total, user, sys, nice, idle;
+
+ user = processorInfo[i].cpu_ticks[CPU_STATE_USER] - lastProcessorInfo[i].cpu_ticks[CPU_STATE_USER];
+ sys = processorInfo[i].cpu_ticks[CPU_STATE_SYSTEM] - lastProcessorInfo[i].cpu_ticks[CPU_STATE_SYSTEM];
+ nice = processorInfo[i].cpu_ticks[CPU_STATE_NICE] - lastProcessorInfo[i].cpu_ticks[CPU_STATE_NICE];
+ idle = processorInfo[i].cpu_ticks[CPU_STATE_IDLE] - lastProcessorInfo[i].cpu_ticks[CPU_STATE_IDLE];
+
+ inUse = user + sys + nice;
+ total = inUse + idle;
+
+ cpudata[inptr].user = (double)user / (double)total;
+ cpudata[inptr].sys = (double)sys / (double)total;
+ cpudata[inptr].nice = (double)nice / (double)total;
+ cpudata[inptr].idle = (double)idle / (double)total;
+ NSLog(@"CPU %d: IDLE: %f\n", i, (double)idle / (double)total);
+ }
// deallocate the last one, since we don't want memory leaks.
/*
@@ -149,11 +160,11 @@
}
-- (BOOL)getNext:(CPUDataPtr)ptr
+- (BOOL)getNext:(CPUDataPtr)ptr forCPU:(unsigned)cpu
{
if (outptr == -1)
return (FALSE);
- *ptr = cpudata[outptr++];
+ *ptr = allcpudata[cpu][outptr++];
if (outptr >= size)
outptr = 0;
if (outptr == inptr)
@@ -162,15 +173,15 @@
}
-- (void)getCurrent:(CPUDataPtr)ptr
+- (void)getCurrent:(CPUDataPtr)ptr forCPU:(unsigned)cpu
{
- *ptr = cpudata[inptr ? inptr - 1 : size - 1];
+ *ptr = allcpudata[cpu][inptr ? inptr - 1 : size - 1];
}
-- (void)getLast:(CPUDataPtr)ptr
+- (void)getLast:(CPUDataPtr)ptr forCPU:(unsigned)cpu
{
- *ptr = cpudata[inptr > 1 ? inptr - 2 : size + inptr - 2];
+ *ptr = allcpudata[cpu][inptr > 1 ? inptr - 2 : size + inptr - 2];
}
@@ -179,4 +190,6 @@
return (size);
}
+- (unsigned)numCPUs { return (numCPUs); }
+
@end
diff --git a/MainController.m b/MainController.m
index 403dbe5..5a5e911 100644
--- a/MainController.m
+++ b/MainController.m
@@ -61,38 +61,49 @@
{
CPUData cpudata;
-
+ unsigned cpu;
+ float height = GRAPH_SIZE / [cpuInfo numCPUs];
+ float width = GRAPH_SIZE;
int x;
- float y, yy;
+ float y, yy = 0;
int barWidth = (int)[[preferences objectForKey:BAR_WIDTH_SIZE_KEY] floatValue];
// double interval = 0.1 * [[preferences objectForKey:UPDATE_FREQUENCY_KEY] floatValue];
[graphImage lockFocus];
// draw the cpu usage graph
+
[cpuInfo startIterate];
- // for (x = 0; [memInfo getNext:&vmdata]; x++) {
- for (x = 0; [cpuInfo getNext:&cpudata]; x+=barWidth) {
-
- // y += vmdata.active * GRAPH_SIZE;
- y = cpudata.sys * GRAPH_SIZE;
- [[preferences objectForKey:SYS_COLOR_KEY] set];
- NSRectFill (NSMakeRect(x - barWidth, 0.0, x, y));
- yy = y;
- // y += vmdata.inactive * GRAPH_SIZE;
- y += cpudata.nice * GRAPH_SIZE;
- [[preferences objectForKey:NICE_COLOR_KEY] set];
- NSRectFill (NSMakeRect(x - barWidth, yy, x, y));
- // y = vmdata.wired * GRAPH_SIZE;
- yy = y;
-
- y += cpudata.user * GRAPH_SIZE;
- [[preferences objectForKey:USER_COLOR_KEY] set];
- NSRectFill (NSMakeRect(x - barWidth, yy, x, y));
-
- // free data here
+ for (cpu = 0; cpu < [cpuInfo numCPUs]; cpu++ ) {
+ yy = cpu * height;
+
[[preferences objectForKey:IDLE_COLOR_KEY] set];
- NSRectFill (NSMakeRect(x - barWidth, y, x, GRAPH_SIZE));
+ NSRectFill(NSMakeRect(0, yy, width, yy + height));
+
+ // for (x = 0; [memInfo getNext:&vmdata]; x++) {
+ for (x = 0; [cpuInfo getNext:&cpudata forCPU:cpu]; x+=barWidth) {
+ // y += vmdata.active * GRAPH_SIZE;
+ y = yy + cpudata.sys * height;
+ [[preferences objectForKey:SYS_COLOR_KEY] set];
+ NSRectFill (NSMakeRect(x - barWidth, yy, x, y));
+ yy = y;
+ // y += vmdata.inactive * GRAPH_SIZE;
+ y += cpudata.nice * height;
+ [[preferences objectForKey:NICE_COLOR_KEY] set];
+ NSRectFill (NSMakeRect(x - barWidth, yy, x, y));
+ // y = vmdata.wired * GRAPH_SIZE;
+ yy = y;
+
+ y += cpudata.user * height;
+ [[preferences objectForKey:USER_COLOR_KEY] set];
+ NSRectFill (NSMakeRect(x - barWidth, yy, x, y));
+ yy = y;
+
+ // free data here
+ y += cpudata.idle * height;
+ [[preferences objectForKey:IDLE_COLOR_KEY] set];
+ NSRectFill (NSMakeRect(x - barWidth, yy, x, y));
+ }
}
// transfer graph image to icon image
@@ -110,7 +121,10 @@
// VMData vmdata, vmdata0;
CPUData cpudata, cpudata0;
int barWidth = (int)[[preferences objectForKey:BAR_WIDTH_SIZE_KEY] floatValue];
- float y, yy;
+ float y, yy = 0;
+ unsigned cpu;
+ float height = GRAPH_SIZE / [cpuInfo numCPUs];
+ float width = GRAPH_SIZE;
// double interval = 0.1 * [[preferences objectForKey:UPDATE_FREQUENCY_KEY] floatValue];
@@ -119,34 +133,39 @@
// offset the old graph image
[graphImage compositeToPoint:NSMakePoint(-barWidth, 0) operation:NSCompositeCopy];
- // [memInfo getLast:&vmdata0];
- [cpuInfo getLast:&cpudata0];
- // [memInfo getCurrent:&vmdata];
- [cpuInfo getCurrent:&cpudata];
-
- // draw chronological graph into graph image
-
- // y += vmdata.active * GRAPH_SIZE;
- y = cpudata.sys * GRAPH_SIZE;
- [[preferences objectForKey:SYS_COLOR_KEY] set];
- NSRectFill (NSMakeRect(GRAPH_SIZE - barWidth, 0.0, GRAPH_SIZE - barWidth, y));
- yy = y;
-
- // y += vmdata.inactive * GRAPH_SIZE;
- y += cpudata.nice * GRAPH_SIZE;
- [[preferences objectForKey:NICE_COLOR_KEY] set];
- NSRectFill (NSMakeRect(GRAPH_SIZE - barWidth, yy, GRAPH_SIZE - barWidth, y));
- yy = y;
-
- // y = vmdata.wired * GRAPH_SIZE;
- y += cpudata.user * GRAPH_SIZE;
- [[preferences objectForKey:USER_COLOR_KEY] set];
- NSRectFill (NSMakeRect(GRAPH_SIZE - barWidth, yy, GRAPH_SIZE - barWidth, y));
-
- // free data here
- [[preferences objectForKey:IDLE_COLOR_KEY] set];
- NSRectFill (NSMakeRect(GRAPH_SIZE - barWidth, y, GRAPH_SIZE - barWidth, GRAPH_SIZE));
-
+ for (cpu = 0; cpu < [cpuInfo numCPUs]; cpu++ ) {
+ yy = cpu * height;
+
+ // [memInfo getLast:&vmdata0];
+ [cpuInfo getLast:&cpudata0 forCPU:cpu];
+ // [memInfo getCurrent:&vmdata];
+ [cpuInfo getCurrent:&cpudata forCPU:cpu];
+
+ // draw chronological graph into graph image
+
+ // y += vmdata.active * GRAPH_SIZE;
+ y = yy;
+ [[preferences objectForKey:SYS_COLOR_KEY] set];
+ NSRectFill (NSMakeRect(width - barWidth, yy, width - barWidth, y));
+ yy = y;
+
+ // y += vmdata.inactive * GRAPH_SIZE;
+ y += cpudata.nice * height;
+ [[preferences objectForKey:NICE_COLOR_KEY] set];
+ NSRectFill (NSMakeRect(width - barWidth, yy, width - barWidth, y));
+ yy = y;
+
+ // y = vmdata.wired * GRAPH_SIZE;
+ y += cpudata.user * height;
+ [[preferences objectForKey:USER_COLOR_KEY] set];
+ NSRectFill (NSMakeRect(width - barWidth, yy, width - barWidth, y));
+ yy = y;
+
+ // free data here
+ y += cpudata.idle * height;
+ [[preferences objectForKey:IDLE_COLOR_KEY] set];
+ NSRectFill (NSMakeRect(width - barWidth, yy, width - barWidth, y));
+ }
// transfer graph image to icon image
[graphImage unlockFocus];
--
1.5.2.4