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