8234270: [REDO] JDK-8204128 NMT might report incorrect numbers for Compiler area
authorzgu
Tue, 26 Nov 2019 09:27:16 -0500
changeset 59269 ac6f7738a0ee
parent 59268 611f6bfe7c30
child 59270 d5a1c6545a2b
8234270: [REDO] JDK-8204128 NMT might report incorrect numbers for Compiler area Reviewed-by: stuefe, minqi
src/hotspot/share/memory/arena.cpp
src/hotspot/share/memory/resourceArea.cpp
src/hotspot/share/prims/whitebox.cpp
src/hotspot/share/services/mallocTracker.hpp
src/hotspot/share/services/memTracker.hpp
test/hotspot/jtreg/runtime/NMT/HugeArenaTracking.java
test/lib/sun/hotspot/WhiteBox.java
--- a/src/hotspot/share/memory/arena.cpp	Tue Nov 26 13:50:26 2019 +0100
+++ b/src/hotspot/share/memory/arena.cpp	Tue Nov 26 09:27:16 2019 -0500
@@ -325,7 +325,7 @@
 // change the size
 void Arena::set_size_in_bytes(size_t size) {
   if (_size_in_bytes != size) {
-    long delta = (long)(size - size_in_bytes());
+    ssize_t delta = size - size_in_bytes();
     _size_in_bytes = size;
     MemTracker::record_arena_size_change(delta, _flags);
   }
--- a/src/hotspot/share/memory/resourceArea.cpp	Tue Nov 26 13:50:26 2019 +0100
+++ b/src/hotspot/share/memory/resourceArea.cpp	Tue Nov 26 09:27:16 2019 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2019, 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
@@ -31,8 +31,11 @@
 
 void ResourceArea::bias_to(MEMFLAGS new_flags) {
   if (new_flags != _flags) {
+    size_t size = size_in_bytes();
+    MemTracker::record_arena_size_change(-ssize_t(size), _flags);
     MemTracker::record_arena_free(_flags);
     MemTracker::record_new_arena(new_flags);
+    MemTracker::record_arena_size_change(ssize_t(size), new_flags);
     _flags = new_flags;
   }
 }
--- a/src/hotspot/share/prims/whitebox.cpp	Tue Nov 26 13:50:26 2019 +0100
+++ b/src/hotspot/share/prims/whitebox.cpp	Tue Nov 26 09:27:16 2019 -0500
@@ -797,6 +797,21 @@
   assert(hash_size > 0, "NMT hash_size should be > 0");
   return (jint)hash_size;
 WB_END
+
+WB_ENTRY(jlong, WB_NMTNewArena(JNIEnv* env, jobject o, jlong init_size))
+  Arena* arena =  new (mtTest) Arena(mtTest, size_t(init_size));
+  return (jlong)arena;
+WB_END
+
+WB_ENTRY(void, WB_NMTFreeArena(JNIEnv* env, jobject o, jlong arena))
+  Arena* a = (Arena*)arena;
+  delete a;
+WB_END
+
+WB_ENTRY(void, WB_NMTArenaMalloc(JNIEnv* env, jobject o, jlong arena, jlong size))
+  Arena* a = (Arena*)arena;
+  a->Amalloc(size_t(size));
+WB_END
 #endif // INCLUDE_NMT
 
 static jmethodID reflected_method_to_jmid(JavaThread* thread, JNIEnv* env, jobject method) {
@@ -2244,6 +2259,9 @@
   {CC"NMTReleaseMemory",    CC"(JJ)V",                (void*)&WB_NMTReleaseMemory   },
   {CC"NMTChangeTrackingLevel", CC"()Z",               (void*)&WB_NMTChangeTrackingLevel},
   {CC"NMTGetHashSize",      CC"()I",                  (void*)&WB_NMTGetHashSize     },
+  {CC"NMTNewArena",         CC"(J)J",                 (void*)&WB_NMTNewArena        },
+  {CC"NMTFreeArena",        CC"(J)V",                 (void*)&WB_NMTFreeArena       },
+  {CC"NMTArenaMalloc",      CC"(JJ)V",                (void*)&WB_NMTArenaMalloc     },
 #endif // INCLUDE_NMT
   {CC"deoptimizeFrames",   CC"(Z)I",                  (void*)&WB_DeoptimizeFrames  },
   {CC"deoptimizeAll",      CC"()V",                   (void*)&WB_DeoptimizeAll     },
--- a/src/hotspot/share/services/mallocTracker.hpp	Tue Nov 26 13:50:26 2019 +0100
+++ b/src/hotspot/share/services/mallocTracker.hpp	Tue Nov 26 09:27:16 2019 -0500
@@ -70,8 +70,9 @@
     }
   }
 
-  inline void resize(long sz) {
+  inline void resize(ssize_t sz) {
     if (sz != 0) {
+      assert(sz >= 0 || _size >= size_t(-sz), "Must be");
       Atomic::add(&_size, size_t(sz));
       DEBUG_ONLY(_peak_size = MAX2(_size, _peak_size);)
     }
@@ -113,7 +114,7 @@
     _arena.deallocate(0);
   }
 
-  inline void record_arena_size_change(long sz) {
+  inline void record_arena_size_change(ssize_t sz) {
     _arena.resize(sz);
   }
 
@@ -207,7 +208,7 @@
      as_snapshot()->by_type(flag)->record_arena_free();
    }
 
-   static inline void record_arena_size_change(long size, MEMFLAGS flag) {
+   static inline void record_arena_size_change(ssize_t size, MEMFLAGS flag) {
      as_snapshot()->by_type(flag)->record_arena_size_change(size);
    }
 
@@ -361,7 +362,7 @@
     MallocMemorySummary::record_arena_free(flags);
   }
 
-  static inline void record_arena_size_change(int size, MEMFLAGS flags) {
+  static inline void record_arena_size_change(ssize_t size, MEMFLAGS flags) {
     MallocMemorySummary::record_arena_size_change(size, flags);
   }
  private:
--- a/src/hotspot/share/services/memTracker.hpp	Tue Nov 26 13:50:26 2019 +0100
+++ b/src/hotspot/share/services/memTracker.hpp	Tue Nov 26 09:27:16 2019 -0500
@@ -63,7 +63,7 @@
 
   static inline void record_new_arena(MEMFLAGS flag) { }
   static inline void record_arena_free(MEMFLAGS flag) { }
-  static inline void record_arena_size_change(int diff, MEMFLAGS flag) { }
+  static inline void record_arena_size_change(ssize_t diff, MEMFLAGS flag) { }
   static inline void record_virtual_memory_reserve(void* addr, size_t size, const NativeCallStack& stack,
                        MEMFLAGS flag = mtNone) { }
   static inline void record_virtual_memory_reserve_and_commit(void* addr, size_t size,
@@ -203,7 +203,7 @@
 
   // Record arena size change. Arena size is the size of all arena
   // chuncks that backing up the arena.
-  static inline void record_arena_size_change(int diff, MEMFLAGS flag) {
+  static inline void record_arena_size_change(ssize_t diff, MEMFLAGS flag) {
     if (tracking_level() < NMT_summary) return;
     MallocTracker::record_arena_size_change(diff, flag);
   }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/runtime/NMT/HugeArenaTracking.java	Tue Nov 26 09:27:16 2019 -0500
@@ -0,0 +1,87 @@
+/*
+ * Copyright (c) 2019, Red Hat, Inc. All rights reserved.
+ *
+ * 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
+ * @key nmt jcmd
+ * @library /test/lib
+ * @modules java.base/jdk.internal.misc
+ *          java.management
+ * @build sun.hotspot.WhiteBox
+ * @run driver ClassFileInstaller sun.hotspot.WhiteBox
+ *                              sun.hotspot.WhiteBox$WhiteBoxPermission
+ * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:NativeMemoryTracking=detail HugeArenaTracking
+ */
+
+import java.util.Random;
+import jdk.test.lib.process.ProcessTools;
+import jdk.test.lib.process.OutputAnalyzer;
+import jdk.test.lib.JDKToolFinder;
+import sun.hotspot.WhiteBox;
+
+public class HugeArenaTracking {
+  private static final long GB = 1024 * 1024 * 1024;
+
+  public static void main(String args[]) throws Exception {
+    OutputAnalyzer output;
+    final WhiteBox wb = WhiteBox.getWhiteBox();
+
+    // Grab my own PID
+    String pid = Long.toString(ProcessTools.getProcessId());
+    ProcessBuilder pb = new ProcessBuilder();
+
+    long arena1 = wb.NMTNewArena(1024);
+    long arena2 = wb.NMTNewArena(1024);
+
+    // Run 'jcmd <pid> VM.native_memory summary'
+    pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.native_memory", "summary"});
+    output = new OutputAnalyzer(pb.start());
+    output.shouldContain("Test (reserved=2KB, committed=2KB)");
+
+    Random rand = new Random();
+
+    // Allocate 2GB+ from arena
+    long total = 0;
+    while (total < 2 * GB) {
+      // Cap to 10M
+      long inc = rand.nextInt(10 * 1024 * 1024);
+      wb.NMTArenaMalloc(arena1, inc);
+      total += inc;
+    }
+
+    ProcessBuilder pb2 = new ProcessBuilder();
+    // Run 'jcmd <pid> VM.native_memory summary'
+    pb2.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.native_memory", "summary", "scale=GB"});
+    output = new OutputAnalyzer(pb2.start());
+    output.shouldContain("Test (reserved=2GB, committed=2GB)");
+
+    wb.NMTFreeArena(arena1);
+
+    output = new OutputAnalyzer(pb.start());
+    output.shouldContain("Test (reserved=1KB, committed=1KB)");
+    wb.NMTFreeArena(arena2);
+
+    output = new OutputAnalyzer(pb.start());
+    output.shouldNotContain("Test (reserved");
+  }
+}
--- a/test/lib/sun/hotspot/WhiteBox.java	Tue Nov 26 13:50:26 2019 +0100
+++ b/test/lib/sun/hotspot/WhiteBox.java	Tue Nov 26 09:27:16 2019 -0500
@@ -222,6 +222,9 @@
   public native long NMTMallocWithPseudoStackAndType(long size, int index, int type);
   public native boolean NMTChangeTrackingLevel();
   public native int NMTGetHashSize();
+  public native long NMTNewArena(long initSize);
+  public native void NMTFreeArena(long arena);
+  public native void NMTArenaMalloc(long arena, long size);
 
   // Compiler
   public native int     matchesMethod(Executable method, String pattern);