HEX
Server: LiteSpeed
System: Linux kapuas.iixcp.rumahweb.net 5.14.0-427.42.1.el9_4.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Nov 1 14:58:02 EDT 2024 x86_64
User: mirz4654 (1666)
PHP: 8.1.33
Disabled: system,exec,escapeshellarg,escapeshellcmd,passthru,proc_close,proc_get_status,proc_nice,proc_open,proc_terminate,shell_exec,popen,pclose,dl,pfsockopen,leak,apache_child_terminate,posix_kill,posix_mkfifo,posix_setsid,posix_setuid,posix_setpgid,ini_alter,show_source,define_syslog_variables,symlink,syslog,openlog,openlog,closelog,ocinumcols,listen,chgrp,apache_note,apache_setenv,debugger_on,debugger_off,ftp_exec,dll,ftp,myshellexec,socket_bind,mail,posix_getwpuid
Upload Files
File: //usr/lib/python3.9/site-packages/tuned/plugins/__pycache__/base.cpython-39.pyc
a

,�g
[�@snddlZddlmZddlZddlZddlZddlmZddl	Z	ddl
mZmZej
��ZGdd�de�ZdS)�N)�commands)�Popen�PIPEc@s6eZdZdZdd�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�Zd2d3�Zej fd4d5�Z!d6d7�Z"d8d9�Z#d:d;�Z$ej fd<d=�Z%d>d?�Z&d@dA�Z'ej fdBdC�Z(dDdE�Z)dFdG�Z*dHdI�Z+dJdK�Z,dLdM�Z-dNdO�Z.dPdQ�Z/d�dSdT�Z0d�dUdV�Z1d�dWdX�Z2d�dYdZ�Z3d[d\�Z4d]d^�Z5d_d`�Z6dadb�Z7dcdd�Z8d�dfdg�Z9d�dhdi�Z:djdk�Z;dldm�Z<dndo�Z=d�dpdq�Z>d�drds�Z?dtdu�Z@dvdw�ZAdxdy�ZBd�dzd{�ZCd�d|d}�ZDd~d�ZEdRS)��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�warningrr)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|<t�t|j��dd�d��|_|	S)z8Create new instance of the plugin and seize the devices.z.Plugin instance with name '%s' already exists.cSs
|djS)Nr%)�priority)�xrrr�<lambda>i�z(Plugin.create_instance.<locals>.<lambda>�r0)	r�	Exceptionr1rrr
r�sorted�items)
rr(r:�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�rrFrrr�destroy_instancems

zPlugin.destroy_instancecCs$t�d|j|jf�|�|�dS)zInitialize an instance.zinitializing instance %s (%s)N)r-�debugr(�_instance_initrIrrr�initialize_instancexszPlugin.initialize_instancecCsBt|j���D]$}t�d|j|jf�|�|�q|j��dS)zDestroy all instances.zdestroying instance %s (%s)N)�listr�valuesr-rKr(rH�clearrIrrrr}szPlugin.destroy_instancescCs|�|�|�|�dSr)�release_devices�_instance_cleanuprIrrrrH�s
zPlugin._destroy_instancecCs
t��dSr��NotImplementedErrorrIrrrrL�szPlugin._instance_initcCs
t��dSrrSrIrrrrR�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)�.0r;rrr�
<listcomp>�r=z0Plugin._get_matching_devices.<locals>.<listcomp>)
rCrWr
Z
match_listrBr[r-�errorr(r)rrFrZZ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�, )rVr-rKr(r_rY�len�activer.�info�join�assigned_devices�updaterX)rrFZ	to_assignr(rrr�assign_free_devices�szPlugin.assign_free_devicescCsV|js
dS|j|jB|j@}d|_|j��|j��|j|8_|j|O_dSrU)rV�processed_devicesrerXrbrPrY)rrFZ
to_releaserrrrQ�s��

zPlugin.release_devicescCs$|jsdg}|D]}|||�qdSr)rV)rrF�callbackrZ�devicerrr�_run_for_each_device�szPlugin._run_for_each_devicecCsdSrr�rrFZenablingrrr�_instance_pre_static�szPlugin._instance_pre_staticcCsdSrrrlrrr�_instance_post_static�szPlugin._instance_post_staticcCs<tj�|�}|j�tjtj�}|D]}|�|�r"dSq"dS�NTF)	�os�path�realpathrZget_list�constsZCFG_PROFILE_DIRSZCFG_DEF_PROFILE_DIRS�
startswith)rrqZ
profile_paths�prrr�_safe_script_path�s
zPlugin._safe_script_pathcCs�|durdSt|�dkr0t�d|j|f�dS|�d�sHt�d�dS|�|�sht�dd|�dStj�	|�}d}|D�]}tj
}	|	�|j�
��|g}
|tjkr�|
�d	�|
�|�t�d
|t|
�f�t�dtt|	�����zVt|g|
ttd|	|dd�}|��\}}
|j�r@t�d
||j|
dd�f�d}Wq|ttf�y�}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}.Fz?Paths outside of the profile directories cannot be used in the z0script_pre or script_post, ignoring script: '%s'TZ
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)rar-r.r(rtr^rvrprq�dirname�environrfrZget_envrsZ
ROLLBACK_FULL�appendrcr6rKrNrArrZcommunicate�
returncode�OSError�IOError)rrFZscript�oprZ�rollbackZdir_name�ret�devr}Z	arguments�proc�out�err�errr�_call_device_script�sL


�




�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)rb�has_static_tuningr�rDrerm�_instance_apply_staticrnrE�has_dynamic_tuningr�getrs�CFG_DYNAMIC_TUNING�CFG_DEF_DYNAMIC_TUNING�_instance_init_dynamicrk�_instance_apply_dynamicrhrfrPrIrrr�instance_apply_tunings"
�

�
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)
rbrarer-r^rdrhr,r�r�rD�_instance_verify_staticrE)rrF�ignore_missingrZrrr�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)rbr�rr�rsr�r�rk�_instance_update_dynamicrhr,rIrrr�instance_update_tuning2szPlugin.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)rsZ
ROLLBACK_NONEr�rr�r�r�rk�_instance_unapply_dynamicrhr�r�rErm�_instance_unapply_staticrnrD�rrFr�rrr�instance_unapply_tuning;s

�zPlugin.instance_unapply_tuningcCs|�|�|�||j�dSr)� _execute_all_non_device_commands�_execute_all_device_commandsrerIrrrr�Ms
zPlugin._instance_apply_staticcCs2d}|�||�dkrd}|�|||�dkr.d}|Sro)�_verify_all_non_device_commands�_verify_all_device_commands)rrFr�rZr�rrrr�QszPlugin._instance_verify_staticcCs|�||j�|�|�dSr)�_cleanup_all_device_commandsrh� _cleanup_all_non_device_commandsr�rrrr�Ys�zPlugin._instance_unapply_staticcCsdSrrrIrrrr�^szPlugin._instance_init_dynamiccsB���fdd��jD�D]}����j|��q�����dS)Ncs(g|] }����j|��dur|�qSr)�_storage_get�	_commands)r\�opt�rjrFrrrr]br=z2Plugin._instance_apply_dynamic.<locals>.<listcomp>)r�_check_and_save_valuer�r�)rrFrj�optionrr�rr�aszPlugin._instance_apply_dynamiccCs
t��dSrrS�rrFrjrrrr�gsz Plugin._instance_unapply_dynamiccCs
t��dSrrSr�rrrr�jszPlugin._instance_update_dynamiccCst��|_|��|��dS)z
		Initialize commands.
		N)r
rr��_autoregister_commands�_check_commandsrrrrrqs
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(rWN�custom�
per_devicer:r�cSs|ddS)Nr%r:r)Z	name_inforrrr<�r=z/Plugin._autoregister_commands.<locals>.<lambda>r>)
r�__dict__rt�getattr�hasattrr�r�r�r
rr@�iterrA)r�member_name�member�command_namercrrrr�ys*







zPlugin._autoregister_commandscCsFt|j���D]2\}}|�dd�r$qd|vs4d|vrtd|��qdS)z2
		Check if all commands are defined correctly.
		r�Fr�rWz,Plugin command '%s' is not defined correctlyN)rNr�rAr��	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	rW)rrFr�r8r�r0rrr�_storage_set�szPlugin._storage_setcCs |�|j|d|�}|j�|�Sr�)r�r(r	r��rrFr�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�r\r�rrrr]�r=z;Plugin._execute_all_non_device_commands.<locals>.<listcomp>r()rNr�rOr�expandr/r��_execute_non_device_command�rrFr��	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�rrrr]�r=z7Plugin._execute_all_device_commands.<locals>.<listcomp>r()rNr�rOrr�r/r��_execute_device_command)rrFrZr�r�rjrrrr��sz#Plugin._execute_all_device_commandscCs`d}dd�t|j���D�D]>}|j�|j�|dd��}|dur|�||||�dkrd}q|S)NTcSsg|]}|ds|�qSr�rr�rrrr]�r=z:Plugin._verify_all_non_device_commands.<locals>.<listcomp>r(F)rNr�rOrr�r/r��_verify_non_device_command)rrFr�r�r�r�rrrr��sz&Plugin._verify_all_non_device_commandsc	Csnd}dd�t|j���D�D]L}|j�|j�|dd��}|durDq|D]}|�|||||�dkrHd}qHq|S)NTcSsg|]}|dr|�qSr�rr�rrrr]�r=z6Plugin._verify_all_device_commands.<locals>.<listcomp>r(F)rNr�rOrr�r/r��_verify_device_command)rrFrZr�r�r�r�rjrrrr��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)r6ra�int�
ValueErrorr-r.)rr��
current_valueZnwsr��valrrr�_process_assignment_modifiers�s(
z$Plugin._process_assignment_modifiersFcCs*|dur|d|||d�S|d|�SdS)Nr��r�r)rrFr�rjr�rrr�_get_current_value�szPlugin._get_current_valuecCs>|�|||�}|�||�}|dur:|dur:|�||||�|Sr)r�r�r�)rrFr�rjr�r�rrrr�s
zPlugin._check_and_save_valuecCsV|ddur$|dd||dd|�n.|�||||�}|durR|d|||ddd�dS�Nr�TFrW�Zsim�remove�r�)rrFr�rjr�rrrr�s
zPlugin._execute_device_commandcCsR|ddur"|dd|dd|�n,|�||d|�}|durN|d||ddd�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��|)rj)r-rcrsZ STR_VERIFY_PROFILE_VALUE_MISSINGZ'STR_VERIFY_PROFILE_DEVICE_VALUE_MISSINGr�r�r�r6r'�strip�_log_verification_result)	rr(r�r�r�rjr�Zvalsr�rrr�
_verify_values:



�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�dSdSro)
r-rcrsZSTR_VERIFY_PROFILE_VALUE_OKr6r�Z"STR_VERIFY_PROFILE_DEVICE_VALUE_OKr^ZSTR_VERIFY_PROFILE_VALUE_FAILZ$STR_VERIFY_PROFILE_DEVICE_VALUE_FAIL)rr(�successr�r�rjrrrr�>s((zPlugin._log_verification_resultcCsv|ddur"|dd||d||�S|j||||d�}|�||�}|durLdS|d|||dd�}|�|d||||�S)Nr�Tr�rWFr(�r�r�r�)rrFr�rjr�r�r�rrrr�MszPlugin._verify_device_commandcCsj|ddur |dd|d||�S|�||�}|�||�}|durDdS|d||dd�}|�|d|||�S)Nr�TrWFr(r�)rrFr�r�r�r�rrrr�Wsz!Plugin._verify_non_device_commandcCsVtdd�t|j���D��D]4}|j�|dd�dusD|d|jvr|�||�qdS)NcSsg|]}|ds|�qSr�rr�rrrr]br=z;Plugin._cleanup_all_non_device_commands.<locals>.<listcomp>r()�reversedrNr�rOr/r�r�_cleanup_non_device_command)rrFr�rrrr�as $z'Plugin._cleanup_all_non_device_commandscCsdtdd�t|j���D��D]B}|j�|dd�dusD|d|jvr|D]}|�||||�qHqdS)NcSsg|]}|dr|�qSr�rr�rrrr]gr=z7Plugin._cleanup_all_device_commands.<locals>.<listcomp>r()r�rNr�rOr/r�r�_cleanup_device_command)rrFrZr�r�rjrrrr�fs $z#Plugin._cleanup_all_device_commandscCsb|ddur$|ddd|dd|�n:|�|||�}|durP|d|||d|d�|�|||�dS�Nr�FrWr��r�r�)rrFr�rjr��	old_valuerrrr�lszPlugin._cleanup_device_commandcCsZ|ddur"|ddddd|�n4|�||�}|durJ|d||ddd�|�||�dSr�r�)rrFr�r�rrrr�usz"Plugin._cleanup_non_device_command)NNN)N)N)N)NF)NN)N)N)F)F)Frr&�__qualname__�__doc__rrr!�propertyr(�classmethodr)r+rr1r9rGrJrMrrHrLrRr r[r_rgrQrkrmrnrvrsZ
ROLLBACK_SOFTr�r�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.constsrsZtuned.profiles.variablesZtunedZ
tuned.logsr
Ztuned.utils.commandsrrp�
subprocessrrZlogsr�r-�objectrrrrr�<module>s