Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- from glob import glob
- from os import getegid
- from time import sleep
- from datetime import datetime
- max_temp = 70
- min_temp = 50
- delay = 5
- cpu_coef = 0
- with open('/proc/cpuinfo') as file:
- for line in file:
- if 'vendor_id' in line and 'Intel' in line:
- cpu_coef = 1
- def gpu_count():
- result = 0
- dl = glob('/sys/bus/pci/drivers/amdgpu/*')
- for line in dl:
- if '0000' in line:
- result += 1
- return result
- def temp(card_num):
- with open('/sys/class/drm/card{0}/device/hwmon/hwmon{1}/temp1_input'.format(card_num, card_num + cpu_coef)) as f:
- for line in f:
- result = round(int(line)/1000)
- f.close()
- return result
- def set_fan_speed(card_num, speed):
- with open('/sys/class/drm/card{0}/device/hwmon/hwmon{1}/pwm1'.format(card_num, card_num + cpu_coef), 'w') as f:
- f.write(str(speed))
- f.close()
- def calc_fan_speed(temp):
- if temp < min_temp:
- result = 0
- elif temp > max_temp:
- result = 255
- else:
- result = (temp - min_temp)*100/(max_temp-min_temp)*(255/100)
- if result > 255:
- result = 255
- return round(result)
- def pwm1_enable(card_num):
- with open('/sys/class/drm/card{0}/device/hwmon/hwmon{1}/pwm1_enable'.format(card_num, card_num + cpu_coef), 'w') as f:
- f.write('1')
- f.close()
- while True:
- try:
- if getegid() == 0:
- print('{}'.format(datetime.now().strftime("%Y-%m-%d %H:%M:%S")), flush=True)
- for num in range(0, gpu_count()):
- try:
- gpu_temp = temp(num)
- fan_speed = calc_fan_speed(gpu_temp)
- set_fan_speed(num, fan_speed)
- print('#{0} : {1}Β°C -> {2}'.format(num, gpu_temp, fan_speed), flush=True)
- except:
- pwm1_enable(num)
- continue
- else:
- print('please run this script under root', flush=True)
- break
- sleep(delay)
- except KeyboardInterrupt:
- print('\n')
- if getegid() == 0:
- for num in range(0, gpu_count()):
- with open('/sys/class/drm/card{0}/device/hwmon/hwmon{1}/pwm1_enable'.format(num, num + cpu_coef), 'w') as f:
- f.write('2')
- print('GPU#{0} fan auto mode enabled'.format(num), flush=True)
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement