�
��\c@s�ddlZddljZddlZddlZddlZddlmZddl Z ddl
mZmZej
j�Zdefd��YZdS(i����N(tcommands(tPopentPIPEtPlugincBsoeZdZd�Zd�Zd�Zed��Zed��Z ed��Z
ed��Zd�Zd �Z
d
�Zd�Zd�Zd
�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zed�Zd�Zd�Z d�Z!ed�Z"d�Z#d�Z$ed �Z%d!�Z&d"�Z'd#�Z(d$�Z)d%�Z*d&�Z+d=d=d=d'�Z-d=d(�Z.d=d)�Z/d=d*�Z0d+�Z1d,�Z2d-�Z3d.�Z4d/�Z5d=ed0�Z6d=d=d1�Z7d2�Z8d3�Z9d4�Z:d=d5�Z;d=d6�Z<d7�Z=d8�Z>d9�Z?d:�Z@d;�ZAd<�ZBRS(>s
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 Cs�|j|jj�|_||_||_||_||_||_t j
�|_|j�||_
||_t|_t|_|j�|_t�|_dS(sPlugin constructor.N(tcreatet __class__t__name__t_storaget_monitors_repositoryt_hardware_inventoryt_device_matchert_device_matcher_udevt_instance_factorytcollectionstOrderedDictt
_instancest_init_commandst_global_cfgt
_variablestFalset_has_dynamic_optionst_devices_initedt#_get_config_options_used_by_dynamict_options_used_by_dynamicRt_cmd( tselftmonitors_repositorytstorage_factorythardware_inventorytdevice_matchertdevice_matcher_udevtinstance_factoryt
global_cfgt variables((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt__init__s
cCs|j�dS(N(tdestroy_instances(R((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pytcleanup,scCs#|js|j�t|_ndS(N(Rt
_init_devicestTrue(R((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pytinit_devices/s
cCs'|jjjd�djdd�dS(Nt.i����t_i(Rt
__module__tsplit(R((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pytname4scCsiS(s-Default configuration options for the plugin.((R((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt_get_config_options<scCsiS(s*Explanation of each config option function((tcls((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pytget_config_options_hintsAscCsgS(snList of config options used by dynamic tuning. Their previous values will be automatically saved and restored.((R((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyRFscCsj|j�j�}xQ|D]I}||ks4|jrE||||<qtjd||jjf�qW|S(s3Merge provided options with plugin default options.s$Unknown option '%s' for plugin '%s'.(R-tcopyRtlogtwarnRR(Rtoptionst effectivetkey((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt_get_effective_optionsKs
!cCs>t|�tkr|St|�j�}|dkp=|dkS(Nttruet1(ttypetbooltstrtlower(Rtvalue((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt_option_boolVsc Csf||jkr"td|��n|j|�}|jj|||||||�}||j|<|S(s8Create new instance of the plugin and seize the devices.s.Plugin instance with name '%s' already exists.(Rt ExceptionR6RR( RR,tdevices_expressiontdevices_udev_regext
script_pretscript_postR3teffective_optionstinstance((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pytcreate_instance`s
cCs{|j|kr(td||f��n|j|jkrMtd|��n|j|j}|j|�|j|j=dS(sDestroy existing instance.s9Plugin instance '%s' does not belong to this plugin '%s'.s+Plugin instance '%s' was already destroyed.N(t_pluginR?R,Rt_destroy_instance(RRE((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pytdestroy_instancels
cCs.tjd|j|jf�|j|�dS(sInitialize an instance.sinitializing instance %s (%s)N(R1tdebugR,t_instance_init(RRE((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pytinitialize_instancewscCs[xGt|jj��D]0}tjd|j|jf�|j|�qW|jj�dS(sDestroy all instances.sdestroying instance %s (%s)N(tlistRtvaluesR1RJR,RHtclear(RRE((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyR#|scCs|j|�|j|�dS(N(trelease_devicest_instance_cleanup(RRE((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyRH�s
cCs
t��dS(N(tNotImplementedError(RRE((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyRK�scCs
t��dS(N(RR(RRE((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyRQ�scCs%t|_t�|_t�|_dS(N(Rt_devices_supportedtsett_assigned_devicest
_free_devices(R((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyR%�s cCsdS(s�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 itN(tNone(Rtdevices((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt_get_device_objects�scCs�|jdkr+t|jj|j|��S|j|�}|dkr`tjd|j �t�S|j
j|j|�}tg|D]}|j^q��SdS(Ns<Plugin '%s' does not support the 'devices_udev_regex' option(RARWRTR
t
match_listR@RYR1terrorR,Rtsys_name(RRERXtudev_devicestx((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt_get_matching_devices�scCs�|js
dStjd|j�|j||j�}t|�dk|_|jsktjd|j�n}|j}|j|jkr�|d|j7}ntj d|dj
|�f�|jj|�|j
|O_
|j|8_dS(Ns assigning devices to instance %sis*instance %s: no matching devices availables (%s)s!instance %s: assigning devices %ss, (RSR1RJR,R_RVtlentactiveR2tinfotjointassigned_devicestupdateRU(RREt to_assignR,((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pytassign_free_devices�s cCsi|js
dS|j|jB|j@}t|_|jj�|jj�|j|8_|j|O_dS(N(RStprocessed_devicesRdRURRaRORV(RREt
to_release((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyRP�s
cCs7|jsdg}nx|D]}|||�qWdS(N(RSRW(RREtcallbackRXtdevice((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt_run_for_each_device�s
cCsdS(N((RREtenabling((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt_instance_pre_static�scCsdS(N((RRERm((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt_instance_post_static�scCs�|dkrdSt|�dkr@tjd|j|f�dS|jd�sdtjdd�tStj j
|�}t}xR|D]J}tj} | j
|jj��|g}
|r�|
jd�n|
j|�tjd|t|
�f�tjdtt| j����y�t|g|
d td
tdtd| d
|dt�}|j�\}}
|jr�tjd||j|
d f�t}nWq�ttfk
r�}tjd||f�t}q�Xq�W|S(Nis1Instance '%s': no device to call script '%s' for.t/s<Relative paths cannot be used in script_pre or script_post. sUse ${i:PROFILE_DIR}.t
full_rollbacks'calling script '%s' with arguments '%s'susing environment '%s'tstdouttstderrt close_fdstenvtcwdtuniversal_newlinessscript '%s' error: %d, '%s'i����sscript '%s' error: %s(RWR`R1R2R,t
startswithR[RtostpathtdirnameR&tenvironReRtget_envtappendRbR;RJRMtitemsRRtcommunicatet
returncodetOSErrortIOError(RREtscripttopRXRqtdir_nametrettdevR|t argumentstproctoutterrte((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt_call_device_script�sB
# !
cCs�|js
dS|jr~|j||jd|j�|j|t�|j|�|j|t�|j||j d|j�n|j
r�|jjt
jt
j�r�|j||j|j�n|jj|j�|jj�dS(sG
Apply static and dynamic tuning if the plugin instance is active.
Ntapply(Rathas_static_tuningR�RBRdRnR&t_instance_apply_staticRoRCthas_dynamic_tuningRtgettconststCFG_DYNAMIC_TUNINGtCFG_DEF_DYNAMIC_TUNINGRlt_instance_apply_dynamicRhReRO(RRE((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pytinstance_apply_tuning�s
$cCs�|js
dSt|j�dkrBtjddj|j��n|jj�}|j r�|j
||jd|�tkrtS|j
|||�tkr�tS|j
||jd|�tkr�tStSdSdS(s<
Verify static tuning if the plugin instance is active.
is)BUG: Some devices have not been tuned: %ss, tverifyN(RaRWR`RdR1R[RcRhR0R�R�RBRt_instance_verify_staticRCR&(RREtignore_missingRX((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pytinstance_verify_tunings !!cCsW|js
dS|jrS|jjtjtj�rS|j||j|j j
��ndS(s<
Apply dynamic tuning if the plugin instance is active.
N(RaR�RR�R�R�R�Rlt_instance_update_dynamicRhR0(RRE((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pytinstance_update_tuning$s $cCs�|jr@|jjtjtj�r@|j||j|j�n|j r�|j
||jd|jd|�|j|t
�|j||�|j|t
�|j
||jd|jd|�ndS(s8
Remove all tunings applied by the plugin instance.
tunapplyRqN(R�RR�R�R�R�Rlt_instance_unapply_dynamicRhR�R�RCRnRt_instance_unapply_staticRoRB(RRERq((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pytinstance_unapply_tuning-s$ cCs$|j|�|j||j�dS(N(t _execute_all_non_device_commandst_execute_all_device_commandsRd(RRE((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyR�<s
cCsOt}|j||�tkr't}n|j|||�tkrKt}n|S(N(R&t_verify_all_non_device_commandsRt_verify_all_device_commands(RRER�RXR�((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyR�@s cCs$|j||j�|j|�dS(N(t_cleanup_all_device_commandsRht _cleanup_all_non_device_commands(RRERq((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyR�Hs
cCswx`g|jD].}|j||j||�dkr
|^q
D] }|j||j||�q?W|j||�dS(N(Rt_storage_gett _commandsRWt_check_and_save_valueR�(RRERktopttoption((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyR�MsEcCs
t��dS(N(RR(RRERk((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyR�SscCs
t��dS(N(RR(RRERk((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyR�VscCs'tj�|_|j�|j�dS(s
Initialize commands.
N(R
RR�t_autoregister_commandst_check_commands(R((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyR]s
cCs\x%|jjD]}|jd�r(q
nt||�}t|d�sLq
n|jd}|jj|i|d6�}d|jkr�d|d<||d<|jd|d<|jd|d<nZd|jkr�||d<n>d|jkr||d<|jd|d<|jd|d<n||j|<q
Wt j
tt|jj
��d d
���|_dS(sd
Register all commands marked using @command_set, @command_get, and @command_custom decorators.
t__t_commandR,RTtcustomt
per_devicetpriorityR�R5cSs|ddS(NiR�((t name_info((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt<lambda>�sN(Rt__dict__RxtgetattrthasattrR�R�R�RWR
RtsortedtiterR(Rtmember_nametmembertcommand_nameRb((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyR�es*
cCsmxft|jj��D]O\}}|jdt�r:qnd|ksRd|krtd|��qqWdS(s2
Check if all commands are defined correctly.
R�R�RTs,Plugin command '%s' is not defined correctlyN(RMR�RR�Rt TypeError(RR�tcommand((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyR��s
"cCskt|�j}|dkr!dn|}|dkr9dn|}|dkrQdn|}d||||fS(Nts%s/%s/%s/%s(R9RRW(Rt
instance_nameR�tdevice_namet
class_name((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt_storage_key�s cCs3|j|j|d|�}|jj||�dS(NR,(R�R,RRT(RRER�R=R�R5((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt_storage_set�scCs,|j|j|d|�}|jj|�S(NR,(R�R,RR�(RRER�R�R5((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyR��scCs,|j|j|d|�}|jj|�S(NR,(R�R,Rtunset(RRER�R�R5((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt_storage_unset�scCs�x�gt|jj��D]}|ds|^qD]M}|jj|jj|dd��}|dk r3|j|||�q3q3WdS(NR�R,( RMR�RNRtexpandR3R�RWt_execute_non_device_command(RRER�t new_value((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyR��s9%cCs�x�gt|jj��D]}|dr|^qD]d}|jj|jj|dd��}|dkrpq3nx$|D]}|j||||�qwWq3WdS(NR�R,( RMR�RNRR�R3R�RWt_execute_device_command(RRERXR�R�Rk((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyR��s9%
cCs�t}x�gt|jj��D]}|ds|^qD]a}|jj|jj|dd��}|dk r9|j ||||�t
kr�t
}q�q9q9W|S(NR�R,(R&RMR�RNRR�R3R�RWt_verify_non_device_commandR(RRER�R�R�R�((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyR��s9%cCs�t}x�gt|jj��D]}|dr|^qD]l}|jj|dd�}|dkrjq9nx8|D]0}|j|||||�tkrqt}qqqqWq9W|S(NR�R,( R&RMR�RNR3R�RWt_verify_device_commandR(RRERXR�R�R�R�Rk((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyR��s9
!cCs�|dk r�t|�}t|�dkr.|S|d }|d}|dkrb|dkr^|S|Syb|dkr�t|�t|�kr�|SdSn/|dkr�t|�t|�kr�|SdSnWq�tk
r�tjd||||f�q�Xn|S(Nit<t>shcannot compare new value '%s' with current value '%s' by operator '%s', using '%s' directly as new value(R�R�(RWR;R`tintt
ValueErrorR1R2(RR�t
current_valuetnwsR�tval((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt_process_assignment_modifiers�s(
$cCs/|dk r |d|d|�S|d�SdS(NR�R�(RW(RR�RkR�((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt_get_current_value�scCsY|j||�}|j||�}|dk rU|dk rU|j||||�n|S(N(R�R�RWR�(RRER�RkR�R�((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyR��s
cCso|ddk r-|dt||tt�n>|j||||�}|dk rk|d||dt�ndS(NR�RTtsim(RWR&RR�(RRER�RkR�((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyR��s
cCsi|ddk r*|dt|tt�n;|j||d|�}|dk re|d|dt�ndS(NR�RTR�(RWR&RR�(RRER�R�((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyR��s
cCsA|jjt|��}tjd|�r=tjdd|�S|S(Ns\s*(0+,?)+([\da-fA-F]*,?)*\s*$s^\s*(0+,?)+R�(RtunquoteR;tretmatchtsub(RR=tv((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt_norm_valuesc Cs�|dkrdSt}|dkri|ri|dkrKtjtj|�ntjtj||f�tS|dk re|j|�}|j|�}yt |�t |�k}Wqet
k
ray"t |d�t |d�k}Wqbt
k
r]t|�t|�k}|s^t|�jd�}x3|D](}|j
�}||k}|r+Pq+q+Wq^qbXqeXn|j||||d|�|S(Nit|Rk(RWRR1RbR�t STR_VERIFY_PROFILE_VALUE_MISSINGt'STR_VERIFY_PROFILE_DEVICE_VALUE_MISSINGR&R�R�R�R;R+tstript_log_verification_result( RR,R�R�R�RkR�tvalsR�((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt
_verify_value
s8
"
cCs�|rh|dkr;tjtj|t|�j�f�n)tjtj||t|�j�f�tS|dkr�tj tj
|t|�j�t|�j�f�n8tj tj||t|�j�t|�j�f�tSdS(N(
RWR1RbR�tSTR_VERIFY_PROFILE_VALUE_OKR;R�t"STR_VERIFY_PROFILE_DEVICE_VALUE_OKR&R[tSTR_VERIFY_PROFILE_VALUE_FAILt$STR_VERIFY_PROFILE_DEVICE_VALUE_FAILR(RR,tsuccessR�R�Rk((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyR�*s))88cCs�|ddk r*|dt||t|�S|j||d|�}|j||�}|dkrddS|d||t�}|j|d||||�S(NR�R�RTR,(RWR&R�R�R�(RRER�RkR�R�R�((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyR�9scCs�|ddk r'|dt|t|�S|j|�}|j||�}|dkrXdS|d|t�}|j|d|||�S(NR�RTR,(RWR&R�R�R�(RRER�R�R�R�((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyR�CscCs�x�tgt|jj��D]}|ds|^q�D]K}|jj|dd�dk sq|d|jkr9|j||�q9q9WdS(NR�R,( treversedRMR�RNR3R�RWRt_cleanup_non_device_command(RRER�((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyR�Ms?2cCs�x�tgt|jj��D]}|dr|^q�D]_}|jj|dd�dk sq|d|jkr9x$|D]}|j|||�qxWq9q9WdS(NR�R,( R�RMR�RNR3R�RWRt_cleanup_device_command(RRERXR�Rk((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyR�Rs?2
cCs|ddk r-|dtd|tt�nN|j|||�}|dk rh|d||dt�n|j|||�dS(NR�RTR�(RWRR�R�(RRER�Rkt old_value((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyR�XscCss|ddk r*|dtdtt�nE|j||�}|dk r_|d|dt�n|j||�dS(NR�RTR�(RWRR�R�(RRER�R�((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyR�asN(CRR*t__doc__R"R$R'tpropertyR,tclassmethodR-R/RR6R>RFRIRLR#RHRKRQR%RYR_RgRPRlRnRoRR�R�R�R�R�R�R�R�R�R�R�RR�R�RWR�R�R�R�R�R�R�R�R�R�R�R�R�R�R�R�R�R�R�R�R�R�(((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyRs|
# !
(R�ttuned.constsR�ttuned.profiles.variablesttunedt
tuned.logsR
ttuned.utils.commandsRRyt
subprocessRRtlogsR�R1tobjectR(((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt<module>s |