Skip to content

Commit 89351b7

Browse files
committed
cpu: priority matching for cpu temp
1 parent 3297336 commit 89351b7

1 file changed

Lines changed: 46 additions & 37 deletions

File tree

src/cpu.cpp

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -565,58 +565,67 @@ 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;
569+
auto const& custom_sensor_name = get_params()->cpu_custom_temp_sensor["hwmon_name"];
570+
auto const& custom_sensor_input = get_params()->cpu_custom_temp_sensor["hwmon_input"];
568571

569572
auto dirs = ls(hwmon.c_str());
570573
for (auto& dir : dirs) {
571-
path = hwmon + dir;
572-
name = read_line(path + "/name");
573-
SPDLOG_DEBUG("hwmon: sensor name: {}", name);
574-
575-
std::map<std::string, std::string> custom_sensor = get_params()->cpu_custom_temp_sensor;
576-
577-
if (!custom_sensor["hwmon_name"].empty() && !custom_sensor["hwmon_input"].empty()) {
578-
if (name != custom_sensor["hwmon_name"])
579-
continue;
580-
581-
find_fallback_input(path, custom_sensor["hwmon_input"].c_str(), input);
574+
const std::string current_path = hwmon + dir;
575+
std::string current_input;
576+
int priority = -1;
577+
578+
const auto name = read_line(current_path + "/name");
579+
SPDLOG_DEBUG("hwmon: sensor name: {}", name);
580+
581+
if (name == custom_sensor_name && !custom_sensor_input.empty()) {
582+
find_fallback_input(current_path, custom_sensor_input.c_str(), input);
583+
// setting a custom sensor should always have the highest prio
584+
priority = 100;
585+
// also shouldn't need to loop through the rest
582586
break;
583587
} else if (name == "coretemp") {
584-
find_input(path, "temp", input, "Package id 0");
585-
break;
588+
if (find_input(current_path, "temp", current_input, "Package id 0"))
589+
priority = 10;
586590
} else if ((name == "zenpower" || name == "k10temp")) {
587-
if (!find_input(path, "temp", input, "Tdie"))
588-
find_input(path, "temp", input, "Tctl");
589-
break;
591+
if (find_input(current_path, "temp", current_input, "Tdie") ||
592+
find_input(current_path, "temp", current_input, "Tctl"))
593+
priority = 9;
590594
} else if (name == "atk0110") {
591-
find_input(path, "temp", input, "CPU Temperature");
592-
break;
595+
if (find_input(current_path, "temp", current_input, "CPU Temperature"))
596+
priority = 8;
593597
} else if (name == "it8603") {
594-
find_input(path, "temp", input, "temp1");
595-
break;
598+
if (find_input(current_path, "temp", current_input, "temp1"))
599+
priority = 7;
596600
} else if (starts_with(name, "cpuss0_")) {
597-
find_fallback_input(path, "temp1", input);
598-
break;
601+
if (find_fallback_input(current_path, "temp1", current_input))
602+
priority = 6;
599603
} else if (starts_with(name, "nct")) {
600-
// Only break if nct module has TSI0_TEMP node
601-
if (find_input(path, "temp", input, "TSI0_TEMP"))
602-
break;
603-
604+
// Only use if nct module has TSI0_TEMP node
605+
if (find_input(current_path, "temp", current_input, "TSI0_TEMP"))
606+
priority = 5;
604607
} else if (name == "asusec") {
605-
// Only break if module has CPU node
606-
if (find_input(path, "temp", input, "CPU"))
607-
break;
608+
// Only use if module has CPU node
609+
if (find_input(current_path, "temp", current_input, "CPU"))
610+
priority = 4;
608611
} else if (name == "l_pcs") {
609612
// E2K (Elbrus 2000) CPU temperature module
610-
find_input(path, "temp", input, "Node 0 Max");
611-
break;
613+
if (find_input(current_path, "temp", current_input, "Node 0 Max"))
614+
priority = 3;
612615
} else if (std::regex_match(name, match, std::regex("cpu\\d*_thermal"))) {
613-
find_fallback_input(path, "temp1", input);
614-
break;
616+
if (find_fallback_input(current_path, "temp1", current_input))
617+
priority = 2;
615618
} else if (name == "apm_xgene") {
616-
find_input(path, "temp", input, "SoC Temperature");
617-
break;
618-
} else {
619-
path.clear();
619+
if (find_input(current_path, "temp", current_input, "SoC Temperature"))
620+
priority = 1;
621+
}
622+
623+
// if we did find a valid input, only use it if priority
624+
// is higher than what we've already found
625+
if (priority > best_priority) {
626+
best_priority = priority;
627+
path = current_path;
628+
input = current_input;
620629
}
621630
}
622631

0 commit comments

Comments
 (0)