
    &`i              	           d Z ddlmZ ddlmZmZ ddlZddlmZ dej	        de
ddfd	Zddej	        de
deddfdZde
fdZ ej        d           G d d                      Zd ZdS )a  Utility for debugging object store memory eager deletion in Datasets.

NOTE: the performance overhead of tracing object allocation is fairly substantial.
This is meant to use in unit test for debugging. Please do not enable in production,
without performance optimization.

Enable with RAY_DATA_TRACE_ALLOCATIONS=1.

Basic usage is to call `trace_allocation` each time a new object is created, and call
`trace_deallocation` when an object should be disposed of. When the workload is
complete, call `leak_report` to view possibly leaked objects.

Note that so called "leaked" objects will be reclaimed eventually by reference counting
in Ray. This is just to debug the eager deletion protocol which is more efficient.
    )StringIO)DictListN)DataContextreflocreturnc                     t          j                    }|j        r>t                      }t	          j        |j                            | g|                     dS dS )zRecord that an object has been created.

    Args:
        ref: The object created.
        loc: A human-readable string identifying the call site.
    N)r   get_currenttrace_allocations_get_mem_actorraygettrace_allocremote)r   r   ctxtracers       u/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/data/_internal/memory_tracing.pytrace_allocationr      s]     
!
#
#C
 7!!"))3%55666667 7    Tfreec                    |r&t           j        j                            | d           t	          j                    }|j        r?t                      }t          j        |j	        
                    | g||                     dS dS )a6  Record that an object has been deleted (and delete if free=True).

    Args:
        ref: The object we no longer need.
        loc: A human-readable string identifying the call site.
        free: Whether to eagerly destroy the object instead of waiting for Ray
            reference counting to kick in.
    F)
local_onlyN)r   _privateinternal_apir   r   r   r   r   r   trace_deallocr   )r   r   r   r   r   s        r   trace_deallocationr   &   s      >!&&su&===

!
#
#C
 ?!!$++SE3==>>>>>? ?r   c                  t    t                      } t          j        | j                                                  S )N)r   r   r   leak_reportr   )r   s    r   r   r   7   s,    F76%,,..///r   )num_cpusc                   p    e Zd Zd Zdeej                 defdZdeej                 dede	fdZ
defdZd	S )
	_MemActorc                 L    i | _         i | _        i | _        d| _        d| _        d S )Nr   )	allocateddeallocatedskip_deallocpeak_memcur_mem)selfs    r   __init__z_MemActor.__init__>   s+    466868r   r   r   c                    |d         }|| j         vrt          j                            |g          }|                    dd          }|scd}ddlm} 	 t          j        |d          }t          |                    |                    }n!# t          $ r t          d           d}Y nw xY wt          d| d	| d
|            ||d}|| j         |<   | xj
        |z  c_
        t          | j
        | j                  | _        d S d S )Nr   object_size)cloudpickleg      @)timeoutz [mem_tracing] ERROR getting sizez[mem_tracing] Allocated 
 bytes at : )
size_bytesr   )r$   r   experimentalget_object_locationsr   r.   lendumps	Exceptionprintr(   maxr'   )r)   r   r   metar2   pickleobjentrys           r   r   z_MemActor.trace_allocE   sC   !fdn$$#88#??D-33J 	$
555555$'#s333C!$V\\#%6%6!7!7JJ  $ $ $<===!#JJJ$ OZOO3OO#OOPPP( E #(DN3LLJ&LLdm<<DMMM) %$s   8B B*)B*freedc                    |d         }| j                             |i                               dd          }|rt          d| d| d|            || j         v rB| xj        |z  c_        | j                             |          | j        |<   || j        |         d<   || j        v rd S t          d| d           d S t          d	| d| d|            || j        |<   d S )
Nr   r2   z[mem_tracing] Freed r0   r1   dealloc_locz%[mem_tracing] WARNING: allocation of z was not traced!z[mem_tracing] Skipped freeing )r$   r   r8   r(   popr%   r&   )r)   r   r   r>   r2   s        r   r   z_MemActor.trace_dealloc]   s   !f^''R0044\1EE
 	)KKKsKKcKKLLLdn$$
*(,(:(:3(?(? %7: %m4d&&&ScSSSTTTTTU:UUUUPSUUVVV%(Dc"""r   r	   c                    t                      }|                    d           | j        D ]}| j        |                             d          }| j        |                             d          }|| j        v r0| j        |         }|                    d| d| d| d| d	           {|                    d| d| d| d           |                    d	           |                    d
           | j        D ]}| j        |                             d          }| j        |                             d          }| j        |                             d          }|                    d| d| d| d| d	           |                    d           |                    d| j         d           |                    d| j         d           |                                S )Nz)[mem_tracing] ===== Leaked objects =====
r2   r   z([mem_tracing] Leaked object, created at z, size z, skipped dealloc at r1   
z-[mem_tracing] ===== End leaked objects =====
z([mem_tracing] ===== Freed objects =====
r@   z [mem_tracing] Freed object from z at z,[mem_tracing] ===== End freed objects =====
z[mem_tracing] Peak size bytes z![mem_tracing] Current size bytes )	r   writer$   r   r&   r%   r'   r(   getvalue)r)   outputr   r2   r   r@   s         r   r   z_MemActor.leak_reporto   st   ABBB> 	 	C,00>>J.%))%00Cd'''"/4Os O O!O O8CO OGJO O O   
 2s 2 2&2 2*-2 2 2    	EFFF@AAA# 	 	C)#.22<@@J"3'++E22C*3/33MBBKLL.3 . .K . .". .&). . .    	DEEEGdmGGGHHHIIIIJJJ   r   N)__name__
__module____qualname__r*   r   r   	ObjectRefstrr   boolr   r    r   r   r"   r"   <   s          =tCM2 = = = = =0)cm!4 )3 )t ) ) ) )$!S ! ! ! ! ! !r   r"   c                  `    t                               ddd                                          S )Nmem_tracing_actorTdetached)nameget_if_existslifetime)r"   optionsr   rM   r   r   r   r      s/     z   fhhr   )T)__doc__ior   typingr   r   r   ray.data.contextr   rJ   rK   r   rL   r   r   r   r"   r   rM   r   r   <module>rY      s9                   



 ( ( ( ( ( (7#- 7c 7d 7 7 7 7? ?CM ? ?4 ?4 ? ? ? ?"0S 0 0 0 0
 QP! P! P! P! P! P! P! P!f    r   