3 À<×e¹3ã@sŒddlZddlZddlZddlZddlmZddlmZddl j Z ddl m Z ddlm Z ddlmZddlZejjƒZGdd„deƒZdS) éN)ÚTunedException)ÚInvalidProfileException)Úcommands)Úexports)ÚProfileRecommenderc@seZdZd;dd„Zdd„Zdd„Zdd „Zd d „Zd d „Zdd„Z dÚDaemonNcCs¼tjdƒtj|_ttjƒ|_ttjƒ|_ tj |_ d|_ tj |_|dk r¼|jtjtjƒ|_t|jtjtjƒƒ|_t|jtjtjƒƒ|_ |jtjtj ƒ|_ |jtjtjƒ|_ |jtjtj ƒ|_||_|jdkrØttjƒ|_|j dkrêd|_ n|j |jkrþ|j|_ |j |j|_tjd|jƒ|j �rJtjdƒtjd|j|j|jfƒt|j d�|_||_||_|jƒt ƒ|_!y|j"|ƒWn2t#k �r¶}ztj$d |ƒWYdd}~XnXdS) Nzinitializing daemonTrFz$using sleep interval of %d second(s)z8dynamic tuning is enabled (can be overridden by plugins)zFusing update interval of %d second(s) (%d times of the sleep interval))Z is_hardcodedz:Cannot set initial profile. No tunings will be enabled: %s)%ÚlogÚdebugÚconstsZCFG_DEF_DAEMONÚ_daemonÚintZCFG_DEF_SLEEP_INTERVALÚ_sleep_intervalZCFG_DEF_UPDATE_INTERVALZ_update_intervalZCFG_DEF_DYNAMIC_TUNINGÚ_dynamic_tuningZ_recommend_commandZCFG_DEF_ROLLBACKÚ _rollbackZget_boolZ CFG_DAEMONÚgetZCFG_SLEEP_INTERVALZCFG_UPDATE_INTERVALZCFG_DYNAMIC_TUNINGZCFG_RECOMMEND_COMMANDZCFG_DEF_RECOMMEND_COMMANDÚ CFG_ROLLBACKÚ _applicationÚ _sleep_cyclesÚinforÚ_profile_recommenderÚ _unit_managerÚ_profile_loaderÚ _init_threadsrÚ_cmdÚ _init_profilerÚerror)ÚselfZ unit_managerÚprofile_loaderÚ profile_namesÚconfigZ applicationÚe©r!ú/usr/lib/python3.6/daemon.pyÚ__init__sF        zDaemon.__init__cCsFd|_tjƒ|_tjƒ|_tjƒ|_tjƒ|_|jjƒtjƒ|_dS)N) Ú_threadÚ threadingZEventÚ _terminateÚ_terminate_profile_switchÚ _not_usedÚ_sighup_processingÚsetÚ_profile_applied)rr!r!r"r7s     zDaemon._init_threadscCs|jdƒdS)zARead configuration files again and load profile according to themN)r)rr!r!r"Úreload_profile_configCszDaemon.reload_profile_configcCsœd}|jjƒ}|dkrP|jƒ\}}|dkrrd}|r<|d7}n|d7}tj|ƒn"|dkrr|rhtjdƒn tjdƒd|_d|_g|_d|_|j |||ƒdS)NTz.No profile is preset, running in manual mode. z(Only post-loaded profile will be enabledzNo profile will be enabled.Ú) rZget_post_loaded_profileÚ_get_startup_profilerrÚ_profileÚ_manualÚ_active_profilesÚ_post_loaded_profileÚset_all_profiles)rrÚmanualÚpost_loaded_profileÚmsgr!r!r"rGs&      zDaemon._init_profilecCsì|pd}|jƒ}|jr2tjd|jƒ||jg}x:|D]2}||jjjƒkr8d|}|j|d|ƒt|ƒ‚q8Wy.|r„|j j |ƒ|_ nd|_ ||_ |jƒ|_ WnJtk ræ}z.ddj|ƒ|f}|j|d|ƒt|ƒ‚WYdd}~XnXdS)Nr-zUsing post-loaded profile '%s'z%Requested profile '%s' doesn't exist.FzCannot load profile(s) '%s': %sú )Úsplitr2rrrZprofile_locatorZget_known_namesÚ_notify_profile_changedrrÚloadr/r0r1rÚjoin)rrr4Z profile_listÚprofileÚerrstrr r!r!r"Ú_load_profiles`s*    zDaemon._load_profilescCs2|jƒr"d}|j|d|ƒt|ƒ‚|j||ƒdS)Nz/Cannot set profile while the daemon is running.F)Ú is_runningr9rr>)rrr4r=r!r!r"Ú set_profilezs zDaemon.set_profilecCs4|s d|_n$t|jƒƒdkr*d}t|ƒ‚n||_dS)NézYWhitespace is not allowed in profile names; only a single post-loaded profile is allowed.)r2Úlenr8r)rÚ profile_namer=r!r!r"Ú_set_post_loaded_profileƒs  zDaemon._set_post_loaded_profileFcCsV|jƒr"d}|j|d|ƒt|ƒ‚|j|ƒ|j||ƒ|rR|j||ƒ|j|ƒdS)Nz/Cannot set profile while the daemon is running.F)r?r9rrDr>Ú_save_active_profileÚ_save_post_loaded_profile)rZactive_profilesr4r5Zsave_instantlyr=r!r!r"r3Œs   zDaemon.set_all_profilescCs|jS)N)r/)rr!r!r"r<›szDaemon.profilecCs|jS)N)r0)rr!r!r"r4Ÿsz Daemon.manualcCs|jr |jSdS)N)r/r2)rr!r!r"r5£szDaemon.post_loaded_profilecCs|jS)N)r)rr!r!r"Úprofile_recommender©szDaemon.profile_recommendercCs|jS)N)r)rr!r!r"r­szDaemon.profile_loadercCs |jdk rtjtj|||ƒ|S)N)rrZ send_signalr ZSIGNAL_PROFILE_CHANGED)rrÚresultr=r!r!r"r9³s zDaemon._notify_profile_changedcCsj|jjddgdgd�\}}|dkr&dS|dd…dkr:dS|jjddgdgd�\}}tjd |ƒdkoh| S) NZ systemctlzis-system-runningr)Z no_errorsFéZstoppingz list-jobsz0\b(shutdown|reboot|halt|poweroff)\.target.*start)rZexecuteÚreÚsearch)rZretcodeÚoutr!r!r"Ú_full_rollback_required¸szDaemon._full_rollback_requiredcCsæ|jdkrtdƒ‚|jj|jjƒ|jdj|jƒ|jƒ|j |j ƒ|jj ƒ|j j ƒtjd|jjƒ|jrxtjƒdj|jƒ}|j|ddƒ|jjƒ|j�r|j}x\|jj|j|jƒ�s|jr¬|d8}|dkr¬|j}tjdƒ|jjƒtjd ƒ|jjƒq¬W|j jƒd}x.|jj|j |jƒ �rD|d k�rD|d7}�qW|j!j"ƒ�rZt#j$}njt#j%}|j&ƒ�svtjd ƒnN|j'd k�r t#j(}tjd t#j)t#j*fƒn$|j�rºtjdƒt#j$}n tjdƒ|j�rØ|jj+|ƒ|jj,ƒdS)Nz2Cannot start the daemon without setting a profile.r7z'static tuning from profile '%s' appliedTZOKrArzupdating monitorszperforming tuningséz1terminating TuneD due to system shutdown / rebootZ not_on_exitzMterminating TuneD and not rolling back any changes due to '%s' option in '%s'z+terminating TuneD, rolling back all changesz"terminating TuneD in one-shot mode)-r/rrZcreateZunitsrEr;r1r0rFr2Z start_tuningr+r*rrÚnamer rÚstartr9r)ÚclearrrÚwaitr&r rr Zupdate_monitorsZ update_tuningr(r'Úis_setr Z ROLLBACK_FULLZ ROLLBACK_SOFTrMrZ ROLLBACK_NONErZGLOBAL_CONFIG_FILEZ stop_tuningZ destroy_all)rrZ _sleep_cntÚiZrollbackr!r!r"Ú _thread_codeÁsX          "        zDaemon._thread_codecCsHy|jj||ƒWn0tk rB}ztjt|ƒƒWYdd}~XnXdS)N)rZsave_active_profilerrrÚstr)rrr4r r!r!r"rEszDaemon._save_active_profilecCsFy|jj|ƒWn0tk r@}ztjt|ƒƒWYdd}~XnXdS)N)rZsave_post_loaded_profilerrrrV)rrCr r!r!r"rFsz Daemon._save_post_loaded_profilecCs&tjdƒ|jjƒ}tjd|ƒ|S)NzWRunning in automatic mode, checking what profile is recommended for your configuration.zUsing '%s' profile)rrrZ recommend)rr<r!r!r"Ú_get_recommended_profiles  zDaemon._get_recommended_profilecCs2|jjƒ\}}|dkr|dk }|s*|jƒ}||fS)N)rZget_active_profilerW)rr<r4r!r!r"r.s zDaemon._get_startup_profilecCs |jjjƒS)z$Return all accessible plugin classes)rÚplugins_repositoryZload_all_plugins)rr!r!r"Úget_all_plugins"szDaemon.get_all_pluginsc Cs.y|jjj|ƒ}Wntk r&dSX|jS)zReturn plugin class docstringr-)rrXÚ load_pluginÚ ImportErrorÚ__doc__)rÚ plugin_nameÚ plugin_classr!r!r"Úget_plugin_documentation&s  zDaemon.get_plugin_documentationc Cs0y|jjj|ƒ}Wntk r&iSX|jƒS)zŒReturn plugin's parameters and their hints Parameters: plugin_name -- plugins name Return: dictionary -- {parameter_name: hint} )rrXrZr[Zget_config_options_hints)rr]r^r!r!r"Úget_plugin_hints0s  zDaemon.get_plugin_hintscCs |jdk S)N)r/)rr!r!r"Ú is_enabledAszDaemon.is_enabledcCs|jdk o|jjƒS)N)r$Zis_alive)rr!r!r"r?DszDaemon.is_runningcCs`|jƒr dS|jdkrdStjdƒ|jjƒtj|jd�|_ |j j ƒ|j j ƒ|j j ƒdS)NFzstarting tuning)ÚtargetT)r?r/rrr(r*r%ZThreadrUr$r'rQr&rP)rr!r!r"rPGs      z Daemon.startcCs||jƒstjdƒdS|jdkr.tjdƒdS|jjƒsFtjdƒdS|jjƒtjd|jj ƒ|j j |ƒ}|jj ƒ|S)NzTuneD is not runningFzno profile is setzprofile is not appliedzverifying profile(s): %s) r?rrr/r+rSr(rQrrOrZ verify_tuningr*)rZignore_missingÚretr!r!r"Úverify_profileVs        zDaemon.verify_profilecCsB|jƒs dStjdƒ|r$|jjƒ|jjƒ|jjƒd|_dS)NFzstopping tuningT)r?rrr'r*r&r$r;)rZprofile_switchr!r!r"Ústopls    z Daemon.stop)NNN)F)F) Ú__name__Ú __module__Ú __qualname__r#rr,rr>r@rDr3Úpropertyr<r4r5rGrr9rMrUrErFrWr.rYr_r`rar?rPrdrer!r!r!r"rs8 &         G r)ÚosÚerrnor%Z tuned.logsZtunedZtuned.exceptionsrZtuned.profiles.exceptionsrZ tuned.constsr Ztuned.utils.commandsrrZtuned.utils.profile_recommenderrrJZlogsrrÚobjectrr!r!r!r"Ús