�
��\c@s�ddlmZddlTddlZddlZddlTddlZddlZddl Z ddl
jZddlZddl
Z
ddlmZddlZejj�Zdefd��YZdefd ��YZd
ejfd��YZdS(i(tbase(t*i����N(tcommandstSchedulerParamscBs>eZddddd�Zed��Zejd��ZRS(cCs1||_||_||_||_||_dS(N(t_cmdtcmdlinet schedulertprioritytaffinity(tselftcmdRRRR((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyt__init__s
cCs*|jdkrdS|jj|j�SdS(N(t _affinitytNoneRtbitmask2cpulist(R ((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyRscCs1|dkrd|_n|jj|�|_dS(N(R
RRtcpulist2bitmask(R tvalue((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyR%sN(t__name__t
__module__R
RtpropertyRtsetter(((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyRst
IRQAffinitiescBseZd�ZRS(cCsi|_d|_dS(N(tirqsR
tdefault(R ((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyR-s (RRR(((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyR,stSchedulerPlugincBs�eZdZiejd6ejd6ejd6ejd6ejd6Z d�Z
d�Zd�Ze
d ��Zd
�Zd�Zd�Zd
�Zd�Zd�Zd�Zd�Zd�Zed�Zd�Zd�Zd�Zd�Zd�Zd�Zed�Z d�Z!d�Z"d�Z#e$dde�d ��Z%e$d!de�d"��Z&d#�Z'd$�Z(d%�Z)ed&�Z*d'�Z+d(�Z,d)�Z-d*�Z.d+�Z/d,�Z0d-�Z1d.�Z2e$d/ded0d1�d2��Z3RS(3s�
Plugin for tuning of scheduler. Currently it can control scheduling
priorities of system threads (it is substitution for the rtctl tool).
tftbtrtotic Cs�tt|�j||||||||�t|_tj|_ttj �|_
|dk r�|jtj
tj�|_t|jtjtj ��|_
nt�|_d|_d|_tj�|_|jdd�|_|jdd�|_dS(Ns.*ttcommand_nameRtirq(tsuperRRtTruet_has_dynamic_optionstconststCFG_DEF_DAEMONt_daemontinttCFG_DEF_SLEEP_INTERVALt_sleep_intervalR
tget_boolt
CFG_DAEMONtgettCFG_SLEEP_INTERVALRRt
_ps_whitelistt
_ps_blacklisttperftcpu_mapt_cpust_storage_keyt_scheduler_storage_keyt_irq_storage_key( R tmonitor_repositorytstorage_factorythardware_inventorytdevice_matchertdevice_matcher_udevtplugin_instance_factoryt
global_cfgt variables((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyR?s+ $ cCs�t|_t|_t|_|jj|ji�|_t |j�dkr�t
jd�|j�i|_|jj
|j�n|j|_x1|jD]&}|jj|j|�|j|<q�W|jj|jjdd��dkr�t|_ntj�|_|jr�|jr�y�tj�|_tjdtjdtjddd dd
dddddd
ddtjtjB� }|j d|j!d|j�tj"|j!|j�|_#|j#j$|�|j#j%�Wq�t|_q�XndS(Nis0recovering scheduling settings from previous runtruntimeit0ttypetconfigttasktcommtmmaptfreqt
wakeup_eventst watermarktsample_typetcpustthreads(&tFalset_has_dynamic_tuningR"t_has_static_tuningt_runtime_tuningt_storageR,R4t_scheduler_originaltlentlogtinfot_restore_ps_affinitytunsettoptionst
_schedulert
_variablestexpandRR*t threadingtEventt
_terminateR&R0t
thread_mapt_threadstevselt
TYPE_SOFTWAREtCOUNT_SW_DUMMYt
SAMPLE_TIDt
SAMPLE_CPUtopenR2tevlistt_evlisttaddRD(R tinstancetkR_((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyt_instance_initQs<
$' cCsdS(N((R Rh((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyt_instance_cleanupyscCsidd6dd6dd6S(Ntisolated_corestps_whitelisttps_blacklist(R
(tcls((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyt_get_config_options|scCs]t|tj�s*|}tj|�}ntj|�}|j|�rYd|d}n|S(Nt[t](t
isinstancetprocfstprocesstprocess_cmdlinet_is_kthread(R RutpidR((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyt_get_cmdline�scCs�tj�}|j�i}x�|j�D]�}yj|j|�}|d}|||<d|kr�x4|dj�D]}|j|�}|||<qrWnWq)ttfk
r�}|jtj ks)|jtj
kr�q)q��q)Xq)W|S(NRxRJ(Rttpidstatstreload_threadstvaluesRytkeystOSErrortIOErrorterrnotENOENTtESRCH(R tpst processestprocR
Rxte((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyt
get_processes�s$
cCsQtj|�}tj|�}tj|�}tjd|||f�||fS(Ns8Read scheduler policy '%s' and priority '%d' of PID '%d'(t
schedutilst
get_schedulertschedstrtget_priorityRRtdebug(R RxRt sched_strR((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyt_get_rt�s cCs3tj|�}tjd|||f�yZtj|�}tj|�}||ksb||kr�tjd||||f�nWn*ttfk
r�}tjd|�nXytj |||�Wnettfk
r.}t
|d�r|jtjkrtjd|�q/tjd||f�nXdS(NsBSetting scheduler policy to '%s' and priority to '%d' of PID '%d'.s9Priority for %s must be in range %d - %d. '%d' was given.s(Failed to get allowed priority range: %sR�sAFailed to set scheduling parameters of PID %d, the task vanished.s1Failed to set scheduling parameters of PID %d: %s(
R�R�RRR�tget_priority_mintget_priority_maxterrortSystemErrorR~t
set_schedulerthasattrR�R�(R RxtschedtprioR�tprio_mintprio_maxR�((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyt_set_rt�s* ! cCs|ddtjj@dkS(Ntstattflagsi(Rttpidstatt
PF_KTHREAD(R Ru((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyRw�scCs+y�tj|�}|dj�r�|dddkrJtjd|�n4|j|�rmtjd|�ntjd|�dSdSWn�ttfk
r�}|j t j
ks�|j t jkr�tjd |�d
Stjd||f�dSn1t
tfk
r&}tjd||f�dSXdS(
NR�tstatetZsYAffinity of zombie task with PID %d cannot be changed, the task's affinity mask is fixed.s[Affinity of kernel thread with PID %d cannot be changed, the task's affinity mask is fixed.sRAffinity of task with PID %d cannot be changed, the task's affinity mask is fixed.iis6Failed to get task info for PID %d, the task vanished.i����s&Failed to get task info for PID %d: %si����(RtRutis_bound_to_cpuRRR�RwtwarnR~RR�R�R�R�tAttributeErrortKeyError(R RxRuR�((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyt_affinity_changeable�s2 $ cCsxy|j|}Wn-tk
r@t|j�}||j|<nX|jdkrt|jdkrt||_||_ndS(N(RPR�RRRR
R(R RxRRtparams((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyt_store_orig_process_rt�s
cCst}|dkr"|dkr"|SyT|j|�\}}|dkrO|}n|j|||�|j|||�Wn�ttfk
r�}t|d�r�|jtj kr�t
jd|�||jkr�|j|=nt
}qt
jd||f�nX|S(NR�s=Failed to read scheduler policy of PID %d, the task vanished.scRefusing to set scheduler and priority of PID %d, reading original scheduling parameters failed: %s(R"R
R�R�R�R�R~R�R�R�RRR�RPRKR�(R RxR�R�tcontt
prev_schedt prev_prioR�((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyt_tune_process_rt�s& !
cCs`y|j|}Wn-tk
r@t|j�}||j|<nX|jdkr\||_ndS(N(RPR�RRRR
(R RxRR�((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyt_store_orig_process_affinitys
cCs�t}|dkr|SyQ|j|�}|rF|j|||�}n|j||�|j||�Wn�ttfk
r�}t|d�r�|j t j
kr�tjd|�||j
kr�|j
|=nt}q�tjd||f�nX|S(NR�s5Failed to read affinity of PID %d, the task vanished.sLRefusing to set CPU affinity of PID %d, reading original affinity failed: %s(R"R
t
_get_affinityt_get_intersect_affinityt
_set_affinityR�R�R~R�R�R�RRR�RPRKR�(R RxRt intersectR�t
prev_affinityR�((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyt_tune_process_affinitys, !
cCs_|j|||�}|sdS|j||�}|sG||jkrKdS||j|_dS(N(R�R�RPR(R RxR
R�R�RR�((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyt
_tune_process5scCs|jj|�}|dkr?|dkr?tjd|�dSyt|�}Wn#tk
rttjd|�dSX||fS(NRs>Invalid scheduler: %s. Scheduler and priority will be ignored.s=Invalid priority: %s. Scheduler and priority will be ignored.(NN(NN(t_dict_schedcfg2numR,R
RRR�R't
ValueError(R t
str_schedulertstr_priorityRR((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyt_convert_sched_params>s
cCsK|dkrd}n2|jj|�}|sGtjd|�d}n|S(NRs)Invalid affinity: %s. It will be ignored.(R
Rthex2cpulistRRR�(R tstr_affinityR((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyt_convert_affinityMs cCsO|\}}}}}|j||�\}}|j|�}|||||fS(N(R�R�(R tvalst rule_prioRRRtregex((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyt_convert_sched_cfgXs
cCs�tt|�j|�y|j�}Wn+ttfk
rS}tjd|�dSXg|jj �D]*\}}|t
|�jdd�f^qd}g|D]E\}}tj
d|�r�t|�dkr�||j|�f^q�}t|dd��}t�} i|_x6|D].\}\}
}}}
}ytj|�}Wn0tjk
rt}tjdt
|��qnXg|j �D]0\}}tj||�dk r�||f^q�}tg|D]*\}}||||||
|ff^q��}| j|�t
|�jd d
�}|jdd�}|||
g|j|<qWxE| j �D]7\}\}}}}}
}|j|||||
�qSW|jj|j|j�|jr�|jr�tj d
|j!d|g�|_"|j"j#�ndS(NsIerror applying tuning, cannot get information about running processes: %st:isgroup\.itkeycSs|ddS(Nii((toption_vals((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyt<lambda>lss(error compiling regular expression: '%s't(s\(t)s\)ttargettargs($R!Rt_instance_apply_staticR�R~RRRR�RWtitemststrtsplittretmatchRQR�tsortedtdictt
_sched_lookuptcompiletsearchR
tupdatetreplaceR�ROtsetR4RPR&RNRZtThreadt_thread_codet_threadtstart(R RhR�R�toptionRt sched_cfgR�tbuft sched_allR�RRRR�RRxR
R�R�((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyR�_sL @- C7
(
!cCsy|j�}Wn+ttfk
r=}tjd|�dSXx�|jj�D]�\}}||ksN|||jkrqNn|jdk r�|j
dk r�|j||j|j
�n|jdk rN|j
||j�qNqNWi|_|jj|j�dS(NsKerror unapplying tuning, cannot get information about running processes: %s(R�R~RRRR�RPR�RRR
RR�RR�RORUR4(R R�R�Rxtorig_params((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyRT�s"
cCsVtt|�j||�|jrH|jrH|jj�|jj�n|j �dS(N(
R!Rt_instance_unapply_staticR&RNR\R�R�tjoinRT(R Rht
full_rollback((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyR��s
c
Csy|j|�}Wnittfk
r~}|jtjksO|jtjkrctjd|�ntjd||f�dSX|j j
|j||�}|dk r||j
krtjd||t|�f�|\}}} |j||||| �|jj|j|j
�ndS(Ns3Failed to get cmdline of PID %d, the task vanished.s#Failed to get cmdline of PID %d: %ss-tuning new process '%s' with PID '%d' by '%s'(RyR~RR�R�R�RRR�R�Rt re_lookupR�R
RPR�R�ROR�R4(
R RhRxRR
R�tvR�R�R((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyt_add_pid�s$ cCsJ||jkrF|j|=tjd|�|jj|j|j�ndS(Ns)removed PID %d from the rollback database(RPRRR�ROR�R4(R RhRx((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyt_remove_pid�s
c CsJ|jj|j�}tj�}|jj�}x|D]}|j|�q7Wx�|jj �sEt
|j|jd��dkrQ|jj �rQt}x�|r>t
}x�|jD]�}|jj|�}|r�t}|jtjkr|j|t|j�|�q7|jtjkr7|j|t|j��q7q�q�Wq�WqQqQWdS(Ni�i(Rtre_lookup_compileR�tselecttpollRft
get_pollfdtregisterR\tis_setRQR)R"RKR2tread_on_cpuR@R0tRECORD_COMMR�R'ttidtRECORD_EXITR�( R RhRR�tfdstfdtread_eventstcputevent((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyR��s$
2 Rmt
per_devicecCs^|r
dS|rZ|dk rZdjgtjdt|��D]}d|^q;�|_ndS(Nt|s(?<!\\);s(%s)(R
R�R�R�R�R.(R tenablingRtverifytignore_missingR�((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyR.�sRncCs^|r
dS|rZ|dk rZdjgtjdt|��D]}d|^q;�|_ndS(NR�s(?<!\\);s(%s)(R
R�R�R�R�R/(R R�RR�R�R�((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyR/�scCs*tj|�}tjd||f�|S(NsRead affinity '%s' of PID %d(R�tget_affinityRRR�(R Rxtres((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyR��scCs�tjd||f�ytj||�tSWn�ttfk
r�}t|d�r||jtj kr|tjd|�nD|j
|�}|dks�|dkr�tjd|||f�ntSXdS(Ns'Setting CPU affinity of PID %d to '%s'.R�s4Failed to set affinity of PID %d, the task vanished.ii����s,Failed to set affinity of PID %d to '%s': %s(
RRR�R�tset_affinityR"R�R~R�R�R�R�R�RK(R RxRR�R�((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyR��s! cCs/t|�jt|��}|r+t|�S|S(N(R�tintersectiontlist(R t affinity1t affinity2t affinity3taff((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyR�s
cCs�g|D]0}tj|j|j|��dk r|^q}|jdkr�g|D]0}tj|j|j|��dkrS|^qS}ntg|D]}|j|f^q��}x|D]}y|j||�}Wnkt t
fk
rE} | jtjks| jtj
kr(tjd|�q�tjd|| f�q�nX|j||dt�}
|
sjq�n||jkr�||j|_n|r�d||kr�|j||dj�|t�q�q�WdS(NRs3Failed to get cmdline of PID %d, the task vanished.sARefusing to set affinity of PID %d, failed to get its cmdline: %sR�RJ(R�R�R.t_get_stat_commR
R/R�RxRyR~RR�R�R�RRR�R�R�R"RPRt_set_all_obj_affinityR|(R tobjsRRJR�tpsltpsdRxR
R�R�((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyR
s6$'(
cCs2y|ddSWntttfk
r-dSXdS(NR�RCR(R~RR�(R R((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyR)scCsdy3tj�}|j�|j|j�|t�Wn*ttfk
r_}tj d|�nXdS(NsIerror applying tuning, cannot get information about running processes: %s(
RtRzR{RR|RKR~RRRR�(R RR�R�((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyt_set_ps_affinity/s
cCs�y`|jj|�}tjd||f�d|}t|d��}|j|�WdQXtSWnpttfk
r�}t |d�r�|j
t
jkr�|r�tjd|�ntjd|||f�t
SXdS(Ns&Setting SMP affinity of IRQ %s to '%s's/proc/irq/%s/smp_affinitytwR�s/Setting SMP affinity of IRQ %s is not supporteds0Failed to set SMP affinity of IRQ %s to '%s': %s(Rtcpulist2hexRRR�RdtwriteR"R~RR�R�tEIOR�RK(R R Rt restoringtaffinity_hextfilenameRR�((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyt_set_irq_affinity8s
! cCs�yL|jj|�}tjd|�tdd��}|j|�WdQXWn0ttfk
r~}tjd||f�nXdS(Ns(Setting default SMP IRQ affinity to '%s's/proc/irq/default_smp_affinityRs2Failed to set default SMP IRQ affinity to '%s': %s( RR RRR�RdR
R~RR�(R RR
RR�((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyt_set_default_irq_affinityOs cCs-t�}tj�}x�|j�D]�}y)||d}tjd||f�Wntk
rgq"nX|j|||�}t|�t|�kr�q"n|j ||t
�r"||j|<q"q"W|jj
d�}|jj|�}|j|||�}|j|�||_|jj|j|�dS(NRsRead affinity of IRQ '%s': '%s's/proc/irq/default_smp_affinity(RRtt
interruptsR}RRR�R�R�R�RRKRRt read_fileR�RRROR5(R Rtirq_originalRR R�Rtprev_affinity_hex((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyt_set_all_irq_affinityZs(
cCs�|jj|jd�}|dkr(dSx0|jj�D]\}}|j||t�q8W|j}|j |�|jj
|j�dS(N(ROR,R5R
RR�RR"RRRU(R RR R((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyt_restore_all_irq_affinityrs
cCs_t|�jt|��}|r>tjtj||f�ntjtj|||f�|S(N(R�tissubsetRRRSR$tSTR_VERIFY_PROFILE_VALUE_OKR�tSTR_VERIFY_PROFILE_VALUE_FAIL(R tirq_descriptiontcorrect_affinitytcurrent_affinityR�((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyt_verify_irq_affinity|scCs�tj�}t}x||j�D]n}yQ||d}tjd||f�d|}|j|||�sut}nWqtk
r�qqXqW|j j
d�}|j j|�}|jd||�s�t}n|S(NRs#Read SMP affinity of IRQ '%s': '%s'sSMP affinity of IRQ %ss/proc/irq/default_smp_affinitysdefault IRQ SMP affinity(RtRR"R}RRR�RRKR�RRR�(R RRR�R RRtcurrent_affinity_hex((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyt_verify_all_irq_affinity�s,
RlRi
c
Csd}|dk r�t|jj|��}t|j�}|j|�r[t||�}q�djg|jD]}t|�^qk�} t j
d|| f�n|s�|r�|dkr�dS|r�|j|�S|r�|j|�|j
|�n
|j�dS(Nt,sJInvalid isolated_cores specified, '%s' does not match available cores '%s'(R
R�Rtcpulist_unpackR2RR�R�R�RRR�RRRR(
R R�RR�R�Rtisolatedtpresenttxtstr_cpus((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyt_isolated_cores�s"+
(4RRt__doc__R�t
SCHED_FIFOtSCHED_BATCHtSCHED_RRtSCHED_OTHERt
SCHED_IDLER�RRjRktclassmethodRpRyR�R�R�RwR�R�R�R�RKR�R�R�R�R�R�RTR�R�R�R�tcommand_customR.R/R�R�R�RRRRRRRRRR&(((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyR1s\
(
*
(RRt
decoratorst
tuned.logsttunedR�t
subprocessRZR0R�ttuned.constsR$RtR�ttuned.utils.commandsRR�tlogsR,RRtobjectRRtPluginR(((sB/usr/lib/python2.7/site-packages/tuned/plugins/plugin_scheduler.pyt<module>s
|