a �DOgaY�@svdZddlZddlZddlZddlZgd�ZGdd�d�Zddd�Zdd d �ZGd d �d �Z d d�Z e dkrre �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.9/profile.py�__init__0sz_Utils.__init__c CsR|��}z4z|�|�Wnty*Yn0W|�|||�n|�|||�0dSr)rr� SystemExit�_show)r� statement�filename�sort�profr r r r3s  z _Utils.runc CsV|��}z8z|�|||�Wnty.Yn0W|�|||�n|�|||�0dSr)rrr r )rr�globals�localsrrrr r r r<s  z _Utils.runctxcCs"|dur|�|�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 rcCstt��|||||�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^src@s�eZdZdZdZd4dd�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 �Zd5d"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd6d0d1�Zd2d3�ZdS)7raProfiler 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|_|dur&|j}||_|sHtj|_|_|j |_ nj||_|��}z t |�}Wn t y�||_|j |_ Yn00|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.�lengthrr 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�Nr2r3�rr5r6r7rr.r r r r(�s zProfile.trace_dispatch_icCs`|j}|�d|j|j}|dkr,|j|_|j||||�rL|�d|_n|�d||_dS)NgN@r2r3r:r r r �trace_dispatch_mac�szProfile.trace_dispatch_maccCsT|j}|�|j|j}|dkr(|j|_|j||||�rD|�|_n |�||_dSr9)r'r.r$rr#r4)rr5r6r7r'r.r r r r-�s zProfile.trace_dispatch_lc CsD|j\}}}}}}||ur*|r*|�||�S|||||||f|_dS�Nr1)r!�trace_dispatch_return) rr5r.�rpt�rit�ret�rfn�rframe�rcurr r r �trace_dispatch_exception�s   z Profile.trace_dispatch_exceptioncCs|jr�|j|jdur�|j\}}}}}}t|tj�s�|j|jusXJd|||j||jf��|�|d�|jdus�|j|jdus�Jd|jdf��|j} | j| j| j f} |dd| ||jf|_|j } | | vr�| | \} } }}}| | d|||f| | <nddddif| | <dS)N�����zBad callr�����r1) r!�f_back� isinstancer� fake_framer=�f_code� co_filename�co_firstlineno�co_namer )rr5r.r>r?r@rArBrCZfcode�fnr �cc�ns�tt�ct�callersr r r �trace_dispatch_calls0 �  ���zProfile.trace_dispatch_callc Csndd|jf}|dd|||jf|_|j}||vrX||\}}}}} ||d||| f||<nddddif||<dS)Nrrr1)r#r!r ) rr5r.rNr rOrPrQrRrSr r r �trace_dispatch_c_calls zProfile.trace_dispatch_c_callcCs�||jdurB||jdjus0Jd|jdf��|�|jdd�|j\}}}}}}||}||}|\} } } } } }| | || || | |f|_|j}||\}}}}}|s�||}|d}| |vr�|| d|| <nd|| <||d||||f||<dS)NrEz Bad returnrFrr1)r!rGr=r )rr5r.r>r?r@rArCZ frame_totalZpptZpitZpetZpfn�pframeZpcurr rOrPrQrRrSr r r r="s$"zProfile.trace_dispatch_return)�call� exception�returnr2Z c_exceptionZc_returncCs"|jdrdS||_|�|�dS)Nr)r!r"r/)rr"r r r �set_cmdXszProfile.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 rar r r r � fake_code]srbc@seZdZdd�ZdS)zProfile.fake_framecCs||_||_dSr)rJrG)r�codeZpriorr r r r hszProfile.fake_frame.__init__N)rrrr r r r r rIgsrIcCsF|�dd|�}|jr |jd}nd}|�||�}|jd||d�dS)NZprofilerrErW)rbr!rIr4)rr^rcrVr5r r r r/ls   zProfile.simulate_callcCsJ|j}|�|j}|jdr:|jd||jd|�d}q|�||_dS)NrrYrEr)r'r.r!r4)rr'r.r r r �simulate_cmd_completexs   zProfile.simulate_cmd_completercCs$ddl}|�|����|���dSr[)�pstatsZStatsZ strip_dirsZ sort_statsr)rrrer r r r�szProfile.print_statscCsDt|d��&}|��t�|j|�Wd�n1s60YdS)N�wb)�open� create_stats�marshal�dump�stats)r�file�fr r r r�s zProfile.dump_statscCs|��|��dSr)rd�snapshot_statsr`r r r rh�szProfile.create_statsc Cs^i|_|j��D]H\}\}}}}}|��}d}|��D] }||7}q6|||||f|j|<qdSr[)rkr �items�copy�values) r�funcrOrPrQrRrSZncZcallcntr r r rn�s  zProfile.snapshot_statscCsddl}|j}|�|||�Sr[)�__main__�__dict__r)rr"rs�dictr r r r�sz Profile.runc CsB|�|�t�|j�zt|||�Wt�d�n t�d�0|Sr)rZ�sys� setprofiler)�exec)rr"rrr r r r�s   zProfile.runctxc OsF|�t|��t�|j�z||i|��Wt�d�St�d�0dSr)rZr_rvrwr))rrr�args�kwr r r �runcall�s  �zProfile.runcallcCs@|jturtd��|j}d|_z|�||�W||_S||_0dS)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 vr�| |7} | |7} q�|r�td | �td | �| |d kr�td | ��| |d| }|�rtd|�|S)NcSst|�D]}d}qdSr<��range)�n�i�xr r r �f1�s z$Profile._calibrate_inner..f1cSst|�D] }|d�qdS)N�dr�)r~r�r�r r r rm�s z#Profile._calibrate_inner..fz elapsed time without profiling =zf(m)zelapsed time with profiling =g)rmr�z!'CPU seconds' profiler reported =ztotal # calls =r1z internal error: total calls = %dg@z+mean stopwatch overhead per profile event =) r'�printrrrrr ro� ValueError)rr~rr'r�rm�t0�t1Zelapsed_noprofile�pZelapsed_profileZ total_callsZ reported_timerr]�funcnamerOrPrQrRrS�meanr r r r}�sB   �      zProfile._calibrate_inner)NN)r)r)rrrrr$r r,r(r;r-rDrTrUr=r4rZrbrIr/rdrrrhrnrrr{r�r}r r r r rgs@& ' '�     1 rc Cs�ddl}ddlm}d}||d�}d|_|jdddd dd �|jd d d ddd�|jdddddd �tjdd�s�|��t�d�|� �\}}|tjdd�<|j dur�|j � |j �|_ t |�dk�r�|jr�ddl}d}|j|dd�}nh|d} tj �d|j �| ��t�| �� } t| ��| d�}Wd�n1�s:0Y| dddd�}zt||d|j |j�Wn8t�y�} zdt_t�| j�WYd} ~ n d} ~ 00n|��|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 classrr1rz(run_module(modname, run_name='__main__'))� run_module�modnamerxrs)�__file__r� __package__� __cached__)�osZoptparser�Zallow_interspersed_argsZ add_optionrv�argv� print_usage�exit� parse_argsr��path�abspathr*r��runpyr��insert�dirname�io� open_code�compile�readrr�BrokenPipeError�stdout�errno) r�r�r��parser�optionsryr�rcZglobsZprogname�fp�excr r r �main)sZ   � � �   � 0�$r�rs)Nr)Nr) rr�rvr%ri�__all__rrrrr�rr r r r � s ' E9