<!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
<e>                 @   s   d dl m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jZd dlmZ d dlZd dlZd dlZd dlZd dlZd dlZG d	d
 d
eZdS )    )print_functionN)commands)Locator   )TunedAdminDBusException)TunedException)ProfileRecommenderc               @   s  e Zd Zdddejejf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d Zdd Zdd Zdd Zdfd d!Zdgd"d#Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Z d8d9 Z!d:d; Z"d<d= Z#d>d? Z$d@dA Z%dBdC Z&dDdE Z'dFdG Z(dHdI Z)dJdK Z*dLdM Z+dNdO Z,dhdQdRZ-didSdTZ.djdUdVZ/dkdWdXZ0dYdZ Z1d[d\ Z2d]d^ Z3d_d` Z4dadb Z5dcdd Z6deS )lAdminTFc             C   s   || _ || _|| _|| _t|| _ttj| _	t
j | _d| _d| _d| _d | _d | _|| _t | _| j rtjjtjtjtj|| _y| jjtj| j W n2 tk
r } z| j| d| _ W Y d d }~X nX d S )N TF) _dbusZ_debug_async_timeoutr   _cmdprofiles_locatorconstsZLOAD_DIRECTORIES_profiles_locator	threadingZEvent_daemon_action_finished_daemon_action_profile_daemon_action_result_daemon_action_errstr_controller
_log_token
_log_levelr   _profile_recommendertunedZadminZDBusControllerZDBUS_BUSZDBUS_INTERFACEZDBUS_OBJECTZset_signal_handlerZSIGNAL_PROFILE_CHANGED_signal_profile_changed_cbr   _error)selfZdbusdebugZasyncotimeoutZ	log_levele r"   /usr/lib/python3.6/admin.py__init__   s*    


zAdmin.__init__c             C   s   t |tjd d S )N)file)printsysstderr)r   messager"   r"   r#   r   +   s    zAdmin._errorc             C   s*   | j j s&|| _|| _|| _| j j  d S )N)r   Zis_setr   r   r   set)r   profile_nameresultZerrstrr"   r"   r#   r   .   s
    
z Admin._signal_profile_changed_cbc             C   sn   yt jt| jjtjd W nJ tk
rF } z|jtj	kS d }~X n$ t
tfk
rh } zdS d }~X nX dS )Nr   FT)oskillintr   Z	read_filer   ZPID_FILEOSErrorerrnoZEPERM
ValueErrorIOError)r   r!   r"   r"   r#   _tuned_is_running6   s    zAdmin._tuned_is_runningc          -   O   s6  |d ks|dkrdS d }d }d}yt | d| }W n( tk
rZ } zd| _W Y d d }~X nX yt | d| }W nB tk
r } z&| js| jt|d|   dS W Y d d }~X nX | jr y0| jj| j | jj|f|| | jj	 }W n4 t
k
r } z| j| d| _W Y d d }~X nX | js2|||}|S )Nr
   FZ_action_dbus_Z_action_z , action '%s' is not implemented)getattrAttributeErrorr   r   strr   Zset_on_exit_action_log_capture_finish
set_actionZrunr   )r   Zaction_nameargskwargsactionZaction_dbusresr!   r"   r"   r#   r<   @   s6    

zAdmin.actionc             C   sh   t d xZ|D ]R}|d d k	rP|d dkrPt | jjd|d  dd|d   qt d|d   qW d S )NzAvailable profiles:r   r
   z- %sr      )r&   r   Z	align_str)r   profile_namesprofiler"   r"   r#   _print_profiles^   s
    
&zAdmin._print_profilesc             C   sd   y| j j }W n6 tk
rD } zdd | j j D }W Y d d }~X nX | j| | j  | j jdS )Nc             S   s   g | ]}|d fqS )r
   r"   ).0r@   r"   r"   r#   
<listcomp>k   s    z4Admin._action_dbus_list_profiles.<locals>.<listcomp>T)r   Z	profiles2r   profilesrA   _action_dbus_activeexit)r   r?   r!   r"   r"   r#   _action_dbus_list_profilesf   s    &
z Admin._action_dbus_list_profilesc             C   s   | j | jj  | j  dS )NT)rA   r   Zget_known_names_summary_action_active)r   r"   r"   r#   _action_list_profilesp   s    zAdmin._action_list_profilesc             C   s&   | j j }|dkrd }| j jd |S )Nr
   T)r   active_profilerF   )r   r+   r"   r"   r#   _dbus_get_active_profileu   s
    
zAdmin._dbus_get_active_profilec             C   s   | j j \}}|S )N)r   get_active_profile)r   r+   manualr"   r"   r#   _get_active_profile|   s    zAdmin._get_active_profilec             C   s.   | j j \}}|d kr|d k	}|r(tjS tjS )N)r   rL   r   ZACTIVE_PROFILE_MANUALZACTIVE_PROFILE_AUTO)r   r@   rM   r"   r"   r#   _get_profile_mode   s    zAdmin._get_profile_modec             C   s   | j j }|dkrd }|S )Nr
   )r   post_loaded_profile)r   r+   r"   r"   r#   _dbus_get_post_loaded_profile   s    
z#Admin._dbus_get_post_loaded_profilec             C   s   | j j }|S )N)r   Zget_post_loaded_profile)r   r+   r"   r"   r#   _get_post_loaded_profile   s    
zAdmin._get_post_loaded_profilec             C   sl   |d dkrXt d t |d  t   t d t |d  t   t d t |d  dS t d	|  d
S d S )Nr   TzProfile name:r   zProfile summary:   zProfile description:   z,Unable to get information about profile '%s'F)r&   )r   r@   profile_infor"   r"   r#   _print_profile_info   s    zAdmin._print_profile_infor
   c             C   sB   |dkr| j  }|r*| j|| jj|}ntd d}| jj|S )Nr
   zNo current active profile.F)rK   rV   r   rU   r&   rF   )r   r@   r=   r"   r"   r#   _action_dbus_profile_info   s    zAdmin._action_dbus_profile_infoc             C   s|   |dkrXy | j  }|d kr&td dS W n. tk
rV } z| jt| dS d }~X nX | j|| jj|tj	tj
gddgS )Nr
   zNo current active profile.F)rN   r&   r   r   r7   rV   r   Zget_profile_attrsr   ZPROFILE_ATTR_SUMMARYZPROFILE_ATTR_DESCRIPTION)r   r@   r!   r"   r"   r#   _action_profile_info   s    zAdmin._action_profile_infoc             C   s$   |d krt d dS t d|  dS )NzNo current active profile.FzCurrent active profile: %sT)r&   )r   r+   r"   r"   r#   _print_profile_name   s
    zAdmin._print_profile_namec             C   s   |rt d|  d S )NzCurrent post-loaded profile: %s)r&   )r   r+   r"   r"   r#   _print_post_loaded_profile   s    z Admin._print_post_loaded_profilec             C   s4   | j  }| j|}|r(| j }| j| | jj|S )N)rK   rY   rQ   rZ   r   rF   )r   rJ   r=   rP   r"   r"   r#   rE      s    

zAdmin._action_dbus_activec             C   s   y2| j  }| j }|r0|r$|d7 }nd}||7 }W n. tk
r` } z| jt| dS d }~X nX |d k	r| j  rtd td|  |rtd|  dS | j|}| j| |S )N r
   FzKIt seems that tuned daemon is not running, preset profile is not activated.zPreset profile: %szPreset post-loaded profile: %sT)	rN   rR   r   r   r7   r4   r&   rY   rZ   )r   r+   rP   r!   r=   r"   r"   r#   rH      s(    


zAdmin._action_activec             C   s   t d|  d S )NzProfile selection mode: )r&   )r   moder"   r"   r#   _print_profile_mode   s    zAdmin._print_profile_modec             C   sB   | j j \}}| j| |dkr6| j| | j jdS | j jdS )Nr
   FT)r   Zprofile_moder]   r   rF   )r   r\   errorr"   r"   r#   _action_dbus_profile_mode   s    

zAdmin._action_dbus_profile_modec             C   sJ   y| j  }| j| dS  tk
rD } z| jt| dS d }~X nX d S )NTF)rO   r]   r   r   r7   )r   r\   r!   r"   r"   r#   _action_profile_mode   s    
zAdmin._action_profile_modec             C   s>   |r,| j j  r:| j j  r:| jd d}n| jd|  |S )NzCannot enable the tuning.FzUnable to switch profile: %s)r   Z
is_runningstartr   )r   retmsgr"   r"   r#   _profile_print_status   s    
zAdmin._profile_print_statusc             C   sr   t j  | j| j kr.td| j  | jjdS | jj rn| j|krn| j	sbtd| j
  | jjdS | jjdS dS )NzOperation timed out after waiting %d seconds(s), you may try to increase timeout by using --timeout command line option or using --async.FzError changing profile: %sT)time
_timestampr   r&   r   rF   r   ZisSetr   r   r   )r   r+   r"   r"   r#   _action_dbus_wait_profile   s    

zAdmin._action_dbus_wait_profilec             C   s|   | j d ks| j dkrd S y2| jj| j }d | _ t|dtjd tjj  W n, tk
rv } z| jd W Y d d }~X nX d S )Nr
   )endr%   zUError: Failed to stop log capture. Restart the TuneD daemon to prevent a memory leak.)	r   r   Zlog_capture_finishr&   r'   r(   flushr   r   )r   Zlog_msgsr!   r"   r"   r#   r8     s    zAdmin._log_capture_finishc             C   s   t |dkr| j S dj|}|dkr2| jjdS | jj  | j rn| jd k	rn| j	d d }| jj
| j|| _| jj|\}}| js| r| jj| j||S tj | _| jj| j| | j||S )Nr   r[   r
   F      )len_action_dbus_listjoinr   rF   r   clearr   r   r   log_capture_startr   Zswitch_profilerd   re   rf   r9   rg   )r   rD   r+   r    rb   rc   r"   r"   r#   _action_dbus_profile  s     


zAdmin._action_dbus_profilec             C   s<   t d | jjdddg\}}|dkr0t d nt d d S )NzTrying to (re)start tuned...Zservicer   Zrestartr   z#TuneD (re)started, changes applied.zQTuneD (re)start failed, you need to (re)start TuneD by hand for changes to apply.)r&   r   Zexecute)r   rb   rc   r"   r"   r#   _restart_tuned+  s
    
zAdmin._restart_tunedc             C   sz   || j j krdy| jj|| | j  dS  tk
r` } z| jt| | jd dS d }~X qvX n| jd|  dS d S )NTzUnable to switch profile.Fz%Requested profile '%s' doesn't exist.)r   Zget_known_namesr   Zsave_active_profilerr   r   r   r7   )r   r+   rM   r!   r"   r"   r#   _set_profile3  s    
zAdmin._set_profilec             C   s6   t |dkr| j S dj|}|dkr*dS | j|dS )Nr   r[   r
   FT)rl   rI   rn   rs   )r   rD   r+   r"   r"   r#   _action_profileA  s    
zAdmin._action_profilec             C   s   | j j }| jj  | j rF| jd k	rF| jd d }| j j| j|| _| j j	 \}}| js`| rt| j j
| j||S tj | _| j j| j| | j||S )Nrj   rk   )r   recommend_profiler   ro   r   r   r   rp   r   Zauto_profilerF   rd   re   rf   r9   rg   )r   r+   r    rb   rc   r"   r"   r#   _action_dbus_auto_profileI  s    


zAdmin._action_dbus_auto_profilec             C   s   | j j }| j|dS )NF)r   	recommendrs   )r   r+   r"   r"   r#   _action_auto_profileY  s    
zAdmin._action_auto_profilec             C   s   t | jj  | jjdS )NT)r&   r   ru   rF   )r   r"   r"   r#   _action_dbus_recommend_profile]  s    z$Admin._action_dbus_recommend_profilec             C   s   t | jj  dS )NT)r&   r   rw   )r   r"   r"   r#   _action_recommend_profilea  s    zAdmin._action_recommend_profilec             C   sr   |r| j j }n
| j j }|r(td n0td td td td td td tdtj  | j 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.)r   Zverify_profile_ignore_missingZverify_profiler&   r   ZLOG_FILErF   )r   ignore_missingrb   r"   r"   r#   _action_dbus_verify_profilee  s    

z!Admin._action_dbus_verify_profilec             C   s   t d dS )Nz Not supported in no_daemon mode.F)r&   )r   r|   r"   r"   r#   _action_verify_profilev  s    zAdmin._action_verify_profilec             C   s:   d}| j j| j|| _| j j }|s.| jd | j j|S )Nrj   rk   zCannot disable active profile.r>   )r   rp   r   r   Zoffr   rF   )r   r    rb   r"   r"   r#   _action_dbus_offz  s    

zAdmin._action_dbus_offc             C   s   t d dS )Nz Not supported in no_daemon mode.F)r&   )r   r"   r"   r#   _action_off  s    zAdmin._action_offrD   c             C   s(   |dkr| j  S |dkr$| j|dS dS )aD  Print 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
		rD   plugins)verboseN)rG   _action_dbus_list_plugins)r   list_choicer   r"   r"   r#   rm     s    	zAdmin._action_dbus_listc             C   s(   |dkr| j  S |dkr$| j|dS dS )aa  Print 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
		rD   r   )r   N)rI   _action_list_plugins)r   r   r   r"   r"   r#   _action_list  s    	zAdmin._action_listc             C   s   | j j }xv|j D ]j}t| | st|| dkr8q| j j|}x8|| D ],}td|  |j|d}|rNtd|  qNW qW | j jdS )zvPrint accessible plugins

		Keyword arguments:
		verbose -- if is set to True then parameters and hints are printed
		r   z	%sNz		%sT)r   Zget_pluginskeysr&   rl   Zget_plugin_hintsgetrF   )r   r   r   pluginZhintsZ	parameterZhintr"   r"   r#   r     s    
zAdmin._action_dbus_list_pluginsc             C   s   t d dS )Nz Not supported in no_daemon mode.F)r&   )r   r   r"   r"   r#   r     s    zAdmin._action_list_pluginsc             C   s0   | j j||\}}|s$| jd|  | j j|S )NzUnable to acquire devices: %s)r   Zinstance_acquire_devicesr   rF   )r   devicesinstancerb   rc   r"   r"   r#   %_action_dbus_instance_acquire_devices  s    z+Admin._action_dbus_instance_acquire_devicesc             C   s   t d dS )Nz Not supported in no_daemon mode.F)r&   )r   r   r   r"   r"   r#    _action_instance_acquire_devices  s    z&Admin._action_instance_acquire_devicesc             C   s^   | j j|\}}}|s0| jd|  | j jdS x |D ]\}}td||f  q6W | j jdS )NzUnable to list instances: %sFz%s (%s)T)r   Zget_instancesr   rF   r&   )r   plugin_namerb   rc   Zpairsr   r   r"   r"   r#   _action_dbus_get_instances  s    z Admin._action_dbus_get_instancesc             C   s   t d dS )Nz Not supported in no_daemon mode.F)r&   )r   r   r"   r"   r#   _action_get_instances  s    zAdmin._action_get_instancesc             C   sR   | j j|\}}}|s0| jd|  | j jdS x|D ]}t| q6W | j jdS )NzUnable to list devices: %sFT)r   Zinstance_get_devicesr   rF   r&   )r   r   rb   rc   r   Zdevicer"   r"   r#   !_action_dbus_instance_get_devices  s    
z'Admin._action_dbus_instance_get_devicesc             C   s   t d dS )Nz Not supported in no_daemon mode.F)r&   )r   r   r"   r"   r#   _action_instance_get_devices  s    z"Admin._action_instance_get_devicesN)r
   )r
   )rD   F)rD   F)F)F)7__name__
__module____qualname__r   ZADMIN_TIMEOUTloggingZERRORr$   r   r   r4   r<   rA   rG   rI   rK   rN   rO   rQ   rR   rV   rW   rX   rY   rZ   rE   rH   r]   r_   r`   rd   rg   r8   rq   rr   rs   rt   rv   rx   ry   rz   r}   r~   r   r   rm   r   r   r   r   r   r   r   r   r   r"   r"   r"   r#   r	      sd   




		




		r	   )Z
__future__r   Ztuned.adminr   Ztuned.utils.commandsr   Ztuned.profilesr   r   
exceptionsr   Ztuned.exceptionsr   Ztuned.constsr   Ztuned.utils.profile_recommenderr   r-   r'   r1   re   r   r   objectr	   r"   r"   r"   r#   <module>   s   
