1 /* 2 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 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) 109 .addJavaOpts("-cp", "/test-classes/") 110 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED") 111 .addClassOptions("failcount"); 112 DockerTestUtils.dockerRunJava(opts).shouldHaveExitValue(0).shouldContain("TEST PASSED!!!"); 113 } 114 115 private static void testMemoryAndSwapLimit(String memory, String memandswap) throws Exception { 116 Common.logNewTestCase("testMemoryAndSwapLimit, memory = " + memory + ", memory and swap = " + memandswap); 117 DockerRunOptions opts = 118 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester"); 119 opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/") 120 .addDockerOpts("--memory=" + memory) 121 .addDockerOpts("--memory-swap=" + memandswap) 122 .addJavaOpts("-cp", "/test-classes/") 123 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED") 124 .addClassOptions("memoryswap", memory, memandswap); 125 DockerTestUtils.dockerRunJava(opts).shouldHaveExitValue(0).shouldContain("TEST PASSED!!!"); 126 } 127 128 private static void testKernelMemoryLimit(String value) throws Exception { 129 Common.logNewTestCase("testKernelMemoryLimit, value = " + value); 130 DockerRunOptions opts = 131 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester"); 132 opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/") 133 .addDockerOpts("--kernel-memory=" + value) 134 .addJavaOpts("-cp", "/test-classes/") 135 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED") 136 .addClassOptions("kernelmem", value); 137 OutputAnalyzer oa = DockerTestUtils.dockerRunJava(opts); 138 139 // Some container runtimes (e.g. runc, docker 18.09) 140 // have been built without kernel memory accounting. In 141 // that case, the runtime issues a message on stderr saying 142 // so. Skip the test in that case. 143 if (oa.getStderr().contains("kernel memory accounting disabled")) { 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 }