8327946: containers/docker/TestJFREvents.java fails when host kernel config vm.swappiness=0 after JDK-8325139

Reviewed-by: sgehwolf
This commit is contained in:
SendaoYan 2024-04-17 08:39:12 +00:00 committed by Severin Gehwolf
parent 9d63fee49c
commit 7744b0046a

View File

@ -30,6 +30,7 @@
* Also make sure that PIDs are based on value provided by container, * Also make sure that PIDs are based on value provided by container,
* not by the host system. * not by the host system.
* @requires (docker.support & os.maxMemory >= 2g) * @requires (docker.support & os.maxMemory >= 2g)
* @modules java.base/jdk.internal.platform
* @library /test/lib * @library /test/lib
* @modules java.base/jdk.internal.misc * @modules java.base/jdk.internal.misc
* java.management * java.management
@ -44,6 +45,7 @@ import jdk.test.lib.containers.docker.DockerTestUtils;
import jdk.test.lib.Asserts; import jdk.test.lib.Asserts;
import jdk.test.lib.process.OutputAnalyzer; import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.Utils; import jdk.test.lib.Utils;
import jdk.internal.platform.Metrics;
public class TestJFREvents { public class TestJFREvents {
@ -52,6 +54,7 @@ public class TestJFREvents {
private static final String TEST_ENV_VALUE = "unique_value_abc592903xyz"; private static final String TEST_ENV_VALUE = "unique_value_abc592903xyz";
private static final int availableCPUs = Runtime.getRuntime().availableProcessors(); private static final int availableCPUs = Runtime.getRuntime().availableProcessors();
private static final int UNKNOWN = -100; private static final int UNKNOWN = -100;
private static boolean isCgroupV1 = false;
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
System.out.println("Test Environment: detected availableCPUs = " + availableCPUs); System.out.println("Test Environment: detected availableCPUs = " + availableCPUs);
@ -59,6 +62,14 @@ public class TestJFREvents {
return; return;
} }
// If cgroups is not configured, report success.
Metrics metrics = Metrics.systemMetrics();
if (metrics == null) {
System.out.println("TEST PASSED!!!");
return;
}
isCgroupV1 = "cgroupv1".equals(metrics.getProvider());
DockerTestUtils.buildJdkContainerImage(imageName); DockerTestUtils.buildJdkContainerImage(imageName);
try { try {
@ -217,32 +228,36 @@ public class TestJFREvents {
private static void testSwapMemory(String memValueToSet, String swapValueToSet, String expectedTotalValue, String expectedFreeValue) throws Exception { private static void testSwapMemory(String memValueToSet, String swapValueToSet, String expectedTotalValue, String expectedFreeValue) throws Exception {
Common.logNewTestCase("Memory: --memory = " + memValueToSet + " --memory-swap = " + swapValueToSet); Common.logNewTestCase("Memory: --memory = " + memValueToSet + " --memory-swap = " + swapValueToSet);
OutputAnalyzer out = DockerTestUtils.dockerRunJava( DockerRunOptions opts = commonDockerOpts();
commonDockerOpts() opts.addDockerOpts("--memory=" + memValueToSet)
.addDockerOpts("--memory=" + memValueToSet) .addDockerOpts("--memory-swap=" + swapValueToSet)
.addDockerOpts("--memory-swap=" + swapValueToSet) .addClassOptions("jdk.SwapSpace");
.addClassOptions("jdk.SwapSpace")); if (isCgroupV1) {
out.shouldHaveExitValue(0) // With Cgroupv1, The default memory-swappiness vaule is inherited from the host machine, which maybe 0
opts.addDockerOpts("--memory-swappiness=60");
}
OutputAnalyzer out = DockerTestUtils.dockerRunJava(opts);
out.shouldHaveExitValue(0)
.shouldContain("totalSize = " + expectedTotalValue) .shouldContain("totalSize = " + expectedTotalValue)
.shouldContain("freeSize = "); .shouldContain("freeSize = ");
List<String> ls = out.asLinesWithoutVMWarnings(); List<String> ls = out.asLinesWithoutVMWarnings();
for (String cur : ls) { for (String cur : ls) {
int idx = cur.indexOf("freeSize = "); int idx = cur.indexOf("freeSize = ");
if (idx != -1) { if (idx != -1) {
int startNbr = idx+11; int startNbr = idx+11;
int endNbr = cur.indexOf(' ', startNbr); int endNbr = cur.indexOf(' ', startNbr);
if (endNbr == -1) endNbr = cur.length(); if (endNbr == -1) endNbr = cur.length();
String freeSizeStr = cur.substring(startNbr, endNbr); String freeSizeStr = cur.substring(startNbr, endNbr);
long freeval = Long.parseLong(freeSizeStr); long freeval = Long.parseLong(freeSizeStr);
long totalval = Long.parseLong(expectedTotalValue); long totalval = Long.parseLong(expectedTotalValue);
if (0 <= freeval && freeval <= totalval) { if (0 <= freeval && freeval <= totalval) {
System.out.println("Found freeSize value " + freeval + " is fine"); System.out.println("Found freeSize value " + freeval + " is fine");
} else { } else {
System.out.println("Found freeSize value " + freeval + " is bad"); System.out.println("Found freeSize value " + freeval + " is bad");
throw new Exception("Found free size value is bad"); throw new Exception("Found free size value is bad");
} }
} }
} }
} }