
    &`i!                        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mZ d dlm	Z	m
Z
mZ d dlmZ  e            Zdadadaed             Zed             Zed	             Zedd
ee         fd            Z e edg d                    Z	 ddedee	g df                  de	g df         dedede
e         fdZddZd Zd ZdS )    N)defaultdict
namedtuple)CallableListOptional)DeveloperAPIF        c                 0   t           rdS | t          vr/t                              |            t          j                    adS t
          rMt          j                    t          z
  dk    r.t                                           t          j                    adS dS )a  Returns True if this is the "first" call for a given key.

    Various logging settings can adjust the definition of "first".

    Example:

        .. testcode::

            import logging
            from ray.util.debug import log_once

            logger = logging.getLogger(__name__)
            if log_once("some_key"):
                logger.info("Some verbose logging statement")
    FT      N@)	_disabled_loggedaddtime_last_logged_periodic_logclearkeys    b/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/util/debug.pylog_oncer      s}    (  u	G		Cy{{t	 49;;5<<y{{uu    c                  
    da dS )z-Make log_once() return False in this process.TN)r    r   r   disable_log_once_globallyr   3   s    
 IIIr   c                  
    da dS )z9Make log_once() periodically return True in this process.TN)r   r   r   r   enable_periodic_loggingr   ;   s    
 MMMr   r   c                 t    | t                                            dS t                               |            dS )ziResets log_once for the provided key.

    If you don't provide a key, resets log_once for all keys.
    N)r   r   discardr   s    r   reset_log_oncer   C   s2     {r   Suspect	tracebackmemory_increaseslopervaluehist   descinitcoderepeatsmax_num_trialsreturnc                    d }t                      }g }t          |          D ]}t          j        d           t	          t
                    }	||dz   z  }
t          |  d|
 d           |
 |             t          |
          D ]M} ||           t          j                      |             t          j                     t          |	|           Nt          d           |
                                 |
                                 t          |	          }t          |d d	
          D ]b}t          |          dk    rt          |           t          d           |                    |j                   |                    |           ct          j                     t          |          dk    rut          t          |           d           t'          |          D ]D\  }}|dk    r n8t          | d|j        d          d|j         d|j         d|j         	           Et          d            t          |          dk    rt          |d                    |S )a  Runs given code (and init code) n times and checks for memory leaks.

    Args:
        desc: A descriptor of the test.
        init: Optional code to be executed initially.
        code: The actual code to be checked for producing memory leaks.
        repeats: How many times to repeatedly execute `code`.
        max_num_trials: The maximum number of trials to run. A new trial is only
            run, if the previous one produced a memory leak. For all non-1st trials,
            `repeats` calculates as: actual_repeats = `repeats` * (trial + 1), where
            the first trial is 0.

    Returns:
        A list of Suspect objects, describing possible memory leaks. If list
        is empty, no leaks have been found.
    c                 h    | dz   dz  dk    r%t          d| dz   dz  rdnd| dz    dd	
           d S d S )Nr'   
   r   .d     
T)endflush)print)is    r   _i_printz2_test_some_code_for_memory_leaks.<locals>._i_print~   sU    ER<1#!a%3A22MAMMMNNNNNN r      r'   r4   z times.Nr5   c                     | j         S N)r#   )ss    r   <lambda>z2_test_some_code_for_memory_leaks.<locals>.<lambda>   s	    a6G r   T)r   reverser   z-> added to retry listz suspects found. Top-ten:r0   z) line=z mem-increase=zB slope=zB/detection rval=z(No remaining suspects found -> returning)setrangetracemallocstartr   listr8   gccollect_take_snapshotr   _find_memory_leaks_in_tablesortedlen_pprint_suspectr   r"   appendstop	enumerater#   r$   r%   )r(   r)   r*   r+   r,   r:   
suspicioussuspicious_statstrialtableactual_repeatsr9   suspectssuspectr>   s                  r    _test_some_code_for_memory_leaksrX   f   s   0O O O
 J~&& 8 8"D!!
 !EAI./////000 DFFF~&& 	. 	.AHQKKK JLLLDFFFJLLL5*----d 	   .u55h,G,GQUVVV 	- 	-G:!##(((.///NN7,---##G,,,, z??QS__???@@@!"233  1r66E B BR B B@Q B BWB B78xB B    <=== q  (+,,,r   c                     t          j                    }|                    d          }|d d         D ]2}|r	|j        |v r%| |j                                     |j                   3d S )Nr"   r2   )rD   take_snapshot
statisticsr"   rN   size)rT   rQ   snapshot	top_statsstats        r   rI   rI      sz    (**H ##K00I $3$ 4 4 	4T^z99$.!((3334 4r   c                   	 dd l }dd l}g }|                                 D ]K\  }}|d         |d         z
  }|dk    rt          |d                   	t          j        dk    rdnd}t          	fddd	d
t          j        d|t                    dz   fD                       r|j
                            |                    t          |                    |                    |                    }|dk    rs|j        dk    r|j        dk    s,|j        dk    r|j        dk    s|j        dk    r<|j        dk    r1|                    t%          |||j        |j        |                     M|S )Nr   rA   r	   ntz\\/c              3       K   | ]}|v V  	d S r=   r   ).0r>   	top_stacks     r   	<genexpr>z._find_memory_leaks_in_table.<locals>.<genexpr>   s;       
 
 N
 
 
 
 
 
r   rD   pycharmzthirdparty_files/psutilz\.z.py)xyi  r   g      ?g      4@g?g      $@gffffff?r!   )numpyscipy.statsitemsstrosnameanyresub__name__stats
linregressarangerL   arrayr$   r%   rN   r    )
rT   npscipyrV   r"   r&   r#   drive_separatorlinere   s
            @r   rJ   rJ      s   H ;;== * *	4r(T!W, c!! 	"&&	$&GtOO&& 
 
 
 
 )uox885@	
 
 
 
 
 		  {%%		#d))(<(<%OO
 T!!Z$4;#6#6
T!!dkC&7&7
T!!dkD&8&8OO'$3*;     Or   c                 $   t          d           t          d                    | j                                                             t          d| j         d           t          d| j         d           t          d| j                    d S )NzMost suspicious memory allocation in traceback (only printing out this one, but all (less suspicious) suspects will be investigated as well):r5   zIncrease total=BzSlope=z B/detectionzRval=)r8   joinr"   formatr#   r$   r%   )rW   s    r   rM   rM   
  s    		3  
 
$))G%,,..
/
/000	
6G3
6
6
6777	
.7=
.
.
.///	
"'.
"
"#####r   r=   )r'   )rG   rn   rq   r   rD   collectionsr   r   typingr   r   r   ray.util.annotationsr   rB   r   r   r   r   r   r   r   rm   r   r    intrX   rI   rJ   rM   r   r   r   <module>r      s   				 				 				      / / / / / / / / + + + + + + + + + + - - - - - -
#%%	   B             ,J	
 	
 	
  4 ] ]
]
8BH%
&] 2t8
] 	]
 ] 
']] ] ] ]@4 4 4 42 2 2j	$ 	$ 	$ 	$ 	$r   