o
    biU                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dlm	Z	m
Z
mZmZ d dlmZ d dlmZ d dlmZ d dlmZ eeZdd	d
ddidZde	eef dee
e e
e f fddZG dd deZdS )    N)Path)DictListOptionalTuple)try_to_create_directory)RuntimeEnvContext)RuntimeEnvPlugin)RuntimeEnvSetupErrorfalsezworker_process_%p)ROCPROFSYS_TIME_OUTPUTROCPROFSYS_OUTPUT_PREFIXFtrue)envargsrocprof_sys_configreturnc                 C   s   dg}i }d| v r/| d   D ]\}}t|dkr%|d| d|  q|d| |g7 }qd| v r7| d }|d ||fS )z
    Function to convert dictionary of rocprof-sys options into
    rocprof-sys-python command line

    The function returns:
    - List[str]: rocprof-sys-python cmd line split into list of str
    zrocprof-sys-pythonr      z--=-r   )itemslenappend)r   rocprof_sys_cmdrocprof_sys_envoption
option_val r   X/home/ubuntu/.local/lib/python3.10/site-packages/ray/_private/runtime_env/rocprof_sys.pyparse_rocprof_sys_config   s   

r    c                   @   s   e Zd ZdZdefddZdeeef deeef fddZ	e
fd	ee d
ddedejdef
ddZe
fdee d
ddedeej fddZdS )RocProfSysPlugin_rocprof_sysresources_dirc                 C   s<   g | _ i | _tj|\}}t|d d | _t| j d S )Nlogsrocprof_sys)r   r   ospathsplitr   _rocprof_sys_dirr   )selfr#   session_dirruntime_dirr   r   r   __init__=   s
   zRocProfSysPlugin.__init__r   r   c                    sD  t t| jd }t|\}}t|}||d< |tj t	| t
| dd}|d W d   n1 s;w   Y  zW|| dg }tj||tjtjdI dH }| I dH \}}	|	 dkrk|	 n| }
d	d
|g}tj|tjtjdI dH }| I dH \}}|jdkrW dS d|
fW S  ty   Y dS w )a{  
        Function to validate if rocprof_sys_config is a valid rocprof_sys profile options
        Args:
            rocprof_sys_config: dictionary mapping rocprof_sys option to it's value
        Returns:
            a tuple consists of a boolean indicating if the rocprof_sys_config
            is valid option and an error message if the rocprof_sys_config is invalid
        testROCPROFSYS_OUTPUT_PATHz/test.pywzimport time
N)r   stdoutstderr rmz-r)r1   r2   r   )TNF)Fzrocprof_sys is not installed)strr   r)   r    copydeepcopyupdater&   environr   openwriteasynciocreate_subprocess_exec
subprocessPIPEcommunicatestrip
returncodeFileNotFoundError)r*   r   test_folderr   r   rocprof_sys_env_copyfprocessr1   r2   	error_msgclean_up_cmdcleanup_process_r   r   r   _check_rocprof_sys_scriptF   sB   



z*RocProfSysPlugin._check_rocprof_sys_scripturiruntime_env
RuntimeEnvcontextloggerc                    s   |  }|s	dS |rtjdkrtdt|tr(|dkr t}ntd| d| |I d H \}}|s@|| td| d|vrHi |d< tt	| j
|d d	< t|\| _| _dS )
Nr   linuxz,rocprof-sys CLI is only available in Linux.
defaultz Unsupported rocprof_sys config: zH. The supported config is 'default' or Dictionary of rocprof_sys optionszErocprof-sys profile failed to run with the following error message:
 r   r/   )r%   sysplatformr
   
isinstancer5   ROCPROFSYS_DEFAULT_CONFIGrL   warningr   r)   r    r   r   )r*   rM   rN   rP   rQ   r   is_valid_rocprof_sys_configrH   r   r   r   createw   s>   


zRocProfSysPlugin.createurisc                 C   s*   | d d| j|_|j| j d S )NzRunning rocprof-sys profiler )infojoinr   py_executableenv_varsr8   r   )r*   r[   rN   rP   rQ   r   r   r   modify_context   s   
zRocProfSysPlugin.modify_contextN)__name__
__module____qualname__namer5   r-   r   r   boolrL   default_loggerr   r   loggingLoggerintrZ   r   ra   r   r   r   r   r!   :   s<    	


6
2r!   )r<   r6   rh   r&   r>   rT   pathlibr   typingr   r   r   r   ray._common.utilsr    ray._private.runtime_env.contextr   ray._private.runtime_env.pluginr	   ray.exceptionsr
   	getLoggerrb   rg   rW   r5   r    r!   r   r   r   r   <module>   s2    


