QfirdZddlZddlZddlZddlZddlZddlmZmZddl m Z ddl m Z ddl mZgdZeeGdd Ze d Gd d Ze d GddZGddZGddZdZdZdZdZdZdZdZedk(rddlZ ddlZGddej@Z!e"ejFdkDrejFdZ$ndZ$ e!e$Z%ejFddD]Z&e%jOe&e(d e%jR!e%jUe(d"e%jR!yy#e$rYwxYw#e+$rYywxYw)#z3Class for printing reports on profiled python code.N)StrEnum _simple_enum) cmp_to_key) dataclass)Dict)StatsSortKeyFunctionProfile StatsProfilec6eZdZdZdZdZdZdZdZdZ dZ d Z d Z y ) r )callsncalls) cumulativecumtime)filenamemodulelinenamenflpcallsstdname)timetottimec|d}tj||}||_|ddD]}||j|<||_|SNr)str__new___value__value2member_map_ _all_values)clsvaluesvalueobj other_values -/opt/alt/python312/lib64/python3.12/pstats.pyrzSortKey.__new__0sNq kk#u% !!":K25C " "; /&  N) __name__ __module__ __qualname__CALLS CUMULATIVEFILENAMELINENAMENFLPCALLSSTDNAMETIMErr(r'r r $s4 E(J#H D D C FG Dr(r T) unsafe_hashcTeZdZUeed<eed<eed<eed<eed<eed<eed<y) r rrpercall_tottimerpercall_cumtime file_name line_numberN)r)r*r+r__annotations__floatintr5r(r'r r :s( K N NNr(r c0eZdZUdZeed<eeefed<y)r z0Class for keeping track of an item in inventory.total_tt func_profilesN) r)r*r+__doc__r=r<rrr r5r(r'r r Ds:O_,--r(r ceZdZdZdddZdZdZdZdZd Z d d d d d d d ddddddd Z dZ dZ dZ dZdZdZdZdZdZdZdZdZd#d Zd!Zd"Zy)$raThis class is used for creating reports from data generated by the Profile class. It is a "friend" of that class, and imports data either by direct access to members of Profile class, or by reading in a dictionary that was emitted (via marshal) from the Profile class. The big change from the previous Profiler (in terms of raw functionality) is that an "add()" method has been provided to combine Stats from several distinct profile runs. Both the constructor and the add() method now take arbitrarily many file names as arguments. All the print methods now take an argument that indicates how many lines to print. If the arg is a floating-point number between 0 and 1.0, then it is taken as a decimal percentage of the available lines to be printed (e.g., .1 means print 10% of all available lines). If it is an integer, it is taken to mean the number of lines of data that you wish to have printed. The sort_stats() method now processes some additional options (i.e., in addition to the old -1, 0, 1, or 2 that are respectively interpreted as 'stdname', 'calls', 'time', and 'cumulative'). It takes either an arbitrary number of quoted strings or SortKey enum to select the sort order. For example sort_stats('time', 'name') or sort_stats(SortKey.TIME, SortKey.NAME) sorts on the major key of 'internal function time', and on the minor key of 'the name of the function'. Look at the two tables in sort_stats() and get_sort_arg_defs(self) for more examples. All methods return self, so you can string together commands like: Stats('foo', 'goo').strip_dirs().sort_stats('calls'). print_stats(5).print_callers(5) N)streamc|xstj|_t|sd}n |d}|dd}|j ||j |yr)sysstdoutrDleninitadd)selfrDargsargs r'__init__zStats.__init__lsJ*  4yCq'C8D #$r(cjd|_g|_d|_d|_d|_d|_d|_t|_i|_ i|_ |j| |jy#t$r7td|jr|jdndz|jwxYw)NrzInvalid timing data %sfile) all_calleesfilesfcn_listr@ total_calls prim_calls max_name_lenset top_levelstats sort_arg_dict load_statsget_top_level_stats ExceptionprintrD)rKrMs r'rIz Stats.initvs        $ $ &  *%)ZZ4::b>R9?C{{ L  s !A22AB2c|i|_yt|trst|d5}t j ||_ddd t j|}tj|jdz|z}|g|_ n4t|dr(|j|j|_i|_|jstd|jd|y#1swYxYw#YuxYw)Nrbz create_statszCannot create or construct a z object from )r\ isinstanceropenmarshalloadosstatrctimest_mtimerUhasattrrd TypeError __class__)rKrMf file_statss r'r^zStats.load_statss ;DJ  S !c4A$\\!_ ! WWS\ jj!4!45>DDJ S. )    DJCIzz#~~s45 5!  sC* :C6*C36C:c|jjD]\}\}}}}}|xj|z c_|xj|z c_|xj|z c_d|vr|j j |tt||jkDstt||_ y)N)jprofilerprofiler) r\itemsrWrXr@r[rJrHfunc_std_stringrY)rKfuncccncttctcallerss r'r_zStats.get_top_level_statss/3zz/?/?/A +D+2r2r7    "  OO "O MM "M*g5""4(?4()D,=,==$'(=$>!0Br(c|s|St|D]^}t|t|k7r t|}|xj|jz c_|xj|jz c_|xj |j z c_|xj |j z c_|jD]}|jj||j|jkr|j|_ d|_ |jjD]C\}}||jvr|j|}nddddif}t|||j|<Ea|SNr)reversedtyperrUrWrXr@r[rJrYrVr\ruadd_func_stats)rKarg_listitemrwrj old_func_stats r'rJz Stats.adds+KX&DDzT$Z'T{ JJ$** $J    0 0 0  OOt .O MMT]] *M""4('  4#4#44$($5$5! DM"jj..0 d4::%$(JJt$4M%&1a$5M#1-#F 4 1'* r(ct|d5}tj|j|dddy#1swYyxYw)z:Write the profile data to a file we know how to load back.wbN)rfrgdumpr\)rKrrps r' dump_statszStats.dump_statss, (D !Q LLQ '" ! !s !7A)))rrPz call count)))rPzcumulative time))rz file name))rz line number))rz function name))rrrzname/file/line)))rrPzprimitive call count)))rz standard name)))rPz internal time) r rrrrrrrrrrrrc|jsSix|_}i}|jjD]!\}}|}|s ||vrd||<|||<|dd}|r#|D]}||=|jS)z)Expand all abbreviations that are unique.rNrP)r]sort_arg_dict_defaultru)rKdictbad_listwordtupfragments r'get_sort_arg_defszStats.get_sort_arg_defss!!(* *D H!77==? c4'-.*%(DN'}H @!J!!!!r(c |s d|_|St|dk(r"t|dtrddddd|dg}n=t|dk\r/|ddD]'}t |t |dk7st d |j }d }d |_d }|D]I}t|tr |j}|||dz}|xj|||dzz c_d }Kg}|jjD]1\}\} } } } } |j| | | | f|zt||fz3|jtt!|j" gx|_}|D]}|j|d|S)Nrrrr rr)rPrrrrzCan't have mixed argument typer5rQz, )keyrP)rVrHrer>rrnr sort_typer r$r\ruappendrvsortr TupleCompcompare)rKfieldrM sort_arg_defs sort_tuple connectorr stats_listrwrxryrzr{r|rVtuples r' sort_statszStats.sort_statssDMK u:?z%(C8$"!')*/q35EZ1_QRy9U1X.#$DEE!..0   D$(zz#mD&9!&< gg?g?rz List reduced from z to z due to restriction ) rerrecompileerrorsearchrvrrHr=r>)rKsellistmsgnew_listrexrwcounts r'eval_print_amountzStats.eval_print_amountGs c3  %jjoH::od34OOD)IE#u%#*:s*:ECK",-<C%!s*:U*:< t9H % D 3x=#/ /C}'88 %=CC}$ %sDD"!D"c |jr|jddn"t|jj}|s t diSt t |j}i}t ||}|D]}|j|\}}}} } |\} } } ||k(r t|nt|dzt|z}t t |}|dk(rdnt t ||z }t t | }|dk(rdnt t | |z }t|||||| | }||| <|S)a?This method returns an instance of StatsProfile, which contains a mapping of function names to instances of FunctionProfile. Each FunctionProfile instance holds information related to the function's profile such as how long the function took to run, how many times it was called, etc... Nr/rP) rVrr\keysr r=f8r@rr )rK func_listr@rA stats_profilerwrxryrzr{r|r:r; func_namerrr8rr9 func_profiles r'get_stats_profilezStats.get_stats_profileas0 )- DMM!$4 @Q;R 2& &DMM*+ $X}= D&*jj&6 #BBG04 -I{I "bSWs2w}s2w/FFBrFmG$&!Gbr"R%y1AOBrFmG$&!Gbr"R%y1AO*L(4M) $#&r(c|j}|jr"|jdd}d|jzdz}n%t|jj }d}|D]}|j |||\}}t|}|sd|fSt||j|t|jkr5d}|D].}tt||kDstt|}0|dz|fS)Nz Ordered by:  z! Random listing order was used rrRr) rYrVrrr\rrrHrarDrv)rKsel_listwidth stat_listr selectionrrws r'get_print_listzStats.get_print_lists!! == a(I#dnn4t;CTZZ__./I6C!I!33Iy#NNIs"Ii<  c $ 3tzz? "E!-.6 56E"Qw !!r(c|jD]}t||j|jrt|jd}|jD]#}t|t ||j%t||j dd|j|j |j k7r%td|j zd|jtd|jz|jt|j|j|\}}|rT|j|D]}|j|t|jt|j|S)NrR zfunction calls endrSz(%d primitive calls)zin %.3f seconds) rUrarDr[func_get_function_namerWrXr@r print_title print_line)rKamountrindentrwrrs r' print_statszStats.print_statss H ( -# :: t{{ #NND &06T[[ I# fd&&(8c T   t . (4??:$++ V $--/dkkB 4;;))&1 t     % t{{ # t{{ # r(c`|j|\}}|r|j|j|d|D]D}||jvr!|j |||j|2|j ||iFt |j t |j |S)Nz called...rR)rrprint_call_headingrTprint_call_linerarD)rKrrrrws r' print_calleeszStats.print_calleess))&1 t       # #E; 74+++((d6F6Ft6LM((b9  t{{ # t{{ # r(c|j|\}}|rn|j|d|D]+}|j|\}}}}} |j||| d-t |j t |j |S)Nzwas called by...z<-rR)rrr\rrarD) rKrrrrwrxryrzr{r|s r' print_callerszStats.print_callerss))&1 t   # #E+= >*.**T*:'BB$$UD'4@ t{{ # t{{ # r(cPtdj||z|jd}|jj D]>\}}}}}|s t t |j } t| t}n|rtd|zdz|jyy)Nz Function rRFrz ncalls tottime cumtime) raljustrDr\r#nextiterrer) rK name_size column_title subheaderrxryrzr{r|r$s r'rzStats.print_call_headings k *\9 L '+zz'8'8': #BBGT'.."234&ue4  (;  #i-"@@t{{ S r(c htt|j||zd|j|st|jyt |j }d}|D]}t|}||} t | tr]| \} } } } | | k7rd| | fz}nd| fz}|jddt|zzdt| dt| d |}|d z}n(|d | d t|j|d }|d z}t||z|z|jd}y)NrrrRrQz%d/%dz%drrz r(z) r) rarvrrDsortedrrerrjustrHrr\)rKrsource call_dictarrowclistrrwrr$ryrxrzr{substats left_widths r'rzStats.print_call_lines  of%++I6>CdkkZ t{{ # y~~'(D"4(DdOE%'!&BB8&"b1H#re|H-5^^AaF mO-L-/VRVTC&] *.r$**T:J1:M7NO&] &#h.T[[ AF!r(cbtdd|jtd|jy)Nz- ncalls tottime percall cumtime percallrrzfilename:lineno(function)rRrarDrs r'rzStats.print_titles" =3T[[Y ) )r)r*r+rBrNrIr^r_rJrrrrrrrrrrrrrrrrrr5r(r'rrJsB&*&,?4(DCHHBDBFGMFFF ""%N : &4!F"0.   T2=7r(rceZdZdZdZdZy)raThis class provides a generic function for comparing any two tuples. Each instance records a list of tuple-indices (from most significant to least significant), and sort direction (ascending or descending) for each tuple-index. The compare functions can then be used as the function argument to the system sort() function when a list of tuples need to be sorted in the instances order.c||_yrcomp_select_list)rKrs r'rNzTupleComp.__init__s 0r(cf|jD]"\}}||}||}||kr| cS||kDs |cSyr~r)rKleftrightindex directionlrs r'rzTupleComp.comparesJ $ 5 5 E9U Ae A1u!z!1u  !6r(N)r)r*r+rBrNrr5r(r'rrs&1r(rcR|\}}}tjj|||fSr)ripathbasename)rrrrs r'rr!s*$HdD 77  H %tT 11r(c |dS)Nrr5)rws r'rr%s 7Nr(c~|dddk(r1|d}|jdr|jdrd|ddzS|Sd|zS) Nr)~r<>z{%s}rrPz %s:%d(%s)) startswithendswith)rrs r'rvrv(sQ!} | ??3 DMM#$6D2J& &KY&&r(c\|\}}}}}|\}}} } } ||z||z|| z|| zt| |fS)z3Add together all the stats for two profile entries.) add_callers) targetrrxryrzr{r|t_cct_nct_ttt_ct t_callerss r'rr9sO$BBG(.%D$dI tGRWbgr$w)W- //r(c i}|jD] \}}|||< |jD]O\}}||vrAt|tr#tdt|||D||<=||xx|z cc<K|||<Q|S)z*Combine two caller lists in a single list.c3,K|] \}}||zywrr5).0ijs r' zadd_callers..Is)[d}|jD]}||z } |S)z@Sum the caller statistics to get total number of calls received.r)r#)r|ryr s r' count_callsr#Qs& B! e " Ir(c d|zS)Nz%8.3fr5)xs r'rr\s Q;r(__main__ceZdZddZdZdZdZdZdZdZ d Z d Z d Z d Z d ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZy)ProfileBrowserNctjj|d|_d|_t j |_||j|yy)N% ) cmdCmdrNpromptr\rFrGrDdo_read)rKprofiles r'rNzProfileBrowser.__init__ksD GG  T "DKDJ**DK" W%#r(c|j}g}|D]} |jt||jrt|j||yt d|j y#t$rYnwxYw t |}|dkDs|dkrt d|j |j|#t$rYnwxYw|j|)Nrrz#Fraction argument must be in [0, 1]rRNo statistics object is loaded.) splitrr> ValueErrorr=rarDr\getattr)rKfnrrL processedtermfracs r'genericzProfileBrowser.genericss::A?,C0C C Cctd|jtd|jtd|jtd|jtd|jtd|jy)NzArguments may be:rRz0* An integer maximum number of entries to print.z:* A decimal fractional number between 0 and 1, controllingz- what fraction of selected entries to print.z8* A regular expression; only entries with function namesz that match it are printed.rrs r' generic_helpzProfileBrowser.generic_helpsb %DKK 8 D4;; W NUYU`U` a A T LSWS^S^ _ 0t{{ Cr(c|jr |jj|ytd|jy#t$r'}td|d||jYd}~yd}~wwxYw)NzFailed to load statistics for z: rRr1r)r\rJOSErrorrarD)rKres r'do_addzProfileBrowser.do_addsjzz`JJNN4( 7dkkJ `D!LSWS^S^__ `sA A2 A--A2c2td|jy)Nz>Add profile info from given file to current statistics object.rRrrs r'help_addzProfileBrowser.help_adds RY]YdYd er(c&|jd|S)Nrr9rKrs r' do_calleeszProfileBrowser.do_callees<<6 6r(cRtd|j|jy)Nz6Print callees statistics from the current stat object.rRrarDr;rs r' help_calleeszProfileBrowser.help_callees JQUQ\Q\ ]    r(c&|jd|S)NrrCrDs r' do_callerszProfileBrowser.do_callersrFr(cRtd|j|jy)Nz6Print callers statistics from the current stat object.rRrHrs r' help_callerszProfileBrowser.help_callersrJr(c2td|jy)NrQrRrrrDs r'do_EOFzProfileBrowser.do_EOFs "4;; 'r(c2td|jyNzLeave the profile browser.rRrrs r'help_EOFzProfileBrowser.help_EOF .T[[ Ar(cy)Nrr5rDs r'do_quitzProfileBrowser.do_quitsr(c2td|jyrRrrs r' help_quitzProfileBrowser.help_quitrTr(c|r t||_|dz|_ yt|jdkDr!|jdd}|j|ytd|j y#t$r.}t|jd|j Yd}~yd}~wt $r9}t|jjdz||j Yd}~yd}~wwxYw) NrrR:r*rz1No statistics object is current -- cannot reload.r) rr\r=rarLrDr`ror)r-rHr.)rKrerrs r'r.zProfileBrowser.do_reads!&tDJ#Tk  T[[!A%{{3B' T"IPTP[P[\#((1+DKK8 #--0036$++Ns#A00 C'9$B"" C'./C""C'c`td|jtd|jy)Nz+Read in profile data from a specified file.rRz*Without argument, reload the current file.rrs r' help_readzProfileBrowser.help_reads ?dkk R >T[[ Qr(c|jr|jjytd|jy)Nr1rRr)r\rrarDrDs r' do_reversezProfileBrowser.do_reverses2zz ((*7dkkJr(c2td|jy)Nz/Reverse the sort order of the profiling report.rRrrs r' help_reversezProfileBrowser.help_reverses C$++ Vr(c|jstd|jy|jj|rJt fd|j Dr(|jj |j ytd|jtjjD]$\}}t|d|d|j&y)Nr1rRc3&K|]}|v ywrr5)rr%abbrevss r'rz)ProfileBrowser.do_sort..sALqQ'\Lsz/Valid sort keys (unique prefixes are accepted):z -- rr) r\rarDrallr2rrrru)rKrrr$res @r'do_sortzProfileBrowser.do_sorts::7dkkJjj224GADJJLAA% %%tzz|4 GdkkZ$)$?$?$E$E$GLS%U1X6T[[I%Hr(c`td|jtd|jy)Nz.Sort profile data according to specified keys.rRz3(Typing `sort' without arguments lists valid keys.)rrs r' help_sortzProfileBrowser.help_sorts B U Gdkk Zr(cltjDcgc]}|j|s|c}Scc}wr)rrr)rKtextrLas r' complete_sortzProfileBrowser.complete_sorts-$::Q:!all4>PA:Q QQs11c&|jd|S)NrrCrDs r'do_statszProfileBrowser.do_statss<< t4 4r(cRtd|j|jy)Nz.Print statistics from the current stat object.rRrHrs r' help_statszProfileBrowser.help_statss B U    r(c|jr|jjytd|jy)Nr1rR)r\rrarDrDs r'do_stripzProfileBrowser.do_strips(zz %%'7dkkJr(c2td|jy)Nzrs9,  & ! A g* t t... |7|7| 22 '"/" z PPd 388}qhhqk    -xx|G NN7 #$ :P jw~~.G    @    s%)E*A%EEEE$#E$