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