From 8bc2fbe57893b110fdb5fd567df4615e7833e5ae Mon Sep 17 00:00:00 2001 From: Sonia Zaldana Calles Date: Tue, 18 Jun 2024 14:05:11 +0000 Subject: [PATCH] 8333769: Pretouching tests dont test pretouching Reviewed-by: stuefe, asmehra --- src/hotspot/os/aix/os_aix.cpp | 2 + src/hotspot/os/bsd/os_bsd.cpp | 16 +++ src/hotspot/os/linux/os_linux.cpp | 9 ++ src/hotspot/os/windows/os_windows.cpp | 13 ++ src/hotspot/share/prims/whitebox.cpp | 6 + src/hotspot/share/runtime/os.hpp | 1 + .../jtreg/gc/TestAlwaysPreTouchBehavior.java | 130 ++++++++++++++++++ .../parallel/TestAlwaysPreTouchBehavior.java | 79 ----------- test/lib/jdk/test/whitebox/WhiteBox.java | 1 + 9 files changed, 178 insertions(+), 79 deletions(-) create mode 100644 test/hotspot/jtreg/gc/TestAlwaysPreTouchBehavior.java delete mode 100644 test/hotspot/jtreg/gc/parallel/TestAlwaysPreTouchBehavior.java diff --git a/src/hotspot/os/aix/os_aix.cpp b/src/hotspot/os/aix/os_aix.cpp index f0a984d3d1f..39f3e420662 100644 --- a/src/hotspot/os/aix/os_aix.cpp +++ b/src/hotspot/os/aix/os_aix.cpp @@ -287,6 +287,8 @@ julong os::physical_memory() { return Aix::physical_memory(); } +size_t os::rss() { return (size_t)0; } + // Cpu architecture string #if defined(PPC32) static char cpu_arch[] = "ppc"; diff --git a/src/hotspot/os/bsd/os_bsd.cpp b/src/hotspot/os/bsd/os_bsd.cpp index e5b6c74ce2f..fe1e7098fd5 100644 --- a/src/hotspot/os/bsd/os_bsd.cpp +++ b/src/hotspot/os/bsd/os_bsd.cpp @@ -210,6 +210,22 @@ julong os::physical_memory() { return Bsd::physical_memory(); } +size_t os::rss() { + size_t rss = 0; +#ifdef __APPLE__ + mach_task_basic_info info; + mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT; + + kern_return_t ret = task_info(mach_task_self(), MACH_TASK_BASIC_INFO, + (task_info_t)&info, &count); + if (ret == KERN_SUCCESS) { + rss = info.resident_size; + } +#endif // __APPLE__ + + return rss; +} + // Cpu architecture string #if defined(ZERO) static char cpu_arch[] = ZERO_LIBARCH; diff --git a/src/hotspot/os/linux/os_linux.cpp b/src/hotspot/os/linux/os_linux.cpp index b56d4082354..52866a44b26 100644 --- a/src/hotspot/os/linux/os_linux.cpp +++ b/src/hotspot/os/linux/os_linux.cpp @@ -359,6 +359,15 @@ julong os::physical_memory() { return phys_mem; } +size_t os::rss() { + size_t size = 0; + os::Linux::meminfo_t info; + if (os::Linux::query_process_memory_info(&info)) { + size = info.vmrss * K; + } + return size; +} + static uint64_t initial_total_ticks = 0; static uint64_t initial_steal_ticks = 0; static bool has_initial_tick_info = false; diff --git a/src/hotspot/os/windows/os_windows.cpp b/src/hotspot/os/windows/os_windows.cpp index 773bda647e1..49f05fe94b3 100644 --- a/src/hotspot/os/windows/os_windows.cpp +++ b/src/hotspot/os/windows/os_windows.cpp @@ -858,6 +858,19 @@ julong os::physical_memory() { return win32::physical_memory(); } +size_t os::rss() { + size_t rss = 0; + PROCESS_MEMORY_COUNTERS_EX pmex; + ZeroMemory(&pmex, sizeof(PROCESS_MEMORY_COUNTERS_EX)); + pmex.cb = sizeof(pmex); + BOOL ret = GetProcessMemoryInfo( + GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS *)&pmex, sizeof(pmex)); + if (ret) { + rss = pmex.WorkingSetSize; + } + return rss; +} + bool os::has_allocatable_memory_limit(size_t* limit) { MEMORYSTATUSEX ms; ms.dwLength = sizeof(ms); diff --git a/src/hotspot/share/prims/whitebox.cpp b/src/hotspot/share/prims/whitebox.cpp index 821444ea389..5ed593b0d2f 100644 --- a/src/hotspot/share/prims/whitebox.cpp +++ b/src/hotspot/share/prims/whitebox.cpp @@ -2657,6 +2657,11 @@ WB_ENTRY(void, WB_CleanMetaspaces(JNIEnv* env, jobject target)) ClassLoaderDataGraph::safepoint_and_clean_metaspaces(); WB_END +// Reports resident set size (RSS) in bytes +WB_ENTRY(jlong, WB_Rss(JNIEnv* env, jobject o)) + return os::rss(); +WB_END + #define CC (char*) static JNINativeMethod methods[] = { @@ -2944,6 +2949,7 @@ static JNINativeMethod methods[] = { {CC"setVirtualThreadsNotifyJvmtiMode", CC"(Z)Z", (void*)&WB_SetVirtualThreadsNotifyJvmtiMode}, {CC"preTouchMemory", CC"(JJ)V", (void*)&WB_PreTouchMemory}, {CC"cleanMetaspaces", CC"()V", (void*)&WB_CleanMetaspaces}, + {CC"rss", CC"()J", (void*)&WB_Rss}, }; diff --git a/src/hotspot/share/runtime/os.hpp b/src/hotspot/share/runtime/os.hpp index a6626c1389f..ce7a07d4c43 100644 --- a/src/hotspot/share/runtime/os.hpp +++ b/src/hotspot/share/runtime/os.hpp @@ -344,6 +344,7 @@ class os: AllStatic { static julong physical_memory(); static bool has_allocatable_memory_limit(size_t* limit); static bool is_server_class_machine(); + static size_t rss(); // Returns the id of the processor on which the calling thread is currently executing. // The returned value is guaranteed to be between 0 and (os::processor_count() - 1). diff --git a/test/hotspot/jtreg/gc/TestAlwaysPreTouchBehavior.java b/test/hotspot/jtreg/gc/TestAlwaysPreTouchBehavior.java new file mode 100644 index 00000000000..c282c2876ea --- /dev/null +++ b/test/hotspot/jtreg/gc/TestAlwaysPreTouchBehavior.java @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2014, 2024, Alibaba Group Holding Limited. All rights reserved. + * Copyright (c) 2024, Red Hat Inc. + * 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. + */ + +package gc; + +/** + * @test id=ParallelCollector + * @summary tests AlwaysPreTouch + * @requires vm.gc.Parallel + * @requires os.maxMemory > 2G + * @requires os.family != "aix" + * @library /test/lib + * @build jdk.test.whitebox.WhiteBox + * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xmx512m -Xms512m -XX:+UseParallelGC -XX:+AlwaysPreTouch gc.TestAlwaysPreTouchBehavior + */ + + /** + * @test id=SerialCollector + * @summary tests AlwaysPreTouch + * @requires vm.gc.Serial + * @requires os.maxMemory > 2G + * @requires os.family != "aix" + * @library /test/lib + * @build jdk.test.whitebox.WhiteBox + * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xmx512m -Xms512m -XX:+UseSerialGC -XX:+AlwaysPreTouch gc.TestAlwaysPreTouchBehavior + */ + +/** + * @test id=Shenandoah + * @summary tests AlwaysPreTouch + * @requires vm.gc.Shenandoah + * @requires os.maxMemory > 2G + * @requires os.family != "aix" + * @library /test/lib + * @build jdk.test.whitebox.WhiteBox + * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xmx512m -Xms512m -XX:+UseShenandoahGC -XX:+AlwaysPreTouch gc.TestAlwaysPreTouchBehavior + */ + +/** + * @test id=G1 + * @summary tests AlwaysPreTouch + * @requires vm.gc.G1 + * @requires os.maxMemory > 2G + * @requires os.family != "aix" + * @library /test/lib + * @build jdk.test.whitebox.WhiteBox + * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xmx512m -Xms512m -XX:+AlwaysPreTouch gc.TestAlwaysPreTouchBehavior + */ + +/** + * @test id=ZGenerational + * @summary tests AlwaysPreTouch + * @requires vm.gc.ZGenerational + * @requires os.maxMemory > 2G + * @requires os.family != "aix" + * @library /test/lib + * @build jdk.test.whitebox.WhiteBox + * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:+UseZGC -XX:+ZGenerational -Xmx512m -Xms512m -XX:+AlwaysPreTouch gc.TestAlwaysPreTouchBehavior + */ + +/** + * @test id=ZSinglegen + * @summary tests AlwaysPreTouch + * @requires vm.gc.ZSinglegen + * @requires os.maxMemory > 2G + * @requires os.family != "aix" + * @library /test/lib + * @build jdk.test.whitebox.WhiteBox + * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:+UseZGC -XX:-ZGenerational -Xmx512m -Xms512m -XX:+AlwaysPreTouch gc.TestAlwaysPreTouchBehavior + */ + +/** + * @test id=Epsilon + * @summary tests AlwaysPreTouch + * @requires vm.gc.Epsilon + * @requires os.maxMemory > 2G + * @requires os.family != "aix" + * @library /test/lib + * @build jdk.test.whitebox.WhiteBox + * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC -Xmx512m -Xms512m -XX:+AlwaysPreTouch gc.TestAlwaysPreTouchBehavior + */ + + +import jdk.test.lib.Asserts; + +import jdk.test.whitebox.WhiteBox; + +public class TestAlwaysPreTouchBehavior { + + public static void main(String [] args) { + long rss = WhiteBox.getWhiteBox().rss(); + System.out.println("RSS: " + rss); + if (rss == 0) { + System.out.println("cannot get RSS, just skip"); + return; // Did not get available RSS, just ignore this test. + } + Runtime runtime = Runtime.getRuntime(); + long committedMemory = runtime.totalMemory(); + Asserts.assertGreaterThan(rss, committedMemory, "RSS of this process(" + rss + "b) should be bigger than or equal to committed heap mem(" + committedMemory + "b)"); + } +} + diff --git a/test/hotspot/jtreg/gc/parallel/TestAlwaysPreTouchBehavior.java b/test/hotspot/jtreg/gc/parallel/TestAlwaysPreTouchBehavior.java deleted file mode 100644 index 3a5b2b557cc..00000000000 --- a/test/hotspot/jtreg/gc/parallel/TestAlwaysPreTouchBehavior.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (c) 2014, 2024, Alibaba Group Holding Limited. 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. - */ - -package gc.parallel; - -/** - * @test TestAlwaysPreTouchBehavior - * @summary Tests AlwaysPreTouch Bahavior, pages of java heap should be pretouched with AlwaysPreTouch enabled. This test reads RSS of test process, which should be bigger than heap size(1g) with AlwaysPreTouch enabled. - * @requires vm.gc.Parallel - * @requires vm.debug != true - * @requires os.family == "linux" - * @requires os.maxMemory > 2G - * @library /test/lib - * @run main/othervm -Xmx1g -Xms1g -XX:+UseParallelGC -XX:+AlwaysPreTouch -XX:+UnlockDiagnosticVMOptions -XX:-UseMadvPopulateWrite gc.parallel.TestAlwaysPreTouchBehavior - */ -import java.lang.management.ManagementFactory; -import java.lang.management.ThreadInfo; -import java.lang.management.ThreadMXBean; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.io.IOException; -import java.util.*; -import javax.management.*; -import java.lang.management.*; -import jdk.test.lib.Utils; -import jdk.test.lib.Asserts; -import java.lang.management.*; -import java.util.stream.*; -import java.io.*; - -public class TestAlwaysPreTouchBehavior { - public static long getProcessRssInKb() throws IOException { - String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0]; - // Read RSS from /proc/$pid/status. Only available on Linux. - String processStatusFile = "/proc/" + pid + "/status"; - BufferedReader reader = new BufferedReader(new FileReader(processStatusFile)); - String line = null; - while ((line = reader.readLine()) != null) { - if (line.startsWith("VmRSS:")) { - break; - } - } - reader.close(); - return Long.valueOf(line.split("\\s+")[1].trim()); - } - public static void main(String [] args) { - long rss = 0; - Runtime runtime = Runtime.getRuntime(); - long committedMemory = runtime.totalMemory() / 1024; // in kb - try { - rss = getProcessRssInKb(); - } catch (Exception e) { - System.out.println("cannot get RSS, just skip"); - return; // Did not get avaiable RSS, just ignore this test - } - Asserts.assertGreaterThanOrEqual(rss, committedMemory, "RSS of this process(" + rss + "kb) should be bigger than or equal to committed heap mem(" + committedMemory + "kb)"); - } -} diff --git a/test/lib/jdk/test/whitebox/WhiteBox.java b/test/lib/jdk/test/whitebox/WhiteBox.java index 2402e39974e..3b930aec16f 100644 --- a/test/lib/jdk/test/whitebox/WhiteBox.java +++ b/test/lib/jdk/test/whitebox/WhiteBox.java @@ -798,4 +798,5 @@ public class WhiteBox { public native boolean setVirtualThreadsNotifyJvmtiMode(boolean enabled); public native void preTouchMemory(long addr, long size); + public native long rss(); }