1 /*
  2  * Copyright (c) 2019, 2025, 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 "cgroupSubsystem_linux.hpp"
 29 #include "cgroupUtil_linux.hpp"
 30 #include "memory/allocation.hpp"
 31 #include "runtime/os.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 upper_mem_bound, julong upper_swap_bound) override;
 83     jlong memory_and_swap_usage_in_bytes(julong upper_mem_bound, julong upper_swap_bound) override;
 84     jlong memory_soft_limit_in_bytes(julong upper_bound) override;
 85     jlong memory_throttle_limit_in_bytes() override;
 86     jlong memory_max_usage_in_bytes() override;
 87     jlong rss_usage_in_bytes() override;
 88     jlong cache_usage_in_bytes() override;
 89     jlong kernel_memory_usage_in_bytes();
 90     jlong kernel_memory_limit_in_bytes(julong upper_bound);
 91     jlong kernel_memory_max_usage_in_bytes();
 92     void print_version_specific_info(outputStream* st, julong upper_mem_bound) override;
 93     bool needs_hierarchy_adjustment() override {
 94       return reader()->needs_hierarchy_adjustment();
 95     }
 96     bool is_read_only() override {
 97       return reader()->is_read_only();
 98     }
 99     const char* subsystem_path() override { return reader()->subsystem_path(); }
100     const char* mount_point() override { return reader()->mount_point(); }
101     const char* cgroup_path() override { return reader()->cgroup_path(); }
102   private:
103     jlong read_mem_swappiness();
104     jlong read_mem_swap(julong upper_memsw_bound);
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 CgroupV1CpuacctController final : public CgroupCpuacctController {
145 
146   private:
147     CgroupV1Controller _reader;
148     CgroupV1Controller* reader() { return &_reader; }
149   public:
150     jlong cpu_usage_in_micros() override;
151     void set_subsystem_path(const char *cgroup_path) override {
152       reader()->set_subsystem_path(cgroup_path);
153     }
154     bool is_read_only() override {
155       return reader()->is_read_only();
156     }
157     const char* subsystem_path() override {
158       return reader()->subsystem_path();
159     }
160     const char* mount_point() override {
161       return reader()->mount_point();
162     }
163     bool needs_hierarchy_adjustment() override {
164       return reader()->needs_hierarchy_adjustment();
165     }
166     const char* cgroup_path() override { return reader()->cgroup_path(); }
167 
168   public:
169     CgroupV1CpuacctController(const CgroupV1Controller& reader) : _reader(reader) {
170     }
171 };
172 
173 class CgroupV1Subsystem: public CgroupSubsystem {
174 
175   public:
176     CgroupV1Subsystem(CgroupV1Controller* cpuset,
177                       CgroupV1CpuController* cpu,
178                       CgroupV1CpuacctController* cpuacct,
179                       CgroupV1Controller* pids,
180                       CgroupV1MemoryController* memory);
181 
182     jlong kernel_memory_usage_in_bytes();
183     jlong kernel_memory_limit_in_bytes();
184     jlong kernel_memory_max_usage_in_bytes();
185 
186     char * cpu_cpuset_cpus();
187     char * cpu_cpuset_memory_nodes();
188 
189     jlong pids_max();
190     jlong pids_current();
191     bool is_containerized();
192 
193     const char * container_type() {
194       return "cgroupv1";
195     }
196     CachingCgroupController<CgroupMemoryController>* memory_controller() { return _memory; }
197     CachingCgroupController<CgroupCpuController>* cpu_controller() { return _cpu; }
198     CgroupCpuacctController* cpuacct_controller() { return _cpuacct; }
199 
200   private:
201     /* controllers */
202     CachingCgroupController<CgroupMemoryController>* _memory = nullptr;
203     CgroupV1Controller* _cpuset = nullptr;
204     CachingCgroupController<CgroupCpuController>* _cpu = nullptr;
205     CgroupV1CpuacctController* _cpuacct = nullptr;
206     CgroupV1Controller* _pids = nullptr;
207 
208 };
209 
210 #endif // CGROUP_V1_SUBSYSTEM_LINUX_HPP