1
0
Fork 0

Daemon: handle plugin selection logic

This commit is contained in:
Jan Vcelak 2012-10-05 18:56:20 +02:00
parent 44e56043c1
commit c66d5e44cb
4 changed files with 47 additions and 63 deletions

View file

@ -26,14 +26,14 @@ import os
import sys
def usage():
print "Usage: tuned [-d|--daemon] [-c conffile|--config=conffile] [-D|--debug]"
print "Usage: tuned [-d|--daemon] [-p name|--profile=name] [-D|--debug]"
def error(message):
print >>sys.stderr, message
if __name__ == "__main__":
try:
opts, args = getopt.getopt(sys.argv[1:], "dc:D", ["daemon", "config=", "debug", "no-dbus"])
opts, args = getopt.getopt(sys.argv[1:], "dp:D", ["daemon", "profile=", "debug", "no-dbus"])
except getopt.error as e:
error("Error parsing command-line arguments: %s" % e)
usage()
@ -44,7 +44,7 @@ if __name__ == "__main__":
usage()
sys.exit(1)
config_file = []
profile = None
daemonize = False
debug = False
dbus = True
@ -52,8 +52,8 @@ if __name__ == "__main__":
for (opt, val) in opts:
if opt in ["-d", "--daemon"]:
daemonize = True
elif opt in ["-c", "--config"]:
config_file.append(val)
elif opt in ["-p", "--profile"]:
profile = val
elif opt in ["-D", "--debug"]:
debug = True
elif opt == "--no-dbus":
@ -70,7 +70,7 @@ if __name__ == "__main__":
else:
log.warn("Superuser permissions are needed. Most tunings will not work!")
app = tuned.Application(config_file, dbus)
app = tuned.Application(profile, dbus)
if daemonize:
log.switch_to_file()

View file

@ -23,6 +23,7 @@ import exports
import exports.dbus
import monitors
import plugins
import profiles
import signal
import storage
import units
@ -33,7 +34,7 @@ DBUS_INTERFACE = "com.redhat.tuned.control"
DBUS_OBJECT = "/Tuned"
class Application(object):
def __init__(self, config_file, enable_dbus = True):
def __init__(self, profile_name, enable_dbus = True):
self._storage_provider = storage.PickleProvider()
self._storage_factory = storage.Factory(self._storage_provider)
@ -41,8 +42,10 @@ class Application(object):
self._monitors_repository = monitors.Repository()
self._unit_manager = units.Manager(self._plugins_repository, self._monitors_repository)
self._daemon = daemon.Daemon(self._unit_manager)
self._controller = controller.Controller(self._daemon, config_file)
self._profile_loader = profiles.Loader()
self._daemon = daemon.Daemon(self._unit_manager, self._profile_loader, profile_name)
self._controller = controller.Controller(self._daemon)
self._dbus_exporter = None
if enable_dbus:
@ -56,7 +59,7 @@ class Application(object):
exports.register_object(self._controller)
def _init_signals(self):
utils.handle_signal(signal.SIGHUP, self._controller.switch_to_default_profile)
utils.handle_signal(signal.SIGHUP, self._controller.reload)
utils.handle_signal([signal.SIGINT, signal.SIGTERM], self._controller.terminate)
@property

View file

@ -19,7 +19,6 @@ __all__ = ["Controller"]
import exports
import logs
import profile
import threading
log = logs.get()
@ -30,16 +29,10 @@ class Controller(exports.interfaces.ExportableInterface):
and export the controller interface (currently only over D-Bus).
"""
def __init__(self, daemon, config_file):
def __init__(self, daemon):
super(self.__class__, self).__init__()
self._daemon = daemon
self._terminate = threading.Event()
if config_file:
if not isinstance(config_file, list):
config_file = [config_file]
self.config_file = config_file
else:
self.config_file = self.get_default_profile()
def run(self):
"""
@ -59,25 +52,6 @@ class Controller(exports.interfaces.ExportableInterface):
def terminate(self):
self._terminate.set()
def get_default_profile(self):
try:
with open("/etc/tuned/active_profile", "r") as f:
profiles = f.read().split("\n")
for i in range(len(profiles)):
profiles[i] = profile.Profile.find_profile(profiles[i])
return profiles
except (OSError,IOError,EOFError) as e:
log.error("Cannot read active profile from /etc/tuned/active_profile: %s" % (e))
return []
def switch_to_default_profile(self):
profile = self.get_default_profile()
log.info("Switching to default profile: %s" % profile)
if len(profile) != 0:
return self.switch_profile(profile)
return False
@property
def config_file(self):
return self._config_file
@ -110,8 +84,8 @@ class Controller(exports.interfaces.ExportableInterface):
else:
return self.stop() and self.start()
@exports.export("as", "b")
def switch_profile(self, profiles):
@exports.export("s", "b")
def switch_profile(self, profile):
for i in range(len(profiles)):
profiles[i] = profile.Profile.find_profile(profiles[i])
try:
@ -122,16 +96,9 @@ class Controller(exports.interfaces.ExportableInterface):
return self.reload()
@exports.export("", "as")
@exports.export("", "s")
def active_profile(self):
profiles = []
for cfg in self.config_file:
try:
profiles.append(cfg.split('/')[-2])
except IndexError:
profiles.append(cfg)
return profiles
return self._profile
@exports.export("", "b")
def status(self):

View file

@ -29,24 +29,35 @@ import threading
import logs
log = logs.get()
DEFAULT_CONFIG_FILE = ["/etc/tuned/tuned.conf"]
ACTIVE_PROFILE_FILENAME = "/etc/tuned/saved_profile"
DEFAULT_PROFILE_NAME = "balanced"
class Daemon(object):
def __init__(self, unit_manager, profile_loader):
def __init__(self, unit_manager, profile_loader, profile_name=None):
log.debug("initializing daemon")
self._unit_manager = unit_manager
self._profile_loader = profile_loader
self._config_file = DEFAULT_CONFIG_FILE
self._profile = None
self._thread = None
self._terminate = threading.Event()
if profile_name is None:
profile_name = self.get_active_profile()
self.set_profile(profile_name)
def set_profile(self, profile_name):
if self.is_running():
raise Exception("Cannot set profile while the daemon is running.")
self._profile = self._profile_loader.load(profile_name)
def _thread_code(self):
self._profile = self._profile_loader.load("default")
# self._profile = profile.Profile(self._unit_manager, self._config_file)
if self._profile is None:
raise Exception("Cannot start the daemon without setting a profile.")
for unit_info in self._profile.units:
self._unit_manager.create(unit_info.name, unit_info.plugin, unit_info.options)
self.save_active_profile()
self._unit_manager.plugins_repository.do_static_tuning()
self._terminate.clear()
@ -60,11 +71,18 @@ class Daemon(object):
def save_active_profile(self):
try:
with open("/etc/tuned/active_profile", "w") as f:
data = "\n".join(self._config_file)
f.write(data)
with open(ACTIVE_PROFILE_FILENAME, "w") as f:
f.write(self._profile.name)
except (OSError,IOError) as e:
log.error("Cannot write active profile into /etc/tuned/active_profile: %s" % (e))
log.error("Cannot write active profile into %s: %s" % (ACTIVE_PROFILE_FILENAME, e))
def get_active_profile(self):
try:
with open(ACTIVE_PROFILE_FILENAME, "r") as f:
return f.read().strip()
except (OSError, IOError, EOFError) as e:
log.error("Cannot read active profile, setting default.")
return DEFAULT_PROFILE_NAME
@property
def config_file(self):
@ -104,10 +122,6 @@ class Daemon(object):
self._thread.join()
self._thread = None
if self._profile:
self._profile.cleanup()
self._profile = None
return True
def cleanup(self):