ó
¦–Õ\c           @` s!  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 d  d l m Z m Z m Z d d l m Z d d	 l m Z e d
 d! ƒ Z d e f d „  ƒ  YZ e d d" ƒ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ e d d# ƒ Z d e f d „  ƒ  YZ d  S($   i    (   t   absolute_importt   divisiont   print_function(   t
   namedtuple(   t   starmap(   t   default_timer(   t   sleep(   t   Processt   Pipet   current_processi   (   t   Callback(   t   import_requiredt   TaskDatat   keyt   taskt
   start_timet   end_timet	   worker_idt   Profilerc           B` s_   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z RS(
   s4  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
    >>> dsk = {'x': 1, 'y': (add, 'x', 10), 'z': (mul, 'y', 2)}
    >>> with Profiler() as prof:
    ...     get(dsk, 'z')
    22

    >>> prof.results  # doctest: +SKIP
    [('y', (add, 'x', 10), 1435352238.48039, 1435352238.480655, 140285575100160),
     ('z', (mul, 'y', 2), 1435352238.480657, 1435352238.480803, 140285566707456)]

    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()  # doctest: +SKIP

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

    >>> prof.clear()

    c         C` s   i  |  _  g  |  _ i  |  _ d  S(   N(   t   _resultst   resultst   _dsk(   t   self(    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyt   __init__9   s    		c         C` s   |  j  ƒ  t t |  ƒ j ƒ  S(   N(   t   cleart   superR   t	   __enter__(   R   (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyR   >   s    
c         C` s   |  j  j | ƒ d  S(   N(   R   t   update(   R   t   dsk(    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyt   _startB   s    c         C` s'   t  ƒ  } | | | | f |  j | <d  S(   N(   R   R   (   R   R   R   t   statet   start(    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyt   _pretaskE   s    	c         C` s&   t  ƒ  } |  j | c | | f 7<d  S(   N(   R   R   (   R   R   t   valueR   R   t   idt   end(    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyt	   _posttaskI   s    	c         C` sT   t  d „  |  j j ƒ  Dƒ ƒ } |  j t t t | j ƒ  ƒ ƒ 7_ |  j j ƒ  d  S(   Nc         s` s3   |  ]) \ } } t  | ƒ d  k r | | f Vq d S(   i   N(   t   len(   t   .0t   kt   v(    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pys	   <genexpr>N   s    (	   t   dictR   t   itemsR   t   listR   R   t   valuesR   (   R   R   R   t   failedR   (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyt   _finishM   s    $c         K` s&   d d l  m } | |  j |  j |  S(   Ni   (   t
   plot_tasks(   t   profile_visualizeR/   R   R   (   R   t   kwargsR/   (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyt   _plotR   s    c         K` s   d d l  m } | |  |  S(   s   Visualize the profiling run in a bokeh plot.

        See also
        --------
        dask.diagnostics.profile_visualize.visualize
        i   (   t	   visualize(   R0   R3   (   R   R1   R3   (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyR3   V   s    c         C` s!   |  j  j ƒ  |  j 2i  |  _ d S(   s#   Clear out old results from profilerN(   R   R   R   R   (   R   (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyR   `   s    (   t   __name__t
   __module__t   __doc__R   R   R   R    R$   R.   R2   R3   R   (    (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyR      s   &								
t   ResourceDatat   timet   memt   cput   ResourceProfilerc           B` sƒ   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d	 „  Z
 d
 „  Z e Z d „  Z d „  Z d „  Z RS(   s  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:  # doctest: +SKIP
    ...     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()  # doctest: +SKIP

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

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

    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.
    i   c         C` s(   | |  _  t |  _ d  |  _ g  |  _ d  S(   N(   t   _dtt   Falset   _enteredt   Nonet   _trackerR   (   R   t   dt(    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyR   Ž   s    			c         C` s   |  j  d  k	 o |  j  j ƒ  S(   N(   R@   R?   t   is_alive(   R   (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyt   _is_running”   s    c         C` sE   |  j  ƒ  s. t |  j ƒ |  _ |  j j ƒ  n  |  j j j d ƒ d  S(   Nt   collect(   RC   t   _TrackerR<   R@   R   t   parent_connt   send(   R   (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyt   _start_collect—   s    c         C` sK   |  j  ƒ  rG |  j j j d ƒ |  j j t t |  j j j ƒ  ƒ ƒ n  d  S(   Nt	   send_data(	   RC   R@   RF   RG   R   t   extendR   R7   t   recv(   R   (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyt   _stop_collect   s    c         C` s0   t  |  _ |  j ƒ  |  j ƒ  t t |  ƒ j ƒ  S(   N(   t   TrueR>   R   RH   R   R;   R   (   R   (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyR   ¢   s    	

c         G` s7   t  |  _ |  j ƒ  |  j ƒ  t t |  ƒ j | Œ  d  S(   N(   R=   R>   RL   t   closeR   R;   t   __exit__(   R   t   args(    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyRO   ¨   s    	

c         C` s   |  j  ƒ  d  S(   N(   RH   (   R   R   (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyR   ®   s    c         C` s   |  j  s |  j ƒ  n  d  S(   N(   R>   RL   (   R   R   R   R-   (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyR.   ±   s    	c         C` s)   |  j  ƒ  r% |  j j ƒ  d |  _ n  d S(   s%   Shutdown the resource tracker processN(   RC   R@   t   shutdownR?   (   R   (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyRN   µ   s    c         C` s   g  |  _  d  S(   N(   R   (   R   (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyR   ½   s    c         K` s    d d l  m } | |  j |  S(   Ni   (   t   plot_resources(   R0   RR   R   (   R   R1   RR   (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyR2   À   s    c         K` s   d d l  m } | |  |  S(   s   Visualize the profiling run in a bokeh plot.

        See also
        --------
        dask.diagnostics.profile_visualize.visualize
        i   (   R3   (   R0   R3   (   R   R1   R3   (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyR3   Ä   s    (   R4   R5   R6   R   RC   RH   RL   R   RO   R   R.   RN   t   __del__R   R2   R3   (    (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyR;   j   s   #										RE   c           B` s5   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z RS(   s.   Background process for tracking resource usagei   c         C` sG   t  j |  ƒ t |  _ | |  _ t ƒ  j |  _ t ƒ  \ |  _	 |  _
 d  S(   N(   R   R   RM   t   daemonRA   R	   t   pidt
   parent_pidR   RF   t
   child_conn(   R   RA   (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyR   Ñ   s
    		c         C` s:   |  j  j s, |  j  j d ƒ |  j  j ƒ  n  |  j ƒ  d  S(   NRQ   (   RF   t   closedRG   RN   t   join(   R   (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyRQ   Ø   s    c         C` sK   |  j  g g  |  j  j ƒ  D]- } | j | k r | j ƒ  d k r | ^ q S(   Nt   zombie(   t   parentt   childrenRU   t   status(   R   RU   t   p(    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyt   _update_pidsÞ   s    c         C` s~  t  d d ƒ } | j |  j ƒ |  _ t ƒ  } g  } x7t rly |  j j ƒ  } Wn t k
 re q6 n X| d k rv Pq6 | d k rD|  j	 | ƒ } xÕ | s« |  j j
 ƒ  r@t ƒ  } d } } xU | D]M }	 y |	 j ƒ  j }
 |	 j ƒ  } Wn t k
 rý qÅ X| |
 7} | | 7} qÅ W| j | | d | f ƒ t |  j ƒ q” Wq6 | d k r6 |  j j | ƒ g  } q6 q6 W|  j j ƒ  d  S(   Nt   psutils9   Tracking resource usage requires `psutil` to be installedRQ   RD   i    g    €„.ARI   (   R   R   RV   R[   R	   RM   RW   RK   t   KeyboardInterruptR_   t   pollR   t   memory_infot   rsst   cpu_percentt	   Exceptiont   appendR   RA   RG   RN   (   R   R`   RU   t   datat   msgt   pst   ticR9   R:   R^   t   mem2t   cpu2(    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyt   runâ   s<    			

(   R4   R5   R6   R   RQ   R_   Rn   (    (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyRE   Ï   s
   		t	   CacheDatat   metrict
   cache_timet	   free_timet   CacheProfilerc           B` s\   e  Z d  Z d	 d	 d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z RS(
   sù  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
    >>> dsk = {'x': 1, 'y': (add, 'x', 10), 'z': (mul, 'y', 2)}
    >>> with CacheProfiler() as prof:
    ...     get(dsk, 'z')
    22

    >>> prof.results    # doctest: +SKIP
    [CacheData('y', (add, 'x', 10), 1, 1435352238.48039, 1435352238.480655),
     CacheData('z', (mul, 'y', 2), 1, 1435352238.480657, 1435352238.480803)]

    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')

    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()  # doctest: +SKIP

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

    >>> prof.clear()

    c         C` sV   |  j  ƒ  | r | n d „  |  _ | r4 | |  _ n | rI | j |  _ n	 d |  _ d  S(   Nc         S` s   d S(   Ni   (    (   R!   (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyt   <lambda>?  t    t   count(   R   t   _metrict   _metric_nameR4   (   R   Rp   t   metric_name(    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyR   =  s    
c         C` s   |  j  ƒ  t t |  ƒ j ƒ  S(   N(   R   R   Rs   R   (   R   (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyR   G  s    
c         C` s,   |  j  j | ƒ |  j s( t ƒ  |  _ n  d  S(   N(   R   R   t   _start_timeR   (   R   R   (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyR   K  s    	c   
      C` sˆ   t  ƒ  } |  j | ƒ | f |  j | <x\ | d j |  j ƒ D]D } |  j j | ƒ \ } }	 |  j j t | | | | |	 | ƒ ƒ q< Wd  S(   Nt   released(   R   Rw   t   _cachet   intersectiont   popR   Rg   Ro   (
   R   R   R!   R   R   R"   t   tR'   Rp   R   (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyR$   P  s
    	c         C` sf   t  ƒ  } xI |  j j ƒ  D]8 \ } \ } } |  j j t | | | | | | ƒ ƒ q W|  j j ƒ  d  S(   N(   R   R|   R*   R   Rg   Ro   R   (   R   R   R   R-   R   R'   Rp   R   (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyR.   W  s    	"*c         K` s2   d d l  m } | |  j |  j |  j |  j |  S(   Ni   (   t
   plot_cache(   R0   R€   R   R   Rz   Rx   (   R   R1   R€   (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyR2   ]  s    c         K` s   d d l  m } | |  |  S(   s   Visualize the profiling run in a bokeh plot.

        See also
        --------
        dask.diagnostics.profile_visualize.visualize
        i   (   R3   (   R0   R3   (   R   R1   R3   (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyR3   b  s    c         C` s(   g  |  _  i  |  _ i  |  _ d |  _ d S(   s#   Clear out old results from profilerN(   R   R|   R   R?   Rz   (   R   (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyR   l  s    			N(   R4   R5   R6   R?   R   R   R   R$   R.   R2   R3   R   (    (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyRs     s   /
						
N(   R   R   R   R   R   (   R8   R9   R:   (   R   R   Rp   Rq   Rr   (   t
   __future__R    R   R   t   collectionsR   t	   itertoolsR   t   timeitR   R8   R   t   multiprocessingR   R   R	   t	   callbacksR
   t   utilsR   R   R   R7   R;   RE   Ro   Rs   (    (    (    s7   lib/python2.7/site-packages/dask/diagnostics/profile.pyt   <module>   s    	Ue9	