def main(): default_fan_config = '''#Fan Control Matrix. [<Temp in C>,<Fanspeed in %>] speed_matrix: - [0, 0] - [30, 33] - [45, 50] - [60, 66] - [65, 69] - [70, 75] - [75, 89] - [80, 100] # optional # cards: # can be any card returned from `ls /sys/class/drm | grep "^card[[:digit:]]$"` # - card0 ''' config = None for location in CONFIG_LOCATIONS: if os.path.isfile(location): config = load_config(location) break if config is None: logger.info(f'no config found, creating one in {CONFIG_LOCATIONS[-1]}') with open(CONFIG_LOCATIONS[-1], 'w') as f: f.write(default_fan_config) f.flush() config = load_config(CONFIG_LOCATIONS[-1]) FanController(config).main()
def _verify_card(self): for endpoint in ('temp1_input', 'pwm1_max', 'pwm1_min', 'pwm1_enable', 'pwm1'): if endpoint not in self._endpoints: logger.info('skipping card: %s as its missing endpoint %s', self._identifier, endpoint) raise FileNotFoundError
def __init__(self, config): self._scanner = Scanner(config.get('cards')) if len(self._scanner.cards) < 1: logger.error('no compatible cards found, exiting') sys.exit(1) self.curve = Curve(config.get('speed_matrix')) self._frequency = 1
def write_endpoint(self, endpoint, data): try: with open(self._endpoints[endpoint], 'w') as e: return e.write(str(data)) except PermissionError: logger.error('Failed writing to devfs file, are you sure your running as root?') sys.exit(1)
def main(self): logger.info(f'starting amdgpu-fan') speed_by_card = {} while True: for name, card in self._scanner.cards.items(): current_speed = speed_by_card.get(name) # Reset current speed if it is off for more than 10% if current_speed is not None and abs(current_speed - card.fan_speed) > 10: current_speed = None temp = card.gpu_temp speed = max(0, int(self.curve.get_speed(int(temp)))) if current_speed is not None and current_speed >= speed: speed = max( 0, int(self.curve.get_speed(int(temp) + self.temp_drop))) if current_speed <= speed: continue logger.debug( f'{name}: Temp {temp}, Setting fan speed to: {speed}, fan speed: {card.fan_speed}, min: {card.fan_min}, max: {card.fan_max}' ) card.set_fan_speed(speed) speed_by_card[name] = speed time.sleep(self._interval)
def __init__(self, config): self._scanner = Scanner(config.get('cards')) if len(self._scanner.cards) < 1: logger.error('no compatible cards found, exiting') sys.exit(1) speed_matrix = config.get('speed_matrix') self.temp_drop = config.get('temp_drop', 5) self.curve = Curve(speed_matrix) self._frequency = 1 self._interval = 1 / self._frequency
def main(self): logger.info(f'starting amdgpu-fan') while True: for name, card in self._scanner.cards.items(): temp = card.gpu_temp speed = int(self.curve.get_speed(int(temp))) if speed < 0: speed = 0 logger.debug(f'{name}: Temp {temp}, Setting fan speed to: {speed}, fan speed{card.fan_speed}, min:{card.fan_min}, max:{card.fan_max}') card.set_fan_speed(speed) time.sleep(self._frequency)
def main(): config = None for location in CONFIG_LOCATIONS: if os.path.isfile(location): config = load_config(location) break if config is None: logger.info(f'no config found, creating one in {CONFIG_LOCATIONS[-1]}') with open(CONFIG_LOCATIONS[-1], 'w+') as f: f.write(yaml.dump(dict(speed_matrix=[[0, 0], [100, 100]]))) f.flush() config = load_config(CONFIG_LOCATIONS[-1]) FanController(config).main()
def load_config(path): logger.debug(f'loading config from {path}') with open(path) as f: return yaml.load(f, Loader=yaml.FullLoader)
def load_config(path): logger.debug(f'loading config from {path}') with open(path) as f: return yaml.safe_load(f)