Your IP : 3.128.94.112


Current Path : /lib64/python3.6/site-packages/psutil/__pycache__/
Upload File :
Current File : //lib64/python3.6/site-packages/psutil/__pycache__/_pslinux.cpython-36.pyc

3

��JZb$�@s�dZddlmZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlmZddlmZddlmZddlmZdd	lmZdd
lmZddlmZddlmZdd
lmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddl$m%Z%ddl$m&Z&ddl$m'Z'ddl$m(Z(ddl)m*Z*ddl)m+Z+ddl)m,Z,ej-d�k�r�ddl.Z.ndZ.d d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/gZ/d0Z0ej1j2d1ej3��Z4e5ed2�Z6e7�Z8e6�r2x(e9e�D]Z:e:j;d3��re/j<e:��qWej=d4�Z>ej=d5�Z?da@e(�rTd�nd6ZAejBd7kZCd8ZDe.dk�rxe
jEZFne.jGd9d:eHe
jE�i�ZIeIjFZFe.dk�r�dZJdZKd;ZLdZMn Gd<d=�d=e.jG�ZNeO�jPeNjQ�ejRejSejTejUejVejWejXejXejYejZd>�
Z[ej\ej]ej^ej_ej`ejaejbejcejdejeejfd?�Zged@dAdBdCdDdEdFdGdHdIdJg
�ZhedKdLdMdNdOdPdQdRdSdTg	�ZiedUdVdWdXdYdZg�Zjed[d\�Zked]ekjld��ZmedadVdbdcd_dddedfdgdhdid`g�Znedjdkdljoenjl��ZpedmdLdMdNdOdndog�Zqdpdq�Zrdrds�Zse(�rdtdu�Ztndvdu�Ztdwdx�Zudydz�Zvd{d|�Zwd}d~�Zxedd���Zye8d�fd�d��Zzyeyd��Wn0e{k
�r�e
j|�ed�d��d�d�d��a}YnXd�d��Z~d�d��Zd�d��Z�d�d��Z�d�d��Z�d�d��Z�d�d��Z�d�d��Z�ej1j2d���s�ej1j2d���r�d�d��Z�ej�Z�Gd�d��d�e{�Z�Gd�d��d��Z�e��Z�d�d�d��Z�d�d��Z�d�d��Z�ej�Z�d�d��Z�d�d�d��Z�d�d��Z�d�d��Z�d�d��Z�d�d��Z�d�d��Z�d�d��Z�d�d��Z�d�d��Z�d�d��Z�Gd�d��d�e7�Z�dS)�zLinux platform implementation.�)�divisionN)�defaultdict)�
namedtuple�)�_common)�_psposix)�
_psutil_linux)�
_psutil_posix)�ENCODING)�
ENCODING_ERRS)�
isfile_strict)�memoize)�memoize_when_activated)�NIC_DUPLEX_FULL)�NIC_DUPLEX_HALF)�NIC_DUPLEX_UNKNOWN)�parse_environ_block)�path_exists_strict)�
supports_ipv6)�
usage_percent)�b)�
basestring)�long)�PY3)�AccessDenied)�
NoSuchProcess)�
ZombieProcess���PROCFS_PATH�IOPRIO_CLASS_NONE�IOPRIO_CLASS_RT�IOPRIO_CLASS_BE�IOPRIO_CLASS_IDLE�CONN_ESTABLISHED�
CONN_SYN_SENT�
CONN_SYN_RECV�CONN_FIN_WAIT1�CONN_FIN_WAIT2�CONN_TIME_WAIT�
CONN_CLOSE�CONN_CLOSE_WAIT�
CONN_LAST_ACK�CONN_LISTEN�CONN_CLOSINGz/sys/class/power_supplyz/proc/%s/smaps�
linux_prlimitZRLIM�
SC_CLK_TCK�SC_PAGE_SIZEi �littlei�
AddressFamily�AF_LINK�c@seZdZdZdZdZdZdS)�
IOPriorityrrr5rN)�__name__�
__module__�__qualname__r r!r"r#�r:r:� /usr/lib64/python3.6/_pslinux.pyr6jsr6)
�R�S�D�T�t�Z�X�x�K�W)Z01Z02Z03Z04Z05Z06Z07Z08Z09Z0AZ0B�svmem�total�	available�percent�used�free�active�inactive�buffers�cached�shared�sdiskioZ
read_countZwrite_countZ
read_bytesZwrite_bytesZ	read_timeZ
write_timeZread_merged_countZwrite_merged_count�	busy_time�	popenfile�path�fdZposition�mode�flags�pmemz"rss vms shared text lib data dirty�pfullmem�uss�pss�swap�
pmmap_grouped�rss�sizeZshared_cleanZshared_dirtyZ
private_cleanZ
private_dirtyZ
referencedZ	anonymous�	pmmap_extzaddr perms � �pioZ
read_charsZwrite_charscKst|df|�S)N�rb)�open)�fname�kwargsr:r:r;�open_binary�srgcKs*tr|jdt�|jdt�t|df|�S)z�On Python 3 opens a file in text mode by using fs encoding and
    a proper en/decoding errors handler.
    On Python 2 this is just an alias for open(name, 'rt').
    �encoding�errors�rt)r�
setdefaultr
rrd)rerfr:r:r;�	open_text�srlcCs|jttd�S)N)rhri)�decoder
r)�sr:r:r;rm�srmcCs|S)Nr:)rnr:r:r;rm�scCstjdjS)z+Return updated psutil.PROCFS_PATH constant.Zpsutil)�sys�modulesrr:r:r:r;�get_procfs_path�srqcCsNt|t�st|��tj|�}|jd�d}|jd�rJt|�rJ|dd�}|S)zWrapper around os.readlink().�rz
 (deleted)N�
i����)�
isinstancer�AssertionError�os�readlink�split�endswithr)rTr:r:r;rw�s
rwcCsXtjdtjdtjdi}||tjtjBtjB@}|tj@rH|jddd�}|jdd�}|S)zZConvert file's open() flags into a readable string.
    Used by Process.open_files().
    �r�wzw+�arzr+)rv�O_RDONLY�O_WRONLY�O_RDWR�O_APPEND�replace)rWZ	modes_maprVr:r:r;�file_flags_to_mode�s
r�cCsDy&td|d��}t|j��SQRXWnttfk
r>tSXdS)zKReturn the sector size of a partition.
    Used by disk_io_counters().
    z"/sys/block/%s/queue/hw_sector_sizerjN)rd�int�read�IOError�
ValueError�SECTOR_SIZE_FALLBACK)�	partition�fr:r:r;�get_sector_size�s
r�cCs�td|��}|j�j�dd�}WdQRXdddddd	d
g}t|�}|dkrX|jd�|d
krj|jd�|dkr||jd�td|�adS)z�Set a namedtuple of variable fields depending on the CPU times
    available on this Linux kernel version which may be:
    (user, nice, system, idle, iowait, irq, softirq, [steal, [guest,
     [guest_nice]]])
    Used by cpu_times() function.
    z%s/statrN�user�nice�systemZidleZiowaitZirqZsoftirq�Zsteal�	ZguestrsZ
guest_nice�	scputimes)rg�readlinerx�len�appendrr�)�procfs_pathr��values�fieldsZvlenr:r:r;�set_scputimes_ntuples	


r�TcCsTy,|rt|�nt|��}|j�j�SQRXWn"tk
rN|tk	rH|S�YnXdS)z�Return file content.
    fallback: the value returned in case the file does not exist or
              cannot be read
    binary: whether to open the file in binary or text mode.
    N)rgrlr��stripr��_DEFAULT)re�fallback�binaryr�r:r:r;�cat sr�z/procr�zuser system idlegcCs|d}||jdd�}y|d}|d}|d}Wntk
rH|SXytdt��}Wntk
rp|SXd}|�:x2|D]*}|j�}|jd�r�|t|j�d	�7}q�WWd
QRX|t	9}|}||}	||}
|
t
|
d|�8}
|	|
7}	|	|t
|d|�7}	t|	�S)
a�Fallback for kernels < 3.14 where /proc/meminfo does not provide
    "MemAvailable:" column, see:
    https://blog.famzah.net/2014/09/24/
    This code reimplements the algorithm outlined here:
    https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/
        commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773

    XXX: on recent kernels this calculation differs by ~1.5% than
    "MemAvailable:" as it's calculated slightly differently, see:
    https://gitlab.com/procps-ng/procps/issues/42
    https://github.com/famzah/linux-memavailable-procfs/issues/2
    It is still way more realistic than doing (free + cached) though.
    sMemFree:sCached:rs
Active(file):sInactive(file):s
SReclaimable:z%s/zoneinfoslowrNr5g@)�get�KeyErrorrgrqr�r��
startswithr�rx�PAGESIZE�min)�memsrKr�Zlru_active_fileZlru_inactive_fileZslab_reclaimabler�Z
watermark_low�line�availZ	pagecacher:r:r;�calculate_avail_vmem=s4

"r�cNCs�g}i}tdt���4}x,|D]$}|j�}t|d�d||d<qWWdQRX|d}|d}y|d}Wn"tk
r�d}|jd	�YnXy|d
}Wn"tk
r�d}|jd�YnX||jdd�7}y|d
}	WnHtk
�r$y|d}	Wn$tk
�rd}	|jd�YnXYnXy|d}
Wn$tk
�rVd}
|jd�YnXy|d}WnXtk
�r�y|d|d|d}Wn$tk
�r�d}|jd�YnXYnX||||}|dk�r�||}y|d}
Wntk
�r
t|�}
YnX|
dk�r$d}
|jd�|
|k�r2|}
t||
|dd�}|�rzddj	|�t
|�dk�rfdndf}tj|t
�t||
||||
||||	�
S)a�Report virtual memory stats.
    This implementation matches "free" and "vmstat -s" cmdline
    utility values and procps-ng-3.3.12 source was used as a reference
    (2016-09-18):
    https://gitlab.com/procps-ng/procps/blob/
        24fd2605c51fccc375ab0287cec33aa767f06718/proc/sysinfo.c
    For reference, procps-ng-3.3.10 is the version available on Ubuntu
    16.04.

    Note about "available" memory: up until psutil 4.3 it was
    calculated as "avail = (free + buffers + cached)". Now
    "MemAvailable:" column (kernel 3.14) from /proc/meminfo is used as
    it's more accurate.
    That matches "available" column in newer versions of "free".
    z
%s/meminforirNs	MemTotal:sMemFree:sBuffers:rNsCached:rOs
SReclaimable:sShmem:s
MemShared:rPsActive:rLs	Inactive:sInact_dirty:sInact_clean:sInact_laundry:rMs
MemAvailable:rH)�_roundz6%s memory stats couldn't be determined and %s set to 0z, ZwasZwere)rgrqrxr�r�r�r�r�r�joinr��warnings�warn�RuntimeWarningrF)Zmissing_fieldsr�r�r�r�rGrKrNrOrPrLrMrJr�rI�msgr:r:r;�virtual_memoryrsz
&
	


r�c%Cs�i}tdt���4}x,|D]$}|j�}t|d�d||d<qWWdQRXy|d}|d}Wn:tk
r�tj�\}}}}}}}||9}||9}YnX||}t||dd�}	ytd	t��}WnDtk
�r}
z&d
t	|
�}t
j|t�d}}
WYdd}
~
Xn�X|��d}}
x�|D]j}|j
d��rJt|jd�d�d
d}n&|j
d��rpt|jd�d�d
d}
|dk	�r|
dk	�rP�qWd}t
j|t�d}}
WdQRXtj||||	||
�S)zReturn swap memory metrics.z
%s/meminforirNs
SwapTotal:s	SwapFree:)r�z	%s/vmstatzP'sin' and 'sout' swap memory stats couldn't be determined and were set to 0 (%s)spswpin� rspswpoutzK'sin' and 'sout' swap memory stats couldn't be determined and were set to 0)rgrqrxr�r��cextZ
linux_sysinforr��strr�r�r�r�rZsswap)r�r�r�r�rGrK�_Zunit_multiplierrJrI�errr�ZsinZsoutr:r:r;�swap_memory�sD
&

r�cCs^t�}t|�td|��}|j�j�}WdQRX|dttj�d�}dd�|D�}t|�S)z�Return a named tuple representing the following system-wide
    CPU times:
    (user, nice, system, idle, iowait, irq, softirq [steal, [guest,
     [guest_nice]]])
    Last 3 fields may not be available on all Linux kernel versions.
    z%s/statNrcSsg|]}t|�t�qSr:)�float�CLOCK_TICKS)�.0rCr:r:r;�
<listcomp>-szcpu_times.<locals>.<listcomp>)rqr�rgr�rxr�r��_fields)r�r�r�r�r:r:r;�	cpu_times!sr�cCs�t�}t|�g}td|��d}|j�xT|D]L}|jd�r.|j�}|dttj�d�}dd�|D�}t|�}|j	|�q.W|SQRXdS)zfReturn a list of namedtuple representing the CPU times
    for every CPU available on the system.
    z%s/statscpurcSsg|]}t|�t�qSr:)r�r�)r�rCr:r:r;r�?sz!per_cpu_times.<locals>.<listcomp>N)
rqr�rgr�r�rxr�r�r�r�)r��cpusr�r�r�r��entryr:r:r;�
per_cpu_times1s

r�cCs�y
tjd�Stk
r�d}tdt���*}x"|D]}|j�jd�r4|d7}q4WWdQRX|dkr�tjd�}t	dt���4}x,|D]$}|j
d	�d}|j|�r�|d7}q�WWdQRX|dkr�dS|SXdS)
z0Return the number of logical CPUs in the system.�SC_NPROCESSORS_ONLNrz
%s/cpuinfos	processorrNzcpu\dz%s/statra)rv�sysconfr�rgrq�lowerr��re�compilerlrx�match)Znumr�r��searchr:r:r;�cpu_count_logicalEs$




r�cCs�i}i}tdt���z}xr|D]j}|j�j�}|sXd|krRd|krR|d||d<i}q|jd�sl|jd�r|jdd�\}}t|�||<qWWdQRXt|j��p�dS)z2Return the number of physical cores in the system.z
%s/cpuinfosphysical ids	cpu coress	:rN)	rgrqr�r�r�rxr��sumr�)�mappingZcurrent_infor�r��key�valuer:r:r;�cpu_count_physicalbs


r�cCs�tdt����}d}d}d}xx|D]p}|jd�rBt|j�d�}n6|jd�r^t|j�d�}n|jd�rxt|j�d�}|dk	r"|dk	r"|dk	r"Pq"WWdQRXd}tj||||�S)z*Return various CPU stats as a named tuple.z%s/statNsctxtrsintrssoftirqr)rgrqr�r�rxrZ	scpustats)r�Zctx_switchesZ
interruptsZsoft_interruptsr�Zsyscallsr:r:r;�	cpu_stats{s"



r�z/sys/devices/system/cpu/cpufreqz$/sys/devices/system/cpu/cpu0/cpufreqcCs�g}tjd�}|r$|jdd�d�ntjd�}|jdd�d�tjj}x�|D]�}t||d�dd	�}|dkr�t||d
�dd	�}|dkr�td��t|�d}tt||d
���d}tt||d���d}|jt	j
|||��qLW|S)z�Return frequency metrics for all CPUs.
        Contrarily to other OSes, Linux updates these values in
        real-time.
        z'/sys/devices/system/cpu/cpufreq/policy*cSsttjj|�dd��S)N�)r�rvrT�basename)rCr:r:r;�<lambda>�szcpu_freq.<locals>.<lambda>)r�z)/sys/devices/system/cpu/cpu[0-9]*/cpufreqcSsttjd|�jd��S)Nz[0-9]+r)r�r�r��group)rCr:r:r;r��sZscaling_cur_freqN)r�Zcpuinfo_cur_freqz!can't find current frequency filei�Zscaling_max_freqZscaling_min_freq)�glob�sortrvrTr�r��NotImplementedErrorr�r�rZscpufreq)�ret�lsZpjoinrTZcurrZmax_Zmin_r:r:r;�cpu_freq�s&


r�c@seZdZdS)�_Ipv6UnsupportedErrorN)r7r8r9r:r:r:r;r��sr�c@sZeZdZdZdd�Zdd�Zdd�Zedd	��Zeddd��Z	edd
d��Z
ddd�Zd
S)�ConnectionsawA wrapper on top of /proc/net/* files, retrieving per-process
    and system-wide open connections (TCP, UDP, UNIX) similarly to
    "netstat -an".

    Note: in case of UNIX sockets we're only able to determine the
    local endpoint/path, not the one it's connected to.
    According to [1] it would be possible but not easily.

    [1] http://serverfault.com/a/417946
    cCs�dtjtjf}dtjtjf}dtjtjf}dtjtjf}dtjdf}|||||f||f|f|f||f|f|f|f||||f||f||fd�|_d|_dS)N�tcp�tcp6�udp�udp6�unix)�allr��tcp4r�r��udp4r�r��inetZinet4Zinet6)�socket�AF_INET�SOCK_STREAM�AF_INET6Z
SOCK_DGRAMZAF_UNIX�tmap�_procfs_path)�selfr�r�r�r�r�r:r:r;�__init__�s"
zConnections.__init__cCs�tt�}x�tjd|j|f�D]�}ytd|j||f�}WnJtk
r�}z.|jtjtj	fkrbwn|jtj
krrwn�WYdd}~XqX|jd�r|dd�dd�}||j|t
|�f�qW|S)Nz%s/%s/fdz%s/%s/fd/%szsocket:[r�r���)r�listrv�listdirr�rw�OSError�errno�ENOENT�ESRCH�EINVALr�r�r�)r��pid�inodesrU�inoder�r:r:r;�get_proc_inodes�s
zConnections.get_proc_inodescCsli}xbt�D]X}y|j|j|��Wqtk
rb}z"|jtjtjtjtjfkrR�WYdd}~XqXqW|S)N)	�pids�updater�r�r�r�r��EPERM�EACCES)r�r�r�r�r:r:r;�get_all_inodes�szConnections.get_all_inodesc
Cs�|jd�\}}t|d�}|s fStr.|jd�}|tjkrntrZtj|tj	|�ddd��}q�tj|tj	|��}nxtj	|�}yJtr�tjtj
tjd	tj
d|����}n tjtj
tjd
tj
d|����}Wn"tk
r�t�s�t�n�YnXtj||�S)a�Accept an "ip:port" address as displayed in /proc/net/*
        and convert it into a human readable form, like:

        "0500000A:0016" -> ("10.0.0.5", 22)
        "0000000000000000FFFF00000100007F:9E49" -> ("::ffff:127.0.0.1", 40521)

        The IP address portion is a little or big endian four-byte
        hexadecimal number; that is, the least significant byte is listed
        first, so we need to reverse the order of the bytes to convert it
        to an IP address.
        The port is represented as a two-byte hexadecimal number.

        Reference:
        http://linuxdevcenter.com/pub/a/linux/2000/11/16/LinuxAdmin.html
        �:��asciiNr�>4I�<4Ir�)r�)r�)rxr�r�encoder�r��
LITTLE_ENDIANZ	inet_ntop�base64Z	b16decoder��struct�pack�unpackr�rr�r�addr)r�familyZipZportr:r:r;�decode_address
s0



zConnections.decode_addressNccsF|jd�rtjj|�rdSt|td���}|j��xt|d�D]�\}}y(|j�dd�\
}}	}
}}}}}}}Wn&t	k
r�t
d|||f��YnX||kr�||d\}
}nd	\}
}|dk	r�||
kr�qBqB|tjkr�t
|}ntj}ytj|	|�}	tj|
|�}
Wntk
�rwBYnX||||	|
||
fVqBWWdQRXdS)
z.Parse /proc/net/tcp* and /proc/net/udp* files.�6N)�	bufferingrrsz,error while parsing %s; malformed line %s %rrr�)Nr�)ryrvrT�existsrl�BIGFILE_BUFFERINGr��	enumeraterxr��RuntimeErrorr�r��TCP_STATUSESr�	CONN_NONEr�rr�)�filer�type_r��
filter_pidr��linenor�r��laddr�raddr�statusr�r�rUr:r:r;�process_inetDs2(

zConnections.process_inetccst|td���}|j�x�|D]�}|j�}y|dd�\}}}}}}}	Wn.tk
rtd|kr`wtd||f��YnX|	|kr�||	}
ndg}
xd|
D]\\}}|dk	r�||kr�q�q�t|�dkr�|d}
nd	}
t|�}d	}tj	}||||
|||fVq�WqWWdQRXdS)
zParse /proc/net/unix files.)rr�raz)error while parsing %s; malformed line %rNrr��r�)Nr�r�)
rlr	r�rxr�rr�r�rr
)rrr�rr�r��tokensr�rr�Zpairsr�rUrTrrr:r:r;�process_unixls2


zConnections.process_unixc
Cs"||jkr,td|djdd�|jD��f��t�|_|dk	rP|j|�}|sXgSn|j�}t�}x�|j|D]�\}}}|tj	tj
fkr�|jd|j|f||||d�}n|jd|j|f|||d�}xT|D]L\}	}}}
}}}
|r�t
j|	|||
||�}nt
j|	|||
|||
�}|j|�q�WqjWt|�S)Nz+invalid %r kind argument; choose between %sz, cSsg|]}t|��qSr:)�repr)r�rCr:r:r;r��sz(Connections.retrieve.<locals>.<listcomp>z	%s/net/%s)r)r�r�r�rqr�r�r��setr�r�r�rrrZpconnZsconn�addr�)r��kindr�r�r�r�rrr�rUrrrZ	bound_pidZconnr:r:r;�retrieve�s4

zConnections.retrieve)N)N)N)r7r8r9�__doc__r�r�r��staticmethodrrrrr:r:r:r;r��s
7'%r�r�cCs
tj|�S)z$Return system-wide open connections.)�_connectionsr)rr:r:r;�net_connections�sr"cCs�tdt���}|j�}WdQRXi}x�|dd�D]�}|jd�}|dksVtt|���|d|�j�}||dd�j�j�}tt	|�\}}}	}
}}}
}}}}}}}}}|||||	||
|f||<q4W|S)zsReturn network I/O statistics for every network interface
    installed on the system as a dict of raw tuples.
    z
%s/net/devNr5r�rr)
rlrq�	readlines�rfindrurr�rx�mapr�)r��lines�retdictr�Zcolon�namer�Z
bytes_recvZpackets_recvZerrinZdropinZfifoinZframeinZcompressedinZmulticastinZ
bytes_sentZpackets_sentZerroutZdropoutZfifooutZ
collisionsoutZ
carrieroutZ
compressedoutr:r:r;�net_io_counters�s
*r)cCsptjttjttjti}t�j�}i}xF|D]>}t	j
|�}t	j|�}tj|�\}}t
j|||||�||<q*W|S)z)Get NIC stats (isup, duplex, speed, mtu).)r�ZDUPLEX_FULLrZDUPLEX_HALFrZDUPLEX_UNKNOWNrr)�keys�
cext_posixZ
net_if_mtuZnet_if_flagsZnet_if_duplex_speedrZ	snicstats)Z
duplex_map�namesr�r(ZmtuZisupZduplexZspeedr:r:r;�net_if_stats�s




r-cCs`dd�}i}|�}tdt���}|j�}WdQRX�x$|D�]}|j�}t|�}|dkr�|d}t|d�}	tt|dd	��\
}
}}}
}}}}}}n�|d	kr�|d}tt|dd	��\}	}
}}}
}}}}}}nN|d
k�r|d}tt|dd��\}	}}
}d}}}
}}ntd|��||kr<t|�}||9}||9}|	|
|||||
||f	||<q<W|S)
zcReturn disk I/O statistics for every disk installed on the
    system as a dict of raw tuples.
    cSs�g}tdt���}|j�dd�}WdQRXxXt|�D]L}|j�\}}}}|dj�rd|j|�q8|sz|dj|�r8|j|�q8W|S)Nz
%s/partitionsr5rr�r�)rlrqr#�reversedrx�isdigitr�r�)�
partitionsr�r&r�r�r(r:r:r;�get_partitions�sz(disk_io_counters.<locals>.get_partitionsz%s/diskstatsN�rr5r�rrz!not sure how to interpret line %r)	rlrqr#rxr�r�r%r�r�)r1r'r0r�r&r�r�Z
fields_lenr(ZreadsZreads_mergedZrbytesZrtimeZwritesZ
writes_mergedZwbytesZwtimer�rRZssizer:r:r;�disk_io_counters�s6(*
r4Fc
Cs�t�}tdt���V}xN|D]F}|j�}|jd�sB|j|j��q|jd�d}|dkr|jd�qWWdQRXg}tj�}xT|D]L}|\}}	}}
|dkr�d}|s�|dks�||kr�q�t	j
||	||
�}|j|�q�W|S)	z8Return mounted disk partitions as a list of namedtuples.z%s/filesystemsZnodev�	rZzfsNZnoner)rrlrqr�r�rrxr��disk_partitionsrZ	sdiskpartr�)r�Zfstypesr�r�Zfstype�retlistr0r�ZdeviceZ
mountpointZopts�ntupler:r:r;r6?s*


r6c	Cs.tjt�}tjd�}|jtjd��ttdd�|D���}x�|D]�}ytt|d��d}Wn8t	t
fk
r�}ztjd|t
�w@WYdd}~XnXttjjtjj|�d	�d
d�}t|ddd
�}t|ddd
�}t|ddd
d�}|dk	r�t|�d}|dk	�rt|�d}||j||||f�q@W|S)a�Return hardware (CPU and others) temperatures as a dict
    including hardware name, label, current, max and critical
    temperatures.

    Implementation notes:
    - /sys/class/hwmon looks like the most recent interface to
      retrieve this info, and this implementation relies on it
      only (old distros will probably use something else)
    - lm-sensors on Ubuntu 16.04 relies on /sys/class/hwmon
    - /sys/class/thermal/thermal_zone* is another one but it's more
      difficult to parse
    z/sys/class/hwmon/hwmon*/temp*_*z&/sys/class/hwmon/hwmon*/device/temp*_*cSsg|]}|jd�d�qS)r�r)rx)r�rCr:r:r;r�ssz(sensors_temperatures.<locals>.<listcomp>�_inputg@�@zignoring %rNr(F)r�Z_max)r�Z_crit�_labelr)r�r�)�collectionsrr�r��extend�sortedrr�r�r�r�r�r�r�rvrTr��dirnamer�)	r��	basenames�base�currentr��	unit_nameZhighZcritical�labelr:r:r;�sensors_temperatures`s*



rDcCs�tjt�}tjd�}|s"tjd�}ttdd�|D���}x�|D]�}ytt|d��}Wn8tt	fk
r�}zt
jd|t�w>WYdd}~XnXtt
jjt
jj|�d�d	d
�}t|ddd	d
�}||jtj||��q>Wt|�S)a�Return hardware fans info (for CPU and other peripherals) as a
    dict including hardware label and current speed.

    Implementation notes:
    - /sys/class/hwmon looks like the most recent interface to
      retrieve this info, and this implementation relies on it
      only (old distros will probably use something else)
    - lm-sensors on Ubuntu 16.04 relies on /sys/class/hwmon
    z/sys/class/hwmon/hwmon*/fan*_*z%/sys/class/hwmon/hwmon*/device/fan*_*cSsg|]}|jd�d�qS)r�r)rx)r�rCr:r:r;r��sz sensors_fans.<locals>.<listcomp>r9zignoring %rNr(F)r�r:r)r�r�)r;rr�r�r=rr�r�r�r�r�r�r�rvrTr�r>r�rZsfan�dict)r�r?r@rAr�rBrCr:r:r;�sensors_fans�s 




rFc
s�t���fdd�}tjjtd�}tjj|�s0dS||d|d�}||d|d�}||d	|d
�}|dksv|dkrzdS|dk	r�yd||}Wq�tk
r�d}Yq�Xn tt|d
dd��}|dkr�dSd}|tjjtd�tjjtd��}|dk	�r|dk}n4t|dddd�j	�}|dk�r*d}n|dk�r8d}|�rFt
j}	n2yt||d�}	Wntk
�rvt
j}	YnXt
j
||	|�S)aReturn battery information.
    Implementation note: it appears /sys/class/power_supply/BAT0/
    directory structure may vary and provide files with the same
    meaning but under different names, see:
    https://github.com/giampaolo/psutil/issues/966
    cs:x4|D],}t|�d�}|�kr|j�r.t|�S|SqWdS)zvAttempt to read the content of multiple files which may
        not exist. If none of them exist return None.
        )r�N)r�r/r�)�pathsrTr�)�nullr:r;�	multi_cat�s

z"sensors_battery.<locals>.multi_catZBAT0Nz/energy_nowz/charge_nowz
/power_nowz/current_nowz/energy_fullz/charge_fullgY@gz	/capacityr)r�z
AC0/onlinez	AC/onlinez/statusrF)r�r�Zdischarging�charging�fullTir�r�)rJrK)�objectrvrTr��POWER_SUPPLY_PATHr�ZeroDivisionErrorr�r�r�rZPOWER_TIME_UNLIMITEDZPOWER_TIME_UNKNOWNZsbattery)
rI�rootZ
energy_nowZ	power_nowZenergy_fullrIZ
power_pluggedZonlinerZsecsleftr:)rHr;�sensors_battery�sT







rPc
Cs`g}tj�}xN|D]F}|\}}}}}}|s,q|dkr8d}tj||pDd|||�}	|j|	�qW|S)z:Return currently connected users as a list of namedtuples.�:0.0�:0Z	localhostN)rQrR)r��usersrZsuserr�)
r7Zrawlist�itemr�ZttyZhostnameZtstampZuser_processr��ntr:r:r;rSs
rScCsbdt�}t|��F}x2|D]*}|jd�rt|j�j�d�}|a|SqWtd|��WdQRXdS)zAReturn the system boot time expressed in seconds since the epoch.z%s/statsbtimerzline 'btime' not found in %sN)rqrgr�r�r�rx�	BOOT_TIMEr)rTr�r�r�r:r:r;�	boot_times



rWcCsdd�tjtt���D�S)z7Returns a list of PIDs currently running on the system.cSsg|]}|j�rt|��qSr:)r/r�)r�rCr:r:r;r�,szpids.<locals>.<listcomp>)rvr�rrqr:r:r:r;r�*sr�cCs�tj|�sdSybdt�|f}t|��B}x.|D]&}|jd�r.t|j�d�}||kSq.Wtd|��WdQRXWnttfk
r�|t	�kSXdS)zcCheck for the existence of a unix PID. Linux TIDs are not
    supported (always return False).
    Fz%s/%s/statussTgid:rz'Tgid' line not found in %sN)
r�
pid_existsrqrgr�r�rxr��EnvironmentErrorr�)r�rTr�r�Ztgidr:r:r;rX/s




rXc	Cs�i}t�}x�t�D]�}y(td||f��}|j�}WdQRXWn>tk
r|}z"|jtjtjtjtj	fkrl�WYdd}~XqX|j
d�}||dd�j�}t|d�}|||<qW|S)zsObtain a {pid: ppid, ...} dict for all running processes in
    one shot. Used to speed up Process.children().
    z
%s/%s/statN�)r5r)
rqr�rgr�rYr�r�r�r�r�r$rxr�)	r�r�r�r��datar��rparZdset�ppidr:r:r;�ppid_mapOs
r^cstj���fdd��}|S)zlDecorator which translates bare OSError and IOError exceptions
    into NoSuchProcess and AccessDenied.
    cs�y�|f|�|�Stk
r�}zv|jtjtjfkrBt|j|j��|jtjkr\t|j|j��|jtj	kr�t
jjd|j
|jf�r�t|j|j���WYdd}~XnXdS)Nz%s/%s)rYr�r�r�rr��_namer�rr�rvrTrr�)r��argsrfr�)�funr:r;�wrapperksz wrap_exceptions.<locals>.wrapper)�	functools�wraps)rarbr:)rar;�wrap_exceptionsgsrec@sneZdZdZddddgZdd�Zedd	��Zed
d��Zedd
��Z	dd�Z
dd�Zedd��Z
dd�Zedd��Zedd��Zedd��Zejjdej��r�edd��Zndd�Zed d!��Zed"d#��Zed`d%d&��Zed'd(��Zed)d*��Ze�r"eejd+�ejd,�ejd-�fd.d/��ZneZe�r:ed0d1��Z nd2d1�Z ed3d4��Z!eejd5�fd6d7��Z"eejd8�fd9d:��Z#ed;d<��Z$ed=d>��Z%ed?d@��Z&edAdB��Z'ejdC�fdDdE�Z(edFdG��Z)e*e+dH��r�edIdJ��Z,edKdL��Z-e.�redadMdN��Z/edOdP��Z0edQdR��Z1edbdTdU��Z2edVdW��Z3edXdY��Z4eejdZ�fd[d\��Z5eejd]�fd^d_��Z6d$S)c�ProcesszLinux process implementation.r�r_�_ppidr�cCs||_d|_d|_t�|_dS)N)r�r_rgrqr�)r�r�r:r:r;r��szProcess.__init__c
Csftd|j|jf��}|j�}WdQRX|jd�}||jd�d|�}||dd�j�}|g|S)a{Parse /proc/{pid}/stat file. Return a list of fields where
        process name is in position 0.
        Using "man proc" as a reference: where "man proc" refers to
        position N, always substract 2 (e.g starttime pos 22 in
        'man proc' == pos 20 in the list returned here).
        The return value is cached in case oneshot() ctx manager is
        in use.
        z
%s/%s/statNrZ�(rr5)rgr�r�r�r$�findrx)r�r�r[r\r(Zothersr:r:r;�_parse_stat_file�s

zProcess._parse_stat_filec	Cs(td|j|jf��
}|j�SQRXdS)z�Read /proc/{pid}/stat file and return its content.
        The return value is cached in case oneshot() ctx manager is
        in use.
        z%s/%s/statusN)rgr�r�r�)r�r�r:r:r;�_read_status_file�szProcess._read_status_filec	Cs0td|j|jftd��}|j�j�SQRXdS)Nz%s/%s/smaps)r)rgr�r�r	r�r�)r�r�r:r:r;�_read_smaps_file�s
zProcess._read_smaps_filecCs"|jj�|jj�|jj�dS)N)rjZcache_activaterkrl)r�r:r:r;�
oneshot_enter�s

zProcess.oneshot_entercCs"|jj�|jj�|jj�dS)N)rjZcache_deactivaterkrl)r�r:r:r;�oneshot_exit�s

zProcess.oneshot_exitcCs|j�d}trt|�}|S)Nr)rjrrm)r�r(r:r:r;r(�szProcess.namecCs�ytd|j|jf�Stk
r�}z�|jtjtjfkr�tjj	d|j|jf�rTdSt
|j�snt|j|j��nt
|j|j|j��|jtjtjfkr�t|j|j���WYdd}~XnXdS)Nz	%s/%s/exez%s/%sr)rwr�r�r�r�r�r�rvrT�lexistsrXrr_rrgr�r�r)r�r�r:r:r;�exe�s
zProcess.exec
Csltd|j|jf��}|j�}WdQRX|s0gS|jd�r>dnd}|j|�rX|dd�}dd�|j|�D�S)Nz
%s/%s/cmdlinerrrarcSsg|]}|�qSr:r:)r�rCr:r:r;r��sz#Process.cmdline.<locals>.<listcomp>r�)rlr�r�r�ryrx)r�r�r[�sepr:r:r;�cmdline�s
zProcess.cmdlinec	Cs0td|j|jf��}|j�}WdQRXt|�S)Nz
%s/%s/environ)rlr�r�r�r)r�r�r[r:r:r;�environ�szProcess.environcCs:t|j�d�}tj�}y||Stk
r4dSXdS)N�)r�rjrZget_terminal_mapr�)r�Ztty_nrr�r:r:r;�terminal�szProcess.terminalz/proc/%s/iocCs�d|j|jf}i}t|��:}x2|D]*}|j�}|r$|jd�\}}t|�||<q$WWdQRX|sltd|��t|d|d|d|d|d|d	�S)
Nz%s/%s/ios: z%s file was emptyssyscrssyscws
read_bytesswrite_bytessrcharswchar)r�r�rgr�rxr�rrb)r�rer�r�r�r(r�r:r:r;�io_counters�s"

zProcess.io_counterscCstd|j��dS)Nz+couldn't find /proc/%s/io (kernel too old?))r�r�)r�r:r:r;rvscCsX|j�}t|d�t}t|d�t}t|d�t}t|d�t}tj||||�S)N��
r3r2)rjr�r�rZ	pcputimes)r�r��utime�stimeZchildren_utimeZchildren_stimer:r:r;r�szProcess.cpu_timescCst|j�d�S)zWhat CPU the process is on.�%)r�rj)r�r:r:r;�cpu_numszProcess.cpu_numNcCstj|j||j�S)N)rZwait_pidr�r_)r�Ztimeoutr:r:r;�waitszProcess.waitcCs&|j�}tpt�}t|d�t|S)N�)rjrVrWr�r�)r�r�Zbtr:r:r;�create_time"s
zProcess.create_timec	Cs`td|j|jf��2}dd�|j�j�dd�D�\}}}}}}}WdQRXt|||||||�S)Nz%s/%s/statmcSsg|]}t|�t�qSr:)r�r�)r�rCr:r:r;r�<sz'Process.memory_info.<locals>.<listcomp>r)rgr�r�r�rxrX)	r�r�Zvmsr^rP�text�libr[Zdirtyr:r:r;�memory_info-s
6zProcess.memory_infosPrivate.*:\s+(\d+)sPss.*:\s+(\d+)sSwap.*:\s+(\d+)c	Csj|j�}|j�}ttt|j|���d}ttt|j|���d}ttt|j|���d}t||||f�S)Ni)r�rlr�r%r��findallrY)	r�Z_private_reZ_pss_reZ_swap_reZ	basic_memZ
smaps_datarZr[r\r:r:r;�memory_full_infoCs
zProcess.memory_full_infocCsXdd�}|j�}|sgS|jd�}g}|jd�}|g}�x|||�D�]
\}}|jdd�}y|\}	}
}}}
}Wn*tk
r�|dg\}	}
}}}
}YnX|s�d}n4tr�t|�}|j�}|jd	�r�t|�r�|dd�}|j	t|	�t|
�||d|j
dd�|j
d
d�|j
dd�|j
dd�|j
dd�|j
dd�|j
dd�|j
dd�|j
dd�f
�qDW|S)z�Return process's mapped memory regions as a list of named
            tuples. Fields are explained in 'man proc'; here is an updated
            (Apr 2012) version: http://goo.gl/fmebo
            css�i}x�|D]�}|jdd�}|djd�sB|j�|fV|j|�q
yt|d�d||d<Wq
tk
r�|djd�r�w
ntd|��Yq
Xq
W|j�|fVdS)Nrtr�:risVmFlags:z#don't know how to interpret line %r)rxry�popr�r�r�r�)r&�
current_blockr[r�r�r:r:r;�
get_blocksis
z'Process.memory_maps.<locals>.get_blocks�
rNrtrz[anon]z
 (deleted)rssRss:sSize:sPss:s
Shared_Clean:s
Shared_Dirty:sPrivate_Clean:sPrivate_Dirty:sReferenced:s
Anonymous:sSwap:i����)rlrxr�r�rrmr�ryrr�r�)r�r�r[r&r�Z
first_liner��headerZhfieldsrZperms�offsetZdevr�rTr:r:r;�memory_mapscsF











zProcess.memory_mapscCstd|j��dS)Nzn/proc/%s/smaps does not exist on kernels < 2.6.14 or if CONFIG_MMU kernel configuration option is not enabled.)r�r�)r�r:r:r;r��scCs|ytd|j|jf�Stk
rv}zF|jtjtjfkrdt|j�sRt|j|j	��nt
|j|j	|j���WYdd}~XnXdS)Nz	%s/%s/cwd)rwr�r�r�r�r�r�rXrr_rrg)r�r�r:r:r;�cwd�s
zProcess.cwdsctxt_switches:\t(\d+)cCsL|j�}|j|�}|s,td|j|jf��ntjt|d�t|d��SdS)Nz�'voluntary_ctxt_switches' and 'nonvoluntary_ctxt_switches'lines were not found in %s/%s/status; the kernel is probably older than 2.6.23rr)rkr�r�r�r�rZpctxswr�)r�Z	_ctxsw_rer[Zctxswr:r:r;�num_ctx_switches�s
zProcess.num_ctx_switchessThreads:\t(\d+)cCs|j�}t|j|�d�S)Nr)rkr�r�)r�Z_num_threads_rer[r:r:r;�num_threads�szProcess.num_threadsc
Cstjd|j|jf�}|j�g}d}x�|D]�}d|j|j|f}y$t|��}|j�j�}WdQRXWn6tk
r�}z|j	t	j
kr�d}w,�WYdd}~XnX||jd�dd�}|jd�}	t
|	d�t}
t
|	d	�t}tjt|�|
|�}|j|�q,W|�rtjd
|j|jf�|S)Nz
%s/%s/taskFz%s/%s/task/%s/statTrZr5r��rwz%s/%s)rvr�r�r�r�rgr�r�r�r�r�rirxr�r�rZpthreadr�r��stat)
r�Z
thread_idsr7�
hit_enoentZ	thread_idrer��str�r�ryrzr8r:r:r;�threads�s0


zProcess.threadscCstj|j�S)N)r+�getpriorityr�)r�r:r:r;�nice_get�szProcess.nice_getcCstj|j|�S)N)r+�setpriorityr�)r�r�r:r:r;�nice_set�szProcess.nice_setcCstj|j�S)N)r�Zproc_cpu_affinity_getr�)r�r:r:r;�cpu_affinity_get�szProcess.cpu_affinity_getsCpus_allowed_list:\t(\d+)-(\d+)cCsV|j�}|j|�}|r@ttt|dd�t|dd�d��Stttt����SdS)Nrr)rkr�r��ranger�r�r�)r�Z_rer[r�r:r:r;�_get_eligible_cpus�s

*zProcess._get_eligible_cpuscCs�ytj|j|�Wn�ttfk
r�}zvt|t�s>|jtjkr�|j�}t	t
tt����}x<|D]4}||krztd||f��||kr^td||f��q^W�WYdd}~XnXdS)Nz(invalid CPU number %r; choose between %sz0CPU number %r is not eligible; choose between %s)
r�Zproc_cpu_affinity_setr�r�r�rtr�r�r��tupler�r�r�)r�r�r�Z
eligible_cpusZall_cpusZcpur:r:r;�cpu_affinity_sets 
zProcess.cpu_affinity_set�proc_ioprio_getcCs,tj|j�\}}tdk	r t|�}tj||�S)N)r�r�r��enumr6rZpionice)r��ioclassr�r:r:r;�
ionice_getszProcess.ionice_getcCs�|dk	rNtr.t|ttf�r.d|}t|��d|ko@dknsNtd��|tdfkrx|rnd|}t|��t}d}nH|tkr�|r�d|}t|��d}n&|tt	fkr�|dkr�d}ntd|��t
j|j||�S)	Nz)value argument is not an integer (gor %r)rrz0value argument range expected is between 0 and 7z3can't specify value with IOPRIO_CLASS_NONE (got %r)z3can't specify value with IOPRIO_CLASS_IDLE (got %r)rzinvalid ioclass argument %r)
rrtr�r�	TypeErrorr�r r#r!r"r�Zproc_ioprio_setr�)r�r�r�r�r:r:r;�
ionice_set%s2zProcess.ionice_setcCs�|jdkrtd��yP|dkr*tj|j|�St|�dkrFtdt|���|\}}tj|j|||�WnNtk
r�}z2|jtjkr�t	|j�r�t
|j|j|j��n�WYdd}~XnXdS)Nrz)can't use prlimit() against PID 0 processr5z4second argument must be a (soft, hard) tuple, got %s)
r�r�r�r/r�rr�r�ZENOSYSrXrr_rg)r�ZresourceZlimitsZsoftZhardr�r:r:r;�rlimitGs
zProcess.rlimitcCs$|j�d}tr|j�}tj|d�S)Nr�?)rjrrm�
PROC_STATUSESr�)r�Zletterr:r:r;rbszProcess.statusc
Cs�g}tjd|j|jf�}d}�xH|D�]>}d|j|j|f}yt|�}WnNtk
r�}z2|jtjtjfkrvd}w&n|jtj	kr�w&n�WYdd}~Xq&X|j
d�o�t|�r&d|j|j|f}yBt|��0}t
|j�j�d�}	t
|j�j�dd�}
WdQRXWn:tk
�r:}z|jtjk�r(d}n�WYdd}~Xq&Xt|
�}t|t
|�t
|	�||
�}|j|�q&W|�r�tjd	|j|jf�|S)
Nz%s/%s/fdFz%s/%s/fd/%sT�/z%s/%s/fdinfo/%srr�z%s/%s)rvr�r�r�rwr�r�r�r�r�r�rrgr�r�rxr�r�rSr�r�)
r�r7�filesr�rUrrTr�r��posrWrVr8r:r:r;�
open_filesjs@
$zProcess.open_filesr�cCs(tj||j�}tjd|j|jf�|S)Nz%s/%s)r!rr�rvr�r�)r�rr�r:r:r;�connections�szProcess.connectionscCsttjd|j|jf��S)Nz%s/%s/fd)r�rvr�r�r�)r�r:r:r;�num_fds�szProcess.num_fdscCst|j�d�S)Nr5)r�rj)r�r:r:r;r]�szProcess.ppidsUid:\t(\d+)\t(\d+)\t(\d+)cCs6|j�}|j|�d\}}}tjt|�t|�t|��S)Nr)rkr�rZpuidsr�)r�Z_uids_rer[�real�	effective�savedr:r:r;�uids�szProcess.uidssGid:\t(\d+)\t(\d+)\t(\d+)cCs6|j�}|j|�d\}}}tjt|�t|�t|��S)Nr)rkr�rZpgidsr�)r�Z_gids_rer[r�r�r�r:r:r;�gids�szProcess.gids)N)N)r�)7r7r8r9r�	__slots__r�rrjrkrlrmrnrer(rprrrsrurvrTr�getpidrvr�r|r}rr��	HAS_SMAPSr�r�r�r�r�r�r�r�r�r�r�r�r��hasattrr�r�r��HAS_PRLIMITr�rr�r�r�r]r�r�r:r:r:r;rf�st			B
		!2rf)rrr�)rZr[r\)r�)F)�rZ
__future__rr�r;r�rcr�rvr�r�rro�	tracebackr�rrrrrrr�r	r+r
rrr
rrrrrrrrZ_compatrrrr�_exceptionsrrr�version_infor�Z__extra__all__rMrTrr�r�r�r�rLr��dirr(r�r�r�r�r�rVr	�	byteorderr�r�Z	AF_PACKETr4�IntEnumr�r3r r!r"r#r6�globalsr��__members__ZSTATUS_RUNNINGZSTATUS_SLEEPINGZSTATUS_DISK_SLEEPZSTATUS_STOPPEDZSTATUS_TRACING_STOPZ
STATUS_ZOMBIEZSTATUS_DEADZSTATUS_WAKE_KILLZ
STATUS_WAKINGr�r$r%r&r'r(r)r*r+r,r-r.rrFrQrSrXr�rYr]r�r`rbrgrlrmrqrwr�r�r�r��	Exception�	print_excr�r�r�r�r�r�r�r�r�r�Znet_if_addrsr�r�r!r"r)r-Z
disk_usager4r6rDrFrPrSrWr�rXr^rerfr:r:r:r;�<module>sH















5u:*q
%H
!1 T 

?>