a
    &0É_"  ã                   @   s–   d dl mZmZ d dl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„ Zejd	d
„ ƒZejdd„ ƒZejdd„ ƒZG dd„ deƒZdS )é    )Úabsolute_importÚprint_functionNé   )Ú_)ÚgetattrÚopen)ÚencodingÚerrorÚ
extensionsÚpycompatÚutilc                 C   sH   |}t j| |gd zt  |¡}W n ty6   Y dS 0 t|ddƒS dS )zBload profiler extension. return profile method, or None on failure)Z	whitelistNÚprofile)r
   ZloadallÚfindÚKeyErrorr   )ÚuiÚprofilerZextnameÚmod© r   ú5/usr/lib/python3/dist-packages/mercurial/profiling.pyÚ_loadprofiler   s    r   c                 c   sh  |   dd¡}|   dd¡}|  dd¡}|  dd¡}|dvrN|  tdƒ| ¡ d}zd	d
lm} W n  ty~   t tdƒ¡‚Y n0 | 	¡ }|j
dd zjd V  W | ¡  |dkrÐd	dlm} | |¡}	|	 |¡ n.| | ¡ ¡}
|
 t |¡¡ |
j|||d nd| ¡  |dkr4d	dlm} | |¡}	|	 |¡ n.| | ¡ ¡}
|
 t |¡¡ |
j|||d 0 d S )Nó	   profilings   formats   sorts   limits   nested)ó   textó   kcachegrinds-   unrecognized profiling format '%s' - Ignored
r   r   )ÚlsprofsY   lsprof not available - install from http://codespeak.net/svn/user/arigo/hack/misc/lsprof/T)Zsubcallsr   )Úlsprofcalltree)ÚlimitÚfileÚclimit)ÚconfigÚ	configintÚwarnr   Ú r   ÚImportErrorr	   ÚAbortZProfilerÚenableÚdisabler   ZKCacheGrindÚoutputZStatsZgetstatsÚsortr   ZsysstrZpprint)r   ÚfpÚformatZfieldr   r   r   Úpr   ZcalltreeZstatsr   r   r   Ú	lsprofile&   sF    ÿÿ

õ

r+   c                 c   sè   zddl m } W n  ty0   t tdƒ¡‚Y n0 |  dd¡}d }d}| |d| ||¡}t ¡ }zH| 	¡  d V  W | 
¡  | ¡  tdt ¡ | | ¡ |jdd	f ƒ n8| 
¡  | ¡  tdt ¡ | | ¡ |jdd	f ƒ 0 d S )
Nr   )Ú
flamegraphsW   flamegraph not available - install from https://github.com/evanhempel/python-flamegraphr   ó   freqTg      ð?s7   Collected %d stack frames (%d unique) in %2.2f seconds.)Úunique)r,   r"   r	   r#   r   r   ZProfileThreadr   ZtimerÚstartÚstopÚjoinÚprintZ
num_frames)r   r(   r,   ÚfreqZfilter_Zcollapse_recursionÚthreadZ
start_timer   r   r   ÚflameprofileM   sL    ÿÿ
ÿ

ýÿÿþ

ýÿÿr5   c                 c   sr  ddl m} |  dd¡}|dkr8|jjdkrJ| |¡ n|  tdƒ| ¡ |  ddt	j
r\dp^d	¡}|jd
|d z d V  W | ¡ }|  dd¡}|jj|jj|jj|jj|jjdœ}||v rÆ|| }n|  tdƒ| ¡ |jj}i }	dd„ }
|dkr$|  |
ddd¡}|  |
dd¡}|	j||d n6|dkrZ|  |
ddd¡}||	d< |  dd¡}||	d< |j|f||dœ|	¤Ž nú| ¡ }|  dd¡}|jj|jj|jj|jj|jjdœ}||v rÀ|| }n|  tdƒ| ¡ |jj}i }	dd„ }
|dkr|  |
ddd¡}|  |
dd¡}|	j||d n6|dkrT|  |
ddd¡}||	d< |  dd¡}||	d< |j|f||dœ|	¤Ž 0 d S )Nr   )Ústatprofr   r-   r   s+   invalid sampling frequency '%s' - ignoring
s
   time-tracks   cpus   reals   thread)Z	mechanismÚtracks
   statformat)s   bylines   bymethodó   hotpaths   jsonó   chromes#   unknown profiler output format: %s
c                 S   sf   t | ttfƒrt| ƒS |  d¡r6t| d d… ƒd }nt| ƒ}d|  krRdkrZn n|S t| ƒ‚d S )Nó   %éÿÿÿÿéd   r   r   )Ú
isinstanceÚfloatÚintÚendswithÚ
ValueError)ÚsÚvr   r   r   Úfraction˜   s    
zstatprofile.<locals>.fractionr9   s   showming{®Gázt?s   showmax)ZminthresholdZmaxthresholdr8   gš™™™™™©?r   s   showtimeÚshowtime)Údatar)   )r!   r6   r   ÚstateZprofile_levelÚresetr    r   r   r   Ú	iswindowsr/   r0   ZDisplayFormatsZByLineZByMethodZHotpathZJsonZChromeZ
configwithÚupdateZ
configboolZdisplay)r   r(   r6   r3   r7   rF   Z
profformatZformatsZdisplayformatÚkwargsrD   ZshowminZshowmaxr   rE   r   r   r   Ústatprofilep   sx    ÿû


Öû



rL   c                   @   sB   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )r   z²Start profiling.

    Profiling is active when the context manager is active. When the context
    manager exits, profiling results will be written to the configured output.
    Tc                 C   s:   || _ d | _d | _d| _d | _d | _|| _d| _d| _d S )NTF)	Ú_uiÚ_outputÚ_fpÚ
_fpdocloseÚ_flushfpÚ	_profilerÚ_enabledÚ_enteredÚ_started)Úselfr   Zenabledr   r   r   Ú__init__¸   s    zprofile.__init__c                 C   s   d| _ | jr|  ¡  | S )NT)rT   rS   r/   ©rV   r   r   r   Ú	__enter__Ã   s    zprofile.__enter__c                 C   sp  | j st d¡‚| jrdS d| _tj d¡}d}|du rF| j dd¡}|dvrzt	| j|ƒ}|du rz| j 
tdƒ| ¡ d	}| j dd
¡| _zÊ| jdkr¢t ¡ | _nf| jrÄ| j | j¡}t|dƒ| _nDtjrîG dd„ dtƒ}d| _|| jƒ| _nd| _| jj| _| jj| _|durn$|dkr$t}n|dkr4t}nt}|| j| jƒ| _| j ¡  W n   |  ¡  ‚ Y n0 dS )zˆStart profiling.

        The profiling will stop at the context exit.

        If the profiler was already started, this has no effect.s   use a context manager to startNTs   HGPROFr   s   type)ó   lsó   stató   flames%   unrecognized profiler '%s' - ignored
r[   s   outputó   blackboxs   wbc                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )zprofile.start.<locals>.uifpc                 S   s
   || _ d S ©N)rM   )rV   r   r   r   r   rW   ì   s    z$profile.start.<locals>.uifp.__init__c                 S   s   | j  |¡ d S r^   )rM   Z	write_err)rV   rF   r   r   r   Úwriteï   s    z!profile.start.<locals>.uifp.writec                 S   s   | j  ¡  d S r^   )rM   ÚflushrX   r   r   r   r`   ò   s    z!profile.start.<locals>.uifp.flushN)Ú__name__Ú
__module__Ú__qualname__rW   r_   r`   r   r   r   r   Úuifpë   s   rd   FrZ   r\   )rT   r	   ZProgrammingErrorrU   r   ÚenvironÚgetrM   r   r   r    r   rN   r   ZstringiorO   Z
expandpathr   r   rI   ÚobjectrP   ZferrZfoutrQ   r+   r5   rL   rR   rY   Ú_closefp)rV   r   ZproffnÚpathrd   r   r   r   r/   É   sT    

ÿ






zprofile.startc                 C   sd   d }| j d urX|  ¡  | j  |||¡}| jdkrXd| j ¡  }| dd¡}| j d|¡ |  	¡  |S )Nr]   s   Profile:
%sr:   s   %%s   profile)
rR   Ú_uiflushÚ__exit__rN   rO   ÚgetvalueÚreplacerM   Úlogrh   )rV   Zexception_typeZexception_valueÚ	tracebackZ	propagateÚvalr   r   r   rk     s    
ÿ
zprofile.__exit__c                 C   s   | j r| jd ur| j ¡  d S r^   )rP   rO   ÚcloserX   r   r   r   rh     s    zprofile._closefpc                 C   s   | j r| j  ¡  d S r^   )rQ   r`   rX   r   r   r   rj      s    zprofile._uiflushN)T)
ra   rb   rc   Ú__doc__rW   rY   r/   rk   rh   rj   r   r   r   r   r   ±   s   
Cr   )Z
__future__r   r   Ú
contextlibZi18nr   r   r   r   r!   r   r	   r
   r   r   Úcontextmanagerr+   r5   rL   rg   r   r   r   r   r   Ú<module>   s   	
&
"
@