U e5dú[ã@sxdZddlZddlZddlZddlZdddgZGdd„dƒZdd d„Zdd d„ZGd d„dƒZ d d „Z e dkrte ƒdS)z Class for profiling Python code.éNÚrunÚrunctxÚProfilec@s0eZdZdZdd„Zdd„Zdd„Zdd „Zd S) Ú_UtilszŠSupport class for utility functions which are shared by profile.py and cProfile.py modules. Not supposed to be used directly. cCs ||_dS©N)Úprofiler)Úselfr©r ú/usr/lib64/python3.8/profile.pyÚ__init__0sz_Utils.__init__c CsF| ¡}z(z| |¡Wntk r,YnXW5| |||¡XdSr)rÚ_showrÚ SystemExit)rÚ statementÚfilenameÚsortÚprofr r r r3s  z _Utils.runc CsJ| ¡}z,z| |||¡Wntk r0YnXW5| |||¡XdSr)rr rr )rrÚglobalsÚlocalsrrrr r r r<s  z _Utils.runctxcCs"|dk r| |¡n | |¡dSr)Ú dump_statsÚ print_stats)rrrrr r r r Es z _Utils._showN)Ú__name__Ú __module__Ú __qualname__Ú__doc__r rrr r r r r r*s   réÿÿÿÿcCsttƒ |||¡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)rrrr r r rQs cCsttƒ |||||¡S)z·Run 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)rrrrrr r r r^sc@söeZdZdZdZd5dd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dd„Z dd„Z dd„Z e e e e e e dœZdd„ZGdd„dƒZGdd„dƒZdd„Zdd „Zd6d"d#„Zd$d%„Zd&d'„Zd(d)„Zd*d+„Zd,d-„Zd.d/„Zd0e_d7d1d2„Zd3d4„ZdS)8raProfiler 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. rNcCsÌi|_d|_d|_d|_|dkr&|j}||_|sHtj|_|_|j |_ nl||_| ¡}z t |ƒ}Wn"t k r„||_|j |_ Yn0X|dkr˜|j |_ n|j|_ |tfdd„}||_| ¡|_| d¡dS)NÚécSs ||ƒƒSrr )ÚtimerÚsumr r r Úget_time_timer¯sz(Profile.__init__..get_time_timerr)ÚtimingsÚcurÚcmdÚ c_func_nameÚbiasÚtimeÚ process_timerÚget_timeÚtrace_dispatch_iÚ dispatcherÚlenÚ TypeErrorÚtrace_dispatchÚtrace_dispatch_lrÚtÚ simulate_call)rrr$r.Zlengthrr r r r �s0    zProfile.__init__cCs„|j}|ƒ}|d|d|j|j}|dkr8|j|_|j||||ƒrd|ƒ}|d|d|_n|ƒ}|d|d||_dS)NréÚc_call©rr.r$rr#Údispatch)rÚframeÚeventÚargrr.Úrr r r r,·szProfile.trace_dispatchcCsT|j}|ƒ|j|j}|dkr(|j|_|j||||ƒrD|ƒ|_n |ƒ||_dS©Nr1r2©rr4r5r6rr.r r r r(És zProfile.trace_dispatch_icCs`|j}|ƒd|j|j}|dkr,|j|_|j||||ƒrL|ƒd|_n|ƒd||_dS)NgN@r1r2r9r r r Útrace_dispatch_macØszProfile.trace_dispatch_maccCsT|j}|ƒ|j|j}|dkr(|j|_|j||||ƒrD|ƒ|_n |ƒ||_dSr8)r'r.r$rr#r3)rr4r5r6r'r.r r r r-æs zProfile.trace_dispatch_lc CsD|j\}}}}}}||k r*|r*| ||¡S|||||||f|_dS©Nr0)r!Útrace_dispatch_return) rr4r.ÚrptÚritÚretÚrfnÚrframeÚrcurr r r Útrace_dispatch_exceptionùs   z Profile.trace_dispatch_exceptioncCs|jr�|j|jdk r�|j\}}}}}}t|tjƒs�|j|jksXtd|||j||jfƒ‚| |d¡|jdks�|j|jdks�td|jdfƒ‚|j} | j| j | j f} |dd| ||jf|_|j } | | krò| | \} } }}}| | d|||f| | <nddddif| | <dS)NéþÿÿÿzBad callréýÿÿÿr0) r!Úf_backÚ isinstancerÚ fake_frameÚAssertionErrorr<Úf_codeÚ co_filenameÚco_firstlinenoÚco_namer )rr4r.r=r>r?r@rArBZfcodeÚfnr ÚccÚnsÚttÚctÚcallersr r r Útrace_dispatch_calls4 þ  ÿÿÿzProfile.trace_dispatch_callc Csndd|jf}|dd|||jf|_|j}||krX||\}}}}} ||d||| f||<nddddif||<dS)Nrrr0)r#r!r ) rr4r.rNr rOrPrQrRrSr r r Útrace_dispatch_c_calls zProfile.trace_dispatch_c_callcCsú||jdk rB||jdjks0td|jdfƒ‚| |jdd¡|j\}}}}}}||}||}|\} } } } } }| | || || | |f|_|j}||\}}}}}|sº||}|d}| |krÔ|| d|| <nd|| <||d||||f||<dS)NrDz Bad returnrErr0)r!rFrIr<r )rr4r.r=r>r?r@rBZ frame_totalZpptZpitZpetZpfnÚpframeZpcurr rOrPrQrRrSr r r r<"s$"zProfile.trace_dispatch_return)ÚcallZ exceptionÚreturnr1Z c_exceptionZc_returncCs"|jdrdS||_| |¡dS)Nr)r!r"r/)rr"r r r Úset_cmdXs zProfile.set_cmdc@seZdZdd„Zdd„ZdS)zProfile.fake_codecCs||_||_||_d|_dS©Nr)rKÚco_linerMrL)rrÚlineÚnamer r r r ^szProfile.fake_code.__init__cCst|j|j|jfƒSr)ÚreprrKr[rM©rr r r Ú__repr__dszProfile.fake_code.__repr__N)rrrr r`r r r r Ú fake_code]srac@seZdZdd„ZdS)zProfile.fake_framecCs||_||_dSr)rJrF)rÚcodeZpriorr r r r hszProfile.fake_frame.__init__N)rrrr r r r r rHgsrHcCsF| dd|¡}|jr |jd}nd}| ||¡}|jd||dƒdS)NZprofilerrDrW)rar!rHr3)rr]rbrVr4r r r r/ls   zProfile.simulate_callcCsJ|j}|ƒ|j}|jdr:|jd||jd|ƒd}q|ƒ||_dS)NrrXrDr)r'r.r!r3)rr'r.r r r Úsimulate_cmd_completexs   zProfile.simulate_cmd_completercCs$ddl}| |¡ ¡ |¡ ¡dSrZ)ÚpstatsZStatsZ strip_dirsZ sort_statsr)rrrdr r r rƒszProfile.print_statsc Cs0t|dƒ�}| ¡t |j|¡W5QRXdS)NÚwb)ÚopenÚ create_statsÚmarshalÚdumpÚstats)rÚfileÚfr r r rˆs zProfile.dump_statscCs| ¡| ¡dSr)rcÚsnapshot_statsr_r r r rg�szProfile.create_statsc Cs^i|_|j ¡D]H\}\}}}}}| ¡}d}| ¡D] }||7}q6|||||f|j|<qdSrZ)rjr ÚitemsÚcopyÚvalues) rÚfuncrOrPrQrRrSZncZcallcntr r r rm‘s  zProfile.snapshot_statscCsddl}|j}| |||¡SrZ)Ú__main__Ú__dict__r)rr"rrÚdictr r r ržsz Profile.runc Cs8| |¡t |j¡zt|||ƒW5t d¡X|Sr)rYÚsysÚ setprofiler)Úexec)rr"rrr r r r£s    zProfile.runctxc Os¨t|ƒdkr|^}}}nV|s&tdƒ‚nHd|krZ| d¡}|^}}ddl}|jdtdd�ntdt|ƒdƒ‚| t|ƒ¡t  |j ¡z|||ŽW¢St  d¡XdS) Nrz:descriptor 'runcall' of 'Profile' object needs an argumentrqrz0Passing 'func' as keyword argument is deprecated)Ú stacklevelz7runcall expected at least 1 positional argument, got %dr0) r*r+ÚpopÚwarningsÚwarnÚDeprecationWarningrYr^rurvr))ÚargsÚkwrrqrzr r r Úruncall­s(    ÿ ÿ zProfile.runcallz($self, func, /, *args, **kw)cCs<|jtk rtdƒ‚|j}d|_z| ||¡W¢S||_XdS)Nz&Subclasses must override .calibrate().r)Ú __class__rr+r$Ú_calibrate_inner)rÚmÚverboseZ saved_biasr r r Ú calibrateîs zProfile.calibratecCs|j}dd„}|fdd„}||ƒ|ƒ}||ƒ|ƒ}||}|rLtd|ƒtƒ} |ƒ}|  dtƒtƒ¡|ƒ}||} |r†td| ƒd} d} | j ¡D]0\\} }}\}}}}}|d kr˜| |7} | |7} q˜|râtd | ƒtd | ƒ| |d krútd | ƒ‚| |d| }|�rtd|ƒ|S)NcSst|ƒD]}d}qdSr;©Úrange)ÚnÚiÚxr r r Úf1s z$Profile._calibrate_inner..f1cSst|ƒD] }|dƒqdS)Nédr…)r‚rŠrˆr r r rls z#Profile._calibrate_inner..fz elapsed time without profiling =zf(m)zelapsed time with profiling =g)rlrŠz!'CPU seconds' profiler reported =ztotal # calls =r0z internal error: total calls = %dg@z+mean stopwatch overhead per profile event =) r'Úprintrrrrr rnÚ ValueError)rr‚rƒr'rŠrlZt0Út1Zelapsed_noprofileÚpZelapsed_profileZ total_callsZ reported_timerr\ÚfuncnamerOrPrQrRrSZmeanr r r r�ùsB   ÿ      zProfile._calibrate_inner)NN)r)r)rrrrr$r r,r(r:r-rCrTrUr<r3rYrarHr/rcrrrgrmrrrÚ__text_signature__r„r�r r r r rgsB& ' 'ú     + c Csšddl}ddlm}d}||d�}d|_|jdddd dd �|jd d d ddd�|jdddddd �tjdd…s„| ¡t d¡|  ¡\}}|tjdd…<|j dk r¸|j   |j ¡|_ t |ƒdk�rŽ|jrêddl}d}|j|ddœ}nR|d} tj  d|j  | ¡¡t | ¡�} t|  ¡| dƒ}W5QRX| ddddœ}zt||d|j |jƒWn6tk �rŠ} zdt_t | j¡W5d} ~ XYnXn| ¡|S)Nr)Ú OptionParserzMprofile.py [-o output_file_path] [-s sort] [-m module | scriptfile] [arg] ...)ÚusageFz-oz --outfileÚoutfilezSave stats to )ÚdestÚhelpÚdefaultz-mÚmoduleÚ store_truezProfile a library module.)r•Úactionr–r—z-sz--sortrz?Sort order when printing to stdout, based on pstats.Stats classrr0rz(run_module(modname, run_name='__main__'))Ú run_moduleÚmodnamerwrr)Ú__file__rÚ __package__Ú __cached__)ÚosZoptparser’Zallow_interspersed_argsZ add_optionruÚargvZ print_usageÚexitÚ parse_argsr”ÚpathÚabspathr*r˜Úrunpyr›ÚinsertÚdirnameÚioÚ open_codeÚcompileÚreadrrÚBrokenPipeErrorÚstdoutÚerrno) r r’r“ÚparserZoptionsr}r¦rbZglobsZprognameÚfpÚexcr r r Úmain9s^   ÿ ÿ þ   þ ü r³rr)Nr)Nr) rr©rur%rhÚ__all__rrrrr³rr r r r Ú s ' U9