From e1ead3dfdf905c359a55af4a003f0a2be04de5b8 Mon Sep 17 00:00:00 2001 From: Domen Tabernik Date: Fri, 8 Mar 2019 21:27:41 +0000 Subject: [PATCH] Removed time from logging and addressed bug with systemd service stop. --- superfans.py | 38 ++++++++++++++++++------------------- superfans_gpu_controller.py | 34 ++++++++++++++++++++------------- 2 files changed, 40 insertions(+), 32 deletions(-) diff --git a/superfans.py b/superfans.py index 5beca1c..a7b99a3 100644 --- a/superfans.py +++ b/superfans.py @@ -72,13 +72,13 @@ def set_fan_with_full_preset(config, speed, zone): # Make sure fans are on Full setting, or else this won't stick for long s = get_preset(config) if s is False: - print(time.ctime() +": Unable to get current fan status; exiting") + print("Unable to get current fan status; exiting") return False if s != FAN_PRESET_FULL: - print(time.ctime() +": The fan controller is currently not set to Full mode (required for manual fan settings, which will otherwise be adjusted by the BMC within minutes); setting it now.") + print("The fan controller is currently not set to Full mode (required for manual fan settings, which will otherwise be adjusted by the BMC within minutes); setting it now.") set_preset(config, preset='full') - print(time.ctime() +": Waiting 5 seconds to let fans spin up...") + print("Waiting 5 seconds to let fans spin up...") time.sleep(5) ok = True @@ -88,10 +88,10 @@ def set_fan_with_full_preset(config, speed, zone): ok = ipmi_raw_cmd('0x30 0x70 0x66 0x01 0x01 0x%02x' % speed, **config) if ok: - print(time.ctime() +": Set %s fans on %s to %d%%." % (zone, config['hostname'], speed)) + print("Set %s fans on %s to %d%%." % (zone, config['hostname'], speed)) return True else: - print(time.ctime() +": Unable to update fans.") + print("Unable to update fans.") return False def set_fan(config, speed, zone): @@ -103,10 +103,10 @@ def set_fan(config, speed, zone): ok = ipmi_raw_cmd('0x30 0x70 0x66 0x01 0x%02x 0x%02x' % (zone, speed), **config) if ok: - print(time.ctime() +": Set %s fans on %s to %d%%." % (FAN_ZONES_STR[zone], config['hostname'], speed)) + print("Set %s fans on %s to %d%%." % (FAN_ZONES_STR[zone], config['hostname'], speed)) return True else: - print(time.ctime() +": Unable to update fans.") + print("Unable to update fans.") return False def get_fan(config, fan): @@ -148,9 +148,9 @@ def _set_preset(config): if fan_speed2 is False: return False - print(time.ctime() +": Preset: %s" % s) - print(time.ctime() +": Current fan speed (CPU Zone): %d%%" % int(fan_speed, 16)) - print(time.ctime() +": Current fan speed (Peripheral zone): %d%%" % int(fan_speed2, 16)) + print("Preset: %s" % s) + print("Current fan speed (CPU Zone): %d%%" % int(fan_speed, 16)) + print("Current fan speed (Peripheral zone): %d%%" % int(fan_speed2, 16)) return True @@ -159,7 +159,7 @@ def set_preset(config, preset): return False if ipmi_raw_cmd("0x30 0x45 0x01 0x0%d" % preset, **config): - print(time.ctime() +": Updated preset on %s." % config['hostname']) + print("Updated preset on %s." % config['hostname']) return True return False @@ -168,7 +168,7 @@ def ipmi_raw_cmd(raw_cmd, hostname = 'localhost', username=None, password=None, if hostname == 'localhost': if os.geteuid() != 0: - print(time.ctime() +": In order to communicate with the kernel's IPMI module, you must be root.") + print("In order to communicate with the kernel's IPMI module, you must be root.") sys.exit(1) cmd = 'ipmitool raw %s' % raw_cmd else: @@ -181,9 +181,9 @@ def ipmi_raw_cmd(raw_cmd, hostname = 'localhost', username=None, password=None, try: s = subprocess.check_output(cmd + " 2>&1", shell=True) except subprocess.CalledProcessError, ex: - print(time.ctime() +": Error: Problem running ipmitool") - print(time.ctime() +": Command: %s" % cmd) - print(time.ctime() +": Return code: %d" % ex) + print("Error: Problem running ipmitool") + print("Command: %s" % cmd) + print("Return code: %d" % ex) return False out = s.strip() @@ -197,7 +197,7 @@ def ipmi_fan_status(hostname = 'localhost', username=None, password=None, use_en if hostname == 'localhost': if os.geteuid() != 0: - print(time.ctime() +": In order to communicate with the kernel's IPMI module, you must be root.") + print("In order to communicate with the kernel's IPMI module, you must be root.") sys.exit(1) cmd = 'ipmitool sensor | grep FAN ' else: @@ -209,9 +209,9 @@ def ipmi_fan_status(hostname = 'localhost', username=None, password=None, use_en try: s = subprocess.check_output(cmd + " 2>&1", shell=True) except subprocess.CalledProcessError, ex: - print(time.ctime() +": Error: Problem running ipmitool") - print(time.ctime() +": Command: %s" % cmd) - print(time.ctime() +": Return code: %d" % ex) + print("Error: Problem running ipmitool") + print("Command: %s" % cmd) + print("Return code: %d" % ex) return False fan_status_return = {} diff --git a/superfans_gpu_controller.py b/superfans_gpu_controller.py index a223fd2..7d48007 100644 --- a/superfans_gpu_controller.py +++ b/superfans_gpu_controller.py @@ -3,7 +3,16 @@ # author: Domen Tabernik # 2019 -import time, superfans, subprocess +import time, superfans, subprocess, signal, sys + +class GracefulKiller: + kill_now = False + def __init__(self): + signal.signal(signal.SIGINT, self.exit_gracefully) + signal.signal(signal.SIGTERM, self.exit_gracefully) + + def exit_gracefully(self,signum, frame): + self.kill_now = True def retrieve_nvidia_gpu_temperature(): cmd = 'nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader' @@ -34,13 +43,13 @@ def superfans_gpu_controller(fan_settings, FAN_INCREASED_MIN_TIME=120, sleep_sec # save default present before changing anything default_preset = superfans.get_preset(superfan_config) - print(time.ctime() + ': Started fan control using GPU temperature.') - print(time.ctime() + ': Using settings:') + print('Started fan control using GPU temperature.') + print('Using settings:') for k in sorted(fan_settings.keys()): - print(time.ctime() + ': \t%d C = %d ' % (k, fan_settings[k]) + "%") + print('\t%d C = %d ' % (k, fan_settings[k]) + "%") print(time.ctime() + ':') - try: + try: FAN_MEMBERS = superfans.FAN_ZONES_MEMBERS[superfans.FAN_ZONE_SYS1] + \ superfans.FAN_ZONES_MEMBERS[superfans.FAN_ZONE_SYS2] @@ -50,7 +59,9 @@ def superfans_gpu_controller(fan_settings, FAN_INCREASED_MIN_TIME=120, sleep_sec prev_GPU_temp = [] - while True: + # ensure correct ending when SIGINT and SIGTERM are received + k = GracefulKiller() + while not k.kill_now: # get GPU temperature GPU_temp = retrieve_nvidia_gpu_temperature() @@ -100,9 +111,9 @@ def superfans_gpu_controller(fan_settings, FAN_INCREASED_MIN_TIME=120, sleep_sec superfans.set_fan(superfan_config, target_fan, superfans.FAN_ZONE_SYS2) if update_sys1_fan or update_sys2_fan: - print(time.ctime() + ': \tCurrent GPU measurements: %s' % ','.join(map(str,GPU_temp))) - print(time.ctime() + ': \tMoving average GPU measurements: %s' % ','.join(map(str,mean_GPU_temp))) - print(time.ctime() + ': \tTarget difference: SYS1 fan = %f; SYS2 fan = %f' % (max(diff_sys1_fan), max(diff_sys2_fan))) + print('\tCurrent GPU measurements: %s' % ','.join(map(str,GPU_temp))) + print('\tMoving average GPU measurements: %s' % ','.join(map(str,mean_GPU_temp))) + print('\tTarget difference: SYS1 fan = %f; SYS2 fan = %f' % (max(diff_sys1_fan), max(diff_sys2_fan))) print(time.ctime() + ':') @@ -113,10 +124,7 @@ def superfans_gpu_controller(fan_settings, FAN_INCREASED_MIN_TIME=120, sleep_sec finally: # revert back to default preset before finishing superfans.set_preset(superfan_config, default_preset) - print(time.ctime() + ': Reverted back to system default.') - - - + print('Reverted back to system default.') if __name__ == "__main__": # fan settings = {[in deg C]: [% fan], ...}