U
    @f*%                     @   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ZG dd	 d	ejZd
d Zeejjfejejde_dd 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   P/var/www/html/chatgpt/venv/lib/python3.8/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 |   X d S Nr   F)r   r   r"   profile_origr   runctx)r   cmdglobalslocalsr   r   r   r&   U   s
    zProfile.runctxc                 O   s>   t | dds|   ztjj| |f||W S |   X d S r$   )r   r   r"   r%   r   runcall)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   r/   Ztrace_dispatch_c_callr0   r1   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 n6 tk
r   d i  | _ | _| d | d Y nX d S )Nr   Znew_tasklet)r   r   r   r   popKeyErrorr   )r   t0t1r3   ptitetfnr2   rcurr   r   r   r   SwitchTaskletx   s    
zProfile.SwitchTaskletc                 C   s   | j }i | _ | | j| j| _| D ]\}\}}| || | D ]\}}|| jkrd|| j|< qF| j| \}}}	}
}||d 7 }|	|d 7 }	|
|d 7 }
|d  D ]\}}||d| ||< q|||	|
|f| j|< qFq&d S )Nr            )r   Unwindr   r   items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|\}}}}}}|| }	||kr<|| \}
}}}}nddddi f\}
}}}}|sh||	 }|
d }
|r~|\}}}}}}nd}||kr|| d ||< n|rd||< |
|d || ||f||< |\}}}}}}||| ||	 |||f}|}q |S )z8A function to unwind a 'cur' frame and tally the resultsr      Nr   )r   r   r   ZrptZritretZrfnr2   r>   Zframe_totalrH   rI   rJ   rK   rL   ZpptZpitZpetZpfnZpframeZpcurr   r   r   rC      s*    zProfile.Unwind)NN)r   )__name__
__module____qualname__r%   r   r   r   r   r   r   r#   r&   r*   r4   r5   r?   r"   rC   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   argr3   currentfr   r   ContextWrapper   s
    
z#ContextWrap.<locals>.ContextWrapper)	functoolswraps)rV   rW   r   rU   r   ContextWrap   s    rZ   )returnZc_returnc                 C   s   i | ]\}}|t |qS r   )rZ   ).0rF   rG   r   r   r   
<dictcomp>   s      r]   rM   c                 C   sL   t  }z|| }W n tk
r(   Y nX |dk	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   r^      s    r^   c                 C   sN   t  }z|| ||}W n tk
r,   Y nX |dk	rB|| n| S dS )zRun 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&   r_   r`   ra   )rb   r(   r)   rc   re   r   r   r   r&      s    r&   )NrM   )N)__doc__
__import__r%   __all__Zeventlet.patcherr   r(   dirr   rX   Zeventletr   r   _threadoriginalrP   r   r   rZ   dictdispatchr4   r5   rD   r^   r&   r   r   r   r   <module>   s(    


