Skip to content

Instantly share code, notes, and snippets.

@matcap
Created July 27, 2017 21:43
Show Gist options
  • Save matcap/41abf1b903828a261544268bb7a911c1 to your computer and use it in GitHub Desktop.
Save matcap/41abf1b903828a261544268bb7a911c1 to your computer and use it in GitHub Desktop.
Nvidia fan curve script
#!/usr/bin/python3
import subprocess as sp
import time
import sys
import re
def exec_cmd(cmd):
proc = sp.Popen(cmd, shell=True, stdout=sp.PIPE)
res = proc.communicate()
out = res[0].decode(sys.stdout.encoding)
return out
temp_prog = re.compile(r"(GPU Current Temp) +: +([0-9]+) +C")
def get_gpu_temp():
out = exec_cmd("nvidia-smi -q -d TEMPERATURE")
return int(temp_prog.search(out).group(2))
def set_gpu_fan(speed):
if speed < 0:
speed = 0
if speed > 90:
speed = 90
cmd = "nvidia-settings -a [gpu:0]/GPUFanControlState=1 -a [fan:0]/GPUTargetFanSpeed={}".format(speed)
exec_cmd(cmd)
ACTIVATION_TEMP = 50 # Threshold temperature
ACTIVATION_ERR = 3 # Temperature error around threshold
CURVE_MIN = 15 # Minimum fan speed
CURVE_SLOPE = 2 # Slope of fan speed curve
def fan_curve(temp):
if temp < ACTIVATION_TEMP - ACTIVATION_ERR:
return 0
elif temp > ACTIVATION_TEMP + ACTIVATION_ERR:
return int(CURVE_MIN + (temp - ACTIVATION_TEMP) * CURVE_SLOPE)
else:
return None
current_fan_speed = None
while True:
temp = get_gpu_temp()
speed = fan_curve(temp)
if speed != None and speed != current_fan_speed:
current_fan_speed = speed
set_gpu_fan(speed)
print("Changed fan speed to {}% at temp of {}C".format(speed, temp))
time.sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment