1 /* 2 * Copyright (c) 2005, 2024, 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 25 #include "precompiled.hpp" 26 #include "classfile/classLoaderDataGraph.hpp" 27 #include "classfile/javaClasses.inline.hpp" 28 #include "classfile/stringTable.hpp" 29 #include "classfile/symbolTable.hpp" 30 #include "classfile/systemDictionary.hpp" 31 #include "code/codeCache.hpp" 32 #include "compiler/oopMap.hpp" 33 #include "gc/parallel/objectStartArray.inline.hpp" 34 #include "gc/parallel/parallelArguments.hpp" 35 #include "gc/parallel/parallelScavengeHeap.inline.hpp" 36 #include "gc/parallel/parMarkBitMap.inline.hpp" 37 #include "gc/parallel/psAdaptiveSizePolicy.hpp" 38 #include "gc/parallel/psCompactionManager.inline.hpp" 39 #include "gc/parallel/psOldGen.hpp" 40 #include "gc/parallel/psParallelCompact.inline.hpp" 41 #include "gc/parallel/psPromotionManager.inline.hpp" 42 #include "gc/parallel/psRootType.hpp" 43 #include "gc/parallel/psScavenge.hpp" 44 #include "gc/parallel/psStringDedup.hpp" 45 #include "gc/parallel/psYoungGen.hpp" 46 #include "gc/shared/classUnloadingContext.hpp" 47 #include "gc/shared/fullGCForwarding.inline.hpp" 48 #include "gc/shared/gcCause.hpp" 49 #include "gc/shared/gcHeapSummary.hpp" 50 #include "gc/shared/gcId.hpp" 51 #include "gc/shared/gcLocker.hpp" 52 #include "gc/shared/gcTimer.hpp" 53 #include "gc/shared/gcTrace.hpp" 54 #include "gc/shared/gcTraceTime.inline.hpp" 55 #include "gc/shared/gcVMOperations.hpp" 56 #include "gc/shared/isGCActiveMark.hpp" 57 #include "gc/shared/oopStorage.inline.hpp" 58 #include "gc/shared/oopStorageSet.inline.hpp" 59 #include "gc/shared/oopStorageSetParState.inline.hpp" 60 #include "gc/shared/preservedMarks.inline.hpp" 61 #include "gc/shared/referencePolicy.hpp" 62 #include "gc/shared/referenceProcessor.hpp" 63 #include "gc/shared/referenceProcessorPhaseTimes.hpp" 64 #include "gc/shared/spaceDecorator.hpp" 65 #include "gc/shared/strongRootsScope.hpp" 66 #include "gc/shared/taskTerminator.hpp" 67 #include "gc/shared/weakProcessor.inline.hpp" 68 #include "gc/shared/workerPolicy.hpp" 69 #include "gc/shared/workerThread.hpp" 70 #include "gc/shared/workerUtils.hpp" 71 #include "logging/log.hpp" 72 #include "memory/iterator.inline.hpp" 73 #include "memory/metaspaceUtils.hpp" 74 #include "memory/resourceArea.hpp" 75 #include "memory/universe.hpp" 76 #include "nmt/memTracker.hpp" 77 #include "oops/access.inline.hpp" 78 #include "oops/instanceClassLoaderKlass.inline.hpp" 79 #include "oops/instanceKlass.inline.hpp" 80 #include "oops/instanceMirrorKlass.inline.hpp" 81 #include "oops/methodData.hpp" 82 #include "oops/objArrayKlass.inline.hpp" 83 #include "oops/oop.inline.hpp" 84 #include "runtime/atomic.hpp" 85 #include "runtime/handles.inline.hpp" 86 #include "runtime/java.hpp" 87 #include "runtime/safepoint.hpp" 88 #include "runtime/threads.hpp" 89 #include "runtime/vmThread.hpp" 90 #include "services/memoryService.hpp" 91 #include "utilities/align.hpp" 92 #include "utilities/debug.hpp" 93 #include "utilities/events.hpp" 94 #include "utilities/formatBuffer.hpp" 95 #include "utilities/macros.hpp" 96 #include "utilities/stack.inline.hpp" 97 #if INCLUDE_JVMCI 98 #include "jvmci/jvmci.hpp" 99 #endif 100 101 #include <math.h> 102 103 // All sizes are in HeapWords. 104 const size_t ParallelCompactData::Log2RegionSize = 16; // 64K words 105 const size_t ParallelCompactData::RegionSize = (size_t)1 << Log2RegionSize; 106 static_assert(ParallelCompactData::RegionSize >= BitsPerWord, "region-start bit word-aligned"); 107 const size_t ParallelCompactData::RegionSizeBytes = 108 RegionSize << LogHeapWordSize; 109 const size_t ParallelCompactData::RegionSizeOffsetMask = RegionSize - 1; 110 const size_t ParallelCompactData::RegionAddrOffsetMask = RegionSizeBytes - 1; 111 const size_t ParallelCompactData::RegionAddrMask = ~RegionAddrOffsetMask; 112 113 const ParallelCompactData::RegionData::region_sz_t 114 ParallelCompactData::RegionData::dc_shift = 27; 115 116 const ParallelCompactData::RegionData::region_sz_t 117 ParallelCompactData::RegionData::dc_mask = ~0U << dc_shift; 118 119 const ParallelCompactData::RegionData::region_sz_t 120 ParallelCompactData::RegionData::dc_one = 0x1U << dc_shift; 121 122 const ParallelCompactData::RegionData::region_sz_t 123 ParallelCompactData::RegionData::los_mask = ~dc_mask; 124 125 const ParallelCompactData::RegionData::region_sz_t 126 ParallelCompactData::RegionData::dc_claimed = 0x8U << dc_shift; 127 128 const ParallelCompactData::RegionData::region_sz_t 129 ParallelCompactData::RegionData::dc_completed = 0xcU << dc_shift; 130 131 bool ParallelCompactData::RegionData::is_clear() { 132 return (_destination == nullptr) && 133 (_source_region == 0) && 134 (_partial_obj_addr == nullptr) && 135 (_partial_obj_size == 0) && 136 (_dc_and_los == 0) && 137 (_shadow_state == 0); 138 } 139 140 #ifdef ASSERT 141 void ParallelCompactData::RegionData::verify_clear() { 142 assert(_destination == nullptr, "inv"); 143 assert(_source_region == 0, "inv"); 144 assert(_partial_obj_addr == nullptr, "inv"); 145 assert(_partial_obj_size == 0, "inv"); 146 assert(_dc_and_los == 0, "inv"); 147 assert(_shadow_state == 0, "inv"); 148 } 149 #endif 150 151 SpaceInfo PSParallelCompact::_space_info[PSParallelCompact::last_space_id]; 152 153 SpanSubjectToDiscoveryClosure PSParallelCompact::_span_based_discoverer; 154 ReferenceProcessor* PSParallelCompact::_ref_processor = nullptr; 155 156 void SplitInfo::record(size_t split_region_idx, HeapWord* split_point, size_t preceding_live_words) { 157 assert(split_region_idx != 0, "precondition"); 158 159 // Obj denoted by split_point will be deferred to the next space. 160 assert(split_point != nullptr, "precondition"); 161 162 const ParallelCompactData& sd = PSParallelCompact::summary_data(); 163 164 PSParallelCompact::RegionData* split_region_ptr = sd.region(split_region_idx); 165 assert(preceding_live_words < split_region_ptr->data_size(), "inv"); 166 167 HeapWord* preceding_destination = split_region_ptr->destination(); 168 assert(preceding_destination != nullptr, "inv"); 169 170 // How many regions does the preceding part occupy 171 uint preceding_destination_count; 172 if (preceding_live_words == 0) { 173 preceding_destination_count = 0; 174 } else { 175 // -1 so that the ending address doesn't fall on the region-boundary 176 if (sd.region_align_down(preceding_destination) == 177 sd.region_align_down(preceding_destination + preceding_live_words - 1)) { 178 preceding_destination_count = 1; 179 } else { 180 preceding_destination_count = 2; 181 } 182 } 183 184 _split_region_idx = split_region_idx; 185 _split_point = split_point; 186 _preceding_live_words = preceding_live_words; 187 _preceding_destination = preceding_destination; 188 _preceding_destination_count = preceding_destination_count; 189 } 190 191 void SplitInfo::clear() 192 { 193 _split_region_idx = 0; 194 _split_point = nullptr; 195 _preceding_live_words = 0; 196 _preceding_destination = nullptr; 197 _preceding_destination_count = 0; 198 assert(!is_valid(), "sanity"); 199 } 200 201 #ifdef ASSERT 202 void SplitInfo::verify_clear() 203 { 204 assert(_split_region_idx == 0, "not clear"); 205 assert(_split_point == nullptr, "not clear"); 206 assert(_preceding_live_words == 0, "not clear"); 207 assert(_preceding_destination == nullptr, "not clear"); 208 assert(_preceding_destination_count == 0, "not clear"); 209 } 210 #endif // #ifdef ASSERT 211 212 213 void PSParallelCompact::print_on_error(outputStream* st) { 214 _mark_bitmap.print_on_error(st); 215 } 216 217 ParallelCompactData::ParallelCompactData() : 218 _heap_start(nullptr), 219 DEBUG_ONLY(_heap_end(nullptr) COMMA) 220 _region_vspace(nullptr), 221 _reserved_byte_size(0), 222 _region_data(nullptr), 223 _region_count(0) {} 224 225 bool ParallelCompactData::initialize(MemRegion reserved_heap) 226 { 227 _heap_start = reserved_heap.start(); 228 const size_t heap_size = reserved_heap.word_size(); 229 DEBUG_ONLY(_heap_end = _heap_start + heap_size;) 230 231 assert(region_align_down(_heap_start) == _heap_start, 232 "region start not aligned"); 233 234 return initialize_region_data(heap_size); 235 } 236 237 PSVirtualSpace* 238 ParallelCompactData::create_vspace(size_t count, size_t element_size) 239 { 240 const size_t raw_bytes = count * element_size; 241 const size_t page_sz = os::page_size_for_region_aligned(raw_bytes, 10); 242 const size_t granularity = os::vm_allocation_granularity(); 243 _reserved_byte_size = align_up(raw_bytes, MAX2(page_sz, granularity)); 244 245 const size_t rs_align = page_sz == os::vm_page_size() ? 0 : 246 MAX2(page_sz, granularity); 247 ReservedSpace rs(_reserved_byte_size, rs_align, page_sz); 248 os::trace_page_sizes("Parallel Compact Data", raw_bytes, raw_bytes, rs.base(), 249 rs.size(), page_sz); 250 251 MemTracker::record_virtual_memory_tag((address)rs.base(), mtGC); 252 253 PSVirtualSpace* vspace = new PSVirtualSpace(rs, page_sz); 254 if (vspace != nullptr) { 255 if (vspace->expand_by(_reserved_byte_size)) { 256 return vspace; 257 } 258 delete vspace; 259 // Release memory reserved in the space. 260 rs.release(); 261 } 262 263 return nullptr; 264 } 265 266 bool ParallelCompactData::initialize_region_data(size_t heap_size) 267 { 268 assert(is_aligned(heap_size, RegionSize), "precondition"); 269 270 const size_t count = heap_size >> Log2RegionSize; 271 _region_vspace = create_vspace(count, sizeof(RegionData)); 272 if (_region_vspace != nullptr) { 273 _region_data = (RegionData*)_region_vspace->reserved_low_addr(); 274 _region_count = count; 275 return true; 276 } 277 return false; 278 } 279 280 void ParallelCompactData::clear_range(size_t beg_region, size_t end_region) { 281 assert(beg_region <= _region_count, "beg_region out of range"); 282 assert(end_region <= _region_count, "end_region out of range"); 283 284 const size_t region_cnt = end_region - beg_region; 285 memset(_region_data + beg_region, 0, region_cnt * sizeof(RegionData)); 286 } 287 288 void 289 ParallelCompactData::summarize_dense_prefix(HeapWord* beg, HeapWord* end) 290 { 291 assert(is_region_aligned(beg), "not RegionSize aligned"); 292 assert(is_region_aligned(end), "not RegionSize aligned"); 293 294 size_t cur_region = addr_to_region_idx(beg); 295 const size_t end_region = addr_to_region_idx(end); 296 HeapWord* addr = beg; 297 while (cur_region < end_region) { 298 _region_data[cur_region].set_destination(addr); 299 _region_data[cur_region].set_destination_count(0); 300 _region_data[cur_region].set_source_region(cur_region); 301 302 // Update live_obj_size so the region appears completely full. 303 size_t live_size = RegionSize - _region_data[cur_region].partial_obj_size(); 304 _region_data[cur_region].set_live_obj_size(live_size); 305 306 ++cur_region; 307 addr += RegionSize; 308 } 309 } 310 311 // The total live words on src_region would overflow the target space, so find 312 // the overflowing object and record the split point. The invariant is that an 313 // obj should not cross space boundary. 314 HeapWord* ParallelCompactData::summarize_split_space(size_t src_region, 315 SplitInfo& split_info, 316 HeapWord* const destination, 317 HeapWord* const target_end, 318 HeapWord** target_next) { 319 assert(destination <= target_end, "sanity"); 320 assert(destination + _region_data[src_region].data_size() > target_end, 321 "region should not fit into target space"); 322 assert(is_region_aligned(target_end), "sanity"); 323 324 size_t partial_obj_size = _region_data[src_region].partial_obj_size(); 325 326 if (destination + partial_obj_size > target_end) { 327 assert(partial_obj_size > 0, "inv"); 328 // The overflowing obj is from a previous region. 329 // 330 // source-regions: 331 // 332 // *************** 333 // | A|AA | 334 // *************** 335 // ^ 336 // | split-point 337 // 338 // dest-region: 339 // 340 // ******** 341 // |~~~~A | 342 // ******** 343 // ^^ 344 // || target-space-end 345 // | 346 // | destination 347 // 348 // AAA would overflow target-space. 349 // 350 HeapWord* overflowing_obj = _region_data[src_region].partial_obj_addr(); 351 size_t split_region = addr_to_region_idx(overflowing_obj); 352 353 // The number of live words before the overflowing object on this split region 354 size_t preceding_live_words; 355 if (is_region_aligned(overflowing_obj)) { 356 preceding_live_words = 0; 357 } else { 358 // Words accounted by the overflowing object on the split region 359 size_t overflowing_size = pointer_delta(region_align_up(overflowing_obj), overflowing_obj); 360 preceding_live_words = region(split_region)->data_size() - overflowing_size; 361 } 362 363 split_info.record(split_region, overflowing_obj, preceding_live_words); 364 365 // The [overflowing_obj, src_region_start) part has been accounted for, so 366 // must move back the new_top, now that this overflowing obj is deferred. 367 HeapWord* new_top = destination - pointer_delta(region_to_addr(src_region), overflowing_obj); 368 369 // If the overflowing obj was relocated to its original destination, 370 // those destination regions would have their source_region set. Now that 371 // this overflowing obj is relocated somewhere else, reset the 372 // source_region. 373 { 374 size_t range_start = addr_to_region_idx(region_align_up(new_top)); 375 size_t range_end = addr_to_region_idx(region_align_up(destination)); 376 for (size_t i = range_start; i < range_end; ++i) { 377 region(i)->set_source_region(0); 378 } 379 } 380 381 // Update new top of target space 382 *target_next = new_top; 383 384 return overflowing_obj; 385 } 386 387 // Obj-iteration to locate the overflowing obj 388 HeapWord* region_start = region_to_addr(src_region); 389 HeapWord* region_end = region_start + RegionSize; 390 HeapWord* cur_addr = region_start + partial_obj_size; 391 size_t live_words = partial_obj_size; 392 393 while (true) { 394 assert(cur_addr < region_end, "inv"); 395 cur_addr = PSParallelCompact::mark_bitmap()->find_obj_beg(cur_addr, region_end); 396 // There must be an overflowing obj in this region 397 assert(cur_addr < region_end, "inv"); 398 399 oop obj = cast_to_oop(cur_addr); 400 size_t obj_size = obj->size(); 401 if (destination + live_words + obj_size > target_end) { 402 // Found the overflowing obj 403 split_info.record(src_region, cur_addr, live_words); 404 *target_next = destination + live_words; 405 return cur_addr; 406 } 407 408 live_words += obj_size; 409 cur_addr += obj_size; 410 } 411 } 412 413 size_t ParallelCompactData::live_words_in_space(const MutableSpace* space, 414 HeapWord** full_region_prefix_end) { 415 size_t cur_region = addr_to_region_idx(space->bottom()); 416 const size_t end_region = addr_to_region_idx(region_align_up(space->top())); 417 size_t live_words = 0; 418 if (full_region_prefix_end == nullptr) { 419 for (/* empty */; cur_region < end_region; ++cur_region) { 420 live_words += _region_data[cur_region].data_size(); 421 } 422 } else { 423 bool first_set = false; 424 for (/* empty */; cur_region < end_region; ++cur_region) { 425 size_t live_words_in_region = _region_data[cur_region].data_size(); 426 if (!first_set && live_words_in_region < RegionSize) { 427 *full_region_prefix_end = region_to_addr(cur_region); 428 first_set = true; 429 } 430 live_words += live_words_in_region; 431 } 432 if (!first_set) { 433 // All regions are full of live objs. 434 assert(is_region_aligned(space->top()), "inv"); 435 *full_region_prefix_end = space->top(); 436 } 437 assert(*full_region_prefix_end != nullptr, "postcondition"); 438 assert(is_region_aligned(*full_region_prefix_end), "inv"); 439 assert(*full_region_prefix_end >= space->bottom(), "in-range"); 440 assert(*full_region_prefix_end <= space->top(), "in-range"); 441 } 442 return live_words; 443 } 444 445 bool ParallelCompactData::summarize(SplitInfo& split_info, 446 HeapWord* source_beg, HeapWord* source_end, 447 HeapWord** source_next, 448 HeapWord* target_beg, HeapWord* target_end, 449 HeapWord** target_next) 450 { 451 HeapWord* const source_next_val = source_next == nullptr ? nullptr : *source_next; 452 log_develop_trace(gc, compaction)( 453 "sb=" PTR_FORMAT " se=" PTR_FORMAT " sn=" PTR_FORMAT 454 "tb=" PTR_FORMAT " te=" PTR_FORMAT " tn=" PTR_FORMAT, 455 p2i(source_beg), p2i(source_end), p2i(source_next_val), 456 p2i(target_beg), p2i(target_end), p2i(*target_next)); 457 458 size_t cur_region = addr_to_region_idx(source_beg); 459 const size_t end_region = addr_to_region_idx(region_align_up(source_end)); 460 461 HeapWord *dest_addr = target_beg; 462 for (/* empty */; cur_region < end_region; cur_region++) { 463 size_t words = _region_data[cur_region].data_size(); 464 465 // Skip empty ones 466 if (words == 0) { 467 continue; 468 } 469 470 if (split_info.is_split(cur_region)) { 471 assert(words > split_info.preceding_live_words(), "inv"); 472 words -= split_info.preceding_live_words(); 473 } 474 475 _region_data[cur_region].set_destination(dest_addr); 476 477 // If cur_region does not fit entirely into the target space, find a point 478 // at which the source space can be 'split' so that part is copied to the 479 // target space and the rest is copied elsewhere. 480 if (dest_addr + words > target_end) { 481 assert(source_next != nullptr, "source_next is null when splitting"); 482 *source_next = summarize_split_space(cur_region, split_info, dest_addr, 483 target_end, target_next); 484 return false; 485 } 486 487 uint destination_count = split_info.is_split(cur_region) 488 ? split_info.preceding_destination_count() 489 : 0; 490 491 HeapWord* const last_addr = dest_addr + words - 1; 492 const size_t dest_region_1 = addr_to_region_idx(dest_addr); 493 const size_t dest_region_2 = addr_to_region_idx(last_addr); 494 495 // Initially assume that the destination regions will be the same and 496 // adjust the value below if necessary. Under this assumption, if 497 // cur_region == dest_region_2, then cur_region will be compacted 498 // completely into itself. 499 destination_count += cur_region == dest_region_2 ? 0 : 1; 500 if (dest_region_1 != dest_region_2) { 501 // Destination regions differ; adjust destination_count. 502 destination_count += 1; 503 // Data from cur_region will be copied to the start of dest_region_2. 504 _region_data[dest_region_2].set_source_region(cur_region); 505 } else if (is_region_aligned(dest_addr)) { 506 // Data from cur_region will be copied to the start of the destination 507 // region. 508 _region_data[dest_region_1].set_source_region(cur_region); 509 } 510 511 _region_data[cur_region].set_destination_count(destination_count); 512 dest_addr += words; 513 } 514 515 *target_next = dest_addr; 516 return true; 517 } 518 519 #ifdef ASSERT 520 void ParallelCompactData::verify_clear() { 521 for (uint cur_idx = 0; cur_idx < region_count(); ++cur_idx) { 522 if (!region(cur_idx)->is_clear()) { 523 log_warning(gc)("Uncleared Region: %u", cur_idx); 524 region(cur_idx)->verify_clear(); 525 } 526 } 527 } 528 #endif // #ifdef ASSERT 529 530 STWGCTimer PSParallelCompact::_gc_timer; 531 ParallelOldTracer PSParallelCompact::_gc_tracer; 532 elapsedTimer PSParallelCompact::_accumulated_time; 533 unsigned int PSParallelCompact::_maximum_compaction_gc_num = 0; 534 CollectorCounters* PSParallelCompact::_counters = nullptr; 535 ParMarkBitMap PSParallelCompact::_mark_bitmap; 536 ParallelCompactData PSParallelCompact::_summary_data; 537 538 PSParallelCompact::IsAliveClosure PSParallelCompact::_is_alive_closure; 539 540 class PCAdjustPointerClosure: public BasicOopIterateClosure { 541 template <typename T> 542 void do_oop_work(T* p) { PSParallelCompact::adjust_pointer(p); } 543 544 public: 545 virtual void do_oop(oop* p) { do_oop_work(p); } 546 virtual void do_oop(narrowOop* p) { do_oop_work(p); } 547 548 virtual ReferenceIterationMode reference_iteration_mode() { return DO_FIELDS; } 549 }; 550 551 static PCAdjustPointerClosure pc_adjust_pointer_closure; 552 553 bool PSParallelCompact::IsAliveClosure::do_object_b(oop p) { return mark_bitmap()->is_marked(p); } 554 555 void PSParallelCompact::post_initialize() { 556 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); 557 _span_based_discoverer.set_span(heap->reserved_region()); 558 _ref_processor = 559 new ReferenceProcessor(&_span_based_discoverer, 560 ParallelGCThreads, // mt processing degree 561 ParallelGCThreads, // mt discovery degree 562 false, // concurrent_discovery 563 &_is_alive_closure); // non-header is alive closure 564 565 _counters = new CollectorCounters("Parallel full collection pauses", 1); 566 567 // Initialize static fields in ParCompactionManager. 568 ParCompactionManager::initialize(mark_bitmap()); 569 } 570 571 bool PSParallelCompact::initialize_aux_data() { 572 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); 573 MemRegion mr = heap->reserved_region(); 574 assert(mr.byte_size() != 0, "heap should be reserved"); 575 576 initialize_space_info(); 577 578 if (!_mark_bitmap.initialize(mr)) { 579 vm_shutdown_during_initialization( 580 err_msg("Unable to allocate " SIZE_FORMAT "KB bitmaps for parallel " 581 "garbage collection for the requested " SIZE_FORMAT "KB heap.", 582 _mark_bitmap.reserved_byte_size()/K, mr.byte_size()/K)); 583 return false; 584 } 585 586 if (!_summary_data.initialize(mr)) { 587 vm_shutdown_during_initialization( 588 err_msg("Unable to allocate " SIZE_FORMAT "KB card tables for parallel " 589 "garbage collection for the requested " SIZE_FORMAT "KB heap.", 590 _summary_data.reserved_byte_size()/K, mr.byte_size()/K)); 591 return false; 592 } 593 594 return true; 595 } 596 597 void PSParallelCompact::initialize_space_info() 598 { 599 memset(&_space_info, 0, sizeof(_space_info)); 600 601 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); 602 PSYoungGen* young_gen = heap->young_gen(); 603 604 _space_info[old_space_id].set_space(heap->old_gen()->object_space()); 605 _space_info[eden_space_id].set_space(young_gen->eden_space()); 606 _space_info[from_space_id].set_space(young_gen->from_space()); 607 _space_info[to_space_id].set_space(young_gen->to_space()); 608 609 _space_info[old_space_id].set_start_array(heap->old_gen()->start_array()); 610 } 611 612 void 613 PSParallelCompact::clear_data_covering_space(SpaceId id) 614 { 615 // At this point, top is the value before GC, new_top() is the value that will 616 // be set at the end of GC. The marking bitmap is cleared to top; nothing 617 // should be marked above top. The summary data is cleared to the larger of 618 // top & new_top. 619 MutableSpace* const space = _space_info[id].space(); 620 HeapWord* const bot = space->bottom(); 621 HeapWord* const top = space->top(); 622 HeapWord* const max_top = MAX2(top, _space_info[id].new_top()); 623 624 _mark_bitmap.clear_range(bot, top); 625 626 const size_t beg_region = _summary_data.addr_to_region_idx(bot); 627 const size_t end_region = 628 _summary_data.addr_to_region_idx(_summary_data.region_align_up(max_top)); 629 _summary_data.clear_range(beg_region, end_region); 630 631 // Clear the data used to 'split' regions. 632 SplitInfo& split_info = _space_info[id].split_info(); 633 if (split_info.is_valid()) { 634 split_info.clear(); 635 } 636 DEBUG_ONLY(split_info.verify_clear();) 637 } 638 639 void PSParallelCompact::pre_compact() 640 { 641 // Update the from & to space pointers in space_info, since they are swapped 642 // at each young gen gc. Do the update unconditionally (even though a 643 // promotion failure does not swap spaces) because an unknown number of young 644 // collections will have swapped the spaces an unknown number of times. 645 GCTraceTime(Debug, gc, phases) tm("Pre Compact", &_gc_timer); 646 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); 647 _space_info[from_space_id].set_space(heap->young_gen()->from_space()); 648 _space_info[to_space_id].set_space(heap->young_gen()->to_space()); 649 650 // Increment the invocation count 651 heap->increment_total_collections(true); 652 653 CodeCache::on_gc_marking_cycle_start(); 654 655 heap->print_heap_before_gc(); 656 heap->trace_heap_before_gc(&_gc_tracer); 657 658 // Fill in TLABs 659 heap->ensure_parsability(true); // retire TLABs 660 661 if (VerifyBeforeGC && heap->total_collections() >= VerifyGCStartAt) { 662 Universe::verify("Before GC"); 663 } 664 665 DEBUG_ONLY(mark_bitmap()->verify_clear();) 666 DEBUG_ONLY(summary_data().verify_clear();) 667 } 668 669 void PSParallelCompact::post_compact() 670 { 671 GCTraceTime(Info, gc, phases) tm("Post Compact", &_gc_timer); 672 ParCompactionManager::remove_all_shadow_regions(); 673 674 CodeCache::on_gc_marking_cycle_finish(); 675 CodeCache::arm_all_nmethods(); 676 677 for (unsigned int id = old_space_id; id < last_space_id; ++id) { 678 // Clear the marking bitmap, summary data and split info. 679 clear_data_covering_space(SpaceId(id)); 680 { 681 MutableSpace* space = _space_info[id].space(); 682 HeapWord* top = space->top(); 683 HeapWord* new_top = _space_info[id].new_top(); 684 if (ZapUnusedHeapArea && new_top < top) { 685 space->mangle_region(MemRegion(new_top, top)); 686 } 687 // Update top(). Must be done after clearing the bitmap and summary data. 688 space->set_top(new_top); 689 } 690 } 691 692 #ifdef ASSERT 693 { 694 mark_bitmap()->verify_clear(); 695 summary_data().verify_clear(); 696 } 697 #endif 698 699 ParCompactionManager::flush_all_string_dedup_requests(); 700 701 MutableSpace* const eden_space = _space_info[eden_space_id].space(); 702 MutableSpace* const from_space = _space_info[from_space_id].space(); 703 MutableSpace* const to_space = _space_info[to_space_id].space(); 704 705 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); 706 bool eden_empty = eden_space->is_empty(); 707 708 // Update heap occupancy information which is used as input to the soft ref 709 // clearing policy at the next gc. 710 Universe::heap()->update_capacity_and_used_at_gc(); 711 712 bool young_gen_empty = eden_empty && from_space->is_empty() && 713 to_space->is_empty(); 714 715 PSCardTable* ct = heap->card_table(); 716 MemRegion old_mr = heap->old_gen()->committed(); 717 if (young_gen_empty) { 718 ct->clear_MemRegion(old_mr); 719 } else { 720 ct->dirty_MemRegion(old_mr); 721 } 722 723 { 724 // Delete metaspaces for unloaded class loaders and clean up loader_data graph 725 GCTraceTime(Debug, gc, phases) t("Purge Class Loader Data", gc_timer()); 726 ClassLoaderDataGraph::purge(true /* at_safepoint */); 727 DEBUG_ONLY(MetaspaceUtils::verify();) 728 } 729 730 // Need to clear claim bits for the next mark. 731 ClassLoaderDataGraph::clear_claimed_marks(); 732 733 heap->prune_scavengable_nmethods(); 734 735 #if COMPILER2_OR_JVMCI 736 DerivedPointerTable::update_pointers(); 737 #endif 738 739 // Signal that we have completed a visit to all live objects. 740 Universe::heap()->record_whole_heap_examined_timestamp(); 741 } 742 743 HeapWord* PSParallelCompact::compute_dense_prefix_for_old_space(MutableSpace* old_space, 744 HeapWord* full_region_prefix_end) { 745 const size_t region_size = ParallelCompactData::RegionSize; 746 const ParallelCompactData& sd = summary_data(); 747 748 // Iteration starts with the region *after* the full-region-prefix-end. 749 const RegionData* const start_region = sd.addr_to_region_ptr(full_region_prefix_end); 750 // If final region is not full, iteration stops before that region, 751 // because fill_dense_prefix_end assumes that prefix_end <= top. 752 const RegionData* const end_region = sd.addr_to_region_ptr(old_space->top()); 753 assert(start_region <= end_region, "inv"); 754 755 size_t max_waste = old_space->capacity_in_words() * (MarkSweepDeadRatio / 100.0); 756 const RegionData* cur_region = start_region; 757 for (/* empty */; cur_region < end_region; ++cur_region) { 758 assert(region_size >= cur_region->data_size(), "inv"); 759 size_t dead_size = region_size - cur_region->data_size(); 760 if (max_waste < dead_size) { 761 break; 762 } 763 max_waste -= dead_size; 764 } 765 766 HeapWord* const prefix_end = sd.region_to_addr(cur_region); 767 assert(sd.is_region_aligned(prefix_end), "postcondition"); 768 assert(prefix_end >= full_region_prefix_end, "in-range"); 769 assert(prefix_end <= old_space->top(), "in-range"); 770 return prefix_end; 771 } 772 773 void PSParallelCompact::fill_dense_prefix_end(SpaceId id) { 774 // Comparing two sizes to decide if filling is required: 775 // 776 // The size of the filler (min-obj-size) is 2 heap words with the default 777 // MinObjAlignment, since both markword and klass take 1 heap word. 778 // With +UseCompactObjectHeaders, the minimum filler size is only one word, 779 // because the Klass* gets encoded in the mark-word. 780 // 781 // The size of the gap (if any) right before dense-prefix-end is 782 // MinObjAlignment. 783 // 784 // Need to fill in the gap only if it's smaller than min-obj-size, and the 785 // filler obj will extend to next region. 786 787 if (MinObjAlignment >= checked_cast<int>(CollectedHeap::min_fill_size())) { 788 return; 789 } 790 791 assert(!UseCompactObjectHeaders, "Compact headers can allocate small objects"); 792 assert(CollectedHeap::min_fill_size() == 2, "inv"); 793 HeapWord* const dense_prefix_end = dense_prefix(id); 794 assert(_summary_data.is_region_aligned(dense_prefix_end), "precondition"); 795 assert(dense_prefix_end <= space(id)->top(), "precondition"); 796 if (dense_prefix_end == space(id)->top()) { 797 // Must not have single-word gap right before prefix-end/top. 798 return; 799 } 800 RegionData* const region_after_dense_prefix = _summary_data.addr_to_region_ptr(dense_prefix_end); 801 802 if (region_after_dense_prefix->partial_obj_size() != 0 || 803 _mark_bitmap.is_marked(dense_prefix_end)) { 804 // The region after the dense prefix starts with live bytes. 805 return; 806 } 807 808 HeapWord* block_start = start_array(id)->block_start_reaching_into_card(dense_prefix_end); 809 if (block_start == dense_prefix_end - 1) { 810 assert(!_mark_bitmap.is_marked(block_start), "inv"); 811 // There is exactly one heap word gap right before the dense prefix end, so we need a filler object. 812 // The filler object will extend into region_after_dense_prefix. 813 const size_t obj_len = 2; // min-fill-size 814 HeapWord* const obj_beg = dense_prefix_end - 1; 815 CollectedHeap::fill_with_object(obj_beg, obj_len); 816 _mark_bitmap.mark_obj(obj_beg); 817 _summary_data.addr_to_region_ptr(obj_beg)->add_live_obj(1); 818 region_after_dense_prefix->set_partial_obj_size(1); 819 region_after_dense_prefix->set_partial_obj_addr(obj_beg); 820 assert(start_array(id) != nullptr, "sanity"); 821 start_array(id)->update_for_block(obj_beg, obj_beg + obj_len); 822 } 823 } 824 825 bool PSParallelCompact::check_maximum_compaction(size_t total_live_words, 826 MutableSpace* const old_space, 827 HeapWord* full_region_prefix_end) { 828 829 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); 830 831 // Check System.GC 832 bool is_max_on_system_gc = UseMaximumCompactionOnSystemGC 833 && GCCause::is_user_requested_gc(heap->gc_cause()); 834 835 // Check if all live objs are larger than old-gen. 836 const bool is_old_gen_overflowing = (total_live_words > old_space->capacity_in_words()); 837 838 // JVM flags 839 const uint total_invocations = heap->total_full_collections(); 840 assert(total_invocations >= _maximum_compaction_gc_num, "sanity"); 841 const size_t gcs_since_max = total_invocations - _maximum_compaction_gc_num; 842 const bool is_interval_ended = gcs_since_max > HeapMaximumCompactionInterval; 843 844 // If all regions in old-gen are full 845 const bool is_region_full = 846 full_region_prefix_end >= _summary_data.region_align_down(old_space->top()); 847 848 if (is_max_on_system_gc || is_old_gen_overflowing || is_interval_ended || is_region_full) { 849 _maximum_compaction_gc_num = total_invocations; 850 return true; 851 } 852 853 return false; 854 } 855 856 void PSParallelCompact::summary_phase() 857 { 858 GCTraceTime(Info, gc, phases) tm("Summary Phase", &_gc_timer); 859 860 MutableSpace* const old_space = _space_info[old_space_id].space(); 861 { 862 size_t total_live_words = 0; 863 HeapWord* full_region_prefix_end = nullptr; 864 { 865 // old-gen 866 size_t live_words = _summary_data.live_words_in_space(old_space, 867 &full_region_prefix_end); 868 total_live_words += live_words; 869 } 870 // young-gen 871 for (uint i = eden_space_id; i < last_space_id; ++i) { 872 const MutableSpace* space = _space_info[i].space(); 873 size_t live_words = _summary_data.live_words_in_space(space); 874 total_live_words += live_words; 875 _space_info[i].set_new_top(space->bottom() + live_words); 876 _space_info[i].set_dense_prefix(space->bottom()); 877 } 878 879 bool maximum_compaction = check_maximum_compaction(total_live_words, 880 old_space, 881 full_region_prefix_end); 882 HeapWord* dense_prefix_end = maximum_compaction 883 ? full_region_prefix_end 884 : compute_dense_prefix_for_old_space(old_space, 885 full_region_prefix_end); 886 SpaceId id = old_space_id; 887 _space_info[id].set_dense_prefix(dense_prefix_end); 888 889 if (dense_prefix_end != old_space->bottom()) { 890 fill_dense_prefix_end(id); 891 _summary_data.summarize_dense_prefix(old_space->bottom(), dense_prefix_end); 892 } 893 894 // Compacting objs in [dense_prefix_end, old_space->top()) 895 _summary_data.summarize(_space_info[id].split_info(), 896 dense_prefix_end, old_space->top(), nullptr, 897 dense_prefix_end, old_space->end(), 898 _space_info[id].new_top_addr()); 899 } 900 901 // Summarize the remaining spaces in the young gen. The initial target space 902 // is the old gen. If a space does not fit entirely into the target, then the 903 // remainder is compacted into the space itself and that space becomes the new 904 // target. 905 SpaceId dst_space_id = old_space_id; 906 HeapWord* dst_space_end = old_space->end(); 907 HeapWord** new_top_addr = _space_info[dst_space_id].new_top_addr(); 908 for (unsigned int id = eden_space_id; id < last_space_id; ++id) { 909 const MutableSpace* space = _space_info[id].space(); 910 const size_t live = pointer_delta(_space_info[id].new_top(), 911 space->bottom()); 912 const size_t available = pointer_delta(dst_space_end, *new_top_addr); 913 914 if (live > 0 && live <= available) { 915 // All the live data will fit. 916 bool done = _summary_data.summarize(_space_info[id].split_info(), 917 space->bottom(), space->top(), 918 nullptr, 919 *new_top_addr, dst_space_end, 920 new_top_addr); 921 assert(done, "space must fit into old gen"); 922 923 // Reset the new_top value for the space. 924 _space_info[id].set_new_top(space->bottom()); 925 } else if (live > 0) { 926 // Attempt to fit part of the source space into the target space. 927 HeapWord* next_src_addr = nullptr; 928 bool done = _summary_data.summarize(_space_info[id].split_info(), 929 space->bottom(), space->top(), 930 &next_src_addr, 931 *new_top_addr, dst_space_end, 932 new_top_addr); 933 assert(!done, "space should not fit into old gen"); 934 assert(next_src_addr != nullptr, "sanity"); 935 936 // The source space becomes the new target, so the remainder is compacted 937 // within the space itself. 938 dst_space_id = SpaceId(id); 939 dst_space_end = space->end(); 940 new_top_addr = _space_info[id].new_top_addr(); 941 done = _summary_data.summarize(_space_info[id].split_info(), 942 next_src_addr, space->top(), 943 nullptr, 944 space->bottom(), dst_space_end, 945 new_top_addr); 946 assert(done, "space must fit when compacted into itself"); 947 assert(*new_top_addr <= space->top(), "usage should not grow"); 948 } 949 } 950 } 951 952 // This method should contain all heap-specific policy for invoking a full 953 // collection. invoke_no_policy() will only attempt to compact the heap; it 954 // will do nothing further. If we need to bail out for policy reasons, scavenge 955 // before full gc, or any other specialized behavior, it needs to be added here. 956 // 957 // Note that this method should only be called from the vm_thread while at a 958 // safepoint. 959 // 960 // Note that the all_soft_refs_clear flag in the soft ref policy 961 // may be true because this method can be called without intervening 962 // activity. For example when the heap space is tight and full measure 963 // are being taken to free space. 964 bool PSParallelCompact::invoke(bool clear_all_soft_refs) { 965 assert(SafepointSynchronize::is_at_safepoint(), "should be at safepoint"); 966 assert(Thread::current() == (Thread*)VMThread::vm_thread(), 967 "should be in vm thread"); 968 969 SvcGCMarker sgcm(SvcGCMarker::FULL); 970 IsSTWGCActiveMark mark; 971 972 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); 973 clear_all_soft_refs = clear_all_soft_refs 974 || heap->soft_ref_policy()->should_clear_all_soft_refs(); 975 976 return PSParallelCompact::invoke_no_policy(clear_all_soft_refs); 977 } 978 979 // This method contains no policy. You should probably 980 // be calling invoke() instead. 981 bool PSParallelCompact::invoke_no_policy(bool clear_all_soft_refs) { 982 assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint"); 983 assert(ref_processor() != nullptr, "Sanity"); 984 985 if (GCLocker::check_active_before_gc()) { 986 return false; 987 } 988 989 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); 990 991 GCIdMark gc_id_mark; 992 _gc_timer.register_gc_start(); 993 _gc_tracer.report_gc_start(heap->gc_cause(), _gc_timer.gc_start()); 994 995 GCCause::Cause gc_cause = heap->gc_cause(); 996 PSYoungGen* young_gen = heap->young_gen(); 997 PSOldGen* old_gen = heap->old_gen(); 998 PSAdaptiveSizePolicy* size_policy = heap->size_policy(); 999 1000 // The scope of casr should end after code that can change 1001 // SoftRefPolicy::_should_clear_all_soft_refs. 1002 ClearedAllSoftRefs casr(clear_all_soft_refs, 1003 heap->soft_ref_policy()); 1004 1005 // Make sure data structures are sane, make the heap parsable, and do other 1006 // miscellaneous bookkeeping. 1007 pre_compact(); 1008 1009 const PreGenGCValues pre_gc_values = heap->get_pre_gc_values(); 1010 1011 { 1012 const uint active_workers = 1013 WorkerPolicy::calc_active_workers(ParallelScavengeHeap::heap()->workers().max_workers(), 1014 ParallelScavengeHeap::heap()->workers().active_workers(), 1015 Threads::number_of_non_daemon_threads()); 1016 ParallelScavengeHeap::heap()->workers().set_active_workers(active_workers); 1017 1018 GCTraceCPUTime tcpu(&_gc_tracer); 1019 GCTraceTime(Info, gc) tm("Pause Full", nullptr, gc_cause, true); 1020 1021 heap->pre_full_gc_dump(&_gc_timer); 1022 1023 TraceCollectorStats tcs(counters()); 1024 TraceMemoryManagerStats tms(heap->old_gc_manager(), gc_cause, "end of major GC"); 1025 1026 if (log_is_enabled(Debug, gc, heap, exit)) { 1027 accumulated_time()->start(); 1028 } 1029 1030 // Let the size policy know we're starting 1031 size_policy->major_collection_begin(); 1032 1033 #if COMPILER2_OR_JVMCI 1034 DerivedPointerTable::clear(); 1035 #endif 1036 1037 ref_processor()->start_discovery(clear_all_soft_refs); 1038 1039 ClassUnloadingContext ctx(1 /* num_nmethod_unlink_workers */, 1040 false /* unregister_nmethods_during_purge */, 1041 false /* lock_nmethod_free_separately */); 1042 1043 marking_phase(&_gc_tracer); 1044 1045 summary_phase(); 1046 1047 #if COMPILER2_OR_JVMCI 1048 assert(DerivedPointerTable::is_active(), "Sanity"); 1049 DerivedPointerTable::set_active(false); 1050 #endif 1051 1052 forward_to_new_addr(); 1053 1054 adjust_pointers(); 1055 1056 compact(); 1057 1058 ParCompactionManager::_preserved_marks_set->restore(&ParallelScavengeHeap::heap()->workers()); 1059 1060 ParCompactionManager::verify_all_region_stack_empty(); 1061 1062 // Reset the mark bitmap, summary data, and do other bookkeeping. Must be 1063 // done before resizing. 1064 post_compact(); 1065 1066 // Let the size policy know we're done 1067 size_policy->major_collection_end(old_gen->used_in_bytes(), gc_cause); 1068 1069 if (UseAdaptiveSizePolicy) { 1070 log_debug(gc, ergo)("AdaptiveSizeStart: collection: %d ", heap->total_collections()); 1071 log_trace(gc, ergo)("old_gen_capacity: " SIZE_FORMAT " young_gen_capacity: " SIZE_FORMAT, 1072 old_gen->capacity_in_bytes(), young_gen->capacity_in_bytes()); 1073 1074 // Don't check if the size_policy is ready here. Let 1075 // the size_policy check that internally. 1076 if (UseAdaptiveGenerationSizePolicyAtMajorCollection && 1077 AdaptiveSizePolicy::should_update_promo_stats(gc_cause)) { 1078 // Swap the survivor spaces if from_space is empty. The 1079 // resize_young_gen() called below is normally used after 1080 // a successful young GC and swapping of survivor spaces; 1081 // otherwise, it will fail to resize the young gen with 1082 // the current implementation. 1083 if (young_gen->from_space()->is_empty()) { 1084 young_gen->from_space()->clear(SpaceDecorator::Mangle); 1085 young_gen->swap_spaces(); 1086 } 1087 1088 // Calculate optimal free space amounts 1089 assert(young_gen->max_gen_size() > 1090 young_gen->from_space()->capacity_in_bytes() + 1091 young_gen->to_space()->capacity_in_bytes(), 1092 "Sizes of space in young gen are out-of-bounds"); 1093 1094 size_t young_live = young_gen->used_in_bytes(); 1095 size_t eden_live = young_gen->eden_space()->used_in_bytes(); 1096 size_t old_live = old_gen->used_in_bytes(); 1097 size_t cur_eden = young_gen->eden_space()->capacity_in_bytes(); 1098 size_t max_old_gen_size = old_gen->max_gen_size(); 1099 size_t max_eden_size = young_gen->max_gen_size() - 1100 young_gen->from_space()->capacity_in_bytes() - 1101 young_gen->to_space()->capacity_in_bytes(); 1102 1103 // Used for diagnostics 1104 size_policy->clear_generation_free_space_flags(); 1105 1106 size_policy->compute_generations_free_space(young_live, 1107 eden_live, 1108 old_live, 1109 cur_eden, 1110 max_old_gen_size, 1111 max_eden_size, 1112 true /* full gc*/); 1113 1114 size_policy->check_gc_overhead_limit(eden_live, 1115 max_old_gen_size, 1116 max_eden_size, 1117 true /* full gc*/, 1118 gc_cause, 1119 heap->soft_ref_policy()); 1120 1121 size_policy->decay_supplemental_growth(true /* full gc*/); 1122 1123 heap->resize_old_gen( 1124 size_policy->calculated_old_free_size_in_bytes()); 1125 1126 heap->resize_young_gen(size_policy->calculated_eden_size_in_bytes(), 1127 size_policy->calculated_survivor_size_in_bytes()); 1128 } 1129 1130 log_debug(gc, ergo)("AdaptiveSizeStop: collection: %d ", heap->total_collections()); 1131 } 1132 1133 if (UsePerfData) { 1134 PSGCAdaptivePolicyCounters* const counters = heap->gc_policy_counters(); 1135 counters->update_counters(); 1136 counters->update_old_capacity(old_gen->capacity_in_bytes()); 1137 counters->update_young_capacity(young_gen->capacity_in_bytes()); 1138 } 1139 1140 heap->resize_all_tlabs(); 1141 1142 // Resize the metaspace capacity after a collection 1143 MetaspaceGC::compute_new_size(); 1144 1145 if (log_is_enabled(Debug, gc, heap, exit)) { 1146 accumulated_time()->stop(); 1147 } 1148 1149 heap->print_heap_change(pre_gc_values); 1150 1151 // Track memory usage and detect low memory 1152 MemoryService::track_memory_usage(); 1153 heap->update_counters(); 1154 1155 heap->post_full_gc_dump(&_gc_timer); 1156 } 1157 1158 if (VerifyAfterGC && heap->total_collections() >= VerifyGCStartAt) { 1159 Universe::verify("After GC"); 1160 } 1161 1162 heap->print_heap_after_gc(); 1163 heap->trace_heap_after_gc(&_gc_tracer); 1164 1165 AdaptiveSizePolicyOutput::print(size_policy, heap->total_collections()); 1166 1167 _gc_timer.register_gc_end(); 1168 1169 _gc_tracer.report_dense_prefix(dense_prefix(old_space_id)); 1170 _gc_tracer.report_gc_end(_gc_timer.gc_end(), _gc_timer.time_partitions()); 1171 1172 return true; 1173 } 1174 1175 class PCAddThreadRootsMarkingTaskClosure : public ThreadClosure { 1176 private: 1177 uint _worker_id; 1178 1179 public: 1180 PCAddThreadRootsMarkingTaskClosure(uint worker_id) : _worker_id(worker_id) { } 1181 void do_thread(Thread* thread) { 1182 assert(ParallelScavengeHeap::heap()->is_stw_gc_active(), "called outside gc"); 1183 1184 ResourceMark rm; 1185 1186 ParCompactionManager* cm = ParCompactionManager::gc_thread_compaction_manager(_worker_id); 1187 1188 MarkingNMethodClosure mark_and_push_in_blobs(&cm->_mark_and_push_closure, 1189 !NMethodToOopClosure::FixRelocations, 1190 true /* keepalive nmethods */); 1191 1192 thread->oops_do(&cm->_mark_and_push_closure, &mark_and_push_in_blobs); 1193 1194 // Do the real work 1195 cm->follow_marking_stacks(); 1196 } 1197 }; 1198 1199 void steal_marking_work(TaskTerminator& terminator, uint worker_id) { 1200 assert(ParallelScavengeHeap::heap()->is_stw_gc_active(), "called outside gc"); 1201 1202 ParCompactionManager* cm = 1203 ParCompactionManager::gc_thread_compaction_manager(worker_id); 1204 1205 do { 1206 oop obj = nullptr; 1207 ObjArrayTask task; 1208 if (ParCompactionManager::steal_objarray(worker_id, task)) { 1209 cm->follow_array((objArrayOop)task.obj(), task.index()); 1210 } else if (ParCompactionManager::steal(worker_id, obj)) { 1211 cm->follow_contents(obj); 1212 } 1213 cm->follow_marking_stacks(); 1214 } while (!terminator.offer_termination()); 1215 } 1216 1217 class MarkFromRootsTask : public WorkerTask { 1218 StrongRootsScope _strong_roots_scope; // needed for Threads::possibly_parallel_threads_do 1219 OopStorageSetStrongParState<false /* concurrent */, false /* is_const */> _oop_storage_set_par_state; 1220 TaskTerminator _terminator; 1221 uint _active_workers; 1222 1223 public: 1224 MarkFromRootsTask(uint active_workers) : 1225 WorkerTask("MarkFromRootsTask"), 1226 _strong_roots_scope(active_workers), 1227 _terminator(active_workers, ParCompactionManager::oop_task_queues()), 1228 _active_workers(active_workers) {} 1229 1230 virtual void work(uint worker_id) { 1231 ParCompactionManager* cm = ParCompactionManager::gc_thread_compaction_manager(worker_id); 1232 cm->create_marking_stats_cache(); 1233 { 1234 CLDToOopClosure cld_closure(&cm->_mark_and_push_closure, ClassLoaderData::_claim_stw_fullgc_mark); 1235 ClassLoaderDataGraph::always_strong_cld_do(&cld_closure); 1236 1237 // Do the real work 1238 cm->follow_marking_stacks(); 1239 } 1240 1241 { 1242 PCAddThreadRootsMarkingTaskClosure closure(worker_id); 1243 Threads::possibly_parallel_threads_do(_active_workers > 1 /* is_par */, &closure); 1244 } 1245 1246 // Mark from OopStorages 1247 { 1248 _oop_storage_set_par_state.oops_do(&cm->_mark_and_push_closure); 1249 // Do the real work 1250 cm->follow_marking_stacks(); 1251 } 1252 1253 if (_active_workers > 1) { 1254 steal_marking_work(_terminator, worker_id); 1255 } 1256 } 1257 }; 1258 1259 class ParallelCompactRefProcProxyTask : public RefProcProxyTask { 1260 TaskTerminator _terminator; 1261 1262 public: 1263 ParallelCompactRefProcProxyTask(uint max_workers) 1264 : RefProcProxyTask("ParallelCompactRefProcProxyTask", max_workers), 1265 _terminator(_max_workers, ParCompactionManager::oop_task_queues()) {} 1266 1267 void work(uint worker_id) override { 1268 assert(worker_id < _max_workers, "sanity"); 1269 ParCompactionManager* cm = (_tm == RefProcThreadModel::Single) ? ParCompactionManager::get_vmthread_cm() : ParCompactionManager::gc_thread_compaction_manager(worker_id); 1270 BarrierEnqueueDiscoveredFieldClosure enqueue; 1271 ParCompactionManager::FollowStackClosure complete_gc(cm, (_tm == RefProcThreadModel::Single) ? nullptr : &_terminator, worker_id); 1272 _rp_task->rp_work(worker_id, PSParallelCompact::is_alive_closure(), &cm->_mark_and_push_closure, &enqueue, &complete_gc); 1273 } 1274 1275 void prepare_run_task_hook() override { 1276 _terminator.reset_for_reuse(_queue_count); 1277 } 1278 }; 1279 1280 static void flush_marking_stats_cache(const uint num_workers) { 1281 for (uint i = 0; i < num_workers; ++i) { 1282 ParCompactionManager* cm = ParCompactionManager::gc_thread_compaction_manager(i); 1283 cm->flush_and_destroy_marking_stats_cache(); 1284 } 1285 } 1286 1287 void PSParallelCompact::marking_phase(ParallelOldTracer *gc_tracer) { 1288 // Recursively traverse all live objects and mark them 1289 GCTraceTime(Info, gc, phases) tm("Marking Phase", &_gc_timer); 1290 1291 uint active_gc_threads = ParallelScavengeHeap::heap()->workers().active_workers(); 1292 1293 ClassLoaderDataGraph::verify_claimed_marks_cleared(ClassLoaderData::_claim_stw_fullgc_mark); 1294 { 1295 GCTraceTime(Debug, gc, phases) tm("Par Mark", &_gc_timer); 1296 1297 MarkFromRootsTask task(active_gc_threads); 1298 ParallelScavengeHeap::heap()->workers().run_task(&task); 1299 } 1300 1301 // Process reference objects found during marking 1302 { 1303 GCTraceTime(Debug, gc, phases) tm("Reference Processing", &_gc_timer); 1304 1305 ReferenceProcessorStats stats; 1306 ReferenceProcessorPhaseTimes pt(&_gc_timer, ref_processor()->max_num_queues()); 1307 1308 ref_processor()->set_active_mt_degree(active_gc_threads); 1309 ParallelCompactRefProcProxyTask task(ref_processor()->max_num_queues()); 1310 stats = ref_processor()->process_discovered_references(task, pt); 1311 1312 gc_tracer->report_gc_reference_stats(stats); 1313 pt.print_all_references(); 1314 } 1315 1316 { 1317 GCTraceTime(Debug, gc, phases) tm("Flush Marking Stats", &_gc_timer); 1318 1319 flush_marking_stats_cache(active_gc_threads); 1320 } 1321 1322 // This is the point where the entire marking should have completed. 1323 ParCompactionManager::verify_all_marking_stack_empty(); 1324 1325 { 1326 GCTraceTime(Debug, gc, phases) tm("Weak Processing", &_gc_timer); 1327 WeakProcessor::weak_oops_do(&ParallelScavengeHeap::heap()->workers(), 1328 is_alive_closure(), 1329 &do_nothing_cl, 1330 1); 1331 } 1332 1333 { 1334 GCTraceTime(Debug, gc, phases) tm_m("Class Unloading", &_gc_timer); 1335 1336 ClassUnloadingContext* ctx = ClassUnloadingContext::context(); 1337 1338 bool unloading_occurred; 1339 { 1340 CodeCache::UnlinkingScope scope(is_alive_closure()); 1341 1342 // Follow system dictionary roots and unload classes. 1343 unloading_occurred = SystemDictionary::do_unloading(&_gc_timer); 1344 1345 // Unload nmethods. 1346 CodeCache::do_unloading(unloading_occurred); 1347 } 1348 1349 { 1350 GCTraceTime(Debug, gc, phases) t("Purge Unlinked NMethods", gc_timer()); 1351 // Release unloaded nmethod's memory. 1352 ctx->purge_nmethods(); 1353 } 1354 { 1355 GCTraceTime(Debug, gc, phases) ur("Unregister NMethods", &_gc_timer); 1356 ParallelScavengeHeap::heap()->prune_unlinked_nmethods(); 1357 } 1358 { 1359 GCTraceTime(Debug, gc, phases) t("Free Code Blobs", gc_timer()); 1360 ctx->free_nmethods(); 1361 } 1362 1363 // Prune dead klasses from subklass/sibling/implementor lists. 1364 Klass::clean_weak_klass_links(unloading_occurred); 1365 1366 // Clean JVMCI metadata handles. 1367 JVMCI_ONLY(JVMCI::do_unloading(unloading_occurred)); 1368 } 1369 1370 { 1371 GCTraceTime(Debug, gc, phases) tm("Report Object Count", &_gc_timer); 1372 _gc_tracer.report_object_count_after_gc(is_alive_closure(), &ParallelScavengeHeap::heap()->workers()); 1373 } 1374 #if TASKQUEUE_STATS 1375 ParCompactionManager::oop_task_queues()->print_and_reset_taskqueue_stats("Oop Queue"); 1376 ParCompactionManager::_objarray_task_queues->print_and_reset_taskqueue_stats("ObjArrayOop Queue"); 1377 #endif 1378 } 1379 1380 template<typename Func> 1381 void PSParallelCompact::adjust_in_space_helper(SpaceId id, volatile uint* claim_counter, Func&& on_stripe) { 1382 MutableSpace* sp = PSParallelCompact::space(id); 1383 HeapWord* const bottom = sp->bottom(); 1384 HeapWord* const top = sp->top(); 1385 if (bottom == top) { 1386 return; 1387 } 1388 1389 const uint num_regions_per_stripe = 2; 1390 const size_t region_size = ParallelCompactData::RegionSize; 1391 const size_t stripe_size = num_regions_per_stripe * region_size; 1392 1393 while (true) { 1394 uint counter = Atomic::fetch_then_add(claim_counter, num_regions_per_stripe); 1395 HeapWord* cur_stripe = bottom + counter * region_size; 1396 if (cur_stripe >= top) { 1397 break; 1398 } 1399 HeapWord* stripe_end = MIN2(cur_stripe + stripe_size, top); 1400 on_stripe(cur_stripe, stripe_end); 1401 } 1402 } 1403 1404 void PSParallelCompact::adjust_in_old_space(volatile uint* claim_counter) { 1405 // Regions in old-space shouldn't be split. 1406 assert(!_space_info[old_space_id].split_info().is_valid(), "inv"); 1407 1408 auto scan_obj_with_limit = [&] (HeapWord* obj_start, HeapWord* left, HeapWord* right) { 1409 assert(mark_bitmap()->is_marked(obj_start), "inv"); 1410 oop obj = cast_to_oop(obj_start); 1411 return obj->oop_iterate_size(&pc_adjust_pointer_closure, MemRegion(left, right)); 1412 }; 1413 1414 adjust_in_space_helper(old_space_id, claim_counter, [&] (HeapWord* stripe_start, HeapWord* stripe_end) { 1415 assert(_summary_data.is_region_aligned(stripe_start), "inv"); 1416 RegionData* cur_region = _summary_data.addr_to_region_ptr(stripe_start); 1417 HeapWord* obj_start; 1418 if (cur_region->partial_obj_size() != 0) { 1419 obj_start = cur_region->partial_obj_addr(); 1420 obj_start += scan_obj_with_limit(obj_start, stripe_start, stripe_end); 1421 } else { 1422 obj_start = stripe_start; 1423 } 1424 1425 while (obj_start < stripe_end) { 1426 obj_start = mark_bitmap()->find_obj_beg(obj_start, stripe_end); 1427 if (obj_start >= stripe_end) { 1428 break; 1429 } 1430 obj_start += scan_obj_with_limit(obj_start, stripe_start, stripe_end); 1431 } 1432 }); 1433 } 1434 1435 void PSParallelCompact::adjust_in_young_space(SpaceId id, volatile uint* claim_counter) { 1436 adjust_in_space_helper(id, claim_counter, [](HeapWord* stripe_start, HeapWord* stripe_end) { 1437 HeapWord* obj_start = stripe_start; 1438 while (obj_start < stripe_end) { 1439 obj_start = mark_bitmap()->find_obj_beg(obj_start, stripe_end); 1440 if (obj_start >= stripe_end) { 1441 break; 1442 } 1443 oop obj = cast_to_oop(obj_start); 1444 obj_start += obj->oop_iterate_size(&pc_adjust_pointer_closure); 1445 } 1446 }); 1447 } 1448 1449 void PSParallelCompact::adjust_pointers_in_spaces(uint worker_id, volatile uint* claim_counters) { 1450 auto start_time = Ticks::now(); 1451 adjust_in_old_space(&claim_counters[0]); 1452 for (uint id = eden_space_id; id < last_space_id; ++id) { 1453 adjust_in_young_space(SpaceId(id), &claim_counters[id]); 1454 } 1455 log_trace(gc, phases)("adjust_pointers_in_spaces worker %u: %.3f ms", worker_id, (Ticks::now() - start_time).seconds() * 1000); 1456 } 1457 1458 class PSAdjustTask final : public WorkerTask { 1459 SubTasksDone _sub_tasks; 1460 WeakProcessor::Task _weak_proc_task; 1461 OopStorageSetStrongParState<false, false> _oop_storage_iter; 1462 uint _nworkers; 1463 volatile uint _claim_counters[PSParallelCompact::last_space_id] = {}; 1464 1465 enum PSAdjustSubTask { 1466 PSAdjustSubTask_code_cache, 1467 1468 PSAdjustSubTask_num_elements 1469 }; 1470 1471 public: 1472 PSAdjustTask(uint nworkers) : 1473 WorkerTask("PSAdjust task"), 1474 _sub_tasks(PSAdjustSubTask_num_elements), 1475 _weak_proc_task(nworkers), 1476 _nworkers(nworkers) { 1477 1478 ClassLoaderDataGraph::verify_claimed_marks_cleared(ClassLoaderData::_claim_stw_fullgc_adjust); 1479 if (nworkers > 1) { 1480 Threads::change_thread_claim_token(); 1481 } 1482 } 1483 1484 ~PSAdjustTask() { 1485 Threads::assert_all_threads_claimed(); 1486 } 1487 1488 void work(uint worker_id) { 1489 ParCompactionManager* cm = ParCompactionManager::gc_thread_compaction_manager(worker_id); 1490 cm->preserved_marks()->adjust_during_full_gc(); 1491 { 1492 // adjust pointers in all spaces 1493 PSParallelCompact::adjust_pointers_in_spaces(worker_id, _claim_counters); 1494 } 1495 { 1496 ResourceMark rm; 1497 Threads::possibly_parallel_oops_do(_nworkers > 1, &pc_adjust_pointer_closure, nullptr); 1498 } 1499 _oop_storage_iter.oops_do(&pc_adjust_pointer_closure); 1500 { 1501 CLDToOopClosure cld_closure(&pc_adjust_pointer_closure, ClassLoaderData::_claim_stw_fullgc_adjust); 1502 ClassLoaderDataGraph::cld_do(&cld_closure); 1503 } 1504 { 1505 AlwaysTrueClosure always_alive; 1506 _weak_proc_task.work(worker_id, &always_alive, &pc_adjust_pointer_closure); 1507 } 1508 if (_sub_tasks.try_claim_task(PSAdjustSubTask_code_cache)) { 1509 NMethodToOopClosure adjust_code(&pc_adjust_pointer_closure, NMethodToOopClosure::FixRelocations); 1510 CodeCache::nmethods_do(&adjust_code); 1511 } 1512 _sub_tasks.all_tasks_claimed(); 1513 } 1514 }; 1515 1516 void PSParallelCompact::adjust_pointers() { 1517 // Adjust the pointers to reflect the new locations 1518 GCTraceTime(Info, gc, phases) tm("Adjust Pointers", &_gc_timer); 1519 uint nworkers = ParallelScavengeHeap::heap()->workers().active_workers(); 1520 PSAdjustTask task(nworkers); 1521 ParallelScavengeHeap::heap()->workers().run_task(&task); 1522 } 1523 1524 // Split [start, end) evenly for a number of workers and return the 1525 // range for worker_id. 1526 static void split_regions_for_worker(size_t start, size_t end, 1527 uint worker_id, uint num_workers, 1528 size_t* worker_start, size_t* worker_end) { 1529 assert(start < end, "precondition"); 1530 assert(num_workers > 0, "precondition"); 1531 assert(worker_id < num_workers, "precondition"); 1532 1533 size_t num_regions = end - start; 1534 size_t num_regions_per_worker = num_regions / num_workers; 1535 size_t remainder = num_regions % num_workers; 1536 // The first few workers will get one extra. 1537 *worker_start = start + worker_id * num_regions_per_worker 1538 + MIN2(checked_cast<size_t>(worker_id), remainder); 1539 *worker_end = *worker_start + num_regions_per_worker 1540 + (worker_id < remainder ? 1 : 0); 1541 } 1542 1543 void PSParallelCompact::forward_to_new_addr() { 1544 GCTraceTime(Info, gc, phases) tm("Forward", &_gc_timer); 1545 uint nworkers = ParallelScavengeHeap::heap()->workers().active_workers(); 1546 1547 struct ForwardTask final : public WorkerTask { 1548 uint _num_workers; 1549 1550 explicit ForwardTask(uint num_workers) : 1551 WorkerTask("PSForward task"), 1552 _num_workers(num_workers) {} 1553 1554 static void forward_objs_in_range(ParCompactionManager* cm, 1555 HeapWord* start, 1556 HeapWord* end, 1557 HeapWord* destination) { 1558 HeapWord* cur_addr = start; 1559 HeapWord* new_addr = destination; 1560 1561 while (cur_addr < end) { 1562 cur_addr = mark_bitmap()->find_obj_beg(cur_addr, end); 1563 if (cur_addr >= end) { 1564 return; 1565 } 1566 assert(mark_bitmap()->is_marked(cur_addr), "inv"); 1567 oop obj = cast_to_oop(cur_addr); 1568 if (new_addr != cur_addr) { 1569 cm->preserved_marks()->push_if_necessary(obj, obj->mark()); 1570 FullGCForwarding::forward_to(obj, cast_to_oop(new_addr)); 1571 } 1572 size_t obj_size = obj->size(); 1573 new_addr += obj_size; 1574 cur_addr += obj_size; 1575 } 1576 } 1577 1578 void work(uint worker_id) override { 1579 ParCompactionManager* cm = ParCompactionManager::gc_thread_compaction_manager(worker_id); 1580 for (uint id = old_space_id; id < last_space_id; ++id) { 1581 MutableSpace* sp = PSParallelCompact::space(SpaceId(id)); 1582 HeapWord* dense_prefix_addr = dense_prefix(SpaceId(id)); 1583 HeapWord* top = sp->top(); 1584 1585 if (dense_prefix_addr == top) { 1586 continue; 1587 } 1588 1589 const SplitInfo& split_info = _space_info[SpaceId(id)].split_info(); 1590 1591 size_t dense_prefix_region = _summary_data.addr_to_region_idx(dense_prefix_addr); 1592 size_t top_region = _summary_data.addr_to_region_idx(_summary_data.region_align_up(top)); 1593 size_t start_region; 1594 size_t end_region; 1595 split_regions_for_worker(dense_prefix_region, top_region, 1596 worker_id, _num_workers, 1597 &start_region, &end_region); 1598 for (size_t cur_region = start_region; cur_region < end_region; ++cur_region) { 1599 RegionData* region_ptr = _summary_data.region(cur_region); 1600 size_t partial_obj_size = region_ptr->partial_obj_size(); 1601 1602 if (partial_obj_size == ParallelCompactData::RegionSize) { 1603 // No obj-start 1604 continue; 1605 } 1606 1607 HeapWord* region_start = _summary_data.region_to_addr(cur_region); 1608 HeapWord* region_end = region_start + ParallelCompactData::RegionSize; 1609 1610 if (split_info.is_split(cur_region)) { 1611 // Part 1: will be relocated to space-1 1612 HeapWord* preceding_destination = split_info.preceding_destination(); 1613 HeapWord* split_point = split_info.split_point(); 1614 forward_objs_in_range(cm, region_start + partial_obj_size, split_point, preceding_destination + partial_obj_size); 1615 1616 // Part 2: will be relocated to space-2 1617 HeapWord* destination = region_ptr->destination(); 1618 forward_objs_in_range(cm, split_point, region_end, destination); 1619 } else { 1620 HeapWord* destination = region_ptr->destination(); 1621 forward_objs_in_range(cm, region_start + partial_obj_size, region_end, destination + partial_obj_size); 1622 } 1623 } 1624 } 1625 } 1626 } task(nworkers); 1627 1628 ParallelScavengeHeap::heap()->workers().run_task(&task); 1629 debug_only(verify_forward();) 1630 } 1631 1632 #ifdef ASSERT 1633 void PSParallelCompact::verify_forward() { 1634 HeapWord* old_dense_prefix_addr = dense_prefix(SpaceId(old_space_id)); 1635 RegionData* old_region = _summary_data.region(_summary_data.addr_to_region_idx(old_dense_prefix_addr)); 1636 HeapWord* bump_ptr = old_region->partial_obj_size() != 0 1637 ? old_dense_prefix_addr + old_region->partial_obj_size() 1638 : old_dense_prefix_addr; 1639 SpaceId bump_ptr_space = old_space_id; 1640 1641 for (uint id = old_space_id; id < last_space_id; ++id) { 1642 MutableSpace* sp = PSParallelCompact::space(SpaceId(id)); 1643 HeapWord* dense_prefix_addr = dense_prefix(SpaceId(id)); 1644 HeapWord* top = sp->top(); 1645 HeapWord* cur_addr = dense_prefix_addr; 1646 1647 while (cur_addr < top) { 1648 cur_addr = mark_bitmap()->find_obj_beg(cur_addr, top); 1649 if (cur_addr >= top) { 1650 break; 1651 } 1652 assert(mark_bitmap()->is_marked(cur_addr), "inv"); 1653 assert(bump_ptr <= _space_info[bump_ptr_space].new_top(), "inv"); 1654 // Move to the space containing cur_addr 1655 if (bump_ptr == _space_info[bump_ptr_space].new_top()) { 1656 bump_ptr = space(space_id(cur_addr))->bottom(); 1657 bump_ptr_space = space_id(bump_ptr); 1658 } 1659 oop obj = cast_to_oop(cur_addr); 1660 if (cur_addr == bump_ptr) { 1661 assert(!FullGCForwarding::is_forwarded(obj), "inv"); 1662 } else { 1663 assert(FullGCForwarding::forwardee(obj) == cast_to_oop(bump_ptr), "inv"); 1664 } 1665 bump_ptr += obj->size(); 1666 cur_addr += obj->size(); 1667 } 1668 } 1669 } 1670 #endif 1671 1672 // Helper class to print 8 region numbers per line and then print the total at the end. 1673 class FillableRegionLogger : public StackObj { 1674 private: 1675 Log(gc, compaction) log; 1676 static const int LineLength = 8; 1677 size_t _regions[LineLength]; 1678 int _next_index; 1679 bool _enabled; 1680 size_t _total_regions; 1681 public: 1682 FillableRegionLogger() : _next_index(0), _enabled(log_develop_is_enabled(Trace, gc, compaction)), _total_regions(0) { } 1683 ~FillableRegionLogger() { 1684 log.trace(SIZE_FORMAT " initially fillable regions", _total_regions); 1685 } 1686 1687 void print_line() { 1688 if (!_enabled || _next_index == 0) { 1689 return; 1690 } 1691 FormatBuffer<> line("Fillable: "); 1692 for (int i = 0; i < _next_index; i++) { 1693 line.append(" " SIZE_FORMAT_W(7), _regions[i]); 1694 } 1695 log.trace("%s", line.buffer()); 1696 _next_index = 0; 1697 } 1698 1699 void handle(size_t region) { 1700 if (!_enabled) { 1701 return; 1702 } 1703 _regions[_next_index++] = region; 1704 if (_next_index == LineLength) { 1705 print_line(); 1706 } 1707 _total_regions++; 1708 } 1709 }; 1710 1711 void PSParallelCompact::prepare_region_draining_tasks(uint parallel_gc_threads) 1712 { 1713 GCTraceTime(Trace, gc, phases) tm("Drain Task Setup", &_gc_timer); 1714 1715 // Find the threads that are active 1716 uint worker_id = 0; 1717 1718 // Find all regions that are available (can be filled immediately) and 1719 // distribute them to the thread stacks. The iteration is done in reverse 1720 // order (high to low) so the regions will be removed in ascending order. 1721 1722 const ParallelCompactData& sd = PSParallelCompact::summary_data(); 1723 1724 // id + 1 is used to test termination so unsigned can 1725 // be used with an old_space_id == 0. 1726 FillableRegionLogger region_logger; 1727 for (unsigned int id = to_space_id; id + 1 > old_space_id; --id) { 1728 SpaceInfo* const space_info = _space_info + id; 1729 HeapWord* const new_top = space_info->new_top(); 1730 1731 const size_t beg_region = sd.addr_to_region_idx(space_info->dense_prefix()); 1732 const size_t end_region = 1733 sd.addr_to_region_idx(sd.region_align_up(new_top)); 1734 1735 for (size_t cur = end_region - 1; cur + 1 > beg_region; --cur) { 1736 if (sd.region(cur)->claim_unsafe()) { 1737 ParCompactionManager* cm = ParCompactionManager::gc_thread_compaction_manager(worker_id); 1738 bool result = sd.region(cur)->mark_normal(); 1739 assert(result, "Must succeed at this point."); 1740 cm->region_stack()->push(cur); 1741 region_logger.handle(cur); 1742 // Assign regions to tasks in round-robin fashion. 1743 if (++worker_id == parallel_gc_threads) { 1744 worker_id = 0; 1745 } 1746 } 1747 } 1748 region_logger.print_line(); 1749 } 1750 } 1751 1752 static void compaction_with_stealing_work(TaskTerminator* terminator, uint worker_id) { 1753 assert(ParallelScavengeHeap::heap()->is_stw_gc_active(), "called outside gc"); 1754 1755 ParCompactionManager* cm = 1756 ParCompactionManager::gc_thread_compaction_manager(worker_id); 1757 1758 // Drain the stacks that have been preloaded with regions 1759 // that are ready to fill. 1760 1761 cm->drain_region_stacks(); 1762 1763 guarantee(cm->region_stack()->is_empty(), "Not empty"); 1764 1765 size_t region_index = 0; 1766 1767 while (true) { 1768 if (ParCompactionManager::steal(worker_id, region_index)) { 1769 PSParallelCompact::fill_and_update_region(cm, region_index); 1770 cm->drain_region_stacks(); 1771 } else if (PSParallelCompact::steal_unavailable_region(cm, region_index)) { 1772 // Fill and update an unavailable region with the help of a shadow region 1773 PSParallelCompact::fill_and_update_shadow_region(cm, region_index); 1774 cm->drain_region_stacks(); 1775 } else { 1776 if (terminator->offer_termination()) { 1777 break; 1778 } 1779 // Go around again. 1780 } 1781 } 1782 } 1783 1784 class FillDensePrefixAndCompactionTask: public WorkerTask { 1785 uint _num_workers; 1786 TaskTerminator _terminator; 1787 1788 public: 1789 FillDensePrefixAndCompactionTask(uint active_workers) : 1790 WorkerTask("FillDensePrefixAndCompactionTask"), 1791 _num_workers(active_workers), 1792 _terminator(active_workers, ParCompactionManager::region_task_queues()) { 1793 } 1794 1795 virtual void work(uint worker_id) { 1796 { 1797 auto start = Ticks::now(); 1798 PSParallelCompact::fill_dead_objs_in_dense_prefix(worker_id, _num_workers); 1799 log_trace(gc, phases)("Fill dense prefix by worker %u: %.3f ms", worker_id, (Ticks::now() - start).seconds() * 1000); 1800 } 1801 compaction_with_stealing_work(&_terminator, worker_id); 1802 } 1803 }; 1804 1805 void PSParallelCompact::fill_range_in_dense_prefix(HeapWord* start, HeapWord* end) { 1806 #ifdef ASSERT 1807 { 1808 assert(start < end, "precondition"); 1809 assert(mark_bitmap()->find_obj_beg(start, end) == end, "precondition"); 1810 HeapWord* bottom = _space_info[old_space_id].space()->bottom(); 1811 if (start != bottom) { 1812 HeapWord* obj_start = mark_bitmap()->find_obj_beg_reverse(bottom, start); 1813 HeapWord* after_obj = obj_start + cast_to_oop(obj_start)->size(); 1814 assert(after_obj == start, "precondition"); 1815 } 1816 } 1817 #endif 1818 1819 CollectedHeap::fill_with_objects(start, pointer_delta(end, start)); 1820 HeapWord* addr = start; 1821 do { 1822 size_t size = cast_to_oop(addr)->size(); 1823 start_array(old_space_id)->update_for_block(addr, addr + size); 1824 addr += size; 1825 } while (addr < end); 1826 } 1827 1828 void PSParallelCompact::fill_dead_objs_in_dense_prefix(uint worker_id, uint num_workers) { 1829 ParMarkBitMap* bitmap = mark_bitmap(); 1830 1831 HeapWord* const bottom = _space_info[old_space_id].space()->bottom(); 1832 HeapWord* const prefix_end = dense_prefix(old_space_id); 1833 1834 if (bottom == prefix_end) { 1835 return; 1836 } 1837 1838 size_t bottom_region = _summary_data.addr_to_region_idx(bottom); 1839 size_t prefix_end_region = _summary_data.addr_to_region_idx(prefix_end); 1840 1841 size_t start_region; 1842 size_t end_region; 1843 split_regions_for_worker(bottom_region, prefix_end_region, 1844 worker_id, num_workers, 1845 &start_region, &end_region); 1846 1847 if (start_region == end_region) { 1848 return; 1849 } 1850 1851 HeapWord* const start_addr = _summary_data.region_to_addr(start_region); 1852 HeapWord* const end_addr = _summary_data.region_to_addr(end_region); 1853 1854 // Skip live partial obj (if any) from previous region. 1855 HeapWord* cur_addr; 1856 RegionData* start_region_ptr = _summary_data.region(start_region); 1857 if (start_region_ptr->partial_obj_size() != 0) { 1858 HeapWord* partial_obj_start = start_region_ptr->partial_obj_addr(); 1859 assert(bitmap->is_marked(partial_obj_start), "inv"); 1860 cur_addr = partial_obj_start + cast_to_oop(partial_obj_start)->size(); 1861 } else { 1862 cur_addr = start_addr; 1863 } 1864 1865 // end_addr is inclusive to handle regions starting with dead space. 1866 while (cur_addr <= end_addr) { 1867 // Use prefix_end to handle trailing obj in each worker region-chunk. 1868 HeapWord* live_start = bitmap->find_obj_beg(cur_addr, prefix_end); 1869 if (cur_addr != live_start) { 1870 // Only worker 0 handles proceeding dead space. 1871 if (cur_addr != start_addr || worker_id == 0) { 1872 fill_range_in_dense_prefix(cur_addr, live_start); 1873 } 1874 } 1875 if (live_start >= end_addr) { 1876 break; 1877 } 1878 assert(bitmap->is_marked(live_start), "inv"); 1879 cur_addr = live_start + cast_to_oop(live_start)->size(); 1880 } 1881 } 1882 1883 void PSParallelCompact::compact() { 1884 GCTraceTime(Info, gc, phases) tm("Compaction Phase", &_gc_timer); 1885 1886 uint active_gc_threads = ParallelScavengeHeap::heap()->workers().active_workers(); 1887 1888 initialize_shadow_regions(active_gc_threads); 1889 prepare_region_draining_tasks(active_gc_threads); 1890 1891 { 1892 GCTraceTime(Trace, gc, phases) tm("Par Compact", &_gc_timer); 1893 1894 FillDensePrefixAndCompactionTask task(active_gc_threads); 1895 ParallelScavengeHeap::heap()->workers().run_task(&task); 1896 1897 #ifdef ASSERT 1898 verify_filler_in_dense_prefix(); 1899 1900 // Verify that all regions have been processed. 1901 for (unsigned int id = old_space_id; id < last_space_id; ++id) { 1902 verify_complete(SpaceId(id)); 1903 } 1904 #endif 1905 } 1906 } 1907 1908 #ifdef ASSERT 1909 void PSParallelCompact::verify_filler_in_dense_prefix() { 1910 HeapWord* bottom = _space_info[old_space_id].space()->bottom(); 1911 HeapWord* dense_prefix_end = dense_prefix(old_space_id); 1912 HeapWord* cur_addr = bottom; 1913 while (cur_addr < dense_prefix_end) { 1914 oop obj = cast_to_oop(cur_addr); 1915 oopDesc::verify(obj); 1916 if (!mark_bitmap()->is_marked(cur_addr)) { 1917 Klass* k = cast_to_oop(cur_addr)->klass(); 1918 assert(k == Universe::fillerArrayKlass() || k == vmClasses::FillerObject_klass(), "inv"); 1919 } 1920 cur_addr += obj->size(); 1921 } 1922 } 1923 1924 void PSParallelCompact::verify_complete(SpaceId space_id) { 1925 // All Regions served as compaction targets, from dense_prefix() to 1926 // new_top(), should be marked as filled and all Regions between new_top() 1927 // and top() should be available (i.e., should have been emptied). 1928 ParallelCompactData& sd = summary_data(); 1929 SpaceInfo si = _space_info[space_id]; 1930 HeapWord* new_top_addr = sd.region_align_up(si.new_top()); 1931 HeapWord* old_top_addr = sd.region_align_up(si.space()->top()); 1932 const size_t beg_region = sd.addr_to_region_idx(si.dense_prefix()); 1933 const size_t new_top_region = sd.addr_to_region_idx(new_top_addr); 1934 const size_t old_top_region = sd.addr_to_region_idx(old_top_addr); 1935 1936 size_t cur_region; 1937 for (cur_region = beg_region; cur_region < new_top_region; ++cur_region) { 1938 const RegionData* const c = sd.region(cur_region); 1939 assert(c->completed(), "region %zu not filled: destination_count=%u", 1940 cur_region, c->destination_count()); 1941 } 1942 1943 for (cur_region = new_top_region; cur_region < old_top_region; ++cur_region) { 1944 const RegionData* const c = sd.region(cur_region); 1945 assert(c->available(), "region %zu not empty: destination_count=%u", 1946 cur_region, c->destination_count()); 1947 } 1948 } 1949 #endif // #ifdef ASSERT 1950 1951 // Return the SpaceId for the space containing addr. If addr is not in the 1952 // heap, last_space_id is returned. In debug mode it expects the address to be 1953 // in the heap and asserts such. 1954 PSParallelCompact::SpaceId PSParallelCompact::space_id(HeapWord* addr) { 1955 assert(ParallelScavengeHeap::heap()->is_in_reserved(addr), "addr not in the heap"); 1956 1957 for (unsigned int id = old_space_id; id < last_space_id; ++id) { 1958 if (_space_info[id].space()->contains(addr)) { 1959 return SpaceId(id); 1960 } 1961 } 1962 1963 assert(false, "no space contains the addr"); 1964 return last_space_id; 1965 } 1966 1967 // Skip over count live words starting from beg, and return the address of the 1968 // next live word. Callers must also ensure that there are enough live words in 1969 // the range [beg, end) to skip. 1970 HeapWord* PSParallelCompact::skip_live_words(HeapWord* beg, HeapWord* end, size_t count) 1971 { 1972 ParMarkBitMap* m = mark_bitmap(); 1973 HeapWord* cur_addr = beg; 1974 while (true) { 1975 cur_addr = m->find_obj_beg(cur_addr, end); 1976 assert(cur_addr < end, "inv"); 1977 size_t obj_size = cast_to_oop(cur_addr)->size(); 1978 // Strictly greater-than 1979 if (obj_size > count) { 1980 return cur_addr + count; 1981 } 1982 count -= obj_size; 1983 cur_addr += obj_size; 1984 } 1985 } 1986 1987 // On starting to fill a destination region (dest-region), we need to know the 1988 // location of the word that will be at the start of the dest-region after 1989 // compaction. A dest-region can have one or more source regions, but only the 1990 // first source-region contains this location. This location is retrieved by 1991 // calling `first_src_addr` on a dest-region. 1992 // Conversely, a source-region has a dest-region which holds the destination of 1993 // the first live word on this source-region, based on which the destination 1994 // for the rest of live words can be derived. 1995 // 1996 // Note: 1997 // There is some complication due to space-boundary-fragmentation (an obj can't 1998 // cross space-boundary) -- a source-region may be split and behave like two 1999 // distinct regions with their own dest-region, as depicted below. 2000 // 2001 // source-region: region-n 2002 // 2003 // ********************** 2004 // | A|A~~~~B|B | 2005 // ********************** 2006 // n-1 n n+1 2007 // 2008 // AA, BB denote two live objs. ~~~~ denotes unknown number of live objs. 2009 // 2010 // Assuming the dest-region for region-n is the final region before 2011 // old-space-end and its first-live-word is the middle of AA, the heap content 2012 // will look like the following after compaction: 2013 // 2014 // ************** ************* 2015 // A|A~~~~ | |BB | 2016 // ************** ************* 2017 // ^ ^ 2018 // | old-space-end | eden-space-start 2019 // 2020 // Therefore, in this example, region-n will have two dest-regions: 2021 // 1. the final region in old-space 2022 // 2. the first region in eden-space. 2023 // To handle this special case, we introduce the concept of split-region, whose 2024 // contents are relocated to two spaces. `SplitInfo` captures all necessary 2025 // info about the split, the first part, spliting-point, and the second part. 2026 HeapWord* PSParallelCompact::first_src_addr(HeapWord* const dest_addr, 2027 SpaceId src_space_id, 2028 size_t src_region_idx) 2029 { 2030 const size_t RegionSize = ParallelCompactData::RegionSize; 2031 const ParallelCompactData& sd = summary_data(); 2032 assert(sd.is_region_aligned(dest_addr), "precondition"); 2033 2034 const RegionData* const src_region_ptr = sd.region(src_region_idx); 2035 assert(src_region_ptr->data_size() > 0, "src region cannot be empty"); 2036 2037 const size_t partial_obj_size = src_region_ptr->partial_obj_size(); 2038 HeapWord* const src_region_destination = src_region_ptr->destination(); 2039 2040 HeapWord* const region_start = sd.region_to_addr(src_region_idx); 2041 HeapWord* const region_end = sd.region_to_addr(src_region_idx) + RegionSize; 2042 2043 // Identify the actual destination for the first live words on this region, 2044 // taking split-region into account. 2045 HeapWord* region_start_destination; 2046 const SplitInfo& split_info = _space_info[src_space_id].split_info(); 2047 if (split_info.is_split(src_region_idx)) { 2048 // The second part of this split region; use the recorded split point. 2049 if (dest_addr == src_region_destination) { 2050 return split_info.split_point(); 2051 } 2052 region_start_destination = split_info.preceding_destination(); 2053 } else { 2054 region_start_destination = src_region_destination; 2055 } 2056 2057 // Calculate the offset to be skipped 2058 size_t words_to_skip = pointer_delta(dest_addr, region_start_destination); 2059 2060 HeapWord* result; 2061 if (partial_obj_size > words_to_skip) { 2062 result = region_start + words_to_skip; 2063 } else { 2064 words_to_skip -= partial_obj_size; 2065 result = skip_live_words(region_start + partial_obj_size, region_end, words_to_skip); 2066 } 2067 2068 if (split_info.is_split(src_region_idx)) { 2069 assert(result < split_info.split_point(), "postcondition"); 2070 } else { 2071 assert(result < region_end, "postcondition"); 2072 } 2073 2074 return result; 2075 } 2076 2077 void PSParallelCompact::decrement_destination_counts(ParCompactionManager* cm, 2078 SpaceId src_space_id, 2079 size_t beg_region, 2080 HeapWord* end_addr) 2081 { 2082 ParallelCompactData& sd = summary_data(); 2083 2084 #ifdef ASSERT 2085 MutableSpace* const src_space = _space_info[src_space_id].space(); 2086 HeapWord* const beg_addr = sd.region_to_addr(beg_region); 2087 assert(src_space->contains(beg_addr) || beg_addr == src_space->end(), 2088 "src_space_id does not match beg_addr"); 2089 assert(src_space->contains(end_addr) || end_addr == src_space->end(), 2090 "src_space_id does not match end_addr"); 2091 #endif // #ifdef ASSERT 2092 2093 RegionData* const beg = sd.region(beg_region); 2094 RegionData* const end = sd.addr_to_region_ptr(sd.region_align_up(end_addr)); 2095 2096 // Regions up to new_top() are enqueued if they become available. 2097 HeapWord* const new_top = _space_info[src_space_id].new_top(); 2098 RegionData* const enqueue_end = 2099 sd.addr_to_region_ptr(sd.region_align_up(new_top)); 2100 2101 for (RegionData* cur = beg; cur < end; ++cur) { 2102 assert(cur->data_size() > 0, "region must have live data"); 2103 cur->decrement_destination_count(); 2104 if (cur < enqueue_end && cur->available() && cur->claim()) { 2105 if (cur->mark_normal()) { 2106 cm->push_region(sd.region(cur)); 2107 } else if (cur->mark_copied()) { 2108 // Try to copy the content of the shadow region back to its corresponding 2109 // heap region if the shadow region is filled. Otherwise, the GC thread 2110 // fills the shadow region will copy the data back (see 2111 // MoveAndUpdateShadowClosure::complete_region). 2112 copy_back(sd.region_to_addr(cur->shadow_region()), sd.region_to_addr(cur)); 2113 ParCompactionManager::push_shadow_region_mt_safe(cur->shadow_region()); 2114 cur->set_completed(); 2115 } 2116 } 2117 } 2118 } 2119 2120 size_t PSParallelCompact::next_src_region(MoveAndUpdateClosure& closure, 2121 SpaceId& src_space_id, 2122 HeapWord*& src_space_top, 2123 HeapWord* end_addr) 2124 { 2125 ParallelCompactData& sd = PSParallelCompact::summary_data(); 2126 2127 size_t src_region_idx = 0; 2128 2129 // Skip empty regions (if any) up to the top of the space. 2130 HeapWord* const src_aligned_up = sd.region_align_up(end_addr); 2131 RegionData* src_region_ptr = sd.addr_to_region_ptr(src_aligned_up); 2132 HeapWord* const top_aligned_up = sd.region_align_up(src_space_top); 2133 const RegionData* const top_region_ptr = sd.addr_to_region_ptr(top_aligned_up); 2134 2135 while (src_region_ptr < top_region_ptr && src_region_ptr->data_size() == 0) { 2136 ++src_region_ptr; 2137 } 2138 2139 if (src_region_ptr < top_region_ptr) { 2140 // The next source region is in the current space. Update src_region_idx 2141 // and the source address to match src_region_ptr. 2142 src_region_idx = sd.region(src_region_ptr); 2143 HeapWord* const src_region_addr = sd.region_to_addr(src_region_idx); 2144 if (src_region_addr > closure.source()) { 2145 closure.set_source(src_region_addr); 2146 } 2147 return src_region_idx; 2148 } 2149 2150 // Switch to a new source space and find the first non-empty region. 2151 uint space_id = src_space_id + 1; 2152 assert(space_id < last_space_id, "not enough spaces"); 2153 2154 for (/* empty */; space_id < last_space_id; ++space_id) { 2155 HeapWord* bottom = _space_info[space_id].space()->bottom(); 2156 HeapWord* top = _space_info[space_id].space()->top(); 2157 // Skip empty space 2158 if (bottom == top) { 2159 continue; 2160 } 2161 2162 // Identify the first region that contains live words in this space 2163 size_t cur_region = sd.addr_to_region_idx(bottom); 2164 size_t end_region = sd.addr_to_region_idx(sd.region_align_up(top)); 2165 2166 for (/* empty */ ; cur_region < end_region; ++cur_region) { 2167 RegionData* cur = sd.region(cur_region); 2168 if (cur->live_obj_size() > 0) { 2169 HeapWord* region_start_addr = sd.region_to_addr(cur_region); 2170 HeapWord* region_end_addr = region_start_addr + ParallelCompactData::RegionSize; 2171 HeapWord* first_live_word = mark_bitmap()->find_obj_beg(region_start_addr, region_end_addr); 2172 assert(first_live_word < region_end_addr, "inv"); 2173 2174 src_space_id = SpaceId(space_id); 2175 src_space_top = top; 2176 closure.set_source(first_live_word); 2177 return cur_region; 2178 } 2179 } 2180 } 2181 2182 ShouldNotReachHere(); 2183 } 2184 2185 HeapWord* PSParallelCompact::partial_obj_end(HeapWord* region_start_addr) { 2186 ParallelCompactData& sd = summary_data(); 2187 assert(sd.is_region_aligned(region_start_addr), "precondition"); 2188 2189 // Use per-region partial_obj_size to locate the end of the obj, that extends 2190 // to region_start_addr. 2191 size_t start_region_idx = sd.addr_to_region_idx(region_start_addr); 2192 size_t end_region_idx = sd.region_count(); 2193 size_t accumulated_size = 0; 2194 for (size_t region_idx = start_region_idx; region_idx < end_region_idx; ++region_idx) { 2195 size_t cur_partial_obj_size = sd.region(region_idx)->partial_obj_size(); 2196 accumulated_size += cur_partial_obj_size; 2197 if (cur_partial_obj_size != ParallelCompactData::RegionSize) { 2198 break; 2199 } 2200 } 2201 return region_start_addr + accumulated_size; 2202 } 2203 2204 // Use region_idx as the destination region, and evacuate all live objs on its 2205 // source regions to this destination region. 2206 void PSParallelCompact::fill_region(ParCompactionManager* cm, MoveAndUpdateClosure& closure, size_t region_idx) 2207 { 2208 ParMarkBitMap* const bitmap = mark_bitmap(); 2209 ParallelCompactData& sd = summary_data(); 2210 RegionData* const region_ptr = sd.region(region_idx); 2211 2212 // Get the source region and related info. 2213 size_t src_region_idx = region_ptr->source_region(); 2214 SpaceId src_space_id = space_id(sd.region_to_addr(src_region_idx)); 2215 HeapWord* src_space_top = _space_info[src_space_id].space()->top(); 2216 HeapWord* dest_addr = sd.region_to_addr(region_idx); 2217 2218 closure.set_source(first_src_addr(dest_addr, src_space_id, src_region_idx)); 2219 2220 // Adjust src_region_idx to prepare for decrementing destination counts (the 2221 // destination count is not decremented when a region is copied to itself). 2222 if (src_region_idx == region_idx) { 2223 src_region_idx += 1; 2224 } 2225 2226 // source-region: 2227 // 2228 // ********** 2229 // | ~~~ | 2230 // ********** 2231 // ^ 2232 // |-- closure.source() / first_src_addr 2233 // 2234 // 2235 // ~~~ : live words 2236 // 2237 // destination-region: 2238 // 2239 // ********** 2240 // | | 2241 // ********** 2242 // ^ 2243 // |-- region-start 2244 if (bitmap->is_unmarked(closure.source())) { 2245 // An object overflows the previous destination region, so this 2246 // destination region should copy the remainder of the object or as much as 2247 // will fit. 2248 HeapWord* const old_src_addr = closure.source(); 2249 { 2250 HeapWord* region_start = sd.region_align_down(closure.source()); 2251 HeapWord* obj_start = bitmap->find_obj_beg_reverse(region_start, closure.source()); 2252 HeapWord* obj_end; 2253 if (obj_start != closure.source()) { 2254 assert(bitmap->is_marked(obj_start), "inv"); 2255 // Found the actual obj-start, try to find the obj-end using either 2256 // size() if this obj is completely contained in the current region. 2257 HeapWord* next_region_start = region_start + ParallelCompactData::RegionSize; 2258 HeapWord* partial_obj_start = (next_region_start >= src_space_top) 2259 ? nullptr 2260 : sd.addr_to_region_ptr(next_region_start)->partial_obj_addr(); 2261 // This obj extends to next region iff partial_obj_addr of the *next* 2262 // region is the same as obj-start. 2263 if (partial_obj_start == obj_start) { 2264 // This obj extends to next region. 2265 obj_end = partial_obj_end(next_region_start); 2266 } else { 2267 // Completely contained in this region; safe to use size(). 2268 obj_end = obj_start + cast_to_oop(obj_start)->size(); 2269 } 2270 } else { 2271 // This obj extends to current region. 2272 obj_end = partial_obj_end(region_start); 2273 } 2274 size_t partial_obj_size = pointer_delta(obj_end, closure.source()); 2275 closure.copy_partial_obj(partial_obj_size); 2276 } 2277 2278 if (closure.is_full()) { 2279 decrement_destination_counts(cm, src_space_id, src_region_idx, closure.source()); 2280 closure.complete_region(dest_addr, region_ptr); 2281 return; 2282 } 2283 2284 // Finished copying without using up the current destination-region 2285 HeapWord* const end_addr = sd.region_align_down(closure.source()); 2286 if (sd.region_align_down(old_src_addr) != end_addr) { 2287 assert(sd.region_align_up(old_src_addr) == end_addr, "only one region"); 2288 // The partial object was copied from more than one source region. 2289 decrement_destination_counts(cm, src_space_id, src_region_idx, end_addr); 2290 2291 // Move to the next source region, possibly switching spaces as well. All 2292 // args except end_addr may be modified. 2293 src_region_idx = next_src_region(closure, src_space_id, src_space_top, end_addr); 2294 } 2295 } 2296 2297 // Handle the rest obj-by-obj, where we know obj-start. 2298 do { 2299 HeapWord* cur_addr = closure.source(); 2300 HeapWord* const end_addr = MIN2(sd.region_align_up(cur_addr + 1), 2301 src_space_top); 2302 // To handle the case where the final obj in source region extends to next region. 2303 HeapWord* final_obj_start = (end_addr == src_space_top) 2304 ? nullptr 2305 : sd.addr_to_region_ptr(end_addr)->partial_obj_addr(); 2306 // Apply closure on objs inside [cur_addr, end_addr) 2307 do { 2308 cur_addr = bitmap->find_obj_beg(cur_addr, end_addr); 2309 if (cur_addr == end_addr) { 2310 break; 2311 } 2312 size_t obj_size; 2313 if (final_obj_start == cur_addr) { 2314 obj_size = pointer_delta(partial_obj_end(end_addr), cur_addr); 2315 } else { 2316 // This obj doesn't extend into next region; size() is safe to use. 2317 obj_size = cast_to_oop(cur_addr)->size(); 2318 } 2319 closure.do_addr(cur_addr, obj_size); 2320 cur_addr += obj_size; 2321 } while (cur_addr < end_addr && !closure.is_full()); 2322 2323 if (closure.is_full()) { 2324 decrement_destination_counts(cm, src_space_id, src_region_idx, closure.source()); 2325 closure.complete_region(dest_addr, region_ptr); 2326 return; 2327 } 2328 2329 decrement_destination_counts(cm, src_space_id, src_region_idx, end_addr); 2330 2331 // Move to the next source region, possibly switching spaces as well. All 2332 // args except end_addr may be modified. 2333 src_region_idx = next_src_region(closure, src_space_id, src_space_top, end_addr); 2334 } while (true); 2335 } 2336 2337 void PSParallelCompact::fill_and_update_region(ParCompactionManager* cm, size_t region_idx) 2338 { 2339 MoveAndUpdateClosure cl(mark_bitmap(), region_idx); 2340 fill_region(cm, cl, region_idx); 2341 } 2342 2343 void PSParallelCompact::fill_and_update_shadow_region(ParCompactionManager* cm, size_t region_idx) 2344 { 2345 // Get a shadow region first 2346 ParallelCompactData& sd = summary_data(); 2347 RegionData* const region_ptr = sd.region(region_idx); 2348 size_t shadow_region = ParCompactionManager::pop_shadow_region_mt_safe(region_ptr); 2349 // The InvalidShadow return value indicates the corresponding heap region is available, 2350 // so use MoveAndUpdateClosure to fill the normal region. Otherwise, use 2351 // MoveAndUpdateShadowClosure to fill the acquired shadow region. 2352 if (shadow_region == ParCompactionManager::InvalidShadow) { 2353 MoveAndUpdateClosure cl(mark_bitmap(), region_idx); 2354 region_ptr->shadow_to_normal(); 2355 return fill_region(cm, cl, region_idx); 2356 } else { 2357 MoveAndUpdateShadowClosure cl(mark_bitmap(), region_idx, shadow_region); 2358 return fill_region(cm, cl, region_idx); 2359 } 2360 } 2361 2362 void PSParallelCompact::copy_back(HeapWord *shadow_addr, HeapWord *region_addr) 2363 { 2364 Copy::aligned_conjoint_words(shadow_addr, region_addr, _summary_data.RegionSize); 2365 } 2366 2367 bool PSParallelCompact::steal_unavailable_region(ParCompactionManager* cm, size_t ®ion_idx) 2368 { 2369 size_t next = cm->next_shadow_region(); 2370 ParallelCompactData& sd = summary_data(); 2371 size_t old_new_top = sd.addr_to_region_idx(_space_info[old_space_id].new_top()); 2372 uint active_gc_threads = ParallelScavengeHeap::heap()->workers().active_workers(); 2373 2374 while (next < old_new_top) { 2375 if (sd.region(next)->mark_shadow()) { 2376 region_idx = next; 2377 return true; 2378 } 2379 next = cm->move_next_shadow_region_by(active_gc_threads); 2380 } 2381 2382 return false; 2383 } 2384 2385 // The shadow region is an optimization to address region dependencies in full GC. The basic 2386 // idea is making more regions available by temporally storing their live objects in empty 2387 // shadow regions to resolve dependencies between them and the destination regions. Therefore, 2388 // GC threads need not wait destination regions to be available before processing sources. 2389 // 2390 // A typical workflow would be: 2391 // After draining its own stack and failing to steal from others, a GC worker would pick an 2392 // unavailable region (destination count > 0) and get a shadow region. Then the worker fills 2393 // the shadow region by copying live objects from source regions of the unavailable one. Once 2394 // the unavailable region becomes available, the data in the shadow region will be copied back. 2395 // Shadow regions are empty regions in the to-space and regions between top and end of other spaces. 2396 void PSParallelCompact::initialize_shadow_regions(uint parallel_gc_threads) 2397 { 2398 const ParallelCompactData& sd = PSParallelCompact::summary_data(); 2399 2400 for (unsigned int id = old_space_id; id < last_space_id; ++id) { 2401 SpaceInfo* const space_info = _space_info + id; 2402 MutableSpace* const space = space_info->space(); 2403 2404 const size_t beg_region = 2405 sd.addr_to_region_idx(sd.region_align_up(MAX2(space_info->new_top(), space->top()))); 2406 const size_t end_region = 2407 sd.addr_to_region_idx(sd.region_align_down(space->end())); 2408 2409 for (size_t cur = beg_region; cur < end_region; ++cur) { 2410 ParCompactionManager::push_shadow_region(cur); 2411 } 2412 } 2413 2414 size_t beg_region = sd.addr_to_region_idx(_space_info[old_space_id].dense_prefix()); 2415 for (uint i = 0; i < parallel_gc_threads; i++) { 2416 ParCompactionManager *cm = ParCompactionManager::gc_thread_compaction_manager(i); 2417 cm->set_next_shadow_region(beg_region + i); 2418 } 2419 } 2420 2421 void MoveAndUpdateClosure::copy_partial_obj(size_t partial_obj_size) 2422 { 2423 size_t words = MIN2(partial_obj_size, words_remaining()); 2424 2425 // This test is necessary; if omitted, the pointer updates to a partial object 2426 // that crosses the dense prefix boundary could be overwritten. 2427 if (source() != copy_destination()) { 2428 DEBUG_ONLY(PSParallelCompact::check_new_location(source(), destination());) 2429 Copy::aligned_conjoint_words(source(), copy_destination(), words); 2430 } 2431 update_state(words); 2432 } 2433 2434 void MoveAndUpdateClosure::complete_region(HeapWord* dest_addr, PSParallelCompact::RegionData* region_ptr) { 2435 assert(region_ptr->shadow_state() == ParallelCompactData::RegionData::NormalRegion, "Region should be finished"); 2436 region_ptr->set_completed(); 2437 } 2438 2439 void MoveAndUpdateClosure::do_addr(HeapWord* addr, size_t words) { 2440 assert(destination() != nullptr, "sanity"); 2441 _source = addr; 2442 2443 // The start_array must be updated even if the object is not moving. 2444 if (_start_array != nullptr) { 2445 _start_array->update_for_block(destination(), destination() + words); 2446 } 2447 2448 // Avoid overflow 2449 words = MIN2(words, words_remaining()); 2450 assert(words > 0, "inv"); 2451 2452 if (copy_destination() != source()) { 2453 DEBUG_ONLY(PSParallelCompact::check_new_location(source(), destination());) 2454 assert(source() != destination(), "inv"); 2455 assert(FullGCForwarding::is_forwarded(cast_to_oop(source())), "inv"); 2456 assert(FullGCForwarding::forwardee(cast_to_oop(source())) == cast_to_oop(destination()), "inv"); 2457 Copy::aligned_conjoint_words(source(), copy_destination(), words); 2458 cast_to_oop(copy_destination())->init_mark(); 2459 } 2460 2461 update_state(words); 2462 } 2463 2464 void MoveAndUpdateShadowClosure::complete_region(HeapWord* dest_addr, PSParallelCompact::RegionData* region_ptr) { 2465 assert(region_ptr->shadow_state() == ParallelCompactData::RegionData::ShadowRegion, "Region should be shadow"); 2466 // Record the shadow region index 2467 region_ptr->set_shadow_region(_shadow); 2468 // Mark the shadow region as filled to indicate the data is ready to be 2469 // copied back 2470 region_ptr->mark_filled(); 2471 // Try to copy the content of the shadow region back to its corresponding 2472 // heap region if available; the GC thread that decreases the destination 2473 // count to zero will do the copying otherwise (see 2474 // PSParallelCompact::decrement_destination_counts). 2475 if (((region_ptr->available() && region_ptr->claim()) || region_ptr->claimed()) && region_ptr->mark_copied()) { 2476 region_ptr->set_completed(); 2477 PSParallelCompact::copy_back(PSParallelCompact::summary_data().region_to_addr(_shadow), dest_addr); 2478 ParCompactionManager::push_shadow_region_mt_safe(_shadow); 2479 } 2480 } 2481