implemented cooldown period of 5 min in cpuload trigger

This commit is contained in:
2025-03-09 23:44:45 +01:00
parent 5667ccd784
commit 1f9beae3e8
3 changed files with 21 additions and 8 deletions
+1 -1
View File
@@ -46,7 +46,7 @@
<intervalInSeconds>5</intervalInSeconds> <intervalInSeconds>5</intervalInSeconds>
<governor>performance</governor> <governor>performance</governor>
<customConfig> <customConfig>
<fiveMinuteLoadThreshold>0.1</fiveMinuteLoadThreshold> <oneMinuteLoadThreshold>0.01</oneMinuteLoadThreshold>
</customConfig> </customConfig>
</trigger> </trigger>
</triggers> </triggers>
+19 -6
View File
@@ -2,31 +2,44 @@ from cpu_governor_auto_adjust.trigger import Trigger
from cpu_governor_auto_adjust.config import Config from cpu_governor_auto_adjust.config import Config
from functools import cached_property from functools import cached_property
from os import getloadavg from os import getloadavg
from datetime import datetime, timedelta
class CpuLoadTrigger(Trigger): class CpuLoadTrigger(Trigger):
def __init__(self, _config: Config) -> None: def __init__(self, _config: Config) -> None:
super().__init__(_config) super().__init__(_config)
self.timestamp_last_governor_change = datetime.now() - timedelta(minutes=10)
@cached_property @cached_property
def five_minute_load_threshold(self) -> float: def one_minute_load_threshold(self) -> float:
return float(self.config.custom_config['fiveMinuteLoadThreshold']) return float(self.config.custom_config['oneMinuteLoadThreshold'])
@property @property
def current_load(self) -> float: def current_load(self) -> float:
return getloadavg()[1] _current_load = getloadavg()[0]
self.log.debug("current load: %s", _current_load)
return _current_load
def run(self) -> None: def run(self) -> None:
current_active = self.active current_active = self.active
new_active = self.current_load >= self.five_minute_load_threshold current_time = datetime.now()
new_active = self.current_load >= self.one_minute_load_threshold
if not current_active and new_active: if not current_active and new_active:
self.log.info( self.log.info(
"activating trigger, load: %s, threshold: %s, governor: %s", "activating trigger, load: %s, threshold: %s, governor: %s",
self.current_load, self.five_minute_load_threshold, self.governor.name self.current_load, self.one_minute_load_threshold, self.governor.name
) )
self.timestamp_last_governor_change = current_time
elif current_active and not new_active: elif current_active and not new_active:
if (current_time - timedelta(minutes=5)) < self.timestamp_last_governor_change:
self.log.info("cooldown period, staying on governor: %s", self.governor.name)
return
self.log.info( self.log.info(
"deactivating trigger, load: %s, threshold: %s, governor: %s", "deactivating trigger, load: %s, threshold: %s, governor: %s",
self.current_load, self.five_minute_load_threshold, self.governor.name self.current_load, self.one_minute_load_threshold, self.governor.name
) )
self.active = new_active self.active = new_active
+1 -1
View File
@@ -1,6 +1,6 @@
[project] [project]
name = "cpu_governor_auto_adjust" name = "cpu_governor_auto_adjust"
version = "0.1.35" version = "0.1.39"
description = "This application has been developed to automatically change cpu governor based on certain triggers." description = "This application has been developed to automatically change cpu governor based on certain triggers."
authors = [ authors = [
{ name = "Martin Reurekas", email = "martin@semrks.nl" } { name = "Martin Reurekas", email = "martin@semrks.nl" }