8198225: os::attempt_reserve_memory_at records memory as committed
authorstefank
Wed, 21 Feb 2018 12:40:07 +0100
changeset 49034 57d0c33ad636
parent 49033 3acc342c0738
child 49035 c5381ad58ed3
8198225: os::attempt_reserve_memory_at records memory as committed Reviewed-by: shade, zgu, stuefe
src/hotspot/share/prims/whitebox.cpp
src/hotspot/share/runtime/os.cpp
test/hotspot/jtreg/runtime/NMT/VirtualAllocAttemptReserveMemoryAt.java
test/lib/sun/hotspot/WhiteBox.java
--- a/src/hotspot/share/prims/whitebox.cpp	Wed Feb 21 12:40:05 2018 +0100
+++ b/src/hotspot/share/prims/whitebox.cpp	Wed Feb 21 12:40:07 2018 +0100
@@ -602,6 +602,13 @@
   return addr;
 WB_END
 
+WB_ENTRY(jlong, WB_NMTAttemptReserveMemoryAt(JNIEnv* env, jobject o, jlong addr, jlong size))
+  addr = (jlong)(uintptr_t)os::attempt_reserve_memory_at((size_t)size, (char*)(uintptr_t)addr);
+  MemTracker::record_virtual_memory_type((address)addr, mtTest);
+
+  return addr;
+WB_END
+
 WB_ENTRY(void, WB_NMTCommitMemory(JNIEnv* env, jobject o, jlong addr, jlong size))
   os::commit_memory((char *)(uintptr_t)addr, size, !ExecMem);
   MemTracker::record_virtual_memory_type((address)(uintptr_t)addr, mtTest);
@@ -1974,6 +1981,7 @@
   {CC"NMTMallocWithPseudoStack", CC"(JI)J",           (void*)&WB_NMTMallocWithPseudoStack},
   {CC"NMTFree",             CC"(J)V",                 (void*)&WB_NMTFree            },
   {CC"NMTReserveMemory",    CC"(J)J",                 (void*)&WB_NMTReserveMemory   },
+  {CC"NMTAttemptReserveMemoryAt",    CC"(JJ)J",       (void*)&WB_NMTAttemptReserveMemoryAt },
   {CC"NMTCommitMemory",     CC"(JJ)V",                (void*)&WB_NMTCommitMemory    },
   {CC"NMTUncommitMemory",   CC"(JJ)V",                (void*)&WB_NMTUncommitMemory  },
   {CC"NMTReleaseMemory",    CC"(JJ)V",                (void*)&WB_NMTReleaseMemory   },
--- a/src/hotspot/share/runtime/os.cpp	Wed Feb 21 12:40:05 2018 +0100
+++ b/src/hotspot/share/runtime/os.cpp	Wed Feb 21 12:40:07 2018 +0100
@@ -1706,7 +1706,7 @@
   } else {
     result = pd_attempt_reserve_memory_at(bytes, addr);
     if (result != NULL) {
-      MemTracker::record_virtual_memory_reserve_and_commit((address)result, bytes, CALLER_PC);
+      MemTracker::record_virtual_memory_reserve((address)result, bytes, CALLER_PC);
     }
   }
   return result;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/runtime/NMT/VirtualAllocAttemptReserveMemoryAt.java	Wed Feb 21 12:40:07 2018 +0100
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2018, 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
+ * 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 Test that os::attempt_reserve_memory_at doesn't register the memory as committed
+ * @key nmt jcmd
+ * @library /test/lib
+ * @modules java.base/jdk.internal.misc
+ *          java.management
+ * @build sun.hotspot.WhiteBox
+ * @run main ClassFileInstaller sun.hotspot.WhiteBox
+ * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:NativeMemoryTracking=detail VirtualAllocAttemptReserveMemoryAt
+ *
+ */
+
+import jdk.test.lib.process.ProcessTools;
+import jdk.test.lib.process.OutputAnalyzer;
+import jdk.test.lib.JDKToolFinder;
+
+import sun.hotspot.WhiteBox;
+
+import static jdk.test.lib.Asserts.*;
+
+public class VirtualAllocAttemptReserveMemoryAt {
+
+    public static WhiteBox wb = WhiteBox.getWhiteBox();
+
+    public static void main(String args[]) throws Exception {
+        long reserveSize = 4 * 1024 * 1024; // 4096KB
+
+        String pid = Long.toString(ProcessTools.getProcessId());
+        ProcessBuilder pb = new ProcessBuilder();
+
+        // Find an address
+        long addr = wb.NMTReserveMemory(reserveSize);
+
+        // Release it
+        wb.NMTReleaseMemory(addr, reserveSize);
+
+        long attempt_addr = wb.NMTAttemptReserveMemoryAt(addr, reserveSize);
+
+        if (attempt_addr == 0) {
+            // We didn't manage ot get the requested memory address.
+            // It's not necessarily a bug, so giving up.
+            return;
+        }
+
+        assertEQ(addr, attempt_addr);
+
+        pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid,
+                "VM.native_memory", "detail" });
+
+        OutputAnalyzer output = new OutputAnalyzer(pb.start());
+
+        output.shouldContain("Test (reserved=4096KB, committed=0KB)");
+
+        wb.NMTReleaseMemory(addr, reserveSize);
+        output = new OutputAnalyzer(pb.start());
+        output.shouldNotContain("Test (reserved=");
+        output.shouldNotMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
+                + Long.toHexString(addr + reserveSize) + "\\] reserved 4096KB for Test");
+    }
+}
--- a/test/lib/sun/hotspot/WhiteBox.java	Wed Feb 21 12:40:05 2018 +0100
+++ b/test/lib/sun/hotspot/WhiteBox.java	Wed Feb 21 12:40:07 2018 +0100
@@ -206,6 +206,7 @@
   public native long NMTMalloc(long size);
   public native void NMTFree(long mem);
   public native long NMTReserveMemory(long size);
+  public native long NMTAttemptReserveMemoryAt(long addr, long size);
   public native void NMTCommitMemory(long addr, long size);
   public native void NMTUncommitMemory(long addr, long size);
   public native void NMTReleaseMemory(long addr, long size);