
    ܙd/                        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 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)configc                   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     0lib/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'   r0   r2   r4   r9   __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   registerrD      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   r@   remove)r   rC   lsts      r   
unregisterrH      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)r@   r   notify)eventrC   s     r   	broadcastrL      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%   rK   s     r   on_startzListener.on_start   	     	r   c                     dS )ziCalled when there is a *END* event.

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

        Parameters
        ----------
        event : Event
        unreachableN)r0   rP   r2   rS   AssertionErrorrO   s     r   rJ   zListener.notify   sV     > 	0MM%     \ 	0KK ///r   N)	r
   r   r   r   abcabstractmethodrP   rS   rJ   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_tsrO   s     r   rP   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_   r`   rb   )r%   rK   lasts      r   rS   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.
        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 9 9 * * X*   X  r   rZ   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 rm   rn   rA   timerO   s     r   rP   zRecordingListener.on_start1  )    DIKK/00000r   c                 `    | j                             t          j                    |f           d S 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   (  sK         
  1 1 11 1 1 1 1r   rk   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)rD   rH   rB   s     r   install_listenerru   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)rZ   ru   rg   ri   )r   callbacktls      r   install_timerry   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)rk   ru   )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   rL   )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   rL   )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c                  p    	 t          j                    S # t          $ r t          j                    cY S w xY wrm   )	threadingget_native_identAttributeError	get_identr   r   r   _get_native_identr     sF    %)+++ % % %"$$$$$%s    55rC   c           
      &   t          j                    }t                      }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getpidr   rn   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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        | |           d d d            d S # 1 swxY w Y   d S )Nw)r   openjsondump)r   outfilenamerC   s     r   _write_chrome_tracez=_setup_chrome_trace_exit_handler.<locals>._write_chrome_trace  s     )22(C   	 CIc3	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	 s   AAAN)rk   rD   r   CHROME_TRACEatexit)r   r   rC   s    @@r    _setup_chrome_trace_exit_handlerr     s^     !""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   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   