From 5e96ba838437a1ea66b1ff6ab0e7ca2f0685ab9c Mon Sep 17 00:00:00 2001 From: Linus Dierheimer Date: Thu, 16 Jun 2022 14:55:02 +0200 Subject: [PATCH] Fix cpu temp detection the second --- src/modules/cpu.c | 51 +++++++++++++++++++++------------------------ src/util/FFstrbuf.c | 9 ++++++++ src/util/FFstrbuf.h | 2 ++ 3 files changed, 35 insertions(+), 27 deletions(-) diff --git a/src/modules/cpu.c b/src/modules/cpu.c index 86e4ef3f6..be1b5bfdf 100644 --- a/src/modules/cpu.c +++ b/src/modules/cpu.c @@ -6,24 +6,6 @@ #define FF_CPU_MODULE_NAME "CPU" #define FF_CPU_NUM_FORMAT_ARGS 15 -static long parseLong(const FFstrbuf* content) -{ - long value; - if(sscanf(content->chars, "%li", &value) != 1) - return -1; - - return value; -} - -static double parseDouble(const FFstrbuf* content) -{ - double herz; - if(sscanf(content->chars, "%lf", &herz) != 1) - return 0; - - return herz; -} - static double getGhz(const char* policyFile, const char* cpuFile) { FFstrbuf content; @@ -33,10 +15,14 @@ static double getGhz(const char* policyFile, const char* cpuFile) if(content.length == 0) ffGetFileContent(cpuFile, &content); - double herz = parseDouble(&content); + double herz = ffStrbufToDouble(&content); ffStrbufDestroy(&content); + //ffStrbufToDouble failed + if(herz != herz) + return 0; + herz /= 1000.0; //to MHz return herz / 1000.0; //to GHz } @@ -88,7 +74,12 @@ void ffPrintCPU(FFinstance* instance) fclose(cpuinfo); - double procGhz = parseDouble(&procGhzString) / 1000.0; //to GHz + double procGhz = ffStrbufToDouble(&procGhzString); + if(procGhz != procGhz) + procGhz = 0; //NaN + else + procGhz /= 1000.0; //To GHz + ffStrbufDestroy(&procGhzString); double biosLimit = getGhz("/sys/devices/system/cpu/cpufreq/policy0/bios_limit", "/sys/devices/system/cpu/cpu0/cpufreq/bios_limit"); @@ -158,13 +149,19 @@ void ffPrintCPU(FFinstance* instance) FFTempValue* value = ffListGet(&temps->values, i); if( - ffStrbufFirstIndexS(&value->name, "cpu") < value->name.length || - ffStrbufCompS(&value->name, "k10temp") == 0 || - ffStrbufCompS(&value->name, "coretemp") == 0 - ) { - cpuTemp = (double) parseLong(&value->value) / 1000.0; - break; - } + ffStrbufFirstIndexS(&value->name, "cpu") == value->name.length && + ffStrbufCompS(&value->name, "k10temp") != 0 && + ffStrbufCompS(&value->name, "coretemp") != 0 + ) continue; + + double temp = ffStrbufToDouble(&value->value); + + //NaN + if(temp != temp) + continue; + + cpuTemp = temp / 1000.0; + break; } FFstrbuf cpu; diff --git a/src/util/FFstrbuf.c b/src/util/FFstrbuf.c index 92d33be4f..354847063 100644 --- a/src/util/FFstrbuf.c +++ b/src/util/FFstrbuf.c @@ -526,6 +526,15 @@ void ffStrbufPutTo(const FFstrbuf* strbuf, FILE* file) fputc('\n', file); } +double ffStrbufToDouble(const FFstrbuf* strbuf) +{ + double value; + if(sscanf(strbuf->chars, "%lf", &value) != 1) + return 0.0 / 0.0; //NaN + + return value; +} + void ffStrbufDestroy(FFstrbuf* strbuf) { free(strbuf->chars); diff --git a/src/util/FFstrbuf.h b/src/util/FFstrbuf.h index 076c4e467..1cd076fec 100644 --- a/src/util/FFstrbuf.h +++ b/src/util/FFstrbuf.h @@ -93,6 +93,8 @@ bool ffStrbufRemoveIgnCaseEndS(FFstrbuf* strbuf, const char* end); void ffStrbufWriteTo(const FFstrbuf* strbuf, FILE* file); void ffStrbufPutTo(const FFstrbuf* strbuf, FILE* file); +double ffStrbufToDouble(const FFstrbuf* strbuf); + void ffStrbufDestroy(FFstrbuf* strbuf); #endif