Remove aggregator, fix changing HWMON paths

Add logic to use actual device paths for the PWM sensors instead of their
standart HWMON paths.

This was done after noticing that due to the load order of kernel modules,
the index of HWMON paths could change.
This commit is contained in:
Tabascl
2024-07-07 22:19:27 +02:00
parent e2509cea8b
commit 78b2a62643
7 changed files with 37 additions and 21 deletions

View File

@@ -23,7 +23,6 @@ public:
void DoFanControl();
private:
int AggregateTemperature();
void PrintInfo();
bool ExceedsHysteresis(int temperature);
void ApplyFanPower(std::shared_ptr<Fan> fan, int targetFanPower);

View File

@@ -12,7 +12,7 @@ enum class PWM_MODE { DC = 0, PWM };
class PWMControl : public Printable, public Serializable {
public:
PWMControl(std::string controlPath);
PWMControl(std::string controlPath, int deviceIndex);
~PWMControl();
void SetPower(int percent);
@@ -26,9 +26,11 @@ public:
json toJson() const override;
private:
std::string mConfigPath;
std::string mControlPath;
std::string mEnablePath;
std::string mModePath;
int mDeviceIndex;
std::string mInitialEnable;
std::string mInitialMode;

View File

@@ -8,7 +8,7 @@
#include <fan/HwmonFan.h>
#include <pwm/PWMControl.h>
#define SETTLE_TIMEOUT 10
#define SETTLE_TIMEOUT 30
using namespace std;

View File

@@ -37,7 +37,7 @@ Serializer::DeserializeFans(vector<shared_ptr<Sensor>> availableSensors) {
vector<shared_ptr<Fan>> fans;
// Create a for the sensors first, then searching becomes cheaper
// Create a map for the sensors first, then searching becomes cheaper
map<string, shared_ptr<Sensor>> sensorMap;
for (auto s : availableSensors) {
sensorMap[s->toString()] = s;
@@ -46,7 +46,7 @@ Serializer::DeserializeFans(vector<shared_ptr<Sensor>> availableSensors) {
auto data = ReadJson();
try {
for (auto &el : data["fans"].items()) {
auto pwmControl = make_shared<PWMControl>(el.value()["PWMControl"]);
auto pwmControl = make_shared<PWMControl>(el.value()["PWMControl"]["Path"], el.value()["PWMControl"]["Index"]);
auto rpmSensor = sensorMap[el.value()["LMSensor"]];
int minPWM = el.value()["MinPWM"];

View File

@@ -29,7 +29,7 @@ void FanCurve::DoFanControl() {
for (auto s : mTempSensors)
BOOST_LOG_TRIVIAL(trace) << s->toString();
int temp = AggregateTemperature();
int temp = mAggregator->aggregate(mTempSensors);
int t0 = 0, t1 = 0, p0 = 0, p1 = 0;
int targetFanPower;
@@ -71,10 +71,6 @@ void FanCurve::DoFanControl() {
}
}
int FanCurve::AggregateTemperature() {
return mAggregator->aggregate(mTempSensors);
}
void FanCurve::PrintInfo() {
BOOST_LOG_FUNCTION()
@@ -123,7 +119,7 @@ void FanCurve::ApplyFanPower(std::shared_ptr<Fan> fan, int targetFanPower) {
BOOST_LOG_FUNCTION();
if (!fan->ZeroFanModeSupported() && fan->RPM() <= 0) {
BOOST_LOG_TRIVIAL(warning) << "Fan stopped completely!";
BOOST_LOG_TRIVIAL(warning) << "Fan " << fan->toString() << " stopped completely!";
fan->PWM(fan->StartPWM());
fan->AdjustPWMLimits();
} else {

View File

@@ -1,8 +1,8 @@
#include <boost/log/attributes/named_scope.hpp>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <boost/log/attributes/named_scope.hpp>
#include <boost/log/trivial.hpp>
#include <pwm/PWMControl.h>
@@ -15,12 +15,24 @@
using namespace std;
namespace fs = filesystem;
PWMControl::PWMControl(string controlPath) : mControlPath(controlPath) {
fs::path pathEnable(mControlPath + PWM_POSTFIX_ENABLE);
fs::path pathMode(mControlPath + PWM_POSTFIX_MODE);
PWMControl::PWMControl(string controlPath, int deviceIndex)
: mConfigPath(controlPath), mDeviceIndex(deviceIndex) {
auto path = fs::path{controlPath};
mEnablePath = pathEnable;
mModePath = pathMode;
int fileCount =
distance(fs::directory_iterator(path), fs::directory_iterator{});
if (fileCount != 1)
throw runtime_error("More than one HWMON device present, unsupported");
for (auto de : fs::directory_iterator(path)) {
auto testPath = de.path();
mControlPath =
fs::path{de.path() / (string("pwm").append(to_string(deviceIndex)))}
.string();
}
mEnablePath = fs::path{mControlPath + PWM_POSTFIX_ENABLE}.string();
mModePath = fs::path{mControlPath + PWM_POSTFIX_MODE}.string();
ifstream istrm;
@@ -85,6 +97,6 @@ void PWMControl::Reset() {
const string PWMControl::toString() const { return fs::path(mControlPath); }
json PWMControl::toJson() const {
json obj = {"PWMControl", mControlPath};
json obj = {"PWMControl", {{"Path", mConfigPath}, {"Index", mDeviceIndex}}};
return obj;
}

View File

@@ -1,6 +1,7 @@
#include <filesystem>
#include <iostream>
#include <regex>
#include <string>
#include <pwm/PWMControlFacade.h>
@@ -20,14 +21,20 @@ vector<shared_ptr<PWMControl>> PWMControlFacade::PWMControls() {
for (const fs::directory_entry &hwmon_device :
fs::directory_iterator{HWMON_BASE_PATH}) {
// Resolve symlink to get device path instead of hwmon path, the latter of
// which can change
fs::path actual_path =
fs::canonical(HWMON_BASE_PATH / fs::read_symlink(hwmon_device));
for (const fs::directory_entry &control :
fs::directory_iterator{hwmon_device}) {
fs::directory_iterator{actual_path}) {
auto filename = control.path().filename().string();
if (regex_match(filename, re_ctrl)) {
auto controlPath = control.path().string();
auto controlIndex = filename.back() - '0';
auto controlPath = actual_path.parent_path();
controls.push_back(make_shared<PWMControl>(controlPath));
controls.push_back(make_shared<PWMControl>(controlPath, controlIndex));
}
}
}