Skip to content

Commit

Permalink
linux: assign CPU temperatures by package/core or CCD
Browse files Browse the repository at this point in the history
Closes #806.
Closes #1048.
Closes #1176.
Closes #1335.
Addresses #879 (on Linux).
  • Loading branch information
leahneukirchen authored and BenBE committed Jan 20, 2025
1 parent c079bcc commit 3a9f468
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions linux/LibSensors.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ in the source distribution for its full text.
#include <errno.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -206,6 +207,8 @@ void LibSensors_getCPUTemperatures(CPUData* cpus, unsigned int existingCPUs, uns
unsigned int coreTempCount = 0;
int topPriority = 99;

int ccdID = 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)) {
const int priority = tempDriverPriority(chip);
Expand All @@ -223,6 +226,8 @@ void LibSensors_getCPUTemperatures(CPUData* cpus, unsigned int existingCPUs, uns

topPriority = priority;

int physicalID = -1;

int m = 0;
for (const sensors_feature* feature = sym_sensors_get_features(chip, &m); feature; feature = sym_sensors_get_features(chip, &m)) {
if (feature->type != SENSORS_FEATURE_TEMP)
Expand All @@ -238,9 +243,6 @@ void LibSensors_getCPUTemperatures(CPUData* cpus, unsigned int existingCPUs, uns
/* Feature name IDs start at 1, adjust to start at 0 to match data indices */
tempID--;

if (tempID > existingCPUs)
continue;

const sensors_subfeature* subFeature = sym_sensors_get_subfeature(chip, feature, SENSORS_SUBFEATURE_TEMP_INPUT);
if (!subFeature)
continue;
Expand Down Expand Up @@ -291,6 +293,46 @@ void LibSensors_getCPUTemperatures(CPUData* cpus, unsigned int existingCPUs, uns
}
}

char *label = sym_sensors_get_label(chip, feature);
if (label) {
bool skip = true;
/* Intel coretemp names, labels mention package and phyiscal id */
if (String_startsWith(label, "Package id ")) {
physicalID = strtoul(label + strlen("Package id "), NULL, 10);
} else if (String_startsWith(label, "Physical id ")) {
physicalID = strtoul(label + strlen("Physical id "), NULL, 10);
} else if (String_startsWith(label, "Core ")) {
int coreID = strtoul(label + strlen("Core "), NULL, 10);
for (size_t i = 1; i < existingCPUs + 1; i++) {
if (cpus[i].physicalID == physicalID && cpus[i].coreID == coreID) {
data[i] = temp;
coreTempCount++;
}
}
}

/* AMD k10temp/zenpower names, only CCD is known */
else if (String_startsWith(label, "Tccd")) {
for (size_t i = 1; i <= existingCPUs; i++) {
if (cpus[i].ccdID == ccdID) {
data[i] = temp;
coreTempCount++;
}
}
ccdID++;
} else {
skip = false;
}

free(label);

if (skip)
continue;
}

if (tempID > existingCPUs)
continue;

/* If already set, e.g. Ryzen reporting platform temperature for each die, use the bigger one */
if (isNaN(data[tempID])) {
data[tempID] = temp;
Expand Down

0 comments on commit 3a9f468

Please sign in to comment.