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 uses_mem_hierarchy();
104     jlong read_mem_swappiness();
105     jlong read_mem_swap(julong upper_memsw_bound);
106 
107   public:
108     CgroupV1MemoryController(const CgroupV1Controller& reader)
109       : _reader(reader) {
110     }
111 
112 };
113 
114 class CgroupV1CpuController final : public CgroupCpuController {
115 
116   private:
117     CgroupV1Controller _reader;
118     CgroupV1Controller* reader() { return &_reader; }
119   public:
120     int cpu_quota() override;
121     int cpu_period() override;
122     int cpu_shares() override;
123     void set_subsystem_path(const char *cgroup_path) override {
124       reader()->set_subsystem_path(cgroup_path);
125     }
126     bool is_read_only() override {
127       return reader()->is_read_only();
128     }
129     const char* subsystem_path() override {
130       return reader()->subsystem_path();
131     }
132     const char* mount_point() override {
133       return reader()->mount_point();
134     }
135     bool needs_hierarchy_adjustment() override {
136       return reader()->needs_hierarchy_adjustment();
137     }
138     const char* cgroup_path() override { return reader()->cgroup_path(); }
139 
140   public:
141     CgroupV1CpuController(const CgroupV1Controller& reader) : _reader(reader) {
142     }
143 };
144 
145 class CgroupV1CpuacctController final : public CgroupCpuacctController {
146 
147   private:
148     CgroupV1Controller _reader;
149     CgroupV1Controller* reader() { return &_reader; }
150   public:
151     jlong cpu_usage_in_micros() override;
152     void set_subsystem_path(const char *cgroup_path) override {
153       reader()->set_subsystem_path(cgroup_path);
154     }
155     bool is_read_only() override {
156       return reader()->is_read_only();
157     }
158     const char* subsystem_path() override {
159       return reader()->subsystem_path();
160     }
161     const char* mount_point() override {
162       return reader()->mount_point();
163     }
164     bool needs_hierarchy_adjustment() override {
165       return reader()->needs_hierarchy_adjustment();
166     }
167     const char* cgroup_path() override { return reader()->cgroup_path(); }
168 
169   public:
170     CgroupV1CpuacctController(const CgroupV1Controller& reader) : _reader(reader) {
171     }
172 };
173 
174 class CgroupV1Subsystem: public CgroupSubsystem {
175 
176   public:
177     CgroupV1Subsystem(CgroupV1Controller* cpuset,
178                       CgroupV1CpuController* cpu,
179                       CgroupV1CpuacctController* cpuacct,
180                       CgroupV1Controller* pids,
181                       CgroupV1MemoryController* memory);
182 
183     jlong kernel_memory_usage_in_bytes();
184     jlong kernel_memory_limit_in_bytes();
185     jlong kernel_memory_max_usage_in_bytes();
186 
187     char * cpu_cpuset_cpus();
188     char * cpu_cpuset_memory_nodes();
189 
190     jlong pids_max();
191     jlong pids_current();
192     bool is_containerized();
193 
194     const char * container_type() {
195       return "cgroupv1";
196     }
197     CachingCgroupController<CgroupMemoryController>* memory_controller() { return _memory; }
198     CachingCgroupController<CgroupCpuController>* cpu_controller() { return _cpu; }
199     CgroupCpuacctController* cpuacct_controller() { return _cpuacct; }
200 
201   private:
202     /* controllers */
203     CachingCgroupController<CgroupMemoryController>* _memory = nullptr;
204     CgroupV1Controller* _cpuset = nullptr;
205     CachingCgroupController<CgroupCpuController>* _cpu = nullptr;
206     CgroupV1CpuacctController* _cpuacct = nullptr;
207     CgroupV1Controller* _pids = nullptr;
208 
209 };
210 
211 #endif // CGROUP_V1_SUBSYSTEM_LINUX_HPP