From 3e763b04c43ebc78506e552e49ef2243fcd6cc4c Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Mon, 25 Dec 2023 14:54:11 +0100 Subject: [PATCH] linux: assign CPU temperatures by package/core or CCD Closes #806. Closes #1048. Closes #1176. Closes #1335. Addresses #879 (on Linux). --- linux/LibSensors.c | 48 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/linux/LibSensors.c b/linux/LibSensors.c index 844bbd69d..08997b3e1 100644 --- a/linux/LibSensors.c +++ b/linux/LibSensors.c @@ -16,6 +16,7 @@ in the source distribution for its full text. #include #include #include +#include #include #include #include @@ -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); @@ -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) @@ -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; @@ -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;