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
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 */
4040import jdk .test .lib .containers .docker .Common ;
4141import jdk .test .lib .containers .docker .DockerRunOptions ;
4242import jdk .test .lib .containers .docker .DockerTestUtils ;
43+ import sun .hotspot .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 )
@@ -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 );
0 commit comments