Add documentation, cleanup
This commit is contained in:
@@ -9,11 +9,11 @@ find_package(Boost 1.79.0 COMPONENTS json)
|
|||||||
|
|
||||||
add_executable(app
|
add_executable(app
|
||||||
src/main.cxx
|
src/main.cxx
|
||||||
src/sensor/NvidiaTemperatureSensor.cxx
|
src/sensor/NvidiaSensor.cxx
|
||||||
src/fan/Pwm.cxx
|
src/fan/Pwm.cxx
|
||||||
src/fan/PwmControl.cxx
|
src/fan/PwmControl.cxx
|
||||||
src/SensorsWrapper.cxx
|
src/SensorsWrapper.cxx
|
||||||
src/sensor/HwmonTemperatureSensor.cxx
|
src/sensor/HwmonSensor.cxx
|
||||||
)
|
)
|
||||||
|
|
||||||
set_property(TARGET app PROPERTY CXX_STANDARD 17)
|
set_property(TARGET app PROPERTY CXX_STANDARD 17)
|
||||||
|
|||||||
17
app/doc/ApplicationStart.plantuml
Normal file
17
app/doc/ApplicationStart.plantuml
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
@startuml
|
||||||
|
|
||||||
|
start
|
||||||
|
:Initialization;
|
||||||
|
|
||||||
|
if (Mapping exists) then (yes)
|
||||||
|
:Load mapping;
|
||||||
|
else (no)
|
||||||
|
:Generate mapping;
|
||||||
|
endif
|
||||||
|
|
||||||
|
:Enter fan control loop;
|
||||||
|
:Load Fan Curves;
|
||||||
|
|
||||||
|
stop
|
||||||
|
|
||||||
|
@enduml
|
||||||
25
app/doc/Class.plantuml
Normal file
25
app/doc/Class.plantuml
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
@startuml
|
||||||
|
interface Fan {
|
||||||
|
{abstract} void PWM(int percent)
|
||||||
|
{abstract} int RPM()
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Sensor {
|
||||||
|
{abstract} int value()
|
||||||
|
{abstract} string name()
|
||||||
|
}
|
||||||
|
|
||||||
|
class HwmonFan {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Fan <|-- HwmonFan
|
||||||
|
|
||||||
|
Sensor <|-- HwmonSensor
|
||||||
|
Sensor <|-- NvidiaSensor
|
||||||
|
|
||||||
|
HwmonFan - Sensor
|
||||||
|
HwmonFan -- PwmControl
|
||||||
|
|
||||||
|
|
||||||
|
@enduml
|
||||||
7
app/doc/InteractiveMapping.plantuml
Normal file
7
app/doc/InteractiveMapping.plantuml
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
@startuml
|
||||||
|
actor User as usr
|
||||||
|
participant Interface as if
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@enduml
|
||||||
@@ -1,17 +1,23 @@
|
|||||||
#ifndef HWMONFAN_H_
|
#ifndef HWMONFAN_H_
|
||||||
#define HWMONFAN_H_
|
#define HWMONFAN_H_
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#include <fan/Fan.h>
|
#include <fan/Fan.h>
|
||||||
#include <fan/PwmControl.h>
|
#include <fan/PwmControl.h>
|
||||||
|
#include <sensor/Sensor.h>
|
||||||
|
|
||||||
class HwmonFan : public Fan {
|
class HwmonFan : public Fan {
|
||||||
public:
|
public:
|
||||||
|
HwmonFan(std::unique_ptr<PwmControl> pwmControl,
|
||||||
|
std::unique_ptr<Sensor> rpmSensor);
|
||||||
|
|
||||||
void PWM(int percent) override;
|
void PWM(int percent) override;
|
||||||
int RPM() override;
|
int RPM() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PwmControl mPwmControl;
|
std::unique_ptr<PwmControl> mPwmControl;
|
||||||
// PwmSensor mPwmSensor;
|
std::unique_ptr<Sensor> mRpmSensor;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // HWMONFAN_H_
|
#endif // HWMONFAN_H_
|
||||||
|
|||||||
22
app/include/sensor/HwmonSensor.h
Normal file
22
app/include/sensor/HwmonSensor.h
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#ifndef HWMONSENSOR_H_
|
||||||
|
#define HWMONSENSOR_H_
|
||||||
|
|
||||||
|
#include <sensors/sensors.h>
|
||||||
|
|
||||||
|
#include <sensor/Sensor.h>
|
||||||
|
|
||||||
|
class HwmonSensor : public Sensor {
|
||||||
|
public:
|
||||||
|
HwmonSensor(const sensors_chip_name *chipName, const sensors_feature *feature,
|
||||||
|
const sensors_subfeature *subfeature);
|
||||||
|
|
||||||
|
int value() override;
|
||||||
|
std::string name() override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
const sensors_chip_name *mChipName;
|
||||||
|
const sensors_feature *mFeature;
|
||||||
|
const sensors_subfeature *mSubFeature;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // HWMONSENSOR_H_
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
#ifndef HWMONTEMPERATURESENSOR_H_
|
|
||||||
#define HWMONTEMPERATURESENSOR_H_
|
|
||||||
|
|
||||||
#include <sensors/sensors.h>
|
|
||||||
|
|
||||||
#include <sensor/TemperatureSensor.h>
|
|
||||||
|
|
||||||
class HwmonTemperatureSensor : public TemperatureSensor {
|
|
||||||
public:
|
|
||||||
HwmonTemperatureSensor(const sensors_chip_name *chipName,
|
|
||||||
const sensors_feature *feature,
|
|
||||||
const sensors_subfeature *subfeature);
|
|
||||||
|
|
||||||
int temperature() override;
|
|
||||||
std::string name() override;
|
|
||||||
|
|
||||||
private:
|
|
||||||
const sensors_chip_name *mChipName;
|
|
||||||
const sensors_feature *mFeature;
|
|
||||||
const sensors_subfeature *mSubFeature;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // HWMONTEMPERATURESENSOR_H_
|
|
||||||
15
app/include/sensor/NvidiaSensor.h
Normal file
15
app/include/sensor/NvidiaSensor.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#ifndef NVIDIASENSOR_H_
|
||||||
|
#define NVIDIASENSOR_H_
|
||||||
|
|
||||||
|
#include <sensor/Sensor.h>
|
||||||
|
|
||||||
|
class NvidiaSensor : public Sensor {
|
||||||
|
public:
|
||||||
|
NvidiaSensor();
|
||||||
|
~NvidiaSensor();
|
||||||
|
|
||||||
|
int value() override;
|
||||||
|
std::string name() override;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // NVIDIASENSOR_H_
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
#ifndef NVIDIA_H_
|
|
||||||
#define NVIDIA_H_
|
|
||||||
|
|
||||||
#include <sensor/TemperatureSensor.h>
|
|
||||||
|
|
||||||
class NvidiaTemperatureSensor : public TemperatureSensor {
|
|
||||||
public:
|
|
||||||
NvidiaTemperatureSensor();
|
|
||||||
~NvidiaTemperatureSensor();
|
|
||||||
|
|
||||||
int temperature() override;
|
|
||||||
std::string name() override;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // NVIDIA_H_
|
|
||||||
15
app/include/sensor/Sensor.h
Normal file
15
app/include/sensor/Sensor.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#ifndef SENSOR_H_
|
||||||
|
#define SENSOR_H_
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
class Sensor {
|
||||||
|
public:
|
||||||
|
// Read the current value
|
||||||
|
virtual int value() = 0;
|
||||||
|
// Name for displaying. Should be descriptive, e.g. "GPU" or the label from
|
||||||
|
// libsensors.
|
||||||
|
virtual std::string name() = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // SENSOR_H_
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
#ifndef TEMPERATURESENSOR_H_
|
|
||||||
#define TEMPERATURESENSOR_H_
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
class TemperatureSensor {
|
|
||||||
public:
|
|
||||||
virtual int temperature() = 0;
|
|
||||||
virtual std::string name() = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // TEMPERATURESENSOR_H_
|
|
||||||
@@ -55,6 +55,12 @@ void PwmControl::enableManualControl() {
|
|||||||
|
|
||||||
void PwmControl::reset() {
|
void PwmControl::reset() {
|
||||||
ofstream ostrm(mEnablePath, ios::trunc);
|
ofstream ostrm(mEnablePath, ios::trunc);
|
||||||
|
|
||||||
ostrm << mInitialEnable;
|
ostrm << mInitialEnable;
|
||||||
ostrm.close();
|
ostrm.close();
|
||||||
|
|
||||||
|
ostrm.open(mModePath, ios::trunc);
|
||||||
|
ostrm << mInitialMode;
|
||||||
|
|
||||||
|
ostrm.close();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +1,24 @@
|
|||||||
#include <exception>
|
#include <exception>
|
||||||
|
#include <filesystem>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <regex>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <sensors/sensors.h>
|
#include <sensors/sensors.h>
|
||||||
|
|
||||||
#include <fan/Pwm.h>
|
#include <fan/PwmControl.h>
|
||||||
#include <sensor/HwmonTemperatureSensor.h>
|
#include <sensor/HwmonSensor.h>
|
||||||
#include <sensor/NvidiaTemperatureSensor.h>
|
#include <sensor/NvidiaSensor.h>
|
||||||
|
|
||||||
#define CONFIG_FILE "/etc/conf.d/sensors"
|
#define CONFIG_FILE "/etc/conf.d/sensors"
|
||||||
|
#define HWMON_BASE_PATH "/sys/class/hwmon"
|
||||||
|
|
||||||
std::vector<std::shared_ptr<TemperatureSensor>> sensors() {
|
namespace fs = std::filesystem;
|
||||||
std::vector<std::shared_ptr<TemperatureSensor>> sensors;
|
|
||||||
|
|
||||||
auto config = fopen(CONFIG_FILE, "r");
|
template <sensors_subfeature_type T>
|
||||||
if (sensors_init(config) != 0) {
|
std::vector<std::shared_ptr<Sensor>> sensors() {
|
||||||
throw std::runtime_error("Config file doesn't exist");
|
std::vector<std::shared_ptr<Sensor>> sensors;
|
||||||
}
|
|
||||||
|
|
||||||
int c = 0;
|
int c = 0;
|
||||||
for (const sensors_chip_name *chipName;
|
for (const sensors_chip_name *chipName;
|
||||||
@@ -26,32 +27,64 @@ std::vector<std::shared_ptr<TemperatureSensor>> sensors() {
|
|||||||
int d = 0;
|
int d = 0;
|
||||||
for (const sensors_feature *feature;
|
for (const sensors_feature *feature;
|
||||||
(feature = sensors_get_features(chipName, &d)) != NULL;) {
|
(feature = sensors_get_features(chipName, &d)) != NULL;) {
|
||||||
auto tempFeature = sensors_get_subfeature(chipName, feature,
|
auto subFeature = sensors_get_subfeature(chipName, feature, T);
|
||||||
SENSORS_SUBFEATURE_TEMP_INPUT);
|
if (subFeature) {
|
||||||
if (tempFeature)
|
sensors.push_back(
|
||||||
sensors.push_back(std::make_shared<HwmonTemperatureSensor>(
|
std::make_shared<HwmonSensor>(chipName, feature, subFeature));
|
||||||
chipName, feature, tempFeature));
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sensors.push_back(std::make_shared<NvidiaTemperatureSensor>());
|
|
||||||
|
|
||||||
return sensors;
|
return sensors;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<std::shared_ptr<PwmControl>> pwmControls() {
|
||||||
|
std::vector<std::shared_ptr<PwmControl>> controls;
|
||||||
|
|
||||||
|
const std::regex re_ctrl_enable("pwm[0-9]_enable");
|
||||||
|
const std::regex re_ctrl_mode("pwm[0-9]_mode");
|
||||||
|
const std::regex re_ctrl("pwm[0-9]");
|
||||||
|
|
||||||
|
if (!fs::exists(HWMON_BASE_PATH)) {
|
||||||
|
std::cout << HWMON_BASE_PATH << " doesn't exist" << std::endl;
|
||||||
|
} else {
|
||||||
|
for (const fs::directory_entry &hwmon_device :
|
||||||
|
fs::directory_iterator{HWMON_BASE_PATH}) {
|
||||||
|
|
||||||
|
for (const fs::directory_entry &control :
|
||||||
|
fs::directory_iterator{hwmon_device}) {
|
||||||
|
auto filename = control.path().filename().string();
|
||||||
|
|
||||||
|
if (regex_match(filename, re_ctrl)) {
|
||||||
|
auto controlPath = control.path().string();
|
||||||
|
|
||||||
|
controls.push_back(std::make_shared<PwmControl>(controlPath));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return controls;
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
class PWM pwm;
|
auto config = fopen(CONFIG_FILE, "r");
|
||||||
std::cout << '\n';
|
if (sensors_init(config) != 0) {
|
||||||
pwm.dumpValues();
|
throw std::runtime_error("Config file doesn't exist");
|
||||||
|
}
|
||||||
|
|
||||||
auto controls = pwm.getControls();
|
auto tempSensors = sensors<SENSORS_SUBFEATURE_TEMP_INPUT>();
|
||||||
std::cout << pwm.readValue(controls[0], PWM_CONTROL_PROPERTY::ENABLE)
|
tempSensors.push_back(std::make_shared<NvidiaSensor>());
|
||||||
<< std::endl;
|
|
||||||
|
|
||||||
auto tempSensors = sensors();
|
|
||||||
|
|
||||||
for (auto s : tempSensors) {
|
for (auto s : tempSensors) {
|
||||||
std::cout << s->name() << ": " << s->temperature() << std::endl;
|
std::cout << s->name() << ": " << s->value() << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "pwm" << std::endl;
|
||||||
|
|
||||||
|
auto pwmSensors = sensors<SENSORS_SUBFEATURE_FAN_INPUT>();
|
||||||
|
for (auto s : pwmSensors) {
|
||||||
|
std::cout << s->name() << ": " << s->value() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
19
app/src/sensor/HwmonSensor.cxx
Normal file
19
app/src/sensor/HwmonSensor.cxx
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#include <sensors/sensors.h>
|
||||||
|
|
||||||
|
#include <sensor/HwmonSensor.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
HwmonSensor::HwmonSensor(const sensors_chip_name *chipName,
|
||||||
|
const sensors_feature *feature,
|
||||||
|
const sensors_subfeature *subfeature)
|
||||||
|
: mChipName(chipName), mFeature(feature), mSubFeature(subfeature) {}
|
||||||
|
|
||||||
|
int HwmonSensor::value() {
|
||||||
|
double *value;
|
||||||
|
sensors_get_value(mChipName, mSubFeature->number, value);
|
||||||
|
|
||||||
|
return static_cast<int>(*value);
|
||||||
|
}
|
||||||
|
|
||||||
|
string HwmonSensor::name() { return sensors_get_label(mChipName, mFeature); }
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
#include <sensors/sensors.h>
|
|
||||||
|
|
||||||
#include <sensor/HwmonTemperatureSensor.h>
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
HwmonTemperatureSensor::HwmonTemperatureSensor(
|
|
||||||
const sensors_chip_name *chipName, const sensors_feature *feature,
|
|
||||||
const sensors_subfeature *subfeature)
|
|
||||||
: mChipName(chipName), mFeature(feature), mSubFeature(subfeature) {}
|
|
||||||
|
|
||||||
int HwmonTemperatureSensor::temperature() {
|
|
||||||
double *value;
|
|
||||||
sensors_get_value(mChipName, mSubFeature->number, value);
|
|
||||||
|
|
||||||
return static_cast<int>(*value);
|
|
||||||
}
|
|
||||||
|
|
||||||
string HwmonTemperatureSensor::name() {
|
|
||||||
return sensors_get_label(mChipName, mFeature);
|
|
||||||
}
|
|
||||||
21
app/src/sensor/NvidiaSensor.cxx
Normal file
21
app/src/sensor/NvidiaSensor.cxx
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#include <include/nvml.h>
|
||||||
|
|
||||||
|
#include <sensor/NvidiaSensor.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
NvidiaSensor::NvidiaSensor() { nvmlInit_v2(); }
|
||||||
|
NvidiaSensor::~NvidiaSensor() { nvmlShutdown(); }
|
||||||
|
|
||||||
|
int NvidiaSensor::value() {
|
||||||
|
nvmlDevice_t device;
|
||||||
|
|
||||||
|
nvmlDeviceGetHandleByIndex_v2(0, &device);
|
||||||
|
|
||||||
|
unsigned int temp;
|
||||||
|
nvmlDeviceGetTemperature(device, NVML_TEMPERATURE_GPU, &temp);
|
||||||
|
|
||||||
|
return static_cast<int>(temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
string NvidiaSensor::name() { return "GPU"; }
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
#include <include/nvml.h>
|
|
||||||
|
|
||||||
#include <sensor/NvidiaTemperatureSensor.h>
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
NvidiaTemperatureSensor::NvidiaTemperatureSensor() { nvmlInit_v2(); }
|
|
||||||
NvidiaTemperatureSensor::~NvidiaTemperatureSensor() { nvmlShutdown(); }
|
|
||||||
|
|
||||||
int NvidiaTemperatureSensor::temperature() {
|
|
||||||
nvmlDevice_t device;
|
|
||||||
|
|
||||||
nvmlDeviceGetHandleByIndex_v2(0, &device);
|
|
||||||
|
|
||||||
unsigned int temp;
|
|
||||||
nvmlDeviceGetTemperature(device, NVML_TEMPERATURE_GPU, &temp);
|
|
||||||
|
|
||||||
return static_cast<int>(temp);
|
|
||||||
}
|
|
||||||
|
|
||||||
string NvidiaTemperatureSensor::name() { return "GPU"; }
|
|
||||||
Reference in New Issue
Block a user