
    tniO                     8   d Z ddlZddlmZ ddlZddlZddlZ ej                     d ej                     Z	  ej	                    d         Z
n# e$ r dZ
Y nw xY w ej                    Z G d d          Z G d	 d
          Z G d d          Zd Zd ZdS )zY
runpod | serverless | rp_debugger.py

A collection of functions to help with debugging.
    N)timezone 	brand_rawzUnable to get processor info.c                   B    e Zd ZdZdZg Zi Zd Zd Zd Z	d Z
d Zd ZdS )	Checkpointsa  
    A singleton class to store checkpoint times.

    Format:
    [
        {
            'name': 'checkpoint_name',
            'start': 1234567890.123456,
            'end': 1234567890.123456
            'duration_ms': 1234567890.123456
        },
    ]

    Usage:
    from rp_debugger import Checkpoints

    checkpoints = Checkpoints()

    # Add a checkpoint
    checkpoints.add('checkpoint_name')

    # Start a checkpoint
    checkpoints.start('checkpoint_name')

    # Stop a checkpoint
    checkpoints.stop('checkpoint_name')
    Nc                     t           j        Ft                              |           t           _        g t           j        _        i t           j        _        t           j        S N)r   _Checkpoints__instanceobject__new__checkpointsname_lookup)clss    `/var/www/html/gpu-tools/venv/lib/python3.11/site-packages/runpod/serverless/utils/rp_debugger.pyr   zCheckpoints.__new__<   s>    !)%+^^C%8%8K"13K".13K".%%    c                     || j         v rt          d| d          | j                            d|i           t	          | j                  dz
  }|| j         |<   dS )zP
        Add a checkpoint.
        Returns the index of the checkpoint.
        zCheckpoint name "z" already exists.name   N)r   KeyErrorr   appendlenselfr   indexs      r   addzCheckpoints.addC   so    
 4###FtFFFGGG///D$%%)!&r   c                 2   || j         vrt          d| d          | j         |         }t          j                    | j        |         d<   t
          j                            t          j                  	                                dz   | j        |         d<   dS )z%
        Start a checkpoint.
        Checkpoint name '' does not exist.startZ	start_utcN)
r   r   timeperf_counterr   datetimenowr   utc	isoformatr   s      r   r   zCheckpoints.startP   s     t'''FtFFFGGG &+/+<+>+>(!!(,//99;;cA 	,,,r   c                 n   || j         vrt          d| d          | j         |         }d| j        |         vrt          d          t          j                    | j        |         d<   t
          j                            t          j                  	                                dz   | j        |         d<   dS )	z$
        Stop a checkpoint.
        r   r   r   z Checkpoint has not been started.endr    stop_utcN)
r   r   r   r"   r#   r$   r%   r   r&   r'   r   s      r   stopzCheckpoints.stop]   s     t'''FtFFFGGG &$*5111=>>>)-):)<)<&!!(,//99;;cA 	
+++r   c                     g }| j         D ]e}d|vsd|vr|d         }|d         }||z
  dz  |d<   |                    d           |                    d           |                    |           f|S )z5
        Get the results of the checkpoints.
        r   r)   i  duration_ms)r   popr   )r   results
checkpoint
start_timeend_times        r   get_checkpointszCheckpoints.get_checkpointsn   s     * 
	' 
	'Jj((E,C,C#G,J!%(H)1J)>$(FJ}%NN7###NN5!!!NN:&&&&r   c                 "    g | _         i | _        dS )z(
        Clear the checkpoints.
        N)r   r   r   s    r   clearzCheckpoints.clear   s     r   )__name__
__module____qualname____doc__r
   r   r   r   r   r   r+   r3   r6    r   r   r   r      s         8 JKK& & &' ' '
 
 

 
 
"  &    r   r   c                   $    e Zd ZdZd Zd Zd ZdS )	LineTimerz
    A utility that can be used to time code execution using the with statement.
    When used the times should be added to the checkpoints object.
    c                 x    t                      | _        || _        | j                            | j                   d S r	   )r   r   r   r   )r   r   s     r   __init__zLineTimer.__init__   s4    &==	TY'''''r   c                 D    | j                             | j                   d S r	   )r   r   r   r5   s    r   	__enter__zLineTimer.__enter__   s!    ty)))))r   c                 D    | j                             | j                   d S r	   )r   r+   r   )r   argss     r   __exit__zLineTimer.__exit__   s!    di(((((r   N)r7   r8   r9   r:   r?   rA   rD   r;   r   r   r=   r=      sK         
( ( (
* * *) ) ) ) )r   r=   c                       e Zd ZdZd Zd ZdS )FunctionTimerz:
    A class-based decorator to benchmark a function.
    c                 :    || _         t                      | _        d S r	   )functionr   r   )r   rH   s     r   r?   zFunctionTimer.__init__   s     &==r   c                    | j                             | j        j                   	 | j                             | j        j                    | j        |i |}| j        j        | j         j        v r$| j                             | j        j                   nB# | j        j        | j         j        v r%| j                             | j        j                   w w xY w|S r	   )r   r   rH   r7   r   r   r+   )r   rC   kwargsresults       r   __call__zFunctionTimer.__call__   s    T]3444	>""4=#9:::"T]D3F33F }%)9)EEE %%dm&<=== }%)9)EEE %%dm&<==== F s   1B ?CN)r7   r8   r9   r:   r?   rL   r;   r   r   rF   rF      s<         ) ) )    r   rF   c                  .   t          d           ddl} t          d           t                      }|                                }|                                 t          d           t
          t          t          | j        d}t          d           ||dS )	z%
    Return the debugger output.
    zGetting debugger output...r   NzGetting checkpoints...zGetting system info...)os	processorpython_versionrunpodzDebugger output complete.)system_info
timestamps)	printrQ   r   r3   r6   OS_INFO	PROCESSORPYTHON_VERSION__version__)rQ   r   ckpt_resultsrR   s       r   get_debugger_outputrZ      s     

&'''MMM	
"###--K..00L	
"### ($	 K 

%&&& #"  r   c                  J    t                      } |                                  dS )z$
    Clear the debugger output.
    N)r   r6   )r   s    r   clear_debugger_outputr\      s%     --Kr   )r:   r$   r   platformr"   cpuinfosystemreleaserU   get_cpu_inforV   r   rP   rW   r   r=   rF   rZ   r\   r;   r   r   <module>rb      sm             
 X_
5
5!1!1!3!3
5
50$$&&{3II 0 0 0/III0 )(**k k k k k k k k\) ) ) ) ) ) ) )$       .  <    s   A AA