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 obj_size = p->size() + BrooksPointer::word_size(); 405 if (_compact_point + obj_size > _to_region->end()) { 406 // Object doesn't fit. Pick next to-region and start compacting there. 407 _to_region->set_new_top(_compact_point); 408 ShenandoahHeapRegion* new_to_region = _to_regions->current(); 409 _to_regions->next(); 410 if (new_to_region == NULL) { 411 new_to_region = _from_region; 412 } 413 assert(new_to_region != _to_region, "must not reuse same to-region"); 414 assert(new_to_region != NULL, "must not be NULL"); 415 _to_region = new_to_region; 416 _compact_point = _to_region->bottom(); 417 } 418 assert(_compact_point + obj_size <= _to_region->end(), "must fit"); 419 assert(oopDesc::unsafe_equals(p, ShenandoahBarrierSet::resolve_oop_static_not_null(p)), 420 "expect forwarded oop"); 421 BrooksPointer::set_raw(p, _compact_point + BrooksPointer::word_size()); 422 _compact_point += obj_size; 423 } 424 }; 425 426 class ShenandoahPrepareForCompactionTask : public AbstractGangTask { 427 private: 428 429 ShenandoahHeapRegionSet** _copy_queues; 430 ShenandoahHeapRegionSet* _from_regions; 431 432 ShenandoahHeapRegion* next_from_region(ShenandoahHeapRegionSet* copy_queue) { 433 ShenandoahHeapRegion* from_region = _from_regions->claim_next(); 434 while (from_region != NULL && (from_region->is_humongous() || from_region->is_pinned())) { 435 from_region = _from_regions->claim_next(); 436 } 437 if (from_region != NULL) { 438 assert(copy_queue != NULL, "sanity"); 439 assert(! from_region->is_humongous(), "must not get humongous regions here"); 440 assert(! from_region->is_pinned(), "no pinned region in mark-compact"); 441 copy_queue->add_region(from_region); 442 } 443 return from_region; 444 } 445 446 public: 447 ShenandoahPrepareForCompactionTask(ShenandoahHeapRegionSet* from_regions, ShenandoahHeapRegionSet** copy_queues) : 448 AbstractGangTask("Shenandoah Prepare For Compaction Task"), 449 _from_regions(from_regions), _copy_queues(copy_queues) { 450 } 451 452 void work(uint worker_id) { 453 ShenandoahHeap* heap = ShenandoahHeap::heap(); 454 ShenandoahHeapRegionSet* copy_queue = _copy_queues[worker_id]; 455 ShenandoahHeapRegion* from_region = next_from_region(copy_queue); 456 if (from_region == NULL) return; 457 ShenandoahHeapRegionSet* to_regions = new ShenandoahHeapRegionSet(ShenandoahHeap::heap()->max_regions()); 458 ShenandoahPrepareForCompactionObjectClosure cl(to_regions, from_region); 459 while (from_region != NULL) { 460 assert(from_region != NULL, "sanity"); 461 cl.set_from_region(from_region); 462 heap->marked_object_iterate(from_region, &cl); 463 if (from_region != cl.to_region()) { 464 assert(from_region != NULL, "sanity"); 465 to_regions->add_region(from_region); 466 } 467 from_region = next_from_region(copy_queue); 468 } 469 assert(cl.to_region() != NULL, "should not happen"); 470 cl.to_region()->set_new_top(cl.compact_point()); 471 while (to_regions->count() > 0) { 472 ShenandoahHeapRegion* r = to_regions->current(); 473 to_regions->next(); 474 if (r == NULL) { 475 to_regions->print(); 476 } 477 assert(r != NULL, "should not happen"); 478 r->set_new_top(r->bottom()); 479 } 480 delete to_regions; 481 } 482 }; 483 484 void ShenandoahMarkCompact::phase2_calculate_target_addresses(ShenandoahHeapRegionSet** copy_queues) { 485 GCTraceTime(Info, gc, phases) time("Phase 2: Compute new object addresses", _gc_timer); 486 ShenandoahHeap* heap = ShenandoahHeap::heap(); 487 488 ShenandoahMCReclaimHumongousRegionClosure cl; 489 heap->heap_region_iterate(&cl); 490 491 // Initialize copy queues. 492 for (uint i = 0; i < heap->max_workers(); i++) { 493 copy_queues[i] = new ShenandoahHeapRegionSet(heap->max_regions()); 494 } 495 496 ShenandoahHeapRegionSet* from_regions = heap->regions(); 497 from_regions->clear_current_index(); 498 ShenandoahPrepareForCompactionTask prepare_task(from_regions, copy_queues); 499 heap->workers()->run_task(&prepare_task); 500 } 501 502 class ShenandoahAdjustPointersClosure : public MetadataAwareOopClosure { 503 private: 504 ShenandoahHeap* _heap; 505 size_t _new_obj_offset; 506 public: 507 508 ShenandoahAdjustPointersClosure() : _heap(ShenandoahHeap::heap()) { 509 } 510 511 private: 512 template <class T> 513 inline void do_oop_work(T* p) { 514 T o = oopDesc::load_heap_oop(p); 515 if (! oopDesc::is_null(o)) { 516 oop obj = oopDesc::decode_heap_oop_not_null(o); 517 assert(_heap->is_marked_complete(obj), "must be marked"); 518 oop forw = oop(BrooksPointer::get_raw(obj)); 519 oopDesc::encode_store_heap_oop(p, forw); 520 if (UseShenandoahMatrix) { 521 if (_heap->is_in_reserved(p)) { 522 assert(_heap->is_in_reserved(forw), "must be in heap"); 523 // We're moving a to a', which points to b, about to be moved to b'. 524 // We already know b' from the fwd pointer of b. 525 // In the object closure, we see a, and we know a' (by looking at its 526 // fwd ptr). We store the offset in the OopClosure, which is going 527 // to visit all of a's fields, and then, when we see each field, we 528 // subtract the offset from each field address to get the final ptr. 529 _heap->connection_matrix()->set_connected(((HeapWord*) p) - _new_obj_offset, forw); 530 } 531 } 532 } 533 } 534 public: 535 void do_oop(oop* p) { 536 do_oop_work(p); 537 } 538 void do_oop(narrowOop* p) { 539 do_oop_work(p); 540 } 541 void set_new_obj_offset(size_t new_obj_offset) { 542 _new_obj_offset = new_obj_offset; 543 } 544 }; 545 546 class ShenandoahAdjustPointersObjectClosure : public ObjectClosure { 547 private: 548 ShenandoahAdjustPointersClosure _cl; 549 ShenandoahHeap* _heap; 550 public: 551 ShenandoahAdjustPointersObjectClosure() : 552 _heap(ShenandoahHeap::heap()) { 553 } 554 void do_object(oop p) { 555 assert(_heap->is_marked_complete(p), "must be marked"); 556 HeapWord* forw = BrooksPointer::get_raw(p); 557 _cl.set_new_obj_offset(pointer_delta((HeapWord*) p, forw)); 558 p->oop_iterate(&_cl); 559 } 560 }; 561 562 class ShenandoahAdjustPointersTask : public AbstractGangTask { 563 private: 564 ShenandoahHeapRegionSet* _regions; 565 public: 566 567 ShenandoahAdjustPointersTask(ShenandoahHeapRegionSet* regions) : 568 AbstractGangTask("Shenandoah Adjust Pointers Task"), 569 _regions(regions) { 570 } 571 572 void work(uint worker_id) { 573 ShenandoahHeap* heap = ShenandoahHeap::heap(); 574 ShenandoahHeapRegion* r = _regions->claim_next(); 575 ShenandoahAdjustPointersObjectClosure obj_cl; 576 while (r != NULL) { 577 if (! r->is_humongous_continuation()) { 578 heap->marked_object_iterate(r, &obj_cl); 579 } 580 r = _regions->claim_next(); 581 } 582 } 583 }; 584 585 class ShenandoahAdjustRootPointersTask : public AbstractGangTask { 586 private: 587 ShenandoahRootProcessor* _rp; 588 589 public: 590 591 ShenandoahAdjustRootPointersTask(ShenandoahRootProcessor* rp) : 592 AbstractGangTask("Shenandoah Adjust Root Pointers Task"), 593 _rp(rp) { 594 } 595 596 void work(uint worker_id) { 597 ShenandoahAdjustPointersClosure cl; 598 CLDToOopClosure adjust_cld_closure(&cl, true); 599 MarkingCodeBlobClosure adjust_code_closure(&cl, 600 CodeBlobToOopClosure::FixRelocations); 601 602 _rp->process_all_roots(&cl, &cl, 603 &adjust_cld_closure, 604 &adjust_code_closure, worker_id); 605 } 606 }; 607 608 void ShenandoahMarkCompact::phase3_update_references() { 609 GCTraceTime(Info, gc, phases) time("Phase 2: Adjust pointers", _gc_timer); 610 ShenandoahHeap* heap = ShenandoahHeap::heap(); 611 612 if (UseShenandoahMatrix) { 613 heap->connection_matrix()->clear_all(); 614 } 615 616 // Need cleared claim bits for the roots processing 617 ClassLoaderDataGraph::clear_claimed_marks(); 618 619 WorkGang* workers = heap->workers(); 620 uint nworkers = workers->active_workers(); 621 { 622 COMPILER2_PRESENT(DerivedPointerTable::clear()); 623 624 ShenandoahRootProcessor rp(heap, nworkers); 625 ShenandoahAdjustRootPointersTask task(&rp); 626 workers->run_task(&task); 627 COMPILER2_PRESENT(DerivedPointerTable::update_pointers()); 628 } 629 630 ShenandoahHeapRegionSet* regions = heap->regions(); 631 regions->clear_current_index(); 632 ShenandoahAdjustPointersTask adjust_pointers_task(regions); 633 workers->run_task(&adjust_pointers_task); 634 } 635 636 class ShenandoahCompactObjectsClosure : public ObjectClosure { 637 private: 638 ShenandoahHeap* _heap; 639 public: 640 ShenandoahCompactObjectsClosure() : _heap(ShenandoahHeap::heap()) { 641 } 642 void do_object(oop p) { 643 assert(_heap->is_marked_complete(p), "must be marked"); 644 size_t size = p->size(); 645 HeapWord* compact_to = BrooksPointer::get_raw(p); 646 HeapWord* compact_from = (HeapWord*) p; 647 if (compact_from != compact_to) { 648 Copy::aligned_conjoint_words(compact_from, compact_to, size); 649 } 650 oop new_obj = oop(compact_to); 651 // new_obj->init_mark(); 652 BrooksPointer::initialize(new_obj); 653 } 654 }; 655 656 class ShenandoahCompactObjectsTask : public AbstractGangTask { 657 ShenandoahHeapRegionSet** _regions; 658 public: 659 ShenandoahCompactObjectsTask(ShenandoahHeapRegionSet** regions) : 660 AbstractGangTask("Shenandoah Compact Objects Task"), 661 _regions(regions) { 662 } 663 void work(uint worker_id) { 664 ShenandoahHeap* heap = ShenandoahHeap::heap(); 665 ShenandoahHeapRegionSet* copy_queue = _regions[worker_id]; 666 copy_queue->clear_current_index(); 667 ShenandoahCompactObjectsClosure cl; 668 ShenandoahHeapRegion* r = copy_queue->current(); 669 copy_queue->next(); 670 while (r != NULL) { 671 assert(! r->is_humongous(), "must not get humongous regions here"); 672 heap->marked_object_iterate(r, &cl); 673 r->set_top(r->new_top()); 674 r = copy_queue->current(); 675 copy_queue->next(); 676 } 677 } 678 }; 679 680 class ShenandoahPostCompactClosure : public ShenandoahHeapRegionClosure { 681 size_t _live; 682 ShenandoahHeap* _heap; 683 public: 684 685 ShenandoahPostCompactClosure() : _live(0), _heap(ShenandoahHeap::heap()) { 686 _heap->clear_free_regions(); 687 } 688 689 bool doHeapRegion(ShenandoahHeapRegion* r) { 690 // Need to reset the complete-top-at-mark-start pointer here because 691 // the complete marking bitmap is no longer valid. This ensures 692 // size-based iteration in marked_object_iterate(). 693 _heap->set_complete_top_at_mark_start(r->bottom(), r->bottom()); 694 r->set_in_collection_set(false); 695 if (r->is_humongous()) { 696 _live += ShenandoahHeapRegion::RegionSizeBytes; 697 } else { 698 size_t live = r->used(); 699 if (live == 0) { 700 r->recycle(); 701 _heap->add_free_region(r); 702 } 703 r->set_live_data(live); 704 _live += live; 705 } 706 return false; 707 } 708 709 size_t get_live() { return _live; } 710 711 }; 712 713 void ShenandoahMarkCompact::phase4_compact_objects(ShenandoahHeapRegionSet** copy_queues) { 714 GCTraceTime(Info, gc, phases) time("Phase 4: Move objects", _gc_timer); 715 ShenandoahHeap* heap = ShenandoahHeap::heap(); 716 ShenandoahCompactObjectsTask compact_task(copy_queues); 717 heap->workers()->run_task(&compact_task); 718 719 heap->clear_cset_fast_test(); 720 721 // Reset complete bitmap. We're about to reset the complete-top-at-mark-start pointer 722 // and must ensure the bitmap is in sync. 723 heap->reset_complete_mark_bitmap(heap->workers()); 724 725 { 726 ShenandoahHeap::ShenandoahHeapLock lock(heap); 727 ShenandoahPostCompactClosure post_compact; 728 heap->heap_region_iterate(&post_compact); 729 730 heap->set_used(post_compact.get_live()); 731 732 } 733 734 heap->clear_cancelled_concgc(); 735 736 // Also clear the next bitmap in preparation for next marking. 737 heap->reset_next_mark_bitmap(heap->workers()); 738 739 for (uint i = 0; i < heap->max_workers(); i++) { 740 delete copy_queues[i]; 741 } 742 743 }