Skip to content

Commit 781df52

Browse files
committed
8300659: Refactor TestMemoryAwareness to use WhiteBox api for host values
Backport-of: 3c61d5aa48606dab2d2c639d5f0a56313476917d
1 parent ab27843 commit 781df52

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
@@ -115,6 +115,7 @@
115115
#endif
116116

117117
#ifdef LINUX
118+
#include "os_linux.hpp"
118119
#include "osContainer_linux.hpp"
119120
#include "cgroupSubsystem_linux.hpp"
120121
#endif
@@ -2200,6 +2201,18 @@ WB_ENTRY(jboolean, WB_IsContainerized(JNIEnv* env, jobject o))
22002201
return false;
22012202
WB_END
22022203

2204+
// Physical memory of the host machine (including containers)
2205+
WB_ENTRY(jlong, WB_HostPhysicalMemory(JNIEnv* env, jobject o))
2206+
LINUX_ONLY(return os::Linux::physical_memory();)
2207+
return os::physical_memory();
2208+
WB_END
2209+
2210+
// Physical swap of the host machine (including containers), Linux only.
2211+
WB_ENTRY(jlong, WB_HostPhysicalSwap(JNIEnv* env, jobject o))
2212+
LINUX_ONLY(return (jlong)os::Linux::host_swap();)
2213+
return -1; // Not used/implemented on other platforms
2214+
WB_END
2215+
22032216
WB_ENTRY(jint, WB_ValidateCgroup(JNIEnv* env,
22042217
jobject o,
22052218
jstring proc_cgroups,
@@ -2592,6 +2605,8 @@ static JNINativeMethod methods[] = {
25922605
{CC"validateCgroup",
25932606
CC"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I",
25942607
(void*)&WB_ValidateCgroup },
2608+
{CC"hostPhysicalMemory", CC"()J", (void*)&WB_HostPhysicalMemory },
2609+
{CC"hostPhysicalSwap", CC"()J", (void*)&WB_HostPhysicalSwap },
25952610
{CC"printOsInfo", CC"()V", (void*)&WB_PrintOsInfo },
25962611
{CC"disableElfSectionCache", CC"()V", (void*)&WB_DisableElfSectionCache },
25972612
{CC"resolvedMethodItemsCount", CC"()J", (void*)&WB_ResolvedMethodItemsCount },

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, 2022, 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 jdk.test.whitebox.WhiteBox PrintContainerInfo CheckOperatingSystemMXBean
3737
* @run driver jdk.test.lib.helpers.ClassFileInstaller -jar whitebox.jar jdk.test.whitebox.WhiteBox
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 jdk.test.whitebox.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)
@@ -207,12 +210,17 @@ private static void testOperatingSystemMXBeanAwareness(String memoryAllocation,
207210

208211
// in case of warnings like : "Your kernel does not support swap limit capabilities
209212
// or the cgroup is not mounted. Memory limited without swap."
210-
// the getTotalSwapSpaceSize and getFreeSwapSpaceSize return the system
211-
// values as the container setup isn't supported in that case.
213+
// the getTotalSwapSpaceSize either returns the system (or host) values, or 0
214+
// if a container memory limit is in place and gets detected. A value of 0 is because,
215+
// Metrics.getMemoryLimit() returns the same value as Metrics.getMemoryAndSwapLimit().
216+
//
217+
// getFreeSwapSpaceSize() are a function of what getTotalSwapSpaceSize() returns. Either
218+
// a number > 0, or 0 if getTotalSwapSpaceSize() == 0.
212219
try {
213220
out.shouldContain("OperatingSystemMXBean.getTotalSwapSpaceSize: " + expectedSwap);
214221
} catch(RuntimeException ex) {
215-
out.shouldMatch("OperatingSystemMXBean.getTotalSwapSpaceSize: [0-9]+");
222+
String hostSwap = getHostSwap();
223+
out.shouldMatch("OperatingSystemMXBean.getTotalSwapSpaceSize: (0|" + hostSwap + ")");
216224
}
217225

218226
try {
@@ -224,17 +232,18 @@ private static void testOperatingSystemMXBeanAwareness(String memoryAllocation,
224232

225233

226234
// JDK-8292541: Ensure OperatingSystemMXBean ignores container memory limits above the host's physical memory.
227-
private static void testOperatingSystemMXBeanIgnoresMemLimitExceedingPhysicalMemory(final String hostMaxMem)
235+
private static void testOSMXBeanIgnoresMemLimitExceedingPhysicalMemory()
228236
throws Exception {
237+
String hostMaxMem = getHostMaxMemory();
229238
String badMem = hostMaxMem + "0";
230239
testOperatingSystemMXBeanAwareness(badMem, hostMaxMem, badMem, hostMaxMem);
231240
}
232241

233242
// JDK-8292541: Ensure Metrics ignores container memory limits above the host's physical memory.
234-
private static void testMetricsIgnoresMemLimitExceedingPhysicalMemory(final String hostMaxMem)
243+
private static void testMetricsExceedingPhysicalMemory()
235244
throws Exception {
236245
Common.logNewTestCase("Metrics ignore container memory limit exceeding physical memory");
237-
String badMem = hostMaxMem + "0";
246+
String badMem = getHostMaxMemory() + "0";
238247
DockerRunOptions opts = Common.newOpts(imageName)
239248
.addJavaOpts("-XshowSettings:system")
240249
.addDockerOpts("--memory", badMem);

test/lib/jdk/test/whitebox/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, 2022, 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
@@ -624,6 +624,8 @@ public native int validateCgroup(String procCgroups,
624624
String procSelfCgroup,
625625
String procSelfMountinfo);
626626
public native void printOsInfo();
627+
public native long hostPhysicalMemory();
628+
public native long hostPhysicalSwap();
627629

628630
// Decoder
629631
public native void disableElfSectionCache();

0 commit comments

Comments
 (0)