a
    „'fR%  ã                   @   sà   d Z edƒZejZddlmZ eeeƒ eeƒd ddlZddl	Z	ddl
mZ ddl
mZ ddlZe ejjj¡ZG dd	„ d	ejƒZd
d„ Zeejjfi ejejdœ¤Že_edd„ e ej¡D ƒƒe_ddd„Zddd„ZdS )a  This module is API-equivalent to the standard library :mod:`profile` module
lbut it is greenthread-aware as well as thread-aware.  Use this module
to profile Eventlet-based applications in preference to either :mod:`profile` or :mod:`cProfile`.
FIXME: No testcases for this module.
Zprofileé    )Úslurp_properties)ZsrckeysN)Úgreenthread)Úpatcherc                   @   sv   e Zd ZejZddd„Zdd„ Zdd„ Zd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dS )ÚProfileNc                 C   s.   t  ¡ | _t ¡ | _| j | ||¡ i | _d S ©N)	r   Ú
getcurrentÚcurrent_taskletÚthreadÚ	get_identÚ	thread_idÚbaseÚ__init__Úsleeping)ÚselfÚtimerZbias© r   úd/var/www/staging/api/virtual_environments/venv/lib/python3.9/site-packages/eventlet/green/profile.pyr   4   s    

zProfile.__init__c                 G   s   | j |Ž  dS )z6make callable, allowing an instance to be the profilerN)Ú
dispatcher)r   Úargsr   r   r   Ú__call__:   s    zProfile.__call__c                 C   s4   d| _ d | _i | _t ¡ | _t ¡ | _|  	d¡ d S )NTÚprofiler)
Ú
_has_setupÚcurÚtimingsr   r   r   r	   r
   r   Úsimulate_call©r   r   r   r   Ú_setup>   s    

zProfile._setupÚstartc                 C   s8   t | ddƒrd S |  ¡  |  d¡ d| _t | j¡ d S )NÚrunningFr   T)Úgetattrr   r   r   ÚsysÚ
setprofiler   )r   Únamer   r   r   r   F   s    
zProfile.startc                 C   s   t  d ¡ d| _|  ¡  d S )NF)r    r!   r   ÚTallyTimingsr   r   r   r   ÚstopN   s    
zProfile.stopc                 C   s@   t | ddƒs|  ¡  ztj | |||¡W |  ¡  S |  ¡  0 d S ©Nr   F)r   r   Úprofile_origr   Úrunctxr#   )r   ÚcmdÚglobalsÚlocalsr   r   r   r'   U   s    þzProfile.runctxc                 O   sJ   t | ddƒs|  ¡  z&tjj| |g|¢R i |¤ŽW |  ¡  S |  ¡  0 d S r%   )r   r   r&   r   Úruncallr#   )r   Úfuncr   Úkwr   r   r   r+   ]   s    þzProfile.runcallc                 C   s"   t | jd tjƒrdS |  ||¡S )a{  A hack function to override error checking in parent class.  It
        allows invalid returns (where frames weren't preveiously entered into
        the profiler) which can happen for all the tasklets that suddenly start
        to get monitored. This means that the time will eventually be attributed
        to a call high in the chain, when there is a tasklet switch
        éþÿÿÿFr   )Ú
isinstancer   r   Ú
fake_frameZtrace_dispatch_callÚtrace_dispatch_return©r   ÚframeÚtr   r   r   Ú!trace_dispatch_return_extend_backe   s     z)Profile.trace_dispatch_return_extend_backc                 C   s"   t | jd tjƒrdS |  ||¡S )Nr.   Fr   )r/   r   r   r0   Ztrace_dispatch_c_callr1   r2   r   r   r   Ú#trace_dispatch_c_return_extend_backq   s     z+Profile.trace_dispatch_c_return_extend_backc                 C   sŽ   | j \}}}}}}	||| ||||	f}
|
| jf| j|< || _z| j |¡\| _ | _W n4 tyˆ   d i  | _ | _|  d¡ |  d¡ Y n0 d S )Nr   Znew_tasklet)r   r   r   r   ÚpopÚKeyErrorr   )r   Út0Út1r4   ÚptÚitÚetÚfnr3   Úrcurr   r   r   r   ÚSwitchTaskletx   s    
zProfile.SwitchTaskletc                 C   sè   | j }i | _ |  | j| j¡| _t |¡D ]º\}\}}|  ||¡ t |¡D ]–\}}|| jvrh|| j|< qJ| j| \}}}	}
}||d 7 }|	|d 7 }	|
|d 7 }
t |d ¡D ]\}}| |d¡| ||< q®|||	|
|f| j|< qJq(d S )Nr   é   é   é   )r   ÚUnwindr   r   ÚsixÚ	iteritemsÚget)r   Z
oldtimingsZtaskletr   r   ÚkÚvÚccÚnsÚttÚctÚcallersZk1Zv1r   r   r   r#   ‰   s    
zProfile.TallyTimingsc                 C   sô   |d rð|\}}}}}}|| }	||v r<|| \}
}}}}nddddi f\}
}}}}|sh||	 }|
d }
|r~|\}}}}}}nd}||v rœ|| d ||< n|r¨d||< |
|d || ||f||< |\}}}}}}||| ||	 |||f}|}q |S )z8A function to unwind a 'cur' frame and tally the resultséÿÿÿÿr   é   Nr   )r   r   r   ÚrptZritÚretZrfnr3   r?   Zframe_totalrJ   rK   rL   rM   rN   ZpptZpitZpetZpfnZpframeZpcurr   r   r   rD   ¤   s*    zProfile.Unwind)NN)r   )Ú__name__Ú
__module__Ú__qualname__r&   r   r   r   r   r   r   r$   r'   r+   r5   r6   r@   r#   rD   r   r   r   r   r   1   s   

r   c                    s   t  ˆ ¡‡ fdd„ƒ}|S )Nc                    s2   t  ¡ }|| jkr&|  | j||¡ d}ˆ | ||ƒS )Ng        )r   r   r   r@   )r   Úargr4   Úcurrent©Úfr   r   ÚContextWrapperÈ   s
    
z#ContextWrap.<locals>.ContextWrapper)Ú	functoolsÚwraps)rY   rZ   r   rX   r   ÚContextWrapÇ   s    r]   )ÚreturnZc_returnc                 c   s   | ]\}}|t |ƒfV  qd S r   )r]   )Ú.0rH   rI   r   r   r   Ú	<genexpr>Ø   ó    r`   rO   c                 C   sJ   t ƒ }z| | ¡}W n ty&   Y n0 |dur<| |¡ n
| |¡S dS )a  Run statement under profiler optionally saving results in filename

    This function takes a single argument that can be passed to the
    "exec" statement, and an optional file name.  In all cases this
    routine attempts to "exec" its first argument and gather profiling
    statistics from the execution. If no file name is present, then this
    function automatically prints a simple profiling report, sorted by the
    standard name string (file/line/function-name) that is presented in
    each line.
    N)r   ÚrunÚ
SystemExitÚ
dump_statsÚprint_stats)Ú	statementÚfilenameÚsortÚprofr   r   r   rb   Ü   s    rb   c                 C   sL   t ƒ }z| | ||¡}W n ty*   Y n0 |dur@| |¡ n| ¡ S dS )z·Run statement under profiler, supplying your own globals and locals,
    optionally saving results in filename.

    statement and filename have the same semantics as profile.run
    N)r   r'   rc   rd   re   )rf   r)   r*   rg   ri   r   r   r   r'   ò   s    r'   )NrO   )N)Ú__doc__Ú
__import__r&   Ú__all__Zeventlet.patcherr   r)   Údirr    r[   Zeventletr   r   rE   ÚoriginalÚmovesÚ_threadrS   r	   r   r]   ÚdictÚdispatchr5   r6   Ú	viewitemsrb   r'   r   r   r   r   Ú<module>   s(    þ
