1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 #include "precompiled.hpp" 25 26 #include "logging/log.hpp" 27 #include "logging/logStream.hpp" 28 #include "memory/binaryTreeDictionary.inline.hpp" 29 #include "memory/freeList.inline.hpp" 30 #include "memory/metaspace/chunkManager.hpp" 31 #include "memory/metaspace/metachunk.hpp" 32 #include "memory/metaspace/metaspaceCommon.hpp" 33 #include "memory/metaspace/metaspaceStatistics.hpp" 34 #include "memory/metaspace/occupancyMap.hpp" 35 #include "memory/metaspace/virtualSpaceNode.hpp" 36 #include "runtime/mutexLocker.hpp" 37 #include "utilities/debug.hpp" 38 #include "utilities/globalDefinitions.hpp" 39 #include "utilities/ostream.hpp" 40 41 namespace metaspace { 42 namespace internals { 43 44 45 void ChunkManager::remove_chunk(Metachunk* chunk) { 46 size_t word_size = chunk->word_size(); 47 ChunkIndex index = list_index(word_size); 48 if (index != HumongousIndex) { 49 free_chunks(index)->remove_chunk(chunk); 50 } else { 51 humongous_dictionary()->remove_chunk(chunk); 52 } 53 54 // Chunk has been removed from the chunks free list, update counters. 55 account_for_removed_chunk(chunk); 56 } 57 58 bool ChunkManager::attempt_to_coalesce_around_chunk(Metachunk* chunk, ChunkIndex target_chunk_type) { 59 assert_lock_strong(MetaspaceExpand_lock); 60 assert(chunk != NULL, "invalid chunk pointer"); 61 // Check for valid merge combinations. 62 assert((chunk->get_chunk_type() == SpecializedIndex && 63 (target_chunk_type == SmallIndex || target_chunk_type == MediumIndex)) || 64 (chunk->get_chunk_type() == SmallIndex && target_chunk_type == MediumIndex), 65 "Invalid chunk merge combination."); 66 67 const size_t target_chunk_word_size = 68 get_size_for_nonhumongous_chunktype(target_chunk_type, this->is_class()); 69 70 // [ prospective merge region ) 71 MetaWord* const p_merge_region_start = 72 (MetaWord*) align_down(chunk, target_chunk_word_size * sizeof(MetaWord)); 73 MetaWord* const p_merge_region_end = 74 p_merge_region_start + target_chunk_word_size; 75 76 // We need the VirtualSpaceNode containing this chunk and its occupancy map. 77 VirtualSpaceNode* const vsn = chunk->container(); 78 OccupancyMap* const ocmap = vsn->occupancy_map(); 79 80 // The prospective chunk merge range must be completely contained by the 81 // committed range of the virtual space node. 82 if (p_merge_region_start < vsn->bottom() || p_merge_region_end > vsn->top()) { 83 return false; 84 } 85 86 // Only attempt to merge this range if at its start a chunk starts and at its end 87 // a chunk ends. If a chunk (can only be humongous) straddles either start or end 88 // of that range, we cannot merge. 89 if (!ocmap->chunk_starts_at_address(p_merge_region_start)) { 90 return false; 91 } 92 if (p_merge_region_end < vsn->top() && 93 !ocmap->chunk_starts_at_address(p_merge_region_end)) { 94 return false; 95 } 96 97 // Now check if the prospective merge area contains live chunks. If it does we cannot merge. 98 if (ocmap->is_region_in_use(p_merge_region_start, target_chunk_word_size)) { 99 return false; 100 } 101 102 // Success! Remove all chunks in this region... 103 log_trace(gc, metaspace, freelist)("%s: coalescing chunks in area [%p-%p)...", 104 (is_class() ? "class space" : "metaspace"), 105 p_merge_region_start, p_merge_region_end); 106 107 const int num_chunks_removed = 108 remove_chunks_in_area(p_merge_region_start, target_chunk_word_size); 109 110 // ... and create a single new bigger chunk. 111 Metachunk* const p_new_chunk = 112 ::new (p_merge_region_start) Metachunk(target_chunk_type, is_class(), target_chunk_word_size, vsn); 113 assert(p_new_chunk == (Metachunk*)p_merge_region_start, "Sanity"); 114 p_new_chunk->set_origin(origin_merge); 115 116 log_trace(gc, metaspace, freelist)("%s: created coalesced chunk at %p, size " SIZE_FORMAT_HEX ".", 117 (is_class() ? "class space" : "metaspace"), 118 p_new_chunk, p_new_chunk->word_size() * sizeof(MetaWord)); 119 120 // Fix occupancy map: remove old start bits of the small chunks and set new start bit. 121 ocmap->wipe_chunk_start_bits_in_region(p_merge_region_start, target_chunk_word_size); 122 ocmap->set_chunk_starts_at_address(p_merge_region_start, true); 123 124 // Mark chunk as free. Note: it is not necessary to update the occupancy 125 // map in-use map, because the old chunks were also free, so nothing 126 // should have changed. 127 p_new_chunk->set_is_tagged_free(true); 128 129 // Add new chunk to its freelist. 130 ChunkList* const list = free_chunks(target_chunk_type); 131 list->return_chunk_at_head(p_new_chunk); 132 133 // And adjust ChunkManager:: _free_chunks_count (_free_chunks_total 134 // should not have changed, because the size of the space should be the same) 135 _free_chunks_count -= num_chunks_removed; 136 _free_chunks_count ++; 137 138 // VirtualSpaceNode::container_count does not have to be modified: 139 // it means "number of active (non-free) chunks", so merging free chunks 140 // should not affect that count. 141 142 // At the end of a chunk merge, run verification tests. 143 if (VerifyMetaspace) { 144 DEBUG_ONLY(this->locked_verify()); 145 DEBUG_ONLY(vsn->verify()); 146 } 147 148 return true; 149 } 150 151 // Remove all chunks in the given area - the chunks are supposed to be free - 152 // from their corresponding freelists. Mark them as invalid. 153 // - This does not correct the occupancy map. 154 // - This does not adjust the counters in ChunkManager. 155 // - Does not adjust container count counter in containing VirtualSpaceNode 156 // Returns number of chunks removed. 157 int ChunkManager::remove_chunks_in_area(MetaWord* p, size_t word_size) { 158 assert(p != NULL && word_size > 0, "Invalid range."); 159 const size_t smallest_chunk_size = get_size_for_nonhumongous_chunktype(SpecializedIndex, is_class()); 160 assert_is_aligned(word_size, smallest_chunk_size); 161 162 Metachunk* const start = (Metachunk*) p; 163 const Metachunk* const end = (Metachunk*)(p + word_size); 164 Metachunk* cur = start; 165 int num_removed = 0; 166 while (cur < end) { 167 Metachunk* next = (Metachunk*)(((MetaWord*)cur) + cur->word_size()); 168 DEBUG_ONLY(do_verify_chunk(cur)); 169 assert(cur->get_chunk_type() != HumongousIndex, "Unexpected humongous chunk found at %p.", cur); 170 assert(cur->is_tagged_free(), "Chunk expected to be free (%p)", cur); 171 log_trace(gc, metaspace, freelist)("%s: removing chunk %p, size " SIZE_FORMAT_HEX ".", 172 (is_class() ? "class space" : "metaspace"), 173 cur, cur->word_size() * sizeof(MetaWord)); 174 cur->remove_sentinel(); 175 // Note: cannot call ChunkManager::remove_chunk, because that 176 // modifies the counters in ChunkManager, which we do not want. So 177 // we call remove_chunk on the freelist directly (see also the 178 // splitting function which does the same). 179 ChunkList* const list = free_chunks(list_index(cur->word_size())); 180 list->remove_chunk(cur); 181 num_removed ++; 182 cur = next; 183 } 184 return num_removed; 185 } 186 187 size_t ChunkManager::free_chunks_total_words() { 188 return _free_chunks_total; 189 } 190 191 size_t ChunkManager::free_chunks_total_bytes() { 192 return free_chunks_total_words() * BytesPerWord; 193 } 194 195 // Update internal accounting after a chunk was added 196 void ChunkManager::account_for_added_chunk(const Metachunk* c) { 197 assert_lock_strong(MetaspaceExpand_lock); 198 _free_chunks_count ++; 199 _free_chunks_total += c->word_size(); 200 } 201 202 // Update internal accounting after a chunk was removed 203 void ChunkManager::account_for_removed_chunk(const Metachunk* c) { 204 assert_lock_strong(MetaspaceExpand_lock); 205 assert(_free_chunks_count >= 1, 206 "ChunkManager::_free_chunks_count: about to go negative (" SIZE_FORMAT ").", _free_chunks_count); 207 assert(_free_chunks_total >= c->word_size(), 208 "ChunkManager::_free_chunks_total: about to go negative" 209 "(now: " SIZE_FORMAT ", decrement value: " SIZE_FORMAT ").", _free_chunks_total, c->word_size()); 210 _free_chunks_count --; 211 _free_chunks_total -= c->word_size(); 212 } 213 214 size_t ChunkManager::free_chunks_count() { 215 #ifdef ASSERT 216 if (!UseConcMarkSweepGC && !MetaspaceExpand_lock->is_locked()) { 217 MutexLockerEx cl(MetaspaceExpand_lock, 218 Mutex::_no_safepoint_check_flag); 219 // This lock is only needed in debug because the verification 220 // of the _free_chunks_totals walks the list of free chunks 221 slow_locked_verify_free_chunks_count(); 222 } 223 #endif 224 return _free_chunks_count; 225 } 226 227 ChunkIndex ChunkManager::list_index(size_t size) { 228 return get_chunk_type_by_size(size, is_class()); 229 } 230 231 size_t ChunkManager::size_by_index(ChunkIndex index) const { 232 index_bounds_check(index); 233 assert(index != HumongousIndex, "Do not call for humongous chunks."); 234 return get_size_for_nonhumongous_chunktype(index, is_class()); 235 } 236 237 void ChunkManager::locked_verify_free_chunks_total() { 238 assert_lock_strong(MetaspaceExpand_lock); 239 assert(sum_free_chunks() == _free_chunks_total, 240 "_free_chunks_total " SIZE_FORMAT " is not the" 241 " same as sum " SIZE_FORMAT, _free_chunks_total, 242 sum_free_chunks()); 243 } 244 245 void ChunkManager::locked_verify_free_chunks_count() { 246 assert_lock_strong(MetaspaceExpand_lock); 247 assert(sum_free_chunks_count() == _free_chunks_count, 248 "_free_chunks_count " SIZE_FORMAT " is not the" 249 " same as sum " SIZE_FORMAT, _free_chunks_count, 250 sum_free_chunks_count()); 251 } 252 253 void ChunkManager::verify() { 254 MutexLockerEx cl(MetaspaceExpand_lock, 255 Mutex::_no_safepoint_check_flag); 256 locked_verify(); 257 } 258 259 void ChunkManager::locked_verify() { 260 locked_verify_free_chunks_count(); 261 locked_verify_free_chunks_total(); 262 for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) { 263 ChunkList* list = free_chunks(i); 264 if (list != NULL) { 265 Metachunk* chunk = list->head(); 266 while (chunk) { 267 DEBUG_ONLY(do_verify_chunk(chunk);) 268 assert(chunk->is_tagged_free(), "Chunk should be tagged as free."); 269 chunk = chunk->next(); 270 } 271 } 272 } 273 } 274 275 void ChunkManager::locked_print_free_chunks(outputStream* st) { 276 assert_lock_strong(MetaspaceExpand_lock); 277 st->print_cr("Free chunk total " SIZE_FORMAT " count " SIZE_FORMAT, 278 _free_chunks_total, _free_chunks_count); 279 } 280 281 void ChunkManager::locked_print_sum_free_chunks(outputStream* st) { 282 assert_lock_strong(MetaspaceExpand_lock); 283 st->print_cr("Sum free chunk total " SIZE_FORMAT " count " SIZE_FORMAT, 284 sum_free_chunks(), sum_free_chunks_count()); 285 } 286 287 ChunkList* ChunkManager::free_chunks(ChunkIndex index) { 288 assert(index == SpecializedIndex || index == SmallIndex || index == MediumIndex, 289 "Bad index: %d", (int)index); 290 291 return &_free_chunks[index]; 292 } 293 294 // These methods that sum the free chunk lists are used in printing 295 // methods that are used in product builds. 296 size_t ChunkManager::sum_free_chunks() { 297 assert_lock_strong(MetaspaceExpand_lock); 298 size_t result = 0; 299 for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) { 300 ChunkList* list = free_chunks(i); 301 302 if (list == NULL) { 303 continue; 304 } 305 306 result = result + list->count() * list->size(); 307 } 308 result = result + humongous_dictionary()->total_size(); 309 return result; 310 } 311 312 size_t ChunkManager::sum_free_chunks_count() { 313 assert_lock_strong(MetaspaceExpand_lock); 314 size_t count = 0; 315 for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) { 316 ChunkList* list = free_chunks(i); 317 if (list == NULL) { 318 continue; 319 } 320 count = count + list->count(); 321 } 322 count = count + humongous_dictionary()->total_free_blocks(); 323 return count; 324 } 325 326 ChunkList* ChunkManager::find_free_chunks_list(size_t word_size) { 327 ChunkIndex index = list_index(word_size); 328 assert(index < HumongousIndex, "No humongous list"); 329 return free_chunks(index); 330 } 331 332 // Helper for chunk splitting: given a target chunk size and a larger free chunk, 333 // split up the larger chunk into n smaller chunks, at least one of which should be 334 // the target chunk of target chunk size. The smaller chunks, including the target 335 // chunk, are returned to the freelist. The pointer to the target chunk is returned. 336 // Note that this chunk is supposed to be removed from the freelist right away. 337 Metachunk* ChunkManager::split_chunk(size_t target_chunk_word_size, Metachunk* larger_chunk) { 338 assert(larger_chunk->word_size() > target_chunk_word_size, "Sanity"); 339 340 const ChunkIndex larger_chunk_index = larger_chunk->get_chunk_type(); 341 const ChunkIndex target_chunk_index = get_chunk_type_by_size(target_chunk_word_size, is_class()); 342 343 MetaWord* const region_start = (MetaWord*)larger_chunk; 344 const size_t region_word_len = larger_chunk->word_size(); 345 MetaWord* const region_end = region_start + region_word_len; 346 VirtualSpaceNode* const vsn = larger_chunk->container(); 347 OccupancyMap* const ocmap = vsn->occupancy_map(); 348 349 // Any larger non-humongous chunk size is a multiple of any smaller chunk size. 350 // Since non-humongous chunks are aligned to their chunk size, the larger chunk should start 351 // at an address suitable to place the smaller target chunk. 352 assert_is_aligned(region_start, target_chunk_word_size); 353 354 // Remove old chunk. 355 free_chunks(larger_chunk_index)->remove_chunk(larger_chunk); 356 larger_chunk->remove_sentinel(); 357 358 // Prevent access to the old chunk from here on. 359 larger_chunk = NULL; 360 // ... and wipe it. 361 DEBUG_ONLY(memset(region_start, 0xfe, region_word_len * BytesPerWord)); 362 363 // In its place create first the target chunk... 364 MetaWord* p = region_start; 365 Metachunk* target_chunk = ::new (p) Metachunk(target_chunk_index, is_class(), target_chunk_word_size, vsn); 366 assert(target_chunk == (Metachunk*)p, "Sanity"); 367 target_chunk->set_origin(origin_split); 368 369 // Note: we do not need to mark its start in the occupancy map 370 // because it coincides with the old chunk start. 371 372 // Mark chunk as free and return to the freelist. 373 do_update_in_use_info_for_chunk(target_chunk, false); 374 free_chunks(target_chunk_index)->return_chunk_at_head(target_chunk); 375 376 // This chunk should now be valid and can be verified. 377 DEBUG_ONLY(do_verify_chunk(target_chunk)); 378 379 // In the remaining space create the remainder chunks. 380 p += target_chunk->word_size(); 381 assert(p < region_end, "Sanity"); 382 383 while (p < region_end) { 384 385 // Find the largest chunk size which fits the alignment requirements at address p. 386 ChunkIndex this_chunk_index = prev_chunk_index(larger_chunk_index); 387 size_t this_chunk_word_size = 0; 388 for(;;) { 389 this_chunk_word_size = get_size_for_nonhumongous_chunktype(this_chunk_index, is_class()); 390 if (is_aligned(p, this_chunk_word_size * BytesPerWord)) { 391 break; 392 } else { 393 this_chunk_index = prev_chunk_index(this_chunk_index); 394 assert(this_chunk_index >= target_chunk_index, "Sanity"); 395 } 396 } 397 398 assert(this_chunk_word_size >= target_chunk_word_size, "Sanity"); 399 assert(is_aligned(p, this_chunk_word_size * BytesPerWord), "Sanity"); 400 assert(p + this_chunk_word_size <= region_end, "Sanity"); 401 402 // Create splitting chunk. 403 Metachunk* this_chunk = ::new (p) Metachunk(this_chunk_index, is_class(), this_chunk_word_size, vsn); 404 assert(this_chunk == (Metachunk*)p, "Sanity"); 405 this_chunk->set_origin(origin_split); 406 ocmap->set_chunk_starts_at_address(p, true); 407 do_update_in_use_info_for_chunk(this_chunk, false); 408 409 // This chunk should be valid and can be verified. 410 DEBUG_ONLY(do_verify_chunk(this_chunk)); 411 412 // Return this chunk to freelist and correct counter. 413 free_chunks(this_chunk_index)->return_chunk_at_head(this_chunk); 414 _free_chunks_count ++; 415 416 log_trace(gc, metaspace, freelist)("Created chunk at " PTR_FORMAT ", word size " 417 SIZE_FORMAT_HEX " (%s), in split region [" PTR_FORMAT "..." PTR_FORMAT ").", 418 p2i(this_chunk), this_chunk->word_size(), chunk_size_name(this_chunk_index), 419 p2i(region_start), p2i(region_end)); 420 421 p += this_chunk_word_size; 422 423 } 424 425 return target_chunk; 426 } 427 428 Metachunk* ChunkManager::free_chunks_get(size_t word_size) { 429 assert_lock_strong(MetaspaceExpand_lock); 430 431 slow_locked_verify(); 432 433 Metachunk* chunk = NULL; 434 bool we_did_split_a_chunk = false; 435 436 if (list_index(word_size) != HumongousIndex) { 437 438 ChunkList* free_list = find_free_chunks_list(word_size); 439 assert(free_list != NULL, "Sanity check"); 440 441 chunk = free_list->head(); 442 443 if (chunk == NULL) { 444 // Split large chunks into smaller chunks if there are no smaller chunks, just large chunks. 445 // This is the counterpart of the coalescing-upon-chunk-return. 446 447 ChunkIndex target_chunk_index = get_chunk_type_by_size(word_size, is_class()); 448 449 // Is there a larger chunk we could split? 450 Metachunk* larger_chunk = NULL; 451 ChunkIndex larger_chunk_index = next_chunk_index(target_chunk_index); 452 while (larger_chunk == NULL && larger_chunk_index < NumberOfFreeLists) { 453 larger_chunk = free_chunks(larger_chunk_index)->head(); 454 if (larger_chunk == NULL) { 455 larger_chunk_index = next_chunk_index(larger_chunk_index); 456 } 457 } 458 459 if (larger_chunk != NULL) { 460 assert(larger_chunk->word_size() > word_size, "Sanity"); 461 assert(larger_chunk->get_chunk_type() == larger_chunk_index, "Sanity"); 462 463 // We found a larger chunk. Lets split it up: 464 // - remove old chunk 465 // - in its place, create new smaller chunks, with at least one chunk 466 // being of target size, the others sized as large as possible. This 467 // is to make sure the resulting chunks are "as coalesced as possible" 468 // (similar to VirtualSpaceNode::retire()). 469 // Note: during this operation both ChunkManager and VirtualSpaceNode 470 // are temporarily invalid, so be careful with asserts. 471 472 log_trace(gc, metaspace, freelist)("%s: splitting chunk " PTR_FORMAT 473 ", word size " SIZE_FORMAT_HEX " (%s), to get a chunk of word size " SIZE_FORMAT_HEX " (%s)...", 474 (is_class() ? "class space" : "metaspace"), p2i(larger_chunk), larger_chunk->word_size(), 475 chunk_size_name(larger_chunk_index), word_size, chunk_size_name(target_chunk_index)); 476 477 chunk = split_chunk(word_size, larger_chunk); 478 479 // This should have worked. 480 assert(chunk != NULL, "Sanity"); 481 assert(chunk->word_size() == word_size, "Sanity"); 482 assert(chunk->is_tagged_free(), "Sanity"); 483 484 we_did_split_a_chunk = true; 485 486 } 487 } 488 489 if (chunk == NULL) { 490 return NULL; 491 } 492 493 // Remove the chunk as the head of the list. 494 free_list->remove_chunk(chunk); 495 496 log_trace(gc, metaspace, freelist)("ChunkManager::free_chunks_get: free_list: " PTR_FORMAT " chunks left: " SSIZE_FORMAT ".", 497 p2i(free_list), free_list->count()); 498 499 } else { 500 chunk = humongous_dictionary()->get_chunk(word_size); 501 502 if (chunk == NULL) { 503 return NULL; 504 } 505 506 log_debug(gc, metaspace, alloc)("Free list allocate humongous chunk size " SIZE_FORMAT " for requested size " SIZE_FORMAT " waste " SIZE_FORMAT, 507 chunk->word_size(), word_size, chunk->word_size() - word_size); 508 } 509 510 // Chunk has been removed from the chunk manager; update counters. 511 account_for_removed_chunk(chunk); 512 do_update_in_use_info_for_chunk(chunk, true); 513 chunk->container()->inc_container_count(); 514 chunk->inc_use_count(); 515 516 // Remove it from the links to this freelist 517 chunk->set_next(NULL); 518 chunk->set_prev(NULL); 519 520 // Run some verifications (some more if we did a chunk split) 521 #ifdef ASSERT 522 if (VerifyMetaspace) { 523 locked_verify(); 524 VirtualSpaceNode* const vsn = chunk->container(); 525 vsn->verify(); 526 if (we_did_split_a_chunk) { 527 vsn->verify_free_chunks_are_ideally_merged(); 528 } 529 } 530 #endif 531 532 return chunk; 533 } 534 535 Metachunk* ChunkManager::chunk_freelist_allocate(size_t word_size) { 536 assert_lock_strong(MetaspaceExpand_lock); 537 slow_locked_verify(); 538 539 // Take from the beginning of the list 540 Metachunk* chunk = free_chunks_get(word_size); 541 if (chunk == NULL) { 542 return NULL; 543 } 544 545 assert((word_size <= chunk->word_size()) || 546 (list_index(chunk->word_size()) == HumongousIndex), 547 "Non-humongous variable sized chunk"); 548 LogTarget(Debug, gc, metaspace, freelist) lt; 549 if (lt.is_enabled()) { 550 size_t list_count; 551 if (list_index(word_size) < HumongousIndex) { 552 ChunkList* list = find_free_chunks_list(word_size); 553 list_count = list->count(); 554 } else { 555 list_count = humongous_dictionary()->total_count(); 556 } 557 LogStream ls(lt); 558 ls.print("ChunkManager::chunk_freelist_allocate: " PTR_FORMAT " chunk " PTR_FORMAT " size " SIZE_FORMAT " count " SIZE_FORMAT " ", 559 p2i(this), p2i(chunk), chunk->word_size(), list_count); 560 ResourceMark rm; 561 locked_print_free_chunks(&ls); 562 } 563 564 return chunk; 565 } 566 567 void ChunkManager::return_single_chunk(Metachunk* chunk) { 568 const ChunkIndex index = chunk->get_chunk_type(); 569 assert_lock_strong(MetaspaceExpand_lock); 570 DEBUG_ONLY(do_verify_chunk(chunk);) 571 assert(chunk != NULL, "Expected chunk."); 572 assert(chunk->container() != NULL, "Container should have been set."); 573 assert(chunk->is_tagged_free() == false, "Chunk should be in use."); 574 index_bounds_check(index); 575 576 // Note: mangle *before* returning the chunk to the freelist or dictionary. It does not 577 // matter for the freelist (non-humongous chunks), but the humongous chunk dictionary 578 // keeps tree node pointers in the chunk payload area which mangle will overwrite. 579 DEBUG_ONLY(chunk->mangle(badMetaWordVal);) 580 581 if (index != HumongousIndex) { 582 // Return non-humongous chunk to freelist. 583 ChunkList* list = free_chunks(index); 584 assert(list->size() == chunk->word_size(), "Wrong chunk type."); 585 list->return_chunk_at_head(chunk); 586 log_trace(gc, metaspace, freelist)("returned one %s chunk at " PTR_FORMAT " to freelist.", 587 chunk_size_name(index), p2i(chunk)); 588 } else { 589 // Return humongous chunk to dictionary. 590 assert(chunk->word_size() > free_chunks(MediumIndex)->size(), "Wrong chunk type."); 591 assert(chunk->word_size() % free_chunks(SpecializedIndex)->size() == 0, 592 "Humongous chunk has wrong alignment."); 593 _humongous_dictionary.return_chunk(chunk); 594 log_trace(gc, metaspace, freelist)("returned one %s chunk at " PTR_FORMAT " (word size " SIZE_FORMAT ") to freelist.", 595 chunk_size_name(index), p2i(chunk), chunk->word_size()); 596 } 597 chunk->container()->dec_container_count(); 598 do_update_in_use_info_for_chunk(chunk, false); 599 600 // Chunk has been added; update counters. 601 account_for_added_chunk(chunk); 602 603 // Attempt coalesce returned chunks with its neighboring chunks: 604 // if this chunk is small or special, attempt to coalesce to a medium chunk. 605 if (index == SmallIndex || index == SpecializedIndex) { 606 if (!attempt_to_coalesce_around_chunk(chunk, MediumIndex)) { 607 // This did not work. But if this chunk is special, we still may form a small chunk? 608 if (index == SpecializedIndex) { 609 if (!attempt_to_coalesce_around_chunk(chunk, SmallIndex)) { 610 // give up. 611 } 612 } 613 } 614 } 615 616 } 617 618 void ChunkManager::return_chunk_list(Metachunk* chunks) { 619 if (chunks == NULL) { 620 return; 621 } 622 LogTarget(Trace, gc, metaspace, freelist) log; 623 if (log.is_enabled()) { // tracing 624 log.print("returning list of chunks..."); 625 } 626 unsigned num_chunks_returned = 0; 627 size_t size_chunks_returned = 0; 628 Metachunk* cur = chunks; 629 while (cur != NULL) { 630 // Capture the next link before it is changed 631 // by the call to return_chunk_at_head(); 632 Metachunk* next = cur->next(); 633 if (log.is_enabled()) { // tracing 634 num_chunks_returned ++; 635 size_chunks_returned += cur->word_size(); 636 } 637 return_single_chunk(cur); 638 cur = next; 639 } 640 if (log.is_enabled()) { // tracing 641 log.print("returned %u chunks to freelist, total word size " SIZE_FORMAT ".", 642 num_chunks_returned, size_chunks_returned); 643 } 644 } 645 646 void ChunkManager::collect_statistics(ChunkManagerStatistics* out) const { 647 MutexLockerEx cl(MetaspaceExpand_lock, Mutex::_no_safepoint_check_flag); 648 for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) { 649 out->chunk_stats(i).add(num_free_chunks(i), size_free_chunks_in_bytes(i) / sizeof(MetaWord)); 650 } 651 } 652 653 } // namespace metaspace 654 } // namespace internals 655 656 657