Blame SOURCES/8146115-pr3508-rh1463098.patch

60224f
# HG changeset patch
60224f
# User poonam
60224f
# Date 1530903013 0
60224f
#      Fri Jul 06 18:50:13 2018 +0000
60224f
# Node ID 2f2d2af6fa5c44e67e0a9987f56392315a1e4b64
60224f
# Parent  95b72537801cc9946c27ad27f07e3f0790a21b08
60224f
8146115, PR3508, RH1463098: Improve docker container detection and resource configuration usage
60224f
Reviewed-by: bobv, dbuck
60224f
60224f
diff --git openjdk.orig/hotspot/src/os/aix/vm/os_aix.cpp openjdk/hotspot/src/os/aix/vm/os_aix.cpp
60224f
--- openjdk.orig/hotspot/src/os/aix/vm/os_aix.cpp
60224f
+++ openjdk/hotspot/src/os/aix/vm/os_aix.cpp
60224f
@@ -4008,6 +4008,16 @@
60224f
 };
60224f
 
60224f
 int os::active_processor_count() {
60224f
+  // User has overridden the number of active processors
60224f
+  if (ActiveProcessorCount > 0) {
60224f
+    if (PrintActiveCpus) {
60224f
+      tty->print_cr("active_processor_count: "
60224f
+                    "active processor count set by user : %d",
60224f
+                     ActiveProcessorCount);
60224f
+    }
60224f
+    return ActiveProcessorCount;
60224f
+  }
60224f
+
60224f
   int online_cpus = ::sysconf(_SC_NPROCESSORS_ONLN);
60224f
   assert(online_cpus > 0 && online_cpus <= processor_count(), "sanity check");
60224f
   return online_cpus;
60224f
diff --git openjdk.orig/hotspot/src/os/bsd/vm/os_bsd.cpp openjdk/hotspot/src/os/bsd/vm/os_bsd.cpp
60224f
--- openjdk.orig/hotspot/src/os/bsd/vm/os_bsd.cpp
60224f
+++ openjdk/hotspot/src/os/bsd/vm/os_bsd.cpp
60224f
@@ -3770,6 +3770,16 @@
60224f
 };
60224f
 
60224f
 int os::active_processor_count() {
60224f
+  // User has overridden the number of active processors
60224f
+  if (ActiveProcessorCount > 0) {
60224f
+    if (PrintActiveCpus) {
60224f
+      tty->print_cr("active_processor_count: "
60224f
+                    "active processor count set by user : %d",
60224f
+                     ActiveProcessorCount);
60224f
+    }
60224f
+    return ActiveProcessorCount;
60224f
+  }
60224f
+
60224f
   return _processor_count;
60224f
 }
60224f
 
60224f
diff --git openjdk.orig/hotspot/src/os/linux/vm/globals_linux.hpp openjdk/hotspot/src/os/linux/vm/globals_linux.hpp
60224f
--- openjdk.orig/hotspot/src/os/linux/vm/globals_linux.hpp
60224f
+++ openjdk/hotspot/src/os/linux/vm/globals_linux.hpp
60224f
@@ -1,5 +1,5 @@
60224f
 /*
60224f
- * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
60224f
+ * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
60224f
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
60224f
  *
60224f
  * This code is free software; you can redistribute it and/or modify it
60224f
@@ -49,8 +49,13 @@
60224f
   product(bool, UseSHM, false,                                          \
60224f
           "Use SYSV shared memory for large pages")                     \
60224f
                                                                         \
60224f
-  diagnostic(bool, PrintActiveCpus, false,                              \
60224f
-          "Print the number of CPUs detected in os::active_processor_count")
60224f
+  product(bool, UseContainerSupport, true,                              \
60224f
+          "Enable detection and runtime container configuration support") \
60224f
+                                                                        \
60224f
+  product(bool, PreferContainerQuotaForCPUCount, true,                  \
60224f
+          "Calculate the container CPU availability based on the value" \
60224f
+          " of quotas (if set), when true. Otherwise, use the CPU"      \
60224f
+          " shares value, provided it is less than quota.")
60224f
 
60224f
 //
60224f
 // Defines Linux-specific default values. The flags are available on all
60224f
diff --git openjdk.orig/hotspot/src/os/linux/vm/osContainer_linux.cpp openjdk/hotspot/src/os/linux/vm/osContainer_linux.cpp
60224f
new file mode 100644
60224f
--- /dev/null
60224f
+++ openjdk/hotspot/src/os/linux/vm/osContainer_linux.cpp
60224f
@@ -0,0 +1,680 @@
60224f
+/*
60224f
+ * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
60224f
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
60224f
+ *
60224f
+ * This code is free software; you can redistribute it and/or modify it
60224f
+ * under the terms of the GNU General Public License version 2 only, as
60224f
+ * published by the Free Software Foundation.
60224f
+ *
60224f
+ * This code is distributed in the hope that it will be useful, but WITHOUT
60224f
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
60224f
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
60224f
+ * version 2 for more details (a copy is included in the LICENSE file that
60224f
+ * accompanied this code).
60224f
+ *
60224f
+ * You should have received a copy of the GNU General Public License version
60224f
+ * 2 along with this work; if not, write to the Free Software Foundation,
60224f
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
60224f
+ *
60224f
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
60224f
+ * or visit www.oracle.com if you need additional information or have any
60224f
+ * questions.
60224f
+ *
60224f
+ */
60224f
+
60224f
+#include <string.h>
60224f
+#include <math.h>
60224f
+#include <errno.h>
60224f
+#include "utilities/globalDefinitions.hpp"
60224f
+#include "memory/allocation.hpp"
60224f
+#include "runtime/os.hpp"
60224f
+#include "osContainer_linux.hpp"
60224f
+
60224f
+#define PER_CPU_SHARES 1024
60224f
+
60224f
+bool  OSContainer::_is_initialized   = false;
60224f
+bool  OSContainer::_is_containerized = false;
60224f
+julong _unlimited_memory;
60224f
+
60224f
+class CgroupSubsystem: CHeapObj<mtInternal> {
60224f
+ friend class OSContainer;
60224f
+
60224f
+ private:
60224f
+    /* mountinfo contents */
60224f
+    char *_root;
60224f
+    char *_mount_point;
60224f
+
60224f
+    /* Constructed subsystem directory */
60224f
+    char *_path;
60224f
+
60224f
+ public:
60224f
+    CgroupSubsystem(char *root, char *mountpoint) {
60224f
+      _root = os::strdup(root);
60224f
+      _mount_point = os::strdup(mountpoint);
60224f
+      _path = NULL;
60224f
+    }
60224f
+
60224f
+    /*
60224f
+     * Set directory to subsystem specific files based
60224f
+     * on the contents of the mountinfo and cgroup files.
60224f
+     */
60224f
+    void set_subsystem_path(char *cgroup_path) {
60224f
+      char buf[MAXPATHLEN+1];
60224f
+      if (_root != NULL && cgroup_path != NULL) {
60224f
+        if (strcmp(_root, "/") == 0) {
60224f
+          int buflen;
60224f
+          strncpy(buf, _mount_point, MAXPATHLEN);
60224f
+          buf[MAXPATHLEN-1] = '\0';
60224f
+          if (strcmp(cgroup_path,"/") != 0) {
60224f
+            buflen = strlen(buf);
60224f
+            if ((buflen + strlen(cgroup_path)) > (MAXPATHLEN-1)) {
60224f
+              return;
60224f
+            }
60224f
+            strncat(buf, cgroup_path, MAXPATHLEN-buflen);
60224f
+            buf[MAXPATHLEN-1] = '\0';
60224f
+          }
60224f
+          _path = os::strdup(buf);
60224f
+        } else {
60224f
+          if (strcmp(_root, cgroup_path) == 0) {
60224f
+            strncpy(buf, _mount_point, MAXPATHLEN);
60224f
+            buf[MAXPATHLEN-1] = '\0';
60224f
+            _path = os::strdup(buf);
60224f
+          } else {
60224f
+            char *p = strstr(_root, cgroup_path);
60224f
+            if (p != NULL && p == _root) {
60224f
+              if (strlen(cgroup_path) > strlen(_root)) {
60224f
+                int buflen;
60224f
+                strncpy(buf, _mount_point, MAXPATHLEN);
60224f
+                buf[MAXPATHLEN-1] = '\0';
60224f
+                buflen = strlen(buf);
60224f
+                if ((buflen + strlen(cgroup_path)) > (MAXPATHLEN-1)) {
60224f
+                  return;
60224f
+                }
60224f
+                strncat(buf, cgroup_path + strlen(_root), MAXPATHLEN-buflen);
60224f
+                buf[MAXPATHLEN-1] = '\0';
60224f
+                _path = os::strdup(buf);
60224f
+              }
60224f
+            }
60224f
+          }
60224f
+        }
60224f
+      }
60224f
+    }
60224f
+
60224f
+    char *subsystem_path() { return _path; }
60224f
+};
60224f
+
60224f
+CgroupSubsystem* memory = NULL;
60224f
+CgroupSubsystem* cpuset = NULL;
60224f
+CgroupSubsystem* cpu = NULL;
60224f
+CgroupSubsystem* cpuacct = NULL;
60224f
+
60224f
+typedef char * cptr;
60224f
+
60224f
+PRAGMA_DIAG_PUSH
60224f
+PRAGMA_FORMAT_NONLITERAL_IGNORED
60224f
+template <typename T> int subsystem_file_contents(CgroupSubsystem* c,
60224f
+                                              const char *filename,
60224f
+                                              const char *scan_fmt,
60224f
+                                              T returnval) {
60224f
+  FILE *fp = NULL;
60224f
+  char *p;
60224f
+  char file[MAXPATHLEN+1];
60224f
+  char buf[MAXPATHLEN+1];
60224f
+
60224f
+  if (c == NULL) {
60224f
+    if (PrintContainerInfo) {
60224f
+      tty->print_cr("subsystem_file_contents: CgroupSubsytem* is NULL");
60224f
+    }
60224f
+    return OSCONTAINER_ERROR;
60224f
+  }
60224f
+  if (c->subsystem_path() == NULL) {
60224f
+    if (PrintContainerInfo) {
60224f
+      tty->print_cr("subsystem_file_contents: subsystem path is NULL");
60224f
+    }
60224f
+    return OSCONTAINER_ERROR;
60224f
+  }
60224f
+
60224f
+  strncpy(file, c->subsystem_path(), MAXPATHLEN);
60224f
+  file[MAXPATHLEN-1] = '\0';
60224f
+  int filelen = strlen(file);
60224f
+  if ((filelen + strlen(filename)) > (MAXPATHLEN-1)) {
60224f
+    if (PrintContainerInfo) {
60224f
+      tty->print_cr("File path too long %s, %s", file, filename);
60224f
+    }
60224f
+    return OSCONTAINER_ERROR;
60224f
+  }
60224f
+  strncat(file, filename, MAXPATHLEN-filelen);
60224f
+  if (PrintContainerInfo) {
60224f
+    tty->print_cr("Path to %s is %s", filename, file);
60224f
+  }
60224f
+  fp = fopen(file, "r");
60224f
+  if (fp != NULL) {
60224f
+    p = fgets(buf, MAXPATHLEN, fp);
60224f
+    if (p != NULL) {
60224f
+      int matched = sscanf(p, scan_fmt, returnval);
60224f
+      if (matched == 1) {
60224f
+        fclose(fp);
60224f
+        return 0;
60224f
+      } else {
60224f
+        if (PrintContainerInfo) {
60224f
+          tty->print_cr("Type %s not found in file %s", scan_fmt, file);
60224f
+        }
60224f
+      }
60224f
+    } else {
60224f
+      if (PrintContainerInfo) {
60224f
+        tty->print_cr("Empty file %s", file);
60224f
+      }
60224f
+    }
60224f
+  } else {
60224f
+    if (PrintContainerInfo) {
60224f
+      tty->print_cr("Open of file %s failed, %s", file, strerror(errno));
60224f
+    }
60224f
+  }
60224f
+  if (fp != NULL)
60224f
+    fclose(fp);
60224f
+  return OSCONTAINER_ERROR;
60224f
+}
60224f
+PRAGMA_DIAG_POP
60224f
+
60224f
+#define GET_CONTAINER_INFO(return_type, subsystem, filename,              \
60224f
+                           logstring, scan_fmt, variable)                 \
60224f
+  return_type variable;                                                   \
60224f
+{                                                                         \
60224f
+  int err;                                                                \
60224f
+  err = subsystem_file_contents(subsystem,                                \
60224f
+                                filename,                                 \
60224f
+                                scan_fmt,                                 \
60224f
+                                &variable);                               \
60224f
+  if (err != 0)                                                           \
60224f
+    return (return_type) OSCONTAINER_ERROR;                               \
60224f
+                                                                          \
60224f
+  if (PrintContainerInfo)                                                 \
60224f
+    tty->print_cr(logstring, variable);                                   \
60224f
+}
60224f
+
60224f
+#define GET_CONTAINER_INFO_CPTR(return_type, subsystem, filename,         \
60224f
+                               logstring, scan_fmt, variable, bufsize)    \
60224f
+  char variable[bufsize];                                                 \
60224f
+{                                                                         \
60224f
+  int err;                                                                \
60224f
+  err = subsystem_file_contents(subsystem,                                \
60224f
+                                filename,                                 \
60224f
+                                scan_fmt,                                 \
60224f
+                                variable);                                \
60224f
+  if (err != 0)                                                           \
60224f
+    return (return_type) NULL;                                            \
60224f
+                                                                          \
60224f
+  if (PrintContainerInfo)                                                 \
60224f
+    tty->print_cr(logstring, variable);                                   \
60224f
+}
60224f
+
60224f
+/* init
60224f
+ *
60224f
+ * Initialize the container support and determine if
60224f
+ * we are running under cgroup control.
60224f
+ */
60224f
+void OSContainer::init() {
60224f
+  int mountid;
60224f
+  int parentid;
60224f
+  int major;
60224f
+  int minor;
60224f
+  FILE *mntinfo = NULL;
60224f
+  FILE *cgroup = NULL;
60224f
+  char buf[MAXPATHLEN+1];
60224f
+  char tmproot[MAXPATHLEN+1];
60224f
+  char tmpmount[MAXPATHLEN+1];
60224f
+  char tmpbase[MAXPATHLEN+1];
60224f
+  char *p;
60224f
+  jlong mem_limit;
60224f
+
60224f
+  assert(!_is_initialized, "Initializing OSContainer more than once");
60224f
+
60224f
+  _is_initialized = true;
60224f
+  _is_containerized = false;
60224f
+
60224f
+  _unlimited_memory = (LONG_MAX / os::vm_page_size()) * os::vm_page_size();
60224f
+
60224f
+  if (PrintContainerInfo) {
60224f
+    tty->print_cr("OSContainer::init: Initializing Container Support");
60224f
+  }
60224f
+  if (!UseContainerSupport) {
60224f
+    if (PrintContainerInfo) {
60224f
+      tty->print_cr("Container Support not enabled");
60224f
+    }
60224f
+    return;
60224f
+  }
60224f
+
60224f
+  /*
60224f
+   * Find the cgroup mount point for memory and cpuset
60224f
+   * by reading /proc/self/mountinfo
60224f
+   *
60224f
+   * Example for docker:
60224f
+   * 219 214 0:29 /docker/7208cebd00fa5f2e342b1094f7bed87fa25661471a4637118e65f1c995be8a34 /sys/fs/cgroup/memory ro,nosuid,nodev,noexec,relatime - cgroup cgroup rw,memory
60224f
+   *
60224f
+   * Example for host:
60224f
+   * 34 28 0:29 / /sys/fs/cgroup/memory rw,nosuid,nodev,noexec,relatime shared:16 - cgroup cgroup rw,memory
60224f
+   */
60224f
+  mntinfo = fopen("/proc/self/mountinfo", "r");
60224f
+  if (mntinfo == NULL) {
60224f
+      if (PrintContainerInfo) {
60224f
+        tty->print_cr("Can't open /proc/self/mountinfo, %s",
60224f
+                       strerror(errno));
60224f
+      }
60224f
+      return;
60224f
+  }
60224f
+
60224f
+  while ( (p = fgets(buf, MAXPATHLEN, mntinfo)) != NULL) {
60224f
+    // Look for the filesystem type and see if it's cgroup
60224f
+    char fstype[MAXPATHLEN+1];
60224f
+    fstype[0] = '\0';
60224f
+    char *s =  strstr(p, " - ");
60224f
+    if (s != NULL &&
60224f
+        sscanf(s, " - %s", fstype) == 1 &&
60224f
+        strcmp(fstype, "cgroup") == 0) {
60224f
+
60224f
+      if (strstr(p, "memory") != NULL) {
60224f
+        int matched = sscanf(p, "%d %d %d:%d %s %s",
60224f
+                             &mountid,
60224f
+                             &parentid,
60224f
+                             &major,
60224f
+                             &minor,
60224f
+                             tmproot,
60224f
+                             tmpmount);
60224f
+        if (matched == 6) {
60224f
+          memory = new CgroupSubsystem(tmproot, tmpmount);
60224f
+        }
60224f
+        else
60224f
+          if (PrintContainerInfo) {
60224f
+            tty->print_cr("Incompatible str containing cgroup and memory: %s", p);
60224f
+          }
60224f
+      } else if (strstr(p, "cpuset") != NULL) {
60224f
+        int matched = sscanf(p, "%d %d %d:%d %s %s",
60224f
+                             &mountid,
60224f
+                             &parentid,
60224f
+                             &major,
60224f
+                             &minor,
60224f
+                             tmproot,
60224f
+                             tmpmount);
60224f
+        if (matched == 6) {
60224f
+          cpuset = new CgroupSubsystem(tmproot, tmpmount);
60224f
+        }
60224f
+        else {
60224f
+          if (PrintContainerInfo) {
60224f
+            tty->print_cr("Incompatible str containing cgroup and cpuset: %s", p);
60224f
+          }
60224f
+        }
60224f
+      } else if (strstr(p, "cpu,cpuacct") != NULL || strstr(p, "cpuacct,cpu") != NULL) {
60224f
+        int matched = sscanf(p, "%d %d %d:%d %s %s",
60224f
+                             &mountid,
60224f
+                             &parentid,
60224f
+                             &major,
60224f
+                             &minor,
60224f
+                             tmproot,
60224f
+                             tmpmount);
60224f
+        if (matched == 6) {
60224f
+          cpu = new CgroupSubsystem(tmproot, tmpmount);
60224f
+          cpuacct = new CgroupSubsystem(tmproot, tmpmount);
60224f
+        }
60224f
+        else {
60224f
+          if (PrintContainerInfo) {
60224f
+            tty->print_cr("Incompatible str containing cgroup and cpu,cpuacct: %s", p);
60224f
+          }
60224f
+        }
60224f
+      } else if (strstr(p, "cpuacct") != NULL) {
60224f
+        int matched = sscanf(p, "%d %d %d:%d %s %s",
60224f
+                             &mountid,
60224f
+                             &parentid,
60224f
+                             &major,
60224f
+                             &minor,
60224f
+                             tmproot,
60224f
+                             tmpmount);
60224f
+        if (matched == 6) {
60224f
+          cpuacct = new CgroupSubsystem(tmproot, tmpmount);
60224f
+        }
60224f
+        else {
60224f
+          if (PrintContainerInfo) {
60224f
+            tty->print_cr("Incompatible str containing cgroup and cpuacct: %s", p);
60224f
+          }
60224f
+        }
60224f
+      } else if (strstr(p, "cpu") != NULL) {
60224f
+        int matched = sscanf(p, "%d %d %d:%d %s %s",
60224f
+                             &mountid,
60224f
+                             &parentid,
60224f
+                             &major,
60224f
+                             &minor,
60224f
+                             tmproot,
60224f
+                             tmpmount);
60224f
+        if (matched == 6) {
60224f
+          cpu = new CgroupSubsystem(tmproot, tmpmount);
60224f
+        }
60224f
+        else {
60224f
+          if (PrintContainerInfo) {
60224f
+            tty->print_cr("Incompatible str containing cgroup and cpu: %s", p);
60224f
+          }
60224f
+        }
60224f
+      }
60224f
+    }
60224f
+  }
60224f
+
60224f
+  fclose(mntinfo);
60224f
+
60224f
+  if (memory == NULL) {
60224f
+    if (PrintContainerInfo) {
60224f
+      tty->print_cr("Required cgroup memory subsystem not found");
60224f
+    }
60224f
+    return;
60224f
+  }
60224f
+  if (cpuset == NULL) {
60224f
+    if (PrintContainerInfo) {
60224f
+      tty->print_cr("Required cgroup cpuset subsystem not found");
60224f
+    }
60224f
+    return;
60224f
+  }
60224f
+  if (cpu == NULL) {
60224f
+    if (PrintContainerInfo) {
60224f
+      tty->print_cr("Required cgroup cpu subsystem not found");
60224f
+    }
60224f
+    return;
60224f
+  }
60224f
+  if (cpuacct == NULL) {
60224f
+    if (PrintContainerInfo) {
60224f
+      tty->print_cr("Required cgroup cpuacct subsystem not found");
60224f
+    }
60224f
+    return;
60224f
+  }
60224f
+
60224f
+  /*
60224f
+   * Read /proc/self/cgroup and map host mount point to
60224f
+   * local one via /proc/self/mountinfo content above
60224f
+   *
60224f
+   * Docker example:
60224f
+   * 5:memory:/docker/6558aed8fc662b194323ceab5b964f69cf36b3e8af877a14b80256e93aecb044
60224f
+   *
60224f
+   * Host example:
60224f
+   * 5:memory:/user.slice
60224f
+   *
60224f
+   * Construct a path to the process specific memory and cpuset
60224f
+   * cgroup directory.
60224f
+   *
60224f
+   * For a container running under Docker from memory example above
60224f
+   * the paths would be:
60224f
+   *
60224f
+   * /sys/fs/cgroup/memory
60224f
+   *
60224f
+   * For a Host from memory example above the path would be:
60224f
+   *
60224f
+   * /sys/fs/cgroup/memory/user.slice
60224f
+   *
60224f
+   */
60224f
+  cgroup = fopen("/proc/self/cgroup", "r");
60224f
+  if (cgroup == NULL) {
60224f
+    if (PrintContainerInfo) {
60224f
+      tty->print_cr("Can't open /proc/self/cgroup, %s",
60224f
+                     strerror(errno));
60224f
+      }
60224f
+    return;
60224f
+  }
60224f
+
60224f
+  while ( (p = fgets(buf, MAXPATHLEN, cgroup)) != NULL) {
60224f
+    int cgno;
60224f
+    int matched;
60224f
+    char *controller;
60224f
+    char *base;
60224f
+
60224f
+    /* Skip cgroup number */
60224f
+    strsep(&p, ":");
60224f
+    /* Get controller and base */
60224f
+    controller = strsep(&p, ":");
60224f
+    base = strsep(&p, "\n");
60224f
+
60224f
+    if (controller != NULL) {
60224f
+      if (strstr(controller, "memory") != NULL) {
60224f
+        memory->set_subsystem_path(base);
60224f
+      } else if (strstr(controller, "cpuset") != NULL) {
60224f
+        cpuset->set_subsystem_path(base);
60224f
+      } else if (strstr(controller, "cpu,cpuacct") != NULL || strstr(controller, "cpuacct,cpu") != NULL) {
60224f
+        cpu->set_subsystem_path(base);
60224f
+        cpuacct->set_subsystem_path(base);
60224f
+      } else if (strstr(controller, "cpuacct") != NULL) {
60224f
+        cpuacct->set_subsystem_path(base);
60224f
+      } else if (strstr(controller, "cpu") != NULL) {
60224f
+        cpu->set_subsystem_path(base);
60224f
+      }
60224f
+    }
60224f
+  }
60224f
+
60224f
+  fclose(cgroup);
60224f
+
60224f
+  // We need to update the amount of physical memory now that
60224f
+  // command line arguments have been processed.
60224f
+  if ((mem_limit = memory_limit_in_bytes()) > 0) {
60224f
+    os::Linux::set_physical_memory(mem_limit);
60224f
+  }
60224f
+
60224f
+  _is_containerized = true;
60224f
+
60224f
+}
60224f
+
60224f
+const char * OSContainer::container_type() {
60224f
+  if (is_containerized()) {
60224f
+    return "cgroupv1";
60224f
+  } else {
60224f
+    return NULL;
60224f
+  }
60224f
+}
60224f
+
60224f
+
60224f
+/* memory_limit_in_bytes
60224f
+ *
60224f
+ * Return the limit of available memory for this process.
60224f
+ *
60224f
+ * return:
60224f
+ *    memory limit in bytes or
60224f
+ *    -1 for unlimited
60224f
+ *    OSCONTAINER_ERROR for not supported
60224f
+ */
60224f
+jlong OSContainer::memory_limit_in_bytes() {
60224f
+  GET_CONTAINER_INFO(julong, memory, "/memory.limit_in_bytes",
60224f
+                     "Memory Limit is: " JULONG_FORMAT, JULONG_FORMAT, memlimit);
60224f
+
60224f
+  if (memlimit >= _unlimited_memory) {
60224f
+    if (PrintContainerInfo) {
60224f
+      tty->print_cr("Memory Limit is: Unlimited");
60224f
+    }
60224f
+    return (jlong)-1;
60224f
+  }
60224f
+  else {
60224f
+    return (jlong)memlimit;
60224f
+  }
60224f
+}
60224f
+
60224f
+jlong OSContainer::memory_and_swap_limit_in_bytes() {
60224f
+  GET_CONTAINER_INFO(julong, memory, "/memory.memsw.limit_in_bytes",
60224f
+                     "Memory and Swap Limit is: " JULONG_FORMAT, JULONG_FORMAT, memswlimit);
60224f
+  if (memswlimit >= _unlimited_memory) {
60224f
+    if (PrintContainerInfo) {
60224f
+      tty->print_cr("Memory and Swap Limit is: Unlimited");
60224f
+    }
60224f
+    return (jlong)-1;
60224f
+  } else {
60224f
+    return (jlong)memswlimit;
60224f
+  }
60224f
+}
60224f
+
60224f
+jlong OSContainer::memory_soft_limit_in_bytes() {
60224f
+  GET_CONTAINER_INFO(julong, memory, "/memory.soft_limit_in_bytes",
60224f
+                     "Memory Soft Limit is: " JULONG_FORMAT, JULONG_FORMAT, memsoftlimit);
60224f
+  if (memsoftlimit >= _unlimited_memory) {
60224f
+    if (PrintContainerInfo) {
60224f
+      tty->print_cr("Memory Soft Limit is: Unlimited");
60224f
+    }
60224f
+    return (jlong)-1;
60224f
+  } else {
60224f
+    return (jlong)memsoftlimit;
60224f
+  }
60224f
+}
60224f
+
60224f
+/* memory_usage_in_bytes
60224f
+ *
60224f
+ * Return the amount of used memory for this process.
60224f
+ *
60224f
+ * return:
60224f
+ *    memory usage in bytes or
60224f
+ *    -1 for unlimited
60224f
+ *    OSCONTAINER_ERROR for not supported
60224f
+ */
60224f
+jlong OSContainer::memory_usage_in_bytes() {
60224f
+  GET_CONTAINER_INFO(jlong, memory, "/memory.usage_in_bytes",
60224f
+                     "Memory Usage is: " JLONG_FORMAT, JLONG_FORMAT, memusage);
60224f
+  return memusage;
60224f
+}
60224f
+
60224f
+/* memory_max_usage_in_bytes
60224f
+ *
60224f
+ * Return the maximum amount of used memory for this process.
60224f
+ *
60224f
+ * return:
60224f
+ *    max memory usage in bytes or
60224f
+ *    OSCONTAINER_ERROR for not supported
60224f
+ */
60224f
+jlong OSContainer::memory_max_usage_in_bytes() {
60224f
+  GET_CONTAINER_INFO(jlong, memory, "/memory.max_usage_in_bytes",
60224f
+                     "Maximum Memory Usage is: " JLONG_FORMAT, JLONG_FORMAT, memmaxusage);
60224f
+  return memmaxusage;
60224f
+}
60224f
+
60224f
+/* active_processor_count
60224f
+ *
60224f
+ * Calculate an appropriate number of active processors for the
60224f
+ * VM to use based on these three inputs.
60224f
+ *
60224f
+ * cpu affinity
60224f
+ * cgroup cpu quota & cpu period
60224f
+ * cgroup cpu shares
60224f
+ *
60224f
+ * Algorithm:
60224f
+ *
60224f
+ * Determine the number of available CPUs from sched_getaffinity
60224f
+ *
60224f
+ * If user specified a quota (quota != -1), calculate the number of
60224f
+ * required CPUs by dividing quota by period.
60224f
+ *
60224f
+ * If shares are in effect (shares != -1), calculate the number
60224f
+ * of CPUs required for the shares by dividing the share value
60224f
+ * by PER_CPU_SHARES.
60224f
+ *
60224f
+ * All results of division are rounded up to the next whole number.
60224f
+ *
60224f
+ * If neither shares or quotas have been specified, return the
60224f
+ * number of active processors in the system.
60224f
+ *
60224f
+ * If both shares and quotas have been specified, the results are
60224f
+ * based on the flag PreferContainerQuotaForCPUCount.  If true,
60224f
+ * return the quota value.  If false return the smallest value
60224f
+ * between shares or quotas.
60224f
+ *
60224f
+ * If shares and/or quotas have been specified, the resulting number
60224f
+ * returned will never exceed the number of active processors.
60224f
+ *
60224f
+ * return:
60224f
+ *    number of CPUs
60224f
+ */
60224f
+int OSContainer::active_processor_count() {
60224f
+  int quota_count = 0, share_count = 0;
60224f
+  int cpu_count, limit_count;
60224f
+  int result;
60224f
+
60224f
+  cpu_count = limit_count = os::Linux::active_processor_count();
60224f
+  int quota  = cpu_quota();
60224f
+  int period = cpu_period();
60224f
+  int share  = cpu_shares();
60224f
+
60224f
+  if (quota > -1 && period > 0) {
60224f
+    quota_count = ceilf((float)quota / (float)period);
60224f
+    if (PrintContainerInfo) {
60224f
+      tty->print_cr("CPU Quota count based on quota/period: %d", quota_count);
60224f
+    }
60224f
+  }
60224f
+  if (share > -1) {
60224f
+    share_count = ceilf((float)share / (float)PER_CPU_SHARES);
60224f
+    if (PrintContainerInfo) {
60224f
+      tty->print_cr("CPU Share count based on shares: %d", share_count);
60224f
+    }
60224f
+  }
60224f
+
60224f
+  // If both shares and quotas are setup results depend
60224f
+  // on flag PreferContainerQuotaForCPUCount.
60224f
+  // If true, limit CPU count to quota
60224f
+  // If false, use minimum of shares and quotas
60224f
+  if (quota_count !=0 && share_count != 0) {
60224f
+    if (PreferContainerQuotaForCPUCount) {
60224f
+      limit_count = quota_count;
60224f
+    } else {
60224f
+      limit_count = MIN2(quota_count, share_count);
60224f
+    }
60224f
+  } else if (quota_count != 0) {
60224f
+    limit_count = quota_count;
60224f
+  } else if (share_count != 0) {
60224f
+    limit_count = share_count;
60224f
+  }
60224f
+
60224f
+  result = MIN2(cpu_count, limit_count);
60224f
+  if (PrintContainerInfo) {
60224f
+    tty->print_cr("OSContainer::active_processor_count: %d", result);
60224f
+  }
60224f
+  return result;
60224f
+}
60224f
+
60224f
+char * OSContainer::cpu_cpuset_cpus() {
60224f
+  GET_CONTAINER_INFO_CPTR(cptr, cpuset, "/cpuset.cpus",
60224f
+                     "cpuset.cpus is: %s", "%1023s", cpus, 1024);
60224f
+  return os::strdup(cpus);
60224f
+}
60224f
+
60224f
+char * OSContainer::cpu_cpuset_memory_nodes() {
60224f
+  GET_CONTAINER_INFO_CPTR(cptr, cpuset, "/cpuset.mems",
60224f
+                     "cpuset.mems is: %s", "%1023s", mems, 1024);
60224f
+  return os::strdup(mems);
60224f
+}
60224f
+
60224f
+/* cpu_quota
60224f
+ *
60224f
+ * Return the number of milliseconds per period
60224f
+ * process is guaranteed to run.
60224f
+ *
60224f
+ * return:
60224f
+ *    quota time in milliseconds
60224f
+ *    -1 for no quota
60224f
+ *    OSCONTAINER_ERROR for not supported
60224f
+ */
60224f
+int OSContainer::cpu_quota() {
60224f
+  GET_CONTAINER_INFO(int, cpu, "/cpu.cfs_quota_us",
60224f
+                     "CPU Quota is: %d", "%d", quota);
60224f
+  return quota;
60224f
+}
60224f
+
60224f
+int OSContainer::cpu_period() {
60224f
+  GET_CONTAINER_INFO(int, cpu, "/cpu.cfs_period_us",
60224f
+                     "CPU Period is: %d", "%d", period);
60224f
+  return period;
60224f
+}
60224f
+
60224f
+/* cpu_shares
60224f
+ *
60224f
+ * Return the amount of cpu shares available to the process
60224f
+ *
60224f
+ * return:
60224f
+ *    Share number (typically a number relative to 1024)
60224f
+ *                 (2048 typically expresses 2 CPUs worth of processing)
60224f
+ *    -1 for no share setup
60224f
+ *    OSCONTAINER_ERROR for not supported
60224f
+ */
60224f
+int OSContainer::cpu_shares() {
60224f
+  GET_CONTAINER_INFO(int, cpu, "/cpu.shares",
60224f
+                     "CPU Shares is: %d", "%d", shares);
60224f
+  // Convert 1024 to no shares setup
60224f
+  if (shares == 1024) return -1;
60224f
+
60224f
+  return shares;
60224f
+}
60224f
+
60224f
diff --git openjdk.orig/hotspot/src/os/linux/vm/osContainer_linux.hpp openjdk/hotspot/src/os/linux/vm/osContainer_linux.hpp
60224f
new file mode 100644
60224f
--- /dev/null
60224f
+++ openjdk/hotspot/src/os/linux/vm/osContainer_linux.hpp
60224f
@@ -0,0 +1,68 @@
60224f
+/*
60224f
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
60224f
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
60224f
+ *
60224f
+ * This code is free software; you can redistribute it and/or modify it
60224f
+ * under the terms of the GNU General Public License version 2 only, as
60224f
+ * published by the Free Software Foundation.
60224f
+ *
60224f
+ * This code is distributed in the hope that it will be useful, but WITHOUT
60224f
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
60224f
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
60224f
+ * version 2 for more details (a copy is included in the LICENSE file that
60224f
+ * accompanied this code).
60224f
+ *
60224f
+ * You should have received a copy of the GNU General Public License version
60224f
+ * 2 along with this work; if not, write to the Free Software Foundation,
60224f
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
60224f
+ *
60224f
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
60224f
+ * or visit www.oracle.com if you need additional information or have any
60224f
+ * questions.
60224f
+ *
60224f
+ */
60224f
+
60224f
+#ifndef OS_LINUX_VM_OSCONTAINER_LINUX_HPP
60224f
+#define OS_LINUX_VM_OSCONTAINER_LINUX_HPP
60224f
+
60224f
+#include "utilities/globalDefinitions.hpp"
60224f
+#include "utilities/macros.hpp"
60224f
+#include "memory/allocation.hpp"
60224f
+
60224f
+#define OSCONTAINER_ERROR (-2)
60224f
+
60224f
+class OSContainer: AllStatic {
60224f
+
60224f
+ private:
60224f
+  static bool   _is_initialized;
60224f
+  static bool   _is_containerized;
60224f
+
60224f
+ public:
60224f
+  static void init();
60224f
+  static inline bool is_containerized();
60224f
+  static const char * container_type();
60224f
+
60224f
+  static jlong memory_limit_in_bytes();
60224f
+  static jlong memory_and_swap_limit_in_bytes();
60224f
+  static jlong memory_soft_limit_in_bytes();
60224f
+  static jlong memory_usage_in_bytes();
60224f
+  static jlong memory_max_usage_in_bytes();
60224f
+
60224f
+  static int active_processor_count();
60224f
+
60224f
+  static char * cpu_cpuset_cpus();
60224f
+  static char * cpu_cpuset_memory_nodes();
60224f
+
60224f
+  static int cpu_quota();
60224f
+  static int cpu_period();
60224f
+
60224f
+  static int cpu_shares();
60224f
+
60224f
+};
60224f
+
60224f
+inline bool OSContainer::is_containerized() {
60224f
+  assert(_is_initialized, "OSContainer not initialized");
60224f
+  return _is_containerized;
60224f
+}
60224f
+
60224f
+#endif // OS_LINUX_VM_OSCONTAINER_LINUX_HPP
60224f
diff --git openjdk.orig/hotspot/src/os/linux/vm/os_linux.cpp openjdk/hotspot/src/os/linux/vm/os_linux.cpp
60224f
--- openjdk.orig/hotspot/src/os/linux/vm/os_linux.cpp
60224f
+++ openjdk/hotspot/src/os/linux/vm/os_linux.cpp
60224f
@@ -37,6 +37,7 @@
60224f
 #include "mutex_linux.inline.hpp"
60224f
 #include "oops/oop.inline.hpp"
60224f
 #include "os_share_linux.hpp"
60224f
+#include "osContainer_linux.hpp"
60224f
 #include "prims/jniFastGetField.hpp"
60224f
 #include "prims/jvm.h"
60224f
 #include "prims/jvm_misc.hpp"
60224f
@@ -179,13 +180,62 @@
60224f
 julong os::Linux::available_memory() {
60224f
   // values in struct sysinfo are "unsigned long"
60224f
   struct sysinfo si;
60224f
+  julong avail_mem;
60224f
+
60224f
+  if (OSContainer::is_containerized()) {
60224f
+    jlong mem_limit, mem_usage;
60224f
+    if ((mem_limit = OSContainer::memory_limit_in_bytes()) < 1) {
60224f
+      if (PrintContainerInfo) {
60224f
+        tty->print_cr("container memory limit %s: " JLONG_FORMAT ", using host value",
60224f
+                       mem_limit == OSCONTAINER_ERROR ? "failed" : "unlimited", mem_limit);
60224f
+      }
60224f
+    }
60224f
+
60224f
+    if (mem_limit > 0 && (mem_usage = OSContainer::memory_usage_in_bytes()) < 1) {
60224f
+      if (PrintContainerInfo) {
60224f
+        tty->print_cr("container memory usage failed: " JLONG_FORMAT ", using host value", mem_usage);
60224f
+      }
60224f
+    }
60224f
+
60224f
+    if (mem_limit > 0 && mem_usage > 0 ) {
60224f
+      avail_mem = mem_limit > mem_usage ? (julong)mem_limit - (julong)mem_usage : 0;
60224f
+      if (PrintContainerInfo) {
60224f
+        tty->print_cr("available container memory: " JULONG_FORMAT, avail_mem);
60224f
+      }
60224f
+      return avail_mem;
60224f
+    }
60224f
+  }
60224f
+
60224f
   sysinfo(&si);
60224f
-
60224f
-  return (julong)si.freeram * si.mem_unit;
60224f
+  avail_mem = (julong)si.freeram * si.mem_unit;
60224f
+  if (Verbose) {
60224f
+    tty->print_cr("available memory: " JULONG_FORMAT, avail_mem);
60224f
+  }
60224f
+  return avail_mem;
60224f
 }
60224f
 
60224f
 julong os::physical_memory() {
60224f
-  return Linux::physical_memory();
60224f
+  jlong phys_mem = 0;
60224f
+  if (OSContainer::is_containerized()) {
60224f
+    jlong mem_limit;
60224f
+    if ((mem_limit = OSContainer::memory_limit_in_bytes()) > 0) {
60224f
+      if (PrintContainerInfo) {
60224f
+        tty->print_cr("total container memory: " JLONG_FORMAT, mem_limit);
60224f
+      }
60224f
+      return mem_limit;
60224f
+    }
60224f
+
60224f
+    if (PrintContainerInfo) {
60224f
+      tty->print_cr("container memory limit %s: " JLONG_FORMAT ", using host value",
60224f
+                     mem_limit == OSCONTAINER_ERROR ? "failed" : "unlimited", mem_limit);
60224f
+    }
60224f
+  }
60224f
+
60224f
+  phys_mem = Linux::physical_memory();
60224f
+  if (Verbose) {
60224f
+    tty->print_cr("total system memory: " JLONG_FORMAT, phys_mem);
60224f
+  }
60224f
+  return phys_mem;
60224f
 }
60224f
 
60224f
 ////////////////////////////////////////////////////////////////////////////////
60224f
@@ -2129,6 +2179,8 @@
60224f
   os::Posix::print_load_average(st);
60224f
 
60224f
   os::Linux::print_full_memory_info(st);
60224f
+
60224f
+  os::Linux::print_container_info(st);
60224f
 }
60224f
 
60224f
 // Try to identify popular distros.
60224f
@@ -2194,6 +2246,57 @@
60224f
    st->cr();
60224f
 }
60224f
 
60224f
+void os::Linux::print_container_info(outputStream* st) {
60224f
+if (!OSContainer::is_containerized()) {
60224f
+    return;
60224f
+  }
60224f
+
60224f
+  st->print("container (cgroup) information:\n");
60224f
+
60224f
+  const char *p_ct = OSContainer::container_type();
60224f
+  st->print("container_type: %s\n", p_ct != NULL ? p_ct : "failed");
60224f
+
60224f
+  char *p = OSContainer::cpu_cpuset_cpus();
60224f
+  st->print("cpu_cpuset_cpus: %s\n", p != NULL ? p : "failed");
60224f
+  free(p);
60224f
+
60224f
+  p = OSContainer::cpu_cpuset_memory_nodes();
60224f
+  st->print("cpu_memory_nodes: %s\n", p != NULL ? p : "failed");
60224f
+  free(p);
60224f
+
60224f
+  int i = OSContainer::active_processor_count();
60224f
+  if (i > 0) {
60224f
+    st->print("active_processor_count: %d\n", i);
60224f
+  } else {
60224f
+    st->print("active_processor_count: failed\n");
60224f
+  }
60224f
+
60224f
+  i = OSContainer::cpu_quota();
60224f
+  st->print("cpu_quota: %d\n", i);
60224f
+
60224f
+  i = OSContainer::cpu_period();
60224f
+  st->print("cpu_period: %d\n", i);
60224f
+
60224f
+  i = OSContainer::cpu_shares();
60224f
+  st->print("cpu_shares: %d\n", i);
60224f
+
60224f
+  jlong j = OSContainer::memory_limit_in_bytes();
60224f
+  st->print("memory_limit_in_bytes: " JLONG_FORMAT "\n", j);
60224f
+
60224f
+  j = OSContainer::memory_and_swap_limit_in_bytes();
60224f
+  st->print("memory_and_swap_limit_in_bytes: " JLONG_FORMAT "\n", j);
60224f
+
60224f
+  j = OSContainer::memory_soft_limit_in_bytes();
60224f
+  st->print("memory_soft_limit_in_bytes: " JLONG_FORMAT "\n", j);
60224f
+
60224f
+  j = OSContainer::OSContainer::memory_usage_in_bytes();
60224f
+  st->print("memory_usage_in_bytes: " JLONG_FORMAT "\n", j);
60224f
+
60224f
+  j = OSContainer::OSContainer::memory_max_usage_in_bytes();
60224f
+  st->print("memory_max_usage_in_bytes: " JLONG_FORMAT "\n", j);
60224f
+  st->cr();
60224f
+}
60224f
+
60224f
 void os::print_memory_info(outputStream* st) {
60224f
 
60224f
   st->print("Memory:");
60224f
@@ -4966,6 +5069,10 @@
60224f
   }
60224f
 }
60224f
 
60224f
+void os::pd_init_container_support() {
60224f
+  OSContainer::init();
60224f
+}
60224f
+
60224f
 // this is called _after_ the global arguments have been parsed
60224f
 jint os::init_2(void)
60224f
 {
60224f
@@ -5146,7 +5253,7 @@
60224f
 // sched_getaffinity gives an accurate answer as it accounts for cpusets.
60224f
 // If anything goes wrong we fallback to returning the number of online
60224f
 // processors - which can be greater than the number available to the process.
60224f
-int os::active_processor_count() {
60224f
+int os::Linux::active_processor_count() {
60224f
   cpu_set_t cpus;  // can represent at most 1024 (CPU_SETSIZE) processors
60224f
   int cpus_size = sizeof(cpu_set_t);
60224f
   int cpu_count = 0;
60224f
@@ -5164,10 +5271,48 @@
60224f
             "which may exceed available processors", strerror(errno), cpu_count);
60224f
   }
60224f
 
60224f
-  assert(cpu_count > 0 && cpu_count <= processor_count(), "sanity check");
60224f
+  assert(cpu_count > 0 && cpu_count <= os::processor_count(), "sanity check");
60224f
   return cpu_count;
60224f
 }
60224f
 
60224f
+// Determine the active processor count from one of
60224f
+// three different sources:
60224f
+//
60224f
+// 1. User option -XX:ActiveProcessorCount
60224f
+// 2. kernel os calls (sched_getaffinity or sysconf(_SC_NPROCESSORS_ONLN)
60224f
+// 3. extracted from cgroup cpu subsystem (shares and quotas)
60224f
+//
60224f
+// Option 1, if specified, will always override.
60224f
+// If the cgroup subsystem is active and configured, we
60224f
+// will return the min of the cgroup and option 2 results.
60224f
+// This is required since tools, such as numactl, that
60224f
+// alter cpu affinity do not update cgroup subsystem
60224f
+// cpuset configuration files.
60224f
+int os::active_processor_count() {
60224f
+  // User has overridden the number of active processors
60224f
+  if (ActiveProcessorCount > 0) {
60224f
+    if (PrintActiveCpus) {
60224f
+      tty->print_cr("active_processor_count: "
60224f
+                    "active processor count set by user : %d",
60224f
+                    ActiveProcessorCount);
60224f
+    }
60224f
+    return ActiveProcessorCount;
60224f
+  }
60224f
+
60224f
+  int active_cpus;
60224f
+  if (OSContainer::is_containerized()) {
60224f
+    active_cpus = OSContainer::active_processor_count();
60224f
+    if (PrintActiveCpus) {
60224f
+      tty->print_cr("active_processor_count: determined by OSContainer: %d",
60224f
+                     active_cpus);
60224f
+    }
60224f
+  } else {
60224f
+    active_cpus = os::Linux::active_processor_count();
60224f
+  }
60224f
+
60224f
+  return active_cpus;
60224f
+}
60224f
+
60224f
 void os::set_native_thread_name(const char *name) {
60224f
   // Not yet implemented.
60224f
   return;
60224f
diff --git openjdk.orig/hotspot/src/os/linux/vm/os_linux.hpp openjdk/hotspot/src/os/linux/vm/os_linux.hpp
60224f
--- openjdk.orig/hotspot/src/os/linux/vm/os_linux.hpp
60224f
+++ openjdk/hotspot/src/os/linux/vm/os_linux.hpp
60224f
@@ -35,6 +35,7 @@
60224f
 
60224f
 class Linux {
60224f
   friend class os;
60224f
+  friend class OSContainer;
60224f
   friend class TestReserveMemorySpecial;
60224f
 
60224f
   // For signal-chaining
60224f
@@ -79,6 +80,9 @@
60224f
 
60224f
   static julong available_memory();
60224f
   static julong physical_memory() { return _physical_memory; }
60224f
+  static void set_physical_memory(julong phys_mem) { _physical_memory = phys_mem; }
60224f
+  static int active_processor_count();
60224f
+
60224f
   static void initialize_system_info();
60224f
 
60224f
   static int commit_memory_impl(char* addr, size_t bytes, bool exec);
60224f
@@ -116,6 +120,7 @@
60224f
   static bool release_memory_special_huge_tlbfs(char* base, size_t bytes);
60224f
 
60224f
   static void print_full_memory_info(outputStream* st);
60224f
+  static void print_container_info(outputStream* st);
60224f
   static void print_distro_info(outputStream* st);
60224f
   static void print_libversion_info(outputStream* st);
60224f
 
60224f
diff --git openjdk.orig/hotspot/src/os/solaris/vm/os_solaris.cpp openjdk/hotspot/src/os/solaris/vm/os_solaris.cpp
60224f
--- openjdk.orig/hotspot/src/os/solaris/vm/os_solaris.cpp
60224f
+++ openjdk/hotspot/src/os/solaris/vm/os_solaris.cpp
60224f
@@ -357,6 +357,16 @@
60224f
 }
60224f
 
60224f
 int os::active_processor_count() {
60224f
+  // User has overridden the number of active processors
60224f
+  if (ActiveProcessorCount > 0) {
60224f
+    if (Verbose) {
60224f
+      tty->print_cr("active_processor_count: "
60224f
+                    "active processor count set by user : %d",
60224f
+                     ActiveProcessorCount);
60224f
+    }
60224f
+    return ActiveProcessorCount;
60224f
+  }
60224f
+
60224f
   int online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
60224f
   pid_t pid = getpid();
60224f
   psetid_t pset = PS_NONE;
60224f
diff --git openjdk.orig/hotspot/src/os/windows/vm/os_windows.cpp openjdk/hotspot/src/os/windows/vm/os_windows.cpp
60224f
--- openjdk.orig/hotspot/src/os/windows/vm/os_windows.cpp
60224f
+++ openjdk/hotspot/src/os/windows/vm/os_windows.cpp
60224f
@@ -716,6 +716,16 @@
60224f
 #endif
60224f
 
60224f
 int os::active_processor_count() {
60224f
+  // User has overridden the number of active processors
60224f
+  if (ActiveProcessorCount > 0) {
60224f
+    if (PrintActiveCpus) {
60224f
+      tty->print_cr("active_processor_count: "
60224f
+                    "active processor count set by user : %d",
60224f
+                     ActiveProcessorCount);
60224f
+    }
60224f
+    return ActiveProcessorCount;
60224f
+  }
60224f
+
60224f
   DWORD_PTR lpProcessAffinityMask = 0;
60224f
   DWORD_PTR lpSystemAffinityMask = 0;
60224f
   int proc_count = processor_count();
60224f
diff --git openjdk.orig/hotspot/src/share/vm/runtime/arguments.cpp openjdk/hotspot/src/share/vm/runtime/arguments.cpp
60224f
--- openjdk.orig/hotspot/src/share/vm/runtime/arguments.cpp
60224f
+++ openjdk/hotspot/src/share/vm/runtime/arguments.cpp
60224f
@@ -1,5 +1,5 @@
60224f
 /*
60224f
- * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
60224f
+ * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
60224f
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
60224f
  *
60224f
  * This code is free software; you can redistribute it and/or modify it
60224f
@@ -1801,20 +1801,34 @@
60224f
     }
60224f
   }
60224f
 
60224f
+  // Convert Fraction to Precentage values
60224f
+  if (FLAG_IS_DEFAULT(MaxRAMPercentage) &&
60224f
+      !FLAG_IS_DEFAULT(MaxRAMFraction))
60224f
+    MaxRAMPercentage = 100.0 / MaxRAMFraction;
60224f
+
60224f
+   if (FLAG_IS_DEFAULT(MinRAMPercentage) &&
60224f
+       !FLAG_IS_DEFAULT(MinRAMFraction))
60224f
+     MinRAMPercentage = 100.0 / MinRAMFraction;
60224f
+
60224f
+   if (FLAG_IS_DEFAULT(InitialRAMPercentage) &&
60224f
+       !FLAG_IS_DEFAULT(InitialRAMFraction))
60224f
+     InitialRAMPercentage = 100.0 / InitialRAMFraction;
60224f
+
60224f
   // If the maximum heap size has not been set with -Xmx,
60224f
   // then set it as fraction of the size of physical memory,
60224f
   // respecting the maximum and minimum sizes of the heap.
60224f
   if (FLAG_IS_DEFAULT(MaxHeapSize)) {
60224f
-    julong reasonable_max = phys_mem / MaxRAMFraction;
60224f
-
60224f
-    if (phys_mem <= MaxHeapSize * MinRAMFraction) {
60224f
+    julong reasonable_max = (julong)((phys_mem * MaxRAMPercentage) / 100);
60224f
+    const julong reasonable_min = (julong)((phys_mem * MinRAMPercentage) / 100);
60224f
+    if (reasonable_min < MaxHeapSize) {
60224f
       // Small physical memory, so use a minimum fraction of it for the heap
60224f
-      reasonable_max = phys_mem / MinRAMFraction;
60224f
+      reasonable_max = reasonable_min;
60224f
     } else {
60224f
       // Not-small physical memory, so require a heap at least
60224f
       // as large as MaxHeapSize
60224f
       reasonable_max = MAX2(reasonable_max, (julong)MaxHeapSize);
60224f
     }
60224f
+
60224f
     if (!FLAG_IS_DEFAULT(ErgoHeapSizeLimit) && ErgoHeapSizeLimit != 0) {
60224f
       // Limit the heap size to ErgoHeapSizeLimit
60224f
       reasonable_max = MIN2(reasonable_max, (julong)ErgoHeapSizeLimit);
60224f
@@ -1856,7 +1870,7 @@
60224f
     reasonable_minimum = limit_by_allocatable_memory(reasonable_minimum);
60224f
 
60224f
     if (InitialHeapSize == 0) {
60224f
-      julong reasonable_initial = phys_mem / InitialRAMFraction;
60224f
+      julong reasonable_initial = (julong)((phys_mem * InitialRAMPercentage) / 100);
60224f
 
60224f
       reasonable_initial = MAX3(reasonable_initial, reasonable_minimum, (julong)min_heap_size());
60224f
       reasonable_initial = MIN2(reasonable_initial, (julong)MaxHeapSize);
60224f
@@ -1881,6 +1895,94 @@
60224f
   }
60224f
 }
60224f
 
60224f
+// This option inspects the machine and attempts to set various
60224f
+// parameters to be optimal for long-running, memory allocation
60224f
+// intensive jobs.  It is intended for machines with large
60224f
+// amounts of cpu and memory.
60224f
+jint Arguments::set_aggressive_heap_flags() {
60224f
+  // initHeapSize is needed since _initial_heap_size is 4 bytes on a 32 bit
60224f
+  // VM, but we may not be able to represent the total physical memory
60224f
+  // available (like having 8gb of memory on a box but using a 32bit VM).
60224f
+  // Thus, we need to make sure we're using a julong for intermediate
60224f
+  // calculations.
60224f
+  julong initHeapSize;
60224f
+  julong total_memory = os::physical_memory();
60224f
+
60224f
+  if (total_memory < (julong) 256 * M) {
60224f
+    jio_fprintf(defaultStream::error_stream(),
60224f
+            "You need at least 256mb of memory to use -XX:+AggressiveHeap\n");
60224f
+    vm_exit(1);
60224f
+  }
60224f
+
60224f
+  // The heap size is half of available memory, or (at most)
60224f
+  // all of possible memory less 160mb (leaving room for the OS
60224f
+  // when using ISM).  This is the maximum; because adaptive sizing
60224f
+  // is turned on below, the actual space used may be smaller.
60224f
+
60224f
+  initHeapSize = MIN2(total_memory / (julong) 2,
60224f
+                      total_memory - (julong) 160 * M);
60224f
+
60224f
+  initHeapSize = limit_by_allocatable_memory(initHeapSize);
60224f
+
60224f
+  if (FLAG_IS_DEFAULT(MaxHeapSize)) {
60224f
+    FLAG_SET_CMDLINE(uintx, MaxHeapSize, initHeapSize);
60224f
+    FLAG_SET_CMDLINE(uintx, InitialHeapSize, initHeapSize);
60224f
+    // Currently the minimum size and the initial heap sizes are the same.
60224f
+    set_min_heap_size(initHeapSize);
60224f
+  }
60224f
+  if (FLAG_IS_DEFAULT(NewSize)) {
60224f
+    // Make the young generation 3/8ths of the total heap.
60224f
+    FLAG_SET_CMDLINE(uintx, NewSize,
60224f
+            ((julong) MaxHeapSize / (julong) 8) * (julong) 3);
60224f
+    FLAG_SET_CMDLINE(uintx, MaxNewSize, NewSize);
60224f
+  }
60224f
+
60224f
+#ifndef _ALLBSD_SOURCE  // UseLargePages is not yet supported on BSD.
60224f
+  FLAG_SET_DEFAULT(UseLargePages, true);
60224f
+#endif
60224f
+
60224f
+  // Increase some data structure sizes for efficiency
60224f
+  FLAG_SET_CMDLINE(uintx, BaseFootPrintEstimate, MaxHeapSize);
60224f
+  FLAG_SET_CMDLINE(bool, ResizeTLAB, false);
60224f
+  FLAG_SET_CMDLINE(uintx, TLABSize, 256 * K);
60224f
+
60224f
+  // See the OldPLABSize comment below, but replace 'after promotion'
60224f
+  // with 'after copying'.  YoungPLABSize is the size of the survivor
60224f
+  // space per-gc-thread buffers.  The default is 4kw.
60224f
+  FLAG_SET_CMDLINE(uintx, YoungPLABSize, 256 * K);     // Note: this is in words
60224f
+
60224f
+  // OldPLABSize is the size of the buffers in the old gen that
60224f
+  // UseParallelGC uses to promote live data that doesn't fit in the
60224f
+  // survivor spaces.  At any given time, there's one for each gc thread.
60224f
+  // The default size is 1kw. These buffers are rarely used, since the
60224f
+  // survivor spaces are usually big enough.  For specjbb, however, there
60224f
+  // are occasions when there's lots of live data in the young gen
60224f
+  // and we end up promoting some of it.  We don't have a definite
60224f
+  // explanation for why bumping OldPLABSize helps, but the theory
60224f
+  // is that a bigger PLAB results in retaining something like the
60224f
+  // original allocation order after promotion, which improves mutator
60224f
+  // locality.  A minor effect may be that larger PLABs reduce the
60224f
+  // number of PLAB allocation events during gc.  The value of 8kw
60224f
+  // was arrived at by experimenting with specjbb.
60224f
+  FLAG_SET_CMDLINE(uintx, OldPLABSize, 8 * K);      // Note: this is in words
60224f
+
60224f
+  // Enable parallel GC and adaptive generation sizing
60224f
+  FLAG_SET_CMDLINE(bool, UseParallelGC, true);
60224f
+
60224f
+  // Encourage steady state memory management
60224f
+  FLAG_SET_CMDLINE(uintx, ThresholdTolerance, 100);
60224f
+
60224f
+  // This appears to improve mutator locality
60224f
+  FLAG_SET_CMDLINE(bool, ScavengeBeforeFullGC, false);
60224f
+
60224f
+  // Get around early Solaris scheduling bug
60224f
+  // (affinity vs other jobs on system)
60224f
+  // but disallow DR and offlining (5008695).
60224f
+  FLAG_SET_CMDLINE(bool, BindGCTaskThreadsToCPUs, true);
60224f
+
60224f
+  return JNI_OK;
60224f
+}
60224f
+
60224f
 // This must be called after ergonomics because we want bytecode rewriting
60224f
 // if the server compiler is used, or if UseSharedSpaces is disabled.
60224f
 void Arguments::set_bytecode_flags() {
60224f
@@ -2644,6 +2746,14 @@
60224f
     return result;
60224f
   }
60224f
 
60224f
+  // We need to ensure processor and memory resources have been properly
60224f
+  // configured - which may rely on arguments we just processed - before
60224f
+  // doing the final argument processing. Any argument processing that
60224f
+  // needs to know about processor and memory resources must occur after
60224f
+  // this point.
60224f
+
60224f
+  os::init_container_support();
60224f
+
60224f
   // Do final processing now that all arguments have been parsed
60224f
   result = finalize_vm_init_args(&scp, scp_assembly_required);
60224f
   if (result != JNI_OK) {
60224f
@@ -3117,94 +3227,6 @@
60224f
       _exit_hook = CAST_TO_FN_PTR(exit_hook_t, option->extraInfo);
60224f
     } else if (match_option(option, "abort", &tail)) {
60224f
       _abort_hook = CAST_TO_FN_PTR(abort_hook_t, option->extraInfo);
60224f
-    // -XX:+AggressiveHeap
60224f
-    } else if (match_option(option, "-XX:+AggressiveHeap", &tail)) {
60224f
-
60224f
-      // This option inspects the machine and attempts to set various
60224f
-      // parameters to be optimal for long-running, memory allocation
60224f
-      // intensive jobs.  It is intended for machines with large
60224f
-      // amounts of cpu and memory.
60224f
-
60224f
-      // initHeapSize is needed since _initial_heap_size is 4 bytes on a 32 bit
60224f
-      // VM, but we may not be able to represent the total physical memory
60224f
-      // available (like having 8gb of memory on a box but using a 32bit VM).
60224f
-      // Thus, we need to make sure we're using a julong for intermediate
60224f
-      // calculations.
60224f
-      julong initHeapSize;
60224f
-      julong total_memory = os::physical_memory();
60224f
-
60224f
-      if (total_memory < (julong)256*M) {
60224f
-        jio_fprintf(defaultStream::error_stream(),
60224f
-                    "You need at least 256mb of memory to use -XX:+AggressiveHeap\n");
60224f
-        vm_exit(1);
60224f
-      }
60224f
-
60224f
-      // The heap size is half of available memory, or (at most)
60224f
-      // all of possible memory less 160mb (leaving room for the OS
60224f
-      // when using ISM).  This is the maximum; because adaptive sizing
60224f
-      // is turned on below, the actual space used may be smaller.
60224f
-
60224f
-      initHeapSize = MIN2(total_memory / (julong)2,
60224f
-                          total_memory - (julong)160*M);
60224f
-
60224f
-      initHeapSize = limit_by_allocatable_memory(initHeapSize);
60224f
-
60224f
-      if (FLAG_IS_DEFAULT(MaxHeapSize)) {
60224f
-         FLAG_SET_CMDLINE(uintx, MaxHeapSize, initHeapSize);
60224f
-         FLAG_SET_CMDLINE(uintx, InitialHeapSize, initHeapSize);
60224f
-         // Currently the minimum size and the initial heap sizes are the same.
60224f
-         set_min_heap_size(initHeapSize);
60224f
-      }
60224f
-      if (FLAG_IS_DEFAULT(NewSize)) {
60224f
-         // Make the young generation 3/8ths of the total heap.
60224f
-         FLAG_SET_CMDLINE(uintx, NewSize,
60224f
-                                ((julong)MaxHeapSize / (julong)8) * (julong)3);
60224f
-         FLAG_SET_CMDLINE(uintx, MaxNewSize, NewSize);
60224f
-      }
60224f
-
60224f
-#ifndef _ALLBSD_SOURCE  // UseLargePages is not yet supported on BSD.
60224f
-      FLAG_SET_DEFAULT(UseLargePages, true);
60224f
-#endif
60224f
-
60224f
-      // Increase some data structure sizes for efficiency
60224f
-      FLAG_SET_CMDLINE(uintx, BaseFootPrintEstimate, MaxHeapSize);
60224f
-      FLAG_SET_CMDLINE(bool, ResizeTLAB, false);
60224f
-      FLAG_SET_CMDLINE(uintx, TLABSize, 256*K);
60224f
-
60224f
-      // See the OldPLABSize comment below, but replace 'after promotion'
60224f
-      // with 'after copying'.  YoungPLABSize is the size of the survivor
60224f
-      // space per-gc-thread buffers.  The default is 4kw.
60224f
-      FLAG_SET_CMDLINE(uintx, YoungPLABSize, 256*K);      // Note: this is in words
60224f
-
60224f
-      // OldPLABSize is the size of the buffers in the old gen that
60224f
-      // UseParallelGC uses to promote live data that doesn't fit in the
60224f
-      // survivor spaces.  At any given time, there's one for each gc thread.
60224f
-      // The default size is 1kw. These buffers are rarely used, since the
60224f
-      // survivor spaces are usually big enough.  For specjbb, however, there
60224f
-      // are occasions when there's lots of live data in the young gen
60224f
-      // and we end up promoting some of it.  We don't have a definite
60224f
-      // explanation for why bumping OldPLABSize helps, but the theory
60224f
-      // is that a bigger PLAB results in retaining something like the
60224f
-      // original allocation order after promotion, which improves mutator
60224f
-      // locality.  A minor effect may be that larger PLABs reduce the
60224f
-      // number of PLAB allocation events during gc.  The value of 8kw
60224f
-      // was arrived at by experimenting with specjbb.
60224f
-      FLAG_SET_CMDLINE(uintx, OldPLABSize, 8*K);  // Note: this is in words
60224f
-
60224f
-      // Enable parallel GC and adaptive generation sizing
60224f
-      FLAG_SET_CMDLINE(bool, UseParallelGC, true);
60224f
-
60224f
-      // Encourage steady state memory management
60224f
-      FLAG_SET_CMDLINE(uintx, ThresholdTolerance, 100);
60224f
-
60224f
-      // This appears to improve mutator locality
60224f
-      FLAG_SET_CMDLINE(bool, ScavengeBeforeFullGC, false);
60224f
-
60224f
-      // Get around early Solaris scheduling bug
60224f
-      // (affinity vs other jobs on system)
60224f
-      // but disallow DR and offlining (5008695).
60224f
-      FLAG_SET_CMDLINE(bool, BindGCTaskThreadsToCPUs, true);
60224f
-
60224f
     } else if (match_option(option, "-XX:+NeverTenure", &tail)) {
60224f
       // The last option must always win.
60224f
       FLAG_SET_CMDLINE(bool, AlwaysTenure, false);
60224f
@@ -3605,6 +3627,15 @@
60224f
     return JNI_ERR;
60224f
   }
60224f
 
60224f
+  // This must be done after all arguments have been processed
60224f
+  // and the container support has been initialized since AggressiveHeap
60224f
+  // relies on the amount of total memory available.
60224f
+  if (AggressiveHeap) {
60224f
+    jint result = set_aggressive_heap_flags();
60224f
+    if (result != JNI_OK) {
60224f
+      return result;
60224f
+    }
60224f
+  }
60224f
   // This must be done after all arguments have been processed.
60224f
   // java_compiler() true means set to "NONE" or empty.
60224f
   if (java_compiler() && !xdebug_mode()) {
60224f
diff --git openjdk.orig/hotspot/src/share/vm/runtime/arguments.hpp openjdk/hotspot/src/share/vm/runtime/arguments.hpp
60224f
--- openjdk.orig/hotspot/src/share/vm/runtime/arguments.hpp
60224f
+++ openjdk/hotspot/src/share/vm/runtime/arguments.hpp
60224f
@@ -1,5 +1,5 @@
60224f
 /*
60224f
- * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
60224f
+ * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
60224f
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
60224f
  *
60224f
  * This code is free software; you can redistribute it and/or modify it
60224f
@@ -365,6 +365,8 @@
60224f
   // Aggressive optimization flags.
60224f
   static void set_aggressive_opts_flags();
60224f
 
60224f
+  static jint set_aggressive_heap_flags();
60224f
+
60224f
   // Argument parsing
60224f
   static void do_pd_flag_adjustments();
60224f
   static bool parse_argument(const char* arg, Flag::Flags origin);
60224f
diff --git openjdk.orig/hotspot/src/share/vm/runtime/globals.hpp openjdk/hotspot/src/share/vm/runtime/globals.hpp
60224f
--- openjdk.orig/hotspot/src/share/vm/runtime/globals.hpp
60224f
+++ openjdk/hotspot/src/share/vm/runtime/globals.hpp
60224f
@@ -2076,13 +2076,23 @@
60224f
   product_pd(uint64_t, MaxRAM,                                              \
60224f
           "Real memory size (in bytes) used to set maximum heap size")      \
60224f
                                                                             \
60224f
+  product(bool, AggressiveHeap, false,                                      \
60224f
+          "Optimize heap options for long-running memory intensive apps")   \
60224f
+                                                                            \
60224f
   product(uintx, ErgoHeapSizeLimit, 0,                                      \
60224f
           "Maximum ergonomically set heap size (in bytes); zero means use " \
60224f
-          "MaxRAM / MaxRAMFraction")                                        \
60224f
+          "MaxRAM * MaxRAMPercentage / 100")                                \
60224f
                                                                             \
60224f
   experimental(bool, UseCGroupMemoryLimitForHeap, false,                    \
60224f
           "Use CGroup memory limit as physical memory limit for heap "      \
60224f
-          "sizing")                                                         \
60224f
+          "sizing"                                                          \
60224f
+          "Deprecated, replaced by container support")                      \
60224f
+                                                                            \
60224f
+  diagnostic(bool, PrintContainerInfo, false,                               \
60224f
+          "Print container related information")                            \
60224f
+                                                                            \
60224f
+  diagnostic(bool, PrintActiveCpus, false,                                  \
60224f
+           "Print the number of CPUs detected in os::active_processor_count") \
60224f
                                                                             \
60224f
   product(uintx, MaxRAMFraction, 4,                                         \
60224f
           "Maximum fraction (1/n) of real memory used for maximum heap "    \
60224f
@@ -2099,6 +2109,19 @@
60224f
   product(uintx, InitialRAMFraction, 64,                                    \
60224f
           "Fraction (1/n) of real memory used for initial heap size")       \
60224f
                                                                             \
60224f
+  product(double, MaxRAMPercentage, 25.0,                                   \
60224f
+          "Maximum percentage of real memory used for maximum heap size")   \
60224f
+                                                                            \
60224f
+  product(double, MinRAMPercentage, 50.0,                                   \
60224f
+          "Minimum percentage of real memory used for maximum heap"         \
60224f
+          "size on systems with small physical memory size")                \
60224f
+                                                                            \
60224f
+  product(double, InitialRAMPercentage, 1.5625,                             \
60224f
+          "Percentage of real memory used for initial heap size")           \
60224f
+                                                                            \
60224f
+  product(intx, ActiveProcessorCount, -1,                                   \
60224f
+          "Specify the CPU count the VM should use and report as active")   \
60224f
+                                                                            \
60224f
   develop(uintx, MaxVirtMemFraction, 2,                                     \
60224f
           "Maximum fraction (1/n) of virtual memory used for ergonomically "\
60224f
           "determining maximum heap size")                                  \
60224f
diff --git openjdk.orig/hotspot/src/share/vm/runtime/os.hpp openjdk/hotspot/src/share/vm/runtime/os.hpp
60224f
--- openjdk.orig/hotspot/src/share/vm/runtime/os.hpp
60224f
+++ openjdk/hotspot/src/share/vm/runtime/os.hpp
60224f
@@ -152,8 +152,16 @@
60224f
   static size_t page_size_for_region(size_t region_size, size_t min_pages, bool must_be_aligned);
60224f
 
60224f
   static void initialize_initial_active_processor_count();
60224f
+
60224f
+  LINUX_ONLY(static void pd_init_container_support();)
60224f
+
60224f
  public:
60224f
   static void init(void);                      // Called before command line parsing
60224f
+
60224f
+  static void init_container_support() {       // Called during command line parsing.
60224f
+     LINUX_ONLY(pd_init_container_support();)
60224f
+  }
60224f
+
60224f
   static void init_before_ergo(void);          // Called after command line parsing
60224f
                                                // before VM ergonomics processing.
60224f
   static jint init_2(void);                    // Called after command line parsing
60224f
diff --git openjdk.orig/hotspot/src/share/vm/runtime/thread.cpp openjdk/hotspot/src/share/vm/runtime/thread.cpp
60224f
--- openjdk.orig/hotspot/src/share/vm/runtime/thread.cpp
60224f
+++ openjdk/hotspot/src/share/vm/runtime/thread.cpp
60224f
@@ -3332,6 +3332,7 @@
60224f
   Arguments::init_version_specific_system_properties();
60224f
 
60224f
   // Parse arguments
60224f
+  // Note: this internally calls os::init_container_support()
60224f
   jint parse_result = Arguments::parse(args);
60224f
   if (parse_result != JNI_OK) return parse_result;
60224f