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