Fri, 24 Jul 2020 14:31:02 +0200
8250627: Use -XX:+/-UseContainerSupport for enabling/disabling Java container metrics
Reviewed-by: aph, dholmes, bobv, shade
1.1 --- a/make/lib/CoreLibraries.gmk Wed Aug 19 18:01:24 2020 +0200 1.2 +++ b/make/lib/CoreLibraries.gmk Fri Jul 24 14:31:02 2020 +0200 1.3 @@ -154,6 +154,10 @@ 1.4 LIBJAVA_SRC_DIRS += $(JDK_TOPDIR)/src/macosx/native/sun/util/locale/provider 1.5 endif 1.6 1.7 +ifeq ($(OPENJDK_TARGET_OS), linux) 1.8 + LIBJAVA_SRC_DIRS += $(JDK_TOPDIR)/src/linux/native/jdk/internal/platform/cgroupv1 1.9 +endif 1.10 + 1.11 ifeq ($(OPENJDK_TARGET_OS), windows) 1.12 LIBJAVA_SRC_DIRS += $(JDK_TOPDIR)/src/$(OPENJDK_TARGET_OS_API_DIR)/native/sun/security/provider \ 1.13 $(JDK_TOPDIR)/src/$(OPENJDK_TARGET_OS_API_DIR)/native/sun/io
2.1 --- a/make/mapfiles/libjava/mapfile-vers Wed Aug 19 18:01:24 2020 +0200 2.2 +++ b/make/mapfiles/libjava/mapfile-vers Fri Jul 24 14:31:02 2020 +0200 2.3 @@ -278,6 +278,7 @@ 2.4 Java_sun_misc_VM_initialize; 2.5 Java_sun_misc_VMSupport_initAgentProperties; 2.6 Java_sun_misc_VMSupport_getVMTemporaryDirectory; 2.7 + Java_jdk_internal_platform_cgroupv1_Metrics_isUseContainerSupport; 2.8 2.9 # ZipFile.c needs this one 2.10 throwFileNotFoundException;
3.1 --- a/src/linux/classes/jdk/internal/platform/cgroupv1/Metrics.java Wed Aug 19 18:01:24 2020 +0200 3.2 +++ b/src/linux/classes/jdk/internal/platform/cgroupv1/Metrics.java Fri Jul 24 14:31:02 2020 +0200 3.3 @@ -57,6 +57,9 @@ 3.4 } 3.5 3.6 private static Metrics initContainerSubSystems() { 3.7 + if (!isUseContainerSupport()) { 3.8 + return null; 3.9 + } 3.10 Metrics metrics = new Metrics(); 3.11 3.12 /** 3.13 @@ -458,4 +461,6 @@ 3.14 return SubSystem.getLongEntry(blkio, "blkio.throttle.io_serviced", "Total"); 3.15 } 3.16 3.17 + private static native boolean isUseContainerSupport(); 3.18 + 3.19 }
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/src/linux/native/jdk/internal/platform/cgroupv1/Metrics.c Fri Jul 24 14:31:02 2020 +0200 4.3 @@ -0,0 +1,35 @@ 4.4 +/* 4.5 + * Copyright (c) 2020, Red Hat, Inc. 4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4.7 + * 4.8 + * This code is free software; you can redistribute it and/or modify it 4.9 + * under the terms of the GNU General Public License version 2 only, as 4.10 + * published by the Free Software Foundation. Oracle designates this 4.11 + * particular file as subject to the "Classpath" exception as provided 4.12 + * by Oracle in the LICENSE file that accompanied this code. 4.13 + * 4.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 4.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 4.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 4.17 + * version 2 for more details (a copy is included in the LICENSE file that 4.18 + * accompanied this code). 4.19 + * 4.20 + * You should have received a copy of the GNU General Public License version 4.21 + * 2 along with this work; if not, write to the Free Software Foundation, 4.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 4.23 + * 4.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 4.25 + * or visit www.oracle.com if you need additional information or have any 4.26 + * questions. 4.27 + */ 4.28 + 4.29 +#include "jni.h" 4.30 +#include "jvm.h" 4.31 + 4.32 +#include "jdk_internal_platform_cgroupv1_Metrics.h" 4.33 + 4.34 +JNIEXPORT jboolean JNICALL 4.35 +Java_jdk_internal_platform_cgroupv1_Metrics_isUseContainerSupport(JNIEnv *env, jclass ignored) 4.36 +{ 4.37 + return JVM_IsUseContainerSupport(); 4.38 +}
5.1 --- a/src/share/javavm/export/jvm.h Wed Aug 19 18:01:24 2020 +0200 5.2 +++ b/src/share/javavm/export/jvm.h Fri Jul 24 14:31:02 2020 +0200 5.3 @@ -175,6 +175,9 @@ 5.4 JNIEXPORT jint JNICALL 5.5 JVM_ActiveProcessorCount(void); 5.6 5.7 +JNIEXPORT jboolean JNICALL 5.8 +JVM_IsUseContainerSupport(void); 5.9 + 5.10 JNIEXPORT void * JNICALL 5.11 JVM_LoadLibrary(const char *name); 5.12
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/test/jdk/internal/platform/docker/CheckUseContainerSupport.java Fri Jul 24 14:31:02 2020 +0200 6.3 @@ -0,0 +1,46 @@ 6.4 +/* 6.5 + * Copyright (c) 2020, Red Hat, Inc. 6.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6.7 + * 6.8 + * This code is free software; you can redistribute it and/or modify it 6.9 + * under the terms of the GNU General Public License version 2 only, as 6.10 + * published by the Free Software Foundation. 6.11 + * 6.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 6.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 6.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 6.15 + * version 2 for more details (a copy is included in the LICENSE file that 6.16 + * accompanied this code). 6.17 + * 6.18 + * You should have received a copy of the GNU General Public License version 6.19 + * 2 along with this work; if not, write to the Free Software Foundation, 6.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 6.21 + * 6.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 6.23 + * or visit www.oracle.com if you need additional information or have any 6.24 + * questions. 6.25 + */ 6.26 + 6.27 +import jdk.internal.platform.Metrics; 6.28 + 6.29 +public class CheckUseContainerSupport { 6.30 + 6.31 + // Usage: boolean value of -XX:+/-UseContainerSupport 6.32 + // passed as the only argument 6.33 + public static void main(String[] args) throws Exception { 6.34 + if (args.length != 1) { 6.35 + throw new RuntimeException("Expected only one boolean argument"); 6.36 + } 6.37 + boolean expectedContainerSupport = Boolean.parseBoolean(args[0]); 6.38 + boolean actualContainerSupport = (Metrics.systemMetrics() != null); 6.39 + if (expectedContainerSupport != actualContainerSupport) { 6.40 + String msg = "-XX:" + ( expectedContainerSupport ? "+" : "-") + "UseContainerSupport, but got " + 6.41 + "Metrics.systemMetrics() == " + (Metrics.systemMetrics() == null ? "null" : "non-null"); 6.42 + System.out.println(msg); 6.43 + System.out.println("TEST FAILED!!!"); 6.44 + return; 6.45 + } 6.46 + System.out.println("TEST PASSED!!!"); 6.47 + } 6.48 + 6.49 +}
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/test/jdk/internal/platform/docker/TestUseContainerSupport.java Fri Jul 24 14:31:02 2020 +0200 7.3 @@ -0,0 +1,71 @@ 7.4 +/* 7.5 + * Copyright (c) 2020, Red Hat, Inc. 7.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 7.7 + * 7.8 + * This code is free software; you can redistribute it and/or modify it 7.9 + * under the terms of the GNU General Public License version 2 only, as 7.10 + * published by the Free Software Foundation. 7.11 + * 7.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 7.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 7.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 7.15 + * version 2 for more details (a copy is included in the LICENSE file that 7.16 + * accompanied this code). 7.17 + * 7.18 + * You should have received a copy of the GNU General Public License version 7.19 + * 2 along with this work; if not, write to the Free Software Foundation, 7.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 7.21 + * 7.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 7.23 + * or visit www.oracle.com if you need additional information or have any 7.24 + * questions. 7.25 + */ 7.26 + 7.27 +/* 7.28 + * @test 7.29 + * @summary UseContainerSupport flag should reflect Metrics being available 7.30 + * @library /lib 7.31 + * @build CheckUseContainerSupport 7.32 + * @run main/timeout=360 TestUseContainerSupport 7.33 + */ 7.34 + 7.35 +import jdk.test.lib.Utils; 7.36 +import jdk.test.lib.containers.docker.Common; 7.37 +import jdk.test.lib.containers.docker.DockerRunOptions; 7.38 +import jdk.test.lib.containers.docker.DockerTestUtils; 7.39 + 7.40 +public class TestUseContainerSupport { 7.41 + private static final String imageName = Common.imageName("useContainerSupport"); 7.42 + 7.43 + public static void main(String[] args) throws Exception { 7.44 + if (!DockerTestUtils.canTestDocker()) { 7.45 + return; 7.46 + } 7.47 + 7.48 + DockerTestUtils.buildJdkDockerImage(imageName, "Dockerfile-BasicTest", "jdk-docker"); 7.49 + 7.50 + try { 7.51 + testUseContainerSupport(true); 7.52 + testUseContainerSupport(false); 7.53 + } finally { 7.54 + DockerTestUtils.removeDockerImage(imageName); 7.55 + } 7.56 + } 7.57 + 7.58 + private static void testUseContainerSupport(boolean useContainerSupport) throws Exception { 7.59 + String testMsg = " with -XX:" + (useContainerSupport ? "+" : "-") + "UseContainerSupport"; 7.60 + Common.logNewTestCase("Test TestUseContainerSupport" + testMsg); 7.61 + DockerRunOptions opts = 7.62 + new DockerRunOptions(imageName, "/jdk/bin/java", "CheckUseContainerSupport"); 7.63 + opts.addClassOptions(Boolean.valueOf(useContainerSupport).toString()); 7.64 + opts.addDockerOpts("--memory", "200m") 7.65 + .addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/"); 7.66 + if (useContainerSupport) { 7.67 + opts.addJavaOpts("-XX:+UseContainerSupport"); 7.68 + } else { 7.69 + opts.addJavaOpts("-XX:-UseContainerSupport"); 7.70 + } 7.71 + opts.addJavaOpts("-cp", "/test-classes/"); 7.72 + DockerTestUtils.dockerRunJava(opts).shouldHaveExitValue(0).shouldContain("TEST PASSED!!!"); 7.73 + } 7.74 +}