Skip to content

Commit b96b743

Browse files
committedFeb 25, 2022
8281015: Further simplify NMT backend
Reviewed-by: zgu, mbaesken
1 parent 9471f24 commit b96b743

10 files changed

+126
-217
lines changed
 

‎src/hotspot/share/runtime/os.cpp

+11-18
Original file line numberDiff line numberDiff line change
@@ -649,18 +649,14 @@ void* os::malloc(size_t size, MEMFLAGS memflags, const NativeCallStack& stack) {
649649
return NULL;
650650
}
651651

652-
const NMT_TrackingLevel level = MemTracker::tracking_level();
653-
const size_t nmt_overhead =
654-
MemTracker::malloc_header_size(level) + MemTracker::malloc_footer_size(level);
652+
const size_t outer_size = size + MemTracker::overhead_per_malloc();
655653

656-
const size_t outer_size = size + nmt_overhead;
657-
658-
void* const outer_ptr = (u_char*)::malloc(outer_size);
654+
void* const outer_ptr = ::malloc(outer_size);
659655
if (outer_ptr == NULL) {
660656
return NULL;
661657
}
662658

663-
void* inner_ptr = MemTracker::record_malloc((address)outer_ptr, size, memflags, stack, level);
659+
void* const inner_ptr = MemTracker::record_malloc((address)outer_ptr, size, memflags, stack);
664660

665661
DEBUG_ONLY(::memset(inner_ptr, uninitBlockPad, size);)
666662
DEBUG_ONLY(break_if_ptr_caught(inner_ptr);)
@@ -696,19 +692,17 @@ void* os::realloc(void *memblock, size_t size, MEMFLAGS memflags, const NativeCa
696692
return NULL;
697693
}
698694

699-
const NMT_TrackingLevel level = MemTracker::tracking_level();
700-
const size_t nmt_overhead =
701-
MemTracker::malloc_header_size(level) + MemTracker::malloc_footer_size(level);
702-
703-
const size_t new_outer_size = size + nmt_overhead;
695+
const size_t new_outer_size = size + MemTracker::overhead_per_malloc();
704696

705697
// If NMT is enabled, this checks for heap overwrites, then de-accounts the old block.
706-
void* const old_outer_ptr = MemTracker::record_free(memblock, level);
698+
void* const old_outer_ptr = MemTracker::record_free(memblock);
707699

708700
void* const new_outer_ptr = ::realloc(old_outer_ptr, new_outer_size);
701+
if (new_outer_ptr == NULL) {
702+
return NULL;
703+
}
709704

710-
// If NMT is enabled, this checks for heap overwrites, then de-accounts the old block.
711-
void* const new_inner_ptr = MemTracker::record_malloc(new_outer_ptr, size, memflags, stack, level);
705+
void* const new_inner_ptr = MemTracker::record_malloc(new_outer_ptr, size, memflags, stack);
712706

713707
DEBUG_ONLY(break_if_ptr_caught(new_inner_ptr);)
714708

@@ -728,10 +722,9 @@ void os::free(void *memblock) {
728722

729723
DEBUG_ONLY(break_if_ptr_caught(memblock);)
730724

731-
const NMT_TrackingLevel level = MemTracker::tracking_level();
732-
733725
// If NMT is enabled, this checks for heap overwrites, then de-accounts the old block.
734-
void* const old_outer_ptr = MemTracker::record_free(memblock, level);
726+
void* const old_outer_ptr = MemTracker::record_free(memblock);
727+
735728
::free(old_outer_ptr);
736729
}
737730

‎src/hotspot/share/services/mallocSiteTable.cpp

+13-9
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,11 @@ bool MallocSiteTable::walk(MallocSiteWalker* walker) {
106106
* 2. Overflow hash bucket.
107107
* Under any of above circumstances, caller should handle the situation.
108108
*/
109-
MallocSite* MallocSiteTable::lookup_or_add(const NativeCallStack& key, size_t* bucket_idx,
110-
size_t* pos_idx, MEMFLAGS flags) {
109+
MallocSite* MallocSiteTable::lookup_or_add(const NativeCallStack& key, uint32_t* marker, MEMFLAGS flags) {
111110
assert(flags != mtNone, "Should have a real memory type");
112111
const unsigned int hash = key.calculate_hash();
113112
const unsigned int index = hash_to_index(hash);
114-
*bucket_idx = (size_t)index;
115-
*pos_idx = 0;
113+
*marker = 0;
116114

117115
// First entry for this hash bucket
118116
if (_table[index] == NULL) {
@@ -122,41 +120,47 @@ MallocSite* MallocSiteTable::lookup_or_add(const NativeCallStack& key, size_t* b
122120

123121
// swap in the head
124122
if (Atomic::replace_if_null(&_table[index], entry)) {
123+
*marker = build_marker(index, 0);
125124
return entry->data();
126125
}
127126

128127
delete entry;
129128
}
130129

130+
unsigned pos_idx = 0;
131131
MallocSiteHashtableEntry* head = _table[index];
132-
while (head != NULL && (*pos_idx) <= MAX_BUCKET_LENGTH) {
132+
while (head != NULL && pos_idx < MAX_BUCKET_LENGTH) {
133133
if (head->hash() == hash) {
134134
MallocSite* site = head->data();
135135
if (site->flag() == flags && site->equals(key)) {
136+
*marker = build_marker(index, pos_idx);
136137
return head->data();
137138
}
138139
}
139140

140-
if (head->next() == NULL && (*pos_idx) < MAX_BUCKET_LENGTH) {
141+
if (head->next() == NULL && pos_idx < (MAX_BUCKET_LENGTH - 1)) {
141142
MallocSiteHashtableEntry* entry = new_entry(key, flags);
142143
// OOM check
143144
if (entry == NULL) return NULL;
144145
if (head->atomic_insert(entry)) {
145-
(*pos_idx) ++;
146+
pos_idx ++;
147+
*marker = build_marker(index, pos_idx);
146148
return entry->data();
147149
}
148150
// contended, other thread won
149151
delete entry;
150152
}
151153
head = (MallocSiteHashtableEntry*)head->next();
152-
(*pos_idx) ++;
154+
pos_idx ++;
153155
}
154156
return NULL;
155157
}
156158

157159
// Access malloc site
158-
MallocSite* MallocSiteTable::malloc_site(size_t bucket_idx, size_t pos_idx) {
160+
MallocSite* MallocSiteTable::malloc_site(uint32_t marker) {
161+
uint16_t bucket_idx = bucket_idx_from_marker(marker);
159162
assert(bucket_idx < table_size, "Invalid bucket index");
163+
const uint16_t pos_idx = pos_idx_from_marker(marker);
160164
MallocSiteHashtableEntry* head = _table[bucket_idx];
161165
for (size_t index = 0;
162166
index < pos_idx && head != NULL;

‎src/hotspot/share/services/mallocSiteTable.hpp

+24-15
Original file line numberDiff line numberDiff line change
@@ -111,21 +111,31 @@ class MallocSiteTable : AllStatic {
111111
table_size = (table_base_size * NMT_TrackingStackDepth - 1)
112112
};
113113

114-
// The table must not be wider than the maximum value the bucket_idx field
115-
// in the malloc header can hold.
114+
// Table cannot be wider than a 16bit bucket idx can hold
115+
#define MAX_MALLOCSITE_TABLE_SIZE (USHRT_MAX - 1)
116+
// Each bucket chain cannot be longer than what a 16 bit pos idx can hold (hopefully way shorter)
117+
#define MAX_BUCKET_LENGTH (USHRT_MAX - 1)
118+
116119
STATIC_ASSERT(table_size <= MAX_MALLOCSITE_TABLE_SIZE);
117120

121+
static uint32_t build_marker(unsigned bucket_idx, unsigned pos_idx) {
122+
assert(bucket_idx <= MAX_MALLOCSITE_TABLE_SIZE && pos_idx < MAX_BUCKET_LENGTH, "overflow");
123+
return (uint32_t)bucket_idx << 16 | pos_idx;
124+
}
125+
static uint16_t bucket_idx_from_marker(uint32_t marker) { return marker >> 16; }
126+
static uint16_t pos_idx_from_marker(uint32_t marker) { return marker & 0xFFFF; }
127+
118128
public:
129+
119130
static bool initialize();
120131

121132
// Number of hash buckets
122133
static inline int hash_buckets() { return (int)table_size; }
123134

124135
// Access and copy a call stack from this table. Shared lock should be
125136
// acquired before access the entry.
126-
static inline bool access_stack(NativeCallStack& stack, size_t bucket_idx,
127-
size_t pos_idx) {
128-
MallocSite* site = malloc_site(bucket_idx, pos_idx);
137+
static inline bool access_stack(NativeCallStack& stack, uint32_t marker) {
138+
MallocSite* site = malloc_site(marker);
129139
if (site != NULL) {
130140
stack = *site->call_stack();
131141
return true;
@@ -134,23 +144,22 @@ class MallocSiteTable : AllStatic {
134144
}
135145

136146
// Record a new allocation from specified call path.
137-
// Return true if the allocation is recorded successfully, bucket_idx
138-
// and pos_idx are also updated to indicate the entry where the allocation
139-
// information was recorded.
147+
// Return true if the allocation is recorded successfully and updates marker
148+
// to indicate the entry where the allocation information was recorded.
140149
// Return false only occurs under rare scenarios:
141150
// 1. out of memory
142151
// 2. overflow hash bucket
143152
static inline bool allocation_at(const NativeCallStack& stack, size_t size,
144-
size_t* bucket_idx, size_t* pos_idx, MEMFLAGS flags) {
145-
MallocSite* site = lookup_or_add(stack, bucket_idx, pos_idx, flags);
153+
uint32_t* marker, MEMFLAGS flags) {
154+
MallocSite* site = lookup_or_add(stack, marker, flags);
146155
if (site != NULL) site->allocate(size);
147156
return site != NULL;
148157
}
149158

150-
// Record memory deallocation. bucket_idx and pos_idx indicate where the allocation
159+
// Record memory deallocation. marker indicates where the allocation
151160
// information was recorded.
152-
static inline bool deallocation_at(size_t size, size_t bucket_idx, size_t pos_idx) {
153-
MallocSite* site = malloc_site(bucket_idx, pos_idx);
161+
static inline bool deallocation_at(size_t size, uint32_t marker) {
162+
MallocSite* site = malloc_site(marker);
154163
if (site != NULL) {
155164
site->deallocate(size);
156165
return true;
@@ -170,8 +179,8 @@ class MallocSiteTable : AllStatic {
170179
// Delete a bucket linked list
171180
static void delete_linked_list(MallocSiteHashtableEntry* head);
172181

173-
static MallocSite* lookup_or_add(const NativeCallStack& key, size_t* bucket_idx, size_t* pos_idx, MEMFLAGS flags);
174-
static MallocSite* malloc_site(size_t bucket_idx, size_t pos_idx);
182+
static MallocSite* lookup_or_add(const NativeCallStack& key, uint32_t* marker, MEMFLAGS flags);
183+
static MallocSite* malloc_site(uint32_t marker);
175184
static bool walk(MallocSiteWalker* walker);
176185

177186
static inline unsigned int hash_to_index(unsigned int hash) {

‎src/hotspot/share/services/mallocTracker.cpp

+34-39
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include "runtime/safefetch.inline.hpp"
2828
#include "services/mallocSiteTable.hpp"
2929
#include "services/mallocTracker.hpp"
30-
#include "services/mallocTracker.inline.hpp"
3130
#include "services/memTracker.hpp"
3231
#include "utilities/debug.hpp"
3332
#include "utilities/ostream.hpp"
@@ -115,20 +114,6 @@ void MallocHeader::mark_block_as_dead() {
115114
set_footer(_footer_canary_dead_mark);
116115
}
117116

118-
void MallocHeader::release() {
119-
assert(MemTracker::enabled(), "Sanity");
120-
121-
assert_block_integrity();
122-
123-
MallocMemorySummary::record_free(size(), flags());
124-
MallocMemorySummary::record_free_malloc_header(sizeof(MallocHeader));
125-
if (MemTracker::tracking_level() == NMT_detail) {
126-
MallocSiteTable::deallocation_at(size(), _bucket_idx, _pos_idx);
127-
}
128-
129-
mark_block_as_dead();
130-
}
131-
132117
void MallocHeader::print_block_on_error(outputStream* st, address bad_address) const {
133118
assert(bad_address >= (address)this, "sanity");
134119

@@ -233,13 +218,8 @@ bool MallocHeader::check_block_integrity(char* msg, size_t msglen, address* p_co
233218
return true;
234219
}
235220

236-
bool MallocHeader::record_malloc_site(const NativeCallStack& stack, size_t size,
237-
size_t* bucket_idx, size_t* pos_idx, MEMFLAGS flags) const {
238-
return MallocSiteTable::allocation_at(stack, size, bucket_idx, pos_idx, flags);
239-
}
240-
241221
bool MallocHeader::get_stack(NativeCallStack& stack) const {
242-
return MallocSiteTable::access_stack(stack, _bucket_idx, _pos_idx);
222+
return MallocSiteTable::access_stack(stack, _mst_marker);
243223
}
244224

245225
bool MallocTracker::initialize(NMT_TrackingLevel level) {
@@ -255,39 +235,54 @@ bool MallocTracker::initialize(NMT_TrackingLevel level) {
255235

256236
// Record a malloc memory allocation
257237
void* MallocTracker::record_malloc(void* malloc_base, size_t size, MEMFLAGS flags,
258-
const NativeCallStack& stack, NMT_TrackingLevel level) {
259-
assert(level != NMT_off, "precondition");
260-
void* memblock; // the address for user data
261-
MallocHeader* header = NULL;
262-
263-
if (malloc_base == NULL) {
264-
return NULL;
238+
const NativeCallStack& stack)
239+
{
240+
assert(MemTracker::enabled(), "precondition");
241+
assert(malloc_base != NULL, "precondition");
242+
243+
MallocMemorySummary::record_malloc(size, flags);
244+
MallocMemorySummary::record_new_malloc_header(sizeof(MallocHeader));
245+
uint32_t mst_marker = 0;
246+
if (MemTracker::tracking_level() == NMT_detail) {
247+
MallocSiteTable::allocation_at(stack, size, &mst_marker, flags);
265248
}
266249

267250
// Uses placement global new operator to initialize malloc header
268-
269-
header = ::new (malloc_base)MallocHeader(size, flags, stack, level);
270-
memblock = (void*)((char*)malloc_base + sizeof(MallocHeader));
251+
MallocHeader* const header = ::new (malloc_base)MallocHeader(size, flags, stack, mst_marker);
252+
void* const memblock = (void*)((char*)malloc_base + sizeof(MallocHeader));
271253

272254
// The alignment check: 8 bytes alignment for 32 bit systems.
273255
// 16 bytes alignment for 64-bit systems.
274256
assert(((size_t)memblock & (sizeof(size_t) * 2 - 1)) == 0, "Alignment check");
275257

276258
#ifdef ASSERT
277-
if (level > NMT_off) {
278-
// Read back
279-
assert(get_size(memblock) == size, "Wrong size");
280-
assert(get_flags(memblock) == flags, "Wrong flags");
259+
// Read back
260+
{
261+
MallocHeader* const header2 = malloc_header(memblock);
262+
assert(header2->size() == size, "Wrong size");
263+
assert(header2->flags() == flags, "Wrong flags");
264+
header2->assert_block_integrity();
281265
}
282266
#endif
283267

284268
return memblock;
285269
}
286270

287271
void* MallocTracker::record_free(void* memblock) {
288-
assert(MemTracker::tracking_level() != NMT_off && memblock != NULL, "precondition");
289-
MallocHeader* header = malloc_header(memblock);
290-
header->release();
272+
assert(MemTracker::enabled(), "Sanity");
273+
assert(memblock != NULL, "precondition");
274+
275+
MallocHeader* const header = malloc_header(memblock);
276+
header->assert_block_integrity();
277+
278+
MallocMemorySummary::record_free(header->size(), header->flags());
279+
MallocMemorySummary::record_free_malloc_header(sizeof(MallocHeader));
280+
if (MemTracker::tracking_level() == NMT_detail) {
281+
MallocSiteTable::deallocation_at(header->size(), header->mst_marker());
282+
}
283+
284+
header->mark_block_as_dead();
285+
291286
return (void*)header;
292287
}
293288

@@ -300,7 +295,7 @@ bool MallocTracker::print_pointer_information(const void* p, outputStream* st) {
300295
assert(MemTracker::enabled(), "NMT must be enabled");
301296
if (CanUseSafeFetch32() && os::is_readable_pointer(p)) {
302297
const NMT_TrackingLevel tracking_level = MemTracker::tracking_level();
303-
const MallocHeader* mhdr = (const MallocHeader*)MallocTracker::get_base(const_cast<void*>(p), tracking_level);
298+
const MallocHeader* mhdr = malloc_header(p);
304299
char msg[256];
305300
address p_corrupted;
306301
if (os::is_readable_pointer(mhdr) &&

‎src/hotspot/share/services/mallocTracker.hpp

+24-73
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ class MallocMemorySummary : AllStatic {
267267
*
268268
* 8 9 10 11 12 13 14 15 16 ++
269269
* +--------+--------+--------+--------+--------+--------+--------+--------+ ------------------------
270-
* ... | bucket idx | pos idx | flags | unused | canary | ... User payload ....
270+
* ... | malloc site table marker | flags | unused | canary | ... User payload ....
271271
* +--------+--------+--------+--------+--------+--------+--------+--------+ ------------------------
272272
*
273273
* Layout on 32-bit:
@@ -279,7 +279,7 @@ class MallocMemorySummary : AllStatic {
279279
*
280280
* 8 9 10 11 12 13 14 15 16 ++
281281
* +--------+--------+--------+--------+--------+--------+--------+--------+ ------------------------
282-
* ... | bucket idx | pos idx | flags | unused | canary | ... User payload ....
282+
* ... | malloc site table marker | flags | unused | canary | ... User payload ....
283283
* +--------+--------+--------+--------+--------+--------+--------+--------+ ------------------------
284284
*
285285
* Notes:
@@ -294,16 +294,12 @@ class MallocMemorySummary : AllStatic {
294294
class MallocHeader {
295295

296296
NOT_LP64(uint32_t _alt_canary);
297-
size_t _size;
298-
uint16_t _bucket_idx;
299-
uint16_t _pos_idx;
300-
uint8_t _flags;
301-
uint8_t _unused;
297+
const size_t _size;
298+
const uint32_t _mst_marker;
299+
const uint8_t _flags;
300+
const uint8_t _unused;
302301
uint16_t _canary;
303302

304-
#define MAX_MALLOCSITE_TABLE_SIZE (USHRT_MAX - 1)
305-
#define MAX_BUCKET_LENGTH (USHRT_MAX - 1)
306-
307303
static const uint16_t _header_canary_life_mark = 0xE99E;
308304
static const uint16_t _header_canary_dead_mark = 0xD99D;
309305
static const uint16_t _footer_canary_life_mark = 0xE88E;
@@ -314,11 +310,7 @@ class MallocHeader {
314310
// We discount sizes larger than these
315311
static const size_t max_reasonable_malloc_size = LP64_ONLY(256 * G) NOT_LP64(3500 * M);
316312

317-
// If block is broken, print out a report to tty (optionally with
318-
// hex dump surrounding the broken block), then trigger a fatal error
319-
void assert_block_integrity() const;
320313
void print_block_on_error(outputStream* st, address bad_address) const;
321-
void mark_block_as_dead();
322314

323315
static uint16_t build_footer(uint8_t b1, uint8_t b2) { return ((uint16_t)b1 << 8) | (uint16_t)b2; }
324316

@@ -328,51 +320,32 @@ class MallocHeader {
328320

329321
public:
330322

331-
MallocHeader(size_t size, MEMFLAGS flags, const NativeCallStack& stack, NMT_TrackingLevel level) {
323+
MallocHeader(size_t size, MEMFLAGS flags, const NativeCallStack& stack, uint32_t mst_marker)
324+
: _size(size), _mst_marker(mst_marker), _flags(NMTUtil::flag_to_index(flags)),
325+
_unused(0), _canary(_header_canary_life_mark)
326+
{
332327
assert(size < max_reasonable_malloc_size, "Too large allocation size?");
333-
334-
_flags = NMTUtil::flag_to_index(flags);
335-
set_size(size);
336-
if (level == NMT_detail) {
337-
size_t bucket_idx;
338-
size_t pos_idx;
339-
if (record_malloc_site(stack, size, &bucket_idx, &pos_idx, flags)) {
340-
assert(bucket_idx <= MAX_MALLOCSITE_TABLE_SIZE, "Overflow bucket index");
341-
assert(pos_idx <= MAX_BUCKET_LENGTH, "Overflow bucket position index");
342-
_bucket_idx = (uint16_t)bucket_idx;
343-
_pos_idx = (uint16_t)pos_idx;
344-
}
345-
}
346-
347-
_unused = 0;
348-
_canary = _header_canary_life_mark;
349328
// On 32-bit we have some bits more, use them for a second canary
350329
// guarding the start of the header.
351330
NOT_LP64(_alt_canary = _header_alt_canary_life_mark;)
352331
set_footer(_footer_canary_life_mark); // set after initializing _size
353-
354-
MallocMemorySummary::record_malloc(size, flags);
355-
MallocMemorySummary::record_new_malloc_header(sizeof(MallocHeader));
356332
}
357333

358334
inline size_t size() const { return _size; }
359335
inline MEMFLAGS flags() const { return (MEMFLAGS)_flags; }
336+
inline uint32_t mst_marker() const { return _mst_marker; }
360337
bool get_stack(NativeCallStack& stack) const;
361338

362-
// Cleanup tracking information and mark block as dead before the memory is released.
363-
void release();
339+
void mark_block_as_dead();
364340

365341
// If block is broken, fill in a short descriptive text in out,
366342
// an option pointer to the corruption in p_corruption, and return false.
367343
// Return true if block is fine.
368344
bool check_block_integrity(char* msg, size_t msglen, address* p_corruption) const;
369345

370-
private:
371-
inline void set_size(size_t size) {
372-
_size = size;
373-
}
374-
bool record_malloc_site(const NativeCallStack& stack, size_t size,
375-
size_t* bucket_idx, size_t* pos_idx, MEMFLAGS flags) const;
346+
// If block is broken, print out a report to tty (optionally with
347+
// hex dump surrounding the broken block), then trigger a fatal error
348+
void assert_block_integrity() const;
376349
};
377350

378351
// This needs to be true on both 64-bit and 32-bit platforms
@@ -385,15 +358,9 @@ class MallocTracker : AllStatic {
385358
// Initialize malloc tracker for specific tracking level
386359
static bool initialize(NMT_TrackingLevel level);
387360

388-
// malloc tracking header size for specific tracking level
389-
static inline size_t malloc_header_size(NMT_TrackingLevel level) {
390-
return (level == NMT_off) ? 0 : sizeof(MallocHeader);
391-
}
392-
393-
// malloc tracking footer size for specific tracking level
394-
static inline size_t malloc_footer_size(NMT_TrackingLevel level) {
395-
return (level == NMT_off) ? 0 : sizeof(uint16_t);
396-
}
361+
// The overhead that is incurred by switching on NMT (we need, per malloc allocation,
362+
// space for header and 16-bit footer)
363+
static const size_t overhead_per_malloc = sizeof(MallocHeader) + sizeof(uint16_t);
397364

398365
// Parameter name convention:
399366
// memblock : the beginning address for user data
@@ -405,30 +372,11 @@ class MallocTracker : AllStatic {
405372

406373
// Record malloc on specified memory block
407374
static void* record_malloc(void* malloc_base, size_t size, MEMFLAGS flags,
408-
const NativeCallStack& stack, NMT_TrackingLevel level);
375+
const NativeCallStack& stack);
409376

410377
// Record free on specified memory block
411378
static void* record_free(void* memblock);
412379

413-
// Offset memory address to header address
414-
static inline void* get_base(void* memblock);
415-
static inline void* get_base(void* memblock, NMT_TrackingLevel level) {
416-
if (memblock == NULL || level == NMT_off) return memblock;
417-
return (char*)memblock - malloc_header_size(level);
418-
}
419-
420-
// Get memory size
421-
static inline size_t get_size(void* memblock) {
422-
MallocHeader* header = malloc_header(memblock);
423-
return header->size();
424-
}
425-
426-
// Get memory type
427-
static inline MEMFLAGS get_flags(void* memblock) {
428-
MallocHeader* header = malloc_header(memblock);
429-
return header->flags();
430-
}
431-
432380
static inline void record_new_arena(MEMFLAGS flags) {
433381
MallocMemorySummary::record_new_arena(flags);
434382
}
@@ -451,8 +399,11 @@ class MallocTracker : AllStatic {
451399
private:
452400
static inline MallocHeader* malloc_header(void *memblock) {
453401
assert(memblock != NULL, "NULL pointer");
454-
MallocHeader* header = (MallocHeader*)((char*)memblock - sizeof(MallocHeader));
455-
return header;
402+
return (MallocHeader*)((char*)memblock - sizeof(MallocHeader));
403+
}
404+
static inline const MallocHeader* malloc_header(const void *memblock) {
405+
assert(memblock != NULL, "NULL pointer");
406+
return (const MallocHeader*)((const char*)memblock - sizeof(MallocHeader));
456407
}
457408
};
458409

‎src/hotspot/share/services/mallocTracker.inline.hpp

-36
This file was deleted.

‎src/hotspot/share/services/memTracker.cpp

+1-5
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include "runtime/vmOperations.hpp"
3434
#include "services/memBaseline.hpp"
3535
#include "services/memReporter.hpp"
36-
#include "services/mallocTracker.inline.hpp"
36+
#include "services/mallocTracker.hpp"
3737
#include "services/memTracker.hpp"
3838
#include "services/nmtCommon.hpp"
3939
#include "services/nmtPreInit.hpp"
@@ -90,10 +90,6 @@ void MemTracker::initialize() {
9090
}
9191
}
9292

93-
void* MemTracker::malloc_base(void* memblock) {
94-
return MallocTracker::get_base(memblock);
95-
}
96-
9793
void Tracker::record(address addr, size_t size) {
9894
if (MemTracker::tracking_level() < NMT_summary) return;
9995
switch(_type) {

‎src/hotspot/share/services/memTracker.hpp

+13-20
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "services/nmtCommon.hpp"
3232
#include "services/threadStackTracker.hpp"
3333
#include "services/virtualMemoryTracker.hpp"
34+
#include "utilities/debug.hpp"
3435
#include "utilities/nativeCallStack.hpp"
3536

3637
#define CURRENT_PC ((MemTracker::tracking_level() == NMT_detail) ? \
@@ -87,38 +88,30 @@ class MemTracker : AllStatic {
8788
return _tracking_level > NMT_off;
8889
}
8990

91+
// Per-malloc overhead incurred by NMT, depending on the current NMT level
92+
static size_t overhead_per_malloc() {
93+
return enabled() ? MallocTracker::overhead_per_malloc : 0;
94+
}
95+
9096
static inline void* record_malloc(void* mem_base, size_t size, MEMFLAGS flag,
91-
const NativeCallStack& stack, NMT_TrackingLevel level) {
92-
if (level != NMT_off) {
93-
return MallocTracker::record_malloc(mem_base, size, flag, stack, level);
97+
const NativeCallStack& stack) {
98+
assert(mem_base != NULL, "caller should handle NULL");
99+
if (enabled()) {
100+
return MallocTracker::record_malloc(mem_base, size, flag, stack);
94101
}
95102
return mem_base;
96103
}
97104

98-
static inline size_t malloc_header_size(NMT_TrackingLevel level) {
99-
return MallocTracker::malloc_header_size(level);
100-
}
101-
102-
// malloc tracking footer size for specific tracking level
103-
static inline size_t malloc_footer_size(NMT_TrackingLevel level) {
104-
return MallocTracker::malloc_footer_size(level);
105-
}
106-
107-
// To malloc base address, which is the starting address
108-
// of malloc tracking header if tracking is enabled.
109-
// Otherwise, it returns the same address.
110-
static void* malloc_base(void* memblock);
111-
112105
// Record malloc free and return malloc base address
113-
static inline void* record_free(void* memblock, NMT_TrackingLevel level) {
106+
static inline void* record_free(void* memblock) {
114107
// Never turned on
115-
if (level == NMT_off || memblock == NULL) {
108+
assert(memblock != NULL, "caller should handle NULL");
109+
if (!enabled()) {
116110
return memblock;
117111
}
118112
return MallocTracker::record_free(memblock);
119113
}
120114

121-
122115
// Record creation of an arena
123116
static inline void record_new_arena(MEMFLAGS flag) {
124117
if (!enabled()) return;

‎src/hotspot/share/services/nmtCommon.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class NMTUtil : AllStatic {
8787

8888
// Map memory type to index
8989
static inline int flag_to_index(MEMFLAGS flag) {
90-
assert(flag_is_valid(flag), "Invalid flag");
90+
assert(flag_is_valid(flag), "Invalid flag (%u)", (unsigned)flag);
9191
return static_cast<int>(flag);
9292
}
9393

@@ -98,7 +98,7 @@ class NMTUtil : AllStatic {
9898

9999
// Map an index to memory type
100100
static MEMFLAGS index_to_flag(int index) {
101-
assert(flag_index_is_valid(index), "Invalid flag");
101+
assert(flag_index_is_valid(index), "Invalid flag index (%d)", index);
102102
return static_cast<MEMFLAGS>(index);
103103
}
104104

‎src/hotspot/share/utilities/debug.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,10 @@ extern "C" JNIEXPORT void verify() {
478478
extern "C" JNIEXPORT void pp(void* p) {
479479
Command c("pp");
480480
FlagSetting fl(DisplayVMOutput, true);
481+
if (p == NULL) {
482+
tty->print_cr("NULL");
483+
return;
484+
}
481485
if (Universe::heap()->is_in(p)) {
482486
oop obj = cast_to_oop(p);
483487
obj->print();

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Feb 25, 2022

@openjdk-notifier[bot]
Please sign in to comment.