added code to better control governor

This commit is contained in:
2024-12-15 17:51:13 +01:00
parent c638a0ddd6
commit 8cb98e102f
6 changed files with 57 additions and 15 deletions
+4 -5
View File
@@ -9,10 +9,7 @@ from typing import NamedTuple, Optional
class TriggerTuple(NamedTuple): class TriggerTuple(NamedTuple):
name: str name: str
interval_in_seconds: int interval_in_seconds: int
governor: str
class MissingConfig(Exception):
pass
class Config: class Config:
@@ -53,10 +50,12 @@ class Config:
for trigger in _triggers: for trigger in _triggers:
name = trigger.xpath('name').pop().text name = trigger.xpath('name').pop().text
interval_in_seconds = trigger.xpath('intervalInSeconds').pop().text interval_in_seconds = trigger.xpath('intervalInSeconds').pop().text
governor = trigger.xpath('governor').pop().text
ret_val.append( ret_val.append(
TriggerTuple( TriggerTuple(
name=name, name=name,
interval_in_seconds=int(interval_in_seconds) interval_in_seconds=int(interval_in_seconds),
governor=governor
) )
) )
+1
View File
@@ -6,6 +6,7 @@
<trigger> <trigger>
<name>roon</name> <name>roon</name>
<intervalInSeconds>2</intervalInSeconds> <intervalInSeconds>2</intervalInSeconds>
<governor>performance</governor>
</trigger> </trigger>
</triggers> </triggers>
</cpuGovernorAutoAdjust> </cpuGovernorAutoAdjust>
+10
View File
@@ -0,0 +1,10 @@
class TriggerImportError(Exception):
pass
class MissingConfig(Exception):
pass
class GovernorNotFound(Exception):
pass
+26 -2
View File
@@ -1,16 +1,40 @@
from app_class import AppClass from app_class import AppClass
from config import Config from config import Config
from cpufreq import cpuFreq from cpufreq import cpuFreq, cpufreq
from functools import cached_property from functools import cached_property
from mapping import governor_priority_mapping
from exceptions import GovernorNotFound
from typing import Optional
class Governor(AppClass): class Governor(AppClass):
def __init__(self, _config: Config) -> None: def __init__(self, _config: Config) -> None:
super().__init__(_config) super().__init__(_config)
self.log.info("current governor is: %s", self.current_governor)
@cached_property @cached_property
def _cpufreq(self) -> cpuFreq: def _cpufreq(self) -> Optional[cpuFreq]:
try:
return cpuFreq() return cpuFreq()
except cpufreq.CPUFreqErrorInit:
self.log.warning("cpu architecture has no governor support")
return None
@property
def current_governor(self) -> Optional[str]:
if self._cpufreq is None:
return None
_governor = set(self._cpufreq.get_governors().values())
_governor_to_return = list(_governor)[0]
if len(_governor) == 0:
raise GovernorNotFound("Unable to retrieve current governor")
if len(_governor) > 1:
self.log.error("multiple governors have been set, which is not expected.")
# returning governor with highest priority based on performance
for gov in _governor:
if governor_priority_mapping[gov] < governor_priority_mapping[_governor_to_return]:
_governor_to_return == gov
return _governor_to_return
def set_governor(self, governor_name: str) -> None: def set_governor(self, governor_name: str) -> None:
self.log.debug( self.log.debug(
+13 -2
View File
@@ -1,7 +1,18 @@
from types import MappingProxyType from types import MappingProxyType
from triggers import RoonTrigger from triggers import RoonTrigger
_dict_mapping = { _trigger_mapping = {
'roon': RoonTrigger 'roon': RoonTrigger
} }
trigger_mapping = MappingProxyType(_dict_mapping) trigger_mapping = MappingProxyType(_trigger_mapping)
_governor_priority_mapping = {
'performance': 0,
'schedutil': 1,
'ondemand': 2,
'conservative': 3,
'userspace': 4,
'performance': 5
}
governor_priority_mapping = MappingProxyType(_governor_priority_mapping)
+2 -5
View File
@@ -1,9 +1,6 @@
from app_class import AppClass from app_class import AppClass
from config import Config, TriggerTuple, MissingConfig from config import Config, TriggerTuple
from exceptions import MissingConfig, TriggerImportError
class TriggerImportError(Exception):
pass
class Trigger(AppClass): class Trigger(AppClass):