o
    •Û_²9  ã                   @   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m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                   @   sx  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dZd d!„ZdZd"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,d[dQdR„Z-d[dSdT„Z.d\dUdV„Z/d\dWdX„Z0dYS )]ÚAdminTFc              
   C   sØ   || _ || _|| _|| _t|ƒ| _ttjƒ| _	t
 ¡ | _d| _d| _d| _d | _d | _|| _tƒ | _| j rjtj tjtjtj|¡| _z| j tj| j¡ W d S  tyi } z|  |¡ d| _ W Y d }~d S d }~ww d S )NÚ TF) Ú_dbusÚ_debugÚ_asyncÚ_timeoutr   Ú_cmdÚprofiles_locatorÚconstsÚLOAD_DIRECTORIESÚ_profiles_locatorÚ	threadingÚEventÚ_daemon_action_finishedÚ_daemon_action_profileÚ_daemon_action_resultÚ_daemon_action_errstrÚ_controllerÚ
_log_tokenÚ
_log_levelr   Ú_profile_recommenderÚtunedÚadminÚDBusControllerÚDBUS_BUSÚDBUS_INTERFACEÚDBUS_OBJECTÚset_signal_handlerÚDBUS_SIGNAL_PROFILE_CHANGEDÚ_signal_profile_changed_cbr   Ú_error)ÚselfÚdbusÚdebugÚasyncoÚtimeoutÚ	log_levelÚe© r/   ú3/usr/lib/python3/dist-packages/tuned/admin/admin.pyÚ__init__   s0   


€þüzAdmin.__init__c                 C   s   t |tjd d S )N)Úfile)ÚprintÚsysÚstderr)r(   Úmessager/   r/   r0   r'   +   s   zAdmin._errorc                 C   s.   | j  ¡ s|| _|| _|| _| j  ¡  d S d S ©N)r   Úis_setr   r   r   Úset)r(   Úprofile_nameÚresultÚerrstrr/   r/   r0   r&   .   s   
üz Admin._signal_profile_changed_cbc              
   C   sz   zt  t| j tj¡ƒd¡ W dS  ty( } z|jtj	kW  Y d }~S d }~w t
tfy< } zW Y d }~dS d }~ww )Nr   FT)ÚosÚkillÚintr   Ú	read_filer   ÚPID_FILEÚOSErrorÚerrnoÚEPERMÚ
ValueErrorÚIOError)r(   r.   r/   r/   r0   Ú_tuned_is_running6   s   ü€€ÿzAdmin._tuned_is_runningc              
   O   sJ  |d u s|dkr
dS d }d }d}z	t | d| ƒ}W n ty. } z	d| _W Y d }~nd }~ww z	t | d| ƒ}W n' ty_ } z| jsU|  t|ƒd|  ¡ W Y d }~dS W Y d }~nd }~ww | jr™z| j | j¡ | jj|g|¢R i |¤Ž | j 	¡ }W n t
y˜ } z|  |¡ d| _W Y d }~nd }~ww | js£||i |¤Ž}|S )Nr
   FÚ_action_dbus_Ú_action_z , action '%s' is not implemented)ÚgetattrÚAttributeErrorr   r'   Ústrr   Úset_on_exit_actionÚ_log_capture_finishÚ
set_actionÚrunr   )r(   Úaction_nameÚargsÚkwargsÚactionÚaction_dbusÚresr.   r/   r/   r0   rT   @   sF   €ÿþ€ÿÿ
€þzAdmin.actionc              	   C   sd   t dƒ |D ])}|d d ur'|d dkr't | j d|d  dd|d  ¡ƒ qt d|d  ƒ qd S )NzAvailable profiles:r   r
   z- %sr   é   )r3   r   Ú	align_str)r(   Úprofile_namesÚprofiler/   r/   r0   Ú_print_profiles^   s   &üzAdmin._print_profilesc              
   C   sf   z| j  ¡ }W n ty# } zdd„ | j  ¡ D ƒ}W Y d }~nd }~ww |  |¡ |  ¡  | j  d¡S )Nc                 S   s   g | ]}|d f‘qS ©r
   r/   )Ú.0rZ   r/   r/   r0   Ú
<listcomp>k   s    z4Admin._action_dbus_list_profiles.<locals>.<listcomp>T)r   Ú	profiles2r   Úprofilesr[   Ú_action_dbus_activeÚexit)r(   rY   r.   r/   r/   r0   Ú_action_dbus_list_profilesf   s    €þ
z Admin._action_dbus_list_profilesc                 C   s   |   | j ¡ ¡ |  ¡  dS ©NT)r[   r   Úget_known_names_summaryÚ_action_active©r(   r/   r/   r0   Ú_action_list_profilesp   s   zAdmin._action_list_profilesc                 C   s&   | j  ¡ }|dkrd }| j  d¡ |S )Nr
   T)r   Úactive_profilerb   ©r(   r:   r/   r/   r0   Ú_dbus_get_active_profileu   s
   
zAdmin._dbus_get_active_profilec                 C   s   | j  ¡ \}}|S r7   )r   Úget_active_profile)r(   r:   Úmanualr/   r/   r0   Ú_get_active_profile|   ó   zAdmin._get_active_profilec                 C   s.   | j  ¡ \}}|d u r|d u}|rtjS tjS r7   )r   rl   r   ÚACTIVE_PROFILE_MANUALÚACTIVE_PROFILE_AUTO)r(   rZ   rm   r/   r/   r0   Ú_get_profile_mode€   s   zAdmin._get_profile_modec                 C   s   | j  ¡ }|dkrd }|S )Nr
   )r   Úpost_loaded_profilerj   r/   r/   r0   Ú_dbus_get_post_loaded_profile†   s   
z#Admin._dbus_get_post_loaded_profilec                 C   s   | j  ¡ }|S r7   )r   Úget_post_loaded_profilerj   r/   r/   r0   Ú_get_post_loaded_profileŒ   s   
zAdmin._get_post_loaded_profilec                 C   sh   |d dkr,t dƒ t |d ƒ t ƒ  t dƒ t |d ƒ t ƒ  t dƒ t |d ƒ dS t d	| ƒ d
S )Nr   TzProfile name:r   zProfile summary:é   zProfile description:é   z,Unable to get information about profile '%s'F©r3   )r(   rZ   Úprofile_infor/   r/   r0   Ú_print_profile_info   s   zAdmin._print_profile_infor
   c                 C   sB   |dkr|   ¡ }|r|  || j |¡¡}ntdƒ d}| j |¡S ©Nr
   úNo current active profile.F)rk   r{   r   rz   r3   rb   )r(   rZ   rV   r/   r/   r0   Ú_action_dbus_profile_infoŸ   s   zAdmin._action_dbus_profile_infoc              
   C   s„   |dkr0z|   ¡ }|d u rtdƒ W dS W n ty/ } z|  t|ƒ¡ W Y d }~dS d }~ww |  || j |tj	tj
gddg¡¡S r|   )rn   r3   r   r'   rL   r{   r   Úget_profile_attrsr   ÚPROFILE_ATTR_SUMMARYÚPROFILE_ATTR_DESCRIPTION)r(   rZ   r.   r/   r/   r0   Ú_action_profile_info©   s   þ€þ$zAdmin._action_profile_infoc                 C   s$   |d u r
t dƒ dS t d| ƒ dS )Nr}   FzCurrent active profile: %sTry   rj   r/   r/   r0   Ú_print_profile_nameµ   s
   zAdmin._print_profile_namec                 C   s   |r
t d| ƒ d S d S )NzCurrent post-loaded profile: %sry   rj   r/   r/   r0   Ú_print_post_loaded_profile½   s   ÿz Admin._print_post_loaded_profilec                 C   s4   |   ¡ }|  |¡}|r|  ¡ }|  |¡ | j |¡S r7   )rk   rƒ   rt   r„   r   rb   )r(   ri   rV   rs   r/   r/   r0   ra   Á   s   

zAdmin._action_dbus_activec              
   C   s¸   z|   ¡ }|  ¡ }|r|r|d7 }nd}||7 }W n ty3 } z|  t|ƒ¡ W Y d }~dS d }~ww |d urP|  ¡ sPtdƒ td| ƒ |rNtd| ƒ dS |  |¡}|  |¡ |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   rv   r   r'   rL   rG   r3   rƒ   r„   )r(   r:   rs   r.   rV   r/   r/   r0   rf   É   s.   
€€þ

zAdmin._action_activec                 C   s   t d| ƒ d S )NzProfile selection mode: ry   )r(   Úmoder/   r/   r0   Ú_print_profile_modeâ   s   zAdmin._print_profile_modec                 C   sB   | j  ¡ \}}|  |¡ |dkr|  |¡ | j  d¡S | j  d¡S )Nr
   FT)r   Úprofile_moder‡   r'   rb   )r(   r†   Úerrorr/   r/   r0   Ú_action_dbus_profile_modeå   s   

zAdmin._action_dbus_profile_modec              
   C   sN   z|   ¡ }|  |¡ W dS  ty& } z|  t|ƒ¡ W Y d }~dS d }~ww )NTF)rr   r‡   r   r'   rL   )r(   r†   r.   r/   r/   r0   Ú_action_profile_modeí   s   
€þzAdmin._action_profile_modec                 C   s8   |r| j  ¡ s| j  ¡ s|  d¡ d}|S |  |¡ |S )NzCannot enable the tuning.F)r   Ú
is_runningÚstartr'   ©r(   ÚretÚmsgr/   r/   r0   Ú_profile_print_statusö   s   

ÿzAdmin._profile_print_statusc                 C   sr   t   ¡ | j| j krtd| j ƒ | j d¡S | j ¡ r7| j|kr7| j	s1td| j
 ƒ | j d¡S | j d¡S dS )Nz‰Operation 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   r3   r   rb   r   ÚisSetr   r   r   rj   r/   r/   r0   Ú_action_dbus_wait_profileÿ   s   

zAdmin._action_dbus_wait_profilec              
   C   s~   | j d u s
| j dkrd S z| j | j ¡}d | _ t|dtjd tj ¡  W d S  ty> } z|  d¡ W Y d }~d S d }~ww )Nr
   )Úendr2   zUError: Failed to stop log capture. Restart the Tuned daemon to prevent a memory leak.)	r   r   Úlog_capture_finishr3   r4   r5   Úflushr   r'   )r(   Úlog_msgsr.   r/   r/   r0   rN     s   ÿ€ÿzAdmin._log_capture_finishc                 C   sÀ   t |ƒdkr
|  ¡ S d |¡}|dkr| j d¡S | j ¡  | js6| jd ur6| j	d d }| j 
| j|¡| _| j |¡\}}| jsC|sM| j |  ||¡¡S t ¡ | _| j | j|¡ |  ||¡S )Nr   r…   r
   Fé   é   )ÚlenÚ_action_dbus_listÚjoinr   rb   r   Úclearr   r   r   Úlog_capture_startr   Úswitch_profiler‘   r’   r“   rO   r•   )r(   r`   r:   r,   r   r   r/   r/   r0   Ú_action_dbus_profile  s"   

ÿ

zAdmin._action_dbus_profilec                 C   s<   t dƒ | j g d¢¡\}}|dkrt dƒ d S t dƒ d S )NzTrying to (re)start tuned...)Úservicer   Úrestartr   z#Tuned (re)started, changes applied.zQTuned (re)start failed, you need to (re)start tuned by hand for changes to apply.)r3   r   ÚexecuterŽ   r/   r/   r0   Ú_restart_tuned+  s
   zAdmin._restart_tunedc              
   C   s|   || j  ¡ v r5z| j ||¡ |  ¡  W dS  ty4 } z|  t|ƒ¡ |  d¡ W Y d }~dS d }~ww |  d| ¡ dS )NTzUnable to switch profile.Fz%Requested profile '%s' doesn't exist.)r   Úget_known_namesr   Úsave_active_profiler¦   r   r'   rL   )r(   r:   rm   r.   r/   r/   r0   Ú_set_profile3  s   
€ýzAdmin._set_profilec                 C   s6   t |ƒdkr
|  ¡ S d |¡}|dkrdS |  |d¡S )Nr   r…   r
   FT)rœ   rh   rž   r©   )r(   r`   r:   r/   r/   r0   Ú_action_profileA  s   
zAdmin._action_profilec                 C   s–   | j  ¡ }| j ¡  | js"| jd ur"| jd d }| j  | j|¡| _| j  	¡ \}}| js.|s8| j  
|  ||¡¡S t ¡ | _| j  | j|¡ |  ||¡S )Nrš   r›   )r   Úrecommend_profiler   rŸ   r   r   r   r    r   Úauto_profilerb   r‘   r’   r“   rO   r•   )r(   r:   r,   r   r   r/   r/   r0   Ú_action_dbus_auto_profileI  s   

ÿ

zAdmin._action_dbus_auto_profilec                 C   s   | j  ¡ }|  |d¡S )NF)r   Ú	recommendr©   rj   r/   r/   r0   Ú_action_auto_profileY  s   
zAdmin._action_auto_profilec                 C   s   t | j ¡ ƒ | j d¡S rd   )r3   r   r«   rb   rg   r/   r/   r0   Ú_action_dbus_recommend_profile]  s   z$Admin._action_dbus_recommend_profilec                 C   s   t | j ¡ ƒ dS rd   )r3   r   r®   rg   r/   r/   r0   Ú_action_recommend_profilea  ro   zAdmin._action_recommend_profilec                 C   sr   |r| j  ¡ }n| j  ¡ }|rtdƒ ntdƒ tdƒ tdƒ tdƒ tdƒ tdƒ tdtj ƒ | j  |¡S )	NzHVerfication 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   Úverify_profile_ignore_missingÚverify_profiler3   r   ÚLOG_FILErb   )r(   Úignore_missingr   r/   r/   r0   Ú_action_dbus_verify_profilee  s   

z!Admin._action_dbus_verify_profilec                 C   ó   t dƒ dS ©Nz Not supported in no_daemon mode.Fry   )r(   r¶   r/   r/   r0   Ú_action_verify_profilev  ó   zAdmin._action_verify_profilec                 C   s:   d}| j  | j|¡| _| j  ¡ }|s|  d¡ | j  |¡S )NrW   zCannot disable active profile.)r   r    r   r   Úoffr'   rb   )r(   r,   r   r/   r/   r0   Ú_action_dbus_offz  s   ÿ

zAdmin._action_dbus_offc                 C   r¸   r¹   ry   rg   r/   r/   r0   Ú_action_off„  r»   zAdmin._action_offr`   c                 C   ó(   |dkr|   ¡ 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
		r`   Úplugins©ÚverboseN)rc   Ú_action_dbus_list_plugins©r(   Úlist_choicerÂ   r/   r/   r0   r   ˆ  ó
   	ÿzAdmin._action_dbus_listc                 C   r¿   )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
		r`   rÀ   rÁ   N)rh   Ú_action_list_pluginsrÄ   r/   r/   r0   Ú_action_list–  rÆ   zAdmin._action_listc                 C   s„   | j  ¡ }| ¡ D ]2}t|ƒ |rt|| ƒdkrq	| j  |¡}|| D ]}td| ƒ | |d¡}|r:td| ƒ q$q	| 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   Úget_pluginsÚkeysr3   rœ   Úget_plugin_hintsÚgetrb   )r(   rÂ   rÀ   ÚpluginÚhintsÚ	parameterÚhintr/   r/   r0   rÃ   ¤  s   
€üzAdmin._action_dbus_list_pluginsc                 C   r¸   r¹   ry   )r(   rÂ   r/   r/   r0   rÇ   ·  r»   zAdmin._action_list_pluginsNr\   )r`   F)F)1Ú__name__Ú
__module__Ú__qualname__r   ÚADMIN_TIMEOUTÚloggingÚERRORr1   r'   r&   rG   rT   r[   rc   rh   rk   rn   rr   rt   rv   r{   r~   r‚   rƒ   r„   ra   rf   r‡   rŠ   r‹   r‘   r•   rN   r¢   r¦   r©   rª   r­   r¯   r°   r±   r·   rº   r½   r¾   r   rÈ   rÃ   rÇ   r/   r/   r/   r0   r	      s\    
þ




		



r	   )Ú
__future__r   Útuned.adminr   Útuned.utils.commandsr   Útuned.profilesr   r   Ú
exceptionsr   Útuned.exceptionsr   Útuned.constsr   Útuned.utils.profile_recommenderr   r=   r4   rC   r’   r   rÕ   Úobjectr	   r/   r/   r/   r0   Ú<module>   s   