2017-11-17 03:01:01 +00:00
|
|
|
/*
|
2022-07-08 15:55:14 +00:00
|
|
|
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
|
2017-11-17 03:01:01 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @test
|
2022-08-26 12:25:16 +00:00
|
|
|
* @bug 8146115 8292083
|
2020-11-12 13:23:57 +00:00
|
|
|
* @key cgroups
|
2017-11-17 03:01:01 +00:00
|
|
|
* @summary Test JVM's memory resource awareness when running inside docker container
|
|
|
|
* @requires docker.support
|
|
|
|
* @library /test/lib
|
|
|
|
* @modules java.base/jdk.internal.misc
|
2020-05-20 19:12:20 +00:00
|
|
|
* java.base/jdk.internal.platform
|
2017-11-17 03:01:01 +00:00
|
|
|
* java.management
|
|
|
|
* jdk.jartool/sun.tools.jar
|
2022-07-08 15:55:14 +00:00
|
|
|
* @build AttemptOOM jdk.test.whitebox.WhiteBox PrintContainerInfo CheckOperatingSystemMXBean
|
|
|
|
* @run driver jdk.test.lib.helpers.ClassFileInstaller -jar whitebox.jar jdk.test.whitebox.WhiteBox
|
2017-11-17 03:01:01 +00:00
|
|
|
* @run driver TestMemoryAwareness
|
|
|
|
*/
|
2018-06-12 22:51:45 +00:00
|
|
|
import jdk.test.lib.containers.docker.Common;
|
2017-11-17 03:01:01 +00:00
|
|
|
import jdk.test.lib.containers.docker.DockerRunOptions;
|
|
|
|
import jdk.test.lib.containers.docker.DockerTestUtils;
|
2019-09-03 15:52:36 +00:00
|
|
|
import jdk.test.lib.process.OutputAnalyzer;
|
2017-11-17 03:01:01 +00:00
|
|
|
|
2022-08-26 12:25:16 +00:00
|
|
|
import static jdk.test.lib.Asserts.assertNotNull;
|
|
|
|
|
2017-11-17 03:01:01 +00:00
|
|
|
public class TestMemoryAwareness {
|
|
|
|
private static final String imageName = Common.imageName("memory");
|
|
|
|
|
2022-08-26 16:22:14 +00:00
|
|
|
private static String getHostMaxMemory() throws Exception {
|
|
|
|
DockerRunOptions opts = Common.newOpts(imageName);
|
|
|
|
String goodMem = Common.run(opts).firstMatch("total physical memory: (\\d+)", 1);
|
|
|
|
assertNotNull(goodMem, "no match for 'total physical memory' in trace output");
|
|
|
|
return goodMem;
|
|
|
|
}
|
|
|
|
|
2017-11-17 03:01:01 +00:00
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
if (!DockerTestUtils.canTestDocker()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Common.prepareWhiteBox();
|
2021-08-17 23:22:44 +00:00
|
|
|
DockerTestUtils.buildJdkContainerImage(imageName);
|
2017-11-17 03:01:01 +00:00
|
|
|
|
|
|
|
try {
|
2022-09-15 08:47:05 +00:00
|
|
|
testMemoryLimit("100m", "104857600", false);
|
|
|
|
testMemoryLimit("500m", "524288000", false);
|
|
|
|
testMemoryLimit("1g", "1073741824", false);
|
|
|
|
testMemoryLimit("4g", "4294967296", false);
|
|
|
|
testMemoryLimit("100m", "104857600", true /* additional cgroup mount */);
|
2017-11-17 03:01:01 +00:00
|
|
|
|
|
|
|
testMemorySoftLimit("500m", "524288000");
|
|
|
|
testMemorySoftLimit("1g", "1073741824");
|
|
|
|
|
|
|
|
// Add extra 10 Mb to allocator limit, to be sure to cause OOM
|
|
|
|
testOOM("256m", 256 + 10);
|
|
|
|
|
2019-12-12 03:20:57 +00:00
|
|
|
testOperatingSystemMXBeanAwareness(
|
|
|
|
"100M", Integer.toString(((int) Math.pow(2, 20)) * 100),
|
|
|
|
"150M", Integer.toString(((int) Math.pow(2, 20)) * (150 - 100))
|
|
|
|
);
|
|
|
|
testOperatingSystemMXBeanAwareness(
|
|
|
|
"128M", Integer.toString(((int) Math.pow(2, 20)) * 128),
|
|
|
|
"256M", Integer.toString(((int) Math.pow(2, 20)) * (256 - 128))
|
|
|
|
);
|
|
|
|
testOperatingSystemMXBeanAwareness(
|
|
|
|
"1G", Integer.toString(((int) Math.pow(2, 20)) * 1024),
|
|
|
|
"1500M", Integer.toString(((int) Math.pow(2, 20)) * (1500 - 1024))
|
|
|
|
);
|
2022-09-30 08:44:10 +00:00
|
|
|
testOperatingSystemMXBeanAwareness(
|
|
|
|
"100M", Integer.toString(((int) Math.pow(2, 20)) * 100),
|
|
|
|
"200M", Integer.toString(((int) Math.pow(2, 20)) * (200 - 100)),
|
|
|
|
true /* additional cgroup fs mounts */
|
|
|
|
);
|
2022-08-26 16:22:14 +00:00
|
|
|
final String hostMaxMem = getHostMaxMemory();
|
|
|
|
testOperatingSystemMXBeanIgnoresMemLimitExceedingPhysicalMemory(hostMaxMem);
|
|
|
|
testMetricsIgnoresMemLimitExceedingPhysicalMemory(hostMaxMem);
|
|
|
|
testContainerMemExceedsPhysical(hostMaxMem);
|
2017-11-17 03:01:01 +00:00
|
|
|
} finally {
|
2019-12-12 03:20:57 +00:00
|
|
|
if (!DockerTestUtils.RETAIN_IMAGE_AFTER_TEST) {
|
|
|
|
DockerTestUtils.removeDockerImage(imageName);
|
|
|
|
}
|
2017-11-17 03:01:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-15 08:47:05 +00:00
|
|
|
private static void testMemoryLimit(String valueToSet, String expectedTraceValue, boolean addCgmounts)
|
2017-11-17 03:01:01 +00:00
|
|
|
throws Exception {
|
|
|
|
|
|
|
|
Common.logNewTestCase("memory limit: " + valueToSet);
|
|
|
|
|
|
|
|
DockerRunOptions opts = Common.newOpts(imageName)
|
|
|
|
.addDockerOpts("--memory", valueToSet);
|
|
|
|
|
2022-09-15 08:47:05 +00:00
|
|
|
if (addCgmounts) {
|
|
|
|
opts = opts.addDockerOpts("--volume", "/sys/fs/cgroup:/cgroups-in:ro");
|
|
|
|
}
|
|
|
|
|
2017-11-17 03:01:01 +00:00
|
|
|
Common.run(opts)
|
|
|
|
.shouldMatch("Memory Limit is:.*" + expectedTraceValue);
|
|
|
|
}
|
|
|
|
|
2022-08-26 12:25:16 +00:00
|
|
|
// JDK-8292083
|
|
|
|
// Ensure that Java ignores container memory limit values above the host's physical memory.
|
2022-08-26 16:22:14 +00:00
|
|
|
private static void testContainerMemExceedsPhysical(final String hostMaxMem)
|
2022-08-26 12:25:16 +00:00
|
|
|
throws Exception {
|
|
|
|
Common.logNewTestCase("container memory limit exceeds physical memory");
|
2022-08-26 16:22:14 +00:00
|
|
|
String badMem = hostMaxMem + "0";
|
|
|
|
// set a container memory limit to the bad value
|
|
|
|
DockerRunOptions opts = Common.newOpts(imageName)
|
2022-08-26 12:25:16 +00:00
|
|
|
.addDockerOpts("--memory", badMem);
|
2022-08-26 16:22:14 +00:00
|
|
|
|
2022-08-26 12:25:16 +00:00
|
|
|
Common.run(opts)
|
2022-08-26 16:22:14 +00:00
|
|
|
.shouldMatch("container memory limit (ignored: " + badMem + "|unlimited: -1), using host value " + hostMaxMem);
|
2022-08-26 12:25:16 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 03:01:01 +00:00
|
|
|
|
|
|
|
private static void testMemorySoftLimit(String valueToSet, String expectedTraceValue)
|
|
|
|
throws Exception {
|
|
|
|
Common.logNewTestCase("memory soft limit: " + valueToSet);
|
|
|
|
|
|
|
|
DockerRunOptions opts = Common.newOpts(imageName, "PrintContainerInfo");
|
|
|
|
Common.addWhiteBoxOpts(opts);
|
|
|
|
opts.addDockerOpts("--memory-reservation=" + valueToSet);
|
|
|
|
|
|
|
|
Common.run(opts)
|
|
|
|
.shouldMatch("Memory Soft Limit.*" + expectedTraceValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// provoke OOM inside the container, see how VM reacts
|
|
|
|
private static void testOOM(String dockerMemLimit, int sizeToAllocInMb) throws Exception {
|
|
|
|
Common.logNewTestCase("OOM");
|
|
|
|
|
|
|
|
DockerRunOptions opts = Common.newOpts(imageName, "AttemptOOM")
|
2022-07-06 08:24:47 +00:00
|
|
|
.addDockerOpts("--memory", dockerMemLimit, "--memory-swap", dockerMemLimit);
|
2017-11-17 03:01:01 +00:00
|
|
|
opts.classParams.add("" + sizeToAllocInMb);
|
|
|
|
|
2019-09-03 15:52:36 +00:00
|
|
|
// make sure we avoid inherited Xmx settings from the jtreg vmoptions
|
|
|
|
// set Xmx ourselves instead
|
|
|
|
System.out.println("sizeToAllocInMb is:" + sizeToAllocInMb + " sizeToAllocInMb/2 is:" + sizeToAllocInMb/2);
|
|
|
|
String javaHeapSize = sizeToAllocInMb/2 + "m";
|
|
|
|
opts.addJavaOptsAppended("-Xmx" + javaHeapSize);
|
|
|
|
|
|
|
|
OutputAnalyzer out = DockerTestUtils.dockerRunJava(opts);
|
|
|
|
|
|
|
|
if (out.getExitValue() == 0) {
|
|
|
|
throw new RuntimeException("We exited successfully, but we wanted to provoke an OOM inside the container");
|
|
|
|
}
|
|
|
|
|
|
|
|
out.shouldContain("Entering AttemptOOM main")
|
|
|
|
.shouldNotContain("AttemptOOM allocation successful")
|
|
|
|
.shouldContain("java.lang.OutOfMemoryError");
|
2017-11-17 03:01:01 +00:00
|
|
|
}
|
|
|
|
|
2019-12-12 03:20:57 +00:00
|
|
|
private static void testOperatingSystemMXBeanAwareness(String memoryAllocation, String expectedMemory,
|
|
|
|
String swapAllocation, String expectedSwap) throws Exception {
|
2022-09-30 08:44:10 +00:00
|
|
|
testOperatingSystemMXBeanAwareness(memoryAllocation, expectedMemory, swapAllocation, expectedSwap, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void testOperatingSystemMXBeanAwareness(String memoryAllocation, String expectedMemory,
|
|
|
|
String swapAllocation, String expectedSwap, boolean addCgroupMounts) throws Exception {
|
|
|
|
|
2019-12-12 03:20:57 +00:00
|
|
|
Common.logNewTestCase("Check OperatingSystemMXBean");
|
|
|
|
|
|
|
|
DockerRunOptions opts = Common.newOpts(imageName, "CheckOperatingSystemMXBean")
|
|
|
|
.addDockerOpts(
|
|
|
|
"--memory", memoryAllocation,
|
|
|
|
"--memory-swap", swapAllocation
|
2020-05-20 19:12:20 +00:00
|
|
|
)
|
|
|
|
// CheckOperatingSystemMXBean uses Metrics (jdk.internal.platform) for
|
|
|
|
// diagnostics
|
|
|
|
.addJavaOpts("--add-exports")
|
|
|
|
.addJavaOpts("java.base/jdk.internal.platform=ALL-UNNAMED");
|
2022-09-30 08:44:10 +00:00
|
|
|
if (addCgroupMounts) {
|
|
|
|
// Extra cgroup mount should be ignored by product code
|
|
|
|
opts.addDockerOpts("--volume", "/sys/fs/cgroup:/cgroup-in:ro");
|
|
|
|
}
|
2019-12-12 03:20:57 +00:00
|
|
|
|
2020-01-03 10:10:42 +00:00
|
|
|
OutputAnalyzer out = DockerTestUtils.dockerRunJava(opts);
|
|
|
|
out.shouldHaveExitValue(0)
|
|
|
|
.shouldContain("Checking OperatingSystemMXBean")
|
|
|
|
.shouldContain("OperatingSystemMXBean.getTotalPhysicalMemorySize: " + expectedMemory)
|
|
|
|
.shouldContain("OperatingSystemMXBean.getTotalMemorySize: " + expectedMemory)
|
|
|
|
.shouldMatch("OperatingSystemMXBean\\.getFreeMemorySize: [1-9][0-9]+")
|
2020-09-25 17:16:38 +00:00
|
|
|
.shouldMatch("OperatingSystemMXBean\\.getFreePhysicalMemorySize: [1-9][0-9]+");
|
|
|
|
|
|
|
|
// in case of warnings like : "Your kernel does not support swap limit capabilities
|
|
|
|
// or the cgroup is not mounted. Memory limited without swap."
|
|
|
|
// the getTotalSwapSpaceSize and getFreeSwapSpaceSize return the system
|
|
|
|
// values as the container setup isn't supported in that case.
|
2020-01-03 10:10:42 +00:00
|
|
|
try {
|
|
|
|
out.shouldContain("OperatingSystemMXBean.getTotalSwapSpaceSize: " + expectedSwap);
|
|
|
|
} catch(RuntimeException ex) {
|
2020-09-25 17:16:38 +00:00
|
|
|
out.shouldMatch("OperatingSystemMXBean.getTotalSwapSpaceSize: [0-9]+");
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
out.shouldMatch("OperatingSystemMXBean\\.getFreeSwapSpaceSize: [1-9][0-9]+");
|
|
|
|
} catch(RuntimeException ex) {
|
|
|
|
out.shouldMatch("OperatingSystemMXBean\\.getFreeSwapSpaceSize: 0");
|
2020-01-03 10:10:42 +00:00
|
|
|
}
|
2019-12-12 03:20:57 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 16:22:14 +00:00
|
|
|
|
|
|
|
// JDK-8292541: Ensure OperatingSystemMXBean ignores container memory limits above the host's physical memory.
|
|
|
|
private static void testOperatingSystemMXBeanIgnoresMemLimitExceedingPhysicalMemory(final String hostMaxMem)
|
|
|
|
throws Exception {
|
|
|
|
String badMem = hostMaxMem + "0";
|
|
|
|
testOperatingSystemMXBeanAwareness(badMem, hostMaxMem, badMem, hostMaxMem);
|
|
|
|
}
|
|
|
|
|
|
|
|
// JDK-8292541: Ensure Metrics ignores container memory limits above the host's physical memory.
|
|
|
|
private static void testMetricsIgnoresMemLimitExceedingPhysicalMemory(final String hostMaxMem)
|
|
|
|
throws Exception {
|
|
|
|
Common.logNewTestCase("Metrics ignore container memory limit exceeding physical memory");
|
|
|
|
String badMem = hostMaxMem + "0";
|
|
|
|
DockerRunOptions opts = Common.newOpts(imageName)
|
|
|
|
.addJavaOpts("-XshowSettings:system")
|
|
|
|
.addDockerOpts("--memory", badMem);
|
|
|
|
|
|
|
|
DockerTestUtils.dockerRunJava(opts).shouldMatch("Memory Limit: Unlimited");
|
|
|
|
}
|
2017-11-17 03:01:01 +00:00
|
|
|
}
|