eaiovnaovbqoebvqoeavibavo B  fB@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_tracescCszxtdD]l}t|dkr:|dkr:|r.d||fSd||fSt|dksN|dkrj|r^d||fSd ||fS|d }qWdS) N)BZKiBZMiBZGiBTiBdrz%+.1f %sz%.1f %si(r z%+.0f %sz%.0f %si)abs)sizeZsignZunitr 0/opt/alt/python37/lib64/python3.7/tracemalloc.py _format_size s     rc@sDeZdZdZdZddZddZddZd d Zd d Z d dZ dS) StatisticzS Statistic difference on memory allocations between two Snapshot instance. ) tracebackr countcCs||_||_||_dS)N)rr r)selfrr rr r r__init__%szStatistic.__init__cCst|j|j|jfS)N)hashrr r)rr r r__hash__*szStatistic.__hash__cCs$|j|jko"|j|jko"|j|jkS)N)rr r)rotherr r r__eq__-s  zStatistic.__eq__cCsBd|jt|jd|jf}|jr>|j|j}|dt|d7}|S)Nz%s: size=%s, count=%iFz , average=%s)rrr r)rtextaverager r r__str__2s   zStatistic.__str__cCsd|j|j|jfS)Nz))rr r)rr r r__repr__<szStatistic.__repr__cCs|j|j|jfS)N)r rr)rr r r _sort_key@szStatistic._sort_keyN) __name__ __module__ __qualname____doc__ __slots__rrrrrrr r r rrs rc@sDeZdZdZdZddZddZddZd d Zd d Z d dZ dS) StatisticDiffzd Statistic difference on memory allocations between an old and a new Snapshot instance. )rr size_diffr count_diffcCs"||_||_||_||_||_dS)N)rr r$rr%)rrr r$rr%r r rrKs zStatisticDiff.__init__cCst|j|j|j|j|jfS)N)rrr r$rr%)rr r rrRszStatisticDiff.__hash__cCs<|j|jko:|j|jko:|j|jko:|j|jko:|j|jkS)N)rr r$rr%)rrr r rrVs     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)rrr r$rr%)rrrr r rr]s    zStatisticDiff.__str__cCsd|j|j|j|j|jfS)Nz9)rr r$rr%)rr r rris zStatisticDiff.__repr__cCs t|j|jt|j|j|jfS)N)r r$r r%rr)rr r rrns  zStatisticDiff._sort_keyN) rrr r!r"rrrrrrr r r rr#Ds r#cCsg}xp|D]d\}}||d}|dk rPt||j|j|j|j|j|j}nt||j|j|j|j}||qWx6|D]*\}}t|d|j d|j }||qW|S)Nr)itemspopr#r rappend) old_group new_group statisticsrstatZpreviousr r r_compare_grouped_statsts  r-c@s\eZdZdZdZddZeddZeddZd d Z d d Z d dZ ddZ ddZ dS)Framez Frame of a traceback. )_framecCs ||_dS)N)r/)rframer r rrszFrame.__init__cCs |jdS)Nr)r/)rr r rfilenameszFrame.filenamecCs |jdS)N)r/)rr r rlinenosz Frame.linenocCs |j|jkS)N)r/)rrr r rrsz Frame.__eq__cCs |j|jkS)N)r/)rrr r r__lt__sz Frame.__lt__cCs t|jS)N)rr/)rr r rrszFrame.__hash__cCsd|j|jfS)Nz%s:%s)r1r3)rr r rrsz Frame.__str__cCsd|j|jfS)Nz)r1r3)rr r rrszFrame.__repr__N)rrr r!r"rpropertyr1r3rr4rrrr r r rr.s  r.c@sfeZdZdZdZddZddZddZd d Zd d Z d dZ ddZ ddZ ddZ dddZdS) Tracebackz` Sequence of Frame instances sorted from the oldest frame to the most recent frame. )_framescCst|tt||_dS)N)rrtuplereversedr7)rframesr r rrs zTraceback.__init__cCs t|jS)N)lenr7)rr r r__len__szTraceback.__len__cCs4t|tr"tdd|j|DSt|j|SdS)Ncss|]}t|VqdS)N)r.).0tracer r r sz(Traceback.__getitem__..) isinstanceslicer8r7r.)rindexr r r __getitem__s zTraceback.__getitem__cCs |j|jkS)N)r/r7)rr0r r r __contains__szTraceback.__contains__cCs t|jS)N)rr7)rr r rrszTraceback.__hash__cCs |j|jkS)N)r7)rrr r rrszTraceback.__eq__cCs |j|jkS)N)r7)rrr r rr4szTraceback.__lt__cCs t|dS)Nr)str)rr r rrszTraceback.__str__cCsdt|fS)Nz)r8)rr r rrszTraceback.__repr__NFcCsg}|dk r2|dkr$|| d}q6|d|}n|}|rBt|}xH|D]@}|d|j|jft|j|j}|rH|d|qHW|S)Nrz File "%s", line %sz %s)r9r(r1r3 linecachegetlinestrip)rlimitZmost_recent_firstlinesZ frame_slicer0liner r rformats zTraceback.format)NF)rrr r!r"rr<rCrDrrr4rrrLr r r rr6sr6cCs 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)rr6)objr:r r rget_object_tracebacksrNc@s`eZdZdZdZddZeddZeddZed d Z d d Z d dZ ddZ ddZ dS)Tracez" Trace of a memory block. )_tracecCs ||_dS)N)rP)rr>r r rrszTrace.__init__cCs |jdS)Nr)rP)rr r rdomainsz Trace.domaincCs |jdS)Nr2)rP)rr r rr sz Trace.sizecCst|jdS)N)r6rP)rr r rr szTrace.tracebackcCs |j|jkS)N)rP)rrr r rrsz Trace.__eq__cCs t|jS)N)rrP)rr r rrszTrace.__hash__cCsd|jt|jdfS)Nz%s: %sF)rrr )rr r rrsz Trace.__str__cCsd|jt|jd|jfS)Nz'F)rQrr r)rr r rrszTrace.__repr__N)rrr r!r"rr5rQr rrrrrr r r rrOs   rOc@s<eZdZddZddZddZddZd d Zd d Zd S)_TracescCst|||_dS)N)rr_traces)rtracesr r rr s z_Traces.__init__cCs t|jS)N)r;rT)rr r rr<%sz_Traces.__len__cCs4t|tr"tdd|j|DSt|j|SdS)Ncss|]}t|VqdS)N)rO)r=r>r r rr?*sz&_Traces.__getitem__..)r@rAr8rTrO)rrBr r rrC(s z_Traces.__getitem__cCs |j|jkS)N)rPrT)rr>r r rrD.sz_Traces.__contains__cCs |j|jkS)N)rT)rrr r rr1sz_Traces.__eq__cCs dt|S)Nz)r;)rr r rr4sz_Traces.__repr__N) rrr rr<rCrDrrr r r rrSs rScCs&tj|}|dr"|dd}|S)Nz.pyc)ospathnormcaseendswith)r1r r r_normalize_filename8s   r[c@seZdZddZddZdS) BaseFiltercCs ||_dS)N) inclusive)rr]r r rr@szBaseFilter.__init__cCstdS)N)NotImplementedError)rr>r r r_matchCszBaseFilter._matchN)rrr rr_r r r rr\?sr\csJeZdZdfdd ZeddZddZd d Zd d Zd dZ Z S)FilterNFcs2t|||_t||_||_||_||_dS)N)superrr]r[_filename_patternr3 all_framesrQ)rr]filename_patternr3rcrQ) __class__r rrHs   zFilter.__init__cCs|jS)N)rb)rr r rrdQszFilter.filename_patterncCs6t|}t||jsdS|jdkr(dS||jkSdS)NFT)r[fnmatchrbr3)rr1r3r r r_match_frame_implUs  zFilter._match_frame_implcCs||||j AS)N)rgr])rr1r3r r r _match_frame^szFilter._match_framecsHjr,tfdd|Dr"jSj Sn|d\}}||SdS)Nc3s|]\}}||VqdS)N)rg)r=r1r3)rr rr?csz*Filter._match_traceback..r)rcanyr]rh)rrr1r3r )rr_match_tracebackas    zFilter._match_tracebackcCsD|\}}}||}|jdk r@|jr2|o0||jkS|p>||jkS|S)N)rjrQr])rr>rQr rZresr r rr_ls   z Filter._match)NFN) rrr rr5rdrgrhrjr_ __classcell__r r )rerr`Gs    r`cs0eZdZfddZeddZddZZS) DomainFiltercst|||_dS)N)rar_domain)rr]rQ)rer rrxs zDomainFilter.__init__cCs|jS)N)rm)rr r rrQ|szDomainFilter.domaincCs|\}}}||jk|j AS)N)rQr])rr>rQr rr r rr_s zDomainFilter._match)rrr rr5rQr_rkr r )rerrlws  rlc@sXeZdZdZddZddZeddZdd Zd d Z d d Z dddZ dddZ dS)SnapshotzB Snapshot of traces of memory blocks allocated by Python. cCst||_||_dS)N)rSrUtraceback_limit)rrUror r rrs zSnapshot.__init__c Cs*t|d}t||tjWdQRXdS)z1 Write the snapshot into a file. wbN)openpickledumpZHIGHEST_PROTOCOL)rr1fpr r rrss z Snapshot.dumpc Cs t|d }t|SQRXdS)z. Load a snapshot from a file. rbN)rqrrload)r1rtr r rrvs z Snapshot.loadcs@|rtfdd|DsdS|rr rr?sz)Snapshot._filter_trace..Fc3s|]}| VqdS)N)r_)r=rw)r>r rr?sT)ri)rinclude_filtersexclude_filtersr>r )r>r _filter_traces    zSnapshot._filter_tracecst|tstdt|j|rnggx(|D] }|jrD|q.|q.WfddjjD}n jj }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|]}|r|qSr )rz)r=r>)ryrxrr r sz*Snapshot.filter_traces..) r@r TypeErrortyperr]r(rUrTcopyrnro)rfiltersrwZ new_tracesr )ryrxrr filter_tracess    zSnapshot.filter_tracesc Cs|dkrtd|f|r.|dkr.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) N)rr1r3zunknown key_type: %r)r3r1z/cumulative mode cannot by used with key type %rrr3r2r) ValueErrorrUrTKeyErrorr6r rr) rkey_type cumulativeZstatsZ tracebacksr>rQr Ztrace_tracebackrr:r,r0r r r _group_bysX       "zSnapshot._group_byFcCs,|||}t|}|jdtjd|S)zd Group statistics by key_type. Return a sorted list of Statistic instances. T)reversekey)rlistvaluessortrr)rrrZgroupedr+r r rr+s  zSnapshot.statisticscCs6|||}|||}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)rr-rr#r)rZ old_snapshotrrr*r)r+r r r compare_tos    zSnapshot.compare_toN)F)F) rrr r!rrs staticmethodrvrzrrr+rr r r rrns  3 rncCs$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) is_tracing RuntimeErrorrZget_traceback_limitrn)rUror r r take_snapshot s r)Zcollections.abcrr functoolsrrfrFZos.pathrWrrZ _tracemallocrrrrr#r-r.r6rNrOrSr[r\r`rlrnrr r r rs0 &0# ?%0