diff --git a/cpu_governor_auto_adjust.xml b/cpu_governor_auto_adjust.xml index c2cc598..59c0d37 100644 --- a/cpu_governor_auto_adjust.xml +++ b/cpu_governor_auto_adjust.xml @@ -40,5 +40,14 @@ 23:59 + + cpu_load + run_once + 5 + performance + + 0.1 + + diff --git a/cpu_governor_auto_adjust/mapping.py b/cpu_governor_auto_adjust/mapping.py index 57955b2..6359db1 100644 --- a/cpu_governor_auto_adjust/mapping.py +++ b/cpu_governor_auto_adjust/mapping.py @@ -1,10 +1,11 @@ from types import MappingProxyType -from cpu_governor_auto_adjust.triggers import TestTrigger1, TestTrigger2, RoonTrigger, WakeupTrigger +from cpu_governor_auto_adjust.triggers import TestTrigger1, TestTrigger2, RoonTrigger, WakeupTrigger, CpuLoadTrigger _trigger_mapping = { 'test_trigger1': TestTrigger1, 'test_trigger2': TestTrigger2, 'roon': RoonTrigger, - 'wakeup': WakeupTrigger + 'wakeup': WakeupTrigger, + 'cpu_load': CpuLoadTrigger } trigger_mapping = MappingProxyType(_trigger_mapping) diff --git a/cpu_governor_auto_adjust/triggers/__init__.py b/cpu_governor_auto_adjust/triggers/__init__.py index f48056f..8c0fa95 100644 --- a/cpu_governor_auto_adjust/triggers/__init__.py +++ b/cpu_governor_auto_adjust/triggers/__init__.py @@ -1,6 +1,7 @@ -__all__ = ["test_trigger1", "test_trigger2", "roon", "wakeup"] +__all__ = ["test_trigger1", "test_trigger2", "roon", "wakeup", "cpu_load"] from .test_trigger1 import TestTrigger1 from .test_trigger2 import TestTrigger2 from .roon import RoonTrigger from .wakeup import WakeupTrigger +from .cpu_load import CpuLoadTrigger diff --git a/cpu_governor_auto_adjust/triggers/cpu_load.py b/cpu_governor_auto_adjust/triggers/cpu_load.py new file mode 100644 index 0000000..64b9c87 --- /dev/null +++ b/cpu_governor_auto_adjust/triggers/cpu_load.py @@ -0,0 +1,32 @@ +from cpu_governor_auto_adjust.trigger import Trigger +from cpu_governor_auto_adjust.config import Config +from functools import cached_property +from os import getloadavg + + +class CpuLoadTrigger(Trigger): + def __init__(self, _config: Config) -> None: + super().__init__(_config) + + @cached_property + def five_minute_load_threshold(self) -> float: + return float(self.config.custom_config['fiveMinuteLoadThreshold']) + + @property + def current_load(self) -> float: + return getloadavg()[1] + + def run(self) -> None: + current_active = self.active + new_active = self.current_load >= self.five_minute_load_threshold + if not current_active and new_active: + self.log.info( + "activating trigger, load: %s, threshold: %s, governor: %s", + self.current_load, self.five_minute_load_threshold, self.governor.name + ) + elif current_active and not new_active: + self.log.info( + "deactivating trigger, load: %s, threshold: %s, governor: %s", + self.current_load, self.five_minute_load_threshold, self.governor.name + ) + self.active = new_active diff --git a/pyproject.toml b/pyproject.toml index eb021c5..11b89f1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "cpu_governor_auto_adjust" -version = "0.1.34" +version = "0.1.35" description = "This application has been developed to automatically change cpu governor based on certain triggers." authors = [ { name = "Martin Reurekas", email = "martin@semrks.nl" }