diff --git a/src/cpu.cpp b/src/cpu.cpp index 57b62c82..87f20d9e 100644 --- a/src/cpu.cpp +++ b/src/cpu.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #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(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; diff --git a/src/cpu.h b/src/cpu.h index f4b3b2a0..b7f37b11 100644 --- a/src/cpu.h +++ b/src/cpu.h @@ -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; };