Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/hotspot/share/prims/whitebox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2232,6 +2232,13 @@ WB_ENTRY(jint, WB_ValidateCgroup(JNIEnv* env,
return ret;
WB_END

// Available cpus of the host machine, Linux only.
// Used in container testing.
WB_ENTRY(jint, WB_HostCPUs(JNIEnv* env, jobject o))
LINUX_ONLY(return os::Linux::active_processor_count();)
return -1; // Not used/implemented on other platforms
WB_END

WB_ENTRY(void, WB_PrintOsInfo(JNIEnv* env, jobject o))
os::print_os_info(tty);
WB_END
Expand Down Expand Up @@ -2610,6 +2617,7 @@ static JNINativeMethod methods[] = {
(void*)&WB_ValidateCgroup },
{CC"hostPhysicalMemory", CC"()J", (void*)&WB_HostPhysicalMemory },
{CC"hostPhysicalSwap", CC"()J", (void*)&WB_HostPhysicalSwap },
{CC"hostCPUs", CC"()I", (void*)&WB_HostCPUs },
{CC"printOsInfo", CC"()V", (void*)&WB_PrintOsInfo },
{CC"disableElfSectionCache", CC"()V", (void*)&WB_DisableElfSectionCache },
{CC"resolvedMethodItemsCount", CC"()J", (void*)&WB_ResolvedMethodItemsCount },
Expand Down
1 change: 1 addition & 0 deletions test/hotspot/jtreg/TEST.ROOT
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ requires.properties= \
vm.musl \
vm.flagless \
docker.support \
systemd.support \
jdk.containerized

# Minimum jtreg version
Expand Down
28 changes: 28 additions & 0 deletions test/hotspot/jtreg/containers/systemd/HelloSystemd.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2024, Red Hat, Inc.
* 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.
*/

public class HelloSystemd {
public static void main(String args[]) {
System.out.println("Hello Systemd");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2024, Red Hat, Inc.
* 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.
*/

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
* @bug 8322420 8217338
* @summary Memory/CPU awareness test for JDK-under-test inside a systemd slice.
* @requires systemd.support
* @library /test/lib
* @modules java.base/jdk.internal.platform
* @build HelloSystemd jdk.test.whitebox.WhiteBox
* @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
*/
public class SystemdMemoryAwarenessTest {

private static final int MB = 1024 * 1024;
private static final WhiteBox wb = WhiteBox.getWhiteBox();
private static final String TEST_SLICE_NAME = SystemdMemoryAwarenessTest.class.getSimpleName() + "HS";

public static void main(String[] args) throws Exception {
testHelloSystemd();
}

private static void testHelloSystemd() throws Exception {
SystemdRunOptions opts = SystemdTestUtils.newOpts("HelloSystemd");
// 1 GB memory, but the limit in the lower hierarchy is 512M
opts.memoryLimit("1024M");
int expectedMemLimit = 512;
// expected detected limit we test for, 512MB
opts.sliceDMemoryLimit(String.format("%dM", expectedMemLimit));
int physicalCpus = wb.hostCPUs();
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.");
}
// 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);

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;
}
}

}
1 change: 1 addition & 0 deletions test/jdk/TEST.ROOT
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ requires.properties= \
vm.jvmci \
vm.jvmci.enabled \
docker.support \
systemd.support \
release.implementor \
jdk.containerized

Expand Down
36 changes: 29 additions & 7 deletions test/jtreg-ext/requires/VMProps.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public Map<String, String> call() {
map.put("vm.compiler1.enabled", this::isCompiler1Enabled);
map.put("vm.compiler2.enabled", this::isCompiler2Enabled);
map.put("docker.support", this::dockerSupport);
map.put("systemd.support", this::systemdSupport);
map.put("vm.musl", this::isMusl);
map.put("release.implementor", this::implementor);
map.put("jdk.containerized", this::jdkContainerized);
Expand Down Expand Up @@ -484,7 +485,7 @@ protected String dockerSupport() {

if (isSupported) {
try {
isSupported = checkDockerSupport();
isSupported = checkProgramSupport("checkDockerSupport()", Container.ENGINE_COMMAND);
} catch (Exception e) {
isSupported = false;
}
Expand All @@ -494,6 +495,27 @@ protected String dockerSupport() {
return "" + isSupported;
}

/**
* A simple check for systemd support
*
* @return true if systemd is supported in a given environment
*/
protected String systemdSupport() {
log("Entering systemdSupport()");

boolean isSupported = Platform.isLinux();
if (isSupported) {
try {
isSupported = checkProgramSupport("checkSystemdSupport()", "systemd-run");
} catch (Exception e) {
isSupported = false;
}
}

log("systemdSupport(): returning isSupported = " + isSupported);
return "" + isSupported;
}

// Configures process builder to redirect process stdout and stderr to a file.
// Returns file names for stdout and stderr.
private Map<String, String> redirectOutputToLogFile(String msg, ProcessBuilder pb, String fileNameBase) {
Expand Down Expand Up @@ -528,17 +550,17 @@ private void printLogfileContent(Map<String, String> logFileNames) {
});
}

private boolean checkDockerSupport() throws IOException, InterruptedException {
log("checkDockerSupport(): entering");
ProcessBuilder pb = new ProcessBuilder("which", Container.ENGINE_COMMAND);
private boolean checkProgramSupport(String logString, String cmd) throws IOException, InterruptedException {
log(logString + ": entering");
ProcessBuilder pb = new ProcessBuilder("which", cmd);
Map<String, String> logFileNames =
redirectOutputToLogFile("checkDockerSupport(): which <container-engine>",
pb, "which-container");
redirectOutputToLogFile(logString + ": which " + cmd,
pb, "which-cmd");
Process p = pb.start();
p.waitFor(10, TimeUnit.SECONDS);
int exitValue = p.exitValue();

log(String.format("checkDockerSupport(): exitValue = %s, pid = %s", exitValue, p.pid()));
log(String.format("%s: exitValue = %s, pid = %s", logString, exitValue, p.pid()));
if (exitValue != 0) {
printLogfileContent(logFileNames);
}
Expand Down
152 changes: 152 additions & 0 deletions test/lib/jdk/test/lib/containers/systemd/SystemdRunOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* Copyright (c) 2024, Red Hat, Inc.
* 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.
*/

package jdk.test.lib.containers.systemd;

import static jdk.test.lib.Asserts.assertNotNull;

import java.util.ArrayList;
import java.util.Collections;


// This class represents options for running java inside systemd slices
// in test environment.
public class SystemdRunOptions {
public ArrayList<String> javaOpts = new ArrayList<>();
public String classToRun; // class or "-version"
public ArrayList<String> classParams = new ArrayList<>();
public String memoryLimit; // used in slice for MemoryLimit property
public String cpuLimit; // used in slice for CPUQuota property
public String sliceName; // name of the slice (nests CPU in memory)
public String sliceDMemoryLimit; // used in jdk_internal.slice.d
public String sliceDCpuLimit; // used in jdk_internal.slice.d

/**
* Convenience constructor for most common use cases in testing.
* @param classToRun a class to run, or "-version"
* @param javaOpts java options to use
*
* @return Default docker run options
*/
public SystemdRunOptions(String classToRun, String... javaOpts) {
this.classToRun = classToRun;
Collections.addAll(this.javaOpts, javaOpts);
this.sliceName = defaultSliceName();
}

private static String defaultSliceName() {
// Create a unique name for a systemd slice
// jtreg guarantees that test.name is unique among all concurrently executing
// tests. For example, if you have two test roots:
//
// $ find test -type f
// test/foo/TEST.ROOT
// test/foo/my/TestCase.java
// test/bar/TEST.ROOT
// test/bar/my/TestCase.java
// $ jtreg -concur:2 test/foo test/bar
//
// jtreg will first run all the tests under test/foo. When they are all finished, then
// jtreg will run all the tests under test/bar. So you will never have two concurrent
// test cases whose test.name is "my/TestCase.java"
String testname = System.getProperty("test.name");
assertNotNull(testname, "must be set by jtreg");
testname = testname.replace(".java", "");
testname = testname.replace("/", "_");
testname = testname.replace("\\", "_");
testname = testname.replace("-", "_");

// Example:
// Memory: "test_containers_systemd_TestMemoryAwareness"
// CPU: "test_containers_systemd_TestMemoryAwareness-cpu" => derived
return testname;
}

/**
* The memory limit set with a .slice file in the systemd
* config directory.
*
* @param memLimit The memory limit to set (e.g. 1000M).
* @return The run options.
*/
public SystemdRunOptions memoryLimit(String memLimit) {
this.memoryLimit = memLimit;
return this;
}

/**
* The memory limit to set in the top-level jdk_internal.slice.d
* systemd config directory.
*
* @param memoryLimit The memory limit to set.
* @return The run options.
*/
public SystemdRunOptions sliceDMemoryLimit(String memoryLimit) {
this.sliceDMemoryLimit = memoryLimit;
return this;
}

/**
* The CPU limit set with a .slice file in the systemd
* config directory.
*
* @param cpuLimit
* @return The run options.
*/
public SystemdRunOptions cpuLimit(String cpuLimit) {
this.cpuLimit = cpuLimit;
return this;
}

/**
* The Cpu limit set in the top-level jdk_internal.slice.d
* systemd config directory.
*
* @param cpuLimit The CPU limit to set to.
* @return The run options.
*/
public SystemdRunOptions sliceDCpuLimit(String cpuLimit) {
this.sliceDCpuLimit = cpuLimit;
return this;
}

public SystemdRunOptions sliceName(String name) {
this.sliceName = name;
return this;
}

public SystemdRunOptions addJavaOpts(String... opts) {
Collections.addAll(javaOpts, opts);
return this;
}

public SystemdRunOptions addClassOptions(String... opts) {
Collections.addAll(classParams,opts);
return this;
}

public boolean hasSliceDLimit() {
return this.sliceDMemoryLimit != null ||
this.sliceDCpuLimit != null;
}
}
Loading