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
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 */
4040import jdk .test .lib .containers .docker .Common ;
4141import jdk .test .lib .containers .docker .DockerRunOptions ;
4242import jdk .test .lib .containers .docker .DockerTestUtils ;
43+ import jdk .test .whitebox .WhiteBox ;
4344import jdk .test .lib .process .OutputAnalyzer ;
4445
4546import static jdk .test .lib .Asserts .assertNotNull ;
4647
4748public 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 );
0 commit comments