a ”ˆ«hæ?ã@s ddlmZddlZddlmZddlmZddl m Z ddl m Z ddl mZddlmZddlZddlZddlZddlZddlZddlZGd d „d eƒZdS) é)Úprint_functionN)Úcommands)ÚLocatoré)ÚTunedAdminDBusException)ÚTunedException)ÚProfileRecommenderc@s°eZdZdddejejfdd„Zdd„Zdd„Z d d „Z d d „Z d d„Z dd„Z dd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd „Zdhd"d#„Zdid$d%„Zd&d'„Zd(d)„Zd*d+„Zd,d-„Zd.d/„Zd0d1„Zd2d3„Zd4d5„Zd6d7„Z d8d9„Z!d:d;„Z"dd?„Z$d@dA„Z%dBdC„Z&dDdE„Z'dFdG„Z(dHdI„Z)dJdK„Z*dLdM„Z+dNdO„Z,dPdQ„Z-djdSdT„Z.dkdUdV„Z/dldWdX„Z0dmdYdZ„Z1d[d\„Z2d]d^„Z3d_d`„Z4dadb„Z5dcdd„Z6dedf„Z7dgS)nÚAdminTFcCsz||_||_||_||_t|ƒ|_t|ƒ|_t  ¡|_ d|_ d|_ d|_ d|_d|_||_tƒ|_|jrp| ¡nd|_dS)NÚTF)Ú_dbusÚ_debugÚ_asyncÚ_timeoutrÚ_cmdÚprofiles_locatorÚ_profiles_locatorÚ threadingÚEventÚ_daemon_action_finishedÚ_daemon_action_profileÚ_daemon_action_resultÚ_daemon_action_errstrÚ _controllerÚ _log_tokenÚ _log_levelrÚ_profile_recommenderÚ _init_dbusÚ _dbus_working)ÚselfZ profile_dirsZdbusÚdebugZasyncoÚtimeoutZ log_level©r!ú5/usr/lib/python3.9/site-packages/tuned/admin/admin.pyÚ__init__s   zAdmin.__init__c Csjtj tjtjtj|j¡|_z|j  tj |j ¡WdSt yd}z|  |¡WYd}~dSd}~00dS©NTF)ÚtunedZadminZDBusControllerÚconstsZDBUS_BUSZDBUS_INTERFACEZ DBUS_OBJECTr rZset_signal_handlerZSIGNAL_PROFILE_CHANGEDÚ_signal_profile_changed_cbrÚ_error©rÚer!r!r"r%s zAdmin._init_dbuscCst|tjddS)N)Úfile)ÚprintÚsysÚstderr)rÚmessager!r!r"r(.sz Admin._errorcCs*|j ¡s&||_||_||_|j ¡dS©N)rÚis_setrrrÚset)rÚ profile_nameÚresultZerrstrr!r!r"r'1s  z Admin._signal_profile_changed_cbc Cs|zt t|j tj¡ƒd¡WnXtyN}z|jtj kWYd}~Sd}~0t t fyv}zWYd}~dSd}~00dS)NrFT) ÚosÚkillÚintrZ read_filer&ZPID_FILEÚOSErrorÚerrnoÚEPERMÚ ValueErrorÚIOErrorr)r!r!r"Ú_tuned_is_running9s zAdmin._tuned_is_runningc OsP|dus|dkrdSd}d}d}zt|d|ƒ}Wn*ty\}zd|_WYd}~n d}~00zt|d|ƒ}WnNty¾}z6|jsª| t|ƒd|¡WYd}~dSWYd}~n d}~00|jr6z6|j |j¡|jj|g|¢Ri|¤Ž|j  ¡}Wn6t y4}z| |¡d|_WYd}~n d}~00|jsL||i|¤Ž}|S)Nr FZ _action_dbus_Z_action_z , action '%s' is not implemented) ÚgetattrÚAttributeErrorrr(ÚstrrZset_on_exit_actionÚ_log_capture_finishÚ set_actionÚrunr)rÚ action_nameÚargsÚkwargsÚactionZ action_dbusÚresr*r!r!r"rGCs8$ÿ z Admin.actionc Csdtdƒ|D]R}|ddurN|ddkrNt|j d|ddd|d¡ƒq td|dƒq dS)NzAvailable profiles:rr z- %sré)r,rZ align_str)rÚ profile_namesÚprofiler!r!r"Ú_print_profilesas &zAdmin._print_profilesc Csfz|j ¡}Wn8tyF}z dd„|j ¡Dƒ}WYd}~n d}~00| |¡| ¡|j d¡S)NcSsg|] }|df‘qS)r r!)Ú.0rKr!r!r"Ú nóz4Admin._action_dbus_list_profiles..T)rZ profiles2rÚprofilesrLÚ_action_dbus_activeÚexit)rrJr*r!r!r"Ú_action_dbus_list_profilesis* z Admin._action_dbus_list_profilescCs| |j ¡¡| ¡dS©NT)rLrZget_known_names_summaryÚ_action_active©rr!r!r"Ú_action_list_profilessszAdmin._action_list_profilescCs&|j ¡}|dkrd}|j d¡|S)Nr T)rÚactive_profilerR©rr3r!r!r"Ú_dbus_get_active_profilexs   zAdmin._dbus_get_active_profilecCs|j ¡\}}|Sr0)rÚget_active_profile)rr3Úmanualr!r!r"Ú_get_active_profileszAdmin._get_active_profilecCs.|j ¡\}}|dur|du}|r(tjStjSr0)rr[r&ZACTIVE_PROFILE_MANUALZACTIVE_PROFILE_AUTO)rrKr\r!r!r"Ú_get_profile_modeƒszAdmin._get_profile_modecCs|j ¡}|dkrd}|S)Nr )rÚpost_loaded_profilerYr!r!r"Ú_dbus_get_post_loaded_profile‰s z#Admin._dbus_get_post_loaded_profilecCs|j ¡}|Sr0)rZget_post_loaded_profilerYr!r!r"Ú_get_post_loaded_profiles zAdmin._get_post_loaded_profilecCsl|ddkrXtdƒt|dƒtƒtdƒt|dƒtƒtdƒt|dƒdStd |ƒd SdS) NrTz Profile name:rzProfile summary:ézProfile description:éz,Unable to get information about profile '%s'F©r,)rrKÚ profile_infor!r!r"Ú_print_profile_info“s     zAdmin._print_profile_infor cCsB|dkr| ¡}|r*| ||j |¡¡}n tdƒd}|j |¡S©Nr úNo current active profile.F)rZrfrrer,rR)rrKrHr!r!r"Ú_action_dbus_profile_info¢szAdmin._action_dbus_profile_infoc Cs„|dkr`z"| ¡}|dur(tdƒWdSWn4ty^}z| t|ƒ¡WYd}~dSd}~00| ||j |tj tj gddg¡¡Srg) r]r,rr(r@rfrZget_profile_attrsr&ZPROFILE_ATTR_SUMMARYZPROFILE_ATTR_DESCRIPTION)rrKr*r!r!r"Ú_action_profile_info¬s zAdmin._action_profile_infocCs$|durtdƒdStd|ƒdS)NrhFzCurrent active profile: %sTrdrYr!r!r"Ú_print_profile_name¸s  zAdmin._print_profile_namecCs|rtd|ƒdS)NzCurrent post-loaded profile: %srdrYr!r!r"Ú_print_post_loaded_profileÀsz Admin._print_post_loaded_profilecCs4| ¡}| |¡}|r(| ¡}| |¡|j |¡Sr0)rZrkr`rlrrR)rrXrHr_r!r!r"rQÄs   zAdmin._action_dbus_activec Cs¸z2| ¡}| ¡}|r0|r$|d7}nd}||7}Wn4tyf}z| t|ƒ¡WYd}~dSd}~00|dur | ¡s tdƒtd|ƒ|rœtd|ƒdS| |¡}| |¡|S)Nú r FzKIt seems that tuned daemon is not running, preset profile is not activated.zPreset profile: %szPreset post-loaded profile: %sT) r]rarr(r@r=r,rkrl)rr3r_r*rHr!r!r"rUÌs(      zAdmin._action_activecCstd|ƒdS)NzProfile selection mode: rd)rÚmoder!r!r"Ú_print_profile_modeåszAdmin._print_profile_modecCsB|j ¡\}}| |¡|dkr6| |¡|j d¡S|j d¡S)Nr FT)rZ profile_moderor(rR)rrnÚerrorr!r!r"Ú_action_dbus_profile_modeès    zAdmin._action_dbus_profile_modec CsRz| ¡}| |¡WdStyL}z| t|ƒ¡WYd}~dSd}~00dSr$)r^rorr(r@)rrnr*r!r!r"Ú_action_profile_modeðs zAdmin._action_profile_modecCs:|r(|j ¡s6|j ¡s6| d¡d}n| d|¡|S)NzCannot enable the tuning.FzUnable to switch profile: %s)rZ is_runningÚstartr(©rÚretÚmsgr!r!r"Ú_profile_print_statusùs  zAdmin._profile_print_statuscCsrt ¡|j|jkr.td|jƒ|j d¡S|j ¡rn|j|krn|j sbtd|j ƒ|j d¡S|j d¡SdS)Nz‰Operation timed out after waiting %d seconds(s), you may try to increase timeout by using --timeout command line option or using --async.FzError changing profile: %sT) ÚtimeÚ _timestamprr,rrRrÚisSetrrrrYr!r!r"Ú_action_dbus_wait_profiles     zAdmin._action_dbus_wait_profilec Cs~|jdus|jdkrdSz2|j |j¡}d|_t|dtjdtj ¡Wn.tyx}z| d¡WYd}~n d}~00dS)Nr )Úendr+zUError: Failed to stop log capture. Restart the TuneD daemon to prevent a memory leak.) rrZlog_capture_finishr,r-r.Úflushrr()rZlog_msgsr*r!r!r"rAsÿzAdmin._log_capture_finishcCsÀt|ƒdkr| ¡Sd |¡}|dkr2|j d¡S|j ¡|jsl|jdurl|j dd}|j  |j|¡|_ |j  |¡\}}|js†|sš|j |  ||¡¡St ¡|_|j |j|¡|  ||¡S)Nrrmr Féé)ÚlenÚ_action_dbus_listÚjoinrrRrÚclearr rrÚlog_capture_startrZswitch_profilerwrxryrBr{)rrPr3r rurvr!r!r"Ú_action_dbus_profiles"    ÿ  zAdmin._action_dbus_profilecCs8tdƒ|j gd¢¡\}}|dkr,tdƒ‚tdƒdS)NzTrying to (re)start tuned...)Zservicer%Zrestartrz5TuneD (re)start failed, check TuneD logs for details.zTuneD (re)started.)r,rZexecuterrtr!r!r"Ú_restart_tuned.s zAdmin._restart_tunedc Cs®||j ¡vr˜zH|jr4| ¡| ¡r4| |g¡WS|j ||¡| ¡tdƒWdSt y”}z&|  t |ƒ¡|  d¡WYd}~dSd}~00n|  d|¡dSdS)Nz[TuneD is not active on the DBus, not checking whether the profile was successfully applied.TzUnable to switch profile.Fz%Requested profile '%s' doesn't exist.) rZget_known_namesr r†rr…rZsave_active_profiler,rr(r@)rr3r\r*r!r!r"Ú _set_profile5s  zAdmin._set_profilecCs6t|ƒdkr| ¡Sd |¡}|dkr*dS| |d¡S)Nrrmr FT)r€rWr‚r‡)rrPr3r!r!r"Ú_action_profileHs   zAdmin._action_profilecCs–|j ¡}|j ¡|jsD|jdurD|jdd}|j |j|¡|_|j  ¡\}}|js\|sp|j  |  ||¡¡St   ¡|_ |j |j|¡|  ||¡S)Nr~r)rÚrecommend_profilerrƒr rrr„rZ auto_profilerRrwrxryrBr{)rr3r rurvr!r!r"Ú_action_dbus_auto_profilePs  ÿ  zAdmin._action_dbus_auto_profilecCs|j ¡}| |d¡S)NF)rÚ recommendr‡rYr!r!r"Ú_action_auto_profile`s zAdmin._action_auto_profilecCst|j ¡ƒ|j d¡SrT)r,rr‰rRrVr!r!r"Ú_action_dbus_recommend_profiledsz$Admin._action_dbus_recommend_profilecCst|j ¡ƒdSrT)r,rr‹rVr!r!r"Ú_action_recommend_profilehszAdmin._action_recommend_profilecCsr|r|j ¡}n |j ¡}|r(tdƒn0tdƒtdƒtdƒtdƒtdƒtdƒtdtjƒ|j |¡S) NzIVerification succeeded, current system settings match the preset profile.zLVerification failed, current system settings differ from the preset profile.z=You can mostly fix this by restarting the TuneD daemon, e.g.:z systemctl restart tunedÚorz service tuned restartzNSometimes (if some plugins like bootloader are used) a reboot may be required.z&See TuneD log file ('%s') for details.)rZverify_profile_ignore_missingZverify_profiler,r&ZLOG_FILErR)rÚignore_missingrur!r!r"Ú_action_dbus_verify_profilels   z!Admin._action_dbus_verify_profilecCs tdƒdS©Nz Not supported in no_daemon mode.Frd)rrr!r!r"Ú_action_verify_profile}szAdmin._action_verify_profilecCs:d}|j |j|¡|_|j ¡}|s.| d¡|j |¡S)NrIzCannot disable active profile.)rr„rrZoffr(rR)rr rur!r!r"Ú_action_dbus_offsÿ  zAdmin._action_dbus_offcCs tdƒdSr’rdrVr!r!r"Ú _action_off‹szAdmin._action_offrPcCs(|dkr| ¡S|dkr$|j|dSdS)aDPrint accessible profiles or plugins got from TuneD dbus api Keyword arguments: list_choice -- argument from command line deciding what will be listed verbose -- if True then list plugin's config options and their hints if possible. Functional only with plugin listing, with profiles this argument is omitted rPÚplugins©ÚverboseN)rSÚ_action_dbus_list_plugins©rZ list_choicer˜r!r!r"rs zAdmin._action_dbus_listcCs(|dkr| ¡S|dkr$|j|dSdS)aaPrint accessible profiles or plugins with no daemon mode Keyword arguments: list_choice -- argument from command line deciding what will be listed verbose -- Plugins cannot be listed in this mode, so verbose argument is here only because argparse module always supplies verbose option and if verbose was not here it would result in error rPr–r—N)rWÚ_action_list_pluginsršr!r!r"Ú _action_lists zAdmin._action_listcCs„|j ¡}| ¡D]d}t|ƒ|rt||ƒdkr4q|j |¡}||D],}td|ƒ| |d¡}|rHtd|ƒqHq|j d¡S)zvPrint accessible plugins Keyword arguments: verbose -- if is set to True then parameters and hints are printed rz %sNz %sT)rZ get_pluginsÚkeysr,r€Zget_plugin_hintsÚgetrR)rr˜r–ÚpluginÚhintsZ parameterÚhintr!r!r"r™«s      zAdmin._action_dbus_list_pluginscCs tdƒdSr’rd)rr˜r!r!r"r›¾szAdmin._action_list_pluginscCs0|j ||¡\}}|s$| d|¡|j |¡S)NzUnable to acquire devices: %s)rZinstance_acquire_devicesr(rR)rÚdevicesÚinstancerurvr!r!r"Ú%_action_dbus_instance_acquire_devicesÂsz+Admin._action_dbus_instance_acquire_devicescCs tdƒdSr’rd)rr¢r£r!r!r"Ú _action_instance_acquire_devicesÈsz&Admin._action_instance_acquire_devicescCsZ|j |¡\}}}|s0| d|¡|j d¡S|D]\}}td||fƒq4|j d¡S)NzUnable to list instances: %sFz%s (%s)T)rZ get_instancesr(rRr,)rÚ plugin_namerurvÚpairsr£rŸr!r!r"Ú_action_dbus_get_instancesÌs  z Admin._action_dbus_get_instancescCs tdƒdSr’rd)rr¦r!r!r"Ú_action_get_instancesÕszAdmin._action_get_instancescCsN|j |¡\}}}|s0| d|¡|j d¡S|D] }t|ƒq4|j d¡S)NzUnable to list devices: %sFT)rZinstance_get_devicesr(rRr,)rr£rurvr¢Zdevicer!r!r"Ú!_action_dbus_instance_get_devicesÙs  z'Admin._action_dbus_instance_get_devicescCs tdƒdSr’rd)rr£r!r!r"Ú_action_instance_get_devicesâsz"Admin._action_instance_get_devicesN)r )r )rPF)rPF)F)F)8Ú__name__Ú __module__Ú __qualname__r&Z ADMIN_TIMEOUTÚloggingZERRORr#rr(r'r=rGrLrSrWrZr]r^r`rarfrirjrkrlrQrUrorqrrrwr{rAr…r†r‡rˆrŠrŒrrŽr‘r“r”r•rrœr™r›r¤r¥r¨r©rªr«r!r!r!r"r shþ                r )Z __future__rZ tuned.adminr%Ztuned.utils.commandsrZtuned.profilesrrÚ exceptionsrZtuned.exceptionsrZ tuned.constsr&Ztuned.utils.profile_recommenderrr5r-r9rxrr¯Úobjectr r!r!r!r"Ús