a
    zecY                     @   sr   d dl Z d dlZd dlZd dlZg dZG dd dZdddZddd	ZG d
d dZdd Z	e
dkrne	  dS )    N)runrunctxProfilec                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
_Utilsc                 C   s
   || _ d S N)profiler)selfr    r	   /usr/lib/python3.9/profile.py__init__0   s    z_Utils.__init__c                 C   sR   |   }z4z|| W n ty*   Y n0 W | ||| n| ||| 0 d S r   )r   r   
SystemExit_show)r   	statementfilenamesortprofr	   r	   r
   r   3   s    z
_Utils.runc                 C   sV   |   }z8z|||| W n ty.   Y n0 W | ||| n| ||| 0 d S r   )r   r   r   r   )r   r   globalslocalsr   r   r   r	   r	   r
   r   <   s    z_Utils.runctxc                 C   s"   |d ur| | n
|| d S r   )
dump_statsprint_stats)r   r   r   r   r	   r	   r
   r   E   s    z_Utils._showN)__name__
__module____qualname__r   r   r   r   r	   r	   r	   r
   r   *   s   		r   c                 C   s   t t| ||S r   )r   r   r   )r   r   r   r	   r	   r
   r   Q   s    r   c                 C   s   t t| ||||S r   )r   r   r   )r   r   r   r   r   r	   r	   r
   r   ^   s    r   c                   @   s   e Zd ZdZd3ddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Ze
e	eeeedZdd ZG dd dZG dd dZdd Zdd Zd4d!d"Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd5d/d0Zd1d2 ZdS )6r   r   Nc                 C   s   i | _ d | _d| _d| _|d u r&| j}|| _|sHtj | _| _| j	| _
nj|| _|  }zt|}W n  ty   || _| j	| _
Y n00 |dkr| j| _
n| j| _
|tfdd}|| _|  | _| d d S )N    c                 S   s
   ||  S r   r	   )timersumr	   r	   r
   get_time_timer   s    z(Profile.__init__.<locals>.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)r   r   r#   r-   lengthr   r	   r	   r
   r      s0    


zProfile.__init__c                 C   s   | j }| }|d |d  | j | j }|dkr8|j| _| j| | ||rd| }|d |d  | _n| }|d |d  | | _d S )Nr      c_callr   r-   r#   r   r"   dispatch)r   frameeventargr   r-   rr	   r	   r
   r+      s    zProfile.trace_dispatchc                 C   sT   | j }| | j | j }|dkr(|j| _| j| | ||rD| | _n| | | _d S Nr1   r2   r   r4   r5   r6   r   r-   r	   r	   r
   r'      s    
zProfile.trace_dispatch_ic                 C   s`   | j }| d | j | j }|dkr,|j| _| j| | ||rL| d | _n| d | | _d S )Ng      N@r1   r2   r9   r	   r	   r
   trace_dispatch_mac   s    zProfile.trace_dispatch_macc                 C   sT   | j }| | j | j }|dkr(|j| _| j| | ||rD| | _n| | | _d S r8   )r&   r-   r#   r   r"   r3   )r   r4   r5   r6   r&   r-   r	   r	   r
   r,      s    
zProfile.trace_dispatch_lc           	      C   sD   | j \}}}}}}||ur*|r*| ||S ||| ||||f| _ dS Nr0   )r    trace_dispatch_return)	r   r4   r-   rptritretrfnrframercurr	   r	   r
   trace_dispatch_exception   s
    z Profile.trace_dispatch_exceptionc                 C   s   | j r@|j| j d ur@| j \}}}}}}t|tjs@| |d |j}	|	j|	j|	j	f}
|dd|
|| j f| _ | j
}|
|v r||
 \}}}}}||d |||f||
< nddddi f||
< dS Nr   r0   )r    f_back
isinstancer   
fake_framer<   f_codeco_filenameco_firstlinenoco_namer   )r   r4   r-   r=   r>   r?   r@   rA   rB   Zfcodefnr   ccnsttctcallersr	   r	   r
   trace_dispatch_call  s    zProfile.trace_dispatch_callc           
      C   sn   dd| j f}|dd||| jf| _| j}||v rX|| \}}}}}	||d |||	f||< nddddi f||< dS )Nr   r   r0   )r"   r    r   )
r   r4   r-   rM   r   rN   rO   rP   rQ   rR   r	   r	   r
   trace_dispatch_c_call  s    zProfile.trace_dispatch_c_callc                 C   s   || j d ur | | j d d | j \}}}}}}|| }|| }|\}	}
}}}}|	|
| || |||f| _ | j}|| \}}}}}|s|| }|d }||v r|| d ||< nd||< ||d || ||f||< dS rD   )r    r<   r   )r   r4   r-   r=   r>   r?   r@   rB   Zframe_totalZpptZpitZpetZpfnpframeZpcurr   rN   rO   rP   rQ   rR   r	   r	   r
   r<   "  s"    zProfile.trace_dispatch_return)call	exceptionreturnr1   Zc_exceptionZc_returnc                 C   s"   | j d rd S || _| | d S )Nr   )r    r!   r.   )r   r!   r	   r	   r
   set_cmdX  s    zProfile.set_cmdc                   @   s   e Zd Zdd Zdd ZdS )zProfile.fake_codec                 C   s   || _ || _|| _d| _d S Nr   )rJ   co_linerL   rK   )r   r   linenamer	   r	   r
   r   ^  s    zProfile.fake_code.__init__c                 C   s   t | j| j| jfS r   )reprrJ   r[   rL   r   r	   r	   r
   __repr__d  s    zProfile.fake_code.__repr__N)r   r   r   r   r`   r	   r	   r	   r
   	fake_code]  s   ra   c                   @   s   e Zd Zdd ZdS )zProfile.fake_framec                 C   s   || _ || _d S r   )rI   rF   )r   codeZpriorr	   r	   r
   r   h  s    zProfile.fake_frame.__init__N)r   r   r   r   r	   r	   r	   r
   rH   g  s   rH   c                 C   sF   |  dd|}| jr | jd }nd }| ||}| jd | |d d S )NZprofiler   rE   rV   )ra   r    rH   r3   )r   r]   rb   rU   r4   r	   r	   r
   r.   l  s    zProfile.simulate_callc                 C   sJ   | j }| | j }| jd r:| jd | | jd | d}q| | | _d S )Nr   rX   rE   r   )r&   r-   r    r3   )r   r&   r-   r	   r	   r
   simulate_cmd_completex  s    
zProfile.simulate_cmd_completer   c                 C   s$   dd l }||  |  d S rZ   )pstatsZStatsZ
strip_dirsZ
sort_statsr   )r   r   rd   r	   r	   r
   r     s    zProfile.print_statsc                 C   sD   t |d&}|   t| j| W d    n1 s60    Y  d S )Nwb)opencreate_statsmarshaldumpstats)r   filefr	   r	   r
   r     s    zProfile.dump_statsc                 C   s   |    |   d S r   )rc   snapshot_statsr_   r	   r	   r
   rg     s    zProfile.create_statsc           	      C   s^   i | _ | j D ]H\}\}}}}}| }d}| D ]}||7 }q6|||||f| j |< qd S rZ   )rj   r   itemscopyvalues)	r   funcrN   rO   rP   rQ   rR   ZncZcallcntr	   r	   r
   rm     s    
zProfile.snapshot_statsc                 C   s   dd l }|j}| |||S rZ   )__main____dict__r   )r   r!   rr   dictr	   r	   r
   r     s    zProfile.runc              	   C   sB   |  | t| j zt||| W td  ntd  0 | S r   )rY   sys
setprofiler(   exec)r   r!   r   r   r	   r	   r
   r     s    
zProfile.runctxc             	   O   sF   |  t| t| j z||i |W td  S td  0 d S r   )rY   r^   ru   rv   r(   )r   rq   argskwr	   r	   r
   runcall  s    
zProfile.runcallc                 C   s@   | j turtd| j}d| _z| ||W || _S || _0 d S )Nz&Subclasses must override .calibrate().r   )	__class__r   r*   r#   _calibrate_inner)r   mverboseZ
saved_biasr	   r	   r
   	calibrate  s    
zProfile.calibratec                 C   s  | j }dd }|fdd}|| | }|| | }|| }|rLtd| t }	| }|	dt t  | }|| }
|rtd|
 d}d}|	j D ]0\\}}}\}}}}}|d	v r||7 }||7 }q|rtd
| td| ||d krtd| || d | }|rtd| |S )Nc                 S   s   t | D ]}d}qd S r;   range)nixr	   r	   r
   f1  s    z$Profile._calibrate_inner.<locals>.f1c                 S   s   t | D ]}|d qd S )Nd   r   )r}   r   r   r	   r	   r
   rl     s    z#Profile._calibrate_inner.<locals>.fz elapsed time without profiling =zf(m)zelapsed time with profiling =g        )rl   r   z!'CPU seconds' profiler reported =ztotal # calls =r0   z internal error: total calls = %dg       @z+mean stopwatch overhead per profile event =)	r&   printr   r   r   r   r   rn   
ValueError)r   r}   r~   r&   r   rl   t0t1Zelapsed_noprofilepZelapsed_profileZtotal_callsZreported_timer   r\   funcnamerN   rO   rP   rQ   rR   meanr	   r	   r
   r|     sB    





zProfile._calibrate_inner)NN)r   )r   )r   r   r   r#   r   r+   r'   r:   r,   rC   rS   rT   r<   r3   rY   ra   rH   r.   rc   r   r   rg   rm   r   r   rz   r   r|   r	   r	   r	   r
   r   g   s>   '
''


1
r   c               
   C   s  dd l } ddlm} d}||d}d|_|jdddd	d d
 |jdddddd |jdddddd
 tjdd  s|  td |	 \}}|tjd d < |j
d ur| j|j
|_
t|dkr|jrdd l}d}|j|d d}nh|d }	tjd| j|	 t|	 }
t|
 |	d}W d    n1 s:0    Y  |	dd d d}zt||d |j
|j W n8 ty } zd t_t|j W Y d }~n
d }~0 0 n|  |S )Nr   )OptionParserzMprofile.py [-o output_file_path] [-s sort] [-m module | scriptfile] [arg] ...)usageFz-oz	--outfileoutfilezSave stats to <outfile>)desthelpdefaultz-mmodule
store_truezProfile a library module.)r   actionr   r   z-sz--sortr   z?Sort order when printing to stdout, based on pstats.Stats classr   r0   r   z(run_module(modname, run_name='__main__'))
run_modulemodnamerw   rr   )__file__r   __package__
__cached__)osZoptparser   Zallow_interspersed_argsZ
add_optionru   argvprint_usageexit
parse_argsr   pathabspathr)   r   runpyr   insertdirnameio	open_codecompilereadr   r   BrokenPipeErrorstdouterrno)r   r   r   parseroptionsrx   r   rb   ZglobsZprognamefpexcr	   r	   r
   main)  sZ    





0$r   rr   )Nr   )Nr   )r   ru   r$   rh   __all__r   r   r   r   r   r   r	   r	   r	   r
   <module>   s   
'

	   E9