a DOgF@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)sizesignZunitr#/usr/lib64/python3.9/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 rrrr__init__%szStatistic.__init__cCst|j|j|jfSr)hashrr rrrrr__hash__*szStatistic.__hash__cCs2t|tstS|j|jko0|j|jko0|j|jkSr) isinstancerNotImplementedrr rrotherrrr__eq__-s    zStatistic.__eq__cCsBd|jt|jd|jf}|jr>|j|j}|dt|d7}|S)Nz%s: size=%s, count=%iF , average=%s)rrr rrtextZaveragerrr__str__4s  zStatistic.__str__cCsd|j|j|jfS)Nz)rrrrr__repr__>szStatistic.__repr__cCs|j|j|jfSr)r rrrrrr _sort_keyBszStatistic._sort_keyN __name__ __module__ __qualname____doc__ __slots__rrrr#r$r%rrrrrs 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/rrrrMs zStatisticDiff.__init__cCst|j|j|j|j|jfSr)rrr r.rr/rrrrrTszStatisticDiff.__hash__cCsJt|tstS|j|jkoH|j|jkoH|j|jkoH|j|jkoH|j|jkSr)rr,rrr r.rr/rrrrrXs      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/r!rrrr#as   zStatisticDiff.__str__cCsd|j|j|j|j|jfS)Nz9r-rrrrr$ms  zStatisticDiff.__repr__cCs t|j|jt|j|j|jfSr)r r.r r/rrrrrrr%rs  zStatisticDiff._sort_keyNr&rrrrr,Fs  r,cCsg}|D]d\}}||d}|durNt||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 statisticsrstatZpreviousrrr_compare_grouped_statsxs"   r8c@s\eZdZdZdZddZeddZeddZd d Z d d Z d dZ ddZ ddZ dS)Framez Frame of a traceback. _framecCs ||_dSrr:rframerrrrszFrame.__init__cCs |jdSr0r:rrrrfilenameszFrame.filenamecCs |jdSNr:rrrrlinenosz Frame.linenocCst|tstS|j|jkSrrr9rr;rrrrrs z Frame.__eq__cCst|tstS|j|jkSrrBrrrr__lt__s z Frame.__lt__cCs t|jSr)rr;rrrrrszFrame.__hash__cCsd|j|jfS)Nz%s:%sr>rArrrrr#sz Frame.__str__cCsd|j|jfS)NzrDrrrrr$szFrame.__repr__N)r'r(r)r*r+rpropertyr>rArrCrr#r$rrrrr9s  r9c@steZdZdZdZdddZeddZdd Zd d Z d d Z ddZ ddZ ddZ ddZddZdddZdS) Tracebackz` Sequence of Frame instances sorted from the oldest frame to the most recent frame. )_frames _total_nframeNcCs"t|tt||_||_dSr)rrtuplereversedrGrH)rframes total_nframerrrrs zTraceback.__init__cCs|jSr)rHrrrrrLszTraceback.total_nframecCs t|jSr)lenrGrrrr__len__szTraceback.__len__cCs4t|tr"tdd|j|DSt|j|SdS)Ncss|]}t|VqdSr)r9.0tracerrr z(Traceback.__getitem__..)rslicerIrGr9rindexrrr __getitem__s zTraceback.__getitem__cCs |j|jvSr)r;rGr<rrr __contains__szTraceback.__contains__cCs t|jSr)rrGrrrrrszTraceback.__hash__cCst|tstS|j|jkSrrrFrrGrrrrrs zTraceback.__eq__cCst|tstS|j|jkSrrYrrrrrCs zTraceback.__lt__cCs t|dSr0)strrrrrr#szTraceback.__str__cCs8dt|}|jdur"|d7}n|d|jd7}|S)Nz z total_nframe=)rIrHrL)rsrrrr$s   zTraceback.__repr__FcCsg}|dur2|dkr$|| d}q6|d|}n|}|rBt|}|D]@}|d|j|jft|j|j}|rF|d|qF|S)Nrz File "%s", line %sz %s)rJr3r>rA linecachegetlinestrip)rlimitZmost_recent_firstlinesZ frame_slicer=linerrrformats  zTraceback.format)N)NF)r'r(r)r*r+rrErLrNrWrXrrrCr#r$rcrrrrrFs  rFcCs t|}|durt|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)rrF)objrKrrrget_object_tracebacksrec@s`eZdZdZdZddZeddZeddZed d Z d d Z d dZ ddZ ddZ dS)Tracez" Trace of a memory block. _tracecCs ||_dSrrgrrQrrrrszTrace.__init__cCs |jdSr0rgrrrrdomainsz Trace.domaincCs |jdSr?rgrrrrr sz Trace.sizecCst|jddS)N)rFrhrrrrr#szTrace.tracebackcCst|tstS|j|jkSr)rrfrrhrrrrr's z Trace.__eq__cCs t|jSr)rrhrrrrr,szTrace.__hash__cCsd|jt|jdfS)Nz%s: %sF)rrr rrrrr#/sz Trace.__str__cCsd|jt|jd|jfS)Nz'F)rjrr rrrrrr$2szTrace.__repr__N)r'r(r)r*r+rrErjr rrrr#r$rrrrrfs   rfc@s<eZdZddZddZddZddZd d Zd d Zd S)_TracescCst|||_dSr)rr_traces)rtracesrrrr8s z_Traces.__init__cCs t|jSr)rMrmrrrrrN=sz_Traces.__len__cCs4t|tr"tdd|j|DSt|j|SdS)Ncss|]}t|VqdSr)rfrOrrrrRBrSz&_Traces.__getitem__..)rrTrIrmrfrUrrrrW@s z_Traces.__getitem__cCs |j|jvSr)rhrmrirrrrXFsz_Traces.__contains__cCst|tstS|j|jkSr)rrlrrmrrrrrIs z_Traces.__eq__cCs dt|S)Nz)rMrrrrr$Nsz_Traces.__repr__N) r'r(r)rrNrWrXrr$rrrrrl7s rlcCs&tj|}|dr"|dd}|S)Nz.pyc)ospathnormcaseendswith)r>rrr_normalize_filenameRs   rtc@seZdZddZddZdS) BaseFiltercCs ||_dSr) inclusive)rrvrrrrZszBaseFilter.__init__cCstdSr)NotImplementedErrorrirrr_match]szBaseFilter._matchN)r'r(r)rrxrrrrruYsrucsJeZdZdfdd ZeddZddZd d Zd d Zd dZ Z S)FilterNFcs2t|||_t||_||_||_||_dSr)superrrvrt_filename_patternrA all_framesrj)rrvfilename_patternrAr|rj __class__rrrbs   zFilter.__init__cCs|jSr)r{rrrrr}kszFilter.filename_patterncCs6t|}t||jsdS|jdur(dS||jkSdS)NFT)rtfnmatchr{rArr>rArrr_match_frame_implos  zFilter._match_frame_implcCs||||j ASr)rrvrrrr _match_framexszFilter._match_framecsHjr,tfdd|Dr"jSj Sn|d\}}||SdS)Nc3s|]\}}||VqdSr)r)rPr>rArrrrR}sz*Filter._match_traceback..r)r|anyrvr)rrr>rArrr_match_traceback{s   zFilter._match_tracebackcCsF|\}}}}||}|jdurB|jr4|o2||jkS|p@||jkS|Sr)rrjrv)rrQrjr rrLresrrrrxs   z Filter._match)NFN) r'r(r)rrEr}rrrrx __classcell__rrr~rryas    rycs0eZdZfddZeddZddZZS) DomainFiltercst|||_dSr)rzr_domain)rrvrjr~rrrs zDomainFilter.__init__cCs|jSr)rrrrrrjszDomainFilter.domaincCs|\}}}}||jk|j ASr)rjrv)rrQrjr rrLrrrrxs zDomainFilter._match)r'r(r)rrErjrxrrrr~rrs  rc@sXeZdZdZddZddZeddZdd Zd d Z d d Z dddZ dddZ dS)SnapshotzB Snapshot of traces of memory blocks allocated by Python. cCst||_||_dSr)rlrntraceback_limit)rrnrrrrrs zSnapshot.__init__cCs>t|d }t||tjWdn1s00YdS)z1 Write the snapshot into a file. wbN)openpickledumpHIGHEST_PROTOCOL)rr>fprrrrs z Snapshot.dumpcCs8t|d}t|WdS1s*0YdS)z. Load a snapshot from a file. rbN)rrload)r>rrrrrs z Snapshot.loadcs@|rtfdd|DsdS|r.Fc3s|]}| VqdSrrrrrrrRsT)r)rinclude_filtersexclude_filtersrQrrr _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)rrOrrrrr s z*Snapshot.filter_traces..) rr TypeErrortyper'rvr3rnrmcopyrr)rfiltersrZ new_tracesrrr filter_tracess    zSnapshot.filter_tracesc Cs|dvrtd|f|r.|dvr.td|i}i}|s |jjD]}|\}}}} z ||} WnXty|dkr||} n(|dkr|dd} n|dddff} t| } | ||<Yn0z(|| } | j|7_| jd7_WqDtyt| |d|| <YqD0qDn|jjD]}|\}}}} |D]} z || } WnDty||dkrZ| f} n| ddff} t| } | || <Yn0z(|| } | j|7_| jd7_Wn$tyt| |d|| <Yn0q(q|S) N)rr>rAzunknown key_type: %r)rAr>z/cumulative mode cannot by used with key type %rrrAr@r) ValueErrorrnrmKeyErrorrFr rr)rkey_type cumulativeZstatsZ tracebacksrQrjr Ztrace_tracebackrLrrKr7r=rrr _group_bysZ         zSnapshot._group_byFcCs,|||}t|}|jdtjd|S)zd Group statistics by key_type. Return a sorted list of Statistic instances. Treversekey)rlistvaluessortrr%)rrrZgroupedr6rrrr6s  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)rr8rr,r%)rZ old_snapshotrrr5r4r6rrr compare_tos    zSnapshot.compare_toN)F)F) r'r(r)r*rr staticmethodrrrrr6rrrrrrs  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)rnrrrr take_snapshot's r)collections.abcrr functoolsrrr]Zos.pathrprZ _tracemallocrrrrr,r8r9rFrerfrlrtruryrrrrrrrs2 (2&M'0