Fri, 03 Jul 2020 15:09:27 +0200
8226575: OperatingSystemMXBean should be made container aware
Reviewed-by: andrew
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/runtime/containers/docker/CheckOperatingSystemMXBean.java Fri Jul 03 15:09:27 2020 +0200 1.3 @@ -0,0 +1,43 @@ 1.4 +/* 1.5 + * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 1.6 + * Copyright (c) 2019, Red Hat Inc. All rights reserved. 1.7 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.8 + * 1.9 + * This code is free software; you can redistribute it and/or modify it 1.10 + * under the terms of the GNU General Public License version 2 only, as 1.11 + * published by the Free Software Foundation. 1.12 + * 1.13 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.14 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.15 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.16 + * version 2 for more details (a copy is included in the LICENSE file that 1.17 + * accompanied this code). 1.18 + * 1.19 + * You should have received a copy of the GNU General Public License version 1.20 + * 2 along with this work; if not, write to the Free Software Foundation, 1.21 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.22 + * 1.23 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.24 + * or visit www.oracle.com if you need additional information or have any 1.25 + * questions. 1.26 + */ 1.27 + 1.28 +import com.sun.management.OperatingSystemMXBean; 1.29 +import java.lang.management.ManagementFactory; 1.30 + 1.31 +public class CheckOperatingSystemMXBean { 1.32 + 1.33 + public static void main(String[] args) { 1.34 + System.out.println("Checking OperatingSystemMXBean"); 1.35 + 1.36 + OperatingSystemMXBean osBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean(); 1.37 + System.out.println(String.format("Runtime.availableProcessors: %d", Runtime.getRuntime().availableProcessors())); 1.38 + System.out.println(String.format("OperatingSystemMXBean.getAvailableProcessors: %d", osBean.getAvailableProcessors())); 1.39 + System.out.println(String.format("OperatingSystemMXBean.getTotalPhysicalMemorySize: %d", osBean.getTotalPhysicalMemorySize())); 1.40 + System.out.println(String.format("OperatingSystemMXBean.getFreePhysicalMemorySize: %d", osBean.getFreePhysicalMemorySize())); 1.41 + System.out.println(String.format("OperatingSystemMXBean.getTotalSwapSpaceSize: %d", osBean.getTotalSwapSpaceSize())); 1.42 + System.out.println(String.format("OperatingSystemMXBean.getFreeSwapSpaceSize: %d", osBean.getFreeSwapSpaceSize())); 1.43 + System.out.println(String.format("OperatingSystemMXBean.getSystemCpuLoad: %f", osBean.getSystemCpuLoad())); 1.44 + } 1.45 + 1.46 +}
2.1 --- a/test/runtime/containers/docker/TestCPUAwareness.java Mon May 11 18:49:01 2020 +0000 2.2 +++ b/test/runtime/containers/docker/TestCPUAwareness.java Fri Jul 03 15:09:27 2020 +0200 2.3 @@ -25,7 +25,9 @@ 2.4 /* 2.5 * @test 2.6 * @summary Test JVM's CPU resource awareness when running inside docker container 2.7 - * @library /testlibrary 2.8 + * @library /testlibrary /testlibrary/whitebox 2.9 + * @build sun.hotspot.WhiteBox PrintContainerInfo CheckOperatingSystemMXBean 2.10 + * @run driver ClassFileInstaller -jar whitebox.jar sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission 2.11 * @run driver TestCPUAwareness 2.12 */ 2.13 2.14 @@ -72,6 +74,14 @@ 2.15 testCpuQuotaAndPeriod(150*1000, 100*1000); 2.16 testCpuQuotaAndPeriod(400*1000, 100*1000); 2.17 2.18 + testOperatingSystemMXBeanAwareness("0.5", "1"); 2.19 + testOperatingSystemMXBeanAwareness("1.0", "1"); 2.20 + if (availableCPUs > 2) { 2.21 + testOperatingSystemMXBeanAwareness("1.2", "2"); 2.22 + testOperatingSystemMXBeanAwareness("1.8", "2"); 2.23 + testOperatingSystemMXBeanAwareness("2.0", "2"); 2.24 + } 2.25 + 2.26 } finally { 2.27 DockerTestUtils.removeDockerImage(imageName); 2.28 } 2.29 @@ -202,4 +212,21 @@ 2.30 .shouldMatch("CPU Shares is.*" + shares) 2.31 .shouldMatch("active_processor_count.*" + expectedAPC); 2.32 } 2.33 + 2.34 + private static void testOperatingSystemMXBeanAwareness(String cpuAllocation, String expectedCpus) throws Exception { 2.35 + Common.logNewTestCase("Check OperatingSystemMXBean"); 2.36 + 2.37 + DockerRunOptions opts = Common.newOpts(imageName, "CheckOperatingSystemMXBean") 2.38 + .addDockerOpts( 2.39 + "--cpus", cpuAllocation 2.40 + ); 2.41 + 2.42 + DockerTestUtils.dockerRunJava(opts) 2.43 + .shouldHaveExitValue(0) 2.44 + .shouldContain("Checking OperatingSystemMXBean") 2.45 + .shouldContain("Runtime.availableProcessors: " + expectedCpus) 2.46 + .shouldContain("OperatingSystemMXBean.getAvailableProcessors: " + expectedCpus) 2.47 + .shouldMatch("OperatingSystemMXBean\\.getSystemCpuLoad: [0-9]+\\.[0-9]+") 2.48 + ; 2.49 + } 2.50 }
3.1 --- a/test/runtime/containers/docker/TestMemoryAwareness.java Mon May 11 18:49:01 2020 +0000 3.2 +++ b/test/runtime/containers/docker/TestMemoryAwareness.java Fri Jul 03 15:09:27 2020 +0200 3.3 @@ -26,7 +26,7 @@ 3.4 * @test 3.5 * @summary Test JVM's memory resource awareness when running inside docker container 3.6 * @library /testlibrary /testlibrary/whitebox 3.7 - * @build AttemptOOM sun.hotspot.WhiteBox PrintContainerInfo 3.8 + * @build AttemptOOM sun.hotspot.WhiteBox PrintContainerInfo CheckOperatingSystemMXBean 3.9 * @run driver ClassFileInstaller -jar whitebox.jar sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission 3.10 * @run driver TestMemoryAwareness 3.11 */ 3.12 @@ -59,6 +59,18 @@ 3.13 // Add extra 10 Mb to allocator limit, to be sure to cause OOM 3.14 testOOM("256m", 256 + 10); 3.15 3.16 + testOperatingSystemMXBeanAwareness( 3.17 + "100M", Integer.toString(((int) Math.pow(2, 20)) * 100), 3.18 + "150M", Integer.toString(((int) Math.pow(2, 20)) * (150 - 100)) 3.19 + ); 3.20 + testOperatingSystemMXBeanAwareness( 3.21 + "128M", Integer.toString(((int) Math.pow(2, 20)) * 128), 3.22 + "256M", Integer.toString(((int) Math.pow(2, 20)) * (256 - 128)) 3.23 + ); 3.24 + testOperatingSystemMXBeanAwareness( 3.25 + "1G", Integer.toString(((int) Math.pow(2, 20)) * 1024), 3.26 + "1500M", Integer.toString(((int) Math.pow(2, 20)) * (1500 - 1024)) 3.27 + ); 3.28 } finally { 3.29 DockerTestUtils.removeDockerImage(imageName); 3.30 } 3.31 @@ -106,4 +118,24 @@ 3.32 .shouldContain("java.lang.OutOfMemoryError"); 3.33 } 3.34 3.35 + private static void testOperatingSystemMXBeanAwareness(String memoryAllocation, String expectedMemory, 3.36 + String swapAllocation, String expectedSwap) throws Exception { 3.37 + Common.logNewTestCase("Check OperatingSystemMXBean"); 3.38 + 3.39 + DockerRunOptions opts = Common.newOpts(imageName, "CheckOperatingSystemMXBean") 3.40 + .addDockerOpts( 3.41 + "--memory", memoryAllocation, 3.42 + "--memory-swap", swapAllocation 3.43 + ); 3.44 + 3.45 + DockerTestUtils.dockerRunJava(opts) 3.46 + .shouldHaveExitValue(0) 3.47 + .shouldContain("Checking OperatingSystemMXBean") 3.48 + .shouldContain("OperatingSystemMXBean.getTotalPhysicalMemorySize: " + expectedMemory) 3.49 + .shouldMatch("OperatingSystemMXBean\\.getFreePhysicalMemorySize: [1-9][0-9]+") 3.50 + .shouldContain("OperatingSystemMXBean.getTotalSwapSpaceSize: " + expectedSwap) 3.51 + .shouldMatch("OperatingSystemMXBean\\.getFreeSwapSpaceSize: [1-9][0-9]+") 3.52 + ; 3.53 + } 3.54 + 3.55 }