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 read_mem_swappiness();
103 jlong read_mem_swap(julong host_total_memsw);
104
105 public:
106 CgroupV1MemoryController(const CgroupV1Controller& reader)
107 : _reader(reader) {
108 }
109
110 };
111
112 class CgroupV1CpuController final : public CgroupCpuController {
113
114 private:
115 CgroupV1Controller _reader;
116 CgroupV1Controller* reader() { return &_reader; }
117 public:
118 int cpu_quota() override;
119 int cpu_period() override;
120 int cpu_shares() override;
121 void set_subsystem_path(const char *cgroup_path) override {
122 reader()->set_subsystem_path(cgroup_path);
123 }
124 bool is_read_only() override {
125 return reader()->is_read_only();
126 }
127 const char* subsystem_path() override {
128 return reader()->subsystem_path();
129 }
130 const char* mount_point() override {
131 return reader()->mount_point();
132 }
133 bool needs_hierarchy_adjustment() override {
134 return reader()->needs_hierarchy_adjustment();
135 }
136 const char* cgroup_path() override { return reader()->cgroup_path(); }
137
138 public:
139 CgroupV1CpuController(const CgroupV1Controller& reader) : _reader(reader) {
140 }
141 };
142
143 class CgroupV1Subsystem: public CgroupSubsystem {
144
145 public:
146 CgroupV1Subsystem(CgroupV1Controller* cpuset,
147 CgroupV1CpuController* cpu,
148 CgroupV1Controller* cpuacct,
149 CgroupV1Controller* pids,
150 CgroupV1MemoryController* memory);
151
152 jlong kernel_memory_usage_in_bytes();
153 jlong kernel_memory_limit_in_bytes();
154 jlong kernel_memory_max_usage_in_bytes();
155
156 char * cpu_cpuset_cpus();
157 char * cpu_cpuset_memory_nodes();
158
159 jlong pids_max();
160 jlong pids_current();
161 bool is_containerized();
162
163 const char * container_type() {
164 return "cgroupv1";
165 }
166 CachingCgroupController<CgroupMemoryController>* memory_controller() { return _memory; }
167 CachingCgroupController<CgroupCpuController>* cpu_controller() { return _cpu; }
168
169 private:
170 /* controllers */
171 CachingCgroupController<CgroupMemoryController>* _memory = nullptr;
172 CgroupV1Controller* _cpuset = nullptr;
173 CachingCgroupController<CgroupCpuController>* _cpu = nullptr;
174 CgroupV1Controller* _cpuacct = nullptr;
175 CgroupV1Controller* _pids = nullptr;
176
177 };
178
179 #endif // CGROUP_V1_SUBSYSTEM_LINUX_HPP