\c@sddlZddljZddlZddlZddlZddlmZddl Z ddl m Z m Z ej jZdefdYZdS(iN(tcommands(tPopentPIPEtPlugincBsoeZdZdZdZdZedZedZ edZ edZ dZ d Z d Zd Zd Zd ZdZdZdZdZdZdZdZdZdZdZdZedZdZdZ dZ!edZ"dZ#dZ$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=ed0Z6d=d=d1Z7d2Z8d3Z9d4Z:d=d5Z;d=d6Z<d7Z=d8Z>d9Z?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_factoryt collectionst OrderedDictt _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|jdS(N(tdestroy_instances(R((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pytcleanup,scCs#|js|jt|_ndS(N(Rt _init_devicestTrue(R((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt init_devices/s  cCs'|jjjddjdddS(Nt.it_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|jj}xQ|D]I}||ks4|jrE||||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 ExceptionR6R R( RR,tdevices_expressiontdevices_udev_regext script_pret script_postR3teffective_optionstinstance((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pytcreate_instance`s cCs{|j|kr(td||fn|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|jjD]0}tjd|j|jf|j|qW|jjdS(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.pyRHs cCs tdS(N(tNotImplementedError(RRE((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyRKscCs tdS(N(RR(RRE((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyRQscCs%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(sOverride 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_objectsscCs|jdkr+t|jj|j|S|j|}|dkr`tjd|j tS|j j|j|}tg|D]}|j ^qSdS(Ns<Plugin '%s' does not support the 'devices_udev_regex' option( RARWRTR t match_listR@RYR1terrorR,R tsys_name(RRERXt udev_devicestx((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt_get_matching_devicess cCs|js dStjd|j|j||j}t|dk|_|jsktjd|jn}|j}|j|jkr|d|j7}ntj d|dj |f|j j ||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_devicess    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.pyRPs     cCs7|jsdg}nx|D]}|||qWdS(N(RSRW(RREtcallbackRXtdevice((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt_run_for_each_devices   cCsdS(N((RREtenabling((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt_instance_pre_staticscCsdS(N((RRERm((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt_instance_post_staticscCs|dkrdSt|dkr@tjd|j|fdS|jdsdtjddtStj j |}t }xR|D]J}tj } | j |jj|g} |r| jdn| j|tjd|t| ftjdtt| jyt|g| d td td t d | d |dt } | j\} } | jrtjd|| j| d ft}nWqttfk r}tjd||ft}qXqW|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'isscript '%s' error: %s(RWR`R1R2R,t startswithR[RtostpathtdirnameR&tenvironReRtget_envtappendRbR;RJRMtitemsRRt communicatet returncodetOSErrortIOError(RREtscripttopRXRqtdir_nametrettdevR|t argumentstproctoutterrte((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt_call_device_scriptsB      #   ! cCs|js dS|jr~|j||jd|j|j|t|j||j|t|j||j d|jn|j r|j j t jt jr|j||j|jn|jj|j|jjdS(sG Apply static and dynamic tuning if the plugin instance is active. Ntapply(Rathas_static_tuningRRBRdRnR&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_tunings    $cCs|js dSt|jdkrBtjddj|jn|jj}|j r|j ||j d|t krt S|j |||t krt S|j ||jd|t krt StSdSdS(s< Verify static tuning if the plugin instance is active. is)BUG: Some devices have not been tuned: %ss, tverifyN(RaRWR`RdR1R[RcRhR0RRRBRt_instance_verify_staticRCR&(RREtignore_missingRX((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pytinstance_verify_tunings   !!cCsW|js dS|jrS|jjtjtjrS|j||j|j j ndS(s< Apply dynamic tuning if the plugin instance is active. N( RaRRRRRRRlt_instance_update_dynamicRhR0(RRE((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pytinstance_update_tuning$s $cCs|jr@|jjtjtjr@|j||j|jn|j r|j ||j d|jd||j |t |j|||j|t |j ||jd|jd|ndS(s8 Remove all tunings applied by the plugin instance. tunapplyRqN(RRRRRRRlt_instance_unapply_dynamicRhRRRCRnRt_instance_unapply_staticRoRB(RRERq((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pytinstance_unapply_tuning-s$  cCs$|j||j||jdS(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(RRERRXR((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.pyRHs  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.pyRMsEcCs tdS(N(RR(RRERk((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyRSscCs tdS(N(RR(RRERk((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyRVscCs'tj|_|j|jdS(s Initialize commands. N(R RRt_autoregister_commandst_check_commands(R((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyR]s cCs\x%|jjD]}|jdr(q nt||}t|dsLq n|jd}|jj|i|d6}d|jkrd |d<||d<|jd|d<|jd|dd|jkr||d<|jd|d<|jd|dsN(Rt__dict__RxtgetattrthasattrRRRRWR RtsortedtiterR(Rt member_nametmembert command_nameRb((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyRes*     cCsmxft|jjD]O\}}|jdtr:qnd|ksRd|krtd|qqWdS(s2 Check if all commands are defined correctly. RRRTs,Plugin command '%s' is not defined correctlyN(RMRRRRt TypeError(RRtcommand((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyRs "cCskt|j}|dkr!dn|}|dkr9dn|}|dkrQdn|}d||||fS(Nts %s/%s/%s/%s(R9RRW(Rt instance_nameRt device_namet class_name((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt _storage_keys  cCs3|j|j|d|}|jj||dS(NR,(RR,RRT(RRERR=RR5((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt _storage_setscCs,|j|j|d|}|jj|S(NR,(RR,RR(RRERRR5((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyRscCs,|j|j|d|}|jj|S(NR,(RR,Rtunset(RRERRR5((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt_storage_unsetscCsxgt|jjD]}|ds|^qD]M}|jj|jj|dd}|dk r3|j|||q3q3WdS(NRR,( RMRRNRtexpandR3RRWt_execute_non_device_command(RRERt new_value((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyRs9% cCsxgt|jjD]}|dr|^qD]d}|jj|jj|dd}|dkrpq3nx$|D]}|j||||qwWq3WdS(NRR,( RMRRNRRR3RRWt_execute_device_command(RRERXRRRk((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyRs 9%  cCst}xgt|jjD]}|ds|^qD]a}|jj|jj|dd}|dk r9|j ||||t krt }qq9q9W|S(NRR,( R&RMRRNRRR3RRWt_verify_non_device_commandR(RRERRRR((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyRs9% cCst}xgt|jjD]}|dr|^qD]l}|jj|dd}|dkrjq9nx8|D]0}|j|||||tkrqt}qqqqWq9W|S(NRR,( R&RMRRNR3RRWt_verify_device_commandR(RRERXRRRRRk((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyRs9  !cCs|dk rt|}t|dkr.|S|d }|d}|dkrb|dkr^|S|Syb|dkrt|t|kr|SdSn/|dkrt|t|kr|SdSnWqtk rtjd||||fqXn|S(Nitshcannot compare new value '%s' with current value '%s' by operator '%s', using '%s' directly as new value(RR(RWR;R`tintt ValueErrorR1R2(RRt current_valuetnwsRtval((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt_process_assignment_modifierss(         $cCs/|dk r |d|d|S|dSdS(NRR(RW(RRRkR((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt_get_current_values cCsY|j||}|j||}|dk rU|dk rU|j||||n|S(N(RRRWR(RRERRkRR((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyRs cCso|ddk r-|dt||ttn>|j||||}|dk rk|d||dtndS(NRRTtsim(RWR&RR(RRERRkR((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyRs  cCsi|ddk r*|dt|ttn;|j||d|}|dk re|d|dtndS(NRRTR(RWR&RR(RRERR((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyRs  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||ftS|dk re|j|}|j|}yt |t |k}Wqet k ray"t |dt |dk}Wqbt k r]t |t |k}|s^t |j d}x3|D](}|j }||k}|r+Pq+q+Wq^qbXqeXn|j||||d||S(Nit|Rk(RWRR1RbRt STR_VERIFY_PROFILE_VALUE_MISSINGt'STR_VERIFY_PROFILE_DEVICE_VALUE_MISSINGR&RRRR;R+tstript_log_verification_result( RR,RRRRkRtvalsR((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyt _verify_value s8    "     cCs|rh|dkr;tjtj|t|jfn)tjtj||t|jftS|dkrtj tj |t|jt|jfn8tj tj ||t|jt|jft SdS(N( RWR1RbRtSTR_VERIFY_PROFILE_VALUE_OKR;Rt"STR_VERIFY_PROFILE_DEVICE_VALUE_OKR&R[tSTR_VERIFY_PROFILE_VALUE_FAILt$STR_VERIFY_PROFILE_DEVICE_VALUE_FAILR(RR,tsuccessRRRk((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(NRRRTR,(RWR&RRR(RRERRkRRR((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyR9s cCs|ddk r'|dt|t|S|j|}|j||}|dkrXdS|d|t}|j|d|||S(NRRTR,(RWR&RRR(RRERRRR((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyRCs cCsxtgt|jjD]}|ds|^qD]K}|jj|dddk sq|d|jkr9|j||q9q9WdS(NRR,( treversedRMRRNR3RRWRt_cleanup_non_device_command(RRER((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyRMs?2cCsxtgt|jjD]}|dr|^qD]_}|jj|dddk sq|d|jkr9x$|D]}|j|||qxWq9q9WdS(NRR,( RRMRRNR3RRWRt_cleanup_device_command(RRERXRRk((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyRRs?2 cCs|ddk r-|dtd|ttnN|j|||}|dk rh|d||dtn|j|||dS(NRRTR(RWRRR(RRERRkt old_value((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyRXs  cCss|ddk r*|dtdttnE|j||}|dk r_|d|dtn|j||dS(NRRTR(RWRRR(RRERR((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyRas  N(CRR*t__doc__R"R$R'tpropertyR,t classmethodR-R/RR6R>RFRIRLR#RHRKRQR%RYR_RgRPRlRnRoRRRRRRRRRRRRRRRRWRRRRRRRRRRRRRRRRRRRRRR(((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyR s|                #                       !    (Rt tuned.constsRttuned.profiles.variablesttunedt tuned.logsR ttuned.utils.commandsRRyt subprocessRRtlogsRR1tobjectR(((s6/usr/lib/python2.7/site-packages/tuned/plugins/base.pyts