Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/hotspot/os/linux/cgroupSubsystem_linux.hpp
64440 views
1
/*
2
* Copyright (c) 2019, 2021, 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_SUBSYSTEM_LINUX_HPP
26
#define CGROUP_SUBSYSTEM_LINUX_HPP
27
28
#include "memory/allocation.hpp"
29
#include "runtime/os.hpp"
30
#include "logging/log.hpp"
31
#include "utilities/globalDefinitions.hpp"
32
#include "utilities/macros.hpp"
33
#include "osContainer_linux.hpp"
34
35
// Shared cgroups code (used by cgroup version 1 and version 2)
36
37
/*
38
* PER_CPU_SHARES has been set to 1024 because CPU shares' quota
39
* is commonly used in cloud frameworks like Kubernetes[1],
40
* AWS[2] and Mesos[3] in a similar way. They spawn containers with
41
* --cpu-shares option values scaled by PER_CPU_SHARES. Thus, we do
42
* the inverse for determining the number of possible available
43
* CPUs to the JVM inside a container. See JDK-8216366.
44
*
45
* [1] https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/#meaning-of-cpu
46
* In particular:
47
* When using Docker:
48
* The spec.containers[].resources.requests.cpu is converted to its core value, which is potentially
49
* fractional, and multiplied by 1024. The greater of this number or 2 is used as the value of the
50
* --cpu-shares flag in the docker run command.
51
* [2] https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_ContainerDefinition.html
52
* [3] https://github.com/apache/mesos/blob/3478e344fb77d931f6122980c6e94cd3913c441d/src/docker/docker.cpp#L648
53
* https://github.com/apache/mesos/blob/3478e344fb77d931f6122980c6e94cd3913c441d/src/slave/containerizer/mesos/isolators/cgroups/constants.hpp#L30
54
*/
55
#define PER_CPU_SHARES 1024
56
57
#define CGROUPS_V1 1
58
#define CGROUPS_V2 2
59
#define INVALID_CGROUPS_V2 3
60
#define INVALID_CGROUPS_V1 4
61
#define INVALID_CGROUPS_NO_MOUNT 5
62
#define INVALID_CGROUPS_GENERIC 6
63
64
// Five controllers: cpu, cpuset, cpuacct, memory, pids
65
#define CG_INFO_LENGTH 5
66
#define CPUSET_IDX 0
67
#define CPU_IDX 1
68
#define CPUACCT_IDX 2
69
#define MEMORY_IDX 3
70
#define PIDS_IDX 4
71
72
typedef char * cptr;
73
74
class CgroupController: public CHeapObj<mtInternal> {
75
public:
76
virtual char *subsystem_path() = 0;
77
};
78
79
PRAGMA_DIAG_PUSH
80
PRAGMA_FORMAT_NONLITERAL_IGNORED
81
template <typename T> int subsystem_file_line_contents(CgroupController* c,
82
const char *filename,
83
const char *matchline,
84
const char *scan_fmt,
85
T returnval) {
86
FILE *fp = NULL;
87
char *p;
88
char file[MAXPATHLEN+1];
89
char buf[MAXPATHLEN+1];
90
char discard[MAXPATHLEN+1];
91
bool found_match = false;
92
93
if (c == NULL) {
94
log_debug(os, container)("subsystem_file_line_contents: CgroupController* is NULL");
95
return OSCONTAINER_ERROR;
96
}
97
if (c->subsystem_path() == NULL) {
98
log_debug(os, container)("subsystem_file_line_contents: subsystem path is NULL");
99
return OSCONTAINER_ERROR;
100
}
101
102
strncpy(file, c->subsystem_path(), MAXPATHLEN);
103
file[MAXPATHLEN-1] = '\0';
104
int filelen = strlen(file);
105
if ((filelen + strlen(filename)) > (MAXPATHLEN-1)) {
106
log_debug(os, container)("File path too long %s, %s", file, filename);
107
return OSCONTAINER_ERROR;
108
}
109
strncat(file, filename, MAXPATHLEN-filelen);
110
log_trace(os, container)("Path to %s is %s", filename, file);
111
fp = fopen(file, "r");
112
if (fp != NULL) {
113
int err = 0;
114
while ((p = fgets(buf, MAXPATHLEN, fp)) != NULL) {
115
found_match = false;
116
if (matchline == NULL) {
117
// single-line file case
118
int matched = sscanf(p, scan_fmt, returnval);
119
found_match = (matched == 1);
120
} else {
121
// multi-line file case
122
if (strstr(p, matchline) != NULL) {
123
// discard matchline string prefix
124
int matched = sscanf(p, scan_fmt, discard, returnval);
125
found_match = (matched == 2);
126
} else {
127
continue; // substring not found
128
}
129
}
130
if (found_match) {
131
fclose(fp);
132
return 0;
133
} else {
134
err = 1;
135
log_debug(os, container)("Type %s not found in file %s", scan_fmt, file);
136
}
137
}
138
if (err == 0) {
139
log_debug(os, container)("Empty file %s", file);
140
}
141
} else {
142
log_debug(os, container)("Open of file %s failed, %s", file, os::strerror(errno));
143
}
144
if (fp != NULL)
145
fclose(fp);
146
return OSCONTAINER_ERROR;
147
}
148
PRAGMA_DIAG_POP
149
150
#define GET_CONTAINER_INFO(return_type, subsystem, filename, \
151
logstring, scan_fmt, variable) \
152
return_type variable; \
153
{ \
154
int err; \
155
err = subsystem_file_line_contents(subsystem, \
156
filename, \
157
NULL, \
158
scan_fmt, \
159
&variable); \
160
if (err != 0) { \
161
log_trace(os, container)(logstring, (return_type) OSCONTAINER_ERROR); \
162
return (return_type) OSCONTAINER_ERROR; \
163
} \
164
\
165
log_trace(os, container)(logstring, variable); \
166
}
167
168
#define GET_CONTAINER_INFO_CPTR(return_type, subsystem, filename, \
169
logstring, scan_fmt, variable, bufsize) \
170
char variable[bufsize]; \
171
{ \
172
int err; \
173
err = subsystem_file_line_contents(subsystem, \
174
filename, \
175
NULL, \
176
scan_fmt, \
177
variable); \
178
if (err != 0) \
179
return (return_type) NULL; \
180
\
181
log_trace(os, container)(logstring, variable); \
182
}
183
184
#define GET_CONTAINER_INFO_LINE(return_type, controller, filename, \
185
matchline, logstring, scan_fmt, variable) \
186
return_type variable; \
187
{ \
188
int err; \
189
err = subsystem_file_line_contents(controller, \
190
filename, \
191
matchline, \
192
scan_fmt, \
193
&variable); \
194
if (err != 0) \
195
return (return_type) OSCONTAINER_ERROR; \
196
\
197
log_trace(os, container)(logstring, variable); \
198
}
199
200
201
class CachedMetric : public CHeapObj<mtInternal>{
202
private:
203
volatile jlong _metric;
204
volatile jlong _next_check_counter;
205
public:
206
CachedMetric() {
207
_metric = -1;
208
_next_check_counter = min_jlong;
209
}
210
bool should_check_metric() {
211
return os::elapsed_counter() > _next_check_counter;
212
}
213
jlong value() { return _metric; }
214
void set_value(jlong value, jlong timeout) {
215
_metric = value;
216
// Metric is unlikely to change, but we want to remain
217
// responsive to configuration changes. A very short grace time
218
// between re-read avoids excessive overhead during startup without
219
// significantly reducing the VMs ability to promptly react to changed
220
// metric config
221
_next_check_counter = os::elapsed_counter() + timeout;
222
}
223
};
224
225
class CachingCgroupController : public CHeapObj<mtInternal> {
226
private:
227
CgroupController* _controller;
228
CachedMetric* _metrics_cache;
229
230
public:
231
CachingCgroupController(CgroupController* cont) {
232
_controller = cont;
233
_metrics_cache = new CachedMetric();
234
}
235
236
CachedMetric* metrics_cache() { return _metrics_cache; }
237
CgroupController* controller() { return _controller; }
238
};
239
240
class CgroupSubsystem: public CHeapObj<mtInternal> {
241
public:
242
jlong memory_limit_in_bytes();
243
int active_processor_count();
244
jlong limit_from_str(char* limit_str);
245
246
virtual int cpu_quota() = 0;
247
virtual int cpu_period() = 0;
248
virtual int cpu_shares() = 0;
249
virtual jlong pids_max() = 0;
250
virtual jlong pids_current() = 0;
251
virtual jlong memory_usage_in_bytes() = 0;
252
virtual jlong memory_and_swap_limit_in_bytes() = 0;
253
virtual jlong memory_soft_limit_in_bytes() = 0;
254
virtual jlong memory_max_usage_in_bytes() = 0;
255
virtual char * cpu_cpuset_cpus() = 0;
256
virtual char * cpu_cpuset_memory_nodes() = 0;
257
virtual jlong read_memory_limit_in_bytes() = 0;
258
virtual const char * container_type() = 0;
259
virtual CachingCgroupController* memory_controller() = 0;
260
virtual CachingCgroupController* cpu_controller() = 0;
261
};
262
263
// Utility class for storing info retrieved from /proc/cgroups,
264
// /proc/self/cgroup and /proc/self/mountinfo
265
// For reference see man 7 cgroups and CgroupSubsystemFactory
266
class CgroupInfo : public StackObj {
267
friend class CgroupSubsystemFactory;
268
friend class WhiteBox;
269
270
private:
271
char* _name;
272
int _hierarchy_id;
273
bool _enabled;
274
bool _data_complete; // indicating cgroup v1 data is complete for this controller
275
char* _cgroup_path; // cgroup controller path from /proc/self/cgroup
276
char* _root_mount_path; // root mount path from /proc/self/mountinfo. Unused for cgroup v2
277
char* _mount_path; // mount path from /proc/self/mountinfo.
278
279
public:
280
CgroupInfo() {
281
_name = NULL;
282
_hierarchy_id = -1;
283
_enabled = false;
284
_data_complete = false;
285
_cgroup_path = NULL;
286
_root_mount_path = NULL;
287
_mount_path = NULL;
288
}
289
290
};
291
292
class CgroupSubsystemFactory: AllStatic {
293
friend class WhiteBox;
294
295
public:
296
static CgroupSubsystem* create();
297
private:
298
static inline bool is_cgroup_v2(u1* flags) {
299
return *flags == CGROUPS_V2;
300
}
301
302
#ifdef ASSERT
303
static inline bool is_valid_cgroup(u1* flags) {
304
return *flags == CGROUPS_V1 || *flags == CGROUPS_V2;
305
}
306
static inline bool is_cgroup_v1(u1* flags) {
307
return *flags == CGROUPS_V1;
308
}
309
#endif
310
311
// Determine the cgroup type (version 1 or version 2), given
312
// relevant paths to files. Sets 'flags' accordingly.
313
static bool determine_type(CgroupInfo* cg_infos,
314
const char* proc_cgroups,
315
const char* proc_self_cgroup,
316
const char* proc_self_mountinfo,
317
u1* flags);
318
static void cleanup(CgroupInfo* cg_infos);
319
};
320
321
#endif // CGROUP_SUBSYSTEM_LINUX_HPP
322
323