<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
        integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
</html>
3
<e3                 @   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	j
Z
d dlmZ d dlmZ d dlmZ d dlZej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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-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd=d9d:ZdS )>DaemonNc             C   s  t jd tj| _ttj| _ttj| _	tj
| _d| _tj| _|d k	r|jtjtj| _t|jtjtj| _t|jtjtj| _	|jtjtj
| _|jtjtj| _|jtjtj| _|| _| jdkrttj| _| j	dkrd| _n| j	| jk r| j| _	| j	| j | _t jd| j  | jrJt jd t jd| j| j | jf  t| j d| _|| _|| _| j  t  | _!y| j"| W n2 t#k
r } zt j$d	|  W Y d d }~X nX d S )
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))Zis_hardcodedz:Cannot set initial profile. No tunings will be enabled: %s)%logdebugconstsZCFG_DEF_DAEMON_daemonintZCFG_DEF_SLEEP_INTERVAL_sleep_intervalZCFG_DEF_UPDATE_INTERVALZ_update_intervalZCFG_DEF_DYNAMIC_TUNING_dynamic_tuningZ_recommend_commandZCFG_DEF_ROLLBACK	_rollbackZget_boolZ
CFG_DAEMONgetZCFG_SLEEP_INTERVALZCFG_UPDATE_INTERVALZCFG_DYNAMIC_TUNINGZCFG_RECOMMEND_COMMANDZCFG_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    



zDaemon.__init__c             C   sF   d | _ tj | _tj | _tj | _tj | _| jj  tj | _d S )N)	_thread	threadingZEvent
_terminate_terminate_profile_switch	_not_used_sighup_processingset_profile_applied)r   r!   r!   r"   r   7   s    




zDaemon._init_threadsc             C   s   | j d dS )zARead configuration files again and load profile according to themN)r   )r   r!   r!   r"   reload_profile_configC   s    zDaemon.reload_profile_configc             C   s   d}| j j }|d krP| j \}}|d krrd}|r<|d7 }n|d7 }tj| n"|dkrr|rhtjd n
tjd d | _d | _g | _d | _| j	||| 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   Z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!   r"   r   G   s&    


zDaemon._init_profilec             C   s   |pd}|j  }| jr2tjd| j  || jg }x:|D ]2}|| jjj kr8d| }| j|d| t|q8W y.|r| j	j
|| _nd | _|| _|j  | _W nJ tk
r } z.ddj||f }| j|d| t|W Y d d }~X nX d S )Nr-   zUsing post-loaded profile '%s'z%Requested profile '%s' doesn't exist.FzCannot load profile(s) '%s': %s )splitr2   r   r   r   Zprofile_locatorZget_known_names_notify_profile_changedr   r   loadr/   r0   r1   r   join)r   r   r4   Zprofile_listprofileerrstrr    r!   r!   r"   _load_profiles`   s*    

zDaemon._load_profilesc             C   s2   | j  r"d}| j|d| t|| j|| d S )Nz/Cannot set profile while the daemon is running.F)
is_runningr9   r   r>   )r   r   r4   r=   r!   r!   r"   set_profilez   s    zDaemon.set_profilec             C   s4   |sd | _ n$t|j dkr*d}t|n|| _ d S )N   zYWhitespace is not allowed in profile names; only a single post-loaded profile is allowed.)r2   lenr8   r   )r   profile_namer=   r!   r!   r"   _set_post_loaded_profile   s    
zDaemon._set_post_loaded_profileFc             C   sV   | j  r"d}| j|d| t|| j| | j|| |rR| j|| | j| d S )Nz/Cannot set profile while the daemon is running.F)r?   r9   r   rD   r>   _save_active_profile_save_post_loaded_profile)r   Zactive_profilesr4   r5   Zsave_instantlyr=   r!   r!   r"   r3      s    
zDaemon.set_all_profilesc             C   s   | j S )N)r/   )r   r!   r!   r"   r<      s    zDaemon.profilec             C   s   | j S )N)r0   )r   r!   r!   r"   r4      s    zDaemon.manualc             C   s   | j r| jS d S )N)r/   r2   )r   r!   r!   r"   r5      s    zDaemon.post_loaded_profilec             C   s   | j S )N)r   )r   r!   r!   r"   profile_recommender   s    zDaemon.profile_recommenderc             C   s   | j S )N)r   )r   r!   r!   r"   r      s    zDaemon.profile_loaderc             C   s    | j d k	rtjtj||| |S )N)r   r   Zsend_signalr
   ZSIGNAL_PROFILE_CHANGED)r   r   resultr=   r!   r!   r"   r9      s    
zDaemon._notify_profile_changedc             C   sj   | 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jd	|d koh| S )
NZ	systemctlzis-system-runningr   )Z	no_errorsF   Zstoppingz	list-jobsz0\b(shutdown|reboot|halt|poweroff)\.target.*start)r   Zexecuteresearch)r   Zretcodeoutr!   r!   r"   _full_rollback_required   s    zDaemon._full_rollback_requiredc             C   s  | j d krtd| jj| j j | jdj| j| j | j	| j
 | jj  | jj  tjd| j j  | jrxtj  dj| j}| j|dd | jj  | jr| j}x\| jj| j| js| jr|d8 }|dkr| j}tjd | jj  tjd	 | jj  qW | jj  d}x.| jj| j | j rD|d
k rD|d7 }qW | j!j" rZt#j$}njt#j%}| j& svtjd nN| j'dkrt#j(}tjdt#j)t#j*f  n$| jrtjd t#j$}n
tjd | jr| jj+| | jj,  d S )Nz2Cannot start the daemon without setting a profile.r7   z'static tuning from profile '%s' appliedTZOKrA   r   zupdating monitorszperforming 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/   r   r   ZcreateZunitsrE   r;   r1   r0   rF   r2   Zstart_tuningr+   r*   r   r   namer   r   startr9   r)   clearr   r   waitr&   r   r   r	   Zupdate_monitorsZupdate_tuningr(   r'   is_setr
   ZROLLBACK_FULLZROLLBACK_SOFTrM   r   ZROLLBACK_NONEr   ZGLOBAL_CONFIG_FILEZstop_tuningZdestroy_all)r   r   Z
_sleep_cntiZrollbackr!   r!   r"   _thread_code   sX    







"



zDaemon._thread_codec             C   sH   y| j j|| W n0 tk
rB } ztjt| W Y d d }~X nX d S )N)r   Zsave_active_profiler   r   r   str)r   r   r4   r    r!   r!   r"   rE     s    zDaemon._save_active_profilec             C   sF   y| j j| W n0 tk
r@ } ztjt| W Y d d }~X nX d S )N)r   Zsave_post_loaded_profiler   r   r   rV   )r   rC   r    r!   r!   r"   rF     s    z Daemon._save_post_loaded_profilec             C   s&   t jd | jj }t jd|  |S )NzWRunning in automatic mode, checking what profile is recommended for your configuration.zUsing '%s' profile)r   r   r   Z	recommend)r   r<   r!   r!   r"   _get_recommended_profile  s    

zDaemon._get_recommended_profilec             C   s2   | j j \}}|d kr|d k	}|s*| j }||fS )N)r   Zget_active_profilerW   )r   r<   r4   r!   r!   r"   r.     s    zDaemon._get_startup_profilec             C   s   | j jj S )z$Return all accessible plugin classes)r   plugins_repositoryZload_all_plugins)r   r!   r!   r"   get_all_plugins"  s    zDaemon.get_all_pluginsc             C   s.   y| j jj|}W n tk
r&   dS X |jS )zReturn plugin class docstringr-   )r   rX   load_pluginImportError__doc__)r   plugin_nameplugin_classr!   r!   r"   get_plugin_documentation&  s    
zDaemon.get_plugin_documentationc             C   s0   y| j jj|}W n tk
r&   i S X |j S )zReturn plugin's parameters and their hints

		Parameters:
		plugin_name -- plugins name

		Return:
		dictionary -- {parameter_name: hint}
		)r   rX   rZ   r[   Zget_config_options_hints)r   r]   r^   r!   r!   r"   get_plugin_hints0  s    	
zDaemon.get_plugin_hintsc             C   s
   | j d k	S )N)r/   )r   r!   r!   r"   
is_enabledA  s    zDaemon.is_enabledc             C   s   | j d k	o| j j S )N)r$   Zis_alive)r   r!   r!   r"   r?   D  s    zDaemon.is_runningc             C   s`   | j  rdS | jd krdS tjd | jj  tj| jd| _	| j
j  | jj  | j	j  dS )NFzstarting tuning)targetT)r?   r/   r   r   r(   r*   r%   ZThreadrU   r$   r'   rQ   r&   rP   )r   r!   r!   r"   rP   G  s    





zDaemon.startc             C   s|   | j  stjd dS | jd kr.tjd dS | jj sFtjd dS | jj  tjd| jj	  | j
j|}| jj  |S )NzTuneD is not runningFzno profile is setzprofile is not appliedzverifying profile(s): %s)r?   r   r   r/   r+   rS   r(   rQ   r   rO   r   Zverify_tuningr*   )r   Zignore_missingretr!   r!   r"   verify_profileV  s    






zDaemon.verify_profilec             C   sB   | j  sdS tjd |r$| jj  | jj  | jj  d | _dS )NFzstopping tuningT)r?   r   r   r'   r*   r&   r$   r;   )r   Zprofile_switchr!   r!   r"   stopl  s    



zDaemon.stop)NNN)F)F) __name__
__module____qualname__r#   r   r,   r   r>   r@   rD   r3   propertyr<   r4   r5   rG   r   r9   rM   rU   rE   rF   rW   r.   rY   r_   r`   ra   r?   rP   rd   re   r!   r!   r!   r"   r      s8   
&	

	G
r   )oserrnor%   Z
tuned.logsZtunedZtuned.exceptionsr   Ztuned.profiles.exceptionsr   Ztuned.constsr
   Ztuned.utils.commandsr   r   Ztuned.utils.profile_recommenderr   rJ   Zlogsr   r   objectr   r!   r!   r!   r"   <module>   s   

