Add average and max aggregator functions

This commit is contained in:
2023-03-15 15:16:56 +01:00
parent 7b0b419a22
commit f383eb1b79
8 changed files with 141 additions and 72 deletions

View File

@@ -1,5 +1,5 @@
pkgname=fantasize pkgname=fantasize
pkgver=0.1.8 pkgver=0.1.9
pkgrel=1 pkgrel=1
pkgdesc='C++ fan control for Linux' pkgdesc='C++ fan control for Linux'
url='https://github.com/Tabascl/fantasize.git' url='https://github.com/Tabascl/fantasize.git'

View File

@@ -19,6 +19,7 @@ add_executable(${PROJECT_NAME}
src/fan/HwmonFan.cxx src/fan/HwmonFan.cxx
src/fan/FanCurve.cxx src/fan/FanCurve.cxx
src/fan/FanLabeler.cxx src/fan/FanLabeler.cxx
src/fan/Aggregators.cxx
src/FanGenerator.cxx src/FanGenerator.cxx
src/Serializer.cxx src/Serializer.cxx
src/sensor/SensorManager.cxx src/sensor/SensorManager.cxx

View File

@@ -7,6 +7,7 @@
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <Settings.h> #include <Settings.h>
#include <fan/Aggregators.h>
#include <fan/Fan.h> #include <fan/Fan.h>
#include <fan/FanCurve.h> #include <fan/FanCurve.h>
#include <sensor/Sensor.h> #include <sensor/Sensor.h>
@@ -16,20 +17,22 @@
using json = nlohmann::json; using json = nlohmann::json;
class Serializer { class Serializer
{
public: public:
Serializer(); Serializer();
void SerializeFans(std::vector<std::shared_ptr<Fan>> fans); void SerializeFans(std::vector<std::shared_ptr<Fan>> fans);
std::vector<std::shared_ptr<Fan>> std::vector<std::shared_ptr<Fan>> DeserializeFans(
DeserializeFans(std::vector<std::shared_ptr<Sensor>> availableSensors); std::vector<std::shared_ptr<Sensor>> availableSensors);
std::vector<std::shared_ptr<FanCurve>> std::vector<std::shared_ptr<FanCurve>> DeserializeFanCurves(
DeserializeFanCurves(std::vector<std::shared_ptr<Sensor>> availableSensors, std::vector<std::shared_ptr<Sensor>> availableSensors,
std::vector<std::shared_ptr<Fan>> availableFans); std::vector<std::shared_ptr<Fan>> availableFans);
std::shared_ptr<Settings> DeserializeSettings(); std::shared_ptr<Settings> DeserializeSettings();
private: private:
void WriteJson(json o); void WriteJson(json o);
json ReadJson(); json ReadJson();
std::unique_ptr<Aggregator> aggregatorFromString(std::string str) const;
}; };
#endif // SERIALIZER_H_ #endif // SERIALIZER_H_

View File

@@ -4,19 +4,23 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
#include <fan/Aggregators.h>
#include <fan/HwmonFan.h> #include <fan/HwmonFan.h>
#include <sensor/Sensor.h> #include <sensor/Sensor.h>
struct FanStep { struct FanStep
{
int Temp; int Temp;
int Percent; int Percent;
}; };
class FanCurve { class FanCurve
{
public: public:
FanCurve(std::vector<FanStep> steps, FanCurve(std::vector<FanStep> steps,
std::vector<std::shared_ptr<Sensor>> sensors, std::vector<std::shared_ptr<Sensor>> sensors,
std::vector<std::shared_ptr<Fan>> fans); std::vector<std::shared_ptr<Fan>> fans,
std::unique_ptr<Aggregator> aggregator);
void DoFanControl(); void DoFanControl();
@@ -27,6 +31,7 @@ private:
std::vector<FanStep> mSteps; std::vector<FanStep> mSteps;
std::vector<std::shared_ptr<Sensor>> mTempSensors; std::vector<std::shared_ptr<Sensor>> mTempSensors;
std::vector<std::shared_ptr<Fan>> mFans; std::vector<std::shared_ptr<Fan>> mFans;
std::unique_ptr<Aggregator> mAggregator;
}; };
#endif // FANCURVE_H_ #endif // FANCURVE_H_

View File

@@ -1,4 +1,3 @@
#include "Settings.h"
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
@@ -9,17 +8,21 @@
#include <fan/HwmonFan.h> #include <fan/HwmonFan.h>
#include <pwm/PWMControl.h> #include <pwm/PWMControl.h>
#include <sensor/LMSensor.h> #include <sensor/LMSensor.h>
#include <string>
using namespace std; using namespace std;
namespace fs = filesystem; namespace fs = filesystem;
Serializer::Serializer() { Serializer::Serializer()
{
if (!fs::exists(SERIALIZATION_DIR)) { if (!fs::exists(SERIALIZATION_DIR)) {
fs::create_directory(SERIALIZATION_DIR); fs::create_directory(SERIALIZATION_DIR);
} }
} }
void Serializer::SerializeFans(vector<shared_ptr<Fan>> fans) { void
Serializer::SerializeFans(vector<shared_ptr<Fan>> fans)
{
json obj; json obj;
for (auto f : fans) { for (auto f : fans) {
@@ -30,7 +33,8 @@ void Serializer::SerializeFans(vector<shared_ptr<Fan>> fans) {
} }
vector<shared_ptr<Fan>> vector<shared_ptr<Fan>>
Serializer::DeserializeFans(vector<shared_ptr<Sensor>> availableSensors) { Serializer::DeserializeFans(vector<shared_ptr<Sensor>> availableSensors)
{
vector<shared_ptr<Fan>> fans; vector<shared_ptr<Fan>> fans;
// Create a for the sensors first, then searching becomes cheaper // Create a for the sensors first, then searching becomes cheaper
@@ -41,7 +45,7 @@ Serializer::DeserializeFans(vector<shared_ptr<Sensor>> availableSensors) {
auto data = ReadJson(); auto data = ReadJson();
try { try {
for (auto &el : data["fans"].items()) { for (auto& el : data["fans"].items()) {
auto pwmControl = make_shared<PWMControl>(el.value()["PWMControl"]); auto pwmControl = make_shared<PWMControl>(el.value()["PWMControl"]);
auto rpmSensor = sensorMap[el.value()["LMSensor"]]; auto rpmSensor = sensorMap[el.value()["LMSensor"]];
@@ -56,20 +60,22 @@ Serializer::DeserializeFans(vector<shared_ptr<Sensor>> availableSensors) {
fans.push_back(fan); fans.push_back(fan);
} }
} catch (const std::exception &e) { } catch (const std::exception& e) {
std::cout << "Deserialization error! Message: " << e.what() << std::endl; std::cout << "Deserialization error! Message: " << e.what() << std::endl;
} }
return fans; return fans;
} }
void Serializer::WriteJson(json o) { void
Serializer::WriteJson(json o)
{
json obj; json obj;
if (fs::exists(fs::path(SERIALIZATION_DIR) / FANS_JSON_FILENAME)) { if (fs::exists(fs::path(SERIALIZATION_DIR) / FANS_JSON_FILENAME)) {
obj = ReadJson(); obj = ReadJson();
} }
for (auto &[key, value] : o.items()) { for (auto& [key, value] : o.items()) {
obj[key] = value; obj[key] = value;
} }
@@ -77,14 +83,18 @@ void Serializer::WriteJson(json o) {
ostrm << obj.dump(2) << "\n"; ostrm << obj.dump(2) << "\n";
} }
json Serializer::ReadJson() { json
Serializer::ReadJson()
{
ifstream istrm(fs::path(SERIALIZATION_DIR) / FANS_JSON_FILENAME); ifstream istrm(fs::path(SERIALIZATION_DIR) / FANS_JSON_FILENAME);
return json::parse(istrm); return json::parse(istrm);
} }
vector<shared_ptr<FanCurve>> Serializer::DeserializeFanCurves( vector<shared_ptr<FanCurve>>
std::vector<std::shared_ptr<Sensor>> availableSensors, Serializer::DeserializeFanCurves(
std::vector<std::shared_ptr<Fan>> availableFans) { std::vector<std::shared_ptr<Sensor>> availableSensors,
std::vector<std::shared_ptr<Fan>> availableFans)
{
auto data = ReadJson(); auto data = ReadJson();
map<string, shared_ptr<Sensor>> sensorMap; map<string, shared_ptr<Sensor>> sensorMap;
@@ -99,32 +109,47 @@ vector<shared_ptr<FanCurve>> Serializer::DeserializeFanCurves(
vector<shared_ptr<FanCurve>> curves; vector<shared_ptr<FanCurve>> curves;
for (auto &el : data["fancurves"].items()) { for (auto& el : data["fancurves"].items()) {
vector<FanStep> steps; vector<FanStep> steps;
vector<shared_ptr<Sensor>> sensors; vector<shared_ptr<Sensor>> sensors;
vector<shared_ptr<Fan>> fans; vector<shared_ptr<Fan>> fans;
for (auto &step : el.value()["FanSteps"].items()) { for (auto& step : el.value()["FanSteps"].items()) {
steps.push_back(FanStep{step.value()[0], step.value()[1]}); steps.push_back(FanStep{ step.value()[0], step.value()[1] });
} }
for (auto &sensor : el.value()["Sensors"].items()) { for (auto& sensor : el.value()["Sensors"].items()) {
if (sensorMap.contains(sensor.value())) if (sensorMap.contains(sensor.value()))
sensors.push_back(sensorMap[sensor.value()]); sensors.push_back(sensorMap[sensor.value()]);
} }
for (auto &fan : el.value()["Fans"].items()) { for (auto& fan : el.value()["Fans"].items()) {
if (fanMap.contains(fan.value())) if (fanMap.contains(fan.value()))
fans.push_back(fanMap[fan.value()]); fans.push_back(fanMap[fan.value()]);
} }
curves.push_back(make_shared<FanCurve>(steps, sensors, fans)); std::unique_ptr<Aggregator> aggregator =
aggregatorFromString(el.value()["AggregateFunction"]);
curves.push_back(
make_shared<FanCurve>(steps, sensors, fans, std::move(aggregator)));
} }
return curves; return curves;
} }
shared_ptr<Settings> Serializer::DeserializeSettings() { std::unique_ptr<Aggregator>
Serializer::aggregatorFromString(std::string str) const
{
if (str == "max")
return std::make_unique<MaxAggregator>();
else
return std::make_unique<AverageAggregator>();
}
shared_ptr<Settings>
Serializer::DeserializeSettings()
{
int frequency = FREQUENCY_DEFAULT; int frequency = FREQUENCY_DEFAULT;
auto data = ReadJson(); auto data = ReadJson();

View File

@@ -4,17 +4,25 @@
#include <boost/log/trivial.hpp> #include <boost/log/trivial.hpp>
#include <fan/FanCurve.h> #include <fan/FanCurve.h>
#include <memory>
using namespace std; using namespace std;
FanCurve::FanCurve(std::vector<FanStep> steps, FanCurve::FanCurve(std::vector<FanStep> steps,
std::vector<std::shared_ptr<Sensor>> sensors, std::vector<std::shared_ptr<Sensor>> sensors,
std::vector<std::shared_ptr<Fan>> fans) std::vector<std::shared_ptr<Fan>> fans,
: mSteps(steps), mTempSensors(sensors), mFans(fans) { std::unique_ptr<Aggregator> aggregator)
: mSteps(steps)
, mTempSensors(sensors)
, mFans(fans)
, mAggregator(std::move(aggregator))
{
PrintInfo(); PrintInfo();
} }
void FanCurve::DoFanControl() { void
FanCurve::DoFanControl()
{
BOOST_LOG_FUNCTION(); BOOST_LOG_FUNCTION();
int temp = AggregateTemperature(); int temp = AggregateTemperature();
@@ -51,17 +59,15 @@ void FanCurve::DoFanControl() {
} }
} }
// Dummy Implementation using AVG int
int FanCurve::AggregateTemperature() { FanCurve::AggregateTemperature()
int sum = 0; {
for (auto s : mTempSensors) { return mAggregator->aggregate(mTempSensors);
sum += s->value();
}
return sum / mTempSensors.size();
} }
void FanCurve::PrintInfo() { void
FanCurve::PrintInfo()
{
BOOST_LOG_FUNCTION() BOOST_LOG_FUNCTION()
BOOST_LOG_TRIVIAL(info) << "### Fan curve:"; BOOST_LOG_TRIVIAL(info) << "### Fan curve:";
@@ -90,4 +96,10 @@ void FanCurve::PrintInfo() {
} }
BOOST_LOG_TRIVIAL(info) << sStream.str(); BOOST_LOG_TRIVIAL(info) << sStream.str();
sStream.str(string());
sStream << "Aggregate function: " << mAggregator->toString();
BOOST_LOG_TRIVIAL(info) << sStream.str();
} }

View File

@@ -20,7 +20,7 @@
#include <App.h> #include <App.h>
#define PROJECT_VERSION "v0.1.8" #define PROJECT_VERSION "v0.1.9"
namespace po = boost::program_options; namespace po = boost::program_options;
namespace logging = boost::log; namespace logging = boost::log;
@@ -29,26 +29,32 @@ App app;
static int doInitialSetup = 0; static int doInitialSetup = 0;
void signal_handler(int s) { app.Shutdown(); } void
signal_handler(int s)
{
app.Shutdown();
}
void InitLogging(bool verbose) { void
InitLogging(bool verbose)
{
logging::add_console_log( logging::add_console_log(
std::clog, std::clog,
logging::keywords::format = logging::keywords::format =
(logging::expressions::stream (logging::expressions::stream
<< "[" << "["
<< logging::expressions::format_date_time<boost::posix_time::ptime>( << logging::expressions::format_date_time<boost::posix_time::ptime>(
"TimeStamp", "%Y-%m-%d %H:%M:%S") "TimeStamp", "%Y-%m-%d %H:%M:%S")
<< "][" << "]["
<< logging::expressions::format_named_scope( << logging::expressions::format_named_scope(
"Scope", logging::keywords::format = "%c") "Scope", logging::keywords::format = "%c")
<< "]" << "]"
<< "[" << logging::trivial::severity << "] " << "[" << logging::trivial::severity << "] "
<< logging::expressions::smessage)); << logging::expressions::smessage));
logging::add_common_attributes(); logging::add_common_attributes();
logging::core::get()->add_global_attribute( logging::core::get()->add_global_attribute(
"Scope", logging::attributes::named_scope()); "Scope", logging::attributes::named_scope());
BOOST_LOG_FUNCTION(); BOOST_LOG_FUNCTION();
@@ -60,7 +66,9 @@ void InitLogging(bool verbose) {
} }
} }
int main(int argc, char **argv) { int
main(int argc, char** argv)
{
BOOST_LOG_FUNCTION() BOOST_LOG_FUNCTION()
BOOST_LOG_TRIVIAL(info) << "Version: " << PROJECT_VERSION; BOOST_LOG_TRIVIAL(info) << "Version: " << PROJECT_VERSION;
@@ -69,8 +77,8 @@ int main(int argc, char **argv) {
po::options_description desc("Allowed options"); po::options_description desc("Allowed options");
desc.add_options()("help,h", "produce help message")( desc.add_options()("help,h", "produce help message")(
"setup,s", po::bool_switch(), "setup,s", po::bool_switch(), "run initial setup")(
"run initial setup")("verbose,v", po::bool_switch(), "print debug info"); "verbose,v", po::bool_switch(), "print debug info");
po::variables_map vm; po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm); po::store(po::parse_command_line(argc, argv, desc), vm);
@@ -90,7 +98,7 @@ int main(int argc, char **argv) {
app.Init(); app.Init();
app.NormalOperation(); app.NormalOperation();
} }
} catch (const std::exception &e) { } catch (const std::exception& e) {
std::cout << "An exception was caught: " << e.what() << std::endl; std::cout << "An exception was caught: " << e.what() << std::endl;
} }

View File

@@ -15,7 +15,9 @@
using namespace std; using namespace std;
namespace fs = filesystem; namespace fs = filesystem;
PWMControl::PWMControl(string controlPath) : mControlPath(controlPath) { PWMControl::PWMControl(string controlPath)
: mControlPath(controlPath)
{
fs::path pathEnable(mControlPath + PWM_POSTFIX_ENABLE); fs::path pathEnable(mControlPath + PWM_POSTFIX_ENABLE);
fs::path pathMode(mControlPath + PWM_POSTFIX_MODE); fs::path pathMode(mControlPath + PWM_POSTFIX_MODE);
@@ -35,21 +37,24 @@ PWMControl::PWMControl(string controlPath) : mControlPath(controlPath) {
istrm.close(); istrm.close();
} }
PWMControl::~PWMControl() { PWMControl::~PWMControl()
{
BOOST_LOG_FUNCTION(); BOOST_LOG_FUNCTION();
BOOST_LOG_TRIVIAL(trace) << "Cleanup"; BOOST_LOG_TRIVIAL(trace) << "Cleanup";
Reset(); Reset();
} }
void PWMControl::Power(int percent) { void
PWMControl::Power(int percent)
{
BOOST_LOG_FUNCTION(); BOOST_LOG_FUNCTION();
int pwmValue = (PWM_MAX_VALUE * percent) / 100; int pwmValue = (PWM_MAX_VALUE * percent) / 100;
if (percent != mCurrentValue) { if (percent != mCurrentValue) {
BOOST_LOG_TRIVIAL(trace) BOOST_LOG_TRIVIAL(trace) << "Updating control value of " << toString()
<< "Updating control value to " << percent << "% (" << pwmValue << ")"; << " to " << percent << "% (" << pwmValue << ")";
ofstream ostrm(mControlPath, ios::trunc); ofstream ostrm(mControlPath, ios::trunc);
ostrm << pwmValue; ostrm << pwmValue;
ostrm.close(); ostrm.close();
@@ -58,7 +63,9 @@ void PWMControl::Power(int percent) {
} }
} }
int PWMControl::Power() { int
PWMControl::Power()
{
int value; int value;
ifstream istrm; ifstream istrm;
@@ -68,13 +75,17 @@ int PWMControl::Power() {
return (value * 100) / PWM_MAX_VALUE; return (value * 100) / PWM_MAX_VALUE;
} }
void PWMControl::EnableManualControl() { void
PWMControl::EnableManualControl()
{
ofstream ostrm(mEnablePath, ios::trunc); ofstream ostrm(mEnablePath, ios::trunc);
ostrm << static_cast<int>(PWM_ENABLE::MANUAL_CONTROL); ostrm << static_cast<int>(PWM_ENABLE::MANUAL_CONTROL);
ostrm.close(); ostrm.close();
} }
void PWMControl::Reset() { void
PWMControl::Reset()
{
ofstream ostrm(mEnablePath, ios::trunc); ofstream ostrm(mEnablePath, ios::trunc);
ostrm << mInitialEnable; ostrm << mInitialEnable;
@@ -86,11 +97,15 @@ void PWMControl::Reset() {
ostrm.close(); ostrm.close();
} }
const string PWMControl::toString() const { const string
PWMControl::toString() const
{
return fs::path(mControlPath).filename(); return fs::path(mControlPath).filename();
} }
json PWMControl::toJson() const { json
json obj = {"PWMControl", mControlPath}; PWMControl::toJson() const
{
json obj = { "PWMControl", mControlPath };
return obj; return obj;
} }