3 d?Z&ejfd@dAZ'dBdCZ(dDdEZ)dFdGZ*dHdIZ+dJdKZ,dLdMZ-d|dOdPZ.d}dQdRZ/d~dSdTZ0ddUdVZ1dWdXZ2dYdZZ3d[d\Z4d]d^Z5d_d`Z6ddbdcZ7ddddeZ8dfdgZ9dhdiZ:djdkZ;ddldmZdrdsZ?dtduZ@ddvdwZAddxdyZBdzd{ZCdNS)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|j|jj|_||_||_||_||_||_t j |_ |j ||_ ||_d|_d|_|j|_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_cfg variablesr/usr/lib/python3.6/base.py__init__s  zPlugin.__init__cCs |jdS)N)destroy_instances)rrrrcleanup,szPlugin.cleanupcCs|js|jd|_dS)NT)r _init_devices)rrrr init_devices/szPlugin.init_devicescCs|jjjddjdddS)N._)r __module__split)rrrrname4sz Plugin.namecCsiS)z-Default configuration options for the plugin.r)rrrr_get_config_options<szPlugin._get_config_optionscCsiS)z*Explanation of each config option functionr)clsrrrget_config_options_hintsAszPlugin.get_config_options_hintscCsgS)znList of config options used by dynamic tuning. Their previous values will be automatically saved and restored.r)rrrrrFsz*Plugin._get_config_options_used_by_dynamiccCsP|jj}x>|D]6}||ks$|jr2||||<qtjd||jjfqW|S)z3Merge provided options with plugin default options.z$Unknown option '%s' for plugin '%s'.)r(copyrlogwarnrr)roptionsZ effectivekeyrrr_get_effective_optionsKs   zPlugin._get_effective_optionscCs,t|tkr|St|j}|dkp*|dkS)Ntrue1)typeboolstrlower)rvaluerrr _option_boolVs  zPlugin._option_boolc CsF||jkrtd||j|}|jj|||||||}||j|<|S)z8Create new instance of the plugin and seize the devices.z.Plugin instance with name '%s' already exists.)r Exceptionr0r r) rr'devices_expressiondevices_udev_regex script_pre script_postr.Zeffective_optionsinstancerrrcreate_instance`s     zPlugin.create_instancecCsV|j|krtd||f|j|jkr2td||j|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_pluginr9r'r_destroy_instance)rr>rrrdestroy_instancels     zPlugin.destroy_instancecCs$tjd|j|jf|j|dS)zInitialize an instance.zinitializing instance %s (%s)N)r,debugr'_instance_init)rr>rrrinitialize_instancewszPlugin.initialize_instancecCsFx6t|jjD]$}tjd|j|jf|j|qW|jjdS)zDestroy all instances.zdestroying instance %s (%s)N)listrvaluesr,rBr'r@clear)rr>rrrr|szPlugin.destroy_instancescCs|j||j|dS)N)release_devices_instance_cleanup)rr>rrrr@s zPlugin._destroy_instancecCs tdS)N)NotImplementedError)rr>rrrrCszPlugin._instance_initcCs tdS)N)rJ)rr>rrrrIszPlugin._instance_cleanupcCsd|_t|_t|_dS)NF)_devices_supportedset_assigned_devices _free_devices)rrrrrszPlugin._init_devicescCsdS)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)rdevicesrrr_get_device_objectsszPlugin._get_device_objectscCsj|jdkrt|jj|j|S|j|}|dkrDtjd|jtS|j j|j|}tdd|DSdS)Nzsz0Plugin._get_matching_devices..) r;rLr Z match_listr:rPr,errorr'r )rr>rOZ udev_devicesrrr_get_matching_devicess  zPlugin._get_matching_devicescCs|js dStjd|j|j||j}t|dk|_|jsNtjd|jn`|j}|j|jkrn|d|j7}tj d|dj |f|j j ||j |O_ |j|8_dS)Nz assigning devices to instance %srz*instance %s: no matching devices availablez (%s)z!instance %s: assigning devices %sz, )rKr,rBr'rUrNlenactiver-infojoinassigned_devicesupdaterM)rr>Z to_assignr'rrrassign_free_devicess  zPlugin.assign_free_devicescCsV|js dS|j|jB|j@}d|_|jj|jj|j|8_|j|O_dS)NF)rKprocessed_devicesrZrMrWrGrN)rr>Z to_releaserrrrHs  zPlugin.release_devicescCs(|js dg}x|D]}|||qWdS)N)rK)rr>callbackrOdevicerrr_run_for_each_devices zPlugin._run_for_each_devicecCsdS)Nr)rr>enablingrrr_instance_pre_staticszPlugin._instance_pre_staticcCsdS)Nr)rr>rarrr_instance_post_staticszPlugin._instance_post_staticcCsn|dkr dSt|dkr0tjd|j|fdS|jdsHtjddStjj|}d}x|D]}tj } | j |j j |g} |t jkr| jd| j|tjd |t| ftjd tt| jyVt|g| ttd| |dd } | j\} } | jr$tjd || j| ddfd}Wq`ttfk rd}ztjd||fd}WYdd}~Xq`Xq`W|S)Nrz1Instance '%s': no device to call script '%s' for./zZscriptoprOrollbackZdir_nameretZdevrmZ argumentsprocouterrerrr_call_device_scriptsB         zPlugin._call_device_scriptcCs|js dS|jrZ|j||jd|j|j|d|j||j|d|j||jd|j|j r|j j t j t jr|j||j|j|jj|j|jjdS)zG Apply static and dynamic tuning if the plugin instance is active. NZapplyT)rWhas_static_tuningr{r<rZrb_instance_apply_staticrcr=has_dynamic_tuningrgetrnCFG_DYNAMIC_TUNINGCFG_DEF_DYNAMIC_TUNINGr`_instance_apply_dynamicr]r[rG)rr>rrrinstance_apply_tunings       zPlugin.instance_apply_tuningcCs|js dSt|jdkr.tjddj|j|jj}|jr|j ||j d|dkrXdS|j |||dkrndS|j ||j d|dkrdSdSdSdS)z< Verify static tuning if the plugin instance is active. Nrz)BUG: Some devices have not been tuned: %sz, ZverifyFT) rWrVrZr,rTrYr]r+r|r{r<_instance_verify_staticr=)rr>ignore_missingrOrrrinstance_verify_tunings zPlugin.instance_verify_tuningcCs<|js dS|jr8|jjtjtjr8|j||j|j j dS)z< Apply dynamic tuning if the plugin instance is active. N) rWr~rrrnrrr`_instance_update_dynamicr]r+)rr>rrrinstance_update_tuning$szPlugin.instance_update_tuningcCs|tjkrdS|jr8|jjtjtjr8|j||j|j |j r|j ||j d|j |d|j |d|j|||j|d|j ||jd|j |ddS)z8 Remove all tunings applied by the plugin instance. NZunapply)ruF)rnZ ROLLBACK_NONEr~rrrrr`_instance_unapply_dynamicr]r|r{r=rb_instance_unapply_staticrcr<)rr>rurrrinstance_unapply_tuning-s     zPlugin.instance_unapply_tuningcCs|j||j||jdS)N) _execute_all_non_device_commands_execute_all_device_commandsrZ)rr>rrrr}?s zPlugin._instance_apply_staticcCs2d}|j||dkrd}|j|||dkr.d}|S)NTF)_verify_all_non_device_commands_verify_all_device_commands)rr>rrOrvrrrrCs zPlugin._instance_verify_staticcCs|j||j|j|dS)N)_cleanup_all_device_commandsr] _cleanup_all_non_device_commands)rr>rurrrrKszPlugin._instance_unapply_staticcsFx4fddjDD]}jj|qWjdS)Ncs(g|] }jj|dkr|qS)N) _storage_get _commands)rQopt)r_r>rrrrSQsz2Plugin._instance_apply_dynamic..)r_check_and_save_valuerr)rr>r_Zoptionr)r_r>rrrPszPlugin._instance_apply_dynamiccCs tdS)N)rJ)rr>r_rrrrVsz Plugin._instance_unapply_dynamiccCs tdS)N)rJ)rr>r_rrrrYszPlugin._instance_update_dynamiccCstj|_|j|jdS)z Initialize commands. N)r rr_autoregister_commands_check_commands)rrrrr`s zPlugin._init_commandscCsx|jjD]}|jdrq t||}t|ds0q |jd}|jj|d|i}d|jkrd|d<||d<|jd|d<|jd|d<nBd |jkr||d <n.d|jkr||d<|jd|d<|jd|d<||j|<q Wtj t t |jj d d d |_dS) zd Register all commands marked using @command_set, @command_get, and @command_custom decorators. ___commandr'rLNcustom per_devicepriorityrcSs |ddS)Nr"rr)Z name_inforrrsz/Plugin._autoregister_commands..)r/) r__dict__rigetattrhasattrrrrr rsortediterrp)r member_namemember command_namerXrrrrhs*        zPlugin._autoregister_commandscCsJxDt|jjD]2\}}|jddr&qd|ks6d|krtd|qWdS)z2 Check if all commands are defined correctly. rFrrLz,Plugin command '%s' is not defined correctlyN)rErrpr TypeError)rrcommandrrrrs  zPlugin._check_commandsNcCsJt|j}|dkrdn|}|dkr&dn|}|dkr6dn|}d||||fS)Nz %s/%s/%s/%s)r3r)rZ instance_namer device_name class_namerrr _storage_keys  zPlugin._storage_keycCs&|j|j|d|}|jj||dS)Nr')rr'r rL)rr>rr7rr/rrr _storage_setszPlugin._storage_setcCs |j|j|d|}|jj|S)Nr')rr'r r)rr>rrr/rrrrszPlugin._storage_getcCs |j|j|d|}|jj|S)Nr')rr'r Zunset)rr>rrr/rrr_storage_unsetszPlugin._storage_unsetcCsVxPddt|jjDD]4}|jj|jj|dd}|dk r|j|||qWdS)NcSsg|]}|ds|qS)rr)rQrrrrrSsz;Plugin._execute_all_non_device_commands..r')rErrFrexpandr.r_execute_non_device_command)rr>r new_valuerrrrsz'Plugin._execute_all_non_device_commandscCshxbddt|jjDD]F}|jj|jj|dd}|dkrBqx|D]}|j||||qHWqWdS)NcSsg|]}|dr|qS)rr)rQrrrrrSsz7Plugin._execute_all_device_commands..r')rErrFrrr.r_execute_device_command)rr>rOrrr_rrrrs  z#Plugin._execute_all_device_commandscCsdd}xZddt|jjDD]>}|jj|jj|dd}|dk r|j||||dkrd}qW|S)NTcSsg|]}|ds|qS)rr)rQrrrrrSsz:Plugin._verify_all_non_device_commands..r'F)rErrFrrr.r_verify_non_device_command)rr>rrvrrrrrrsz&Plugin._verify_all_non_device_commandscCsnd}xdddt|jjDD]H}|jj|dd}|dkr>qx&|D]}|j|||||dkrDd}qDWqW|S)NTcSsg|]}|dr|qS)rr)rQrrrrrSsz6Plugin._verify_all_device_commands..r'F)rErrFr.r_verify_device_command)rr>rOrrvrrr_rrrrs  z"Plugin._verify_all_device_commandsc Cs|dk rt|}t|dkr |S|dd}|dd}|dkrP|dkrL|S|SyF|dkrtt|t|krn|SdSn |dkrt|t|kr|SdSWn*tk rtjd||||fYnX|S)Nr"<>zhcannot compare new value '%s' with current value '%s' by operator '%s', using '%s' directly as new value)rr)r5rVint ValueErrorr,r-)rr current_valueZnwsrtvalrrr_process_assignment_modifierss(   z$Plugin._process_assignment_modifiersFcCs&|dk r|d||dS|dSdS)Nr)rr)rrr_rrrr_get_current_valueszPlugin._get_current_valuecCs<|j||}|j||}|dk r8|dk r8|j|||||S)N)rrr)rr>rr_rrrrrrs   zPlugin._check_and_save_valuecCsR|ddk r"|dd||ddn,|j||||}|dk rN|d||ddddS)NrTFrL)simremove)r)rr>rr_rrrrrs  zPlugin._execute_device_commandcCsN|ddk r |dd|ddn*|j||d|}|dk rJ|d|ddddS)NrTFrL)rr)r)rr>rrrrrrs  z"Plugin._execute_non_device_commandcCs.|jjt|}tjd|r*tjdd|S|S)Nz\s*(0+,?)+([\da-fA-F]*,?)*\s*$z ^\s*(0+,?)+r)rZunquoter5rematchsub)rr7vrrr _norm_values zPlugin._norm_valuec Cs(|dkr dSd}|dkrN|rN|dkr6tjtj|ntjtj||fdS|dk r|j|}|j|}yt|t|k}Wntk ryt|dt|dk}Wn^tk rt|t|k}|st|j d}x"|D]}|j }||k}|rPqWYnXYnX|j |||||d|S)NFT|)r_) r,rXrnZ STR_VERIFY_PROFILE_VALUE_MISSINGZ'STR_VERIFY_PROFILE_DEVICE_VALUE_MISSINGrrrr5r&strip_log_verification_result) rr'rrrr_rvvalsrrrr _verify_value s8       zPlugin._verify_valuecCs|rL|dkr*tjtj|t|jfntjtj||t|jfdS|dkr|tjtj|t|jt|jfn(tjtj ||t|jt|jfdSdS)NTF) r,rXrnZSTR_VERIFY_PROFILE_VALUE_OKr5rZ"STR_VERIFY_PROFILE_DEVICE_VALUE_OKrTZSTR_VERIFY_PROFILE_VALUE_FAILZ$STR_VERIFY_PROFILE_DEVICE_VALUE_FAIL)rr'successrrr_rrrr-s((zPlugin._log_verification_resultcCsp|ddk r |dd||d|S|j|||d}|j||}|dkrHdS|d||dd}|j|d||||S)NrT)rrLFr')rrr)rr>rr_rrrrrrr<s  zPlugin._verify_device_commandcCsd|ddk r|dd|d|S|j|}|j||}|dkr@dS|d|dd}|j|d|||S)NrTrLFr')rrr)rr>rrrrrrrrFs   z!Plugin._verify_non_device_commandcCsZxTtddt|jjDD]4}|jj|dddk sF|d|jkr|j||qWdS)NcSsg|]}|ds|qS)rr)rQrrrrrSQsz;Plugin._cleanup_all_non_device_commands..r')reversedrErrFr.rr_cleanup_non_device_command)rr>rrrrrPs"$z'Plugin._cleanup_all_non_device_commandscCslxftddt|jjDD]F}|jj|dddk sF|d|jkrx|D]}|j||||qLWqWdS)NcSsg|]}|dr|qS)rr)rQrrrrrSVsz7Plugin._cleanup_all_device_commands..r')rrErrFr.rr_cleanup_device_command)rr>rOrrr_rrrrUs"$ z#Plugin._cleanup_all_device_commandscCs^|ddk r"|ddd|ddn8|j|||}|dk rL|d||d|d|j|||dS)NrFrL)rr)rr)rr>rr_r old_valuerrrr[s  zPlugin._cleanup_device_commandcCsV|ddk r |dddddn2|j||}|dk rF|d|ddd|j||dS)NrFrL)rr)rr)rr>rrrrrrds   z"Plugin._cleanup_non_device_command)NNN)N)N)N)NF)NN)N)N)F)F)Drr% __qualname____doc__rrr propertyr' classmethodr(r*rr0r8r?rArDrr@rCrIrrPrUr\rHr`rbrcrnZ ROLLBACK_SOFTr{rrrrr}rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr s|         #          !     r)rZ tuned.constsrnZtuned.profiles.variablesZtunedZ tuned.logsr Ztuned.utils.commandsrrj subprocessrrZlogsrr,objectrrrrrs