Skip to content

Commit 139a906

Browse files
committed
Add Whitebox check for host cpu
1 parent e19217c commit 139a906

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

src/hotspot/share/prims/whitebox.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ class VM_WhiteBoxOperation : public VM_Operation {
158158
bool allow_nested_vm_operations() const { return true; }
159159
};
160160

161+
#ifdef LINUX
162+
class Whitebox_Linux : public os::Linux {
163+
public:
164+
static int host_cpus() { return os::Linux::active_processor_count(); }
165+
};
166+
#endif
167+
161168

162169
WB_ENTRY(jlong, WB_GetObjectAddress(JNIEnv* env, jobject o, jobject obj))
163170
return (jlong)(void*)JNIHandles::resolve(obj);
@@ -2488,6 +2495,12 @@ WB_ENTRY(jint, WB_ValidateCgroup(JNIEnv* env,
24882495
return ret;
24892496
WB_END
24902497

2498+
// Physical cpus of the host machine (including containers), Linux only.
2499+
WB_ENTRY(jint, WB_HostCPUs(JNIEnv* env, jobject o))
2500+
LINUX_ONLY(return Whitebox_Linux::host_cpus();)
2501+
return -1; // Not used/implemented on other platforms
2502+
WB_END
2503+
24912504
WB_ENTRY(void, WB_PrintOsInfo(JNIEnv* env, jobject o))
24922505
os::print_os_info(tty);
24932506
WB_END
@@ -2929,6 +2942,7 @@ static JNINativeMethod methods[] = {
29292942
(void*)&WB_ValidateCgroup },
29302943
{CC"hostPhysicalMemory", CC"()J", (void*)&WB_HostPhysicalMemory },
29312944
{CC"hostPhysicalSwap", CC"()J", (void*)&WB_HostPhysicalSwap },
2945+
{CC"hostCPUs", CC"()I", (void*)&WB_HostCPUs },
29322946
{CC"printOsInfo", CC"()V", (void*)&WB_PrintOsInfo },
29332947
{CC"disableElfSectionCache", CC"()V", (void*)&WB_DisableElfSectionCache },
29342948
{CC"resolvedMethodItemsCount", CC"()J", (void*)&WB_ResolvedMethodItemsCount },

test/hotspot/jtreg/containers/systemd/SystemdMemoryAwarenessTest.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,38 @@
2727
* @summary Memory/CPU awareness test for JDK-under-test inside a systemd slice.
2828
* @requires systemd.support
2929
* @library /test/lib
30-
* @build HelloSystemd
31-
* @run driver SystemdMemoryAwarenessTest
30+
* @build HelloSystemd jdk.test.whitebox.WhiteBox
31+
* @run driver jdk.test.lib.helpers.ClassFileInstaller -jar whitebox.jar jdk.test.whitebox.WhiteBox
32+
* @run main/othervm -Xbootclasspath/a:whitebox.jar -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI SystemdMemoryAwarenessTest
3233
*/
3334
import java.nio.file.Files;
3435
import java.nio.file.NoSuchFileException;
36+
import jdk.test.whitebox.WhiteBox;
3537
import jdk.test.lib.containers.systemd.SystemdRunOptions;
3638
import jdk.test.lib.containers.systemd.SystemdTestUtils;
3739
import jdk.test.lib.containers.systemd.SystemdTestUtils.ResultFiles;
3840

3941

4042
public class SystemdMemoryAwarenessTest {
4143

44+
private static final WhiteBox wb = WhiteBox.getWhiteBox();
45+
4246
public static void main(String[] args) throws Exception {
4347
testHelloSystemd();
4448
}
4549

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

5264
ResultFiles files = SystemdTestUtils.buildSystemdSlices(opts);
@@ -55,7 +67,7 @@ private static void testHelloSystemd() throws Exception {
5567
SystemdTestUtils.systemdRunJava(opts)
5668
.shouldHaveExitValue(0)
5769
.shouldContain("Hello Systemd")
58-
.shouldContain("OSContainer::active_processor_count: 2")
70+
.shouldContain("OSContainer::active_processor_count: " + coreLimit)
5971
.shouldContain("Memory Limit is: 1048576000"); // 1 GB
6072
} finally {
6173
try {

test/lib/jdk/test/whitebox/WhiteBox.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,7 @@ public native int validateCgroup(String procCgroups,
764764
public native void printOsInfo();
765765
public native long hostPhysicalMemory();
766766
public native long hostPhysicalSwap();
767+
public native int hostCPUs();
767768

768769
// Decoder
769770
public native void disableElfSectionCache();

0 commit comments

Comments
 (0)