aboutsummaryrefslogtreecommitdiffstats
path: root/linux/LibSensors.c
diff options
context:
space:
mode:
authorDaniel Lange <DLange@git.local>2021-01-11 20:43:28 +0100
committerDaniel Lange <DLange@git.local>2021-01-11 20:43:28 +0100
commit94123a215ba971baf4ad7e12cc479258ddb8600e (patch)
tree6f053d8e1ce754ab7104b7364be9754231ab84de /linux/LibSensors.c
parent50cb99f0acecb15992f3d5610c02676a6c94f2a4 (diff)
parentc55320e9e2a8916e911bcd39ab37b79e3a7d03b2 (diff)
downloaddebian_htop-94123a215ba971baf4ad7e12cc479258ddb8600e.tar.gz
debian_htop-94123a215ba971baf4ad7e12cc479258ddb8600e.tar.bz2
debian_htop-94123a215ba971baf4ad7e12cc479258ddb8600e.zip
Update upstream source from tag 'upstream/3.0.5'
Update to upstream version '3.0.5' with Debian dir 10922042e094a59dc72f9c39ddd4bfd59c7a302c
Diffstat (limited to 'linux/LibSensors.c')
-rw-r--r--linux/LibSensors.c81
1 files changed, 73 insertions, 8 deletions
diff --git a/linux/LibSensors.c b/linux/LibSensors.c
index a30e21b..158829a 100644
--- a/linux/LibSensors.c
+++ b/linux/LibSensors.c
@@ -4,6 +4,7 @@
#include <dlfcn.h>
#include <errno.h>
+#include <math.h>
#include <sensors/sensors.h>
#include "XUtils.h"
@@ -16,13 +17,20 @@ static int (*sym_sensors_snprintf_chip_name)(char*, size_t, const sensors_chip_n
static const sensors_feature* (*sym_sensors_get_features)(const sensors_chip_name*, int*);
static const sensors_subfeature* (*sym_sensors_get_subfeature)(const sensors_chip_name*, const sensors_feature*, sensors_subfeature_type);
static int (*sym_sensors_get_value)(const sensors_chip_name*, int, double*);
+static char* (*sym_sensors_get_label)(const sensors_chip_name*, const sensors_feature*);
static void* dlopenHandle = NULL;
int LibSensors_init(FILE* input) {
if (!dlopenHandle) {
+ /* Find the unversioned libsensors.so (symlink) and prefer that, but Debian has .so.5 and Fedora .so.4 without
+ matching symlinks (unless people install the -dev packages) */
dlopenHandle = dlopen("libsensors.so", RTLD_LAZY);
if (!dlopenHandle)
+ dlopenHandle = dlopen("libsensors.so.5", RTLD_LAZY);
+ if (!dlopenHandle)
+ dlopenHandle = dlopen("libsensors.so.4", RTLD_LAZY);
+ if (!dlopenHandle)
goto dlfailure;
/* Clear any errors */
@@ -41,6 +49,7 @@ int LibSensors_init(FILE* input) {
resolve(sensors_get_features);
resolve(sensors_get_subfeature);
resolve(sensors_get_value);
+ resolve(sensors_get_label);
#undef resolve
}
@@ -64,17 +73,23 @@ void LibSensors_cleanup(void) {
}
}
-int LibSensors_getCPUTemperatures(CPUData* cpus, int cpuCount) {
+void LibSensors_getCPUTemperatures(CPUData* cpus, unsigned int cpuCount) {
+ for (unsigned int i = 0; i <= cpuCount; i++)
+ cpus[i].temperature = NAN;
+
if (!dlopenHandle)
- return -ENOTSUP;
+ return;
- int tempCount = 0;
+ unsigned int coreTempCount = 0;
int n = 0;
for (const sensors_chip_name *chip = sym_sensors_get_detected_chips(NULL, &n); chip; chip = sym_sensors_get_detected_chips(NULL, &n)) {
char buffer[32];
sym_sensors_snprintf_chip_name(buffer, sizeof(buffer), chip);
- if (!String_startsWith(buffer, "coretemp") && !String_startsWith(buffer, "cpu_thermal"))
+ if (!String_startsWith(buffer, "coretemp") &&
+ !String_startsWith(buffer, "cpu_thermal") &&
+ !String_startsWith(buffer, "k10temp") &&
+ !String_startsWith(buffer, "zenpower"))
continue;
int m = 0;
@@ -82,7 +97,27 @@ int LibSensors_getCPUTemperatures(CPUData* cpus, int cpuCount) {
if (feature->type != SENSORS_FEATURE_TEMP)
continue;
- if (feature->number > cpuCount)
+ char* label = sym_sensors_get_label(chip, feature);
+ if (!label)
+ continue;
+
+ unsigned int tempId;
+ if (String_startsWith(label, "Package ")) {
+ tempId = 0;
+ } else if (String_startsWith(label, "temp")) {
+ /* Raspberry Pi has only temp1 */
+ tempId = 0;
+ } else if (String_startsWith(label, "Tdie")) {
+ tempId = 0;
+ } else if (String_startsWith(label, "Core ")) {
+ tempId = 1 + atoi(label + strlen("Core "));
+ } else {
+ tempId = UINT_MAX;
+ }
+
+ free(label);
+
+ if (tempId > cpuCount)
continue;
const sensors_subfeature *sub_feature = sym_sensors_get_subfeature(chip, feature, SENSORS_SUBFEATURE_TEMP_INPUT);
@@ -92,13 +127,43 @@ int LibSensors_getCPUTemperatures(CPUData* cpus, int cpuCount) {
if (r != 0)
continue;
- cpus[feature->number].temperature = temp;
- tempCount++;
+ cpus[tempId].temperature = temp;
+ if (tempId > 0)
+ coreTempCount++;
}
}
}
- return tempCount;
+ const double packageTemp = cpus[0].temperature;
+
+ /* Only package temperature - copy to all cpus */
+ if (coreTempCount == 0 && !isnan(packageTemp)) {
+ for (unsigned int i = 1; i <= cpuCount; i++)
+ cpus[i].temperature = packageTemp;
+
+ return;
+ }
+
+ /* No package temperature - set to max core temperature */
+ if (isnan(packageTemp) && coreTempCount != 0) {
+ double maxTemp = NAN;
+ for (unsigned int i = 1; i <= cpuCount; i++) {
+ const double coreTemp = cpus[i].temperature;
+ if (isnan(coreTemp))
+ continue;
+
+ maxTemp = MAXIMUM(maxTemp, coreTemp);
+ }
+
+ cpus[0].temperature = maxTemp;
+ }
+
+ /* Half the temperatures, probably HT/SMT - copy to second half */
+ const unsigned int delta = cpuCount / 2;
+ if (coreTempCount == delta) {
+ for (unsigned int i = 1; i <= delta; i++)
+ cpus[i + delta].temperature = cpus[i].temperature;
+ }
}
#endif /* HAVE_SENSORS_SENSORS_H */

© 2014-2024 Faster IT GmbH | imprint | privacy policy