o
    _V                     @   sn   d dl Z d dlmZ d dlZd dlZd dlZd dlmZ d dl	Z	d dl
mZmZ ej ZG dd deZdS )    N)commands)PopenPIPEc                   @   s  e Zd ZdZdd Zdd Zdd Zedd	 Ze	d
d Z
e	dd Ze	dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd|d3d4Zd5d6 Zd7d8 Z d9d: Z!d|d;d<Z"d=d> Z#d?d@ Z$d|dAdBZ%dCdD Z&dEdF Z'dGdH Z(dIdJ Z)dKdL Z*dMdN Z+	O	Od}dPdQZ,d~dRdSZ-d~dTdUZ.d~dVdWZ/dXdY Z0dZd[ Z1d\d] Z2d^d_ Z3d`da Z4ddbdcZ5ddddeZ6dfdg Z7dhdi Z8djdk Z9d~dldmZ:	Od~dndoZ;dpdq Z<drds Z=dtdu Z>dvdw Z?dxdy Z@dzd{ ZAdOS )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   | | jj| _|| _|| _|| _|| _|| _t	
 | _|   || _|| _d| _d| _|  | _t | _dS )zPlugin constructor.FN)create	__class____name___storage_monitors_repository_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)	selfmonitors_repositorystorage_factoryhardware_inventorydevice_matcherdevice_matcher_udevinstance_factory
global_cfg	variables r#   4/usr/lib/python3/dist-packages/tuned/plugins/base.py__init__   s   

zPlugin.__init__c                 C   s   |    d S N)destroy_instancesr   r#   r#   r$   cleanup,   s   zPlugin.cleanupc                 C   s   | j s|   d| _ d S d S )NT)r   _init_devicesr(   r#   r#   r$   init_devices/   s   
zPlugin.init_devicesc                 C   s   | j jdd ddd S )N._   )r   
__module__splitr(   r#   r#   r$   name4   s   zPlugin.namec                 C      i S )z-Default configuration options for the plugin.r#   r(   r#   r#   r$   _get_config_options<      zPlugin._get_config_optionsc                 C   r3   )z*Explanation of each config option functionr#   )clsr#   r#   r$   get_config_options_hintsA   r5   zPlugin.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   r5   z*Plugin._get_config_options_used_by_dynamicc                 C   sL   |    }|D ]}||v s| jr|| ||< qtd|| jjf  q|S )z3Merge provided options with plugin default options.z$Unknown option '%s' for plugin '%s'.)r4   copyr   logwarnr   r   )r   options	effectivekeyr#   r#   r$   _get_effective_optionsK   s   zPlugin._get_effective_optionsc                 C   s,   t |tu r|S t| }|dkp|dkS )Ntrue1)typeboolstrlower)r   valuer#   r#   r$   _option_boolV   s   zPlugin._option_boolc           	   	   C   sF   || j v rtd| | |}| j| ||||||}|| j |< |S )z8Create new instance of the plugin and seize the devices.z.Plugin instance with name '%s' already exists.)r   	Exceptionr>   r   r   )	r   r2   devices_expressiondevices_udev_regex
script_prescript_postr;   effective_optionsinstancer#   r#   r$   create_instance`   s   


zPlugin.create_instancec                 C   sV   |j | krtd|| f |j| jvrtd| | j|j }| | | j|j= dS )zDestroy existing instance.z9Plugin instance '%s' does not belong to this plugin '%s'.z+Plugin instance '%s' was already destroyed.N)_pluginrG   r2   r   _destroy_instancer   rM   r#   r#   r$   destroy_instancel   s   

zPlugin.destroy_instancec                 C   s$   t d|j| jf  | | dS )zInitialize an instance.zinitializing instance %s (%s)N)r9   debugr2   _instance_initrQ   r#   r#   r$   initialize_instancew   s   zPlugin.initialize_instancec                 C   sB   t | j D ]}td|j| jf  | | q| j  dS )zDestroy all instances.zdestroying instance %s (%s)N)listr   valuesr9   rS   r2   rP   clearrQ   r#   r#   r$   r'   |   s   zPlugin.destroy_instancesc                 C   s   |  | | | d S r&   )release_devices_instance_cleanuprQ   r#   r#   r$   rP      s   
zPlugin._destroy_instancec                 C      t  r&   NotImplementedErrorrQ   r#   r#   r$   rT         zPlugin._instance_initc                 C   r[   r&   r\   rQ   r#   r#   r$   rZ      r^   zPlugin._instance_cleanupc                 C   s   d| _ t | _t | _d S NF)_devices_supportedset_assigned_devices_free_devicesr(   r#   r#   r$   r*      s   zPlugin._init_devicesc                 C   s   dS )zOverride 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   r5   zPlugin._get_device_objectsc                 C   sf   |j d u rt| j|j|S | |}|d u r"td| j t S | j	|j |}tdd |D S )Nz<Plugin '%s' does not support the 'devices_udev_regex' optionc                 S   s   g | ]}|j qS r#   )sys_name).0xr#   r#   r$   
<listcomp>   s    z0Plugin._get_matching_devices.<locals>.<listcomp>)
rI   ra   r   
match_listrH   re   r9   errorr2   r   )r   rM   rd   udev_devicesr#   r#   r$   _get_matching_devices   s   

zPlugin._get_matching_devicesc                 C   s   | j sd S td|j  | || j}t|dk|_|js(td|j  d S |j}|j| jkr8|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, )r`   r9   rS   r2   rm   rc   lenactiver:   infojoinassigned_devicesupdaterb   )r   rM   	to_assignr2   r#   r#   r$   assign_free_devices   s   zPlugin.assign_free_devicesc                 C   sV   | j sd S |j|jB | j@ }d|_|j  |j  |  j|8  _|  j|O  _d S r_   )r`   processed_devicesrs   rb   rp   rX   rc   )r   rM   
to_releaser#   r#   r$   rY      s   

zPlugin.release_devicesc                 C   s$   | j sd g}|D ]}||| qd S r&   )r`   )r   rM   callbackrd   devicer#   r#   r$   _run_for_each_device   s
   zPlugin._run_for_each_devicec                 C      d S r&   r#   r   rM   enablingr#   r#   r$   _instance_pre_static      zPlugin._instance_pre_staticc                 C   r|   r&   r#   r}   r#   r#   r$   _instance_post_static   r   zPlugin._instance_post_staticFc                 C   s\  |d u rd S t |dkrtd|j|f  d S |ds$td dS tj|}d}|D ]}}tj	}	|	
| j  |g}
|rE|
d |
| td|t|
f  td	tt|	   z*t|g|
 ttd|	|dd
}| \}}|jrtd||j|d d f  d}W q. ttfy } ztd||f  d}W Y d }~q.d }~ww |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}.FTfull_rollbackz'calling script '%s' with arguments '%s'zusing environment '%s')stdoutstderr	close_fdsenvcwduniversal_newlineszscript '%s' error: %d, '%s'r-   zscript '%s' error: %s)ro   r9   r:   r2   
startswithrk   ospathdirnameenvironrt   r   get_envappendrq   rC   rS   rV   itemsr   r   communicate
returncodeOSErrorIOError)r   rM   scriptoprd   r   dir_nameretdevr   	argumentsprocouterrer#   r#   r$   _call_device_script   sH   




zPlugin._call_device_scriptc                 C   s   |j sdS |jr-| ||jd|j | |d | | | |d | ||jd|j |j	rB| j
tjtjrB| || j|j |j|j |j  dS )zG
		Apply static and dynamic tuning if the plugin instance is active.
		NapplyT)rp   has_static_tuningr   rJ   rs   r   _instance_apply_staticr   rK   has_dynamic_tuningr   getconstsCFG_DYNAMIC_TUNINGCFG_DEF_DYNAMIC_TUNINGr{   _instance_apply_dynamicrw   rt   rX   rQ   r#   r#   r$   instance_apply_tuning   s    


zPlugin.instance_apply_tuningc                 C   s   |j sdS t|jdkrtdd|j  |j }|jrF| 	||j
d|dkr,dS | |||dkr7dS | 	||jd|dkrDdS dS dS )z<
		Verify static tuning if the plugin instance is active.
		Nr   z)BUG: Some devices have not been tuned: %srn   verifyFT)rp   ro   rs   r9   rk   rr   rw   r8   r   r   rJ   _instance_verify_staticrK   )r   rM   ignore_missingrd   r#   r#   r$   instance_verify_tuning  s    

zPlugin.instance_verify_tuningc                 C   sD   |j sdS |jr| jtjtjr | || j|j	
  dS dS dS )z<
		Apply dynamic tuning if the plugin instance is active.
		N)rp   r   r   r   r   r   r   r{   _instance_update_dynamicrw   r8   rQ   r#   r#   r$   instance_update_tuning$  s
   zPlugin.instance_update_tuningc                 C   s   |j r| jtjtjr| || j|j |j	rD| j
||jd|j|d | |d | || | |d | j
||jd|j|d dS dS )z8
		Remove all tunings applied by the plugin instance.
		unapply)r   FN)r   r   r   r   r   r   r{   _instance_unapply_dynamicrw   r   r   rK   r   _instance_unapply_staticr   rJ   r   rM   r   r#   r#   r$   instance_unapply_tuning-  s   
zPlugin.instance_unapply_tuningc                 C   s   |  | | ||j d S r&   ) _execute_all_non_device_commands_execute_all_device_commandsrs   rQ   r#   r#   r$   r   <  s   
zPlugin._instance_apply_staticc                 C   s2   d}|  ||dkrd}| |||dkrd}|S NTF)_verify_all_non_device_commands_verify_all_device_commands)r   rM   r   rd   r   r#   r#   r$   r   @  s   zPlugin._instance_verify_staticc                 C   s   |  ||j | | d S r&   )_cleanup_all_device_commandsrw    _cleanup_all_non_device_commandsr   r#   r#   r$   r   H  s   zPlugin._instance_unapply_staticc                    sB    fddj D D ]}j|   q  d S )Nc                    s(   g | ]} j|  d u r|qS r&   )_storage_get	_commands)rg   optrz   rM   r   r#   r$   ri   N  s   ( z2Plugin._instance_apply_dynamic.<locals>.<listcomp>)r   _check_and_save_valuer   r   )r   rM   rz   optionr#   r   r$   r   M  s   zPlugin._instance_apply_dynamicc                 C   r[   r&   r\   r   rM   rz   r#   r#   r$   r   S  r^   z Plugin._instance_unapply_dynamicc                 C   r[   r&   r\   r   r#   r#   r$   r   V  r^   zPlugin._instance_update_dynamicc                 C   s   t  | _|   |   dS )z
		Initialize commands.
		N)r   r   r   _autoregister_commands_check_commandsr(   r#   r#   r$   r   ]  s   
zPlugin._init_commandsc                 C   s   | j jD ]c}|drqt| |}t|dsq|jd }| j|d|i}d|jv rAd|d< ||d< |jd |d< |jd |d< n!d	|jv rK||d	< nd|jv rb||d< |jd |d< |jd |d< || j|< qt	t
t| j d
d d| _dS )zd
		Register all commands marked using @command_set, @command_get, and @command_custom decorators.
		___commandr2   ra   Ncustom
per_devicepriorityr   c                 S   s   | d d S )Nr/   r   r#   )	name_infor#   r#   r$   <lambda>  s    z/Plugin._autoregister_commands.<locals>.<lambda>)r=   )r   __dict__r   getattrhasattrr   r   r   r   r   sortediterr   )r   member_namemembercommand_namerq   r#   r#   r$   r   e  s*   







&zPlugin._autoregister_commandsc                 C   sF   t | j D ]\}}|ddrqd|vsd|vr td| qdS )z2
		Check if all commands are defined correctly.
		r   Fr   ra   z,Plugin command '%s' is not defined correctlyN)rV   r   r   r   	TypeError)r   r   commandr#   r#   r$   r     s   zPlugin._check_commandsNc                 C   sJ   t | j}|d u rdn|}|d u rdn|}|d u rdn|}d||||f S )N z%s/%s/%s/%s)rA   r   )r   instance_namer   device_name
class_namer#   r#   r$   _storage_key  s   
zPlugin._storage_keyc                 C   s&   |  |j|d |}| j|| d S Nr2   )r   r2   r	   ra   )r   rM   r   rE   r   r=   r#   r#   r$   _storage_set  s   zPlugin._storage_setc                 C       |  |j|d |}| j|S r   )r   r2   r	   r   r   rM   r   r   r=   r#   r#   r$   r        zPlugin._storage_getc                 C   r   r   )r   r2   r	   unsetr   r#   r#   r$   _storage_unset  r   zPlugin._storage_unsetc                 C   sR   dd t | j D D ]}| j|j|d d }|d ur&| ||| qd S )Nc                 S      g | ]}|d  s|qS r   r#   rg   r   r#   r#   r$   ri         z;Plugin._execute_all_non_device_commands.<locals>.<listcomp>r2   )rV   r   rW   r   expandr;   r   _execute_non_device_commandr   rM   r   	new_valuer#   r#   r$   r     s   z'Plugin._execute_all_non_device_commandsc                 C   s`   dd t | j D D ]!}| j|j|d d }|d u r q|D ]
}| |||| q"qd S )Nc                 S      g | ]}|d  r|qS r   r#   r   r#   r#   r$   ri     r   z7Plugin._execute_all_device_commands.<locals>.<listcomp>r2   )rV   r   rW   r   r   r;   r   _execute_device_command)r   rM   rd   r   r   rz   r#   r#   r$   r     s   z#Plugin._execute_all_device_commandsc                 C   s`   d}dd t | j D D ]}| j|j|d d }|d ur-| ||||dkr-d}q|S )NTc                 S   r   r   r#   r   r#   r#   r$   ri     r   z:Plugin._verify_all_non_device_commands.<locals>.<listcomp>r2   F)rV   r   rW   r   r   r;   r   _verify_non_device_command)r   rM   r   r   r   r   r#   r#   r$   r     s   z&Plugin._verify_all_non_device_commandsc              	   C   sf   d}dd t | j D D ]"}|j|d d }|d u rq|D ]}| |||||dkr/d}q q|S )NTc                 S   r   r   r#   r   r#   r#   r$   ri     r   z6Plugin._verify_all_device_commands.<locals>.<listcomp>r2   F)rV   r   rW   r;   r   _verify_device_command)r   rM   rd   r   r   r   r   rz   r#   r#   r$   r     s   z"Plugin._verify_all_device_commandsc              
   C   s   |d uret |}t|dkr|S |d d }|dd  }|d u r(|dv r&|S |S z'|dkr;t|t|kr8|W S W d S |dkrMt|t|k rJ|W S W d S W |S  tyd   td||||f  Y |S w |S )Nr/   )<>r   r   zhcannot compare new value '%s' with current value '%s' by operator '%s', using '%s' directly as new value)rC   ro   int
ValueErrorr9   r:   )r   r   current_valuenwsr   valr#   r#   r$   _process_assignment_modifiers  s0   z$Plugin._process_assignment_modifiersc                 C   s"   |d ur|d ||dS |d  S )Nr   r   r#   )r   r   rz   r   r#   r#   r$   _get_current_value  s   
zPlugin._get_current_valuec                 C   s<   |  ||}| ||}|d ur|d ur| |||| |S r&   )r   r   r   )r   rM   r   rz   r   r   r#   r#   r$   r     s
   zPlugin._check_and_save_valuec                 C   sV   |d d ur|d d||dd d S |  ||||}|d ur)|d ||dd d S d S Nr   TFra   simr   )r   rM   r   rz   r   r#   r#   r$   r     s   zPlugin._execute_device_commandc                 C   sR   |d d ur|d d|dd d S |  ||d |}|d ur'|d |dd d S d S r   r   r   r#   r#   r$   r     s   z"Plugin._execute_non_device_commandc                 C   s.   | j t|}td|rtdd|S |S )Nz\s*(0+,?)+([\da-fA-F]*,?)*\s*$z^\s*(0+,?)+r   )r   unquoterC   rematchsub)r   rE   vr#   r#   r$   _norm_value  s   zPlugin._norm_valuec           	      C   s  |d u rd S d}|d u r(|r(|d u rt tj|  dS t tj||f  dS |d ur| |}| |}z
t|t|k}W nA ty   zt|dt|dk}W n+ ty~   t|t|k}|s|t|	d}|D ]}|
 }||k}|r{ nqmY nw Y nw | j|||||d |S )NFT   |)rz   )r9   rq   r    STR_VERIFY_PROFILE_VALUE_MISSING'STR_VERIFY_PROFILE_DEVICE_VALUE_MISSINGr  r   r   rC   r1   strip_log_verification_result)	r   r2   r   r   r   rz   r   valsr   r#   r#   r$   _verify_value
  sF   


zPlugin._verify_valuec                 C   s   |r'|d u rt tj|t| f  dS t tj||t| f  dS |d u r@t tj|t| t| f  dS t tj	||t| t| f  dS r   )
r9   rq   r   STR_VERIFY_PROFILE_VALUE_OKrC   r  "STR_VERIFY_PROFILE_DEVICE_VALUE_OKrk   STR_VERIFY_PROFILE_VALUE_FAIL$STR_VERIFY_PROFILE_DEVICE_VALUE_FAIL)r   r2   successr   r   rz   r#   r#   r$   r  *  s   &(zPlugin._log_verification_resultc                 C   sn   |d d ur|d d||d|S | j |||d}| ||}|d u r$d S |d ||d}| |d ||||S )Nr   Tr   ra   r2   r   r   r
  )r   rM   r   rz   r   r   r   r#   r#   r$   r   9  s   zPlugin._verify_device_commandc                 C   sb   |d d ur|d d|d|S |  |}| ||}|d u r d S |d |d}| |d |||S )Nr   Tra   r2   r  )r   rM   r   r   r   r   r#   r#   r$   r   C  s   
z!Plugin._verify_non_device_commandc                 C   sV   t dd t| j D D ]}|j|d d d us"|d | jv r(| || qd S )Nc                 S   r   r   r#   r   r#   r#   r$   ri   N  r   z;Plugin._cleanup_all_non_device_commands.<locals>.<listcomp>r2   )reversedrV   r   rW   r;   r   r   _cleanup_non_device_command)r   rM   r   r#   r#   r$   r   M  s
    $z'Plugin._cleanup_all_non_device_commandsc                 C   sb   t dd t| j D D ] }|j|d d d us"|d | jv r.|D ]	}| ||| q$qd S )Nc                 S   r   r   r#   r   r#   r#   r$   ri   S  r   z7Plugin._cleanup_all_device_commands.<locals>.<listcomp>r2   )r  rV   r   rW   r;   r   r   _cleanup_device_command)r   rM   rd   r   rz   r#   r#   r$   r   R  s    $z#Plugin._cleanup_all_device_commandsc                 C   s^   |d d ur|d dd |dd d S |  |||}|d ur&|d ||dd | ||| d S Nr   Fra   r   r   r   )r   rM   r   rz   	old_valuer#   r#   r$   r  X  s   zPlugin._cleanup_device_commandc                 C   sV   |d d ur|d dd dd d S |  ||}|d ur#|d |dd | || d S r  r  )r   rM   r   r  r#   r#   r$   r  a  s   z"Plugin._cleanup_non_device_command)F)NNNr&   r_   )NN)Br   r0   __qualname____doc__r%   r)   r+   propertyr2   classmethodr4   r7   r   r>   rF   rN   rR   rU   r'   rP   rT   rZ   r*   re   rm   rv   rY   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#   r#   r$   r      s    	





#
	


	

	


!


	r   )r   tuned.constsr   tuned.profiles.variablestuned
tuned.logsr   tuned.utils.commandsr   r   
subprocessr   r   logsr   r9   objectr   r#   r#   r#   r$   <module>   s    
