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.opt-1.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)�.0Zchildrrr�
<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)r1r&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#rA�KeyErrorr"Zmanager)r)r�procinforrr�loadps

zProcInfoLoader.loadc	Cs`xZ|��D]N}y4|�|�}|jdkr@|jr@|�|j�}|�|�Wq
tk
rVYq
Xq
WdS)zload all processes informationN)rDrG�parentr(�appendr)r)rrFZ	pprocinforrr�load_all|s
zProcInfoLoader.load_allN)rrrr?r$rDrGrJrrrrr@ds
r@c@seZdZdZdZdS)�
ResourceErrorz*Error raise when resource limit is reachedzUnknown Resource LimitN)rrrr?�limitrrrrrK�srKc@seZdZdZdZdS)rKz*Error raise when resource limit is reachedzUnknown Resource LimitN)rrrr?rLrrrrrK�sc@seZdZdZdZdS)�	XCPUErrorz+Error raised when CPU Time limit is reachedzCPU TimeN)rrrr?rLrrrrrM�srMc@seZdZdZdZdS)�LineageMemoryErrorz\Error raised when the total amount of memory used by a process and
    it's child is reachedzLineage total MemoryN)rrrr?rLrrrrrN�srNc@seZdZdZdZdS)�TimeoutErrorz9Error raised when the process is running for to much timez	Real TimeN)rrrr?rLrrrrrO�srOc@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)rUrSrWrrrr$�s
zMemorySentinel.__init__cCs|j��dS)zstop apN)rT�set)r)rrr�stop�szMemorySentinel.stopcCsLt�}x@|j��sF|j|�|j���kr6t�|jt	�|j�
|j�qWdS)N)r@rT�isSetrSrGrWr-r�killpgr
�waitrU)r)ZpilrrrrR�s
zMemorySentinel._runN)	rrrr?r�getpidr$rYrRrrrrrP�srPc@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)rdrbrcZmax_reprieverrrr$�s"zResourceController.__init__cCs
t��dS)N)rO)r)�sig�framerrr�_hangle_sig_timeout�sz&ResourceController._hangle_sig_timeoutcCs8|j|jkr$|jd7_td��nt�t��t�dS)Nr9zMemory limit reached)rnrerNrr[r]r)r)rqrrrrr�_hangle_sig_memory�s
z%ResourceController._hangle_sig_memorycCs8|j|jkr$|jd7_td��nt�t��t�dS)Nr9zSoft CPU time limit reached)rnrerMrr[r]r)r)rqrrrrr�_handle_sigxcpu�s
z"ResourceController._handle_sigxcpucCsb|j|jkrN|jd7_t�t��t�|jdkr^td|j�|_	|j	�
�nt�t��t�dS)Nr9r)rnrerr[r]r	rmr�	_time_outrf�startr)r)rrrrv�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�
MainThreadrNr9)$r�getName�AssertionErrorr�setpgrprmrbrr	rsrkr�maxr#rprvrfrrorwrdrrrjrrurlrrcrPrgrrhr
rtri)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 limitrNr9)rmrbrf�cancelrpr#rrorr	rkrdrrrjrrlrcrgrYrrhr
ri)r)rrr�clean_limits





zResourceController.clean_limit)NNNr_)
rrrr$rsrtrurvr}rrrrrr^�s

r^)+r?Z
__docformat__rr<�resourcerrrrrrrr	r
�	threadingrrr
rrZlogilab.common.treer�	Exceptionrrr'r2r3r4r5r*r"r@�
BaseExceptionrK�	NameErrorrMrNrO�MemoryErrorZRESOURCE_LIMIT_EXCEPTIONrPr^rrrr�<module>s8,$