src/hotspot/os/linux/os_linux.cpp
changeset 47903 7f22774a5f42
parent 47881 0ce0ac68ace7
child 48005 9fd89aabb6cd
--- a/src/hotspot/os/linux/os_linux.cpp	Wed Nov 15 08:25:28 2017 -0500
+++ b/src/hotspot/os/linux/os_linux.cpp	Wed Nov 15 10:34:17 2017 -0500
@@ -38,6 +38,7 @@
 #include "oops/oop.inline.hpp"
 #include "os_linux.inline.hpp"
 #include "os_share_linux.hpp"
+#include "osContainer_linux.hpp"
 #include "prims/jniFastGetField.hpp"
 #include "prims/jvm_misc.hpp"
 #include "runtime/arguments.hpp"
@@ -171,13 +172,52 @@
 julong os::Linux::available_memory() {
   // values in struct sysinfo are "unsigned long"
   struct sysinfo si;
+  julong avail_mem;
+
+  if (OSContainer::is_containerized()) {
+    jlong mem_limit, mem_usage;
+    if ((mem_limit = OSContainer::memory_limit_in_bytes()) > 0) {
+      if ((mem_usage = OSContainer::memory_usage_in_bytes()) > 0) {
+        if (mem_limit > mem_usage) {
+          avail_mem = (julong)mem_limit - (julong)mem_usage;
+        } else {
+          avail_mem = 0;
+        }
+        log_trace(os)("available container memory: " JULONG_FORMAT, avail_mem);
+        return avail_mem;
+      } else {
+        log_debug(os,container)("container memory usage call failed: " JLONG_FORMAT, mem_usage);
+      }
+    } else {
+      log_debug(os,container)("container memory unlimited or failed: " JLONG_FORMAT, mem_limit);
+    }
+  }
+
   sysinfo(&si);
-
-  return (julong)si.freeram * si.mem_unit;
+  avail_mem = (julong)si.freeram * si.mem_unit;
+  log_trace(os)("available memory: " JULONG_FORMAT, avail_mem);
+  return avail_mem;
 }
 
 julong os::physical_memory() {
-  return Linux::physical_memory();
+  if (OSContainer::is_containerized()) {
+    jlong mem_limit;
+    if ((mem_limit = OSContainer::memory_limit_in_bytes()) > 0) {
+      log_trace(os)("total container memory: " JLONG_FORMAT, mem_limit);
+      return (julong)mem_limit;
+    } else {
+      if (mem_limit == OSCONTAINER_ERROR) {
+        log_debug(os,container)("container memory limit call failed");
+      }
+      if (mem_limit == -1) {
+        log_debug(os,container)("container memory unlimited, using host value");
+      }
+    }
+  }
+
+  jlong phys_mem = Linux::physical_memory();
+  log_trace(os)("total system memory: " JLONG_FORMAT, phys_mem);
+  return phys_mem;
 }
 
 // Return true if user is running as root.
@@ -1950,6 +1990,8 @@
   os::Posix::print_load_average(st);
 
   os::Linux::print_full_memory_info(st);
+
+  os::Linux::print_container_info(st);
 }
 
 // Try to identify popular distros.
@@ -2087,6 +2129,66 @@
   st->cr();
 }
 
+void os::Linux::print_container_info(outputStream* st) {
+  if (OSContainer::is_containerized()) {
+    st->print("container (cgroup) information:\n");
+
+    char *p = OSContainer::container_type();
+    if (p == NULL)
+      st->print("container_type() failed\n");
+    else {
+      st->print("container_type: %s\n", p);
+    }
+
+    p = OSContainer::cpu_cpuset_cpus();
+    if (p == NULL)
+      st->print("cpu_cpuset_cpus() failed\n");
+    else {
+      st->print("cpu_cpuset_cpus: %s\n", p);
+      free(p);
+    }
+
+    p = OSContainer::cpu_cpuset_memory_nodes();
+    if (p < 0)
+      st->print("cpu_memory_nodes() failed\n");
+    else {
+      st->print("cpu_memory_nodes: %s\n", p);
+      free(p);
+    }
+
+    int i = OSContainer::active_processor_count();
+    if (i < 0)
+      st->print("active_processor_count() failed\n");
+    else
+      st->print("active_processor_count: %d\n", i);
+
+    i = OSContainer::cpu_quota();
+    st->print("cpu_quota: %d\n", i);
+
+    i = OSContainer::cpu_period();
+    st->print("cpu_period: %d\n", i);
+
+    i = OSContainer::cpu_shares();
+    st->print("cpu_shares: %d\n", i);
+
+    jlong j = OSContainer::memory_limit_in_bytes();
+    st->print("memory_limit_in_bytes: " JLONG_FORMAT "\n", j);
+
+    j = OSContainer::memory_and_swap_limit_in_bytes();
+    st->print("memory_and_swap_limit_in_bytes: " JLONG_FORMAT "\n", j);
+
+    j = OSContainer::memory_soft_limit_in_bytes();
+    st->print("memory_soft_limit_in_bytes: " JLONG_FORMAT "\n", j);
+
+    j = OSContainer::OSContainer::memory_usage_in_bytes();
+    st->print("memory_usage_in_bytes: " JLONG_FORMAT "\n", j);
+
+    j = OSContainer::OSContainer::memory_max_usage_in_bytes();
+    st->print("memory_max_usage_in_bytes: " JLONG_FORMAT "\n", j);
+    st->cr();
+  }
+}
+
 void os::print_memory_info(outputStream* st) {
 
   st->print("Memory:");
@@ -4798,6 +4900,10 @@
   }
 }
 
+void os::pd_init_container_support() {
+  OSContainer::init();
+}
+
 // this is called _after_ the global arguments have been parsed
 jint os::init_2(void) {
 
@@ -4946,12 +5052,12 @@
 // dynamic check - see 6515172 for details.
 // If anything goes wrong we fallback to returning the number of online
 // processors - which can be greater than the number available to the process.
-int os::active_processor_count() {
+int os::Linux::active_processor_count() {
   cpu_set_t cpus;  // can represent at most 1024 (CPU_SETSIZE) processors
   cpu_set_t* cpus_p = &cpus;
   int cpus_size = sizeof(cpu_set_t);
 
-  int configured_cpus = processor_count();  // upper bound on available cpus
+  int configured_cpus = os::processor_count();  // upper bound on available cpus
   int cpu_count = 0;
 
 // old build platforms may not support dynamic cpu sets
@@ -5014,10 +5120,44 @@
     CPU_FREE(cpus_p);
   }
 
-  assert(cpu_count > 0 && cpu_count <= processor_count(), "sanity check");
+  assert(cpu_count > 0 && cpu_count <= os::processor_count(), "sanity check");
   return cpu_count;
 }
 
+// Determine the active processor count from one of
+// three different sources:
+//
+// 1. User option -XX:ActiveProcessorCount
+// 2. kernel os calls (sched_getaffinity or sysconf(_SC_NPROCESSORS_ONLN)
+// 3. extracted from cgroup cpu subsystem (shares and quotas)
+//
+// Option 1, if specified, will always override.
+// If the cgroup subsystem is active and configured, we
+// will return the min of the cgroup and option 2 results.
+// This is required since tools, such as numactl, that
+// alter cpu affinity do not update cgroup subsystem
+// cpuset configuration files.
+int os::active_processor_count() {
+  // User has overridden the number of active processors
+  if (ActiveProcessorCount > 0) {
+    log_trace(os)("active_processor_count: "
+                  "active processor count set by user : %d",
+                  ActiveProcessorCount);
+    return ActiveProcessorCount;
+  }
+
+  int active_cpus;
+  if (OSContainer::is_containerized()) {
+    active_cpus = OSContainer::active_processor_count();
+    log_trace(os)("active_processor_count: determined by OSContainer: %d",
+                   active_cpus);
+  } else {
+    active_cpus = os::Linux::active_processor_count();
+  }
+
+  return active_cpus;
+}
+
 void os::set_native_thread_name(const char *name) {
   if (Linux::_pthread_setname_np) {
     char buf [16]; // according to glibc manpage, 16 chars incl. '/0'