1 /* 2 * Copyright (c) 2017, 2019, 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 */ 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 73 jlong OSContainer::memory_limit_in_bytes() { 74 assert(cgroup_subsystem != NULL, "cgroup subsystem not available"); 75 return cgroup_subsystem->memory_limit_in_bytes(); 76 } 77 78 jlong OSContainer::available_memory() { 79 assert(cgroup_subsystem != NULL, "cgroup subsystem not available"); 80 return cgroup_subsystem->available_memory(); 81 } 82 83 int OSContainer::active_processor_count() { 84 assert(cgroup_subsystem != NULL, "cgroup subsystem not available"); 85 int physical_proc = os::Linux::active_processor_count(); 86 return cgroup_subsystem->active_processor_count(physical_proc); 87 } 88 89 void OSContainer::print_container_info(outputStream* st) { 90 assert(cgroup_subsystem != NULL, "cgroup subsystem not available"); 91 int physical_proc = os::Linux::active_processor_count(); 92 cgroup_subsystem->print_container_info(st, physical_proc); 93 }