Skip to content

Commit 42207e1

Browse files
committed
support SYS energy API
Change-Id: I2313b939f9cc85f66fa90fbea873c2365f67582d
1 parent a567a51 commit 42207e1

File tree

5 files changed

+98
-4
lines changed

5 files changed

+98
-4
lines changed

src/cpucounters.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1749,6 +1749,12 @@ void PCM::initEnergyMonitoring()
17491749
pp_energy_status.push_back(std::make_shared<CounterWidthExtender>(
17501750
new CounterWidthExtender::MsrHandleCounter(MSR[socketRefCore[0]], MSR_PP1_ENERGY_STATUS), 32, 10000));
17511751
}
1752+
1753+
if (systemEnergyMetricAvailable() && MSR.size() && (system_energy_status.get() == nullptr))
1754+
{
1755+
system_energy_status = std::make_shared<CounterWidthExtender>(
1756+
new CounterWidthExtender::MsrHandleCounter(MSR[socketRefCore[0]], MSR_SYS_ENERGY_STATUS, 0x00000000FFFFFFFF), 32, 10000);
1757+
}
17521758
}
17531759

17541760
static const uint32 UBOX0_DEV_IDS[] = {
@@ -6964,6 +6970,11 @@ void PCM::getAllCounterStates(SystemCounterState & systemState, std::vector<Sock
69646970
// aggregate socket uncore iMC, energy and package C state counters into system
69656971
systemState += socketStates[s];
69666972
}
6973+
6974+
if (systemEnergyMetricAvailable() && system_energy_status.get() != nullptr)
6975+
{
6976+
systemState.systemEnergyStatus = system_energy_status->read();
6977+
}
69676978
}
69686979

69696980
void PCM::getUncoreCounterStates(SystemCounterState & systemState, std::vector<SocketCounterState> & socketStates)

src/cpucounters.h

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,7 @@ class PCM_API PCM
810810
std::vector<std::shared_ptr<CounterWidthExtender> > energy_status;
811811
std::vector<std::shared_ptr<CounterWidthExtender> > dram_energy_status;
812812
std::vector<std::shared_ptr<CounterWidthExtender> > pp_energy_status;
813+
std::shared_ptr<CounterWidthExtender> system_energy_status;
813814
std::vector<std::vector<std::pair<UncorePMU, UncorePMU>>> cxlPMUs; // socket X CXL ports X UNIT {0,1}
814815

815816
std::vector<std::shared_ptr<CounterWidthExtender> > memory_bw_local;
@@ -2510,6 +2511,25 @@ class PCM_API PCM
25102511
);
25112512
}
25122513

2514+
bool systemEnergyMetricAvailable() const
2515+
{
2516+
return (
2517+
useSKLPath()
2518+
|| cpu_family_model == PCM::SKX
2519+
|| cpu_family_model == PCM::ICX
2520+
|| cpu_family_model == PCM::ADL
2521+
|| cpu_family_model == PCM::RPL
2522+
|| cpu_family_model == PCM::MTL
2523+
|| cpu_family_model == PCM::LNL
2524+
|| cpu_family_model == PCM::ARL
2525+
|| cpu_family_model == PCM::SPR
2526+
|| cpu_family_model == PCM::EMR
2527+
|| cpu_family_model == PCM::GNR
2528+
|| cpu_family_model == PCM::SRF
2529+
|| cpu_family_model == PCM::GRR
2530+
);
2531+
}
2532+
25132533
bool packageThermalMetricsAvailable() const
25142534
{
25152535
return packageEnergyMetricsAvailable();
@@ -3368,6 +3388,25 @@ uint64 getConsumedEnergy(const int powerPlane, const CounterStateType& before, c
33683388
return after.PPEnergyStatus[powerPlane] - before.PPEnergyStatus[powerPlane];
33693389
}
33703390

3391+
/*! \brief Returns energy consumed by system
3392+
\param before CPU counter state before the experiment
3393+
\param after CPU counter state after the experiment
3394+
*/
3395+
template <class CounterStateType>
3396+
uint64 getSystemConsumedEnergy(const CounterStateType& before, const CounterStateType& after)
3397+
{
3398+
return after.systemEnergyStatus - before.systemEnergyStatus;
3399+
}
3400+
3401+
/*! \brief Checks is systemEnergyStatusValid is valid in the state
3402+
* \param s CPU counter state
3403+
*/
3404+
template <class CounterStateType>
3405+
bool systemEnergyStatusValid(const CounterStateType& s)
3406+
{
3407+
return s.systemEnergyStatus != 0;
3408+
}
3409+
33713410
/*! \brief Returns energy consumed by DRAM (measured in internal units)
33723411
\param before CPU counter state before the experiment
33733412
\param after CPU counter state after the experiment
@@ -3435,6 +3474,31 @@ double getConsumedJoules(const int powerPlane, const CounterStateType& before, c
34353474
return double(getConsumedEnergy(powerPlane, before, after)) * m->getJoulesPerEnergyUnit();
34363475
}
34373476

3477+
/*! \brief Returns Joules consumed by system
3478+
\param before CPU counter state before the experiment
3479+
\param after CPU counter state after the experiment
3480+
*/
3481+
template <class CounterStateType>
3482+
double getSystemConsumedJoules(const CounterStateType& before, const CounterStateType& after)
3483+
{
3484+
PCM* m = PCM::getInstance();
3485+
if (!m) return -1.;
3486+
3487+
auto unit = m->getJoulesPerEnergyUnit();
3488+
3489+
switch (m->getCPUFamilyModel())
3490+
{
3491+
case PCM::SPR:
3492+
case PCM::EMR:
3493+
case PCM::GNR:
3494+
case PCM::SRF:
3495+
unit = 1.0;
3496+
break;
3497+
}
3498+
3499+
return double(getSystemConsumedEnergy(before, after)) * unit;
3500+
}
3501+
34383502
/*! \brief Returns Joules consumed by DRAM
34393503
\param before CPU counter state before the experiment
34403504
\param after CPU counter state after the experiment
@@ -3860,11 +3924,14 @@ class SystemCounterState : public SocketCounterState
38603924
friend std::vector<uint64> getPCICFGEvent(const PCM::RawEventEncoding& eventEnc, const SystemCounterState& before, const SystemCounterState& after);
38613925
friend std::vector<uint64> getMMIOEvent(const PCM::RawEventEncoding& eventEnc, const SystemCounterState& before, const SystemCounterState& after);
38623926
friend std::vector<uint64> getPMTEvent(const PCM::RawEventEncoding& eventEnc, const SystemCounterState& before, const SystemCounterState& after);
3927+
template <class CounterStateType> friend bool systemEnergyStatusValid(const CounterStateType& s);
3928+
template <class CounterStateType> friend uint64 getSystemConsumedEnergy(const CounterStateType& before, const CounterStateType& after);
38633929

38643930
std::vector<std::vector<uint64> > incomingQPIPackets; // each 64 byte
38653931
std::vector<std::vector<uint64> > outgoingQPIFlits; // idle or data/non-data flits depending on the architecture
38663932
std::vector<std::vector<uint64> > TxL0Cycles;
38673933
uint64 uncoreTSC;
3934+
uint64 systemEnergyStatus;
38683935
std::unordered_map<PCM::RawEventEncoding, std::vector<uint64> , PCM::PCICFGRegisterEncodingHash, PCM::PCICFGRegisterEncodingCmp> PCICFGValues{};
38693936
std::unordered_map<PCM::RawEventEncoding, std::vector<uint64>, PCM::MMIORegisterEncodingHash, PCM::MMIORegisterEncodingCmp> MMIOValues{};
38703937
std::unordered_map<PCM::RawEventEncoding, std::vector<uint64>, PCM::PMTRegisterEncodingHash2> PMTValues{};
@@ -3890,7 +3957,8 @@ class SystemCounterState : public SocketCounterState
38903957
friend uint64 getOutgoingQPILinkBytes(uint32 socketNr, uint32 linkNr, const SystemCounterState & now);
38913958

38923959
SystemCounterState() :
3893-
uncoreTSC(0)
3960+
uncoreTSC(0),
3961+
systemEnergyStatus(0)
38943962
{
38953963
PCM * m = PCM::getInstance();
38963964
accel_counters.resize(m->getNumberofAccelCounters());
@@ -3922,6 +3990,7 @@ class SystemCounterState : public SocketCounterState
39223990

39233991
return *this;
39243992
}
3993+
39253994
virtual ~ SystemCounterState() {}
39263995
};
39273996

src/pcm.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,12 @@ void print_output(PCM * m,
336336
cout << resetColor();
337337
cout << setNextColor() << "\n Instructions retired: " << unit_format(getInstructionsRetired(sstate1, sstate2)) << " ;"
338338
<< setNextColor() << " Active cycles: " << unit_format(getCycles(sstate1, sstate2)) << " ;"
339-
<< setNextColor() << " Time (TSC): " << unit_format(getInvariantTSC(cstates1[0], cstates2[0])) << "ticks;\n\n";
339+
<< setNextColor() << " Time (TSC): " << unit_format(getInvariantTSC(cstates1[0], cstates2[0])) << "ticks;";
340+
if (m->systemEnergyMetricAvailable() && systemEnergyStatusValid(sstate1) && systemEnergyStatusValid(sstate2))
341+
{
342+
cout << setNextColor() << " SYS energy: " << getSystemConsumedJoules(sstate1, sstate2) << " J;";
343+
}
344+
cout << "\n\n";
340345

341346
cout << resetColor() << setNextColor() << " Core C-state residencies: "<< setNextColor() << "C0 (active,non-halted): " << (getCoreCStateResidency(0, sstate1, sstate2)*100.) << " %;";
342347
for (int s = 1; s <= PCM::MAX_C_STATE; ++s)

src/types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ constexpr auto MSR_SMI_COUNT = 0x34;
506506
*/
507507

508508
constexpr auto MSR_PKG_ENERGY_STATUS = 0x611;
509+
constexpr auto MSR_SYS_ENERGY_STATUS = 0x64D;
509510
constexpr auto MSR_RAPL_POWER_UNIT = 0x606;
510511
constexpr auto MSR_PKG_POWER_INFO = 0x614;
511512

src/width_extender.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,20 @@ class CounterWidthExtender
4040
{
4141
std::shared_ptr<SafeMsrHandle> msr;
4242
uint64 msr_addr;
43-
MsrHandleCounter(std::shared_ptr<SafeMsrHandle> msr_, uint64 msr_addr_) : msr(msr_), msr_addr(msr_addr_) { }
43+
uint64 msr_mask;
44+
MsrHandleCounter( std::shared_ptr<SafeMsrHandle> msr_,
45+
const uint64 msr_addr_,
46+
const uint64 msr_mask_ = ~uint64(0ULL)) :
47+
msr(msr_),
48+
msr_addr(msr_addr_),
49+
msr_mask(msr_mask_)
50+
{
51+
}
4452
uint64 operator () ()
4553
{
4654
uint64 value = 0;
4755
msr->read(msr_addr, &value);
48-
return value;
56+
return value & msr_mask;
4957
}
5058
};
5159

0 commit comments

Comments
 (0)