Fix CPU power calculations

pull/507/head
medusalix 3 years ago
parent 87f82215e8
commit 23cdf7aacf
No known key found for this signature in database
GPG Key ID: 3DEC28D3714042F3

@ -8,6 +8,7 @@
#include <string.h>
#include <algorithm>
#include <regex>
#include <inttypes.h>
#include "string_utils.h"
#ifndef PROCDIR
@ -307,15 +308,17 @@ static bool get_cpu_power_rapl(CPUPowerData* cpuPowerData, int& power) {
rewind(powerData_rapl->energyCounterFile);
fflush(powerData_rapl->energyCounterFile);
int energyCounterValue = 0;
if (fscanf(powerData_rapl->energyCounterFile, "%d", &energyCounterValue) != 1)
uint64_t energyCounterValue = 0;
if (fscanf(powerData_rapl->energyCounterFile, "%" SCNu64, &energyCounterValue) != 1)
return false;
Clock::time_point now = Clock::now();
Clock::duration timeDiff = now - powerData_rapl->lastCounterValueTime;
int energyCounterDiff = energyCounterValue - powerData_rapl->lastCounterValue;
int64_t timeDiffMicro = std::chrono::duration_cast<std::chrono::microseconds>(timeDiff).count();
uint64_t energyCounterDiff = energyCounterValue - powerData_rapl->lastCounterValue;
power = (int)((float)energyCounterDiff / (float)timeDiff.count() * 1000);
if (powerData_rapl->lastCounterValue > 0 && energyCounterValue > powerData_rapl->lastCounterValue)
power = energyCounterDiff / timeDiffMicro;
powerData_rapl->lastCounterValue = energyCounterValue;
powerData_rapl->lastCounterValueTime = now;

@ -93,6 +93,7 @@ struct CPUPowerData_zenpower : public CPUPowerData {
struct CPUPowerData_rapl : public CPUPowerData {
CPUPowerData_rapl() {
this->source = CPU_POWER_RAPL;
this->lastCounterValue = 0;
this->lastCounterValueTime = Clock::now();
};
@ -102,7 +103,7 @@ struct CPUPowerData_rapl : public CPUPowerData {
};
FILE* energyCounterFile {nullptr};
int lastCounterValue;
uint64_t lastCounterValue;
Clock::time_point lastCounterValueTime;
};

Loading…
Cancel
Save