Skip to content

Commit

Permalink
Linux/LibSensors: adjust data for k10temp
Browse files Browse the repository at this point in the history
Adjust the temperature data gathered from the k10temp driver.  Only a
control value, an optional die value, and one value per CCD are
provided.

See https://www.kernel.org/doc/html/latest/hwmon/k10temp.html for
details.
  • Loading branch information
cgzones committed Apr 9, 2024
1 parent 993130e commit 82601d1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
49 changes: 48 additions & 1 deletion linux/LibSensors.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ static const struct TempDriverDefs {
[TD_ACPITZ] = { "acpitz", 1 },
};

void LibSensors_getCPUTemperatures(CPUData* cpus, unsigned int existingCPUs, unsigned int activeCPUs) {
void LibSensors_getCPUTemperatures(CPUData* cpus, unsigned int existingCPUs, unsigned int activeCPUs, unsigned short maxCoreId) {
assert(existingCPUs > 0 && existingCPUs < 16384);

float* data = xMallocArray(existingCPUs + 1, sizeof(float));
Expand Down Expand Up @@ -233,6 +233,53 @@ void LibSensors_getCPUTemperatures(CPUData* cpus, unsigned int existingCPUs, uns
}
}

/*
* k10temp, see https://www.kernel.org/doc/html/latest/hwmon/k10temp.html
* temp1 = Tctl, (optional) temp2 = Tdie, temp3..temp10 = Tccd1..8
*/
if (topDriver == TD_K10TEMP) {
/* Display Tdie instead of Tctl if available */
if (!isNaN(data[1]))
data[0] = data[1];

/* Compute number of CCD entries */
unsigned int ccd_entries = 0;
for (size_t i = 2; i <= existingCPUs; i++) {
if (isNaN(data[i]))
break;

ccd_entries++;
}

if (ccd_entries == 0) {
const float ccd_temp = data[0];
for (size_t i = 1; i <= existingCPUs; i++)
data[i] = ccd_temp;
} else if (ccd_entries == 1) {
const float ccd_temp = data[2];
for (size_t i = 1; i <= existingCPUs; i++)
data[i] = ccd_temp;
} else {
assert(ccd_entries <= 64);
float ccd_data[ccd_entries];
for (size_t i = 0; i < ccd_entries; i++)
ccd_data[i] = data[i + 2];

/* Estimate the size of the CCDs, might be off of due to
* disabled cores on downgraded CPUs */
const unsigned int ccd_size = maxCoreId / ccd_entries + 1;

for (size_t i = 0; i < existingCPUs; i++) {
unsigned short coreId = cpus[i + 1].coreId;
unsigned int index = MINIMUM(coreId / ccd_size, ccd_entries - 1);
data[i + 1] = ccd_data[index];
}
}

/* No further adjustments */
goto out;
}

/* Adjust data for chips not providing a platform temperature */
if (coreTempCount + 1 == activeCPUs || coreTempCount + 1 == activeCPUs / 2) {
memmove(&data[1], &data[0], existingCPUs * sizeof(*data));
Expand Down
2 changes: 1 addition & 1 deletion linux/LibSensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ int LibSensors_init(void);
void LibSensors_cleanup(void);
int LibSensors_reload(void);

void LibSensors_getCPUTemperatures(CPUData* cpus, unsigned int existingCPUs, unsigned int activeCPUs);
void LibSensors_getCPUTemperatures(CPUData* cpus, unsigned int existingCPUs, unsigned int activeCPUs, unsigned short maxCoreId);

#endif /* HEADER_LibSensors */
2 changes: 1 addition & 1 deletion linux/LinuxMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ void Machine_scan(Machine* super) {

#ifdef HAVE_SENSORS_SENSORS_H
if (settings->showCPUTemperature)
LibSensors_getCPUTemperatures(this->cpuData, super->existingCPUs, super->activeCPUs);
LibSensors_getCPUTemperatures(this->cpuData, super->existingCPUs, super->activeCPUs, this->maxCoreId);
#endif
}

Expand Down

0 comments on commit 82601d1

Please sign in to comment.