Skip to content

Commit ebb50a8

Browse files
committed
system: match cpu temp sensor discovery with zero value readings
1 parent 74c6a27 commit ebb50a8

1 file changed

Lines changed: 30 additions & 18 deletions

File tree

src/system/system_monitor_service.cpp

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <algorithm>
99
#include <chrono>
10+
#include <cstdlib>
1011
#include <cstring>
1112
#include <dlfcn.h>
1213
#include <filesystem>
@@ -81,6 +82,19 @@ namespace {
8182
return static_cast<double>(raw);
8283
}
8384

85+
double readCpuTempInputCelsius(const std::filesystem::path& path) {
86+
std::ifstream file{path};
87+
long long raw = 0;
88+
if (file.is_open()) {
89+
file >> raw;
90+
if (file.fail()) {
91+
raw = 0;
92+
}
93+
}
94+
95+
return static_cast<double>(raw / 1000);
96+
}
97+
8498
std::optional<std::uint64_t> readUint64File(const std::filesystem::path& path) {
8599
std::ifstream file{path};
86100
if (!file.is_open()) {
@@ -470,22 +484,22 @@ namespace {
470484
for (const auto& file : fs::directory_iterator{path}) {
471485
const fs::path inputPath = file.path();
472486
const std::string fileName = inputPath.filename().string();
473-
const std::string filePath = inputPath.string();
474-
if (!fileName.starts_with("temp") || !fileName.ends_with("_input") || filePath.contains("nvme")) {
475-
continue;
476-
}
477-
478-
const std::string id = fileName.substr(4, fileName.size() - 10);
479-
const std::string base = fileName.substr(0, fileName.size() - 6);
480-
const std::string label = readSmallTextFile(path / (base + "_label")).value_or("temp" + id);
481-
const auto tempC = readTempInputCelsius(inputPath);
482-
if (!tempC.has_value()) {
487+
const std::string fileSuffix = "input";
488+
std::string filePath = inputPath.string();
489+
if (!filePath.contains(fileSuffix) || filePath.contains("nvme")) {
483490
continue;
484491
}
485492

493+
const int fileId = fileName.size() > 4 ? std::atoi(fileName.c_str() + 4) : 0;
494+
const std::string basePath = filePath.erase(filePath.find(fileSuffix), fileSuffix.length());
495+
const std::string label =
496+
readSmallTextFile(fs::path(basePath + "label")).value_or("temp" + std::to_string(fileId));
497+
const fs::path valuePath = fs::path(basePath + "input");
486498
const std::string sensorName = hwmonName + "/" + label;
487499
foundSensors[sensorName] = TempSensorReading{
488-
.tempC = *tempC, .score = 0, .source = formatHwmonTempSource(hwmonName, label, inputPath)
500+
.tempC = readCpuTempInputCelsius(valuePath),
501+
.score = 0,
502+
.source = formatHwmonTempSource(hwmonName, label, valuePath)
489503
};
490504

491505
if (!gotCpu && isPrimaryCpuSensorLabel(label)) {
@@ -507,14 +521,12 @@ namespace {
507521
}
508522

509523
const std::string label = readSmallTextFile(basePath / "type").value_or("temp" + std::to_string(i));
510-
const auto tempC = readTempInputCelsius(tempPath);
511-
if (!tempC.has_value()) {
512-
continue;
513-
}
514-
515524
const std::string sensorName = "thermal" + std::to_string(i) + "/" + label;
516-
foundSensors[sensorName] =
517-
TempSensorReading{.tempC = *tempC, .score = 0, .source = formatThermalZoneTempSource(label, tempPath)};
525+
foundSensors[sensorName] = TempSensorReading{
526+
.tempC = readCpuTempInputCelsius(tempPath),
527+
.score = 0,
528+
.source = formatThermalZoneTempSource(label, tempPath)
529+
};
518530
}
519531
}
520532
} catch (...) {

0 commit comments

Comments
 (0)