
    q&f7"                         d dl Z ddlmZ ddlmZ ddlmZmZmZmZm	Z	 d Z
e j                  d        Ze j                  d        Ze j                  d	        Z G d
 d      Zy)    N   )_)open)encodingerror
extensionspycompatutilc                     |}t        j                  | |g       	 t        j                  |      }t        |dd      S # t        $ r Y yw xY w)zBload profiler extension. return profile method, or None on failure)	whitelistprofileN)r   loadallfindgetattrKeyError)uiprofilerextnamemods       5/usr/lib/python3/dist-packages/mercurial/profiling.py_loadprofilerr      sQ    GrgY/-oog& sIt,,  s   > 	A
	A
c              #     K   | j                  dd      }| j                  dd      }| j                  dd      }| j                  dd      }|dvr| j                  t        d      |z         d}	 d	d
lm} |j                         }|j                  d       	 d  |j                          |dk(  r)d	dlm} |j                  |      }	|	j                  |       y |j                  |j!                               }
|
j#                  t%        j&                  |             |
j)                  |||       y # t        $ r t        j                  t        d            w xY w# |j                          |dk(  r)d	dlm} |j                  |      }	|	j                  |       w |j                  |j!                               }
|
j#                  t%        j&                  |             |
j)                  |||       w xY ww)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)subcallsr   )lsprofcalltree)limitfileclimit)config	configintwarnr    r   ImportErrorr   AbortProfilerenabledisabler   KCacheGrindoutputStatsgetstatssortr	   sysstrpprint)r   fpformatfieldr   r!   r   pr   calltreestatss              r   	lsprofiler8   $   s    YY|Y/FIIlG,ELLx0E\\,	2F..
CDvMN
 	AHHdH>			^#(%11!4HOOB LL.EJJxu-.LLu2fL=/  
kkI
 	

 	
		^#(%11!4HOOB LL.EJJxu-.LLu2fL=s8   A,G7/D2 5"G7E BG72(EG7BG44G7c           
   #     K   	 ddl m } | j                  dd      }d }d}|j                  |d|z  ||      }t        j                         }	 |j                          d  |j                          |j                          t        dt        j                         |z
  |j                         |j                  d	      fz         y # t        $ r t        j                  t	        d            w xY w# |j                          |j                          t        dt        j                         |z
  |j                         |j                  d	      fz         w xY ww)
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#   ProfileThreadr
   timerstartstopjoinprint
num_frames)r   r2   r:   freqfilter_collapse_recursionthread
start_times           r   flameprofilerI   K   s=    
) <<g.DG%%
C$J!3F J
F

z)!!#!!!.	
+  
kkC
 	

& 	F

z)!!#!!!.	
s6   EC AEC0  A%E(C--E0A&EEc              #     K   ddl m} | j                  dd      }|dkD  r+|j                  j                  dk(  r/|j                  |       n| j                  t        d      |z         | j                  ddt        j                  xr dxs d	      }|j                  d
|       	 d  |j                         }| j                  dd      }|j                  j                  |j                  j                  |j                  j                   |j                  j"                  |j                  j$                  d}||v r||   }n3| j                  t        d      |z         |j                  j                   }i }	d }
|dk(  r;| j'                  |
ddd      }| j'                  |
dd      }|	j)                  ||       n5|dk(  r0| j'                  |
ddd      }||	d<   | j+                  dd      }||	d<    |j,                  |f||d|	 y # |j                         }| j                  dd      }|j                  j                  |j                  j                  |j                  j                   |j                  j"                  |j                  j$                  d}||v r||   }n3| j                  t        d      |z         |j                  j                   }i }	d }
|dk(  r;| j'                  |
ddd      }| j'                  |
dd      }|	j)                  ||       n5|dk(  r0| j'                  |
ddd      }||	d<   | j+                  dd      }||	d<    |j,                  |f||d|	 w xY ww)Nr   )statprofr   r;   r   s+   invalid sampling frequency '%s' - ignoring
s
   time-tracks   cpus   reals   thread)	mechanismtracks
   statformat)s   bylines   bymethod   hotpaths   json   chromes#   unknown profiler output format: %s
c                     t        | t        t        f      rt        |       S | j                  d      rt        | d d       dz  }nt        |       }d|cxk  rdk  r|S  t	        |       t	        |       )N   %d   r   r   )
isinstancefloatintendswith
ValueError)svs     r   fractionzstatprofile.<locals>.fraction   sj    !eS\*Qxzz$!CR&MC'!HA{{ Q-*Q-    rO   s   showming{Gzt?s   showmax)minthresholdmaxthresholdrN   g?r   s   showtimeshowtime)datar3   )r%   rK   r#   stateprofile_levelresetr$   r   r"   r	   	iswindowsr?   r@   DisplayFormatsByLineByMethodHotpathJsonChrome
configwithupdate
configbooldisplay)r   r2   rK   rD   rM   r`   
profformatformatsdisplayformatkwargsr[   showminshowmaxr   r_   s                  r   statprofileru   n   s9    <<g.Dax>>''1,NN4 
ABTIJIImX%7%7%BF%MgE NNYeN4-H}}YY|];
  ..55!0099 //77,,11..55
  #J/MGGA=>KL$33;;M		  "mmHlJNGmmHlJGGMMwWME:%MM(L*dKE#F7O}}\;?H!)F:G$}GGU }}YY|];
  ..55!0099 //77,,11..55
  #J/MGGA=>KL$33;;M		  "mmHlJNGmmHlJGGMMwWME:%MM(L*dKE#F7O}}\;?H!)F:G$}GGs    B"M%%H )EM%EM""M%c                   6    e Zd ZdZd	dZd Zd Zd Zd Zd Z	y)
r   zStart profiling.

    Profiling is active when the context manager is active. When the context
    manager exits, profiling results will be written to the configured output.
    c                     || _         d | _        d | _        d| _        d | _        d | _        || _        d| _        d| _        y )NTF)	_ui_output_fp
_fpdoclose_flushfp	_profiler_enabled_entered_started)selfr   enableds      r   __init__zprofile.__init__   sA    r\   c                 L    d| _         | j                  r| j                          | S )NT)r   r~   r?   r   s    r   	__enter__zprofile.__enter__   s    ==JJLr\   c                 X   | j                   st        j                  d      | j                  ryd| _        t        j
                  j                  d      }d}|| j                  j                  dd      }|dvrAt        | j                  |      }|)| j                  j                  t        d      |z         d	}| j                  j                  dd
      | _        	 | j                  dk(  rt        j                         | _        n| j                  r1t        j                   | j                        }t#        |d      | _        nvt$        j&                  r) G d d      }d| _         || j                        | _        n=d| _        | j                  j*                  | _        | j                  j,                  | _        |n|dk(  rt0        }n|dk(  rt2        }nt4        } || j                  | j                        | _        | j6                  j9                          y#  | j;                           xY w)zStart 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                       e Zd Zd Zd Zd Zy)profile.start.<locals>.uifpc                     || _         y N)rx   )r   r   s     r   r   z$profile.start.<locals>.uifp.__init__   s	    #%r\   c                 :    | j                   j                  |       y r   )rx   	write_err)r   r`   s     r   writez!profile.start.<locals>.uifp.write   s    **40r\   c                 8    | j                   j                          y r   )rx   flushr   s    r   r   z!profile.start.<locals>.uifp.flush   s    (r\   N)__name__
__module____qualname__r   r   r    r\   r   uifpr      s    &1)r\   r   Fr   r   )r   r   ProgrammingErrorr   r   environgetrx   r"   r   r$   r   ry   r
   stringiorz   
expandpathr   r	   rd   r{   ferrfoutr|   r8   rI   ru   r}   r   _closefp)r   r   proffnpathr   s        r   r?   zprofile.start   s    }}(()JKK==##''	2xx|W=H55"488X6F~?@8K #xx|Y?'	||{*==?t||4e,##) ) #(>"'88== $!U""X%%$#DHHdhh7DNNN$$&	MMOs   D9H H)c                 P   d }| j                   | j                          | j                   j                  |||      }| j                  dk(  rKd| j                  j                         z  }|j                  dd      }| j                  j                  d|       | j                          |S )Nr   s   Profile:
%srQ   s   %%s   profile)
r}   _uiflush__exit__ry   rz   getvaluereplacerx   logr   )r   exception_typeexception_value	traceback	propagatevals         r   r   zprofile.__exit__
  s    	>>%MMO//I ||{*%(9(9(;; kk$.Z-r\   c                 l    | j                   r(| j                  | j                  j                          y y y r   )r{   rz   closer   s    r   r   zprofile._closefp  s'    ??txx3HHNN  4?r\   c                 R    | j                   r| j                   j                          y y r   )r|   r   r   s    r   r   zprofile._uiflush  s    ==MM! r\   N)T)
r   r   r   __doc__r   r   r?   r   r   r   r   r\   r   r   r      s'    	AF "r\   r   )
contextlibi18nr   r	   r   r%   r   r   r   r
   r   contextmanagerr8   rI   ru   r   r   r\   r   <module>r      s      	- #> #>L 
 
D =H =H@q" q"r\   