a
    kh                     @   s   d dl Z d dlZd dlZd dlZddlmZmZmZ ddlm	Z	 d dl
Z
dd Zdd Zdd	d
ZdddZdd Zdd Zdd Zedkre  dS )    N   )startfinalize_select_backend)set_command_linec                  C   s   t jddt jd} | jddtdd | jdd	td
d g dd | jddtddddgd | jddtdddgd | jddtdd d dgd | jddtdd d dgd | jdt jdd  |  }||jfS )!NzBThe proton command utility for profiling scripts and pytest tests.z
    proton [options] script.py [script_args] [script_options]
    proton [options] pytest [pytest_args] [script_options]
    python -m triton.profiler.proton [options] script.py [script_args] [script_options]
)descriptionusageformatter_classz-nz--namezName of the profiling session)typehelpz-bz	--backendzProfiling backend)ZcuptiZcupti_pcsamplingZ	roctracer)r
   r   defaultchoicesz-cz	--contextzProfiling contextZshadowpythonz-dz--datazProfiling datatreez-kz--hookzProfiling hooktritonz-iz--instrumentzInstrumentation analysis typeprint-mem-spacestarget_argszSubcommand and its arguments)nargsr   )argparseArgumentParserRawTextHelpFormatteradd_argumentstr	REMAINDER
parse_argsr   )parserargs r   D/var/www/auris/lib/python3.9/site-packages/triton/profiler/proton.pyparse_arguments
   s&    r   c                 C   s   t j| dkS )Npytest)ospathbasename)scriptr   r   r   	is_pytest   s    r%   c              
   C   s,  t j| }ddd|dttjti}tj}| g| t_tjt j| t jt	j
}|dkrttt|dd }dt jd< d	t jd
< |t jd< zzHt|d }t| |d}	W d    n1 s0    Y  t|	| W n4 ty }
 ztd|
  W Y d }
~
n
d }
~
0 0 W |t_n|t_0 d S )N__name____main____file____builtins__r   zlibPrintLoadStoreMemSpaces.so1ZTRITON_ALWAYS_COMPILE0ZTRITON_DISABLE_LINE_INFOZLLVM_PASS_PLUGIN_PATHrbexecz.An error occurred while executing the script: )r!   r"   abspathr)   sysr&   argvappenddirnamer   r(   r   nextpathlibPathrglobenvironopencompilereadr-   	Exceptionprint)r$   r   instrumentation_passZscript_pathZclean_globalsZoriginal_argvZtop_level_triton_pathZinstrumentation_pass_pathfilecodeer   r   r   execute_as_main#   s0    


.&rA   c                 C   sV   t   | d }t| dkr&| dd  ng }t|rFdd l}|| nt||| d S )Nr   r   )r   lenr%   r    mainrA   )r   r=   r$   script_argsr    r   r   r   do_setup_and_executeE   s    rE   c                 C   s>   | j r| j nt }t| j| j| j|| jd t| t  d S )N)contextdatabackendhook)	rH   r   r   namerF   rG   rI   rE   r   )r   r   rH   r   r   r   run_profilingR   s    rK   c                 C   s   t || j d S N)rE   
instrumentr   r   r   r   r   run_instrumentation\   s    rO   c                  C   s,   t  \} }| jrt| | d S t| | d S rL   )r   rM   rO   rK   rN   r   r   r   rC   `   s
    

rC   r'   )N)N)r   r/   r!   r4   Zprofiler   r   r   flagsr   r   r   r%   rA   rE   rK   rO   rC   r&   r   r   r   r   <module>   s   
"

