HEX
Server: Apache
System: Linux insrv.serversdaddy.com 5.14.0-362.24.2.el9_3.x86_64 #1 SMP PREEMPT_DYNAMIC Sat Mar 30 14:11:54 EDT 2024 x86_64
User: jsaha (1066)
PHP: 8.3.6
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //lib/python3.9/site-packages/tuned/plugins/__pycache__/base.cpython-39.pyc
a

Y��dW�@snddlZddlmZddlZddlZddlZddlmZddl	Z	ddl
mZmZej
��ZGdd�de�ZdS)�N)�commands)�Popen�PIPEc@s"eZdZdZdd�Zdd�Zdd�Zedd	��Ze	d
d��Z
e	dd
��Ze	dd��Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zejfd2d3�Z d4d5�Z!d6d7�Z"d8d9�Z#ejfd:d;�Z$d<d=�Z%d>d?�Z&ejfd@dA�Z'dBdC�Z(dDdE�Z)dFdG�Z*dHdI�Z+dJdK�Z,dLdM�Z-d|dOdP�Z.d}dQdR�Z/d~dSdT�Z0ddUdV�Z1dWdX�Z2dYdZ�Z3d[d\�Z4d]d^�Z5d_d`�Z6d�dbdc�Z7d�ddde�Z8dfdg�Z9dhdi�Z:djdk�Z;d�dldm�Z<d�dndo�Z=dpdq�Z>drds�Z?dtdu�Z@dvdw�ZAdxdy�ZBdzd{�ZCdNS)��Plugina
	Base class for all plugins.

	Plugins change various system settings in order to get desired performance or power
	saving. Plugins use Monitor objects to get information from the running system.

	Intentionally a lot of logic is included in the plugin to increase plugin flexibility.
	c		Csn|�|jj�|_||_||_||_||_||_t	�
�|_|��||_
||_d|_d|_|��|_t�|_dS)zPlugin constructor.FN)�create�	__class__�__name__�_storageZ_monitors_repositoryZ_hardware_inventory�_device_matcher�_device_matcher_udev�_instance_factory�collections�OrderedDict�
_instances�_init_commands�_global_cfg�
_variables�_has_dynamic_options�_devices_inited�#_get_config_options_used_by_dynamic�_options_used_by_dynamicr�_cmd)	�selfZmonitors_repositoryZstorage_factoryZhardware_inventoryZdevice_matcherZdevice_matcher_udevZinstance_factoryZ
global_cfgZ	variables�r�6/usr/lib/python3.9/site-packages/tuned/plugins/base.py�__init__s

zPlugin.__init__cCs|��dS�N)�destroy_instances�rrrr�cleanup,szPlugin.cleanupcCs|js|��d|_dS)NT)r�
_init_devicesrrrr�init_devices/szPlugin.init_devicescCs|jj�d�d�dd�dS)N�.����_�)r�
__module__�splitrrrr�name4szPlugin.namecCsiS)z-Default configuration options for the plugin.rrrrr�_get_config_options<szPlugin._get_config_optionscCsiS)z*Explanation of each config option functionr)�clsrrr�get_config_options_hintsAszPlugin.get_config_options_hintscCsgS)znList of config options used by dynamic tuning. Their previous values will be automatically saved and restored.rrrrrrFsz*Plugin._get_config_options_used_by_dynamiccCsL|����}|D]6}||vs"|jr0||||<qt�d||jjf�q|S)z3Merge provided options with plugin default options.z$Unknown option '%s' for plugin '%s'.)r)�copyr�log�warnrr)r�optionsZ	effective�keyrrr�_get_effective_optionsKszPlugin._get_effective_optionscCs,t|�tur|St|���}|dkp*|dkS)N�true�1)�type�bool�str�lower)r�valuerrr�_option_boolVszPlugin._option_boolc		CsF||jvrtd|��|�|�}|j�|||||||�}||j|<|S)z8Create new instance of the plugin and seize the devices.z.Plugin instance with name '%s' already exists.)r�	Exceptionr1rr)	rr(�devices_expression�devices_udev_regex�
script_pre�script_postr/Zeffective_options�instancerrr�create_instance`s

�
zPlugin.create_instancecCsV|j|krtd||f��|j|jvr2td|��|j|j}|�|�|j|j=dS)zDestroy existing instance.z9Plugin instance '%s' does not belong to this plugin '%s'.z+Plugin instance '%s' was already destroyed.N)Z_pluginr:r(r�_destroy_instance�rr?rrr�destroy_instancels

zPlugin.destroy_instancecCs$t�d|j|jf�|�|�dS)zInitialize an instance.zinitializing instance %s (%s)N)r-�debugr(�_instance_initrBrrr�initialize_instancewszPlugin.initialize_instancecCsBt|j���D]$}t�d|j|jf�|�|�q|j��dS)zDestroy all instances.zdestroying instance %s (%s)N)�listr�valuesr-rDr(rA�clearrBrrrr|szPlugin.destroy_instancescCs|�|�|�|�dSr)�release_devices�_instance_cleanuprBrrrrA�s
zPlugin._destroy_instancecCs
t��dSr��NotImplementedErrorrBrrrrE�szPlugin._instance_initcCs
t��dSrrLrBrrrrK�szPlugin._instance_cleanupcCsd|_t�|_t�|_dS�NF)�_devices_supported�set�_assigned_devices�
_free_devicesrrrrr �szPlugin._init_devicescCsdS)z�Override this in a subclass to transform a list of device names (e.g. ['sda'])
		   to a list of pyudev.Device objects, if your plugin supports itNr)r�devicesrrr�_get_device_objects�szPlugin._get_device_objectscCsj|jdurt|j�|j|��S|�|�}|durDt�d|j�t�S|j	�|j|�}tdd�|D��SdS)Nz<Plugin '%s' does not support the 'devices_udev_regex' optioncSsg|]
}|j�qSr)Zsys_name)�.0�xrrr�
<listcomp>��z0Plugin._get_matching_devices.<locals>.<listcomp>)
r<rPr
Z
match_listr;rTr-�errorr(r)rr?rSZudev_devicesrrr�_get_matching_devices�s

zPlugin._get_matching_devicescCs�|js
dSt�d|j�|�||j�}t|�dk|_|jsNt�d|j�n`|j}|j|jkrn|d|j7}t�	d|d�
|�f�|j�|�|j
|O_
|j|8_dS)Nz assigning devices to instance %srz*instance %s: no matching devices availablez (%s)z!instance %s: assigning devices %s�, )rOr-rDr(rZrR�len�activer.�info�join�assigned_devices�updaterQ)rr?Z	to_assignr(rrr�assign_free_devices�szPlugin.assign_free_devicescCsV|js
dS|j|jB|j@}d|_|j��|j��|j|8_|j|O_dSrN)rO�processed_devicesr`rQr]rIrR)rr?Z
to_releaserrrrJ�s��

zPlugin.release_devicescCs$|jsdg}|D]}|||�qdSr)rO)rr?�callbackrS�devicerrr�_run_for_each_device�szPlugin._run_for_each_devicecCsdSrr�rr?Zenablingrrr�_instance_pre_static�szPlugin._instance_pre_staticcCsdSrrrgrrr�_instance_post_static�szPlugin._instance_post_staticcCsj|durdSt|�dkr0t�d|j|f�dS|�d�sHt�d�dStj�|�}d}|D�]}tj	}	|	�
|j���|g}
|t
jkr�|
�d�|
�|�t�d|t|
�f�t�d	tt|	�����zVt|g|
ttd|	|dd
�}|��\}}
|j�r t�d||j|
dd�f�d}Wq\ttf�yb}z"t�d
||f�d}WYd}~q\d}~00q\|S)Nrz1Instance '%s': no device to call script '%s' for.�/zQRelative paths cannot be used in script_pre or script_post. Use ${i:PROFILE_DIR}.FTZ
full_rollbackz'calling script '%s' with arguments '%s'zusing environment '%s')�stdout�stderrZ	close_fds�env�cwdZuniversal_newlineszscript '%s' error: %d, '%s'r#zscript '%s' error: %s)r\r-r.r(�
startswithrY�os�path�dirname�environrarZget_env�constsZ
ROLLBACK_FULL�appendr^r6rDrG�itemsrrZcommunicate�
returncode�OSError�IOError)rr?Zscript�oprS�rollbackZdir_name�ret�devrsZ	arguments�proc�out�err�errr�_call_device_script�sB






�zPlugin._call_device_scriptcCs�|js
dS|jrZ|�||jd|j�|�|d�|�|�|�|d�|�||jd|j�|j	r�|j
�tj
tj�r�|�||j|j�|j�|j�|j��dS)zG
		Apply static and dynamic tuning if the plugin instance is active.
		NZapplyT)r]�has_static_tuningr�r=r`rh�_instance_apply_staticrir>�has_dynamic_tuningr�getrt�CFG_DYNAMIC_TUNING�CFG_DEF_DYNAMIC_TUNINGrf�_instance_apply_dynamicrcrarIrBrrr�instance_apply_tuning�s 
�

�zPlugin.instance_apply_tuningcCs�|js
dSt|j�dkr.t�dd�|j��|j��}|jr�|�	||j
d|�dkrXdS|�|||�dkrndS|�	||jd|�dkr�dSdSdSdS)z<
		Verify static tuning if the plugin instance is active.
		Nrz)BUG: Some devices have not been tuned: %sr[ZverifyFT)
r]r\r`r-rYr_rcr,r�r�r=�_instance_verify_staticr>)rr?�ignore_missingrSrrr�instance_verify_tunings 
�
zPlugin.instance_verify_tuningcCs<|js
dS|jr8|j�tjtj�r8|�||j|j	�
��dS)z<
		Apply dynamic tuning if the plugin instance is active.
		N)r]r�rr�rtr�r�rf�_instance_update_dynamicrcr,rBrrr�instance_update_tuning$szPlugin.instance_update_tuningcCs�|tjkrdS|jr8|j�tjtj�r8|�||j|j	�|j
r�|j||jd|j	|d�|�
|d�|�||�|�|d�|j||jd|j	|d�dS)z8
		Remove all tunings applied by the plugin instance.
		NZunapply)r{F)rtZ
ROLLBACK_NONEr�rr�r�r�rf�_instance_unapply_dynamicrcr�r�r>rh�_instance_unapply_staticrir=�rr?r{rrr�instance_unapply_tuning-s

�zPlugin.instance_unapply_tuningcCs|�|�|�||j�dSr)� _execute_all_non_device_commands�_execute_all_device_commandsr`rBrrrr�?s
zPlugin._instance_apply_staticcCs2d}|�||�dkrd}|�|||�dkr.d}|S�NTF)�_verify_all_non_device_commands�_verify_all_device_commands)rr?r�rSr|rrrr�CszPlugin._instance_verify_staticcCs|�||j�|�|�dSr)�_cleanup_all_device_commandsrc� _cleanup_all_non_device_commandsr�rrrr�Ks�zPlugin._instance_unapply_staticcsB���fdd��jD�D]}����j|��q�����dS)Ncs(g|] }����j|��dur|�qSr)�_storage_get�	_commands)rU�opt�rer?rrrrWQrXz2Plugin._instance_apply_dynamic.<locals>.<listcomp>)r�_check_and_save_valuer�r�)rr?re�optionrr�rr�PszPlugin._instance_apply_dynamiccCs
t��dSrrL�rr?rerrrr�Vsz Plugin._instance_unapply_dynamiccCs
t��dSrrLr�rrrr�YszPlugin._instance_update_dynamiccCst��|_|��|��dS)z
		Initialize commands.
		N)r
rr��_autoregister_commands�_check_commandsrrrrr`s
zPlugin._init_commandscCs�|jjD]�}|�d�rqt||�}t|d�s.q|jd}|j�|d|i�}d|jvr�d|d<||d<|jd|d<|jd|d<nBd	|jvr�||d	<n.d|jvr�||d<|jd|d<|jd|d<||j|<qt�	t
t|j���d
d�d��|_dS)
zd
		Register all commands marked using @command_set, @command_get, and @command_custom decorators.
		�__�_commandr(rPN�custom�
per_device�priorityr�cSs|ddS)Nr%r�r)Z	name_inforrr�<lambda>�rXz/Plugin._autoregister_commands.<locals>.<lambda>)r0)
r�__dict__ro�getattr�hasattrr�r�r�r
r�sorted�iterrv)r�member_name�member�command_namer^rrrr�hs*







zPlugin._autoregister_commandscCsFt|j���D]2\}}|�dd�r$qd|vs4d|vrtd|��qdS)z2
		Check if all commands are defined correctly.
		r�Fr�rPz,Plugin command '%s' is not defined correctlyN)rGr�rvr��	TypeError)rr��commandrrrr��s
zPlugin._check_commandsNcCsJt|�j}|durdn|}|dur&dn|}|dur6dn|}d||||fS)N�z%s/%s/%s/%s)r4r)rZ
instance_namer��device_name�
class_namerrr�_storage_key�s
�zPlugin._storage_keycCs&|�|j|d|�}|j�||�dS�Nr()r�r(r	rP)rr?r�r8r�r0rrr�_storage_set�szPlugin._storage_setcCs |�|j|d|�}|j�|�Sr�)r�r(r	r��rr?r�r�r0rrrr��szPlugin._storage_getcCs |�|j|d|�}|j�|�Sr�)r�r(r	Zunsetr�rrr�_storage_unset�szPlugin._storage_unsetcCsRdd�t|j���D�D]4}|j�|j�|dd��}|dur|�|||�qdS)NcSsg|]}|ds|�qS�r�r�rUr�rrrrW�rXz;Plugin._execute_all_non_device_commands.<locals>.<listcomp>r()rGr�rHr�expandr/r��_execute_non_device_command�rr?r��	new_valuerrrr��sz'Plugin._execute_all_non_device_commandscCs`dd�t|j���D�D]B}|j�|j�|dd��}|dur@q|D]}|�||||�qDqdS)NcSsg|]}|dr|�qSr�rr�rrrrW�rXz7Plugin._execute_all_device_commands.<locals>.<listcomp>r()rGr�rHrr�r/r��_execute_device_command)rr?rSr�r�rerrrr��sz#Plugin._execute_all_device_commandscCs`d}dd�t|j���D�D]>}|j�|j�|dd��}|dur|�||||�dkrd}q|S)NTcSsg|]}|ds|�qSr�rr�rrrrW�rXz:Plugin._verify_all_non_device_commands.<locals>.<listcomp>r(F)rGr�rHrr�r/r��_verify_non_device_command)rr?r�r|r�r�rrrr��sz&Plugin._verify_all_non_device_commandsc	Csfd}dd�t|j���D�D]D}|j�|dd�}|dur<q|D]}|�|||||�dkr@d}q@q|S)NTcSsg|]}|dr|�qSr�rr�rrrrW�rXz6Plugin._verify_all_device_commands.<locals>.<listcomp>r(F)rGr�rHr/r��_verify_device_command)rr?rSr�r|r�r�rerrrr��sz"Plugin._verify_all_device_commandsc
Cs�|dur�t|�}t|�dkr |S|dd�}|dd�}|durP|dvrL|S|SzN|dkrxt|�t|�krp|WSWdSn$|dkr�t|�t|�kr�|WSWdSWn(ty�t�d||||f�Yn0|S)Nr%)�<�>r�r�zhcannot compare new value '%s' with current value '%s' by operator '%s', using '%s' directly as new value)r6r\�int�
ValueErrorr-r.)rr��
current_valueZnwsrz�valrrr�_process_assignment_modifiers�s(
z$Plugin._process_assignment_modifiersFcCs&|dur|d||d�S|d�SdS)Nr��r�r)rr�rer�rrr�_get_current_value�szPlugin._get_current_valuecCs<|�||�}|�||�}|dur8|dur8|�||||�|Sr)r�r�r�)rr?r�rer�r�rrrr��s
zPlugin._check_and_save_valuecCsP|ddur"|dd||dd�n*|�||||�}|durL|d||dd�dS�Nr�TFrP�Zsim�r�)rr?r�rer�rrrr��s
zPlugin._execute_device_commandcCsL|ddur |dd|dd�n(|�||d|�}|durH|d|dd�dSr�r�r�rrrr��s
z"Plugin._execute_non_device_commandcCs.|j�t|��}t�d|�r*t�dd|�S|S)Nz\s*(0+,?)+([\da-fA-F]*,?)*\s*$z^\s*(0+,?)+r�)r�unquoter6�re�match�sub)rr8�vrrr�_norm_valueszPlugin._norm_valuec	Cs|durdSd}|durN|rN|dur6t�tj|�nt�tj||f�dS|du�r|�|�}|�|�}zt|�t|�k}Wn�t�yzt|d�t|d�k}WnVty�t|�t|�k}|s�t|��	d�}|D]}|�
�}||k}|r�q�q�Yn0Yn0|j|||||d�|S)NFT��|)re)r-r^rtZ STR_VERIFY_PROFILE_VALUE_MISSINGZ'STR_VERIFY_PROFILE_DEVICE_VALUE_MISSINGr�r�r�r6r'�strip�_log_verification_result)	rr(r�r�r�rer|Zvalsr�rrr�
_verify_value
s:



�zPlugin._verify_valuecCs�|rL|dur*t�tj|t|���f�nt�tj||t|���f�dS|dur|t�tj|t|���t|���f�n(t�tj	||t|���t|���f�dSdSr�)
r-r^rtZSTR_VERIFY_PROFILE_VALUE_OKr6r�Z"STR_VERIFY_PROFILE_DEVICE_VALUE_OKrYZSTR_VERIFY_PROFILE_VALUE_FAILZ$STR_VERIFY_PROFILE_DEVICE_VALUE_FAIL)rr(�successr�r�rerrrr�-s((zPlugin._log_verification_resultcCsn|ddur |dd||d|�S|j|||d�}|�||�}|durHdS|d||d�}|�|d||||�S)Nr�Tr�rPr(�r�r�r�)rr?r�rer�r�r�rrrr�<szPlugin._verify_device_commandcCsb|ddur|dd|d|�S|�|�}|�||�}|dur@dS|d|d�}|�|d|||�S)Nr�TrPr(r�)rr?r�r�r�r�rrrr�Fs
z!Plugin._verify_non_device_commandcCsVtdd�t|j���D��D]4}|j�|dd�dusD|d|jvr|�||�qdS)NcSsg|]}|ds|�qSr�rr�rrrrWQrXz;Plugin._cleanup_all_non_device_commands.<locals>.<listcomp>r()�reversedrGr�rHr/r�r�_cleanup_non_device_command)rr?r�rrrr�Ps $z'Plugin._cleanup_all_non_device_commandscCsbtdd�t|j���D��D]@}|j�|dd�dusD|d|jvr|D]}|�|||�qHqdS)NcSsg|]}|dr|�qSr�rr�rrrrWVrXz7Plugin._cleanup_all_device_commands.<locals>.<listcomp>r()r�rGr�rHr/r�r�_cleanup_device_command)rr?rSr�rerrrr�Us $z#Plugin._cleanup_all_device_commandscCs\|ddur"|ddd|dd�n6|�|||�}|durJ|d||dd�|�|||�dS�Nr�FrPr��r�r�)rr?r�re�	old_valuerrrr�[szPlugin._cleanup_device_commandcCsT|ddur |ddddd�n0|�||�}|durD|d|dd�|�||�dSr�r�)rr?r�r�rrrr�dsz"Plugin._cleanup_non_device_command)NNN)N)N)N)NF)NN)N)N)Drr&�__qualname__�__doc__rrr!�propertyr(�classmethodr)r+rr1r9r@rCrFrrArErKr rTrZrbrJrfrhrirtZ
ROLLBACK_SOFTr�r�r�r�r�r�r�r�r�r�r�rr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrrs�	




#	�
	


	


!�


	r)r�Ztuned.constsrtZtuned.profiles.variablesZtunedZ
tuned.logsr
Ztuned.utils.commandsrrp�
subprocessrrZlogsr�r-�objectrrrrr�<module>s