
    &`i                         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Z e j	        e
          Zd Zd ZddefdZ G d d	e          Z G d
 d          ZdS )    N)get_system_memoryc                 H    | j         }t          | d          r
|| j        z  }|S )zBGet the estimated non-shared memory usage from psutil memory_info.shared)rsshasattrr   )memory_infomems     o/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/_private/memory_monitor.pyget_rssr      s-    
/C{H%% "{!!J    c                 4    t          | d          r| j        S dS )zCGet the estimated shared memory usage from psutil virtual mem info.r   r   )r   r   )virtual_memorys    r
   
get_sharedr      s$     ~x(( $$qr   
   nc           	         g }t          j        ddg          D ]h}	 |                    t          |j        d                   |j        |j        d         f           D# t           j        $ r Y Ut           j        $ r Y ew xY wd}t          |d          d|          D ]^\  }}}|d	                    |t          |dz  d	          d
                    |          dd                                                   z  }_|S )zGet the top n memory usage of the process

    Params:
        n: Number of top n process memory usage to return.
    Returns:
        (str) The formatted string of top n process memory usage.
    r   cmdlinezPID	MEM	COMMANDT)reverseNz
{}	{}GiB	{}   @    d   )psutilprocess_iterappendr   infopidNoSuchProcessAccessDeniedsortedformatroundjoinstrip)r   
proc_statsprocproc_strr   r   r   s          r
   get_top_n_memory_usager(   #   s>    J#]I$>??  	=122DHdi	>RS    # 	 	 	 H" 
	 
	 
	 H
	 #H#J===bqbA 
 
S'%,,sg**CHHW,=,=dsd,C,I,I,K,K
 
 	
 Os   A AA?.A?>A?c                   *    e Zd Zd Zed             ZdS )RayOutOfMemoryErrorc                 <    t                               | |           d S )N)	Exception__init__)selfmsgs     r
   r-   zRayOutOfMemoryError.__init__J   s    4%%%%%r   c           	         t          d          }d                    t          d|z                      d                    t          j                    t          | d          t          |d                    z   d| z   d                    t          t          t          j                              d	z  d                    z   d
z   S )Nr   )r   zMore than {}% of the memory on r   znode {} is used ({} / {} GB). r   z"The top 10 memory consumers are:

z0

In addition, up to {} GiB of shared memory is r   zcurrently being used by the Ray object store.
---
--- Tip: Use the `ray memory` command to list active objects in the cluster.
--- To disable OOM exceptions, set RAY_DISABLE_MEMORY_MONITOR=1.
---
)	r(   r!   intplatformnoder"   r   r   r   )used_gbtotal_gb	thresholdr'   s       r
   get_messagezRayOutOfMemoryError.get_messageM   s    )B///-44Sy5I5IJJ.55w!2!2E(A4F4F  @X??	@
 CIIj!6!8!899WEqII 33	
r   N)__name__
__module____qualname__r-   staticmethodr7    r   r
   r*   r*   I   s>        & & & 
 
 \
 
 
r   r*   c                   &    e Zd ZdZddZd Zd ZdS )	MemoryMonitora;  Helper class for raising errors on low memory.

    This presents a much cleaner error message to users than what would happen
    if we actually ran out of memory.

    The monitor tries to use the cgroup memory limit and usage if it is set
    and available so that it is more reasonable inside containers. Otherwise,
    it uses `psutil` to check the memory usage.

    The environment variable `RAY_MEMORY_MONITOR_ERROR_THRESHOLD` can be used
    to overwrite the default error_threshold setting.

    Used by test only. For production code use memory_monitor.cc
    ffffff?   c                     || _         d| _        	 t          t          j        d                    | _        n# t          t          f$ r
 || _        Y nw xY w	 t          dd          5 }t          |
                                          dz  | _        d d d            n# 1 swxY w Y   n$# t          $ r t          j        dz  | _        Y nw xY wt          st                               d           dt          j        v pdt          j        v | _        d S )	Nr   "RAY_MEMORY_MONITOR_ERROR_THRESHOLDz+/sys/fs/cgroup/memory/memory.limit_in_bytesrbr   zWARNING: Not monitoring node memory since `psutil` is not installed. Install this with `pip install psutil` to enable debugging of memory-related crashes. RAY_DEBUG_DISABLE_MEMORY_MONITORRAY_DISABLE_MEMORY_MONITOR)check_intervallast_checkedfloatosgetenverror_threshold
ValueError	TypeErroropenr1   readcgroup_memory_limit_gbIOErrorsysmaxsizer   loggerwarningenvirondisabled)r.   rK   rF   fs       r
   r-   zMemoryMonitor.__init__q   s~    -	3#(	>??$ $D   I& 	3 	3 	3#2D   	3	BCTJJ Ha.1!&&((mmw.G+H H H H H H H H H H H H H H H 	B 	B 	B*-+*AD'''	B 	NN7   /"*< :+rz9 	sE   &7 AAB( &*BB( B  B( #B $B( (C	C	c                 R    ddl m} t                      dz  } |            dz  }||fS )Nr   )get_used_memoryr   )ray._private.utilsrZ   r   )r.   rZ   r5   r4   s       r
   get_memory_usagezMemoryMonitor.get_memory_usage   sA    666666$&&'2!/##w/  r   c                 z   | j         rd S t          j                    | j        z
  | j        k    rt          j                    | _        |                                 \  }}||| j        z  k    r.t          t                              ||| j                            t          	                    d| d|            d S d S )NzMemory usage is z / )
rW   timerG   rF   r\   rK   r*   r7   rT   debug)r.   r4   r5   s      r
   raise_if_low_memoryz!MemoryMonitor.raise_if_low_memory   s    = 	F9;;**T-@@@ $	D $ 5 5 7 7GXD$8888)'334+?    FFFHFFGGGGG A@r   N)r?   r@   )r8   r9   r:   __doc__r-   r\   r`   r<   r   r
   r>   r>   a   sU         
 
 
 
:! ! !H H H H Hr   r>   )r   )loggingrI   r2   rR   r^   rayray._common.utilsr   r   	getLoggerr8   rT   r   r   r1   r(   r,   r*   r>   r<   r   r
   <module>rf      s    				  



  



 0 / / / / / 		8	$	$    # #c # # # #L
 
 
 
 
) 
 
 
0DH DH DH DH DH DH DH DH DH DHr   