
    J/Ph?/                        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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j*        r e)             dS dS )!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)configutilsc                   N    e Zd ZdZ ej                    Z ej                    ZdS )EventStatuszStatus of an event.
    N)__name__
__module____qualname____doc__enumautoSTARTEND     P/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/numba/core/event.pyr
   r
   2   s2         DIKKE
$)++CCCr   r
   )znumba:compiler_lockznumba:compileznumba:llvm_locknumba:run_passc                 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   sG     x   T%?%? = = =oo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dS )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                 p    t          |          | _        || _        || _        ||d         d n|| _        d S Nr   )r   _kind_status_data_exc_details)selfr   statusdataexc_detailss        r   __init__zEvent.__init__d   sF     &&

 + 3{1~7M "T"- 	r   c                     | j         S )zFEvent kind

        Returns
        -------
        res : str
        )r"   r&   s    r   r   z
Event.kindl        z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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                 t    | 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    9( 	??/11.4 	C
CCdlCCDCCCCr   NN)r   r   r   r   r*   propertyr   r'   r(   r1   r3   r5   r:   __repr__r   r   r   r   r   X   s        
 
/ / / /   X   X   X 1 1 X1 / / X/ 
) 
) X
)D D D
 HHHr   r   c                     t          |t                    sJ t          |           } t          |                              |           dS )zvRegister a listener for a given event kind.

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

    Parameters
    ----------
    kind : str
    listener : Listener
    N)r?   r@   r   rA   remove)r   rD   lsts      r   
unregisterrI      sI     h)))))tD
d
CJJxr   c                 Z    t           | j                 D ]}|                    |            dS )zeBroadcast an event to all registered listeners.

    Parameters
    ----------
    event : Event
    N)rA   r   notify)eventrD   s     r   	broadcastrM      s9      
+   r   c                   X    e Zd ZdZej        d             Zej        d             Zd ZdS )r@   z(Base class for all event listeners.
    c                     dS )zkCalled when there is a *START* event.

        Parameters
        ----------
        event : Event
        Nr   r&   rL   s     r   on_startzListener.on_start   	     	r   c                     dS )ziCalled when there is a *END* event.

        Parameters
        ----------
        event : Event
        Nr   rP   s     r   on_endzListener.on_end   rR   r   c                     |j         r|                     |           dS |j        r|                     |           dS t	          d          )zpNotify this Listener with the given Event.

        Parameters
        ----------
        event : Event
        unreachableN)r1   rQ   r3   rT   AssertionErrorrP   s     r   rK   zListener.notify   sV     > 	0MM%     \ 	0KK ///r   N)	r   r   r   r   abcabstractmethodrQ   rT   rK   r   r   r   r@   r@      si            	  0 0 0 0 0r   r@   c                   P    e Zd ZdZd Zd Zd Zed             Zed             Z	dS )TimingListenerzA listener that measures the total time spent between *START* and
    *END* events during the time this listener is active.
    c                     d| _         d S r!   )_depthr,   s    r   r*   zTimingListener.__init__  s    r   c                 b    | j         dk    rt                      | _        | xj         dz  c_         d S )Nr      )r]   timer_tsrP   s     r   rQ   zTimingListener.on_start	  s.    ;!wwDHqr   c                     | xj         dz  c_         | j         dk    r1t          | dd          }t                      | j        z
  |z   | _        d S d S )Nr_   r   	_duration)r]   getattrr`   ra   rc   )r&   rL   lasts      r   rT   zTimingListener.on_end  sS    q;!4a00D#gg0D8DNNN 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.
        rc   )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.
        )rc   r,   s    r   durationzTimingListener.duration  s     ~r   N)
r   r   r   r   r*   rQ   rT   r<   rh   rj   r   r   r   r[   r[     s             
9 9 9 * * X*   X  r   r[   c                   $    e Zd ZdZd Zd Zd ZdS )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 | _         d S N)bufferr,   s    r   r*   zRecordingListener.__init__.  s    r   c                 `    | j                             t          j                    |f           d S rn   ro   rB   timerP   s     r   rQ   zRecordingListener.on_start1  )    DIKK/00000r   c                 `    | j                             t          j                    |f           d S rn   rq   rP   s     r   rT   zRecordingListener.on_end4  rs   r   N)r   r   r   r   r*   rQ   rT   r   r   r   rl   rl   (  sK         
  1 1 11 1 1 1 1r   rl   c              #   ~   K   t          | |           	 |V  t          | |           dS # t          | |           w xY 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)rE   rI   rC   s     r   install_listenerrv   8  sR      $ T8#4"""""
4""""s   * <c              #      K   t                      }t          | |          5  |V  ddd           n# 1 swxY w Y   |j        r ||j                   dS dS )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)r[   rv   rh   rj   )r   callbacktls      r   install_timerrz   Q  s      * 
		B	$	#	#                 
w  s   266c              #      K   t                      }t          | |          5  |V  ddd           dS # 1 swxY w Y   dS )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)rl   rv   )r   rls     r   install_recorderr}   n  s      & 
		B	$	#	#                   s   377c                 \    t          | t          j        |          }t          |           dS )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   rM   )r   r(   evts      r   start_eventr     s*     T+"3$
?
?
?CcNNNNNr   c                 ^    t          | t          j        ||          }t          |           dS )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   rM   )r   r(   r)   r   s       r   	end_eventr     s4     +/+  C cNNNNNr   c              #       K   t                      5 }|j         fd            }t                      dV  ddd           dS # 1 swxY w Y   dS )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          |            d S )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rD   c           
      0   t          j                    }t          j                    }g }| j        D ]e\  }}|j        }t          |j                  }|dz  }|j        rdnd}	|d         }
|}t          |||||	|
|          }|
                    |           f|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_idro   r(   strr   r1   dictrB   )rD   r   r   evsr   recr(   r   	ts_scaledr   r   r   evs                r   _prepare_chrome_trace_datar     s     )++C

!
#
#C
C?  Cx#(mmN	L)SScF|#)
 
 
 	

2Jr   c                      t                      t          d           t          j        t          j        fd            } dS )z\Setup a RecordingListener and an exit handler to write the captured
    events to file.
    r   c                      t                    } t          d          5 }t          j        | |t          j                   d d d            d S # 1 swxY w Y   d S )Nw)cls)r   openjsondumpr   _LazyJSONEncoder)r   outfilenamerD   s     r   _write_chrome_tracez=_setup_chrome_trace_exit_handler.<locals>._write_chrome_trace  s     )22(C   	<CIc3E$:;;;;	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	<s   "AAAN)rl   rE   r   CHROME_TRACEatexit)r   r   rD   s    @@r    _setup_chrome_trace_exit_handlerr     s^     !""Hx((("H_< < < < < _< < <r   rn   r;   )+r   r   r   r   rX   r   rr   r   timeitr   r`   
contextlibr   r   collectionsr   
numba.corer   r   Enumr
   	frozensetr   r   r   listrA   rE   rI   rM   ABCr@   r[   rl   rv   rz   r}   r   r   r   r   r   r   r   r   r   <module>r      s  ! !F 
			   



       ) ) ) ) ) ) 0 0 0 0 0 0 0 0 # # # # # # $ $ $ $ $ $ $ $    $)           ,X X X X X X X Xv k$
' 
' 
'    #0 #0 #0 #0 #0sw #0 #0 #0L# # # # #X # # #L1 1 1 1 1 1 1 1  # # #0   8   .      $    *):    0< < <  
 '$$&&&&&' 'r   