8233494: Avoid calling MallocTracker::record_malloc and record_free when NMT is off

Reviewed-by: mdoerr, zgu
This commit is contained in:
Claes Redestad 2019-11-05 09:57:10 +01:00
parent 9f62f4586d
commit af729c390c
4 changed files with 15 additions and 16 deletions

View File

@ -755,8 +755,8 @@ void* os::realloc(void *memblock, size_t size, MEMFLAGS memflags, const NativeCa
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 @@ void os::free(void *memblock) {
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 @@ void os::free(void *memblock) {
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
}

View File

@ -119,6 +119,7 @@ bool MallocTracker::transition(NMT_TrackingLevel from, NMT_TrackingLevel to) {
// 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 @@ void* MallocTracker::record_malloc(void* malloc_base, size_t size, MEMFLAGS flag
// 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_malloc(void* malloc_base, size_t size, MEMFLAGS flag
}
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;
}

View File

@ -147,7 +147,7 @@ void Tracker::record(address addr, size_t size) {
// 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);
}
}

View File

@ -157,7 +157,10 @@ class MemTracker : AllStatic {
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 @@ class MemTracker : AllStatic {
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);
}