1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   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_V1_SUBSYSTEM_LINUX_HPP
  26 #define CGROUP_V1_SUBSYSTEM_LINUX_HPP
  27 
  28 #include "runtime/os.hpp"
  29 #include "cgroupSubsystem_linux.hpp"
  30 
  31 // Cgroups version 1 specific implementation
  32 
  33 class CgroupV1Controller: public CgroupController {
  34   friend class CgroupSubsystemFactory;
  35   private:
  36     /* mountinfo contents */
  37     char *_root;
  38     char *_mount_point;
  39 
  40     /* Constructed subsystem directory */
  41     char *_path;
  42 
  43   public:
  44     CgroupV1Controller(char *root, char *mountpoint) {
  45       _root = os::strdup(root);
  46       _mount_point = os::strdup(mountpoint);
  47       _path = NULL;
  48     }
  49 
  50     virtual void set_subsystem_path(char *cgroup_path);
  51     char *subsystem_path() { return _path; }
  52 };
  53 
  54 class CgroupV1MemoryController: public CgroupV1Controller {
  55   friend class CgroupSubsystemFactory;
  56 
  57   public:
  58     bool is_hierarchical() { return _uses_mem_hierarchy; }
  59   private:
  60     /* Some container runtimes set limits via cgroup
  61      * hierarchy. If set to true consider also memory.stat
  62      * file if everything else seems unlimited */
  63     bool _uses_mem_hierarchy;
  64     jlong uses_mem_hierarchy();
  65     void set_subsystem_path(char *cgroup_path);
  66     void set_hierarchical(bool value) { _uses_mem_hierarchy = value; }
  67 
  68   public:
  69     CgroupV1MemoryController(char *root, char *mountpoint) : CgroupV1Controller(root, mountpoint) {
  70       _uses_mem_hierarchy = false;
  71     }
  72 
  73 };
  74 
  75 class CgroupV1Subsystem: CgroupSubsystem {
  76   friend class CgroupSubsystemFactory;
  77 
  78   public:
  79     jlong memory_limit_in_bytes();
  80     jlong memory_and_swap_limit_in_bytes();
  81     jlong memory_soft_limit_in_bytes();
  82     jlong memory_usage_in_bytes();
  83     jlong memory_max_usage_in_bytes();
  84     char * cpu_cpuset_cpus();
  85     char * cpu_cpuset_memory_nodes();
  86 
  87     int cpu_quota();
  88     int cpu_period();
  89 
  90     int cpu_shares();
  91 
  92     const char * container_type() {
  93       return "cgroupv1";
  94     }
  95 
  96   private:
  97     julong _unlimited_memory;
  98 
  99     /* controllers */
 100     CgroupV1MemoryController* _memory = NULL;
 101     CgroupV1Controller* _cpuset = NULL;
 102     CgroupV1Controller* _cpu = NULL;
 103     CgroupV1Controller* _cpuacct = NULL;
 104 
 105   public:
 106     CgroupV1Subsystem(CgroupV1Controller* cpuset,
 107                       CgroupV1Controller* cpu,
 108                       CgroupV1Controller* cpuacct,
 109                       CgroupV1MemoryController* memory) {
 110       _cpuset = cpuset;
 111       _cpu = cpu;
 112       _cpuacct = cpuacct;
 113       _memory = memory;
 114       _unlimited_memory = (LONG_MAX / os::vm_page_size()) * os::vm_page_size();
 115     }
 116 };
 117 
 118 #endif // CGROUP_V1_SUBSYSTEM_LINUX_HPP