Skip to content

Commit 781d335

Browse files
authored
Merge pull request #162 from intel-innersource/rdementi/pcm-raw-custom-msr
pcm-raw: support custom MSR_EVENTs
2 parents 8a9a2b0 + c6e495d commit 781d335

File tree

4 files changed

+373
-34
lines changed

4 files changed

+373
-34
lines changed

src/cpucounters.cpp

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4283,7 +4283,11 @@ void BasicCounterState::readAndAggregateTSC(std::shared_ptr<SafeMsrHandle> msr)
42834283
uint64 cInvariantTSC = 0;
42844284
PCM * m = PCM::getInstance();
42854285
const auto cpu_model = m->getCPUModel();
4286-
if(m->isAtom() == false || cpu_model == PCM::AVOTON) msr->read(IA32_TIME_STAMP_COUNTER, &cInvariantTSC);
4286+
if (m->isAtom() == false || cpu_model == PCM::AVOTON)
4287+
{
4288+
msr->read(IA32_TIME_STAMP_COUNTER, &cInvariantTSC);
4289+
MSRValues[IA32_TIME_STAMP_COUNTER] = cInvariantTSC;
4290+
}
42874291
else
42884292
{
42894293
#ifdef _MSC_VER
@@ -4413,14 +4417,22 @@ void BasicCounterState::readAndAggregate(std::shared_ptr<SafeMsrHandle> msr)
44134417
readAndAggregateTSC(msr);
44144418

44154419
// reading core C state counters
4416-
for(int i=0; i <= (int)(PCM::MAX_C_STATE) ;++i)
4417-
if(m->coreCStateMsr && m->coreCStateMsr[i])
4418-
msr->read(m->coreCStateMsr[i], &(cCStateResidency[i]));
4420+
for (int i = 0; i <= (int)(PCM::MAX_C_STATE); ++i)
4421+
{
4422+
if (m->coreCStateMsr && m->coreCStateMsr[i])
4423+
{
4424+
const auto index = m->coreCStateMsr[i];
4425+
msr->read(index, &(cCStateResidency[i]));
4426+
MSRValues[index] = cCStateResidency[i];
4427+
}
4428+
}
44194429

44204430
// reading temperature
44214431
msr->read(MSR_IA32_THERM_STATUS, &thermStatus);
4432+
MSRValues[MSR_IA32_THERM_STATUS] = thermStatus;
44224433

44234434
msr->read(MSR_SMI_COUNT, &cSMICount);
4435+
MSRValues[MSR_SMI_COUNT] = cSMICount;
44244436

44254437
InstRetiredAny += checked_uint64(m->extractCoreFixedCounterValue(cInstRetiredAny), extract_bits(overflows, 32, 32));
44264438
CpuClkUnhaltedThread += checked_uint64(m->extractCoreFixedCounterValue(cCpuClkUnhaltedThread), extract_bits(overflows, 33, 33));
@@ -4657,6 +4669,8 @@ void PCM::programPCU(uint32* PCUCntConf, const uint64 filter)
46574669
PCM::ErrorCode PCM::program(const RawPMUConfigs& curPMUConfigs_, const bool silent)
46584670
{
46594671
if (MSR.empty()) return PCM::MSRAccessDenied;
4672+
threadMSRConfig = RawPMUConfig{};
4673+
packageMSRConfig = RawPMUConfig{};
46604674
RawPMUConfigs curPMUConfigs = curPMUConfigs_;
46614675
constexpr auto globalRegPos = 0;
46624676
if (curPMUConfigs.count("core"))
@@ -4789,6 +4803,14 @@ PCM::ErrorCode PCM::program(const RawPMUConfigs& curPMUConfigs_, const bool sile
47894803
{
47904804
programIIOCounters(events64);
47914805
}
4806+
else if (type == "package_msr")
4807+
{
4808+
packageMSRConfig = pmuConfig.second;
4809+
}
4810+
else if (type == "thread_msr")
4811+
{
4812+
threadMSRConfig = pmuConfig.second;
4813+
}
47924814
else
47934815
{
47944816
std::cerr << "ERROR: unrecognized PMU type \"" << type << "\"\n";
@@ -5066,6 +5088,28 @@ void PCM::readAndAggregateEnergyCounters(const uint32 socket, CounterStateType &
50665088
result.DRAMEnergyStatus += dram_energy_status[socket]->read();
50675089
}
50685090

5091+
template <class CounterStateType>
5092+
void PCM::readMSRs(std::shared_ptr<SafeMsrHandle> msr, const PCM::RawPMUConfig& msrConfig, CounterStateType& result)
5093+
{
5094+
auto read = [&msr, &result](const RawEventConfig & cfg) {
5095+
const auto index = cfg.first[MSREventPosition::index];
5096+
if (result.MSRValues.find(index) == result.MSRValues.end())
5097+
{
5098+
uint64 val{ 0 };
5099+
msr->read(index, &val);
5100+
result.MSRValues[index] = val;
5101+
}
5102+
};
5103+
for (const auto& cfg : msrConfig.programmable)
5104+
{
5105+
read(cfg);
5106+
}
5107+
for (const auto& cfg : msrConfig.fixed)
5108+
{
5109+
read(cfg);
5110+
}
5111+
}
5112+
50695113
template <class CounterStateType>
50705114
void PCM::readAndAggregatePackageCStateResidencies(std::shared_ptr<SafeMsrHandle> msr, CounterStateType & result)
50715115
{
@@ -5202,6 +5246,7 @@ void PCM::readPackageThermalHeadroom(const uint32 socket, CounterStateType & res
52025246
{
52035247
uint64 val = 0;
52045248
MSR[socketRefCore[socket]]->read(MSR_PACKAGE_THERM_STATUS,&val);
5249+
result.MSRValues[MSR_PACKAGE_THERM_STATUS] = val;
52055250
result.ThermalHeadroom = extractThermalHeadroom(val);
52065251
}
52075252
else
@@ -5257,6 +5302,7 @@ void PCM::getAllCounterStates(SystemCounterState & systemState, std::vector<Sock
52575302
{
52585303
socketStates[topology[core].socket].UncoreCounterState::readAndAggregate(MSR[core]); // read package C state counters
52595304
}
5305+
readMSRs(MSR[core], threadMSRConfig, coreStates[core]);
52605306
}
52615307
);
52625308
asyncCoreResults.push_back(task.get_future());
@@ -5269,11 +5315,12 @@ void PCM::getAllCounterStates(SystemCounterState & systemState, std::vector<Sock
52695315
{
52705316
int32 refCore = socketRefCore[s];
52715317
if (refCore<0) refCore = 0;
5272-
std::packaged_task<void()> task([this, s, &socketStates]() -> void
5318+
std::packaged_task<void()> task([this, s, &socketStates, refCore]() -> void
52735319
{
52745320
readAndAggregateUncoreMCCounters(s, socketStates[s]);
52755321
readAndAggregateEnergyCounters(s, socketStates[s]);
52765322
readPackageThermalHeadroom(s, socketStates[s]);
5323+
readMSRs(MSR[refCore], packageMSRConfig, socketStates[s]);
52775324
} );
52785325
asyncCoreResults.push_back(task.get_future());
52795326
coreTaskQueues[refCore]->push(task);

src/cpucounters.h

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,17 @@ class PCM_API PCM
846846
int divider[4]; //We usually like to have some kind of divider (i.e. /10e6 )
847847
};
848848

849+
enum MSREventPosition
850+
{
851+
index = 0,
852+
type = 1
853+
};
854+
enum MSRType
855+
{
856+
Static = 0,
857+
Freerun = 1
858+
};
859+
849860
private:
850861
ProgramMode mode;
851862
CustomCoreEventDescription coreEventDesc[PERF_MAX_CUSTOM_COUNTERS];
@@ -955,6 +966,11 @@ class PCM_API PCM
955966
void readPackageThermalHeadroom(const uint32 socket, CounterStateType & counterState);
956967
template <class CounterStateType>
957968
void readAndAggregatePackageCStateResidencies(std::shared_ptr<SafeMsrHandle> msr, CounterStateType & result);
969+
public:
970+
struct RawPMUConfig;
971+
private:
972+
template <class CounterStateType>
973+
void readMSRs(std::shared_ptr<SafeMsrHandle> msr, const RawPMUConfig & msrConfig, CounterStateType & result);
958974
void readQPICounters(SystemCounterState & counterState);
959975
void reportQPISpeed() const;
960976
void readCoreCounterConfig(const bool complainAboutMSR = false);
@@ -1428,6 +1444,7 @@ class PCM_API PCM
14281444
}
14291445
return false;
14301446
}
1447+
RawPMUConfig threadMSRConfig{}, packageMSRConfig{};
14311448
public:
14321449

14331450
//! \brief Reads CPU model id
@@ -2265,11 +2282,12 @@ class BasicCounterState
22652282
friend double getBadSpeculation(const CounterStateType & before, const CounterStateType & after);
22662283
template <class CounterStateType>
22672284
friend double getRetiring(const CounterStateType & before, const CounterStateType & after);
2268-
2285+
template <class CounterStateType>
2286+
friend uint64 getMSREvent(const uint64 & index, const PCM::MSRType & type, const CounterStateType& before, const CounterStateType& after);
22692287
protected:
2270-
checked_uint64 InstRetiredAny;
2271-
checked_uint64 CpuClkUnhaltedThread;
2272-
checked_uint64 CpuClkUnhaltedRef;
2288+
checked_uint64 InstRetiredAny{};
2289+
checked_uint64 CpuClkUnhaltedThread{};
2290+
checked_uint64 CpuClkUnhaltedRef{};
22732291
checked_uint64 Event[PERF_MAX_CUSTOM_COUNTERS];
22742292
enum
22752293
{
@@ -2290,6 +2308,7 @@ class BasicCounterState
22902308
uint64 MemoryBWTotal;
22912309
uint64 SMICount;
22922310
uint64 FrontendBoundSlots, BadSpeculationSlots, BackendBoundSlots, RetiringSlots, AllSlotsRaw;
2311+
std::unordered_map<uint64, uint64> MSRValues;
22932312

22942313
public:
22952314
BasicCounterState() :
@@ -4056,6 +4075,34 @@ inline double getRetiring(const CounterStateType & before, const CounterStateTyp
40564075
return 0.;
40574076
}
40584077

4078+
template <class CounterStateType>
4079+
uint64 getMSREvent(const uint64& index, const PCM::MSRType& type, const CounterStateType& before, const CounterStateType& after)
4080+
{
4081+
switch (type)
4082+
{
4083+
case PCM::MSRType::Freerun:
4084+
{
4085+
const auto beforeIt = before.MSRValues.find(index);
4086+
const auto afterIt = after.MSRValues.find(index);
4087+
if (beforeIt != before.MSRValues.end() && afterIt != after.MSRValues.end())
4088+
{
4089+
return afterIt->second - beforeIt->second;
4090+
}
4091+
break;
4092+
}
4093+
case PCM::MSRType::Static:
4094+
{
4095+
const auto result = after.MSRValues.find(index);
4096+
if (result != after.MSRValues.end())
4097+
{
4098+
return result->second;
4099+
}
4100+
break;
4101+
}
4102+
}
4103+
return 0ULL;
4104+
}
4105+
40594106
} // namespace pcm
40604107

40614108
#endif

0 commit comments

Comments
 (0)