????

Your IP : 18.191.130.191


Current Path : /lib/python3.6/site-packages/glances/__pycache__/
Upload File :
Current File : //lib/python3.6/site-packages/glances/__pycache__/processes.cpython-36.opt-1.pyc

3

Eo�c�Q�@s�ddlZddlmZddlmZmZmZmZddlm	Z	m
Z
ddlmZddl
mZddlZddd	d
ddgZd
ddddddd�ZGdd�de�Zdd�Zd"dd�Zd#dd�Zd$dd�Zd%d d!�Ze�ZdS)&�N)�iterkeys)�BSD�LINUX�MACOS�WINDOWS)�Timer�getTimeSinceLastUpdate)�
GlancesFilter)�logger�cpu_percent�memory_percent�username�	cpu_times�io_counters�namezdisk IOzCPU consumptionzmemory consumptionzprocess timez	user namez
processs name�None)rrrrr
rNc@s8eZdZdZd@dd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
edd��Zedd��Z
edd��Zejdd��Zedd��Zedd��Zejdd��Zedd��Zedd ��Zd!d"�Zed#d$��Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�ZdAd2d3�Zed4d5��ZdBd7d8�Zd9d:�Zd;d<�Z dCd>d?�Z!d1S)D�GlancesProcessesz-Get processed stats using the psutil library.�<cCsFi|_i|_||_td�|_i|_d|_d|_|jddd�g|_	|j
�i|_d|_d|_
ytj�}|j�Wn8tk
r�}ztjdj|��d|_WYdd}~XnXtjd�d|_ytj�}|j�Wn:tk
�r}ztjd	j|��d|_WYdd}~XnXtjd
�d|_d|_t�|_d|_d
|_i|_|j�dS)z0Init the class to collect stats about processes.rN�autoT)rFz.PsUtil can not grab processes io_counters ({})z%PsUtil can grab processes io_countersz'PsUtil can not grab processes gids ({})zPsUtil can grab processes gidsrr)rr) Zusername_cacheZ
cmdline_cache�
cache_timeoutr�cache_timer�io_old�	auto_sort�	_sort_key�set_sort_key�processlist�reset_processcount�processlist_cache�disable_tag�disable_extended_tag�psutil�Processr�	Exceptionr
�warning�format�disable_io_counters�debug�gids�disable_gids�_max_processesr	�_filter�no_kernel_threads�_max_values_list�_max_values�reset_max_values)�selfr�p�e�r2�/usr/lib/python3.6/processes.py�__init__&sF


zGlancesProcesses.__init__cCsdddddd�|_dS)zReset the global process countrN)�totalZrunningZsleeping�thread�pid_max)�processcount)r/r2r2r3rpsz#GlancesProcesses.reset_processcountcsj|j|jd<x2t|j�D]$�ttt�fdd�|���|j�<qWtdd�|D��|jd<t|�|jd<dS)	z?Update the global process count from the current processes listr7cs|d�kS)N�statusr2)�v)�kr2r3�<lambda>{sz6GlancesProcesses.update_processcount.<locals>.<lambda>css"|]}|ddk	r|dVqdS)�num_threadsNr2)�.0�ir2r2r3�	<genexpr>}sz7GlancesProcesses.update_processcount.<locals>.<genexpr>r6r5N)r7r8r�len�list�filter�sum)r/Zplistr2)r;r3�update_processcountts
$z$GlancesProcesses.update_processcountcCsd|_|j�dS)zEnable process stats.FN)r�update)r/r2r2r3�enable�szGlancesProcesses.enablecCs
d|_dS)zDisable process stats.TN)r)r/r2r2r3�disable�szGlancesProcesses.disablecCsd|_|j�dS)zEnable extended process stats.FN)rrF)r/r2r2r3�enable_extended�sz GlancesProcesses.enable_extendedcCs
d|_dS)zDisable extended process stats.TN)r)r/r2r2r3�disable_extended�sz!GlancesProcesses.disable_extendedcCsJtrBy"tdd��}t|j��SQRXWqFttfk
r>dSXndSdS)a�
        Get the maximum PID value.

        On Linux, the value is read from the `/proc/sys/kernel/pid_max` file.

        From `man 5 proc`:
        The default value for this file, 32768, results in the same range of
        PIDs as on earlier kernels. On 32-bit platforms, 32768 is the maximum
        value for pid_max. On 64-bit systems, pid_max can be set to any value
        up to 2^22 (PID_MAX_LIMIT, approximately 4 million).

        If the file is unreadable or not available for whatever reason,
        returns None.

        Some other OSes:
        - On FreeBSD and macOS the maximum is 99999.
        - On OpenBSD >= 6.0 the maximum is 99999 (was 32766).
        - On NetBSD the maximum is 30000.

        :returns: int or None
        z/proc/sys/kernel/pid_max�rbN)r�open�int�read�OSError�IOError)r/�fr2r2r3r7�szGlancesProcesses.pid_maxcCst|jdtjdd�S)z5Get the current number of processes showed in the UI.�r5�)�minr)�glances_processesr8)r/r2r2r3�processes_count�sz GlancesProcesses.processes_countcCs|jS)z5Get the maximum number of processes showed in the UI.)r))r/r2r2r3�
max_processes�szGlancesProcesses.max_processescCs
||_dS)z5Set the maximum number of processes showed in the UI.N)r))r/�valuer2r2r3rW�scCs|jjS)z+Get the process filter (given by the user).)r*Zfilter_input)r/r2r2r3�process_filter_input�sz%GlancesProcesses.process_filter_inputcCs|jjS)z.Get the process filter (current apply filter).)r*rC)r/r2r2r3�process_filter�szGlancesProcesses.process_filtercCs||j_dS)zSet the process filter.N)r*rC)r/rXr2r2r3rZ�scCs|jjS)zGet the process filter key.)r*Z
filter_key)r/r2r2r3�process_filter_key�sz#GlancesProcesses.process_filter_keycCs|jjS)z,Get the process regular expression compiled.)r*Z	filter_re)r/r2r2r3�process_filter_re�sz"GlancesProcesses.process_filter_recCs
d|_dS)z&Ignore kernel threads in process list.TN)r+)r/r2r2r3�disable_kernel_threads�sz'GlancesProcesses.disable_kernel_threadscCs|jdks|jdkrdSdS)zDReturn True to sort processes in reverse 'key' order, False instead.rr
FT)�sort_key)r/r2r2r3�sort_reverse�szGlancesProcesses.sort_reversecCs|jS)zReturn the max values dict.)r-)r/r2r2r3�
max_values�szGlancesProcesses.max_valuescCs
|j|S)z/Get the maximum values of the given stat (key).)r-)r/�keyr2r2r3�get_max_values�szGlancesProcesses.get_max_valuescCs||j|<dS)z0Set the maximum value for a specific stat (key).N)r-)r/rarXr2r2r3�set_max_values�szGlancesProcesses.set_max_valuescCs$i|_x|jD]}d|j|<qWdS)zReset the maximum values dict.gN)r-r,)r/r;r2r2r3r.�sz!GlancesProcesses.reset_max_valuesc2szg�_�j��jrdStd�}ddddddg}d	d
dg}dd
g}�jsR|jd��jsb|jd�|j|��jj	�r�||7}�jj
�j��jj�d}nd}t
t�fdd�tj|dd����_dd��jD��_t�j�jdd��_�j�j�d}�x$�jD�]�|�r��j�r�i}�y
tj�d�}dddg}	t�rH|	dg7}	t�rX|	dg7}	|j|	dd�}t�r�ytdd�|j�D��|d<Wn>tjtfk
�r�Yn$tjtfk
�r�d|d<YnXy,t|j dd ��|d<t|j d!d ��|d!<Wn.tjtjfk
�r$d|d<d|d!<YnXWnDtjt!t"fk
�rl}
zt#j$d"j%|
��d|d#<WYdd}
~
XnXt#j&d$j%�d��d|d#<�j'|�d}d�d%<|�d&<t(�d�dd'�j)��d<d�k�rJ�ddk	�rJ�dj*�dj+g}y|�j,�d�d<d'}Wn*tk
�r8|d(d(g�d<d(}YnX|�j,�d<nd(d(gd(d(g�d<d(}�d|g7<|�r��d�j-k�r�y(tj�dd)�j|dd��j-�d<Wntjk
�r�YnXy�j'�j-�d�Wntk
�r�YnXn�fd*d+�|D��j-�d<�qWt
t�fd,d��j���_x:�j.D]0��fd-d��jD�}
|
�rB�j/�t0|
���qBWdS).zUpdate the processes stats.NZprocess_diskrrrrr9r=Zmemory_info�nice�pidZcmdliner
rr'FTcsXto|jddkoVto$|jddkoVto8|jddkoV�joTtoT|jdjdkS)NrZidlezSystem Idle ProcessZkernel_taskr'r)r�inforrr+r�real)r0)r/r2r3r<%sz)GlancesProcesses.update.<locals>.<lambda>)ZattrsZad_valuecSsg|]
}|j�qSr2)rf)r>r0r2r2r3�
<listcomp>-sz+GlancesProcesses.update.<locals>.<listcomp>)�	sorted_by�reverseZcpu_affinityZioniceZnum_ctx_switchesZnum_fdsZnum_handlescSsg|]
}|j�qSr2)Zswap)r>r:r2r2r3rhPsZmemory_swapZtcp)ZkindZudpz Can not grab extended stats ({})�extended_statsz"Grab extended stats for process {}ra�time_since_updaterSr)recsi|]}�||�qSr2r2)r>�cached)�procr2r3�
<dictcomp>�sz+GlancesProcesses.update.<locals>.<dictcomp>cs�jj|�S)N)r*Zis_filtered)r0)r/r2r3r<�scs g|]}|�dk	r|��qS)Nr2)r>r?)r;r2r3rh�s)1rrrrr%�appendr(�extendrZfinished�setr�resetrBrCr Zprocess_iter�
sort_statsr^rErr!rrZas_dictrDZmemory_mapsZ
NoSuchProcess�KeyError�AccessDenied�NotImplementedErrorrAZconnections�
ValueError�AttributeErrorr
�errorr$r&rF�str�upperZ
read_bytesZwrite_bytesrrr,rc�max)r/rlZsorted_attrsZdisplayed_attrZcached_attrsZ	is_cached�firstZextendedZtop_processrkr1Zio_newZio_tagZvalues_listr2)r;rnr/r3rF�s�






	




"zGlancesProcesses.updatecCs|jS)zGet the number of processes.)r8)r/r2r2r3�	get_count�szGlancesProcesses.get_countNcCs|jS)zGet the processlist.)r)r/rir2r2r3�getlist�szGlancesProcesses.getlistcCs|jS)zGet the current sort key.)r)r/r2r2r3r^�szGlancesProcesses.sort_keyTcCs&|dkrd|_d|_n||_||_dS)zSet the current sort key.rTrN)rr)r/rarr2r2r3r�s
zGlancesProcesses.set_sort_keycCsbtj|�}y,|j|j�d�tjdj||j���Wn&tjk
r\tjdj|��YnXdS)z�Decrease nice level
        On UNIX this is a number which usually goes from -20 to 20.
        The higher the nice value, the lower the priority of the process.rSz8Set nice level of process {} to {} (higher the priority)zSCan not decrease (higher the priority) the nice level of process {} (access denied)N)r r!rdr
rfr$rvr#)r/rer0r2r2r3�
nice_decrease�s
zGlancesProcesses.nice_decreasecCsbtj|�}y,|j|j�d�tjdj||j���Wn&tjk
r\tjdj|��YnXdS)z�Increase nice level
        On UNIX this is a number which usually goes from -20 to 20.
        The higher the nice value, the lower the priority of the process.rSz7Set nice level of process {} to {} (lower the priority)zRCan not increase (lower the priority) the nice level of process {} (access denied)N)r r!rdr
rfr$rvr#)r/rer0r2r2r3�
nice_increase�s
zGlancesProcesses.nice_increase�cCs,tj|�}tjdj|��|j�|j|�S)zKill process with pidzSend kill signal to process: {})r r!r
r&r$�kill�wait)r/reZtimeoutr0r2r2r3r��s
zGlancesProcesses.kill)r)N)T)r�)"�__name__�
__module__�__qualname__�__doc__r4rrErGrHrIrJ�propertyr7rVrW�setterrYrZr[r\r]r_r`rbrcr.rFrr�r^rr�r�r�r2r2r2r3r#s@
J
!.

	

rcCs|dkrtd�S|S)z Manage None value in dict value.N�inf)�float)rXr2r2r3�weighted�sr�cCs0||d||d||d||dS)zCSpecific case for io_counters

    :return: Sum of io_r + io_w
    rrRrSr�r2)�processri�sorted_by_secondaryr2r2r3�_sort_io_counters�sr�cCs||d||dS)a Specific case for cpu_times

    Patch for "Sorting by process time works not as expected #1321"
    By default PsUtil only takes user time into account
    see (https://github.com/giampaolo/psutil/issues/1339)
    The following implementation takes user and system time into account
    rrSr2)r�rir�r2r2r3�_sort_cpu_times�sr�cCs"d}|dkrt}n|dkrt}|S)z3Return a sort lambda function for the sorted_by keyNrr)r�r�)rir��retr2r2r3�_sort_lambda�sr�Tcs��dkr�dkr|St��d�}|dk	rhy|j||d�Wq�tk
rd|j�fdd�|d�Yq�XnHy|j��fdd�|d�Wn*ttfk
r�|jdd�dd�YnX|S)	z]Return the stats (dict) sorted by (sorted_by).

    Reverse the sort if reverse is True.
    N)rir�)rarjcst|d�t|��fS)Nr)r�)r�)r�r2r3r<szsort_stats.<locals>.<lambda>cst|��t|��fS)N)r�)r�)rir�r2r3r<scSs|ddk	r|dSdS)Nr�~r2)r�r2r2r3r<!sF)r��sortr"ru�	TypeError)Zstatsrir�rjZsort_lambdar2)rir�r3rts"
rt)rr)rr)rr)rrT)�osZglances.compatrZglances.globalsrrrrZ
glances.timerrrZglances.filterr	Zglances.loggerr
r Zsort_processes_key_listZsort_for_human�objectrr�r�r�r�rtrUr2r2r2r3�<module>
s0@




$