
    xKg/                        d Z ddlZddlZ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
 ddlmZmZ ddlmZ ddlmZ  G d dej$                        Z eg d	      Zd
 Z G d d      Z ee      Zd Zd Zd Z G d dej:                        Z G d de      Z G d de      Z ed        Z!ed        Z"ed        Z#ddZ$d dZ%edd       Z&de fdZ'd Z(ejR                  r e(        yy)!ag  
The ``numba.core.event`` module provides a simple event system for applications
to register callbacks to listen to specific compiler events.

The following events are built in:

- ``"numba:compile"`` is broadcast when a dispatcher is compiling. Events of
  this kind have ``data`` defined to be a ``dict`` with the following
  key-values:

  - ``"dispatcher"``: the dispatcher object that is compiling.
  - ``"args"``: the argument types.
  - ``"return_type"``: the return type.

- ``"numba:compiler_lock"`` is broadcast when the internal compiler-lock is
  acquired. This is mostly used internally to measure time spent with the lock
  acquired.

- ``"numba:llvm_lock"`` is broadcast when the internal LLVM-lock is acquired.
  This is used internally to measure time spent with the lock acquired.

- ``"numba:run_pass"`` is broadcast when a compiler pass is running.

    - ``"name"``: pass name.
    - ``"qualname"``: qualified name of the function being compiled.
    - ``"module"``: module name of the function being compiled.
    - ``"flags"``: compilation flags.
    - ``"args"``: argument types.
    - ``"return_type"`` return type.

Applications can register callbacks that are listening for specific events using
``register(kind: str, listener: Listener)``, where ``listener`` is an instance
of ``Listener`` that defines custom actions on occurrence of the specific event.
    N)default_timer)contextmanager	ExitStack)defaultdict)configc                   T    e Zd ZdZ ej
                         Z ej
                         Zy)EventStatuszStatus of an event.
    N)__name__
__module____qualname____doc__enumautoSTARTEND     T/home/alanp/www/video.onchill/myenv/lib/python3.12/site-packages/numba/core/event.pyr	   r	   2   s!    DIIKE
$))+Cr   r	   )znumba:compiler_lockznumba:compileznumba:llvm_locknumba:run_passc                 X    | j                  d      r| t        vr|  d}t        |      | S )a>  Guard to ensure that an event kind is valid.

    All event kinds with a "numba:" prefix must be defined in the pre-defined
    ``numba.core.event._builtin_kinds``.
    Custom event kinds are allowed by not using the above prefix.

    Parameters
    ----------
    kind : str

    Return
    ------
    res : str
    znumba:zG is not a valid event kind, it starts with the reserved prefix 'numba:')
startswith_builtin_kinds
ValueError)kindmsgs     r   _guard_kindr   B   s7     x T%? = =oKr   c                       e Zd ZdZddZed        Zed        Zed        Zed        Z	ed        Z
ed	        Zd
 ZeZy)EventzAn event.

    Parameters
    ----------
    kind : str
    status : EventStatus
    data : any; optional
        Additional data for the event.
    exc_details : 3-tuple; optional
        Same 3-tuple for ``__exit__``.
    Nc                 l    t        |      | _        || _        || _        ||d   d | _        y || _        y Nr   )r   _kind_status_data_exc_details)selfr   statusdataexc_detailss        r   __init__zEvent.__init__d   s@     &

 + 3{1~7M ""- 	r   c                     | j                   S )zFEvent kind

        Returns
        -------
        res : str
        )r!   r%   s    r   r   z
Event.kindl        zzr   c                     | j                   S )zPEvent status

        Returns
        -------
        res : EventStatus
        )r"   r+   s    r   r&   zEvent.statusv   s     ||r   c                     | j                   S )zIEvent data

        Returns
        -------
        res : object
        )r#   r+   s    r   r'   z
Event.data   r,   r   c                 <    | j                   t        j                  k(  S )zSIs it a *START* event?

        Returns
        -------
        res : bool
        )r"   r	   r   r+   s    r   is_startzEvent.is_start   s     ||{0000r   c                 <    | j                   t        j                  k(  S )zRIs it an *END* event?

        Returns
        -------
        res : bool
        )r"   r	   r   r+   s    r   is_endzEvent.is_end   s     ||{..r   c                     | j                   du S )zIs the event carrying an exception?

        This is used for *END* event. This method will never return ``True``
        in a *START* event.

        Returns
        -------
        res : bool
        N)r$   r+   s    r   	is_failedzEvent.is_failed   s       D((r   c                     | j                    t        | j                         j                   nd}d| j                   d| j                   d| dS )NNonezEvent(z, z, data: ))r'   typer   r!   r"   )r%   r'   s     r   __str__zEvent.__str__   sM    99( 		?//0.4 	

|2dll^8D6CCr   NN)r
   r   r   r   r)   propertyr   r&   r'   r0   r2   r4   r9   __repr__r   r   r   r   r   X   s    
/       1 1 / / 
) 
)D
 Hr   r   c                 n    t        |t              sJ t        |       } t        |    j	                  |       y)zvRegister a listener for a given event kind.

    Parameters
    ----------
    kind : str
    listener : Listener
    N)
isinstanceListenerr   _registeredappendr   listeners     r   registerrD      s1     h)))tDX&r   c                 r    t        |t              sJ t        |       } t        |    }|j	                  |       y)zxUnregister a listener for a given event kind.

    Parameters
    ----------
    kind : str
    listener : Listener
    N)r>   r?   r   r@   remove)r   rC   lsts      r   
unregisterrH      s4     h)))tD
d
CJJxr   c                 V    t         | j                     D ]  }|j                  |         y)zeBroadcast an event to all registered listeners.

    Parameters
    ----------
    event : Event
    N)r@   r   notify)eventrC   s     r   	broadcastrL      s#      

+ ,r   c                   ^    e Zd ZdZej
                  d        Zej
                  d        Zd Zy)r?   z(Base class for all event listeners.
    c                      y)zkCalled when there is a *START* event.

        Parameters
        ----------
        event : Event
        Nr   r%   rK   s     r   on_startzListener.on_start        	r   c                      y)ziCalled when there is a *END* event.

        Parameters
        ----------
        event : Event
        Nr   rO   s     r   on_endzListener.on_end   rQ   r   c                     |j                   r| j                  |       y|j                  r| j                  |       yt	        d      )zpNotify this Listener with the given Event.

        Parameters
        ----------
        event : Event
        unreachableN)r0   rP   r2   rS   AssertionErrorrO   s     r   rJ   zListener.notify   s5     >>MM% \\KK //r   N)	r
   r   r   r   abcabstractmethodrP   rS   rJ   r   r   r   r?   r?      s>      	 0r   r?   c                   B    e Zd ZdZd Zd Zd Zed        Zed        Z	y)TimingListenerzA listener that measures the total time spent between *START* and
    *END* events during the time this listener is active.
    c                     d| _         y r    )_depthr+   s    r   r)   zTimingListener.__init__  s	    r   c                 j    | j                   dk(  rt               | _        | xj                   dz  c_         y )Nr      )r\   timer_tsrO   s     r   rP   zTimingListener.on_start	  s%    ;;!wDHqr   c                     | xj                   dz  c_         | j                   dk(  r-t        | dd      }t               | j                  z
  |z   | _        y y )Nr^   r   	_duration)r\   getattrr_   r`   rb   )r%   rK   lasts      r   rS   zTimingListener.on_end  sF    q;;!4a0D#g0D8DN r   c                     t        | d      S )zReturns a ``bool`` indicating whether a measurement has been made.

        When this returns ``False``, the matching event has never fired.
        If and only if this returns ``True``, ``.duration`` can be read without
        error.
        rb   )hasattrr+   s    r   donezTimingListener.done  s     t[))r   c                     | j                   S )zReturns the measured duration.

        This may raise ``AttributeError``. Users can use ``.done`` to check
        that a measurement has been made.
        )rb   r+   s    r   durationzTimingListener.duration  s     ~~r   N)
r
   r   r   r   r)   rP   rS   r;   rg   ri   r   r   r   rZ   rZ     s>    
9 * *  r   rZ   c                   "    e Zd ZdZd Zd Zd Zy)RecordingListenera  A listener that records all events and stores them in the ``.buffer``
    attribute as a list of 2-tuple ``(float, Event)``, where the first element
    is the time the event occurred as returned by ``time.time()`` and the second
    element is the event.
    c                     g | _         y N)bufferr+   s    r   r)   zRecordingListener.__init__.  s	    r   c                 b    | j                   j                  t        j                         |f       y rm   rn   rA   timerO   s     r   rP   zRecordingListener.on_start1      DIIK/0r   c                 b    | j                   j                  t        j                         |f       y rm   rp   rO   s     r   rS   zRecordingListener.on_end4  rr   r   N)r
   r   r   r   r)   rP   rS   r   r   r   rk   rk   (  s    
11r   rk   c              #   h   K   t        | |       	 | t        | |       y# t        | |       w xY ww)a  Install a listener for event "kind" temporarily within the duration of
    the context.

    Returns
    -------
    res : Listener
        The *listener* provided.

    Examples
    --------

    >>> with install_listener("numba:compile", listener):
    >>>     some_code()  # listener will be active here.
    >>> other_code()     # listener will be unregistered by this point.

    N)rD   rH   rB   s     r   install_listenerru   8  s0     $ T8#4"
4"s   2! 2/2c              #      K   t               }t        | |      5  | ddd       |j                  r ||j                         yy# 1 sw Y   )xY ww)a  Install a TimingListener temporarily to measure the duration of
    an event.

    If the context completes successfully, the *callback* function is executed.
    The *callback* function is expected to take a float argument for the
    duration in seconds.

    Returns
    -------
    res : TimingListener

    Examples
    --------

    This is equivalent to:

    >>> with install_listener(kind, TimingListener()) as res:
    >>>    ...
    N)rZ   ru   rg   ri   )r   callbacktls      r   install_timerry   Q  sF     * 
	B	$	# 
$ 
ww  
$	#s   AA(AAAc              #   j   K   t               }t        | |      5  | ddd       y# 1 sw Y   yxY ww)an  Install a RecordingListener temporarily to record all events.

    Once the context is closed, users can use ``RecordingListener.buffer``
    to access the recorded events.

    Returns
    -------
    res : RecordingListener

    Examples
    --------

    This is equivalent to:

    >>> with install_listener(kind, RecordingListener()) as res:
    >>>    ...
    N)rk   ru   )r   rls     r   install_recorderr|   n  s*     & 
	B	$	# 
$	#	#s   3'	303c                 R    t        | t        j                  |      }t        |       y)zTrigger the start of an event of *kind* with *data*.

    Parameters
    ----------
    kind : str
        Event kind.
    data : any; optional
        Extra event data.
    )r   r&   r'   N)r   r	   r   rL   )r   r'   evts      r   start_eventr     s     T+"3"3$
?CcNr   c                 T    t        | t        j                  ||      }t        |       y)a  Trigger the end of an event of *kind*, *exc_details*.

    Parameters
    ----------
    kind : str
        Event kind.
    data : any; optional
        Extra event data.
    exc_details : 3-tuple; optional
        Same 3-tuple for ``__exit__``. Or, ``None`` if no error.
    )r   r&   r'   r(   N)r   r	   r   rL   )r   r'   r(   r~   s       r   	end_eventr     s$     +//+C cNr   c              #       K   t               5 }|j                   fd       }t                d ddd       y# 1 sw Y   yxY ww)a;  A context manager to trigger the start and end events of *kind* with
    *data*. The start event is triggered when entering the context.
    The end event is triggered when exiting the context.

    Parameters
    ----------
    kind : str
        Event kind.
    data : any; optional
        Extra event data.
    c                  "    t        |        y )N)r'   r(   )r   )r(   r'   r   s    r   on_exitztrigger_event.<locals>.on_exit  s    d;?r   )r'   N)r   pushr   )r   r'   scoper   s   ``  r   trigger_eventr     s@      
		@ 
	@ 	Dt$ 
s   A'?	AAArC   c           
      B   t        j                         }t        j                         }g }| j                  D ]e  \  }}|j
                  }t        |j                        }|dz  }|j                  rdnd}	|d   }
|}t        |||||	|
|      }|j                  |       g |S )zGPrepare events in `listener` for serializing as chrome trace data.
    i@B BEname)catpidtidtsphr   args)osgetpid	threadingget_native_idrn   r'   strr   r0   dictrA   )rC   r   r   evsr   recr'   r   	ts_scaledr   r   r   evs                r   _prepare_chrome_trace_datar     s     ))+C

!
!
#C
C??Cxx#((mN	LLScF|#)
 	

2 # Jr   c                      t               t        d       t        j                  t        j                  fd       } y)z\Setup a RecordingListener and an exit handler to write the captured
    events to file.
    r   c                      t              } t        d      5 }t        j                  | |       d d d        y # 1 sw Y   y xY w)Nw)r   openjsondump)r   outfilenamerC   s     r   _write_chrome_tracez=_setup_chrome_trace_exit_handler.<locals>._write_chrome_trace  s5     )2(C CIIc3 !  s	   9AN)rk   rD   r   CHROME_TRACEatexit)r   r   rC   s    @@r    _setup_chrome_trace_exit_handlerr     s9     !"Hx(""H__   r   rm   r:   )*r   r   r   r   rW   r   rq   r   timeitr   r_   
contextlibr   r   collectionsr   
numba.corer   Enumr	   	frozensetr   r   r   listr@   rD   rH   rL   ABCr?   rZ   rk   ru   ry   r|   r   r   r   r   r   r   r   r   r   <module>r      s(  !F 
   
    ) 0 # $))    ,X Xv $
'#0sww #0L#X #L1 1  # #0  8  .$  *): 0   
$& r   