o
    `۷i!                     @   s  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d Zed	d
 Zedd Zed!dee fd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dZd!ddZdd Zdd  ZdS )#    N)defaultdict
namedtuple)CallableListOptional)DeveloperAPIF        c                 C   sR   t rdS | tvrt|  t adS tr't t dkr't  t 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key r   D/home/ubuntu/vllm_env/lib/python3.10/site-packages/ray/util/debug.pylog_once   s   
r   c                   C      da dS )z-Make log_once() return False in this process.TN)r
   r   r   r   r   disable_log_once_globally3      r   c                   C   r   )z9Make log_once() periodically return True in this process.TN)r   r   r   r   r   enable_periodic_logging;   r   r   r   c                 C   s"   | du r
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   r   r   r   reset_log_onceC   s   r   Suspect	tracebackmemory_increaseslopervaluehist   descinitcoderepeatsmax_num_trialsreturnc                 C   s  dd }t  }g }t|D ]}td tt}	||d  }
t|  d|
 d |dur/|  t|
D ]}|| t  |  t  t	|	| q3td |
  |
  t|	}t|d	d
 ddD ]}t|dkrst| td ||j || qct  t|dkrtt| d t|D ]"\}}|dkr nt| d|jd  d|j d|j d|j 	 qqtd  t|dkrt|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                 S   sB   | d d dkrt d| d d rdnd| d  dd	d
 d S d S )Nr#   
   r   .d     
T)endflush)print)ir   r   r   _i_print~   s   .z2_test_some_code_for_memory_leaks.<locals>._i_print   r#   r.   z times.Nr/   c                 S   s   | j S N)r   )sr   r   r   <lambda>   s    z2_test_some_code_for_memory_leaks.<locals>.<lambda>T)r   reverser   z-> added to retry listz suspects found. Top-ten:r*   z) line=z mem-increase=zB slope=zB/detection rval=z(No remaining suspects found -> returning)setrangetracemallocstartr   listr2   gccollect_take_snapshotr   _find_memory_leaks_in_tablesortedlen_pprint_suspectr   r   appendstop	enumerater   r    r!   )r$   r%   r&   r'   r(   r4   
suspicioussuspicious_statstrialtableactual_repeatsr3   suspectssuspectr7   r   r   r    _test_some_code_for_memory_leaksf   sZ   
rQ   c                 C   sH   t  }|d}|d d D ]}|r|j|v r!| |j |j qd S )Nr   r,   )r=   take_snapshot
statisticsr   rG   size)rM   rJ   snapshot	top_statsstatr   r   r   rB      s   
rB   c           	         s  dd l }dd l}g }|  D ]w\}}|d |d  }|dkrqt|d  tjdkr,dnd}t fddd	d
dtd|t	d fD rEq|j
j|t|||d}|dkr|jdkrc|jdksw|jdkrm|jdksw|jdkr|jdkr|t|||j|j|d q|S )Nr   r:   r   ntz\\/c                 3   s    | ]}| v V  qd S r6   r   ).0r7   	top_stackr   r   	<genexpr>   s
    
z._find_memory_leaks_in_table.<locals>.<genexpr>r=   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arangerE   arrayr    r!   rG   r   )	rM   npscipyrO   r   r"   r   drive_separatorliner   r[   r   rC      sB   	 
rC   c                 C   sT   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):r/   zIncrease total=BzSlope=z B/detectionzRval=)r2   joinr   formatr   r    r!   )rP   r   r   r   rF   
  s   rF   r6   )r#   )r@   re   rh   r   r=   collectionsr   r   typingr   r   r   ray.util.annotationsr   r;   r   r
   r   r   r   r   r   rd   r   r   intrQ   rB   rC   rF   r   r   r   r   <module>   sV    
!




`5