1 /*
  2  * Copyright (c) 2019, 2024, 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 #include "cgroupUtil_linux.hpp"
 32 
 33 // Cgroups version 1 specific implementation
 34 
 35 class CgroupV1Controller: public CgroupController {
 36   private:
 37     /* mountinfo contents */
 38     char* _root;
 39     bool _read_only;
 40 
 41     /* Constructed subsystem directory */
 42     char* _path;
 43 
 44   public:
 45     CgroupV1Controller(char *root,
 46                        char *mountpoint,
 47                        bool ro) : _root(os::strdup(root)),
 48                                   _read_only(ro),
 49                                   _path(nullptr) {
 50       _cgroup_path = nullptr;
 51       _mount_point = os::strdup(mountpoint);
 52     }
 53     // Shallow copy constructor
 54     CgroupV1Controller(const CgroupV1Controller& o) : _root(o._root),
 55                                                       _read_only(o._read_only),
 56                                                       _path(o._path) {
 57       _cgroup_path = o._cgroup_path;
 58       _mount_point = o._mount_point;
 59     }
 60     ~CgroupV1Controller() {
 61       // At least one subsystem controller exists with paths to malloc'd path
 62       // names
 63     }
 64 
 65     void set_subsystem_path(const char *cgroup_path);
 66     const char* subsystem_path() override { return _path; }
 67     bool is_read_only() override { return _read_only; }
 68     bool needs_hierarchy_adjustment() override;
 69 };
 70 
 71 class CgroupV1MemoryController final : public CgroupMemoryController {
 72 
 73   private:
 74     CgroupV1Controller _reader;
 75     CgroupV1Controller* reader() { return &_reader; }
 76   public:
 77     void set_subsystem_path(const char *cgroup_path) override {
 78       reader()->set_subsystem_path(cgroup_path);
 79     }
 80     jlong read_memory_limit_in_bytes(julong upper_bound) override;
 81     jlong memory_usage_in_bytes() override;
 82     jlong memory_and_swap_limit_in_bytes(julong host_mem, julong host_swap) override;
 83     jlong memory_and_swap_usage_in_bytes(julong host_mem, julong host_swap) override;
 84     jlong memory_soft_limit_in_bytes(julong upper_bound) override;
 85     jlong memory_max_usage_in_bytes() override;
 86     jlong rss_usage_in_bytes() override;
 87     jlong cache_usage_in_bytes() override;
 88     jlong kernel_memory_usage_in_bytes();
 89     jlong kernel_memory_limit_in_bytes(julong host_mem);
 90     jlong kernel_memory_max_usage_in_bytes();
 91     void print_version_specific_info(outputStream* st, julong host_mem) override;
 92     bool needs_hierarchy_adjustment() override {
 93       return reader()->needs_hierarchy_adjustment();
 94     }
 95     bool is_read_only() override {
 96       return reader()->is_read_only();
 97     }
 98     const char* subsystem_path() override { return reader()->subsystem_path(); }
 99     const char* mount_point() override { return reader()->mount_point(); }
100     const char* cgroup_path() override { return reader()->cgroup_path(); }
101   private:
102     jlong uses_mem_hierarchy();
103     jlong read_mem_swappiness();
104     jlong read_mem_swap(julong host_total_memsw);
105 
106   public:
107     CgroupV1MemoryController(const CgroupV1Controller& reader)
108       : _reader(reader) {
109     }
110 
111 };
112 
113 class CgroupV1CpuController final : public CgroupCpuController {
114 
115   private:
116     CgroupV1Controller _reader;
117     CgroupV1Controller* reader() { return &_reader; }
118   public:
119     int cpu_quota() override;
120     int cpu_period() override;
121     int cpu_shares() override;
122     void set_subsystem_path(const char *cgroup_path) override {
123       reader()->set_subsystem_path(cgroup_path);
124     }
125     bool is_read_only() override {
126       return reader()->is_read_only();
127     }
128     const char* subsystem_path() override {
129       return reader()->subsystem_path();
130     }
131     const char* mount_point() override {
132       return reader()->mount_point();
133     }
134     bool needs_hierarchy_adjustment() override {
135       return reader()->needs_hierarchy_adjustment();
136     }
137     const char* cgroup_path() override { return reader()->cgroup_path(); }
138 
139   public:
140     CgroupV1CpuController(const CgroupV1Controller& reader) : _reader(reader) {
141     }
142 };
143 
144 class CgroupV1Subsystem: public CgroupSubsystem {
145 
146   public:
147     CgroupV1Subsystem(CgroupV1Controller* cpuset,
148                       CgroupV1CpuController* cpu,
149                       CgroupV1Controller* cpuacct,
150                       CgroupV1Controller* pids,
151                       CgroupV1MemoryController* memory);
152 
153     jlong kernel_memory_usage_in_bytes();
154     jlong kernel_memory_limit_in_bytes();
155     jlong kernel_memory_max_usage_in_bytes();
156 
157     char * cpu_cpuset_cpus();
158     char * cpu_cpuset_memory_nodes();
159 
160     jlong pids_max();
161     jlong pids_current();
162     bool is_containerized();
163 
164     const char * container_type() {
165       return "cgroupv1";
166     }
167     CachingCgroupController<CgroupMemoryController>* memory_controller() { return _memory; }
168     CachingCgroupController<CgroupCpuController>* cpu_controller() { return _cpu; }
169 
170   private:
171     /* controllers */
172     CachingCgroupController<CgroupMemoryController>* _memory = nullptr;
173     CgroupV1Controller* _cpuset = nullptr;
174     CachingCgroupController<CgroupCpuController>* _cpu = nullptr;
175     CgroupV1Controller* _cpuacct = nullptr;
176     CgroupV1Controller* _pids = nullptr;
177 
178 };
179 
180 #endif // CGROUP_V1_SUBSYSTEM_LINUX_HPP