1 /*
   2  * Copyright (c) 2019, Red Hat Inc.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef CGROUP_V2_SUBSYSTEM_LINUX_HPP
  26 #define CGROUP_V2_SUBSYSTEM_LINUX_HPP
  27 
  28 #include "cgroupSubsystem_linux.hpp"
  29 
  30 class CgroupV2Controller: public CgroupController {
  31   private:
  32     /* the mount path of the cgroup v2 hierarchy */
  33     char *_mount_path;
  34     /* The cgroup path for the controller */
  35     char *_cgroup_path;
  36 
  37     /* Constructed full path to the subsystem directory */
  38     char *_path;
  39     static char* construct_path(char* mount_path, char *cgroup_path);
  40 
  41   public:
  42     CgroupV2Controller(char * mount_path, char *cgroup_path) {
  43       _mount_path = mount_path;
  44       _cgroup_path = os::strdup(cgroup_path);
  45       _path = construct_path(mount_path, cgroup_path);
  46     }
  47 
  48     char *subsystem_path() { return _path; }
  49 };
  50 
  51 class CgroupV2Subsystem: public CgroupSubsystem {
  52   private:
  53     /* One unified controller */
  54     CachingCgroupController* _unified = NULL;
  55 
  56     char *mem_limit_val();
  57     char *mem_swp_limit_val();
  58     char *mem_soft_limit_val();
  59     char *cpu_quota_val();
  60     jlong limit_from_str(char* limit_str);
  61 
  62   public:
  63     CgroupV2Subsystem(CgroupController * unified) {
  64       _unified = new CachingCgroupController(unified);
  65     }
  66 
  67     jlong read_memory_limit_in_bytes();
  68     int cpu_quota();
  69     int cpu_period();
  70     int cpu_shares();
  71     jlong memory_and_swap_limit_in_bytes();
  72     jlong memory_soft_limit_in_bytes();
  73     jlong memory_usage_in_bytes();
  74     jlong memory_max_usage_in_bytes();
  75     char * cpu_cpuset_cpus();
  76     char * cpu_cpuset_memory_nodes();
  77     const char * container_type() {
  78       return "cgroupv2";
  79     }
  80     CachingCgroupController * memory_controller() { return _unified; }
  81 };
  82 
  83 #endif // CGROUP_V2_SUBSYSTEM_LINUX_HPP