Current File : /home/mmdealscpanel/yummmdeals.com/units.tar
__init__.py000064400000000027150516644620006665 0ustar00from .manager import *
manager.py000064400000013654150516644620006552 0ustar00import collections
import os
import re
import traceback
import tuned.exceptions
import tuned.logs
import tuned.plugins.exceptions
import tuned.consts as consts
from tuned.utils.global_config import GlobalConfig
from tuned.utils.commands import commands

log = tuned.logs.get()

__all__ = ["Manager"]

class Manager(object):
	"""
	Manager creates plugin instances and keeps a track of them.
	"""

	def __init__(self, plugins_repository, monitors_repository,
			def_instance_priority, hardware_inventory, config = None):
		super(Manager, self).__init__()
		self._plugins_repository = plugins_repository
		self._monitors_repository = monitors_repository
		self._def_instance_priority = def_instance_priority
		self._hardware_inventory = hardware_inventory
		self._instances = []
		self._plugins = []
		self._config = config or GlobalConfig()
		self._cmd = commands()

	@property
	def plugins(self):
		return self._plugins

	@property
	def instances(self):
		return self._instances

	@property
	def plugins_repository(self):
		return self._plugins_repository

	def _unit_matches_cpuinfo(self, unit):
		if unit.cpuinfo_regex is None:
			return True
		cpuinfo_string = self._config.get(consts.CFG_CPUINFO_STRING)
		if cpuinfo_string is None:
			cpuinfo_string = self._cmd.read_file("/proc/cpuinfo")
		return re.search(unit.cpuinfo_regex, cpuinfo_string,
				re.MULTILINE) is not None

	def _unit_matches_uname(self, unit):
		if unit.uname_regex is None:
			return True
		uname_string = self._config.get(consts.CFG_UNAME_STRING)
		if uname_string is None:
			uname_string = " ".join(os.uname())
		return re.search(unit.uname_regex, uname_string,
				re.MULTILINE) is not None

	def create(self, instances_config):
		instance_info_list = []
		for instance_name, instance_info in list(instances_config.items()):
			if not instance_info.enabled:
				log.debug("skipping disabled instance '%s'" % instance_name)
				continue
			if not self._unit_matches_cpuinfo(instance_info):
				log.debug("skipping instance '%s', cpuinfo does not match" % instance_name)
				continue
			if not self._unit_matches_uname(instance_info):
				log.debug("skipping instance '%s', uname does not match" % instance_name)
				continue

			instance_info.options.setdefault("priority", self._def_instance_priority)
			instance_info.options["priority"] = int(instance_info.options["priority"])
			instance_info_list.append(instance_info)

		instance_info_list.sort(key=lambda x: x.options["priority"])
		plugins_by_name = collections.OrderedDict()
		for instance_info in instance_info_list:
			instance_info.options.pop("priority")
			plugins_by_name[instance_info.type] = None

		for plugin_name, none in list(plugins_by_name.items()):
			try:
				plugin = self._plugins_repository.create(plugin_name)
				plugins_by_name[plugin_name] = plugin
				self._plugins.append(plugin)
			except tuned.plugins.exceptions.NotSupportedPluginException:
				log.info("skipping plugin '%s', not supported on your system" % plugin_name)
				continue
			except Exception as e:
				log.error("failed to initialize plugin %s" % plugin_name)
				log.exception(e)
				continue

		instances = []
		for instance_info in instance_info_list:
			plugin = plugins_by_name[instance_info.type]
			if plugin is None:
				continue
			log.debug("creating '%s' (%s)" % (instance_info.name, instance_info.type))
			new_instance = plugin.create_instance(instance_info.name, instance_info.devices, instance_info.devices_udev_regex, \
				instance_info.script_pre, instance_info.script_post, instance_info.options)
			instances.append(new_instance)
		for instance in instances:
			instance.plugin.init_devices()
			instance.plugin.assign_free_devices(instance)
			instance.plugin.initialize_instance(instance)
		# At this point we should be able to start the HW events
		# monitoring/processing thread, without risking race conditions
		self._hardware_inventory.start_processing_events()
		self._instances.extend(instances)

	def _try_call(self, caller, exc_ret, f, *args, **kwargs):
		try:
			return f(*args, **kwargs)
		except Exception as e:
			trace = traceback.format_exc()
			log.error("BUG: Unhandled exception in %s: %s"
					% (caller, str(e)))
			log.error(trace)
			return exc_ret

	def destroy_all(self):
		for instance in self._instances:
			log.debug("destroying instance %s" % instance.name)
			self._try_call("destroy_all", None,
					instance.plugin.destroy_instance,
					instance)
		for plugin in self._plugins:
			log.debug("cleaning plugin '%s'" % plugin.name)
			self._try_call("destroy_all", None, plugin.cleanup)

		del self._plugins[:]
		del self._instances[:]

	def update_monitors(self):
		for monitor in self._monitors_repository.monitors:
			log.debug("updating monitor %s" % monitor)
			self._try_call("update_monitors", None, monitor.update)

	def start_tuning(self):
		for instance in self._instances:
			self._try_call("start_tuning", None,
					instance.apply_tuning)

	def verify_tuning(self, ignore_missing):
		ret = True
		for instance in self._instances:
			res = self._try_call("verify_tuning", False,
					instance.verify_tuning, ignore_missing)
			if res == False:
				ret = False
		return ret

	def update_tuning(self):
		for instance in self._instances:
			self._try_call("update_tuning", None,
					instance.update_tuning)

	# rollback parameter is a helper telling plugins whether soft or full
	# rollback is needed, e.g. for bootloader plugin we need grub.cfg
	# tuning to persist across reboots and restarts of the daemon, so in
	# this case the rollback is usually set to consts.ROLLBACK_SOFT,
	# but we also need to clean it all up when TuneD is disabled or the
	# profile is changed. In this case the rollback is set to
	# consts.ROLLBACK_FULL. In practice it means to remove all temporal
	# or helper files, unpatch third party config files, etc.
	def stop_tuning(self, rollback = consts.ROLLBACK_SOFT):
		self._hardware_inventory.stop_processing_events()
		for instance in reversed(self._instances):
			self._try_call("stop_tuning", None,
					instance.unapply_tuning, rollback)
__pycache__/manager.cpython-36.opt-1.pyc000064400000013166150516644620013773 0ustar003

�<�e��@s~ddlZddlZddlZddlZddlZddlZddlZddlj	Z	ddl
mZddlm
Z
ejj�ZdgZGdd�de�ZdS)�N)�GlobalConfig)�commands�Managercs�eZdZdZd�fdd�	Zedd��Zedd��Zed	d
��Zdd�Z	d
d�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zejfdd�Z�ZS) rz?
	Manager creates plugin instances and keeps a track of them.
	NcsJtt|�j�||_||_||_||_g|_g|_|p:t	�|_
t�|_dS)N)
�superr�__init__�_plugins_repository�_monitors_repository�_def_instance_priority�_hardware_inventory�
_instances�_pluginsr�_configr�_cmd)�self�plugins_repositoryZmonitors_repositoryZdef_instance_priorityZhardware_inventory�config)�	__class__��/usr/lib/python3.6/manager.pyrszManager.__init__cCs|jS)N)r)rrrr�plugins!szManager.pluginscCs|jS)N)r)rrrr�	instances%szManager.instancescCs|jS)N)r)rrrrr)szManager.plugins_repositorycCsF|jdkrdS|jjtj�}|dkr0|jjd�}tj|j|tj	�dk	S)NTz
/proc/cpuinfo)
Z
cpuinfo_regexr
�get�constsZCFG_CPUINFO_STRINGrZ	read_file�re�search�	MULTILINE)r�unitZcpuinfo_stringrrr�_unit_matches_cpuinfo-s

zManager._unit_matches_cpuinfocCsH|jdkrdS|jjtj�}|dkr2djtj��}tj	|j|tj
�dk	S)NT� )Zuname_regexr
rrZCFG_UNAME_STRING�join�os�unamerrr)rrZuname_stringrrr�_unit_matches_uname6s

zManager._unit_matches_unamec
Cs2g}x�t|j��D]�\}}|js0tjd|�q|j|�sJtjd|�q|j|�sdtjd|�q|jjd|j	�t
|jd�|jd<|j|�qW|jdd�d�t
j�}x"|D]}|jjd�d||j<q�Wx�t|j��D]�\}}y$|jj|�}|||<|jj|�Wq�tjjjk
�r8tjd|�w�Yq�tk
�rt}	z tjd	|�tj|	�w�WYdd}	~	Xq�Xq�Wg}
xf|D]^}||j}|dk�r��q�tjd
|j|jf�|j|j|j|j|j |j!|j�}|
j|��q�Wx0|
D](}|j"j#�|j"j$|�|j"j%|��q�W|j&j'�|j(j)|
�dS)Nzskipping disabled instance '%s'z.skipping instance '%s', cpuinfo does not matchz,skipping instance '%s', uname does not match�prioritycSs
|jdS)Nr#)�options)�xrrr�<lambda>Psz Manager.create.<locals>.<lambda>)�keyz2skipping plugin '%s', not supported on your systemzfailed to initialize plugin %szcreating '%s' (%s))*�list�itemsZenabled�log�debugrr"r$�
setdefaultr	�int�append�sort�collections�OrderedDict�pop�typer�creater�tunedr�
exceptionsZNotSupportedPluginException�info�	Exception�errorZ	exception�nameZcreate_instanceZdevicesZdevices_udev_regexZ
script_preZscript_post�pluginZinit_devicesZassign_free_devicesZinitialize_instancer
Zstart_processing_eventsr�extend)
rZinstances_configZinstance_info_listZ
instance_nameZ
instance_infoZplugins_by_nameZplugin_nameZnoner;�erZnew_instance�instancerrrr4?s\









zManager.createcOsXy
|||�Stk
rR}z,tj�}tjd|t|�f�tj|�|Sd}~XnXdS)Nz"BUG: Unhandled exception in %s: %s)r8�	traceback�
format_excr*r9�str)rZcallerZexc_ret�f�args�kwargsr=Ztracerrr�	_try_callus

zManager._try_callcCs�x2|jD](}tjd|j�|jdd|jj|�qWx.|jD]$}tjd|j�|jdd|j�q<W|jdd�=|jdd�=dS)Nzdestroying instance %s�destroy_allzcleaning plugin '%s')	rr*r+r:rEr;Zdestroy_instancerZcleanup)rr>r;rrrrFs
zManager.destroy_allcCs4x.|jjD]"}tjd|�|jdd|j�q
WdS)Nzupdating monitor %s�update_monitors)rZmonitorsr*r+rE�update)rZmonitorrrrrG�szManager.update_monitorscCs$x|jD]}|jdd|j�qWdS)N�start_tuning)rrEZapply_tuning)rr>rrrrI�szManager.start_tuningcCs6d}x,|jD]"}|jdd|j|�}|dkrd}qW|S)NT�
verify_tuningF)rrErJ)rZignore_missing�retr>�resrrrrJ�s
zManager.verify_tuningcCs$x|jD]}|jdd|j�qWdS)N�
update_tuning)rrErM)rr>rrrrM�szManager.update_tuningcCs4|jj�x$t|j�D]}|jdd|j|�qWdS)N�stop_tuning)r
Zstop_processing_events�reversedrrEZunapply_tuning)rZrollbackr>rrrrN�s
zManager.stop_tuning)N)�__name__�
__module__�__qualname__�__doc__r�propertyrrrrr"r4rErFrGrIrJrMrZ
ROLLBACK_SOFTrN�
__classcell__rr)rrrs		6

	
)r0r rr?Ztuned.exceptionsr5Z
tuned.logsZtuned.plugins.exceptionsZtuned.constsrZtuned.utils.global_configrZtuned.utils.commandsrZlogsrr*�__all__�objectrrrrr�<module>s

__pycache__/__init__.cpython-36.pyc000064400000000211150516644620013144 0ustar003

�<�e�@sddlTdS)�)�*N)Zmanager�rr�/usr/lib/python3.6/__init__.py�<module>s__pycache__/__init__.cpython-36.opt-1.pyc000064400000000211150516644620014103 0ustar003

�<�e�@sddlTdS)�)�*N)Zmanager�rr�/usr/lib/python3.6/__init__.py�<module>s__pycache__/manager.cpython-36.pyc000064400000013166150516644620013034 0ustar003

�<�e��@s~ddlZddlZddlZddlZddlZddlZddlZddlj	Z	ddl
mZddlm
Z
ejj�ZdgZGdd�de�ZdS)�N)�GlobalConfig)�commands�Managercs�eZdZdZd�fdd�	Zedd��Zedd��Zed	d
��Zdd�Z	d
d�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zejfdd�Z�ZS) rz?
	Manager creates plugin instances and keeps a track of them.
	NcsJtt|�j�||_||_||_||_g|_g|_|p:t	�|_
t�|_dS)N)
�superr�__init__�_plugins_repository�_monitors_repository�_def_instance_priority�_hardware_inventory�
_instances�_pluginsr�_configr�_cmd)�self�plugins_repositoryZmonitors_repositoryZdef_instance_priorityZhardware_inventory�config)�	__class__��/usr/lib/python3.6/manager.pyrszManager.__init__cCs|jS)N)r)rrrr�plugins!szManager.pluginscCs|jS)N)r)rrrr�	instances%szManager.instancescCs|jS)N)r)rrrrr)szManager.plugins_repositorycCsF|jdkrdS|jjtj�}|dkr0|jjd�}tj|j|tj	�dk	S)NTz
/proc/cpuinfo)
Z
cpuinfo_regexr
�get�constsZCFG_CPUINFO_STRINGrZ	read_file�re�search�	MULTILINE)r�unitZcpuinfo_stringrrr�_unit_matches_cpuinfo-s

zManager._unit_matches_cpuinfocCsH|jdkrdS|jjtj�}|dkr2djtj��}tj	|j|tj
�dk	S)NT� )Zuname_regexr
rrZCFG_UNAME_STRING�join�os�unamerrr)rrZuname_stringrrr�_unit_matches_uname6s

zManager._unit_matches_unamec
Cs2g}x�t|j��D]�\}}|js0tjd|�q|j|�sJtjd|�q|j|�sdtjd|�q|jjd|j	�t
|jd�|jd<|j|�qW|jdd�d�t
j�}x"|D]}|jjd�d||j<q�Wx�t|j��D]�\}}y$|jj|�}|||<|jj|�Wq�tjjjk
�r8tjd|�w�Yq�tk
�rt}	z tjd	|�tj|	�w�WYdd}	~	Xq�Xq�Wg}
xf|D]^}||j}|dk�r��q�tjd
|j|jf�|j|j|j|j|j |j!|j�}|
j|��q�Wx0|
D](}|j"j#�|j"j$|�|j"j%|��q�W|j&j'�|j(j)|
�dS)Nzskipping disabled instance '%s'z.skipping instance '%s', cpuinfo does not matchz,skipping instance '%s', uname does not match�prioritycSs
|jdS)Nr#)�options)�xrrr�<lambda>Psz Manager.create.<locals>.<lambda>)�keyz2skipping plugin '%s', not supported on your systemzfailed to initialize plugin %szcreating '%s' (%s))*�list�itemsZenabled�log�debugrr"r$�
setdefaultr	�int�append�sort�collections�OrderedDict�pop�typer�creater�tunedr�
exceptionsZNotSupportedPluginException�info�	Exception�errorZ	exception�nameZcreate_instanceZdevicesZdevices_udev_regexZ
script_preZscript_post�pluginZinit_devicesZassign_free_devicesZinitialize_instancer
Zstart_processing_eventsr�extend)
rZinstances_configZinstance_info_listZ
instance_nameZ
instance_infoZplugins_by_nameZplugin_nameZnoner;�erZnew_instance�instancerrrr4?s\









zManager.createcOsXy
|||�Stk
rR}z,tj�}tjd|t|�f�tj|�|Sd}~XnXdS)Nz"BUG: Unhandled exception in %s: %s)r8�	traceback�
format_excr*r9�str)rZcallerZexc_ret�f�args�kwargsr=Ztracerrr�	_try_callus

zManager._try_callcCs�x2|jD](}tjd|j�|jdd|jj|�qWx.|jD]$}tjd|j�|jdd|j�q<W|jdd�=|jdd�=dS)Nzdestroying instance %s�destroy_allzcleaning plugin '%s')	rr*r+r:rEr;Zdestroy_instancerZcleanup)rr>r;rrrrFs
zManager.destroy_allcCs4x.|jjD]"}tjd|�|jdd|j�q
WdS)Nzupdating monitor %s�update_monitors)rZmonitorsr*r+rE�update)rZmonitorrrrrG�szManager.update_monitorscCs$x|jD]}|jdd|j�qWdS)N�start_tuning)rrEZapply_tuning)rr>rrrrI�szManager.start_tuningcCs6d}x,|jD]"}|jdd|j|�}|dkrd}qW|S)NT�
verify_tuningF)rrErJ)rZignore_missing�retr>�resrrrrJ�s
zManager.verify_tuningcCs$x|jD]}|jdd|j�qWdS)N�
update_tuning)rrErM)rr>rrrrM�szManager.update_tuningcCs4|jj�x$t|j�D]}|jdd|j|�qWdS)N�stop_tuning)r
Zstop_processing_events�reversedrrEZunapply_tuning)rZrollbackr>rrrrN�s
zManager.stop_tuning)N)�__name__�
__module__�__qualname__�__doc__r�propertyrrrrr"r4rErFrGrIrJrMrZ
ROLLBACK_SOFTrN�
__classcell__rr)rrrs		6

	
)r0r rr?Ztuned.exceptionsr5Z
tuned.logsZtuned.plugins.exceptionsZtuned.constsrZtuned.utils.global_configrZtuned.utils.commandsrZlogsrr*�__all__�objectrrrrr�<module>s