
    &`iP5                         U d dl Z d dlmZ d dlZd dlmc mZ d dlmc m	Z	  e j
        e          ZdZeed<    G d d          ZdS )    N)Optionali'  _CGROUP_CPU_MAX_WEIGHTc            
           e Zd ZdZ	 	 	 	 ddedee         dee         dee         fdZ	d	efd
Z
defdZdefdZedee         d	efd            Zedee         d	efd            Zedee         d	efd            ZdS )ResourceIsolationConfiga  Configuration for enabling resource isolation by reserving memory and cpu for ray system processes through cgroupv2.

    Validates configuration for resource isolation by enforcing types, correct combinations of values, applying default values,
    and sanity checking cpu and memory reservations. Also, converts system_reserved_cpu into cpu.weights for cgroupv2.

    Raises:
        ValueError: On invalid inputs.

    Attributes:
        enable_resource_isolation: True if cgroupv2 based isolation of ray
            system processes is enabled.
        cgroup_path: The path for the cgroup the raylet should use to enforce
            resource isolation.
        system_reserved_cpu: The amount of cores reserved for ray system
            processes. Must be >= ray_constants.MINIMUM_SYSTEM_RESERVED_CPU_CORES
            and < the total number of cores available.
        system_reserved_memory: The amount of memory in bytes reserved
            for ray system processes. Must be >= ray_constants.MINIMUM_SYSTEM_RESERVED_MEMORY_BYTES
            and system_reserved_cpu + object_store_bytes < the total memory available.

    TODO(54703): Link documentation when it's available.
    FNenable_resource_isolationcgroup_pathsystem_reserved_cpusystem_reserved_memoryc                 x   || _         || _        || _        d| _        d | _        d| _        |s?| j        rt          d          |rt          d          | j        rt          d          d S |                     |          | _        |                     |          | _        | 	                    |          | _        d S )N Fzcgroup_path cannot be set when resource isolation is not enabled. Set enable_resource_isolation to True if you're using ray.init or use the --enable-resource-isolation flag if you're using the ray cli.zsystem_reserved_cpu cannot be set when resource isolation is not enabled. Set enable_resource_isolation to True if you're using ray.init or use the --enable-resource-isolation flag if you're using the ray cli.zsystem_reserved_memory cannot be set when resource isolation is not enabled. Set enable_resource_isolation to True if you're using ray.init or use the --enable-resource-isolation flag if you're using the ray cli.)
_resource_isolation_enabledr   r
   system_pidssystem_reserved_cpu_weight_constructed
ValueError%_validate_and_get_system_reserved_cpu(_validate_and_get_system_reserved_memory_validate_and_get_cgroup_path)selfr   r   r	   r
   s        z/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/_private/resource_isolation_config.py__init__z ResourceIsolationConfig.__init__'   s    ,E(&&<# 04'
 "( 	  T  
 #  T   *  T  
 F*.*T*T+
 +
' '+&S&S"'
 '
#  ==kJJ    returnc                     | j         S )N)r   )r   s    r   
is_enabledz"ResourceIsolationConfig.is_enabled[   s    //r   object_store_memory_bytesc                 $   |                                  s
J d            | j        r
J d            | xj        |z  c_        t          j        j                                        }| j        |k    rt          d| j         d| d          d| _        dS )aT  Adds object_store_memory to the memory reserved for system processes.

        Args:
            object_store_memory_bytes: The amount processes. Must be >= ray_constants.MINIMUM_SYSTEM_RESERVED_CPU_CORES
                and < the total number of cores available.

        Raises:
            AssertionError: If called with resource isolation not enabled or called more than once for the same instance.
            ValueError: If the input is not an integer or if the system_reserved_memory + object_store_memory is greater
                than the total memory available on the system.

        zaCannot add object_store_memory to system_reserved_memory when enable_resource_isolation is False.z|Cannot call add_object_store_memory more than once with an instance ResourceIsolationConfig. This is a bug in the ray code. +The total requested system_reserved_memory=zh, calculated by object_store_bytes + system_reserved_memory, is greater than the total memory available=zR. Pick a smaller number of bytes for object_store_bytes or system_reserved_memory.TN)r   r   r
   ray_commonutilsget_system_memoryr   )r   r   available_system_memorys      r   add_object_store_memoryz/ResourceIsolationConfig.add_object_store_memory^   s        	
 	
2	
 	
  $ 	
 	
G	
 	
$ 	##'@@##"%+"3"E"E"G"G&)@@@-d>Y - -4- - -   !r   r   c                     || _         dS )z>A comma-separated list of pids to move into the system cgroup.N)r   )r   r   s     r   add_system_pidsz'ResourceIsolationConfig.add_system_pids~   s    &r   c                 r    | st           j        } t          | t                    st	          d|  d          | S )ac  Returns the ray_constants.DEFAULT_CGROUP_PATH if cgroup_path is not specified.

        Args:
            cgroup_path: The path for the cgroup the raylet should use to enforce
                resource isolation.

        Returns:
            str: The validated cgroup path.

        Raises:
            ValueError: If cgroup_path is not a string.
        Invalid value=z| for cgroup_path. Use a string to represent the path for the cgroup that the raylet should use to enable resource isolation.)ray_constantsDEFAULT_CGROUP_PATH
isinstancestrr   )r   s    r   r   z5ResourceIsolationConfig._validate_and_get_cgroup_path   sV      	<';K+s++ 	0 0 0 0   r   c           	         t          j        d          }|t          j        k     rt	          d| dt          j                   | sLt          t          t          t          j        t          j        |z            t          j	                            } t          | t
                    s(t          | t                    st	          d|  d          t          |           } | t          j        k     rt	          d|  dt          j                   | |k    rt	          d|  d	| d
          t          | t          |          z  t          z            S )a  If system_reserved_cpu is specified, validates it, otherwise returns the default value.

        Validation entails checking the type, ensuring that the value is in range, and converts it
        into cpu.weights for cgroupv2. See https://docs.kernel.org/admin-guide/cgroup-v2.html#weights
        for more information.

        If system_reserved_cpu is not specified, returns a default value between
        [DEFAULT_MIN_SYSTEM_RESERVED_CPU_CORES, DEFAULT_MAX_SYSTEM_RESERVED_CPU_CORES].

        # TODO(54703): The errors from this method are user-facing and thus need
        to be linked the user-facing documentation once it's available.

        Args:
            system_reserved_cpu: The amount of cores reserved for ray system
                processes. Must be >= ray_constants.MINIMUM_SYSTEM_RESERVED_CPU_CORES
                and < the total number of cores available.

        Raises:
            ValueError: If system_reserved_cpu is specified, but invalid or if the system
                does not have enough available cpus.

        F)truncatez1The available number of cpu cores on this system z is less than the minimum amount that is required for ray's system processes. Pick a number of cpu cores greater than or equal to r(   z for system_reserved_cpu. Use a float to represent the number of cores that need to be reserved for ray system processes to enable resource isolation.z"The requested system_reserved_cpu=z is less than the minimum number of cpus that can be used for resource isolation. Pick a number of cpu cores to reserve for ray system processes greater than or equal to z: is greater than or equal to the number of cpus available=zI. Pick a smaller number of cpu cores to reserve for ray system processes.)r!   get_num_cpusr)   %DEFAULT_MIN_SYSTEM_RESERVED_CPU_CORESr   floatminmax&DEFAULT_SYSTEM_RESERVED_CPU_PROPORTION%DEFAULT_MAX_SYSTEM_RESERVED_CPU_CORESr+   intr   )r	   available_system_cpuss     r   r   z=ResourceIsolationConfig._validate_and_get_system_reserved_cpu   s   4 !& 2E B B B =#VVV}DY } }GTGz} }   # 
	"'%K%L/0 
 "G 	# 	# *E22	-s33	 E!4 E E E   $$788!TTTb5H b b -:,_b b   "777Z5H Z Z0EZ Z Z    5)>#?#??$%
 
 	
r   c           	      ,   t           j        j                                        }|t          j        k     rt          d| dt          j                   | sLt          t          t          t          j        t          j
        |z            t          j                            } t          | t                    st          d|  d          | t          j        k     rt          d|  dt          j                   | |k    rt          d|  d| d	          | S )
a>  If system_reserved_memory is not specified, returns the default value. Otherwise,
        checks the type, makes sure that the value is in range.

        Args:
            system_reserved_memory: The amount of memory in bytes reserved
                for ray system processes. Must be >= ray_constants.MINIMUM_SYSTEM_RESERVED_MEMORY_BYTES
                and < the total memory available.

        Returns:
            int: The validated system reserved memory in bytes.

        Raises:
            ValueError: If system_reserved_memory is specified, but invalid.
        z$The available memory on this system z~ is less than the minimum amount that is required for ray's system processes. Pick a number of bytes greater than or equal to zInvalid value z for system_reserved_memory. Use an integer to represent the number bytes that need to be reserved for ray system processes to enable resource isolation.z%The requested system_reserved_memory z is less than the minimum number of bytes that can be used for resource isolation. Pick a number of bytes to reserve for ray system processes greater than or equal to r   z0 is greater than the amount of memory available=.)r   r    r!   r"   r)   (DEFAULT_MIN_SYSTEM_RESERVED_MEMORY_BYTESr   r6   r2   r3   )DEFAULT_SYSTEM_RESERVED_MEMORY_PROPORTION(DEFAULT_MAX_SYSTEM_RESERVED_MEMORY_BYTESr+   )r
   r#   s     r   r   z@ResourceIsolationConfig._validate_and_get_system_reserved_memory   s   $ #&+"3"E"E"G"G $DE E |7N | |CPCy| |   & 
	%(%N%O12 
 "J 	& 	&" 0#66 	E!7 E E E   #DE E e8N e e -:,be e   "$;;;M>T M M2IM M M   &%r   )FNNN)__name__
__module____qualname____doc__boolr   r,   r1   r6   r   r   r$   r&   staticmethodr   r   r    r   r   r   r      s|        2 +0%)/3042K 2K#'2K c]2K &e_	2K
 !)2K 2K 2K 2Kh0D 0 0 0 0! ! ! ! !@'3 ' ' ' ' 8C= S    \2 P
%e_P
	P
 P
 P
 \P
d @& (@&	@& @& @& \@& @& @&r   r   )loggingtypingr   ray._common.utilsr   ray._private.ray_constants_privater)   ray._private.utilsr!   	getLoggerr=   loggerr   r6   __annotations__r   rC   r   r   <module>rM      s               2 2 2 2 2 2 2 2 2 " " " " " " " " "		8	$	$ $  # # #a& a& a& a& a& a& a& a& a& a&r   