Skip to content

Commit f2cf88a

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

1 file changed

Lines changed: 42 additions & 30 deletions

File tree

src/cpu.cpp

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -565,58 +565,70 @@ 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+
const std::string current_path = hwmon + dir;
573+
std::string current_input;
574+
int priority = -1;
575+
576+
const auto name = read_line(current_path + "/name");
577+
SPDLOG_DEBUG("hwmon: sensor name: {}", name);
574578

575579
std::map<std::string, std::string> custom_sensor = get_params()->cpu_custom_temp_sensor;
576580

577581
if (!custom_sensor["hwmon_name"].empty() && !custom_sensor["hwmon_input"].empty()) {
578582
if (name != custom_sensor["hwmon_name"])
579583
continue;
580584

581-
find_fallback_input(path, custom_sensor["hwmon_input"].c_str(), input);
585+
find_fallback_input(current_path, custom_sensor["hwmon_input"].c_str(), input);
586+
// setting a custom sensor should always have the highest prio
587+
priority = 100;
588+
// also shouldn't need to loop through the rest
582589
break;
583590
} else if (name == "coretemp") {
584-
find_input(path, "temp", input, "Package id 0");
585-
break;
591+
if (find_input(path, "temp", input, "Package id 0"))
592+
priority = 10;
586593
} else if ((name == "zenpower" || name == "k10temp")) {
587-
if (!find_input(path, "temp", input, "Tdie"))
588-
find_input(path, "temp", input, "Tctl");
589-
break;
594+
if (find_input(path, "temp", input, "Tdie") ||
595+
find_input(path, "temp", input, "Tctl"))
596+
priority = 9;
590597
} else if (name == "atk0110") {
591-
find_input(path, "temp", input, "CPU Temperature");
592-
break;
598+
if (find_input(current_path, "temp", current_input, "CPU Temperature"))
599+
priority = 8;
593600
} else if (name == "it8603") {
594-
find_input(path, "temp", input, "temp1");
595-
break;
601+
if (find_input(current_path, "temp", current_input, "temp1"))
602+
priority = 7;
596603
} else if (starts_with(name, "cpuss0_")) {
597-
find_fallback_input(path, "temp1", input);
598-
break;
604+
if (find_fallback_input(current_path, "temp1", current_input))
605+
priority = 6;
599606
} 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-
607+
// Only use if nct module has TSI0_TEMP node
608+
if (find_input(current_path, "temp", current_input, "TSI0_TEMP"))
609+
priority = 5;
604610
} else if (name == "asusec") {
605-
// Only break if module has CPU node
606-
if (find_input(path, "temp", input, "CPU"))
607-
break;
611+
// Only use if module has CPU node
612+
if (find_input(current_path, "temp", current_input, "CPU"))
613+
priority = 4;
608614
} else if (name == "l_pcs") {
609615
// E2K (Elbrus 2000) CPU temperature module
610-
find_input(path, "temp", input, "Node 0 Max");
611-
break;
616+
if (find_input(current_path, "temp", current_input, "Node 0 Max"))
617+
priority = 3;
612618
} else if (std::regex_match(name, match, std::regex("cpu\\d*_thermal"))) {
613-
find_fallback_input(path, "temp1", input);
614-
break;
619+
if (find_fallback_input(current_path, "temp1", current_input))
620+
priority = 2;
615621
} else if (name == "apm_xgene") {
616-
find_input(path, "temp", input, "SoC Temperature");
617-
break;
618-
} else {
619-
path.clear();
622+
if (find_input(current_path, "temp", current_input, "SoC Temperature"))
623+
priority = 1;
624+
}
625+
626+
// if we did find a valid input, only use it if priority
627+
// is higher than what we've already found
628+
if (priority > best_priority) {
629+
best_priority = priority;
630+
path = current_path;
631+
input = current_input;
620632
}
621633
}
622634

0 commit comments

Comments
 (0)