diff --git a/src/hotspot/os/linux/globals_linux.hpp b/src/hotspot/os/linux/globals_linux.hpp index 8539eab9e9703..1cb0b553c5273 100644 --- a/src/hotspot/os/linux/globals_linux.hpp +++ b/src/hotspot/os/linux/globals_linux.hpp @@ -94,7 +94,9 @@ product(bool, UseMadvPopulateWrite, true, DIAGNOSTIC, \ "Use MADV_POPULATE_WRITE in os::pd_pretouch_memory.") \ \ - + product(bool, PrintMemoryMapAtExit, false, DIAGNOSTIC, \ + "Print an annotated memory map at exit") \ + \ // end of RUNTIME_OS_FLAGS // diff --git a/src/hotspot/share/nmt/memMapPrinter.cpp b/src/hotspot/share/nmt/memMapPrinter.cpp index ec5003c562e55..5480904d57c40 100644 --- a/src/hotspot/share/nmt/memMapPrinter.cpp +++ b/src/hotspot/share/nmt/memMapPrinter.cpp @@ -30,16 +30,17 @@ #include "logging/logAsyncWriter.hpp" #include "gc/shared/collectedHeap.hpp" #include "memory/universe.hpp" +#include "memory/resourceArea.hpp" #include "nmt/memflags.hpp" +#include "nmt/memFlagBitmap.hpp" +#include "nmt/memMapPrinter.hpp" +#include "nmt/memTracker.hpp" +#include "nmt/virtualMemoryTracker.hpp" #include "runtime/nonJavaThread.hpp" #include "runtime/osThread.hpp" #include "runtime/thread.hpp" #include "runtime/threadSMR.hpp" #include "runtime/vmThread.hpp" -#include "nmt/memFlagBitmap.hpp" -#include "nmt/memMapPrinter.hpp" -#include "nmt/memTracker.hpp" -#include "nmt/virtualMemoryTracker.hpp" #include "utilities/globalDefinitions.hpp" #include "utilities/growableArray.hpp" #include "utilities/ostream.hpp" @@ -203,6 +204,8 @@ static void print_thread_details(uintx thread_id, const char* name, outputStream // Given a region [from, to), if it intersects a known thread stack, print detail infos about that thread. static void print_thread_details_for_supposed_stack_address(const void* from, const void* to, outputStream* st) { + ResourceMark rm; + #define HANDLE_THREAD(T) \ if (T != nullptr && vma_touches_thread_stack(from, to, T)) { \ print_thread_details((uintx)(T->osthread()->thread_id()), T->name(), st); \ diff --git a/src/hotspot/share/runtime/java.cpp b/src/hotspot/share/runtime/java.cpp index d3e76364a03e0..674716a859852 100644 --- a/src/hotspot/share/runtime/java.cpp +++ b/src/hotspot/share/runtime/java.cpp @@ -48,6 +48,7 @@ #include "memory/oopFactory.hpp" #include "memory/resourceArea.hpp" #include "memory/universe.hpp" +#include "nmt/memMapPrinter.hpp" #include "nmt/memTracker.hpp" #include "oops/constantPool.hpp" #include "oops/generateOopMap.hpp" @@ -485,6 +486,9 @@ void before_exit(JavaThread* thread, bool halt) { if (DumpPerfMapAtExit) { CodeCache::write_perf_map(); } + if (PrintMemoryMapAtExit) { + MemMapPrinter::print_all_mappings(tty, false); + } #endif if (JvmtiExport::should_post_thread_life()) { diff --git a/test/hotspot/jtreg/runtime/NMT/PrintMemoryMapAtExitTest.java b/test/hotspot/jtreg/runtime/NMT/PrintMemoryMapAtExitTest.java new file mode 100644 index 0000000000000..2c51c079c737c --- /dev/null +++ b/test/hotspot/jtreg/runtime/NMT/PrintMemoryMapAtExitTest.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2024, 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. + */ + +/* + * @test + * @summary Verify PrintMemoryMapAtExit on normal JVM exit for summary tracking level + * @requires os.family == "linux" + * @modules java.base/jdk.internal.misc + * @library /test/lib + * @run driver PrintMemoryMapAtExitTest + */ + +import jdk.test.lib.process.OutputAnalyzer; +import jdk.test.lib.process.ProcessTools; + +public class PrintMemoryMapAtExitTest { + + public static void main(String args[]) throws Exception { + + ProcessBuilder pb = ProcessTools.createTestJavaProcessBuilder( + "-XX:+UnlockDiagnosticVMOptions", "-XX:+PrintMemoryMapAtExit", + "-XX:NativeMemoryTracking=summary", "-version"); + + OutputAnalyzer output = new OutputAnalyzer(pb.start()); + output.shouldHaveExitValue(0); + output.shouldContain("Memory mappings"); + output.shouldContain("JAVAHEAP"); + output.shouldHaveExitValue(0); + } +}