
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!DOCTYPE html>
<html>
3

  \A  ã               @   s  d dl mZmZ d dlmZ d dlZd dlZd dlZd dl	Z	d dl
T d dl
mZmZ dd„ ZG dd	„ d	ƒZG d
d„ dƒZdd„ ZeG dd„ dƒƒZeG dd„ deƒƒZdd„ ZG dd„ dƒZG dd„ deƒZdd„ ZG dd„ dƒZG dd„ deƒZG dd„ deƒZG d d!„ d!ƒZd"d#„ ZdS )$é    )ÚSequenceÚIterable)Útotal_orderingN)Ú*)Ú_get_object_tracebackÚ_get_tracesc             C   sz   xtdD ]l}t | ƒdk r:|dkr:|r.d| |f S d| |f S t | ƒdk sN|dkrj|r^d| |f S d| |f S | d
 } qW d S )NÚBÚKiBÚMiBÚGiBÚTiBéd   z%+.1f %sz%.1f %sé
   i   z%+.0f %sz%.0f %s)r   r	   r
   r   r   i (  )Úabs)ÚsizeZsignZunit© r   ú#/usr/lib64/python3.6/tracemalloc.pyÚ_format_size   s    
r   c               @   sD   e Zd ZdZdZdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dS )Ú	StatisticzS
    Statistic difference on memory allocations between two Snapshot instance.
    Ú	tracebackr   Úcountc             C   s   || _ || _|| _d S )N)r   r   r   )Úselfr   r   r   r   r   r   Ú__init__%   s    zStatistic.__init__c             C   s   t | j| j| jfƒS )N)Úhashr   r   r   )r   r   r   r   Ú__hash__*   s    zStatistic.__hash__c             C   s$   | j |j ko"| j|jko"| j|jkS )N)r   r   r   )r   Úotherr   r   r   Ú__eq__-   s    zStatistic.__eq__c             C   sB   d| j t| jdƒ| jf }| jr>| j| j }|dt|dƒ 7 }|S )Nz%s: size=%s, count=%iFz, average=%s)r   r   r   r   )r   ÚtextÚaverager   r   r   Ú__str__2   s    

zStatistic.__str__c             C   s   d| j | j| jf S )Nz)<Statistic traceback=%r size=%i count=%i>)r   r   r   )r   r   r   r   Ú__repr__<   s    zStatistic.__repr__c             C   s   | j | j| jfS )N)r   r   r   )r   r   r   r   Ú	_sort_key@   s    zStatistic._sort_keyN)r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r   r   r   r   r    r!   r   r   r   r   r      s   
r   c               @   sD   e Zd ZdZdZdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Z	dd„ Z
dS )ÚStatisticDiffzd
    Statistic difference on memory allocations between an old and a new
    Snapshot instance.
    r   r   Ú	size_diffr   Ú
count_diffc             C   s"   || _ || _|| _|| _|| _d S )N)r   r   r(   r   r)   )r   r   r   r(   r   r)   r   r   r   r   K   s
    zStatisticDiff.__init__c             C   s   t | j| j| j| j| jfƒS )N)r   r   r   r(   r   r)   )r   r   r   r   r   R   s    zStatisticDiff.__hash__c             C   s<   | j |j ko:| j|jko:| j|jko:| j|jko:| j|jkS )N)r   r   r(   r   r)   )r   r   r   r   r   r   V   s
    zStatisticDiff.__eq__c             C   sP   d| j t| jdƒt| jdƒ| j| jf }| jrL| j| j }|dt|dƒ 7 }|S )Nz %s: size=%s (%s), count=%i (%+i)FTz, average=%s)r   r   r   r(   r   r)   )r   r   r   r   r   r   r   ]   s    


zStatisticDiff.__str__c             C   s   d| j | j| j| j| jf S )Nz9<StatisticDiff traceback=%r size=%i (%+i) count=%i (%+i)>)r   r   r(   r   r)   )r   r   r   r   r    i   s    zStatisticDiff.__repr__c             C   s    t | jƒ| jt | jƒ| j| jfS )N)r   r(   r   r)   r   r   )r   r   r   r   r!   n   s    zStatisticDiff._sort_keyN)r   r   r(   r   r)   )r"   r#   r$   r%   r&   r   r   r   r   r    r!   r   r   r   r   r'   D   s   r'   c             C   s²   g }xp|j ƒ D ]d\}}| j|d ƒ}|d k	rPt||j|j|j |j|j|j ƒ}nt||j|j|j|jƒ}|j|ƒ qW x6| j ƒ D ]*\}}t|d|j d|j ƒ}|j|ƒ q€W |S )Nr   )ÚitemsÚpopr'   r   r   Úappend)Ú	old_groupÚ	new_groupÚ
statisticsr   ÚstatZpreviousr   r   r   Ú_compare_grouped_statst   s    r1   c               @   s\   e Zd ZdZdZdd„ Zedd„ ƒZedd„ ƒZd	d
„ Z	dd„ Z
dd„ Zdd„ Zdd„ ZdS )ÚFramez
    Frame of a traceback.
    Ú_framec             C   s
   || _ d S )N)r3   )r   Úframer   r   r   r   �   s    zFrame.__init__c             C   s
   | j d S )Nr   )r3   )r   r   r   r   Úfilename“   s    zFrame.filenamec             C   s
   | j d S )Né   )r3   )r   r   r   r   Úlineno—   s    zFrame.linenoc             C   s   | j |j kS )N)r3   )r   r   r   r   r   r   ›   s    zFrame.__eq__c             C   s   | j |j k S )N)r3   )r   r   r   r   r   Ú__lt__ž   s    zFrame.__lt__c             C   s
   t | jƒS )N)r   r3   )r   r   r   r   r   ¡   s    zFrame.__hash__c             C   s   d| j | jf S )Nz%s:%s)r5   r7   )r   r   r   r   r   ¤   s    zFrame.__str__c             C   s   d| j | jf S )Nz<Frame filename=%r lineno=%r>)r5   r7   )r   r   r   r   r    §   s    zFrame.__repr__N)r3   )r"   r#   r$   r%   r&   r   Úpropertyr5   r7   r   r8   r   r   r    r   r   r   r   r2   ˆ   s   r2   c               @   sf   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zddd„ZdS )Ú	Tracebackz`
    Sequence of Frame instances sorted from the most recent frame
    to the oldest frame.
    Ú_framesc             C   s   t j| ƒ || _d S )N)r   r   r;   )r   Úframesr   r   r   r   ³   s    
zTraceback.__init__c             C   s
   t | jƒS )N)Úlenr;   )r   r   r   r   Ú__len__¹   s    zTraceback.__len__c             C   s4   t |tƒr"tdd„ | j| D ƒƒS t| j| ƒS d S )Nc             s   s   | ]}t |ƒV  qd S )N)r2   )Ú.0Útracer   r   r   ú	<genexpr>¾   s    z(Traceback.__getitem__.<locals>.<genexpr>)Ú
isinstanceÚsliceÚtupler;   r2   )r   Úindexr   r   r   Ú__getitem__¼   s    
zTraceback.__getitem__c             C   s   |j | jkS )N)r3   r;   )r   r4   r   r   r   Ú__contains__Â   s    zTraceback.__contains__c             C   s
   t | jƒS )N)r   r;   )r   r   r   r   r   Å   s    zTraceback.__hash__c             C   s   | j |j kS )N)r;   )r   r   r   r   r   r   È   s    zTraceback.__eq__c             C   s   | j |j k S )N)r;   )r   r   r   r   r   r8   Ë   s    zTraceback.__lt__c             C   s   t | d ƒS )Nr   )Ústr)r   r   r   r   r   Î   s    zTraceback.__str__c             C   s   dt | ƒf S )Nz<Traceback %r>)rD   )r   r   r   r   r    Ñ   s    zTraceback.__repr__Nc             C   sn   g }|d k	r|dk r|S xP| d |… D ]@}|j d|j|jf ƒ tj|j|jƒjƒ }|r&|j d| ƒ q&W |S )Nr   z  File "%s", line %sz    %s)r,   r5   r7   Ú	linecacheÚgetlineÚstrip)r   ÚlimitÚlinesr4   Úliner   r   r   ÚformatÔ   s    zTraceback.format)r;   )N)r"   r#   r$   r%   r&   r   r>   rF   rG   r   r   r8   r   r    rO   r   r   r   r   r:   «   s   r:   c             C   s    t | ƒ}|dk	rt|ƒS dS dS )zé
    Get the traceback where the Python object *obj* was allocated.
    Return a Traceback instance.

    Return None if the tracemalloc module is not tracing memory allocations or
    did not trace the allocation of the object.
    N)r   r:   )Úobjr<   r   r   r   Úget_object_tracebacká   s    rQ   c               @   s`   e Zd ZdZdZdd„ Zedd„ ƒZedd„ ƒZed	d
„ ƒZ	dd„ Z
dd„ Zdd„ Zdd„ ZdS )ÚTracez"
    Trace of a memory block.
    Ú_tracec             C   s
   || _ d S )N)rS   )r   r@   r   r   r   r   ö   s    zTrace.__init__c             C   s
   | j d S )Nr   )rS   )r   r   r   r   Údomainû   s    zTrace.domainc             C   s
   | j d S )Nr6   )rS   )r   r   r   r   r   ÿ   s    z
Trace.sizec             C   s   t | jd ƒS )Né   )r:   rS   )r   r   r   r   r     s    zTrace.tracebackc             C   s   | j |j kS )N)rS   )r   r   r   r   r   r     s    zTrace.__eq__c             C   s
   t | jƒS )N)r   rS   )r   r   r   r   r   
  s    zTrace.__hash__c             C   s   d| j t| jdƒf S )Nz%s: %sF)r   r   r   )r   r   r   r   r     s    zTrace.__str__c             C   s   d| j t| jdƒ| jf S )Nz'<Trace domain=%s size=%s, traceback=%r>F)rT   r   r   r   )r   r   r   r   r      s    zTrace.__repr__N)rS   )r"   r#   r$   r%   r&   r   r9   rT   r   r   r   r   r   r    r   r   r   r   rR   ð   s   rR   c               @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )Ú_Tracesc             C   s   t j| ƒ || _d S )N)r   r   Ú_traces)r   Útracesr   r   r   r     s    
z_Traces.__init__c             C   s
   t | jƒS )N)r=   rW   )r   r   r   r   r>     s    z_Traces.__len__c             C   s4   t |tƒr"tdd„ | j| D ƒƒS t| j| ƒS d S )Nc             s   s   | ]}t |ƒV  qd S )N)rR   )r?   r@   r   r   r   rA      s    z&_Traces.__getitem__.<locals>.<genexpr>)rB   rC   rD   rW   rR   )r   rE   r   r   r   rF     s    
z_Traces.__getitem__c             C   s   |j | jkS )N)rS   rW   )r   r@   r   r   r   rG   $  s    z_Traces.__contains__c             C   s   | j |j kS )N)rW   )r   r   r   r   r   r   '  s    z_Traces.__eq__c             C   s   dt | ƒ S )Nz<Traces len=%s>)r=   )r   r   r   r   r    *  s    z_Traces.__repr__N)	r"   r#   r$   r   r>   rF   rG   r   r    r   r   r   r   rV     s   rV   c             C   s&   t jj| ƒ} | jdƒr"| d d… } | S )Nz.pycr6   éÿÿÿÿ)ÚosÚpathÚnormcaseÚendswith)r5   r   r   r   Ú_normalize_filename.  s    
r^   c               @   s   e Zd Zdd„ Zdd„ ZdS )Ú
BaseFilterc             C   s
   || _ d S )N)Ú	inclusive)r   r`   r   r   r   r   6  s    zBaseFilter.__init__c             C   s   t ‚d S )N)ÚNotImplementedError)r   r@   r   r   r   Ú_match9  s    zBaseFilter._matchN)r"   r#   r$   r   rb   r   r   r   r   r_   5  s   r_   c                   sJ   e Zd Zd‡ fdd„	Zedd„ ƒZdd„ Zd	d
„ Zdd„ Zdd„ Z	‡  Z
S )ÚFilterNFc                s2   t ƒ j|ƒ || _t|ƒ| _|| _|| _|| _d S )N)Úsuperr   r`   r^   Ú_filename_patternr7   Ú
all_framesrT   )r   r`   Úfilename_patternr7   rf   rT   )Ú	__class__r   r   r   >  s    
zFilter.__init__c             C   s   | j S )N)re   )r   r   r   r   rg   G  s    zFilter.filename_patternc             C   s6   t |ƒ}tj|| jƒsdS | jd kr(dS || jkS d S )NFT)r^   Úfnmatchre   r7   )r   r5   r7   r   r   r   Ú_match_frame_implK  s    
zFilter._match_frame_implc             C   s   | j ||ƒ| j A S )N)rj   r`   )r   r5   r7   r   r   r   Ú_match_frameT  s    zFilter._match_framec                sH   ˆ j r,t‡ fdd„|D ƒƒr"ˆ jS ˆ j S n|d \}}ˆ j||ƒS d S )Nc             3   s   | ]\}}ˆ j ||ƒV  qd S )N)rj   )r?   r5   r7   )r   r   r   rA   Y  s   z*Filter._match_traceback.<locals>.<genexpr>r   )rf   Úanyr`   rk   )r   r   r5   r7   r   )r   r   Ú_match_tracebackW  s    

zFilter._match_tracebackc             C   sD   |\}}}| j |ƒ}| jd k	r@| jr2|o0|| jkS |p>|| jkS |S )N)rm   rT   r`   )r   r@   rT   r   r   Úresr   r   r   rb   b  s    


zFilter._match)NFN)r"   r#   r$   r   r9   rg   rj   rk   rm   rb   Ú__classcell__r   r   )rh   r   rc   =  s   	rc   c                   s0   e Zd Z‡ fdd„Zedd„ ƒZdd„ Z‡  ZS )ÚDomainFilterc                s   t ƒ j|ƒ || _d S )N)rd   r   Ú_domain)r   r`   rT   )rh   r   r   r   n  s    zDomainFilter.__init__c             C   s   | j S )N)rq   )r   r   r   r   rT   r  s    zDomainFilter.domainc             C   s   |\}}}|| j k| j A S )N)rT   r`   )r   r@   rT   r   r   r   r   r   rb   v  s    
zDomainFilter._match)r"   r#   r$   r   r9   rT   rb   ro   r   r   )rh   r   rp   m  s   rp   c               @   sX   e Zd ZdZdd„ Zdd„ Zedd„ ƒZdd	„ Zd
d„ Z	dd„ Z
ddd„Zddd„ZdS )ÚSnapshotzB
    Snapshot of traces of memory blocks allocated by Python.
    c             C   s   t |ƒ| _|| _d S )N)rV   rX   Útraceback_limit)r   rX   rs   r   r   r   r   €  s    
zSnapshot.__init__c             C   s*   t |dƒ�}tj| |tjƒ W dQ R X dS )z1
        Write the snapshot into a file.
        ÚwbN)ÚopenÚpickleÚdumpZHIGHEST_PROTOCOL)r   r5   Úfpr   r   r   rw   †  s    zSnapshot.dumpc          	   C   s    t | dƒ�}tj|ƒS Q R X dS )z.
        Load a snapshot from a file.
        ÚrbN)ru   rv   Úload)r5   rx   r   r   r   rz   �  s    zSnapshot.loadc                s@   |rt ‡ fdd„|D ƒƒsdS |r<t ‡ fdd„|D ƒƒr<dS dS )Nc             3   s   | ]}|j ˆ ƒV  qd S )N)rb   )r?   Útrace_filter)r@   r   r   rA   —  s   z)Snapshot._filter_trace.<locals>.<genexpr>Fc             3   s   | ]}|j ˆ ƒ V  qd S )N)rb   )r?   r{   )r@   r   r   rA   ›  s   T)rl   )r   Úinclude_filtersÚexclude_filtersr@   r   )r@   r   Ú_filter_trace•  s    

zSnapshot._filter_tracec                s†   t |tƒstdt|ƒj ƒ‚|rng ‰g ‰ x(|D ] }|jrDˆj|ƒ q.ˆ j|ƒ q.W ‡ ‡‡fdd„ˆjjD ƒ}nˆjjj	ƒ }t
|ˆjƒS )zð
        Create a new Snapshot instance with a filtered traces sequence, filters
        is a list of Filter or DomainFilter instances.  If filters is an empty
        list, return a new Snapshot instance with a copy of the traces.
        z)filters must be a list of filters, not %sc                s   g | ]}ˆj ˆˆ |ƒr|‘qS r   )r~   )r?   r@   )r}   r|   r   r   r   ú
<listcomp>±  s    z*Snapshot.filter_traces.<locals>.<listcomp>)rB   r   Ú	TypeErrorÚtyper"   r`   r,   rX   rW   Úcopyrr   rs   )r   Úfiltersr{   Z
new_tracesr   )r}   r|   r   r   Úfilter_traces   s    

zSnapshot.filter_tracesc             C   sè  |dkrt d|f ƒ‚|r.|d	kr.t d| ƒ‚i }i }|�sxÒ| jjD ]Æ}|\}}}y|| }	W nZ tk
rº   |dkr~|}
n(|dkr”|d d… }
n|d d dff}
t|
ƒ}	|	||< Y nX y(||	 }| j|7  _| jd7  _W qF tk
�r
   t|	|dƒ||	< Y qFX qFW nÒxÐ| jjD ]Ä}|\}}}x²|D ]ª}y|| }	W nF tk
�r†   |dk�rd|f}
n|d dff}
t|
ƒ}	|	||< Y nX y(||	 }| j|7  _| jd7  _W n& tk
�rÖ   t|	|dƒ||	< Y nX �q0W �qW |S )
Nr   r5   r7   zunknown key_type: %rz/cumulative mode cannot by used with key type %rr6   r   )r   r5   r7   )r7   r5   )Ú
ValueErrorrX   rW   ÚKeyErrorr:   r   r   r   )r   Úkey_typeÚ
cumulativeZstatsZ
tracebacksr@   rT   r   Ztrace_tracebackr   r<   r0   r4   r   r   r   Ú	_group_by¹  sX    



"zSnapshot._group_byFc             C   s,   | j ||ƒ}t|jƒ ƒ}|jdtjd� |S )zd
        Group statistics by key_type. Return a sorted list of Statistic
        instances.
        T)ÚreverseÚkey)r‰   ÚlistÚvaluesÚsortr   r!   )r   r‡   rˆ   Zgroupedr/   r   r   r   r/   ì  s    zSnapshot.statisticsc             C   s6   | j ||ƒ}|j ||ƒ}t||ƒ}|jdtjd� |S )z­
        Compute the differences with an old snapshot old_snapshot. Get
        statistics as a sorted list of StatisticDiff instances, grouped by
        group_by.
        T)rŠ   r‹   )r‰   r1   rŽ   r'   r!   )r   Zold_snapshotr‡   rˆ   r.   r-   r/   r   r   r   Ú
compare_toö  s
    
zSnapshot.compare_toN)F)F)r"   r#   r$   r%   r   rw   Ústaticmethodrz   r~   r„   r‰   r/   r�   r   r   r   r   rr   {  s   3

rr   c              C   s$   t ƒ stdƒ‚tƒ } tƒ }t| |ƒS )zI
    Take a snapshot of traces of memory blocks allocated by Python.
    zLthe tracemalloc module must be tracing memory allocations to take a snapshot)Z
is_tracingÚRuntimeErrorr   Zget_traceback_limitrr   )rX   rs   r   r   r   Útake_snapshot  s
    r’   )Úcollectionsr   r   Ú	functoolsr   ri   rI   Zos.pathrZ   rv   Z_tracemallocr   r   r   r   r'   r1   r2   r:   rQ   rR   rV   r^   r_   rc   rp   rr   r’   r   r   r   r   Ú<module>   s0   &0#5%0 	