Add CPU package temperature to CPU module.

This is done by looking for a specified thermal zone in
/sys/class/thermal/thermal_zone*/ .
Unfortunately there is no better way to find the
temperature, then to check all registered zones.
This still needs to be extended to support other hardware,
right now only x86 CPUs and Raspberry pi are tested.
Maybe the FF_CPU_THERMAL_ZONES array could be user configurable,
but I could not figure out how to add an array as a config parameter.
This commit is contained in:
xzvf
2022-06-15 16:14:46 +02:00
parent ae8b5bde65
commit d8bb36f119
3 changed files with 68 additions and 3 deletions
+1 -1
View File
@@ -12,7 +12,7 @@ Here i just add things that are easy to forget.
- `libffprint`: contains the printing functions, logos, format etc
- `fastfetch` and `flashfetch`: Executables, that initialize the config of libffprint. Fist one at runtime, second one at compile time
- [ ] Better OS output for all possible combinations of /etc/os-release variables.
- [ ] Expose temperatures to CPU format string
- [X] Expose temperatures to CPU format string
- [ ] Expose temperatures to GPU format string
- [ ] Find wayland compositor by looking at \${XDG_RUNTIME_DIR}/${WAYLAND_DISPLAY:-wayland-0}
- [ ] Make LocalIP module more configurable
+2 -1
View File
@@ -236,7 +236,7 @@ static inline void printCommandHelp(const char* command)
}
else if(strcasecmp(command, "cpu-format") == 0)
{
constructAndPrintCommandHelpFormat("cpu", "{2} ({7}) @ {14}GHz", 14,
constructAndPrintCommandHelpFormat("cpu", "{2} ({7}) @ {15}GHz ({8}°C)", 15,
"CPU name",
"Prettified CPU name",
"CPU Vendor name (Vendor ID)",
@@ -244,6 +244,7 @@ static inline void printCommandHelp(const char* command)
"CPU logical core count configured",
"CPU physical core count",
"Always set core count",
"CPU package temperature",
"frequency bios limit",
"frequency scaling max",
"frequency scaling min",
+65 -1
View File
@@ -1,9 +1,16 @@
#include "fastfetch.h"
#include <string.h>
#include <errno.h>
#define FF_CPU_MODULE_NAME "CPU"
#define FF_CPU_NUM_FORMAT_ARGS 14
#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)
{
@@ -104,6 +111,59 @@ 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);
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");
}
double ghz = biosLimit;
if(ghz == 0)
ghz = scalingMaxFreq;
@@ -163,6 +223,9 @@ 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},
@@ -172,6 +235,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, &biosLimit},
{FF_FORMAT_ARG_TYPE_DOUBLE, &scalingMaxFreq},
{FF_FORMAT_ARG_TYPE_DOUBLE, &scalingMinFreq},