< prev index next >

test/jdk/jdk/internal/platform/docker/TestDockerMemoryMetrics.java

Print this page
o  rev 57733 : 8231111: Cgroups v2: Rework Metrics in java.base so as to recognize unified hierarchy
|  Reviewed-by: bobv, mchung
~
o  rev 56862 : 8231111: Cgroups v2: Rework Metrics in java.base so as to recognize unified hierarchy
|  Reviewed-by: bobv
~


   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 import jdk.test.lib.Utils;
  25 import jdk.test.lib.containers.docker.Common;
  26 import jdk.test.lib.containers.docker.DockerRunOptions;
  27 import jdk.test.lib.containers.docker.DockerTestUtils;
  28 import jdk.test.lib.process.OutputAnalyzer;
  29 
  30 /*
  31  * @test
  32  * @summary Test JDK Metrics class when running inside docker container
  33  * @requires docker.support
  34  * @library /test/lib
  35  * @modules java.base/jdk.internal.platform
  36  * @build MetricsMemoryTester
  37  * @run main/timeout=360 TestDockerMemoryMetrics
  38  */
  39 
  40 public class TestDockerMemoryMetrics {
  41     private static final String imageName = Common.imageName("metrics-memory");
  42 
  43     public static void main(String[] args) throws Exception {
  44         if (!DockerTestUtils.canTestDocker()) {
  45             return;
  46         }
  47 
  48         // These tests create a docker image and run this image with
  49         // varying docker memory options.  The arguments passed to the docker
  50         // container include the Java test class to be run along with the
  51         // resource to be examined and expected result.
  52 
  53         DockerTestUtils.buildJdkDockerImage(imageName, "Dockerfile-BasicTest", "jdk-docker");
  54         try {
  55             testMemoryLimit("200m");
  56             testMemoryLimit("1g");
  57 
  58             testMemoryAndSwapLimit("200m", "1g");
  59             testMemoryAndSwapLimit("100m", "200m");
  60 






  61             testKernelMemoryLimit("100m");
  62             testKernelMemoryLimit("1g");
  63 
  64             testOomKillFlag("100m", false);





  65             testOomKillFlag("100m", true);
  66 
  67             testMemoryFailCount("64m");
  68 
  69             testMemorySoftLimit("500m","200m");
  70 
  71         } finally {

  72             DockerTestUtils.removeDockerImage(imageName);
  73         }
  74     }

  75 
  76     private static void testMemoryLimit(String value) throws Exception {
  77         Common.logNewTestCase("testMemoryLimit, value = " + value);
  78         DockerRunOptions opts =
  79                 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester");
  80         opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
  81                 .addDockerOpts("--memory=" + value)
  82                 .addJavaOpts("-cp", "/test-classes/")
  83                 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED")
  84                 .addClassOptions("memory", value);
  85         DockerTestUtils.dockerRunJava(opts).shouldHaveExitValue(0).shouldContain("TEST PASSED!!!");
  86     }
  87 
  88     private static void testMemoryFailCount(String value) throws Exception {
  89         Common.logNewTestCase("testMemoryFailCount" + value);
  90         DockerRunOptions opts =
  91                 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester");
  92         opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
  93                 .addDockerOpts("--memory=" + value)
  94                 .addJavaOpts("-Xmx" + value)


 130             System.out.println("Kernel memory accounting disabled, " +
 131                                        "skipping the test case");
 132             return;
 133         }
 134 
 135         oa.shouldHaveExitValue(0).shouldContain("TEST PASSED!!!");
 136     }
 137 
 138     private static void testOomKillFlag(String value, boolean oomKillFlag) throws Exception {
 139         Common.logNewTestCase("testOomKillFlag, oomKillFlag = " + oomKillFlag);
 140         DockerRunOptions opts =
 141                 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester");
 142         opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
 143                 .addDockerOpts("--memory=" + value);
 144         if (!oomKillFlag) {
 145             opts.addDockerOpts("--oom-kill-disable");
 146         }
 147         opts.addJavaOpts("-cp", "/test-classes/")
 148                 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED")
 149                 .addClassOptions("memory", value, oomKillFlag + "");
 150         DockerTestUtils.dockerRunJava(opts).shouldHaveExitValue(0).shouldContain("TEST PASSED!!!");

 151     }
 152 
 153     private static void testMemorySoftLimit(String mem, String softLimit) throws Exception {
 154         Common.logNewTestCase("testMemorySoftLimit, memory = " + mem + ", soft limit = " + softLimit);
 155         DockerRunOptions opts =
 156                 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester");
 157         opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
 158                 .addDockerOpts("--memory=" + mem)
 159                 .addDockerOpts("--memory-reservation=" + softLimit);
 160         opts.addJavaOpts("-cp", "/test-classes/")
 161                 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED")
 162                 .addClassOptions("softlimit", softLimit);
 163         DockerTestUtils.dockerRunJava(opts).shouldHaveExitValue(0).shouldContain("TEST PASSED!!!");
 164     }
 165 }


   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 import jdk.internal.platform.Metrics;
  25 import jdk.test.lib.Utils;
  26 import jdk.test.lib.containers.docker.Common;
  27 import jdk.test.lib.containers.docker.DockerRunOptions;
  28 import jdk.test.lib.containers.docker.DockerTestUtils;
  29 import jdk.test.lib.process.OutputAnalyzer;
  30 
  31 /*
  32  * @test
  33  * @summary Test JDK Metrics class when running inside docker container
  34  * @requires docker.support
  35  * @library /test/lib
  36  * @modules java.base/jdk.internal.platform
  37  * @build MetricsMemoryTester
  38  * @run main/timeout=360 TestDockerMemoryMetrics
  39  */
  40 
  41 public class TestDockerMemoryMetrics {
  42     private static final String imageName = Common.imageName("metrics-memory");
  43 
  44     public static void main(String[] args) throws Exception {
  45         if (!DockerTestUtils.canTestDocker()) {
  46             return;
  47         }
  48 
  49         // These tests create a docker image and run this image with
  50         // varying docker memory options.  The arguments passed to the docker
  51         // container include the Java test class to be run along with the
  52         // resource to be examined and expected result.
  53 
  54         DockerTestUtils.buildJdkDockerImage(imageName, "Dockerfile-BasicTest", "jdk-docker");
  55         try {
  56             testMemoryLimit("200m");
  57             testMemoryLimit("1g");
  58 
  59             testMemoryAndSwapLimit("200m", "1g");
  60             testMemoryAndSwapLimit("100m", "200m");
  61 
  62             Metrics m = Metrics.systemMetrics();
  63             // kernel memory, '--kernel-memory' switch, and OOM killer,
  64             // '--oom-kill-disable' switch, tests not supported by cgroupv2
  65             // runtimes
  66             if (m != null) {
  67                 if ("cgroupv1".equals(m.getProvider())) {
  68                     testKernelMemoryLimit("100m");
  69                     testKernelMemoryLimit("1g");
  70 
  71                     testOomKillFlag("100m", false);
  72                 } else {
  73                     System.out.println("kernel memory tests and OOM Kill flag tests not " +
  74                                        "possible with cgroupv2.");
  75                 }
  76             }
  77             testOomKillFlag("100m", true);
  78 
  79             testMemoryFailCount("64m");
  80 
  81             testMemorySoftLimit("500m","200m");
  82 
  83         } finally {
  84             if (!DockerTestUtils.RETAIN_IMAGE_AFTER_TEST) {
  85                 DockerTestUtils.removeDockerImage(imageName);
  86             }
  87         }
  88     }
  89 
  90     private static void testMemoryLimit(String value) throws Exception {
  91         Common.logNewTestCase("testMemoryLimit, value = " + value);
  92         DockerRunOptions opts =
  93                 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester");
  94         opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
  95                 .addDockerOpts("--memory=" + value)
  96                 .addJavaOpts("-cp", "/test-classes/")
  97                 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED")
  98                 .addClassOptions("memory", value);
  99         DockerTestUtils.dockerRunJava(opts).shouldHaveExitValue(0).shouldContain("TEST PASSED!!!");
 100     }
 101 
 102     private static void testMemoryFailCount(String value) throws Exception {
 103         Common.logNewTestCase("testMemoryFailCount" + value);
 104         DockerRunOptions opts =
 105                 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester");
 106         opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
 107                 .addDockerOpts("--memory=" + value)
 108                 .addJavaOpts("-Xmx" + value)


 144             System.out.println("Kernel memory accounting disabled, " +
 145                                        "skipping the test case");
 146             return;
 147         }
 148 
 149         oa.shouldHaveExitValue(0).shouldContain("TEST PASSED!!!");
 150     }
 151 
 152     private static void testOomKillFlag(String value, boolean oomKillFlag) throws Exception {
 153         Common.logNewTestCase("testOomKillFlag, oomKillFlag = " + oomKillFlag);
 154         DockerRunOptions opts =
 155                 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester");
 156         opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
 157                 .addDockerOpts("--memory=" + value);
 158         if (!oomKillFlag) {
 159             opts.addDockerOpts("--oom-kill-disable");
 160         }
 161         opts.addJavaOpts("-cp", "/test-classes/")
 162                 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED")
 163                 .addClassOptions("memory", value, oomKillFlag + "");
 164         OutputAnalyzer oa = DockerTestUtils.dockerRunJava(opts);
 165         oa.shouldHaveExitValue(0).shouldContain("TEST PASSED!!!");
 166     }
 167 
 168     private static void testMemorySoftLimit(String mem, String softLimit) throws Exception {
 169         Common.logNewTestCase("testMemorySoftLimit, memory = " + mem + ", soft limit = " + softLimit);
 170         DockerRunOptions opts =
 171                 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester");
 172         opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
 173                 .addDockerOpts("--memory=" + mem)
 174                 .addDockerOpts("--memory-reservation=" + softLimit);
 175         opts.addJavaOpts("-cp", "/test-classes/")
 176                 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED")
 177                 .addClassOptions("softlimit", softLimit);
 178         DockerTestUtils.dockerRunJava(opts).shouldHaveExitValue(0).shouldContain("TEST PASSED!!!");
 179     }
 180 }
< prev index next >