a
    kº”hM  ã                   @   sÂ   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 e  ¡ Zeeef ZG dd„ dƒZG d	d
„ d
eƒZdddœeeeeeef  edœdd„Zdeeeeef  edœdd„ZdS )é    N©Úwraps)ÚOptionalÚUnioné   )Úget_profiling_on)Úprotonc                   @   sX   e Zd ZdZdeeeeef  ddœdd„Zdd„ Z	dd	„ Z
d
d„ Zdd„ Zdd„ ZdS )Úscopea  
    A context manager and decorator for entering and exiting a scope.

    Usage:
        context manager:
        ```python
        with proton.scope("test0", {metric_name: metric_value}):
            foo[1,](x, y)
        ```

        decorator:
        ```python
        @proton.scope("test0", {metric_name: metric_value})
        def foo(x, y):
            ...
        ```

    Args:
        name (str): The name of the scope.
        metrics (dict[str, float], optional): The metrics of the scope. Default is None.
    N©ÚnameÚmetricsÚreturnc                 C   s   || _ || _d | _d S ©N)r   r   Úid©Úselfr   r   © r   úC/var/www/auris/lib/python3.9/site-packages/triton/profiler/scope.pyÚ__init__%   s    zscope.__init__c                 C   s>   t ƒ s
d S t ¡ | _t | j| j¡ | jr:t | j| j¡ d S r   )r   Ú	libprotonÚrecord_scoper   Úenter_scoper   r   Úadd_metrics©r   r   r   r   Ú_enter_scope*   s    
zscope._enter_scopec                 C   s(   t ƒ r| jd u rd S t | j| j¡ d S r   )r   r   r   Ú
exit_scoper   r   r   r   r   Ú_exit_scope2   s    zscope._exit_scopec                 C   s   |   ¡  | S r   )r   r   r   r   r   Ú	__enter__7   s    zscope.__enter__c                 C   s   |   ¡  d S r   )r   )r   Úexc_typeÚ	exc_valueÚ	tracebackr   r   r   Ú__exit__;   s    zscope.__exit__c                    s   t ˆ ƒ‡ ‡fdd„ƒ}|S )Nc                     s0   ˆ  ¡  zˆ | i |¤ŽW ˆ ¡  S ˆ ¡  0 d S r   )r   r   )ÚargsÚkwargs©Úfuncr   r   r   Úwrapper@   s    þzscope.__call__.<locals>.wrapperr   )r   r%   r&   r   r$   r   Ú__call__>   s    zscope.__call__)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústrr   ÚdictÚMetricValueTyper   r   r   r   r!   r'   r   r   r   r   r	      s    r	   c                       sP   e Zd ZdZd
eeeeef  ddœ‡ fdd„Z‡ fdd„Z	‡ fdd	„Z
‡  ZS )Úcpu_timed_scopezÈ
    A scope that measures elapsed time (cpu_time).

    Args:
        name (str): The name of the scope.
        metrics (dict[str, float], optional): Additional metrics to add. Default is None.
    Nr
   c                    s,   t ƒ  ||¡ d | _|r(d|v r(tdƒ‚d S )NÚcpu_timez'The metric name 'cpu_time' is reserved.)Úsuperr   Ú
start_timeÚ
ValueErrorr   ©Ú	__class__r   r   r   T   s    zcpu_timed_scope.__init__c                    s"   t ƒ s
d S t ¡ | _tƒ  ¡  d S r   )r   ÚtimeÚtime_nsr2   r1   r   r   r4   r   r   r   Z   s    
zcpu_timed_scope._enter_scopec                    sB   t ƒ s
d S tƒ  ¡  | jd ur>t ¡ | j }t | jd|i¡ d S )Nzcpu_time (ns)(exc))	r   r1   r   r2   r6   r7   r   r   r   )r   r0   r4   r   r   r   `   s    

zcpu_timed_scope._exit_scope)N)r(   r)   r*   r+   r,   r   r-   Úfloatr   r   r   Ú__classcell__r   r   r4   r   r/   K   s   $r/   F)Ú	triton_opr   )r   r:   r   r   c                C   sb   t ƒ s
dS t ¡ }ttdg ƒt_tj || f¡ |rBt || ¡ nt || ¡ |r^t 	||¡ |S )NéÿÿÿÿÚscopes)
r   r   r   ÚgetattrÚthread_local_scopesr<   ÚappendZenter_opr   r   )r   r:   r   r   r   r   r   r   i   s    r   )r:   r   r   c                 C   sJ   t ƒ s
dS tj ¡ \}}| r*t ||¡ nt ||¡ |rFt ||¡ |S )Nr;   )r   r>   r<   Úpopr   Zexit_opr   r   )r:   r   r   r   r   r   r   r   x   s    r   )FN)Ú	threadingr6   Ú	functoolsr   Útypingr   r   Úflagsr   Ztriton._C.libprotonr   r   Úlocalr>   r8   Úintr.   r	   r/   r,   Úboolr-   r   r   r   r   r   r   Ú<module>   s   =(