3
eb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TiBd   z%+.1f %sz%.1f %s
   i   z%+.0f %sz%.0f %s)r   r	   r
   r   r   i (  )abs)sizeZsignZunit r   !/usr/lib/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| qW |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   Z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)rp   )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   rn   r   r   )rh   r   ro   m  s   ro   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   rr   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   rv     s    zSnapshot.dumpc          	   C   s    t | d}tj|S Q R X dS )z.
        Load a snapshot from a file.
        rbN)rt   ru   load)r5   rw   r   r   r   ry     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?   rz   )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   copyrq   rr   )r   filtersrz   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 nx| 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   rv   staticmethodry   r}   r   r   r/   r   r   r   r   r   rq   {  s   3

rq   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_limitrq   )rX   rr   r   r   r   take_snapshot  s
    r   )collectionsr   r   	functoolsr   ri   rI   os.pathrZ   ru   Z_tracemallocr   r   r   r   r'   r1   r2   r:   rQ   rR   rV   r^   r_   rc   ro   rq   r   r   r   r   r   <module>   s0   &0#5%0 	