forked from mh0rst/turionpowercontrol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPerformanceCounter.cpp
484 lines (393 loc) · 13.8 KB
/
PerformanceCounter.cpp
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
* PerformanceCounter.cpp
*
* This class allows a faster use of performance counter.
* Instructions on how to use:
*
* 1 - Instantiate the object giving a cpuMask and a slot to count. Look that the slot
* will be the same for all the cpus in cpuMask
* 2 - Use setters to setup optional parameters of the performance register
* 3 - Use program() method to program the performance counter. Program method will program the hardware registers
* but will not enable the performance counters
* 4 - Use the enable() method to enable the performance counters
* 5 - Take a snapshot of the current performance counters condition with takeSnapshot() method. This is useful to avoid
* time stretched data retrieving
* 6 - Obtain freezed counters using getCounter() method for all the cpus you need.
* 7 - Repeat from step 5 as long as you need the performance counter data
* 8 - When finished, just issue the disable() method to disable the performance counters. It won't actually deallocate
* but since they are disabled, they can be reused.
*
* If you wish to know the actual hardware condition, you can use the fetch () method that reads the hardware registers
* and changes the protected parameters of this class. Then you can access the parameters via setters/getters.
*
* Created on: 25/mag/2011
* Author: paolo
*/
#include "PerformanceCounter.h"
PerformanceCounter::PerformanceCounter(PROCESSORMASK cpuMask, DWORD slot, DWORD maxslots)
{
if (slot > maxslots)
this->slot = maxslots;
else
this->slot = slot;
this->maxslots = maxslots;
this->cpuMask = cpuMask;
this->eventSelect = 0x00; //Default event
this->countOsMode = true;
this->countUserMode = true;
this->counterMask = 0;
this->edgeDetect = false;
this->enableAPICInterrupt = false;
this->invertCntMask = false;
this->unitMask = 0;
//Note that the number of slots determines the register to be used
//Based on BKDG for 15h (pg 547), the legacy slots do exist, however if they are used
// you are only receiving the performance info for the lower 4 of 6 slots
//Therefore the max number of slots can be used to determine the appropriate register
//If a newer CPU is misdetected/mishandled, the performance info will be correct,
// However it will not give you all available info
if (maxslots == 6)
{
this->pesrReg = BASE_PESR_REG_15;
this->percReg = BASE_PERC_REG_15;
this->offset = 2;
}
else
{
this->pesrReg = BASE_PESR_REG;
this->percReg = BASE_PERC_REG;
this->offset = 1;
}
snapshotRegister = new MSRObject();
}
/*
* MSR Register is different based on the Extended CPU Family
* This method will return the register required to get/set the PESR bits
*
*/
unsigned int PerformanceCounter::getPESRReg(unsigned char slot)
{
return (this->pesrReg + (this->offset * slot));
}
unsigned int PerformanceCounter::getPERCReg(unsigned char slot)
{
return (this->percReg + (this->offset * slot));
}
/*
* program method will program the MS registers according to settings required by the user. This
* method will not enable the performance counter, will just write the parameters.
*
* Returns true if successful, false if not successful.
*
*/
bool PerformanceCounter::program()
{
MSRObject *pCounterMSRObject = new MSRObject();
//Loads the current status of the MS registers for all the cpus in the mask
if (!pCounterMSRObject->readMSR(getPESRReg(this->slot), this->cpuMask))
{
free(pCounterMSRObject);
return false;
}
//Programs the bits of the performance counter register according with specifications
pCounterMSRObject->setBits(8, 8, this->unitMask);
pCounterMSRObject->setBits(16, 1, this->countUserMode);
pCounterMSRObject->setBits(17, 1, this->countOsMode);
pCounterMSRObject->setBits(18, 1, this->edgeDetect);
pCounterMSRObject->setBits(20, 1, this->enableAPICInterrupt);
pCounterMSRObject->setBits(23, 1, this->invertCntMask);
pCounterMSRObject->setBits(24, 8, this->counterMask);
pCounterMSRObject->setBits(0, 8, this->eventSelect & 0xff); //Lower 8 bits of eventSelect
pCounterMSRObject->setBits(32, 4, this->eventSelect & 0xf00); //Higher 4 bits of eventSelect
pCounterMSRObject->setBits(22, 1, 0); //Disables the counter, it must be enabled with another method
//Writes the data in the MS registers;
if (!pCounterMSRObject->writeMSR())
{
free(pCounterMSRObject);
return false;
}
free(pCounterMSRObject);
return true;
}
/*
* fetch method will do the inverse of program method. Program method will program the hardware registers
* with parameters sets in an object of this class, instead fetch will read the hardware registers and will
* modify the parameters of the object. Then the parameters can be read by the main program using the
* getters to obtain actual hardware status.
*
* The only limitation with this method is the fact that you can fetch hardware registers of only one cpu
* in the cpu mask.
*
*/
bool PerformanceCounter::fetch(DWORD cpuIndex)
{
MSRObject *pCounterMSRObject = new MSRObject();
//Loads the current status of the MS registers for all the cpus in the mask.
//Actually it could be optimized reading data for one cpu only.
if (!pCounterMSRObject->readMSR(getPESRReg(this->slot), this->cpuMask))
{
free(pCounterMSRObject);
return false;
}
this->unitMask=pCounterMSRObject->getBits(cpuIndex, 8, 8);
this->countUserMode=pCounterMSRObject->getBits(cpuIndex, 16, 1);
this->countOsMode=pCounterMSRObject->getBits(cpuIndex, 17, 1);
this->edgeDetect=pCounterMSRObject->getBits(cpuIndex, 18, 1);
this->enableAPICInterrupt=pCounterMSRObject->getBits(cpuIndex, 20, 1);
this->invertCntMask=pCounterMSRObject->getBits(cpuIndex, 23, 1);
this->counterMask=pCounterMSRObject->getBits(cpuIndex, 24, 8);
this->enabled=pCounterMSRObject->getBits(cpuIndex, 22,1);
this->eventSelect=pCounterMSRObject->getBits(cpuIndex, 0, 8); //Lower 8 bits of eventSelect
this->eventSelect+=pCounterMSRObject->getBits(cpuIndex, 32, 4) << 4; //Higher 4 bits of eventSelect
return true;
}
/*
* findAvailableSlot() will find a "row" of available slots. It means that it will cycle through the
* performance counter slots and will search for a slot that is, for all processors in the mask,
* not enabled at all or has exactly the same parameters that is going to be programmed.
*
* Returns the performance counter slot if the class finds an available slot for all the processors.
* Returns -1 (0xffffffff) if no available slot is found
* Returns -2 (0xfffffffe) in case of errors
*
*/
unsigned int PerformanceCounter::findAvailableSlot ()
{
MSRObject *pCounterMSRObject = new MSRObject();
unsigned int slot;
unsigned int cpuIndex;
bool valid;
for (slot = 0; slot < this->maxslots; slot++)
{
//Loads the current status of the MS registers for all the cpus in the mask.
if (!pCounterMSRObject->readMSR(getPESRReg(slot), this->cpuMask))
{
free(pCounterMSRObject);
return -2;
}
valid = true;
for (cpuIndex = 0; cpuIndex < pCounterMSRObject->getCount(); cpuIndex++)
{
//If the counter slot is disabled, we proceed to the next cpu in the mask
//If the counter slot is enabled, we check the parameters. If we find that parameters are
//exactly the same as those programmed in the class object, we proceed to the next
//cpu in the mask. If parameters are not the same, we break the cycle and proceed to the next slot
if (pCounterMSRObject->getBits(cpuIndex, 22, 1) != 0)
{
if ((pCounterMSRObject->getBits(cpuIndex, 8, 8) != this->unitMask) ||
(pCounterMSRObject->getBits(cpuIndex, 16, 1) != this->countUserMode) ||
(pCounterMSRObject->getBits(cpuIndex, 17, 1) != this->countOsMode) ||
(pCounterMSRObject->getBits(cpuIndex, 18, 1) != this->edgeDetect) ||
(pCounterMSRObject->getBits(cpuIndex, 20, 1) != this->enableAPICInterrupt) ||
(pCounterMSRObject->getBits(cpuIndex, 23, 1) != this->invertCntMask) ||
(pCounterMSRObject->getBits(cpuIndex, 24, 8) != this->counterMask) ||
(pCounterMSRObject->getBits(cpuIndex, 0, 8) != (this->eventSelect & 0xff)) ||
(pCounterMSRObject->getBits(cpuIndex, 32, 4) != (this->eventSelect & 0xf00)))
{
valid=false;
break;
}
}
}
if (valid == true)
return slot;
}
//We found no valid slot, returns -1 as expected
return -1;
}
/*
* findFreeSlot() will find a "row" of available slots. It means that it will cycle through the
* performance counter slots and will search for a slot that is, for all processors in the mask,
* not enabled at all.
*
* Returns the performance counter slot if the class finds free slots for all the processors.
* Returns -1 (0xffffffff) if no free slot is found
* Returns -2 (0xfffffffe) in case of errors
*
*/
unsigned int PerformanceCounter::findFreeSlot ()
{
MSRObject *pCounterMSRObject = new MSRObject();
unsigned int slot;
unsigned int cpuIndex;
bool valid;
for (slot = 0; slot < this->maxslots; slot++)
{
//Loads the current status of the MS registers for all the cpus in the mask.
if (!pCounterMSRObject->readMSR(getPESRReg(slot), this->cpuMask))
{
free(pCounterMSRObject);
return -2;
}
valid=true;
for (cpuIndex=0;cpuIndex<pCounterMSRObject->getCount();cpuIndex++)
{
//If the counter slot is disabled, we proceed to the next cpu in the mask
//If the counter slos is enabled, we stop the cycle and declare the slot unfree
if (pCounterMSRObject->getBits(cpuIndex, 22, 1) != 0)
{
valid=false;
break;
}
}
if (valid==true) return slot;
}
//We found no valid slot, returns -1 as expected
return -1;
}
/*
* Enables the performance counter slot for all the processors in cpuMask
* Be sure to program them first with program method!
*
* Returns true is successful, false in the other case.
*
*/
bool PerformanceCounter::enable()
{
MSRObject *pCounterMSRObject = new MSRObject();
//Loads the current status of the MS registers for all the cpus in the mask.
if (!pCounterMSRObject->readMSR(getPESRReg(this->slot), this->cpuMask))
{
free(pCounterMSRObject);
return -2;
}
pCounterMSRObject->setBitsLow(22, 1, 0x1); //Sets the bit 22, enables the performance counter
if (!pCounterMSRObject->writeMSR())
{
free(pCounterMSRObject);
return false;
}
this->enabled = true;
free(pCounterMSRObject);
return true;
}
/*
* Disables the performance counter slot for all the processors in cpuMask
*
*
* Returns true is successful, false in the other case.
*
*/
bool PerformanceCounter::disable()
{
MSRObject *pCounterMSRObject = new MSRObject();
//Loads the current status of the MS registers for all the cpus in the mask.
if (!pCounterMSRObject->readMSR(getPESRReg(this->slot), this->cpuMask))
{
free(pCounterMSRObject);
return -2;
}
pCounterMSRObject->setBitsLow(22, 1, 0x0); //Sets the bit 22, disables the performance counter
if (!pCounterMSRObject->writeMSR())
{
free(pCounterMSRObject);
return false;
}
this->enabled=false;
free(pCounterMSRObject);
return true;
}
/*
* takeSnapshot method will read the counter associated with the performance counter and will store it
* inside the object. Then the main program can access each single counter for each processor/core without
* the problem of retrieving time-stretched uncoherent data.
*
* Returns true if the snapshot has been taken correctly, else will return false
*/
bool PerformanceCounter::takeSnapshot()
{
if (!snapshotRegister->readMSR(getPERCReg(this->slot), this->cpuMask))
return false;
return true;
}
/*
* Returns the counter associated with cpuIndex. Remember that cpuIndex is not the absolute number of the cpu
* you want to read the counter, but it is an index as explained in the getBits method in the MSRObject class
*
* Remember also to issue a call to takeSnapshot before calling getCounter, else you will get invalid data
*
*/
uint64_t PerformanceCounter::getCounter(DWORD cpuIndex)
{
return snapshotRegister->getBits(cpuIndex, 0, 64);
}
/*
* Getters and setters, not much interesting
*
*/
bool PerformanceCounter::getEnabled() const {
return enabled;
}
bool PerformanceCounter::getCountUserMode() const {
return countUserMode;
}
unsigned char PerformanceCounter::getCounterMask() const {
return counterMask;
}
PROCESSORMASK PerformanceCounter::getCpuMask() const {
return cpuMask;
}
bool PerformanceCounter::getEdgeDetect() const {
return edgeDetect;
}
bool PerformanceCounter::getEnableAPICInterrupt() const {
return enableAPICInterrupt;
}
unsigned short int PerformanceCounter::getEventSelect() const {
return eventSelect;
}
bool PerformanceCounter::getInvertCntMask() const {
return invertCntMask;
}
unsigned char PerformanceCounter::getSlot() const {
return slot;
}
unsigned char PerformanceCounter::getUnitMask() const {
return unitMask;
}
void PerformanceCounter::setCountUserMode(bool countUserMode) {
this->countUserMode = countUserMode;
}
void PerformanceCounter::setCounterMask(unsigned char counterMask) {
this->counterMask = counterMask;
}
void PerformanceCounter::setCpuMask(PROCESSORMASK cpuMask) {
this->cpuMask = cpuMask;
}
void PerformanceCounter::setEdgeDetect(bool edgeDetect) {
this->edgeDetect = edgeDetect;
}
void PerformanceCounter::setEnableAPICInterrupt(bool enableAPICInterrupt) {
this->enableAPICInterrupt = enableAPICInterrupt;
}
void PerformanceCounter::setEventSelect(unsigned short int eventSelect) {
this->eventSelect = eventSelect;
}
void PerformanceCounter::setInvertCntMask(bool invertCntMask) {
this->invertCntMask = invertCntMask;
}
void PerformanceCounter::setSlot(unsigned char slot) {
this->slot = slot;
}
bool PerformanceCounter::getCountOsMode() const
{
return countOsMode;
}
void PerformanceCounter::setCountOsMode(bool countOsMode)
{
this->countOsMode = countOsMode;
}
void PerformanceCounter::setUnitMask(unsigned char unitMask) {
this->unitMask = unitMask;
}
void PerformanceCounter::setMaxSlots(unsigned char maxslots)
{
this->maxslots = maxslots;
}
/*
* Destructor. Frees resources.
*
*/
PerformanceCounter::~PerformanceCounter() {
free(snapshotRegister);
}