o
    ›¨Êhe  ã                   @   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y?   dZY nw 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      pBÚTB)g      ÐAÚGB)g      0AÚMB)g      �@ÚKB)g        Úbc                 C   s   dd l }td| |¡› �ƒ‚)Nr   z!Blocking detection timed-out at: )ÚinspectÚRuntimeErrorÚgetframeinfo)ÚsignumÚframer   © r   úD/var/www/html/env/lib/python3.10/site-packages/celery/utils/debug.pyÚ_on_blocking"   s   ÿr   c              	   c   sn   � | sdV  dS t d }|tkrdn|}tt d< zt  | ¡V  W |r&|t d< t  ¡  dS |r2|t d< t  ¡  w )zqContext that raises an exception if process is blocking.

    Uses ``SIGALRM`` to detect blocking functions.
    NÚALRM)r   r   Ú	arm_alarmÚreset_alarm)ÚtimeoutÚ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)Ú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   ÚgcÚcollectr   )ÚsamplesÚSÚprevr(   Úafter_collectr   r   r   Ú_memdumpI   s   r.   c                 C   sh   t t|d�}tƒ du r|dƒ dS t| ƒ\}}|r*|dƒ |D ]
}|d|› d�ƒ q|d|› d�ƒ dS )	z¼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`.
    ©ÚfileNz- rss: (psutil not installed).z- rss (sample):z-    > ú,z- rss (end): Ú.)r   Úprintr   r.   )r*   r0   Úsayr,   r-   Úmemr   r   r   r
   S   s   
r
   c              	   c   sN   � t | ƒ| }t|ƒD ]}z| | V  W n ty   Y  dS w ||7 }qdS )zÎGiven 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 )z¨Convert 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)Úfr?   Úir   r   r   Úhfloatw   s   rD   c                    s   t ‡ fdd„tD ƒƒS )z4Convert bytes to human-readable form (e.g., KB, MB).c                 3   s8   � | ]\}}ˆ |krt |rˆ | nˆ ƒ› |› �V  qd S ©N)rD   )Ú.0ÚdivÚunit©Úsr   r   Ú	<genexpr>„   s   € ÿþzhumanbytes.<locals>.<genexpr>)ÚnextÚUNITSrI   r   rI   r   r   ‚   s   þr   c                  C   s    t ƒ } | durtt| ƒjƒS dS )z.Return RSS memory usage as a humanized string.N)r   r   Ú_process_memory_infoÚrssr>   r   r   r   r   Š   s   ÿr   c                   C   s    t du rtdurtt ¡ ƒa t S )z…Return 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y   |  ¡  Y S w rE   )Úmemory_infoÚAttributeErrorÚget_memory_info)Úprocessr   r   r   rN   �   s
   
ÿrN   ú=é1   c           
      C   s¾   ddl }| du rtƒ n| } tt| d�}dd„ | ¡ D ƒ}|| }t ¡  ¡ D ]4\}}| |¡}	|	s2q&||	j	› ƒ ||ƒ t
j|| d� ||ƒ |dƒ ||ƒ t|j| d� |dƒ q&|  ¡ 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)rF   Útr   r   r   Ú
<dictcomp>±   s    zcry.<locals>.<dictcomp>zLOCAL VARIABLES)ÚstreamÚ
)Ú	threadingr   r   r3   Ú	enumerateÚsysÚ_current_framesÚitemsÚgetÚnameÚ	tracebackÚprint_stackr   Úf_localsÚgetvalue)
ÚoutÚsepchrÚseplenr^   ÚPÚtmapÚsepÚtidr   Úthreadr   r   r   r   ¤   s$   

r   )r%   )r%   N)r   )r=   )NrW   rX   ) Ú__doc__rQ   r`   re   Ú
contextlibr   Ú	functoolsr   r   Úcelery.platformsr   Úcelery.utils.textr   Úpsutilr   ÚImportErrorÚ__all__rM   rP   r"   r   r   r	   r.   r
   r   rD   r   r   r   rN   r   r   r   r   r   Ú<module>   s>    ÿ






