B
    u9aW                 @   sl   d dl Z d dlZd dlZdddg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rhe  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.7/profile.py__init__/   s    z_Utils.__init__c             C   sF   |   }z(y|| W n tk
r,   Y nX W d | ||| X d S )N)r   r   
SystemExit_show)r   	statementfilenamesortprofr   r   r	   r   2   s    
z
_Utils.runc             C   sJ   |   }z,y|||| W n tk
r0   Y nX W d | ||| X d S )N)r   r   r   r   )r   r   globalslocalsr   r   r   r   r   r	   r   ;   s    
z_Utils.runctxc             C   s"   |d k	r| | n
|| d S )N)
dump_statsprint_stats)r   r   r   r   r   r   r	   r   D   s    z_Utils._showN)__name__
__module____qualname__r
   r   r   r   r   r   r   r	   r   )   s   		r   c             C   s   t t| ||S )N)r   r   r   )r   r   r   r   r   r	   r   P   s    c             C   s   t t| ||||S )N)r   r   r   )r   r   r   r   r   r   r   r	   r   ]   s    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 kr&| j}|| _|sHtj | _| _| j	| _
nl|| _|  }yt|}W n" tk
r   || _| j	| _
Y n0X |dkr| j| _
n| j| _
|tfdd}|| _|  | _| d d S )N    c             S   s
   ||  S )Nr   )timersumr   r   r	   get_time_timer   s    z(Profile.__init__.<locals>.get_time_timerr   )timingscurcmdc_func_namebiastimeZ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 )Nr/   )r   r+   r"   r   r!   r0   )r   r1   r2   r3   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@r/   )r   r+   r"   r   r!   r0   )r   r1   r2   r3   r   r+   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 )Nr/   )r$   r+   r"   r   r!   r0   )r   r1   r2   r3   r$   r+   r   r   r	   r*      s    
zProfile.trace_dispatch_lc       	      C   sD   | j \}}}}}}||k	r*|r*| ||S ||| ||||f| _ dS )Nr.   )r   trace_dispatch_return)	r   r1   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 k	r@| j \}}}}}}t|tjs@| |d |j}	|	j|	j|	j	f}
|dd|
|| j f| _ | j
}|
|kr||
 \}}}}}||d |||f||
< nddddi f||
< dS )Nr   r.   )r   f_back
isinstancer   
fake_framer6   f_codeco_filenameco_firstlinenoco_namer   )r   r1   r+   r7   r8   r9   r:   r;   r<   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}||krX|| \}}}}}	||d |||	f||< nddddi f||< dS )Nr   r   r.   )r!   r   r   )
r   r1   r+   rF   r   rG   rH   rI   rJ   rK   r   r   r	   trace_dispatch_c_call  s    zProfile.trace_dispatch_c_callc             C   s   || j d k	r | | j d d | j \}}}}}}|| }|| }|\}	}
}}}}|	|
| || |||f| _ | j}|| \}}}}}|s|| }|d }||kr|| d ||< nd||< ||d || ||f||< dS )Nr>   r   r.   )r   r6   r   )r   r1   r+   r7   r8   r9   r:   r<   Zframe_totalZpptZpitZpetZpfnpframeZpcurr   rG   rH   rI   rJ   rK   r   r   r	   r6   !  s"    zProfile.trace_dispatch_return)callZ	exceptionreturnr/   Zc_exceptionZc_returnc             C   s"   | j d rd S || _| | d S )Nr   )r   r    r,   )r   r    r   r   r	   set_cmdW  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   )rC   co_linerE   rD   )r   r   linenamer   r   r	   r
   ]  s    zProfile.fake_code.__init__c             C   s   t | j| j| jfS )N)reprrC   rR   rE   )r   r   r   r	   __repr__c  s    zProfile.fake_code.__repr__N)r   r   r   r
   rV   r   r   r   r	   	fake_code\  s   rW   c               @   s   e Zd Zdd ZdS )zProfile.fake_framec             C   s   || _ || _d S )N)rB   r?   )r   codeZpriorr   r   r	   r
   g  s    zProfile.fake_frame.__init__N)r   r   r   r
   r   r   r   r	   rA   f  s   rA   c             C   sF   |  dd|}| jr | jd }nd }| ||}| jd | |d d S )NZprofiler   r>   rO   )rW   r   rA   r0   )r   rT   rX   rN   r1   r   r   r	   r,   k  s    zProfile.simulate_callc             C   sN   | j }| | j }x*| jd r<| jd | | jd | d}qW | | | _d S )Nr   rP   r>   r   )r$   r+   r   r0   )r   r$   r+   r   r   r	   simulate_cmd_completew  s    zProfile.simulate_cmd_completer   c             C   s$   dd l }||  |  d S )Nr   )pstatsZStatsZ
strip_dirsZ
sort_statsr   )r   r   rZ   r   r   r	   r     s    zProfile.print_statsc          	   C   s0   t |d}|   t| j| W d Q R X 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 )N)rY   snapshot_stats)r   r   r   r	   r]     s    zProfile.create_statsc       	      C   sf   i | _ xZ| j D ]L\}\}}}}}| }d}x| D ]}||7 }q:W |||||f| j |< qW d S )Nr   )r`   r   itemscopyvalues)	r   funcrG   rH   rI   rJ   rK   ZncZcallcntr   r   r	   rc     s    zProfile.snapshot_statsc             C   s   dd l }|j}| |||S )Nr   )__main____dict__r   )r   r    rh   dictr   r   r	   r     s    zProfile.runc          	   C   s8   |  | t| j zt||| W d td  X | S )N)rQ   sys
setprofiler&   exec)r   r    r   r   r   r   r	   r     s    
zProfile.runctxc           	   O   s   t | dkr| ^}}} n>| s&tdn0d|krB|d}| ^}} ntdt | d  |t| t|j z
|| |S td  X d S )Nr   z:descriptor 'runcall' of 'Profile' object needs an argumentrg   z7runcall expected at least 1 positional argument, got %dr.   )r'   r(   poprQ   rU   rk   rl   r&   )argskwr   rg   r   r   r	   runcall  s    



zProfile.runcallc             C   s8   | j tk	rtd| j}d| _z| ||S || _X 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}x>|	j D ]0\\}}}\}}}}}|d	kr||7 }||7 }qW |rtd
| td| ||d krtd| || d | }|rtd| |S )Nc             S   s   xt | D ]}d}q
W d S )Nr.   )range)nixr   r   r	   f1  s    z$Profile._calibrate_inner.<locals>.f1c             S   s   xt | D ]}|d q
W d S )Nd   )rw   )rt   r{   ry   r   r   r	   rb     s    z#Profile._calibrate_inner.<locals>.fz elapsed time without profiling =zf(m)zelapsed time with profiling =g        )rb   r{   z!'CPU seconds' profiler reported =ztotal # calls =r.   z internal error: total calls = %dg       @z+mean stopwatch overhead per profile event =)	r$   printr   r   r   r   r   rd   
ValueError)r   rt   ru   r$   r{   rb   Zt0Zt1Zelapsed_noprofilepZelapsed_profileZtotal_callsZreported_timer   rS   funcnamerG   rH   rI   rJ   rK   Zmeanr   r   r	   rs     sB    

"


zProfile._calibrate_inner)NN)r   )r   )r   r   r   r"   r
   r)   r%   r5   r*   r=   rL   rM   r6   r0   rQ   rW   rA   r,   rY   r   r   r]   rc   r   r   rq   rv   rs   r   r   r   r	   r   f   s<   '
''



=
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
 tjdd  sp|  td |	 \}}|tjd d < t
|dkr|d }tjd| j| t|d}t| |d}W d Q R X |dd d d}	t||	d |j|j n|  |S )Nr   )OptionParserz?profile.py [-o output_file_path] [-s sort] scriptfile [arg] ...)usageFz-oz	--outfileoutfilezSave stats to <outfile>)desthelpdefaultz-sz--sortr   z?Sort order when printing to stdout, based on pstats.Stats classr   r.   r   rbrm   rh   )__file__r   __package__
__cached__)osZoptparser   Zallow_interspersed_argsZ
add_optionrk   argvprint_usageexit
parse_argsr'   pathinsertdirnamer\   compilereadr   r   r   )
r   r   r   parseroptionsro   ZprognamefprX   Zglobsr   r   r	   main4  s6    




r   rh   )Nr   )Nr   )
rk   r#   r^   __all__r   r   r   r   r   r   r   r   r   r	   <module>   s   

'

	   Q%