ó
ßüÚ\c           @` s  d  d l  m Z m Z m Z d  d l m Z d  d l Z d  d l Z d  d l Z d d l	 m
 Z
 m Z d d l m Z d d l m Z e j e ƒ Z Z d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ e ƒ  Z d  a e j ƒ  Z d „  Z e d „ Z d S(   i    (   t   print_functiont   divisiont   absolute_import(   t   dequeNi   (   t   PY2t   PYPY(   t   thread_time(   t   format_bytest   ThrottledGCc           B` s)   e  Z d  Z d d d d „ Z d „  Z RS(   s	  Wrap gc.collect to protect against excessively repeated calls.

    Allows to run throttled garbage collection in the workers as a
    countermeasure to e.g.: https://github.com/dask/zict/issues/19

    collect() does nothing when repeated calls are so costly and so frequent
    that the thread would spend more than max_in_gc_frac doing GC.

    warn_if_longer is a duration in seconds (10s by default) that can be used
    to log a warning level message whenever an actual call to gc.collect()
    lasts too long.
    gš™™™™™©?i   c         C` sF   | |  _  | |  _ t ƒ  |  _ d |  _ | d  k	 r9 | n t |  _ d  S(   Ni    (   t   max_in_gc_fract   warn_if_longerR   t   last_collectt   last_gc_durationt   Nonet   _loggert   logger(   t   selfR	   R
   R   (    (    s5   lib/python2.7/site-packages/distributed/utils_perf.pyt   __init__   s
    			c         C` sÛ   d } t  ƒ  } t | |  j | ƒ } |  j | |  j k  r¾ |  j j d | ƒ t j ƒ  | |  _ t t  ƒ  | | ƒ |  _ |  j |  j	 k r¥ |  j j
 d |  j ƒ q× |  j j d |  j ƒ n |  j j d |  j | ƒ d  S(   Ngíµ ÷Æ°>s9   Calling gc.collect(). %0.3fs elapsed since previous call.s¨   gc.collect() took %0.3fs. This is usually a sign that the some tasks handle too many Python objects at the same time. Rechunking the work into smaller tasks might help.s   gc.collect() took %0.3fssN   gc.collect() lasts %0.3fs but only %0.3fs elapsed since last call: throttling.(   R   t   maxR   R   R	   R   t   debugt   gct   collectR
   t   warning(   R   t   MIN_RUNTIMEt   collect_startt   elapsed(    (    s5   lib/python2.7/site-packages/distributed/utils_perf.pyR   %   s$    		

			N(   t   __name__t
   __module__t   __doc__R   R   R   (    (    (    s5   lib/python2.7/site-packages/distributed/utils_perf.pyR      s   t   FractionalTimerc           B` sJ   e  Z d  Z d Z e d „ Z d „  Z d „  Z d „  Z e	 d „  ƒ Z
 RS(   s    
    An object that measures runtimes, accumulates them and computes
    a running fraction of the recent runtimes over the corresponding
    elapsed time.
    g    eÍÍAc         C` sI   | |  _  | |  _ t ƒ  |  _ t ƒ  |  _ d  |  _ d  |  _ d  |  _ d  S(   N(	   t   _timert
   _n_samplesR   t   _start_stopst
   _durationsR   t
   _cur_startt   _running_sumt   _running_fraction(   R   t	   n_samplest   timer(    (    s5   lib/python2.7/site-packages/distributed/utils_perf.pyR   P   s    				c   
      C` s>  |  j  } |  j } | | k  s8 | r< | | d d k  r< d  St | | |  j ƒ } | j | | f ƒ | j | ƒ t | ƒ } | t | ƒ k s— t ‚ | |  j k r:|  j d  k rÜ | |  j k sÊ t ‚ t
 | ƒ |  _ q:| j ƒ  \ } } | j ƒ  }	 |  j | |	 7_ | | k r:|  j | | |  j |  _ q:n  d  S(   Niÿÿÿÿi   (   R    R!   t   intt   MULTt   appendt   lent   AssertionErrorR   R#   R   t   sumt   popleftR$   (
   R   t   startt   stopt   start_stopst	   durationst   durationt   nt	   old_startt   old_stopt   old_duration(    (    s5   lib/python2.7/site-packages/distributed/utils_perf.pyt   _add_measurementY   s$    		&c         C` s(   |  j  d  k s t ‚ |  j ƒ  |  _  d  S(   N(   R"   R   R+   R   (   R   (    (    s5   lib/python2.7/site-packages/distributed/utils_perf.pyt   start_timingt   s    c         C` sD   |  j  ƒ  } |  j } d  |  _ | d  k	 s0 t ‚ |  j | | ƒ d  S(   N(   R   R"   R   R+   R7   (   R   R/   R.   (    (    s5   lib/python2.7/site-packages/distributed/utils_perf.pyt   stop_timingx   s
    		c         C` s   |  j  S(   N(   R$   (   R   (    (    s5   lib/python2.7/site-packages/distributed/utils_perf.pyt   running_fraction   s    (   R   R   R   R(   R   R   R7   R8   R9   t   propertyR:   (    (    (    s5   lib/python2.7/site-packages/distributed/utils_perf.pyR   G   s   				t   GCDiagnosisc           B` s_   e  Z d  Z d Z d d d „ Z d „  Z d „  Z e d „  ƒ Z d	 „  Z	 d
 „  Z
 d „  Z RS(   sß   
    An object that hooks itself into the gc callbacks to collect
    timing and memory statistics, and log interesting info.

    Don't instantiate this directly except for tests.
    Instead, use the global instance.
    i   gš™™™™™¹?i
   g    €„.Ac         C` s   | |  _  | |  _ t |  _ d  S(   N(   t   _warn_over_fract   _info_over_rss_wint   Falset   _enabled(   R   t   warn_over_fract   info_over_rss_win(    (    s5   lib/python2.7/site-packages/distributed/utils_perf.pyR      s    		c         C` s¬   t  s t r d  S|  j s  t ‚ t d |  j ƒ |  _ y d d  l } Wn t k
 ra d  |  _
 n X| j ƒ  |  _
 |  j } | t j k s t ‚ t j j | ƒ t |  _ d  S(   NR%   i    (   R   R   R@   R+   R   t	   N_SAMPLESt   _fractional_timert   psutilt   ImportErrorR   t   _proct   Processt   _gc_callbackR   t	   callbacksR)   t   True(   R   RE   t   cb(    (    s5   lib/python2.7/site-packages/distributed/utils_perf.pyt   enable”   s    	c         C` s?   t  s t r d  S|  j s t ‚ t j j |  j ƒ t |  _ d  S(   N(	   R   R   R@   R+   R   RJ   t   removeRI   R?   (   R   (    (    s5   lib/python2.7/site-packages/distributed/utils_perf.pyt   disable¦   s
    c         C` s   |  j  S(   N(   R@   (   R   (    (    s5   lib/python2.7/site-packages/distributed/utils_perf.pyt   enabled­   s    c         C` s   |  j  ƒ  |  S(   N(   RM   (   R   (    (    s5   lib/python2.7/site-packages/distributed/utils_perf.pyt	   __enter__±   s    
c         G` s   |  j  ƒ  d  S(   N(   RO   (   R   t   args(    (    s5   lib/python2.7/site-packages/distributed/utils_perf.pyt   __exit__µ   s    c         C` s>  | d d k r d  S|  j  d  k	 r8 |  j  j ƒ  j } n d } | d k rd |  j j ƒ  | |  _ d  S| d k sv t ‚ |  j j ƒ  |  j j	 } | d  k	 rË | |  j
 k rË t j d d | d |  j
 ƒ n  |  j | } | |  j k rt j d t | ƒ | d	 t |  j ƒ ƒ n  | d
 d k r:t j d | d
 ƒ n  d  S(   Nt
   generationi   i    R.   R/   sF   full garbage collections took %d%% CPU time recently (threshold: %d%%)id   sL   full garbage collection released %s from %d reference cycles (threshold: %s)t	   collectedt   uncollectablesH   garbage collector couldn't collect %d objects, please look in gc.garbage(   RG   R   t   memory_infot   rssRD   R8   t   _gc_rss_beforeR+   R9   R:   R=   R   R   R>   t   infoR   (   R   t   phaseRZ   RX   t   fract	   rss_saved(    (    s5   lib/python2.7/site-packages/distributed/utils_perf.pyRI   ¸   s8    		g    ÐcA(   R   R   R   RC   R   RM   RO   R;   RP   RQ   RS   RI   (    (    (    s5   lib/python2.7/site-packages/distributed/utils_perf.pyR<   „   s   				c           C` sS   t  s t r d St 7 t d k r0 t j ƒ  n t j s? t ‚ t d 7a Wd QXd S(   s,   
    Ask to enable global GC diagnosis.
    Ni    i   (   R   R   t   _gc_diagnosis_lockt   _gc_diagnosis_userst   _gc_diagnosisRM   RP   R+   (    (    (    s5   lib/python2.7/site-packages/distributed/utils_perf.pyt   enable_gc_diagnosisæ   s    c         C` s{   t  s t r d St _ t d k rq t d 8a t d k rF t j ƒ  qq |  r_ t j ƒ  d a qq t j sq t ‚ n  Wd QXd S(   s-   
    Ask to disable global GC diagnosis.
    Ni    i   (   R   R   R^   R_   R`   RO   RP   R+   (   t   force(    (    s5   lib/python2.7/site-packages/distributed/utils_perf.pyt   disable_gc_diagnosisõ   s    

	(   t
   __future__R    R   R   t   collectionsR   R   t   loggingt	   threadingt   compatibilityR   R   t   metricsR   t   utilsR   t	   getLoggerR   R   R   t   objectR   R   R<   R`   R_   t   LockR^   Ra   R?   Rc   (    (    (    s5   lib/python2.7/site-packages/distributed/utils_perf.pyt   <module>   s    7=]		