3 \A�@sddlmZmZddlmZddlZddlZddlZddl Z ddl Tddl m Z m Z dd�Z Gdd �d �ZGd d �d �Zd d �ZeGdd�d��ZeGdd�de��Zdd�ZGdd�d�ZGdd�de�Zdd�ZGdd�d�ZGdd�de�ZGdd�de�ZGd d!�d!�Zd"d#�ZdS)$�)�Sequence�Iterable)�total_orderingN)�*)�_get_object_traceback� _get_tracescCszxtd D]l}t|�dkr:|dkr:|r.d||fSd||fSt|�dksN|dkrj|r^d ||fSd ||fS|d }qWdS)N�B�KiB�MiB�GiB�TiB�dz%+.1f %sz%.1f %s� iz%+.0f %sz%.0f %s)rr r r r i()�abs)�sizeZsignZunit�r�#/usr/lib64/python3.6/tracemalloc.py� _format_size s     rc@sDeZdZdZdZdd�Zdd�Zd d �Zd d �Zd d�Z dd�Z dS)� StatisticzS Statistic difference on memory allocations between two Snapshot instance. � tracebackr�countcCs||_||_||_dS)N)rrr)�selfrrrrrr�__init__%szStatistic.__init__cCst|j|j|jf�S)N)�hashrrr)rrrr�__hash__*szStatistic.__hash__cCs$|j|jko"|j|jko"|j|jkS)N)rrr)r�otherrrr�__eq__-s  zStatistic.__eq__cCsBd|jt|jd�|jf}|jr>|j|j}|dt|d�7}|S)Nz%s: size=%s, count=%iFz , average=%s)rrrr)r�text�averagerrr�__str__2s   zStatistic.__str__cCsd|j|j|jfS)Nz))rrr)rrrr�__repr__<szStatistic.__repr__cCs|j|j|jfS)N)rrr)rrrr� _sort_key@szStatistic._sort_keyN)rrr) �__name__� __module__� __qualname__�__doc__� __slots__rrrrr r!rrrrrs rc@sDeZdZdZdZdd�Zd d �Zd d �Zd d�Zdd�Z dd�Z dS)� StatisticDiffzd Statistic difference on memory allocations between an old and a new Snapshot instance. rr� size_diffr� count_diffcCs"||_||_||_||_||_dS)N)rrr(rr))rrrr(rr)rrrrKs zStatisticDiff.__init__cCst|j|j|j|j|jf�S)N)rrrr(rr))rrrrrRszStatisticDiff.__hash__cCs<|j|jko:|j|jko:|j|jko:|j|jko:|j|jkS)N)rrr(rr))rrrrrrVs     zStatisticDiff.__eq__cCsPd|jt|jd�t|jd�|j|jf}|jrL|j|j}|dt|d�7}|S)Nz %s: size=%s (%s), count=%i (%+i)FTz , average=%s)rrrr(rr))rrrrrrr]s    zStatisticDiff.__str__cCsd|j|j|j|j|jfS)Nz9)rrr(rr))rrrrr is zStatisticDiff.__repr__cCs t|j�|jt|j�|j|jfS)N)rr(rr)rr)rrrrr!ns  zStatisticDiff._sort_keyN)rrr(rr)) r"r#r$r%r&rrrrr r!rrrrr'Ds r'cCs�g}xp|j�D]d\}}|j|d�}|dk rPt||j|j|j|j|j|j�}nt||j|j|j|j�}|j|�qWx6|j�D]*\}}t|d|j d|j �}|j|�q�W|S)Nr)�items�popr'rr�append)� old_group� new_group� statisticsr�statZpreviousrrr�_compare_grouped_statsts  r1c@s\eZdZdZdZdd�Zedd��Zedd��Zd d �Z d d �Z d d�Z dd�Z dd�Z dS)�Framez Frame of a traceback. �_framecCs ||_dS)N)r3)r�framerrrr�szFrame.__init__cCs |jdS)Nr)r3)rrrr�filename�szFrame.filenamecCs |jdS)N�)r3)rrrr�lineno�sz Frame.linenocCs |j|jkS)N)r3)rrrrrr�sz Frame.__eq__cCs |j|jkS)N)r3)rrrrr�__lt__�sz Frame.__lt__cCs t|j�S)N)rr3)rrrrr�szFrame.__hash__cCsd|j|jfS)Nz%s:%s)r5r7)rrrrr�sz Frame.__str__cCsd|j|jfS)Nz)r5r7)rrrrr �szFrame.__repr__N)r3)r"r#r$r%r&r�propertyr5r7rr8rrr rrrrr2�s  r2c@sfeZdZdZdZdd�Zdd�Zdd�Zd d �Zd d �Z d d�Z dd�Z dd�Z dd�Z ddd�ZdS)� Tracebackz` Sequence of Frame instances sorted from the most recent frame to the oldest frame. �_framescCstj|�||_dS)N)rrr;)r�framesrrrr�s zTraceback.__init__cCs t|j�S)N)�lenr;)rrrr�__len__�szTraceback.__len__cCs4t|t�r"tdd�|j|D��St|j|�SdS)Ncss|]}t|�VqdS)N)r2)�.0�tracerrr� �sz(Traceback.__getitem__..)� isinstance�slice�tupler;r2)r�indexrrr� __getitem__�s zTraceback.__getitem__cCs |j|jkS)N)r3r;)rr4rrr� __contains__�szTraceback.__contains__cCs t|j�S)N)rr;)rrrrr�szTraceback.__hash__cCs |j|jkS)N)r;)rrrrrr�szTraceback.__eq__cCs |j|jkS)N)r;)rrrrrr8�szTraceback.__lt__cCs t|d�S)Nr)�str)rrrrr�szTraceback.__str__cCsdt|�fS)Nz)rD)rrrrr �szTraceback.__repr__NcCsng}|dk r|dkr|SxP|d|�D]@}|jd|j|jf�tj|j|j�j�}|r&|jd|�q&W|S)Nrz File "%s", line %sz %s)r,r5r7� linecache�getline�strip)r�limit�linesr4�linerrr�format�szTraceback.format)r;)N)r"r#r$r%r&rr>rFrGrrr8rr rOrrrrr:�sr:cCs t|�}|dk rt|�SdSdS)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)rr:)�objr<rrr�get_object_traceback�srQc@s`eZdZdZdZdd�Zedd��Zedd��Zed d ��Z d d �Z d d�Z dd�Z dd�Z dS)�Tracez" Trace of a memory block. �_tracecCs ||_dS)N)rS)rr@rrrr�szTrace.__init__cCs |jdS)Nr)rS)rrrr�domain�sz Trace.domaincCs |jdS)Nr6)rS)rrrrr�sz Trace.sizecCst|jd�S)N�)r:rS)rrrrrszTrace.tracebackcCs |j|jkS)N)rS)rrrrrrsz Trace.__eq__cCs t|j�S)N)rrS)rrrrr szTrace.__hash__cCsd|jt|jd�fS)Nz%s: %sF)rrr)rrrrr sz Trace.__str__cCsd|jt|jd�|jfS)Nz'F)rTrrr)rrrrr szTrace.__repr__N)rS)r"r#r$r%r&rr9rTrrrrrr rrrrrR�s   rRc@s<eZdZdd�Zdd�Zdd�Zdd�Zd d �Zd d �Zd S)�_TracescCstj|�||_dS)N)rr�_traces)r�tracesrrrrs z_Traces.__init__cCs t|j�S)N)r=rW)rrrrr>sz_Traces.__len__cCs4t|t�r"tdd�|j|D��St|j|�SdS)Ncss|]}t|�VqdS)N)rR)r?r@rrrrA sz&_Traces.__getitem__..)rBrCrDrWrR)rrErrrrFs z_Traces.__getitem__cCs |j|jkS)N)rSrW)rr@rrrrG$sz_Traces.__contains__cCs |j|jkS)N)rW)rrrrrr'sz_Traces.__eq__cCs dt|�S)Nz)r=)rrrrr *sz_Traces.__repr__N) r"r#r$rr>rFrGrr rrrrrVs rVcCs&tjj|�}|jd�r"|dd�}|S)Nz.pycr6�����)�os�path�normcase�endswith)r5rrr�_normalize_filename.s   r^c@seZdZdd�Zdd�ZdS)� BaseFiltercCs ||_dS)N)� inclusive)rr`rrrr6szBaseFilter.__init__cCst�dS)N)�NotImplementedError)rr@rrr�_match9szBaseFilter._matchN)r"r#r$rrbrrrrr_5sr_csJeZdZd�fdd� Zedd��Zdd�Zd d �Zd d �Zd d�Z �Z S)�FilterNFcs2t�j|�||_t|�|_||_||_||_dS)N)�superrr`r^�_filename_patternr7� all_framesrT)rr`�filename_patternr7rfrT)� __class__rrr>s   zFilter.__init__cCs|jS)N)re)rrrrrgGszFilter.filename_patterncCs6t|�}tj||j�sdS|jdkr(dS||jkSdS)NFT)r^�fnmatchrer7)rr5r7rrr�_match_frame_implKs  zFilter._match_frame_implcCs|j||�|j AS)N)rjr`)rr5r7rrr� _match_frameTszFilter._match_framecsH�jr,t�fdd�|D��r"�jS�j Sn|d\}}�j||�SdS)Nc3s|]\}}�j||�VqdS)N)rj)r?r5r7)rrrrAYsz*Filter._match_traceback..r)rf�anyr`rk)rrr5r7r)rr�_match_tracebackWs    zFilter._match_tracebackcCsD|\}}}|j|�}|jdk r@|jr2|o0||jkS|p>||jkS|S)N)rmrTr`)rr@rTrr�resrrrrbbs   z Filter._match)NFN) r"r#r$rr9rgrjrkrmrb� __classcell__rr)rhrrc=s    rccs0eZdZ�fdd�Zedd��Zdd�Z�ZS)� DomainFiltercst�j|�||_dS)N)rdr�_domain)rr`rT)rhrrrns zDomainFilter.__init__cCs|jS)N)rq)rrrrrTrszDomainFilter.domaincCs|\}}}||jk|j AS)N)rTr`)rr@rTrrrrrrbvs zDomainFilter._match)r"r#r$rr9rTrbrorr)rhrrpms  rpc@sXeZdZdZdd�Zdd�Zedd��Zdd �Zd d �Z d d �Z ddd�Z ddd�Z dS)�SnapshotzB Snapshot of traces of memory blocks allocated by Python. cCst|�|_||_dS)N)rVrX�traceback_limit)rrXrsrrrr�s zSnapshot.__init__c Cs*t|d��}tj||tj�WdQRXdS)z1 Write the snapshot into a file. �wbN)�open�pickle�dumpZHIGHEST_PROTOCOL)rr5�fprrrrw�s z Snapshot.dumpc Cs t|d�� }tj|�SQRXdS)z. Load a snapshot from a file. �rbN)rurv�load)r5rxrrrrz�s z Snapshot.loadcs@|rt�fdd�|D��sdS|r.Fc3s|]}|j�� VqdS)N)rb)r?r{)r@rrrA�sT)rl)r�include_filters�exclude_filtersr@r)r@r� _filter_trace�s    zSnapshot._filter_tracecs�t|t�stdt|�j��|rng�g�x(|D] }|jrD�j|�q.�j|�q.W���fdd��jjD�}n �jjj �}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 %scsg|]}�j��|�r|�qSr)r~)r?r@)r}r|rrr� �sz*Snapshot.filter_traces..) rBr� TypeError�typer"r`r,rXrW�copyrrrs)r�filtersr{Z new_tracesr)r}r|rr� filter_traces�s    zSnapshot.filter_tracesc Cs�|dkrtd|f��|r.|d kr.td|��i}i}|�sx�|jjD]�}|\}}}y ||} WnZtk r�|dkr~|} n(|dkr�|dd�} n|dddff} t| �} | ||<YnXy(|| } | j|7_| jd7_WqFtk �r t| |d�|| <YqFXqFWn�x�|jjD]�}|\}}}x�|D]�} y || } WnFtk �r�|dk�rd| f} n| ddff} t| �} | || <YnXy(|| } | j|7_| jd7_Wn&tk �r�t| |d�|| <YnX�q0W�qW|S) Nrr5r7zunknown key_type: %rz/cumulative mode cannot by used with key type %rr6r)rr5r7)r7r5)� ValueErrorrXrW�KeyErrorr:rrr) r�key_type� cumulativeZstatsZ tracebacksr@rTrZtrace_tracebackrr<r0r4rrr� _group_by�sX       "zSnapshot._group_byFcCs,|j||�}t|j��}|jdtjd�|S)zd Group statistics by key_type. Return a sorted list of Statistic instances. T)�reverse�key)r��list�values�sortrr!)rr�r�Zgroupedr/rrrr/�s  zSnapshot.statisticscCs6|j||�}|j||�}t||�}|jdtjd�|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�r1r�r'r!)rZ old_snapshotr�r�r.r-r/rrr� compare_to�s    zSnapshot.compare_toN)F)F) r"r#r$r%rrw� staticmethodrzr~r�r�r/r�rrrrrr{s  3 rrcCs$t�std��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� RuntimeErrorrZget_traceback_limitrr)rXrsrrr� take_snapshots r�)� collectionsrr� functoolsrrirIZos.pathrZrvZ _tracemallocrrrrr'r1r2r:rQrRrVr^r_rcrprrr�rrrr�s0 &0# 5%0