forked from mh0rst/turionpowercontrol
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscaler.cpp
More file actions
435 lines (284 loc) · 11.3 KB
/
Copy pathscaler.cpp
File metadata and controls
435 lines (284 loc) · 11.3 KB
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#include "scaler.h"
//TODO: IMPORTANT ********* Scaler must be completely revised
Scaler::Scaler(class Processor *prc) {
processor = prc;
processor->setNode(0);
processor->setCore(0);
samplingRate = DEFAULT_SAMPLING_RATE;
policy = POLICY_STEP;
upperThreshold = 70;
lowerThreshold = 20;
midUpperThreshold = (100 + upperThreshold) >> 1;
midLowerThreshold = (100 - lowerThreshold) >> 1;
PState ps(0);
ps = processor->getMaximumPState();
slowestPowerState = ps.getPState();
}
void Scaler::setSamplingFrequency(int msSmpFreq) {
samplingRate = msSmpFreq;
}
void Scaler::setPolicy(int policy) {
this->policy = policy;
}
void Scaler::setUpperThreshold(int thres) {
if (thres > 100)
upperThreshold = 100;
else
upperThreshold = thres;
midUpperThreshold = (100 + upperThreshold) >> 1;
}
void Scaler::setLowerThreshold(int thres) {
if (thres < 0)
lowerThreshold = 0;
else
lowerThreshold = thres;
midLowerThreshold = (100 - lowerThreshold) >> 1;
}
/* Scaling methods:
STEP - Stepped scaling, means that when CPU usage goes over 70% for a core,
the scaler brings the core at the immediate faster pstate.
When processor usage goes below 20% for a core, the scaler brings the core
at the immediate slower pstate.
ROCKET - If CPU core usage goes above 70%, core is set to full frequency. If
cpu core usage goes below 20%, core is immediately set to slower pstate.
DYNAMIC - If CPU core usage is 100%, core is set to full frequency. If CPU
core usage is above 85%, core is set a faster pstate to two step ahead.
If CPU core usage is above 70%, core is set to the immediate next faster
pstate.
If cpu core usage is 0%, core is set to slowest frequency. If CPU core
usage is below 10%, core is set to a slower pstate two step backward.
If CPU usage is below 20%, core is set to one step backward.
*/
int Scaler::initializeCounters() {
DWORD cpuIndex, nodeId, coreId;
PROCESSORMASK cpuMask;
unsigned int perfCounterSlot;
try {
this->processor->setNode(this->processor->ALL_NODES);
this->processor->setCore(this->processor->ALL_CORES);
cpuMask = this->processor->getMask();
/* We do this to do some "caching" of the mask, instead of calculating each time
we need to retrieve the time stamp counter */
// Allocating space for previous values of counters.
this->prevPerfCounters = (uint64_t *) calloc(
this->processor->getProcessorCores() * this->processor->getProcessorNodes(),
sizeof(uint64_t));
this->prevTSCCounters = (uint64_t *) calloc(
this->processor->getProcessorCores() * this->processor->getProcessorNodes(),
sizeof(uint64_t));
// MSR Object to retrieve the time stamp counter for all the nodes and all the processors
this->tscCounter = new MSRObject();
//Creates a new performance counter, for now we set slot 0, but we will
//use the findAvailable slot method to find an available method to be used
this->perfCounter = new PerformanceCounter(cpuMask, 0, this->processor->getMaxSlots());
//Event 0x76 is Idle Counter
perfCounter->setEventSelect(0x76);
perfCounter->setCountOsMode(true);
perfCounter->setCountUserMode(true);
perfCounter->setCounterMask(0);
perfCounter->setEdgeDetect(false);
perfCounter->setEnableAPICInterrupt(false);
perfCounter->setInvertCntMask(false);
perfCounter->setUnitMask(0);
//Finds an available slot for our purpose
perfCounterSlot = this->perfCounter->findAvailableSlot();
//findAvailableSlot() returns -2 in case of error
if (perfCounterSlot == 0xfffffffe)
throw "unable to access performance counter slots";
//findAvailableSlot() returns -1 in case there aren't available slots
if (perfCounterSlot == 0xffffffff)
throw "unable to find an available performance counter slot";
printf("Performance counter will use slot #%d\n", perfCounterSlot);
//In case there are no errors, we program the object with the slot itself has found
this->perfCounter->setSlot(perfCounterSlot);
// Program the counter slot
if (!this->perfCounter->program())
throw "unable to program performance counter parameters";
// Enable the counter slot
if (!this->perfCounter->enable())
throw "unable to enable performance counters";
/* Here we take a snapshot of the performance counter and a snapshot of the time
* stamp counter to initialize the arrays to let them not show erratic huge numbers
* on first step
*/
if (!this->perfCounter->takeSnapshot())
throw "unable to retrieve performance counter data";
if (!this->tscCounter->readMSR(TIME_STAMP_COUNTER_REG, cpuMask))
throw "unable to retrieve time stamp counter";
cpuIndex = 0;
for (nodeId = 0; nodeId < this->processor->getProcessorNodes(); nodeId++) {
for (coreId = 0x0; coreId < this->processor->getProcessorCores(); coreId++) {
this->prevPerfCounters[cpuIndex] = this->perfCounter->getCounter(cpuIndex);
this->prevTSCCounters[cpuIndex]
= this->tscCounter->getBits(cpuIndex, 0, 64);
cpuIndex++;
}
}
this->perfCounter->disable();
} catch (char const *str) {
if (this->perfCounter->getEnabled())
this->perfCounter->disable();
free(this->perfCounter);
free(this->tscCounter);
free(this->prevPerfCounters);
free(this->prevTSCCounters);
printf("Scaler.cpp::initializeCounters - %s\n", str);
return -1; //In case of error, we return -1
}
return 0; //In case of success, we return 0
}
void Scaler::loopPolicyRocket() {
unsigned char reqPState;
unsigned int enabledPowerStates;
DWORD units, cpuIndex, targetUnit, nodeIndex, coreIndex;
uint64_t deltaUsage;
unsigned int divisor;
PState **ps;
units=this->processor->getProcessorCores()*this->processor->getProcessorNodes();
ps=(PState **)calloc (units, sizeof (PState *));
for (cpuIndex=0;cpuIndex<units;cpuIndex++)
ps[cpuIndex]=new PState(2);
divisor=(1000000/1000)*this->samplingRate;
enabledPowerStates=this->processor->getMaximumPState().getPState();
Signal::activateSignalHandler( SIGINT);
while (!Signal::getSignalStatus()) {
if (!this->perfCounter->takeSnapshot())
throw "unable to retrieve performance counter data";
cpuIndex=0;
for (nodeIndex=0;nodeIndex<this->processor->getProcessorNodes();nodeIndex++) {
this->processor->setNode(nodeIndex);
for (coreIndex=0;coreIndex<this->processor->getProcessorCores();coreIndex++) {
this->processor->setCore(coreIndex);
reqPState=ps[cpuIndex]->getPState();
deltaUsage = ((this->perfCounter->getCounter(cpuIndex))
- this->prevPerfCounters[cpuIndex])/divisor;
targetUnit=(reqPState*enabledPowerStates)+cpuIndex;
/*printf ("CPU %d Usage: %d Current pstate: %d - Raise Freq: %d Reduce Freq: %d \n ",
cpuIndex, deltaUsage, reqPState, raiseTable[targetUnit], reduceTable[targetUnit]);*/
if (deltaUsage>raiseTable[targetUnit]){ reqPState=0; }
else if (deltaUsage<reduceTable[targetUnit]){ reqPState++; }
ps[cpuIndex]->setPState(reqPState);
this->processor->forcePState(ps[cpuIndex]->getPState());
this->prevPerfCounters[cpuIndex] = this->perfCounter->getCounter(cpuIndex);
cpuIndex++;
}
}
//printf("\n");
Sleep(this->samplingRate);
}
for (cpuIndex=0;cpuIndex<units;cpuIndex++)
free (ps[cpuIndex]);
free (ps);
}
void Scaler::loopPolicyStep() {
unsigned char reqPState;
unsigned int enabledPowerStates;
DWORD units, cpuIndex, targetUnit, nodeIndex, coreIndex;
uint64_t deltaUsage;
unsigned int divisor;
PState **ps;
units=this->processor->getProcessorCores()*this->processor->getProcessorNodes();
ps=(PState **)calloc (units, sizeof (PState *));
for (cpuIndex=0;cpuIndex<units;cpuIndex++)
ps[cpuIndex]=new PState(2);
divisor=(1000000/1000)*this->samplingRate;
enabledPowerStates=this->processor->getMaximumPState().getPState();
Signal::activateSignalHandler( SIGINT);
while (!Signal::getSignalStatus()) {
if (!this->perfCounter->takeSnapshot())
throw "unable to retrieve performance counter data";
cpuIndex=0;
for (nodeIndex=0;nodeIndex<this->processor->getProcessorNodes();nodeIndex++) {
this->processor->setNode(nodeIndex);
for (coreIndex=0;coreIndex<this->processor->getProcessorCores();coreIndex++) {
this->processor->setCore(coreIndex);
reqPState=ps[cpuIndex]->getPState();
deltaUsage = ((this->perfCounter->getCounter(cpuIndex))
- this->prevPerfCounters[cpuIndex])/divisor;
targetUnit=(reqPState*enabledPowerStates)+cpuIndex;
/*printf ("CPU %d Usage: %d Current pstate: %d - Raise Freq: %d Reduce Freq: %d \n ",
cpuIndex, deltaUsage, reqPState, raiseTable[targetUnit], reduceTable[targetUnit]);*/
if (deltaUsage>raiseTable[targetUnit]){ if (reqPState!=0) reqPState--; }
else if (deltaUsage<reduceTable[targetUnit]){ reqPState++; }
ps[cpuIndex]->setPState(reqPState);
this->processor->forcePState(ps[cpuIndex]->getPState());
this->prevPerfCounters[cpuIndex] = this->perfCounter->getCounter(cpuIndex);
cpuIndex++;
}
}
//printf("\n");
Sleep(this->samplingRate);
}
for (cpuIndex=0;cpuIndex<units;cpuIndex++)
free (ps[cpuIndex]);
free (ps);
}
void Scaler::createPerformanceTables () {
PState ps(0);
PState ps_back(0);
unsigned int i,unitIndex;
unsigned int nodeIndex, coreIndex;
unsigned int units; //units are total number of processors in the system
unsigned int targetUnit;
unsigned int enabledPowerStates;
unsigned int diff_frequency;
units=this->processor->getProcessorCores()*this->processor->getProcessorNodes();
enabledPowerStates=this->processor->getMaximumPState().getPState();
raiseTable=(uint64_t *)calloc (units*(enabledPowerStates+1), sizeof(uint64_t));
reduceTable=(uint64_t *)calloc (units*(enabledPowerStates+1), sizeof(uint64_t));
for (i=0;i<=enabledPowerStates;i++) {
printf ("Power State %d:" , i);
ps.setPState(i);
unitIndex=0;
for (nodeIndex=0;nodeIndex<this->processor->getProcessorNodes();nodeIndex++) {
this->processor->setNode(nodeIndex);
for (coreIndex=0;coreIndex<this->processor->getProcessorCores();coreIndex++) {
this->processor->setCore (coreIndex);
targetUnit=(i*enabledPowerStates) + unitIndex;
if (i==this->processor->getMaximumPState().getPState()) {
reduceTable[targetUnit]=0;
raiseTable[targetUnit]=this->processor->getFrequency(ps)*this->upperThreshold/100;
}
else
{
ps_back.setPState(i+1);
diff_frequency=this->processor->getFrequency(ps)-this->processor->getFrequency(ps_back);
raiseTable[targetUnit]=(diff_frequency*this->upperThreshold/100)+this->processor->getFrequency(ps_back);
reduceTable[targetUnit]=(diff_frequency*this->lowerThreshold/100)+this->processor->getFrequency(ps_back);
}
//printf ("Ra:%ld Re:%ld ",raiseTable[targetUnit], reduceTable[targetUnit]);
unitIndex++;
}
}
printf ("\n");
}
return;
}
void Scaler::beginScaling() {
if (initializeCounters()) {
perror(
"Scaler::beginScaling - performance counters initialization failed\n");
return;
}
createPerformanceTables();
this->perfCounter->enable();
switch (this->policy) {
case POLICY_STEP:
loopPolicyStep(); //loop will be terminated with a CTRL-C command
break;
case POLICY_ROCKET:
loopPolicyRocket();
break;
}
printf ("CTRL-C pressed. Terminating scaler and freeing resources... ");
this->perfCounter->disable();
if (this->perfCounter->getEnabled())
this->perfCounter->disable();
free(this->perfCounter);
free(this->tscCounter);
free(this->prevPerfCounters);
free(this->prevTSCCounters);
free(this->raiseTable);
free(this->reduceTable);
printf ("done.\n");
}