8233494: Avoid calling MallocTracker::record_malloc and record_free when NMT is off
authorredestad
Tue, 05 Nov 2019 09:57:10 +0100
changeset 58927 d8aced36e872
parent 58926 ecb801342b8c
child 58928 707825662286
8233494: Avoid calling MallocTracker::record_malloc and record_free when NMT is off Reviewed-by: mdoerr, zgu
src/hotspot/share/runtime/os.cpp
src/hotspot/share/services/mallocTracker.cpp
src/hotspot/share/services/memTracker.cpp
src/hotspot/share/services/memTracker.hpp
--- a/src/hotspot/share/runtime/os.cpp	Tue Nov 05 09:37:20 2019 +0800
+++ b/src/hotspot/share/runtime/os.cpp	Tue Nov 05 09:57:10 2019 +0100
@@ -755,8 +755,8 @@
   NOT_PRODUCT(inc_stat_counter(&num_mallocs, 1));
   NOT_PRODUCT(inc_stat_counter(&alloc_bytes, size));
    // NMT support
-  void* membase = MemTracker::record_free(memblock);
   NMT_TrackingLevel level = MemTracker::tracking_level();
+  void* membase = MemTracker::record_free(memblock, level);
   size_t  nmt_header_size = MemTracker::malloc_header_size(level);
   void* ptr = ::realloc(membase, size + nmt_header_size);
   return MemTracker::record_malloc(ptr, size, memflags, stack, level);
@@ -797,7 +797,7 @@
     log_warning(malloc, free)("os::free caught " PTR_FORMAT, p2i(memblock));
     breakpoint();
   }
-  void* membase = MemTracker::record_free(memblock);
+  void* membase = MemTracker::record_free(memblock, MemTracker::tracking_level());
   verify_memory(membase);
 
   GuardedMemory guarded(membase);
@@ -806,7 +806,7 @@
   membase = guarded.release_for_freeing();
   ::free(membase);
 #else
-  void* membase = MemTracker::record_free(memblock);
+  void* membase = MemTracker::record_free(memblock, MemTracker::tracking_level());
   ::free(membase);
 #endif
 }
--- a/src/hotspot/share/services/mallocTracker.cpp	Tue Nov 05 09:37:20 2019 +0800
+++ b/src/hotspot/share/services/mallocTracker.cpp	Tue Nov 05 09:57:10 2019 +0100
@@ -119,6 +119,7 @@
 // Record a malloc memory allocation
 void* MallocTracker::record_malloc(void* malloc_base, size_t size, MEMFLAGS flags,
   const NativeCallStack& stack, NMT_TrackingLevel level) {
+  assert(level != NMT_off, "precondition");
   void*         memblock;      // the address for user data
   MallocHeader* header = NULL;
 
@@ -128,10 +129,6 @@
 
   // Uses placement global new operator to initialize malloc header
 
-  if (level == NMT_off) {
-    return malloc_base;
-  }
-
   header = ::new (malloc_base)MallocHeader(size, flags, stack, level);
   memblock = (void*)((char*)malloc_base + sizeof(MallocHeader));
 
@@ -151,14 +148,9 @@
 }
 
 void* MallocTracker::record_free(void* memblock) {
-  // Never turned on
-  if (MemTracker::tracking_level() == NMT_off ||
-      memblock == NULL) {
-    return memblock;
-  }
+  assert(MemTracker::tracking_level() != NMT_off && memblock != NULL, "precondition");
   MallocHeader* header = malloc_header(memblock);
   header->release();
-
   return (void*)header;
 }
 
--- a/src/hotspot/share/services/memTracker.cpp	Tue Nov 05 09:37:20 2019 +0800
+++ b/src/hotspot/share/services/memTracker.cpp	Tue Nov 05 09:57:10 2019 +0100
@@ -147,7 +147,7 @@
 // Shutdown can only be issued via JCmd, and NMT JCmd is serialized by lock
 void MemTracker::shutdown() {
   // We can only shutdown NMT to minimal tracking level if it is ever on.
-  if (tracking_level () > NMT_minimal) {
+  if (tracking_level() > NMT_minimal) {
     transition_to(NMT_minimal);
   }
 }
--- a/src/hotspot/share/services/memTracker.hpp	Tue Nov 05 09:37:20 2019 +0800
+++ b/src/hotspot/share/services/memTracker.hpp	Tue Nov 05 09:57:10 2019 +0100
@@ -157,7 +157,10 @@
 
   static inline void* record_malloc(void* mem_base, size_t size, MEMFLAGS flag,
     const NativeCallStack& stack, NMT_TrackingLevel level) {
-    return MallocTracker::record_malloc(mem_base, size, flag, stack, level);
+    if (level != NMT_off) {
+      return MallocTracker::record_malloc(mem_base, size, flag, stack, level);
+    }
+    return mem_base;
   }
 
   static inline size_t malloc_header_size(NMT_TrackingLevel level) {
@@ -177,7 +180,11 @@
   static void* malloc_base(void* memblock);
 
   // Record malloc free and return malloc base address
-  static inline void* record_free(void* memblock) {
+  static inline void* record_free(void* memblock, NMT_TrackingLevel level) {
+    // Never turned on
+    if (level == NMT_off || memblock == NULL) {
+      return memblock;
+    }
     return MallocTracker::record_free(memblock);
   }