509318396
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!DOCTYPE html>
<html>
3
À<×e4  ã               @   sn   d dl mZ d dlT ddlZd dl mZ ddlmZ ddlj	Z	ddl
Z
ddlZejjƒ ZG dd„ dejƒZdS )	é   )Úbase)Ú*é    N)Ú
exceptions)Úcommandsc                   sž   e Zd ZdZ‡ fdd„Zdd„ Z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ejfdd„Zdd„ Zeddd�dd „ ƒZ‡  ZS )!ÚSystemdPlugina<  
	`systemd`::
	
	Plug-in for tuning systemd options.
	+
	The [option]`cpu_affinity` option allows setting CPUAffinity in
	`/etc/systemd/system.conf`. This configures the CPU affinity for the
	service manager as well as the default CPU affinity for all forked
	off processes. The option takes a comma-separated list of CPUs with
	optional CPU ranges specified by the minus sign (`-`).
	+
	.Set the CPUAffinity for `systemd` to `0 1 2 3`
	====
	----
	[systemd]
	cpu_affinity=0-3
	----
	====
	+
	NOTE: These tunings are unloaded only on profile change followed by a reboot.
	c                s<   t jjtjƒstjdtj ƒ‚tt| ƒj	||Ž t
ƒ | _d S )NzERequired systemd '%s' configuration file not found, disabling plugin.)ÚosÚpathÚisfileÚconstsÚSYSTEMD_SYSTEM_CONF_FILEr   ZNotSupportedPluginExceptionÚsuperr   Ú__init__r   Ú_cmd)ÚselfÚargsÚkwargs)Ú	__class__© ú$/usr/lib/python3.6/plugin_systemd.pyr   $   s    zSystemdPlugin.__init__c             C   s   d|_ d|_d S )NFT)Z_has_dynamic_tuningZ_has_static_tuning)r   Úinstancer   r   r   Ú_instance_init*   s    zSystemdPlugin._instance_initc             C   s   d S )Nr   )r   r   r   r   r   Ú_instance_cleanup.   s    zSystemdPlugin._instance_cleanupc             C   s   dd iS )NÚcpu_affinityr   )Úclsr   r   r   Ú_get_config_options1   s    z!SystemdPlugin._get_config_optionsc             C   sB   |d k	r>t jd| d |t jd�}|d k	r>|jdkr>|jdƒS d S )Nz^\s*z\s*=\s*(.*)$)Úflagsr   )ÚreÚsearchÚ	MULTILINEÚ	lastindexÚgroup)r   ÚconfÚkeyÚmor   r   r   Ú_get_keyval7   s
    
zSystemdPlugin._get_keyvalc             C   s~   t jd| d dt|ƒ |t jd�\}}|dk rzy|d dkrF|d7 }W n tk
r\   Y nX ||d t|ƒ d 7 }|S |S )	Nz^(\s*z\s*=).*$z\g<1>)r   r   Ú
ú=éÿÿÿÿ)r   ÚsubnÚstrr   Ú
IndexError)r   r"   r#   ÚvalZconf_newZnsubsr   r   r   Ú_add_keyval?   s    (zSystemdPlugin._add_keyvalc             C   s   t jd| d d|t jd�S )Nz^\s*z\s*=.*\nÚ )r   )r   Úsubr   )r   r"   r#   r   r   r   Ú_del_keyK   s    zSystemdPlugin._del_keyc             C   s,   | j jtjd d�}|d kr(tjdƒ d S |S )N)Úerr_retz(error reading systemd configuration file)r   Ú	read_filer   r   ÚlogÚerror)r   Zsystemd_system_confr   r   r   Ú_read_systemd_system_confN   s
    
z'SystemdPlugin._read_systemd_system_confc             C   sp   t jt j }| jj||ƒs8tjdƒ | jj|dd� dS | jj|t jƒsltjdt j ƒ | jj|dd� dS dS )Nz(error writing systemd configuration fileT)Úno_errorFz/error replacing systemd configuration file '%s')	r   r   ZTMP_FILE_SUFFIXr   Úwrite_to_filer3   r4   ÚunlinkÚrename)r   r"   Ztmpfiler   r   r   Ú_write_systemd_system_confU   s    
z(SystemdPlugin._write_systemd_system_confc             C   s   t jjtj| jƒS )N)r   r	   Újoinr   ZPERSISTENT_STORAGE_DIRÚname)r   r   r   r   Ú_get_storage_filenamec   s    z#SystemdPlugin._get_storage_filenamec             C   sl   | j ƒ }|d k	rh| jƒ }| jj|d dd�}| jj|ƒ |d krN| j|tjƒ}n| j|tj|ƒ}| j	|ƒ d S )NT)r1   r6   )
r5   r=   r   r2   r8   r0   r   ÚSYSTEMD_CPUAFFINITY_VARr-   r:   )r   r"   ÚfnameÚcpu_affinity_savedr   r   r   Ú_remove_systemd_tuningf   s    z$SystemdPlugin._remove_systemd_tuningc             C   s0   |t jkr,tjdt j ƒ | jƒ  tjdƒ d S )Nz6removing '%s' systemd tuning previously added by TuneDz[you may need to manualy run 'dracut -f' to update the systemd configuration in initrd image)r   ZROLLBACK_FULLr3   Úinfor>   rA   Zconsole)r   r   Zrollbackr   r   r   Ú_instance_unapply_staticr   s    
z&SystemdPlugin._instance_unapply_staticc          
   C   s<   |d krdS dj dd„ | jjtjddtjdd|ƒƒƒD ƒƒS )Nr.   ú c             s   s   | ]}t |ƒV  qd S )N)r*   )Ú.0Úvr   r   r   ú	<genexpr>|   s    z8SystemdPlugin._cpulist_convert_unpack.<locals>.<genexpr>z\s+ú,z,\s+)r;   r   Úcpulist_unpackr   r/   )r   Zcpulistr   r   r   Ú_cpulist_convert_unpacky   s    z%SystemdPlugin._cpulist_convert_unpackr   F)Z
per_devicec             C   sö   d }d }| j j| jj| j j|ƒƒƒ}djdd„ | j j|ƒD ƒƒ}| jƒ }	|	d k	rh| j|	t	j
ƒ}| j|ƒ}|r|| jd|||ƒS |rò| jƒ }
| j j|
d dd�}|d k	rÄ|d krÄ||krÄ| j j|
|dd� tjdt	j
|t	jf ƒ | j| j|	t	j
|ƒƒ d S )	NrD   c             s   s   | ]}t |ƒV  qd S )N)r*   )rE   rF   r   r   r   rG   ƒ   s    z)SystemdPlugin._cmdline.<locals>.<genexpr>r   T)r1   r6   )Zmakedirz setting '%s' to '%s' in the '%s')r   ZunescapeZ
_variablesÚexpandZunquoter;   rI   r5   r%   r   r>   rJ   Z_verify_valuer=   r2   r7   r3   rB   r   r:   r-   )r   ZenablingÚvalueZverifyZignore_missingZconf_affinityZconf_affinity_unpackedrF   Z
v_unpackedr"   r?   r@   r   r   r   Ú_cmdline~   s"    
zSystemdPlugin._cmdline)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   Úclassmethodr   r%   r-   r0   r5   r:   r=   rA   r   ZROLLBACK_SOFTrC   rJ   Zcommand_customrM   Ú__classcell__r   r   )r   r   r      s   r   )r.   r   Z
decoratorsZ
tuned.logsZtunedr   Ztuned.utils.commandsr   Ztuned.constsr   r   r   ZlogsÚgetr3   ZPluginr   r   r   r   r   Ú<module>   s   

