U
    ð¾|eÉ/  ã                   @   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   úM/var/www/website-v5/atlas_env/lib/python3.8/site-packages/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 )ÚEventzàAn 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 )zÊIs 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
        Ú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 )ÚTimingListenerz€A 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   é   )rC   Ú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 )NrD   r   Ú	_duration)rC   ÚgetattrrE   rF   rG   )r   r:   Úlastr   r   r   r>     s    
zTimingListener.on_endc                 C   s
   t | dƒS )zôReturns 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.
        rG   )Úhasattrr$   r   r   r   Údone  s    zTimingListener.donec                 C   s   | j S )zžReturns the measured duration.

        This may raise ``AttributeError``. Users can use ``.done`` to check
        that a measurement has been made.
        )rG   r$   r   r   r   Úduration  s    zTimingListener.durationN)
r   r	   r
   r   r#   r=   r>   r,   rK   rL   r   r   r   r   rB     s   
	rB   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 rN   ©rO   r2   Útimer<   r   r   r   r=   1  s    zRecordingListener.on_startc                 C   s   | j  t ¡ |f¡ d S rN   rP   r<   r   r   r   r>   4  s    zRecordingListener.on_endN)r   r	   r
   r   r#   r=   r>   r   r   r   r   rM   (  s   rM   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    

rR   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)rB   rR   rK   rL   )r   ÚcallbackÚtlr   r   r   Úinstall_timerQ  s
    rU   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)rM   rR   )r   Zrlr   r   r   Úinstall_recordern  s    rV   c                 C   s   t | tj|d�}t|ƒ dS )z®Trigger 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    
rX   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"   rW   r   r   r   Ú	end_event”  s       ÿrY   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"   )rY   )r"   ©r!   r   r   r   Úon_exit´  s    ztrigger_event.<locals>.on_exit)r!   N)r   ÚpushrX   )r   r!   Úscoper[   r   rZ   r   Útrigger_event¦  s
    r^   c                   C   s,   z
t  ¡ W S  tk
r&   t  ¡  Y S X d S rN   )Ú	threadingZget_native_identÚAttributeErrorÚ	get_identr   r   r   r   Ú_get_native_ident¼  s    
rb   )r4   c              
   C   sx   t  ¡ }tƒ }g }| jD ]Z\}}|j}t|jƒ}|d }|jrBdnd}	|d }
|}t|||||	|
|d�}| 	|¡ q|S )zGPrepare events in `listener` for serializing as chrome trace data.
    i@B ÚBÚEÚname)ÚcatÚpidÚtidÚtsÚphre   Úargs)
ÚosÚgetpidrb   rO   r!   Ústrr   r%   Údictr2   )r4   rg   rh   Úevsri   Úrecr!   rf   Z	ts_scaledrj   re   rk   Úevr   r   r   Ú_prepare_chrome_trace_dataÄ  s*    
     þrs   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)rs   ÚopenÚjsonÚdump)rp   Úout©Úfilenamer4   r   r   Ú_write_chrome_traceä  s    z=_setup_chrome_trace_exit_handler.<locals>._write_chrome_traceN)rM   r5   r   ÚCHROME_TRACEÚatexit)r{   r   ry   r   Ú _setup_chrome_trace_exit_handlerÜ  s
    
r~   )N)NN)N)+r   rl   rv   r}   r@   r   rQ   r_   Útimeitr   rE   Ú
contextlibr   r   Úcollectionsr   Ú
numba.corer   ÚEnumr   Ú	frozensetr   r   r   Úlistr1   r5   r8   r;   ÚABCr/   rB   rM   rR   rU   rV   rX   rY   r^   rb   rs   r~   r|   r   r   r   r   Ú<module>   sT   #ü[&&




