o
    aM                    @   s  d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlZddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddlm!Z! ddlm"Z" ddlm#Z# ddlm$Z$ ddlm%Z% ddlm&Z& ddlm'Z' ddlm(Z( ddlm)Z) ddl*m+Z+ ddl*m,Z, ddl*m-Z- dd l*m.Z. dd!l*m/Z/ dd"l*m0Z0 ej1d#krddl2Z2ndZ2g d$Z3d%Z4ej56d&e7  Z8e9ed'Z:e9ed(Z;e< Z=e>d)Z?e@ ZAdaBe+r%d*nd+ZCejDd,kZEd-ZFe2du r7e
jGZHne2Id.d/eJe
jGiZKeKjHZHe2du rSdZLdZMd0ZNd1ZOnG d2d3 d3e2jIZPeQ RePjS ejTejUejVejWejXejYejZejZej[ej\ej]ej^d4Z_ej`ejaejbejcejdejeejfejgejhejiejjd5Zked6g d7Zled8g d9Zmed:g d;Zned<d=Zoed>eojpd? Zqed@g dAZredBdCdDserjp ZtedEg dFZuedGg dHZvdIdJ ZwdKdL ZxdMdN Zye"dOdP Zze=dQfdRdSZ{zezdT W n e|y   e}  edUdVdWdWdWa~Y nw dZzddXlmZ W n  ey:   ddlZejddQdYZe9edZr8dd[dZZY nw edurLe3d\d] eeD  d^d_ Zd`da Zdbdc Zddde Zdfdg Zdhdi Zdjdk Zdldm Zdndo Zej56dps~ej56dqrdrds Zndtds ZejZG dudv dve|ZG dwdx dxZe Zddzd{Zd|d} Zd~d ZejZdddZG dd dZdddZdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZG dd de<ZdS )zLinux platform implementation.    )divisionN)defaultdict)
namedtuple   )_common)_psposix)_psutil_linux)_psutil_posix)NIC_DUPLEX_FULL)NIC_DUPLEX_HALF)NIC_DUPLEX_UNKNOWN)AccessDenied)NoSuchProcess)ZombieProcess)debug)decode)get_procfs_path)isfile_strict)memoize)memoize_when_activated)open_binary)	open_text)parse_environ_block)path_exists_strict)supports_ipv6)usage_percent)PY3)FileNotFoundError)PermissionError)ProcessLookupError)b)
basestring)      )Z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proc_ioprio_getproc_cpu_affinity_get
SC_CLK_TCKi    littlei   AddressFamilyAF_LINK   r"   c                   @   s   e Zd ZdZdZdZdZdS )
IOPriorityr   r   r:   r"   N)__name__
__module____qualname__r$   r%   r&   r'    r?   r?   1/usr/lib/python3/dist-packages/psutil/_pslinux.pyr;   x   s
    r;   )RSDTtZXxKWIP)Z01Z02Z03Z04Z05Z06Z07Z08Z09Z0AZ0Bsvmem)total	availablepercentusedfreeactiveinactivebufferscachedsharedslabsdiskio)	
read_countwrite_count
read_byteswrite_bytesZ	read_timeZ
write_timeZread_merged_countZwrite_merged_count	busy_time	popenfile)pathfdZpositionmodeflagspmemz"rss vms shared text lib data dirtypfullmem)usspssswappmmap_grouped)r`   rsssizerg   Zshared_cleanZshared_dirtyZprivate_cleanZprivate_dirtyZ
referencedZ	anonymousrh   	pmmap_extzaddr perms  pio)rZ   r[   r\   r]   Z
read_charsZwrite_chars	pcputimes)usersystemchildren_userchildren_systemiowaitc                 C   sL   t | ts	J | t| } | dd } | dr$t| s$| dd } | S )zWrapper around os.readlink(). r   
 (deleted)N)
isinstancer!   osreadlinksplitendswithr   )r`   r?   r?   r@   rz      s   
rz   c                 C   sX   t jdt jdt jdi}|| t jt jB t jB @  }| t j@ r$|ddd}|dd}|S )zZConvert file's open() flags into a readable string.
    Used by Process.open_files().
    rwzw+ar   zr+)ry   O_RDONLYO_WRONLYO_RDWRO_APPENDreplace)rc   Z	modes_maprb   r?   r?   r@   file_flags_to_mode   s   
r   c                 C   s4   |  dd} d}|rd|  }nd|  }t|tjS )zReturn True if the given name refers to a root device (e.g.
    "sda", "nvme0n1") as opposed to a logical partition (e.g.  "sda1",
    "nvme0n1p1"). If name is a virtual device (e.g. "loop1", "ram")
    return True.
    /!Tz/sys/block/%sz/sys/block/%s/device)r   ry   accessF_OK)nameZincluding_virtualr`   r?   r?   r@   is_storage_device   s   

r   c                 C   s   t d|  }|  dd }W d   n1 sw   Y  g d}t|}|dkr1|d |dkr:|d |d	krC|d
 td|adS )zSet 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.
    %s/statr   N)rp   nicerq   Zidlert   ZirqZsoftirq   Zsteal	   Zguest
   Z
guest_nice	scputimes)r   readliner{   lenappendr   r   )procfs_pathfvaluesfieldsZvlenr?   r?   r@   set_scputimes_ntuple  s   	


r   Tc              	   C   sn   z$|rt | nt| }|  W  d   W S 1 sw   Y  W dS  ttfy6   |tur5| Y S  w )zReturn 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)r   r   readstripIOErrorOSError_DEFAULT)fnamefallbackbinaryr   r?   r?   r@   cat  s   
(r   /procr   zuser system idle        )prlimit)Z	use_errnor   c                 C   s   G dd dt j}| }|d u rt| |d t |}n| }|d |_|d |_t| |t |t |}|dkrGt  }t|t	
||j|jfS )Nc                   @   s    e Zd ZdejfdejfgZdS )zprlimit.<locals>.StructRlimitrlim_currlim_maxN)r<   r=   r>   ctypesZ
c_longlongZ_fields_r?   r?   r?   r@   StructRlimitK  s    r   r   r   )r   Z	Structurelibcr   Zbyrefr   r   Z	get_errnor   ry   strerror)pid	resource_limitsr   currentretnewerrnor?   r?   r@   r   J  s   

c                 C   s"   g | ]}| d r| r|qS )ZRLIM)
startswithisupper.0rH   r?   r?   r@   
<listcomp>c  s   " r   c                 C   s  | d }||  dd }z| d }| d }| d }W n ty%   | Y S w z	tdt  }W n ty:   | Y S w d}|  |D ]}| }|drW|t| d	 7 }qBW d
   n1 sbw   Y  |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.
       MemFree:   Cached:r   s   Active(file):s   Inactive(file):   SReclaimable:z%s/zoneinfos   lowr   Nr:   g       @)getKeyErrorr   r   r   r   r   intr{   PAGESIZEmin)memsrR   r   Zlru_active_fileZlru_inactive_fileZslab_reclaimabler   Zwatermark_lowlineavailZ	pagecacher?   r?   r@   calculate_avail_vmemk  s<   
r   c                  C   s  g } i }t dt  }|D ]}| }t|d d ||d < qW d   n1 s+w   Y  |d }|d }z|d }W n tyN   d}| d	 Y nw z|d
 }W n tye   d}| d Y n	w ||dd7 }z|d }	W n  ty   z|d }	W n ty   d}	| d Y nw Y nw z|d }
W n ty   d}
| d Y nw z|d }W n( ty   z|d |d  |d  }W n ty   d}| d Y nw Y nw z|d }W n ty   d}Y nw || | | }|dk r|| }z|d }W n ty   t|}Y nw |dk rd}| d ||kr%|}t|| |dd}| rIdd	| t
| dkr?dndf }t|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".
    
%s/meminfor      r   Ns	   MemTotal:r   s   Buffers:rU   r   rV   r   s   Shmem:s
   MemShared:rW   s   Active:rS   s	   Inactive:s   Inact_dirty:s   Inact_clean:s   Inact_laundry:rT   s   Slab:s   MemAvailable:rO   Zround_z6%s memory stats couldn't be determined and %s set to 0, ZwasZwere)r   r   r{   r   r   r   r   r   r   joinr   warningswarnRuntimeWarningrM   )Zmissing_fieldsr   r   r   r   rN   rR   rU   rV   rW   rS   rT   rX   rQ   r   rP   msgr?   r?   r@   virtual_memory  s   	
	


r   c               
   C   s  i } t dt  }|D ]}| }t|d d | |d < qW d   n1 s)w   Y  z
| d }| d }W n tyT   t \}}}}}}}||9 }||9 }Y nw || }t||dd}	z	t d	t  }W n" ty }
 zd
t	|
 }t
|t d }}W Y d}
~
n[d}
~
ww |L d }}|D ]1}|drt|dd d d }n|drt|dd d d }|dur|dur nqd}t
|t d }}W d   n1 sw   Y  t||||	||S )zReturn swap memory metrics.r   r   r   r   Ns
   SwapTotal:s	   SwapFree:r   z	%s/vmstatzP'sin' and 'sout' swap memory stats couldn't be determined and were set to 0 (%s)s   pswpin    r#   s   pswpoutzK'sin' and 'sout' swap memory stats couldn't be determined and were set to 0)r   r   r{   r   r   cextZlinux_sysinfor   r   strr   r   r   r   r   Zsswap)r   r   r   r   rN   rR   _Zunit_multiplierrQ   rP   errr   ZsinZsoutr?   r?   r@   swap_memory  sV   

r   c                  C   sr   t  } t|  td|  }|  }W d   n1 sw   Y  |dttjd  }dd |D }t| S )zReturn 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.
    r   Nr   c                 S      g | ]}t |t qS r?   floatCLOCK_TICKSr   r?   r?   r@   r   _      zcpu_times.<locals>.<listcomp>)r   r   r   r   r{   r   r   _fields)r   r   r   r   r?   r?   r@   	cpu_timesS  s   r   c                  C   s   t  } t|  g }td|  7}|  |D ]&}|dr<| }|dttjd  }dd |D }t| }|	| q|W  d   S 1 sIw   Y  dS )zfReturn a list of namedtuple representing the CPU times
    for every CPU available on the system.
    r   s   cpur   c                 S   r   r?   r   r   r?   r?   r@   r   q  r   z!per_cpu_times.<locals>.<listcomp>N)
r   r   r   r   r   r{   r   r   r   r   )r   cpusr   r   r   r   entryr?   r?   r@   per_cpu_timesc  s   

$r   c                  C   s   zt dW S  tyv   d} tdt  }|D ]}| dr&| d7 } qW d   n1 s1w   Y  | dkrktd}t	dt  }|D ]}|
d	d }||r[| d7 } qIW d   n1 sfw   Y  | dkrrY dS |  Y S w )
z0Return the number of logical CPUs in the system.SC_NPROCESSORS_ONLNr   
%s/cpuinfos	   processorr   Nzcpu\dr   rm   )ry   sysconf
ValueErrorr   r   lowerr   recompiler   r{   match)Znumr   r   searchr?   r?   r@   cpu_count_logicalw  s2   

r   c               
   C   s.  t  } d}d}t|pt|D ]}t|}| |   W d   n1 s+w   Y  qt| }|dkr;|S i }i }tdt  ?}|D ]4}|  }|sjz
|d ||d < W n	 t	yf   Y nw i }qI|
dr}|d	d
\}	}
t|
||	< qIW d   n1 sw   Y  t| }|pdS )z-Return the number of CPU cores in the system.z9/sys/devices/system/cpu/cpu[0-9]*/topology/core_cpus_listz?/sys/devices/system/cpu/cpu[0-9]*/topology/thread_siblings_listNr   r   	   cpu cores   physical id)r   r   s   	:r   )setglobr   addr   r   r   r   r   r   r   r{   r   sumr   )lsZp1Zp2r`   r   resultmappingZcurrent_infor   keyvaluer?   r?   r@   cpu_count_cores  s@   

r   c                  C   s   t dt  J} d}d}d}| D ]9}|dr t| d }n|dr.t| d }n|dr;t| d }|durI|durI|durI nqW d   n1 sTw   Y  d}t||||S )z*Return various CPU stats as a named tuple.r   Ns   ctxtr   s   intrs   softirqr   )r   r   r   r   r{   r   Z	scpustats)r   Zctx_switchesZ
interruptsZsoft_interruptsr   Zsyscallsr?   r?   r@   	cpu_stats  s*   


r   c               	   C   sj   g } t dt  "}|D ]}| dr"| t|ddd  qW d   | S 1 s.w   Y  | S )z<Return current CPU frequency from cpuinfo if available.
    r   s   cpu mhz   :r   N)r   r   r   r   r   r   r{   )r   r   r   r?   r?   r@   _cpu_get_cpuinfo_freq  s   
r   z'/sys/devices/system/cpu/cpufreq/policy0z$/sys/devices/system/cpu/cpu0/cpufreqc            	      C   s   t  } tdptd}|jdd d g }tjj}t|D ]U\}}t|t| kr0| | }n	t||ddd}|du rNt||d	dd}|du rNt	d
t
|d }t
t||dd }t
t||dd }|t||| q|S )zReturn frequency metrics for all CPUs.
        Contrarily to other OSes, Linux updates these values in
        real-time.
        z,/sys/devices/system/cpu/cpufreq/policy[0-9]*z)/sys/devices/system/cpu/cpu[0-9]*/cpufreqc                 S   s   t td|  S )Nz[0-9]+)r   r   r   group)rH   r?   r?   r@   <lambda>      zcpu_freq.<locals>.<lambda>)r   Zscaling_cur_freqNr   Zcpuinfo_cur_freqz!can't find current frequency filei  Zscaling_max_freqZscaling_min_freq)r   r   sortry   r`   r   	enumerater   r   NotImplementedErrorr   r   r   scpufreq)	Zcpuinfo_freqspathsr   Zpjoinir`   ZcurrZmax_Zmin_r?   r?   r@   cpu_freq  s,   

r  c                   C   s   dd t  D S )z}Alternate implementation using /proc/cpuinfo.
        min and max frequencies are not available and are set to None.
        c                 S   s   g | ]	}t |d d qS )r   )r   r   r   r?   r?   r@   r         zcpu_freq.<locals>.<listcomp>)r   r?   r?   r?   r@   r    s   c                   @   s   e Zd ZdS )_Ipv6UnsupportedErrorN)r<   r=   r>   r?   r?   r?   r@   r    s    r  c                   @   sZ   e Zd ZdZdd Zdd Zdd Zedd	 ZedddZ	edddZ
dddZd
S )Connectionsaw  A 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                 C   s   dt jt jf}dt jt jf}dt jt jf}dt jt jf}dt jd f}|||||f||f|f|f||f|f|f|f||||f||f||fd| _d | _d S )Ntcptcp6udpudp6unix)allr  tcp4r  r  udp4r  r	  inetZinet4Zinet6)socketAF_INETSOCK_STREAMAF_INET6Z
SOCK_DGRAMZAF_UNIXtmap_procfs_path)selfr  r  r  r  r	  r?   r?   r@   __init__   s$   

zConnections.__init__c                 C   s   t t}td| j|f D ]\}ztd| j||f }W n3 ttfy'   Y q tyO } z|j	t	j
kr:W Y d }~q|j	t	jkrJt| W Y d }~q d }~ww |drj|dd  d d }|| |t|f q|S )N%s/%s/fd%s/%s/fd/%szsocket:[r   r6   )r   listry   listdirr  rz   r   r   r   r   EINVALENAMETOOLONGr   r   r   r   )r  r   inodesra   inoder   r?   r?   r@   get_proc_inodes7  s(   

zConnections.get_proc_inodesc                 C   s@   i }t  D ]}z
|| | W q tttfy   Y qw |S N)pidsupdater  r   r   r   )r  r  r   r?   r?   r@   get_all_inodesQ  s   
	zConnections.get_all_inodesc              	   C   s   |  d\}}t|d}|sdS tr|d}|tjkr7tr-t|t	|ddd }nFt|t	|}n<t	|}z)trRttj
tjdgtd|R  }nttj
tjdgtd|R  }W n tyr   t sqt w t||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
        :   r?   asciiNr6   z>4Iz<4I)r{   r   r   encoder  r  LITTLE_ENDIANZ	inet_ntopbase64Z	b16decoder  structZpackunpackr   r   r  r   addr)r+  familyZipZportr?   r?   r@   decode_addressa  s8   



zConnections.decode_addressNc                 c   sF   |  drtj| sdS t| td}|  t|dD ]r\}}z| dd \
}}	}
}}}}}}}W n t	yF   t
d| ||f w ||v rT|| d \}}nd\}}|dura||kraq|tjkrkt| }ntj}zt|	|}	t|
|}
W n	 ty   Y qw ||||	|
||fV  qW d   dS 1 sw   Y  dS )	z.Parse /proc/net/tcp* and /proc/net/udp* files.6N	bufferingr   r   z,error while parsing %s; malformed line %s %rr   Nr6   )r|   ry   r`   existsr   BIGFILE_BUFFERINGr   r   r{   r   RuntimeErrorr  r  TCP_STATUSESr   	CONN_NONEr  r-  r  )filer,  type_r  
filter_pidr   linenor   r   laddrraddrstatusr  r   ra   r?   r?   r@   process_inet  sB   

"zConnections.process_inetc              
   c   s   t | tdz}|  |D ]j}| }z|dd \}}}}}}}	W n ty8   d|vr0Y qtd| |f w |	|v rB||	 }
ndg}
|
D ]0\}}|durT||krTqGt|dkr_|d	 }nd
}tt	|}d
}tj
}|||||||fV  qGqW d   dS 1 sw   Y  dS )zParse /proc/net/unix files.r/  r      rm   z)error while parsing %s; malformed line %rr1  Nr   r6    )r   r3  r   r{   r   r4  r   r   Zsocktype_to_enumr   r6  )r7  r,  r  r9  r   r   tokensr   r8  r  Zpairsr   ra   r`   r<  r=  r?   r?   r@   process_unix  s@   

"zConnections.process_unixc                 C   s  || j vrtd|ddd | j D f t | _|d ur(| |}|s'g S n|  }t }| j | D ]Q\}}}d| j|f }|tj	tj
fv rS| j|||||d}	n	| j||||d}	|	D ]&\}
}}}}}}|rtt|
|||||}nt|
||||||}|| q^q4t|S )Nz+invalid %r kind argument; choose between %sr   c                 S   s   g | ]}t |qS r?   )reprr   r?   r?   r@   r     r   z(Connections.retrieve.<locals>.<listcomp>z	%s/net/%s)r9  )r  r   r   r   r  r  r"  r   r  r  r  r>  rB  r   ZpconnZsconnr   r  )r  kindr   r  r   Z
proto_namer,  r8  r`   r   ra   r;  r<  r=  Z	bound_pidZconnr?   r?   r@   retrieve  s@   


zConnections.retriever  )r<   r=   r>   __doc__r  r  r"  staticmethodr-  r>  rB  rE  r?   r?   r?   r@   r    s    
2'%r  r  c                 C   s
   t | S )z$Return system-wide open connections.)_connectionsrE  )rD  r?   r?   r@   net_connections  s   
rI  c                  C   s   t dt  } |  }W d   n1 sw   Y  i }|dd D ]F}|d}|dks4J t||d|  }||d d   }tt|\}}}	}
}}}}}}}}}}}}|||||	||
|f||< q#|S )zsReturn network I/O statistics for every network interface
    installed on the system as a dict of raw tuples.
    z
%s/net/devNr:   r#  r   r   )	r   r   	readlinesrfindrC  r   r{   mapr   )r   linesretdictr   Zcolonr   r   Z
bytes_recvZpackets_recvZerrinZdropinZfifoinZframeinZcompressedinZmulticastinZ
bytes_sentZpackets_sentZerroutZdropoutZfifooutZcollisionsoutZ
carrieroutZcompressedoutr?   r?   r@   net_io_counters
  s<   


rO  c            	      C   s   t jtt jtt jti} t  }i }|D ]?}zt	
|}t	|}t |\}}W n tyF } z|jtjkr8 t| W Y d}~qd}~ww t|| | ||||< q|S )z)Get NIC stats (isup, duplex, speed, mtu).N)r   ZDUPLEX_FULLr
   ZDUPLEX_HALFr   ZDUPLEX_UNKNOWNr   rO  keys
cext_posixZ
net_if_mtuZnet_if_is_runningZnet_if_duplex_speedr   r   ZENODEVr   r   Z	snicstats)	Z
duplex_mapnamesr   r   ZmtuZisupZduplexZspeedr   r?   r?   r@   net_if_stats/  s&   


rS  Fc                 C   s   dd }dd }t jdt  r| }nt jdr| }ntdt  i }|D ]*}|\
}}}}	}
}}}}}| s?t|s?q*|	t9 }	|
t9 }
|||	|
|||||f	||< q*|S )zcReturn disk I/O statistics for every disk installed on the
    system as a dict of raw tuples.
    c                  s   sB   t dt  } |  }W d    n1 sw   Y  |D ]}| }t|}|dkrJ|d }t|d }tt|dd \
}}}	}
}}}}}}nG|dksR|dkrk|d }tt|dd \}}}}	}
}}}}}}n&|dkr|d }tt|dd  \}}}
}d	 }	 } } }}ntd
| |||
|||	||||f
V  qd S )N%s/diskstats   r"   r:   r#         r?  r   z!not sure how to interpret line %r)r   r   rJ  r{   r   r   rL  r   )r   rM  r   r   Zflenr   readsreads_mergedrbytesrtimewriteswrites_mergedwbyteswtimer   r^   r?   r?   r@   read_procfsR  s6   
z%disk_io_counters.<locals>.read_procfsc                  s   s    t dD ]^} t t jd| D ]Q\}}}d|vrqtt j|d}|   }W d    n1 s8w   Y  t j	|}t
t|d d \
}}}	}
}}}}}}||||	||
||||f
V  qqd S )N
/sys/blockstatr   )ry   r  walkr`   r   r   r   r   r{   basenamerL  r   )blockrootr   filesr   r   r   rX  rY  rZ  r[  r\  r]  r^  r_  r^   r?   r?   r@   
read_sysfs}  s"   z$disk_io_counters.<locals>.read_sysfsrT  ra  zC%s/diskstats nor /sys/block filesystem are available on this system)ry   r`   r2  r   r   r   DISK_SECTOR_SIZE)Zperdiskr`  rh  genrN  r   r   rX  r\  rZ  r^  r[  r_  rY  r]  r^   r?   r?   r@   disk_io_countersN  s0   +
rk  c                   @   s@   e Zd ZdZddgZdd Zdd Zdd	 Zd
d Zdd Z	dS )RootFsDeviceFinderaE  disk_partitions() may return partitions with device == "/dev/root"
    or "rootfs". This container class uses different strategies to try to
    obtain the real device path. Resources:
    https://bootlin.com/blog/find-root-device/
    https://www.systutorials.com/how-to-find-the-disk-where-root-is-on-in-bash-on-linux/
    majorminorc                 C   s(   t dj}t || _t || _d S )Nr   )ry   rb  st_devrm  rn  )r  devr?   r?   r@   r       zRootFsDeviceFinder.__init__c                 C   s   t dt  X}| dd  D ]F}| }t|dk rq|d  r)t|d nd }|d  r7t|d nd }|d }|| jkrV|| jkrV|rVd|   W  d    S qW d    d S 1 sbw   Y  d S )Nz%s/partitionsr:   r#   r   r   r"   /dev/%s)	r   r   rJ  r{   r   isdigitr   rm  rn  )r  r   r   r   rm  rn  r   r?   r?   r@   ask_proc_partitions  s   
"z&RootFsDeviceFinder.ask_proc_partitionsc                 C   s   d| j | jf }t|+}|D ]}|dr.| dd }|r.d|   W  d    S qW d    d S 1 s:w   Y  d S )Nz/sys/dev/block/%s:%s/ueventzDEVNAME=r:   rr  )rm  rn  r   r   r   
rpartition)r  r`   r   r   r   r?   r?   r@   ask_sys_dev_block  s   


"z$RootFsDeviceFinder.ask_sys_dev_blockc              	   C   s   d| j | jf }td}|D ]E}zt|}W n	 ty    Y qw |) |  }||krEtj	
tj	|}d| W  d      S W d    n1 sOw   Y  qd S )Nz%s:%sz/sys/class/block/*/devrr  )rm  rn  r   Ziglobr   r   r   r   ry   r`   rd  dirname)r  Zneedlerg  r7  r   datar   r?   r?   r@   ask_sys_class_block  s$   
z&RootFsDeviceFinder.ask_sys_class_blockc              
   C   s   d }|d u r%z|   }W n ttfy$ } z
t| W Y d }~nd }~ww |d u rHz|  }W n ttfyG } z
t| W Y d }~nd }~ww |d u rkz|  }W n ttfyj } z
t| W Y d }~nd }~ww |d urwtj|ry|S d S d S r  )	rt  r   r   r   rv  ry  ry   r`   r2  )r  r`   r   r?   r?   r@   find  s2   zRootFsDeviceFinder.findN)
r<   r=   r>   rF  	__slots__r  rt  rv  ry  rz  r?   r?   r?   r@   rl    s    	rl  c              	   C   s<  t  }t }td| .}|D ]#}| }|ds"||  q|dd }|dkr2|d qW d   n1 s=w   Y  |dkrStj	drStj
d}ntj
d	| }g }t|}|D ]7}	|	\}
}}}|
d
krrd}
|
dv r}t  p||
}
| s|
dks||vrqdd }}t|
|||||}|| qd|S )z8Return mounted disk partitions as a list of namedtuples.z%s/filesystemsZnodev	r   ZzfsNr   z	/etc/mtabz%s/self/mountsZnoner@  )z	/dev/rootZrootfs)r   r   r   r   r   r   r{   ry   r`   isfilerealpathr   disk_partitionsrl  rz  r   Z	sdiskpartr   )r
  Zfstypesr   r   r   ZfstypeZmounts_pathretlistZ
partitions	partitionZdeviceZ
mountpointZoptsZmaxfileZmaxpathntupler?   r?   r@   r    sB   


r  c                  C   s@  t t} td}|td ttdd |D }td}td}|D ]}|	d|}||vr:|
| q)|D ]}z|d }tt|d	 }tjtj|d
}t|dd}	W n tttfyj   Y q=w t|d dd}
t|d dd}t|d ddd}|
durzt|
d	 }
W n ty   d}
Y nw |durzt|d	 }W n ty   d}Y nw | |	 
|||
|f q=|std}tt|}|D ]}ztj|d}tt|d	 }tj|d}t|dd}	W n tttfy
 } z
t| W Y d}~qd}~ww t|d }tdd |D }d}d}
|D ]n}tj||d }t|ddd}|dkrFttj||d dd}n|dkrXttj||d dd}
|
durrzt|
d	 }
W n tyq   d}
Y nw |durz	t|d	 }W q! ty   d}Y q!w q!| |	 
d||
|f qt| 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*_*c                 S      g | ]	}| d d qS r   r   r{   r   r?   r?   r@   r   7  r  z(sensors_temperatures.<locals>.<listcomp>z5/sys/devices/platform/coretemp.*/hwmon/hwmon*/temp*_*z'/sys/devices/platform/coretemp.*/hwmon/z/sys/class/hwmon/_inputg     @@r   Fr   Z_maxNr   Z_crit_labelr@  r   r   z /sys/class/thermal/thermal_zone*Ztemptypez/trip_point*c                 S   s,   g | ]}d  tj|d dd qS )r   r   r"   )r   ry   r`   rd  r{   )r   pr?   r?   r@   r   w  s
    Z_typecriticalZ_temphigh)collectionsr   r  r   extendsortedr   r   r   subr   r   r   ry   r`   r   rw  r   r   r   r   dict)r   	basenamesZ
basenames2replr   Zaltnamebaser`   r   	unit_namer  r  labelr   Z
trip_pathsZtrip_pointsZ
trip_pointZ	trip_typer?   r?   r@   sensors_temperatures$  s   



	





r  c                  C   s   t t} td}|std}ttdd |D }|D ]H}z
tt|d }W n tt	fyB } z
t
| W Y d}~qd}~ww ttjtj|ddd	}t|d
 ddd}| | t|| qt| 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*_*c                 S   r  r  r  r   r?   r?   r@   r     r  z sensors_fans.<locals>.<listcomp>r  Nr   Fr  r  r@  r  )r  r   r  r   r  r   r   r   r   r   r   ry   r`   r   rw  r   r   Zsfanr  )r   r  r  r   r   r  r  r?   r?   r@   sensors_fans  s&   



r  c                     s  t    fdd} dd ttD }|sdS tjtt|d }| |d |d }| |d	 |d
 }| |d |d }| |d }|dur_|dur_zd| | }W n ty^   d}Y nw tt	|d dd}|dkrodS d}| tjtdtjtd}	|	dur|	dk}nt	|d ddd
 }
|
dkrd}n|
dv rd}|rtj}n4|dur|durz
t|| d }W n! ty   tj}Y nw |durt|d }|dk rtj}ntj}t|||S )a  Return 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
    c               	      sL   | D ]!}t | d}| kr#zt|W   S  ty"   | Y   S w qdS )zvAttempt to read the content of multiple files which may
        not exist. If none of them exist return None.
        r   N)r   r   r   )r   r`   r   Znullr?   r@   	multi_cat  s   z"sensors_battery.<locals>.multi_catc                 S   s&   g | ]}| d sd| v r|qS )ZBATZbattery)r   r   r   r?   r?   r@   r     s    

z#sensors_battery.<locals>.<listcomp>Nr   z/energy_nowz/charge_nowz
/power_nowz/current_nowz/energy_fullz/charge_fullz/time_to_empty_nowg      Y@r   z	/capacityr6   r   z
AC0/onlinez	AC/onliner   z/statusr@  Fr  Zdischarging)ZchargingZfullTi  <   )objectry   r  POWER_SUPPLY_PATHr`   r   r  ZeroDivisionErrorr   r   r   r   ZPOWER_TIME_UNLIMITEDZPOWER_TIME_UNKNOWNZsbattery)r  Zbatsrf  Z
energy_nowZ	power_nowZenergy_fullZtime_to_emptyrP   Zpower_pluggedZonliner=  Zsecsleftr?   r  r@   sensors_battery  sn   

r  c            
      C   s\   g } t  }|D ]#}|\}}}}}}|sq|dv rd}t||p!d|||}	| |	 q| S )z:Return currently connected users as a list of namedtuples.)z:0.0z:0Z	localhostN)r   usersr   Zsuserr   )
r  Zrawlistitemrp   ZttyZhostnameZtstampZuser_processr   ntr?   r?   r@   r    s   r  c                  C   sv   dt   } t| (}|D ]}|dr*t|  d }|a|  W  d   S qtd|  1 s4w   Y  dS )zAReturn the system boot time expressed in seconds since the epoch.r   s   btimer   Nzline 'btime' not found in %s)r   r   r   r   r   r{   	BOOT_TIMEr4  )r`   r   r   r   r?   r?   r@   	boot_time(  s   


r  c                   C   s   dd t tt D S )z7Returns a list of PIDs currently running on the system.c                 S   s   g | ]
}|  rt|qS r?   )rs  r   r   r?   r?   r@   r   =  s    zpids.<locals>.<listcomp>)ry   r  r    r   r?   r?   r?   r@   r   ;     r   c              	   C   s   t | sdS z=dt | f }t|'}|D ]}|dr3t| d }|| k  W  d   W S qtd| 1 s=w   Y  W dS  ttfyT   | t	 v  Y S w )zcCheck for the existence of a unix PID. Linux TIDs are not
    supported (always return False).
    F%s/%s/statuss   Tgid:r   Nz'Tgid' line not found in %s)
r   
pid_existsr   r   r   r   r{   r   EnvironmentErrorr   )r   r`   r   r   Ztgidr?   r?   r@   r  @  s    




r  c               
   C   s   i } t  }t D ]E}ztd||f }| }W d   n1 s"w   Y  W n ttfy3   Y qw |d}||d d  }t|d }|| |< q| S )zsObtain a {pid: ppid, ...} dict for all running processes in
    one shot. Used to speed up Process.children().
    
%s/%s/statN   )r:   r   )	r   r   r   r   r   r   rK  r{   r   )r   r   r   r   rx  rparZdsetppidr?   r?   r@   ppid_map`  s    



r  c                    s   t   fdd}|S )zlDecorator which translates bare OSError and IOError exceptions
    into NoSuchProcess and AccessDenied.
    c                    s   z | g|R i |W S  t y   t| j| j ty'   t| j| j tyA   tj	d| j
| jf s@t| j| j w )N%s/%s)r   r   r   _namer   r   r   ry   r`   r2  r  )r  argskwargsfunr?   r@   wrapperz  s   z wrap_exceptions.<locals>.wrapper)	functoolswraps)r  r  r?   r  r@   wrap_exceptionsv  s   r  c                   @   sd  e Zd ZdZg dZdd Zdd Zeedd Z	eed	d
 Z
eedd Zdd Zdd Zedd Zdd Zedd Zedd Zedd Zejde  r^edd Zedd Zed d! Zed\d#d$Zed%d& Zed'd( Zereed)ed*ed+fd,d-Z neZ ered.d/ Z!ed0d1 Z"eed2fd3d4Z#eed5fd6d7Z$ed8d9 Z%ed:d; Z&ed<d= Z'e(red>d? Z)ed@fdAdBZ*edCdD Z+e,redEdF Z-edGdH Z.e/d"ured\dIdJZ0edKdL Z1edMdN Z2ed]dPdQZ3edRdS Z4edTdU Z5eedVfdWdXZ6eedYfdZd[Z7d"S )^ProcesszLinux process implementation.)r   r  _ppidr  _cachec                 C   s   || _ d | _d | _t | _d S r  )r   r  r  r   r  )r  r   r?   r?   r@   r    s   zProcess.__init__c                 C   s   t d| j| jf  dS )z+Raise NSP if the process disappeared on us.r  N)ry   rb  r  r   r  r?   r?   r@   _assert_alive  s   zProcess._assert_alivec                 C   s   t d| j| jf }| }W d   n1 sw   Y  |d}||dd | }||d d  }i }||d< |d |d	< |d |d
< |d |d< |d |d< |d |d< |d |d< |d |d< |d |d< |d |d< |d |d< |S )aZ  Parse /proc/{pid}/stat file and return a dict with various
        process info.
        Using "man proc" as a reference: where "man proc" refers to
        position N always substract 3 (e.g ppid position 4 in
        'man proc' == position 1 in here).
        The return value is cached in case oneshot() ctx manager is
        in use.
        r  Nr     (r   r:   r   r   r=  r  r#   ttynr   utime   stime   children_utimerV  children_stime   create_time$   cpu_num'   blkio_ticks)r   r  r   r   rK  rz  r{   )r  r   rx  r  r   r   r   r?   r?   r@   _parse_stat_file  s&   

zProcess._parse_stat_filec                 C   s@   t d| j| jf }| W  d   S 1 sw   Y  dS )zRead /proc/{pid}/stat file and return its content.
        The return value is cached in case oneshot() ctx manager is
        in use.
        r  N)r   r  r   r   r  r   r?   r?   r@   _read_status_file  s   $zProcess._read_status_filec                 C   sH   t d| j| jf td}|  W  d    S 1 sw   Y  d S )Nz%s/%s/smapsr/  )r   r  r   r3  r   r   r  r?   r?   r@   _read_smaps_file  s   
$zProcess._read_smaps_filec                 C   (   | j |  | j|  | j|  d S r  )r  Zcache_activater  r  r  r?   r?   r@   oneshot_enter  rq  zProcess.oneshot_enterc                 C   r  r  )r  Zcache_deactivater  r  r  r?   r?   r@   oneshot_exit  rq  zProcess.oneshot_exitc                 C   s   |   d }trt|}|S )Nr   )r  r   r   )r  r   r?   r?   r@   r     s   zProcess.namec              	   C   s   zt d| j| jf W S  ttfy8   tjd| j| jf r#Y dS t| js/t	| j| j
t| j| j
| j tyE   t| j| j
w )Nz	%s/%s/exer  r@  )rz   r  r   r   r   ry   r`   lexistsr  r   r  r   r  r   r   r  r?   r?   r@   exe  s   
zProcess.exec                 C   s   t d| j| jf }| }W d    n1 sw   Y  |s"g S |dr)dnd}||r6|d d }||}|dkrNt|dkrNd|v rN|d}|S )Nz%s/%s/cmdlineru   rm   r6   r   )r   r  r   r   r|   r{   r   )r  r   rx  sepcmdliner?   r?   r@   r    s   



zProcess.cmdlinec                 C   sJ   t d| j| jf }| }W d    t|S 1 sw   Y  t|S )Nz%s/%s/environ)r   r  r   r   r   )r  r   rx  r?   r?   r@   environ  s   

zProcess.environc                 C   s8   t |  d }t }z|| W S  ty   Y d S w )Nr  )r   r  r   Zget_terminal_mapr   )r  Ztty_nrr  r?   r?   r@   terminal  s   
zProcess.terminalz/proc/%s/ioc              
   C   s   d| j | jf }i }t|,}|D ]!}| }|r2z	|d\}}W n	 ty+   Y qw t|||< qW d    n1 s=w   Y  |sJtd| zt|d |d |d |d |d |d	 W S  t	yw } ztd
|d ||f d }~ww )Nz%s/%s/ios   : z%s file was emptys   syscrs   syscws
   read_bytess   write_bytess   rchars   wcharz1%r field was not found in %s; found fields are %rr   )
r  r   r   r   r{   r   r   r4  rn   r   )r  r   r   r   r   r   r   r   r?   r?   r@   io_counters  s@   
zProcess.io_countersc                 C   sh   |   }t|d t }t|d t }t|d t }t|d t }t|d t }t|||||S )Nr  r  r  r  r  )r  r   r   ro   )r  r   r  r  r  r  rt   r?   r?   r@   r   <  s   zProcess.cpu_timesc                 C      t |  d S )zWhat CPU the process is on.r  r   r  r  r?   r?   r@   r  F  s   zProcess.cpu_numNc                 C   s   t | j|| jS r  )r   Zwait_pidr   r  )r  Ztimeoutr?   r?   r@   waitK  s   zProcess.waitc                 C   s&   t |  d }tpt }|t | S )Nr  )r   r  r  r  r   )r  ctimeZbtr?   r?   r@   r  O  s   
zProcess.create_timec           	      C   st   t d| j| jf }dd |  d d D \}}}}}}}W d    n1 s+w   Y  t|||||||S )Nz%s/%s/statmc                 S   s   g | ]}t |t qS r?   )r   r   r   r?   r?   r@   r   i  r   z'Process.memory_info.<locals>.<listcomp>r?  )r   r  r   r   r{   rd   )	r  r   Zvmsrj   rW   textlibrx  Zdirtyr?   r?   r@   memory_infoZ  s
   zProcess.memory_infos   \nPrivate.*:\s+(\d+)s   \nPss\:\s+(\d+)s   \nSwap\:\s+(\d+)c           	      C   sj   |   }|  }ttt||d }ttt||d }ttt||d }t||||f  S )Nr   )r  r  r   rL  r   findallre   )	r  Z_private_reZ_pss_reZ_swap_reZ	basic_memZ
smaps_datarf   rg   rh   r?   r?   r@   memory_full_infop  s   zProcess.memory_full_infoc                 C   sP  dd }|   }|sg S |d}g }|d}|g}|||D ]\}}|dd}z
|\}	}
}}}}W n tyH   |dg \}	}
}}}}Y nw |sNd}ntrTt|}| }|d	rgt|sg|dd
 }|	t|	t|
||
dd|
dd|
dd|
dd|
dd|
dd|
dd|
dd|
dd|
ddf q |S )aP  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

            /proc/{PID}/smaps does not exist on kernels < 2.6.14 or if
            CONFIG_MMU kernel configuration option is not enabled.
            c              	   s   s    i }| D ]@}| d d}|d ds!| |fV  || qzt|d d ||d < W q tyE   |d dr?Y qtd| w | |fV  d S )N   r   r   r   r   s   VmFlags:z#don't know how to interpret line %r)r{   r|   popr   r   r   r   )rM  current_blockrx  r   r   r?   r?   r@   
get_blocks  s"   z'Process.memory_maps.<locals>.get_blocks   
r   Nr  r@  z[anon]rv   rw   s   Rss:s   Size:s   Pss:s   Shared_Clean:s   Shared_Dirty:s   Private_Clean:s   Private_Dirty:s   Referenced:s
   Anonymous:s   Swap:)r  r{   r  r   r   r   r   r|   r   r   r   )r  r  rx  rM  r   Z
first_liner  headerZhfieldsr+  Zpermsoffsetrp  r  r`   r?   r?   r@   memory_maps  sN   	












zProcess.memory_mapsc              	   C   sT   zt d| j| jf W S  ttfy)   t| js t| j| jt| j| j| j	w )Nz	%s/%s/cwd)
rz   r  r   r   r   r  r   r  r   r  r  r?   r?   r@   cwd  s   
zProcess.cwds   ctxt_switches:\t(\d+)c                 C   sF   |   }||}|std| j| jf tt|d t|d S )Nz'voluntary_ctxt_switches' and 'nonvoluntary_ctxt_switches'lines were not found in %s/%s/status; the kernel is probably older than 2.6.23r   r   )r  r  r   r  r   r   Zpctxswr   )r  Z	_ctxsw_rerx  Zctxswr?   r?   r@   num_ctx_switches  s   
zProcess.num_ctx_switchess   Threads:\t(\d+)c                 C   s   |   }t||d S Nr   )r  r   r  )r  Z_num_threads_rerx  r?   r?   r@   num_threads  s   zProcess.num_threadsc              	   C   s   t d| j| jf }|  g }d}|D ]a}d| j| j|f }zt|}|  }W d    n1 s6w   Y  W n tyG   d}Y qw ||	dd d  }|
d}t|d t }	t|d	 t }
tt||	|
}|| q|r}|   |S )
Nz
%s/%s/taskFz%s/%s/task/%s/statTr  r:   r   r  r  )ry   r  r  r   r   r   r   r   r   rz  r{   r   r   r   Zpthreadr   r   r  )r  Z
thread_idsr  
hit_enoentZ	thread_idr   r   str   r  r  r  r?   r?   r@   threads  s4   


zProcess.threadsc                 C      t | jS r  )rQ  getpriorityr   r  r?   r?   r@   nice_get  s   zProcess.nice_getc                 C   s   t | j|S r  )rQ  setpriorityr   )r  r   r?   r?   r@   nice_set  s   zProcess.nice_setc                 C   r  r  )r   r4   r   r  r?   r?   r@   cpu_affinity_get"  s   zProcess.cpu_affinity_gets   Cpus_allowed_list:\t(\d+)-(\d+)c                 C   sR   |   }||}|r ttt|d d t|d d d S tttt S )Nr   r   )r  r  r  ranger   r   r   )r  _rerx  r   r?   r?   r@   _get_eligible_cpus&  s
   
*zProcess._get_eligible_cpusc              
   C   s   z
t | j| W d S  ttfyN } z6t|ts|jtjkrI|  }t	t
tt }|D ]}||vr<td||f ||vrHtd||f q. d }~ww )Nz(invalid CPU number %r; choose between %sz0CPU number %r is not eligible; choose between %s)r   Zproc_cpu_affinity_setr   r   r   rx   r   r  r  tupler  r   r   )r  r   r   Zeligible_cpusZall_cpusZcpur?   r?   r@   cpu_affinity_set0  s.   zProcess.cpu_affinity_setc                 C   s,   t | j\}}td urt|}t||S r  )r   r3   r   enumr;   r   Zpionicer  Zioclassr   r?   r?   r@   
ionice_getF  s   zProcess.ionice_getc                 C   sP   |d u rd}|r|t tfv rtd| |dk s|dkr tdt| j||S )Nr   z%r ioclass accepts no valuer?  zvalue not in 0-7 range)r'   r$   r   r   Zproc_ioprio_setr   r  r?   r?   r@   
ionice_setM  s   zProcess.ionice_setc              
   C   s   | j dkr	tdz#|d u rt| j |W S t|dkr#tdt| t| j || W d S  tyM } z|jtjkrHt| j rHt	| j | j
| j d }~ww )Nr   z)can't use prlimit() against PID 0 processr:   z4second argument must be a (soft, hard) tuple, got %s)r   r   r   r   rC  r   r   ZENOSYSr  r   r  r  )r  r   r   r   r?   r?   r@   rlimitY  s$   
zProcess.rlimitc                 C   s$   |   d }tr| }t|dS )Nr=  ?)r  r   r   PROC_STATUSESr   )r  Zletterr?   r?   r@   r=  s  s   zProcess.statusc                 C   s~  g }t d| j| jf }d}|D ]}d| j| j|f }zt|}W n5 ttfy/   d}Y q tyW } z|jtj	krBW Y d }~q|jtj
krRt| W Y d }~q d }~ww |drt|rd| j| j|f }z+t|}t|  d }	t|  d d}
W d    n1 sw   Y  W n ty   d}Y qw t|
}t|t|t|	||
}|| q|r|   |S )	Nr  Fr  Tr   z%s/%s/fdinfo/%sr   r   )ry   r  r  r   rz   r   r   r   r   r  r  r   r   r   r   r   r   r{   r   r_   r   r  )r  r  rg  r  ra   r7  r`   r   r   posrc   rb   r  r?   r?   r@   
open_files{  sR   


zProcess.open_filesr  c                 C   s   t || j}|   |S r  )rH  rE  r   r  )r  rD  r   r?   r?   r@   connections  s   zProcess.connectionsc                 C   s   t td| j| jf S )Nr  )r   ry   r  r  r   r  r?   r?   r@   num_fds  r  zProcess.num_fdsc                 C   r  )Nr  r  r  r?   r?   r@   r    s   zProcess.ppids   Uid:\t(\d+)\t(\d+)\t(\d+)c                 C   6   |   }||d \}}}tt|t|t|S r  )r  r  r   Zpuidsr   )r  Z_uids_rerx  real	effectivesavedr?   r?   r@   uids     zProcess.uidss   Gid:\t(\d+)\t(\d+)\t(\d+)c                 C   r	  r  )r  r  r   Zpgidsr   )r  Z_gids_rerx  r
  r  r  r?   r?   r@   gids  r  zProcess.gidsr  r  )8r<   r=   r>   rF  r{  r  r  r  r   r  r  r  r  r  r   r  r  r  r  ry   r`   r2  getpidr  r   r  r  r  r  	HAS_SMAPSr   r   r  r  r  r  r  r  r  r  HAS_CPU_AFFINITYr  r  r  HAS_PROC_IO_PRIORITYr   r  r   r  r=  r  r  r  r  r  r  r?   r?   r?   r@   r    s    !



	

	




C










	

/

r  r  r  )F)rF  Z
__future__r   r(  r  r   r  r   ry   r   r  r)  sys	tracebackr   r   r   r@  r   r   r   r   r	   rQ  r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Z_compatr   r   r   r   r    r!   version_infor  Z__extra__all__r  r`   r2  r  r  hasattrr  r  r  r   r   r   Zgetpagesizer   r  r3  	byteorderr'  ri  Z	AF_PACKETr9   IntEnumr   r8   r$   r%   r&   r'   r;   globalsr!  __members__ZSTATUS_RUNNINGZSTATUS_SLEEPINGZSTATUS_DISK_SLEEPZSTATUS_STOPPEDZSTATUS_TRACING_STOPZSTATUS_ZOMBIEZSTATUS_DEADZSTATUS_WAKE_KILLZSTATUS_WAKINGZSTATUS_IDLEZSTATUS_PARKEDr  r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r5  rM   rY   r_   rd   r   re   ri   r   rl   rn   ro   rz   r   r   r   r   	Exception	print_excr   r   ZresourceImportErrorr   ZCDLLr   r  dirr   r   r   r   r   r   r   r   r   r  Znet_if_addrsr  r  rH  rI  rO  rS  
disk_usagerk  rl  r  r  r  r  r  r  r   r  r  r  r  r?   r?   r?   r@   <module>   sj  












	4z:*

! o
%
_
J-q a 