Commit 3ec9c4ad authored by Michael Ott's avatar Michael Ott
Browse files

Handle situations where another program re-programs the fixed counters

parent 67df046c
......@@ -76,7 +76,11 @@ class MSRSensorBase : public SensorBase {
void setMetric(uint64_t metric) {
_metric = metric;
}
void setFirstReading(bool val) {
_firstReading = val;
}
void printConfig(LOG_LEVEL ll, LOGGER &lg, unsigned leadingSpaces = 16) {
std::string leading(leadingSpaces, ' ');
LOG_VAR(ll) << leading << " CPU: " << _cpu;
......
......@@ -131,12 +131,17 @@ void MSRSensorGroup::read() {
try {
for (auto s : _sensors) {
auto ret_val = msr_read(s->getMetric(), &reading.value, s->getCpu());
if (ret_val != -1) {
s->storeReading(reading, 1, !_htAggregation); //1 is no correction...
if (checkStatus(s->getMetric(), s->getCpu()) == 0) {
auto ret_val = msr_read(s->getMetric(), &reading.value, s->getCpu());
if (ret_val != -1) {
s->storeReading(reading, 1, !_htAggregation); //1 is no correction...
#ifdef DEBUG
LOG(debug) << _groupName << "::" << s->getName() << " raw reading: \"" << reading.value << "\"";
LOG(debug) << _groupName << "::" << s->getName() << " raw reading: \"" << reading.value << "\"";
#endif
}
} else {
LOG(debug) << _groupName << "::" << s->getName() << " has been disabled, ignoring reading";
s->setFirstReading(true);
}
}
......@@ -160,8 +165,28 @@ void MSRSensorGroup::read() {
} catch (const std::exception &e) {
LOG(error) << "Sensorgroup " << _groupName << " could not read value: " << e.what();
}
program_fixed();
}
int MSRSensorGroup::checkStatus(uint64_t msr_number, unsigned int cpu) {
if (!_sensorBins[cpu].isActive()) { // CPU is not active, so it won't be programmed
return 0;
}
struct FixedEventControlRegister ctrl_reg;
if (msr_read(IA32_CR_FIXED_CTR_CTRL, &ctrl_reg.value, cpu) != -1) {
if ((msr_number == INST_RETIRED_ANY_ADDR) && !(ctrl_reg.fields.os0 && ctrl_reg.fields.usr0))
return 1;
else if ((msr_number == CPU_CLK_UNHALTED_THREAD_ADDR) && !(ctrl_reg.fields.os1 && ctrl_reg.fields.usr1))
return 1;
else if ((msr_number == CPU_CLK_UNHALTED_REF_ADDR) && !(ctrl_reg.fields.os2 && ctrl_reg.fields.usr2))
return 1;
}
return 0;
}
int32_t MSRSensorGroup::msr_read(uint64_t msr_number, uint64_t *value, unsigned int cpu) {
return pread(_sensorBins[cpu].getFd(), (void *)value, sizeof(uint64_t), msr_number);
}
......@@ -177,7 +202,7 @@ int32_t MSRSensorGroup::msr_write(uint64_t msr_number, uint64_t value, unsigned
* because the counters are already in use.
*/
void MSRSensorGroup::program_fixed() {
unsigned int freeRunning = 0;
for (unsigned int cpu = 0; cpu < _sensorBins.size(); ++cpu) {
if (!_sensorBins[cpu].isActive()) { // CPU is not active, so it won't be programmed
continue;
......@@ -192,7 +217,7 @@ void MSRSensorGroup::program_fixed() {
//are they all enabled?
if (ctrl_reg.fields.os0 && ctrl_reg.fields.usr0 && ctrl_reg.fields.os1 && ctrl_reg.fields.usr1 && ctrl_reg.fields.os2 && ctrl_reg.fields.usr2) {
//yes! Free running counters were set by someone else => we don't need to program them, just read them.
LOG(debug) << "CPU" << cpu << " has free running counter, so there will be no fixed counter programming";
freeRunning++;
continue;
}
//not all of them (or none) are enabled => we program them again
......@@ -225,6 +250,10 @@ void MSRSensorGroup::program_fixed() {
//uint64_t value = (1ULL << 32) + (1ULL << 33) + (1ULL << 34);
msr_write(IA32_CR_PERF_GLOBAL_CTRL, value, cpu);
}
if (freeRunning != _sensorBins.size()) {
LOG(debug) << "Programmed fixed counters on " << _sensorBins.size()-freeRunning << " CPUs, " << freeRunning << " were already free runnning";
}
}
void MSRSensorGroup::addCpu(unsigned int cpu) {
......
......@@ -99,6 +99,7 @@ class MSRSensorGroup : public SensorGroupTemplate<MSRSensorBase> {
};
void program_fixed();
int checkStatus(uint64_t msr_number, unsigned int cpu);
unsigned int _htAggregation; /**< Value for hyper-threading aggregation. Zero indicates disabled HT agg. */
//int _number_metrics_per_cpu;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment