Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- from time import sleep
- from datetime import datetime
- max_temp = 70
- min_temp = 50
- delay = 5
- hwmonprefix = '/sys/class/hwmon'
- def list_hwmons():
- result = []
- for mon in os.listdir(hwmonprefix):
- tempname = os.path.join(hwmonprefix, mon, 'name')
- if os.path.isfile(tempname):
- with open(tempname, 'r') as tempmon:
- drivername = tempmon.read().rstrip('\n')
- if drivername in ['radeon', 'amdgpu']:
- result.append(os.path.join(hwmonprefix, mon))
- return result
- def get_temp(card_num):
- with open(hwmons[card_num] + '/temp1_input') as f:
- for line in f:
- result = round(int(line)/1000)
- f.close()
- return result
- def set_fan_speed(card_num, speed):
- with open(hwmons[card_num] + '/pwm1', '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 set_pwm1_enable(card_num, on_off):
- with open(hwmons[card_num] + '/pwm1_enable', 'w') as f:
- if on_off:
- f.write('1')
- else:
- f.write('2')
- f.close()
- hwmons = list_hwmons()
- while True:
- try:
- if os.getegid() == 0:
- print('{}'.format(datetime.now().strftime("%Y-%m-%d %H:%M:%S")), flush=True)
- for num in range(0, len(hwmons)):
- try:
- gpu_temp = get_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:
- set_pwm1_enable(num, True)
- print('#{} pwm1_enable set to 1 (manual)'.format(num), flush=True)
- continue
- else:
- print('please run this script under root', flush=True)
- break
- sleep(delay)
- except KeyboardInterrupt:
- print('\n')
- if os.getegid() == 0:
- for num in range(0, len(hwmons)):
- set_pwm1_enable(num, False)
- print('#{} pwm1_enable set to 2 (auto)'.format(num), flush=True)
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement