1 /* 2 * Copyright (c) 2014, 2015, 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 #include "code/codeCache.hpp" 26 #include "gc/shared/gcTraceTime.inline.hpp" 27 #include "gc/shared/isGCActiveMark.hpp" 28 #include "gc/shenandoah/brooksPointer.hpp" 29 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp" 30 #include "gc/shenandoah/shenandoahConcurrentMark.inline.hpp" 31 #include "gc/shenandoah/shenandoahMarkCompact.hpp" 32 #include "gc/shenandoah/shenandoahBarrierSet.hpp" 33 #include "gc/shenandoah/shenandoahHeapRegionSet.hpp" 34 #include "gc/shenandoah/shenandoahHeap.hpp" 35 #include "gc/shenandoah/shenandoahHeap.inline.hpp" 36 #include "gc/shenandoah/shenandoahRootProcessor.hpp" 37 #include "gc/shenandoah/vm_operations_shenandoah.hpp" 38 #include "oops/oop.inline.hpp" 39 #include "runtime/biasedLocking.hpp" 40 #include "runtime/thread.hpp" 41 #include "utilities/copy.hpp" 42 #include "gc/shared/taskqueue.inline.hpp" 43 #include "gc/shared/workgroup.hpp" 44 45 class ShenandoahMarkCompactBarrierSet : public ShenandoahBarrierSet { 46 public: 47 ShenandoahMarkCompactBarrierSet(ShenandoahHeap* heap) : ShenandoahBarrierSet(heap) { 48 } 49 oop read_barrier(oop src) { 50 return src; 51 } 52 #ifdef ASSERT 53 bool is_safe(oop o) { 54 if (o == NULL) return true; 55 if (! oopDesc::unsafe_equals(o, read_barrier(o))) { 56 return false; 57 } 58 return true; 59 } 60 bool is_safe(narrowOop o) { 61 oop obj = oopDesc::decode_heap_oop(o); 62 return is_safe(obj); 63 } 64 #endif 65 }; 66 67 class ClearInCollectionSetHeapRegionClosure: public ShenandoahHeapRegionClosure { 68 private: 69 ShenandoahHeap* _heap; 70 public: 71 72 ClearInCollectionSetHeapRegionClosure() : _heap(ShenandoahHeap::heap()) { 73 } 74 75 bool doHeapRegion(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 STWGCTimer* ShenandoahMarkCompact::_gc_timer = NULL; 84 85 void ShenandoahMarkCompact::initialize() { 86 _gc_timer = new (ResourceObj::C_HEAP, mtGC) STWGCTimer(); 87 } 88 89 void ShenandoahMarkCompact::do_mark_compact(GCCause::Cause gc_cause) { 90 91 ShenandoahHeap* _heap = ShenandoahHeap::heap(); 92 ShenandoahCollectorPolicy* policy = _heap->shenandoahPolicy(); 93 94 _gc_timer->register_gc_start(); 95 96 _heap->set_full_gc_in_progress(true); 97 98 assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint"); 99 IsGCActiveMark is_active; 100 101 assert(Thread::current()->is_VM_thread(), "Do full GC only while world is stopped"); 102 103 policy->record_phase_start(ShenandoahCollectorPolicy::full_gc); 104 105 policy->record_phase_start(ShenandoahCollectorPolicy::full_gc_heapdumps); 106 _heap->pre_full_gc_dump(_gc_timer); 107 policy->record_phase_end(ShenandoahCollectorPolicy::full_gc_heapdumps); 108 109 policy->record_phase_start(ShenandoahCollectorPolicy::full_gc_prepare); 110 111 // Full GC is supposed to recover from any GC state: 112 113 // a. Cancel concurrent mark, if in progress 114 if (_heap->concurrent_mark_in_progress()) { 115 _heap->concurrentMark()->cancel(); 116 _heap->stop_concurrent_marking(); 117 } 118 assert(!_heap->concurrent_mark_in_progress(), "sanity"); 119 120 // b. Cancel evacuation, if in progress 121 if (_heap->is_evacuation_in_progress()) { 122 _heap->set_evacuation_in_progress_at_safepoint(false); 123 } 124 assert(!_heap->is_evacuation_in_progress(), "sanity"); 125 126 // c. Reset the bitmaps for new marking 127 _heap->reset_next_mark_bitmap(_heap->workers()); 128 assert(_heap->is_next_bitmap_clear(), "sanity"); 129 130 ClearInCollectionSetHeapRegionClosure cl; 131 _heap->heap_region_iterate(&cl, false, false); 132 133 /* 134 if (ShenandoahVerify) { 135 // Full GC should only be called between regular concurrent cycles, therefore 136 // those verifications should be valid. 137 _heap->verify_heap_after_evacuation(); 138 _heap->verify_heap_after_update_refs(); 139 } 140 */ 141 142 BarrierSet* old_bs = oopDesc::bs(); 143 ShenandoahMarkCompactBarrierSet bs(_heap); 144 oopDesc::set_bs(&bs); 145 146 policy->record_phase_end(ShenandoahCollectorPolicy::full_gc_prepare); 147 148 { 149 GCTraceTime(Info, gc) time("Pause Full", _gc_timer, gc_cause, true); 150 151 if (UseTLAB) { 152 _heap->ensure_parsability(true); 153 } 154 155 CodeCache::gc_prologue(); 156 157 // We should save the marks of the currently locked biased monitors. 158 // The marking doesn't preserve the marks of biased objects. 159 //BiasedLocking::preserve_marks(); 160 161 _heap->set_need_update_refs(true); 162 WorkGang* workers = _heap->workers(); 163 164 // Setup workers for phase 1 165 { 166 uint nworkers = ShenandoahCollectorPolicy::calc_workers_for_init_marking( 167 workers->active_workers(), Threads::number_of_non_daemon_threads()); 168 workers->update_active_workers(nworkers); 169 ShenandoahWorkerScope scope(workers, nworkers); 170 171 OrderAccess::fence(); 172 173 policy->record_phase_start(ShenandoahCollectorPolicy::full_gc_mark); 174 phase1_mark_heap(); 175 policy->record_phase_end(ShenandoahCollectorPolicy::full_gc_mark); 176 } 177 178 // Setup workers for the rest 179 { 180 uint nworkers = ShenandoahCollectorPolicy::calc_workers_for_parallel_evacuation( 181 workers->active_workers(), Threads::number_of_non_daemon_threads()); 182 183 ShenandoahWorkerScope scope(workers, nworkers); 184 185 OrderAccess::fence(); 186 187 policy->record_phase_start(ShenandoahCollectorPolicy::full_gc_calculate_addresses); 188 ShenandoahHeapRegionSet* copy_queues[_heap->max_workers()]; 189 phase2_calculate_target_addresses(copy_queues); 190 policy->record_phase_end(ShenandoahCollectorPolicy::full_gc_calculate_addresses); 191 192 OrderAccess::fence(); 193 194 policy->record_phase_start(ShenandoahCollectorPolicy::full_gc_adjust_pointers); 195 phase3_update_references(); 196 policy->record_phase_end(ShenandoahCollectorPolicy::full_gc_adjust_pointers); 197 198 policy->record_phase_start(ShenandoahCollectorPolicy::full_gc_copy_objects); 199 phase4_compact_objects(copy_queues); 200 policy->record_phase_end(ShenandoahCollectorPolicy::full_gc_copy_objects); 201 202 CodeCache::gc_epilogue(); 203 JvmtiExport::gc_epilogue(); 204 } 205 206 // refs processing: clean slate 207 // rp.enqueue_discovered_references(); 208 209 if (ShenandoahVerify) { 210 _heap->verify_heap_after_evacuation(); 211 } 212 213 _heap->set_bytes_allocated_since_cm(0); 214 215 _heap->set_need_update_refs(false); 216 217 _heap->set_full_gc_in_progress(false); 218 } 219 220 _gc_timer->register_gc_end(); 221 222 policy->record_full_gc(); 223 224 policy->record_phase_start(ShenandoahCollectorPolicy::full_gc_heapdumps); 225 _heap->post_full_gc_dump(_gc_timer); 226 policy->record_phase_end(ShenandoahCollectorPolicy::full_gc_heapdumps); 227 228 policy->record_phase_end(ShenandoahCollectorPolicy::full_gc); 229 230 oopDesc::set_bs(old_bs); 231 232 if (UseShenandoahMatrix) { 233 if (PrintShenandoahMatrix) { 234 outputStream* log = Log(gc)::info_stream(); 235 _heap->connection_matrix()->print_on(log); 236 } 237 } 238 } 239 240 #ifdef ASSERT 241 class VerifyNotForwardedPointersClosure : public MetadataAwareOopClosure { 242 private: 243 template <class T> 244 inline void do_oop_work(T* p) { 245 T o = oopDesc::load_heap_oop(p); 246 if (! oopDesc::is_null(o)) { 247 oop obj = oopDesc::decode_heap_oop_not_null(o); 248 assert(oopDesc::unsafe_equals(obj, ShenandoahBarrierSet::resolve_oop_static_not_null(obj)), 249 "expect forwarded oop"); 250 ShenandoahHeap* heap = ShenandoahHeap::heap(); 251 if (! heap->is_marked_complete(obj)) { 252 tty->print_cr("ref region humongous? %s", BOOL_TO_STR(heap->heap_region_containing(p)->is_humongous())); 253 } 254 assert(heap->is_marked_complete(obj), "must be marked"); 255 assert(! heap->allocated_after_complete_mark_start((HeapWord*) obj), "must be truly marked"); 256 } 257 } 258 public: 259 void do_oop(oop* p) { 260 do_oop_work(p); 261 } 262 void do_oop(narrowOop* p) { 263 do_oop_work(p); 264 } 265 }; 266 267 class ShenandoahMCVerifyAfterMarkingObjectClosure : public ObjectClosure { 268 public: 269 void do_object(oop p) { 270 ShenandoahHeap* heap = ShenandoahHeap::heap(); 271 assert(oopDesc::unsafe_equals(p, ShenandoahBarrierSet::resolve_oop_static_not_null(p)), 272 "expect forwarded oop"); 273 assert(heap->is_marked_complete(p), "must be marked"); 274 assert(! heap->allocated_after_complete_mark_start((HeapWord*) p), "must be truly marked"); 275 VerifyNotForwardedPointersClosure cl; 276 p->oop_iterate(&cl); 277 } 278 }; 279 280 class ShenandoahMCVerifyAfterMarkingRegionClosure : public ShenandoahHeapRegionClosure { 281 bool doHeapRegion(ShenandoahHeapRegion* r) { 282 ShenandoahMCVerifyAfterMarkingObjectClosure cl; 283 if (! r->is_humongous_continuation()) { 284 ShenandoahHeap::heap()->marked_object_iterate(r, &cl); 285 } 286 return false; 287 } 288 }; 289 290 #endif 291 292 void ShenandoahMarkCompact::phase1_mark_heap() { 293 GCTraceTime(Info, gc, phases) time("Phase 1: Mark live objects", _gc_timer); 294 ShenandoahHeap* _heap = ShenandoahHeap::heap(); 295 296 ShenandoahConcurrentMark* cm = _heap->concurrentMark(); 297 298 cm->set_process_references(true); 299 cm->set_unload_classes(true); 300 301 ReferenceProcessor* rp = _heap->ref_processor(); 302 // enable ("weak") refs discovery 303 rp->enable_discovery(true /*verify_no_refs*/); 304 rp->setup_policy(true); // snapshot the soft ref policy to be used in this cycle 305 rp->set_active_mt_degree(_heap->workers()->active_workers()); 306 307 COMPILER2_PRESENT(DerivedPointerTable::clear()); 308 cm->update_roots(); 309 COMPILER2_PRESENT(DerivedPointerTable::update_pointers()); 310 311 cm->mark_roots(); 312 cm->shared_finish_mark_from_roots(/* full_gc = */ true); 313 314 _heap->swap_mark_bitmaps(); 315 316 if (UseShenandoahMatrix) { 317 if (PrintShenandoahMatrix) { 318 outputStream* log = Log(gc)::info_stream(); 319 _heap->connection_matrix()->print_on(log); 320 } 321 } 322 323 if (ShenandoahVerify || (UseShenandoahMatrix && VerifyShenandoahMatrix)) { 324 _heap->verify_heap_reachable_at_safepoint(); 325 } 326 327 if (VerifyDuringGC) { 328 HandleMark hm; // handle scope 329 // Universe::heap()->prepare_for_verify(); 330 _heap->prepare_for_verify(); 331 // Note: we can verify only the heap here. When an object is 332 // marked, the previous value of the mark word (including 333 // identity hash values, ages, etc) is preserved, and the mark 334 // word is set to markOop::marked_value - effectively removing 335 // any hash values from the mark word. These hash values are 336 // used when verifying the dictionaries and so removing them 337 // from the mark word can make verification of the dictionaries 338 // fail. At the end of the GC, the original mark word values 339 // (including hash values) are restored to the appropriate 340 // objects. 341 // Universe::heap()->verify(VerifySilently, VerifyOption_G1UseMarkWord); 342 _heap->verify(VerifyOption_G1UseMarkWord); 343 } 344 345 #ifdef ASSERT 346 ShenandoahMCVerifyAfterMarkingRegionClosure cl; 347 _heap->heap_region_iterate(&cl); 348 #endif 349 } 350 351 class ShenandoahMCReclaimHumongousRegionClosure : public ShenandoahHeapRegionClosure { 352 private: 353 ShenandoahHeap* _heap; 354 public: 355 ShenandoahMCReclaimHumongousRegionClosure() : _heap(ShenandoahHeap::heap()) { 356 } 357 358 bool doHeapRegion(ShenandoahHeapRegion* r) { 359 if (r->is_humongous_start()) { 360 oop humongous_obj = oop(r->bottom() + BrooksPointer::word_size()); 361 if (! _heap->is_marked_complete(humongous_obj)) { 362 _heap->reclaim_humongous_region_at(r); 363 } 364 } 365 return false; 366 } 367 }; 368 369 370 class ShenandoahPrepareForCompactionObjectClosure : public ObjectClosure { 371 372 private: 373 374 ShenandoahHeap* _heap; 375 ShenandoahHeapRegionSet* _to_regions; 376 ShenandoahHeapRegion* _to_region; 377 ShenandoahHeapRegion* _from_region; 378 HeapWord* _compact_point; 379 380 public: 381 382 ShenandoahPrepareForCompactionObjectClosure(ShenandoahHeapRegionSet* to_regions, ShenandoahHeapRegion* to_region) : 383 _heap(ShenandoahHeap::heap()), 384 _to_regions(to_regions), 385 _to_region(to_region), 386 _from_region(NULL), 387 _compact_point(to_region->bottom()) { 388 } 389 390 void set_from_region(ShenandoahHeapRegion* from_region) { 391 _from_region = from_region; 392 } 393 394 ShenandoahHeapRegion* to_region() const { 395 return _to_region; 396 } 397 HeapWord* compact_point() const { 398 return _compact_point; 399 } 400 void do_object(oop p) { 401 assert(_from_region != NULL, "must set before work"); 402 assert(_heap->is_marked_complete(p), "must be marked"); 403 assert(! _heap->allocated_after_complete_mark_start((HeapWord*) p), "must be truly marked"); 404 size_t size = p->size(); 405 size_t obj_size = size + BrooksPointer::word_size(); 406 if (_compact_point + obj_size > _to_region->end()) { 407 // Object doesn't fit. Pick next to-region and start compacting there. 408 _to_region->set_new_top(_compact_point); 409 ShenandoahHeapRegion* new_to_region = _to_regions->current(); 410 _to_regions->next(); 411 if (new_to_region == NULL) { 412 new_to_region = _from_region; 413 } 414 assert(new_to_region != _to_region, "must not reuse same to-region"); 415 assert(new_to_region != NULL, "must not be NULL"); 416 _to_region = new_to_region; 417 _compact_point = _to_region->bottom(); 418 } 419 assert(_compact_point + obj_size <= _to_region->end(), "must fit"); 420 assert(oopDesc::unsafe_equals(p, ShenandoahBarrierSet::resolve_oop_static_not_null(p)), 421 "expect forwarded oop"); 422 BrooksPointer::set_raw(p, _compact_point + BrooksPointer::word_size()); 423 _compact_point += obj_size; 424 } 425 }; 426 427 class ShenandoahPrepareForCompactionTask : public AbstractGangTask { 428 private: 429 430 ShenandoahHeapRegionSet** _copy_queues; 431 ShenandoahHeapRegionSet* _from_regions; 432 433 ShenandoahHeapRegion* next_from_region(ShenandoahHeapRegionSet* copy_queue) { 434 ShenandoahHeapRegion* from_region = _from_regions->claim_next(); 435 while (from_region != NULL && (from_region->is_humongous() || from_region->is_pinned())) { 436 from_region = _from_regions->claim_next(); 437 } 438 if (from_region != NULL) { 439 assert(copy_queue != NULL, "sanity"); 440 assert(! from_region->is_humongous(), "must not get humongous regions here"); 441 assert(! from_region->is_pinned(), "no pinned region in mark-compact"); 442 copy_queue->add_region(from_region); 443 } 444 return from_region; 445 } 446 447 public: 448 ShenandoahPrepareForCompactionTask(ShenandoahHeapRegionSet* from_regions, ShenandoahHeapRegionSet** copy_queues) : 449 AbstractGangTask("Shenandoah Prepare For Compaction Task"), 450 _from_regions(from_regions), _copy_queues(copy_queues) { 451 } 452 453 void work(uint worker_id) { 454 ShenandoahHeap* heap = ShenandoahHeap::heap(); 455 ShenandoahHeapRegionSet* copy_queue = _copy_queues[worker_id]; 456 ShenandoahHeapRegion* from_region = next_from_region(copy_queue); 457 if (from_region == NULL) return; 458 ShenandoahHeapRegionSet* to_regions = new ShenandoahHeapRegionSet(ShenandoahHeap::heap()->max_regions()); 459 ShenandoahPrepareForCompactionObjectClosure cl(to_regions, from_region); 460 while (from_region != NULL) { 461 assert(from_region != NULL, "sanity"); 462 cl.set_from_region(from_region); 463 heap->marked_object_iterate(from_region, &cl); 464 if (from_region != cl.to_region()) { 465 assert(from_region != NULL, "sanity"); 466 to_regions->add_region(from_region); 467 } 468 from_region = next_from_region(copy_queue); 469 } 470 assert(cl.to_region() != NULL, "should not happen"); 471 cl.to_region()->set_new_top(cl.compact_point()); 472 while (to_regions->count() > 0) { 473 ShenandoahHeapRegion* r = to_regions->current(); 474 to_regions->next(); 475 if (r == NULL) { 476 to_regions->print(); 477 } 478 assert(r != NULL, "should not happen"); 479 r->set_new_top(r->bottom()); 480 } 481 delete to_regions; 482 } 483 }; 484 485 void ShenandoahMarkCompact::phase2_calculate_target_addresses(ShenandoahHeapRegionSet** copy_queues) { 486 GCTraceTime(Info, gc, phases) time("Phase 2: Compute new object addresses", _gc_timer); 487 ShenandoahHeap* heap = ShenandoahHeap::heap(); 488 489 ShenandoahMCReclaimHumongousRegionClosure cl; 490 heap->heap_region_iterate(&cl); 491 492 // Initialize copy queues. 493 for (uint i = 0; i < heap->max_workers(); i++) { 494 copy_queues[i] = new ShenandoahHeapRegionSet(heap->max_regions()); 495 } 496 497 ShenandoahHeapRegionSet* from_regions = heap->regions(); 498 from_regions->clear_current_index(); 499 ShenandoahPrepareForCompactionTask prepare_task(from_regions, copy_queues); 500 heap->workers()->run_task(&prepare_task); 501 } 502 503 class ShenandoahAdjustPointersClosure : public MetadataAwareOopClosure { 504 private: 505 ShenandoahHeap* _heap; 506 size_t _new_obj_offset; 507 public: 508 509 ShenandoahAdjustPointersClosure() : _heap(ShenandoahHeap::heap()) { 510 } 511 512 private: 513 template <class T> 514 inline void do_oop_work(T* p) { 515 T o = oopDesc::load_heap_oop(p); 516 if (! oopDesc::is_null(o)) { 517 oop obj = oopDesc::decode_heap_oop_not_null(o); 518 assert(_heap->is_marked_complete(obj), "must be marked"); 519 oop forw = oop(BrooksPointer::get_raw(obj)); 520 oopDesc::encode_store_heap_oop(p, forw); 521 if (UseShenandoahMatrix) { 522 if (_heap->is_in_reserved(p)) { 523 assert(_heap->is_in_reserved(forw), "must be in heap"); 524 // We're moving a to a', which points to b, about to be moved to b'. 525 // We already know b' from the fwd pointer of b. 526 // In the object closure, we see a, and we know a' (by looking at its 527 // fwd ptr). We store the offset in the OopClosure, which is going 528 // to visit all of a's fields, and then, when we see each field, we 529 // subtract the offset from each field address to get the final ptr. 530 _heap->connection_matrix()->set_connected(((HeapWord*) p) - _new_obj_offset, forw); 531 } 532 } 533 } 534 } 535 public: 536 void do_oop(oop* p) { 537 do_oop_work(p); 538 } 539 void do_oop(narrowOop* p) { 540 do_oop_work(p); 541 } 542 void set_new_obj_offset(size_t new_obj_offset) { 543 _new_obj_offset = new_obj_offset; 544 } 545 }; 546 547 class ShenandoahAdjustPointersObjectClosure : public ObjectClosure { 548 private: 549 ShenandoahAdjustPointersClosure _cl; 550 ShenandoahHeap* _heap; 551 public: 552 ShenandoahAdjustPointersObjectClosure() : 553 _heap(ShenandoahHeap::heap()) { 554 } 555 void do_object(oop p) { 556 assert(_heap->is_marked_complete(p), "must be marked"); 557 HeapWord* forw = BrooksPointer::get_raw(p); 558 _cl.set_new_obj_offset(pointer_delta((HeapWord*) p, forw)); 559 p->oop_iterate(&_cl); 560 } 561 }; 562 563 class ShenandoahAdjustPointersTask : public AbstractGangTask { 564 private: 565 ShenandoahHeapRegionSet* _regions; 566 public: 567 568 ShenandoahAdjustPointersTask(ShenandoahHeapRegionSet* regions) : 569 AbstractGangTask("Shenandoah Adjust Pointers Task"), 570 _regions(regions) { 571 } 572 573 void work(uint worker_id) { 574 ShenandoahHeap* heap = ShenandoahHeap::heap(); 575 ShenandoahHeapRegion* r = _regions->claim_next(); 576 ShenandoahAdjustPointersObjectClosure obj_cl; 577 while (r != NULL) { 578 if (! r->is_humongous_continuation()) { 579 heap->marked_object_iterate(r, &obj_cl); 580 } 581 r = _regions->claim_next(); 582 } 583 } 584 }; 585 586 class ShenandoahAdjustRootPointersTask : public AbstractGangTask { 587 private: 588 ShenandoahRootProcessor* _rp; 589 590 public: 591 592 ShenandoahAdjustRootPointersTask(ShenandoahRootProcessor* rp) : 593 AbstractGangTask("Shenandoah Adjust Root Pointers Task"), 594 _rp(rp) { 595 } 596 597 void work(uint worker_id) { 598 ShenandoahAdjustPointersClosure cl; 599 CLDToOopClosure adjust_cld_closure(&cl, true); 600 MarkingCodeBlobClosure adjust_code_closure(&cl, 601 CodeBlobToOopClosure::FixRelocations); 602 603 _rp->process_all_roots(&cl, &cl, 604 &adjust_cld_closure, 605 &adjust_code_closure, worker_id); 606 } 607 }; 608 609 void ShenandoahMarkCompact::phase3_update_references() { 610 GCTraceTime(Info, gc, phases) time("Phase 2: Adjust pointers", _gc_timer); 611 ShenandoahHeap* heap = ShenandoahHeap::heap(); 612 613 if (UseShenandoahMatrix) { 614 heap->connection_matrix()->clear_all(); 615 } 616 617 // Need cleared claim bits for the roots processing 618 ClassLoaderDataGraph::clear_claimed_marks(); 619 620 WorkGang* workers = heap->workers(); 621 uint nworkers = workers->active_workers(); 622 { 623 COMPILER2_PRESENT(DerivedPointerTable::clear()); 624 625 ShenandoahRootProcessor rp(heap, nworkers); 626 ShenandoahAdjustRootPointersTask task(&rp); 627 workers->run_task(&task); 628 COMPILER2_PRESENT(DerivedPointerTable::update_pointers()); 629 } 630 631 ShenandoahHeapRegionSet* regions = heap->regions(); 632 regions->clear_current_index(); 633 ShenandoahAdjustPointersTask adjust_pointers_task(regions); 634 workers->run_task(&adjust_pointers_task); 635 } 636 637 class ShenandoahCompactObjectsClosure : public ObjectClosure { 638 private: 639 ShenandoahHeap* _heap; 640 public: 641 ShenandoahCompactObjectsClosure() : _heap(ShenandoahHeap::heap()) { 642 } 643 void do_object(oop p) { 644 assert(_heap->is_marked_complete(p), "must be marked"); 645 size_t size = p->size(); 646 HeapWord* compact_to = BrooksPointer::get_raw(p); 647 HeapWord* compact_from = (HeapWord*) p; 648 if (compact_from != compact_to) { 649 Copy::aligned_conjoint_words(compact_from, compact_to, size); 650 } 651 oop new_obj = oop(compact_to); 652 // new_obj->init_mark(); 653 BrooksPointer::initialize(new_obj); 654 } 655 }; 656 657 class ShenandoahCompactObjectsTask : public AbstractGangTask { 658 ShenandoahHeapRegionSet** _regions; 659 public: 660 ShenandoahCompactObjectsTask(ShenandoahHeapRegionSet** regions) : 661 AbstractGangTask("Shenandoah Compact Objects Task"), 662 _regions(regions) { 663 } 664 void work(uint worker_id) { 665 ShenandoahHeap* heap = ShenandoahHeap::heap(); 666 ShenandoahHeapRegionSet* copy_queue = _regions[worker_id]; 667 copy_queue->clear_current_index(); 668 ShenandoahCompactObjectsClosure cl; 669 ShenandoahHeapRegion* r = copy_queue->current(); 670 copy_queue->next(); 671 while (r != NULL) { 672 assert(! r->is_humongous(), "must not get humongous regions here"); 673 heap->marked_object_iterate(r, &cl); 674 r->set_top(r->new_top()); 675 r = copy_queue->current(); 676 copy_queue->next(); 677 } 678 } 679 }; 680 681 class ShenandoahPostCompactClosure : public ShenandoahHeapRegionClosure { 682 size_t _live; 683 ShenandoahHeap* _heap; 684 public: 685 686 ShenandoahPostCompactClosure() : _live(0), _heap(ShenandoahHeap::heap()) { 687 _heap->clear_free_regions(); 688 } 689 690 bool doHeapRegion(ShenandoahHeapRegion* r) { 691 // Need to reset the complete-top-at-mark-start pointer here because 692 // the complete marking bitmap is no longer valid. This ensures 693 // size-based iteration in marked_object_iterate(). 694 _heap->set_complete_top_at_mark_start(r->bottom(), r->bottom()); 695 r->set_in_collection_set(false); 696 if (r->is_humongous()) { 697 _live += ShenandoahHeapRegion::RegionSizeBytes; 698 } else { 699 size_t live = r->used(); 700 if (live == 0) { 701 r->recycle(); 702 _heap->add_free_region(r); 703 } 704 r->set_live_data(live); 705 _live += live; 706 } 707 return false; 708 } 709 710 size_t get_live() { return _live; } 711 712 }; 713 714 void ShenandoahMarkCompact::phase4_compact_objects(ShenandoahHeapRegionSet** copy_queues) { 715 GCTraceTime(Info, gc, phases) time("Phase 4: Move objects", _gc_timer); 716 ShenandoahHeap* heap = ShenandoahHeap::heap(); 717 ShenandoahCompactObjectsTask compact_task(copy_queues); 718 heap->workers()->run_task(&compact_task); 719 720 heap->clear_cset_fast_test(); 721 722 // Reset complete bitmap. We're about to reset the complete-top-at-mark-start pointer 723 // and must ensure the bitmap is in sync. 724 heap->reset_complete_mark_bitmap(heap->workers()); 725 726 { 727 ShenandoahHeap::ShenandoahHeapLock lock(heap); 728 ShenandoahPostCompactClosure post_compact; 729 heap->heap_region_iterate(&post_compact); 730 731 heap->set_used(post_compact.get_live()); 732 733 } 734 735 heap->clear_cancelled_concgc(); 736 737 // Also clear the next bitmap in preparation for next marking. 738 heap->reset_next_mark_bitmap(heap->workers()); 739 740 for (uint i = 0; i < heap->max_workers(); i++) { 741 delete copy_queues[i]; 742 } 743 744 }