1 /* 2 * Copyright (c) 2014, 2017, Red Hat, Inc. and/or its affiliates. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 * 22 */ 23 24 #include "precompiled.hpp" 25 26 #include "classfile/javaClasses.inline.hpp" 27 #include "code/codeCache.hpp" 28 #include "gc/shared/gcTraceTime.inline.hpp" 29 #include "gc/shenandoah/brooksPointer.hpp" 30 #include "gc/shenandoah/shenandoahConcurrentMark.inline.hpp" 31 #include "gc/shenandoah/shenandoahCollectionSet.hpp" 32 #include "gc/shenandoah/shenandoahFreeSet.hpp" 33 #include "gc/shenandoah/shenandoahPhaseTimings.hpp" 34 #include "gc/shenandoah/shenandoahMarkCompact.hpp" 35 #include "gc/shenandoah/shenandoahBarrierSet.hpp" 36 #include "gc/shenandoah/shenandoahHeapRegionSet.hpp" 37 #include "gc/shenandoah/shenandoahHeap.hpp" 38 #include "gc/shenandoah/shenandoahHeap.inline.hpp" 39 #include "gc/shenandoah/shenandoahPartialGC.hpp" 40 #include "gc/shenandoah/shenandoahRootProcessor.hpp" 41 #include "gc/shenandoah/shenandoahTraversalGC.hpp" 42 #include "gc/shenandoah/shenandoahUtils.hpp" 43 #include "gc/shenandoah/shenandoahVerifier.hpp" 44 #include "gc/shenandoah/shenandoahWorkerPolicy.hpp" 45 #include "gc/shenandoah/vm_operations_shenandoah.hpp" 46 #include "oops/oop.inline.hpp" 47 #include "runtime/biasedLocking.hpp" 48 #include "runtime/thread.hpp" 49 #include "utilities/copy.hpp" 50 #include "utilities/growableArray.hpp" 51 #include "gc/shared/taskqueue.inline.hpp" 52 #include "gc/shared/workgroup.hpp" 53 54 class ShenandoahMarkCompactBarrierSet : public ShenandoahBarrierSet { 55 public: 56 ShenandoahMarkCompactBarrierSet(ShenandoahHeap* heap) : ShenandoahBarrierSet(heap) {} 57 58 oop read_barrier(oop src) { 59 return src; 60 } 61 62 #ifdef ASSERT 63 void verify_safe_oop(oop o) {} 64 void verify_safe_oop(narrowOop o) {} 65 #endif 66 }; 67 68 class ShenandoahClearRegionStatusClosure: public ShenandoahHeapRegionClosure { 69 private: 70 ShenandoahHeap* const _heap; 71 72 public: 73 ShenandoahClearRegionStatusClosure() : _heap(ShenandoahHeap::heap()) {} 74 75 bool heap_region_do(ShenandoahHeapRegion *r) { 76 _heap->set_next_top_at_mark_start(r->bottom(), r->top()); 77 r->clear_live_data(); 78 r->set_concurrent_iteration_safe_limit(r->top()); 79 return false; 80 } 81 }; 82 83 class ShenandoahEnsureHeapActiveClosure: public ShenandoahHeapRegionClosure { 84 private: 85 ShenandoahHeap* const _heap; 86 87 public: 88 ShenandoahEnsureHeapActiveClosure() : _heap(ShenandoahHeap::heap()) {} 89 bool heap_region_do(ShenandoahHeapRegion* r) { 90 if (r->is_trash()) { 91 r->recycle(); 92 } 93 if (r->is_cset()) { 94 r->make_regular_bypass(); 95 } 96 if (r->is_empty_uncommitted()) { 97 r->make_committed_bypass(); 98 } 99 assert (r->is_committed(), "only committed regions in heap now, see region " SIZE_FORMAT, r->region_number()); 100 101 // Record current region occupancy: this communicates empty regions are free 102 // to the rest of Full GC code. 103 r->set_new_top(r->top()); 104 return false; 105 } 106 }; 107 108 void ShenandoahMarkCompact::initialize(GCTimer* gc_timer) { 109 _gc_timer = gc_timer; 110 } 111 112 void ShenandoahMarkCompact::do_it(GCCause::Cause gc_cause) { 113 ShenandoahHeap* heap = ShenandoahHeap::heap(); 114 115 { 116 if (ShenandoahVerify) { 117 heap->verifier()->verify_before_fullgc(); 118 } 119 120 heap->set_full_gc_in_progress(true); 121 122 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "must be at a safepoint"); 123 assert(Thread::current()->is_VM_thread(), "Do full GC only while world is stopped"); 124 125 { 126 ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_heapdumps); 127 heap->pre_full_gc_dump(_gc_timer); 128 } 129 130 { 131 ShenandoahGCPhase prepare_phase(ShenandoahPhaseTimings::full_gc_prepare); 132 // Full GC is supposed to recover from any GC state: 133 134 // a1. Cancel evacuation, if in progress 135 if (heap->is_evacuation_in_progress()) { 136 heap->set_evacuation_in_progress(false); 137 } 138 assert(!heap->is_evacuation_in_progress(), "sanity"); 139 140 // a2. Cancel update-refs, if in progress 141 if (heap->is_update_refs_in_progress()) { 142 heap->set_update_refs_in_progress(false); 143 } 144 assert(!heap->is_update_refs_in_progress(), "sanity"); 145 146 // a3. Cancel concurrent partial GC, if in progress 147 if (heap->is_concurrent_partial_in_progress()) { 148 heap->partial_gc()->reset(); 149 heap->set_concurrent_partial_in_progress(false); 150 } 151 152 // a3. Cancel concurrent traversal GC, if in progress 153 if (heap->is_concurrent_traversal_in_progress()) { 154 heap->traversal_gc()->reset(); 155 heap->set_concurrent_traversal_in_progress(false); 156 } 157 158 // b. Cancel concurrent mark, if in progress 159 if (heap->is_concurrent_mark_in_progress()) { 160 heap->concurrentMark()->cancel(); 161 heap->stop_concurrent_marking(); 162 } 163 assert(!heap->is_concurrent_mark_in_progress(), "sanity"); 164 165 // c. Reset the bitmaps for new marking 166 heap->reset_next_mark_bitmap(); 167 assert(heap->is_next_bitmap_clear(), "sanity"); 168 169 // d. Abandon reference discovery and clear all discovered references. 170 ReferenceProcessor* rp = heap->ref_processor(); 171 rp->disable_discovery(); 172 rp->abandon_partial_discovery(); 173 rp->verify_no_references_recorded(); 174 175 { 176 ShenandoahHeapLocker lock(heap->lock()); 177 178 // f. Make sure all regions are active. This is needed because we are potentially 179 // sliding the data through them 180 ShenandoahEnsureHeapActiveClosure ecl; 181 heap->heap_region_iterate(&ecl, false, false); 182 183 // g. Clear region statuses, including collection set status 184 ShenandoahClearRegionStatusClosure cl; 185 heap->heap_region_iterate(&cl, false, false); 186 } 187 } 188 189 BarrierSet* old_bs = BarrierSet::barrier_set(); 190 ShenandoahMarkCompactBarrierSet bs(heap); 191 BarrierSet::set_bs(&bs); 192 193 { 194 if (UseTLAB) { 195 heap->make_tlabs_parsable(true); 196 } 197 198 CodeCache::gc_prologue(); 199 200 // We should save the marks of the currently locked biased monitors. 201 // The marking doesn't preserve the marks of biased objects. 202 //BiasedLocking::preserve_marks(); 203 204 heap->set_has_forwarded_objects(true); 205 206 OrderAccess::fence(); 207 208 phase1_mark_heap(); 209 210 heap->set_full_gc_move_in_progress(true); 211 212 // Setup workers for the rest 213 { 214 OrderAccess::fence(); 215 216 // Initialize worker slices 217 ShenandoahHeapRegionSet** worker_slices = NEW_C_HEAP_ARRAY(ShenandoahHeapRegionSet*, heap->max_workers(), mtGC); 218 for (uint i = 0; i < heap->max_workers(); i++) { 219 worker_slices[i] = new ShenandoahHeapRegionSet(); 220 } 221 222 phase2_calculate_target_addresses(worker_slices); 223 224 OrderAccess::fence(); 225 226 phase3_update_references(); 227 228 phase4_compact_objects(worker_slices); 229 230 // Free worker slices 231 for (uint i = 0; i < heap->max_workers(); i++) { 232 delete worker_slices[i]; 233 } 234 FREE_C_HEAP_ARRAY(ShenandoahHeapRegionSet*, worker_slices); 235 236 CodeCache::gc_epilogue(); 237 JvmtiExport::gc_epilogue(); 238 } 239 240 heap->set_has_forwarded_objects(false); 241 heap->set_full_gc_move_in_progress(false); 242 heap->set_full_gc_in_progress(false); 243 244 if (ShenandoahVerify) { 245 heap->verifier()->verify_after_fullgc(); 246 } 247 } 248 249 { 250 ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_heapdumps); 251 heap->post_full_gc_dump(_gc_timer); 252 } 253 254 if (UseTLAB) { 255 ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_resize_tlabs); 256 heap->resize_all_tlabs(); 257 } 258 259 BarrierSet::set_bs(old_bs); 260 } 261 262 263 if (UseShenandoahMatrix && PrintShenandoahMatrix) { 264 LogTarget(Info, gc) lt; 265 LogStream ls(lt); 266 heap->connection_matrix()->print_on(&ls); 267 } 268 } 269 270 void ShenandoahMarkCompact::phase1_mark_heap() { 271 GCTraceTime(Info, gc, phases) time("Phase 1: Mark live objects", _gc_timer); 272 ShenandoahGCPhase mark_phase(ShenandoahPhaseTimings::full_gc_mark); 273 274 ShenandoahHeap* heap = ShenandoahHeap::heap(); 275 276 ShenandoahConcurrentMark* cm = heap->concurrentMark(); 277 278 // Do not trust heuristics, because this can be our last resort collection. 279 // Only ignore processing references and class unloading if explicitly disabled. 280 heap->set_process_references(ShenandoahRefProcFrequency != 0); 281 heap->set_unload_classes(ShenandoahUnloadClassesFrequency != 0); 282 283 ReferenceProcessor* rp = heap->ref_processor(); 284 // enable ("weak") refs discovery 285 rp->enable_discovery(true /*verify_no_refs*/); 286 rp->setup_policy(true); // snapshot the soft ref policy to be used in this cycle 287 rp->set_active_mt_degree(heap->workers()->active_workers()); 288 289 cm->update_roots(ShenandoahPhaseTimings::full_gc_roots); 290 cm->mark_roots(ShenandoahPhaseTimings::full_gc_roots); 291 cm->shared_finish_mark_from_roots(/* full_gc = */ true); 292 293 heap->swap_mark_bitmaps(); 294 295 if (UseShenandoahMatrix && PrintShenandoahMatrix) { 296 LogTarget(Info, gc) lt; 297 LogStream ls(lt); 298 heap->connection_matrix()->print_on(&ls); 299 } 300 } 301 302 class ShenandoahMCReclaimHumongousRegionClosure : public ShenandoahHeapRegionClosure { 303 private: 304 ShenandoahHeap* const _heap; 305 public: 306 ShenandoahMCReclaimHumongousRegionClosure() : _heap(ShenandoahHeap::heap()) {} 307 308 bool heap_region_do(ShenandoahHeapRegion* r) { 309 if (r->is_humongous_start()) { 310 oop humongous_obj = oop(r->bottom() + BrooksPointer::word_size()); 311 if (!_heap->is_marked_complete(humongous_obj)) { 312 _heap->trash_humongous_region_at(r); 313 } 314 } 315 return false; 316 } 317 }; 318 319 class ShenandoahPrepareForCompactionObjectClosure : public ObjectClosure { 320 private: 321 ShenandoahHeap* const _heap; 322 GrowableArray<ShenandoahHeapRegion*>& _empty_regions; 323 int _empty_regions_pos; 324 ShenandoahHeapRegion* _to_region; 325 ShenandoahHeapRegion* _from_region; 326 HeapWord* _compact_point; 327 328 public: 329 ShenandoahPrepareForCompactionObjectClosure(GrowableArray<ShenandoahHeapRegion*>& empty_regions, ShenandoahHeapRegion* to_region) : 330 _heap(ShenandoahHeap::heap()), 331 _empty_regions(empty_regions), 332 _empty_regions_pos(0), 333 _to_region(to_region), 334 _from_region(NULL), 335 _compact_point(to_region->bottom()) {} 336 337 void set_from_region(ShenandoahHeapRegion* from_region) { 338 _from_region = from_region; 339 } 340 341 void finish_region() { 342 assert(_to_region != NULL, "should not happen"); 343 _to_region->set_new_top(_compact_point); 344 } 345 346 bool is_compact_same_region() { 347 return _from_region == _to_region; 348 } 349 350 int empty_regions_pos() { 351 return _empty_regions_pos; 352 } 353 354 void do_object(oop p) { 355 assert(_from_region != NULL, "must set before work"); 356 assert(_heap->is_marked_complete(p), "must be marked"); 357 assert(!_heap->allocated_after_complete_mark_start((HeapWord*) p), "must be truly marked"); 358 359 size_t obj_size = p->size() + BrooksPointer::word_size(); 360 if (_compact_point + obj_size > _to_region->end()) { 361 finish_region(); 362 363 // Object doesn't fit. Pick next empty region and start compacting there. 364 ShenandoahHeapRegion* new_to_region; 365 if (_empty_regions_pos < _empty_regions.length()) { 366 new_to_region = _empty_regions.at(_empty_regions_pos); 367 _empty_regions_pos++; 368 } else { 369 // Out of empty region? Compact within the same region. 370 new_to_region = _from_region; 371 } 372 373 assert(new_to_region != _to_region, "must not reuse same to-region"); 374 assert(new_to_region != NULL, "must not be NULL"); 375 _to_region = new_to_region; 376 _compact_point = _to_region->bottom(); 377 } 378 379 // Object fits into current region, record new location: 380 assert(_compact_point + obj_size <= _to_region->end(), "must fit"); 381 shenandoah_assert_not_forwarded(NULL, p); 382 BrooksPointer::set_raw(p, _compact_point + BrooksPointer::word_size()); 383 _compact_point += obj_size; 384 } 385 }; 386 387 class ShenandoahPrepareForCompactionTask : public AbstractGangTask { 388 private: 389 ShenandoahHeap* const _heap; 390 ShenandoahHeapRegionSet** const _worker_slices; 391 ShenandoahRegionIterator _heap_regions; 392 393 ShenandoahHeapRegion* next_from_region(ShenandoahHeapRegionSet* slice) { 394 ShenandoahHeapRegion* from_region = _heap_regions.next(); 395 396 while (from_region != NULL && (!from_region->is_move_allowed() || from_region->is_humongous())) { 397 from_region = _heap_regions.next(); 398 } 399 400 if (from_region != NULL) { 401 assert(slice != NULL, "sanity"); 402 assert(!from_region->is_humongous(), "this path cannot handle humongous regions"); 403 assert(from_region->is_move_allowed(), "only regions that can be moved in mark-compact"); 404 slice->add_region(from_region); 405 } 406 407 return from_region; 408 } 409 410 public: 411 ShenandoahPrepareForCompactionTask(ShenandoahHeapRegionSet** worker_slices) : 412 AbstractGangTask("Shenandoah Prepare For Compaction Task"), 413 _heap(ShenandoahHeap::heap()), _heap_regions(_heap->region_iterator()), _worker_slices(worker_slices) { 414 } 415 416 void work(uint worker_id) { 417 ShenandoahHeapRegionSet* slice = _worker_slices[worker_id]; 418 ShenandoahHeapRegion* from_region = next_from_region(slice); 419 // No work? 420 if (from_region == NULL) { 421 return; 422 } 423 424 // Sliding compaction. Walk all regions in the slice, and compact them. 425 // Remember empty regions and reuse them as needed. 426 ResourceMark rm; 427 GrowableArray<ShenandoahHeapRegion*> empty_regions(_heap->num_regions()); 428 ShenandoahPrepareForCompactionObjectClosure cl(empty_regions, from_region); 429 while (from_region != NULL) { 430 cl.set_from_region(from_region); 431 _heap->marked_object_iterate(from_region, &cl); 432 433 // Compacted the region to somewhere else? From-region is empty then. 434 if (!cl.is_compact_same_region()) { 435 empty_regions.append(from_region); 436 } 437 from_region = next_from_region(slice); 438 } 439 cl.finish_region(); 440 441 // Mark all remaining regions as empty 442 for (int pos = cl.empty_regions_pos(); pos < empty_regions.length(); ++pos) { 443 ShenandoahHeapRegion* r = empty_regions.at(pos); 444 r->set_new_top(r->bottom()); 445 } 446 } 447 }; 448 449 void ShenandoahMarkCompact::calculate_target_humongous_objects() { 450 ShenandoahHeap* heap = ShenandoahHeap::heap(); 451 452 // Compute the new addresses for humongous objects. We need to do this after addresses 453 // for regular objects are calculated, and we know what regions in heap suffix are 454 // available for humongous moves. 455 // 456 // Scan the heap backwards, because we are compacting humongous regions towards the end. 457 // Maintain the contiguous compaction window in [to_begin; to_end), so that we can slide 458 // humongous start there. 459 // 460 // The complication is potential non-movable regions during the scan. If such region is 461 // detected, then sliding restarts towards that non-movable region. 462 463 size_t to_begin = heap->num_regions(); 464 size_t to_end = heap->num_regions(); 465 466 for (size_t c = heap->num_regions() - 1; c > 0; c--) { 467 ShenandoahHeapRegion *r = heap->get_region(c); 468 if (r->is_humongous_continuation() || (r->new_top() == r->bottom())) { 469 // To-region candidate: record this, and continue scan 470 to_begin = r->region_number(); 471 continue; 472 } 473 474 if (r->is_humongous_start() && r->is_move_allowed()) { 475 // From-region candidate: movable humongous region 476 oop old_obj = oop(r->bottom() + BrooksPointer::word_size()); 477 size_t words_size = old_obj->size() + BrooksPointer::word_size(); 478 size_t num_regions = ShenandoahHeapRegion::required_regions(words_size * HeapWordSize); 479 480 size_t start = to_end - num_regions; 481 482 if (start >= to_begin && start != r->region_number()) { 483 // Fits into current window, and the move is non-trivial. Record the move then, and continue scan. 484 BrooksPointer::set_raw(old_obj, heap->get_region(start)->bottom() + BrooksPointer::word_size()); 485 to_end = start; 486 continue; 487 } 488 } 489 490 // Failed to fit. Scan starting from current region. 491 to_begin = r->region_number(); 492 to_end = r->region_number(); 493 } 494 } 495 496 void ShenandoahMarkCompact::phase2_calculate_target_addresses(ShenandoahHeapRegionSet** worker_slices) { 497 GCTraceTime(Info, gc, phases) time("Phase 2: Compute new object addresses", _gc_timer); 498 ShenandoahGCPhase calculate_address_phase(ShenandoahPhaseTimings::full_gc_calculate_addresses); 499 500 ShenandoahHeap* heap = ShenandoahHeap::heap(); 501 502 { 503 ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_calculate_addresses_regular); 504 505 { 506 ShenandoahHeapLocker lock(heap->lock()); 507 508 ShenandoahMCReclaimHumongousRegionClosure cl; 509 heap->heap_region_iterate(&cl); 510 511 // After some humongous regions were reclaimed, we need to ensure their 512 // backing storage is active. This is needed because we are potentially 513 // sliding the data through them. 514 ShenandoahEnsureHeapActiveClosure ecl; 515 heap->heap_region_iterate(&ecl, false, false); 516 } 517 518 // Compute the new addresses for regular objects 519 ShenandoahPrepareForCompactionTask prepare_task(worker_slices); 520 heap->workers()->run_task(&prepare_task); 521 } 522 523 // Compute the new addresses for humongous objects 524 { 525 ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_calculate_addresses_humong); 526 calculate_target_humongous_objects(); 527 } 528 } 529 530 class ShenandoahAdjustPointersClosure : public MetadataAwareOopClosure { 531 private: 532 ShenandoahHeap* const _heap; 533 size_t _new_obj_offset; 534 535 template <class T> 536 inline void do_oop_work(T* p) { 537 T o = oopDesc::load_heap_oop(p); 538 if (! oopDesc::is_null(o)) { 539 oop obj = oopDesc::decode_heap_oop_not_null(o); 540 assert(_heap->is_marked_complete(obj), "must be marked"); 541 oop forw = oop(BrooksPointer::get_raw(obj)); 542 oopDesc::encode_store_heap_oop(p, forw); 543 if (UseShenandoahMatrix) { 544 if (_heap->is_in_reserved(p)) { 545 assert(_heap->is_in_reserved(forw), "must be in heap"); 546 assert(_new_obj_offset != SIZE_MAX, "should be set"); 547 // We're moving a to a', which points to b, about to be moved to b'. 548 // We already know b' from the fwd pointer of b. 549 // In the object closure, we see a, and we know a' (by looking at its 550 // fwd ptr). We store the offset in the OopClosure, which is going 551 // to visit all of a's fields, and then, when we see each field, we 552 // subtract the offset from each field address to get the final ptr. 553 _heap->connection_matrix()->set_connected(((HeapWord*) p) - _new_obj_offset, forw); 554 } 555 } 556 } 557 } 558 559 public: 560 ShenandoahAdjustPointersClosure() : _heap(ShenandoahHeap::heap()), _new_obj_offset(SIZE_MAX) {} 561 562 void do_oop(oop* p) { do_oop_work(p); } 563 void do_oop(narrowOop* p) { do_oop_work(p); } 564 565 void set_new_obj_offset(size_t new_obj_offset) { 566 _new_obj_offset = new_obj_offset; 567 } 568 }; 569 570 class ShenandoahAdjustPointersObjectClosure : public ObjectClosure { 571 private: 572 ShenandoahHeap* const _heap; 573 ShenandoahAdjustPointersClosure _cl; 574 575 public: 576 ShenandoahAdjustPointersObjectClosure() : 577 _heap(ShenandoahHeap::heap()) { 578 } 579 void do_object(oop p) { 580 assert(_heap->is_marked_complete(p), "must be marked"); 581 HeapWord* forw = BrooksPointer::get_raw(p); 582 _cl.set_new_obj_offset(pointer_delta((HeapWord*) p, forw)); 583 p->oop_iterate(&_cl); 584 } 585 }; 586 587 class ShenandoahAdjustPointersTask : public AbstractGangTask { 588 private: 589 ShenandoahHeap* const _heap; 590 ShenandoahRegionIterator _regions; 591 592 public: 593 ShenandoahAdjustPointersTask() : 594 AbstractGangTask("Shenandoah Adjust Pointers Task"), 595 _heap(ShenandoahHeap::heap()), _regions(_heap->region_iterator()) { 596 } 597 598 void work(uint worker_id) { 599 ShenandoahAdjustPointersObjectClosure obj_cl; 600 ShenandoahHeapRegion* r = _regions.next(); 601 while (r != NULL) { 602 if (!r->is_humongous_continuation()) { 603 _heap->marked_object_iterate(r, &obj_cl); 604 } 605 r = _regions.next(); 606 } 607 } 608 }; 609 610 class ShenandoahAdjustRootPointersTask : public AbstractGangTask { 611 private: 612 ShenandoahRootProcessor* _rp; 613 614 public: 615 ShenandoahAdjustRootPointersTask(ShenandoahRootProcessor* rp) : 616 AbstractGangTask("Shenandoah Adjust Root Pointers Task"), 617 _rp(rp) {} 618 619 void work(uint worker_id) { 620 ShenandoahAdjustPointersClosure cl; 621 CLDToOopClosure adjust_cld_closure(&cl, true); 622 MarkingCodeBlobClosure adjust_code_closure(&cl, 623 CodeBlobToOopClosure::FixRelocations); 624 625 _rp->process_all_roots(&cl, &cl, 626 &adjust_cld_closure, 627 &adjust_code_closure, NULL, worker_id); 628 } 629 }; 630 631 void ShenandoahMarkCompact::phase3_update_references() { 632 GCTraceTime(Info, gc, phases) time("Phase 3: Adjust pointers", _gc_timer); 633 ShenandoahGCPhase adjust_pointer_phase(ShenandoahPhaseTimings::full_gc_adjust_pointers); 634 635 ShenandoahHeap* heap = ShenandoahHeap::heap(); 636 637 if (UseShenandoahMatrix) { 638 heap->connection_matrix()->clear_all(); 639 } 640 641 WorkGang* workers = heap->workers(); 642 uint nworkers = workers->active_workers(); 643 { 644 #if COMPILER2_OR_JVMCI 645 DerivedPointerTable::clear(); 646 #endif 647 ShenandoahRootProcessor rp(heap, nworkers, ShenandoahPhaseTimings::full_gc_roots); 648 ShenandoahAdjustRootPointersTask task(&rp); 649 workers->run_task(&task); 650 #if COMPILER2_OR_JVMCI 651 DerivedPointerTable::update_pointers(); 652 #endif 653 } 654 655 ShenandoahAdjustPointersTask adjust_pointers_task; 656 workers->run_task(&adjust_pointers_task); 657 } 658 659 class ShenandoahCompactObjectsClosure : public ObjectClosure { 660 private: 661 ShenandoahHeap* const _heap; 662 uint const _worker_id; 663 664 public: 665 ShenandoahCompactObjectsClosure(uint worker_id) : 666 _heap(ShenandoahHeap::heap()), _worker_id(worker_id) {} 667 668 void do_object(oop p) { 669 assert(_heap->is_marked_complete(p), "must be marked"); 670 size_t size = (size_t)p->size(); 671 HeapWord* compact_to = BrooksPointer::get_raw(p); 672 HeapWord* compact_from = (HeapWord*) p; 673 if (compact_from != compact_to) { 674 Copy::aligned_conjoint_words(compact_from, compact_to, size); 675 } 676 oop new_obj = oop(compact_to); 677 BrooksPointer::initialize(new_obj); 678 } 679 }; 680 681 class ShenandoahCompactObjectsTask : public AbstractGangTask { 682 private: 683 ShenandoahHeap* const _heap; 684 ShenandoahHeapRegionSet** const _worker_slices; 685 686 public: 687 ShenandoahCompactObjectsTask(ShenandoahHeapRegionSet** worker_slices) : 688 AbstractGangTask("Shenandoah Compact Objects Task"), 689 _heap(ShenandoahHeap::heap()), 690 _worker_slices(worker_slices) { 691 } 692 693 void work(uint worker_id) { 694 ShenandoahHeapRegionSetIterator slice = _worker_slices[worker_id]->iterator(); 695 696 ShenandoahCompactObjectsClosure cl(worker_id); 697 ShenandoahHeapRegion* r = slice.next(); 698 while (r != NULL) { 699 assert(!r->is_humongous(), "must not get humongous regions here"); 700 _heap->marked_object_iterate(r, &cl); 701 r->set_top(r->new_top()); 702 r = slice.next(); 703 } 704 } 705 }; 706 707 class ShenandoahPostCompactClosure : public ShenandoahHeapRegionClosure { 708 private: 709 ShenandoahHeap* const _heap; 710 size_t _live; 711 712 public: 713 ShenandoahPostCompactClosure() : _live(0), _heap(ShenandoahHeap::heap()) { 714 _heap->free_set()->clear(); 715 } 716 717 bool heap_region_do(ShenandoahHeapRegion* r) { 718 assert (!r->is_cset(), "cset regions should have been demoted already"); 719 720 // Need to reset the complete-top-at-mark-start pointer here because 721 // the complete marking bitmap is no longer valid. This ensures 722 // size-based iteration in marked_object_iterate(). 723 // NOTE: See blurb at ShenandoahMCResetCompleteBitmapTask on why we need to skip 724 // pinned regions. 725 if (!r->is_pinned()) { 726 _heap->set_complete_top_at_mark_start(r->bottom(), r->bottom()); 727 } 728 729 size_t live = r->used(); 730 731 // Make empty regions that have been allocated into regular 732 if (r->is_empty() && live > 0) { 733 r->make_regular_bypass(); 734 } 735 736 // Reclaim regular regions that became empty 737 if (r->is_regular() && live == 0) { 738 r->make_trash(); 739 } 740 741 // Recycle all trash regions 742 if (r->is_trash()) { 743 live = 0; 744 r->recycle(); 745 } 746 747 r->set_live_data(live); 748 r->reset_alloc_metadata_to_shared(); 749 _live += live; 750 return false; 751 } 752 753 size_t get_live() { 754 return _live; 755 } 756 }; 757 758 void ShenandoahMarkCompact::compact_humongous_objects() { 759 // Compact humongous regions, based on their fwdptr objects. 760 // 761 // This code is serial, because doing the in-slice parallel sliding is tricky. In most cases, 762 // humongous regions are already compacted, and do not require further moves, which alleviates 763 // sliding costs. We may consider doing this in parallel in future. 764 765 ShenandoahHeap* heap = ShenandoahHeap::heap(); 766 767 for (size_t c = heap->num_regions() - 1; c > 0; c--) { 768 ShenandoahHeapRegion* r = heap->get_region(c); 769 if (r->is_humongous_start()) { 770 oop old_obj = oop(r->bottom() + BrooksPointer::word_size()); 771 size_t words_size = old_obj->size() + BrooksPointer::word_size(); 772 size_t num_regions = ShenandoahHeapRegion::required_regions(words_size * HeapWordSize); 773 774 size_t old_start = r->region_number(); 775 size_t old_end = old_start + num_regions - 1; 776 size_t new_start = heap->heap_region_index_containing(BrooksPointer::get_raw(old_obj)); 777 size_t new_end = new_start + num_regions - 1; 778 779 if (old_start == new_start) { 780 // No need to move the object, it stays at the same slot 781 continue; 782 } 783 784 assert (r->is_move_allowed(), "should be movable"); 785 786 Copy::aligned_conjoint_words(heap->get_region(old_start)->bottom(), 787 heap->get_region(new_start)->bottom(), 788 ShenandoahHeapRegion::region_size_words()*num_regions); 789 790 oop new_obj = oop(heap->get_region(new_start)->bottom() + BrooksPointer::word_size()); 791 BrooksPointer::initialize(new_obj); 792 793 { 794 ShenandoahHeapLocker lock(heap->lock()); 795 796 for (size_t c = old_start; c <= old_end; c++) { 797 ShenandoahHeapRegion* r = heap->get_region(c); 798 r->make_regular_bypass(); 799 r->set_top(r->bottom()); 800 } 801 802 for (size_t c = new_start; c <= new_end; c++) { 803 ShenandoahHeapRegion* r = heap->get_region(c); 804 if (c == new_start) { 805 r->make_humongous_start_bypass(); 806 } else { 807 r->make_humongous_cont_bypass(); 808 } 809 810 // Trailing region may be non-full, record the remainder there 811 size_t remainder = words_size & ShenandoahHeapRegion::region_size_words_mask(); 812 if ((c == new_end) && (remainder != 0)) { 813 r->set_top(r->bottom() + remainder); 814 } else { 815 r->set_top(r->end()); 816 } 817 818 r->reset_alloc_metadata_to_shared(); 819 } 820 } 821 } 822 } 823 } 824 825 // This is slightly different to ShHeap::reset_next_mark_bitmap: 826 // we need to remain able to walk pinned regions. 827 // Since pinned region do not move and don't get compacted, we will get holes with 828 // unreachable objects in them (which may have pointers to unloaded Klasses and thus 829 // cannot be iterated over using oop->size(). The only way to safely iterate over those is using 830 // a valid marking bitmap and valid TAMS pointer. This class only resets marking 831 // bitmaps for un-pinned regions, and later we only reset TAMS for unpinned regions. 832 class ShenandoahMCResetCompleteBitmapTask : public AbstractGangTask { 833 private: 834 ShenandoahRegionIterator _regions; 835 836 public: 837 ShenandoahMCResetCompleteBitmapTask(ShenandoahRegionIterator regions) : 838 AbstractGangTask("Parallel Reset Bitmap Task"), 839 _regions(regions) { 840 } 841 842 void work(uint worker_id) { 843 ShenandoahHeapRegion* region = _regions.next(); 844 ShenandoahHeap* heap = ShenandoahHeap::heap(); 845 while (region != NULL) { 846 if (heap->is_bitmap_slice_committed(region) && !region->is_pinned()) { 847 HeapWord* bottom = region->bottom(); 848 HeapWord* top = heap->complete_top_at_mark_start(region->bottom()); 849 if (top > bottom) { 850 heap->complete_mark_bit_map()->clear_range_large(MemRegion(bottom, top)); 851 } 852 assert(heap->is_complete_bitmap_clear_range(bottom, region->end()), "must be clear"); 853 } 854 region = _regions.next(); 855 } 856 } 857 }; 858 859 void ShenandoahMarkCompact::phase4_compact_objects(ShenandoahHeapRegionSet** worker_slices) { 860 GCTraceTime(Info, gc, phases) time("Phase 4: Move objects", _gc_timer); 861 ShenandoahGCPhase compaction_phase(ShenandoahPhaseTimings::full_gc_copy_objects); 862 863 ShenandoahHeap* heap = ShenandoahHeap::heap(); 864 865 // Compact regular objects first 866 { 867 ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_regular); 868 ShenandoahCompactObjectsTask compact_task(worker_slices); 869 heap->workers()->run_task(&compact_task); 870 } 871 872 // Compact humongous objects after regular object moves 873 { 874 ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_humong); 875 compact_humongous_objects(); 876 } 877 878 // Reset complete bitmap. We're about to reset the complete-top-at-mark-start pointer 879 // and must ensure the bitmap is in sync. 880 ShenandoahMCResetCompleteBitmapTask task(heap->region_iterator()); 881 heap->workers()->run_task(&task); 882 883 // Bring regions in proper states after the collection, and set heap properties. 884 { 885 ShenandoahHeapLocker lock(heap->lock()); 886 ShenandoahPostCompactClosure post_compact; 887 heap->heap_region_iterate(&post_compact); 888 heap->set_used(post_compact.get_live()); 889 890 heap->collection_set()->clear(); 891 heap->free_set()->rebuild(); 892 } 893 894 heap->clear_cancelled_concgc(); 895 896 // Also clear the next bitmap in preparation for next marking. 897 heap->reset_next_mark_bitmap(); 898 }