@@ -62,23 +62,16 @@ void MemoryCounter::update_peak(size_t size, size_t cnt) {
6262}
6363
6464void MallocMemorySnapshot::copy_to (MallocMemorySnapshot* s) {
65- // Use lock to make sure that mtChunks don't get deallocated while the
66- // copy is going on, because their size is adjusted using this
67- // buffer in make_adjustment().
68- ChunkPoolLocker::LockStrategy ls = ChunkPoolLocker::LockStrategy::Lock;
69- if (VMError::is_error_reported () && VMError::is_error_reported_in_current_thread ()) {
70- ls = ChunkPoolLocker::LockStrategy::Try;
71- }
72- ChunkPoolLocker cpl (ls);
7365 s->_all_mallocs = _all_mallocs;
7466 size_t total_size = 0 ;
7567 size_t total_count = 0 ;
7668 for (int index = 0 ; index < mt_number_of_tags; index ++) {
7769 s->_malloc [index] = _malloc[index];
78- total_size += s->_malloc [index].malloc_size ();
70+ total_size += s->_malloc [index].malloc_size () + s-> _malloc [index]. arena_size () ;
7971 total_count += s->_malloc [index].malloc_count ();
8072 }
8173 // malloc counters may be updated concurrently
74+ // Update total here for consistency with saved values of individual sizes.
8275 s->_all_mallocs .set_size_and_count (total_size, total_count);
8376}
8477
@@ -91,15 +84,6 @@ size_t MallocMemorySnapshot::total_arena() const {
9184 return amount;
9285}
9386
94- // Make adjustment by subtracting chunks used by arenas
95- // from total chunks to get total free chunk size
96- void MallocMemorySnapshot::make_adjustment () {
97- size_t arena_size = total_arena ();
98- int chunk_idx = NMTUtil::tag_to_index (mtChunk);
99- _malloc[chunk_idx].record_free (arena_size);
100- _all_mallocs.deallocate (arena_size);
101- }
102-
10387void MallocMemorySummary::initialize () {
10488 _snapshot.initialize ();
10589 MallocLimitHandler::initialize (MallocLimit);
@@ -203,6 +187,61 @@ void* MallocTracker::record_malloc(void* malloc_base, size_t size, MemTag mem_ta
203187 return memblock;
204188}
205189
190+ void MallocTracker::chunk_assigned_to_arena (void * memblock, MemTag new_tag) {
191+ MallocHeader* header = (MallocHeader*)memblock - 1 ;
192+
193+ // Only decrement mtChunk, leave the total malloc amounts unchanged.
194+ assert (header->mem_tag () == mtChunk, " Should only be operating on heap chunks" );
195+ MallocMemorySummary::as_snapshot ()->by_tag (mtChunk)->record_free (header->size ());
196+
197+ uint32_t new_mst_marker = 0 ;
198+ if (MemTracker::tracking_level () == NMT_detail) {
199+ // retrieve the old stack from MST
200+ NativeCallStack old_stack;
201+ if (!MallocSiteTable::access_stack (old_stack, *header)) {
202+ fatal (" NMT is now out of sync." );
203+ }
204+ MallocSiteTable::deallocation_at (header->size (), header->mst_marker ());
205+ // update MST with new tag
206+ if (!MallocSiteTable::allocation_at (old_stack, header->size (), &new_mst_marker, new_tag)) {
207+ fatal (" NMT is now out of sync." );
208+ }
209+ }
210+
211+ // Arena only accounts for payload. Account for the header size and count. Attribute it to the arena's tag.
212+ MallocMemorySummary::as_snapshot ()->by_tag (new_tag)->record_malloc (Chunk::aligned_overhead_size ());
213+
214+ header->set_mem_tag (new_tag);
215+ header->set_mst_marker (new_mst_marker);
216+ }
217+
218+ void MallocTracker::add_chunk_to_pool (void * memblock) {
219+ MallocHeader* header = (MallocHeader*)memblock - 1 ;
220+ assert (header->mem_tag () != mtChunk, " Should only be operating on arena chunks" );
221+
222+ // Only increment mtChunk, leave the total malloc amounts unchanged.
223+ MallocMemorySummary::as_snapshot ()->by_tag (mtChunk)->record_malloc (header->size ());
224+
225+ uint32_t new_mst_marker = 0 ;
226+ if (MemTracker::tracking_level () == NMT_detail) {
227+ NativeCallStack old_stack;
228+ // retrieve the old stack from MST
229+ if (!MallocSiteTable::access_stack (old_stack, *header)) {
230+ fatal (" NMT is now out of sync." );
231+ }
232+ MallocSiteTable::deallocation_at (header->size (), header->mst_marker ());
233+ // update MST with new tag
234+ if (!MallocSiteTable::allocation_at (old_stack, header->size (), &new_mst_marker, mtChunk)) {
235+ fatal (" NMT is now out of sync." );
236+ }
237+ }
238+
239+ MallocMemorySummary::as_snapshot ()->by_tag (header->mem_tag ())->record_free (Chunk::aligned_overhead_size ());
240+
241+ header->set_mem_tag (mtChunk);
242+ header->set_mst_marker (new_mst_marker);
243+ }
244+
206245void * MallocTracker::record_free_block (void * memblock) {
207246 assert (MemTracker::enabled (), " Sanity" );
208247 assert (memblock != nullptr , " precondition" );
0 commit comments