1 /* 2 * Copyright (c) 2014, 2021, Red Hat, Inc. 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 27 #include "compiler/oopMap.hpp" 28 #include "gc/shared/continuationGCSupport.hpp" 29 #include "gc/shared/fullGCForwarding.inline.hpp" 30 #include "gc/shared/gcTraceTime.inline.hpp" 31 #include "gc/shared/preservedMarks.inline.hpp" 32 #include "gc/shared/tlab_globals.hpp" 33 #include "gc/shared/workerThread.hpp" 34 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp" 35 #include "gc/shenandoah/shenandoahClosures.inline.hpp" 36 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp" 37 #include "gc/shenandoah/shenandoahConcurrentGC.hpp" 38 #include "gc/shenandoah/shenandoahCollectionSet.hpp" 39 #include "gc/shenandoah/shenandoahFreeSet.hpp" 40 #include "gc/shenandoah/shenandoahFullGC.hpp" 41 #include "gc/shenandoah/shenandoahPhaseTimings.hpp" 42 #include "gc/shenandoah/shenandoahMark.inline.hpp" 43 #include "gc/shenandoah/shenandoahMonitoringSupport.hpp" 44 #include "gc/shenandoah/shenandoahHeapRegionSet.hpp" 45 #include "gc/shenandoah/shenandoahHeap.inline.hpp" 46 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp" 47 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp" 48 #include "gc/shenandoah/shenandoahMetrics.hpp" 49 #include "gc/shenandoah/shenandoahReferenceProcessor.hpp" 50 #include "gc/shenandoah/shenandoahRootProcessor.inline.hpp" 51 #include "gc/shenandoah/shenandoahSTWMark.hpp" 52 #include "gc/shenandoah/shenandoahUtils.hpp" 53 #include "gc/shenandoah/shenandoahVerifier.hpp" 54 #include "gc/shenandoah/shenandoahVMOperations.hpp" 55 #include "gc/shenandoah/shenandoahWorkerPolicy.hpp" 56 #include "memory/metaspaceUtils.hpp" 57 #include "memory/universe.hpp" 58 #include "oops/compressedOops.inline.hpp" 59 #include "oops/oop.inline.hpp" 60 #include "runtime/javaThread.hpp" 61 #include "runtime/orderAccess.hpp" 62 #include "runtime/vmThread.hpp" 63 #include "utilities/copy.hpp" 64 #include "utilities/events.hpp" 65 #include "utilities/growableArray.hpp" 66 67 ShenandoahFullGC::ShenandoahFullGC() : 68 _gc_timer(ShenandoahHeap::heap()->gc_timer()), 69 _preserved_marks(new PreservedMarksSet(true)) {} 70 71 ShenandoahFullGC::~ShenandoahFullGC() { 72 delete _preserved_marks; 73 } 74 75 bool ShenandoahFullGC::collect(GCCause::Cause cause) { 76 vmop_entry_full(cause); 77 // Always success 78 return true; 79 } 80 81 void ShenandoahFullGC::vmop_entry_full(GCCause::Cause cause) { 82 ShenandoahHeap* const heap = ShenandoahHeap::heap(); 83 TraceCollectorStats tcs(heap->monitoring_support()->full_stw_collection_counters()); 84 ShenandoahTimingsTracker timing(ShenandoahPhaseTimings::full_gc_gross); 85 86 heap->try_inject_alloc_failure(); 87 VM_ShenandoahFullGC op(cause, this); 88 VMThread::execute(&op); 89 } 90 91 void ShenandoahFullGC::entry_full(GCCause::Cause cause) { 92 static const char* msg = "Pause Full"; 93 ShenandoahPausePhase gc_phase(msg, ShenandoahPhaseTimings::full_gc, true /* log_heap_usage */); 94 EventMark em("%s", msg); 95 96 ShenandoahWorkerScope scope(ShenandoahHeap::heap()->workers(), 97 ShenandoahWorkerPolicy::calc_workers_for_fullgc(), 98 "full gc"); 99 100 op_full(cause); 101 } 102 103 void ShenandoahFullGC::op_full(GCCause::Cause cause) { 104 ShenandoahMetricsSnapshot metrics; 105 metrics.snap_before(); 106 107 // Perform full GC 108 do_it(cause); 109 110 ShenandoahHeap* const heap = ShenandoahHeap::heap(); 111 112 metrics.snap_after(); 113 114 if (metrics.is_good_progress()) { 115 heap->notify_gc_progress(); 116 } else { 117 // Nothing to do. Tell the allocation path that we have failed to make 118 // progress, and it can finally fail. 119 heap->notify_gc_no_progress(); 120 } 121 122 // Regardless if progress was made, we record that we completed a "successful" full GC. 123 heap->heuristics()->record_success_full(); 124 heap->shenandoah_policy()->record_success_full(); 125 } 126 127 void ShenandoahFullGC::do_it(GCCause::Cause gc_cause) { 128 ShenandoahHeap* heap = ShenandoahHeap::heap(); 129 130 if (ShenandoahVerify) { 131 heap->verifier()->verify_before_fullgc(); 132 } 133 134 if (VerifyBeforeGC) { 135 Universe::verify(); 136 } 137 138 // Degenerated GC may carry concurrent root flags when upgrading to 139 // full GC. We need to reset it before mutators resume. 140 heap->set_concurrent_strong_root_in_progress(false); 141 heap->set_concurrent_weak_root_in_progress(false); 142 143 heap->set_full_gc_in_progress(true); 144 145 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "must be at a safepoint"); 146 assert(Thread::current()->is_VM_thread(), "Do full GC only while world is stopped"); 147 148 { 149 ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_heapdump_pre); 150 heap->pre_full_gc_dump(_gc_timer); 151 } 152 153 { 154 ShenandoahGCPhase prepare_phase(ShenandoahPhaseTimings::full_gc_prepare); 155 // Full GC is supposed to recover from any GC state: 156 157 // a0. Remember if we have forwarded objects 158 bool has_forwarded_objects = heap->has_forwarded_objects(); 159 160 // a1. Cancel evacuation, if in progress 161 if (heap->is_evacuation_in_progress()) { 162 heap->set_evacuation_in_progress(false); 163 } 164 assert(!heap->is_evacuation_in_progress(), "sanity"); 165 166 // a2. Cancel update-refs, if in progress 167 if (heap->is_update_refs_in_progress()) { 168 heap->set_update_refs_in_progress(false); 169 } 170 assert(!heap->is_update_refs_in_progress(), "sanity"); 171 172 // b. Cancel concurrent mark, if in progress 173 if (heap->is_concurrent_mark_in_progress()) { 174 ShenandoahConcurrentGC::cancel(); 175 heap->set_concurrent_mark_in_progress(false); 176 } 177 assert(!heap->is_concurrent_mark_in_progress(), "sanity"); 178 179 // c. Update roots if this full GC is due to evac-oom, which may carry from-space pointers in roots. 180 if (has_forwarded_objects) { 181 update_roots(true /*full_gc*/); 182 } 183 184 // d. Reset the bitmaps for new marking 185 heap->reset_mark_bitmap(); 186 assert(heap->marking_context()->is_bitmap_clear(), "sanity"); 187 assert(!heap->marking_context()->is_complete(), "sanity"); 188 189 // e. Abandon reference discovery and clear all discovered references. 190 ShenandoahReferenceProcessor* rp = heap->ref_processor(); 191 rp->abandon_partial_discovery(); 192 193 // f. Sync pinned region status from the CP marks 194 heap->sync_pinned_region_status(); 195 196 // The rest of prologue: 197 _preserved_marks->init(heap->workers()->active_workers()); 198 199 assert(heap->has_forwarded_objects() == has_forwarded_objects, "This should not change"); 200 } 201 202 if (UseTLAB) { 203 heap->gclabs_retire(ResizeTLAB); 204 heap->tlabs_retire(ResizeTLAB); 205 } 206 207 OrderAccess::fence(); 208 209 phase1_mark_heap(); 210 211 // Once marking is done, which may have fixed up forwarded objects, we can drop it. 212 // Coming out of Full GC, we would not have any forwarded objects. 213 // This also prevents resolves with fwdptr from kicking in while adjusting pointers in phase3. 214 heap->set_has_forwarded_objects(false); 215 216 heap->set_full_gc_move_in_progress(true); 217 218 // Setup workers for the rest 219 OrderAccess::fence(); 220 221 // Initialize worker slices 222 ShenandoahHeapRegionSet** worker_slices = NEW_C_HEAP_ARRAY(ShenandoahHeapRegionSet*, heap->max_workers(), mtGC); 223 for (uint i = 0; i < heap->max_workers(); i++) { 224 worker_slices[i] = new ShenandoahHeapRegionSet(); 225 } 226 227 { 228 // The rest of code performs region moves, where region status is undefined 229 // until all phases run together. 230 ShenandoahHeapLocker lock(heap->lock()); 231 232 phase2_calculate_target_addresses(worker_slices); 233 234 OrderAccess::fence(); 235 236 phase3_update_references(); 237 238 phase4_compact_objects(worker_slices); 239 240 phase5_epilog(); 241 } 242 243 // Resize metaspace 244 MetaspaceGC::compute_new_size(); 245 246 // Free worker slices 247 for (uint i = 0; i < heap->max_workers(); i++) { 248 delete worker_slices[i]; 249 } 250 FREE_C_HEAP_ARRAY(ShenandoahHeapRegionSet*, worker_slices); 251 252 heap->set_full_gc_move_in_progress(false); 253 heap->set_full_gc_in_progress(false); 254 255 if (ShenandoahVerify) { 256 heap->verifier()->verify_after_fullgc(); 257 } 258 259 if (VerifyAfterGC) { 260 Universe::verify(); 261 } 262 263 { 264 ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_heapdump_post); 265 heap->post_full_gc_dump(_gc_timer); 266 } 267 } 268 269 class ShenandoahPrepareForMarkClosure: public ShenandoahHeapRegionClosure { 270 private: 271 ShenandoahMarkingContext* const _ctx; 272 273 public: 274 ShenandoahPrepareForMarkClosure() : _ctx(ShenandoahHeap::heap()->marking_context()) {} 275 276 void heap_region_do(ShenandoahHeapRegion *r) { 277 _ctx->capture_top_at_mark_start(r); 278 r->clear_live_data(); 279 } 280 281 bool is_thread_safe() { return true; } 282 }; 283 284 void ShenandoahFullGC::phase1_mark_heap() { 285 GCTraceTime(Info, gc, phases) time("Phase 1: Mark live objects", _gc_timer); 286 ShenandoahGCPhase mark_phase(ShenandoahPhaseTimings::full_gc_mark); 287 288 ShenandoahHeap* heap = ShenandoahHeap::heap(); 289 290 ShenandoahPrepareForMarkClosure cl; 291 heap->parallel_heap_region_iterate(&cl); 292 293 heap->set_unload_classes(heap->heuristics()->can_unload_classes()); 294 295 ShenandoahReferenceProcessor* rp = heap->ref_processor(); 296 // enable ("weak") refs discovery 297 rp->set_soft_reference_policy(true); // forcefully purge all soft references 298 299 ShenandoahSTWMark mark(true /*full_gc*/); 300 mark.mark(); 301 heap->parallel_cleaning(true /* full_gc */); 302 } 303 304 class ShenandoahPrepareForCompactionObjectClosure : public ObjectClosure { 305 private: 306 PreservedMarks* const _preserved_marks; 307 ShenandoahHeap* const _heap; 308 GrowableArray<ShenandoahHeapRegion*>& _empty_regions; 309 int _empty_regions_pos; 310 ShenandoahHeapRegion* _to_region; 311 ShenandoahHeapRegion* _from_region; 312 HeapWord* _compact_point; 313 314 public: 315 ShenandoahPrepareForCompactionObjectClosure(PreservedMarks* preserved_marks, 316 GrowableArray<ShenandoahHeapRegion*>& empty_regions, 317 ShenandoahHeapRegion* to_region) : 318 _preserved_marks(preserved_marks), 319 _heap(ShenandoahHeap::heap()), 320 _empty_regions(empty_regions), 321 _empty_regions_pos(0), 322 _to_region(to_region), 323 _from_region(nullptr), 324 _compact_point(to_region->bottom()) {} 325 326 void set_from_region(ShenandoahHeapRegion* from_region) { 327 _from_region = from_region; 328 } 329 330 void finish() { 331 assert(_to_region != nullptr, "should not happen"); 332 _to_region->set_new_top(_compact_point); 333 } 334 335 bool is_compact_same_region() { 336 return _from_region == _to_region; 337 } 338 339 int empty_regions_pos() { 340 return _empty_regions_pos; 341 } 342 343 void do_object(oop p) { 344 assert(_from_region != nullptr, "must set before work"); 345 assert(_heap->complete_marking_context()->is_marked(p), "must be marked"); 346 assert(!_heap->complete_marking_context()->allocated_after_mark_start(p), "must be truly marked"); 347 348 size_t obj_size = p->size(); 349 if (_compact_point + obj_size > _to_region->end()) { 350 finish(); 351 352 // Object doesn't fit. Pick next empty region and start compacting there. 353 ShenandoahHeapRegion* new_to_region; 354 if (_empty_regions_pos < _empty_regions.length()) { 355 new_to_region = _empty_regions.at(_empty_regions_pos); 356 _empty_regions_pos++; 357 } else { 358 // Out of empty region? Compact within the same region. 359 new_to_region = _from_region; 360 } 361 362 assert(new_to_region != _to_region, "must not reuse same to-region"); 363 assert(new_to_region != nullptr, "must not be null"); 364 _to_region = new_to_region; 365 _compact_point = _to_region->bottom(); 366 } 367 368 // Object fits into current region, record new location, if object does not move: 369 assert(_compact_point + obj_size <= _to_region->end(), "must fit"); 370 shenandoah_assert_not_forwarded(nullptr, p); 371 if (_compact_point != cast_from_oop<HeapWord*>(p)) { 372 _preserved_marks->push_if_necessary(p, p->mark()); 373 FullGCForwarding::forward_to(p, cast_to_oop(_compact_point)); 374 } 375 _compact_point += obj_size; 376 } 377 }; 378 379 class ShenandoahPrepareForCompactionTask : public WorkerTask { 380 private: 381 PreservedMarksSet* const _preserved_marks; 382 ShenandoahHeap* const _heap; 383 ShenandoahHeapRegionSet** const _worker_slices; 384 385 public: 386 ShenandoahPrepareForCompactionTask(PreservedMarksSet *preserved_marks, ShenandoahHeapRegionSet **worker_slices) : 387 WorkerTask("Shenandoah Prepare For Compaction"), 388 _preserved_marks(preserved_marks), 389 _heap(ShenandoahHeap::heap()), _worker_slices(worker_slices) { 390 } 391 392 static bool is_candidate_region(ShenandoahHeapRegion* r) { 393 // Empty region: get it into the slice to defragment the slice itself. 394 // We could have skipped this without violating correctness, but we really 395 // want to compact all live regions to the start of the heap, which sometimes 396 // means moving them into the fully empty regions. 397 if (r->is_empty()) return true; 398 399 // Can move the region, and this is not the humongous region. Humongous 400 // moves are special cased here, because their moves are handled separately. 401 return r->is_stw_move_allowed() && !r->is_humongous(); 402 } 403 404 void work(uint worker_id) override; 405 private: 406 template<typename ClosureType> 407 void prepare_for_compaction(ClosureType& cl, 408 GrowableArray<ShenandoahHeapRegion*>& empty_regions, 409 ShenandoahHeapRegionSetIterator& it, 410 ShenandoahHeapRegion* from_region); 411 }; 412 413 void ShenandoahPrepareForCompactionTask::work(uint worker_id) { 414 ShenandoahParallelWorkerSession worker_session(worker_id); 415 ShenandoahHeapRegionSet* slice = _worker_slices[worker_id]; 416 ShenandoahHeapRegionSetIterator it(slice); 417 ShenandoahHeapRegion* from_region = it.next(); 418 // No work? 419 if (from_region == nullptr) { 420 return; 421 } 422 423 // Sliding compaction. Walk all regions in the slice, and compact them. 424 // Remember empty regions and reuse them as needed. 425 ResourceMark rm; 426 427 GrowableArray<ShenandoahHeapRegion*> empty_regions((int)_heap->num_regions()); 428 429 ShenandoahPrepareForCompactionObjectClosure cl(_preserved_marks->get(worker_id), empty_regions, from_region); 430 prepare_for_compaction(cl, empty_regions, it, from_region); 431 } 432 433 template<typename ClosureType> 434 void ShenandoahPrepareForCompactionTask::prepare_for_compaction(ClosureType& cl, 435 GrowableArray<ShenandoahHeapRegion*>& empty_regions, 436 ShenandoahHeapRegionSetIterator& it, 437 ShenandoahHeapRegion* from_region) { 438 while (from_region != nullptr) { 439 assert(is_candidate_region(from_region), "Sanity"); 440 cl.set_from_region(from_region); 441 if (from_region->has_live()) { 442 _heap->marked_object_iterate(from_region, &cl); 443 } 444 445 // Compacted the region to somewhere else? From-region is empty then. 446 if (!cl.is_compact_same_region()) { 447 empty_regions.append(from_region); 448 } 449 from_region = it.next(); 450 } 451 cl.finish(); 452 453 // Mark all remaining regions as empty 454 for (int pos = cl.empty_regions_pos(); pos < empty_regions.length(); ++pos) { 455 ShenandoahHeapRegion* r = empty_regions.at(pos); 456 r->set_new_top(r->bottom()); 457 } 458 } 459 460 void ShenandoahFullGC::calculate_target_humongous_objects() { 461 ShenandoahHeap* heap = ShenandoahHeap::heap(); 462 463 // Compute the new addresses for humongous objects. We need to do this after addresses 464 // for regular objects are calculated, and we know what regions in heap suffix are 465 // available for humongous moves. 466 // 467 // Scan the heap backwards, because we are compacting humongous regions towards the end. 468 // Maintain the contiguous compaction window in [to_begin; to_end), so that we can slide 469 // humongous start there. 470 // 471 // The complication is potential non-movable regions during the scan. If such region is 472 // detected, then sliding restarts towards that non-movable region. 473 474 size_t to_begin = heap->num_regions(); 475 size_t to_end = heap->num_regions(); 476 477 for (size_t c = heap->num_regions(); c > 0; c--) { 478 ShenandoahHeapRegion *r = heap->get_region(c - 1); 479 if (r->is_humongous_continuation() || (r->new_top() == r->bottom())) { 480 // To-region candidate: record this, and continue scan 481 to_begin = r->index(); 482 continue; 483 } 484 485 if (r->is_humongous_start() && r->is_stw_move_allowed()) { 486 // From-region candidate: movable humongous region 487 oop old_obj = cast_to_oop(r->bottom()); 488 size_t words_size = old_obj->size(); 489 size_t num_regions = ShenandoahHeapRegion::required_regions(words_size * HeapWordSize); 490 491 size_t start = to_end - num_regions; 492 493 if (start >= to_begin && start != r->index()) { 494 // Fits into current window, and the move is non-trivial. Record the move then, and continue scan. 495 _preserved_marks->get(0)->push_if_necessary(old_obj, old_obj->mark()); 496 FullGCForwarding::forward_to(old_obj, cast_to_oop(heap->get_region(start)->bottom())); 497 to_end = start; 498 continue; 499 } 500 } 501 502 // Failed to fit. Scan starting from current region. 503 to_begin = r->index(); 504 to_end = r->index(); 505 } 506 } 507 508 class ShenandoahEnsureHeapActiveClosure: public ShenandoahHeapRegionClosure { 509 private: 510 ShenandoahHeap* const _heap; 511 512 public: 513 ShenandoahEnsureHeapActiveClosure() : _heap(ShenandoahHeap::heap()) {} 514 void heap_region_do(ShenandoahHeapRegion* r) { 515 if (r->is_trash()) { 516 r->recycle(); 517 } 518 if (r->is_cset()) { 519 r->make_regular_bypass(); 520 } 521 if (r->is_empty_uncommitted()) { 522 r->make_committed_bypass(); 523 } 524 assert (r->is_committed(), "only committed regions in heap now, see region " SIZE_FORMAT, r->index()); 525 526 // Record current region occupancy: this communicates empty regions are free 527 // to the rest of Full GC code. 528 r->set_new_top(r->top()); 529 } 530 }; 531 532 class ShenandoahTrashImmediateGarbageClosure: public ShenandoahHeapRegionClosure { 533 private: 534 ShenandoahHeap* const _heap; 535 ShenandoahMarkingContext* const _ctx; 536 537 public: 538 ShenandoahTrashImmediateGarbageClosure() : 539 _heap(ShenandoahHeap::heap()), 540 _ctx(ShenandoahHeap::heap()->complete_marking_context()) {} 541 542 void heap_region_do(ShenandoahHeapRegion* r) { 543 if (r->is_humongous_start()) { 544 oop humongous_obj = cast_to_oop(r->bottom()); 545 if (!_ctx->is_marked(humongous_obj)) { 546 assert(!r->has_live(), 547 "Region " SIZE_FORMAT " is not marked, should not have live", r->index()); 548 _heap->trash_humongous_region_at(r); 549 } else { 550 assert(r->has_live(), 551 "Region " SIZE_FORMAT " should have live", r->index()); 552 } 553 } else if (r->is_humongous_continuation()) { 554 // If we hit continuation, the non-live humongous starts should have been trashed already 555 assert(r->humongous_start_region()->has_live(), 556 "Region " SIZE_FORMAT " should have live", r->index()); 557 } else if (r->is_regular()) { 558 if (!r->has_live()) { 559 r->make_trash_immediate(); 560 } 561 } 562 } 563 }; 564 565 void ShenandoahFullGC::distribute_slices(ShenandoahHeapRegionSet** worker_slices) { 566 ShenandoahHeap* heap = ShenandoahHeap::heap(); 567 568 uint n_workers = heap->workers()->active_workers(); 569 size_t n_regions = heap->num_regions(); 570 571 // What we want to accomplish: have the dense prefix of data, while still balancing 572 // out the parallel work. 573 // 574 // Assuming the amount of work is driven by the live data that needs moving, we can slice 575 // the entire heap into equal-live-sized prefix slices, and compact into them. So, each 576 // thread takes all regions in its prefix subset, and then it takes some regions from 577 // the tail. 578 // 579 // Tail region selection becomes interesting. 580 // 581 // First, we want to distribute the regions fairly between the workers, and those regions 582 // might have different amount of live data. So, until we sure no workers need live data, 583 // we need to only take what the worker needs. 584 // 585 // Second, since we slide everything to the left in each slice, the most busy regions 586 // would be the ones on the left. Which means we want to have all workers have their after-tail 587 // regions as close to the left as possible. 588 // 589 // The easiest way to do this is to distribute after-tail regions in round-robin between 590 // workers that still need live data. 591 // 592 // Consider parallel workers A, B, C, then the target slice layout would be: 593 // 594 // AAAAAAAABBBBBBBBCCCCCCCC|ABCABCABCABCABCABCABCABABABABABABABABABABAAAAA 595 // 596 // (.....dense-prefix.....) (.....................tail...................) 597 // [all regions fully live] [left-most regions are fuller that right-most] 598 // 599 600 // Compute how much live data is there. This would approximate the size of dense prefix 601 // we target to create. 602 size_t total_live = 0; 603 for (size_t idx = 0; idx < n_regions; idx++) { 604 ShenandoahHeapRegion *r = heap->get_region(idx); 605 if (ShenandoahPrepareForCompactionTask::is_candidate_region(r)) { 606 total_live += r->get_live_data_words(); 607 } 608 } 609 610 // Estimate the size for the dense prefix. Note that we specifically count only the 611 // "full" regions, so there would be some non-full regions in the slice tail. 612 size_t live_per_worker = total_live / n_workers; 613 size_t prefix_regions_per_worker = live_per_worker / ShenandoahHeapRegion::region_size_words(); 614 size_t prefix_regions_total = prefix_regions_per_worker * n_workers; 615 prefix_regions_total = MIN2(prefix_regions_total, n_regions); 616 assert(prefix_regions_total <= n_regions, "Sanity"); 617 618 // There might be non-candidate regions in the prefix. To compute where the tail actually 619 // ends up being, we need to account those as well. 620 size_t prefix_end = prefix_regions_total; 621 for (size_t idx = 0; idx < prefix_regions_total; idx++) { 622 ShenandoahHeapRegion *r = heap->get_region(idx); 623 if (!ShenandoahPrepareForCompactionTask::is_candidate_region(r)) { 624 prefix_end++; 625 } 626 } 627 prefix_end = MIN2(prefix_end, n_regions); 628 assert(prefix_end <= n_regions, "Sanity"); 629 630 // Distribute prefix regions per worker: each thread definitely gets its own same-sized 631 // subset of dense prefix. 632 size_t prefix_idx = 0; 633 634 size_t* live = NEW_C_HEAP_ARRAY(size_t, n_workers, mtGC); 635 636 for (size_t wid = 0; wid < n_workers; wid++) { 637 ShenandoahHeapRegionSet* slice = worker_slices[wid]; 638 639 live[wid] = 0; 640 size_t regs = 0; 641 642 // Add all prefix regions for this worker 643 while (prefix_idx < prefix_end && regs < prefix_regions_per_worker) { 644 ShenandoahHeapRegion *r = heap->get_region(prefix_idx); 645 if (ShenandoahPrepareForCompactionTask::is_candidate_region(r)) { 646 slice->add_region(r); 647 live[wid] += r->get_live_data_words(); 648 regs++; 649 } 650 prefix_idx++; 651 } 652 } 653 654 // Distribute the tail among workers in round-robin fashion. 655 size_t wid = n_workers - 1; 656 657 for (size_t tail_idx = prefix_end; tail_idx < n_regions; tail_idx++) { 658 ShenandoahHeapRegion *r = heap->get_region(tail_idx); 659 if (ShenandoahPrepareForCompactionTask::is_candidate_region(r)) { 660 assert(wid < n_workers, "Sanity"); 661 662 size_t live_region = r->get_live_data_words(); 663 664 // Select next worker that still needs live data. 665 size_t old_wid = wid; 666 do { 667 wid++; 668 if (wid == n_workers) wid = 0; 669 } while (live[wid] + live_region >= live_per_worker && old_wid != wid); 670 671 if (old_wid == wid) { 672 // Circled back to the same worker? This means liveness data was 673 // miscalculated. Bump the live_per_worker limit so that 674 // everyone gets a piece of the leftover work. 675 live_per_worker += ShenandoahHeapRegion::region_size_words(); 676 } 677 678 worker_slices[wid]->add_region(r); 679 live[wid] += live_region; 680 } 681 } 682 683 FREE_C_HEAP_ARRAY(size_t, live); 684 685 #ifdef ASSERT 686 ResourceBitMap map(n_regions); 687 for (size_t wid = 0; wid < n_workers; wid++) { 688 ShenandoahHeapRegionSetIterator it(worker_slices[wid]); 689 ShenandoahHeapRegion* r = it.next(); 690 while (r != nullptr) { 691 size_t idx = r->index(); 692 assert(ShenandoahPrepareForCompactionTask::is_candidate_region(r), "Sanity: " SIZE_FORMAT, idx); 693 assert(!map.at(idx), "No region distributed twice: " SIZE_FORMAT, idx); 694 map.at_put(idx, true); 695 r = it.next(); 696 } 697 } 698 699 for (size_t rid = 0; rid < n_regions; rid++) { 700 bool is_candidate = ShenandoahPrepareForCompactionTask::is_candidate_region(heap->get_region(rid)); 701 bool is_distributed = map.at(rid); 702 assert(is_distributed || !is_candidate, "All candidates are distributed: " SIZE_FORMAT, rid); 703 } 704 #endif 705 } 706 707 void ShenandoahFullGC::phase2_calculate_target_addresses(ShenandoahHeapRegionSet** worker_slices) { 708 GCTraceTime(Info, gc, phases) time("Phase 2: Compute new object addresses", _gc_timer); 709 ShenandoahGCPhase calculate_address_phase(ShenandoahPhaseTimings::full_gc_calculate_addresses); 710 711 ShenandoahHeap* heap = ShenandoahHeap::heap(); 712 713 // About to figure out which regions can be compacted, make sure pinning status 714 // had been updated in GC prologue. 715 heap->assert_pinned_region_status(); 716 717 { 718 // Trash the immediately collectible regions before computing addresses 719 ShenandoahTrashImmediateGarbageClosure tigcl; 720 heap->heap_region_iterate(&tigcl); 721 722 // Make sure regions are in good state: committed, active, clean. 723 // This is needed because we are potentially sliding the data through them. 724 ShenandoahEnsureHeapActiveClosure ecl; 725 heap->heap_region_iterate(&ecl); 726 } 727 728 // Compute the new addresses for regular objects 729 { 730 ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_calculate_addresses_regular); 731 732 distribute_slices(worker_slices); 733 734 ShenandoahPrepareForCompactionTask task(_preserved_marks, worker_slices); 735 heap->workers()->run_task(&task); 736 } 737 738 // Compute the new addresses for humongous objects 739 { 740 ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_calculate_addresses_humong); 741 calculate_target_humongous_objects(); 742 } 743 } 744 745 class ShenandoahAdjustPointersClosure : public MetadataVisitingOopIterateClosure { 746 private: 747 ShenandoahHeap* const _heap; 748 ShenandoahMarkingContext* const _ctx; 749 750 template <class T> 751 inline void do_oop_work(T* p) { 752 T o = RawAccess<>::oop_load(p); 753 if (!CompressedOops::is_null(o)) { 754 oop obj = CompressedOops::decode_not_null(o); 755 assert(_ctx->is_marked(obj), "must be marked"); 756 if (FullGCForwarding::is_forwarded(obj)) { 757 oop forw = FullGCForwarding::forwardee(obj); 758 RawAccess<IS_NOT_NULL>::oop_store(p, forw); 759 } 760 } 761 } 762 763 public: 764 ShenandoahAdjustPointersClosure() : 765 _heap(ShenandoahHeap::heap()), 766 _ctx(ShenandoahHeap::heap()->complete_marking_context()) {} 767 768 void do_oop(oop* p) { do_oop_work(p); } 769 void do_oop(narrowOop* p) { do_oop_work(p); } 770 void do_method(Method* m) {} 771 void do_nmethod(nmethod* nm) {} 772 }; 773 774 class ShenandoahAdjustPointersObjectClosure : public ObjectClosure { 775 private: 776 ShenandoahHeap* const _heap; 777 ShenandoahAdjustPointersClosure _cl; 778 779 public: 780 ShenandoahAdjustPointersObjectClosure() : 781 _heap(ShenandoahHeap::heap()) { 782 } 783 void do_object(oop p) { 784 assert(_heap->complete_marking_context()->is_marked(p), "must be marked"); 785 p->oop_iterate(&_cl); 786 } 787 }; 788 789 class ShenandoahAdjustPointersTask : public WorkerTask { 790 private: 791 ShenandoahHeap* const _heap; 792 ShenandoahRegionIterator _regions; 793 794 public: 795 ShenandoahAdjustPointersTask() : 796 WorkerTask("Shenandoah Adjust Pointers"), 797 _heap(ShenandoahHeap::heap()) { 798 } 799 800 void work(uint worker_id) { 801 ShenandoahParallelWorkerSession worker_session(worker_id); 802 ShenandoahAdjustPointersObjectClosure obj_cl; 803 ShenandoahHeapRegion* r = _regions.next(); 804 while (r != nullptr) { 805 if (!r->is_humongous_continuation() && r->has_live()) { 806 _heap->marked_object_iterate(r, &obj_cl); 807 } 808 r = _regions.next(); 809 } 810 } 811 }; 812 813 class ShenandoahAdjustRootPointersTask : public WorkerTask { 814 private: 815 ShenandoahRootAdjuster* _rp; 816 PreservedMarksSet* _preserved_marks; 817 public: 818 ShenandoahAdjustRootPointersTask(ShenandoahRootAdjuster* rp, PreservedMarksSet* preserved_marks) : 819 WorkerTask("Shenandoah Adjust Root Pointers"), 820 _rp(rp), 821 _preserved_marks(preserved_marks) {} 822 823 void work(uint worker_id) { 824 ShenandoahParallelWorkerSession worker_session(worker_id); 825 ShenandoahAdjustPointersClosure cl; 826 _rp->roots_do(worker_id, &cl); 827 _preserved_marks->get(worker_id)->adjust_during_full_gc(); 828 } 829 }; 830 831 void ShenandoahFullGC::phase3_update_references() { 832 GCTraceTime(Info, gc, phases) time("Phase 3: Adjust pointers", _gc_timer); 833 ShenandoahGCPhase adjust_pointer_phase(ShenandoahPhaseTimings::full_gc_adjust_pointers); 834 835 ShenandoahHeap* heap = ShenandoahHeap::heap(); 836 837 WorkerThreads* workers = heap->workers(); 838 uint nworkers = workers->active_workers(); 839 { 840 #if COMPILER2_OR_JVMCI 841 DerivedPointerTable::clear(); 842 #endif 843 ShenandoahRootAdjuster rp(nworkers, ShenandoahPhaseTimings::full_gc_adjust_roots); 844 ShenandoahAdjustRootPointersTask task(&rp, _preserved_marks); 845 workers->run_task(&task); 846 #if COMPILER2_OR_JVMCI 847 DerivedPointerTable::update_pointers(); 848 #endif 849 } 850 851 ShenandoahAdjustPointersTask adjust_pointers_task; 852 workers->run_task(&adjust_pointers_task); 853 } 854 855 class ShenandoahCompactObjectsClosure : public ObjectClosure { 856 private: 857 ShenandoahHeap* const _heap; 858 uint const _worker_id; 859 860 public: 861 ShenandoahCompactObjectsClosure(uint worker_id) : 862 _heap(ShenandoahHeap::heap()), _worker_id(worker_id) {} 863 864 void do_object(oop p) { 865 assert(_heap->complete_marking_context()->is_marked(p), "must be marked"); 866 size_t size = p->size(); 867 if (FullGCForwarding::is_forwarded(p)) { 868 HeapWord* compact_from = cast_from_oop<HeapWord*>(p); 869 HeapWord* compact_to = cast_from_oop<HeapWord*>(FullGCForwarding::forwardee(p)); 870 assert(compact_from != compact_to, "Forwarded object should move"); 871 Copy::aligned_conjoint_words(compact_from, compact_to, size); 872 oop new_obj = cast_to_oop(compact_to); 873 874 ContinuationGCSupport::relativize_stack_chunk(new_obj); 875 new_obj->init_mark(); 876 } 877 } 878 }; 879 880 class ShenandoahCompactObjectsTask : public WorkerTask { 881 private: 882 ShenandoahHeap* const _heap; 883 ShenandoahHeapRegionSet** const _worker_slices; 884 885 public: 886 ShenandoahCompactObjectsTask(ShenandoahHeapRegionSet** worker_slices) : 887 WorkerTask("Shenandoah Compact Objects"), 888 _heap(ShenandoahHeap::heap()), 889 _worker_slices(worker_slices) { 890 } 891 892 void work(uint worker_id) { 893 ShenandoahParallelWorkerSession worker_session(worker_id); 894 ShenandoahHeapRegionSetIterator slice(_worker_slices[worker_id]); 895 896 ShenandoahCompactObjectsClosure cl(worker_id); 897 ShenandoahHeapRegion* r = slice.next(); 898 while (r != nullptr) { 899 assert(!r->is_humongous(), "must not get humongous regions here"); 900 if (r->has_live()) { 901 _heap->marked_object_iterate(r, &cl); 902 } 903 r->set_top(r->new_top()); 904 r = slice.next(); 905 } 906 } 907 }; 908 909 class ShenandoahPostCompactClosure : public ShenandoahHeapRegionClosure { 910 private: 911 ShenandoahHeap* const _heap; 912 size_t _live; 913 914 public: 915 ShenandoahPostCompactClosure() : _heap(ShenandoahHeap::heap()), _live(0) { 916 } 917 918 void heap_region_do(ShenandoahHeapRegion* r) { 919 assert (!r->is_cset(), "cset regions should have been demoted already"); 920 921 // Need to reset the complete-top-at-mark-start pointer here because 922 // the complete marking bitmap is no longer valid. This ensures 923 // size-based iteration in marked_object_iterate(). 924 // NOTE: See blurb at ShenandoahMCResetCompleteBitmapTask on why we need to skip 925 // pinned regions. 926 if (!r->is_pinned()) { 927 _heap->complete_marking_context()->reset_top_at_mark_start(r); 928 } 929 930 size_t live = r->used(); 931 932 // Make empty regions that have been allocated into regular 933 if (r->is_empty() && live > 0) { 934 r->make_regular_bypass(); 935 if (ZapUnusedHeapArea) { 936 SpaceMangler::mangle_region(MemRegion(r->top(), r->end())); 937 } 938 } 939 940 // Reclaim regular regions that became empty 941 if (r->is_regular() && live == 0) { 942 r->make_trash(); 943 } 944 945 // Recycle all trash regions 946 if (r->is_trash()) { 947 live = 0; 948 r->recycle(); 949 } 950 951 r->set_live_data(live); 952 r->reset_alloc_metadata(); 953 _live += live; 954 } 955 956 size_t get_live() { 957 return _live; 958 } 959 }; 960 961 void ShenandoahFullGC::compact_humongous_objects() { 962 // Compact humongous regions, based on their fwdptr objects. 963 // 964 // This code is serial, because doing the in-slice parallel sliding is tricky. In most cases, 965 // humongous regions are already compacted, and do not require further moves, which alleviates 966 // sliding costs. We may consider doing this in parallel in the future. 967 968 ShenandoahHeap* heap = ShenandoahHeap::heap(); 969 970 for (size_t c = heap->num_regions(); c > 0; c--) { 971 ShenandoahHeapRegion* r = heap->get_region(c - 1); 972 if (r->is_humongous_start()) { 973 oop old_obj = cast_to_oop(r->bottom()); 974 if (!FullGCForwarding::is_forwarded(old_obj)) { 975 // No need to move the object, it stays at the same slot 976 continue; 977 } 978 size_t words_size = old_obj->size(); 979 size_t num_regions = ShenandoahHeapRegion::required_regions(words_size * HeapWordSize); 980 981 size_t old_start = r->index(); 982 size_t old_end = old_start + num_regions - 1; 983 size_t new_start = heap->heap_region_index_containing(FullGCForwarding::forwardee(old_obj)); 984 size_t new_end = new_start + num_regions - 1; 985 assert(old_start != new_start, "must be real move"); 986 assert(r->is_stw_move_allowed(), "Region " SIZE_FORMAT " should be movable", r->index()); 987 988 Copy::aligned_conjoint_words(r->bottom(), heap->get_region(new_start)->bottom(), words_size); 989 ContinuationGCSupport::relativize_stack_chunk(cast_to_oop<HeapWord*>(r->bottom())); 990 991 oop new_obj = cast_to_oop(heap->get_region(new_start)->bottom()); 992 new_obj->init_mark(); 993 994 { 995 for (size_t c = old_start; c <= old_end; c++) { 996 ShenandoahHeapRegion* r = heap->get_region(c); 997 r->make_regular_bypass(); 998 r->set_top(r->bottom()); 999 } 1000 1001 for (size_t c = new_start; c <= new_end; c++) { 1002 ShenandoahHeapRegion* r = heap->get_region(c); 1003 if (c == new_start) { 1004 r->make_humongous_start_bypass(); 1005 } else { 1006 r->make_humongous_cont_bypass(); 1007 } 1008 1009 // Trailing region may be non-full, record the remainder there 1010 size_t remainder = words_size & ShenandoahHeapRegion::region_size_words_mask(); 1011 if ((c == new_end) && (remainder != 0)) { 1012 r->set_top(r->bottom() + remainder); 1013 } else { 1014 r->set_top(r->end()); 1015 } 1016 1017 r->reset_alloc_metadata(); 1018 } 1019 } 1020 } 1021 } 1022 } 1023 1024 // This is slightly different to ShHeap::reset_next_mark_bitmap: 1025 // we need to remain able to walk pinned regions. 1026 // Since pinned region do not move and don't get compacted, we will get holes with 1027 // unreachable objects in them (which may have pointers to unloaded Klasses and thus 1028 // cannot be iterated over using oop->size(). The only way to safely iterate over those is using 1029 // a valid marking bitmap and valid TAMS pointer. This class only resets marking 1030 // bitmaps for un-pinned regions, and later we only reset TAMS for unpinned regions. 1031 class ShenandoahMCResetCompleteBitmapTask : public WorkerTask { 1032 private: 1033 ShenandoahRegionIterator _regions; 1034 1035 public: 1036 ShenandoahMCResetCompleteBitmapTask() : 1037 WorkerTask("Shenandoah Reset Bitmap") { 1038 } 1039 1040 void work(uint worker_id) { 1041 ShenandoahParallelWorkerSession worker_session(worker_id); 1042 ShenandoahHeapRegion* region = _regions.next(); 1043 ShenandoahHeap* heap = ShenandoahHeap::heap(); 1044 ShenandoahMarkingContext* const ctx = heap->complete_marking_context(); 1045 while (region != nullptr) { 1046 if (heap->is_bitmap_slice_committed(region) && !region->is_pinned() && region->has_live()) { 1047 ctx->clear_bitmap(region); 1048 } 1049 region = _regions.next(); 1050 } 1051 } 1052 }; 1053 1054 void ShenandoahFullGC::phase4_compact_objects(ShenandoahHeapRegionSet** worker_slices) { 1055 GCTraceTime(Info, gc, phases) time("Phase 4: Move objects", _gc_timer); 1056 ShenandoahGCPhase compaction_phase(ShenandoahPhaseTimings::full_gc_copy_objects); 1057 1058 ShenandoahHeap* heap = ShenandoahHeap::heap(); 1059 1060 // Compact regular objects first 1061 { 1062 ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_regular); 1063 ShenandoahCompactObjectsTask compact_task(worker_slices); 1064 heap->workers()->run_task(&compact_task); 1065 } 1066 1067 // Compact humongous objects after regular object moves 1068 { 1069 ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_humong); 1070 compact_humongous_objects(); 1071 } 1072 } 1073 1074 void ShenandoahFullGC::phase5_epilog() { 1075 GCTraceTime(Info, gc, phases) time("Phase 5: Full GC epilog", _gc_timer); 1076 ShenandoahHeap* heap = ShenandoahHeap::heap(); 1077 1078 // Reset complete bitmap. We're about to reset the complete-top-at-mark-start pointer 1079 // and must ensure the bitmap is in sync. 1080 { 1081 ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_reset_complete); 1082 ShenandoahMCResetCompleteBitmapTask task; 1083 heap->workers()->run_task(&task); 1084 } 1085 1086 // Bring regions in proper states after the collection, and set heap properties. 1087 { 1088 ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_rebuild); 1089 ShenandoahPostCompactClosure post_compact; 1090 heap->heap_region_iterate(&post_compact); 1091 heap->set_used(post_compact.get_live()); 1092 1093 heap->collection_set()->clear(); 1094 heap->free_set()->rebuild(); 1095 heap->clear_cancelled_gc(); 1096 } 1097 1098 _preserved_marks->restore(heap->workers()); 1099 _preserved_marks->reclaim(); 1100 }