
    6h*                    
   S SK Jr  S SKrS SKrS SKrS SKrS SKrSSKJr  SSK	J
r
JrJrJrJr  S r\R                   S 5       r\R                   S 5       r\R                   S	 5       r\R                   S
 5       r " S S5      rg)    )annotationsN   )_)encodingerror
extensionspycompatutilc                    Un[         R                  " X/S9   [         R                  " U5      n[        USS5      $ ! [         a     gf = f)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      sO    GrY/-oog& sIt,,  s   < 
A	A	c              #    #    U R                  SS5      nU R                  SS5      nU R                  SS5      nU R                  SS5      nUS;  a  U R                  [        S5      U-  5        Sn S	S
KJn  UR                  5       n UR                  SS9   S v   UR)                  5         US:X  a)  S	SKJn  UR-                  U5      nUR/                  U5        OTUR1                  UR3                  5       5      nUR5                  [6        R8                  " U5      5        UR;                  XAUS9  UR=                  5         g ! [         a     [        R                  " [        S5      5      ef = f! [         a  n[        U5      S:w  a  e [        [        S5      (       d  e [        R                  R!                  [        R                  R"                  5      n	U	S:X  a  Sn
[        R                  " U
5      S eSnU	(       a  [$        R&                  " U	5      nSn[        R                  " [        U5      U-  5      S eS nAff = f! UR)                  5         US:X  a)  S	SKJn  UR-                  U5      nUR/                  U5        OTUR1                  UR3                  5       5      nUR5                  [6        R8                  " U5      5        UR;                  XAUS9  UR=                  5         f = f7f)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)subcallsz(Another profiling tool is already active
monitoringcProfiles    cannot recursively call `lsprof`s	   <unknown>sA   failed to start "lsprofile"; another profiler already running: %sr   )lsprofcalltree)limitfileclimit)config	configintwarnr    r   ImportErrorr   AbortProfilerenable
ValueErrorstrhasattrsysr   get_toolPROFILER_IDr   
strtolocaldisabler    KCacheGrindoutputStatsgetstatssortr	   sysstrpprintflush)r   fpformatfieldr!   r#   r   pexcother_tool_namemsgtoolmr    calltreestatss                   r   	lsprofilerG   &   s[    YY|Y/FIIlG,ELLx0E\\,	2F..
CDvMN
 	A5	$&			^#(%11!4HOOB LL.EJJxu-.LLufL=

W  
kkI
 	

  5s8AAsL)) ..11#..2L2LMj(5C++c",D**?;TA++adTk*4#5* 	
		^#(%11!4HOOB LL.EJJxu-.LLufL=

sW   A.K'1E  7K'E- H? B$K' *E**K'-
H<7C H77H<<K'?B%K$$K'c           	   #    #     SSK J n  U R                  SS5      nS nSnUR                  USU-  XE5      n[        R                  " 5       n UR                  5         S v   UR                  5         UR                  5         SnU[        R                  " 5       U-
  UR                  5       UR                  SS	944-  n[        USS
9  g ! [         a     [        R                  " [	        S5      5      ef = f! UR                  5         UR                  5         SnU[        R                  " 5       U-
  UR                  5       UR                  SS	944-  n[        USS
9  f = f7f)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;   )rI   r(   r   r)   r   r%   ProfileThreadr
   timerstartstopjoin
num_framesprint)	r   r<   rI   freqfilter_collapse_recursionthread
start_timerD   s	            r   flameprofilerX   a   sY    
) <<g.DG%%
C$JF JF	

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

z)!!#!!!.
 	
 	ats6   EC AEC4  A'E*C11E4A(EEc              #    #    SSK Jn  U R                  SS5      nUS:  a,  UR                  R                  S:X  a  UR                  U5        OU R                  [        S5      U-  5        U R                  SS[        R                  =(       a    S=(       d    S	5      nUR                  S
US9   S v   UR                  5       nU R                  SS5      nUR                  R                  UR                  R                  UR                  R                   UR                  R"                  UR                  R$                  S.nXg;   a  Xv   nO3U R                  [        S5      U-  5        UR                  R                   n0 n	S n
US:X  a7  U R'                  U
SSS5      nU R'                  U
SS5      nU	R)                  XS9  O4US:X  a.  U R'                  U
SSS5      nXS'   U R+                  SS5      nXS'   UR,                  " U4XXS.U	D6  UR/                  5         g ! UR                  5       nU R                  SS5      nUR                  R                  UR                  R                  UR                  R                   UR                  R"                  UR                  R$                  S.nXg;   a  Xv   nO3U R                  [        S5      U-  5        UR                  R                   n0 n	S n
US:X  a7  U R'                  U
SSS5      nU R'                  U
SS5      nU	R)                  XS9  O4US:X  a.  U R'                  U
SSS5      nXS'   U R+                  SS5      nXS'   UR,                  " U4XXS.U	D6  UR/                  5         f = f7f)Nr   )statprofr   rJ   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                    [        U [        [        45      (       a  [        U 5      $ U R                  S5      (       a  [        U S S 5      S-  nO[        U 5      nSUs=::  a  S::  a   U$   [	        U 5      e)N   %d   r   r   )
isinstancefloatintendswithr,   )svs     r   fractionstatprofile.<locals>.fraction   sg    !eS\**Qxzz$!CR&MC'!HA{{ Q-    r^   s   showming{Gzt?s   showmax)minthresholdmaxthresholdr]   g?r!   s   showtimeshowtime)datar=   )r'   rZ   r%   stateprofile_levelresetr&   r   r$   r	   	iswindowsrN   rO   DisplayFormatsByLineByMethodHotpathJsonChrome
configwithupdate
configbooldisplayr;   )r   r<   rZ   rS   r\   ro   
profformatformatsdisplayformatkwargsri   showminshowmaxr!   rn   s                  r   statprofiler      s.    <<g.Dax>>''1,NN4 
ABTIJIImX%7%7%BF%MgE NNYeN4.}}YY|];
  ..55!0099 //77,,11..55
  #/MGGA=>KL$33;;M		  "mmHlJNGmmHlJGGMMwME:%MM(L*dKE#7O}}\;?H!):G$GG

W }}YY|];
  ..55!0099 //77,,11..55
  #/MGGA=>KL$33;;M		  "mmHlJNGmmHlJGGMMwME:%MM(L*dKE#7O}}\;?H!):G$GG

s    B+N.H 2E'NE(NNc           	   #    #    U R                  SS5      nU R                  SS5      nU R                  SS5      nUR                  5       nSU-  n[        R                  " 5       nUSS[        U5      SS	[        U5      S
U/	nU(       a  UR                  SU/5        [        R                  " UU1[        R                  S9n	U	R                  R                  5       n
 S v   [        R                  " U	R                  [        R                  5        U	R!                  5         g ! [        R                  " U	R                  [        R                  5        U	R!                  5         f = f7f)Nr   s
   py-spy.exes   py-spy.freqs   py-spy.formatz
/dev/fd/%drecordz--pidz--nativez--ratez--outputz--format)pass_fdsstdout)r$   r%   filenoosgetpidr-   extend
subprocessPopenPIPEr   readlinekillpidsignalSIGINTcommunicate)r   r<   exerS   r=   fdoutput_pathmy_pidcmdprocr   s              r   pyspy_profiler      s    
))L-
0C<<n5DYY|%56F	B"%KYY[F 	FD	
C 

J'(D 	A
&--( 	&--(s    CE"D A E"AEE"c                  @    \ rS rSrSrSS jrS rS rS rS r	S r
S	rg
)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                ~    Xl         S U l        S U l        SU l        S U l        S U l        X l        SU l        SU l        g )NTF)	_ui_output_fp
_fpdoclose_flushfp	_profiler_enabled_entered_started)selfr   enableds      r   __init__profile.__init__   s=    rk   c                V    SU l         U R                  (       a  U R                  5         U $ )NT)r   r   rN   r   s    r   	__enter__profile.__enter__  s    ==JJLrk   c                   U R                   (       d  [        R                  " S5      eU R                  (       a  gSU l        [        R
                  R                  S5      nSnUc  U R                  R                  SS5      nUS;  aB  [        U R                  U5      nUc)  U R                  R                  [        S5      U-  5        S	nU R                  R                  SS
5      U l         U R                  S:X  a  [        R                  " 5       U l        OU R                  (       a2  [        R                   " U R                  5      n[#        US5      U l        O{[$        R&                  (       a)   " S S5      nSU l        U" U R                  5      U l        O=SU l        U R                  R*                  U l        U R                  R,                  U l        Ub  O-US:X  a  [0        nO US:X  a  [2        nOUS:X  a  [4        nO[6        nU" U R                  U R                  5      U l        U R8                  R;                  5         g!   U R=                  5         e = f)zxStart 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   flame   py-spys%   unrecognized profiler '%s' - ignored
r   s   output   blackboxwbc                  &    \ rS rSrS rS rS rSrg)profile.start.<locals>.uifpi-  c                    Xl         g Nr   )r   r   s     r   r   $profile.start.<locals>.uifp.__init__.  s    #%rk   c                :    U R                   R                  U5        g r   )r   	write_err)r   ro   s     r   write!profile.start.<locals>.uifp.write1  s    **40rk   c                8    U R                   R                  5         g r   )r   r;   r   s    r   r;   !profile.start.<locals>.uifp.flush4  s    (rk   r   N)__name__
__module____qualname____firstlineno__r   r   r;   __static_attributes__ rk   r   uifpr   -  s    &1)rk   r   Fr   r   r   )r   r   ProgrammingErrorr   r   environgetr   r$   r   r&   r   r   r
   stringior   
expandpathopenr	   rs   r   ferrfoutr   rG   rX   r   r   r   r   _closefp)r   r   proffnpathr   s        r   rN   profile.start  s    }}(()JKK==##''	2xx|W=H@@"488X6F~?@8K #xx|Y?)	||{*==?t||4d+##) ) #(>"'88== $!U""X%%Y&&$#DHHdhh7DNNN$$&	MMOs   +EI Ic                   S nU R                   b  U R                  5         U R                   R                  XU5      nU R                  S:X  an  U R                  n[        U[        R                  5      (       d   eSUR                  5       -  nUR                  SS5      nU R                  R                  SU5        U R                  5         U$ )Nr   s   Profile:
%sr`   s   %%s   profile)r   _uiflush__exit__r   r   rc   r
   r   getvaluereplacer   logr   )r   exception_typeexception_value	traceback	propagater<   vals          r   r   profile.__exit__P  s    	>>%MMO//I ||{*XX!"dmm4444%5 kk$.Z-rk   c                x    U R                   (       a)  U R                  b  U R                  R                  5         g g g r   )r   r   closer   s    r   r   profile._closefpc  s'    ??txx3HHNN  4?rk   c                \    U R                   (       a  U R                   R                  5         g g r   )r   r;   r   s    r   r   profile._uiflushg  s    ==MM! rk   )	r   r   r   r   r   r   r   r   r   N)T)r   r   r   r   __doc__r   r   rN   r   r   r   r   r   rk   r   r   r      s'    	CJ&"rk   r   )
__future__r   
contextlibr   r   r   r/   i18nr   r'   r   r   r   r	   r
   r   contextmanagerrG   rX   r   r   r   r   rk   r   <module>r      s    #  	   
  	- 7 7t    F > >B ( (Vv" v"rk   