Skip to content

Commit e87109f

Browse files
cpu: Use read_as from file_utils to unify code
[Why] Reading values from file is done basically the same way everywhere. And we now have a helper function which can be used to simplify the code. Signed-off-by: Soeren Grunewald <soeren.grunewald@gmx.net>
1 parent 1d1ac98 commit e87109f

1 file changed

Lines changed: 27 additions & 59 deletions

File tree

src/cpu.cpp

Lines changed: 27 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,8 @@ bool CPUStats::ReadcpuTempFile(int& temp) {
282282
if (!m_cpuTempFile)
283283
return false;
284284

285-
rewind(m_cpuTempFile);
286-
fflush(m_cpuTempFile);
287-
bool ret = (fscanf(m_cpuTempFile, "%d", &temp) == 1);
288-
temp = temp / 1000;
289-
290-
return ret;
285+
temp = read_as<int>(m_cpuTempFile, 0) / 1000;
286+
return temp == 0;
291287
}
292288

293289
bool CPUStats::UpdateCpuTemp() {
@@ -312,41 +308,28 @@ static bool get_cpu_power_k10temp(CPUPowerData* cpuPowerData, float& power) {
312308

313309
if(powerData_k10temp->corePowerFile || powerData_k10temp->socPowerFile)
314310
{
315-
rewind(powerData_k10temp->corePowerFile);
316-
rewind(powerData_k10temp->socPowerFile);
317-
fflush(powerData_k10temp->corePowerFile);
318-
fflush(powerData_k10temp->socPowerFile);
319-
int corePower, socPower;
320-
if (fscanf(powerData_k10temp->corePowerFile, "%d", &corePower) != 1)
321-
goto voltagebased;
322-
if (fscanf(powerData_k10temp->socPowerFile, "%d", &socPower) != 1)
323-
goto voltagebased;
324-
power = (corePower + socPower) / 1000000;
325-
return true;
311+
auto const corePower = read_as<int>(powerData_k10temp->corePowerFile, -1);
312+
auto const socPower = read_as<int>(powerData_k10temp->socPowerFile, -1);
313+
if (corePower != -1 and socPower != -1) {
314+
power = (corePower + socPower) / 1000000;
315+
return true;
316+
}
326317
}
327-
voltagebased:
318+
328319
if (!powerData_k10temp->coreVoltageFile || !powerData_k10temp->coreCurrentFile || !powerData_k10temp->socVoltageFile || !powerData_k10temp->socCurrentFile)
329320
return false;
330-
rewind(powerData_k10temp->coreVoltageFile);
331-
rewind(powerData_k10temp->coreCurrentFile);
332-
rewind(powerData_k10temp->socVoltageFile);
333-
rewind(powerData_k10temp->socCurrentFile);
334-
335-
fflush(powerData_k10temp->coreVoltageFile);
336-
fflush(powerData_k10temp->coreCurrentFile);
337-
fflush(powerData_k10temp->socVoltageFile);
338-
fflush(powerData_k10temp->socCurrentFile);
339-
340-
int coreVoltage, coreCurrent;
341-
int socVoltage, socCurrent;
342321

343-
if (fscanf(powerData_k10temp->coreVoltageFile, "%d", &coreVoltage) != 1)
322+
auto const coreVoltage = read_as<int>(powerData_k10temp->coreVoltageFile, -1);
323+
if (coreVoltage == -1)
344324
return false;
345-
if (fscanf(powerData_k10temp->coreCurrentFile, "%d", &coreCurrent) != 1)
325+
auto const coreCurrent = read_as<int>(powerData_k10temp->coreCurrentFile, -1);
326+
if (coreCurrent == -1)
346327
return false;
347-
if (fscanf(powerData_k10temp->socVoltageFile, "%d", &socVoltage) != 1)
328+
auto const socVoltage = read_as<int>(powerData_k10temp->socVoltageFile, -1);
329+
if (socVoltage == -1)
348330
return false;
349-
if (fscanf(powerData_k10temp->socCurrentFile, "%d", &socCurrent) != 1)
331+
auto const socCurrent = read_as<int>(powerData_k10temp->socCurrentFile, -1);
332+
if (socCurrent== -1)
350333
return false;
351334

352335
power = (coreVoltage * coreCurrent + socVoltage * socCurrent) / 1000000;
@@ -360,17 +343,11 @@ static bool get_cpu_power_zenpower(CPUPowerData* cpuPowerData, float& power) {
360343
if (!powerData_zenpower->corePowerFile || !powerData_zenpower->socPowerFile)
361344
return false;
362345

363-
rewind(powerData_zenpower->corePowerFile);
364-
rewind(powerData_zenpower->socPowerFile);
365-
366-
fflush(powerData_zenpower->corePowerFile);
367-
fflush(powerData_zenpower->socPowerFile);
368-
369-
int corePower, socPower;
370-
371-
if (fscanf(powerData_zenpower->corePowerFile, "%d", &corePower) != 1)
346+
auto const corePower = read_as<int>(powerData_zenpower->corePowerFile, -1);
347+
if (corePower == -1)
372348
return false;
373-
if (fscanf(powerData_zenpower->socPowerFile, "%d", &socPower) != 1)
349+
auto const socPower = read_as<int>(powerData_zenpower->socPowerFile, -1);
350+
if (socPower == -1)
374351
return false;
375352

376353
power = (corePower + socPower) / 1000000;
@@ -383,11 +360,8 @@ static bool get_cpu_power_zenergy(CPUPowerData* cpuPowerData, float& power) {
383360
if (!powerData_zenergy->energyCounterFile)
384361
return false;
385362

386-
rewind(powerData_zenergy->energyCounterFile);
387-
fflush(powerData_zenergy->energyCounterFile);
388-
389-
uint64_t energyCounterValue = 0;
390-
if (fscanf(powerData_zenergy->energyCounterFile, "%" SCNu64, &energyCounterValue) != 1)
363+
auto const energyCounterValue = read_as<uint64_t>(powerData_zenergy->energyCounterFile);
364+
if (energyCounterValue == 0)
391365
return false;
392366

393367
Clock::time_point now = Clock::now();
@@ -411,11 +385,8 @@ static bool get_cpu_power_rapl(CPUPowerData* cpuPowerData, float& power) {
411385
if (!powerData_rapl->energyCounterFile)
412386
return false;
413387

414-
rewind(powerData_rapl->energyCounterFile);
415-
fflush(powerData_rapl->energyCounterFile);
416-
417-
uint64_t energyCounterValue = 0;
418-
if (fscanf(powerData_rapl->energyCounterFile, "%" SCNu64, &energyCounterValue) != 1)
388+
auto const energyCounterValue = read_as<uint64_t>(powerData_rapl->energyCounterFile);
389+
if (energyCounterValue == 0)
419390
return false;
420391

421392
Clock::time_point now = Clock::now();
@@ -448,11 +419,8 @@ static bool get_cpu_power_xgene(CPUPowerData* cpuPowerData, float& power) {
448419
if (!powerData_xgene->powerFile)
449420
return false;
450421

451-
rewind(powerData_xgene->powerFile);
452-
fflush(powerData_xgene->powerFile);
453-
454-
uint64_t powerValue = 0;
455-
if (fscanf(powerData_xgene->powerFile, "%" SCNu64, &powerValue) != 1)
422+
auto const powerValue = read_as<uint64_t>(powerData_xgene->powerFile);
423+
if (powerValue == 0)
456424
return false;
457425

458426
power = (float) powerValue / 1000000.0f;

0 commit comments

Comments
 (0)