§
    cÌc“-  ã                   ó  — U d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZm	Z	 d dl
mZmZ ddlmZ ddgZd„ Z G d	„ d
¦  «        Z G d„ d¦  «        Z G d„ d¦  «        Zi Zeeef         ed<   dd„Zd„ Zedk    r e¦   «          dS dS )é    N)ÚDictÚTextIO)ÚProfilerÚprofiler_entryé   )ÚerrorsÚprofileÚStatsc                 ó¼   — t          ¦   «         }|                     ¦   «          	  | |i |¤Ž}|                     ¦   «         }n# |                     ¦   «         }w xY w||fS )a†  Run a function profile.

    Exceptions are not caught: If you need stats even when exceptions are to be
    raised, pass in a closure that will catch the exceptions and transform them
    appropriately for your driver function.

    Important caveat: only one profile can execute at a time. See BzrProfiler
    for details.

    :return: The functions return value and a stats object.
    )ÚBzrProfilerÚstartÚstop)ÚfÚargsÚkwdsÚprofilerÚretÚstatss         ú//usr/lib/python3/dist-packages/breezy/lsprof.pyr	   r	      sh   € õ ‰}Œ}€HØ‡N‚NÑÔÐð Øˆa�Ð˜ÐÐˆà—’‘”ˆˆø�—’‘”ˆÐÐÐÐØ�ˆ:Ðs   ¤A ÁAc                   óJ   — e Zd ZdZdZ	  ej        ¦   «         Z	 d„ Zd„ Z	d„ Z
dS )r   aa  Bzr utility wrapper around Profiler.

    For most uses the module level 'profile()' function will be suitable.
    However profiling when a simple wrapped function isn't available may
    be easier to accomplish using this class.

    To use it, create a BzrProfiler and call start() on it. Some arbitrary
    time later call stop() to stop profiling and retrieve the statistics
    from the code executed in the interim.

    Note that profiling involves a threading.Lock around the actual profiling.
    This is needed because profiling involves global manipulation of the python
    interpreter state. As such you cannot perform multiple profiles at once.
    Trying to do so will lock out the second profiler unless the global
    breezy.lsprof.BzrProfiler.profiler_block is set to 0. Setting it to 0 will
    cause profiling to fail rather than blocking.
    r   c                 ó~  — i | _         t          ¦   «         | _        | j        j                             | j        j        ¦  «        }|st          j        d¬¦  «        ‚	 | j         	                    d¬¦  «         t          j        | j        ¦  «         dS # t          $ r  | j        j                             ¦   «          ‚ w xY w)z{Start profiling.

        This hooks into threading and will record all calls made until
        stop() is called.
        zAlready profiling something)ÚmsgT)ÚsubcallsN)Ú_g_threadmapr   ÚpÚ	__class__Úprofiler_lockÚacquireÚprofiler_blockr   ÚInternalBzrErrorÚenableÚ	threadingÚ
setprofileÚ_thread_profileÚBaseExceptionÚrelease)ÚselfÚ	permitteds     r   r   zBzrProfiler.startC   sÁ   € ð ˆÔÝ‘”ˆŒØ”NÔ0×8Ò8ØŒNÔ)ñ+ô +ˆ	àð 	MÝÔ)Ð.KÐLÑLÔLÐLð	ØŒF�MŠM 4ˆMÑ(Ô(Ð(ÝÔ  Ô!5Ñ6Ô6Ð6Ð6Ð6øÝð 	ð 	ð 	ØŒNÔ(×0Ò0Ñ2Ô2Ð2Øð	øøøs   Á4B Â*B<c                 ó:  — 	 | j                              ¦   «          | j                             ¦   «         D ]}|                     ¦   «          Œt	          j        d¦  «         | j         }d| _         i }| j                             ¦   «         D ]*\  }}t          |                     ¦   «         i ¦  «        ||<   Œ+d| _        t          |                     ¦   «         |¦  «        | j	        j
                             ¦   «          S # | j	        j
                             ¦   «          w xY w)z¶Stop profiling.

        This unhooks from threading and cleans up the profiler, returning
        the gathered Stats object.

        :return: A breezy.lsprof.Stats object.
        N)r   Údisabler   Úvaluesr"   r#   Úitemsr
   Úgetstatsr   r   r&   )r'   Úppr   ÚthreadsÚtids        r   r   zBzrProfiler.stopV   sý   € ð	3ØŒF�NŠNÑÔÐØÔ'×.Ò.Ñ0Ô0ð ð �Ø—
’
‘”��ÝÔ  Ñ&Ô&Ð&Ø”ˆAØˆDŒFØˆGØÔ,×2Ò2Ñ4Ô4ð 8ð 8‘��RÝ$ R§[¢[¡]¤]°BÑ7Ô7�˜‘�Ø $ˆDÔÝ˜Ÿš™œ wÑ/Ô/àŒNÔ(×0Ò0Ñ2Ô2Ð2Ð2øˆDŒNÔ(×0Ò0Ñ2Ô2Ð2Ð2øøøs   ‚CC: Ã: Dc                 óŠ   — t          j        ¦   «         }t          ¦   «         x| j        |<   }|                     dd¬¦  «         d S )NT)r   Úbuiltins)Ú_threadÚ	get_identr   r   r!   )r'   r   r   r   Úthrr   s         r   r$   zBzrProfiler._thread_profilem   sD   € õ ÔÑ!Ô!ˆÝ%-¡Z¤ZÐ/ˆÔ˜#Ñ à	�Š˜$¨ˆÑ.Ô.Ð.Ð.Ð.ó    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r"   ÚLockr   r   r   r$   © r6   r   r   r   *   sg   € € € € € ðð ð$ €NØOà"�I”NÑ$Ô$€MØ1ðð ð ð&3ð 3ð 3ð./ð /ð /ð /ð /r6   r   c                   ó<   — e Zd ZdZd„ Zdd„Zdd„Zd„ Zd	„ Zdd
„Z	dS )r
   zðWrapper around the collected data.

    A Stats instance is created when the profiler finishes. Normal
    usage is to use save() to write out the data to a file, or pprint()
    to write human-readable information to the command line.
    c                 ó"   — || _         || _        d S ©N)Údatar/   )r'   r@   r/   s      r   Ú__init__zStats.__init__~   s   € ØˆŒ	ØˆŒˆˆr6   Ú
inlinetimeTc                 ó  — |t           j        vs|dk    rt          d|z  ¦  «        ‚t          j        |¦  «        }| j                             ||¬¦  «         | j        D ]%}|j        r|j                             ||¬¦  «         Œ&dS )zeSort the data by the supplied critera.

        :param crit: the data attribute used as the sort key.ÚcodezCan't sort by %s)ÚkeyÚreverseN)r   Ú__dict__Ú
ValueErrorÚoperatorÚ
attrgetterr@   ÚsortÚcalls)r'   ÚcritrF   Úkey_funcÚes        r   rK   z
Stats.sort‚   s”   € ð •~Ô.Ð.Ð.°$¸&².°.ÝÐ/°$Ñ6Ñ7Ô7Ð7åÔ& tÑ,Ô,ˆØŒ	�Š˜8¨WˆÑ5Ô5Ð5à”ð 	<ð 	<ˆAØŒwð <Ø”—’ °7�Ñ;Ô;Ð;øð	<ð 	<r6   Nc                 óÄ  — |€t           j        }| j        }|�
|d|…         }d}d}|                     |dz  ¦  «         |D ]Ÿ}|                     ||j        |j        |j        |j        t          |j	        ¦  «        fz  ¦  «         |j
        rS|j
        D ]K}|                     |d|j        z  |j        |j        |j        dt          |j	        ¦  «        z  fz  ¦  «         ŒLŒ dS )a  Pretty-print the data as plain text for human consumption.

        :param top: only output the top n entries.
            The default value of None means output all data.
        :param file: the output file; if None, output will
            default to stdout.Nz% 12s %12s %11.4f %11.4f   %s
z% 12s %12s %12s %12s %s
)Ú	CallCountÚ	Recursivez	Total(ms)z
Inline(ms)zmodule:lineno(function)z+%s)ÚsysÚstdoutr@   ÚwriteÚ	callcountÚreccallcountÚ	totaltimerB   ÚlabelrD   rL   )r'   ÚtopÚfileÚdÚcolsÚhcolsrO   Úses           r   ÚpprintzStats.pprint�   s$  € ð ˆ<Ý”:ˆDØŒIˆØˆ?Ø�$�3�$”ˆAØ0ˆØ+ˆØ�
Š
�5ð Eñ Eñ 	Fô 	Fð 	Fàð 	@ð 	@ˆAØ�JŠJ�t˜qœ{¨A¬N¸A¼KØ œ|­U°1´6©]¬]ð<ñ <ñ =ô =ð =àŒwð @Øœ'ð @ð @�BØ—J’J˜t u¨r¬|Ñ';¸R¼_Ø')¤|°R´]Ø',­u°R´W©~¬~Ñ'=ð'?ñ  ?ñ @ô @ð @ð @øð	@ð 	@r6   c                 ó˜  — t          t          | j        ¦  «        ¦  «        D ]÷}| j        |         }t          |j        t
          ¦  «        s> t          |¦  «        t          |j        ¦  «        f|dd…         z   ¦  «        | j        |<   |j        r‰t          t          |j        ¦  «        ¦  «        D ]g}|j        |         }t          |j        t
          ¦  «        s> t          |¦  «        t          |j        ¦  «        f|dd…         z   ¦  «        |j        |<   ŒhŒø| j	         
                    ¦   «         D ]}|                     ¦   «          ŒdS )zwReplace all references to code objects with string
        descriptions; this makes it possible to pickle the instance.r   N)ÚrangeÚlenr@   Ú
isinstancerD   ÚstrÚtyperY   rL   r/   r+   Úfreeze)r'   ÚirO   Újr_   Úss         r   rg   zStats.freeze©   s/  € õ
 •s˜4œ9‘~”~Ñ&Ô&ð 	Jð 	JˆAØ”	˜!”ˆAÝ˜aœf¥cÑ*Ô*ð AØ&�t A™wœw­¨a¬f©¬Ð'7¸!¸A¸B¸B¼%Ñ'?Ñ@Ô@�”	˜!‘ØŒwð JÝ�s 1¤7™|œ|Ñ,Ô,ð Jð J�AØœ œ�BÝ% b¤g­sÑ3Ô3ð JØ%-¥T¨"¡X¤X­u°R´W©~¬~Ð.?À"ÀQÀRÀRÄ&Ñ.HÑ%IÔ%I˜œ ™
øøØ”×$Ò$Ñ&Ô&ð 	ð 	ˆAØ�HŠH‰JŒJˆJˆJð	ð 	r6   c                 óT   — t          | j        ¦  «                             |¦  «         dS )z;Output profiling data in calltree format (for KCacheGrind).N)Ú_CallTreeFilterr@   Úoutput)r'   r[   s     r   ÚcalltreezStats.calltreeº   s&   € å˜œ	Ñ"Ô"×)Ò)¨$Ñ/Ô/Ð/Ð/Ð/r6   c                 ó€  — |€yt           j                             |¦  «        }|                     d¦  «        rd}nBt           j                             |¦  «        d         }t          |¦  «        dk    r
|dd…         }t          |d¦  «        5 }|dk    r1|                       t          j	        d¦  «        |¦  «        ¦  «         nb|dk    r2|  
                     t          j	        d¦  «        |¦  «        ¬¦  «         n*|                      ¦   «          t          j        | |d	¦  «         ddd¦  «         dS # 1 swxY w Y   dS )
aâ  Save profiling data to a file.

        :param filename: the name of the output file
        :param format: 'txt' for a text representation;
            'callgrind' for calltree format;
            otherwise a pickled Python object. A format of None indicates
            that the format to use is to be found from the filename. If
            the name starts with callgrind.out, callgrind format is used
            otherwise the format is given by the filename extension.
        Nzcallgrind.outÚ	callgrindr   Úwbzutf-8Útxt)r[   é   )ÚosÚpathÚbasenameÚ
startswithÚsplitextrc   Úopenrn   ÚcodecsÚ	getwriterr`   rg   ÚpickleÚdump)r'   ÚfilenameÚformatrv   ÚextÚoutfiles         r   Úsavez
Stats.save¾   sq  € ð ˆ>Ý”w×'Ò'¨Ñ1Ô1ˆHØ×"Ò" ?Ñ3Ô3ð %Ø$��å”g×&Ò& xÑ0Ô0°Ô3�Ý�s‘8”8˜a’<�<Ø    œW�FÝ�(˜DÑ!Ô!ð 
	. WØ˜Ò$Ð$ð —’Ð7�fÔ.¨wÑ7Ô7¸Ñ@Ô@ÑAÔAÐAÐAØ˜5’�Ø—’Ð!:¥Ô!1°'Ñ!:Ô!:¸7Ñ!CÔ!C�ÑDÔDÐDÐDà—’‘”�Ý”˜D '¨1Ñ-Ô-Ð-ð
	.ð 
	.ð 
	.ñ 
	.ô 
	.ð 
	.ð 
	.ð 
	.ð 
	.ð 
	.ð 
	.ð 
	.øøøð 
	.ð 
	.ð 
	.ð 
	.ð 
	.ð 
	.s   ÂBD3Ä3D7Ä:D7)rB   T)NNr?   )
r7   r8   r9   r:   rA   rK   r`   rg   rn   r‚   r<   r6   r   r
   r
   v   s‹   € € € € € ðð ðð ð ð<ð <ð <ð <ð@ð @ð @ð @ð2ð ð ð"0ð 0ð 0ð.ð .ð .ð .ð .ð .r6   c                   ó<   — e Zd ZU dZeed<   d„ Zd„ Zd„ Zd„ Z	d„ Z
dS )	rl   a„  Converter of a Stats object to input suitable for KCacheGrind.

    This code is taken from http://ddaa.net/blog/python/lsprof-calltree
    with the changes made by J.P. Calderone and Itamar applied. Note that
    isinstance(code, str) needs to be used at times to determine if the code
    object is actually an external code object (with a filename, etc.) or
    a Python built-in.
    Úout_filec                 ó   — || _         d S r?   )r@   )r'   r@   s     r   rA   z_CallTreeFilter.__init__ê   s   € ØˆŒ	ˆ	ˆ	r6   c                 ó¤   — || _         |                     d¦  «         |                      ¦   «          | j        D ]}|                      |¦  «         Œd S )Nzevents: Ticks
)r„   rU   Ú_print_summaryr@   Ú_entry)r'   r„   Úentrys      r   rm   z_CallTreeFilter.outputí   s^   € Ø ˆŒØ�ŠÐ(Ñ)Ô)Ð)Ø×ÒÑÔÐØ”Yð 	ð 	ˆEØ�KŠK˜ÑÔÐÐð	ð 	r6   c                 ó¨   — d}| j         D ])}t          |j        dz  ¦  «        }t          ||¦  «        }Œ*| j                             d|fz  ¦  «         d S )Nr   éè  zsummary: %d
)r@   ÚintrX   Úmaxr„   rU   )r'   Úmax_costr‰   rX   s       r   r‡   z_CallTreeFilter._print_summaryô   s`   € ØˆØ”Yð 	0ð 	0ˆEÝ˜EœO¨dÑ2Ñ3Ô3ˆIÝ˜8 YÑ/Ô/ˆHˆHØŒ×Ò˜O¨x¨kÑ9Ñ:Ô:Ð:Ð:Ð:r6   c                 óæ  — | j         }|j        }t          |j        dz  ¦  «        }t	          |t
          ¦  «        r|                     d¦  «         n-|                     d                     |j        ¦  «        ¦  «         |                     d                     t          |d¦  «        ¦  «        ¦  «         t	          |t
          ¦  «        r)|                     d                     |¦  «        ¦  «         n|                     d|j
        |fz  ¦  «         |j        r|j        }ng }t	          |t
          ¦  «        rd}n|j
        }|D ]}|                      ||¦  «         Œ|                     d	¦  «         d S )
Nr‹   zfi=~
zfi={}
zfn={}
Tz0  {}
ú%d %d
r   ú
)r„   rD   rŒ   rB   rd   re   rU   r   Úco_filenamerY   Úco_firstlinenorL   Ú	_subentry)r'   r‰   r„   rD   rB   rL   ÚlinenoÚsubentrys           r   rˆ   z_CallTreeFilter._entryû   se  € Ø”=ˆØŒzˆÝ˜Ô)¨DÑ0Ñ1Ô1ˆ
Ý�d�CÑ Ô ð 	?Ø�NŠN˜8Ñ$Ô$Ð$Ð$à�NŠN˜9×+Ò+¨DÔ,<Ñ=Ô=Ñ>Ô>Ð>Ø�Š�y×'Ò'­¨d°DÑ(9Ô(9Ñ:Ô:Ñ;Ô;Ð;Ý�d�CÑ Ô ð 	JØ�NŠN˜9×+Ò+¨JÑ7Ô7Ñ8Ô8Ð8Ð8à�NŠN˜9¨Ô(;¸ZÐ'HÑHÑIÔIÐIàŒ;ð 	Ø”KˆEˆEàˆEÝ�d�CÑ Ô ð 	)ØˆFˆFàÔ(ˆFØð 	-ð 	-ˆHØ�NŠN˜6 8Ñ,Ô,Ð,Ð,Ø�Š�tÑÔÐÐÐr6   c                 ó�  — | j         }|j        }t          |j        dz  ¦  «        }t	          |t
          ¦  «        rj|                     d¦  «         |                     d                     t          |d¦  «        ¦  «        ¦  «         |                     d|j	        fz  ¦  «         n‡|                     d                     |j
        ¦  «        ¦  «         |                     d                     t          |d¦  «        ¦  «        ¦  «         |                     d|j	        |j        fz  ¦  «         |                     d||fz  ¦  «         d S )	Nr‹   zcfi=~
zcfn={}
Tzcalls=%d 0
zcfi={}
zcalls=%d %d
r�   )r„   rD   rŒ   rX   rd   re   rU   r   rY   rV   r’   r“   )r'   r•   r–   r„   rD   rX   s         r   r”   z_CallTreeFilter._subentry  s/  € Ø”=ˆØŒ}ˆÝ˜Ô*¨TÑ1Ñ2Ô2ˆ	Ý�d�CÑ Ô ð 	:Ø�NŠN˜9Ñ%Ô%Ð%Ø�NŠN˜:×,Ò,­U°4¸Ñ->Ô->Ñ?Ô?Ñ@Ô@Ð@Ø�NŠN˜>¨XÔ-?Ð,AÑAÑBÔBÐBÐBà�NŠN˜:×,Ò,¨TÔ-=Ñ>Ô>Ñ?Ô?Ð?Ø�NŠN˜:×,Ò,­U°4¸Ñ->Ô->Ñ?Ô?Ñ@Ô@Ð@Ø�NŠN˜?ØÔ" DÔ$7ð.9ñ 9ñ :ô :ð :à�Š�y F¨IÐ#6Ñ6Ñ7Ô7Ð7Ð7Ð7r6   N)r7   r8   r9   r:   r   Ú__annotations__rA   rm   r‡   rˆ   r”   r<   r6   r   rl   rl   Þ   sy   € € € € € € ðð ð ÐÐÑðð ð ðð ð ð;ð ;ð ;ðð ð ð48ð 8ð 8ð 8ð 8r6   rl   Ú_fn2modFc                 ó  — t          | t          ¦  «        r| S 	 t          | j                 }n¯# t          $ r¢ t
          j                             ¦   «         D ]g\  }}|€Œt          |dd ¦  «        €Œt          |j	        t          ¦  «        sŒ5|j	         
                    | j        ¦  «        r|x}t          | j        <    nŒhd| j        z  x}t          | j        <   Y nw xY w|rd| j        || j        fz  S d|| j        | j        fz  S )NÚ__file__z<%s>z%s %s:%dz	%s:%d(%s))rd   re   r™   r’   ÚKeyErrorrS   Úmodulesr,   Úgetattrr›   rw   Úco_namer“   )rD   rn   ÚmnameÚkÚvs        r   rY   rY   (  s2  € Ý�$�ÑÔð ØˆðJÝ˜Ô(Ô)ˆˆøÝð Jð Jð JÝ”K×%Ò%Ñ'Ô'ð 	Jð 	J‰DˆAˆqØˆyØÝ�q˜* dÑ+Ô+Ð3ØÝ˜aœj­#Ñ.Ô.ð ØØŒz×$Ò$ TÔ%5Ñ6Ô6ð Ø45Ð5�� Ô 0Ñ1Ø�ðð 17¸Ô9IÑ0IÐIˆE•G˜DÔ,Ñ-øøðJøøøð ð HØ˜Tœ\¨5°$Ô2EÐFÑFÐFà˜e TÔ%8¸$¼,ÐGÑGÐGs   ™, ¬B)CÃCc                  ól  — t           j        dd …         t           _        t           j        s3t           j                             d¦  «         t          j        d¦  «         dd l} t          | j        t           j        d         d¬¦  «        \  }}|                     ¦   «          | 	                    ¦   «          d S )Nr   z)usage: lsprof.py <script> <arguments...>
rs   r   Ú__main__)Úrun_name)
rS   ÚargvÚstderrrU   ÚexitÚrunpyr	   Úrun_pathrK   r`   )r©   Úresultr   s      r   Úmainr¬   @  sˆ   € ÝŒx˜˜˜Œ|…C„HÝŒ8ð ÝŒ
×ÒÐEÑFÔFÐFÝŒ�‰ŒˆØ€L€L€LÝ˜EœN­C¬H°Q¬KÀ*ÐMÑMÔM�M€FˆEØ	‡J‚J�L„L€LØ	‡L‚L�N„N€N€N€Nr6   r¤   )F)rz   r|   rI   rt   rS   r3   r"   Útypingr   r   Ú_lsprofr   r   Ú r   Ú__all__r	   r   r
   rl   r™   re   Úobjectr˜   rY   r¬   r7   r<   r6   r   ú<module>r²      sŸ  ðð €€€€Ø €€€Ø €€€Ø 	€	€	€	Ø 
€
€
€
Ø €€€Ø Ð Ð Ð Ø Ð Ð Ð Ð Ð Ð Ð Ø ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,à Ð Ð Ð Ð Ð à�gÐ
€ðð ð ð*I/ð I/ð I/ð I/ð I/ñ I/ô I/ð I/ðXe.ð e.ð e.ð e.ð e.ñ e.ô e.ð e.ðPD8ð D8ð D8ð D8ð D8ñ D8ô D8ð D8ðN  €ˆˆc�6ˆkÔ	Ð Ð Ñ ðHð Hð Hð Hð0ð ð ð ˆzÒÐØ€D�F„F€F€F€Fð Ðr6   