
    6hY                    @   S r 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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  \R$                  r\R&                  r/ SQr1 SkrS	 r " S
 S5      r\" 5       r " S S5      r " S S5      rS r\R8                  " 5       rS rS rSq S)S jr!S r"S r#S r$S*S jr%\S 5       r& " S S5      r' " S S5      r(S+S jr)S r*S r+S*S  jr,S,S! jr-S-S" jr.0 r/S# r0S$ r1S.S% jr2S& r3S*S' jr4\5S(:X  a  \
Rl                  " \4" 5       5        gg)/a
  
statprof is intended to be a fairly simple statistical profiler for
python. It was ported directly from a statistical profiler for guile,
also named statprof, available from guile-lib [0].

[0] http://wingolog.org/software/guile-lib/statprof/

To start profiling, call statprof.start():
>>> start()

Then run whatever it is that you want to profile, for example:
>>> import test.pystone; test.pystone.pystones()

Then stop the profiling and print out the results:
>>> stop()
>>> display()
  %   cumulative      self
 time    seconds   seconds  name
 26.72      1.40      0.37  pystone.py:79:Proc0
 13.79      0.56      0.19  pystone.py:133:Proc1
 13.79      0.19      0.19  pystone.py:208:Proc8
 10.34      0.16      0.14  pystone.py:229:Func2
  6.90      0.10      0.10  pystone.py:45:__init__
  4.31      0.16      0.06  pystone.py:53:copy
    ...

All of the numerical data is statistically approximate. In the
following column descriptions, and in all of statprof, "time" refers
to execution time (both user and system), not wall clock time.

% time
    The percent of the time spent inside the procedure itself (not
    counting children).

cumulative seconds
    The total number of seconds spent in the procedure, including
    children.

self seconds
    The total number of seconds spent in the procedure itself (not
    counting children).

name
    The name of the procedure.

By default statprof keeps the data collected from previous runs. If you
want to clear the collected data, call reset():
>>> reset()

reset() can also be used to change the sampling frequency from the
default of 1000 Hz. For example, to tell statprof to sample 50 times a
second:
>>> reset(50)

This means that statprof will sample the call stack after every 1/50 of
a second of user + system time spent running on behalf of the python
process. When your process is idle (for example, blocking in a read(),
as is the case at the listener), the clock does not advance. For this
reason statprof is not currently not suitable for profiling io-bound
operations.

The profiler uses the hash of the code object itself to identify the
procedures, so it won't confuse different procedures with the same name.
They will show up as two different rows in the output.

Right now the profiler is quite simplistic.  I cannot provide
call-graphs or other higher level information.  What you see in the
table is pretty much all there is. Patches are welcome :-)


Threading
---------

Because signals only get delivered to the main thread in Python,
statprof only profiles the main thread. However because the time
reporting function uses per-process timers, the results can be
significantly off if other threads' work patterns are not similar to the
main thread's work patterns.
    )annotationsN)List   )encodingpycompat)startstopresetdisplayprofile>   util.py:checkhg.py:<module>dispatch.py:runpager.py:pagecmdcolor.py:colorcmddispatch.py:<lambda>dispatch.py:dispatchdispatch.py:_dispatchdispatch.py:_runcatchdispatch.py:checkargsextensions.py:closuredispatch.py:runcommanddispatch.py:_runcommandevolve.py:warnobserrorsc                 L    [         R                  " 5       n U S   U S   -   U S   4$ )Nr   r      )ostimes)r   s    4/usr/lib/python3/dist-packages/mercurial/statprof.pyclockr       s)    HHJE!HuQxq**    c                  P    \ rS rSr% S\S'   SS jrSS jrS rS r\	S	 5       r
S
rg)ProfileState   zList[Sample]samplesNc                4    U R                  U5        SU l        g )N   cpu)r
   trackself	frequencys     r   __init__ProfileState.__init__   s    

9
r!   c                    SU l         S U l        U(       a  SU-  U l        O[        U S5      (       d  SU l        O S U l        SU l        / U l        g )N)        r/   g      ?sample_intervalMbP?r   )accumulated_timelast_start_timer0   hasattrremaining_prof_timeprofile_levelr%   r)   s     r   r
   ProfileState.reset   sS     *##&?D 011#/D  #' r!   c                    US   U R                   S   -
  US   U R                   S   -
  4nU R                  S   US   -   U R                  S   US   -   4U l        g Nr   r   )r3   r2   )r*   	stop_time	increments      r   accumulate_timeProfileState.accumulate_time   sq    aL4//22aL4//22
	
 !!!$y|3!!!$y|3!
r!   c                `    U R                   U R                     [        U R                  5      -  $ N)r2   timeidxlenr%   r*   s    r   seconds_per_sampleProfileState.seconds_per_sample   s%    $$T\\2S5FFFr!   c                &    U R                   S:X  a  gg)Ns   realr   r   )r(   rB   s    r   r@   ProfileState.timeidx   s    :: r!   )r2   r3   r6   r5   r0   r%   r(   r?   )__name__
__module____qualname____firstlineno____annotations__r,   r
   r<   rC   propertyr@   __static_attributes__ r!   r   r#   r#      s1    (
G  r!   r#   c                  P    \ rS rSr0 rSrS rS rS r\	S 5       r
S rS rS	 rS
rg)CodeSite   )pathlinenofunctionsourcec                    [        U[        5      (       d   eXl        X l        [        U[        5      (       d   eX0l        S U l        g r?   )
isinstancebytesrR   rS   rT   rU   )r*   rR   rS   rT   s       r   r,   CodeSite.__init__   s>    $&&&&	(E**** r!   c                     U R                   UR                   :H  =(       a    U R                  UR                  :H  $ !    g= f)NF)rS   rR   )r*   others     r   __eq__CodeSite.__eq__   s6    	;;%,,.J499

3JJ	s   8; ?c                D    [        U R                  U R                  45      $ r?   )hashrS   rR   rB   s    r   __hash__CodeSite.__hash__   s    T[[$)),--r!   c                z    X4n U R                   U   $ ! [         a    U " XU5      nXPR                   U'   Us $ f = fr?   )cacheKeyError)clsrR   rS   rT   kvs         r   getCodeSite.get   sG    N	99Q< 	D(+AIIaLH	s    #::c                   U R                   cs   U R                  S-
  n[        U R                  S5       n[	        U5       H!  u  pEXB:X  d  M  UR                  5       U l           O   S S S 5        U R                   c  SU l         U R                   n[        U5      U:  a  US US-
   S-   nU$ ! , (       d  f       NJ= f!    NQ= f)Nr   rbr!      s   ...)rU   rS   openrR   	enumeratestriprA   )r*   lengthrS   fpilinerU   s          r   	getsourceCodeSite.getsource   s    ;;q$))T*b#,R=;*.**,DK! $1 + {{"!v;Nvz+f4F +*
s.   %B: B)B)%B: )
B73B: 7B: :B>c                T    [         R                  R                  U R                  5      $ r?   )r   rR   basenamerB   s    r   filenameCodeSite.filename  s    ww		**r!   c                D    U R                  5       < SU R                  < 3$ )N:)rx   rT   rB   s    r   skipnameCodeSite.skipname	  s    --/4==99r!   )rT   rS   rR   rU   N)rG   rH   rI   rJ   rc   	__slots__r,   r\   r`   classmethodrh   rt   rx   r|   rM   rN   r!   r   rP   rP      s>    E8I.  &+:r!   rP   c                  .    \ rS rSrSrS r\S 5       rSrg)Samplei  stacktimec                    Xl         X l        g r?   r   )r*   r   r   s      r   r,   Sample.__init__  s    
	r!   c           	     T   / nU(       a  UR                  [        R                  [        R                  " UR
                  R                  5      UR                  [        R                  " UR
                  R                  5      5      5        UR                  nU(       a  M  [        X25      $ r?   )appendrP   rh   r   sysbytesf_codeco_filenamef_linenoco_namef_backr   )re   framer   r   s       r   
from_frameSample.from_frame  sx    LL%%ell&>&>?NN%%ell&:&:; LLE e e""r!   N)	rG   rH   rI   rJ   r~   r,   r   r   rM   rN   r!   r   r   r     s     !I # #r!   r   c                   [         R                  S:  a  [        5       n[         R                  U5        [         R                  [         R
                     n[         R                  R                  [        R                  X5      5        [        R                  " [        R                  [         R                  S5        U[         l        g g )Nr   r/   )stater6   r    r<   r2   r@   r%   r   r   r   signal	setitimerITIMER_PROFr0   r3   )signumr   now	timestamps       r   profile_signal_handlerr   )  s    Qgc"**5==9	V..u@A++U-B-BCH # r!   c                   [         R                  5       (       d  [        5       n[        R	                  U5        [
        R                  " 5       U    n[        R                  [        R                     n[        R                  R                  [        R                  X#5      5        U[        l        [        R                  " [        R                   5        [         R                  5       (       d  M  [         R#                  5         g r?   )
stopthreadis_setr    r   r<   sys_current_framesr2   r@   r%   r   r   r   r3   r   sleepr0   clear)tidr   r   r   s       r   samplerthreadr   8  s    !!gc"##%c***5==9	V..u@A #

5(() !! r!   c                 (    [         R                  S:  $ Nr   )r   r6   rN   r!   r   	is_activer   L  s    ""r!   c                   U[         l        [         =R                  S-  sl        [         R                  S:X  Ga>  [        5       [         l        [         R
                  nS[         l        U qU S:X  ag  [        R                  " [        R                  [        5        [        R                  " [        R                  U=(       d    [         R                  S5        gU S:X  a  [        R                  " 5       n[        R                   " 5       R#                  5        VVs/ s H  u  pEXS:X  d  M  UPM     snnS   n[$        R&                  " [(        U4SS9[         l        [         R*                  R-                  5         gggs  snnf )	z:Install the profiling signal handler, and start profiling.r   N   signalr/      threadr   r   )targetargsname)r   r(   r6   r    r3   r5   lastmechanismr   SIGPROFr   r   r   r0   inspectcurrentframer   r   items	threadingThreadr   threadr   )	mechanismr(   rptr   rf   fr   s          r   r   r   S  s   EK	1a %''$(! "	!MM&..*@A""C$@5+@+@# )#((*E!$!4!4!6!<!<!>M!>!*1!>MaPC$++$C6EL LL  $   Ns   E9'E9c                 x   [         =R                  S-  sl        [         R                  S:X  Ga  [        S:X  ah  [        R                  " [        R
                  SS5      n [        R                  " [        R                  [        R                  5        U S   [         l        O<[        S:X  a2  [        R                  5         [         R                  R                  5         [         R                  [        5       5        S[         l        [         R"                  R%                  S5      nU(       a  ['        U5        [         $ )z;Stop profiling, and uninstall the profiling signal handler.r   r   r   r/   r   Ns   STATPROF_DEST)r   r6   r   r   r   r   r   SIG_IGNr5   r   setr   joinr<   r    r3   r   environrh   	save_data)r   statprofpaths     r   r	   r	   m  s    	1aI%""6#5#5sC@CMM&..&..9(+AE%i'NNLLeg& $''++,<=l#Lr!   c           
        [        U S5       nUR                  S[        R                  -  5        [        R                   H  nUR
                  nUR                  nU Vs/ s HA  nSR                  UR                  SUR                  -  =(       d    SUR                  /5      PMC     nnUR                  SUSR                  U5      4-  5        M     S S S 5        g s  snf ! , (       d  f       g = f)Nw+bs   %f %f
   s   %ds   %d %s
    )rm   writer   r2   r%   r   r   r   rR   rS   rT   )rR   filesampler   r   ssitess          r   r   r     s    	dE	d

: 6 667mmF;;DLLE A 

AFFEAHH$4$:AJJGH   JJ{dEJJu,=%>>? $ 
	
 
	s   AC AC'+C C  
C.c                p   [        U S5       nUR                  5       R                  5       nS S S 5        WS   R                  5        Vs/ s H  n[	        U5      PM     sn[
        l        / [
        l        USS   H  nUR                  S5      n[	        US   5      nUSS  n/ nU HL  n	U	R                  S5      n
UR                  [        R                  U
S   [        U
S   5      U
S   5      5        MN     [
        R                  R                  [        X5      5        M     g ! , (       d  f       N= fs  snf )Nrk   r   r   r   r      )rm   read
splitlinessplitfloatr   r2   r%   r   rP   rh   intr   )rR   rq   linesvaluers   partsr   rawsitesr   rawsite	sitepartss              r   	load_datar     s    	dD	R	$$& 
 9>a8HI8HueEl8HIEEMab	

5!U1X9Ge,ILLYq\3y|+<ilK   	VE01  
	 Js   D"	D3"
D0c                    [         R                  S:X  d   S5       e[        R                  R	                  5         [         R                  U 5        g)zClear out the state of the profiler.  Do not call while the
profiler is running.

The optional frequency argument specifies the number of samples to
collect per second.r   s'   Can't reset() while statprof is runningN)r   r6   rP   rc   r   r
   )r+   s    r   r
   r
     s:     !#O%OO#NN	KK	r!   c               #     #    [        5          S v   [        5         [        5         g ! [        5         [        5         f = f7fr?   )r   r	   r   rN   r!   r   r   r     s)     	G	 		s   A ' A =A c                  N    \ rS rSrS rS rS rS rS rS r	S r
\S	 5       rS
rg)	SiteStatsi  c                ,    Xl         SU l        SU l        g r   )site	selfcount
totalcountr*   r   s     r   r,   SiteStats.__init__  s    	r!   c                .    U =R                   S-  sl         g Nr   )r   rB   s    r   addselfSiteStats.addself  s    !r!   c                .    U =R                   S-  sl         g r   )r   rB   s    r   addtotalSiteStats.addtotal  s    1r!   c                T    U R                   [        [        R                  5      -  S-  $ Nd   )r   rA   r   r%   rB   s    r   selfpercentSiteStats.selfpercent  s    ~~EMM 22S88r!   c                T    U R                   [        [        R                  5      -  S-  $ r   )r   rA   r   r%   rB   s    r   totalpercentSiteStats.totalpercent  s    U]]!33c99r!   c                D    U R                   [        R                  5       -  $ r?   )r   r   rC   rB   s    r   selfsecondsSiteStats.selfseconds  s    ~~ 8 8 :::r!   c                D    U R                   [        R                  5       -  $ r?   )r   r   rC   rB   s    r   totalsecondsSiteStats.totalseconds  s    !9!9!;;;r!   c                <   0 nU Hp  n[        UR                  5       HT  u  pEUR                  U5      nU(       d  [        U5      nXbU'   UR	                  5         US:X  d  MD  UR                  5         MV     Mr     UR                  5        Vs/ s H  owPM     sn$ s  snf r   )rn   r   rh   r   r   r   values)re   r%   statsr   rr   r   sitestatr   s           r   
buildstatsSiteStats.buildstats  s    F$V\\2 99T?(H"*$K!!#6$$& 3  !<<>*>a>***s   B)r   r   r   N)rG   rH   rI   rJ   r,   r   r   r   r   r   r   r   r   rM   rN   r!   r   r   r     s9    
9:;< + +r!   r   c                  0    \ rS rSrSrSrSrSrSrSr	Sr
S	rg
)DisplayFormatsi  r   r   r   rl   r         rN   N)rG   rH   rI   rJ   ByLineByMethodAboutMethodHotpath
FlameGraphJsonChromerM   rN   r!   r   r   r     s%    FHKGJDFr!   r   c                j   Uc  [         nU c  SSKJn  UR                  n [	        UR
                  5      S:X  a  U R                  S5        gU[        R                  :X  a  [        X 5        OU[        R                  :X  a  [        X 5        OU[        R                  :X  a  [        X 40 UD6  OU[        R                  :X  a  [        X 40 UD6  OmU[        R                   :X  a  [#        X 40 UD6  OLU[        R$                  :X  a  ['        X 5        O,U[        R(                  :X  a  [+        X 40 UD6  O[-        S5      eU[        R$                  [        R(                  4;  aW  U R                  S5        U R                  S[	        UR
                  5      -  5        U R                  S	UR.                  -  5        gg)
z<Print statistics, either to stdout or the given file object.Nr   )procutilr   s   No samples recorded.
zInvalid display formats   ---
s   Sample count: %d
s!   Total time: %f seconds (%f wall)
)r   utilsr  stdoutrA   r%   r   r   r   display_by_liner  display_by_methodr  display_about_methodr  display_hotpathr  write_to_flamer  write_to_jsonr  write_to_chrome	Exceptionr2   )rq   formatdatakwargsr  s        r   r   r     sV   |	z#__
4<<A
*+&&&!	>**	*$#	>--	-T00	>))	)+F+	>,,	,t*6*	>&&	&d	>((	(+F+011n))>+@+@AA

&T\\)::;
69N9NNO Br!   c                   [         R                  U R                  5      nUR                  SS S9  UR	                  SS-  5        UR	                  SS-  5        U H  nUR
                  nSUR                  5       UR                  =(       d    S	UR                  4-  nUR	                  S
UR                  5       UR                  5       UR                  5       U4-  5        M     g)zoPrint the profiler data with each sample line represented
as one row in a table.  Sorted by self-time per line.Tc                "    U R                  5       $ r?   r   xs    r   <lambda>!display_by_line.<locals>.<lambda>  s
    1==?r!   reversekey   %5.5s %10.10s   %7.7s  %-8.8s
s   %  s
   cumulatives   selfr!      %5.5s  %9.9s  %8.8s  %-8.8s
s   time   secondsr$  s   names   %s:%d:%sr      %6.2f %9.2f %9.2f  %s
N)r   r   r%   sortr   r   rx   rS   rT   r   r   r   )r  rq   r   statr   	sitelabels         r   r  r    s       .E	JJt!:J;HH*
/	0 HH(
4	5
 yyMMOKK2MM#
 
	
 	&  "!!#  "		
 r!   c           	     h   UR                  SS-  5        UR                  SS-  5        [        R                  U R                  5      n[	        [
        5      nU HH  nX4R                  R                  5       S-   UR                  R                  -      R                  U5        MJ     / nUR                  5        H^  u  pgSnSn	Sn
U H9  nXR                  5       -  nXR                  5       -  n	XR                  5       -  n
M;     UR                  XhXU45        M`     UR                  SS S	9  U GH  nUS
   S:  a  M  UR                  SUS
   US   US   US   4-  5        US   R                  SS S	9  US    H  nUR                  5       S:  d  M  UR                  R                  S5      n[!        U["        5      (       d  [$        R&                  " U5      nUR                  5       UR                  5       UR                  R(                  =(       d    SU4nUR                  SU-  5        M     GM     g)zPrint the profiler data with each sample function represented
as one row in a table.  Important lines within that function are
output as nested rows.  Sorted by self-time per line.r   r!  r"  r#     :r   Tc                    U S   $ )Nr   rN   r  s    r   r  #display_by_method.<locals>.<lambda>Y  s    !A$r!   r  rl   皙?r%  r   r   r   c                "    U R                  5       $ r?   r  )rr   s    r   r  r,  h  s
    Q]]_r!      r   s   %33.0f%% %6.2f   line %d: %s
N)r   r   r   r%   defaultdictlistr   rx   rT   r   r   r   r   r   r&  rt   rW   rX   r   bytestrrS   )r  rq   r   groupedr'  functiondatafname	sitestatstotal_cum_sectotal_self_sectotal_percentrT   rU   	stattuples                 r   r  r  6  s'    HH*
/	0 HH(
4	5
   .E$G		""$t+dii.@.@@AHHN  L#MMOD..00M..00N--//M 
 	>)L	
 , d7 A;
&		
 	+DEQKD!A%,,R0!&%00%--f5F $$&$$&II$$*		 :YFG   !r!   c                   Uc  [        S5      eS nSU;   a  UR                  S5      u  pBSn0 n0 nU R                   H  n[        UR                  5       H  u  pU
R
                  U:X  d  M  U(       a  U
R                  5       U:X  d  M4  US-  nU	[        UR                  5      S-
  :w  a%  UR                  U	S-      nX;   a
  Xk   S-   Xk'   OSXk'   X;   a  Xz   S-   Xz'   M  SXz'   M     M     UR                  5        VVs/ s H  u  pX4PM
     nnnUR                  SS S9  U H  u  pUR                  SX-  S	-  [        R                  " UR                  5       5      [        R                  " UR
                  5      UR                  =(       d    S
[        R                  " UR                  S5      5      4-  5        M     [         R#                  U R                  5      nU Vs/ s HH  nUR$                  R
                  U:X  d  M  U(       a   UR$                  R                  5       U:X  d  MF  UPMJ     nnSnSnSnSnU HO  nUUR'                  5       -  nUUR)                  5       -  nUUR+                  5       -  nUUR-                  5       -  nMQ     UR                  S[        R                  " U=(       d    S5      [        R                  " U5      UUUU4-  5        UR                  5        VVs/ s H
  u  nnUU4PM     nnnUR                  SS S9  U HX  u  nnUR                  SX-  S	-  UR                  =(       d    S
[        R                  " UR                  S5      5      4-  5        MZ     g s  snnf s  snf s  snnf )NzInvalid functionr*  r   r   Tc                    U S   $ r   rN   r  s    r   r  &display_about_method.<locals>.<lambda>  s    QqTr!   r  s   %6.2f%%   %s:%s   line %s: %s
r   r   2   sA   
    %s:%s    Total: %0.2fs (%0.2f%%)    Self: %0.2fs (%0.2f%%)

s   ___c                    U S   $ r   rN   r  s    r   r  r=    s    adr!   s           %6.2f%%   line %s: %s
)r  r   r%   rn   r   rT   rx   rA   r   r&  r   r   fsencoder   rS   rt   r   r   r   r   r   r   r   )r  rq   rT   r  rx   relevant_samplesparentschildrenr   rr   r   parentcountr   r   r7  r8  total_self_percenttotal_cum_percentr'  childs                        r   r  r  z  sP   *++Hx%^^D1GH,, .GA}}(8 ; A% FLL)A--#\\!a%0F(*1/A*=*+#%-^a%7HN%&HN / $ 5<MMODO=6OGDLL>L2 
.(3.!!&//"34!!&//2#!!&"2"22"67		
 !   .E A66??h& 	
 QVV__.(: 	
 
  MN**,,$**,,d..00T..00	  HHOh0&1h'
	

 4<>>3CD3C<5%3CHDMM$NM3 u
.(3."!!%//""56	
 !] E: Es   =M2"M8#M8+M8.M=c                v  ^^^^^^	  " U4S jS5      mT" S 5      mU R                   S   R                  nU R                    H>  nTR                  UR                  S S S2   UR                  U-
  5        UR                  nM@     UR	                  SS5      m	UUUUU	4S jmTR
                  S:  a  T" TSS5        g g )	Nc                  (   > \ rS rSrS rU 4S jrSrg) display_hotpath.<locals>.HotNodei  c                ,    Xl         SU l        0 U l        g r   )r   rE  rC  r   s     r   r,   )display_hotpath.<locals>.HotNode.__init__  s    IDJDMr!   c                  > U =R                   U-  sl         US   nU R                  R                  U5      nU(       d  T" U5      nX@R                  U'   [        U5      S:  a  SnU[        U5      :  aJ  X   R	                  5       [
        ;   a0  US-  nU[        U5      :  a  X   R	                  5       [
        ;   a  M0  U[        U5      :  a  UR                  XS  U5        g g U=R                   U-  sl         g r9   )rE  rC  rh   rA   r|   skipsadd)r*   r   r   r   rH  rr   HotNodes         r   rP  $display_hotpath.<locals>.HotNode.add  s    JJ$J8DMM%%d+E&+d#5zA~#e*n):):)<)EFA #e*n):):)<)Es5z>IIeBi. " t#r!   )rC  rE  r   N)rG   rH   rI   rJ   r,   rP  rM   )rQ  s   r   rQ  rK    s    	
	$ 	$r!   rQ  r   r   showtimeTc                   > U R                   nU R                  R                  5        Vs/ s H#  oDR                  TTR                  -  :  d  M!  UPM%     nnU(       Ga  US-  S-
  nUR	                  5       S-   R                  S5      nUR                  nSR                  U5      U(       a  SOS-   S-   T(       a  S	U R                  -  OS-   S
-   n	U	U R                  TR                  -  S-  UU4-  n
[        SS[        U
5      -
  5      nSnUS:X  a  SnSnUUSR                  U5      UR                  b  UR                  OSSR                  [        SS[        [        UR                  5      5      -
  5      5      UR                  S5      4-  nX-   n[        U R                  R                  5        Vs/ s H  oDR                  PM     sn5      nU R                  U-
  STR                  -  :  a	  SU-   S-   nOU R                  U-
  S:X  a  SU-   S-   nTR                  US-   5        Un[        U5      S:  d  U(       a  US-  nUR                  SS S9  U H  nT" UU[        U5      S:  5        M     g s  snf s  snf )Nr   r   r*     r!      \   |s    %4.1f%%s    %5.2fss     %s %sr   r   7   s   , s   %s%s %d: %s%ss   liner   r      g?s   [91ms   [0ms   [90m   
Tc                    U R                   $ r?   )rE  r  s    r   r  1display_hotpath.<locals>._write.<locals>.<lambda>&  s    r!   r  )r   rC  r   rE  rx   ljustrT   maxrA   rjustrS   strrt   sumr   r&  )nodedepthmultiple_siblingsr   cvisiblechildrenindentrx   rT   listpattern
liststringspacing_lenprefixcodepattern
codestringfinalstringchildrensamplesnewdepthrH  _writerq   limitrootrS  s                      r   rq  display_hotpath.<locals>._write  sp   yy}}++-
-!UTZZ=O1PA- 	 
 QY]F$.55b9H}}H 		&!-549 /7:

*CA 	  %

TZZ'#-( J ac*o!56KFa*K$k*#{{6B		#aST[[)9%:!:;<r"( J %1K!DMM4H4H4J"K4Jq774J"KLOzzO+sTZZ/?@)K7*Do-2)K7*DHH[5()!#'8MHT/@A$E5(C$81$<= %g
J #Ls    I6I64I;F)r%   r   rP  r   rh   rE  )
r  rq   rr  r  lasttimer   rQ  rq  rs  rS  s
    ``   @@@@r   r  r    s    $ $2 4=D||A##H,,dd#V[[8%;<;;  zz*d+H6> 6>p zzA~tQ r!   c                   Uc  [         R                  S   S-   n[        R                  R	                  U5      (       d&  UR                  SU-  5        UR                  S5        g 0 nU R                   H]  nUR                   Vs/ s H  owR                  PM     nnUR                  5         SR                  U5      n	X;   a  XY   S-   XY'   MY  SXY'   M_     [        R                  " 5       u  p[        US5       nUR                  5        H  u  pUR                  SX4-  5        M     S S S 5        Uc  S	n[        R                  " S
X4-  5        UR                  SU-  5        g s  snf ! , (       d  f       NG= f)Ns   HOMEs   /flamegraph.pls   error: missing %s
s8   get it here: https://github.com/brendangregg/FlameGraph
   ;r   r   s   %s %d
s   ~/flamegraph.svgs   perl ~/flamegraph.pl %s > %ss   Written to %s
)r   r   r   rR   existsr   r%   r   rT   r  r   r   mkstemprm   r   system)r  rq   
scriptpath
outputfiler  r   r   r   r   rs   fdrR   r   rE  s                 r   r  r  .  s:   %%g.1BB
77>>*%%
'*45
MNE,,%+\\2\\2yy=+/EKEK  !HB	dE	d ;;=KDJJzTM12 ) 
 (
II-0BBCHH*,-% 3 
	s   E!6/E&&
E4c                H   U [         ;   a	  [         U    $ [        R                  R                  [        R
                  S5      S   nU/[        R                  -    H<  nU[        R
                  -   nU R                  U5      (       d  M.  U [        U5      S n   O   U [         U '   U $ )zAttempt to make the path to a Python module easier to read by
removing whatever part of the Python search path it was found
on.r   r   N)

_pathcacher   __file__rsplitr   sepr   rR   
startswithrA   )rR   hgpathprk  s       r   simplifypathr  P  s    
 z$%%bffa03FX RVV??6""F&D	 !
 JtKr!   c           	        / nU R                    H  n/ nUR                   Hf  nUR                  [        R                  " UR
                  5      UR                  =(       d    S[        R                  " UR                  5      45        Mh     UR                  UR                  U45        M     [        R                  " U5      n [        U [        5      (       d  U R                  S5      n UR                  U 5        g )Nr   utf-8)r%   r   r   r   sysstrrR   rS   rT   r   jsondumpsrW   rX   encoder   )r  rq   r%   r   r   r   s         r   r  r  a  s    G,,\\ELLOOEJJ/LL&BOOENN3 " 	U+,  ::gDdE""{{7#HHTNr!   c                  ^^^^^^^^^^^ / m[         R                  " 5       m[         R                  " 5       m0 m/ mUUU4S jmSm[        5       mU R                  S   R                  U R                  S   R                  -
  nUT-  m[        UT-  T5      mUUUUUUUUU4	S jnU R                   GHI  m[        S TR                   5       5      n[         R                  " U5      nTU:X  a  M>  T(       aO  U(       aH  TS   US   :X  a<  TR                  5         UR                  5         T(       a  U(       a  TS   US   :X  a  M<  T(       a  U" 5         T(       a  M  [        U5       Hy  nTR                  TR                  [        T5      45        TR                  U5        Uu  pT" [        T5      5      nTR                  [        SU
U	TR                  S-  USS	95        M{     [         R                  " U5      mGML     T(       a  U" 5         T(       a  M  [        T5       VVs/ s H  u  pUT;  d  M  UPM     nnn[         R                  " S
 [        T5       5       5      n[         R"                  " [        XS9SS9n [%        U [&        5      (       d  U R)                  S5      n UR+                  U 5        UR+                  S5        g s  snnf )Nc                   > U (       d  g U T;   a  TU    $ T" U SS  5      n[        T5      nUTU '   TR                  [        U S   S   SU S   -  S95        Ub  TS   R                  US9  U$ )Nr   r   z%s %s)categoryr   r   )rD  )rA   r   dictupdate)r   rD  myidid2stackstack2idstackids      r   r   write_to_chrome.<locals>.stackid  s    HE?"qr#8}eAhqk%(8JKLRLv.r!   r1   r   r   c                 D  >	 T" [        T
5      5      n T
R                  5       u  pT	R                  5       u  p4TR                  U-
  nTUs=::  a  T::  a=  O  O:[        UT-   TR                  5      nTR	                  [        SUUU US-  SS95        g TR                  U5        g )NE    .Ar   )phr   catsftspid)tuplepopleftr   r^  r   r  rP  )oldsidoldcatoldfuncoldtimeoldidxduration
sampletime	blacklistclamplastseen	laststackmaxthresholdminthresholdr   r%   r  s          r   poplast write_to_chrome.<locals>.poplast  s    y)*#++-"**,;;(83|3Wu_fkk:JNN !C'	 MM&!r!   c              3     #    U  Hf  nS [        [        R                  " UR                  5      5      UR                  =(       d    S4-  [        R                  " UR
                  5      4v   Mh     g7f)z%s:%dr   N)r  r   r  rR   rS   rT   ).0r   s     r   	<genexpr>"write_to_chrome.<locals>.<genexpr>  s]      

 &  !<=LL&B
 / &s   A.A0Br  )r  r   r  r  r  r  c              3  @   #    U  H  u  p[        U5      U4v   M     g 7fr?   )r`  )r  rf   rg   s      r   r  r    s      %"5Q"5s   )traceEventsstackFramesr   )rg  r  rZ  )collectionsdequer   r%   r   r^  r  r   popreversed
appendleftrA   r   r  rn   OrderedDictr  r  rW   rX   r  r   )r  rq   r  r  	totaltimer  r   qstackr   rR   r   sididxr   eventsframesr  r  r  r  r  r%   r  r  s     ``         `  @@@@@@@@r   r  r  y  si   G!!#I  "H
 HH& E IR %%Q(<(<<I|+Ly</7L" "0 ,, 

  

 

 ""5)Fy}r
'BMMOJJL Fy}r
'B I i&!Ac'l ;<  #JD%	*+CNN{{S(	 "  %%e,	G H 	 ) #,G"4"4;398L"4   $$ %"+H"5% F ::dvB1MDdE""{{7#HHTNHHUOs   $K4Kc                     [        S5        g )Na  
The statprof command line allows you to inspect the last profile's results in
the following forms:

usage:
    hotpath [-l --limit percent]
        Shows a graph of calls with the percent of time each takes.
        Red calls take over 10%% of the total time themselves.
    lines
        Shows the actual sampled lines.
    functions
        Shows the samples grouped by function.
    function [filename:]functionname
        Shows the callers and callees of a particular function.
    flame [-s --script-path] [-o --output-file path]
        Writes out a flamegraph to output-file (defaults to ~/flamegraph.svg)
        Requires that ~/flamegraph.pl exist.
        (Specify alternate script path with --script-path.))printrN   r!   r   
printusager    s    		?r!   c                   U c  [         R                  n [        U 5      S:X  a  [        5         g0 nSnS US'   U S   S:X  a  [        R
                  US'   OU S   S:X  a  [        R                  US'   OlU S   S:X  a  [        R                  US'   OOU S   S	:X  a  [        R                  US'   U S   US'   S
nO(U S   S:X  a  [        R                  US'   O[        5         g [        R                  " [        R                  US  S/ SQ5      u  p4SUS'   S nU HW  u  pxUS;   a  [!        U5      US'   M  US;   a  UnM%  US;   a  XS'   M1  US;   a  XS'   M=  US;   a  [        5           g SU-  5       e   U(       d  [        S5        g[#        US9  [%        S0 [        R&                  " U5      D6  g! [        R                   a  n[        U5        [        5          S nAgS nAff = f)Nr   r   r   s   functionhotpaths   formatr   	functionsrT   rl   flames	   hl:f:o:p:)s   helps   limit=s   file=s   output-file=s   script-path=r-  s   limit)z-lz--limit)z-fz--file)z-oz--output-files
   outputfile)z-pz--script-paths
   scriptpath)z-hhelpzunhandled option %szmust specify --file to load)rR   rN   )r   argvrA   r  r   r  r   r  r  r  r   getoptbsysargvgetopterrorr  r   r   r   	strkwargs)	r  displayargsoptstartoptsr   msgrR   or   s	            r   mainr  	  s   |xx
4yA~KH#KAw)!/!7!7I	aG	!/!6!6I	aK	!/!8!8I	aJ	!/!;!;I#'7K 	aG	!/!:!:I	%%XY'L

 !KD!!$)%LK!""D))).&))).&. L3/!335  +,4.h  -.= << c
s   -F+ +G?GG__main__)r   r'   r?   )Nrl   N)r-  )NN)g{Gzt?g+?)7__doc__
__future__r   r  
contextlibr  r   r  r   r   r   r   r   typingr    r   r   r0  contextmanager__all__rO  r    r#   r   rP   r   r   Eventr   r   r   r   r   r	   r   r   r
   r   r   r   r   r  r  r  r  r  r  r  r  r  r  r  rG   exitrN   r!   r   <module>r     s[  0N` #      	  
  
 %%**
:	*+, ,^ 	7: 7:t# #8	$ __
(# !4*
@2(  (+ (+V !PH
BAHHT
nZz.> 
"0vr.AH zHHTV r!   