
    bih/                        d dl mZ d dlmZ d dlmZ d dlmZmZm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZ  ed	d
      Z G d de      Z edd      Z G d de      Z G d de      Z edd      Z G d de      Zy)    )annotations)
namedtuple)starmap)PipeProcesscurrent_process)sleep)default_timer)Callback)import_requiredTaskData)keytask
start_timeend_time	worker_idc                  \     e Zd ZdZd Z fdZ fdZd Zd Zd Z	d Z
d	 Zd
 Zd Z xZS )Profilera  A profiler for dask execution at the task level.

    Records the following information for each task:
        1. Key
        2. Task
        3. Start time in seconds since the epoch
        4. Finish time in seconds since the epoch
        5. Worker id

    Examples
    --------

    >>> from operator import add, mul
    >>> from dask.threaded import get
    >>> from dask.diagnostics import Profiler
    >>> dsk = {'x': 1, 'y': (add, 'x', 10), 'z': (mul, 'y', 2)}
    >>> with Profiler() as prof:
    ...     get(dsk, 'z')
    22

    >>> prof.results        # doctest: +SKIP
    [TaskData(key='y', task=(add, 'x', 10), start_time=..., end_time=..., worker_id=...),
     TaskData(key='z', task=(mul, 'y', 2), start_time=..., end_time=..., worker_id=...)]

    These results can be visualized in a bokeh plot using the ``visualize``
    method. Note that this requires bokeh to be installed.

    >>> prof.visualize()    # doctest: +SKIP

    You can activate the profiler globally

    >>> prof.register()

    If you use the profiler globally you will need to clear out old results
    manually.

    >>> prof.clear()
    >>> prof.unregister()

    c                J    i | _         g | _        i | _        d | _        d | _        y N)_resultsresults_dskr   r   selfs    S/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/dask/diagnostics/profile.py__init__zProfiler.__init__<   s%    	    c                ^    | j                          t               | _        t        |          S r   clearr
   r   super	__enter__r   	__class__s    r   r#   zProfiler.__enter__C   #    

'/w ""r   c                :    t               | _        t        |   | S r   r
   r   r"   __exit__r   argsr%   s     r   r)   zProfiler.__exit__H       %w&&r   c                :    | j                   j                  |       y r   r   updater   dsks     r   _startzProfiler._startL       		r   c                B    t               }|||   |f| j                  |<   y r   r
   r   )r   r   r1   statestarts        r   _pretaskzProfiler._pretaskO   s"    !3s8U3cr   c                J    t               }| j                  |xx   ||fz  cc<   y r   r5   )r   r   valuer1   r6   idends          r   	_posttaskzProfiler._posttaskS   s!    ocsBi'r   c           	     0   | j                   j                         D ci c]  \  }}t        |      dk(  s|| }}}| xj                  t	        t        t        |j                                     z  c_        | j                   j                          y c c}}w )N   )	r   itemslenr   listr   r   valuesr!   )r   r1   r6   failedkvr   s          r   _finishzProfiler._finishW   sk    $(MM$7$7$9IDAqSVq[1a4IIWXw~~/?@AA Js
   BBc                v    ddl m}  || j                  | j                  | j                  | j
                  fi |S )Nr   )
plot_tasks)"dask.diagnostics.profile_visualizerI   r   r   r   r   )r   kwargsrI   s      r   _plotzProfiler._plot\   s3    ALL$))T__dmm
GM
 	
r   c                     ddl m}  || fi |S zVisualize the profiling run in a bokeh plot.

        See also
        --------
        dask.diagnostics.profile_visualize.visualize
        r   )	visualizerJ   rO   r   rK   rO   s      r   rO   zProfiler.visualizec        	A(((r   c                    | j                   j                          | j                  dd= i | _        d| _        d| _        yz#Clear out old results from profilerN)r   r!   r   r   r   r   r   s    r   r!   zProfiler.clearn   s2    LLO	r   )__name__
__module____qualname____doc__r   r#   r)   r2   r8   r=   rG   rL   rO   r!   __classcell__r%   s   @r   r   r      s;    'R#
'4(

	)r   r   ResourceData)timememcpuc                  n     e Zd ZdZddZd Zd Zd Z fdZ fdZ	d Z
d	 Zd
 ZeZd Zd Zd Z xZS )ResourceProfilera   A profiler for resource use.

    Records the following each timestep
        1. Time in seconds since the epoch
        2. Memory usage in MB
        3. % CPU usage

    Examples
    --------

    >>> from operator import add, mul
    >>> from dask.threaded import get
    >>> dsk = {'x': 1, 'y': (add, 'x', 10), 'z': (mul, 'y', 2)}
    >>> with ResourceProfiler() as prof:
    ...     get(dsk, 'z')
    22

    These results can be visualized in a bokeh plot using the ``visualize``
    method. Note that this requires bokeh to be installed.

    >>> prof.visualize() # doctest: +SKIP

    You can activate the profiler globally

    >>> prof.register()

    If you use the profiler globally you will need to clear out old results
    manually.

    >>> prof.clear()

    Note that when used as a context manager data will be collected throughout
    the duration of the enclosed block. In contrast, when registered globally
    data will only be collected while a dask scheduler is active.

    >>> prof.unregister()
    c                X    || _         d| _        d | _        g | _        d | _        d | _        y NF)_dt_entered_trackerr   r   r   )r   dts     r   r   zResourceProfiler.__init__   s,    r   c                V    | j                   d uxr | j                   j                         S r   )re   is_aliver   s    r   _is_runningzResourceProfiler._is_running   s#    }}D(ET]]-C-C-EEr   c                    | j                         s4t        | j                        | _        | j                  j	                          | j                  j
                  j                  d       y )Ncollect)ri   _Trackerrc   re   r7   parent_connsendr   s    r   _start_collectzResourceProfiler._start_collect   sF    !$TXX.DMMM!!!&&y1r   c                   | j                         rq| j                  j                  j                  d       | j                  j                  t        t        | j                  j                  j                                      y y )N	send_data)	ri   re   rm   rn   r   extendr   r[   recvr   s    r   _stop_collectzResourceProfiler._stop_collect   sW    MM%%**;7LLdmm6O6O6T6T6V WX r   c                    d| _         | j                          t               | _        | j	                          t
        |          S NT)rd   r!   r
   r   ro   r"   r#   r$   s    r   r#   zResourceProfiler.__enter__   s6    

'/w ""r   c                    d| _         | j                          | j                          t               | _        t        |   |  y rb   )rd   rt   closer
   r   r"   r)   r*   s     r   r)   zResourceProfiler.__exit__   s5    

%$r   c                $    | j                          y r   )ro   r0   s     r   r2   zResourceProfiler._start   s    r   c                >    | j                   s| j                          y y r   )rd   rt   )r   r1   r6   rD   s       r   rG   zResourceProfiler._finish   s    }}  r   c                h    | j                         r"| j                  j                          d| _        yy)z%Shutdown the resource tracker processN)ri   re   shutdownr   s    r   rx   zResourceProfiler.close   s*    MM""$ DM r   c                .    g | _         d | _        d | _        y r   )r   r   r   r   s    r   r!   zResourceProfiler.clear   s    r   c                `    ddl m}  || j                  | j                  | j                  fi |S )Nr   )plot_resources)rJ   r   r   r   r   )r   rK   r   s      r   rL   zResourceProfiler._plot   s%    EdllDOOT]]UfUUr   c                     ddl m}  || fi |S rN   rP   rQ   s      r   rO   zResourceProfiler.visualize   rR   r      )rU   rV   rW   rX   r   ri   ro   rt   r#   r)   r2   rG   rx   __del__r!   rL   rO   rY   rZ   s   @r   r`   r`   z   sO    $LF2Y
# !! G
V
	)r   r`   c                  6     e Zd ZdZd fd	Zd Zd Zd Z xZS )rl   z.Background process for tracking resource usagec                    t         |           d| _        || _        t	               j
                  | _        t               \  | _        | _	        y rv   )
r"   r   daemonrf   r   pid
parent_pidr   rm   
child_conn)r   rf   r%   s     r   r   z_Tracker.__init__   s=    )+//,0F)$/r   c                    | j                   j                  s5| j                   j                  d       | j                   j                          | j	                          y )Nr|   )rm   closedrn   rx   joinr   s    r   r|   z_Tracker.shutdown   s@    &&!!*-""$		r   c                    | j                   g| j                   j                         D cg c](  }|j                  |k7  s|j                         dk7  s'|* c}z   S c c}w )Nzombie)parentchildrenr   status)r   r   ps      r   _update_pidsz_Tracker._update_pids   sR    }{{++- 
#!((*PXBXA 
 
 	
  
s   AAAc                    t        dd      }|j                  | j                        | _        t	               }g }	 	 | j
                  j                         }|dk(  rn|dk(  r| j                  |      }|r| j
                  j                         st               }dx}}|D ]7  }		 |	j                         j                  }
|	j                         }||
z  }||z  }9 |j                  ||dz  |f       t!        | j"                         |sy| j
                  j                         sn"|dk(  r| j
                  j%                  |       g }| j
                  j'                          y # t        $ r Y 4w xY w# t        $ r Y w xY w)Npsutilz9Tracking resource usage requires `psutil` to be installedr|   rk   r   g    .Arq   )r   r   r   r   r   r   rs   KeyboardInterruptr   pollr
   memory_inforsscpu_percent	Exceptionappendr	   rf   rn   rx   )r   r   r   datamsgpsticr]   r^   r   mem2cpu2s               r   runz_Tracker.run   sq    Q
 nnT__5oo**, j 	!&&s+doo&:&:&<'/C !MC# 	((#$==?#6#6D#$==?D
  4KC4KC	( KKcCi 56$''N doo&:&:&< #$$T*5 6 	1 %   ) ! !s#   E! !*E1!	E.-E.1	E=<E=r   )	rU   rV   rW   rX   r   r|   r   r   rY   rZ   s   @r   rl   rl      s    83

# r   rl   	CacheData)r   r   metric
cache_time	free_timec                  X     e Zd ZdZddZ fdZ fdZd Zd Zd Z	d Z
d	 Zd
 Z xZS )CacheProfilera]  A profiler for dask execution at the scheduler cache level.

    Records the following information for each task:
        1. Key
        2. Task
        3. Size metric
        4. Cache entry time in seconds since the epoch
        5. Cache exit time in seconds since the epoch

    Examples
    --------

    >>> from operator import add, mul
    >>> from dask.threaded import get
    >>> from dask.diagnostics import CacheProfiler
    >>> dsk = {'x': 1, 'y': (add, 'x', 10), 'z': (mul, 'y', 2)}
    >>> with CacheProfiler() as prof:
    ...     get(dsk, 'z')
    22

    >>> prof.results    # doctest: +SKIP
    [CacheData(key='y', task=(add, 'x', 10), metric=1, cache_time=..., free_time=...),
     CacheData(key='z', task=(mul, 'y', 2), metric=1, cache_time=..., free_time=...)]

    The default is to count each task (``metric`` is 1 for all tasks). Other
    functions may used as a metric instead through the ``metric`` keyword. For
    example, the ``nbytes`` function found in ``cachey`` can be used to measure
    the number of bytes in the cache.

    >>> from cachey import nbytes                   # doctest: +SKIP
    >>> with CacheProfiler(metric=nbytes) as prof:  # doctest: +SKIP
    ...     get(dsk, 'z')
    22

    The profiling results can be visualized in a bokeh plot using the
    ``visualize`` method. Note that this requires bokeh to be installed.

    >>> prof.visualize() # doctest: +SKIP

    You can activate the profiler globally

    >>> prof.register()

    If you use the profiler globally you will need to clear out old results
    manually.

    >>> prof.clear()
    >>> prof.unregister()

    c                    | j                          |r|nd | _        |r|| _        y |r|j                  | _        y d| _        y )Nc                     y)Nr    )r:   s    r   <lambda>z(CacheProfiler.__init__.<locals>.<lambda>`  s    r   count)r!   _metric_metric_namerU   )r   r   metric_names      r   r   zCacheProfiler.__init__^  s8    

!'v_ +D &D 'Dr   c                ^    | j                          t               | _        t        |          S r   r    r$   s    r   r#   zCacheProfiler.__enter__h  r&   r   c                :    t               | _        t        |   | S r   r(   r*   s     r   r)   zCacheProfiler.__exit__m  r,   r   c                :    | j                   j                  |       y r   r.   r0   s     r   r2   zCacheProfiler._startq  r3   r   c           
     4   t               }| j                  |      |f| j                  |<   |d   | j                  j                         z  D ]K  }| j                  j	                  |      \  }}	| j
                  j                  t        |||   ||	|             M y )Nreleased)r
   r   _cachekeyspopr   r   r   )
r   r   r:   r1   r6   r;   trE   r   r7   s
             r   r=   zCacheProfiler._posttaskt  s    O LL/3Cz"T[[%5%5%77 	HA KKOOA.MFELL	!SVVUA FG	Hr   c           
         t               }| j                  j                         D ]3  \  }\  }}| j                  j	                  t        |||   |||             5 | j                  j                          y r   )r
   r   r@   r   r   r   r!   )r   r1   r6   rD   r   rE   r   r7   s           r   rG   zCacheProfiler._finish{  sc    O"&++"3"3"5 	HALL	!SVVUA FG	Hr   c                    ddl m}  || j                  | j                  | j                  | j
                  | j                  fi |S )Nr   )
plot_cache)rJ   r   r   r   r   r   r   )r   rK   r   s      r   rL   zCacheProfiler._plot  sA    ALLIIOOMM
 
 	
r   c                     ddl m}  || fi |S rN   rP   rQ   s      r   rO   zCacheProfiler.visualize  rR   r   c                J    g | _         i | _        i | _        d| _        d| _        yrT   )r   r   r   r   r   r   s    r   r!   zCacheProfiler.clear  s%    	r   )NN)rU   rV   rW   rX   r   r#   r)   r2   r=   rG   rL   rO   r!   rY   rZ   s   @r   r   r   *  s7    1f(#
'H

	)r   r   N)
__future__r   collectionsr   	itertoolsr   multiprocessingr   r   r   r\   r	   timeitr
   dask.callbacksr   
dask.utilsr   r   r   r[   r`   rl   r   r   r   r   r   <module>r      s    " "  : :    # & F
bx bJ .*@Am)x m)`8 w 8 v E	
tH tr   