Skip to content

Commit

Permalink
Improve reliability of cpu quota test
Browse files Browse the repository at this point in the history
  • Loading branch information
jerboaa committed Sep 10, 2024
1 parent 30f32d2 commit 0e52e00
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 0e52e00

Please sign in to comment.