o
    b²“iê+  ã                   @   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 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fdd„ƒZdS )ÚResourceIsolationConfiga‚  Configuration for enabling resource isolation by reserving memory
    and cpu for ray system processes through cgroupv2.
    This class 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.
    FNÚenable_resource_isolationÚcgroup_pathÚsystem_reserved_cpuÚsystem_reserved_memoryc                 C   sv   || _ || _|| _d | _d| _|s'|rtdƒ‚| jrtdƒ‚| jr%tdƒ‚d S |  |¡| _|  |¡| _|  |¡| _d S )NFzÑsystem_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.zÔsystem_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.zÉcgroup_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.)	Ú_resource_isolation_enabledr   r   Ú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   © r   úZ/home/ubuntu/.local/lib/python3.10/site-packages/ray/_private/resource_isolation_config.pyÚ__init__'   s4   ÿÿÿÿÿz ResourceIsolationConfig.__init__Úreturnc                 C   s   | j S )N)r	   )r   r   r   r   Ú
is_enabled\   s   z"ResourceIsolationConfig.is_enabledÚobject_store_memoryc                 C   sd   |   ¡ sJ dƒ‚| jrJ dƒ‚|  j|7  _tjj ¡ }| j|kr-td| j› d|› dƒ‚d| _dS )zwThis is only supposed to be called once. It also cannot be
        called if resouce isolation is not enabled.
        zaCannot add object_store_memory to system_reserved_memory when enable_resource_isolation is False.zLCannot add object_store_memory to system_reserved_memory whenmultiple times.ú+The total requested system_reserved_memory=zj, 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_memoryr   r   r   Úadd_object_store_memory_   s   
ÿÿ

þÿ
z/ResourceIsolationConfig.add_object_store_memoryc                 C   s(   | st j} t| tƒstd| › dƒ‚| S )a‹  Returns the ray_constants.DEFAULT_CGROUP_PATH if cgroup_path is not
        specified. Checks the type of cgroup_path.

        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   v   s   

ÿz5ResourceIsolationConfig._validate_and_get_cgroup_pathc                 C   sœ   t  ¡ }| sttjtj| ƒ} t| tƒs!t| tƒs!t	d| › dƒ‚t| ƒ} | tj
k r5t	d| › dtj
› ƒ‚| |krDt	d| › d|› dƒ‚t| t|ƒ t ƒS )ak  If system_reserved_cpu is not specified, returns the default value. Otherwise,
        checks the type, makes sure 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.

        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.
        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 the number of cpus available=zI. Pick a smaller number of cpu cores to reserve for ray system processes.)r   Úget_num_cpusÚminr   Ú!DEFAULT_SYSTEM_RESERVED_CPU_CORESÚ&DEFAULT_SYSTEM_RESERVED_CPU_PROPORTIONr!   ÚfloatÚintr   Ú!MINIMUM_SYSTEM_RESERVED_CPU_CORESr   )r   Úavailable_system_cpusr   r   r   r   ‘   sB   ÿþÿþ
ÿ
ýÿÿÿ

ÿÿz=ResourceIsolationConfig._validate_and_get_system_reserved_cpuc                 C   s‚   t jj ¡ }| stttjtj| ƒƒ} t	| tƒs t
d| › dƒ‚| tjk r0t
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.
        r   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(   r$   r   Ú$DEFAULT_SYSTEM_RESERVED_MEMORY_BYTESÚ)DEFAULT_SYSTEM_RESERVED_MEMORY_PROPORTIONr!   r   Ú$MINIMUM_SYSTEM_RESERVED_MEMORY_BYTES)r   r   r   r   r   r   Î   s6   ÿþÿ

ÿ
ýÿÿÿz@ResourceIsolationConfig._validate_and_get_system_reserved_memory)FNNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úboolr   r"   r'   r(   r   r   r   Ústaticmethodr   r   r   r   r   r   r   r      s>    ûþýü
û5ÿþ<ÿþr   )ÚloggingÚtypingr   Úray._common.utilsr   Úray._private.ray_constantsÚ_privater   Úray._private.utilsr   Ú	getLoggerr/   Úloggerr   r(   Ú__annotations__r   r   r   r   r   Ú<module>   s   
 
