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 "memory/allocation.hpp"
  30 #include "cgroupSubsystem_linux.hpp"
  31 
  32 // Cgroups version 1 specific implementation
  33 
  34 class CgroupV1Controller: public CgroupController {
  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 
  56   public:
  57     bool is_hierarchical() { return _uses_mem_hierarchy; }
  58     void set_subsystem_path(char *cgroup_path);
  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_hierarchical(bool value) { _uses_mem_hierarchy = value; }
  66 
  67   public:
  68     CgroupV1MemoryController(char *root, char *mountpoint) : CgroupV1Controller(root, mountpoint) {
  69       _uses_mem_hierarchy = false;
  70     }
  71 
  72 };
  73 
  74 class CgroupV1Subsystem: public CgroupSubsystem {
  75 
  76   public:
  77     jlong read_memory_limit_in_bytes();
  78     jlong memory_and_swap_limit_in_bytes();
  79     jlong memory_soft_limit_in_bytes();
  80     jlong memory_usage_in_bytes();
  81     jlong memory_max_usage_in_bytes();
  82     char * cpu_cpuset_cpus();
  83     char * cpu_cpuset_memory_nodes();
  84 
  85     int cpu_quota();
  86     int cpu_period();
  87 
  88     int cpu_shares();
  89 
  90     const char * container_type() {
  91       return "cgroupv1";
  92     }
  93     CachingCgroupController * memory_controller() { return _memory; }
  94     CachingCgroupController * cpu_controller() { return _cpu; }
  95 
  96   private:
  97     julong _unlimited_memory;
  98 
  99     /* controllers */
 100     CachingCgroupController* _memory = NULL;
 101     CgroupV1Controller* _cpuset = NULL;
 102     CachingCgroupController* _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 = new CachingCgroupController(cpu);
 112       _cpuacct = cpuacct;
 113       _memory = new CachingCgroupController(memory);
 114       _unlimited_memory = (LONG_MAX / os::vm_page_size()) * os::vm_page_size();
 115     }
 116 };
 117 
 118 #endif // CGROUP_V1_SUBSYSTEM_LINUX_HPP