Skip to content

Commit

Permalink
support SYS energy API
Browse files Browse the repository at this point in the history
Change-Id: I2313b939f9cc85f66fa90fbea873c2365f67582d
  • Loading branch information
rdementi committed Dec 3, 2024
1 parent e8233e3 commit 55cf22a
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/cpucounters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1749,6 +1749,12 @@ void PCM::initEnergyMonitoring()
pp_energy_status.push_back(std::make_shared<CounterWidthExtender>(
new CounterWidthExtender::MsrHandleCounter(MSR[socketRefCore[0]], MSR_PP1_ENERGY_STATUS), 32, 10000));
}

if (systemEnergyMetricAvailable() && MSR.size() && (system_energy_status.get() == nullptr))
{
system_energy_status = std::make_shared<CounterWidthExtender>(
new CounterWidthExtender::MsrHandleCounter(MSR[socketRefCore[0]], MSR_SYS_ENERGY_STATUS, 0x00000000FFFFFFFF), 32, 10000);
}
}

static const uint32 UBOX0_DEV_IDS[] = {
Expand Down Expand Up @@ -6964,6 +6970,11 @@ void PCM::getAllCounterStates(SystemCounterState & systemState, std::vector<Sock
// aggregate socket uncore iMC, energy and package C state counters into system
systemState += socketStates[s];
}

if (systemEnergyMetricAvailable() && system_energy_status.get() != nullptr)
{
systemState.systemEnergyStatus = system_energy_status->read();
}
}

void PCM::getUncoreCounterStates(SystemCounterState & systemState, std::vector<SocketCounterState> & socketStates)
Expand Down
71 changes: 70 additions & 1 deletion src/cpucounters.h
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,7 @@ class PCM_API PCM
std::vector<std::shared_ptr<CounterWidthExtender> > energy_status;
std::vector<std::shared_ptr<CounterWidthExtender> > dram_energy_status;
std::vector<std::shared_ptr<CounterWidthExtender> > pp_energy_status;
std::shared_ptr<CounterWidthExtender> system_energy_status;
std::vector<std::vector<std::pair<UncorePMU, UncorePMU>>> cxlPMUs; // socket X CXL ports X UNIT {0,1}

std::vector<std::shared_ptr<CounterWidthExtender> > memory_bw_local;
Expand Down Expand Up @@ -2510,6 +2511,25 @@ class PCM_API PCM
);
}

bool systemEnergyMetricAvailable() const
{
return (
useSKLPath()
|| cpu_family_model == PCM::SKX
|| cpu_family_model == PCM::ICX
|| cpu_family_model == PCM::ADL
|| cpu_family_model == PCM::RPL
|| cpu_family_model == PCM::MTL
|| cpu_family_model == PCM::LNL
|| cpu_family_model == PCM::ARL
|| cpu_family_model == PCM::SPR
|| cpu_family_model == PCM::EMR
|| cpu_family_model == PCM::GNR
|| cpu_family_model == PCM::SRF
|| cpu_family_model == PCM::GRR
);
}

bool packageThermalMetricsAvailable() const
{
return packageEnergyMetricsAvailable();
Expand Down Expand Up @@ -3368,6 +3388,25 @@ uint64 getConsumedEnergy(const int powerPlane, const CounterStateType& before, c
return after.PPEnergyStatus[powerPlane] - before.PPEnergyStatus[powerPlane];
}

/*! \brief Returns energy consumed by system
\param before CPU counter state before the experiment
\param after CPU counter state after the experiment
*/
template <class CounterStateType>
uint64 getSystemConsumedEnergy(const CounterStateType& before, const CounterStateType& after)
{
return after.systemEnergyStatus - before.systemEnergyStatus;
}

/*! \brief Checks is systemEnergyStatusValid is valid in the state
* \param s CPU counter state
*/
template <class CounterStateType>
bool systemEnergyStatusValid(const CounterStateType& s)
{
return s.systemEnergyStatus != 0;
}

/*! \brief Returns energy consumed by DRAM (measured in internal units)
\param before CPU counter state before the experiment
\param after CPU counter state after the experiment
Expand Down Expand Up @@ -3435,6 +3474,31 @@ double getConsumedJoules(const int powerPlane, const CounterStateType& before, c
return double(getConsumedEnergy(powerPlane, before, after)) * m->getJoulesPerEnergyUnit();
}

/*! \brief Returns Joules consumed by system
\param before CPU counter state before the experiment
\param after CPU counter state after the experiment
*/
template <class CounterStateType>
double getSystemConsumedJoules(const CounterStateType& before, const CounterStateType& after)
{
PCM* m = PCM::getInstance();
if (!m) return -1.;

auto unit = m->getJoulesPerEnergyUnit();

switch (m->getCPUFamilyModel())
{
case PCM::SPR:
case PCM::EMR:
case PCM::GNR:
case PCM::SRF:
unit = 1.0;
break;
}

return double(getSystemConsumedEnergy(before, after)) * unit;
}

/*! \brief Returns Joules consumed by DRAM
\param before CPU counter state before the experiment
\param after CPU counter state after the experiment
Expand Down Expand Up @@ -3860,11 +3924,14 @@ class SystemCounterState : public SocketCounterState
friend std::vector<uint64> getPCICFGEvent(const PCM::RawEventEncoding& eventEnc, const SystemCounterState& before, const SystemCounterState& after);
friend std::vector<uint64> getMMIOEvent(const PCM::RawEventEncoding& eventEnc, const SystemCounterState& before, const SystemCounterState& after);
friend std::vector<uint64> getPMTEvent(const PCM::RawEventEncoding& eventEnc, const SystemCounterState& before, const SystemCounterState& after);
template <class CounterStateType> friend bool systemEnergyStatusValid(const CounterStateType& s);
template <class CounterStateType> friend uint64 getSystemConsumedEnergy(const CounterStateType& before, const CounterStateType& after);

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

SystemCounterState() :
uncoreTSC(0)
uncoreTSC(0),
systemEnergyStatus(0)
{
PCM * m = PCM::getInstance();
accel_counters.resize(m->getNumberofAccelCounters());
Expand Down Expand Up @@ -3922,6 +3990,7 @@ class SystemCounterState : public SocketCounterState

return *this;
}

virtual ~ SystemCounterState() {}
};

Expand Down
7 changes: 6 additions & 1 deletion src/pcm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,12 @@ void print_output(PCM * m,
cout << resetColor();
cout << setNextColor() << "\n Instructions retired: " << unit_format(getInstructionsRetired(sstate1, sstate2)) << " ;"
<< setNextColor() << " Active cycles: " << unit_format(getCycles(sstate1, sstate2)) << " ;"
<< setNextColor() << " Time (TSC): " << unit_format(getInvariantTSC(cstates1[0], cstates2[0])) << "ticks;\n\n";
<< setNextColor() << " Time (TSC): " << unit_format(getInvariantTSC(cstates1[0], cstates2[0])) << "ticks;";
if (m->systemEnergyMetricAvailable() && systemEnergyStatusValid(sstate1) && systemEnergyStatusValid(sstate2))
{
cout << setNextColor() << " SYS energy: " << getSystemConsumedJoules(sstate1, sstate2) << " J;";
}
cout << "\n\n";

cout << resetColor() << setNextColor() << " Core C-state residencies: "<< setNextColor() << "C0 (active,non-halted): " << (getCoreCStateResidency(0, sstate1, sstate2)*100.) << " %;";
for (int s = 1; s <= PCM::MAX_C_STATE; ++s)
Expand Down
1 change: 1 addition & 0 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ constexpr auto MSR_SMI_COUNT = 0x34;
*/

constexpr auto MSR_PKG_ENERGY_STATUS = 0x611;
constexpr auto MSR_SYS_ENERGY_STATUS = 0x64D;
constexpr auto MSR_RAPL_POWER_UNIT = 0x606;
constexpr auto MSR_PKG_POWER_INFO = 0x614;

Expand Down
12 changes: 10 additions & 2 deletions src/width_extender.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,20 @@ class CounterWidthExtender
{
std::shared_ptr<SafeMsrHandle> msr;
uint64 msr_addr;
MsrHandleCounter(std::shared_ptr<SafeMsrHandle> msr_, uint64 msr_addr_) : msr(msr_), msr_addr(msr_addr_) { }
uint64 msr_mask;
MsrHandleCounter( std::shared_ptr<SafeMsrHandle> msr_,
const uint64 msr_addr_,
const uint64 msr_mask_ = ~uint64(0ULL)) :
msr(msr_),
msr_addr(msr_addr_),
msr_mask(msr_mask_)
{
}
uint64 operator () ()
{
uint64 value = 0;
msr->read(msr_addr, &value);
return value;
return value & msr_mask;
}
};

Expand Down

0 comments on commit 55cf22a

Please sign in to comment.