o
    axH                     @   s  d 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gZe edZ!e edZ"e edZ#e$ Z%ej&Z&ej'e
j(ej)e
j*ej+e
j,ej-e
j,ej.e
j/iZ0ej1e
j2ej3e
j4ej5e
j6ej7e
j8ej9e
j:ej;e
j<ej=e
j>ej?e
j@ejAe
jBejCe
jDejEe
jFejGe
jHiZIeJdddddddd d!ZKed"d#d$gZLeLZMed%g d&ZNed'g d(ZOd)d* ZPd+d, ZQd-d. ZRd/d0 ZSd1d2 ZTd3d4 ZUd5d6 ZVejWZWejXZXdKd8d9ZYejZZZe"r@ej[Z[dLd;d<Z\d=d> Z]d?d@ Z^dAdB Z_dCdD Z`dEdF ZadGdH ZbG dIdJ dJecZddS )MzAIX platform implementation.    N)
namedtuple   )_common)_psposix)_psutil_aix)_psutil_posix)NIC_DUPLEX_FULL)NIC_DUPLEX_HALF)NIC_DUPLEX_UNKNOWN)AccessDenied)NoSuchProcess)ZombieProcess)conn_to_ntuple)get_procfs_path)memoize_when_activated)usage_percent)PY3)FileNotFoundError)PermissionError)ProcessLookupErrorZPROCFS_PATHproc_threadsnet_io_countersproc_io_counters                  )ppidrssvmscreate_timenicenum_threadsstatusttynrpmemr    r!   	scputimes)usersystemZidleZiowaitsvmem)totalZ	availablepercentusedfreec                  C   s4   t  \} }}}}t| | | dd}t| ||||S )Nr   Zround_)cextZvirtual_memr   r+   )r,   Zavailr/   ZpinnedZinuser-    r2   //usr/lib/python3/dist-packages/psutil/_psaix.pyvirtual_memoryj   s   r4   c                  C   s:   t  \} }}}| | }t|| dd}t| |||||S )z=Swap system memory as a (total, used, free, sin, sout) tuple.r   r0   )r1   Zswap_memr   r   Zsswap)r,   r/   ZsinZsoutr.   r-   r2   r2   r3   swap_memoryp   s   r5   c                  C   s   t  } tdd t|  D  S )z-Return system-wide CPU times as a named tuplec                 S      g | ]}t |qS r2   )sum.0xr2   r2   r3   
<listcomp>       zcpu_times.<locals>.<listcomp>)r1   per_cpu_timesr(   zipretr2   r2   r3   	cpu_times}   s   rA   c                  C   s   t  } dd | D S )z5Return system per-CPU times as a list of named tuplesc                 S   s   g | ]}t | qS r2   )r(   r8   r2   r2   r3   r;      r<   z!per_cpu_times.<locals>.<listcomp>)r1   r=   r?   r2   r2   r3   r=      s   r=   c                   C   s"   zt dW S  ty   Y dS w )z0Return the number of logical CPUs in the system.SC_NPROCESSORS_ONLNN)ossysconf
ValueErrorr2   r2   r2   r3   cpu_count_logical   s
   rF   c                  C   sr   d} t j| dt jt jd}| \}}tr dd ||fD \}}|jdkr-td| |f |  }t	|p8d S )Nzlsdev -Cc processorT)shellstdoutstderrc                 S      g | ]	}| tjjqS r2   decodesysrH   encodingr8   r2   r2   r3   r;          z#cpu_count_cores.<locals>.<listcomp>r   z%r command error
%s)

subprocessPopenPIPEcommunicater   
returncodeRuntimeErrorstrip
splitlineslen)cmdprH   rI   Z
processorsr2   r2   r3   cpu_count_cores   s   

r[   c                  C   s    t  \} }}}t| |||S )z*Return various CPU stats as a named tuple.)r1   	cpu_statsr   Z	scpustats)Zctx_switchesZ
interruptsZsoft_interruptsZsyscallsr2   r2   r3   r\      s   r\   Fc              	   C   sh   g }t  }|D ])}|\}}}}|dkrd}| st|jsqd }}	t||||||	}
||
 q|S )zReturn system disk partitions.Znone N)r1   disk_partitions
disk_usager,   r   Z	sdiskpartappend)allretlistZ
partitions	partitionZdeviceZ
mountpointZfstypeZoptsZmaxfileZmaxpathntupler2   r2   r3   r^      s   
r^   c                 C   s   t j}| |vrtd| ddd |D f t j|  \}}t|}g }|D ],}|\}}	}
}}}}|	|vr7q'|
|vr<q't||	|
|||t|dkrJ|ndd}|| q'|S )zReturn socket connections.  If pid == -1 return system-wide
    connections (as opposed to connections opened by one process only).
    z+invalid %r kind argument; choose between %sz, c                 S   r6   r2   )reprr8   r2   r2   r3   r;      r<   z#net_connections.<locals>.<listcomp>re   Npid)	r   Z	conn_tmaprE   joinr1   net_connectionsr   TCP_STATUSESr`   )kind_pidZcmapZfamiliestypesrawlistr@   itemfdZfamZtype_ZladdrZraddrr%   rh   ntr2   r2   r3   rj      s&   
rj   c                  C   s   t td} tdd t D }i }|D ]X}t|\}}d}d}tjdd|gtjtjd}|	 \}	}
t
r@d	d |	|
fD \}	}
|jdkr[td
|	}|dur[t|d}|d}| |t}t||||||< q|S )z)Get NIC stats (isup, duplex, speed, mtu).)ZFullZHalfc                 S   s   g | ]}|d  qS )r   r2   r8   r2   r2   r3   r;      r<   z net_if_stats.<locals>.<listcomp>r]   r   z/usr/bin/entstatz-drH   rI   c                 S   rJ   r2   rK   r8   r2   r2   r3   r;      rO   z"Running: (\d+) Mbps.*?(\w+) DuplexNr   r   )r   r	   setnet_if_addrsr1   net_if_statsrP   rQ   rR   rS   r   rT   researchintgroupgetr
   r   Z	snicstats)Z
duplex_mapnamesr@   nameZisupZmtuZduplexZspeedrZ   rH   rI   Z	re_resultr2   r2   r3   rv      s6   


rv   c                   C   s   t  S )z:The system boot time expressed in seconds since the epoch.)r1   	boot_timer2   r2   r2   r3   r~     s   r~   c                  C   s\   g } t  }d}|D ]!}|\}}}}}}	|sq
||v rd}t|||||	}
| |
 q
| S )z:Return currently connected users as a list of namedtuples.)z:0.0z:0	localhost)r1   usersr   Zsuserr`   )rb   ro   r   rp   r)   ZttyZhostnameZtstampZuser_processrh   rr   r2   r2   r3   r     s   r   c                   C   s   dd t t D S )z7Returns a list of PIDs currently running on the system.c                 S   s   g | ]
}|  rt|qS r2   )isdigitry   r8   r2   r2   r3   r;   -  s    zpids.<locals>.<listcomp>)rC   listdirr   r2   r2   r2   r3   pids+  s   r   c                 C   s   t jt jt t| dS )z&Check for the existence of a unix pid.Zpsinfo)rC   pathexistsri   r   strrg   r2   r2   r3   
pid_exists0  s   r   c                    s   t   fdd}|S )zCall callable into a try/except clause and translate ENOENT,
    EACCES and EPERM in NoSuchProcess or AccessDenied exceptions.
    c              	      sp   z | g|R i |W S  t tfy*   t| js!t| j| jt| j| j| j ty7   t	| j| jw N)
r   r   r   rh   r   _namer   _ppidr   r   )selfargskwargsfunr2   r3   wrapper9  s   
z wrap_exceptions.<locals>.wrapper)	functoolswraps)r   r   r2   r   r3   wrap_exceptions5  s   r   c                   @   st  e Zd ZdZg dZdd Zdd Zdd Zee	d	d
 Z
ee	dd Zedd Zedd Zedd Zedd Zedd Zedd ZerRedd Zed=ddZedd Zed d! Zed"d# Zed$d% Zed&d' Zed(d) Zed*d+ Zed,d- Zed.d/ ZeZed0d1 Zd2d3 Z ed4d5 Z!ed6d7 Z"ed>d9d:Z#e$red;d< Z%d8S d8S )?Processz1Wrapper class around underlying C implementation.)rh   r   r   _procfs_path_cachec                 C   s   || _ d | _d | _t | _d S r   )rh   r   r   r   r   )r   rh   r2   r2   r3   __init__O  s   zProcess.__init__c                 C      | j |  | j|  d S r   )_proc_basic_infoZcache_activate
_proc_credr   r2   r2   r3   oneshot_enterU     zProcess.oneshot_enterc                 C   r   r   )r   Zcache_deactivater   r   r2   r2   r3   oneshot_exitY  r   zProcess.oneshot_exitc                 C      t | j| jS r   )r1   Zproc_basic_inforh   r   r   r2   r2   r3   r   ]     zProcess._proc_basic_infoc                 C   r   r   )r1   Z	proc_credrh   r   r   r2   r2   r3   r   b  r   zProcess._proc_credc                 C   s$   | j dkrdS t| j | jdS )Nr   Zswapper )rh   r1   Z	proc_namer   rstripr   r2   r2   r3   r}   g  s   
zProcess.namec                 C   s   |   }|sdS |d }tjj|v r@tj|s%tjtj|  |}tj|r:tj|r:t	|tj
r:|S tj|}tjd dD ]}tjtj||}tj|rft	|tj
rf|  S qHdS )Nr]   r   PATH:)cmdlinerC   r   sepisabsabspathri   cwdisfileaccessX_OKbasenameenvironsplit)r   r   exer   Zpossible_exer2   r2   r3   r   n  s,   
zProcess.exec                 C      t | jS r   )r1   Z	proc_argsrh   r   r2   r2   r3   r        zProcess.cmdlinec                 C   r   r   )r1   Zproc_environrh   r   r2   r2   r3   r     r   zProcess.environc                 C      |   td  S )Nr"   r   proc_info_mapr   r2   r2   r3   r"        zProcess.create_timec                 C   r   )Nr$   r   r   r2   r2   r3   r$     r   zProcess.num_threadsc                 C   sV   t | j}g }|D ]\}}}t|||}|| q
|s)td| j| jf  |S )N%s/%s)	r1   r   rh   r   Zpthreadr`   rC   statr   )r   ro   rb   Z	thread_idutimeZstimerd   r2   r2   r3   threads  s   zProcess.threadsinetc                 C   s,   t || jd}|std| j| jf  |S )N)rm   r   )rj   rh   rC   r   r   )r   rl   r@   r2   r2   r3   connections  s   zProcess.connectionsc                 C   r   r   )
cext_posixgetpriorityrh   r   r2   r2   r3   nice_get  r   zProcess.nice_getc                 C   s   t | j|S r   )r   setpriorityrh   )r   valuer2   r2   r3   nice_set  s   zProcess.nice_setc                 C   s   |   td  | _| jS )Nr   )r   r   r   r   r2   r2   r3   r     s   zProcess.ppidc                 C   s"   |   \}}}}}}t|||S r   r   r   Zpuids)r   real	effectivesaved_r2   r2   r3   uids     zProcess.uidsc                 C   s"   |   \}}}}}}t|||S r   r   )r   r   r   r   r   r2   r2   r3   gids  r   zProcess.gidsc                 C   s   t | j| j}tj| S r   )r1   Zproc_cpu_timesrh   r   r   Z	pcputimes)r   rA   r2   r2   r3   rA     s   
zProcess.cpu_timesc                 C   sP   |   td  }|d@ d? |d@ B }tdD ]}t|j|kr%|  S qd S )Nr&   l           i  z	/dev/**/*)r   r   globrC   r   st_rdev)r   ZttydevZdevr2   r2   r3   terminal  s   zProcess.terminalc                 C   sP   | j }ztd|| jf }|dW S  ty'   td|| jf  Y d S w )Nz	%s/%s/cwd/r   )r   rC   readlinkrh   r   r   r   )r   Zprocfs_pathresultr2   r2   r3   r     s   zProcess.cwdc                 C   s2   |   }|td  d }|td  d }t||S )Nr    i   r!   )r   r   r'   )r   r@   r    r!   r2   r2   r3   memory_info  s   
zProcess.memory_infoc                 C   s   |   td  }t|dS )Nr%   ?)r   r   PROC_STATUSESr{   )r   coder2   r2   r3   r%     s   zProcess.statusc                 C   s   t jddt| jgt jt jd}| \}}tr#dd ||fD \}}d| v r0t| j| j	t
d|}g }|D ]%\}}| }|drM|d	d  }| d
krTq:|t|t| q:|S )Nz/usr/bin/procfilesz-nrs   c                 S   rJ   r2   rK   r8   r2   r2   r3   r;     rO   z&Process.open_files.<locals>.<listcomp>zno such processz (\d+): S_IFREG.*\s*.*name:(.*)\nz//r   zcannot be retrieved)rP   rQ   r   rh   rR   rS   r   lowerr   r   rw   findallrV   
startswithr`   r   Z	popenfilery   )r   rZ   rH   rI   Z	procfilesrb   rq   r   r2   r2   r3   
open_files  s(   

zProcess.open_filesc                 C   s(   | j dkrdS ttd| j| j f S )Nr   z%s/%s/fd)rh   rX   rC   r   r   r   r2   r2   r3   num_fds  s   
zProcess.num_fdsc                 C   s   t jt| j S r   )r   Zpctxswr1   Zproc_num_ctx_switchesrh   r   r2   r2   r3   num_ctx_switches  s   
zProcess.num_ctx_switchesNc                 C   s   t | j|| jS r   )r   Zwait_pidrh   r   )r   Ztimeoutr2   r2   r3   wait  s   zProcess.waitc                 C   sR   zt | j\}}}}W n ty    t| jst| j| j w t||||S r   )	r1   r   rh   OSErrorr   r   r   r   Zpio)r   ZrcZwcrbwbr2   r2   r3   io_counters  s   
zProcess.io_counters)r   r   )&__name__
__module____qualname____doc__	__slots__r   r   r   r   r   r   r   r}   r   r   r   r"   r$   HAS_THREADSr   r   r   r   r   r   r   rA   r   r   r   Zmemory_full_infor%   r   r   r   r   HAS_PROC_IO_COUNTERSr   r2   r2   r2   r3   r   J  sz    















	



r   )F)re   )er   r   r   rC   rw   rP   rM   collectionsr   r]   r   r   r   r1   r   r   r   r	   r
   r   r   r   r   r   r   r   Z_compatr   r   r   r   Z__extra__all__hasattrr   ZHAS_NET_IO_COUNTERSr   ZgetpagesizeZ	PAGE_SIZEZAF_LINKZSIDLZSTATUS_IDLEZSZOMBZSTATUS_ZOMBIEZSACTIVEZSTATUS_RUNNINGZSSWAPZSSTOPZSTATUS_STOPPEDr   ZTCPS_ESTABLISHEDZCONN_ESTABLISHEDZTCPS_SYN_SENTZCONN_SYN_SENTZTCPS_SYN_RCVDZCONN_SYN_RECVZTCPS_FIN_WAIT_1ZCONN_FIN_WAIT1ZTCPS_FIN_WAIT_2ZCONN_FIN_WAIT2ZTCPS_TIME_WAITZCONN_TIME_WAITZTCPS_CLOSEDZ
CONN_CLOSEZTCPS_CLOSE_WAITZCONN_CLOSE_WAITZTCPS_LAST_ACKZCONN_LAST_ACKZTCPS_LISTENZCONN_LISTENZTCPS_CLOSINGZCONN_CLOSINGZPSUTIL_CONN_NONEZ	CONN_NONErk   dictr   r'   Zpfullmemr(   r+   r4   r5   rA   r=   rF   r[   r\   Zdisk_io_countersr_   r^   ru   r   rj   rv   r~   r   r   r   r   objectr   r2   r2   r2   r3   <module>   s   


		

%