Initial commit

This commit is contained in:
2022-07-28 22:16:03 +02:00
commit cdac555c3c
13 changed files with 187 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
oot/
compile_commands.json

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

0
app/.projectile Normal file
View File

18
app/CMakeLists.txt Normal file
View File

@@ -0,0 +1,18 @@
cmake_minimum_required(VERSION 3.0)
project(fantasize)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
add_executable(app
src/main.cxx
src/nvidia.cxx
src/pwm.cxx
)
set_property(TARGET app PROPERTY CXX_STANDARD 17)
target_include_directories(app PUBLIC include /opt/cuda)
target_link_libraries(app PUBLIC sensors nvidia-ml)

11
app/include/nvidia.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef NVIDIA_H_
#define NVIDIA_H_
class Nvidia {
public:
Nvidia();
~Nvidia();
double get_gpu_temperature();
};
#endif // NVIDIA_H_

30
app/include/pwm.h Normal file
View File

@@ -0,0 +1,30 @@
#ifndef PWM_H_
#define PWM_H_
#include <string>
#include <unordered_map>
typedef struct {
std::string control;
std::string enable;
std::string mode;
} PWM_CONTROL;
typedef enum { FULL_SPEED = 0, MANUAL_CONTROL, AUTOMATIC } PWM_ENABLE;
typedef enum { DC = 0, PWM } PWM_MODE;
class PWM {
public:
PWM();
void dumpValues();
void setEnable(PWM_CONTROL control, PWM_ENABLE value);
void setMode(PWM_CONTROL control, PWM_MODE mode);
void setValuePwm(PWM_CONTROL control, int pwm);
void setValuePercent(PWM_CONTROL control, int percentage);
private:
std::unordered_map<std::string, PWM_CONTROL> mPwmControls;
};
#endif // PWM_H_

54
app/src/main.cxx Normal file
View File

@@ -0,0 +1,54 @@
#include <nvidia.h>
#include <pwm.h>
#include <sensors/sensors.h>
#include <iostream>
int main() {
auto config = std::fopen("/etc/conf.d/sensors", "r");
if (sensors_init(config) != 0) {
std::cout << "Fuck" << std::endl;
return 1;
}
int c = 0;
for (const sensors_chip_name *i;
(i = sensors_get_detected_chips(0, &c)) != NULL;) {
std::cout << i->prefix << std::endl;
int d = 0;
for (const sensors_feature *j; (j = sensors_get_features(i, &d)) != NULL;) {
const sensors_subfeature *temp_feature =
sensors_get_subfeature(i, j, SENSORS_SUBFEATURE_TEMP_INPUT);
if (temp_feature) {
std::cout << sensors_get_label(i, j);
double value;
if (sensors_get_value(i, temp_feature->number, &value) == 0)
std::cout << ": " << value << " C" << std::endl;
}
const sensors_subfeature *fan_feature =
sensors_get_subfeature(i, j, SENSORS_SUBFEATURE_FAN_INPUT);
if (fan_feature) {
std::cout << sensors_get_label(i, j);
double value;
if (sensors_get_value(i, fan_feature->number, &value) == 0)
std::cout << ": " << value << " RPM" << std::endl;
}
}
std::cout << std::endl;
}
Nvidia nv;
auto temp = nv.get_gpu_temperature();
std::cout << "\nGPU Temp: " << temp << std::endl;
PWM pwm;
pwm.dumpValues();
return 0;
}

16
app/src/nvidia.cxx Normal file
View File

@@ -0,0 +1,16 @@
#include <include/nvml.h>
#include <nvidia.h>
Nvidia::Nvidia() { nvmlInit_v2(); }
Nvidia::~Nvidia() { nvmlShutdown(); }
double Nvidia::get_gpu_temperature() {
nvmlDevice_t device;
nvmlDeviceGetHandleByIndex_v2(0, &device);
unsigned int temp;
nvmlDeviceGetTemperature(device, NVML_TEMPERATURE_GPU, &temp);
return static_cast<double>(temp);
}

56
app/src/pwm.cxx Normal file
View File

@@ -0,0 +1,56 @@
#include <pwm.h>
#include <filesystem>
#include <iostream>
#include <regex>
namespace fs = std::filesystem;
using namespace std;
#define HWMON_BASE_PATH "/sys/class/hwmon"
#define PWM_POSTFIX_ENABLE "_enable"
#define PWM_POSTFIX_MODE "_mode"
PWM::PWM() {
const regex re_ctrl_enable("pwm[0-9]_enable");
const regex re_ctrl_mode("pwm[0-9]_mode");
const regex re_ctrl("pwm[0-9]");
if (!fs::exists(HWMON_BASE_PATH)) {
cerr << HWMON_BASE_PATH << " doesn't exist" << endl;
} else {
for (const fs::directory_entry &hwmon_device :
fs::directory_iterator{HWMON_BASE_PATH}) {
cout << hwmon_device << endl;
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();
fs::path path_ctrl_enable(
string(controlPath + string(PWM_POSTFIX_ENABLE)));
fs::path path_ctrl_mode(
string(controlPath + string(PWM_POSTFIX_MODE)));
if (fs::exists(path_ctrl_enable) && fs::exists(path_ctrl_mode))
cout << control << endl;
mPwmControls.insert(
{controlPath, PWM_CONTROL{controlPath, path_ctrl_enable.string(),
path_ctrl_mode.string()}});
}
}
}
}
}
void PWM::dumpValues() {
for (auto control : mPwmControls) {
cout << control.second.control << ", " << control.second.enable << ", "
<< control.second.mode << endl;
}
}