U ifB@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_tracescCsdD]|}t|dkr@|dkr@|r0d||fSd||fSt|dksT|dkrx|rhd||fSd ||fS|d }qdS) N)BZKiBZMiBZGiBTiBdrz%+.1f %sz%.1f %si(r z%+.0f %sz%.0f %si)abs)sizeZsignZunitr 0/opt/alt/python38/lib64/python3.8/tracemalloc.py _format_size src@sDeZdZdZdZddZddZddZd d Zd d Z d dZ dS) StatisticzS Statistic difference on memory allocations between two Snapshot instance.  tracebackr countcCs||_||_||_dSNr)selfrr rr r r__init__%szStatistic.__init__cCst|j|j|jfSr)hashrr rrr r r__hash__*szStatistic.__hash__cCs$|j|jko"|j|jko"|j|jkSrrrotherr r r__eq__-s    zStatistic.__eq__cCsBd|jt|jd|jf}|jr>|j|j}|dt|d7}|S)Nz%s: size=%s, count=%iF , average=%s)rrr rrtextZaverager r r__str__2s  zStatistic.__str__cCsd|j|j|jfS)Nz)rrr r r__repr__<szStatistic.__repr__cCs|j|j|jfSr)r rrrr r r _sort_key@szStatistic._sort_keyN __name__ __module__ __qualname____doc__ __slots__rrrr r!r"r 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"||_||_||_||_||_dSrr*)rrr r+rr,r r rrKs zStatisticDiff.__init__cCst|j|j|j|j|jfSr)rrr r+rr,rr r rrRszStatisticDiff.__hash__cCs<|j|jko:|j|jko:|j|jko:|j|jko:|j|jkSrr*rr r rrVs     zStatisticDiff.__eq__cCsPd|jt|jdt|jd|j|jf}|jrL|j|j}|dt|d7}|S)Nz %s: size=%s (%s), count=%i (%+i)FTr)rrr r+rr,rr r rr ]s   zStatisticDiff.__str__cCsd|j|j|j|j|jfS)Nz9r*rr r rr!is  zStatisticDiff.__repr__cCs t|j|jt|j|j|jfSr)r r+r r,rrrr r rr"ns  zStatisticDiff._sort_keyNr#r r r rr)Ds r)cCsg}|D]d\}}||d}|dk rNt||j|j|j|j|j|j}nt||j|j|j|j}||q |D]*\}}t|d|j d|j }||qz|SNr)itemspopr)r rappend) old_group new_group statisticsrstatZpreviousr r r_compare_grouped_statsts*     r5c@s\eZdZdZdZddZeddZeddZd d Z d d Z d dZ ddZ ddZ dS)Framez Frame of a traceback. _framecCs ||_dSrr7rframer r rrszFrame.__init__cCs |jdSr-r7rr r rfilenameszFrame.filenamecCs |jdSNr7rr r rlinenosz Frame.linenocCs |j|jkSrr7rr r rrsz Frame.__eq__cCs |j|jkSrr7rr r r__lt__sz Frame.__lt__cCs t|jSr)rr8rr r rrszFrame.__hash__cCsd|j|jfS)Nz%s:%sr;r>rr r rr sz Frame.__str__cCsd|j|jfS)Nzr@rr r rr!szFrame.__repr__N)r$r%r&r'r(rpropertyr;r>rr?rr r!r r r rr6s  r6c@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||_dSr)rrtuplereversedrD)rframesr r rrs zTraceback.__init__cCs t|jSr)lenrDrr r r__len__szTraceback.__len__cCs4t|tr"tdd|j|DSt|j|SdS)Ncss|]}t|VqdSr)r6.0tracer r r sz(Traceback.__getitem__..) isinstanceslicerErDr6rindexr r r __getitem__s zTraceback.__getitem__cCs |j|jkSr)r8rDr9r r r __contains__szTraceback.__contains__cCs t|jSr)rrDrr r rrszTraceback.__hash__cCs |j|jkSrrCrr r rrszTraceback.__eq__cCs |j|jkSrrCrr r rr?szTraceback.__lt__cCs t|dSr-)strrr r rr szTraceback.__str__cCsdt|fS)Nz)rErr r rr!szTraceback.__repr__NFcCsg}|dk r2|dkr$|| d}q6|d|}n|}|rBt|}|D]@}|d|j|jft|j|j}|rF|d|qF|S)Nrz File "%s", line %sz %s)rFr0r;r> linecachegetlinestrip)rlimitZmost_recent_firstlinesZ frame_slicer:liner r rformats  zTraceback.format)NF)r$r%r&r'r(rrIrRrSrrr?r r!r[r r r rrBsrBcCs 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)rrB)objrGr r rget_object_tracebacksr]c@s`eZdZdZdZddZeddZeddZed d Z d d Z d dZ ddZ ddZ dS)Tracez" Trace of a memory block. _tracecCs ||_dSrr_rrLr r rrszTrace.__init__cCs |jdSr-r_rr r rdomainsz Trace.domaincCs |jdSr<r_rr r rr sz Trace.sizecCst|jdS)N)rBr`rr r rr szTrace.tracebackcCs |j|jkSrr_rr r rrsz Trace.__eq__cCs t|jSr)rr`rr r rrszTrace.__hash__cCsd|jt|jdfS)Nz%s: %sF)rrr rr r rr sz Trace.__str__cCsd|jt|jd|jfS)Nz'F)rbrr rrr r rr!szTrace.__repr__N)r$r%r&r'r(rrArbr rrrr r!r r r rr^s   r^c@s<eZdZddZddZddZddZd d Zd d Zd S)_TracescCst|||_dSr)rr_traces)rtracesr r rr s z_Traces.__init__cCs t|jSr)rHrerr r rrI%sz_Traces.__len__cCs4t|tr"tdd|j|DSt|j|SdS)Ncss|]}t|VqdSr)r^rJr r rrM*sz&_Traces.__getitem__..)rNrOrErer^rPr r rrR(s z_Traces.__getitem__cCs |j|jkSr)r`rerar r rrS.sz_Traces.__contains__cCs |j|jkSr)rerr r rr1sz_Traces.__eq__cCs dt|S)Nz)rHrr r rr!4sz_Traces.__repr__N) r$r%r&rrIrRrSrr!r r r rrds rdcCs&tj|}|dr"|dd}|S)Nz.pyc)ospathnormcaseendswith)r;r r r_normalize_filename8s   rlc@seZdZddZddZdS) BaseFiltercCs ||_dSr) inclusive)rrnr r rr@szBaseFilter.__init__cCstdSr)NotImplementedErrorrar r r_matchCszBaseFilter._matchN)r$r%r&rrpr r r rrm?srmcsJeZdZdfdd ZeddZddZd d Zd d Zd dZ Z S)FilterNFcs2t|||_t||_||_||_||_dSr)superrrnrl_filename_patternr> all_framesrb)rrnfilename_patternr>rtrb __class__r rrHs   zFilter.__init__cCs|jSr)rsrr r rruQszFilter.filename_patterncCs6t|}t||jsdS|jdkr(dS||jkSdS)NFT)rlfnmatchrsr>rr;r>r r r_match_frame_implUs  zFilter._match_frame_implcCs||||j ASr)rzrnryr r r _match_frame^szFilter._match_framecsHjr,tfdd|Dr"jSj Sn|d\}}||SdS)Nc3s|]\}}||VqdSr)rz)rKr;r>rr rrMcsz*Filter._match_traceback..r)rtanyrnr{)rrr;r>r rr_match_tracebackas   zFilter._match_tracebackcCsD|\}}}||}|jdk r@|jr2|o0||jkS|p>||jkS|Sr)r}rbrn)rrLrbr rresr r rrpls   z Filter._match)NFN) r$r%r&rrArurzr{r}rp __classcell__r r rvrrqGs    rqcs0eZdZfddZeddZddZZS) DomainFiltercst|||_dSr)rrr_domain)rrnrbrvr rrxs zDomainFilter.__init__cCs|jSr)rrr r rrb|szDomainFilter.domaincCs|\}}}||jk|j ASr)rbrn)rrLrbr rr r rrps zDomainFilter._match)r$r%r&rrArbrprr r rvrrws  rc@sXeZdZdZddZddZeddZdd Zd d Z d d Z dddZ dddZ dS)SnapshotzB Snapshot of traces of memory blocks allocated by Python. cCst||_||_dSr)rdrftraceback_limit)rrfrr r rrs zSnapshot.__init__c Cs*t|d}t||tjW5QRXdS)z1 Write the snapshot into a file. wbN)openpickledumpZHIGHEST_PROTOCOL)rr;fpr r rrs z Snapshot.dumpc Cs,t|d}t|W5QRSQRXdS)z. Load a snapshot from a file. rbN)rrload)r;rr r rrs z Snapshot.loadcs@|rtfdd|DsdS|r.Fc3s|]}| VqdSrrrrr rrMsT)r|)rinclude_filtersexclude_filtersrLr rr _filter_traces  zSnapshot._filter_tracecst|tstdt|j|rjgg|D] }|jrB|q,|q,fddjjD}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 )rrJrrrr r s z*Snapshot.filter_traces..) rNr TypeErrortyper$rnr0rfrecopyrr)rfiltersrZ new_tracesr rr filter_tracess    zSnapshot.filter_tracesc Cs|dkrtd|f|r.|dkr.td|i}i}|s|jjD]}|\}}}z ||} WnZtk r|dkr||} n(|dkr|dd} n|dddff} t| } | ||<YnXz(|| } | j|7_| jd7_WqDtk rt| |d|| <YqDXqDn|jjD]}|\}}}|D]} z || } WnFtk r~|dkr\| f} n| ddff} t| } | || <YnXz(|| } | j|7_| jd7_Wn&tk rt| |d|| <YnXq(q|S) N)rr;r>zunknown key_type: %r)r>r;z/cumulative mode cannot by used with key type %rrr>r=r) ValueErrorrfreKeyErrorrBr rr) rkey_type cumulativeZstatsZ tracebacksrLrbr Ztrace_tracebackrrGr4r:r r r _group_bysZ        zSnapshot._group_byFcCs,|||}t|}|jdtjd|S)zd Group statistics by key_type. Return a sorted list of Statistic instances. Treversekey)rlistvaluessortrr")rrrZgroupedr3r r rr3s  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. Tr)rr5rr)r")rZ old_snapshotrrr2r1r3r r r compare_tos    zSnapshot.compare_toN)F)F) r$r%r&r'rr staticmethodrrrrr3rr r r rrs  3 rcCs$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_limitr)rfrr r r take_snapshot s r)Zcollections.abcrr functoolsrrxrUZos.pathrhrZ _tracemallocrrrrr)r5r6rBr]r^rdrlrmrqrrrr r r rs2 &0"?%0