Files
cpu_governor_auto_adjust/governor.py
T

57 lines
2.1 KiB
Python

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
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) -> 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)",
governor_name, governor_name
)
if self._config.testmode:
self.log.warning("application is running in testmode, cpu governor not set")
return None
if self._cpufreq is None:
return None
if governor_name not in self._cpufreq.available_governors:
self.log.error("governor %s not supported by cpu", governor_name)
return None
self._cpufreq.set_governors(governor_name)