o
    ×°“i-  ã                   @   s*   d dl Z d dlmZ G dd„ dejƒZdS )é    N)Úmemory_hookc                   @   sD   e Zd ZdZd Zejdfdd„Zdd„ Zdd„ Z	d	d
„ Z
dd„ ZdS )ÚDebugPrintHooka:  Memory hook that prints debug information.

    This memory hook outputs the debug information of input arguments of
    ``malloc`` and ``free`` methods involved in the hooked functions
    at postprocessing time (that is, just after each method is called).

    Example:
        The basic usage is to use it with ``with`` statement.

        Code example::

            >>> import cupy
            >>> from cupy.cuda import memory_hooks
            >>>
            >>> cupy.cuda.set_allocator(cupy.cuda.MemoryPool().malloc)
            >>> with memory_hooks.DebugPrintHook():
            ...     x = cupy.array([1, 2, 3])
            ...     del x  # doctest:+SKIP

        Output example::

            {"hook":"alloc","device_id":0,"mem_size":512,"mem_ptr":150496608256}
            {"hook":"malloc","device_id":0,"size":24,"mem_size":512,"mem_ptr":150496608256,"pmem_id":"0x7f39200c5278"}
            {"hook":"free","device_id":0,"mem_size":512,"mem_ptr":150496608256,"pmem_id":"0x7f39200c5278"}

        where the output format is JSONL (JSON Lines) and
        ``hook`` is the name of hook point, and
        ``device_id`` is the CUDA Device ID, and
        ``size`` is the requested memory size to allocate, and
        ``mem_size`` is the rounded memory size to be allocated, and
        ``mem_ptr`` is the memory pointer, and
        ``pmem_id`` is the pooled memory object ID.

    Attributes:
        file: Output file_like object that redirect to.
        flush: If ``True``, this hook forcibly flushes the text stream
            at the end of print. The default is ``True``.

    Tc                 C   s   || _ || _d S )N)ÚfileÚflush)Úselfr   r   © r   úV/home/ubuntu/.local/lib/python3.10/site-packages/cupy/cuda/memory_hooks/debug_print.pyÚ__init__1   s   
zDebugPrintHook.__init__c                 C   s0   | j  |¡ | j  d¡ | jr| j  ¡  d S d S )NÚ
)r   Úwriter   )r   Úmsgr   r   r   Ú_print5   s
   ÿzDebugPrintHook._printc                 K   s.   d}|d|d |d |d f; }|   |¡ d S )Nz7{"hook":"%s","device_id":%d,"mem_size":%d,"mem_ptr":%d}ÚallocÚ	device_idÚmem_sizeÚmem_ptr)r   ©r   Úkwargsr   r   r   r   Úalloc_postprocess;   s
   
ÿz DebugPrintHook.alloc_postprocessc              	   K   s>   d}|d|d |d |d |d t |d ƒf; }|  |¡ d S )NzP{"hook":"%s","device_id":%d,"size":%d,"mem_size":%d,"mem_ptr":%d,"pmem_id":"%s"}Úmallocr   Úsizer   r   Úpmem_id©Úhexr   r   r   r   r   Úmalloc_postprocessB   s
   ÿz!DebugPrintHook.malloc_postprocessc                 K   s8   d}|d|d |d |d t |d ƒf; }|  |¡ d S )NzF{"hook":"%s","device_id":%d,"mem_size":%d,"mem_ptr":%d,"pmem_id":"%s"}Úfreer   r   r   r   r   r   r   r   r   Úfree_postprocessI   s
   
ÿzDebugPrintHook.free_postprocessN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚnameÚsysÚstdoutr	   r   r   r   r   r   r   r   r   r      s    (r   )r"   Ú	cupy.cudar   Ú
MemoryHookr   r   r   r   r   Ú<module>   s    