<!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
<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   

