o
    0 iZ                     @   sz  d dl Z d dlZd dlZd dlZd dl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mZ d dlmZ d dlmZ d dlmZmZ d dlmZmZmZmZmZ d d	lmZ erd d
lm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" 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)m*Z* d dlm+Z+m,Z,m-Z-m.Z.m/Z/m0Z0 e'de1e.e2dZ3zd dl4m5Z5 d dl6m7Z8 e8Z7e5ddZ9W n e:y   ej;Z9dZ7Y nw da<dd Z=dd Z>d d! Z?d"d# Z@d$d% ZAd&d' ZBd(d) ZCd*d+ ZDd,d- ZEd.d/ ZFd0d1 ZGG d2d3 d3ZHG d4d5 d5ZIG d6d7 d7eIZJG d8d9 d9eIZKd:ZLG d;d< d<ZMG d=d> d>ZNdS )?    N)deque)datetimetimezone)VERSION)Envelope)LRUCache)DEFAULT_SAMPLING_FREQUENCYextract_stack)capture_internal_exception	is_geventloggernowset_in_app_in_frames)TYPE_CHECKING)Any)Callable)Deque)Dict)List)Optional)Set)Type)Union)	TypedDict)ContinuousProfilerModeSDKInfo)ExtractedSampleFrameIdStackIdThreadIdProcessedFrameProcessedStackProcessedSample	timestamp	thread_idstack_id)get_original)
ThreadPooltimesleepc                 C   s   t d u}|rtd t  t rtj}ntj}| dd ur$| d }n| di }|dp0|}t	}|tjkr@t|| ||a n|tjkrMt|| ||a nt
d|tdjt jd |sett dS )	Nz0[Profiling] Continuous Profiler is already setupprofiler_mode_experimentsZcontinuous_profiling_modez$Unknown continuous profiler mode: {}z9[Profiling] Setting up continuous profiler in {mode} mode)modeT)
_schedulerr   debugteardown_continuous_profilerr   GeventContinuousSchedulerr-   ThreadContinuousSchedulergetr   
ValueErrorformatatexitregister)optionssdk_infocapture_funcZalready_initializedZdefault_profiler_moder+   experiments	frequency r=   s/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/sentry_sdk/profiler/continuous_profiler.pysetup_continuous_profilerJ   s<   




r?   c                   C   s   t d u rdS t jS NF)r.   sampledr=   r=   r=   r>   is_profile_session_sampled      rB   c                   C   s$   t d u rd S t  sd S t   d S N)r.   is_auto_start_enabledmanual_startr=   r=   r=   r>   !try_autostart_continuous_profiler   s
   rG   c                   C   s   t d u rd S t  S rD   )r.   
auto_startr=   r=   r=   r>   !try_profile_lifecycle_trace_start   s   rI   c                   C      t d u rd S t   d S rD   )r.   rF   r=   r=   r=   r>   start_profiler      rK   c                   C      t jdtdd t  d S )NzWThe `start_profile_session` function is deprecated. Please use `start_profile` instead.   
stacklevel)warningswarnDeprecationWarningrK   r=   r=   r=   r>   start_profile_session      
rT   c                   C   rJ   rD   )r.   manual_stopr=   r=   r=   r>   stop_profiler   rL   rW   c                   C   rM   )NzUThe `stop_profile_session` function is deprecated. Please use `stop_profile` instead.rN   rO   )rQ   rR   rS   rW   r=   r=   r=   r>   stop_profile_session   rU   rX   c                   C   s   t   d ad S rD   )rW   r.   r=   r=   r=   r>   r0      s   r0   c                   C   s   t d u rd S t jS rD   )r.   profiler_idr=   r=   r=   r>   get_profiler_id   rC   rZ   c                 C   s   | sdS t   t| k S r@   )randomfloat)Zsample_rater=   r=   r>   +determine_profile_session_sampling_decision   s   r]   c                   @   s"   e Zd ZU dZeed< dd ZdS )ContinuousProfileTactivec                 C   s
   d| _ d S r@   )r_   selfr=   r=   r>   stop   s   
zContinuousProfile.stopN)__name__
__module____qualname__r_   bool__annotations__rb   r=   r=   r=   r>   r^      s   
 r^   c                   @   st   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zedd Zdd Zdd ZdS )ContinuousSchedulerunknownc                 C   sz   d| | _ || _|| _|| _| jd| _| jd}t|| _|  | _	d | _
d | _d| _d| _tdd| _t | _d S )Ng      ?Zprofile_lifecycleprofile_session_sample_rateF   )maxlen)intervalr8   r9   r:   r3   	lifecycler]   rA   make_samplersamplerbufferpidrunningsoft_shutdownr   new_profilessetactive_profiles)ra   r<   r8   r9   r:   rj   r=   r=   r>   __init__   s    

zContinuousScheduler.__init__c                 C   s0   | j t kr	dS | jd}|sdS |dS )NFr,   Zcontinuous_profiling_auto_start)rr   osgetpidr8   r3   )ra   r;   r=   r=   r>   rE      s   
z)ContinuousScheduler.is_auto_start_enabledc                 C   s@   | j sd S | jdkrd S td t }| j| |   |S )Ntracez"[Profiling] Auto starting profiler)rA   rn   r   r/   r^   ru   appendensure_running)ra   profiler=   r=   r>   rH     s   

zContinuousScheduler.auto_startc                 C   s$   | j sd S | jdkrd S |   d S NZmanual)rA   rn   r}   r`   r=   r=   r>   rF      s
   
z ContinuousScheduler.manual_startc                 C   s   | j dkrd S |   d S r   )rn   teardownr`   r=   r=   r>   rV   *  s   
zContinuousScheduler.manual_stopc                 C      t rD   NotImplementedErrorr`   r=   r=   r>   r}   1     z"ContinuousScheduler.ensure_runningc                 C   r   rD   r   r`   r=   r=   r>   r   5  r   zContinuousScheduler.teardownc                 C   r   rD   r   r`   r=   r=   r>   pause9  r   zContinuousScheduler.pausec                 C   s   t | j| jt| j| _d S rD   )ProfileBufferr8   r9   PROFILE_BUFFER_SECONDSr:   rq   r`   r=   r=   r>   reset_buffer=  s   
z ContinuousScheduler.reset_bufferc                 C   s   | j d u rd S | j jS rD   )rq   rY   r`   r=   r=   r>   rY   C  s   
zContinuousScheduler.profiler_idc                    sD   t  tdd jdkr fdd}|S  fdd}|S )N   )max_sizer{   c                     s   j sjsdS tj }t }z fddt  D }W n ty0   tt	  Y dS w t
|D ]}jj   q5g }jD ]
}|jsP|| qF|D ]}j| qSjdurhj|| dS )
                Take a sample of the stack on all the threads in the process.
                This should be called at a regular interval to collect samples.
                Tc                    $   g | ]\}}t |t| fqS r=   strr	   .0tidframecachecwdr=   r>   
<listcomp>j      KContinuousScheduler.make_sampler.<locals>._sample_stack.<locals>.<listcomp>FN)ru   rw   lenr   sys_current_framesitemsAttributeErrorr
   exc_inforangeaddpopleftr_   r|   removerq   write)argskwargsru   tssample_Zinactive_profilesr~   r   r   ra   r=   r>   _sample_stackR  s0   






z7ContinuousScheduler.make_sampler.<locals>._sample_stackc                     sd   t  }z fddt  D }W n ty#   tt  Y dS w jdur0j|| dS )r   c                    r   r=   r   r   r   r=   r>   r     r   r   FN)	r   r   r   r   r   r
   r   rq   r   )r   r   r   r   r   r=   r>   r     s   


)ry   getcwdr   rn   )ra   r   r=   r   r>   ro   J  s   

[z ContinuousScheduler.make_samplerc                 C   sz   t  }| jr,|  | _t  | }|| jk rt| j|  | jr%d| _nt  }| js| jd ur;| j  d | _d S d S r@   )	r)   perf_counterrs   rp   rt   rm   thread_sleeprq   flush)ra   lastelapsedr=   r=   r>   run  s   




zContinuousScheduler.runN)rc   rd   re   r-   rx   rE   rH   rF   rV   r}   r   r   r   propertyrY   ro   r   r=   r=   r=   r>   rh      s    

erh   c                       s8   e Zd ZdZdZdZ fddZdd Zdd	 Z  Z	S )
r2   zr
    This scheduler is based on running a daemon thread that will call
    the sampler at a regular interval.
    threadz)sentry.profiler.ThreadContinuousSchedulerc                    s&   t  |||| d | _t | _d S rD   )superrx   r   	threadingLocklockra   r<   r8   r9   r:   	__class__r=   r>   rx     s   z"ThreadContinuousScheduler.__init__c              	   C   s   d| _ t }| jr| j|krd S | jN | jr&| j|kr&	 W d    d S || _d| _|   tj| j	| j
dd| _z| j  W n tyQ   d| _d | _Y n	w W d    d S W d    d S 1 sew   Y  d S )NFT)nametargetdaemon)rt   ry   rz   rs   rr   r   r   r   Threadr   r   r   startRuntimeErrorra   rr   r=   r=   r>   r}     s,   
"z(ThreadContinuousScheduler.ensure_runningc                 C   0   | j rd| _ | jd ur| j  d | _d | _d S r@   rs   r   joinrq   r`   r=   r=   r>   r        


z"ThreadContinuousScheduler.teardown)
rc   rd   re   __doc__r-   r   rx   r}   r   __classcell__r=   r=   r   r>   r2     s    &r2   c                       s4   e Zd ZdZdZ fddZdd Zdd Z  ZS )	r1   as  
    This scheduler is based on the thread scheduler but adapted to work with
    gevent. When using gevent, it may monkey patch the threading modules
    (`threading` and `_thread`). This results in the use of greenlets instead
    of native threads.

    This is an issue because the sampler CANNOT run in a greenlet because
    1. Other greenlets doing sync work will prevent the sampler from running
    2. The greenlet runs in the same thread as other greenlets so when taking
       a sample, other greenlets will have been evicted from the thread. This
       results in a sample containing only the sampler's code.
    Zgeventc                    s>   t d u rtd| jt |||| d | _t | _	d S )Nz"Profiler mode: {} is not available)
r(   r4   r5   r-   r   rx   r   r   r   r   r   r   r=   r>   rx   !  s
   z"GeventContinuousScheduler.__init__c              	   C   s   d| _ t }| jr| j|krd S | jJ | jr&| j|kr&	 W d    d S || _d| _|   td| _z	| j	| j
 W n tyM   d| _d | _Y n	w W d    d S W d    d S 1 saw   Y  d S )NFT   )rt   ry   rz   rs   rr   r   r   r(   r   Zspawnr   r   r   r=   r=   r>   r}   ,  s,   

"z(GeventContinuousScheduler.ensure_runningc                 C   r   r@   r   r`   r=   r=   r>   r   N  r   z"GeventContinuousScheduler.teardown)	rc   rd   re   r   r-   rx   r}   r   r   r=   r=   r   r>   r1     s    "r1   <   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
r   c                 C   sP   || _ || _|| _|| _t j| _t | _	t
 | _t
tj | j | _d S rD   )r8   r9   buffer_sizer:   uuiduuid4hexrY   ProfileChunkchunkr   start_monotonic_timer   r   utcr$   start_timestamp)ra   r8   r9   r   r:   r=   r=   r>   rx   ^  s   zProfileBuffer.__init__c                 C   s:   |  |r|   t | _t | _| j| j| | d S rD   )should_flushr   r   r   r   r   r   r   )ra   monotonic_timer   r=   r=   r>   r   u  s
   
zProfileBuffer.writec                 C   s   || j  | jkS rD   )r   r   )ra   r   r=   r=   r>   r   ~  s   zProfileBuffer.should_flushc                 C   s4   | j | j| j| j}t }|| | | d S rD   )r   to_jsonrY   r8   r9   r   Zadd_profile_chunkr:   )ra   r   enveloper=   r=   r>   r     s   
zProfileBuffer.flushN)rc   rd   re   rx   r   r   r   r=   r=   r=   r>   r   ]  s
    	r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )r   c                 C   s.   t  j| _i | _i | _g | _g | _g | _d S rD   )	r   r   r   chunk_idindexed_framesindexed_stacksframesstackssamplesr`   r=   r=   r>   rx     s   
zProfileChunk.__init__c           	   	      s   |D ]]\}\}}}zG| j vrBt|D ]\}}| jvr,t j j|<  j||  qt j  j |<  j fdd|D   j|| j | d W q ty_   t	t
  Y qw d S )Nc                    s   g | ]} j | qS r=   )r   )r   frame_idr`   r=   r>   r     s    z&ProfileChunk.write.<locals>.<listcomp>r#   )r   	enumerater   r   r   r|   r   r   r   r
   r   r   )	ra   r   r   r   r&   Z	frame_idsr   ir   r=   r`   r>   r     s,   

zProfileChunk.writec                 C   s   | j | j| jdd t D d}t|d |d |d |d  | j|d td	d
||dd}dD ]}|| d urCt|| 	 ||< q1|S )Nc                 S   s"   i | ]}t |jd t |jiqS )r   )r   identr   )r   r   r=   r=   r>   
<dictcomp>  s    
z(ProfileChunk.to_json.<locals>.<dictcomp>)r   r   r   Zthread_metadatar   Zin_app_excludeZin_app_includeZproject_rootr   )r   versionpython2)r   Z
client_sdkplatformr~   rY   r   )releaseenvironmentdist)
r   r   r   r   r   r   r   r   r   strip)ra   rY   r8   r9   r~   payloadkeyr=   r=   r>   r     s6   zProfileChunk.to_jsonN)rc   rd   re   rx   r   r   r=   r=   r=   r>   r     s    
r   )Or6   ry   r[   r   r   r)   r   rQ   collectionsr   r   r   Zsentry_sdk.constsr   Zsentry_sdk.enveloper   Zsentry_sdk._lru_cacher   Zsentry_sdk.profiler.utilsr   r	   Zsentry_sdk.utilsr
   r   r   r   r   typingr   r   r   r   r   r   r   r   r   r   Ztyping_extensionsr   Zsentry_sdk._typesr   r   r   r   r   r   r    r!   r\   intr"   Zgevent.monkeyr'   Zgevent.threadpoolr(   Z_ThreadPoolr   ImportErrorr*   r.   r?   rB   rG   rI   rK   rT   rW   rX   r0   rZ   r]   r^   rh   r2   r1   r   r   r   r=   r=   r=   r>   <module>   s     	
6
 lBI0