Rewrite to use ffDetectTemps.

This commit is contained in:
xzvf
2022-06-16 13:13:36 +02:00
parent bccd1cf960
commit dcaeab7e96
2 changed files with 19 additions and 60 deletions
+18 -59
View File
@@ -6,13 +6,16 @@
#define FF_CPU_MODULE_NAME "CPU"
#define FF_CPU_NUM_FORMAT_ARGS 15
// TODO: possibly add a config option for this
char *FF_CPU_THERMAL_ZONES[] = {
"x86_pkg_temp", // x86 CPU package temperature
"cpu-thermal" // Raspberry Pi CPU temperature (possibly other ARM platforms aswell)
};
static double parseHz(FFstrbuf* content)
static long parseLong(const FFstrbuf* content) {
long value;
if(content->length == 0 || (sscanf(content->chars, "%li", &value) != 1))
return -1;
return value;
}
static double parseHz(const FFstrbuf* content)
{
if(content->length == 0)
return 0;
@@ -111,57 +114,16 @@ void ffPrintCPU(FFinstance* instance)
if(numProcs <= 1)
numProcs = physicalCores;
double cpuPackageTemp = __DBL_MAX__;
// enumerate thermal zones until we either find the x86_pkg_temp zone or we run out of zones.
// NOTE: This should generally run just a couple times (~10 iterations), but maybe we should put this behind allowSlowOperations?
for (int tzIndex = 0;; tzIndex++)
{
char path[64];
snprintf(path, sizeof(path), "/sys/class/thermal/thermal_zone%d/type", tzIndex);
FFstrbuf fileContents;
ffStrbufInit(&fileContents);
if (!ffGetFileContent(path, &fileContents))
{
// this termal zone does not exist
ffStrbufDestroy(&fileContents);
const FFTempsResult *temps = ffDetectTemps(&instance);
double cpuTemp = 0.0/0.0;
for(uint32_t i = 0; i< temps->values.length; i++) {
FFTempValue *v = ffListGet(&temps->values, i);
if(ffStrbufFirstIndexS(&v->name, "cpu") == v->name.length
&& ffStrbufCompS(&v->name, "k10temp") != 0
&& ffStrbufCompS(&v->name, "coretemp") != 0)
break;
}
bool isTargetTZ = false;
for(size_t i = 0; i < sizeof(FF_CPU_THERMAL_ZONES) / sizeof(FF_CPU_THERMAL_ZONES[0]); i++)
{
if (strncmp(fileContents.chars, FF_CPU_THERMAL_ZONES[i], fileContents.length) == 0) {
isTargetTZ = true;
break;
}
}
if(isTargetTZ)
{
snprintf(path, sizeof(path), "/sys/class/thermal/thermal_zone%d/temp", tzIndex);
ffStrbufInit(&fileContents);
if (!ffGetFileContent(path, &fileContents))
{
ffStrbufDestroy(&fileContents);
break;
}
// The temperature is encoded in millicelsius, meaning "49000" is 49C
// NOTE: for strtol errno MUST be manually reset (see manpage)
errno = 0;
long sensorValue = strtol(fileContents.chars, NULL, 10);
if (!errno)
cpuPackageTemp = (float)sensorValue / 1000.0f; // convert to celsius
ffStrbufDestroy(&fileContents);
break;
}
ffStrbufDestroy(&fileContents);
}
if (cpuPackageTemp == __DBL_MAX__)
{
ffPrintError(instance, FF_CPU_MODULE_NAME, 0, &instance->config.cpuKey, &instance->config.cpuFormat, FF_CPU_NUM_FORMAT_ARGS, "Could not find a CPU thermal zone");
cpuTemp = (double) parseLong(&v->value) / 1000.0f;
}
double ghz = biosLimit;
@@ -223,9 +185,6 @@ void ffPrintCPU(FFinstance* instance)
if(ghz > 0)
ffStrbufAppendF(&cpu, " @ %.9gGHz", ghz);
if (cpuPackageTemp != __DBL_MAX__)
ffStrbufAppendF(&cpu, " (%g°C)", cpuPackageTemp);
ffPrintAndWriteToCache(instance, FF_CPU_MODULE_NAME, &instance->config.cpuKey, &cpu, &instance->config.cpuFormat, FF_CPU_NUM_FORMAT_ARGS, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRBUF, &name},
@@ -235,7 +194,7 @@ void ffPrintCPU(FFinstance* instance)
{FF_FORMAT_ARG_TYPE_INT, &numProcsAvailable},
{FF_FORMAT_ARG_TYPE_INT, &physicalCores},
{FF_FORMAT_ARG_TYPE_INT, &numProcs},
{FF_FORMAT_ARG_TYPE_DOUBLE, &cpuPackageTemp},
{FF_FORMAT_ARG_TYPE_DOUBLE, &cpuTemp},
{FF_FORMAT_ARG_TYPE_DOUBLE, &biosLimit},
{FF_FORMAT_ARG_TYPE_DOUBLE, &scalingMaxFreq},
{FF_FORMAT_ARG_TYPE_DOUBLE, &scalingMinFreq},