o
    •Û_V1  ã                   @   sŽ   d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d dlZej ¡ ZG dd„ deƒZdS )	é    N)ÚTunedException)ÚInvalidProfileException)Úcommands)Úexports)ÚProfileRecommenderc                   @   s  e Zd Zd=dd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
	d>dd„Zedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7d8„ Zd9d:„ Zd>d;d<„Z dS )?ÚDaemonNc              
   C   s   t  d¡ tj| _ttjƒ| _ttjƒ| _	tj
| _d| _|d urQ| tjtj¡| _t| tjtj¡ƒ| _t| tjtj¡ƒ| _	| tjtj
¡| _| tjtj¡| _|| _| jdkr_ttjƒ| _| j	dkrhd| _n
| j	| jk rr| j| _	| j	| j | _t  d| j ¡ | jr—t  d¡ t  d| j| j | jf ¡ t| j d| _|| _|| _|  ¡  tƒ | _z|  |¡ W d S  t yÏ } zt  !d	| ¡ W Y d }~d S d }~ww )
Nzinitializing daemonTr   Fz$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))Úis_hardcodedz:Cannot set initial profile. No tunings will be enabled: %s)"ÚlogÚdebugÚconstsÚCFG_DEF_DAEMONÚ_daemonÚintÚCFG_DEF_SLEEP_INTERVALÚ_sleep_intervalÚCFG_DEF_UPDATE_INTERVALÚ_update_intervalÚCFG_DEF_DYNAMIC_TUNINGÚ_dynamic_tuningÚ_recommend_commandÚget_boolÚ
CFG_DAEMONÚgetÚCFG_SLEEP_INTERVALÚCFG_UPDATE_INTERVALÚCFG_DYNAMIC_TUNINGÚCFG_RECOMMEND_COMMANDÚCFG_DEF_RECOMMEND_COMMANDÚ_applicationÚ_sleep_cyclesÚinfor   Ú_profile_recommenderÚ_unit_managerÚ_profile_loaderÚ_init_threadsr   Ú_cmdÚ_init_profiler   Úerror)ÚselfÚunit_managerÚprofile_loaderÚprofile_namesÚconfigÚapplicationÚe© r/   ú5/usr/lib/python3/dist-packages/tuned/daemon/daemon.pyÚ__init__   sF   



€ÿzDaemon.__init__c                 C   s<   d | _ t ¡ | _t ¡ | _t ¡ | _| j ¡  t ¡ | _d S ©N)Ú_threadÚ	threadingÚEventÚ
_terminateÚ_terminate_profile_switchÚ	_not_usedÚsetÚ_profile_applied©r(   r/   r/   r0   r$   5   s   



zDaemon._init_threadsc                 C   s   |   d¡ dS )zARead configuration files again and load profile according to themN)r&   r;   r/   r/   r0   Úreload_profile_config?   s   zDaemon.reload_profile_configc                 C   sœ   d}| j  ¡ }|d u r(|  ¡ \}}|d u r'd}|r|d7 }n|d7 }t |¡ n|dkr9|r4t d¡ nt d¡ d | _d | _g | _d | _|  	|||¡ d S )NTz.No profile is preset, running in manual mode. z(Only post-loaded profile will be enabledzNo profile will be enabled.Ú )
r%   Úget_post_loaded_profileÚ_get_startup_profiler	   r    Ú_profileÚ_manualÚ_active_profilesÚ_post_loaded_profileÚset_all_profiles)r(   r+   ÚmanualÚpost_loaded_profileÚmsgr/   r/   r0   r&   C   s(   


€
zDaemon._init_profilec              
   C   sÞ   | j }| jrt d| j ¡ || jg }|D ]}|| jj ¡ vr5d| }d | j ¡}|  |d|¡ t	|ƒ‚qz|rC| j
 |¡| _W d S d | _W d S  tyn } zdd |¡|f }d | j ¡}|  |d|¡ t	|ƒ‚d }~ww )NzUsing post-loaded profile '%s'z%Requested profile '%s' doesn't exist.ú FzCannot load profile(s) '%s': %s)rB   rC   r	   r    r*   Úprofile_locatorÚget_known_namesÚjoinÚ_notify_profile_changedr   r#   Úloadr@   r   )r(   Úprofile_listÚprofileÚerrstrr+   r.   r/   r/   r0   Ú_load_profiles\   s0   ÿü€üzDaemon._load_profilesc                 C   s"   || _ |r| ¡ | _d S g | _d S r2   )rA   ÚsplitrB   )r(   r+   rE   r/   r/   r0   Ú_set_profiles   s   
zDaemon._set_profilec                 C   s:   |   ¡ rd}|  |d|¡ t|ƒ‚|  ||¡ |  ¡  d S ©Nz/Cannot set profile while the daemon is running.F)Ú
is_runningrL   r   rS   rQ   )r(   r+   rE   rP   r/   r/   r0   Úset_profilez   s   ÿzDaemon.set_profilec                 C   s4   |sd | _ d S t| ¡ ƒdkrd}t|ƒ‚|| _ d S )Né   zYWhitespace is not allowed in profile names; only a single post-loaded profile is allowed.)rC   ÚlenrR   r   )r(   Úprofile_namerP   r/   r/   r0   Ú_set_post_loaded_profile„   s   

zDaemon._set_post_loaded_profileFc                 C   sb   |   ¡ rd}|  |d|¡ t|ƒ‚|  ||¡ |  |¡ |  ¡  |r/|  ||¡ |  |¡ d S d S rT   )rU   rL   r   rS   rZ   rQ   Ú_save_active_profileÚ_save_post_loaded_profile)r(   Úactive_profilesrE   rF   Úsave_instantlyrP   r/   r/   r0   rD      s   ÿ
þzDaemon.set_all_profilesc                 C   ó   | j S r2   ©r@   r;   r/   r/   r0   rO      ó   zDaemon.profilec                 C   r_   r2   )rA   r;   r/   r/   r0   rE   ¡   ra   zDaemon.manualc                 C   s   | j r| jS d S r2   )r@   rC   r;   r/   r/   r0   rF   ¥   s   zDaemon.post_loaded_profilec                 C   r_   r2   )r!   r;   r/   r/   r0   Úprofile_recommender«   ra   zDaemon.profile_recommenderc                 C   r_   r2   )r#   r;   r/   r/   r0   r*   ¯   ra   zDaemon.profile_loaderc                 C   s0   | j d ur| j jd ur| j j tj|||¡ |S r2   )r   Ú_dbus_exporterÚsend_signalr   ÚDBUS_SIGNAL_PROFILE_CHANGED)r(   r+   ÚresultrP   r/   r/   r0   rL   µ   s   zDaemon._notify_profile_changedc                 C   sd   | j jddgdgd\}}|dk rdS |d d… dkrdS | j jddgdgd\}}t d	|¡d u S )
NÚ	systemctlzis-system-runningr   )Ú	no_errorsFé   Ústoppingz	list-jobsz0\b(shutdown|reboot|halt|poweroff)\.target.*start)r%   ÚexecuteÚreÚsearch)r(   ÚretcodeÚoutr/   r/   r0   Ú_full_rollback_requiredº   s   zDaemon._full_rollback_requiredc                 C   s¸  | j d u r	tdƒ‚| j | j j¡ |  d | j¡| j¡ |  	| j
¡ | j ¡  | j ¡  t d| j j ¡ | jr<t ¡  d | j¡}|  |dd¡ | jrƒ| j}| j | j| j¡sƒ| jrz|d8 }|dkrz| j}t d¡ | j ¡  t d	¡ | j ¡  | j | j| j¡rX| j ¡  d}| j | j| j¡s¨|d
k r¨|d7 }| j | j| j¡s¨|d
k s—| j  !¡ r°d}nd}|  "¡ rÇ| jrÁt d¡ d}nt d¡ nt d¡ | jrÕ| j #|¡ | j $¡  d S )Nz2Cannot start the daemon without setting a profile.rH   z'static tuning from profile '%s' appliedTÚOKrW   r   zupdating monitorszperforming tuningsé   Fz+terminating Tuned, rolling back all changesz"terminating Tuned in one-shot modez1terminating Tuned due to system shutdown / reboot)%r@   r   r"   ÚcreateÚunitsr[   rK   rB   rA   r\   rC   Ústart_tuningr:   r9   r	   r    Únamer   r   ÚstartrL   r   r%   Úwaitr6   r   r   r
   Úupdate_monitorsÚupdate_tuningÚclearr8   r7   Úis_setrp   Ústop_tuningÚdestroy_all)r(   r+   Ú
_sleep_cntÚiÚfull_rollbackr/   r/   r0   Ú_thread_codeÃ   sV   
ÿ





ø

ÿ


zDaemon._thread_codec              
   C   sJ   z
| j  ||¡ W d S  ty$ } zt t|ƒ¡ W Y d }~d S d }~ww r2   )r%   Úsave_active_profiler   r	   r'   Ústr)r(   r+   rE   r.   r/   r/   r0   r[     s   €ÿzDaemon._save_active_profilec              
   C   sH   z	| j  |¡ W d S  ty# } zt t|ƒ¡ W Y d }~d S d }~ww r2   )r%   Úsave_post_loaded_profiler   r	   r'   r„   )r(   rY   r.   r/   r/   r0   r\     s   €ÿz Daemon._save_post_loaded_profilec                 C   s&   t  d¡ | j ¡ }t  d| ¡ |S )NzWRunning in automatic mode, checking what profile is recommended for your configuration.zUsing '%s' profile)r	   r    r!   Ú	recommend)r(   rO   r/   r/   r0   Ú_get_recommended_profile  s   

zDaemon._get_recommended_profilec                 C   s2   | j  ¡ \}}|d u r|d u}|s|  ¡ }||fS r2   )r%   Úget_active_profiler‡   )r(   rO   rE   r/   r/   r0   r?     s   zDaemon._get_startup_profilec                 C   s   | j j ¡ S )z$Return all accessible plugin classes)r"   Úplugins_repositoryÚload_all_pluginsr;   r/   r/   r0   Úget_all_plugins  s   zDaemon.get_all_pluginsc                 C   s,   z| j j |¡}W |jS  ty   Y dS w )zReturn plugin class docstringr=   )r"   r‰   Úload_pluginÚImportErrorÚ__doc__©r(   Úplugin_nameÚplugin_classr/   r/   r0   Úget_plugin_documentation  s   ÿþÿzDaemon.get_plugin_documentationc                 C   s0   z| j j |¡}W | ¡ S  ty   i  Y S w )zŒReturn plugin's parameters and their hints

		Parameters:
		plugin_name -- plugins name

		Return:
		dictionary -- {parameter_name: hint}
		)r"   r‰   rŒ   r   Úget_config_options_hintsr   r/   r/   r0   Úget_plugin_hints)  s   	ÿþÿzDaemon.get_plugin_hintsc                 C   s
   | j d uS r2   r`   r;   r/   r/   r0   Ú
is_enabled:  s   
zDaemon.is_enabledc                 C   s   | j d uo	| j  ¡ S r2   )r3   Úis_aliver;   r/   r/   r0   rU   =  s   zDaemon.is_runningc                 C   s`   |   ¡ rdS | jd u rdS t d¡ | j ¡  tj| jd| _	| j
 ¡  | j ¡  | j	 ¡  dS )NFzstarting tuning)ÚtargetT)rU   r@   r	   r    r8   r9   r4   ÚThreadr‚   r3   r7   r{   r6   rw   r;   r/   r/   r0   rw   @  s   





zDaemon.startc                 C   s|   |   ¡ st d¡ dS | jd u rt d¡ dS | j ¡ s#t d¡ dS | j ¡  t d| jj	 ¡ | j
 |¡}| j ¡  |S )Nztuned is not runningFzno profile is setzprofile is not appliedzverifying profile(s): %s)rU   r	   r'   r@   r:   r|   r8   r{   r    rv   r"   Úverify_tuningr9   )r(   Úignore_missingÚretr/   r/   r0   Úverify_profileO  s   






zDaemon.verify_profilec                 C   sB   |   ¡ sdS t d¡ |r| j ¡  | j ¡  | j ¡  d | _dS )NFzstopping tuningT)rU   r	   r    r7   r9   r6   r3   rK   )r(   Úprofile_switchr/   r/   r0   Ústope  s   



zDaemon.stop)NNN)F)!Ú__name__Ú
__module__Ú__qualname__r1   r$   r<   r&   rQ   rS   rV   rZ   rD   ÚpropertyrO   rE   rF   rb   r*   rL   rp   r‚   r[   r\   r‡   r?   r‹   r’   r”   r•   rU   rw   rœ   rž   r/   r/   r/   r0   r      sH    
$



ÿ




	>
r   )ÚosÚerrnor4   Ú
tuned.logsÚtunedÚtuned.exceptionsr   Útuned.profiles.exceptionsr   Útuned.constsr   Útuned.utils.commandsr   r   Útuned.utils.profile_recommenderr   rl   Úlogsr   r	   Úobjectr   r/   r/   r/   r0   Ú<module>   s    
