σ
P'^c           @@ sT  d  Z  d d l m Z m Z m Z d d l Z d d l m Z d d l m	 Z	 d d l
 m Z d d l m Z y d d l m Z Wn e k
 r d Z n Xd	 d
 d d d d d g Z d) d+ d- d/ d0 f Z d a g  Z d   Z e d    Z d   Z d d  Z d d d   Z d d!  Z d" d#  Z d$   Z d%   Z d&   Z d'   Z  d S(1   u[   
    celery.utils.debug
    ~~~~~~~~~~~~~~~~~~

    Utilities for debugging memory usage.

i    (   t   absolute_importt   print_functiont   unicode_literalsN(   t   contextmanager(   t   partial(   t   range(   t   signals(   t   Processu   blockdetectionu
   sample_memu   memdumpu   sampleu
   humanbytesu   mem_rssu   psi   g      D@u   TBg      >@u   GBg      4@u   MBg      $@u   kBg        u   {0!d}bc         C@ s.   d d  l  } t d j | j |     d  S(   Ni    u$   Blocking detection timed-out at: {0}(   t   inspectt   RuntimeErrort   formatt   getframeinfo(   t   signumt   frameR   (    (    s4   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/debug.pyt   _on_blocking)   s    c         c@ sq   |  s d Vn_ t  d } | t k r* d n | } t t  d <z t  j |   VWd | rb | t  d <n  t  j   Xd S(   uV   A timeout context using ``SIGALRM`` that can be used to detect blocking
    functions.Nu   ALRM(   R   R   t   Nonet	   arm_alarmt   reset_alarm(   t   timeoutt   old_handler(    (    s4   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/debug.pyt   blockdetection2   s    

c          C@ s   t    }  t j |   |  S(   u]   Sample RSS memory usage.

    Statistics can then be output by calling :func:`memdump`.

    (   t   mem_rsst   _mem_samplet   append(   t   current_rss(    (    s4   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/debug.pyt
   sample_memF   s    	i
   c         C@ sc   t  } t |  |  k r$ t |  n t | |   } g  t  (d d  l } | j   t   } | | f S(   Ni    (   R   t   lent   listt   samplet   gct   collectR   (   t   samplest   St   prevR   t   after_collect(    (    s4   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/debug.pyt   _memdumpQ   s    -
	c         C@ s   t  t d | } t   d k r/ | d  d St |   \ } } | rx | d  x$ | D] } | d j |   qX Wn  | d j |   d S(   u½   Dump memory statistics.

    Will print a sample of all RSS memory samples added by
    calling :func:`sample_mem`, and in addition print
    used RSS memory after :func:`gc.collect`.

    t   fileu   - rss: (psutil not installed).Nu   - rss (sample):u   -    > {0},u   - rss (end): {0}.(   R   t   printt   psR   R#   R
   (   R   R$   t   sayR!   R"   t   mem(    (    s4   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/debug.pyt   memdump[   s    

c         c@ sW   t  |   | } x@ t |  D]2 } y |  | VWn t k
 rD Pn X| | 7} q Wd S(   uΕ   Given a list `x` a sample of length ``n`` of that list is returned.

    E.g. if `n` is 10, and `x` has 100 items, a list of every 10th
    item is returned.

    ``k`` can be used as offset.

    N(   R   R   t
   IndexError(   t   xt   nt   kt   jt   _(    (    s4   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/debug.pyR   o   s    	i   c         C@ s/   t  |   } | |  k r | Sd j |  d | S(   uS   Convert float to value suitable for humans.

    :keyword p: Float precision.

    u   {0:.{p}}t   p(   t   intR
   (   t   fR0   t   i(    (    s4   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/debug.pyt   hfloat   s    c         @ s   t    f d   t D  S(   u3   Convert bytes to human-readable form (e.g. kB, MB).c         3@ sI   |  ]? \ } }   | k r d  j  t | r4   | n    |  Vq d S(   u   {0}{1}N(   R
   R4   (   t   .0t   divt   unit(   t   s(    s4   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/debug.pys	   <genexpr>   s   (   t   nextt   UNITS(   R8   (    (   R8   s4   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/debug.pyt
   humanbytes   s    c          C@ s,   t    }  |  d k	 r( t t |   j  Sd S(   u.   Return RSS memory usage as a humanized string.N(   R&   R   R;   t   _process_memory_infot   rss(   R0   (    (    s4   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/debug.pyR      s    	c           C@ s1   t  d k r- t d k	 r- t t j    a  n  t  S(   uk   Return the global :class:`psutil.Process` instance,
    or :const:`None` if :mod:`psutil` is not installed.N(   t   _processR   R   t   ost   getpid(    (    (    s4   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/debug.pyR&      s    c         C@ s-   y |  j    SWn t k
 r( |  j   SXd  S(   N(   t   memory_infot   AttributeErrort   get_memory_info(   t   process(    (    s4   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/debug.pyR<   £   s    g      pB(   g      pBu   TBg      ΠA(   g      ΠAu   GBg      0A(   g      0Au   MBg      @(   g      @u   kB(   g        u   {0!d}b(!   t   __doc__t
   __future__R    R   R   R?   t
   contextlibR   t	   functoolsR   t   celery.fiveR   t   celery.platformsR   t   psutilR   t   ImportErrorR   t   __all__R:   R>   R   R   R   R   R#   R)   R   R4   R;   R   R&   R<   (    (    (    s4   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/debug.pyt   <module>   s<   
				

				