3 \A@sddlmZmZddlmZddlZddlZddlZddl Z ddl Tddl m Z m Z ddZ Gdd d ZGd d d Zd d ZeGdddZeGdddeZddZGdddZGdddeZddZGdddZGdddeZGdddeZGd d!d!Zd"d#ZdS)$)SequenceIterable)total_orderingN)*)_get_object_traceback _get_tracescCszxtd D]l}t|dkr:|dkr:|r.d||fSd||fSt|dksN|dkrj|r^d ||fSd ||fS|d }qWdS)NBKiBMiBGiBTiBdz%+.1f %sz%.1f %s iz%+.0f %sz%.0f %s)rr r r r i()abs)sizeZsignZunitr#/usr/lib64/python3.6/tracemalloc.py _format_size s     rc@sDeZdZdZdZddZddZd d Zd d Zd dZ ddZ dS) StatisticzS Statistic difference on memory allocations between two Snapshot instance. tracebackrcountcCs||_||_||_dS)N)rrr)selfrrrrrr__init__%szStatistic.__init__cCst|j|j|jfS)N)hashrrr)rrrr__hash__*szStatistic.__hash__cCs$|j|jko"|j|jko"|j|jkS)N)rrr)rotherrrr__eq__-s  zStatistic.__eq__cCsBd|jt|jd|jf}|jr>|j|j}|dt|d7}|S)Nz%s: size=%s, count=%iFz , average=%s)rrrr)rtextaveragerrr__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@sDeZdZdZdZddZd d Zd d Zd dZddZ ddZ 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|jfS)N)rrrr(rr))rrrrrRszStatisticDiff.__hash__cCs<|j|jko:|j|jko:|j|jko:|j|jko:|j|jkS)N)rrr(rr))rrrrrrVs     zStatisticDiff.__eq__cCsPd|jt|jdt|jd|j|jf}|jrL|j|j}|dt|d7}|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'cCsg}xp|jD]d\}}|j|d}|dk rPt||j|j|j|j|j|j}nt||j|j|j|j}|j|qWx6|jD]*\}}t|d|j d|j }|j|qW|S)Nr)itemspopr'rrappend) old_group new_group statisticsrstatZpreviousrrr_compare_grouped_statsts  r1c@s\eZdZdZdZddZeddZeddZd d Z d d Z d dZ ddZ ddZ dS)Framez Frame of a traceback. _framecCs ||_dS)N)r3)rframerrrrszFrame.__init__cCs |jdS)Nr)r3)rrrrfilenameszFrame.filenamecCs |jdS)N)r3)rrrrlinenosz Frame.linenocCs |j|jkS)N)r3)rrrrrrsz Frame.__eq__cCs |j|jkS)N)r3)rrrrr__lt__sz Frame.__lt__cCs t|jS)N)rr3)rrrrrszFrame.__hash__cCsd|j|jfS)Nz%s:%s)r5r7)rrrrrsz Frame.__str__cCsd|j|jfS)Nz)r5r7)rrrrr szFrame.__repr__N)r3)r"r#r$r%r&rpropertyr5r7rr8rrr rrrrr2s  r2c@sfeZdZdZdZddZddZddZd d Zd d Z d dZ ddZ ddZ ddZ dddZdS) Tracebackz` Sequence of Frame instances sorted from the most recent frame to the oldest frame. _framescCstj|||_dS)N)rrr;)rframesrrrrs zTraceback.__init__cCs t|jS)N)lenr;)rrrr__len__szTraceback.__len__cCs4t|tr"tdd|j|DSt|j|SdS)Ncss|]}t|VqdS)N)r2).0tracerrr sz(Traceback.__getitem__..) isinstanceslicetupler;r2)rindexrrr __getitem__s zTraceback.__getitem__cCs |j|jkS)N)r3r;)rr4rrr __contains__szTraceback.__contains__cCs t|jS)N)rr;)rrrrrszTraceback.__hash__cCs |j|jkS)N)r;)rrrrrrszTraceback.__eq__cCs |j|jkS)N)r;)rrrrrr8szTraceback.__lt__cCs t|dS)Nr)str)rrrrrszTraceback.__str__cCsdt|fS)Nz)rD)rrrrr szTraceback.__repr__NcCsng}|dk r|dkr|SxP|d|D]@}|jd|j|jftj|j|jj}|r&|jd|q&W|S)Nrz File "%s", line %sz %s)r,r5r7 linecachegetlinestrip)rlimitlinesr4linerrrformatszTraceback.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<rrrget_object_tracebacksrQc@s`eZdZdZdZddZeddZeddZed d Z d d Z d dZ ddZ ddZ dS)Tracez" Trace of a memory block. _tracecCs ||_dS)N)rS)rr@rrrrszTrace.__init__cCs |jdS)Nr)rS)rrrrdomainsz Trace.domaincCs |jdS)Nr6)rS)rrrrrsz Trace.sizecCst|jdS)N)r:rS)rrrrrszTrace.tracebackcCs |j|jkS)N)rS)rrrrrrsz Trace.__eq__cCs t|jS)N)rrS)rrrrr szTrace.__hash__cCsd|jt|jdfS)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 rrrrrRs   rRc@s<eZdZddZddZddZddZd d Zd d Zd S)_TracescCstj|||_dS)N)rr_traces)rtracesrrrrs z_Traces.__init__cCs t|jS)N)r=rW)rrrrr>sz_Traces.__len__cCs4t|tr"tdd|j|DSt|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|}|jdr"|dd}|S)Nz.pycr6)ospathnormcaseendswith)r5rrr_normalize_filename.s   r^c@seZdZddZddZdS) BaseFiltercCs ||_dS)N) inclusive)rr`rrrr6szBaseFilter.__init__cCstdS)N)NotImplementedError)rr@rrr_match9szBaseFilter._matchN)r"r#r$rrbrrrrr_5sr_csJeZdZdfdd ZeddZddZd d Zd d Zd dZ Z S)FilterNFcs2tj|||_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||jsdS|jdkr(dS||jkSdS)NFT)r^fnmatchrer7)rr5r7rrr_match_frame_implKs  zFilter._match_frame_implcCs|j|||j AS)N)rjr`)rr5r7rrr _match_frameTszFilter._match_framecsHjr,tfdd|Dr"jSj Sn|d\}}j||SdS)Nc3s|]\}}j||VqdS)N)rj)r?r5r7)rrrrAYsz*Filter._match_traceback..r)rfanyr`rk)rrr5r7r)rr_match_tracebackWs    zFilter._match_tracebackcCsD|\}}}|j|}|jdk r@|jr2|o0||jkS|p>||jkS|S)N)rmrTr`)rr@rTrrresrrrrbbs   z Filter._match)NFN) r"r#r$rr9rgrjrkrmrb __classcell__rr)rhrrc=s    rccs0eZdZfddZeddZddZZS) DomainFiltercstj|||_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@sXeZdZdZddZddZeddZdd Zd d Z d d Z dddZ dddZ dS)SnapshotzB Snapshot of traces of memory blocks allocated by Python. cCst||_||_dS)N)rVrXtraceback_limit)rrXrsrrrrs zSnapshot.__init__c Cs*t|d}tj||tjWdQRXdS)z1 Write the snapshot into a file. wbN)openpickledumpZHIGHEST_PROTOCOL)rr5fprrrrws z Snapshot.dumpc Cs t|d }tj|SQRXdS)z. Load a snapshot from a file. rbN)rurvload)r5rxrrrrzs z Snapshot.loadcs@|rtfdd|DsdS|r.Fc3s|]}|j VqdS)N)rb)r?r{)r@rrrAsT)rl)rinclude_filtersexclude_filtersr@r)r@r _filter_traces    zSnapshot._filter_tracecst|tstdt|j|rnggx(|D] }|jrDj|q.j|q.WfddjjD}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 TypeErrortyper"r`r,rXrWcopyrrrs)rfiltersr{Z new_tracesr)r}r|rr filter_tracess    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|| <YqFXqFWnx|jjD]}|\}}}x|D]} y || } WnFtk r|dkrd| f} n| ddff} t| } | || <YnXy(|| } | j|7_| jd7_Wn&tk rt| |d|| <YnXq0WqW|S) Nrr5r7zunknown key_type: %rz/cumulative mode cannot by used with key type %rr6r)rr5r7)r7r5) ValueErrorrXrWKeyErrorr:rrr) rkey_type cumulativeZstatsZ tracebacksr@rTrZtrace_tracebackrr<r0r4rrr _group_bysX       "zSnapshot._group_byFcCs,|j||}t|j}|jdtjd|S)zd Group statistics by key_type. Return a sorted list of Statistic instances. T)reversekey)rlistvaluessortrr!)rrrZgroupedr/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)rr)rr1rr'r!)rZ old_snapshotrrr.r-r/rrr compare_tos    zSnapshot.compare_toN)F)F) r"r#r$r%rrw staticmethodrzr~rrr/rrrrrrr{s  3 rrcCs$tstdt}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_rcrprrrrrrrs0 &0# 5%0