
    ԋg#                    P   d dl mZ d dlZd dlZd dlZd dlmZ d dlmZm	Z	 d dl
Z
d dlmZ d dlmZ d dlmZ  ej"                  e      xZZej+                   edd	
              G d d      Z G d d      Z G d d      Z e       Zd a ej6                         Zd ZddZddZy)    )annotationsN)deque)CallableFinal)format_bytes)thread_time)RateLimiterFilterzfull garbage collections took60s)ratec                      e Zd ZdZddZd Zy)ThrottledGCa  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 (1s by default) that can be used
    to log a warning level message whenever an actual call to gc.collect()
    lasts too long.
    Nc                v    || _         || _        t               | _        d| _        ||| _        y t
        | _        y Nr   )max_in_gc_fracwarn_if_longerr   last_collectlast_gc_duration_loggerlogger)selfr   r   r   s       .lib/python3.12/site-packages/distributed/gc.py__init__zThrottledGC.__init__"   s6    ,,'M ! & 2f    c                H   d}t               }t        || j                  z
  |      }| j                  |z  | j                  k  r| j
                  j                  d|       t        j                          || _        t        t               |z
  |      | _        | j                  | j                  kD  r'| j
                  j                  d| j                         y | j
                  j                  d| j                         y | j
                  j                  d| j                  |       y )Ngư>z9Calling gc.collect(). %0.3fs elapsed since previous call.zgc.collect() took %0.3fs. This is usually a sign that some tasks handle too many Python objects at the same time. Rechunking the work into smaller tasks might help.zgc.collect() took %0.3fszNgc.collect() lasts %0.3fs but only %0.3fs elapsed since last call: throttling.)r   maxr   r   r   r   debuggccollectr   warning)r   MIN_RUNTIMEcollect_startelapseds       r   r   zThrottledGC.collect)   s     #md&7&77E  7*T-@-@@KKKW JJL -D$'(E{$SD!$$t':'::###
 )) !!"<d>S>STKK7%%	r   )g?   N)__name__
__module____qualname____doc__r   r    r   r   r   r      s    @r   r   c                      e Zd ZU dZdZded<   ded<   ded<   d	ed
<   ded<   ded<   ded<   ded<   ded<   efddZddZddZ	ddZ
edd       Zedd       Zy)FractionalTimerz
    An object that measures runtimes, accumulates them and computes
    a running fraction of the recent runtimes over the corresponding
    elapsed time.
    g    eAzFinal[float]MULTCallable[[], float]_timerint
_n_sampleszdeque[tuple[float, float]]_start_stopsz
deque[int]
_durationsfloat | None
_cur_startz
int | None_running_sum_running_fraction_duration_totalc                    || _         || _        t               | _        t               | _        d | _        d | _        d | _        d| _        y r   )	r-   r/   r   r0   r1   r3   r4   r5   r6   )r   	n_samplestimers      r   r   zFractionalTimer.__init__]   sB    #!G' !% r   c                ~   | j                   }| j                  }||k  s|r||d   d   k  ry t        ||z
  | j                  z        }|j	                  ||f       |j	                  |       | xj
                  |z  c_        t        |      }|t        |      k(  sJ || j                  k\  r| j                  "|| j                  k(  sJ t        |      | _        y |j                         \  }}|j                         }	| xj                  ||	z
  z  c_        ||k\  r%| j                  ||z
  z  | j                  z  | _        y y y )Nr#   )r0   r1   r.   r+   appendr6   lenr/   r4   sumpopleftr5   )
r   startstopstart_stops	durationsdurationn	old_startold_stopold_durations
             r   _add_measurementz FractionalTimer._add_measurementg   s:   ''OO	%<KEKOA4F,F u		12E4=)"(	NC$$$$  (DOO+++$'	N!&1&9&9&;#	8(002!!X%<<!9$))TH_=		I * %  r   c                J    | j                   J | j                         | _         y N)r3   r-   r   s    r   start_timingzFractionalTimer.start_timing   s    &&&++-r   c                v    | j                         }| j                  }d | _        |J | j                  ||       y rK   )r-   r3   rI   )r   rA   r@   s      r   stop_timingzFractionalTimer.stop_timing   s9    {{}   eT*r   c                    d}| j                   | j                         | j                   z
  }| j                  | j                  z  |z   S )Ng        )r3   r-   r6   r+   )r   current_durations     r   duration_totalzFractionalTimer.duration_total   sB    ??&#{{}t>##dii/2BBBr   c                    | j                   S rK   )r5   rL   s    r   running_fractionz FractionalTimer.running_fraction   s    %%%r   N)r8   r.   r9   r,   )r@   floatrA   rU   returnNone)rV   rW   rV   rU   )rV   r2   )r$   r%   r&   r'   r+   __annotations__r   r   rI   rM   rO   propertyrR   rT   r(   r   r   r*   r*   K   s     D,O,,##DO !8(+ C C & &r   r*   c                  J    e Zd ZdZdZddZd Zd Zed        Z	d Z
d Zd	 Zy
)GCDiagnosisz
    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.
       c                <    || _         || _        d| _        d | _        y NF)_info_over_frac_info_over_rss_win_enabled_fractional_timer)r   info_over_fracinfo_over_rss_wins      r   r   zGCDiagnosis.__init__   s!    -"3!%r   c                   | j                   rJ t        | j                        | _        t	        j
                         | _        | j                  }|t        j                  vsJ t        j                  j                  |       d| _         y )N)r8   T)rb   r*   	N_SAMPLESrc   psutilProcess_proc_gc_callbackr   	callbacksr<   )r   cbs     r   enablezGCDiagnosis.enable   sd    ==  !04>>!J^^%
%%%
Br   c                    | j                   sJ t        j                  j                  | j                         d| _         y r_   )rb   r   rl   removerk   rL   s    r   disablezGCDiagnosis.disable   s-    }}}
D--.r   c                    | j                   S rK   )rb   rL   s    r   enabledzGCDiagnosis.enabled   s    }}r   c                &    | j                          | S rK   )rn   rL   s    r   	__enter__zGCDiagnosis.__enter__   s    r   c                $    | j                          y rK   )rq   )r   exc_type	exc_value	tracebacks       r   __exit__zGCDiagnosis.__exit__   s    r   c           	        |d   dk7  ry | j                   j                         j                  }|dk(  r"| j                  j	                          || _        y |dk(  sJ | j                  j                          | j                  j                  }|W|| j                  k\  rt        j                  nt        j                  }t        j                  |dd|z  d| j                  z         | j
                  |z
  }|| j                  k\  rt        j                  nt        j                  }t        j                  |dt        |      |d   t        | j                               |d	   d
kD  rt        j!                  d|d	          y y )N
generation   r@   rA   zFfull garbage collections took %d%% CPU time recently (threshold: %d%%)d   zLfull garbage collection released %s from %d reference cycles (threshold: %s)	collecteduncollectabler   zHgarbage collector couldn't collect %d objects, please look in gc.garbage)rj   memory_inforssrc   rM   _gc_rss_beforerO   rT   r`   loggingINFODEBUGr   logra   r   r   )r   phaseinfor   fraclevel	rss_saveds          r   rk   zGCDiagnosis._gc_callback   sX    "jj$$&**G""//1"%D**,%%66$(D,@,@$@GLLgmmEJJ-d
d*** ''#-	 )T-D-D D'--

7#001	
  1$NN,_% %r   N)g?g    cA)r$   r%   r&   r'   rg   r   rn   rq   rZ   rs   ru   rz   rk   r(   r   r   r\   r\      s@     I&	
  &r   r\   c                     t         5  t        dk(  rt        j                          nt        j                  sJ t        dz  addd       y# 1 sw Y   yxY w)z,
    Ask to enable global GC diagnosis.
    r   r#   N)_gc_diagnosis_lock_gc_diagnosis_users_gc_diagnosisrn   rs   r(   r   r   enable_gc_diagnosisr      sA    
 
!#  " ((((q  
		s   :A

Ac                    t         5  t        dkD  rRt        dz  at        dk(  rt        j                          n+| rt        j                          dant        j                  sJ ddd       y# 1 sw Y   yxY w)z-
    Ask to disable global GC diagnosis.
    r   r#   N)r   r   r   rq   rs   )forces    r   disable_gc_diagnosisr     s^    
 
"1$"a'%%'%%'&'#$,,,, 
		s   AA,,A5c                 X    t         j                  yt         j                  j                  S r   )r   rc   rR   r(   r   r   gc_collect_durationr     s#    &&.**999r   )FrX   ) 
__future__r   r   r   	threadingcollectionsr   typingr   r   rh   
dask.utilsr   distributed.metricsr   distributed.utilsr	   	getLoggerr$   r   r   	addFilterr   r*   r\   r   r   Lockr   r   r   r   r(   r   r   <module>r      s    " 	    "  # + /$7$$X. .   "#BO P4 4nL& L&^R Rj  #Y^^% 
!-":r   