Skip to content

Commit a722b31

Browse files
committed
cpu: priority matching for cpu temp
1 parent 801bd26 commit a722b31

1 file changed

Lines changed: 39 additions & 31 deletions

File tree

src/cpu.cpp

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -565,51 +565,59 @@ bool CPUStats::GetCpuFile() {
565565
std::string name, path, input;
566566
std::string hwmon = "/sys/class/hwmon/";
567567
std::smatch match;
568+
int best_priority = -1;
568569

569570
auto dirs = ls(hwmon.c_str());
570571
for (auto& dir : dirs) {
571-
path = hwmon + dir;
572-
name = read_line(path + "/name");
573-
SPDLOG_DEBUG("hwmon: sensor name: {}", name);
572+
std::string current_path = hwmon + dir;
573+
std::string current_input;
574+
int priority = 0;
575+
576+
name = read_line(current_path + "/name");
577+
SPDLOG_DEBUG("hwmon: sensor name: {}", name);
574578

575579
if (name == "coretemp") {
576-
find_input(path, "temp", input, "Package id 0");
577-
break;
578-
}
579-
else if ((name == "zenpower" || name == "k10temp")) {
580-
if (!find_input(path, "temp", input, "Tdie"))
581-
find_input(path, "temp", input, "Tctl");
582-
break;
580+
if (find_input(current_path, "temp", current_input, "Package id 0"))
581+
priority = 10;
582+
} else if ((name == "zenpower" || name == "k10temp")) {
583+
if (find_input(current_path, "temp", current_input, "Tdie") ||
584+
find_input(current_path, "temp", current_input, "Tctl"))
585+
priority = 9;
583586
} else if (name == "atk0110") {
584-
find_input(path, "temp", input, "CPU Temperature");
585-
break;
587+
if (find_input(current_path, "temp", current_input, "CPU Temperature"))
588+
priority = 8;
586589
} else if (name == "it8603") {
587-
find_input(path, "temp", input, "temp1");
588-
break;
590+
if (find_input(current_path, "temp", current_input, "temp1"))
591+
priority = 7;
589592
} else if (starts_with(name, "cpuss0_")) {
590-
find_fallback_input(path, "temp1", input);
591-
break;
593+
if (find_fallback_input(current_path, "temp1", current_input))
594+
priority = 6;
592595
} else if (starts_with(name, "nct")) {
593-
// Only break if nct module has TSI0_TEMP node
594-
if (find_input(path, "temp", input, "TSI0_TEMP"))
595-
break;
596-
596+
// Only use if nct module has TSI0_TEMP node
597+
if (find_input(current_path, "temp", current_input, "TSI0_TEMP"))
598+
priority = 5;
597599
} else if (name == "asusec") {
598-
// Only break if module has CPU node
599-
if (find_input(path, "temp", input, "CPU"))
600-
break;
600+
// Only use if module has CPU node
601+
if (find_input(current_path, "temp", current_input, "CPU"))
602+
priority = 4;
601603
} else if (name == "l_pcs") {
602604
// E2K (Elbrus 2000) CPU temperature module
603-
find_input(path, "temp", input, "Node 0 Max");
604-
break;
605+
if (find_input(current_path, "temp", current_input, "Node 0 Max"))
606+
priority = 3;
605607
} else if (std::regex_match(name, match, std::regex("cpu\\d*_thermal"))) {
606-
find_fallback_input(path, "temp1", input);
607-
break;
608+
if (find_fallback_input(current_path, "temp1", current_input))
609+
priority = 2;
608610
} else if (name == "apm_xgene") {
609-
find_input(path, "temp", input, "SoC Temperature");
610-
break;
611-
} else {
612-
path.clear();
611+
if (find_input(current_path, "temp", current_input, "SoC Temperature"))
612+
priority = 1;
613+
}
614+
615+
// if we did find a valid input, only use it if priority
616+
// is higher than what we've already found
617+
if (priority > best_priority) {
618+
best_priority = priority;
619+
path = current_path;
620+
input = current_input;
613621
}
614622
}
615623

0 commit comments

Comments
 (0)