From 2f8225e583103c185d258f0af13a86798fe5fb7f Mon Sep 17 00:00:00 2001 From: Severin Gehwolf Date: Wed, 18 Jan 2023 16:25:07 +0100 Subject: [PATCH 1/3] 8300659: Refactor TestMemoryAwareness to use WhiteBox api for host values --- src/hotspot/share/prims/whitebox.cpp | 15 +++++++ .../docker/TestMemoryAwareness.java | 43 +++++++++++-------- test/lib/jdk/test/whitebox/WhiteBox.java | 2 + 3 files changed, 43 insertions(+), 17 deletions(-) diff --git a/src/hotspot/share/prims/whitebox.cpp b/src/hotspot/share/prims/whitebox.cpp index 626a1f90abc04..1dd3af7650bb5 100644 --- a/src/hotspot/share/prims/whitebox.cpp +++ b/src/hotspot/share/prims/whitebox.cpp @@ -113,6 +113,7 @@ #include "jvmci/jvmciRuntime.hpp" #endif #ifdef LINUX +#include "os_linux.hpp" #include "osContainer_linux.hpp" #include "cgroupSubsystem_linux.hpp" #endif @@ -2353,6 +2354,18 @@ WB_ENTRY(jboolean, WB_IsContainerized(JNIEnv* env, jobject o)) return false; WB_END +// Physical memory of the host machine (including containers) +WB_ENTRY(jlong, WB_PhysicalMemory(JNIEnv* env, jobject o)) + LINUX_ONLY(return os::Linux::physical_memory();) + return os::physical_memory(); +WB_END + +// Physical swap of the host machine (including containers), Linux only. +WB_ENTRY(jlong, WB_PhysicalSwap(JNIEnv* env, jobject o)) + LINUX_ONLY(return (jlong)os::Linux::host_swap();) + return -1; // Not used/implemented on other platforms +WB_END + WB_ENTRY(jint, WB_ValidateCgroup(JNIEnv* env, jobject o, jstring proc_cgroups, @@ -2752,6 +2765,8 @@ static JNINativeMethod methods[] = { {CC"validateCgroup", CC"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I", (void*)&WB_ValidateCgroup }, + {CC"physicalMemory", CC"()J", (void*)&WB_PhysicalMemory }, + {CC"physicalSwap", CC"()J", (void*)&WB_PhysicalSwap }, {CC"printOsInfo", CC"()V", (void*)&WB_PrintOsInfo }, {CC"disableElfSectionCache", CC"()V", (void*)&WB_DisableElfSectionCache }, {CC"resolvedMethodItemsCount", CC"()J", (void*)&WB_ResolvedMethodItemsCount }, diff --git a/test/hotspot/jtreg/containers/docker/TestMemoryAwareness.java b/test/hotspot/jtreg/containers/docker/TestMemoryAwareness.java index e71780a3ea787..1146086a25979 100644 --- a/test/hotspot/jtreg/containers/docker/TestMemoryAwareness.java +++ b/test/hotspot/jtreg/containers/docker/TestMemoryAwareness.java @@ -35,23 +35,26 @@ * jdk.jartool/sun.tools.jar * @build AttemptOOM jdk.test.whitebox.WhiteBox PrintContainerInfo CheckOperatingSystemMXBean * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar whitebox.jar jdk.test.whitebox.WhiteBox - * @run driver TestMemoryAwareness + * @run main/othervm -Xbootclasspath/a:whitebox.jar -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI TestMemoryAwareness */ import jdk.test.lib.containers.docker.Common; import jdk.test.lib.containers.docker.DockerRunOptions; import jdk.test.lib.containers.docker.DockerTestUtils; +import jdk.test.whitebox.WhiteBox; import jdk.test.lib.process.OutputAnalyzer; import static jdk.test.lib.Asserts.assertNotNull; public class TestMemoryAwareness { private static final String imageName = Common.imageName("memory"); + private static final WhiteBox wb = WhiteBox.getWhiteBox(); - private static String getHostMaxMemory() throws Exception { - DockerRunOptions opts = Common.newOpts(imageName); - String goodMem = Common.run(opts).firstMatch("total physical memory: (\\d+)", 1); - assertNotNull(goodMem, "no match for 'total physical memory' in trace output"); - return goodMem; + private static String getHostMaxMemory() { + return Long.valueOf(wb.physicalMemory()).toString(); + } + + private static String getHostSwap() { + return Long.valueOf(wb.physicalSwap()).toString(); } public static void main(String[] args) throws Exception { @@ -92,10 +95,9 @@ public static void main(String[] args) throws Exception { "200M", Integer.toString(((int) Math.pow(2, 20)) * (200 - 100)), true /* additional cgroup fs mounts */ ); - final String hostMaxMem = getHostMaxMemory(); - testOperatingSystemMXBeanIgnoresMemLimitExceedingPhysicalMemory(hostMaxMem); - testMetricsIgnoresMemLimitExceedingPhysicalMemory(hostMaxMem); - testContainerMemExceedsPhysical(hostMaxMem); + testOSMXBeanIgnoresMemLimitExceedingPhysicalMemory(); + testMetricsExceedingPhysicalMemory(); + testContainerMemExceedsPhysical(); } finally { if (!DockerTestUtils.RETAIN_IMAGE_AFTER_TEST) { DockerTestUtils.removeDockerImage(imageName); @@ -122,9 +124,10 @@ private static void testMemoryLimit(String valueToSet, String expectedTraceValue // JDK-8292083 // Ensure that Java ignores container memory limit values above the host's physical memory. - private static void testContainerMemExceedsPhysical(final String hostMaxMem) + private static void testContainerMemExceedsPhysical() throws Exception { Common.logNewTestCase("container memory limit exceeds physical memory"); + String hostMaxMem = getHostMaxMemory(); String badMem = hostMaxMem + "0"; // set a container memory limit to the bad value DockerRunOptions opts = Common.newOpts(imageName) @@ -207,12 +210,17 @@ private static void testOperatingSystemMXBeanAwareness(String memoryAllocation, // in case of warnings like : "Your kernel does not support swap limit capabilities // or the cgroup is not mounted. Memory limited without swap." - // the getTotalSwapSpaceSize and getFreeSwapSpaceSize return the system - // values as the container setup isn't supported in that case. + // the getTotalSwapSpaceSize either returns the system (or host) values, or 0 + // if a container memory limit is in place and gets detected. A value of 0 is because, + // Metrics.getMemoryLimit() returns the same value as Metrics.getMemoryAndSwapLimit(). + // + // getFreeSwapSpaceSize() are a function of what getTotalSwapSpaceSize() returns. Either + // a number > 0, or 0 if getTotalSwapSpaceSize() == 0. try { out.shouldContain("OperatingSystemMXBean.getTotalSwapSpaceSize: " + expectedSwap); } catch(RuntimeException ex) { - out.shouldMatch("OperatingSystemMXBean.getTotalSwapSpaceSize: [0-9]+"); + String hostSwap = getHostSwap(); + out.shouldMatch("OperatingSystemMXBean.getTotalSwapSpaceSize: (0|" + hostSwap + ")"); } try { @@ -224,17 +232,18 @@ private static void testOperatingSystemMXBeanAwareness(String memoryAllocation, // JDK-8292541: Ensure OperatingSystemMXBean ignores container memory limits above the host's physical memory. - private static void testOperatingSystemMXBeanIgnoresMemLimitExceedingPhysicalMemory(final String hostMaxMem) + private static void testOSMXBeanIgnoresMemLimitExceedingPhysicalMemory() throws Exception { + String hostMaxMem = getHostMaxMemory(); String badMem = hostMaxMem + "0"; testOperatingSystemMXBeanAwareness(badMem, hostMaxMem, badMem, hostMaxMem); } // JDK-8292541: Ensure Metrics ignores container memory limits above the host's physical memory. - private static void testMetricsIgnoresMemLimitExceedingPhysicalMemory(final String hostMaxMem) + private static void testMetricsExceedingPhysicalMemory() throws Exception { Common.logNewTestCase("Metrics ignore container memory limit exceeding physical memory"); - String badMem = hostMaxMem + "0"; + String badMem = getHostMaxMemory() + "0"; DockerRunOptions opts = Common.newOpts(imageName) .addJavaOpts("-XshowSettings:system") .addDockerOpts("--memory", badMem); diff --git a/test/lib/jdk/test/whitebox/WhiteBox.java b/test/lib/jdk/test/whitebox/WhiteBox.java index a6cf27a79ba5d..c951a8832a75f 100644 --- a/test/lib/jdk/test/whitebox/WhiteBox.java +++ b/test/lib/jdk/test/whitebox/WhiteBox.java @@ -708,6 +708,8 @@ public native int validateCgroup(String procCgroups, String procSelfCgroup, String procSelfMountinfo); public native void printOsInfo(); + public native long physicalMemory(); + public native long physicalSwap(); // Decoder public native void disableElfSectionCache(); From 06ab3d80e320499de39d207a843882e2661dd018 Mon Sep 17 00:00:00 2001 From: Severin Gehwolf Date: Tue, 24 Jan 2023 14:42:51 +0100 Subject: [PATCH 2/3] Rename to hostPhysicalMemory/Swap --- src/hotspot/share/prims/whitebox.cpp | 8 ++++---- .../jtreg/containers/docker/TestMemoryAwareness.java | 4 ++-- test/lib/jdk/test/whitebox/WhiteBox.java | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/hotspot/share/prims/whitebox.cpp b/src/hotspot/share/prims/whitebox.cpp index 1dd3af7650bb5..601f27f7a93c4 100644 --- a/src/hotspot/share/prims/whitebox.cpp +++ b/src/hotspot/share/prims/whitebox.cpp @@ -2355,13 +2355,13 @@ WB_ENTRY(jboolean, WB_IsContainerized(JNIEnv* env, jobject o)) WB_END // Physical memory of the host machine (including containers) -WB_ENTRY(jlong, WB_PhysicalMemory(JNIEnv* env, jobject o)) +WB_ENTRY(jlong, WB_HostPhysicalMemory(JNIEnv* env, jobject o)) LINUX_ONLY(return os::Linux::physical_memory();) return os::physical_memory(); WB_END // Physical swap of the host machine (including containers), Linux only. -WB_ENTRY(jlong, WB_PhysicalSwap(JNIEnv* env, jobject o)) +WB_ENTRY(jlong, WB_HostPhysicalSwap(JNIEnv* env, jobject o)) LINUX_ONLY(return (jlong)os::Linux::host_swap();) return -1; // Not used/implemented on other platforms WB_END @@ -2765,8 +2765,8 @@ static JNINativeMethod methods[] = { {CC"validateCgroup", CC"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I", (void*)&WB_ValidateCgroup }, - {CC"physicalMemory", CC"()J", (void*)&WB_PhysicalMemory }, - {CC"physicalSwap", CC"()J", (void*)&WB_PhysicalSwap }, + {CC"hostPhysicalMemory", CC"()J", (void*)&WB_HostPhysicalMemory }, + {CC"hostPhysicalSwap", CC"()J", (void*)&WB_HostPhysicalSwap }, {CC"printOsInfo", CC"()V", (void*)&WB_PrintOsInfo }, {CC"disableElfSectionCache", CC"()V", (void*)&WB_DisableElfSectionCache }, {CC"resolvedMethodItemsCount", CC"()J", (void*)&WB_ResolvedMethodItemsCount }, diff --git a/test/hotspot/jtreg/containers/docker/TestMemoryAwareness.java b/test/hotspot/jtreg/containers/docker/TestMemoryAwareness.java index 1146086a25979..51859713b6d94 100644 --- a/test/hotspot/jtreg/containers/docker/TestMemoryAwareness.java +++ b/test/hotspot/jtreg/containers/docker/TestMemoryAwareness.java @@ -50,11 +50,11 @@ public class TestMemoryAwareness { private static final WhiteBox wb = WhiteBox.getWhiteBox(); private static String getHostMaxMemory() { - return Long.valueOf(wb.physicalMemory()).toString(); + return Long.valueOf(wb.hostPhysicalMemory()).toString(); } private static String getHostSwap() { - return Long.valueOf(wb.physicalSwap()).toString(); + return Long.valueOf(wb.hostPhysicalSwap()).toString(); } public static void main(String[] args) throws Exception { diff --git a/test/lib/jdk/test/whitebox/WhiteBox.java b/test/lib/jdk/test/whitebox/WhiteBox.java index c951a8832a75f..125f64c967c86 100644 --- a/test/lib/jdk/test/whitebox/WhiteBox.java +++ b/test/lib/jdk/test/whitebox/WhiteBox.java @@ -708,8 +708,8 @@ public native int validateCgroup(String procCgroups, String procSelfCgroup, String procSelfMountinfo); public native void printOsInfo(); - public native long physicalMemory(); - public native long physicalSwap(); + public native long hostPhysicalMemory(); + public native long hostPhysicalSwap(); // Decoder public native void disableElfSectionCache(); From 9d72dbae7ee940f5b8777ec5728e113b6816ec29 Mon Sep 17 00:00:00 2001 From: Severin Gehwolf Date: Tue, 24 Jan 2023 15:30:03 +0100 Subject: [PATCH 3/3] Update copyright year to 2023 --- src/hotspot/share/prims/whitebox.cpp | 2 +- test/hotspot/jtreg/containers/docker/TestMemoryAwareness.java | 2 +- test/lib/jdk/test/whitebox/WhiteBox.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/hotspot/share/prims/whitebox.cpp b/src/hotspot/share/prims/whitebox.cpp index 601f27f7a93c4..9b5949b9974ea 100644 --- a/src/hotspot/share/prims/whitebox.cpp +++ b/src/hotspot/share/prims/whitebox.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2023, Oracle and/or its affiliates. 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 diff --git a/test/hotspot/jtreg/containers/docker/TestMemoryAwareness.java b/test/hotspot/jtreg/containers/docker/TestMemoryAwareness.java index 51859713b6d94..8350087ae7853 100644 --- a/test/hotspot/jtreg/containers/docker/TestMemoryAwareness.java +++ b/test/hotspot/jtreg/containers/docker/TestMemoryAwareness.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2023, Oracle and/or its affiliates. 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 diff --git a/test/lib/jdk/test/whitebox/WhiteBox.java b/test/lib/jdk/test/whitebox/WhiteBox.java index 125f64c967c86..ae8b15c2f7b1c 100644 --- a/test/lib/jdk/test/whitebox/WhiteBox.java +++ b/test/lib/jdk/test/whitebox/WhiteBox.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2023, Oracle and/or its affiliates. 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