1 /* 2 * Copyright (c) 2018, 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 25 /* 26 * @test 27 * @summary Test JVM's memory resource awareness when running inside docker container 28 * @library /testlibrary /testlibrary/whitebox 29 * @build AttemptOOM sun.hotspot.WhiteBox PrintContainerInfo 30 * @run driver ClassFileInstaller -jar whitebox.jar sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission 31 * @run driver TestMemoryAwareness 32 */ 33 34 import com.oracle.java.testlibrary.Common; 35 import com.oracle.java.testlibrary.DockerRunOptions; 36 import com.oracle.java.testlibrary.DockerTestUtils; 37 38 39 public class TestMemoryAwareness { 40 private static final String imageName = Common.imageName("memory"); 41 42 public static void main(String[] args) throws Exception { 43 if (!DockerTestUtils.canTestDocker()) { 44 return; 45 } 46 47 Common.prepareWhiteBox(); 48 DockerTestUtils.buildJdkDockerImage(imageName, "Dockerfile-BasicTest", "jdk-docker"); 49 50 try { 51 testMemoryLimit("100m", "104857600"); 52 testMemoryLimit("500m", "524288000"); 53 testMemoryLimit("1g", "1073741824"); 54 testMemoryLimit("4g", "4294967296"); 55 56 testMemorySoftLimit("500m", "524288000"); 57 testMemorySoftLimit("1g", "1073741824"); 58 59 // Add extra 10 Mb to allocator limit, to be sure to cause OOM 60 testOOM("256m", 256 + 10); 61 62 } finally { 63 DockerTestUtils.removeDockerImage(imageName); 64 } 65 } 66 67 68 private static void testMemoryLimit(String valueToSet, String expectedTraceValue) 69 throws Exception { 70 71 Common.logNewTestCase("memory limit: " + valueToSet); 72 73 DockerRunOptions opts = Common.newOpts(imageName) 74 .addDockerOpts("--memory", valueToSet); 75 76 Common.run(opts) 77 .shouldMatch("Memory Limit is:.*" + expectedTraceValue); 78 } 79 80 81 private static void testMemorySoftLimit(String valueToSet, String expectedTraceValue) 82 throws Exception { 83 Common.logNewTestCase("memory soft limit: " + valueToSet); 84 85 DockerRunOptions opts = Common.newOpts(imageName, "PrintContainerInfo"); 86 Common.addWhiteBoxOpts(opts); 87 opts.addDockerOpts("--memory-reservation=" + valueToSet); 88 89 Common.run(opts) 90 .shouldMatch("Memory Soft Limit.*" + expectedTraceValue); 91 } 92 93 94 // provoke OOM inside the container, see how VM reacts 95 private static void testOOM(String dockerMemLimit, int sizeToAllocInMb) throws Exception { 96 Common.logNewTestCase("OOM"); 97 98 DockerRunOptions opts = Common.newOpts(imageName, "AttemptOOM") 99 .addDockerOpts("--memory", dockerMemLimit, "--memory-swap", dockerMemLimit); 100 opts.classParams.add("" + sizeToAllocInMb); 101 102 DockerTestUtils.dockerRunJava(opts) 103 .shouldHaveExitValue(1) 104 .shouldContain("Entering AttemptOOM main") 105 .shouldNotContain("AttemptOOM allocation successful") 106 .shouldContain("java.lang.OutOfMemoryError"); 107 } 108 109 }