Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temperature reporting fix for AMD Zen CPUs #1176

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
52 changes: 44 additions & 8 deletions linux/LibSensors.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,24 +243,60 @@ void LibSensors_getCPUTemperatures(CPUData* cpus, unsigned int existingCPUs, uns
/* Check for further adjustments */
}

/* Only temperature for core 0, maybe Ryzen - copy to all other cores */
if (coreTempCount == 1 && !isnan(data[1])) {
for (unsigned int i = 2; i <= existingCPUs; i++)
data[i] = data[1];
/* Half the temperatures, probably HT/SMT - copy to second half */
const unsigned int delta = activeCPUs / 2;
if (coreTempCount == delta) {
memcpy(&data[delta + 1], &data[1], delta * sizeof(*data));

/* No further adjustments */
goto out;
}

/* Half the temperatures, probably HT/SMT - copy to second half */
const unsigned int delta = activeCPUs / 2;
if (coreTempCount == delta) {
memcpy(&data[delta + 1], &data[1], delta * sizeof(*data));
/* Check AMD Zen CPUs packages, and mirror temps across packages */
if (coreTempCount > 0 && coreTempCount != existingCPUs) {
double temp[coreTempCount];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is left partially uninitialized if data contains NAN values.

unsigned int count = 0;
unsigned int coresInDie = existingCPUs / coreTempCount;

/* Find package temps */
temp[0] = NAN;
for (unsigned int i = 1; i <= existingCPUs; i++) {
if (!isnan(data[i])) {
temp[count] = data[i];
count++;
if (count == coreTempCount) {
break;
}
}
}

/* Returns to conditional checking if no temp is found */
if (!isnan(temp[0])) {
// Set Temperature
data[0] = temp[0];
for (unsigned int die = 0; die < coreTempCount; die++) {
for (unsigned int i = 1; i <= coresInDie; i++) {
data[(die * coresInDie) + i] = temp[die];
}
}

/* No further adjustments */
goto out;
Comment on lines +282 to +284
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be one level up, because the special-casing should end here regardless of a global temperature found or not.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the goto statement should stay here and a fallback conditional should be created for the case where coreTempCount is 1 and data[0] has the only value in it.

This check can be added into the current conditional statement, however, from looking at all the other conditional statements, we should have that moved out into its own check so that it remains a fallback and is not coupled with the section specified for AMD Zen CPU temperature reporting. (change idea added below)

I may be needlessly worried about a impossible or niche hardware/platform config.

Suggested change
/* No further adjustments */
goto out;
/* No further adjustments */
goto out;
}
/* AMD Zen CPU can report coreTempCount as 1 - use fallback conditional */
}
/* Fallback for single temperature count reporting */
if (coreTempCount == 1 && !isnan(data[0])) {
for (unsigned int i = 1; i <= existingCPUs; i++) {
data[i] = data[0];
/* No further adjustments */
goto out;
}

}
/* AMD Zen CPU can report coreTempCount as 1 - returns if temps are not reported as expected */
}

/* Fallback for single temperature count reporting */
if (coreTempCount == 1 && !isnan(data[0])) {
for (unsigned int i = 1; i <= existingCPUs; i++) {
data[i] = data[0];
}

/* No further adjustments */
goto out;
}


out:
for (unsigned int i = 0; i <= existingCPUs; i++)
cpus[i].temperature = data[i];
Expand Down