< prev index next >

src/hotspot/os/linux/cgroupSubsystem_linux.hpp

Print this page
@  rev 56288 : 8230305: Cgroups v2: Container awareness
|  Reviewed-by: bobv
o  rev 56287 : 8230848: OSContainer: Refactor container detection code
|  Summary: Move cgroups v1 implementation details out of os_linux.cpp
~  Reviewed-by: bobv


  34 /*
  35  * PER_CPU_SHARES has been set to 1024 because CPU shares' quota
  36  * is commonly used in cloud frameworks like Kubernetes[1],
  37  * AWS[2] and Mesos[3] in a similar way. They spawn containers with
  38  * --cpu-shares option values scaled by PER_CPU_SHARES. Thus, we do
  39  * the inverse for determining the number of possible available
  40  * CPUs to the JVM inside a container. See JDK-8216366.
  41  *
  42  * [1] https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/#meaning-of-cpu
  43  *     In particular:
  44  *        When using Docker:
  45  *          The spec.containers[].resources.requests.cpu is converted to its core value, which is potentially
  46  *          fractional, and multiplied by 1024. The greater of this number or 2 is used as the value of the
  47  *          --cpu-shares flag in the docker run command.
  48  * [2] https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_ContainerDefinition.html
  49  * [3] https://github.com/apache/mesos/blob/3478e344fb77d931f6122980c6e94cd3913c441d/src/docker/docker.cpp#L648
  50  *     https://github.com/apache/mesos/blob/3478e344fb77d931f6122980c6e94cd3913c441d/src/slave/containerizer/mesos/isolators/cgroups/constants.hpp#L30
  51  */
  52 #define PER_CPU_SHARES 1024
  53 



  54 class CgroupSubsystem: CHeapObj<mtInternal> {
  55   friend class CgroupSubsystemFactory;
  56   private:
  57     virtual int cpu_quota();
  58     virtual int cpu_period();
  59     virtual int cpu_shares();
  60     virtual jlong memory_usage_in_bytes();
  61     virtual jlong memory_and_swap_limit_in_bytes();
  62     virtual jlong memory_soft_limit_in_bytes();
  63     virtual jlong memory_max_usage_in_bytes();
  64     virtual char * cpu_cpuset_cpus();
  65     virtual char * cpu_cpuset_memory_nodes();
  66   public:
  67     jlong available_memory();
  68     int active_processor_count(int physical_proc_count);
  69     void print_container_info(outputStream* st, int physical_proc_count);
  70     virtual jlong memory_limit_in_bytes();
  71     virtual const char * container_type();
  72 };
  73 


 150     int cpu_period();
 151 
 152     int cpu_shares();
 153 
 154     const char * container_type() {
 155       return "cgroupv1";
 156     }
 157 
 158   public:
 159     CgroupV1Subsystem(CgroupV1Controller* cpuset,
 160                       CgroupV1Controller* cpu,
 161                       CgroupV1Controller* cpuacct,
 162                       CgroupV1MemoryController* memory) {
 163       _cpuset = cpuset;
 164       _cpu = cpu;
 165       _cpuacct = cpuacct;
 166       _memory = memory;
 167       _unlimited_memory = (LONG_MAX / os::vm_page_size()) * os::vm_page_size();
 168     }
 169 };




































































 170 
 171 #endif // CGROUP_SUBSYSTEM_LINUX_HPP


  34 /*
  35  * PER_CPU_SHARES has been set to 1024 because CPU shares' quota
  36  * is commonly used in cloud frameworks like Kubernetes[1],
  37  * AWS[2] and Mesos[3] in a similar way. They spawn containers with
  38  * --cpu-shares option values scaled by PER_CPU_SHARES. Thus, we do
  39  * the inverse for determining the number of possible available
  40  * CPUs to the JVM inside a container. See JDK-8216366.
  41  *
  42  * [1] https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/#meaning-of-cpu
  43  *     In particular:
  44  *        When using Docker:
  45  *          The spec.containers[].resources.requests.cpu is converted to its core value, which is potentially
  46  *          fractional, and multiplied by 1024. The greater of this number or 2 is used as the value of the
  47  *          --cpu-shares flag in the docker run command.
  48  * [2] https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_ContainerDefinition.html
  49  * [3] https://github.com/apache/mesos/blob/3478e344fb77d931f6122980c6e94cd3913c441d/src/docker/docker.cpp#L648
  50  *     https://github.com/apache/mesos/blob/3478e344fb77d931f6122980c6e94cd3913c441d/src/slave/containerizer/mesos/isolators/cgroups/constants.hpp#L30
  51  */
  52 #define PER_CPU_SHARES 1024
  53 
  54 // Four controllers: cpu, cpuset, cpuacct, memory
  55 #define CG_INFO_LENGTH 4
  56 
  57 class CgroupSubsystem: CHeapObj<mtInternal> {
  58   friend class CgroupSubsystemFactory;
  59   private:
  60     virtual int cpu_quota();
  61     virtual int cpu_period();
  62     virtual int cpu_shares();
  63     virtual jlong memory_usage_in_bytes();
  64     virtual jlong memory_and_swap_limit_in_bytes();
  65     virtual jlong memory_soft_limit_in_bytes();
  66     virtual jlong memory_max_usage_in_bytes();
  67     virtual char * cpu_cpuset_cpus();
  68     virtual char * cpu_cpuset_memory_nodes();
  69   public:
  70     jlong available_memory();
  71     int active_processor_count(int physical_proc_count);
  72     void print_container_info(outputStream* st, int physical_proc_count);
  73     virtual jlong memory_limit_in_bytes();
  74     virtual const char * container_type();
  75 };
  76 


 153     int cpu_period();
 154 
 155     int cpu_shares();
 156 
 157     const char * container_type() {
 158       return "cgroupv1";
 159     }
 160 
 161   public:
 162     CgroupV1Subsystem(CgroupV1Controller* cpuset,
 163                       CgroupV1Controller* cpu,
 164                       CgroupV1Controller* cpuacct,
 165                       CgroupV1MemoryController* memory) {
 166       _cpuset = cpuset;
 167       _cpu = cpu;
 168       _cpuacct = cpuacct;
 169       _memory = memory;
 170       _unlimited_memory = (LONG_MAX / os::vm_page_size()) * os::vm_page_size();
 171     }
 172 };
 173 
 174 class CgroupV2Controller: public CgroupController {
 175   friend class CgroupSubsystemFactory;
 176   private:
 177     /* the mount path of the cgroup v2 hierarchy */
 178     char *_mount_path;
 179     /* The cgroup path for the controller */
 180     char *_cgroup_path;
 181 
 182     /* Constructed full path to the subsystem directory */
 183     char *_path;
 184     static char* construct_path(char* mount_path, char *cgroup_path);
 185 
 186   public:
 187     CgroupV2Controller(char * mount_path, char *cgroup_path) {
 188       _mount_path = mount_path;
 189       _cgroup_path = os::strdup(cgroup_path);
 190       _path = construct_path(mount_path, cgroup_path);
 191     }
 192 
 193     char *subsystem_path() { return _path; }
 194 };
 195 
 196 class CgroupV2Subsystem: CgroupSubsystem {
 197   friend class CgroupSubsystemFactory;
 198   private:
 199     /* One unified controller */
 200     CgroupController* _unified = NULL;
 201 
 202     CgroupV2Subsystem(CgroupController* unified) {
 203       _unified = unified;
 204     }
 205 
 206     char *mem_limit_val();
 207     char *mem_swp_limit_val();
 208     char *mem_soft_limit_val();
 209     char *cpu_quota_val();
 210     int cpu_quota();
 211     int cpu_period();
 212     int cpu_shares();
 213     jlong limit_from_str(char* limit_str);
 214     jlong memory_and_swap_limit_in_bytes();
 215     jlong memory_soft_limit_in_bytes();
 216     jlong memory_usage_in_bytes();
 217     jlong memory_max_usage_in_bytes();
 218     char * cpu_cpuset_cpus();
 219     char * cpu_cpuset_memory_nodes();
 220 
 221   public:
 222     jlong memory_limit_in_bytes();
 223     const char * container_type() {
 224       return "cgroupv2";
 225     }
 226 };
 227 
 228 // Class representing info in /proc/self/cgroup.
 229 // See man 7 cgroups
 230 class CgroupInfo : public StackObj {
 231   friend class CgroupSubsystemFactory;
 232 
 233   private:
 234   char* _name;
 235   int _hierarchy_id;
 236   bool _enabled;
 237   char* _cgroup_path;
 238 
 239 };
 240 
 241 
 242 #endif // CGROUP_SUBSYSTEM_LINUX_HPP
< prev index next >