Skip to content

Commit

Permalink
Allow non-default dynamic tuning implementations
Browse files Browse the repository at this point in the history
Let plugins choose between the default (periodic)
dynamic tuning and their own implementation.

If a plugin uses the default periodic tuning,
it must implement the update_tuning method
which is then periodically called by the main
daemon thread.

Non-periodic implementations may, e.g., start
their own threads.
  • Loading branch information
zacikpa committed May 28, 2024
1 parent 56990fc commit 2e5dce3
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 10 deletions.
15 changes: 8 additions & 7 deletions tuned/daemon/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,28 @@ def __init__(self, unit_manager, profile_loader, profile_names=None, config=None
self._daemon = consts.CFG_DEF_DAEMON
self._sleep_interval = int(consts.CFG_DEF_SLEEP_INTERVAL)
self._update_interval = int(consts.CFG_DEF_UPDATE_INTERVAL)
self._dynamic_tuning = consts.CFG_DEF_DYNAMIC_TUNING
self._recommend_command = True
self._rollback = consts.CFG_DEF_ROLLBACK
dynamic_plugins = []
if config is not None:
self._daemon = config.get_bool(consts.CFG_DAEMON, consts.CFG_DEF_DAEMON)
self._sleep_interval = int(config.get(consts.CFG_SLEEP_INTERVAL, consts.CFG_DEF_SLEEP_INTERVAL))
self._update_interval = int(config.get(consts.CFG_UPDATE_INTERVAL, consts.CFG_DEF_UPDATE_INTERVAL))
self._dynamic_tuning = config.get_bool(consts.CFG_DYNAMIC_TUNING, consts.CFG_DEF_DYNAMIC_TUNING)
self._recommend_command = config.get_bool(consts.CFG_RECOMMEND_COMMAND, consts.CFG_DEF_RECOMMEND_COMMAND)
self._rollback = config.get(consts.CFG_ROLLBACK, consts.CFG_DEF_ROLLBACK)
dynamic_plugins = config.get(consts.CFG_DYNAMIC_PLUGINS, [])
self._application = application
self._periodic_tuning = any(plugin.uses_periodic_tuning() for plugin in dynamic_plugins)
if self._sleep_interval <= 0:
self._sleep_interval = int(consts.CFG_DEF_SLEEP_INTERVAL)
if self._update_interval == 0:
self._dynamic_tuning = False
self._periodic_tuning = False
elif self._update_interval < self._sleep_interval:
self._update_interval = self._sleep_interval
self._sleep_cycles = self._update_interval // self._sleep_interval
log.info("using sleep interval of %d second(s)" % self._sleep_interval)
if self._dynamic_tuning:
log.info("dynamic tuning is enabled (can be overridden by plugins)")
if self._periodic_tuning:
log.info("using sleep interval of %d second(s)" % self._sleep_interval)
log.info("periodic tuning is enabled")
log.info("using update interval of %d second(s) (%d times of the sleep interval)" % (self._sleep_cycles * self._sleep_interval, self._sleep_cycles))

self._profile_recommender = ProfileRecommender(is_hardcoded = not self._recommend_command)
Expand Down Expand Up @@ -217,7 +218,7 @@ def _thread_code(self):
# For more details see TuneD rhbz#917587.
_sleep_cnt = self._sleep_cycles
while not self._cmd.wait(self._terminate, self._sleep_interval):
if self._dynamic_tuning:
if self._periodic_tuning:
_sleep_cnt -= 1
if _sleep_cnt <= 0:
_sleep_cnt = self._sleep_cycles
Expand Down
14 changes: 11 additions & 3 deletions tuned/plugins/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ def supports_static_tuning(cls):
def supports_dynamic_tuning(cls):
raise NotImplementedError()

@classmethod
def uses_periodic_tuning(cls):
raise NotImplementedError()

#
# Plugin configuration manipulation and helpers.
#
Expand Down Expand Up @@ -310,7 +314,7 @@ def instance_update_tuning(self, instance):
"""
if not instance.active:
return
if instance.dynamic_tuning_enabled:
if instance.dynamic_tuning_enabled and self.uses_periodic_tuning():
self._run_for_each_device(instance, self._instance_update_dynamic, instance.processed_devices.copy())

def instance_unapply_tuning(self, instance, rollback = consts.ROLLBACK_SOFT):
Expand All @@ -322,6 +326,7 @@ def instance_unapply_tuning(self, instance, rollback = consts.ROLLBACK_SOFT):

if instance.dynamic_tuning_enabled:
self._run_for_each_device(instance, self._instance_unapply_dynamic, instance.processed_devices)
self._instance_deinit_dynamic(instance)
if instance.static_tuning_enabled:
self._call_device_script(instance, instance.script_post,
"unapply", instance.processed_devices,
Expand Down Expand Up @@ -351,11 +356,14 @@ def _instance_unapply_static(self, instance, rollback = consts.ROLLBACK_SOFT):
def _instance_init_dynamic(self, instance):
pass

def _instance_deinit_dynamic(self, instance):
pass

def _instance_apply_dynamic(self, instance, device):
for option in [opt for opt in self._options_used_by_dynamic if self._storage_get(instance, self._commands[opt], device) is None]:
self._check_and_save_value(instance, self._commands[option], device)

self._instance_update_dynamic(instance, device)
if self.uses_periodic_tuning():
self._instance_update_dynamic(instance, device)

def _instance_unapply_dynamic(self, instance, device):
pass
Expand Down
4 changes: 4 additions & 0 deletions tuned/plugins/plugin_cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ def supports_static_tuning(cls):
def supports_dynamic_tuning(cls):
return True

@classmethod
def uses_periodic_tuning(cls):
return True

def _init_devices(self):
self._devices_supported = True
self._free_devices = set()
Expand Down
4 changes: 4 additions & 0 deletions tuned/plugins/plugin_disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ def supports_static_tuning(cls):
def supports_dynamic_tuning(cls):
return True

@classmethod
def uses_periodic_tuning(cls):
return True

def _init_devices(self):
super(DiskPlugin, self)._init_devices()
self._devices_supported = True
Expand Down
4 changes: 4 additions & 0 deletions tuned/plugins/plugin_eeepc_she.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def supports_static_tuning(cls):
def supports_dynamic_tuning(cls):
return True

@classmethod
def uses_periodic_tuning(cls):
return True

@classmethod
def _get_config_options(self):
return {
Expand Down
4 changes: 4 additions & 0 deletions tuned/plugins/plugin_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ def supports_static_tuning(cls):
def supports_dynamic_tuning(cls):
return True

@classmethod
def uses_periodic_tuning(cls):
return True

def _init_devices(self):
self._devices_supported = True
self._free_devices = set()
Expand Down

0 comments on commit 2e5dce3

Please sign in to comment.