o
    `۷i3                     @   sj   U d dl Z d dlmZ d dlZd dlm  mZ d dlm  m	Z	 e 
eZdZeed< G dd dZdS )    N)Optionali'  _CGROUP_CPU_MAX_WEIGHTc                   @   s   e Zd ZdZ					ddedee dee dee dee f
d	d
Z	defddZ
defddZedee defddZedee defddZedee dedefddZdS )ResourceIsolationConfigar  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.

    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_memoryobject_store_memoryc                 C   s   || _ || _|| _d| _d| _|s'| jrtd|rtd| jr%tddS |du r/td| || _| ||| _| || _dS )a  
        Raises:
            ValueError: On invalid inputs.

        Args:
            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_memory + object_store_memory < the total memory available.
            object_store_memory: The amount of memory in bytes reserved for the object store.
                Must be not None when resource isolation is enabled.
         Nzcgroup_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.zobject_store_memory must be resolved before creating a ResourceIsolationConfig when resource isolation is enabled. This is likely a bug in Ray, please report it at https://github.com/ray-project/ray/issues/new/choose.)	_resource_isolation_enabledr   r   system_pidssystem_reserved_cpu_weight
ValueError%_validate_and_get_system_reserved_cpu(_validate_and_get_system_reserved_memory_validate_and_get_cgroup_path)selfr   r   r   r   r	    r   \/home/ubuntu/vllm_env/lib/python3.10/site-packages/ray/_private/resource_isolation_config.py__init__$   s<   z ResourceIsolationConfig.__init__returnc                 C   s   | j S )N)r   )r   r   r   r   
is_enabledm   s   z"ResourceIsolationConfig.is_enabledr   c                 C   s
   || _ dS )z>A comma-separated list of pids to move into the system cgroup.N)r   )r   r   r   r   r   add_system_pidsp   s   
z'ResourceIsolationConfig.add_system_pidsc                 C   s(   | 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   r   r   r   r   t   s   

z5ResourceIsolationConfig._validate_and_get_cgroup_pathc                 C   s   t jdd}|tjk rtd| dtj | s'ttttjtj| tj	} t
| ts9t
| ts9td|  dt| } | tjk rMtd|  dtj | |kr\td|  d	| d
t| t| t 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.)utils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_cpusr   r   r   r      sX   




z=ResourceIsolationConfig._validate_and_get_system_reserved_cpuc                 C   s   t jj }|tjk rtd| dtj | s'ttt	tjtj
| tj} t| ts4td|  d| tjk rDtd|  dtj | | }||krWt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.
            object_store_memory: The amount of memory in bytes reserved for the object store.

        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 z+The total requested system_reserved_memory=z0 is greater than the amount of memory available=.)ray_commonr   get_system_memoryr   (DEFAULT_MIN_SYSTEM_RESERVED_MEMORY_BYTESr   r'   r#   r$   )DEFAULT_SYSTEM_RESERVED_MEMORY_PROPORTION(DEFAULT_MAX_SYSTEM_RESERVED_MEMORY_BYTESr   )r   r	   available_system_memorytotal_system_reserved_memoryr   r   r   r      sR   

z@ResourceIsolationConfig._validate_and_get_system_reserved_memory)FNNNN)__name__
__module____qualname____doc__boolr   r   r"   r'   r   r   r   staticmethodr   r   r   r   r   r   r   r      sH    
IRr   )loggingtypingr   ray._common.utilsr*   ray._private.ray_constants_privater   ray._private.utilsr   	getLoggerr2   loggerr   r'   __annotations__r   r   r   r   r   <module>   s   
 
