changeset 10778:1d9ed8dec94c

8226575: OperatingSystemMXBean should be made container aware Reviewed-by: andrew
author sgehwolf
date Fri, 03 Jul 2020 15:09:27 +0200
parents 3746571843dd
children 9ceaa376784a
files test/runtime/containers/docker/CheckOperatingSystemMXBean.java test/runtime/containers/docker/TestCPUAwareness.java test/runtime/containers/docker/TestMemoryAwareness.java
diffstat 3 files changed, 104 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/runtime/containers/docker/CheckOperatingSystemMXBean.java	Fri Jul 03 15:09:27 2020 +0200
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2019, Red Hat Inc. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import com.sun.management.OperatingSystemMXBean;
+import java.lang.management.ManagementFactory;
+
+public class CheckOperatingSystemMXBean {
+
+    public static void main(String[] args) {
+        System.out.println("Checking OperatingSystemMXBean");
+
+        OperatingSystemMXBean osBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
+        System.out.println(String.format("Runtime.availableProcessors: %d", Runtime.getRuntime().availableProcessors()));
+        System.out.println(String.format("OperatingSystemMXBean.getAvailableProcessors: %d", osBean.getAvailableProcessors()));
+        System.out.println(String.format("OperatingSystemMXBean.getTotalPhysicalMemorySize: %d", osBean.getTotalPhysicalMemorySize()));
+        System.out.println(String.format("OperatingSystemMXBean.getFreePhysicalMemorySize: %d", osBean.getFreePhysicalMemorySize()));
+        System.out.println(String.format("OperatingSystemMXBean.getTotalSwapSpaceSize: %d", osBean.getTotalSwapSpaceSize()));
+        System.out.println(String.format("OperatingSystemMXBean.getFreeSwapSpaceSize: %d", osBean.getFreeSwapSpaceSize()));
+        System.out.println(String.format("OperatingSystemMXBean.getSystemCpuLoad: %f", osBean.getSystemCpuLoad()));
+    }
+
+}
--- a/test/runtime/containers/docker/TestCPUAwareness.java	Mon May 11 18:49:01 2020 +0000
+++ b/test/runtime/containers/docker/TestCPUAwareness.java	Fri Jul 03 15:09:27 2020 +0200
@@ -25,7 +25,9 @@
 /*
  * @test
  * @summary Test JVM's CPU resource awareness when running inside docker container
- * @library /testlibrary
+ * @library /testlibrary /testlibrary/whitebox
+ * @build sun.hotspot.WhiteBox PrintContainerInfo CheckOperatingSystemMXBean
+ * @run driver ClassFileInstaller -jar whitebox.jar sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
  * @run driver TestCPUAwareness
  */
 
@@ -72,6 +74,14 @@
             testCpuQuotaAndPeriod(150*1000, 100*1000);
             testCpuQuotaAndPeriod(400*1000, 100*1000);
 
+            testOperatingSystemMXBeanAwareness("0.5", "1");
+            testOperatingSystemMXBeanAwareness("1.0", "1");
+            if (availableCPUs > 2) {
+                testOperatingSystemMXBeanAwareness("1.2", "2");
+                testOperatingSystemMXBeanAwareness("1.8", "2");
+                testOperatingSystemMXBeanAwareness("2.0", "2");
+            }
+
         } finally {
             DockerTestUtils.removeDockerImage(imageName);
         }
@@ -202,4 +212,21 @@
             .shouldMatch("CPU Shares is.*" + shares)
             .shouldMatch("active_processor_count.*" + expectedAPC);
     }
+
+    private static void testOperatingSystemMXBeanAwareness(String cpuAllocation, String expectedCpus) throws Exception {
+        Common.logNewTestCase("Check OperatingSystemMXBean");
+
+        DockerRunOptions opts = Common.newOpts(imageName, "CheckOperatingSystemMXBean")
+            .addDockerOpts(
+                "--cpus", cpuAllocation
+            );
+
+        DockerTestUtils.dockerRunJava(opts)
+            .shouldHaveExitValue(0)
+            .shouldContain("Checking OperatingSystemMXBean")
+            .shouldContain("Runtime.availableProcessors: " + expectedCpus)
+            .shouldContain("OperatingSystemMXBean.getAvailableProcessors: " + expectedCpus)
+            .shouldMatch("OperatingSystemMXBean\\.getSystemCpuLoad: [0-9]+\\.[0-9]+")
+            ;
+    }
 }
--- a/test/runtime/containers/docker/TestMemoryAwareness.java	Mon May 11 18:49:01 2020 +0000
+++ b/test/runtime/containers/docker/TestMemoryAwareness.java	Fri Jul 03 15:09:27 2020 +0200
@@ -26,7 +26,7 @@
  * @test
  * @summary Test JVM's memory resource awareness when running inside docker container
  * @library /testlibrary /testlibrary/whitebox
- * @build AttemptOOM sun.hotspot.WhiteBox PrintContainerInfo
+ * @build AttemptOOM sun.hotspot.WhiteBox PrintContainerInfo CheckOperatingSystemMXBean
  * @run driver ClassFileInstaller -jar whitebox.jar sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
  * @run driver TestMemoryAwareness
  */
@@ -59,6 +59,18 @@
             // Add extra 10 Mb to allocator limit, to be sure to cause OOM
             testOOM("256m", 256 + 10);
 
+            testOperatingSystemMXBeanAwareness(
+                "100M", Integer.toString(((int) Math.pow(2, 20)) * 100),
+                "150M", Integer.toString(((int) Math.pow(2, 20)) * (150 - 100))
+            );
+            testOperatingSystemMXBeanAwareness(
+                "128M", Integer.toString(((int) Math.pow(2, 20)) * 128),
+                "256M", Integer.toString(((int) Math.pow(2, 20)) * (256 - 128))
+            );
+            testOperatingSystemMXBeanAwareness(
+                "1G", Integer.toString(((int) Math.pow(2, 20)) * 1024),
+                "1500M", Integer.toString(((int) Math.pow(2, 20)) * (1500 - 1024))
+            );
         } finally {
             DockerTestUtils.removeDockerImage(imageName);
         }
@@ -106,4 +118,24 @@
             .shouldContain("java.lang.OutOfMemoryError");
     }
 
+    private static void testOperatingSystemMXBeanAwareness(String memoryAllocation, String expectedMemory,
+            String swapAllocation, String expectedSwap) throws Exception {
+        Common.logNewTestCase("Check OperatingSystemMXBean");
+
+        DockerRunOptions opts = Common.newOpts(imageName, "CheckOperatingSystemMXBean")
+            .addDockerOpts(
+                "--memory", memoryAllocation,
+                "--memory-swap", swapAllocation
+            );
+
+        DockerTestUtils.dockerRunJava(opts)
+            .shouldHaveExitValue(0)
+            .shouldContain("Checking OperatingSystemMXBean")
+            .shouldContain("OperatingSystemMXBean.getTotalPhysicalMemorySize: " + expectedMemory)
+            .shouldMatch("OperatingSystemMXBean\\.getFreePhysicalMemorySize: [1-9][0-9]+")
+            .shouldContain("OperatingSystemMXBean.getTotalSwapSpaceSize: " + expectedSwap)
+            .shouldMatch("OperatingSystemMXBean\\.getFreeSwapSpaceSize: [1-9][0-9]+")
+            ;
+    }
+
 }