U e5dB@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 #/usr/lib64/python3.8/tracemalloc.py _format_size src@s@eZdZdZddZddZddZdd Zd d Zd d Z dS) Statistic 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__ __slots__rrrr r!r"r r r rrs rc@s@eZdZdZddZddZddZdd Zd d Zd d Z dS) StatisticDiffrr 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*     r4c@sXeZdZdZddZeddZeddZdd Zd d Z d d Z ddZ ddZ dS)Frame_framecCs ||_dSrr6rframer r rrszFrame.__init__cCs |jdSr,r6rr r rfilenameszFrame.filenamecCs |jdSNr6rr r rlinenosz Frame.linenocCs |j|jkSrr6rr r rrsz Frame.__eq__cCs |j|jkSrr6rr r r__lt__sz Frame.__lt__cCs t|jSr)rr7rr 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'rpropertyr:r=rr>rr r!r r r rr5s  r5c@sbeZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ dddZ dS) Traceback_framescCst|tt||_dSr)rrtuplereversedrC)rframesr r rrs zTraceback.__init__cCs t|jSr)lenrCrr r r__len__szTraceback.__len__cCs4t|tr"tdd|j|DSt|j|SdS)Ncss|]}t|VqdSr)r5.0tracer r r sz(Traceback.__getitem__..) isinstanceslicerDrCr5rindexr r r __getitem__s zTraceback.__getitem__cCs |j|jkSr)r7rCr8r r r __contains__szTraceback.__contains__cCs t|jSr)rrCrr r rrszTraceback.__hash__cCs |j|jkSrrBrr r rrszTraceback.__eq__cCs |j|jkSrrBrr r rr>szTraceback.__lt__cCs t|dSr,)strrr r rr szTraceback.__str__cCsdt|fS)Nz)rDrr 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)rEr/r:r= linecachegetlinestrip)rlimitZmost_recent_firstlinesZ frame_slicer9liner r rformats  zTraceback.format)NF)r$r%r&r'rrHrQrRrrr>r r!rZr r r rrAsrAcCs t|}|dk rt|SdSdSr)rrA)objrFr r rget_object_tracebacksr\c@s\eZdZdZddZeddZeddZedd Zd d Z d d Z ddZ ddZ dS)Trace_tracecCs ||_dSrr^rrKr r rrszTrace.__init__cCs |jdSr,r^rr r rdomainsz Trace.domaincCs |jdSr;r^rr r rr sz Trace.sizecCst|jdS)N)rAr_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)rarr rrr r rr!szTrace.__repr__N) r$r%r&r'rr@rar 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)rGrdrr r rrH%sz_Traces.__len__cCs4t|tr"tdd|j|DSt|j|SdS)Ncss|]}t|VqdSr)r]rIr r rrL*sz&_Traces.__getitem__..)rMrNrDrdr]rOr r rrQ(s z_Traces.__getitem__cCs |j|jkSr)r_rdr`r r rrR.sz_Traces.__contains__cCs |j|jkSr)rdrr r rr1sz_Traces.__eq__cCs dt|S)Nz)rGrr r rr!4sz_Traces.__repr__N) r$r%r&rrHrQrRrr!r r r rrcs rccCs&tj|}|dr"|dd}|S)Nz.pyc)ospathnormcaseendswith)r:r r r_normalize_filename8s   rkc@seZdZddZddZdS) BaseFiltercCs ||_dSr) inclusive)rrmr r rr@szBaseFilter.__init__cCstdSr)NotImplementedErrorr`r r r_matchCszBaseFilter._matchN)r$r%r&rror r r rrl?srlcsJeZdZdfdd ZeddZddZd d Zd d Zd dZ Z S)FilterNFcs2t|||_t||_||_||_||_dSr)superrrmrk_filename_patternr= all_framesra)rrmfilename_patternr=rsra __class__r rrHs   zFilter.__init__cCs|jSr)rrrr r rrtQszFilter.filename_patterncCs6t|}t||jsdS|jdkr(dS||jkSdS)NFT)rkfnmatchrrr=rr:r=r r r_match_frame_implUs  zFilter._match_frame_implcCs||||j ASr)ryrmrxr r r _match_frame^szFilter._match_framecsHjr,tfdd|Dr"jSj Sn|d\}}||SdS)Nc3s|]\}}||VqdSr)ry)rJr:r=rr rrLcsz*Filter._match_traceback..r)rsanyrmrz)rrr:r=r rr_match_tracebackas   zFilter._match_tracebackcCsD|\}}}||}|jdk r@|jr2|o0||jkS|p>||jkS|Sr)r|rarm)rrKrar rresr r rrols   z Filter._match)NFN) r$r%r&rr@rtryrzr|ro __classcell__r r rurrpGs    rpcs0eZdZfddZeddZddZZS) DomainFiltercst|||_dSr)rqr_domain)rrmrarur rrxs zDomainFilter.__init__cCs|jSr)rrr r rra|szDomainFilter.domaincCs|\}}}||jk|j ASr)rarm)rrKrar rr r rros zDomainFilter._match)r$r%r&rr@raror~r r rurrws  rc@sTeZdZddZddZeddZddZd d Zd d Z dddZ dddZ dS)SnapshotcCst||_||_dSr)rcretraceback_limit)rrerr r rrs zSnapshot.__init__c Cs*t|d}t||tjW5QRXdS)Nwb)openpickledumpZHIGHEST_PROTOCOL)rr:fpr r rrs z Snapshot.dumpc Cs,t|d}t|W5QRSQRXdS)Nrb)rrload)r:rr r rrs z Snapshot.loadcs@|rtfdd|DsdS|r.Fc3s|]}| VqdSrrrrr rrLsT)r{)rinclude_filtersexclude_filtersrKr rr _filter_traces  zSnapshot._filter_tracecst|tstdt|j|rjgg|D] }|jrB|q,|q,fddjjD}n jj }t |j S)Nz)filters must be a list of filters, not %scsg|]}|r|qSr )rrIrrrr r s z*Snapshot.filter_traces..) rMr TypeErrortyper$rmr/rerdcopyrr)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) ValueErrorrerdKeyErrorrAr rr) rkey_type cumulativeZstatsZ tracebacksrKrar Ztrace_tracebackrrFr3r9r r r _group_bysZ        zSnapshot._group_byFcCs,|||}t|}|jdtjd|SNT)reversekey)rlistvaluessortrr")rrrZgroupedr2r r rr2s  zSnapshot.statisticscCs6|||}|||}t||}|jdtjd|Sr)rr4rr(r")rZ old_snapshotrrr1r0r2r r r compare_tos    zSnapshot.compare_toN)F)F) r$r%r&rr staticmethodrrrrr2rr r r rrs  3 rcCs$tstdt}t}t||S)NzLthe tracemalloc module must be tracing memory allocations to take a snapshot) is_tracing RuntimeErrorrZget_traceback_limitr)rerr r r take_snapshot s r)Zcollections.abcrr functoolsrrwrTZos.pathrgrZ _tracemallocrrrrr(r4r5rAr\r]rcrkrlrprrrr r r rs2 &0"?%0