From fc97d2b2793718fa8a994b538cd8556f702d47e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20=C5=A0karvada?= Date: Fri, 30 Nov 2012 14:58:34 +0100 Subject: [PATCH] plugins: added sysfs plugin --- tuned/plugins/base.py | 2 +- tuned/plugins/plugin_sysfs.py | 55 +++++++++++++++++++++++++++++++++++ tuned/utils/commands.py | 6 ++-- 3 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 tuned/plugins/plugin_sysfs.py diff --git a/tuned/plugins/base.py b/tuned/plugins/base.py index a4b4ace..5710663 100644 --- a/tuned/plugins/base.py +++ b/tuned/plugins/base.py @@ -174,7 +174,7 @@ class Plugin(object): def _merge_options(self, options): for key in options: - if key in self._options or self.__class__.__name__ == "SysctlPlugin": + if key in self._options or self.__class__.__name__ in ["SysctlPlugin", "SysfsPlugin"]: self._options[key] = options[key] else: log.warn("Unknown option '%s' for plugin '%s'." % (key, self.__class__.__name__)) diff --git a/tuned/plugins/plugin_sysfs.py b/tuned/plugins/plugin_sysfs.py new file mode 100644 index 0000000..0d70029 --- /dev/null +++ b/tuned/plugins/plugin_sysfs.py @@ -0,0 +1,55 @@ +import base +import os.path +from decorators import * +import tuned.logs +from subprocess import * +import tuned.utils.commands + +log = tuned.logs.get() + +class SysfsPlugin(base.Plugin): + """ + Plugin for applying custom sysfs options, using specific plugins is preferred. + """ + +# TODO: resolve possible conflicts with sysctl settings from other plugins + def _post_init(self): + self._dynamic_tuning = False + self._sysfs_original = {} + self._sysfs = self._options + + for key, value in self._sysfs.iteritems(): + self._sysfs_original[key] = self._read_sysfs(key) + + @classmethod + def tunable_devices(self): + return ["sysfs"] + + @classmethod + def _get_default_options(cls): + return {} + + def _read_sysfs(self, sysfs_file): + data = tuned.utils.commands.read_file(sysfs_file) + value = None + if len(data): + value = tuned.utils.commands.get_active_option(data, False) + return value + + def _write_sysfs(self, sysfs_file, value): + return tuned.utils.commands.write_to_file(sysfs_file, value) + + def _apply_sysfs(self): + for key, value in self._sysfs.iteritems(): + self._write_sysfs(key, value) + return True + + def _revert_sysfs(self): + for key, value in self._sysfs_original.iteritems(): + self._write_sysfs(key, value) + + def cleanup_commands(self): + self._revert_sysfs() + + def execute_commands(self): + self._apply_sysfs() diff --git a/tuned/utils/commands.py b/tuned/utils/commands.py index a9b73f0..436680e 100644 --- a/tuned/utils/commands.py +++ b/tuned/utils/commands.py @@ -49,11 +49,13 @@ def execute(args): # Helper for parsing kernel options like: # [always] never # It will return 'always' -def get_active_option(options): +def get_active_option(options, dosplit = True): m = re.match(r'.*\[([^\]]+)\].*', options) if m: return m.group(1) - return options.split()[0] + if dosplit: + return options.split()[0] + return options def recommend_profile(): profile = consts.DEFAULT_PROFILE