added governor control code
This commit is contained in:
+29
-8
@@ -2,12 +2,26 @@ from app_class import AppClass
|
||||
from config import Config
|
||||
from cpufreq import cpuFreq, cpufreq # type: ignore
|
||||
from functools import cached_property
|
||||
from mapping import governor_priority_mapping
|
||||
from exceptions import GovernorNotFound
|
||||
from typing import Optional
|
||||
from typing import Optional, NamedTuple
|
||||
|
||||
|
||||
class Governor(NamedTuple):
|
||||
name: str
|
||||
priority: int
|
||||
|
||||
|
||||
_governor_list: list[Governor] = [
|
||||
Governor("performance", 0),
|
||||
Governor("schedutil", 1),
|
||||
Governor("ondemand", 2),
|
||||
Governor("conservative", 3),
|
||||
Governor("userspace", 4),
|
||||
Governor("powersave", 5)
|
||||
]
|
||||
|
||||
|
||||
class Governor(AppClass):
|
||||
class GovernorControl(AppClass):
|
||||
def __init__(self, _config: Config) -> None:
|
||||
super().__init__(_config)
|
||||
self.log.info("current governor is: %s", self.current_governor)
|
||||
@@ -21,20 +35,27 @@ class Governor(AppClass):
|
||||
return None
|
||||
|
||||
@property
|
||||
def current_governor(self) -> Optional[str]:
|
||||
def current_governor(self) -> Optional[Governor]:
|
||||
if self._cpufreq is None:
|
||||
return None
|
||||
_governor = set(self._cpufreq.get_governors().values())
|
||||
_governor_to_return = list(_governor)[0]
|
||||
_governor_to_return = self._establish_governor(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
|
||||
t_gov = self._establish_governor(gov)
|
||||
if t_gov.priority < _governor_to_return.priority:
|
||||
_governor_to_return == t_gov
|
||||
return _governor_to_return
|
||||
|
||||
def _establish_governor(self, governor_name: str) -> Governor:
|
||||
governor = next((g for g in _governor_list if g.name == governor_name), None)
|
||||
if governor is None:
|
||||
raise RuntimeError("could not establish governor")
|
||||
return governor
|
||||
|
||||
def set_governor(self, governor_name: str) -> None:
|
||||
self.log.debug(
|
||||
|
||||
Reference in New Issue
Block a user