Skip to content

Commit 1a99b6e

Browse files
committed
8300659: Refactor TestMemoryAwareness to use WhiteBox api for host values
Reviewed-by: sgehwolf Backport-of: 3c61d5aa48606dab2d2c639d5f0a56313476917d
1 parent b9a29eb commit 1a99b6e

File tree

3 files changed

+46
-20
lines changed

3 files changed

+46
-20
lines changed

src/hotspot/share/prims/whitebox.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -94,6 +94,7 @@
9494
#endif // INCLUDE_AOT
9595

9696
#ifdef LINUX
97+
#include "os_linux.hpp"
9798
#include "osContainer_linux.hpp"
9899
#include "cgroupSubsystem_linux.hpp"
99100
#endif
@@ -2026,6 +2027,18 @@ WB_ENTRY(jboolean, WB_IsContainerized(JNIEnv* env, jobject o))
20262027
return false;
20272028
WB_END
20282029

2030+
// Physical memory of the host machine (including containers)
2031+
WB_ENTRY(jlong, WB_HostPhysicalMemory(JNIEnv* env, jobject o))
2032+
LINUX_ONLY(return os::Linux::physical_memory();)
2033+
return os::physical_memory();
2034+
WB_END
2035+
2036+
// Physical swap of the host machine (including containers), Linux only.
2037+
WB_ENTRY(jlong, WB_HostPhysicalSwap(JNIEnv* env, jobject o))
2038+
LINUX_ONLY(return (jlong)os::Linux::host_swap();)
2039+
return -1; // Not used/implemented on other platforms
2040+
WB_END
2041+
20292042
WB_ENTRY(jint, WB_ValidateCgroup(JNIEnv* env,
20302043
jobject o,
20312044
jstring proc_cgroups,
@@ -2305,6 +2318,8 @@ static JNINativeMethod methods[] = {
23052318
{CC"validateCgroup",
23062319
CC"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I",
23072320
(void*)&WB_ValidateCgroup },
2321+
{CC"hostPhysicalMemory", CC"()J", (void*)&WB_HostPhysicalMemory },
2322+
{CC"hostPhysicalSwap", CC"()J", (void*)&WB_HostPhysicalSwap },
23082323
{CC"printOsInfo", CC"()V", (void*)&WB_PrintOsInfo },
23092324
{CC"disableElfSectionCache", CC"()V", (void*)&WB_DisableElfSectionCache },
23102325
{CC"aotLibrariesCount", CC"()I", (void*)&WB_AotLibrariesCount },

test/hotspot/jtreg/containers/docker/TestMemoryAwareness.java

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -35,23 +35,26 @@
3535
* jdk.jartool/sun.tools.jar
3636
* @build AttemptOOM sun.hotspot.WhiteBox PrintContainerInfo CheckOperatingSystemMXBean
3737
* @run driver ClassFileInstaller -jar whitebox.jar sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
38-
* @run driver TestMemoryAwareness
38+
* @run main/othervm -Xbootclasspath/a:whitebox.jar -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI TestMemoryAwareness
3939
*/
4040
import jdk.test.lib.containers.docker.Common;
4141
import jdk.test.lib.containers.docker.DockerRunOptions;
4242
import jdk.test.lib.containers.docker.DockerTestUtils;
43+
import sun.hotspot.WhiteBox;
4344
import jdk.test.lib.process.OutputAnalyzer;
4445

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

4748
public class TestMemoryAwareness {
4849
private static final String imageName = Common.imageName("memory");
50+
private static final WhiteBox wb = WhiteBox.getWhiteBox();
4951

50-
private static String getHostMaxMemory() throws Exception {
51-
DockerRunOptions opts = Common.newOpts(imageName);
52-
String goodMem = Common.run(opts).firstMatch("total physical memory: (\\d+)", 1);
53-
assertNotNull(goodMem, "no match for 'total physical memory' in trace output");
54-
return goodMem;
52+
private static String getHostMaxMemory() {
53+
return Long.valueOf(wb.hostPhysicalMemory()).toString();
54+
}
55+
56+
private static String getHostSwap() {
57+
return Long.valueOf(wb.hostPhysicalSwap()).toString();
5558
}
5659

5760
public static void main(String[] args) throws Exception {
@@ -92,10 +95,9 @@ public static void main(String[] args) throws Exception {
9295
"200M", Integer.toString(((int) Math.pow(2, 20)) * (200 - 100)),
9396
true /* additional cgroup fs mounts */
9497
);
95-
final String hostMaxMem = getHostMaxMemory();
96-
testOperatingSystemMXBeanIgnoresMemLimitExceedingPhysicalMemory(hostMaxMem);
97-
testMetricsIgnoresMemLimitExceedingPhysicalMemory(hostMaxMem);
98-
testContainerMemExceedsPhysical(hostMaxMem);
98+
testOSMXBeanIgnoresMemLimitExceedingPhysicalMemory();
99+
testMetricsExceedingPhysicalMemory();
100+
testContainerMemExceedsPhysical();
99101
} finally {
100102
if (!DockerTestUtils.RETAIN_IMAGE_AFTER_TEST) {
101103
DockerTestUtils.removeDockerImage(imageName);
@@ -122,9 +124,10 @@ private static void testMemoryLimit(String valueToSet, String expectedTraceValue
122124

123125
// JDK-8292083
124126
// Ensure that Java ignores container memory limit values above the host's physical memory.
125-
private static void testContainerMemExceedsPhysical(final String hostMaxMem)
127+
private static void testContainerMemExceedsPhysical()
126128
throws Exception {
127129
Common.logNewTestCase("container memory limit exceeds physical memory");
130+
String hostMaxMem = getHostMaxMemory();
128131
String badMem = hostMaxMem + "0";
129132
// set a container memory limit to the bad value
130133
DockerRunOptions opts = Common.newOpts(imageName)
@@ -204,12 +207,17 @@ private static void testOperatingSystemMXBeanAwareness(String memoryAllocation,
204207
.shouldMatch("OperatingSystemMXBean\\.getFreePhysicalMemorySize: [1-9][0-9]+");
205208
// in case of warnings like : "Your kernel does not support swap limit capabilities
206209
// or the cgroup is not mounted. Memory limited without swap."
207-
// the getTotalSwapSpaceSize and getFreeSwapSpaceSize return the system
208-
// values as the container setup isn't supported in that case.
210+
// the getTotalSwapSpaceSize either returns the system (or host) values, or 0
211+
// if a container memory limit is in place and gets detected. A value of 0 is because,
212+
// Metrics.getMemoryLimit() returns the same value as Metrics.getMemoryAndSwapLimit().
213+
//
214+
// getFreeSwapSpaceSize() are a function of what getTotalSwapSpaceSize() returns. Either
215+
// a number > 0, or 0 if getTotalSwapSpaceSize() == 0.
209216
try {
210217
out.shouldContain("OperatingSystemMXBean.getTotalSwapSpaceSize: " + expectedSwap);
211218
} catch(RuntimeException ex) {
212-
out.shouldMatch("OperatingSystemMXBean.getTotalSwapSpaceSize: [0-9]+");
219+
String hostSwap = getHostSwap();
220+
out.shouldMatch("OperatingSystemMXBean.getTotalSwapSpaceSize: (0|" + hostSwap + ")");
213221
}
214222
try {
215223
out.shouldMatch("OperatingSystemMXBean\\.getFreeSwapSpaceSize: [1-9][0-9]+");
@@ -220,17 +228,18 @@ private static void testOperatingSystemMXBeanAwareness(String memoryAllocation,
220228

221229

222230
// JDK-8292541: Ensure OperatingSystemMXBean ignores container memory limits above the host's physical memory.
223-
private static void testOperatingSystemMXBeanIgnoresMemLimitExceedingPhysicalMemory(final String hostMaxMem)
231+
private static void testOSMXBeanIgnoresMemLimitExceedingPhysicalMemory()
224232
throws Exception {
233+
String hostMaxMem = getHostMaxMemory();
225234
String badMem = hostMaxMem + "0";
226235
testOperatingSystemMXBeanAwareness(badMem, hostMaxMem, badMem, hostMaxMem);
227236
}
228237

229238
// JDK-8292541: Ensure Metrics ignores container memory limits above the host's physical memory.
230-
private static void testMetricsIgnoresMemLimitExceedingPhysicalMemory(final String hostMaxMem)
239+
private static void testMetricsExceedingPhysicalMemory()
231240
throws Exception {
232241
Common.logNewTestCase("Metrics ignore container memory limit exceeding physical memory");
233-
String badMem = hostMaxMem + "0";
242+
String badMem = getHostMaxMemory() + "0";
234243
DockerRunOptions opts = Common.newOpts(imageName)
235244
.addJavaOpts("-XshowSettings:system")
236245
.addDockerOpts("--memory", badMem);

test/lib/sun/hotspot/WhiteBox.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -542,6 +542,8 @@ public native int validateCgroup(String procCgroups,
542542
String procSelfCgroup,
543543
String procSelfMountinfo);
544544
public native void printOsInfo();
545+
public native long hostPhysicalMemory();
546+
public native long hostPhysicalSwap();
545547

546548
// Decoder
547549
public native void disableElfSectionCache();

0 commit comments

Comments
 (0)