Skip to content

Commit b96b743

Browse files
committed
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) &&

0 commit comments

Comments
 (0)