1 /*
2  * Copyright (c) 2017, 2020, 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 #include <string.h>
26 #include <math.h>
27 #include <errno.h>
28 #include "runtime/globals.hpp"
29 #include "runtime/os.hpp"
30 #include "logging/log.hpp"
31 #include "osContainer_linux.hpp"
32 #include "cgroupSubsystem_linux.hpp"
33 
34 
35 bool  OSContainer::_is_initialized   = false;
36 bool  OSContainer::_is_containerized = false;
37 CgroupSubsystem* cgroup_subsystem;
38 
39 /* init
40  *
41  * Initialize the container support and determine if
42  * we are running under cgroup control.
43  */
init()44 void OSContainer::init() {
45   jlong mem_limit;
46 
47   assert(!_is_initialized, "Initializing OSContainer more than once");
48 
49   _is_initialized = true;
50   _is_containerized = false;
51 
52   log_trace(os, container)("OSContainer::init: Initializing Container Support");
53   if (!UseContainerSupport) {
54     log_trace(os, container)("Container Support not enabled");
55     return;
56   }
57 
58   cgroup_subsystem = CgroupSubsystemFactory::create();
59   if (cgroup_subsystem == NULL) {
60     return; // Required subsystem files not found or other error
61   }
62   // We need to update the amount of physical memory now that
63   // cgroup subsystem files have been processed.
64   if ((mem_limit = cgroup_subsystem->memory_limit_in_bytes()) > 0) {
65     os::Linux::set_physical_memory(mem_limit);
66     log_info(os, container)("Memory Limit is: " JLONG_FORMAT, mem_limit);
67   }
68 
69   _is_containerized = true;
70 
71 }
72 
container_type()73 const char * OSContainer::container_type() {
74   assert(cgroup_subsystem != NULL, "cgroup subsystem not available");
75   return cgroup_subsystem->container_type();
76 }
77 
memory_limit_in_bytes()78 jlong OSContainer::memory_limit_in_bytes() {
79   assert(cgroup_subsystem != NULL, "cgroup subsystem not available");
80   return cgroup_subsystem->memory_limit_in_bytes();
81 }
82 
memory_and_swap_limit_in_bytes()83 jlong OSContainer::memory_and_swap_limit_in_bytes() {
84   assert(cgroup_subsystem != NULL, "cgroup subsystem not available");
85   return cgroup_subsystem->memory_and_swap_limit_in_bytes();
86 }
87 
memory_soft_limit_in_bytes()88 jlong OSContainer::memory_soft_limit_in_bytes() {
89   assert(cgroup_subsystem != NULL, "cgroup subsystem not available");
90   return cgroup_subsystem->memory_soft_limit_in_bytes();
91 }
92 
memory_usage_in_bytes()93 jlong OSContainer::memory_usage_in_bytes() {
94   assert(cgroup_subsystem != NULL, "cgroup subsystem not available");
95   return cgroup_subsystem->memory_usage_in_bytes();
96 }
97 
memory_max_usage_in_bytes()98 jlong OSContainer::memory_max_usage_in_bytes() {
99   assert(cgroup_subsystem != NULL, "cgroup subsystem not available");
100   return cgroup_subsystem->memory_max_usage_in_bytes();
101 }
102 
cpu_cpuset_cpus()103 char * OSContainer::cpu_cpuset_cpus() {
104   assert(cgroup_subsystem != NULL, "cgroup subsystem not available");
105   return cgroup_subsystem->cpu_cpuset_cpus();
106 }
107 
cpu_cpuset_memory_nodes()108 char * OSContainer::cpu_cpuset_memory_nodes() {
109   assert(cgroup_subsystem != NULL, "cgroup subsystem not available");
110   return cgroup_subsystem->cpu_cpuset_memory_nodes();
111 }
112 
active_processor_count()113 int OSContainer::active_processor_count() {
114   assert(cgroup_subsystem != NULL, "cgroup subsystem not available");
115   return cgroup_subsystem->active_processor_count();
116 }
117 
cpu_quota()118 int OSContainer::cpu_quota() {
119   assert(cgroup_subsystem != NULL, "cgroup subsystem not available");
120   return cgroup_subsystem->cpu_quota();
121 }
122 
cpu_period()123 int OSContainer::cpu_period() {
124   assert(cgroup_subsystem != NULL, "cgroup subsystem not available");
125   return cgroup_subsystem->cpu_period();
126 }
127 
cpu_shares()128 int OSContainer::cpu_shares() {
129   assert(cgroup_subsystem != NULL, "cgroup subsystem not available");
130   return cgroup_subsystem->cpu_shares();
131 }
132