a ”ˆ«høJã@sžddlmZddlZddlZddlmZddlZddlmZddlm Z ddl m Z ddl Z ddl Z dgZej ¡ZGdd„deƒZGd d„dejjjƒZdS) é)ÚexportsN)ÚTunedException)Úcommands)ÚhotplugÚ Controllerc@s,eZdZdd„Zdd„Zdd„Zdd„Zd S) Ú TimerStorecCstƒ|_t ¡|_dS©N)ÚdictÚ_timersÚ threadingÚLockÚ _timers_lock©Úself©rú;/usr/lib/python3.9/site-packages/tuned/daemon/controller.pyÚ__init__szTimerStore.__init__cCs4|j||j|<Wdƒn1s&0YdSr)r r ©rÚtokenÚtimerrrrÚ store_timerszTimerStore.store_timercCsV|j<z|j|}| ¡|j|=Wn Yn0Wdƒn1sH0YdSr)r r ÚcancelrrrrÚ drop_timers  zTimerStore.drop_timercCsL|j2|j ¡D] }| ¡q|j ¡Wdƒn1s>0YdSr)r r ÚvaluesrÚclear)rrrrrÚ cancel_all"s zTimerStore.cancel_allN)Ú__name__Ú __module__Ú __qualname__rrrrrrrrrs rcs¶eZdZdZ‡fdd„Zdd„Zdd„Zdd „Ze  d ¡d d „ƒZ d d„Z e  dd¡dZdd„ƒZ e  dd¡d[dd„ƒZe  dd¡d\dd„ƒZd]dd„Ze  dd¡d^dd„ƒZe  dd¡d_dd „ƒZd!d"„Ze  dd#¡d`d$d%„ƒZe  dd#¡dad&d'„ƒZe  dd¡dbd(d)„ƒZe  dd*¡dcd+d,„ƒZe  dd¡ddd-d.„ƒZe  dd¡ded/d0„ƒZe  dd¡dfd1d2„ƒZe  dd3¡dgd4d5„ƒZe  dd6¡dhd7d8„ƒZe  dd9¡did:d;„ƒZe  dd¡djdd?„ƒZe  dd¡dld@dA„ƒZ e  ddB¡dmdCdD„ƒZ!e  dd¡dndEdF„ƒZ"e  ddG¡dodHdI„ƒZ#e  dd¡dpdJdK„ƒZ$e  dLd#¡dqdMdN„ƒZ%e  ddO¡drdPdQ„ƒZ&e  ddR¡dsdSdT„ƒZ'e  dUd#¡dtdVdW„ƒZ(e  dd#¡dudXdY„ƒZ)‡Z*S)vrz‘ Controller's purpose is to keep the program running, start/stop the tuning, and export the controller interface (currently only over D-Bus). cs8tt|ƒ ¡||_||_t ¡|_tƒ|_ t ƒ|_ dSr) ÚsuperrrÚ_daemonÚ_global_configr ÚEventÚ _terminaterÚ_cmdrÚ _timer_store)rÚdaemonZ global_config©Ú __class__rrr.s  zController.__init__c CsŠt d¡|j ¡|j tjtj¡}|dkr"t d¡t j   t   ¡¡}|j  dtj¡}z| |¡Wnty„t d¡Yn0d}t ¡}zTt ¡||krÀ|j ¡sÀ|rÀ|jdd}q”|j ¡sä|rÚt d ¡n t d ¡Wn8ttfy}zt d |¡WYd }~n d }~00~| ¡}|j tjtj¡}|sP|rPt ¡|rt|j |jd¡stt ¡qVt d ¡| ¡d S)z1 Controller main loop. The call is blocking. zstarting controllerrzwaiting for udev to settleÚudev_buffer_sizez•cannot set udev monitor receive buffer size, we are probably running inside container or with limited capabilites, TuneD functionality may be limitedTé)Útimeoutzudev settle timed outz udev settledzudev settle failed, '%s'Nzterminating controller) ÚlogÚinfor#rr!Zget_intÚconstsZCFG_STARTUP_UDEV_SETTLE_WAITZ CFG_DEF_STARTUP_UDEV_SETTLE_WAITÚpyudevZMonitorZ from_netlinkZContextZget_sizeZCFG_DEF_UDEV_BUFFER_SIZEZset_receive_buffer_sizeÚEnvironmentErrorÚwarningÚtimeÚis_setZpollÚOSErrorÚIOErrorÚstartÚget_boolÚ CFG_DAEMONÚCFG_DEF_DAEMONrr$ÚwaitZ period_checkÚstop) rZ wait_settleZmonitorr)ÚpÚtÚeÚresr&rrrÚrun6sD   ÿ     $   zController.runcCs|j ¡dSr)r#ÚsetrrrrÚ terminatecszController.terminatecCs`|jj ¡r|jj ¡nBd}|r\|jj ¡|jj ¡| ¡sN|jj ¡|jj ¡}qdS)NT)r Z_sighup_processingr3Z_sighup_pendingrArÚreload)rZ do_reloadrrrÚsighupfs    zController.sighupZsbscCsdSrr)rÚ profile_nameÚresultZerrstrrrrÚprofile_changedrszController.profile_changedcCstj |¡|j |¡dSr©ÚtunedÚlogsÚlog_capture_finishr%r)rrrrrÚ_log_capture_abort{s zController._log_capture_abortÚiiÚsNcCsf|dkr dStj |¡}|dur$dS|dkrVtj||j|gd}|j ||¡| ¡|durbdS|S)NÚr)Úargs) rIrJÚlog_capture_startr ÚTimerrLr%rr6)rZ log_levelr+ÚcallerrrrrrrQs ÿzController.log_capture_startcCs4|dkr dStj |¡}|j |¡|dur0dS|S©NrOrH)rrrSr?rrrrKs   zController.log_capture_finishrOÚbcCsD|dkr dS|j tjtj¡r:|j ¡r,dS|j ¡s:dS|j ¡S)NrOFT) r!r7r.r8r9r Ú is_runningÚ is_enabledr6©rrSrrrr6•s  zController.startFcCs,|j ¡sd}n|jj|d}|j ¡|S)NT©Úprofile_switch)r rVr;r%r)rrZr?rrrÚ_stop s   zController._stopcCs|dkr dS|jddS)NrOFrY)r[rXrrrr;¨szController.stopc Csv|dkr dS|j ¡r*|jdd}|s*dSz|j ¡Wn4tyl}zt d|¡WYd}~dSd}~00| ¡S)NrOFTrYzFailed to reload TuneD: %s)r rVr[Zreload_profile_configrr,Úerrorr6)rrSZstop_okr>rrrrC®s  zController.reloadc Cs|j ¡}d}d}d}zÂz$|r,|jjdd|j ||¡Wnhtjjy¤}zLd}t|ƒ}|r‚|jjj |kr‚t   d|¡d}nt   d|¡WYd}~n d}~00W|rØ|rÀt   d|¡n|sÎt   d¡|j ¡n6|r|rôt   d|¡n|st   d¡|j ¡0||fS) NÚOKTFrYz@Failed to reapply profile '%s'. Did it change on disk and break?zFailed to apply profile '%s'z>Applying previously applied (possibly out-dated) profile '%s'.z$Applying previously applied profile.)r rVr;Z set_profilerIÚ exceptionsrÚstrÚprofileÚnamer,r\r1r-r6)rrEÚmanualZ was_runningÚmsgÚsuccessZreapplyr>rrrÚ_switch_profile½s8 &  û  zController._switch_profilez(bs)cCs(|dkr dS|j |¡sdS| |d¡S)NrO©FÚ Unauthorized)FzInvalid profile_nameT)r$Ú is_valid_namere©rrErSrrrÚswitch_profileØs  zController.switch_profilecCs |dkr dS| ¡}| |d¡S)NrOrfF)Úrecommend_profilere)rrSrErrrÚ auto_profileàszController.auto_profilecCs*|dkr dS|jjdur"|jjjSdSdSrT)r r`rarXrrrÚactive_profileçs   zController.active_profilez(ss)c Cs|dkr dS|jj}|durxz"|j ¡\}}|dur<|du}Wn8tyv}z d}t|ƒ}||fWYd}~Sd}~00|r‚tjntj}|dfS)NrO)Úunknownrgrn) r rbr$Zget_active_profilerr_r.ZACTIVE_PROFILE_MANUALZACTIVE_PROFILE_AUTO)rrSrbr`r>Úmoder\rrrÚ profile_modeðs zController.profile_modecCs|dkr dS|jjpdSrT)r Úpost_loaded_profilerXrrrrqszController.post_loaded_profilecCsB|dkr dS|j ¡r |j ¡|j ¡r>|jjddddddS)NrOFT)Zsave_instantly)r rVr;rWZset_all_profilesrXrrrÚdisables    ÿzController.disablecCs|dkr dS|j ¡S)NrOF)r rVrXrrrrVszController.is_runningÚascCs|dkr gS|jjj ¡SrT)r Úprofile_loaderÚprofile_locatorZget_known_namesrXrrrÚprofilesszController.profilesza(ss)cCs|dkr gS|jjj ¡SrT)r rtruZget_known_names_summaryrXrrrÚ profiles2szController.profiles2z(bsss)cCsR|dks|j |¡sdS|dus(|dkr0| ¡}t|jjj |tj tj gdg¡ƒS)NrO)FrOrOrO) r$rhrmÚtupler rtruZget_profile_attrsr.ZPROFILE_ATTR_SUMMARYZPROFILE_ATTR_DESCRIPTIONrirrrÚ profile_info%s zController.profile_infocCs|dkr dS|jj ¡SrT)r Zprofile_recommenderZ recommendrXrrrrk-szController.recommend_profilecCs|dkr dS|jjddS)NrOF©Zignore_missing©r Úverify_profilerXrrrr|3szController.verify_profilecCs|dkr dS|jjddS)NrOFTrzr{rXrrrÚverify_profile_ignore_missing9sz(Controller.verify_profile_ignore_missingz a{sa{ss}}cCsr|dkr iSi}|j ¡D]R}|j d¡d dd¡d}| ¡}i||<| ¡D]\}}t|ƒ|||<qRq|S)zuReturn dictionary with accesible plugins Return: dictionary -- {plugin_name: {parameter_name: default_value}} rOÚ.éÿÿÿÿÚ_r*)r Úget_all_pluginsrÚsplitZ_get_config_optionsÚitemsr_)rrSÚpluginsZ plugin_classÚ plugin_nameZ conf_optionsÚkeyÚvalrrrr?szController.get_all_pluginscCs(|dks|j |¡sdS|j t|ƒ¡S)z"Return docstring of plugin's classrO)r$rhr Úget_plugin_documentationr_©rr…rSrrrrˆQsz#Controller.get_plugin_documentationza{ss}cCs(|dks|j |¡siS|j t|ƒ¡S)zžReturn dictionary with plugin's parameters and their hints Parameters: plugin_name -- name of plugin Return: dictionary -- {parameter_name: hint} rO)r$rhr Úget_plugin_hintsr_r‰rrrrŠXs zController.get_plugin_hintscCsB|dks|j |¡sdS|jjr>|jjjr>|jjj |¡dSdS)z¤Allows to dynamically add sockets to send signals to Parameters: path -- path to socket to register for sending signals Return: bool -- True on success rOFT)r$rhr Z _applicationZ_unix_socket_exporterZregister_signal_path)rÚpathrSrrrÚregister_socket_signal_pathfs z&Controller.register_socket_signal_pathÚssc Cs¬|dkr dS|j |¡sdS|j |¡s,dSd}|jjjD]&}|j|kr:t d|j¡d}qbq:|s€d|}t |¡d|fSt |j t j ƒs¬d |j j}t |¡d|fSt |j |¡ƒ}t d |jt|ƒf¡|jjjD] }|j|@} t| ƒrÞ|| 8}t d t| ƒ|j|jf¡|j j|j jkrbd |j|j jt| ƒ|j|j jf}t |¡d|fS|j  || ¡|j  || ¡qÞt|ƒr¨d t|ƒ}t |¡d|fSdS)NrOrf)FzInvalid devices©FúInvalid instance_nameFzFound instance '%s'.TúInstance '%s' not foundú>Plugin '%s' does not support hotplugging or dynamic instances.z-Instance '%s' trying to acquire devices '%s'.ú8Moving devices '%s' from instance '%s' to instance '%s'.ztTarget instance '%s' is of type '%s', but devices '%s' are currently handled by instance '%s' which is of type '%s'.z2Ignoring devices not handled by any instance '%s'.©Tr])r$rhr Ú _unit_managerÚ instancesrar,Údebugr\Ú isinstanceÚpluginrÚPluginrAZ devstr2devsr_Úprocessed_devicesÚlenr-Ú_remove_devices_nocheckÚ_add_devices_nocheck) rÚdevicesÚ instance_namerSÚfoundZinstance_targetÚretsZdevsÚinstanceÚ devs_movingrrrÚinstance_acquire_deviceszsX        ÿÿÿ     z#Controller.instance_acquire_devicesz (bsa(ss))cs¢|dkrddgfS|j ˆ¡s(ddgfSˆdkr\ˆ| ¡ ¡vr\dˆ}t |¡d|gfStdd„|jjj ƒ}ˆdkrŠt‡fdd„|ƒ}d d t t d d„|ƒƒfS) aReturn a list of active instances of a plugin or all active instances Parameters: plugin_name -- name of the plugin or an empty string Return: bool -- True on success string -- error message or "OK" list of string pairs -- [(instance_name, plugin_name)] rOFrgúInvalid plugin_namezPlugin '%s' does not existcSs|jSr)Zactive©r¢rrrÚºóz*Controller.get_instances..cs |jjˆkSr)r˜rar¦©r…rrr§¼r¨Tr]cSs|j|jjfSr)rar˜r¦rrrr§½r¨) r$rhrÚkeysr,r\Úfilterr r”r•ÚlistÚmap)rr…rSr¡r•rr©rÚ get_instances¦s      zController.get_instancesz(bsas)cCsv|dkrddgfS|j |¡s(ddgfS|jjjD]&}|j|kr2ddtt|jƒƒfSq2d|}t   |¡d|gfS)zÓReturn a list of devices assigned to an instance Parameters: instance_name -- name of the instance Return: bool -- True on success string -- error message or "OK" list of strings -- device names rOFrgrTr]r) r$rhr r”r•raÚsortedr¬ršr,r\)rrŸrSr¢r¡rrrÚinstance_get_devices¿s      zController.instance_get_deviceszssa{ss}c Cs:|dkr dS|j |¡sdS|j |¡s,dS| ¡D]&\}}|j |¡rT|j |¡s4dSq4dd„|jjjDƒ}|| ¡vr–d|}t |¡d |fS||} t | t j ƒsÆd | j }t |¡d |fS|  d d ¡} |  d d ¡} |  dd ¡} |  dd ¡} t|  d|jjj¡ƒ}z4|  ||| | | | |¡}|  |¡|jjj |¡WnHtyˆ}z.d|t|ƒf}t |¡d |fWYd }~Sd }~00t d||f¡|  |¡|  |¡|jjjD]z}||ksº|j| ksº|j|jkræqº|  ||j¡}t|ƒrºt dt|ƒ|j |j f¡|  ||¡|  ||¡qºdS)aDynamically create a plugin instance Parameters: plugin_name -- name of the plugin instance_name -- name of the new instance dict of string-string -- options for the new instance Return: bool -- True on success string -- error message or "OK" rOrf)Fr¥rŽ)FzInvalid optionscSsi|] }|j|“qSr©ra)Ú.0r<rrrÚ ìr¨z.Controller.instance_create..zPlugin '%s' not foundFr‘ržNÚdevices_udev_regexÚ script_preÚ script_postÚpriorityz Error creating instance '%s': %sz,Created dynamic instance '%s' of plugin '%s'r’r“) r$rhrƒr r”r„rªr,r\r—rr™raÚpopÚintZ_def_instance_priorityZcreate_instanceZinitialize_instancer•ÚappendÚ Exceptionr_r-Zassign_free_devicesZinstance_apply_tuningr˜r·Z_get_matching_devicesršr›rœr)rr…rŸÚoptionsrSr†Úvaluer„r¡r˜ržr´rµr¶r·r¢r>Zother_instancer£rrrÚinstance_createÖsd                 ÿ þ  ÿ zController.instance_createc sH|dkr dS|j ˆ¡sdSz ‡fdd„|jjjDƒd}Wn,tyhdˆ}t |¡d|fYS0|j}t |t j ƒs˜d |j }t |¡d|fS|j  ¡}z4| ||¡|jjj |¡| |¡| |¡WnHty}z.d ˆt|ƒf}t |¡d|fWYd }~Sd }~00t d ˆ¡|D]}| |¡q2d S)z²Destroy a dynamically created plugin instance Parameters: instance_name -- name of the new instance Return: bool -- True on success string -- error message or "OK" rOrfrŽcsg|]}|jˆkr|‘qSrr±)r²Úi©rŸrrÚ &r¨z/Controller.instance_destroy..rrFr‘z Error deleting instance '%s': %sNzDeleted instance '%s'r“)r$rhr r”r•Ú IndexErrorr,r\r˜r—rr™raršÚcopyrœÚremoveZinstance_unapply_tuningZdestroy_instancer»r_r-Z _add_device) rrŸrSr¢r¡r˜ržr>ZdevicerrÀrÚinstance_destroys:            zController.instance_destroy)N)N)N)F)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)+rrrÚ__doc__rr@rBrDrÚsignalrGrLZexportrQrKr6r[r;rCrerjrlrmrprqrrrVrvrwryrkr|r}rrˆrŠrŒr¤r®r°r¾rÅÚ __classcell__rrr'rr(s€ -                                             +      ? )rIrZ tuned.logsZtuned.exceptionsrr Z tuned.constsr.Ztuned.utils.commandsrZ tuned.pluginsrr/r2Ú__all__rJÚgetr,ÚobjectrZ interfacesZExportableInterfacerrrrrÚs