o
    
۾ij                     @   s   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 eeZG dd dZG dd	 d	ZdddZdddZded
efddZdee ded
efddZdS )    N)Counter)suppress)Any)init_loggerc                   @   s6   e Zd ZdZd	dedB ddfddZdefddZdS )
GCDebugConfigz
    Config for GC Debugger.
    - 0: disable GC debugger
    - 1: enable GC debugger with gc.collect elapsed times
    - '{"top_objects":5}': enable GC debugger with top 5 collected objects
    Ngc_debug_confreturnc                 C   s   d| _ d| _|r|dkrn-|dkrd| _ n%zt|}d| _ |dd| _W n ty9   d| _ tdtj	 Y nw t
dt|  d S )	NF01Ttop_objectsz!Failed to parse VLLM_GC_DEBUG(%s)zGC Debug Config. %s)enabledr   jsonloadsget	ExceptionloggererrorenvsVLLM_GC_DEBUGdebugstr)selfr   	json_conf r   G/home/ubuntu/.local/lib/python3.10/site-packages/vllm/utils/gc_utils.py__init__   s   
zGCDebugConfig.__init__c                 C   s   d| j  d| j S )Nzenabled:z,top_objects:)r   r   )r   r   r   r   __repr__*   s   zGCDebugConfig.__repr__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r      s    r   c                   @   s@   e Zd ZdZdeddfddZdedeeef ddfd	d
Z	dS )
GCDebuggerz
    Debugger for GC which logs helpful information for GC understanding.
    To enable, you should call maybe_attach_gc_debug_callback in the process.
    configr   Nc                 C   s    || _ t | _d| _d| _d S )Nr    )r$   timemonotonic_nsstart_time_nsnum_objectsgc_top_collected_objects)r   r$   r   r   r   r   4   s   

zGCDebugger.__init__phaseinfoc              	   C   s   | d}|du rdS |dkr(t | _t|}t|| _t|| j	j
| _dS |dkrQt | j d }td|t| dd| j|| jrLd	| j nd
 dS dS )zA
        Handles a GC event (e.g. GC start or GC finish)
        
generationNstartstopg    .AzSGC took %.3fms to complete. Collected %s objects (out of %d) in GC generation %d.%s	collected?z Top collected objects: 
r%   )r   r&   r'   r(   gcget_objectslenr)   !_compute_top_gc_collected_objectsr$   r   r*   r   r,   r   )r   r+   r,   r-   objects
elpased_msr   r   r   handle=   s.   




zGCDebugger.handle)
r   r    r!   r"   r   r   r   dictintr8   r   r   r   r   r#   .   s    "	r#   r   c                   C   s*   t d t d t d t   dS )z
    Freeze all objects tracked by the garbage collector. It should be invoked
    after server init / warmup, to reduce GC overhead from static objects
    during serving time.
    r         N)r2   collectfreezer   r   r   r   freeze_gc_heap`   s   


r?   c                     sN   t tj} | jr%t|  dtdtttf ddf fdd}tj	
| dS dS )zI
    Attached a callback for GC debug when VLLM_GC_DEBUG is enabled.
    r+   r,   r   Nc                    s     | | d S r   )r8   )r+   r,   debuggerr   r   gc_callbackw   s   z3maybe_attach_gc_debug_callback.<locals>.gc_callback)r   r   r   r   r#   r   r9   r:   r2   	callbacksappend)r$   rB   r   r@   r   maybe_attach_gc_debug_callbacko   s   
"rE   oc                 C   sP   d}t t dt|  d}W d   n1 sw   Y  tt|  | S )z
    Detailed object type.

    TODO(Jialin): Further enhance the detailed type with element types for
    easier debugging. We tried but occasionally it would run into signals
    which kills the engine.
    r%   z(size:)N)r   r   r4   r   type)rF   size_strr   r   r   _compute_detailed_type}   s
   
rJ   r6   topc                 C   s8   |dkrdS dd | D }d dd t||D S )z+
    Group collected objects by types.
    r   r%   c                 S   s   g | ]}t |qS r   )rJ   ).0rF   r   r   r   
<listcomp>   s    z5_compute_top_gc_collected_objects.<locals>.<listcomp>
c                 s   s$    | ]\}}|d d| V  qdS )z>5:Nr   )rL   object_typecountr   r   r   	<genexpr>   s
    
z4_compute_top_gc_collected_objects.<locals>.<genexpr>)joinr   most_common)r6   rK   object_typesr   r   r   r5      s   
r5   )r   N)r2   r   r&   collectionsr   
contextlibr   typingr   	vllm.envsr   vllm.loggerr   r   r   r   r#   r?   rE   r   rJ   listr:   r5   r   r   r   r   <module>   s   
2
