Added basic functionality to control fans of nvidia gpu

This commit is contained in:
Ksan 2025-04-12 22:55:14 +02:00
parent c662b0d216
commit 88a5d2a45e
3 changed files with 92 additions and 0 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
/fanControl
# Prerequisites
*.d

View File

@ -1,2 +1,24 @@
# fanControl
Simple script for controlling your system fans
Currently only works for nvidia gpu.
To install just clone the repo and compile the code:
<code> g++ -O3 -o fanControl main.cpp</code>
Best used with tmux and running <code> sudo ./fanControl </code>
<br>
<h2> Setting custom fan curve </h2>
To make your own custom fan curve just edit "TEMP_THRESHOLD" and "FAN_SPEED".
<b>Example:</b><code> TEMP_THRESHOLD[] = {30, 50, 80};
FAN_SPEEd[] = {0, 30, 50, 80};
</code>
When the GPU temperature is below 30°C, the fans will not spin (0% speed).
Once the temperature reaches 30°C or higher, the fans will spin at 30% speed.
When the temperature reaches 50°C or higher, the fan speed increases to 50%.
At 80°C or higher, the fans will spin at full speed (80%).

69
main.cpp Normal file
View File

@ -0,0 +1,69 @@
#include <iostream>
#include <sstream>
#include <unistd.h>
const int MIN_TEMP = 30;
const int MAX_TEMP = 80;
const int TEMP_THRESHOLD[] ={30, 40, 55, 60, 70, 80, 85};
const int FAN_SPEED[] = { 0, 30, 40, 50, 60, 65, 80, 100};
int getTemperature(){
std::string command = "nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader,nounits";
FILE* fp = popen(command.c_str(), "r");
if(fp == NULL){
std::cerr << "Failed to run command to get GPU temperature." << std::endl;
return -1;
}
char buffer[128];
std::string result = "";
while(fgets(buffer, sizeof(buffer), fp) != NULL){
result += buffer;
}
pclose(fp);
int temp;
std::stringstream(result) >> temp;
return temp;
}
void setGpuFanSpeed(int speed){
if(speed < 0 || speed > 100){
std::cerr << "Invalid fan speed:" << speed << ", speed must be between 0 and 100." << std::endl;
return;
}
std::string command = "nvidia-settings --assign 'GPUTargetFanSpeed=" + std::to_string(speed) + "'";
if(system(command.c_str()) == 0){
std::cout << "Fan speed set to " << speed << "%" << std::endl;
}else{
std::cerr << "Failed to change fan speed." << std::endl;
}
}
int getFanSpeedFromTemperature(int temperature){
for(size_t i = 0; i < std::size(TEMP_THRESHOLD); i++){
if(temperature < TEMP_THRESHOLD[i]) return FAN_SPEED[i];
}
return FAN_SPEED[std::size(FAN_SPEED) - 1]; // max speed
}
int main(int argc, char *argv[]){
while(true){
int temperature = getTemperature();
if(temperature == -1){
return 1;
}
std::cout << "GPU temperature: " << temperature << "°C" << std::endl;
setGpuFanSpeed(getFanSpeedFromTemperature(temperature));
sleep(5);
}
}