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_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 #define CONTAINER_READ_NUMBER_CHECKED(controller, filename, log_string, retval)       \
 73 {                                                                                     \
 74   bool is_ok;                                                                         \
 75   is_ok = controller->read_number(filename, &retval);                                 \
 76   if (!is_ok) {                                                                       \
 77     log_trace(os, container)(log_string " failed: %d", OSCONTAINER_ERROR);            \
 78     return OSCONTAINER_ERROR;                                                         \
 79   }                                                                                   \
 80   log_trace(os, container)(log_string " is: " JULONG_FORMAT, retval);                 \
 81 }
 82 
 83 #define CONTAINER_READ_NUMBER_CHECKED_MAX(controller, filename, log_string, retval)   \
 84 {                                                                                     \
 85   bool is_ok;                                                                         \
 86   is_ok = controller->read_number_handle_max(filename, &retval);                      \
 87   if (!is_ok) {                                                                       \
 88     log_trace(os, container)(log_string " failed: %d", OSCONTAINER_ERROR);            \
 89     return OSCONTAINER_ERROR;                                                         \
 90   }                                                                                   \
 91   log_trace(os, container)(log_string " is: " JLONG_FORMAT, retval);                  \
 92 }
 93 
 94 #define CONTAINER_READ_STRING_CHECKED(controller, filename, log_string, retval, buf_size) \
 95 {                                                                                         \
 96   bool is_ok;                                                                             \
 97   is_ok = controller->read_string(filename, retval, buf_size);                            \
 98   if (!is_ok) {                                                                           \
 99     log_trace(os, container)(log_string " failed: %d", OSCONTAINER_ERROR);                \
100     return nullptr;                                                                       \
101   }                                                                                       \
102   log_trace(os, container)(log_string " is: %s", retval);                                 \
103 }
104 
105 #define CONTAINER_READ_NUMERICAL_KEY_VALUE_CHECKED(controller, filename, key, log_string, retval) \
106 {                                                                                     \
107   bool is_ok;                                                                         \
108   is_ok = controller->read_numerical_key_value(filename, key, &retval);               \
109   if (!is_ok) {                                                                       \
110     log_trace(os, container)(log_string " failed: %d", OSCONTAINER_ERROR);            \
111     return OSCONTAINER_ERROR;                                                         \
112   }                                                                                   \
113   log_trace(os, container)(log_string " is: " JULONG_FORMAT, retval);                 \
114 }
115 
116 class CgroupController: public CHeapObj<mtInternal> {
117   protected:
118     char* _cgroup_path;
119     char* _mount_point;
120   public:
121     virtual const char* subsystem_path() = 0;
122     virtual bool is_read_only() = 0;
123     const char* cgroup_path() { return _cgroup_path; }
124     const char* mount_point() { return _mount_point; }
125     virtual bool needs_hierarchy_adjustment() { return false; }
126 
127     /* Read a numerical value as unsigned long
128      *
129      * returns: false if any error occurred. true otherwise and
130      * the parsed value is set in the provided julong pointer.
131      */
132     bool read_number(const char* filename, julong* result);
133 
134     /* Convenience method to deal with numbers as well as the string 'max'
135      * in interface files. Otherwise same as read_number().
136      *
137      * returns: false if any error occurred. true otherwise and
138      * the parsed value (which might be negative) is being set in
139      * the provided jlong pointer.
140      */
141     bool read_number_handle_max(const char* filename, jlong* result);
142 
143     /* Read a string of at most buf_size - 1 characters from the interface file.
144      * The provided buffer must be at least buf_size in size so as to account
145      * for the null terminating character. Callers must ensure that the buffer
146      * is appropriately in-scope and of sufficient size.
147      *
148      * returns: false if any error occured. true otherwise and the passed
149      * in buffer will contain the first buf_size - 1 characters of the string
150      * or up to the first new line character ('\n') whichever comes first.
151      */
152     bool read_string(const char* filename, char* buf, size_t buf_size);
153 
154     /* Read a tuple value as a number. Tuple is: '<first> <second>'.
155      * Handles 'max' (for unlimited) for any tuple value. This is handy for
156      * parsing interface files like cpu.max which contain such tuples.
157      *
158      * returns: false if any error occurred. true otherwise and the parsed
159      * value of the appropriate tuple entry set in the provided jlong pointer.
160      */
161     bool read_numerical_tuple_value(const char* filename, bool use_first, jlong* result);
162 
163     /* Read a numerical value from a multi-line interface file. The matched line is
164      * determined by the provided 'key'. The associated numerical value is being set
165      * via the passed in julong pointer. Example interface file 'memory.stat'
166      *
167      * returns: false if any error occurred. true otherwise and the parsed value is
168      * being set in the provided julong pointer.
169      */
170     bool read_numerical_key_value(const char* filename, const char* key, julong* result);
171 
172   private:
173     static jlong limit_from_str(char* limit_str);
174 };
175 
176 class CachedMetric : public CHeapObj<mtInternal>{
177   private:
178     volatile jlong _metric;
179     volatile jlong _next_check_counter;
180   public:
181     CachedMetric() {
182       _metric = -1;
183       _next_check_counter = min_jlong;
184     }
185     bool should_check_metric() {
186       return os::elapsed_counter() > _next_check_counter;
187     }
188     jlong value() { return _metric; }
189     void set_value(jlong value, jlong timeout) {
190       _metric = value;
191       // Metric is unlikely to change, but we want to remain
192       // responsive to configuration changes. A very short grace time
193       // between re-read avoids excessive overhead during startup without
194       // significantly reducing the VMs ability to promptly react to changed
195       // metric config
196       _next_check_counter = os::elapsed_counter() + timeout;
197     }
198 };
199 
200 template <class T>
201 class CachingCgroupController : public CHeapObj<mtInternal> {
202   private:
203     T* _controller;
204     CachedMetric* _metrics_cache;
205 
206   public:
207     CachingCgroupController(T* cont) {
208       _controller = cont;
209       _metrics_cache = new CachedMetric();
210     }
211 
212     CachedMetric* metrics_cache() { return _metrics_cache; }
213     T* controller() { return _controller; }
214 };
215 
216 // Pure virtual class representing version agnostic CPU controllers
217 class CgroupCpuController: public CHeapObj<mtInternal> {
218   public:
219     virtual int cpu_quota() = 0;
220     virtual int cpu_period() = 0;
221     virtual int cpu_shares() = 0;
222     virtual bool needs_hierarchy_adjustment() = 0;
223     virtual bool is_read_only() = 0;
224     virtual const char* subsystem_path() = 0;
225     virtual void set_subsystem_path(const char* cgroup_path) = 0;
226     virtual const char* mount_point() = 0;
227     virtual const char* cgroup_path() = 0;
228 };
229 
230 // Pure virtual class representing version agnostic memory controllers
231 class CgroupMemoryController: public CHeapObj<mtInternal> {
232   public:
233     virtual jlong read_memory_limit_in_bytes(julong upper_bound) = 0;
234     virtual jlong memory_usage_in_bytes() = 0;
235     virtual jlong memory_and_swap_limit_in_bytes(julong host_mem, julong host_swap) = 0;
236     virtual jlong memory_and_swap_usage_in_bytes(julong host_mem, julong host_swap) = 0;
237     virtual jlong memory_soft_limit_in_bytes(julong upper_bound) = 0;
238     virtual jlong memory_max_usage_in_bytes() = 0;
239     virtual jlong rss_usage_in_bytes() = 0;
240     virtual jlong cache_usage_in_bytes() = 0;
241     virtual void print_version_specific_info(outputStream* st, julong host_mem) = 0;
242     virtual bool needs_hierarchy_adjustment() = 0;
243     virtual bool is_read_only() = 0;
244     virtual const char* subsystem_path() = 0;
245     virtual void set_subsystem_path(const char* cgroup_path) = 0;
246     virtual const char* mount_point() = 0;
247     virtual const char* cgroup_path() = 0;
248 };
249 
250 class CgroupSubsystem: public CHeapObj<mtInternal> {
251   public:
252     jlong memory_limit_in_bytes();
253     int active_processor_count();
254 
255     virtual jlong pids_max() = 0;
256     virtual jlong pids_current() = 0;
257     virtual bool is_containerized() = 0;
258 
259     virtual char * cpu_cpuset_cpus() = 0;
260     virtual char * cpu_cpuset_memory_nodes() = 0;
261     virtual const char * container_type() = 0;
262     virtual CachingCgroupController<CgroupMemoryController>* memory_controller() = 0;
263     virtual CachingCgroupController<CgroupCpuController>* cpu_controller() = 0;
264 
265     int cpu_quota();
266     int cpu_period();
267     int cpu_shares();
268 
269     jlong memory_usage_in_bytes();
270     jlong memory_and_swap_limit_in_bytes();
271     jlong memory_and_swap_usage_in_bytes();
272     jlong memory_soft_limit_in_bytes();
273     jlong memory_max_usage_in_bytes();
274     jlong rss_usage_in_bytes();
275     jlong cache_usage_in_bytes();
276     void print_version_specific_info(outputStream* st);
277 };
278 
279 // Utility class for storing info retrieved from /proc/cgroups,
280 // /proc/self/cgroup and /proc/self/mountinfo
281 // For reference see man 7 cgroups and CgroupSubsystemFactory
282 class CgroupInfo : public StackObj {
283   friend class CgroupSubsystemFactory;
284   friend class WhiteBox;
285 
286   private:
287     char* _name;
288     int _hierarchy_id;
289     bool _enabled;
290     bool _read_only;            // whether or not the mount path is mounted read-only
291     bool _data_complete;    // indicating cgroup v1 data is complete for this controller
292     char* _cgroup_path;     // cgroup controller path from /proc/self/cgroup
293     char* _root_mount_path; // root mount path from /proc/self/mountinfo. Unused for cgroup v2
294     char* _mount_path;      // mount path from /proc/self/mountinfo.
295 
296   public:
297     CgroupInfo() {
298       _name = nullptr;
299       _hierarchy_id = -1;
300       _enabled = false;
301       _read_only = false;
302       _data_complete = false;
303       _cgroup_path = nullptr;
304       _root_mount_path = nullptr;
305       _mount_path = nullptr;
306     }
307 
308 };
309 
310 class CgroupSubsystemFactory: AllStatic {
311   friend class WhiteBox;
312 
313   public:
314     static CgroupSubsystem* create();
315   private:
316     static inline bool is_cgroup_v2(u1* flags) {
317        return *flags == CGROUPS_V2;
318     }
319 
320 #ifdef ASSERT
321     static inline bool is_valid_cgroup(u1* flags) {
322        return *flags == CGROUPS_V1 || *flags == CGROUPS_V2;
323     }
324     static inline bool is_cgroup_v1(u1* flags) {
325        return *flags == CGROUPS_V1;
326     }
327 #endif
328 
329     static void set_controller_paths(CgroupInfo* cg_infos,
330                                      int controller,
331                                      const char* name,
332                                      char* mount_path,
333                                      char* root_path,
334                                      bool read_only);
335     // Determine the cgroup type (version 1 or version 2), given
336     // relevant paths to files. Sets 'flags' accordingly.
337     static bool determine_type(CgroupInfo* cg_infos,
338                                bool cgroups_v2_enabled,
339                                const char* controllers_file,
340                                const char* proc_self_cgroup,
341                                const char* proc_self_mountinfo,
342                                u1* flags);
343     static void cleanup(CgroupInfo* cg_infos);
344 };
345 
346 #endif // CGROUP_SUBSYSTEM_LINUX_HPP