QfBZdZddlZddlZddlZddlZddlZgdZGddZd dZ d dZ Gdd Z d Z e d k(re yy) z Class for profiling Python code.N)runrunctxProfilec(eZdZdZdZdZdZdZy)_UtilszSupport class for utility functions which are shared by profile.py and cProfile.py modules. Not supposed to be used directly. c||_yN)profiler)selfr s ./opt/alt/python312/lib64/python3.12/profile.py__init__z_Utils.__init__1s   c|j} |j||j|||y#t$rYwxYw#|j|||wxYwr )r r SystemExit_show)r statementfilenamesortprofs r rz _Utils.run4sW}} - HHY  JJtXt ,    JJtXt ,s!7 AAAAAc|j} |j||||j|||y#t$rYwxYw#|j|||wxYwr )r rrr)r rglobalslocalsrrrs r rz _Utils.runctx=s[}} - KK 7F 3 JJtXt ,    JJtXt ,s!9 AAAAAcN||j|y|j|yr ) dump_stats print_stats)r rrrs r rz _Utils._showFs"   OOH %   T "rN)__name__ __module__ __qualname____doc__r rrrrr rr+s !--#rrcBttj|||S)aRun statement under profiler optionally saving results in filename This function takes a single argument that can be passed to the "exec" statement, and an optional file name. In all cases this routine attempts to "exec" its first argument and gather profiling statistics from the execution. If no file name is present, then this function automatically prints a simple profiling report, sorted by the standard name string (file/line/function-name) that is presented in each line. )rrr)rrrs r rrRs '?  y(D 99rcFttj|||||S)zRun statement under profiler, supplying your own globals and locals, optionally saving results in filename. statement and filename have the same semantics as profile.run )rrr)rrrrrs r rr_s! '? ! !)Wfh MMrceZdZdZdZddZdZdZdZdZ d Z d Z d Z d Z e e e e e e d ZdZGddZGddZdZdZddZdZdZdZdZdZdZd dZdZy)!raProfiler class. self.cur is always a tuple. Each such tuple corresponds to a stack frame that is currently active (self.cur[-2]). The following are the definitions of its members. We use this external "parallel stack" to avoid contaminating the program that we are profiling. (old profiler used to write into the frames local dictionary!!) Derived classes can change the definition of some entries, as long as they leave [-2:] intact (frame and previous tuple). In case an internal error is detected, the -3 element is used as the function name. [ 0] = Time that needs to be charged to the parent frame's function. It is used so that a function call will not have to access the timing data for the parent frame. [ 1] = Total time spent in this frame's function, excluding time in subfunctions (this latter is tallied in cur[2]). [ 2] = Total time spent in subfunctions, excluding time executing the frame's function (this latter is tallied in cur[1]). [-3] = Name of the function that corresponds to this frame. [-2] = Actual frame that we correspond to (used to sync exception handling). [-1] = Our parent 6-tuple (corresponds to frame.f_back). Timing data for each function is stored as a 5-tuple in the dictionary self.timings[]. The index is always the name stored in self.cur[-3]. The following are the definitions of the members: [0] = The number of times this function was called, not counting direct or indirect recursion, [1] = Number of times this function appears on the stack, minus one [2] = Total time spent internal to this function [3] = Cumulative time that this function was present on the stack. In non-recursive functions, this is the total execution time from start to finish of each invocation of a function, including time spent in all subfunctions. [4] = A dictionary indicating for each function name, the number of times it was called by us. rNci|_d|_d|_d|_| |j}||_|s.t j x|_|_|j|_ n\||_|j} t|}|dk(r|j|_ n|j|_ |tfd}||_|j|_|j#dy#t$r||_|j|_ YJwxYw)Nc||Sr r )timersums r get_time_timerz(Profile.__init__..get_time_timersuw<'rr )timingscurcmd c_func_namebiastime process_timer(get_timetrace_dispatch_i dispatcherlentrace_dispatchtrace_dispatch_lr) TypeErrort simulate_call)r r(r/r9lengthr*s r r zProfile.__init__s  <99D )-):): :DJ"33DODJ A /Q Q;&*&9&9DO&*&;&;DO */C( .  :&# 8 % "&"7"7 8s: C%%!D D c2|j}|}|d|dz|jz |jz }|dk(r|j|_|j ||||r|}|d|dz|_y|}|d|dz|z |_y)Nrc_callr(r9r/rr.dispatch)r frameeventargr(r9rs r r6zProfile.trace_dispatchs  G aD1Q4K$&& 499 , H "||D  4== eA .AqTAaD[DFAqTAaD[1_DFrc|j}||jz |jz }|dk(r|j|_|j ||||r ||_y||z |_yNr>r?r rArBrCr(r9s r r3zProfile.trace_dispatch_ise  Gdff tyy ( H "||D  4== eQ /WDFWq[DFrc|j}|dz |jz |jz }|dk(r|j|_|j ||||r|dz |_y|dz |z |_y)NgN@r>r?rGs r trace_dispatch_maczProfile.trace_dispatch_macsr  GDL466 !DII - H "||D  4== eQ /WT\DFWT\A%DFrc|j}||jz |jz }|dk(r|j|_|j ||||r ||_y||z |_yrF)r2r9r/rr.r@)r rArBrCr2r9s r r7zProfile.trace_dispatch_lse== J $)) + H "||D  4== eQ /ZDFZ!^DFrcz|j\}}}}}}||ur|r|j||S|||z||||f|_yNr=)r,trace_dispatch_return) r rAr9rptritretrfnrframercurs r trace_dispatch_exceptionz Profile.trace_dispatch_exceptionsP+/88(S#sFD % T--fa8 8AsC5rc|jrZ|j|jdur?|j\}}}}}}t|tjs|j |d|j } | j| j| jf} |dd| ||jf|_|j} | | vr| | \} } }}}| | dz|||f| | <yddddif| | <yNrr=) r,f_back isinstancer fake_framerMf_code co_filenameco_firstlinenoco_namer+)r rAr9rNrOrPrQrRrSfcodefnr+ccnsttctcallerss r trace_dispatch_callzProfile.trace_dispatch_calls 88 DHHRL8/3xx ,Cc3fg&8&89**615 !5!5u}} Eq!R1,, =&-bk #BBGb1fb"g5GBKQ1b.GBKrcdd|jf}|dd|||jf|_|j}||vr||\}}}}} ||dz||| f||<yddddif||<y)Nr%rr=)r.r,r+) r rAr9r`r+rarbrcrdres r trace_dispatch_c_callzProfile.trace_dispatch_c_calls!T%% &q!R1,, =&-bk #BBGbdBG3GBKQ1b.GBKrcf||jdur|j|jdd|j\}}}}}}||z}||z}|\} } } } } }| | |z| |z| | |f|_|j}||\}}}}}|s ||z}|dz}| |vr || dz|| <nd|| <||dz ||z||f||<yrV)r,rMr+)r rAr9rNrOrPrQrS frame_totalpptpitpetpfnpframepcurr+rarbrcrdres r rMzProfile.trace_dispatch_return#s  $  & &txx|Q 7 +/(('S#sE4AgCi +/(S#sFDc 3#4c64G,,")#,BB k!BaB '>"3 c_exceptionc_returncT|jdry||_|j|yN)r,r-r:)r r-s r set_cmdzProfile.set_cmdYs$ 88B< 3rceZdZdZdZy)Profile.fake_codec<||_||_||_d|_yNr)r\co_liner^r])r rlinenames r r zProfile.fake_code.__init___s 'D DLDL"#D rcZt|j|j|jfSr )reprr\r~r^r s r __repr__zProfile.fake_code.__repr__es"))4<<FG GrN)rrrr rr rr fake_coder{^s  $  HrrceZdZdZy)Profile.fake_framec ||_||_yr )r[rX)r codepriors r r zProfile.fake_frame.__init__isDKDKrN)rrrr r rr rZrhs rrZc|jdd|}|jr|jd}nd}|j||}|jd||dy)NprofilerrWrq)rr,rZr@)r rrrorAs r r:zProfile.simulate_callmsT~~iD1 88XXb\FFf- fdE1-rc|j}||jz }|jdr6|jd||jd|d}|jdr6||z |_y)NrxrsrWr)r2r9r,r@)r r2r9s r simulate_cmd_completezProfile.simulate_cmd_completeysh== J hhrl $DMM( #D$((2, :A hhrl arcddl}|j|jj|j yr})pstatsStats strip_dirs sort_statsr)r rrs r rzProfile.print_statss, T%%'2248+-rct|d5}|jtj|j|dddy#1swYyxYw)Nwb)open create_statsmarshaldumpstats)r filefs r rzProfile.dump_statss8 $      LLQ '  s 1AAcD|j|jyr )rsnapshot_statsrs r rzProfile.create_statss ""$ rci|_|jjD]K\}\}}}}}|j}d}|j D]}||z } |||||f|j|<Myr})rr+itemscopyvalues) r funcrarbrcrdrenccallcnts r rzProfile.snapshot_statsst /3||/A/A/C +D+2r2r7llnGB">>+g ,!2r2w6DJJt  0DrcHddl}|j}|j|||Sr})__main____dict__r)r r-rdicts r rz Profile.runs#  {{3d++rc|j|tj|j t |||tjd|S#tjdwxYwr )rysys setprofiler4exec)r r-rrs r rzProfile.runctxsP S t' ! gv & NN4   NN4 s AA-c|jt|tj|j ||i|tjdS#tjdwxYwr )ryrrrr4)r rargskws r runcallzProfile.runcallsM T$Z  t' !$$ NN4 CNN4 s AA/c|jtur td|j}d|_ |j ||||_S#||_wxYw)Nz&Subclasses must override .calibrate().r) __class__rr8r/_calibrate_inner)r mverbose saved_biass r calibratezProfile.calibratesN >> (DE EYY   #((G4"DI DIs A Ac<|j}d}|fd}|||}|||}||z }|r td|t} |}| jdt t |}||z } |r td| d} d} | j jD]\\} }}\}}}}}|dvs| |z } | |z } |rtd| td | | |d zk7rtd | z| |z d z | z }|r td ||S)Nc(t|D]}d}yrLrange)nixs r f1z$Profile._calibrate_inner..f1s1Xrc4t|D] }|d y)Ndr)rrrs r rz#Profile._calibrate_inner..fs1X3rz elapsed time without profiling =zf(m)zelapsed time with profiling =g)rrz!'CPU seconds' profiler reported =ztotal # calls =r=z internal error: total calls = %dg@z+mean stopwatch overhead per profile event =) r2printrrrrr+r ValueError)r rrr2rrt0t1elapsed_noprofilepelapsed_profile total_calls reported_timerrfuncnamerarbrcrdremeans r rzProfile._calibrate_innersN==   !Z ! ZG  46G H I Z FH- Zr'  1? C   ! B &XtX(ARR;&r! # "  5} E #[ 1 !a% ?+MN N  11S8;F  ? F r)NN)rx)r)rrrrr/r r6r3rIr7rTrfrhrMr@ryrrZr:rrrrrrrrrrr rr rrhs$L D#'N %$ ! & $&, #N$-'',)  H  HH  .  ( 7, !b #<rrcddl}ddlm}d}||}d|_|j dddd d |j d d d dd|j ddddd t j dds%|jt jd|j\}}|t j dd|j*|jj|j|_ t|dkDr|jrddl}d}|j |dd}n|d} t jj#d|jj%| t'j(| 5} t+| j-| d}dddt.j0j3dd| } | | j4| j6ddd} t9|d|j|j:|S|j|S#1swYxYw#t<$r5} dt _t j| j@Yd} ~ |Sd} ~ wwxYw)Nr) OptionParserzMprofile.py [-o output_file_path] [-s sort] [-m module | scriptfile] [arg] ...)usageFz-oz --outfileoutfilezSave stats to )desthelpdefaultz-mmodule store_truezProfile a library module.)ractionrrz-sz--sortrz?Sort order when printing to stdout, based on pstats.Stats classrxr=r&z(run_module(modname, run_name='__main__')) run_modulemodnamerr)rloaderorigin)__spec____file__r __package__ __cached__)!osoptparserallow_interspersed_args add_optionrargv print_usageexit parse_argsrpathabspathr5rrunpyrinsertdirnameio open_codecompileread importlib machinery ModuleSpecrrrrBrokenPipeErrorstdouterrno) rrrparseroptionsrrrglobsprognamefpspecexcs r mainr*s % [E  &F%*F" dKi &6 d, (%9 dH6 N 88AB<  '')OWdCHHQK"''//'//: 4y1} >> =D#..7E AwH HHOOArwwx8 9h'2rwwy(F;(&&11z$9A2CD! KK II#" E 4goow|| D M  M'(' CJ HHSYY   M  s$:H#H H I)*IIrrw)rimportlib.machineryrrrr0r__all__rrrrrrr rr rs]'$  &##N :N~~D9x zFr