o
    _I                     @   sp   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m	Z	 ddl
Z
ddlZe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d!ddZdd Zeddddd  Z  ZS )"SystemdPluginzn
	Plugin for tuning systemd options.

	These tunings are unloaded only on profile change followed by reboot.
	c                    s@   t jtjstdtj tt| j	|i | t
 | _d S )NzERequired systemd '%s' configuration file not found, disabling plugin.)ospathisfileconstsSYSTEMD_SYSTEM_CONF_FILEr   NotSupportedPluginExceptionsuperr   __init__r   _cmd)selfargskwargs	__class__ >/usr/lib/python3/dist-packages/tuned/plugins/plugin_systemd.pyr      s   zSystemdPlugin.__init__c                 C   s   d|_ d|_d S )NFT)_has_dynamic_tuning_has_static_tuningr   instancer   r   r   _instance_init   s   
zSystemdPlugin._instance_initc                 C   s   d S Nr   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_options!   s   z!SystemdPlugin._get_config_optionsc                 C   sB   |d urt jd| d |t jd}|d ur|jdkr|dS d S )N^\s*z\s*=\s*(.*)$flagsr   )research	MULTILINE	lastindexgroup)r   confkeymor   r   r   _get_keyval'   s
   
zSystemdPlugin._get_keyvalc                 C   s|   t jd| d dt| |t jd\}}|dk r<z|d dkr#|d7 }W n	 ty-   Y nw ||d t| d 7 }|S |S )	Nz^(\s*z\s*=).*$z\g<1>r#   r   
=)r%   subnstrr'   
IndexError)r   r*   r+   valconf_newnsubsr   r   r   _add_keyval/   s   (zSystemdPlugin._add_keyvalc                 C   s   t jd| d d|t jdS )Nr"   z\s*=.*\n r#   )r%   subr'   )r   r*   r+   r   r   r   _del_key;   s   zSystemdPlugin._del_keyc                 C   s,   | j jtjd d}|d u rtd d S |S )N)err_retz(error reading systemd configuration file)r   	read_filer   r   logerror)r   systemd_system_confr   r   r   _read_systemd_system_conf>   s
   
z'SystemdPlugin._read_systemd_system_confc                 C   sp   t jt j }| j||std | jj|dd dS | j|t js6t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   TMP_FILE_SUFFIXr   write_to_filer=   r>   unlinkrename)r   r*   tmpfiler   r   r   _write_systemd_system_confE   s   
z(SystemdPlugin._write_systemd_system_confc                 C   s   t jtj| jS r   )r   r	   joinr   PERSISTENT_STORAGE_DIRname)r   r   r   r   _get_storage_filenameS   s   z#SystemdPlugin._get_storage_filenamec                 C   sp   |   }|d ur6|  }| jj|d dd}| j| |d u r'| |tj}n| |tj|}| 	| d S d S )NTr;   rA   )
r@   rK   r   r<   rD   r:   r   SYSTEMD_CPUAFFINITY_VARr7   rG   )r   r*   fnamecpu_affinity_savedr   r   r   _remove_systemd_tuningV   s   z$SystemdPlugin._remove_systemd_tuningFc                 C   s.   |rt dtj  |   t d d S 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=   infor   rM   rP   console)r   r   full_rollbackr   r   r   _instance_unapply_staticb   s
   z&SystemdPlugin._instance_unapply_staticc                 C   s<   |d u rdS d dd | jtddtdd|D S )Nr8    c                 s       | ]}t |V  qd S r   r2   .0vr   r   r   	<genexpr>l       z8SystemdPlugin._cpulist_convert_unpack.<locals>.<genexpr>z\s+,z,\s+)rH   r   cpulist_unpackr%   r9   )r   cpulistr   r   r   _cpulist_convert_unpacki   s   0z%SystemdPlugin._cpulist_convert_unpackr   )
per_devicec                 C   s   d }d }| j | j| j |}ddd | j |D }|  }	|	d ur4| |	t	j
}| |}|r>| d|||S |r{|  }
| j j|
d dd}|d urb|d u rb||krb| j j|
|dd tdt	j
|t	jf  | | |	t	j
| d S d S )	NrU   c                 s   rV   r   rW   rX   r   r   r   r[   s   r\   z)SystemdPlugin._cmdline.<locals>.<genexpr>r   TrL   )makedirz setting '%s' to '%s' in the '%s')r   unescape
_variablesexpandunquoterH   r^   r@   r-   r   rM   r`   _verify_valuerK   r<   rC   r=   rQ   r   rG   r7   )r   enablingvalueverifyignore_missingconf_affinityconf_affinity_unpackedrZ   
v_unpackedr*   rN   rO   r   r   r   _cmdlinen   s$   
zSystemdPlugin._cmdline)F)__name__
__module____qualname____doc__r   r   r   classmethodr!   r-   r7   r:   r@   rG   rK   rP   rT   r`   command_customro   __classcell__r   r   r   r   r      s$    


r   )r8   r   
decorators
tuned.logstunedr   tuned.utils.commandsr   tuned.constsr   r   r%   logsgetr=   Pluginr   r   r   r   r   <module>   s    
