관리-도구
편집 파일: base.cpython-39.pyc
a ����,�g [����������������������@���sn���d�dl�Z�d�dlmZ�d�dlZd�dlZd�dlZd�dlmZ�d�dl Z d�dl mZmZ�ej ���ZG�dd��de�ZdS�)�����N)�commands)�Popen�PIPEc�������������������@���s6��e�Zd�ZdZdd��Zdd��Zdd��Zedd ���Ze d d���Z e dd ���Ze dd���Zdd��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 ����������� ������C���sn���|��|�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__c�����������������C���s���|������d�S��N)�destroy_instances�r���r���r���r����cleanup,���s����zPlugin.cleanupc�����������������C���s���|�j�s|�����d|�_�d�S�)NT)r���� _init_devicesr���r���r���r����init_devices/���s����zPlugin.init_devicesc�����������������C���s���|�j�j�d�d��dd�d�S�)N�.����_����)r���� __module__�splitr���r���r���r����name4���s����zPlugin.namec�����������������C���s���i�S�)z-Default configuration options for the plugin.r���r���r���r���r����_get_config_options<���s����zPlugin._get_config_optionsc�����������������C���s���i�S�)z*Explanation of each config option functionr���)�clsr���r���r����get_config_options_hintsA���s����zPlugin.get_config_options_hintsc�����������������C���s���g�S�)znList of config options used by dynamic tuning. Their previous values will be automatically saved and restored.r���r���r���r���r���r���F���s����z*Plugin._get_config_options_used_by_dynamicc�����������������C���sL���|��������}|D�]6}||v�s"|�jr0||�||<�qt�d||�jjf���q|S�)z3Merge provided options with plugin default options.z$Unknown option '%s' for plugin '%s'.)r)����copyr����log�warningr���r���)r����optionsZ effective�keyr���r���r����_get_effective_optionsK���s����zPlugin._get_effective_optionsc�����������������C���s,���t�|�tu�r|S�t|����}|dkp*|dkS�)N�true�1)�type�bool�str�lower)r����valuer���r���r����_option_boolV���s����zPlugin._option_boolc����������� ��� ���C���sf���||�j�v�rtd|���|��|�}|�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.c�����������������S���s ���|�d�j�S�)Nr%���)�priority)�xr���r���r����<lambda>i��������z(Plugin.create_instance.<locals>.<lambda>�r0���) r���� Exceptionr1���r���r���r ���r����sorted�items) r���r(���r:����devices_expression�devices_udev_regex� script_pre�script_postr/���Zeffective_options�instancer���r���r����create_instance`���s���� � zPlugin.create_instancec�����������������C���sV���|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�r���rF���r���r���r����destroy_instancem���s���� zPlugin.destroy_instancec�����������������C���s$���t��d|j|�jf���|��|��dS�)zInitialize an instance.zinitializing instance %s (%s)N)r-����debugr(����_instance_initrI���r���r���r����initialize_instancex���s����zPlugin.initialize_instancec�����������������C���sB���t�|�j����D�]$}t�d|j|�jf���|��|��q|�j����dS�)zDestroy all instances.zdestroying instance %s (%s)N)�listr����valuesr-���rK���r(���rH����clearrI���r���r���r���r���}���s����zPlugin.destroy_instancesc�����������������C���s���|���|��|��|��d�S�r���)�release_devices�_instance_cleanuprI���r���r���r���rH�������s���� zPlugin._destroy_instancec�����������������C���s ���t����d�S�r�����NotImplementedErrorrI���r���r���r���rL�������s����zPlugin._instance_initc�����������������C���s ���t����d�S�r���rS���rI���r���r���r���rR�������s����zPlugin._instance_cleanupc�����������������C���s���d|�_�t��|�_t��|�_d�S��NF)�_devices_supported�set�_assigned_devices� _free_devicesr���r���r���r���r �������s����zPlugin._init_devicesc�����������������C���s���dS�)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����devicesr���r���r����_get_device_objects����s����zPlugin._get_device_objectsc�����������������C���sj���|j�d�u�rt|�j�|j|��S�|��|�}|d�u�rDt�d|�j��t��S�|�j �|j�|�}tdd��|D���S�d�S�)Nz<Plugin '%s' does not support the 'devices_udev_regex' optionc�����������������S���s���g�|�] }|j��qS�r���)Zsys_name)�.0r;���r���r���r���� <listcomp>����r=���z0Plugin._get_matching_devices.<locals>.<listcomp>) rC���rW���r ���Z match_listrB���r[���r-����errorr(���r���)r���rF���rZ���Zudev_devicesr���r���r����_get_matching_devices����s���� zPlugin._get_matching_devicesc�����������������C���s����|�j�s d�S�t�d|j���|��||�j�}t|�dk|_|jsNt�d|j���n`|j}|j|�jkrn|d|�j�7�}t� d|d� |�f���|j�|��|��j |O��_ |��j|8��_d�S�)Nz assigning devices to instance %sr���z*instance %s: no matching devices availablez (%s)z!instance %s: assigning devices %s�, )rV���r-���rK���r(���r_���rY����len�activer.����info�join�assigned_devices�updaterX���)r���rF���Z to_assignr(���r���r���r����assign_free_devices����s����zPlugin.assign_free_devicesc�����������������C���sV���|�j�s d�S�|j|jB�|�j@�}d|_|j����|j����|��j|8��_|��j|O��_d�S�rU���)rV����processed_devicesre���rX���rb���rP���rY���)r���rF���Z to_releaser���r���r���rQ�������s������ zPlugin.release_devicesc�����������������C���s$���|�j�sd�g}|D�]}|||��qd�S�r���)rV���)r���rF����callbackrZ����devicer���r���r����_run_for_each_device����s����zPlugin._run_for_each_devicec�����������������C���s���d�S�r���r����r���rF���Zenablingr���r���r����_instance_pre_static����s����zPlugin._instance_pre_staticc�����������������C���s���d�S�r���r���rl���r���r���r����_instance_post_static����s����zPlugin._instance_post_staticc�����������������C���s<���t�j�|�}|�j�tjtj�}|D�]}|�|�r"�dS�q"dS��NTF) �os�path�realpathr���Zget_list�constsZCFG_PROFILE_DIRSZCFG_DEF_PROFILE_DIRS� startswith)r���rq���Z profile_paths�pr���r���r����_safe_script_path����s���� zPlugin._safe_script_pathc�����������������C���s���|d�u�rd�S�t�|�dkr0t�d|j|f���d�S�|�d�sHt�d��dS�|��|�sht�dd|����dS�tj� |�}d}|D��]}tj } | �|�j� ����|g} |tjkr�| �d ��| �|��t�d |t| �f���t�dtt| ��������zVt|g| �ttd| |dd�}|���\}} |j�r@t�d ||j| d�d��f���d}W�q|�ttf�y��}�z"t�d||f���d}W�Y�d�}~q|d�}~0�0�q||S�)Nr���z1Instance '%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)ra���r-���r.���r(���rt���r^���rv���rp���rq����dirname�environrf���r���Zget_envrs���Z ROLLBACK_FULL�appendrc���r6���rK���rN���rA���r���r���Zcommunicate� returncode�OSError�IOError)r���rF���Zscript�oprZ����rollbackZdir_name�ret�devr}���Z arguments�proc�out�err�er���r���r����_call_device_script����sL���� � �zPlugin._call_device_scriptc�����������������C���s����|j�s dS�|jrZ|��||jd|j��|��|d��|��|��|��|d��|��||jd|j��|j r�|�j �tj tj�r�|��|��|��||�j|j��|j�|j��|j����dS�)zG Apply static and dynamic tuning if the plugin instance is active. NZapplyT)rb����has_static_tuningr����rD���re���rm����_instance_apply_staticrn���rE����has_dynamic_tuningr����getrs����CFG_DYNAMIC_TUNING�CFG_DEF_DYNAMIC_TUNING�_instance_init_dynamicrk����_instance_apply_dynamicrh���rf���rP���rI���r���r���r����instance_apply_tuning��s"���� � � zPlugin.instance_apply_tuningc�����������������C���s����|j�s dS�t|j�dkr.t�dd�|j����|j���}|jr�|�� ||j d|�dkrXdS�|��|||�dkrndS�|�� ||jd|�dkr�dS�dS�dS�dS�)z< Verify static tuning if the plugin instance is active. Nr���z)BUG: Some devices have not been tuned: %sr`���ZverifyFT) rb���ra���re���r-���r^���rd���rh���r,���r����r����rD����_instance_verify_staticrE���)r���rF����ignore_missingrZ���r���r���r����instance_verify_tuning��s ���� � zPlugin.instance_verify_tuningc�����������������C���s<���|j�s dS�|jr8|�j�tjtj�r8|��||�j|j � ����dS�)z< Apply dynamic tuning if the plugin instance is active. N)rb���r����r���r����rs���r����r����rk����_instance_update_dynamicrh���r,���rI���r���r���r����instance_update_tuning2��s����zPlugin.instance_update_tuningc�����������������C���s����|t�jkrdS�|jr8|�j�t�jt�j�r8|��||�j|j ��|j r�|�j||jd|j |d��|�� |d��|��||��|��|d��|�j||jd|j |d��dS�)z8 Remove all tunings applied by the plugin instance. NZunapply)r����F)rs���Z ROLLBACK_NONEr����r���r����r����r����rk����_instance_unapply_dynamicrh���r����r����rE���rm����_instance_unapply_staticrn���rD����r���rF���r����r���r���r����instance_unapply_tuning;��s���� �zPlugin.instance_unapply_tuningc�����������������C���s���|���|��|��||j��d�S�r���)� _execute_all_non_device_commands�_execute_all_device_commandsre���rI���r���r���r���r����M��s���� zPlugin._instance_apply_staticc�����������������C���s2���d}|���||�dkrd}|��|||�dkr.d}|S�ro���)�_verify_all_non_device_commands�_verify_all_device_commands)r���rF���r����rZ���r����r���r���r���r����Q��s����zPlugin._instance_verify_staticc�����������������C���s���|���||j��|��|��d�S�r���)�_cleanup_all_device_commandsrh���� _cleanup_all_non_device_commandsr����r���r���r���r����Y��s�����zPlugin._instance_unapply_staticc�����������������C���s���d�S�r���r���rI���r���r���r���r����^��s����zPlugin._instance_init_dynamicc��������������������sB�������fdd��j�D��D�]}����j|�����q�������d�S�)Nc��������������������s(���g�|�] }�����j|����d�u�r|�qS�r���)�_storage_get� _commands)r\����opt�rj���rF���r���r���r���r]���b��r=���z2Plugin._instance_apply_dynamic.<locals>.<listcomp>)r����_check_and_save_valuer����r����)r���rF���rj����optionr���r����r���r����a��s����zPlugin._instance_apply_dynamicc�����������������C���s ���t����d�S�r���rS����r���rF���rj���r���r���r���r����g��s����z Plugin._instance_unapply_dynamicc�����������������C���s ���t����d�S�r���rS���r����r���r���r���r����j��s����zPlugin._instance_update_dynamicc�����������������C���s���t����|�_|�����|�����dS�)z Initialize commands. N)r ���r���r�����_autoregister_commands�_check_commandsr���r���r���r���r���q��s���� zPlugin._init_commandsc�����������������C���s����|�j�jD�]�}|�d�rqt|�|�}t|d�s.q|jd�}|�j�|d|i�}d|jv�r�d|d<�||d<�|jd�|d<�|jd�|d<�nBd |jv�r�||d <�n.d|jv�r�||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(���rW���N�custom� per_devicer:���r����c�����������������S���s���|�d�d�S�)Nr%���r:���r���)Z name_infor���r���r���r<������r=���z/Plugin._autoregister_commands.<locals>.<lambda>r>���) r����__dict__rt����getattr�hasattrr����r����r����r ���r���r@����iterrA���)r����member_name�member�command_namerc���r���r���r���r����y��s*���� zPlugin._autoregister_commandsc�����������������C���sF���t�|�j����D�]2\}}|�dd�r$qd|vs4d|vrtd|���qdS�)z2 Check if all commands are defined correctly. r����Fr����rW���z,Plugin command '%s' is not defined correctlyN)rN���r����rA���r����� TypeError)r���r�����commandr���r���r���r�������s ����zPlugin._check_commandsNc�����������������C���sJ���t�|��j}|d�u�rdn|}|d�u�r&dn|}|d�u�r6dn|}d||||f�S�)N��z%s/%s/%s/%s)r4���r���)r���Z instance_namer�����device_name� class_namer���r���r����_storage_key���s���� �zPlugin._storage_keyc�����������������C���s&���|���|j|d�|�}|�j�||��d�S��Nr(���)r����r(���r ���rW���)r���rF���r����r8���r����r0���r���r���r����_storage_set���s����zPlugin._storage_setc�����������������C���s ���|���|j|d�|�}|�j�|�S�r����)r����r(���r ���r�����r���rF���r����r����r0���r���r���r���r�������s����zPlugin._storage_getc�����������������C���s ���|���|j|d�|�}|�j�|�S�r����)r����r(���r ���Zunsetr����r���r���r����_storage_unset���s����zPlugin._storage_unsetc�����������������C���sR���dd��t�|�j����D��D�]4}|�j�|j�|d�d���}|d�ur|��|||��qd�S�)Nc�����������������S���s���g�|�]}|d��s|�qS��r����r����r\���r����r���r���r���r]������r=���z;Plugin._execute_all_non_device_commands.<locals>.<listcomp>r(���)rN���r����rO���r����expandr/���r�����_execute_non_device_command�r���rF���r����� new_valuer���r���r���r�������s����z'Plugin._execute_all_non_device_commandsc�����������������C���s`���dd��t�|�j����D��D�]B}|�j�|j�|d�d���}|d�u�r@q|D�]}|��||||��qDqd�S�)Nc�����������������S���s���g�|�]}|d��r|�qS�r����r���r����r���r���r���r]������r=���z7Plugin._execute_all_device_commands.<locals>.<listcomp>r(���)rN���r����rO���r���r����r/���r�����_execute_device_command)r���rF���rZ���r����r����rj���r���r���r���r�������s����z#Plugin._execute_all_device_commandsc�����������������C���s`���d}dd��t�|�j����D��D�]>}|�j�|j�|d�d���}|d�ur|��||||�dkrd}q|S�)NTc�����������������S���s���g�|�]}|d��s|�qS�r����r���r����r���r���r���r]������r=���z:Plugin._verify_all_non_device_commands.<locals>.<listcomp>r(���F)rN���r����rO���r���r����r/���r�����_verify_non_device_command)r���rF���r����r����r����r����r���r���r���r�������s����z&Plugin._verify_all_non_device_commandsc�������������� ���C���sn���d}dd��t�|�j����D��D�]L}|�j�|j�|d�d���}|d�u�rDq|D�]}|��|||||�dkrHd}qHq|S�)NTc�����������������S���s���g�|�]}|d��r|�qS�r����r���r����r���r���r���r]������r=���z6Plugin._verify_all_device_commands.<locals>.<listcomp>r(���F)rN���r����rO���r���r����r/���r�����_verify_device_command)r���rF���rZ���r����r����r����r����rj���r���r���r���r�������s����z"Plugin._verify_all_device_commandsc�������������� ���C���s����|d�ur�t�|�}t|�dkr |S�|d�d��}|dd���}|d�u�rP|dv�rL|S�|S�zN|dkrxt|�t|�krp|W�S�W�d�S�n$|dkr�t|�t|�k�r�|W�S�W�d�S�W�n(�ty����t�d||||f���Y�n0�|S�)Nr%���)�<�>r����r����zhcannot compare new value '%s' with current value '%s' by operator '%s', using '%s' directly as new value)r6���ra����int� ValueErrorr-���r.���)r���r����� current_valueZnwsr�����valr���r���r����_process_assignment_modifiers���s(���� z$Plugin._process_assignment_modifiersFc�����������������C���s*���|d�ur|d�|||d�S�|d�|�S�d�S�)Nr�����r����r���)r���rF���r����rj���r����r���r���r����_get_current_value���s����zPlugin._get_current_valuec�����������������C���s>���|���|||�}|��||�}|d�ur:|d�ur:|��||||��|S�r���)r����r����r����)r���rF���r����rj���r����r����r���r���r���r������s ����zPlugin._check_and_save_valuec�����������������C���sV���|d�d�ur$|d�d||dd|��n.|���||||�}|d�urR|d�|||ddd��d�S��Nr����TFrW����Zsim�remove�r����)r���rF���r����rj���r����r���r���r���r������s ����zPlugin._execute_device_commandc�����������������C���sR���|d�d�ur"|d�d|dd|��n,|���||d�|�}|d�urN|d�||ddd��d�S�r����r����r����r���r���r���r������s ����z"Plugin._execute_non_device_commandc�����������������C���s.���|�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)r���r8����vr���r���r����_norm_value��s����zPlugin._norm_valuec����������� ������C���s��|d�u�rd�S�d}|d�u�rN|rN|d�u�r6t��tj|���nt��tj||f���dS�|d�u�r|��|�}|��|�}zt|�t|�k}W�n��t�y���zt|d�t|d�k}W�nV�ty����t|�t|�k}|s�t|�� d�}|D�]}|� ��}||k}|r��q�q�Y�n0�Y�n0�|�j|||||d��|S�)NFT�����|)rj���)r-���rc���rs���Z STR_VERIFY_PROFILE_VALUE_MISSINGZ'STR_VERIFY_PROFILE_DEVICE_VALUE_MISSINGr����r����r����r6���r'����strip�_log_verification_result) r���r(���r����r����r����rj���r����Zvalsr����r���r���r���� _verify_value��s:���� �zPlugin._verify_valuec�����������������C���s����|rL|d�u�r*t��tj|t|����f���nt��tj||t|����f���dS�|d�u�r|t��tj|t|����t|����f���n(t��tj ||t|����t|����f���dS�d�S�ro���) r-���rc���rs���ZSTR_VERIFY_PROFILE_VALUE_OKr6���r����Z"STR_VERIFY_PROFILE_DEVICE_VALUE_OKr^���ZSTR_VERIFY_PROFILE_VALUE_FAILZ$STR_VERIFY_PROFILE_DEVICE_VALUE_FAIL)r���r(����successr����r����rj���r���r���r���r����>��s����((zPlugin._log_verification_resultc�����������������C���sv���|d�d�ur"|d�d||d||�S�|�j�||||d�}|��||�}|d�u�rLd�S�|d�|||dd�}|��|d�||||�S�)Nr����Tr����rW���Fr(����r����r����r����)r���rF���r����rj���r����r����r����r���r���r���r����M��s����zPlugin._verify_device_commandc�����������������C���sj���|d�d�ur |d�d|d||�S�|���||�}|��||�}|d�u�rDd�S�|d�||dd�}|��|d�|||�S�)Nr����TrW���Fr(���r����)r���rF���r����r����r����r����r���r���r���r����W��s����z!Plugin._verify_non_device_commandc�����������������C���sV���t�dd��t|�j����D���D�]4}|j�|d�d��d�usD|d�|�jv�r|��||��qd�S�)Nc�����������������S���s���g�|�]}|d��s|�qS�r����r���r����r���r���r���r]���b��r=���z;Plugin._cleanup_all_non_device_commands.<locals>.<listcomp>r(���)�reversedrN���r����rO���r/���r����r����_cleanup_non_device_command)r���rF���r����r���r���r���r����a��s���� $z'Plugin._cleanup_all_non_device_commandsc�����������������C���sd���t�dd��t|�j����D���D�]B}|j�|d�d��d�usD|d�|�jv�r|D�]}|��||||��qHqd�S�)Nc�����������������S���s���g�|�]}|d��r|�qS�r����r���r����r���r���r���r]���g��r=���z7Plugin._cleanup_all_device_commands.<locals>.<listcomp>r(���)r����rN���r����rO���r/���r����r����_cleanup_device_command)r���rF���rZ���r����r����rj���r���r���r���r����f��s���� $z#Plugin._cleanup_all_device_commandsc�����������������C���sb���|d�d�ur$|d�dd�|dd|��n:|���|||�}|d�urP|d�|||d|d��|��|||��d�S��Nr����FrW���r�����r����r����)r���rF���r����rj���r����� old_valuer���r���r���r����l��s����zPlugin._cleanup_device_commandc�����������������C���sZ���|d�d�ur"|d�dd�dd|��n4|���||�}|d�urJ|d�||ddd��|��||��d�S�r����r����)r���rF���r����r����r���r���r���r����u��s����z"Plugin._cleanup_non_device_command)NNN)N)N)N)NF)NN)N)N)F)F)Fr���r&����__qualname__�__doc__r���r���r!����propertyr(����classmethodr)���r+���r���r1���r9���rG���rJ���rM���r���rH���rL���rR���r ���r[���r_���rg���rQ���rk���rm���rn���rv���rs���Z ROLLBACK_SOFTr����r����r����r����r����r����r����r����r����r����r����r����r���r����r����r����r����r����r����r����r����r����r����r����r����r����r����r����r����r����r����r����r����r����r����r����r����r���r���r���r���r������s���� ' ��� !�� r���)r����Ztuned.constsrs���Ztuned.profiles.variablesZtunedZ tuned.logsr ���Ztuned.utils.commandsr���rp���� subprocessr���r���Zlogsr����r-����objectr���r���r���r���r����<module>���s���