From ba858208d67863b2f4415fb8651bab45c3f7fdcd Mon Sep 17 00:00:00 2001 From: Kyle Gospodnetich Date: Tue, 24 Sep 2024 10:34:44 -0700 Subject: [PATCH] Add tuned-adm handling --- src/adjustor/drivers/general/__init__.py | 54 +++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/src/adjustor/drivers/general/__init__.py b/src/adjustor/drivers/general/__init__.py index 0971ebf..32580c9 100644 --- a/src/adjustor/drivers/general/__init__.py +++ b/src/adjustor/drivers/general/__init__.py @@ -27,6 +27,7 @@ def __init__( self.old_sched = None self.sched_proc = None self.ppd_supported = None + self.tuned_supported = None self.is_steamdeck = is_steamdeck self.ovr_enabled = False self.should_exit = Event() @@ -54,6 +55,9 @@ def settings(self): except Exception as e: logger.warning(f"powerprofilectl returned with error:\n{e}") + # TuneD + if self.tuned_supported is None: + self.tuned_supported = False if tuned := shutil.which('tuned-adm'): try: if os.environ.get("HHD_PPD_MASK", None): @@ -66,7 +70,7 @@ def settings(self): stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, ) - self.ppd_supported = True + self.tuned_supported = True except Exception as e: logger.warning(f"tuned-adm returned with error:\n{e}") @@ -139,6 +143,54 @@ def update(self, conf: Config): logger.warning(f"powerprofilectl returned with error:\n{e}") self.ppd_supported = False + # Handle TuneD + if self.tuned_supported: + curr = time.time() + ppd_tuned_mapping = { + "power-saver": "powersave", + "balanced": "balanced", + "performance": "throughput-performance" + } + new_profile = ppd_tuned_mapping.get(conf.get("tdp.general.profile", self.target)) + if new_profile != self.target and new_profile and self.target: + logger.info(f"Setting TuneD profile to '{new_profile}'") + self.target = new_profile + try: + subprocess.run( + [shutil.which('tuned-adm'), "profile", new_profile], + check=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + except Exception as e: + self.tuned_supported = False + logger.warning(f"tuned-adm returned with error:\n{e}") + self.tuned_supported = False + elif not self.last_check or curr - self.last_check > 2: + # Update profile every 2 seconds + self.last_check = curr + try: + res = subprocess.run( + [shutil.which('tuned-adm'), "active"], + check=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + + tuned_ppd_mapping = { + "powersave": "power-saver", + "balanced": "balanced", + "throughput-performance": "performance" + } + self.target = tuned_ppd_mapping.get(res.stdout.decode().split(":")[1].strip()) # type: ignore + + if self.target != conf["tdp.general.profile"].to(str): + conf["tdp.general.profile"] = self.target + except Exception as e: + self.tuned_supported = False + logger.warning(f"powerprofilectl returned with error:\n{e}") + self.tuned_supported = False + # Handle sched if self.avail_scheds: # Check health and print error