U
    ,dy/                     @   sp  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 dejZed	d
ddgZdd ZG dd dZeeZdd Zdd Zdd ZG dd dejZG dd deZG dd deZ edd Z!edd  Z"ed!d" Z#d0d#d$Z$d1d%d&Z%ed2d'd(Z&d)d* Z'e d+d,d-Z(d.d/ Z)ej*rle)  dS )3ag  
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                   @   s    e Zd ZdZe Ze ZdS )EventStatuszStatus of an event.
    N)__name__
__module____qualname____doc__enumautoSTARTEND r   r   4/tmp/pip-unpacked-wheel-eu7e0c37/numba/core/event.pyr   2   s   r   znumba:compiler_lockznumba:compileznumba:llvm_locknumba:run_passc                 C   s(   |  dr$| tk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msgr   r   r   _guard_kindB   s    
r   c                   @   sn   e Zd ZdZdddZedd Zedd Zed	d
 Zedd Z	edd Z
edd Zd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                 C   s8   t || _|| _|| _|d ks*|d d kr.d n|| _d S Nr   )r   _kind_status_data_exc_details)selfr   statusdataexc_detailsr   r   r   __init__d   s    

zEvent.__init__c                 C   s   | j S )zFEvent kind

        Returns
        -------
        res : str
        )r   r   r   r   r   r   l   s    z
Event.kindc                 C   s   | j S )zPEvent status

        Returns
        -------
        res : EventStatus
        )r   r$   r   r   r   r    v   s    zEvent.statusc                 C   s   | j S )zIEvent data

        Returns
        -------
        res : object
        )r   r$   r   r   r   r!      s    z
Event.datac                 C   s   | j tjkS )zSIs it a *START* event?

        Returns
        -------
        res : bool
        )r   r   r   r$   r   r   r   is_start   s    zEvent.is_startc                 C   s   | j tjkS )zRIs it an *END* event?

        Returns
        -------
        res : bool
        )r   r   r   r$   r   r   r   is_end   s    zEvent.is_endc                 C   s
   | j dk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$   r   r   r   	is_failed   s    zEvent.is_failedc                 C   s8   | j d k	rt| j j nd}d| j d| j d| dS )NNonezEvent(z, z, data: ))r!   typer
   r   r   )r   r!   r   r   r   __str__   s
    zEvent.__str__)NN)r   r	   r
   r   r#   propertyr   r    r!   r%   r&   r'   r+   __repr__r   r   r   r   r   X   s    

	
	
	
	
	
r   c                 C   s(   t |tstt| } t|  | dS )zvRegister a listener for a given event kind.

    Parameters
    ----------
    kind : str
    listener : Listener
    N)
isinstanceListenerAssertionErrorr   _registeredappendr   listenerr   r   r   register   s    r5   c                 C   s,   t |tstt| } t|  }|| dS )zxUnregister a listener for a given event kind.

    Parameters
    ----------
    kind : str
    listener : Listener
    N)r.   r/   r0   r   r1   remove)r   r4   lstr   r   r   
unregister   s    r8   c                 C   s   t | j D ]}||  q
dS )zeBroadcast an event to all registered listeners.

    Parameters
    ----------
    event : Event
    N)r1   r   notify)eventr4   r   r   r   	broadcast   s    r;   c                   @   s4   e Zd ZdZejdd Zejdd Zdd ZdS )	r/   z(Base class for all event listeners.
    c                 C   s   dS )zkCalled when there is a *START* event.

        Parameters
        ----------
        event : Event
        Nr   r   r:   r   r   r   on_start   s    zListener.on_startc                 C   s   dS )ziCalled when there is a *END* event.

        Parameters
        ----------
        event : Event
        Nr   r<   r   r   r   on_end   s    zListener.on_endc                 C   s0   |j r| | n|jr$| | ntddS )zpNotify this Listener with the given Event.

        Parameters
        ----------
        event : Event
        ZunreachableN)r%   r=   r&   r>   r0   r<   r   r   r   r9      s
    zListener.notifyN)	r   r	   r
   r   abcabstractmethodr=   r>   r9   r   r   r   r   r/      s   
	
	r/   c                   @   s@   e Zd ZdZdd Zdd Zdd Zedd	 Zed
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                 C   s
   d| _ d S r   )_depthr$   r   r   r   r#     s    zTimingListener.__init__c                 C   s$   | j dkrt | _|  j d7  _ d S )Nr      )rB   timer_tsr<   r   r   r   r=   	  s    
zTimingListener.on_startc                 C   s:   |  j d8  _ | j dkr6t| dd}t | j | | _d S )NrC   r   	_duration)rB   getattrrD   rE   rF   )r   r:   lastr   r   r   r>     s    
zTimingListener.on_endc                 C   s
   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.
        rF   )hasattrr$   r   r   r   done  s    zTimingListener.donec                 C   s   | j S )zReturns the measured duration.

        This may raise ``AttributeError``. Users can use ``.done`` to check
        that a measurement has been made.
        )rF   r$   r   r   r   duration  s    zTimingListener.durationN)
r   r	   r
   r   r#   r=   r>   r,   rJ   rK   r   r   r   r   rA     s   
	rA   c                   @   s(   e Zd ZdZdd Zdd Zd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                 C   s
   g | _ d S N)bufferr$   r   r   r   r#   .  s    zRecordingListener.__init__c                 C   s   | j t |f d S rM   rN   r2   timer<   r   r   r   r=   1  s    zRecordingListener.on_startc                 C   s   | j t |f d S rM   rO   r<   r   r   r   r>   4  s    zRecordingListener.on_endN)r   r	   r
   r   r#   r=   r>   r   r   r   r   rL   (  s   rL   c              	   c   s&   t | | z
|V  W 5 t| | X dS )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)r5   r8   r3   r   r   r   install_listener8  s    

rQ   c              	   c   s6   t  }t| | |V  W 5 Q R X |jr2||j 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)rA   rQ   rJ   rK   )r   callbacktlr   r   r   install_timerQ  s
    rT   c              	   c   s&   t  }t| | |V  W 5 Q R X 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   rQ   )r   Zrlr   r   r   install_recordern  s    rU   c                 C   s   t | tj|d}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   r;   )r   r!   evtr   r   r   start_event  s    
rW   c                 C   s   t | tj||d}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   r;   )r   r!   r"   rV   r   r   r   	end_event  s       rX   c              	   #   s<   t  ,}|j fdd}t d dV  W 5 Q R X 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                     s   t  | d d S )N)r!   r"   )rX   )r"   r!   r   r   r   on_exit  s    ztrigger_event.<locals>.on_exit)r!   N)r   pushrW   )r   r!   ZscoperZ   r   rY   r   trigger_event  s
    r\   c                   C   s,   z
t  W S  tk
r&   t   Y S X d S rM   )	threadingZget_native_identAttributeError	get_identr   r   r   r   _get_native_ident  s    
r`   )r4   c              
   C   sp   t  }t }g }| jD ]R\}}|j}t|j}|jr:dnd}|d }	|}
t||||||	|
d}|	| q|S )zGPrepare events in `listener` for serializing as chrome trace data.
    BEname)catpidtidtsphrc   args)
osgetpidr`   rN   r!   strr   r%   dictr2   )r4   re   rf   evsrg   Zrecr!   rd   rh   rc   ri   Zevr   r   r   _prepare_chrome_trace_data  s(    
      ro   c                     s.   t  td tj tj fdd} dS )z\Setup a RecordingListener and an exit handler to write the captured events
    to file.
    r   c               	      s.   t } t d}t| | W 5 Q R X d S )Nw)ro   openjsondump)rn   outfilenamer4   r   r   _write_chrome_trace  s    z=_setup_chrome_trace_exit_handler.<locals>._write_chrome_traceN)rL   r5   r   CHROME_TRACEatexit)rw   r   ru   r    _setup_chrome_trace_exit_handler  s
    
rz   )N)NN)N)+r   rj   rr   ry   r?   r   rP   r]   Ztimeitr   rD   
contextlibr   r   collectionsr   Z
numba.corer   Enumr   	frozensetr   r   r   listr1   r5   r8   r;   ABCr/   rA   rL   rQ   rT   rU   rW   rX   r\   r`   ro   rz   rx   r   r   r   r   <module>   sT   #[&&




