Skip to content

Commit 3c61d5a

Browse files
committed
8300659: Refactor TestMemoryAwareness to use WhiteBox api for host values
Reviewed-by: mbaesken
1 parent b2071f7 commit 3c61d5a

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, 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
@@ -113,6 +113,7 @@
113113
#include "jvmci/jvmciRuntime.hpp"
114114
#endif
115115
#ifdef LINUX
116+
#include "os_linux.hpp"
116117
#include "osContainer_linux.hpp"
117118
#include "cgroupSubsystem_linux.hpp"
118119
#endif
@@ -2353,6 +2354,18 @@ WB_ENTRY(jboolean, WB_IsContainerized(JNIEnv* env, jobject o))
23532354
return false;
23542355
WB_END
23552356

2357+
// Physical memory of the host machine (including containers)
2358+
WB_ENTRY(jlong, WB_HostPhysicalMemory(JNIEnv* env, jobject o))
2359+
LINUX_ONLY(return os::Linux::physical_memory();)
2360+
return os::physical_memory();
2361+
WB_END
2362+
2363+
// Physical swap of the host machine (including containers), Linux only.
2364+
WB_ENTRY(jlong, WB_HostPhysicalSwap(JNIEnv* env, jobject o))
2365+
LINUX_ONLY(return (jlong)os::Linux::host_swap();)
2366+
return -1; // Not used/implemented on other platforms
2367+
WB_END
2368+
23562369
WB_ENTRY(jint, WB_ValidateCgroup(JNIEnv* env,
23572370
jobject o,
23582371
jstring proc_cgroups,
@@ -2752,6 +2765,8 @@ static JNINativeMethod methods[] = {
27522765
{CC"validateCgroup",
27532766
CC"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I",
27542767
(void*)&WB_ValidateCgroup },
2768+
{CC"hostPhysicalMemory", CC"()J", (void*)&WB_HostPhysicalMemory },
2769+
{CC"hostPhysicalSwap", CC"()J", (void*)&WB_HostPhysicalSwap },
27552770
{CC"printOsInfo", CC"()V", (void*)&WB_PrintOsInfo },
27562771
{CC"disableElfSectionCache", CC"()V", (void*)&WB_DisableElfSectionCache },
27572772
{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
@@ -708,6 +708,8 @@ public native int validateCgroup(String procCgroups,
708708
String procSelfCgroup,
709709
String procSelfMountinfo);
710710
public native void printOsInfo();
711+
public native long hostPhysicalMemory();
712+
public native long hostPhysicalSwap();
711713

712714
// Decoder
713715
public native void disableElfSectionCache();

0 commit comments

Comments
 (0)