o
    `qiw                     @   s  d 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 g Zere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)iZ*n=ere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iZ*nerejej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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jCejDejEiZFeG ZHejIZIeJedZKeLdi dddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFZMedGg dHZNedIg dJZOedKg dLZPePZQedMg dNZRedOdPZSedQdRZTeredSg dTZUnedSg dUZUdVdW ZVdXdY ZWdZd[ ZXd\d] ZYd^d_ ZZeserd`da Z[ndbda Z[dcdd Z\erdedf Z]nerdgdf Z]ddidjZ^ej_Z_ej`Z`ejaZaejbZbdkdl Zcdmdn Zder dodp Zedqdr Zfdsdt Zger-zeg ZhW n eiy( Zj zeduej dZhW Y dZj[jndZj[jww dvdw Zkdxdy Zledzd{ Zmd|d} ZnerCd~d ZonerKdd ZonejoZodd Zpejqdd ZrG dd dZsdS )z5FreeBSD, OpenBSD and NetBSD platforms implementation.    N)defaultdict)
namedtuple)ElementTree   )_common)_psposix)_psutil_bsd)FREEBSD)NETBSD)OPENBSD)AccessDenied)NoSuchProcess)ZombieProcess)	conn_tmap)conn_to_ntuple)debug)memoize)memoize_when_activated)usage_percentproc_num_threadsppidstatusreal_uid   effective_uid   	saved_uid   real_gid   effective_gid   	saved_gid   ttynr   create_time	   ctx_switches_vol
   ctx_switches_unvol   read_io_count   write_io_count   	user_time   sys_time   ch_user_time   ch_sys_time   rss   vms   memtext   memdata   memstack   cpunum   name   svmem)total	availablepercentusedfreeactiveinactivebufferscachedsharedwired	scputimesusernicesystemidleirqpmem)r8   r:   textdatastack	pcputimes)rT   rV   children_userchildren_systempmmap_grouped*path rss, private, ref_count, shadow_count	pmmap_ext6addr, perms path rss, private, ref_count, shadow_countsdiskio)
read_countwrite_count
read_byteswrite_bytesZ	read_timeZ
write_timeZ	busy_time)re   rf   rg   rh   c                  C   s   t  } trP| \}}}}}}tdd,}|D ]!}|dr(t| d d }	q|dr7t| d d }
qW d    n1 sBw   Y  || }|| }n| \}}}}}}}	}
|| | }|| | }t|| |dd}t||||||||	||
|S )Nz/proc/meminforbs   Buffers:r   i   s
   MemShared:Zround_)	cextZvirtual_memr
   open
startswithintsplitr   rF   )ZmemrG   rK   rL   rM   rQ   rO   flinerN   rP   rJ   ZavailrI    rr   T/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/psutil/_psbsd.pyvirtual_memory   s>   


rt   c                  C   s4   t  \} }}}}t|| dd}t| |||||S )z@System swap memory as (total, used, free, sin, sout) namedtuple.r   rj   )rk   Zswap_memr   r   Zsswap)rG   rJ   rK   sinZsoutrI   rr   rr   rs   swap_memory   s   rv   c                  C   s"   t  \} }}}}t| ||||S )z,Return system per-CPU times as a namedtuple.)rk   	cpu_timesrR   rS   rr   rr   rs   rw      s   rw   c                  C   s>   g } t  D ]}|\}}}}}t|||||}| | q| S )z(Return system CPU times as a namedtuple.)rk   per_cpu_timesrR   append)retZcpu_trT   rU   rV   rW   rX   itemrr   rr   rs   rx      s   rx   c                   C      t  S )z0Return the number of logical CPUs in the system.)rk   cpu_count_logicalrr   rr   rr   rs   r}         r}   c                   C   s   t  dkrdS d S )Nr   )r}   rr   rr   rr   rs   cpu_count_cores      r   c                  C   s   d} t  }|dur5|d}|dkr5|d|d  }t|}zt|dp)d} W |  n|  w | s>t dkr>dS | S )z-Return the number of CPU cores in the system.Nz	</groups>r'   zgroup/children/group/cpur   )	rk   Zcpu_topologyrfindr   
fromstringlenfindallclearr}   )rz   sindexrootrr   rr   rs   r     s   


c            	      C   s   t rt \} }}}}nBtrAt \} }}}}}}tdd}|D ]}|dr0t| d }q!W d   n1 s;w   Y  ntrNt \} }}}}}}t	
| |||S )z*Return various CPU stats as a named tuple.z
/proc/statri   s   intrr   N)r	   rk   	cpu_statsr
   rl   rm   rn   ro   r   r   Z	scpustats)	ZctxswZintrsZ
soft_intrsZsyscallsZ_trapsZ_faults_forksrp   rq   rr   rr   rs   r      s"   
r   c               
   C   s   g } t  }t|D ]]}z	t|\}}W n	 ty   Y q	w |r\zt|dd dd }W n ttfy=   d}Y nw zt|dd dd }W n ttfy[   d}Y nw | 	t
||| q	| S )zReturn frequency metrics for CPUs. As of Dec 2018 only
        CPU 0 appears to be supported by FreeBSD and all other cores
        match the frequency of CPU 0.
         r   /r   N)r}   rangerk   cpu_freqNotImplementedErrorrn   ro   
IndexError
ValueErrorry   r   scpufreq)rz   num_cpuscpucurrentZavailable_freqZmin_freqZmax_freqrr   rr   rs   r   B  s*     r   c                  C   s   t t } t| ddgS )Ng        )floatrk   r   r   r   )currrr   rr   rs   r   \  s   Fc           	      C   s@   g }t  }|D ]}|\}}}}t||||}|| q|S )zReturn mounted disk partitions as a list of namedtuples.
    'all' argument is ignored, see:
    https://github.com/giampaolo/psutil/issues/906.
    )rk   disk_partitionsr   Z	sdiskpartry   )	allretlistZ
partitions	partitionZdeviceZ
mountpointZfstypeoptsntuplerr   rr   rs   r   f  s   r   c            
      C   s   t   } i }| D ]M}zt|}t|}t|\}}W n ty7 } z|jtjkr- W Y d}~q	d}~ww t	t
drBt
|}d|}d|v }	t
|	||||||< q	|S )z)Get NIC stats (isup, duplex, speed, mtu).N	NicDuplex,running)net_io_counterskeysrk   Z
net_if_mtuZnet_if_flagsZnet_if_duplex_speedOSErrorerrnoZENODEVhasattrr   r   joinZ	snicstats)
namesrz   rD   ZmtuflagsZduplexspeederrZoutput_flagsZisuprr   rr   rs   net_if_stats  s*   







r   c              
   C   s   t |  \}}t }trtd||}ntrtd| }nt||}|D ]}|\}}}}	}
}}t||||	|
|t|}|| q$t	|S )z System-wide network connections.r   )
r   setr   rk   net_connectionsr
   r   TCP_STATUSESaddlist)kindfamiliestypesrz   rawlistr{   fdfamtypeladdrraddrr   pidntrr   rr   rs   r     s   r   c                  C   sb   z	t  \} }}W n
 ty   Y dS w |dk}|rtj}n|dkr&tj}n|d }t| ||S )zReturn battery info.Nr   r   <   )rk   sensors_batteryr   r   ZPOWER_TIME_UNLIMITEDZPOWER_TIME_UNKNOWNZsbattery)rI   ZminsleftZpower_pluggedZsecsleftrr   rr   rs   r     s   r   c               	   C   st   t t} t }t|D ],}z!t|\}}|dkrd}d| }| d t|||| W q t	y7   Y qw | S )z?Return CPU cores temperatures if available, else an empty dict.r   NzCore Zcoretemp)
r   r   r}   r   rk   Zsensors_cpu_temperaturery   r   Zshwtempr   )rz   r   r   r   highrD   rr   rr   rs   sensors_temperatures  s   
r   c                   C   r|   )z:The system boot time expressed in seconds since the epoch.)rk   	boot_timerr   rr   rr   rs   r     r~   r   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 r#| | S | | S )z!Account for system clock updates.r   r   z9system clock was updated; adjusting process create_time())INIT_BOOT_TIMEr   absr   )ctimediffrr   rr   rs   adjust_proc_create_time  s   
r   c            	      C   sR   g } t  }|D ]}|\}}}}}|dkrqt||pd|||}| | q| S )z:Return currently connected users as a list of namedtuples.~N)rk   usersr   Zsuserry   )	r   r   r{   rT   ttyhostnameZtstampr   r   rr   rr   rs   r     s   r   c                   C   s:   z	t d  W dS  ty   Y dS  ty   Y dS w )Nr   FT)ProcessrD   r   r   rr   rr   rr   rs   _pid_0_exists  s   r   c                  C   s*   t  } trd| vrt r| dd | S )z7Returns a list of PIDs currently running on the system.r   )rk   pidsr   r   insert)rz   rr   rr   rs   r     s   r   c                 C   s   t | }|s| t v S dS )NTr   
pid_existsr   r   existsrr   rr   rs   r   #  s   

r   c                 C   s   t | }|s	dS | t v S )NFr   r   rr   rr   rs   r   .  s   

c                    s   t   fdd}|S )z`Decorator which translates bare OSError exceptions into
    NoSuchProcess and AccessDenied.
    c              
      s   | j | j| j}}}z | g|R i |W S  ty5 } zt|r+t||||t|||d }~w tyF } zt	|||d }~w tj
yY } zt||||d }~w tyv } z|dkrodt v rot	||||d d }~ww )Nr   )r   _ppid_nameProcessLookupErrorrk   proc_is_zombier   r   PermissionErrorr   ZZombieProcessErrorr   r   )selfargskwargsr   r   rD   r   funrr   rs   wrapperA  s(   
z wrap_exceptions.<locals>.wrapper)	functoolswraps)r   r   rr   r   rs   wrap_exceptions<  s   r   c              
   c   s    | j | j| j}}}zdV  W dS  ttfy3 } zt| j r)t||||t|||d}~w t	yD } zt
|||d}~ww )z8Same as above, for routines relying on reading /proc fs.N)r   r   r   r   FileNotFoundErrorrk   r   r   r   r   r   )instr   rD   r   r   rr   rr   rs   wrap_exceptions_procfsV  s   r   c                   @   s  e Zd ZdZg dZdd Z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r`edd  Zed!d" ZeZedKd$d%Zed&d' Zed(d) Zed*d+ ZedLd-d.ZedMd0d1Zed2d3 Zed4d5 Z ed6d7 Z!ed8d9 Z"ed:d; Z#e$d<d=Z%e$d<d>Z&ed?d@ Z'edAdB Z(eredCdD Z)edEdF Z*edGdH Z+edMdIdJZ,d/S d/S )Nr   z1Wrapper class around underlying C implementation.)_cacher   r   r   c                 C   s   || _ d | _d | _d S N)r   r   r   )r   r   rr   rr   rs   __init__m  s   
zProcess.__init__c                 C   s   t | j dS )z+Raise NSP if the process disappeared on us.N)rk   	proc_namer   r   rr   rr   rs   _assert_aliver  s   zProcess._assert_alivec                 C   s$   t | j}t|ttksJ |S )z;Retrieves multiple process info in one shot as a raw tuple.)rk   Zproc_oneshot_infor   r   kinfo_proc_mapr   rz   rr   rr   rs   oneshotx  s   zProcess.oneshotc                 C      | j |  d S r   )r   Zcache_activater   rr   rr   rs   oneshot_enter     zProcess.oneshot_enterc                 C   r   r   )r   Zcache_deactivater   rr   rr   rs   oneshot_exit  r   zProcess.oneshot_exitc                 C   s(   |   td  }|d ur|S t| jS )NrD   )r   r   rk   r   r   )r   rD   rr   rr   rs   rD     s   zProcess.namec                 C   s   t r| jdkr	dS t| jS tr8| jdkrdS t|  td| j dW  d    S 1 s1w   Y  d S dd l}| 	 }|rK|
|d pJdS dS )Nr    z/proc/z/exe)r	   r   rk   Zproc_exer
   r   osreadlinkshutilcmdlinewhich)r   r   r   rr   rr   rs   exe  s   


$zProcess.exec              
   C   s   t r	| jdkr	g S trZzt| jW S  tyY } z;|jtjkrT| j| j| j	}}}t
| jr8t||||t| jsDt||||td|d g W  Y d }~S  d }~ww t| jS )Nr   z	ignoring z and returning an empty list)r   r   r
   rk   Zproc_cmdliner   r   EINVALr   r   r   r   r   r   r   )r   r   r   rD   r   rr   rr   rs   r     s$   
zProcess.cmdlinec                 C      t | jS r   )rk   Zproc_environr   r   rr   rr   rs   environ     zProcess.environc                 C   s8   |   td  }t }z|| W S  ty   Y d S w )Nr$   )r   r   r   Zget_terminal_mapKeyError)r   Ztty_nrZtmaprr   rr   rs   terminal  s   
zProcess.terminalc                 C   s   |   td  | _| jS )Nr   )r   r   r   r   rr   rr   rs   r     s   zProcess.ppidc                 C   .   |   }t|td  |td  |td  S )Nr   r   r   )r   r   Zpuidsr   r   Zrawtuplerr   rr   rs   uids     


zProcess.uidsc                 C   r   )Nr   r    r"   )r   r   Zpgidsr   r   rr   rr   rs   gids  r  zProcess.gidsc                 C   s8   |   }t|td  |td  |td  |td  S )Nr0   r2   r4   r6   )r   r   r]   r   r   rr   rr   rs   rw     s   



zProcess.cpu_timesc                 C   s   |   td  S )NrB   )r   r   r   rr   rr   rs   cpu_num  s   zProcess.cpu_numc                 C   s@   |   }t|td  |td  |td  |td  |td  S )Nr8   r:   r<   r>   r@   )r   rY   r   r   rr   rr   rs   memory_info  s   




zProcess.memory_infoFc                 C   s$   |   td  }tr|st|}|S )Nr&   )r   r   r
   r   )r   	monotonicr   rr   rr   rs   r&     s   zProcess.create_timec                 C   s   t rt| jS t|  S r   )HAS_PROC_NUM_THREADSrk   r   r   r   threadsr   rr   rr   rs   num_threads	  s   zProcess.num_threadsc                 C   s$   |   }t|td  |td  S )Nr(   r*   )r   r   Zpctxswr   r   rr   rr   rs   num_ctx_switches  s
   

zProcess.num_ctx_switchesc                 C   sH   t | j}g }|D ]\}}}t|||}|| q
tr"|   |S r   )rk   Zproc_threadsr   r   Zpthreadry   r   r   )r   r   r   	thread_idutimeZstimer   rr   rr   rs   r    s   zProcess.threadsinetc              	   C   s   t | \}}g }trt| j|}ntrt| j||}nt| j||}|D ](}|d d \}}}	}
}}tr@||vs?|	|vr@q't|||	|
||t	}|
| q'|   |S )Nr!   )r   r
   rk   r   r   r   Zproc_net_connectionsr	   r   r   ry   r   )r   r   r   r   rz   r   r{   r   r   r   r   r   r   r   rr   rr   rs   r   %  s$   zProcess.net_connectionsNc                 C   s   t | j|| jS r   )r   Zwait_pidr   r   )r   timeoutrr   rr   rs   wait>  r   zProcess.waitc                 C   r   r   )rk   Zproc_priority_getr   r   rr   rr   rs   nice_getB  r   zProcess.nice_getc                 C   s   t | j|S r   )rk   Zproc_priority_setr   )r   valuerr   rr   rs   nice_setF  s   zProcess.nice_setc                 C   s   |   td  }t|dS )Nr   ?)r   r   PROC_STATUSESget)r   coderr   rr   rs   r   J  s   zProcess.statusc                 C   s(   |   }t|td  |td  ddS )Nr,   r.   r   )r   r   Zpior   r   rr   rr   rs   io_countersP  s   

zProcess.io_countersc                 C   s   t r	| jdkr	dS t| jS )z)Return process current working directory.r   r   )r   r   rk   Zproc_cwdr   rr   rr   rs   cwdZ  s   zProcess.cwdmmapra   rc   c                 C   s   t | j}dd |D S )z8Return files opened by process as a list of namedtuples.c                 S   s   g | ]
\}}t ||qS rr   )r   Z	popenfile).0pathr   rr   rr   rs   
<listcomp>n  s    z&Process.open_files.<locals>.<listcomp>)rk   Zproc_open_filesr   )r   r   rr   rr   rs   
open_filesj  s   zProcess.open_filesc                 C   s   t | j}tr|   |S )z=Return the number of file descriptors opened by this process.)rk   Zproc_num_fdsr   r
   r   r   rr   rr   rs   num_fdsp  s   zProcess.num_fdsc                 C   r   r   )rk   Zproc_cpu_affinity_getr   r   rr   rr   rs   cpu_affinity_get|  r   zProcess.cpu_affinity_getc              
   C   s   t ttt }|D ]}||vrd|d| d}t|qz
t| j| W d S  tyV } z!|j	t	j
t	jhv rQ|D ]}||vrPd|d| d}t||q< d }~ww )Nzinvalid CPU z (choose between ))r   r   r   rx   r   rk   Zproc_cpu_affinity_setr   r   r   r   ZEDEADLK)r   ZcpusZallcpusr   msgr   rr   rr   rs   cpu_affinity_set  s*   
zProcess.cpu_affinity_setc                 C   r   r   )rk   Zproc_memory_mapsr   r   rr   rr   rs   memory_maps  r   zProcess.memory_mapsc                 C   sN   |d u rt | j|S t|dkrd|}t||\}}t | j|||S )Nr   z2second argument must be a (soft, hard) tuple, got )rk   Zproc_getrlimitr   r   r   Zproc_setrlimit)r   resourceZlimitsr   ZsoftZhardrr   rr   rs   rlimit  s   zProcess.rlimitF)r  r   )-__name__
__module____qualname____doc__	__slots__r   r   r   r   r   r   r   rD   r   r   r   r   r   r   r  rw   r	   r  r  Zmemory_full_infor&   r  r	  r  r   r  r  r  r   r  r  r   Znt_mmap_groupedZnt_mmap_extr  r  r  r!  r"  r$  rr   rr   rr   rs   r   h  s    








	









	


	


r   rr   r%  )tr)  
contextlibr   r   r   collectionsr   r   Z	xml.etreer   r   r   r   r   rk   r	   r
   r   r   r   r   r   r   r   r   r   r   Z__extra__all__ZSIDLZSTATUS_IDLEZSRUNZSTATUS_RUNNINGZSSLEEPZSTATUS_SLEEPINGZSSTOPZSTATUS_STOPPEDZSZOMBZSTATUS_ZOMBIEZSWAITZSTATUS_WAITINGZSLOCKZSTATUS_LOCKEDr  ZSDEADZSTATUS_WAKINGZSONPROCZ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getpagesizePAGESIZEZAF_LINKr   r  dictr   rF   rR   rY   Zpfullmemr]   r`   rb   rd   rt   rv   rw   rx   r}   r   r   r   r   
disk_usageZdisk_io_countersr   Znet_if_addrsr   r   r   r   r   r   	Exceptionr   r   r   r   r   r   r   contextmanagerr   r   rr   rr   rr   rs   <module>   sn  	

	
$
/

 






	

