
    ∋dr                       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Zd dlm	Z	 d dl
mZ  ej        e          xZZ G d d          Z G d d	          Z G d
 d          Z e            Zd a ej                    Zd ZddZdS )    )annotationsN)deque)format_bytes)thread_timec                       e Zd ZdZddZd ZdS )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 (10s 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| _        ||nt
          | _        d S )Nr   )max_in_gc_fracwarn_if_longerr   last_collectlast_gc_duration_loggerlogger)selfr   r   r   s       6lib/python3.11/site-packages/distributed/utils_perf.py__init__zThrottledGC.__init__   s:    ,,'MM ! & 2ff    c                   d}t                      }t          || j        z
  |          }| j        |z  | j        k     r| j                            d|           t          j                     || _        t          t                      |z
  |          | _        | j        | j	        k    r"| j        
                    d| j                   d S | j                            d| j                   d S | j                            d| j        |           d S )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EE 7*T-@@@KKW   JLLL -D$'(E{$S$SD!$t':::###
 )     !!"<d>STTTTTK7%	    r   )r	   r
   N)__name__
__module____qualname____doc__r   r    r   r   r   r      sE         @ @ @ @    r   r   c                  H    e Zd ZdZdZefdZd Zd Zd Z	e
d             ZdS )	FractionalTimerz
    An object that measures runtimes, accumulates them and computes
    a running fraction of the recent runtimes over the corresponding
    elapsed time.
    g    eAc                    || _         || _        t                      | _        t                      | _        d | _        d | _        d | _        d S N)_timer
_n_samplesr   _start_stops
_durations
_cur_start_running_sum_running_fraction)r   	n_samplestimers      r   r   zFractionalTimer.__init__Q   sD    #!GG'' !%r   c                \   | j         }| j        }||k     s|r||d         d         k     rd S t          ||z
  | j        z            }|                    ||f           |                    |           t          |          }|t          |          k    sJ || j        k    r| j        #|| j        k    sJ t          |          | _        d S |	                                \  }}|	                                }	| xj        ||	z
  z  c_        ||k    r| j        ||z
  z  | j        z  | _
        d S d S d S )Nr
   )r*   r+   intMULTappendlenr)   r-   sumpopleftr.   )
r   startstopstart_stops	durationsdurationn	old_startold_stopold_durations
             r   _add_measurementz FractionalTimer._add_measurementZ   s\   'O	%<<K<EKOA4F,F,FF u	122E4=)))"""	NNC$$$$$$ (DO++++$'	NN!!!&1&9&9&;&;#	8(0022!!X%<<!!9$$)TH_=	I ***   %$r   c                J    | j         J |                                 | _         d S r'   )r,   r(   r   s    r   start_timingzFractionalTimer.start_timingu   s#    &&&++--r   c                ~    |                                  }| j        }d | _        |J |                     ||           d S r'   )r(   r,   rB   )r   r:   r9   s      r   stop_timingzFractionalTimer.stop_timingy   sE    {{}}   eT*****r   c                    | j         S r'   )r.   rD   s    r   running_fractionz FractionalTimer.running_fraction   s    %%r   N)r   r    r!   r"   r4   r   r   rB   rE   rG   propertyrI   r#   r   r   r%   r%   H   s          D(3 & & & &  6( ( (+ + + & & X& & &r   r%   c                  R    e Zd ZdZdZddZd Zd Zed             Z	d	 Z
d
 Zd ZdS )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.
       皙?    cAc                0    || _         || _        d| _        d S NF)_warn_over_frac_info_over_rss_win_enabled)r   warn_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	        
                    |           d| _         d S )N)r/   T)rT   r%   	N_SAMPLES_fractional_timerpsutilProcess_proc_gc_callbackr   	callbacksr5   )r   cbs     r   enablezGCDiagnosis.enable   sp    =   !04>!J!J!J^%%
%%%%
Br   c                n    | j         sJ t          j                            | j                   d| _         d S rQ   )rT   r   r^   remover]   rD   s    r   disablezGCDiagnosis.disable   s4    }
D-...r   c                    | j         S r'   )rT   rD   s    r   enabledzGCDiagnosis.enabled   s
    }r   c                .    |                                   | S r'   )r`   rD   s    r   	__enter__zGCDiagnosis.__enter__   s    r   c                .    |                                   d S r'   )rc   )r   exc_type	exc_value	tracebacks       r   __exit__zGCDiagnosis.__exit__   s    r   c                |   |d         dk    rd S | j                                         j        }|dk    r"| j                                         || _        d S |dk    sJ | j                                         | j        j        }|2|| j        k    r't          
                    dd|z  d| j        z             | j        |z
  }|| j        k    rBt                              dt          |          |d         t          | j                             |d	         d
k    r#t          
                    d|d	                    d S d S )N
generation   r9   r:   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)r\   memory_inforssrY   rE   _gc_rss_beforerG   rI   rR   r   r   rS   infor   )r   phaserv   rt   frac	rss_saveds         r   r]   zGCDiagnosis._gc_callback   si    ""Fj$$&&*G"//111"%DF**,,,%6(< < <NN-d
d**	   '#-	///KK;Y''[!T455    1$$NN,_%     %$r   N)rN   rO   )r   r    r!   r"   rX   r   r`   rc   rJ   re   rg   rl   r]   r#   r   r   rL   rL      s          I   
	 	 	  
   X    # # # # #r   rL   c                     t           5  t          dk    rt                                           nt          j        sJ t          dz  addd           dS # 1 swxY w Y   dS )z,
    Ask to enable global GC diagnosis.
    r   r
   N)_gc_diagnosis_lock_gc_diagnosis_users_gc_diagnosisr`   re   r#   r   r   enable_gc_diagnosisr~      s    
 
 ! !!##  """" ((((q ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !s   >AAAFc                   t           5  t          dk    r[t          dz  at          dk    rt                                           n,| rt                                           dant          j        sJ ddd           dS # 1 swxY w Y   dS )z-
    Ask to disable global GC diagnosis.
    r   r
   N)r{   r|   r}   rc   re   )forces    r   disable_gc_diagnosisr      s    
 
 	- 	-""1$"a''%%'''' -%%'''&'##$,,,,	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-s   A'A<<B B )F)
__future__r   r   logging	threadingcollectionsr   rZ   
dask.utilsr   distributed.metricsr   	getLoggerr   r   r   r   r%   rL   r}   r|   Lockr{   r~   r   r#   r   r   <module>r      s^   " " " " " " 				             # # # # # # + + + + + +$7$X.. .4 4 4 4 4 4 4 4n:& :& :& :& :& :& :& :&zN N N N N N N Nb  #Y^%% 
! 
! 
!- - - - - -r   