o
    hLG                     @   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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Z+ej,e
j-ej.e
j/ej0e
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
jCiZDeEdddddddddZFeddd gZGeGZHed!g d"ZIed#g d$ZJd%d& ZKd'd( ZLd)d* ZMd+d, ZNd-d. ZOd/d0 ZPd1d2 ZQejRZRejSZSdGd4d5ZTejUZUer(ejVZVdHd7d8ZWd9d: ZXd;d< ZYd=d> ZZd?d@ Z[dAdB Z\dCdD Z]G dEdF dFZ^dS )Iz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PROCFS_PATHproc_threadsnet_io_countersproc_io_counters                  )ppidrssvmscreate_timenicenum_threadsstatusttynrpmemr   r   	scputimes)usersystemidleiowaitsvmem)total	availablepercentusedfreec                  C   s4   t  \} }}}}t| | | dd}t| ||||S )Nr   round_)cextvirtual_memr   r*   )r+   availr/   _pinnedinuser-    r7   A/var/www/vscode/kcb/lib/python3.10/site-packages/psutil/_psaix.pyvirtual_memoryg   s   r9   c                  C   s:   t  \} }}}| | }t|| dd}t| |||||S )z=Swap system memory as a (total, used, free, sin, sout) tuple.r   r0   )r2   swap_memr   r   sswap)r+   r/   sinsoutr.   r-   r7   r7   r8   swap_memorym   s   r>   c                  C   s   t  } tdd t|  D  S )z.Return system-wide CPU times as a named tuple.c                 S   s   g | ]}t |qS r7   )sum.0xr7   r7   r8   
<listcomp>}       zcpu_times.<locals>.<listcomp>)r2   per_cpu_timesr%   zipretr7   r7   r8   	cpu_timesz   s   rI   c                  C   s   t  } dd | D S )z6Return system per-CPU times as a list of named tuples.c                 S   s   g | ]}t | qS r7   )r%   r@   r7   r7   r8   rC      rD   z!per_cpu_times.<locals>.<listcomp>)r2   rE   rG   r7   r7   r8   rE      s   rE   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
ValueErrorr7   r7   r7   r8   cpu_count_logical   s
   rN   c                  C   sv   g d} t j| t jt jd}| \}}dd ||fD \}}|jdkr/| d| }t||  }t|p:d S )N)lsdevz-Cc	processorstdoutstderrc                 s       | ]
}| tjjV  qd S NdecodesysrR   encodingr@   r7   r7   r8   	<genexpr>   s    z"cpu_count_cores.<locals>.<genexpr>r   z command error
)	
subprocessPopenPIPEcommunicate
returncodeRuntimeErrorstrip
splitlineslen)cmdprR   rS   msg
processorsr7   r7   r8   cpu_count_cores   s   
rh   c                  C   s    t  \} }}}t| |||S )z*Return various CPU stats as a named tuple.)r2   	cpu_statsr   	scpustats)ctx_switches
interruptssoft_interruptssyscallsr7   r7   r8   ri      s   ri   Fc           	      C   s\   g }t  }|D ]#}|\}}}}|dkrd}| st|jsqt||||}|| q|S )zReturn system disk partitions.none )r2   disk_partitions
disk_usager+   r   	sdiskpartappend)	allretlist
partitions	partitiondevice
mountpointfstypeoptsntupler7   r7   r8   rq      s   
rq   c                 C   s~   t j|  \}}t|}g }|D ],}|\}}}	}
}}}||vr q|	|vr%qt|||	|
||t|dkr3|ndd}|| q|S )zReturn socket connections.  If pid == -1 return system-wide
    connections (as opposed to connections opened by one process only).
    r~   Npid)r   	conn_tmapr2   net_connectionsr   TCP_STATUSESrt   )kind_pidfamiliestypesrawlistrH   itemfdfamtype_laddrraddrr"   r   ntr7   r7   r8   r      s*   

r   c                  C   s   t td} dd t D }i }|D ]c}t|}t|}d}d}tjdd|gtjtjd}|	 \}	}
d	d
 |	|
fD \}	}
|j
dkrZtd|	}|durZt|d}|d}d|}d|v }| |t}t|||||||< q|S )z)Get NIC stats (isup, duplex, speed, mtu).)FullHalfc                 S   s   h | ]}|d  qS )r   r7   r@   r7   r7   r8   	<setcomp>   rD   znet_if_stats.<locals>.<setcomp>rp   r   z/usr/bin/entstatz-drQ   c                 s   rT   rU   rV   r@   r7   r7   r8   rZ          
znet_if_stats.<locals>.<genexpr>z"Running: (\d+) Mbps.*?(\w+) DuplexNr   r   ,running)r   r	   net_if_addrs
cext_posix
net_if_mtunet_if_flagsr[   r\   r]   r^   r_   researchintgroupjoingetr
   r   	snicstats)
duplex_mapnamesrH   namemtuflagsduplexspeedre   rR   rS   	re_resultoutput_flagsisupr7   r7   r8   net_if_stats   s:   






r   c                   C   s   t  S )z:The system boot time expressed in seconds since the epoch.)r2   	boot_timer7   r7   r7   r8   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)r2   usersr   suserrt   )rv   r   r   r   r&   ttyhostnametstampuser_processr   r   r7   r7   r8   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 r7   )isdigitr   r@   r7   r7   r8   rC   1  s    zpids.<locals>.<listcomp>)rK   listdirr   r7   r7   r7   r8   pids/  s   r   c                 C   s   t jt jt t| dS )z&Check for the existence of a unix pid.psinfo)rK   pathexistsr   r   strr   r7   r7   r8   
pid_exists4  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              
      s   | j | j| j}}}z | g|R i |W S  ttfy6 } zt|s+t|||t||||d }~w tyG } zt	|||d }~ww rU   )
r   _ppid_nameFileNotFoundErrorProcessLookupErrorr   r   r   PermissionErrorr   )selfargskwargsr   r   r   errfunr7   r8   wrapper>  s   z wrap_exceptions.<locals>.wrapper)	functoolswraps)r   r   r7   r   r8   wrap_exceptions9  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.)_cacher   r   _procfs_pathr   c                 C   s   || _ d | _d | _t | _d S rU   )r   r   r   r   r   )r   r   r7   r7   r8   __init__U  s   zProcess.__init__c                 C      | j |  | j|  d S rU   )_proc_basic_infocache_activate
_proc_credr   r7   r7   r8   oneshot_enter[     zProcess.oneshot_enterc                 C   r   rU   )r   cache_deactivater   r   r7   r7   r8   oneshot_exit_  r   zProcess.oneshot_exitc                 C      t | j| jS rU   )r2   proc_basic_infor   r   r   r7   r7   r8   r   c     zProcess._proc_basic_infoc                 C   r   rU   )r2   	proc_credr   r   r   r7   r7   r8   r   h  r   zProcess._proc_credc                 C   s$   | j dkrdS t| j | jdS )Nr   swapper )r   r2   	proc_namer   rstripr   r7   r7   r8   r   m  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 )Nrp   r   PATH:)cmdlinerK   r   sepisabsabspathr   cwdisfileaccessX_OKbasenameenvironsplit)r   r   exer   possible_exer7   r7   r8   r   t  s.   

zProcess.exec                 C      t | jS rU   )r2   	proc_argsr   r   r7   r7   r8   r        zProcess.cmdlinec                 C   r   rU   )r2   proc_environr   r   r7   r7   r8   r     r   zProcess.environc                 C      |   td  S )Nr   r   proc_info_mapr   r7   r7   r8   r        zProcess.create_timec                 C   r   )Nr!   r   r   r7   r7   r8   r!     r   zProcess.num_threadsc                 C   sX   t | j}g }|D ]\}}}t|||}|| q
|s*t| j d| j  |S )N/)	r2   r   r   r   pthreadrt   rK   statr   )r   r   rv   	thread_idutimestimer}   r7   r7   r8   threads  s   zProcess.threadsinetc                 C   s.   t || jd}|st| j d| j  |S )N)r   r   )r   r   rK   r   r   )r   r   rH   r7   r7   r8   r     s   zProcess.net_connectionsc                 C   r   rU   )r   getpriorityr   r   r7   r7   r8   nice_get  r   zProcess.nice_getc                 C   s   t | j|S rU   )r   setpriorityr   )r   valuer7   r7   r8   nice_set  s   zProcess.nice_setc                 C   s   |   td  | _| jS )Nr   )r   r   r   r   r7   r7   r8   r     s   zProcess.ppidc                 C   s"   |   \}}}}}}t|||S rU   r   r   puids)r   real	effectivesaved_r7   r7   r8   uids     zProcess.uidsc                 C   s"   |   \}}}}}}t|||S rU   r  )r   r	  r  r  r  r7   r7   r8   gids  r  zProcess.gidsc                 C   s   t | j| j}tj| S rU   )r2   proc_cpu_timesr   r   r   	pcputimes)r   tr7   r7   r8   rI     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   globrK   r   st_rdev)r   ttydevdevr7   r7   r8   terminal  s   zProcess.terminalc                 C   sV   | j }zt| d| j d}|dW S  ty*   t| d| j  Y dS w )Nr   z/cwdrp   )r   rK   readlinkr   r   r   r   )r   procfs_pathresultr7   r7   r8   r     s   zProcess.cwdc                 C   s2   |   }|td  d }|td  d }t||S )Nr   i   r   )r   r   r$   )r   rH   r   r   r7   r7   r8   memory_info  s   
zProcess.memory_infoc                 C   s   |   td  }t|dS )Nr"   ?)r   r   PROC_STATUSESr   )r   coder7   r7   r8   r"     s   zProcess.statusc                 C   s   t jddt| jgt jt jd}| \}}dd ||fD \}}d| v r.t| j| jt	
d|}g }|D ]%\}}| }|drK|d	d  }| d
krRq8|t|t| q8|S )Nz/usr/bin/procfilesz-nrQ   c                 s   rT   rU   rV   r@   r7   r7   r8   rZ     r   z%Process.open_files.<locals>.<genexpr>zno such processz(\d+): S_IFREG.*name:(.*)\nz//r   zcannot be retrieved)r[   r\   r   r   r]   r^   lowerr   r   r   findallra   
startswithrt   r   	popenfiler   )r   re   rR   rS   	procfilesrv   r   r   r7   r7   r8   
open_files  s*   

zProcess.open_filesc                 C   s,   | j dkrdS tt| j d| j  dS )Nr   r   z/fd)r   rc   rK   r   r   r   r7   r7   r8   num_fds  s   
zProcess.num_fdsc                 C   s   t jt| j S rU   )r   pctxswr2   proc_num_ctx_switchesr   r   r7   r7   r8   num_ctx_switches!     zProcess.num_ctx_switchesNc                 C   s   t | j|| jS rU   )r   wait_pidr   r   )r   timeoutr7   r7   r8   wait%  r'  zProcess.waitc              
   C   s^   zt | j\}}}}W n ty& } zt| js!t| j| j| d }~ww t||||S rU   )	r2   r   r   OSErrorr   r   r   r   pio)r   rcwcrbwbr   r7   r7   r8   io_counters+  s   
zProcess.io_counters)r   rU   )&__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  rI   r  r   r  memory_full_infor"   r"  r#  r&  r*  HAS_PROC_IO_COUNTERSr1  r7   r7   r7   r8   r   P  sz    















	



r   )F)r~   )_r5  r   r  rK   r   r[   rX   collectionsr   rp   r   r   r   r2   r   r   r   r	   r
   r   r   r   r   r   r   r   __extra__all__hasattrr7  HAS_NET_IO_COUNTERSr9  getpagesize	PAGE_SIZEAF_LINKSIDLSTATUS_IDLESZOMBSTATUS_ZOMBIESACTIVESTATUS_RUNNINGSSWAPSSTOPSTATUS_STOPPEDr  TCPS_ESTABLISHEDCONN_ESTABLISHEDTCPS_SYN_SENTCONN_SYN_SENTTCPS_SYN_RCVDCONN_SYN_RECVTCPS_FIN_WAIT_1CONN_FIN_WAIT1TCPS_FIN_WAIT_2CONN_FIN_WAIT2TCPS_TIME_WAITCONN_TIME_WAITTCPS_CLOSED
CONN_CLOSETCPS_CLOSE_WAITCONN_CLOSE_WAITTCPS_LAST_ACKCONN_LAST_ACKTCPS_LISTENCONN_LISTENTCPS_CLOSINGCONN_CLOSINGPSUTIL_CONN_NONE	CONN_NONEr   dictr   r$   pfullmemr%   r*   r9   r>   rI   rE   rN   rh   ri   disk_io_countersrr   rq   r   r   r   r   r   r   r   r   r   r   r7   r7   r7   r8   <module>   s   


		

+