Your IP : 216.73.216.213


Current Path : /opt/alt/python37/lib/python3.7/site-packages/logilab/common/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib/python3.7/site-packages/logilab/common/__pycache__/proc.cpython-37.pyc

B

�d{T�$�@s`dZdZddlZddlZddlmZmZmZmZddl	m	Z	m
Z
mZmZm
Z
ddlmZmZmZmZddlmZddlmZGd	d
�d
e�Zdd�Zd
ZdZdZdZdZdZGdd�de�ZGdd�d�Z yGdd�de!�Z"Wn&e#k
�rGdd�de�Z"YnXGdd�de"�Z$Gdd�de"�Z%Gdd�de"�Z&e"e'fZ(Gd d!�d!e�Z)Gd"d#�d#�Z*dS)$z�module providing:
* process information (linux specific: rely on /proc)
* a class for resource control (memory / time / cpu time)

This module doesn't work on windows platforms (only tested on linux)

:organization: Logilab



zrestructuredtext en�N)�	getrlimit�	setrlimit�
RLIMIT_CPU�	RLIMIT_AS)�signal�SIGXCPU�SIGKILL�SIGUSR2�SIGUSR1)�Timer�
currentThread�Thread�Event)�time)�Nodec@seZdZdS)�
NoSuchProcessN)�__name__�
__module__�__qualname__�rr�D/opt/alt/python37/lib/python3.7/site-packages/logilab/common/proc.pyr(srcCstj�d|�st��dS)zTcheck the a pid is registered in /proc
    raise NoSuchProcess exception if not
    z/proc/%sN)�os�path�existsr)�pidrrr�proc_exists*sr��
����c@sJeZdZdZdd�Zdd�Zdd�Zdd	d
�Zdd�Zd
d�Z	dd�Z
dS)�ProcInfoz4provide access to process information found in /proccCsDt|�|_t�||j�t|j�d|j|_t|��t�|_dS)Nz
/proc/%s/stat)	�intrr�__init__r�file�status�PPID�ppid)�selfrrrrr$;s


zProcInfo.__init__cCs*yt|��t�Stk
r$dSXdS)z,return the memory usage of the process in KorN)r#r&�VSIZE�IOError)r)rrr�memory_usageBszProcInfo.memory_usagecCs|��tdd�|jD��S)NcSsg|]}|���qSr)�lineage_memory_usage)�.0�childrrr�
<listcomp>Jsz1ProcInfo.lineage_memory_usage.<locals>.<listcomp>)r,�sum�children)r)rrrr-IszProcInfo.lineage_memory_usagercCsD|��}t|t�t|t�}|r@|t|t�t|t�7}|S)zareturn the number of jiffies that this process has been scheduled
        in user and kernel mode)r&r#�UTIME�STIME�CUTIME�CSTIME)r)r2r&rrrrrMs
z
ProcInfo.timecCst|j�����S)z3return the list of fields found in /proc/<pid>/stat)�openr%�read�split)r)rrrr&VszProcInfo.statuscCs|��d�d�S)z:return the process name found in /proc/<pid>/stat
        �z())r&�strip)r)rrr�nameZsz
ProcInfo.namecCst�|j�tjS)z&return the age of the process
        )r�statr%�ST_MTIME)r)rrr�age_szProcInfo.ageN)r)rrr�__doc__r$r,r-rr&r<r?rrrrr"8s
	r"c@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�ProcInfoLoaderzmanage process informationcCs
i|_dS)N)�_loaded)r)rrrr$gszProcInfoLoader.__init__ccs*x$t�d�D]}|��rt|�VqWdS)z%return a list of existent process idsz/procN)r�listdir�isdigitr#)r)Zsubdirrrr�	list_pidsjszProcInfoLoader.list_pidscCsDt|�}y
|j|Stk
r>t|�}||_||j|<|SXdS)z%get a ProcInfo object for a given pidN)r#rB�KeyErrorr"�manager)r)r�procinforrr�loadps

zProcInfoLoader.loadc	Cs`xZ|��D]N}y4|�|�}|jdkr@|jr@|�|j�}|�|�Wq
tk
rVYq
Xq
WdS)zload all processes informationN)rErI�parentr(�appendr)r)rrHZ	pprocinforrr�load_all|s
zProcInfoLoader.load_allN)rrrr@r$rErIrLrrrrrAds
rAc@seZdZdZdZdS)�
ResourceErrorz*Error raise when resource limit is reachedzUnknown Resource LimitN)rrrr@�limitrrrrrM�srMc@seZdZdZdZdS)rMz*Error raise when resource limit is reachedzUnknown Resource LimitN)rrrr@rNrrrrrM�sc@seZdZdZdZdS)�	XCPUErrorz+Error raised when CPU Time limit is reachedzCPU TimeN)rrrr@rNrrrrrO�srOc@seZdZdZdZdS)�LineageMemoryErrorz\Error raised when the total amount of memory used by a process and
    it's child is reachedzLineage total MemoryN)rrrr@rNrrrrrP�srPc@seZdZdZdZdS)�TimeoutErrorz9Error raised when the process is running for to much timez	Real TimeN)rrrr@rNrrrrrQ�srQc@s0eZdZdZe��fdd�Zdd�Zdd�ZdS)	�MemorySentinelz\A class checking a process don't use too much memory in a separated
    daemonic thread
    cCs:tj||jdd�||_t�|_||_|�d�||_dS)Nz
Test.Sentinel)�targetr<T)	r
r$�_run�memory_limitr�_stop�interval�	setDaemon�gpid)r)rWrUrYrrrr$�s
zMemorySentinel.__init__cCs|j��dS)zstop apN)rV�set)r)rrr�stop�szMemorySentinel.stopcCsLt�}x@|j��sF|j|�|j���kr6t�|jt	�|j�
|j�qWdS)N)rArV�isSetrUrIrYr-r�killpgr
�waitrW)r)ZpilrrrrT�s
zMemorySentinel._runN)	rrrr@r�getpidr$r[rTrrrrrR�srRc@sFeZdZddd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Zdd�Z	dS)�ResourceControllerN�<cCsntdkrtd��||_||_||_||_d|_d|_d|_d|_	d|_
d|_d|_d|_
d|_d|_d|_dS)N���zUnsupported platformr)r�RuntimeError�max_time�
max_memory�max_cpu_time�	_reprieve�_timer�
_msentinel�_old_max_memory�_old_usr1_hdlr�_old_max_cpu_time�_old_usr2_hdlr�_old_sigxcpu_hdlr�
_limit_set�
_abort_try�_start_time�_elapse_time)r)rfrdreZmax_reprieverrrr$�s"zResourceController.__init__cCs
t��dS)N)rQ)r)�sig�framerrr�_hangle_sig_timeout�sz&ResourceController._hangle_sig_timeoutcCs8|j|jkr$|jd7_td��nt�t��t�dS)Nr:zMemory limit reached)rprgrPrr]r_r)r)rsrtrrr�_hangle_sig_memory�s
z%ResourceController._hangle_sig_memorycCs8|j|jkr$|jd7_td��nt�t��t�dS)Nr:zSoft CPU time limit reached)rprgrOrr]r_r)r)rsrtrrr�_handle_sigxcpu�s
z"ResourceController._handle_sigxcpucCsb|j|jkrN|jd7_t�t��t�|jdkr^td|j�|_	|j	�
�nt�t��t�dS)Nr:r)rprgrr]r_r	ror�	_time_outrh�startr)r)rrrrx�s
zResourceController._time_outcCs$t���dkst�t��|jdk�r|jdk	rttt|j	�|_
ttdt
|j�|j�|j�|_t
t��|_|j��|jdk	r�tt�|_t
|j�|jdf}tt|j�|_tt|�|jdk	�rtdt
|j��|_tt�|_ tt!|j"�|_#t
|j�|j df}tt|�|j��|jd7_dS)zset up the process limit�
MainThreadrNr:)$r�getName�AssertionErrorr�setpgrprordrr	rurmr�maxr#rrrxrhrrqryrfrrrlrrwrnrrerRrirrjr
rvrk)r)Z	cpu_limitZas_limitrrr�setup_limit�s,








zResourceController.setup_limitcCs�|jdkr�|jdk	rJ|j��|jtt��|j7_d|_tt	|j
�|jdk	rltt
|j�tt|j�|jdk	r�|j��d|_tt|j�tt|j�|jd8_dS)zreinstall the old process limitrNr:)rordrh�cancelrrr#rrqrr	rmrfrrrlrrnrerir[rrjr
rk)r)rrr�clean_limits





zResourceController.clean_limit)NNNra)
rrrr$rurvrwrxrr�rrrrr`�s

r`)+r@�
__docformat__rr=�resourcerrrrrrrr	r
�	threadingrrr
rrZlogilab.common.treer�	Exceptionrrr'r3r4r5r6r*r"rA�
BaseExceptionrM�	NameErrorrOrPrQ�MemoryErrorZRESOURCE_LIMIT_EXCEPTIONrRr`rrrr�<module>s8,$