diff --git a/test/hotspot/jtreg/containers/systemd/SystemdMemoryAwarenessTest.java b/test/hotspot/jtreg/containers/systemd/SystemdMemoryAwarenessTest.java index 92cc6c7ff6d83..2c06d48bb1c0a 100644 --- a/test/hotspot/jtreg/containers/systemd/SystemdMemoryAwarenessTest.java +++ b/test/hotspot/jtreg/containers/systemd/SystemdMemoryAwarenessTest.java @@ -21,6 +21,11 @@ * questions. */ +import jdk.test.lib.containers.systemd.SystemdRunOptions; +import jdk.test.lib.containers.systemd.SystemdTestUtils; +import jdk.test.lib.process.OutputAnalyzer; +import jdk.test.whitebox.WhiteBox; +import jtreg.SkippedException; /* * @test @@ -33,11 +38,6 @@ * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar whitebox.jar jdk.test.whitebox.WhiteBox * @run main/othervm -Xbootclasspath/a:whitebox.jar -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI SystemdMemoryAwarenessTest */ -import jdk.test.lib.containers.systemd.SystemdRunOptions; -import jdk.test.lib.containers.systemd.SystemdTestUtils; -import jdk.test.whitebox.WhiteBox; - - public class SystemdMemoryAwarenessTest { private static final int MB = 1024 * 1024; @@ -56,20 +56,30 @@ private static void testHelloSystemd() throws Exception { // expected detected limit we test for, 512MB opts.sliceDMemoryLimit(String.format("%dM", expectedMemLimit)); int physicalCpus = wb.hostCPUs(); - if (physicalCpus < 3) { - System.err.println("WARNING: host system only has " + physicalCpus + " expected >= 3"); + if (physicalCpus < 2) { + System.err.println("WARNING: host system only has " + physicalCpus + " cpus. Expected >= 2"); + System.err.println("The active_processor_count assertion will trivially pass."); } - // 1 or 2 cores limit depending on physical CPUs - int coreLimit = Math.min(physicalCpus, 2); + // Use a CPU core limit of 1 for best coverage + int coreLimit = 1; System.out.println("DEBUG: Running test with a CPU limit of " + coreLimit); opts.cpuLimit(String.format("%d%%", coreLimit * 100)); opts.sliceName(TEST_SLICE_NAME); - SystemdTestUtils.buildAndRunSystemdJava(opts) - .shouldHaveExitValue(0) - .shouldContain("Hello Systemd") - .shouldContain("OSContainer::active_processor_count: " + coreLimit) - .shouldContain(String.format("Memory Limit is: %d", (expectedMemLimit * MB))); + OutputAnalyzer out = SystemdTestUtils.buildAndRunSystemdJava(opts); + out.shouldHaveExitValue(0) + .shouldContain("Hello Systemd") + .shouldContain(String.format("Memory Limit is: %d", (expectedMemLimit * MB))); + try { + out.shouldContain("OSContainer::active_processor_count: " + coreLimit); + } catch (RuntimeException e) { + // CPU delegation needs to be enabled when run as user on cg v2 + if (SystemdTestUtils.RUN_AS_USER) { + String hint = "When run as user on cg v2 cpu delegation needs to be configured!"; + throw new SkippedException(hint); + } + throw e; + } } } diff --git a/test/lib/jdk/test/lib/containers/systemd/SystemdTestUtils.java b/test/lib/jdk/test/lib/containers/systemd/SystemdTestUtils.java index 09f8dd79b73f7..341b24c3e051a 100644 --- a/test/lib/jdk/test/lib/containers/systemd/SystemdTestUtils.java +++ b/test/lib/jdk/test/lib/containers/systemd/SystemdTestUtils.java @@ -43,7 +43,7 @@ public class SystemdTestUtils { private static final String CGROUPS_PROVIDER = Metrics.systemMetrics().getProvider(); private static boolean CGROUPS_V2 = "cgroupv2".equals(CGROUPS_PROVIDER); - private static boolean RUN_AS_USER = !Platform.isRoot() && CGROUPS_V2; + public static boolean RUN_AS_USER = !Platform.isRoot() && CGROUPS_V2; private static final String SLICE_NAMESPACE_PREFIX = "jdk_internal"; private static final String SLICE_D_MEM_CONFIG_FILE = "memory-limit.conf"; private static final String SLICE_D_CPU_CONFIG_FILE = "cpu-limit.conf";