Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/runtime/containers/docker/TestMemoryAwareness.java
32285 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/222324/*25* @test26* @summary Test JVM's memory resource awareness when running inside docker container27* @library /testlibrary /testlibrary/whitebox28* @build AttemptOOM sun.hotspot.WhiteBox PrintContainerInfo CheckOperatingSystemMXBean29* @run driver ClassFileInstaller -jar whitebox.jar sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission30* @run driver TestMemoryAwareness31*/3233import com.oracle.java.testlibrary.Common;34import com.oracle.java.testlibrary.DockerRunOptions;35import com.oracle.java.testlibrary.DockerTestUtils;36import com.oracle.java.testlibrary.OutputAnalyzer;373839public class TestMemoryAwareness {40private static final String imageName = Common.imageName("memory");4142public static void main(String[] args) throws Exception {43if (!DockerTestUtils.canTestDocker()) {44return;45}4647Common.prepareWhiteBox();48DockerTestUtils.buildJdkDockerImage(imageName, "Dockerfile-BasicTest", "jdk-docker");4950try {51testMemoryLimit("100m", "104857600");52testMemoryLimit("500m", "524288000");53testMemoryLimit("1g", "1073741824");54testMemoryLimit("4g", "4294967296");5556testMemorySoftLimit("500m", "524288000");57testMemorySoftLimit("1g", "1073741824");5859// Add extra 10 Mb to allocator limit, to be sure to cause OOM60testOOM("256m", 256 + 10);6162testOperatingSystemMXBeanAwareness(63"100M", Integer.toString(((int) Math.pow(2, 20)) * 100),64"150M", Integer.toString(((int) Math.pow(2, 20)) * (150 - 100))65);66testOperatingSystemMXBeanAwareness(67"128M", Integer.toString(((int) Math.pow(2, 20)) * 128),68"256M", Integer.toString(((int) Math.pow(2, 20)) * (256 - 128))69);70testOperatingSystemMXBeanAwareness(71"1G", Integer.toString(((int) Math.pow(2, 20)) * 1024),72"1500M", Integer.toString(((int) Math.pow(2, 20)) * (1500 - 1024))73);74} finally {75DockerTestUtils.removeDockerImage(imageName);76}77}787980private static void testMemoryLimit(String valueToSet, String expectedTraceValue)81throws Exception {8283Common.logNewTestCase("memory limit: " + valueToSet);8485DockerRunOptions opts = Common.newOpts(imageName)86.addDockerOpts("--memory", valueToSet);8788Common.run(opts)89.shouldMatch("Memory Limit is:.*" + expectedTraceValue);90}919293private static void testMemorySoftLimit(String valueToSet, String expectedTraceValue)94throws Exception {95Common.logNewTestCase("memory soft limit: " + valueToSet);9697DockerRunOptions opts = Common.newOpts(imageName, "PrintContainerInfo");98Common.addWhiteBoxOpts(opts);99opts.addDockerOpts("--memory-reservation=" + valueToSet);100101Common.run(opts)102.shouldMatch("Memory Soft Limit.*" + expectedTraceValue);103}104105106// provoke OOM inside the container, see how VM reacts107private static void testOOM(String dockerMemLimit, int sizeToAllocInMb) throws Exception {108Common.logNewTestCase("OOM");109110DockerRunOptions opts = Common.newOpts(imageName, "AttemptOOM")111.addDockerOpts("--memory", dockerMemLimit, "--memory-swap", dockerMemLimit);112opts.classParams.add("" + sizeToAllocInMb);113114DockerTestUtils.dockerRunJava(opts)115.shouldHaveExitValue(1)116.shouldContain("Entering AttemptOOM main")117.shouldNotContain("AttemptOOM allocation successful")118.shouldContain("java.lang.OutOfMemoryError");119}120121private static void testOperatingSystemMXBeanAwareness(String memoryAllocation, String expectedMemory,122String swapAllocation, String expectedSwap) throws Exception {123Common.logNewTestCase("Check OperatingSystemMXBean");124125DockerRunOptions opts = Common.newOpts(imageName, "CheckOperatingSystemMXBean")126.addDockerOpts(127"--memory", memoryAllocation,128"--memory-swap", swapAllocation129);130131OutputAnalyzer out = DockerTestUtils.dockerRunJava(opts);132out.shouldHaveExitValue(0)133.shouldContain("Checking OperatingSystemMXBean")134.shouldContain("OperatingSystemMXBean.getTotalPhysicalMemorySize: " + expectedMemory)135.shouldMatch("OperatingSystemMXBean\\.getFreePhysicalMemorySize: [1-9][0-9]+");136// in case of warnings like : "Your kernel does not support swap limit capabilities137// or the cgroup is not mounted. Memory limited without swap."138// the getTotalSwapSpaceSize and getFreeSwapSpaceSize return the system139// values as the container setup isn't supported in that case.140try {141out.shouldContain("OperatingSystemMXBean.getTotalSwapSpaceSize: " + expectedSwap);142} catch(RuntimeException ex) {143out.shouldMatch("OperatingSystemMXBean.getTotalSwapSpaceSize: [0-9]+");144}145146try {147out.shouldMatch("OperatingSystemMXBean\\.getFreeSwapSpaceSize: [1-9][0-9]+");148} catch(RuntimeException ex) {149out.shouldMatch("OperatingSystemMXBean\\.getFreeSwapSpaceSize: 0");150}151}152153}154155156