8217766: Container Support doesn't work for some Join Controllers combinations
authorbobv
Tue, 19 Mar 2019 12:29:40 -0400
changeset 54193 d5da034032e9
parent 54192 d909d0a883c4
child 54194 c2238a12f259
8217766: Container Support doesn't work for some Join Controllers combinations Reviewed-by: rriggs, sgehwolf
src/hotspot/os/linux/osContainer_linux.cpp
src/java.base/linux/classes/jdk/internal/platform/cgroupv1/Metrics.java
--- a/src/hotspot/os/linux/osContainer_linux.cpp	Tue Mar 19 12:10:00 2019 -0400
+++ b/src/hotspot/os/linux/osContainer_linux.cpp	Tue Mar 19 12:29:40 2019 -0400
@@ -217,16 +217,11 @@
  * we are running under cgroup control.
  */
 void OSContainer::init() {
-  int mountid;
-  int parentid;
-  int major;
-  int minor;
   FILE *mntinfo = NULL;
   FILE *cgroup = NULL;
   char buf[MAXPATHLEN+1];
   char tmproot[MAXPATHLEN+1];
   char tmpmount[MAXPATHLEN+1];
-  char tmpbase[MAXPATHLEN+1];
   char *p;
   jlong mem_limit;
 
@@ -260,85 +255,24 @@
       return;
   }
 
-  while ( (p = fgets(buf, MAXPATHLEN, mntinfo)) != NULL) {
-    // Look for the filesystem type and see if it's cgroup
-    char fstype[MAXPATHLEN+1];
-    fstype[0] = '\0';
-    char *s =  strstr(p, " - ");
-    if (s != NULL &&
-        sscanf(s, " - %s", fstype) == 1 &&
-        strcmp(fstype, "cgroup") == 0) {
+  while ((p = fgets(buf, MAXPATHLEN, mntinfo)) != NULL) {
+    char tmpcgroups[MAXPATHLEN+1];
+    char *cptr = tmpcgroups;
+    char *token;
 
-      if (strstr(p, "memory") != NULL) {
-        int matched = sscanf(p, "%d %d %d:%d %s %s",
-                             &mountid,
-                             &parentid,
-                             &major,
-                             &minor,
-                             tmproot,
-                             tmpmount);
-        if (matched == 6) {
-          memory = new CgroupSubsystem(tmproot, tmpmount);
-        }
-        else
-          log_debug(os, container)("Incompatible str containing cgroup and memory: %s", p);
-      } else if (strstr(p, "cpuset") != NULL) {
-        int matched = sscanf(p, "%d %d %d:%d %s %s",
-                             &mountid,
-                             &parentid,
-                             &major,
-                             &minor,
-                             tmproot,
-                             tmpmount);
-        if (matched == 6) {
-          cpuset = new CgroupSubsystem(tmproot, tmpmount);
-        }
-        else {
-          log_debug(os, container)("Incompatible str containing cgroup and cpuset: %s", p);
-        }
-      } else if (strstr(p, "cpu,cpuacct") != NULL || strstr(p, "cpuacct,cpu") != NULL) {
-        int matched = sscanf(p, "%d %d %d:%d %s %s",
-                             &mountid,
-                             &parentid,
-                             &major,
-                             &minor,
-                             tmproot,
-                             tmpmount);
-        if (matched == 6) {
-          cpu = new CgroupSubsystem(tmproot, tmpmount);
-          cpuacct = new CgroupSubsystem(tmproot, tmpmount);
-        }
-        else {
-          log_debug(os, container)("Incompatible str containing cgroup and cpu,cpuacct: %s", p);
-        }
-      } else if (strstr(p, "cpuacct") != NULL) {
-        int matched = sscanf(p, "%d %d %d:%d %s %s",
-                             &mountid,
-                             &parentid,
-                             &major,
-                             &minor,
-                             tmproot,
-                             tmpmount);
-        if (matched == 6) {
-          cpuacct = new CgroupSubsystem(tmproot, tmpmount);
-        }
-        else {
-          log_debug(os, container)("Incompatible str containing cgroup and cpuacct: %s", p);
-        }
-      } else if (strstr(p, "cpu") != NULL) {
-        int matched = sscanf(p, "%d %d %d:%d %s %s",
-                             &mountid,
-                             &parentid,
-                             &major,
-                             &minor,
-                             tmproot,
-                             tmpmount);
-        if (matched == 6) {
-          cpu = new CgroupSubsystem(tmproot, tmpmount);
-        }
-        else {
-          log_debug(os, container)("Incompatible str containing cgroup and cpu: %s", p);
-        }
+    // mountinfo format is documented at https://www.kernel.org/doc/Documentation/filesystems/proc.txt
+    if (sscanf(p, "%*d %*d %*d:%*d %s %s %*[^-]- cgroup %*s %s", tmproot, tmpmount, tmpcgroups) != 3) {
+      continue;
+    }
+    while ((token = strsep(&cptr, ",")) != NULL) {
+      if (strcmp(token, "memory") == 0) {
+        memory = new CgroupSubsystem(tmproot, tmpmount);
+      } else if (strcmp(token, "cpuset") == 0) {
+        cpuset = new CgroupSubsystem(tmproot, tmpmount);
+      } else if (strcmp(token, "cpu") == 0) {
+        cpu = new CgroupSubsystem(tmproot, tmpmount);
+      } else if (strcmp(token, "cpuacct") == 0) {
+        cpuacct= new CgroupSubsystem(tmproot, tmpmount);
       }
     }
   }
@@ -392,30 +326,30 @@
     return;
   }
 
-  while ( (p = fgets(buf, MAXPATHLEN, cgroup)) != NULL) {
-    int cgno;
-    int matched;
-    char *controller;
+  while ((p = fgets(buf, MAXPATHLEN, cgroup)) != NULL) {
+    char *controllers;
+    char *token;
     char *base;
 
     /* Skip cgroup number */
     strsep(&p, ":");
-    /* Get controller and base */
-    controller = strsep(&p, ":");
+    /* Get controllers and base */
+    controllers = strsep(&p, ":");
     base = strsep(&p, "\n");
 
-    if (controller != NULL) {
-      if (strstr(controller, "memory") != NULL) {
+    if (controllers == NULL) {
+      continue;
+    }
+
+    while ((token = strsep(&controllers, ",")) != NULL) {
+      if (strcmp(token, "memory") == 0) {
         memory->set_subsystem_path(base);
-      } else if (strstr(controller, "cpuset") != NULL) {
+      } else if (strcmp(token, "cpuset") == 0) {
         cpuset->set_subsystem_path(base);
-      } else if (strstr(controller, "cpu,cpuacct") != NULL || strstr(controller, "cpuacct,cpu") != NULL) {
+      } else if (strcmp(token, "cpu") == 0) {
         cpu->set_subsystem_path(base);
+      } else if (strcmp(token, "cpuacct") == 0) {
         cpuacct->set_subsystem_path(base);
-      } else if (strstr(controller, "cpuacct") != NULL) {
-        cpuacct->set_subsystem_path(base);
-      } else if (strstr(controller, "cpu") != NULL) {
-        cpu->set_subsystem_path(base);
       }
     }
   }
--- a/src/java.base/linux/classes/jdk/internal/platform/cgroupv1/Metrics.java	Tue Mar 19 12:10:00 2019 -0400
+++ b/src/java.base/linux/classes/jdk/internal/platform/cgroupv1/Metrics.java	Tue Mar 19 12:29:40 2019 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -124,13 +124,13 @@
     /**
      * createSubSystem objects and initialize mount points
      */
-    private static void createSubSystem(Metrics metric, String [] mountentry) {
+    private static void createSubSystem(Metrics metric, String[] mountentry) {
         if (mountentry.length < 5) return;
 
         Path p = Paths.get(mountentry[4]);
-        String subsystemName = p.getFileName().toString();
+        String[] subsystemNames = p.getFileName().toString().split(",");
 
-        if (subsystemName != null) {
+        for (String subsystemName: subsystemNames) {
             switch (subsystemName) {
                 case "memory":
                     metric.setMemorySubSystem(new SubSystem(mountentry[3], mountentry[4]));
@@ -138,11 +138,6 @@
                 case "cpuset":
                     metric.setCpuSetSubSystem(new SubSystem(mountentry[3], mountentry[4]));
                     break;
-                case "cpu,cpuacct":
-                case "cpuacct,cpu":
-                    metric.setCpuSubSystem(new SubSystem(mountentry[3], mountentry[4]));
-                    metric.setCpuAcctSubSystem(new SubSystem(mountentry[3], mountentry[4]));
-                    break;
                 case "cpuacct":
                     metric.setCpuAcctSubSystem(new SubSystem(mountentry[3], mountentry[4]));
                     break;
@@ -162,7 +157,7 @@
     /**
      * setSubSystemPath based on the contents of /proc/self/cgroup
      */
-    private static void setSubSystemPath(Metrics metric, String [] entry) {
+    private static void setSubSystemPath(Metrics metric, String[] entry) {
         String controller;
         String base;
         SubSystem subsystem = null;