Skip to content

Commit

Permalink
Add Whitebox check for host cpu
Browse files Browse the repository at this point in the history
  • Loading branch information
jerboaa committed Jul 11, 2024
1 parent e19217c commit 139a906
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
14 changes: 14 additions & 0 deletions src/hotspot/share/prims/whitebox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ class VM_WhiteBoxOperation : public VM_Operation {
bool allow_nested_vm_operations() const { return true; }
};

#ifdef LINUX
class Whitebox_Linux : public os::Linux {
public:
static int host_cpus() { return os::Linux::active_processor_count(); }
};
#endif


WB_ENTRY(jlong, WB_GetObjectAddress(JNIEnv* env, jobject o, jobject obj))
return (jlong)(void*)JNIHandles::resolve(obj);
Expand Down Expand Up @@ -2488,6 +2495,12 @@ WB_ENTRY(jint, WB_ValidateCgroup(JNIEnv* env,
return ret;
WB_END

// Physical cpus of the host machine (including containers), Linux only.
WB_ENTRY(jint, WB_HostCPUs(JNIEnv* env, jobject o))
LINUX_ONLY(return Whitebox_Linux::host_cpus();)
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 @@ -2929,6 +2942,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
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,38 @@
* @summary Memory/CPU awareness test for JDK-under-test inside a systemd slice.
* @requires systemd.support
* @library /test/lib
* @build HelloSystemd
* @run driver SystemdMemoryAwarenessTest
* @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
*/
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import jdk.test.whitebox.WhiteBox;
import jdk.test.lib.containers.systemd.SystemdRunOptions;
import jdk.test.lib.containers.systemd.SystemdTestUtils;
import jdk.test.lib.containers.systemd.SystemdTestUtils.ResultFiles;


public class SystemdMemoryAwarenessTest {

private static final WhiteBox wb = WhiteBox.getWhiteBox();

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

private static void testHelloSystemd() throws Exception {
SystemdRunOptions opts = SystemdTestUtils.newOpts("HelloSystemd");
// 1 GB memory and 2 cores CPU
opts.memoryLimit("1000M").cpuLimit("200%");
// 1 GB memory
opts.memoryLimit("1000M");
int physicalCpus = wb.hostCPUs();
if (physicalCpus < 3) {
System.err.println("WARNING: host system only has " + physicalCpus + " expected >= 3");
}
// 1 or 2 cores limit depending on physical CPUs
int coreLimit = Math.min(physicalCpus, 2);
System.out.println("DEBUG: Running test with a CPU limit of " + coreLimit);
opts.cpuLimit(String.format("%d%%", coreLimit * 100));
opts.sliceName(SystemdMemoryAwarenessTest.class.getSimpleName());

ResultFiles files = SystemdTestUtils.buildSystemdSlices(opts);
Expand All @@ -55,7 +67,7 @@ private static void testHelloSystemd() throws Exception {
SystemdTestUtils.systemdRunJava(opts)
.shouldHaveExitValue(0)
.shouldContain("Hello Systemd")
.shouldContain("OSContainer::active_processor_count: 2")
.shouldContain("OSContainer::active_processor_count: " + coreLimit)
.shouldContain("Memory Limit is: 1048576000"); // 1 GB
} finally {
try {
Expand Down
1 change: 1 addition & 0 deletions test/lib/jdk/test/whitebox/WhiteBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,7 @@ public native int validateCgroup(String procCgroups,
public native void printOsInfo();
public native long hostPhysicalMemory();
public native long hostPhysicalSwap();
public native int hostCPUs();

// Decoder
public native void disableElfSectionCache();
Expand Down

0 comments on commit 139a906

Please sign in to comment.