Skip to content

Commit 5e96ba8

Browse files
Fix cpu temp detection the second
1 parent 481affa commit 5e96ba8

File tree

3 files changed

+35
-27
lines changed

3 files changed

+35
-27
lines changed

src/modules/cpu.c

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,6 @@
66
#define FF_CPU_MODULE_NAME "CPU"
77
#define FF_CPU_NUM_FORMAT_ARGS 15
88

9-
static long parseLong(const FFstrbuf* content)
10-
{
11-
long value;
12-
if(sscanf(content->chars, "%li", &value) != 1)
13-
return -1;
14-
15-
return value;
16-
}
17-
18-
static double parseDouble(const FFstrbuf* content)
19-
{
20-
double herz;
21-
if(sscanf(content->chars, "%lf", &herz) != 1)
22-
return 0;
23-
24-
return herz;
25-
}
26-
279
static double getGhz(const char* policyFile, const char* cpuFile)
2810
{
2911
FFstrbuf content;
@@ -33,10 +15,14 @@ static double getGhz(const char* policyFile, const char* cpuFile)
3315
if(content.length == 0)
3416
ffGetFileContent(cpuFile, &content);
3517

36-
double herz = parseDouble(&content);
18+
double herz = ffStrbufToDouble(&content);
3719

3820
ffStrbufDestroy(&content);
3921

22+
//ffStrbufToDouble failed
23+
if(herz != herz)
24+
return 0;
25+
4026
herz /= 1000.0; //to MHz
4127
return herz / 1000.0; //to GHz
4228
}
@@ -88,7 +74,12 @@ void ffPrintCPU(FFinstance* instance)
8874

8975
fclose(cpuinfo);
9076

91-
double procGhz = parseDouble(&procGhzString) / 1000.0; //to GHz
77+
double procGhz = ffStrbufToDouble(&procGhzString);
78+
if(procGhz != procGhz)
79+
procGhz = 0; //NaN
80+
else
81+
procGhz /= 1000.0; //To GHz
82+
9283
ffStrbufDestroy(&procGhzString);
9384

9485
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)
158149
FFTempValue* value = ffListGet(&temps->values, i);
159150

160151
if(
161-
ffStrbufFirstIndexS(&value->name, "cpu") < value->name.length ||
162-
ffStrbufCompS(&value->name, "k10temp") == 0 ||
163-
ffStrbufCompS(&value->name, "coretemp") == 0
164-
) {
165-
cpuTemp = (double) parseLong(&value->value) / 1000.0;
166-
break;
167-
}
152+
ffStrbufFirstIndexS(&value->name, "cpu") == value->name.length &&
153+
ffStrbufCompS(&value->name, "k10temp") != 0 &&
154+
ffStrbufCompS(&value->name, "coretemp") != 0
155+
) continue;
156+
157+
double temp = ffStrbufToDouble(&value->value);
158+
159+
//NaN
160+
if(temp != temp)
161+
continue;
162+
163+
cpuTemp = temp / 1000.0;
164+
break;
168165
}
169166

170167
FFstrbuf cpu;

src/util/FFstrbuf.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,15 @@ void ffStrbufPutTo(const FFstrbuf* strbuf, FILE* file)
526526
fputc('\n', file);
527527
}
528528

529+
double ffStrbufToDouble(const FFstrbuf* strbuf)
530+
{
531+
double value;
532+
if(sscanf(strbuf->chars, "%lf", &value) != 1)
533+
return 0.0 / 0.0; //NaN
534+
535+
return value;
536+
}
537+
529538
void ffStrbufDestroy(FFstrbuf* strbuf)
530539
{
531540
free(strbuf->chars);

src/util/FFstrbuf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ bool ffStrbufRemoveIgnCaseEndS(FFstrbuf* strbuf, const char* end);
9393
void ffStrbufWriteTo(const FFstrbuf* strbuf, FILE* file);
9494
void ffStrbufPutTo(const FFstrbuf* strbuf, FILE* file);
9595

96+
double ffStrbufToDouble(const FFstrbuf* strbuf);
97+
9698
void ffStrbufDestroy(FFstrbuf* strbuf);
9799

98100
#endif

0 commit comments

Comments
 (0)