@@ -284,12 +284,8 @@ bool CPUStats::ReadcpuTempFile(int& temp) {
284284 if (!m_cpuTempFile)
285285 return false ;
286286
287- rewind (m_cpuTempFile);
288- fflush (m_cpuTempFile);
289- bool ret = (fscanf (m_cpuTempFile, " %d" , &temp) == 1 );
290- temp = temp / 1000 ;
291-
292- return ret;
287+ temp = read_as<int >(m_cpuTempFile).value_or (0 ) / 1000 ;
288+ return temp == 0 ;
293289}
294290
295291bool CPUStats::UpdateCpuTemp () {
@@ -314,44 +310,32 @@ static bool get_cpu_power_k10temp(CPUPowerData* cpuPowerData, float& power) {
314310
315311 if (powerData_k10temp->corePowerFile || powerData_k10temp->socPowerFile )
316312 {
317- rewind (powerData_k10temp->corePowerFile );
318- rewind (powerData_k10temp->socPowerFile );
319- fflush (powerData_k10temp->corePowerFile );
320- fflush (powerData_k10temp->socPowerFile );
321- int corePower, socPower;
322- if (fscanf (powerData_k10temp->corePowerFile , " %d" , &corePower) != 1 )
323- goto voltagebased;
324- if (fscanf (powerData_k10temp->socPowerFile , " %d" , &socPower) != 1 )
325- goto voltagebased;
326- power = (corePower + socPower) / 1000000 ;
327- return true ;
313+ auto const corePower = read_as<int >(powerData_k10temp->corePowerFile );
314+ auto const socPower = read_as<int >(powerData_k10temp->socPowerFile );
315+ if (corePower and socPower) {
316+ power = (corePower.value () + socPower.value ()) / 1000000 ;
317+ return true ;
318+ }
328319 }
329- voltagebased:
320+
330321 if (!powerData_k10temp->coreVoltageFile || !powerData_k10temp->coreCurrentFile || !powerData_k10temp->socVoltageFile || !powerData_k10temp->socCurrentFile )
331322 return false ;
332- rewind (powerData_k10temp->coreVoltageFile );
333- rewind (powerData_k10temp->coreCurrentFile );
334- rewind (powerData_k10temp->socVoltageFile );
335- rewind (powerData_k10temp->socCurrentFile );
336-
337- fflush (powerData_k10temp->coreVoltageFile );
338- fflush (powerData_k10temp->coreCurrentFile );
339- fflush (powerData_k10temp->socVoltageFile );
340- fflush (powerData_k10temp->socCurrentFile );
341-
342- int coreVoltage, coreCurrent;
343- int socVoltage, socCurrent;
344323
345- if (fscanf (powerData_k10temp->coreVoltageFile , " %d" , &coreVoltage) != 1 )
324+ auto const coreVoltage = read_as<int >(powerData_k10temp->coreVoltageFile );
325+ if (not coreVoltage)
346326 return false ;
347- if (fscanf (powerData_k10temp->coreCurrentFile , " %d" , &coreCurrent) != 1 )
327+ auto const coreCurrent = read_as<int >(powerData_k10temp->coreCurrentFile );
328+ if (not coreCurrent)
348329 return false ;
349- if (fscanf (powerData_k10temp->socVoltageFile , " %d" , &socVoltage) != 1 )
330+ auto const socVoltage = read_as<int >(powerData_k10temp->socVoltageFile );
331+ if (not socVoltage)
350332 return false ;
351- if (fscanf (powerData_k10temp->socCurrentFile , " %d" , &socCurrent) != 1 )
333+ auto const socCurrent = read_as<int >(powerData_k10temp->socCurrentFile );
334+ if (not socCurrent)
352335 return false ;
353336
354- power = (coreVoltage * coreCurrent + socVoltage * socCurrent) / 1000000 ;
337+ power = (coreVoltage.value () * coreCurrent.value () +
338+ socVoltage.value () * socCurrent.value ()) / 1000000 ;
355339
356340 return true ;
357341}
@@ -362,20 +346,14 @@ static bool get_cpu_power_zenpower(CPUPowerData* cpuPowerData, float& power) {
362346 if (!powerData_zenpower->corePowerFile || !powerData_zenpower->socPowerFile )
363347 return false ;
364348
365- rewind (powerData_zenpower->corePowerFile );
366- rewind (powerData_zenpower->socPowerFile );
367-
368- fflush (powerData_zenpower->corePowerFile );
369- fflush (powerData_zenpower->socPowerFile );
370-
371- int corePower, socPower;
372-
373- if (fscanf (powerData_zenpower->corePowerFile , " %d" , &corePower) != 1 )
349+ auto const corePower = read_as<int >(powerData_zenpower->corePowerFile );
350+ if (not corePower)
374351 return false ;
375- if (fscanf (powerData_zenpower->socPowerFile , " %d" , &socPower) != 1 )
352+ auto const socPower = read_as<int >(powerData_zenpower->socPowerFile );
353+ if (not socPower)
376354 return false ;
377355
378- power = (corePower + socPower) / 1000000 ;
356+ power = (corePower. value () + socPower. value () ) / 1000000 ;
379357
380358 return true ;
381359}
@@ -385,23 +363,20 @@ static bool get_cpu_power_zenergy(CPUPowerData* cpuPowerData, float& power) {
385363 if (!powerData_zenergy->energyCounterFile )
386364 return false ;
387365
388- rewind (powerData_zenergy->energyCounterFile );
389- fflush (powerData_zenergy->energyCounterFile );
390-
391- uint64_t energyCounterValue = 0 ;
392- if (fscanf (powerData_zenergy->energyCounterFile , " %" SCNu64, &energyCounterValue) != 1 )
366+ auto const energyCounterValue = read_as<uint64_t >(powerData_zenergy->energyCounterFile );
367+ if (not energyCounterValue)
393368 return false ;
394369
395370 Clock::time_point now = Clock::now ();
396371 Clock::duration timeDiff = now - powerData_zenergy->lastCounterValueTime ;
397372 int64_t timeDiffMicro = std::chrono::duration_cast<std::chrono::microseconds>(timeDiff).count ();
398- uint64_t energyCounterDiff = energyCounterValue - powerData_zenergy->lastCounterValue ;
373+ uint64_t energyCounterDiff = energyCounterValue. value () - powerData_zenergy->lastCounterValue ;
399374
400375
401376 if (powerData_zenergy->lastCounterValue > 0 && energyCounterValue > powerData_zenergy->lastCounterValue )
402377 power = (float ) energyCounterDiff / (float ) timeDiffMicro;
403378
404- powerData_zenergy->lastCounterValue = energyCounterValue;
379+ powerData_zenergy->lastCounterValue = energyCounterValue. value () ;
405380 powerData_zenergy->lastCounterValueTime = now;
406381
407382 return true ;
@@ -413,22 +388,19 @@ static bool get_cpu_power_rapl(CPUPowerData* cpuPowerData, float& power) {
413388 if (!powerData_rapl->energyCounterFile )
414389 return false ;
415390
416- rewind (powerData_rapl->energyCounterFile );
417- fflush (powerData_rapl->energyCounterFile );
418-
419- uint64_t energyCounterValue = 0 ;
420- if (fscanf (powerData_rapl->energyCounterFile , " %" SCNu64, &energyCounterValue) != 1 )
391+ auto const energyCounterValue = read_as<uint64_t >(powerData_rapl->energyCounterFile );
392+ if (not energyCounterValue)
421393 return false ;
422394
423395 Clock::time_point now = Clock::now ();
424396 Clock::duration timeDiff = now - powerData_rapl->lastCounterValueTime ;
425397 int64_t timeDiffMicro = std::chrono::duration_cast<std::chrono::microseconds>(timeDiff).count ();
426- uint64_t energyCounterDiff = energyCounterValue - powerData_rapl->lastCounterValue ;
398+ uint64_t energyCounterDiff = energyCounterValue. value () - powerData_rapl->lastCounterValue ;
427399
428400 if (powerData_rapl->lastCounterValue > 0 && energyCounterValue > powerData_rapl->lastCounterValue )
429401 power = energyCounterDiff / timeDiffMicro;
430402
431- powerData_rapl->lastCounterValue = energyCounterValue;
403+ powerData_rapl->lastCounterValue = energyCounterValue. value () ;
432404 powerData_rapl->lastCounterValueTime = now;
433405
434406 return true ;
@@ -450,14 +422,11 @@ static bool get_cpu_power_xgene(CPUPowerData* cpuPowerData, float& power) {
450422 if (!powerData_xgene->powerFile )
451423 return false ;
452424
453- rewind (powerData_xgene->powerFile );
454- fflush (powerData_xgene->powerFile );
455-
456- uint64_t powerValue = 0 ;
457- if (fscanf (powerData_xgene->powerFile , " %" SCNu64, &powerValue) != 1 )
425+ auto const powerValue = read_as<uint64_t >(powerData_xgene->powerFile );
426+ if (not powerValue)
458427 return false ;
459428
460- power = (float ) powerValue / 1000000 .0f ;
429+ power = (float ) powerValue. value () / 1000000 .0f ;
461430
462431 return true ;
463432}
0 commit comments