Your IP : 18.216.167.229


Current Path : /usr/lib/python3.6/site-packages/tuned/plugins/__pycache__/
Upload File :
Current File : //usr/lib/python3.6/site-packages/tuned/plugins/__pycache__/plugin_disk.cpython-36.pyc

3

�<�e�A�@sjddlZddlmZddlTddlZddljZddlm	Z	ddl
Z
ddlZejj
�ZGdd�dej�ZdS)�N�)�hotplug)�*)�commandscs�eZdZdZ�fdd�Z�fdd�Zdd�Zdd	�Zed
d��Z	dd
�Z
dd�Z�fdd�Z�fdd�Z
�fdd�Zedd��Zedd��Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Z�fd,d-�Zd.d/�ZdZd1d2�Zd3d4�Zed5d6d7�d8d9��Ze d5�d[d;d<��Z!ed=d6d7�d>d?��Z"e d=�d\d@dA��Z#edBd6d7�dCdD��Z$e dB�d]dEdF��Z%dGdH�Z&dIdJ�Z'edKd6d7�dLdM��Z(e dK�d^dNdO��Z)e*dPd6d7�dQdR��Z+dSdT�Z,edUd6d7�dVdW��Z-e dU�d_dXdY��Z.�Z/S)`�
DiskPlugina�	
	`disk`::
	
	Plug-in for tuning various block device options. This plug-in can also
	dynamically change the advanced power management and spindown timeout
	setting for a drive according to the current drive utilization. The
	dynamic tuning is controlled by the [option]`dynamic` and the global
	[option]`dynamic_tuning` option in `tuned-main.conf`.
	+
	The disk plug-in operates on all supported block devices unless a
	comma separated list of [option]`devices` is passed to it.
	+
	.Operate only on the sda block device
	====
	----
	[disk]
	# Comma separated list of devices, all devices if commented out.
	devices=sda
	----
	====
	+
	The [option]`elevator` option sets the Linux I/O scheduler.
	+
	.Use the bfq I/O scheduler on xvda block device
	====
	----
	[disk]
	device=xvda
	elevator=bfq
	----
	====
	+
	The [option]`scheduler_quantum` option only applies to the CFQ I/O
	scheduler. It defines the number of I/O requests that CFQ sends to
	one device at one time, essentially limiting queue depth. The default
	value is 8 requests. The device being used may support greater queue
	depth, but increasing the value of quantum will also increase latency,
	especially for large sequential write work loads.
	+
	The [option]`apm` option sets the Advanced Power Management feature
	on drives that support it. It corresponds to using the `-B` option of
	the `hdparm` utility. The [option]`spindown` option puts the drive
	into idle (low-power) mode, and also sets the standby (spindown)
	timeout for the drive. It corresponds to using `-S` option of the
	`hdparm` utility.
	+
	.Use a medium-agressive power management with spindown
	====
	----
	[disk]
	apm=128
	spindown=6
	----
	====
	+
	The [option]`readahead` option controls how much extra data the
	operating system reads from disk when performing sequential
	I/O operations. Increasing the `readahead` value might improve
	performance in application environments where sequential reading of
	large files takes place. The default unit for readahead is KiB. This
	can be adjusted to sectors by specifying the suffix 's'. If the
	suffix is specified, there must be at least one space between the
	number and suffix (for example, `readahead=8192 s`).
	+
	.Set the `readahead` to 4MB unless already set to a higher value
	====
	----
	[disk]
	readahead=>4096
	----
	====
	The disk readahead value can be multiplied by the constant
	specified by the [option]`readahead_multiply` option.
	csrtt|�j||�ddddddddd	d
ddg|_d
dddddddddd	dg|_t|j�|_d|_d|_t	�|_
dS)N��������}�i�U�F�7��r����������n�Z�<�g{�G�z�?)�superr�__init__�
_power_levels�_spindown_levels�len�_levels�_level_steps�_load_smallestr�_cmd)�self�args�kwargs)�	__class__��!/usr/lib/python3.6/plugin_disk.pyrXszDiskPlugin.__init__cs�tt|�j�d|_d|_t�|_t�|_xL|jj	d�D]<}|j
|�r8|jj|j�|jr8|j
|j�r8|jj|j�q8Wt�|_dS)NT�block)rr�
_init_devicesZ_devices_supported�_use_hdparm�setZ
_free_devices�_hdparm_apm_devices�_hardware_inventoryZget_devices�_device_is_supported�addZsys_name�_is_hdparm_apm_supportedZ_assigned_devices)r'�device)r*r+r,r.bs
zDiskPlugin._init_devicescs�fdd�|D�S)Ncsg|]}�jjd|��qS)r-)r2Z
get_device)�.0�x)r'r+r,�
<listcomp>qsz2DiskPlugin._get_device_objects.<locals>.<listcomp>r+)r'Zdevicesr+)r'r,�_get_device_objectspszDiskPlugin._get_device_objectscCs�|jjddd|gtjgdd�\}}}|tjkrFtjd�d|_dS|rntjd|�tjd	||f�dSd
|kr�tjd|�dSdS)N�hdparmz-Cz/dev/%sT)�	no_errorsZ
return_errz4hdparm command not found, ignoring for other devicesFz#Device '%s' not supported by hdparmz(rc: %s, msg: '%s')�unknownz3Driver for device '%s' does not support apm command)	r&�execute�errno�ENOENT�log�warnr/�info�debug)r'r6�rc�outZerr_msgr+r+r,r5ss
z#DiskPlugin._is_hdparm_apm_supportedcCs2|jdko0|jjdd�dko0|jdkp0|jjdkS)	N�diskZ	removable�0�scsi�virtio�xen�nvme)rIrJrKrL)Zdevice_typeZ
attributes�get�parentZ	subsystem)�clsr6r+r+r,r3�s

zDiskPlugin._device_is_supportedcCs|jj|d|j�dS)Nr-)r2Z	subscribe�_hardware_events_callback)r'r+r+r,�_hardware_events_init�sz DiskPlugin._hardware_events_initcCs|jj|�dS)N)r2Zunsubscribe)r'r+r+r,�_hardware_events_cleanup�sz#DiskPlugin._hardware_events_cleanupcs(|j|�s|dkr$tt|�j||�dS)N�remove)r3rrrP)r'Zeventr6)r*r+r,rP�sz$DiskPlugin._hardware_events_callbackcs,|jdk	r|jj|�tt|�j||�dS)N)�
_load_monitorZ
add_devicerr�_added_device_apply_tuning)r'�instance�device_name)r*r+r,rU�s
z%DiskPlugin._added_device_apply_tuningcs,|jdk	r|jj|�tt|�j||�dS)N)rTZ
remove_devicerr�_removed_device_unapply_tuning)r'rVrW)r*r+r,rX�s
z)DiskPlugin._removed_device_unapply_tuningcCsdddddddd�S)NT)�dynamic�elevator�apm�spindown�	readahead�readahead_multiply�scheduler_quantumr+)rOr+r+r,�_get_config_options�szDiskPlugin._get_config_optionscCsddgS)Nr[r\r+)rOr+r+r,�#_get_config_options_used_by_dynamic�sz.DiskPlugin._get_config_options_used_by_dynamiccCsdd|_d|_d|_|j|jd�rTd|_|jjd|j�|_	i|_
i|_i|_i|_
nd|_d|_	dS)NTrrYrGF)Z_has_static_tuning�_apm_errcnt�_spindown_errcntZ_option_boolZoptionsZ_has_dynamic_tuning�_monitors_repositoryZcreateZassigned_devicesrTZ_device_idle�_stats�_idle�_spindown_change_delayed)r'rVr+r+r,�_instance_init�szDiskPlugin._instance_initcCs"|jdk	r|jj|j�d|_dS)N)rTrd�delete)r'rVr+r+r,�_instance_cleanup�s
zDiskPlugin._instance_cleanupcCs�|rd}|j}n
d}|j}|tjkr(dS|dkr6d}nL|tjkrbtjd|_|_tjd�dS|d7}|tjkr�tjd|�|r�||_n||_dS)Nr\r[rrzIhdparm command not found, ignoring future set_apm / set_spindown commandsz5disabling set_%s command: too many consecutive errors)	rcrb�consts�ERROR_THRESHOLDr?r@rArBrC)r'rEr\�sZcntr+r+r,�_update_errcnt�s&


zDiskPlugin._update_errcntcCsNtjd|�|jjdd|d|gtjgd�\}}|j|d�d|j|<dS)Nzchanging spindown to %dr;z-S%dz/dev/%s)r<TF)rArDr&r>r?r@rnrg)r'rVr6�new_spindown_levelrErFr+r+r,�_change_spindown�s&zDiskPlugin._change_spindowncCs2|jjddd|gtjgd�\}}d|ko0d|kS)Nr;z-Cz/dev/%s)r<ZstandbyZsleeping)r&r>r?r@)r'r6rErFr+r+r,�_drive_spinning�s"zDiskPlugin._drive_spinningcCs(||jkrdS|jj|�}|dkr&dS||jkr<|j||�|j|||�|j||�|j|}|j|}|dd|jkr�|d|j	kr�|d|j	kr�d}n.|ddkr�|ddks�|ddkr�d}nd}|dk�r�|d|7<|j
|d}|j|d}tj
d|d�|jtjk�rb|j|��rT|dk�rTtj
d|�d|j|<n|j|||�|jtjk�r�tj
d	|�|jjd
d|d|gtjgd
�\}	}
|j|	d�n4|j|�r�|j|��r�|j|d}|j|||�tj
d||d|df�tj
d||d|d|df�dS)N�levelr�read�writerztuning level changed to %dz;delaying spindown change to %d, drive has already spun downTzchanging APM_level to %dr;z-B%dz/dev/%s)r<Fz %s load: read %0.2f, write %0.2fz$%s idle: read %d, write %d, level %d���)r1rTZget_device_loadre�_init_stats_and_idle�
_update_stats�_update_idlerfr#r$r r!rArDrcrkrlrqrgrprbr&r>r?r@rn)r'rVr6�loadZstatsZidleZlevel_changeZnew_power_levelrorErFr+r+r,�_instance_update_dynamic�sF



.$
&z#DiskPlugin._instance_update_dynamiccCsDddgddgddgd�|j|<dddd�|j|<d|j|<dS)N�rr)�new�old�max)rrrsrtF)rerfrg)r'rVr6r+r+r,rvs$zDiskPlugin._init_stats_and_idlecCs�|j|d|j|d<}||j|d<dd�t||�D�}||j|d<|j|d}dd�t||�D�}||j|d<t|d�t|d�|j|d	<t|d
�t|d
�|j|d<dS)Nr|r}cSsg|]}|d|d�qS)rrr+)r7Znew_oldr+r+r,r9'sz,DiskPlugin._update_stats.<locals>.<listcomp>�diffr~cSsg|]}t|��qSr+)r~)r7Zpairr+r+r,r9,srrs�rt)re�zip�float)r'rVr6Znew_loadZold_loadrZold_max_loadZmax_loadr+r+r,rw"s"zDiskPlugin._update_statscCsLxFdD]>}|j|||jkr6|j||d7<qd|j||<qWdS)Nrsrtrr)rsrt)rer%rf)r'rVr6Z	operationr+r+r,rx3s
zDiskPlugin._update_idlecs0||jkrtjd|�ntt|�j||�dS)Nz<There is no dynamic tuning available for device '%s' at time)r1rArCrr�_instance_apply_dynamic)r'rVr6)r*r+r,r�;s
z"DiskPlugin._instance_apply_dynamiccCsdS)Nr+)r'rVr6r+r+r,�_instance_unapply_dynamicDsz$DiskPlugin._instance_unapply_dynamic�/sys/block/cCs@d|kr0tjj||jdd�|�}tjj|�r0|Stjj|||�S)N�/�!)�os�path�join�replace�exists)r'r6�suffix�prefixZdevr+r+r,�_sysfs_pathGs
zDiskPlugin._sysfs_pathcCs|j|d�S)Nzqueue/scheduler)r�)r'r6r+r+r,�_elevator_fileNszDiskPlugin._elevator_filerZT)Z
per_devicecCs0|j|�}|s,|jj|||r$tjgndd�|S)NF)�no_error)r�r&�
write_to_filer?r@)r'�valuer6�simrS�sys_filer+r+r,�
_set_elevatorQs


zDiskPlugin._set_elevatorFcCs"|j|�}|jj|jj||d��S)N)r�)r�r&Zget_active_option�	read_file)r'r6�ignore_missingr�r+r+r,�
_get_elevatorYs
zDiskPlugin._get_elevatorr[cCs|||jkr(|s tjd|�dSt|�S|jtjkrt|sl|jjddt|�d|gt	j
gd�\}}|j|d�t|�SdSdS)Nz+apm option is not supported for device '%s'r;z-Bz/dev/)r<F)r1rArC�strrbrkrlr&r>r?r@rn)r'r�r6r�rSrErFr+r+r,�_set_apm`s
(zDiskPlugin._set_apmcCs�||jkr |stjd|�dSd}d}|jjddd|gtjgd�\}}|tjkrZdS|dkrhd}n@tjd	|tj	�}|r�yt
|jd
��}Wntk
r�d}YnX|r�tj
d|�|S)Nz+apm option is not supported for device '%s'Fr;z-Bz/dev/)r<rTz
.*=\s*(\d+).*rz2could not get current APM settings for device '%s')r1rArCr&r>r?r@�re�match�S�int�group�
ValueError�error)r'r6r�r��errrErF�mr+r+r,�_get_apmps(
"
zDiskPlugin._get_apmr\cCs|||jkr(|s tjd|�dSt|�S|jtjkrt|sl|jjddt|�d|gt	j
gd�\}}|j|d�t|�SdSdS)Nz0spindown option is not supported for device '%s'r;z-Sz/dev/)r<T)r1rArCr�rcrkrlr&r>r?r@rn)r'r�r6r�rSrErFr+r+r,�
_set_spindown�s
(zDiskPlugin._set_spindowncCs$||jkr |stjd|�dSdS)Nz0spindown option is not supported for device '%s'�)r1rArC)r'r6r�r+r+r,�
_get_spindown�s

zDiskPlugin._get_spindowncCs|j|d�S)Nzqueue/read_ahead_kb)r�)r'r6r+r+r,�_readahead_file�szDiskPlugin._readahead_filecCs^t|�jdd�}yt|d�}Wntk
r4dSXt|�dkrZ|dddkrZ|d}|S)Nrrrm�)r��splitr�r�r")r'r��val�vr+r+r,�	_parse_ra�szDiskPlugin._parse_rar]cCsZ|j|�}|j|�}|dkr0tjd||f�n&|sV|jj|d||rNtjgndd�|S)Nz,Invalid readahead value '%s' for device '%s'z%dF)r�)r�r�rAr�r&r�r?r@)r'r�r6r�rSr�r�r+r+r,�_set_readahead�s

zDiskPlugin._set_readaheadcCs6|j|�}|jj||d�j�}t|�dkr.dSt|�S)N)r�r)r�r&r��stripr"r�)r'r6r�r�r�r+r+r,�_get_readahead�s

zDiskPlugin._get_readaheadr^c	Cs�|rdS|jd|d�}|r^|j|�}|dkr0dStt|�|�}|jj||�|j||d�n2|jj|�}|dkrvdS|j||d�|jj|�dS)Nr^)Zcommand_namerWF)	Z_storage_keyr�r�r�Z_storager0r�rMZunset)	r'ZenablingZ
multiplierr6Zverifyr�Zstorage_keyZ
old_readaheadZ
new_readaheadr+r+r,�_multiply_readahead�s"
zDiskPlugin._multiply_readaheadcCs|j|d�S)Nzqueue/iosched/quantum)r�)r'r6r+r+r,�_scheduler_quantum_file�sz"DiskPlugin._scheduler_quantum_filer_cCs8|j|�}|s4|jj|dt|�|r,tjgndd�|S)Nz%dF)r�)r�r&r�r�r?r@)r'r�r6r�rSr�r+r+r,�_set_scheduler_quantum�s

z!DiskPlugin._set_scheduler_quantumcCsH|j|�}|jj||d�j�}t|�dkr@|s<tjd|�dSt|�S)N)r�rz>disk_scheduler_quantum option is not supported for device '%s')r�r&r�r�r"rArCr�)r'r6r�r�r�r+r+r,�_get_scheduler_quantum�s
z!DiskPlugin._get_scheduler_quantum)r�)F)F)F)F)F)0�__name__�
__module__�__qualname__�__doc__rr.r:r5�classmethodr3rQrRrPrUrXr`rarhrjrnrprqrzrvrwrxr�r�r�r�Zcommand_setr�Zcommand_getr�r�r�r�r�r�r�r�r�Zcommand_customr�r�r�r��
__classcell__r+r+)r*r,rsZJ
2	
r)r?�rZ
decoratorsZ
tuned.logsZtunedZtuned.constsrkZtuned.utils.commandsrr�r�ZlogsrMrAZPluginrr+r+r+r,�<module>s



?>