Advertisement
Guest User

amd_gpu_fan_ctrl.py

a guest
Mar 8th, 2017
982
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.00 KB | None | 0 0
  1. import os
  2. from time import sleep
  3. from datetime import datetime
  4.  
  5.  
  6. max_temp = 70
  7. min_temp = 50
  8. delay = 5
  9.  
  10. hwmonprefix = '/sys/class/hwmon'
  11.  
  12.  
  13. def list_hwmons():
  14.     result = []
  15.     for mon in os.listdir(hwmonprefix):
  16.         tempname = os.path.join(hwmonprefix, mon, 'name')
  17.         if os.path.isfile(tempname):
  18.             with open(tempname, 'r') as tempmon:
  19.                 drivername = tempmon.read().rstrip('\n')
  20.                 if drivername in ['radeon', 'amdgpu']:
  21.                     result.append(os.path.join(hwmonprefix, mon))
  22.     return result
  23.  
  24.  
  25. def get_temp(card_num):
  26.     with open(hwmons[card_num] + '/temp1_input') as f:
  27.         for line in f:
  28.             result = round(int(line)/1000)
  29.         f.close()
  30.     return result
  31.  
  32.  
  33. def set_fan_speed(card_num, speed):
  34.     with open(hwmons[card_num] + '/pwm1', 'w') as f:
  35.         f.write(str(speed))
  36.         f.close()
  37.  
  38.  
  39. def calc_fan_speed(temp):
  40.     if temp < min_temp:
  41.         result = 0
  42.     elif temp > max_temp:
  43.         result = 255
  44.     else:
  45.         result = (temp - min_temp)*100/(max_temp-min_temp)*(255/100)
  46.  
  47.     if result > 255:
  48.         result = 255
  49.  
  50.     return round(result)
  51.  
  52.  
  53. def set_pwm1_enable(card_num, on_off):
  54.     with open(hwmons[card_num] + '/pwm1_enable', 'w') as f:
  55.         if on_off:
  56.             f.write('1')
  57.         else:
  58.             f.write('2')
  59.         f.close()
  60.  
  61.  
  62. hwmons = list_hwmons()
  63.  
  64. while True:
  65.     try:
  66.         if os.getegid() == 0:
  67.             print('{}'.format(datetime.now().strftime("%Y-%m-%d %H:%M:%S")), flush=True)
  68.             for num in range(0, len(hwmons)):
  69.                 try:
  70.                     gpu_temp = get_temp(num)
  71.                     fan_speed = calc_fan_speed(gpu_temp)
  72.                     set_fan_speed(num, fan_speed)
  73.                     print('#{0} : {1}°C -> {2}'.format(num, gpu_temp, fan_speed), flush=True)
  74.                 except:
  75.                     set_pwm1_enable(num, True)
  76.                     print('#{} pwm1_enable set to 1 (manual)'.format(num), flush=True)
  77.                     continue
  78.         else:
  79.             print('please run this script under root', flush=True)
  80.             break
  81.         sleep(delay)
  82.     except KeyboardInterrupt:
  83.         print('\n')
  84.         if os.getegid() == 0:
  85.             for num in range(0, len(hwmons)):
  86.                 set_pwm1_enable(num, False)
  87.                 print('#{} pwm1_enable set to 2 (auto)'.format(num), flush=True)
  88.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement