
    &`iU                     "   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j        e          Zdd	d
ddidZde	eef         dee
e         e
e         f         fdZ G 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                    dg}i }d| v rY| d                                          D ]>\  }}t          |          dk    r|                    d| d|            4|d| |gz  }?d| v r| 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_vals        x/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/_private/runtime_env/rocprof_sys.pyparse_rocprof_sys_configr!      s     ,,OO###"4V"<"B"B"D"D 	> 	>FJ 6{{Q&&'AF'A'AZ'A'ABBBBLLL*#==""",U34   O++    c                       e Zd ZdZdefdZdeeef         deeef         fdZ	e
fdee         dd	d
edej        def
dZe
fdee         dd	d
edeej                 fdZdS )RocProfSysPlugin_rocprof_sysresources_dirc                     g | _         i | _        t          j                            |          \  }}t          |          dz  dz  | _        t          | j                   d S )Nlogsrocprof_sys)r   r   ospathsplitr   _rocprof_sys_dirr   )selfr&   session_dirruntime_dirs       r    __init__zRocProfSysPlugin.__init__=   s]    !! $&7==#?#? [ $[ 1 1F :] J 566666r"   r   r   c                   K   t          t          | j                  dz            }t          |          \  }}t	          j        |          }||d<   |                    t          j                   t          |           t          | dd          5 }|                    d           ddd           n# 1 swxY w Y   	 || dgz   }t          j        ||t          j        t          j        d d{V }|                                 d{V \  }}	|	                                dk    r|	                                n|                                }
d	d
|g}t          j        |t          j        t          j        d d{V }|                                 d{V \  }}|j        dk    rdS d|
fS # t&          $ r Y dS w xY 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)r6   r7   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processr6   r7   	error_msgclean_up_cmdcleanup_process_s                 r    _check_rocprof_sys_scriptz*RocProfSysPlugin._check_rocprof_sys_scriptF   sf      $t455>??+CDV+W+W(#}_==9D56##BJ///,,, [***C00 	%AGGO$$$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%	;-K1I1I1I0JJO#: (!!	        G $+#6#6#8#8888888NFF*0,,..B*>*>FLLNNI !$4L$+$B!!% % %      O
 )4466666666DAq!Q&&!zi''  	; 	; 	;:::	;s+   B55B9<B9C,F3 /F3 3
G Guriruntime_env
RuntimeEnvcontextloggerc                   K   |                                 }|sdS |rt          j        dk    rt          d          t	          |t
                    r!|dk    rt          }nt          d| d          |                     |           d {V \  }}|s'|                    |           t          d|           d|vri |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   r4   )r)   sysplatformr   
isinstancer:   ROCPROFSYS_DEFAULT_CONFIGrQ   warningr   r-   r!   r   r   )r.   rR   rS   rU   rV   r   is_valid_rocprof_sys_configrM   s           r    createzRocProfSysPlugin.createw   s      )4466! 	1 	X#,'"9"9&'VWWW(#.. 	!Y..%>""*87I 8 8 8   8<7U7U8
 8
 2
 2
 2
 2
 2
 2
.#Y + 	NN9%%%&0$-0 0  
 ***(*u%>A&''?
 ?
5!":; 6N6
 6
2d2 qr"   urisc                     |                     d           d                    | j                  |_        |j                            | j                   d S )NzRunning rocprof-sys profiler )infojoinr   py_executableenv_varsr=   r   )r.   ra   rS   rU   rV   s        r    modify_contextzRocProfSysPlugin.modify_context   sN     	2333 #)= > > 455555r"   N)__name__
__module____qualname__namer:   r1   r   r   boolrQ   default_loggerr   r	   loggingLoggerintr`   r   rh    r"   r    r$   r$   :   s       D7c 7 7 7 7/;"&sCx./;	tSy	/; /; /; /;l "0+ +c]+ "+ #	+
 + 
+ + + +d ,:	6 	63i	6 "	6 #		6
 (	6 	6 	6 	6 	6 	6r"   r$   )rA   r;   ro   r*   rC   rZ   pathlibr   typingr   r   r   r   ray._common.utilsr    ray._private.runtime_env.contextr	   ray._private.runtime_env.pluginr
   ray.exceptionsr   	getLoggerri   rn   r]   r:   r!   r$   rr   r"   r    <module>rz      sv      				     



       . . . . . . . . . . . . 5 5 5 5 5 5 > > > > > > < < < < < < / / / / / /""8,, #*$7 
 	V  ,S#X,
49d3i , , , ,6s6 s6 s6 s6 s6' s6 s6 s6 s6 s6r"   