U
    !h*A                     @   s  d 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 g 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$e
j%ej&e
j'ej(e
j)ej*e
j+ej,e
j-ej.e
j/ej0e
j1ej2iZ3e
j4ej5e
j6ej7e
j8ej9e
j:ej;e
j<ej=iZ>e?ddddddddddddZ@e?dddddddddZAeddd d!d"gZBed#d$d%d&d'd(d)d*d+gZCed,d-d.d/d0gZDed1eDjEd2 ZFd3d4 ZGd5d6 ZHd7d8 ZId9d: ZJd;d< ZKd=d> ZLd?d@ ZMe
N r@dAdB ZOejPZPe
jQZQd\dDdEZRdFdG ZSe
jTZTejUZUd]dIdJZVdKdL ZWdMdN ZXz
eX ZYW n6 eZk
r Z[ zedOe[ dZYW 5 dZ[[[X Y nX dPdQ Z\dRdS Z]dTdU Z^ej_Z_dVdW Z`dXdY ZaG dZd[ d[ZbdS )^zmacOS platform implementation.    N)
namedtuple   )_common)_psposix)_psutil_osx)_psutil_posix)AccessDenied)NoSuchProcess)ZombieProcess)	conn_tmap)conn_to_ntuple)debug)isfile_strict)memoize_when_activated)parse_environ_block)usage_percent                     	   
   )ppidruideuidsuidrgidegidsgidttynrctimestatusname)cpuutimecpustimerssvmspfaultspageins
numthreadsvolctxsw	scputimesusernicesystemidlesvmemtotalZ	availablepercentusedfreeactiveinactivewiredpmemr(   r)   r*   r+   pfullmem)ussc            	   	   C   sT   t  \} }}}}}|| }|| }||8 }t| | | dd}t| |||||||S )z&System virtual memory as a namedtuple.r   Zround_)cextZvirtual_memr   r3   )	r4   r8   r9   r:   r7   ZspeculativeZavailr6   r5    r@   3./venv/lib/python3.8/site-packages/psutil/_psosx.pyvirtual_memoryo   s    rB   c                  C   s4   t  \} }}}}t|| dd}t| |||||S )z=Swap system memory as a (total, used, free, sin, sout) tuple.r   r>   )r?   Zswap_memr   r   Zsswap)r4   r6   r7   ZsinZsoutr5   r@   r@   rA   swap_memory~   s    rC   c                  C   s   t  \} }}}t| |||S )z(Return system CPU times as a namedtuple.)r?   	cpu_timesr.   )r/   r0   r1   r2   r@   r@   rA   rD      s    rD   c                  C   s:   g } t  D ](}|\}}}}t||||}| | q| S )z)Return system CPU times as a named tuple.)r?   per_cpu_timesr.   append)retZcpu_tr/   r0   r1   r2   itemr@   r@   rA   rE      s    rE   c                   C   s   t  S )z0Return the number of logical CPUs in the system.)r?   cpu_count_logicalr@   r@   r@   rA   rI      s    rI   c                   C   s   t  S )z-Return the number of CPU cores in the system.)r?   cpu_count_coresr@   r@   r@   rA   rJ      s    rJ   c                  C   s"   t  \} }}}}t| |||S N)r?   	cpu_statsr   Z	scpustats)Zctx_switchesZ
interruptsZsoft_interruptsZsyscallsZ_trapsr@   r@   rA   rL      s       rL   c                  C   s   t  \} }}t| ||gS )zReturn CPU frequency.
        On macOS per-cpu frequency is not supported.
        Also, the returned frequency never changes, see:
        https://arstechnica.com/civis/viewtopic.php?f=19&t=465002.
        )r?   cpu_freqr   Zscpufreq)ZcurrZmin_Zmax_r@   r@   rA   rM      s    rM   Fc           	      C   sj   g }t  }|D ]T}|\}}}}|dkr,d}| sJtj|rtj|sJqt||||}|| q|S )z8Return mounted disk partitions as a list of namedtuples.Znone )	r?   disk_partitionsospathisabsexistsr   Z	sdiskpartrF   )	allretlistZ
partitions	partitionZdeviceZ
mountpointZfstypeZoptsntupler@   r@   rA   rO      s    rO   c                  C   sd   zt  \} }}W n tk
r(   Y dS X |dk}|r>tj}n|dkrNtj}n|d }t| ||S )zReturn battery information.Nr   <   )r?   sensors_batteryNotImplementedErrorr   ZPOWER_TIME_UNLIMITEDZPOWER_TIME_UNKNOWNZsbattery)r5   ZminsleftZpower_pluggedZsecsleftr@   r@   rA   rZ      s    rZ   inetc              	   C   sl   g }t  D ]\}zt|| }W n tk
r8   Y q
Y q
X |r
|D ]"}t||g }|tj|  qBq
|S )z System-wide network connections.)pidsProcessnet_connectionsr	   listrF   r   Zsconn)kindrG   pidZconscr@   r@   rA   r_      s    

r_   c            
      C   s   t   } i }| D ]}z&t|}t|}t|\}}W n0 tk
rl } z|jtjkr\ W 5 d}~X Y qX t	t
drt
|}d|}d|k}	t
|	||||||< q|S )z)Get NIC stats (isup, duplex, speed, mtu).N	NicDuplex,running)net_io_counterskeys
cext_posixZ
net_if_mtuZnet_if_flagsZnet_if_duplex_speedOSErrorerrnoZENODEVhasattrr   rd   joinZ	snicstats)
namesrG   r%   ZmtuflagsZduplexZspeederrZoutput_flagsZisupr@   r@   rA   net_if_stats  s,    





    
rq   c                   C   s   t  S )z:The system boot time expressed in seconds since the epoch.)r?   	boot_timer@   r@   r@   rA   rr      s    rr   zignoring exception on import: c                 C   sN   t dkr| S t t  }|dks*t|dk r.| S td |dk rF| | S | | S )z!Account for system clock updates.r   r   z9system clock was updated; adjusting process create_time())INIT_BOOT_TIMErr   absr   )r#   Zdiffr@   r@   rA   adjust_proc_create_time-  s    
ru   c            	      C   s\   g } t  }|D ]F}|\}}}}}|dkr,q|s2qt||p>d|pDd||}| | q| S )z:Return currently connected users as a list of namedtuples.~N)ri   usersr   ZsuserrF   )	rU   rawlistrH   r/   ZttyZhostnameZtstamprb   ntr@   r@   rA   rw   <  s    rw   c                  C   sd   t  } d| kr`ztd  | dd W n2 tk
r@   Y n  tk
r^   | dd Y nX | S Nr   )r?   r]   r^   create_timeinsertr	   r   )Zlsr@   r@   rA   r]   P  s    r]   c                 C   s:   zt | td  }|t jkW S  tk
r4   Y dS X d S )Nr$   F)r?   proc_kinfo_oneshotkinfo_proc_mapSZOMBrj   )rb   str@   r@   rA   	is_zombiec  s
    r   c                    s   t   fdd}|S )z`Decorator which translates bare OSError exceptions into
    NoSuchProcess and AccessDenied.
    c              
      s   | j | j| j  }}}z | f||W S  tk
rl } z&t|rPt||||t|||W 5 d }~X Y n. tk
r } zt|||W 5 d }~X Y nX d S rK   )	rb   _ppid_nameProcessLookupErrorr   r
   r	   PermissionErrorr   )selfargskwargsrb   r   r%   rp   funr@   rA   wrapperp  s    z wrap_exceptions.<locals>.wrapper)	functoolswraps)r   r   r@   r   rA   wrap_exceptionsk  s    r   c                   @   sn  e Zd ZdZddddgZdd Zeedd	 Zeed
d Z	dd Z
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d"d# Zed$d% Zed&d' ZedAd)d*Zed+d, Zed-d. Zed/d0 ZedBd2d3Zed4d5 ZedCd7d8Zed9d: Zed;d< Z ed=d> Z!ed?d@ Z"d6S )Dr^   z1Wrapper class around underlying C implementation._cacher   r   rb   c                 C   s   || _ d | _d | _d S rK   )rb   r   r   )r   rb   r@   r@   rA   __init__  s    zProcess.__init__c                 C   s$   t | j}t|ttks t|S rK   )r?   r}   rb   lenr~   AssertionErrorr   rG   r@   r@   rA   _get_kinfo_proc  s    zProcess._get_kinfo_procc                 C   s$   t | j}t|ttks t|S rK   )r?   Zproc_pidtaskinfo_oneshotrb   r   pidtaskinfo_mapr   r   r@   r@   rA   _get_pidtaskinfo  s    zProcess._get_pidtaskinfoc                 C   s   | j |  | j|  d S rK   )r   Zcache_activater   r   r@   r@   rA   oneshot_enter  s    zProcess.oneshot_enterc                 C   s   | j |  | j|  d S rK   )r   Zcache_deactivater   r   r@   r@   rA   oneshot_exit  s    zProcess.oneshot_exitc                 C   s(   |   td  }|d k	r|S t| jS )Nr%   )r   r~   r?   Z	proc_namerb   )r   r%   r@   r@   rA   r%     s    zProcess.namec                 C   s   t | jS rK   )r?   Zproc_exerb   r   r@   r@   rA   exe  s    zProcess.exec                 C   s   t | jS rK   )r?   Zproc_cmdlinerb   r   r@   r@   rA   cmdline  s    zProcess.cmdlinec                 C   s   t t| jS rK   )r   r?   Zproc_environrb   r   r@   r@   rA   environ  s    zProcess.environc                 C   s   |   td  | _| jS )Nr   )r   r~   r   r   r@   r@   rA   r     s    zProcess.ppidc                 C   s   t | jS rK   )r?   Zproc_cwdrb   r   r@   r@   rA   cwd  s    zProcess.cwdc                 C   s.   |   }t|td  |td  |td  S )Nr   r   r   r   r   Zpuidsr~   r   Zrawtupler@   r@   rA   uids  s    


zProcess.uidsc                 C   s.   |   }t|td  |td  |td  S )Nr   r    r!   r   r   r@   r@   rA   gids  s    


zProcess.gidsc                 C   s>   |   td  }t }z
|| W S  tk
r8   Y d S X d S )Nr"   )r   r~   r   Zget_terminal_mapKeyError)r   Ztty_nrZtmapr@   r@   rA   terminal  s    
zProcess.terminalc                 C   s6   |   }t|td  |td  |td  |td  S )Nr(   r)   r*   r+   )r   r;   r   r   r@   r@   rA   memory_info  s    



zProcess.memory_infoc                 C   s"   |   }t| j}t||f  S rK   )r   r?   Zproc_memory_ussrb   r<   )r   Z	basic_memr=   r@   r@   rA   memory_full_info  s    zProcess.memory_full_infoc                 C   s(   |   }t|td  |td  ddS )Nr&   r'   g        )r   r   Z	pcputimesr   r   r@   r@   rA   rD     s    

zProcess.cpu_timesFc                 C   s    |   td  }|st|}|S )Nr#   )r   r~   ru   )r   	monotonicr#   r@   r@   rA   r{     s    zProcess.create_timec                 C   s   |   td  }t|dS )Nr-   r   )r   r   r   Zpctxsw)r   Zvolr@   r@   rA   num_ctx_switches  s    zProcess.num_ctx_switchesc                 C   s   |   td  S )Nr,   )r   r   r   r@   r@   rA   num_threads   s    zProcess.num_threadsc                 C   sN   | j dkrg S g }t| j }|D ]&\}}t|r"t||}|| q"|S rz   )rb   r?   Zproc_open_filesr   r   Z	popenfilerF   )r   filesrx   rQ   fdrW   r@   r@   rA   
open_files  s    
zProcess.open_filesr\   c              	   C   s\   t | \}}t| j||}g }|D ]2}|\}}}	}
}}t|||	|
||t}|| q$|S rK   )r   r?   Zproc_net_connectionsrb   r   TCP_STATUSESrF   )r   ra   Zfamiliestypesrx   rG   rH   r   ZfamtypeZladdrZraddrr$   ry   r@   r@   rA   r_     s           zProcess.net_connectionsc                 C   s   | j dkrdS t| j S rz   )rb   r?   Zproc_num_fdsr   r@   r@   rA   num_fds  s    
zProcess.num_fdsNc                 C   s   t | j|| jS rK   )r   Zwait_pidrb   r   )r   timeoutr@   r@   rA   wait#  s    zProcess.waitc                 C   s   t | jS rK   )ri   getpriorityrb   r   r@   r@   rA   nice_get'  s    zProcess.nice_getc                 C   s   t | j|S rK   )ri   setpriorityrb   )r   valuer@   r@   rA   nice_set+  s    zProcess.nice_setc                 C   s   |   td  }t|dS )Nr$   ?)r   r~   PROC_STATUSESget)r   coder@   r@   rA   r$   /  s    zProcess.statusc                 C   s<   t | j}g }|D ]"\}}}t|||}|| q|S rK   )r?   Zproc_threadsrb   r   ZpthreadrF   )r   rx   rU   Z	thread_idutimeZstimerW   r@   r@   rA   threads5  s    zProcess.threads)F)r\   )N)#__name__
__module____qualname____doc__	__slots__r   r   r   r   r   r   r   r%   r   r   r   r   r   r   r   r   r   r   rD   r{   r   r   r   r_   r   r   r   r   r$   r   r@   r@   r@   rA   r^     sr   









	









r^   )F)r\   )cr   rk   r   rP   collectionsr   rN   r   r   r   r?   r   ri   r   r	   r
   r   r   r   r   r   r   r   Z__extra__all__ZgetpagesizeZPAGESIZEZAF_LINKZTCPS_ESTABLISHEDZCONN_ESTABLISHEDZTCPS_SYN_SENTZCONN_SYN_SENTZTCPS_SYN_RECEIVEDZ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_NONEr   ZSIDLZSTATUS_IDLEZSRUNZSTATUS_RUNNINGZSSLEEPZSTATUS_SLEEPINGZSSTOPZSTATUS_STOPPEDr   ZSTATUS_ZOMBIEr   dictr~   r   r.   r3   r;   _fieldsr<   rB   rC   rD   rE   rI   rJ   rL   Zhas_cpu_freqrM   
disk_usageZdisk_io_countersrO   rZ   rg   Znet_if_addrsr_   rq   rr   rs   	Exceptionrp   ru   rw   r]   Z
pid_existsr   r   r^   r@   r@   r@   rA   <module>   s                     
  	
	



