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
+27 -3
View File
@@ -1,17 +1,41 @@
from app_class import AppClass
from config import Config
from cpufreq import cpuFreq
from cpufreq import cpuFreq, cpufreq
from functools import cached_property
from mapping import governor_priority_mapping
from exceptions import GovernorNotFound
from typing import Optional
class Governor(AppClass):
def __init__(self, _config: Config) -> None:
super().__init__(_config)
self.log.info("current governor is: %s", self.current_governor)
@cached_property
def _cpufreq(self) -> cpuFreq:
return cpuFreq()
def _cpufreq(self) -> Optional[cpuFreq]:
try:
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:
self.log.debug(
"setting cpu governor to %s by using command self._cpufreq.set_governors(%s)",