Advertisement
Guest User

amd_gpu_fan_ctrl.py

a guest
Feb 22nd, 2017
1,541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. from glob import glob
  4. from os import getegid
  5. from time import sleep
  6. from datetime import datetime
  7.  
  8. max_temp = 70
  9. min_temp = 50
  10. delay = 5
  11.  
  12. cpu_coef = 0
  13. with open('/proc/cpuinfo') as file:
  14.     for line in file:
  15.         if 'vendor_id' in line and 'Intel' in line:
  16.             cpu_coef = 1
  17.  
  18. def gpu_count():
  19.     result = 0
  20.  
  21.     dl = glob('/sys/bus/pci/drivers/amdgpu/*')
  22.  
  23.  
  24.     for line in dl:
  25.         if '0000' in line:
  26.             result += 1
  27.     return result
  28.  
  29.  
  30. def temp(card_num):
  31.     with open('/sys/class/drm/card{0}/device/hwmon/hwmon{1}/temp1_input'.format(card_num, card_num + cpu_coef)) as f:
  32.         for line in f:
  33.             result = round(int(line)/1000)
  34.         f.close()
  35.     return result
  36.  
  37.  
  38. def set_fan_speed(card_num, speed):
  39.     with open('/sys/class/drm/card{0}/device/hwmon/hwmon{1}/pwm1'.format(card_num, card_num + cpu_coef), 'w') as f:
  40.         f.write(str(speed))
  41.         f.close()
  42.  
  43.  
  44. def calc_fan_speed(temp):
  45.  
  46.     if temp < min_temp:
  47.         result = 0
  48.     elif temp > max_temp:
  49.         result = 255
  50.     else:
  51.         result = (temp - min_temp)*100/(max_temp-min_temp)*(255/100)
  52.  
  53.     if result > 255:
  54.         result = 255
  55.  
  56.     return round(result)
  57.  
  58.  
  59. def pwm1_enable(card_num):
  60.     with open('/sys/class/drm/card{0}/device/hwmon/hwmon{1}/pwm1_enable'.format(card_num, card_num + cpu_coef), 'w') as f:
  61.         f.write('1')
  62.         f.close()
  63.  
  64. while True:
  65.     try:
  66.         if getegid() == 0:
  67.             print('{}'.format(datetime.now().strftime("%Y-%m-%d %H:%M:%S")), flush=True)
  68.             for num in range(0, gpu_count()):
  69.                 try:
  70.                     gpu_temp = 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.                     pwm1_enable(num)
  76.                     continue
  77.         else:
  78.             print('please run this script under root', flush=True)
  79.             break
  80.         sleep(delay)
  81.     except KeyboardInterrupt:
  82.         print('\n')
  83.         if getegid() == 0:
  84.             for num in range(0, gpu_count()):
  85.                 with open('/sys/class/drm/card{0}/device/hwmon/hwmon{1}/pwm1_enable'.format(num, num + cpu_coef), 'w') as f:
  86.                     f.write('2')
  87.                     print('GPU#{0} fan auto mode enabled'.format(num), flush=True)
  88.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement