<!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
`g4                 @   s   d dl mZ d dlZ d dlZ d dlmZ d dlZd dljZd dlm	Z	 dgZ
e jj ZG dd deZG dd de jjjZdS )	    )exportsN)TunedException)commands
Controllerc               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )

TimerStorec             C   s   t  | _tj | _d S )N)dict_timers	threadingZLock_timers_lock)self r    /usr/lib/python3.6/controller.py__init__   s    zTimerStore.__init__c          
   C   s    | j  || j|< W d Q R X d S )N)r
   r   )r   tokentimerr   r   r   store_timer   s    zTimerStore.store_timerc             C   sB   | j 2 y| j| }|j  | j|= W n   Y nX W d Q R X d S )N)r
   r   cancel)r   r   r   r   r   r   
drop_timer   s    
zTimerStore.drop_timerc          	   C   s<   | j , x| jj D ]}|j  qW | jj  W d Q R X d S )N)r
   r   valuesr   clear)r   r   r   r   r   
cancel_all   s    zTimerStore.cancel_allN)__name__
__module____qualname__r   r   r   r   r   r   r   r   r      s   	r   c                   s  e Zd ZdZ fddZdd Zdd Zdd	 Zej	d
dd Z
dd ZejdddUddZejdddVddZejdddWddZdXddZejdddYddZejdddZdd Zd!d" Zejdd#d[d$d%Zejdd#d\d&d'Zejddd]d(d)Zejdd*d^d+d,Zejddd_d-d.Zejddd`d/d0Zejdddad1d2Zejdd3dbd4d5Zejdd6dcd7d8Zejdd9ddd:d;Zejddded<d=Zejdddfd>d?Zejdddgd@dAZ ejddBdhdCdDZ!ejdddidEdFZ"ejddGdjdHdIZ#ejdddkdJdKZ$ejdLd#dldMdNZ%ejddOdmdPdQZ&ejddRdndSdTZ'  Z(S )or   z
	Controller's purpose is to keep the program running, start/stop the tuning,
	and export the controller interface (currently only over D-Bus).
	c                s8   t t| j  || _|| _tj | _t | _	t
 | _d S )N)superr   r   _daemon_global_configr	   ZEvent
_terminater   _cmdr   _timer_store)r   daemonZglobal_config)	__class__r   r   r   +   s    
zController.__init__c             C   sx   t jd | j }| jjtjtj}| r6|r6tj  |rb| j	j
  x| jj| j	ds`tj  qFW t jd | j  dS )z1
		Controller main loop. The call is blocking.
		zstarting controller   zterminating controllerN)loginfostartr   get_boolconsts
CFG_DAEMONCFG_DEF_DAEMONr   r   r   r   waitZperiod_checkstop)r   resr    r   r   r   run3   s    



zController.runc             C   s   | j j  d S )N)r   set)r   r   r   r   	terminateF   s    zController.terminatec             C   s0   | j jj s,| j jj  | j s,| j jj  d S )N)r   Z_sighup_processingZis_setr.   reloadr   )r   r   r   r   sighupI   s    zController.sighupZsbsc             C   s   d S )Nr   )r   profile_nameresultZerrstrr   r   r   profile_changedO   s    zController.profile_changedc             C   s   t jj| | jj| d S )N)tunedlogslog_capture_finishr   r   )r   r   r   r   r   _log_capture_abortX   s    zController._log_capture_abortZiisNc             C   sf   |dkrdS t jj|}|d kr$dS |dkrVtj|| j|gd}| jj|| |j  |d krbdS |S )N r   )args)	r5   r6   log_capture_startr	   ZTimerr8   r   r   r%   )r   Z	log_levelZtimeoutcallerr   r   r   r   r   r<   \   s    zController.log_capture_startc             C   s4   |dkrdS t jj|}| jj| |d kr0dS |S )Nr:   )r5   r6   r7   r   r   )r   r   r=   r,   r   r   r   r7   j   s
    zController.log_capture_finishr:   bc             C   sD   |dkrdS | j jtjtjr:| jj r,dS | jj s:dS | jj S )Nr:   FT)	r   r&   r'   r(   r)   r   
is_running
is_enabledr%   )r   r=   r   r   r   r%   r   s    

zController.startFc             C   s,   | j j sd}n| j j|d}| jj  |S )NT)profile_switch)r   r?   r+   r   r   )r   rA   r,   r   r   r   _stop}   s
    

zController._stopc             C   s   |dkrdS | j ddS )Nr:   F)rA   )rB   )r   r=   r   r   r   r+      s    zController.stopc             C   sp   |dkrdS | j j r*| jdd}|s*dS y| j j  W n. tk
rf } ztjd|  dS d }~X nX | j S )Nr:   FT)rA   zFailed to reload TuneD: %s)r   r?   rB   Zreload_profile_configr   r#   errorr%   )r   r=   Zstop_oker   r   r   r0      s    
zController.reloadc             C   s   | j j }d}d}d}zy$|r,| j jdd | j j|| W nf tjjk
r } zFd}t|}|r| j jj	|krt
jd|  d}nt
jd|  W Y d d }~X nX W d |r|rt
jd|  n|st
jd | j j  X ||fS )	NOKTF)rA   z@Failed to reapply profile '%s'. Did it change on disk and break?zFailed to apply profile '%s'z>Applying previously applied (possibly out-dated) profile '%s'.z$Applying previously applied profile.)r   r?   r+   Zset_profiler5   
exceptionsr   strprofilenamer#   rC   warnr$   r%   )r   r2   manualZwas_runningmsgsuccessZreapplyrD   r   r   r   _switch_profile   s,    
$
zController._switch_profilez(bs)c             C   s(   |dkrdS | j j|sdS | j|dS )Nr:   FUnauthorizedInvalid profile_nameT)FrO   )FrP   )r   is_valid_namerN   )r   r2   r=   r   r   r   switch_profile   s
    zController.switch_profilec             C   s    |dkrdS | j  }| j|dS )Nr:   FrO   )FrO   )recommend_profilerN   )r   r=   r2   r   r   r   auto_profile   s    zController.auto_profilec             C   s*   |dkrdS | j jd k	r"| j jjS dS d S )Nr:   )r   rH   rI   )r   r=   r   r   r   active_profile   s
    
zController.active_profilez(ss)c             C   s   |dkrdS | j j}|d krpy"| jj \}}|d kr<|d k	}W n0 tk
rn } zd}t|}||fS d }~X nX |rztjntj}|dfS )Nr:   unknownrO   )rV   rO   )	r   rK   r   Zget_active_profiler   rG   r'   ZACTIVE_PROFILE_MANUALZACTIVE_PROFILE_AUTO)r   r=   rK   rH   rD   moderC   r   r   r   profile_mode   s    zController.profile_modec             C   s   |dkrdS | j jpdS )Nr:   )r   post_loaded_profile)r   r=   r   r   r   rY      s    zController.post_loaded_profilec             C   sB   |dkrdS | j j r | j j  | j j r>| j jd dd dd dS )Nr:   FT)Zsave_instantly)r   r?   r+   r@   Zset_all_profiles)r   r=   r   r   r   disable   s    


zController.disablec             C   s   |dkrdS | j j S )Nr:   F)r   r?   )r   r=   r   r   r   r?      s    zController.is_runningasc             C   s   |dkrg S | j jjj S )Nr:   )r   profile_loaderprofile_locatorZget_known_names)r   r=   r   r   r   profiles   s    zController.profilesza(ss)c             C   s   |dkrg S | j jjj S )Nr:   )r   r\   r]   Zget_known_names_summary)r   r=   r   r   r   	profiles2   s    zController.profiles2z(bsss)c             C   sT   |dks| j j| rdS |d ks*|dkr2| j }t| jjjj|tj	tj
gdgS )Nr:   F)Fr:   r:   r:   )r   rQ   rU   tupler   r\   r]   Zget_profile_attrsr'   ZPROFILE_ATTR_SUMMARYZPROFILE_ATTR_DESCRIPTION)r   r2   r=   r   r   r   profile_info  s
    zController.profile_infoc             C   s   |dkrdS | j jj S )Nr:   )r   Zprofile_recommenderZ	recommend)r   r=   r   r   r   rS   
  s    zController.recommend_profilec             C   s   |dkrdS | j jddS )Nr:   F)ignore_missing)r   verify_profile)r   r=   r   r   r   rc     s    zController.verify_profilec             C   s   |dkrdS | j jddS )Nr:   FT)rb   )r   rc   )r   r=   r   r   r   verify_profile_ignore_missing  s    z(Controller.verify_profile_ignore_missingz	a{sa{ss}}c             C   sz   |dkri S i }xd| j j D ]V}|jjdd jddd }|j }i ||< x$|j D ]\}}t||| |< qVW qW |S )zuReturn dictionary with accesible plugins

		Return:
		dictionary -- {plugin_name: {parameter_name: default_value}}
		r:   .r"   _)r   get_all_pluginsr   splitZ_get_config_optionsitemsrG   )r   r=   ZpluginsZplugin_classplugin_nameZconf_optionskeyvalr   r   r   rh     s    zController.get_all_pluginsc             C   s*   |dks| j j| rdS | jjt|S )z"Return docstring of plugin's classr:   )r   rQ   r   get_plugin_documentationrG   )r   rk   r=   r   r   r   rn   .  s    z#Controller.get_plugin_documentationza{ss}c             C   s*   |dks| j j| ri S | jjt|S )zReturn dictionary with plugin's parameters and their hints

		Parameters:
		plugin_name -- name of plugin

		Return:
		dictionary -- {parameter_name: hint}
		r:   )r   rQ   r   get_plugin_hintsrG   )r   rk   r=   r   r   r   ro   5  s    
zController.get_plugin_hintsc             C   sD   |dks| j j| rdS | jjr@| jjjr@| jjjj| dS dS )zAllows to dynamically add sockets to send signals to

		Parameters:
		path -- path to socket to register for sending signals

		Return:
		bool -- True on success
		r:   FT)r   rQ   r   Z_applicationZ_unix_socket_exporterZregister_signal_path)r   pathr=   r   r   r   register_socket_signal_pathC  s    
z&Controller.register_socket_signal_pathZssc       
      C   s  |dkrdS | j j|sdS | j j|s,dS d}x2| jjjD ]$}|j|kr<tjd|j  d}P q<W |sd| }tj| d|fS t	| j j
|}tjd	|jt|f  x| jjjD ]}|j|@ }	t|	r||	8 }tjd
t|	|j|jf  |jj|jjkr6d|j|jjt|	|j|jjf }tj| d|fS |jj||	 |jj||	 qW t|r~dt| }tj| d|fS dS )Nr:   FrO   Invalid devicesInvalid instance_namezFound instance '%s'.TzInstance '%s' not foundz-Instance '%s' trying to acquire devices '%s'.z8Moving 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'.rE   )FrO   )Frr   )Frs   )TrE   )r   rQ   r   _unit_manager	instancesrI   r#   debugrC   r.   Zdevstr2devsrG   processed_deviceslenr$   pluginZ_remove_devices_nocheckZ_add_devices_nocheck)
r   Zdevicesinstance_namer=   foundZinstance_targetretsZdevsinstanceZdevs_movingr   r   r   instance_acquire_devicesW  sJ    





z#Controller.instance_acquire_devicesz	(bsa(ss))c                s   |dkrddg fS | j j s(ddg fS  dkr\ | j j kr\d  }tj| d|g fS tdd | jjj	} dkrt fdd|}d	d
t
tdd |fS )a  Return 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)]
		r:   FrO   zInvalid plugin_namezPlugin '%s' does not existc             S   s   | j S )N)Zactive)r}   r   r   r   <lambda>  s    z*Controller.get_instances.<locals>.<lambda>c                s   | j j kS )N)ry   rI   )r}   )rk   r   r   r     s    TrE   c             S   s   | j | jj fS )N)rI   ry   )r}   r   r   r   r     s    )r   rQ   rh   keysr#   rC   filterr   rt   ru   listmap)r   rk   r=   r|   ru   r   )rk   r   get_instances  s    



zController.get_instancesz(bsas)c             C   sv   |dkrddg fS | j j|s(ddg fS x0| jjjD ]"}|j|kr4ddtt|jfS q4W d| }t	j
| d|g fS )zReturn 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
		r:   FrO   zInvalid instance_nameTrE   zInstance '%s' not found)r   rQ   r   rt   ru   rI   sortedr   rw   r#   rC   )r   rz   r=   r}   r|   r   r   r   instance_get_devices  s    



zController.instance_get_devices)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))r   r   r   __doc__r   r-   r/   r1   r   signalr4   r8   Zexportr<   r7   r%   rB   r+   r0   rN   rR   rT   rU   rX   rY   rZ   r?   r^   r_   ra   rS   rc   rd   rh   rn   ro   rq   r~   r   r   __classcell__r   r   )r!   r   r   %   sv   	

























'

)r5   r   Z
tuned.logsZtuned.exceptionsr   r	   Ztuned.constsr'   Ztuned.utils.commandsr   __all__r6   getr#   objectr   Z
interfacesZExportableInterfacer   r   r   r   r   <module>   s   

