a dF@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)BKiBMiBGiBTiBdrz%+.1f %sz%.1f %si(r z%+.0f %sz%.0f %si)abs)sizesignunitr#/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.  tracebackrcountcCs||_||_||_dSNr)selfrrrrrr__init__%szStatistic.__init__cCst|j|j|jfSr)hashrrrrrrr__hash__*szStatistic.__hash__cCs2t|tstS|j|jko0|j|jko0|j|jkSr) isinstancerNotImplementedrrrrotherrrr__eq__-s    zStatistic.__eq__cCsBd|jt|jd|jf}|jr>|j|j}|dt|d7}|S)Nz%s: size=%s, count=%iF , average=%s)rrrrrtextaveragerrr__str__4s  zStatistic.__str__cCsd|j|j|jfS)Nz)rrrrr__repr__>szStatistic.__repr__cCs|j|j|jfSr)rrrrrrr _sort_keyBszStatistic._sort_keyN __name__ __module__ __qualname____doc__ __slots__rrr#r(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"||_||_||_||_||_dSrr2)rrrr3rr4rrrrMs zStatisticDiff.__init__cCst|j|j|j|j|jfSr)rrrr3rr4rrrrrTszStatisticDiff.__hash__cCsJt|tstS|j|jkoH|j|jkoH|j|jkoH|j|jkoH|j|jkSr)rr1r rrr3rr4r!rrrr#Xs      zStatisticDiff.__eq__cCsPd|jt|jdt|jd|j|jf}|jrL|j|j}|dt|d7}|S)Nz %s: size=%s (%s), count=%i (%+i)FTr$)rrrr3rr4r%rrrr(as   zStatisticDiff.__str__cCsd|j|j|j|j|jfS)Nz9r2rrrrr)ms  zStatisticDiff.__repr__cCs t|j|jt|j|j|jfSr)rr3rr4rrrrrrr*rs  zStatisticDiff._sort_keyNr+rrrrr1Fs  r1cCsg}|D]d\}}||d}|durNt||j|j|j|j|j|j}nt||j|j|j|j}||q |D]*\}}t|d|j d|j }||qz|SNr)itemspopr1rrappend) old_group new_group statisticsrstatpreviousrrr_compare_grouped_statsxs"   r>c@s\eZdZdZdZddZeddZeddZd d Z d d Z d dZ ddZ ddZ dS)Framez Frame of a traceback. _framecCs ||_dSrr@rframerrrrszFrame.__init__cCs |jdSr5r@rrrrfilenameszFrame.filenamecCs |jdSNr@rrrrlinenosz Frame.linenocCst|tstS|j|jkSrrr?r rAr!rrrr#s z Frame.__eq__cCst|tstS|j|jkSrrHr!rrr__lt__s z Frame.__lt__cCs t|jSr)rrArrrrrszFrame.__hash__cCsd|j|jfS)Nz%s:%srDrGrrrrr(sz Frame.__str__cCsd|j|jfS)NzrJrrrrr)szFrame.__repr__N)r,r-r.r/r0rpropertyrDrGr#rIrr(r)rrrrr?s  r?c@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)rrtuplereversedrMrN)rframes total_nframerrrrs zTraceback.__init__cCs|jSr)rNrrrrrRszTraceback.total_nframecCs t|jSr)lenrMrrrr__len__szTraceback.__len__cCs4t|tr"tdd|j|DSt|j|SdS)Ncss|]}t|VqdSr)r?.0tracerrr z(Traceback.__getitem__..)rslicerOrMr?rindexrrr __getitem__s zTraceback.__getitem__cCs |j|jvSr)rArMrBrrr __contains__szTraceback.__contains__cCs t|jSr)rrMrrrrrszTraceback.__hash__cCst|tstS|j|jkSrrrLr rMr!rrrr#s zTraceback.__eq__cCst|tstS|j|jkSrr_r!rrrrIs zTraceback.__lt__cCs t|dSr5)strrrrrr(szTraceback.__str__cCs8dt|}|jdur"|d7}n|d|jd7}|S)Nz z total_nframe=)rOrNrR)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)rPr8rDrG linecachegetlinestrip)rlimitmost_recent_firstlines frame_slicerClinerrrformats  zTraceback.format)N)NF)r,r-r.r/r0rrKrRrTr]r^rr#rIr(r)rkrrrrrLs  rLcCs 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)rrL)objrQrrrget_object_tracebacksrmc@s`eZdZdZdZddZeddZeddZed d Z d d Z d dZ ddZ ddZ dS)Tracez" Trace of a memory block. _tracecCs ||_dSrrorrWrrrrszTrace.__init__cCs |jdSr5rorrrrdomainsz Trace.domaincCs |jdSrErorrrrrsz Trace.sizecCst|jddS)N)rLrprrrrr#szTrace.tracebackcCst|tstS|j|jkSr)rrnr rpr!rrrr#'s z Trace.__eq__cCs t|jSr)rrprrrrr,szTrace.__hash__cCsd|jt|jdfS)Nz%s: %sF)rrrrrrrr(/sz Trace.__str__cCsd|jt|jd|jfS)Nz'F)rrrrrrrrrr)2szTrace.__repr__N)r,r-r.r/r0rrKrrrrr#rr(r)rrrrrns   rnc@s<eZdZddZddZddZddZd d Zd d Zd S)_TracescCst|||_dSr)rr_traces)rtracesrrrr8s z_Traces.__init__cCs t|jSr)rSrurrrrrT=sz_Traces.__len__cCs4t|tr"tdd|j|DSt|j|SdS)Ncss|]}t|VqdSr)rnrUrrrrXBrYz&_Traces.__getitem__..)rrZrOrurnr[rrrr]@s z_Traces.__getitem__cCs |j|jvSr)rprurqrrrr^Fsz_Traces.__contains__cCst|tstS|j|jkSr)rrtr rur!rrrr#Is z_Traces.__eq__cCs dt|S)Nz)rSrrrrr)Nsz_Traces.__repr__N) r,r-r.rrTr]r^r#r)rrrrrt7s rtcCs&tj|}|dr"|dd}|S)Nz.pyc)ospathnormcaseendswith)rDrrr_normalize_filenameRs   r|c@seZdZddZddZdS) BaseFiltercCs ||_dSr) inclusive)rr~rrrrZszBaseFilter.__init__cCstdSr)NotImplementedErrorrqrrr_match]szBaseFilter._matchN)r,r-r.rrrrrrr}Ysr}csJeZdZdfdd ZeddZddZd d Zd d Zd dZ Z S)FilterNFcs2t|||_t||_||_||_||_dSr)superrr~r|_filename_patternrG all_framesrr)rr~filename_patternrGrrr __class__rrrbs   zFilter.__init__cCs|jSr)rrrrrrkszFilter.filename_patterncCs6t|}t||jsdS|jdur(dS||jkSdS)NFT)r|fnmatchrrGrrDrGrrr_match_frame_implos  zFilter._match_frame_implcCs||||j ASr)rr~rrrr _match_framexszFilter._match_framecsHjr,tfdd|Dr"jSj Sn|d\}}||SdS)Nc3s|]\}}||VqdSr)r)rVrDrGrrrrX}sz*Filter._match_traceback..r)ranyr~r)rrrDrGrrr_match_traceback{s   zFilter._match_tracebackcCsF|\}}}}||}|jdurB|jr4|o2||jkS|p@||jkS|Sr)rrrr~)rrWrrrrrRresrrrrs   z Filter._match)NFN) r,r-r.rrKrrrrr __classcell__rrrrras    rcs0eZdZfddZeddZddZZS) DomainFiltercst|||_dSr)rr_domain)rr~rrrrrrs zDomainFilter.__init__cCs|jSr)rrrrrrrszDomainFilter.domaincCs|\}}}}||jk|j ASr)rrr~)rrWrrrrrRrrrrs zDomainFilter._match)r,r-r.rrKrrrrrrrrrs  rc@sXeZdZdZddZddZeddZdd Zd d Z d d Z dddZ dddZ dS)SnapshotzB Snapshot of traces of memory blocks allocated by Python. cCst||_||_dSr)rtrvtraceback_limit)rrvrrrrrs zSnapshot.__init__cCs>t|d }t||tjWdn1s00YdS)z1 Write the snapshot into a file. wbN)openpickledumpHIGHEST_PROTOCOL)rrDfprrrrs z Snapshot.dumpcCs8t|d}t|WdS1s*0YdS)z. Load a snapshot from a file. rbN)rrload)rDrrrrrs z Snapshot.loadcs@|rtfdd|DsdS|r.Fc3s|]}| VqdSrrrrrrrXsT)r)rinclude_filtersexclude_filtersrWrrr _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)rrUrrrrr s z*Snapshot.filter_traces..) rr TypeErrortyper,r~r8rvrucopyrr)rfiltersr 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)rrDrGzunknown key_type: %r)rGrDz/cumulative mode cannot by used with key type %rrrGrFr) ValueErrorrvruKeyErrorrLrrr)rkey_type cumulativestats tracebacksrWrrrtrace_tracebackrRrrQr<rCrrr _group_bysZ         zSnapshot._group_byFcCs,|||}t|}|jdtjd|S)zd Group statistics by key_type. Return a sorted list of Statistic instances. Treversekey)rlistvaluessortrr*)rrrgroupedr;rrrr;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. Tr)rr>rr1r*)r old_snapshotrrr:r9r;rrr compare_tos    zSnapshot.compare_toN)F)F) r,r-r.r/rr staticmethodrrrrr;rrrrrrs  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 RuntimeErrorrget_traceback_limitr)rvrrrr take_snapshot's r)collections.abcrr functoolsrrrcos.pathrxr _tracemallocrrrrr1r>r?rLrmrnrtr|r}rrrrrrrrs2 (2&M'0