U
    de                     @   s  d Z ddlZddl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
 ddlmZ zddlmZ W n ek
r   dZY nX d	Zd
Zdag Zdd Zedd Zdd Zd'ddZd(ddZd)ddZd*ddZdd Zdd Zdd  Zd!d" Zd+d%d&ZdS ),z:Utilities for debugging memory usage, blocking calls, etc.    N)contextmanager)partial)pprint)signals)
WhateverIO)Process)blockdetection
sample_memmemdumpsample
humanbytesmem_rsspscry))g      pBZTB)g      AZGB)g      0AZMB)g      @ZKB)g        bc                 C   s    dd l }td|| d S )Nr   z!Blocking detection timed-out at: )inspectRuntimeErrorgetframeinfo)signumframer    r   6/tmp/pip-unpacked-wheel-mu1yl971/celery/utils/debug.py_on_blocking"   s    r   c              	   c   sX   | sdV  nHt d }|tkr dn|}tt d< zt | V  W 5 |rJ|t d< t   X dS )zqContext that raises an exception if process is blocking.

    Uses ``SIGALRM`` to detect blocking functions.
    NZALRM)r   r   Zreset_alarmZ	arm_alarm)timeoutZold_handlerr   r   r   r   )   s    r   c                  C   s   t  } t|  | S )z\Sample RSS memory usage.

    Statistics can then be output by calling :func:`memdump`.
    )r   _mem_sampleappend)Zcurrent_rssr   r   r   r	   ?   s    
r	   
   c                 C   sL   t }t|| krt|nt|| }g t d d < dd l}|  t }||fS )Nr   )r   lenlistr   gcZcollectr   )samplesSprevr   after_collectr   r   r   _memdumpI   s    r$   c                 C   sh   t t|d}t dkr"|d dS t| \}}|rT|d |D ]}|d| d q>|d| d dS )	zDump 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`.
    fileNz- rss: (psutil not installed).z- rss (sample):z-    > ,z- rss (end): .)r   printr   r$   )r    r&   Zsayr"   r#   Zmemr   r   r   r
   S   s    
r
   c              	   c   sP   t | | }t|D ]6}z| | V  W n tk
r@   Y  qLY nX ||7 }qdS )zGiven a list `x` a sample of length ``n`` of that list is returned.

    For example, if `n` is 10, and `x` has 100 items, a list of every tenth.
    item is returned.

    ``k`` can be used as offset.
    N)r   range
IndexError)xnkj_r   r   r   r   f   s    r      c                 C   s"   t | }|| kr|S dj| |dS )zConvert float to value suitable for humans.

    Arguments:
        f (float): The floating point number.
        p (int): Floating point precision (default is 5).
    z{0:.{p}}p)intformat)fr3   ir   r   r   hfloatw   s    r8   c                    s   t  fddtD S )z4Convert bytes to human-readable form (e.g., KB, MB).c                 3   s6   | ].\}} |krt |r  | n  | V  qd S N)r8   ).0divunitsr   r   	<genexpr>   s    zhumanbytes.<locals>.<genexpr>)nextUNITSr=   r   r=   r   r      s    r   c                  C   s    t  } | dk	rtt| jS dS )z.Return RSS memory usage as a humanized string.N)r   r   _process_memory_infoZrssr2   r   r   r   r      s    r   c                   C   s    t dkrtdk	rtt a t S )zReturn the global :class:`psutil.Process` instance.

    Note:
        Returns :const:`None` if :pypi:`psutil` is not installed.
    N)_processr   osgetpidr   r   r   r   r      s    r   c                 C   s,   z
|   W S  tk
r&   |   Y S X d S r9   )Zmemory_infoAttributeErrorZget_memory_info)processr   r   r   rB      s    
rB   =1   c           
      C   s   ddl }| dkrt n| } tt| d}dd | D }|| }t  D ]h\}}||}	|	sdqL||	j	  || t
j|| d || |d || t|j| d |d qL|  S )	zpReturn stack-trace of all active threads.

    See Also:
        Taken from https://gist.github.com/737056.
    r   Nr%   c                 S   s   i | ]}|j |qS r   )ident)r:   tr   r   r   
<dictcomp>   s      zcry.<locals>.<dictcomp>zLOCAL VARIABLES)stream
)	threadingr   r   r)   	enumeratesys_current_framesitemsgetname	tracebackprint_stackr   f_localsgetvalue)
outZsepchrZseplenrO   PZtmapseptidr   threadr   r   r   r      s$    

r   )r   )r   N)r   )r1   )NrH   rI   ) __doc__rD   rQ   rV   
contextlibr   	functoolsr   r   Zcelery.platformsr   Zcelery.utils.textr   Zpsutilr   ImportError__all__rA   rC   r   r   r   r	   r$   r
   r   r8   r   r   r   rB   r   r   r   r   r   <module>   s:   







