< prev index next >

src/hotspot/share/gc/shenandoah/shenandoahFullGC.cpp

Print this page

   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/gcTraceTime.inline.hpp"
  30 #include "gc/shared/preservedMarks.inline.hpp"
  31 #include "gc/shared/tlab_globals.hpp"
  32 #include "gc/shared/workerThread.hpp"
  33 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
  34 #include "gc/shenandoah/shenandoahConcurrentGC.hpp"
  35 #include "gc/shenandoah/shenandoahCollectionSet.hpp"

  36 #include "gc/shenandoah/shenandoahFreeSet.hpp"
  37 #include "gc/shenandoah/shenandoahFullGC.hpp"

  38 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
  39 #include "gc/shenandoah/shenandoahMark.inline.hpp"
  40 #include "gc/shenandoah/shenandoahMonitoringSupport.hpp"
  41 #include "gc/shenandoah/shenandoahHeapRegionSet.hpp"
  42 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  43 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
  44 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
  45 #include "gc/shenandoah/shenandoahMetrics.hpp"

  46 #include "gc/shenandoah/shenandoahOopClosures.inline.hpp"
  47 #include "gc/shenandoah/shenandoahReferenceProcessor.hpp"
  48 #include "gc/shenandoah/shenandoahRootProcessor.inline.hpp"
  49 #include "gc/shenandoah/shenandoahSTWMark.hpp"
  50 #include "gc/shenandoah/shenandoahUtils.hpp"
  51 #include "gc/shenandoah/shenandoahVerifier.hpp"
  52 #include "gc/shenandoah/shenandoahVMOperations.hpp"
  53 #include "gc/shenandoah/shenandoahWorkerPolicy.hpp"

  54 #include "memory/metaspaceUtils.hpp"
  55 #include "memory/universe.hpp"
  56 #include "oops/compressedOops.inline.hpp"
  57 #include "oops/oop.inline.hpp"
  58 #include "runtime/javaThread.hpp"
  59 #include "runtime/orderAccess.hpp"
  60 #include "runtime/vmThread.hpp"
  61 #include "utilities/copy.hpp"
  62 #include "utilities/events.hpp"
  63 #include "utilities/growableArray.hpp"
  64 































































  65 ShenandoahFullGC::ShenandoahFullGC() :
  66   _gc_timer(ShenandoahHeap::heap()->gc_timer()),
  67   _preserved_marks(new PreservedMarksSet(true)) {}
  68 
  69 ShenandoahFullGC::~ShenandoahFullGC() {
  70   delete _preserved_marks;
  71 }
  72 
  73 bool ShenandoahFullGC::collect(GCCause::Cause cause) {
  74   vmop_entry_full(cause);
  75   // Always success
  76   return true;
  77 }
  78 
  79 void ShenandoahFullGC::vmop_entry_full(GCCause::Cause cause) {
  80   ShenandoahHeap* const heap = ShenandoahHeap::heap();
  81   TraceCollectorStats tcs(heap->monitoring_support()->full_stw_collection_counters());
  82   ShenandoahTimingsTracker timing(ShenandoahPhaseTimings::full_gc_gross);
  83 
  84   heap->try_inject_alloc_failure();
  85   VM_ShenandoahFullGC op(cause, this);
  86   VMThread::execute(&op);
  87 }
  88 
  89 void ShenandoahFullGC::entry_full(GCCause::Cause cause) {
  90   static const char* msg = "Pause Full";
  91   ShenandoahPausePhase gc_phase(msg, ShenandoahPhaseTimings::full_gc, true /* log_heap_usage */);
  92   EventMark em("%s", msg);
  93 
  94   ShenandoahWorkerScope scope(ShenandoahHeap::heap()->workers(),
  95                               ShenandoahWorkerPolicy::calc_workers_for_fullgc(),
  96                               "full gc");
  97 
  98   op_full(cause);
  99 }
 100 
 101 void ShenandoahFullGC::op_full(GCCause::Cause cause) {

 102   ShenandoahMetricsSnapshot metrics;
 103   metrics.snap_before();
 104 
 105   // Perform full GC
 106   do_it(cause);
 107 
 108   metrics.snap_after();
 109 


























 110   if (metrics.is_good_progress()) {
 111     ShenandoahHeap::heap()->notify_gc_progress();
 112   } else {
 113     // Nothing to do. Tell the allocation path that we have failed to make
 114     // progress, and it can finally fail.
 115     ShenandoahHeap::heap()->notify_gc_no_progress();
 116   }




 117 }
 118 
 119 void ShenandoahFullGC::do_it(GCCause::Cause gc_cause) {
 120   ShenandoahHeap* heap = ShenandoahHeap::heap();












 121 
 122   if (ShenandoahVerify) {
 123     heap->verifier()->verify_before_fullgc();
 124   }
 125 
 126   if (VerifyBeforeGC) {
 127     Universe::verify();
 128   }
 129 
 130   // Degenerated GC may carry concurrent root flags when upgrading to
 131   // full GC. We need to reset it before mutators resume.
 132   heap->set_concurrent_strong_root_in_progress(false);
 133   heap->set_concurrent_weak_root_in_progress(false);
 134 
 135   heap->set_full_gc_in_progress(true);
 136 
 137   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "must be at a safepoint");
 138   assert(Thread::current()->is_VM_thread(), "Do full GC only while world is stopped");
 139 
 140   {

 144 
 145   {
 146     ShenandoahGCPhase prepare_phase(ShenandoahPhaseTimings::full_gc_prepare);
 147     // Full GC is supposed to recover from any GC state:
 148 
 149     // a0. Remember if we have forwarded objects
 150     bool has_forwarded_objects = heap->has_forwarded_objects();
 151 
 152     // a1. Cancel evacuation, if in progress
 153     if (heap->is_evacuation_in_progress()) {
 154       heap->set_evacuation_in_progress(false);
 155     }
 156     assert(!heap->is_evacuation_in_progress(), "sanity");
 157 
 158     // a2. Cancel update-refs, if in progress
 159     if (heap->is_update_refs_in_progress()) {
 160       heap->set_update_refs_in_progress(false);
 161     }
 162     assert(!heap->is_update_refs_in_progress(), "sanity");
 163 
 164     // b. Cancel concurrent mark, if in progress
 165     if (heap->is_concurrent_mark_in_progress()) {
 166       ShenandoahConcurrentGC::cancel();
 167       heap->set_concurrent_mark_in_progress(false);
 168     }
 169     assert(!heap->is_concurrent_mark_in_progress(), "sanity");
 170 
 171     // c. Update roots if this full GC is due to evac-oom, which may carry from-space pointers in roots.
 172     if (has_forwarded_objects) {
 173       update_roots(true /*full_gc*/);
 174     }
 175 
 176     // d. Reset the bitmaps for new marking
 177     heap->reset_mark_bitmap();
 178     assert(heap->marking_context()->is_bitmap_clear(), "sanity");
 179     assert(!heap->marking_context()->is_complete(), "sanity");
 180 
 181     // e. Abandon reference discovery and clear all discovered references.
 182     ShenandoahReferenceProcessor* rp = heap->ref_processor();
 183     rp->abandon_partial_discovery();
 184 
 185     // f. Sync pinned region status from the CP marks
 186     heap->sync_pinned_region_status();
 187 









 188     // The rest of prologue:
 189     _preserved_marks->init(heap->workers()->active_workers());
 190 
 191     assert(heap->has_forwarded_objects() == has_forwarded_objects, "This should not change");
 192   }
 193 
 194   if (UseTLAB) {

 195     heap->gclabs_retire(ResizeTLAB);
 196     heap->tlabs_retire(ResizeTLAB);
 197   }
 198 
 199   OrderAccess::fence();
 200 
 201   phase1_mark_heap();
 202 
 203   // Once marking is done, which may have fixed up forwarded objects, we can drop it.
 204   // Coming out of Full GC, we would not have any forwarded objects.
 205   // This also prevents resolves with fwdptr from kicking in while adjusting pointers in phase3.
 206   heap->set_has_forwarded_objects(false);
 207 
 208   heap->set_full_gc_move_in_progress(true);
 209 
 210   // Setup workers for the rest
 211   OrderAccess::fence();
 212 
 213   // Initialize worker slices
 214   ShenandoahHeapRegionSet** worker_slices = NEW_C_HEAP_ARRAY(ShenandoahHeapRegionSet*, heap->max_workers(), mtGC);
 215   for (uint i = 0; i < heap->max_workers(); i++) {
 216     worker_slices[i] = new ShenandoahHeapRegionSet();
 217   }
 218 
 219   {
 220     // The rest of code performs region moves, where region status is undefined
 221     // until all phases run together.
 222     ShenandoahHeapLocker lock(heap->lock());
 223 
 224     phase2_calculate_target_addresses(worker_slices);
 225 
 226     OrderAccess::fence();
 227 
 228     phase3_update_references();
 229 
 230     phase4_compact_objects(worker_slices);


 231   }
 232 
 233   {
 234     // Epilogue

 235     _preserved_marks->restore(heap->workers());
 236     _preserved_marks->reclaim();






 237   }
 238 
 239   // Resize metaspace
 240   MetaspaceGC::compute_new_size();
 241 
 242   // Free worker slices
 243   for (uint i = 0; i < heap->max_workers(); i++) {
 244     delete worker_slices[i];
 245   }
 246   FREE_C_HEAP_ARRAY(ShenandoahHeapRegionSet*, worker_slices);
 247 
 248   heap->set_full_gc_move_in_progress(false);
 249   heap->set_full_gc_in_progress(false);
 250 
 251   if (ShenandoahVerify) {
 252     heap->verifier()->verify_after_fullgc();
 253   }
 254 

 255   if (VerifyAfterGC) {
 256     Universe::verify();
 257   }
 258 
 259   {
 260     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_heapdump_post);
 261     heap->post_full_gc_dump(_gc_timer);
 262   }
 263 }
 264 
 265 class ShenandoahPrepareForMarkClosure: public ShenandoahHeapRegionClosure {
 266 private:
 267   ShenandoahMarkingContext* const _ctx;
 268 
 269 public:
 270   ShenandoahPrepareForMarkClosure() : _ctx(ShenandoahHeap::heap()->marking_context()) {}
 271 
 272   void heap_region_do(ShenandoahHeapRegion *r) {
 273     _ctx->capture_top_at_mark_start(r);
 274     r->clear_live_data();


 275   }


 276 };
 277 
 278 void ShenandoahFullGC::phase1_mark_heap() {
 279   GCTraceTime(Info, gc, phases) time("Phase 1: Mark live objects", _gc_timer);
 280   ShenandoahGCPhase mark_phase(ShenandoahPhaseTimings::full_gc_mark);
 281 
 282   ShenandoahHeap* heap = ShenandoahHeap::heap();
 283 
 284   ShenandoahPrepareForMarkClosure cl;
 285   heap->heap_region_iterate(&cl);
 286 
 287   heap->set_unload_classes(heap->heuristics()->can_unload_classes());
 288 
 289   ShenandoahReferenceProcessor* rp = heap->ref_processor();
 290   // enable ("weak") refs discovery
 291   rp->set_soft_reference_policy(true); // forcefully purge all soft references
 292 
 293   ShenandoahSTWMark mark(true /*full_gc*/);
 294   mark.mark();
 295   heap->parallel_cleaning(true /* full_gc */);









 296 }
 297 











































































































































































































































 298 class ShenandoahPrepareForCompactionObjectClosure : public ObjectClosure {
 299 private:
 300   PreservedMarks*          const _preserved_marks;
 301   ShenandoahHeap*          const _heap;
 302   GrowableArray<ShenandoahHeapRegion*>& _empty_regions;
 303   int _empty_regions_pos;
 304   ShenandoahHeapRegion*          _to_region;
 305   ShenandoahHeapRegion*          _from_region;
 306   HeapWord* _compact_point;
 307 
 308 public:
 309   ShenandoahPrepareForCompactionObjectClosure(PreservedMarks* preserved_marks,
 310                                               GrowableArray<ShenandoahHeapRegion*>& empty_regions,
 311                                               ShenandoahHeapRegion* to_region) :
 312     _preserved_marks(preserved_marks),
 313     _heap(ShenandoahHeap::heap()),
 314     _empty_regions(empty_regions),
 315     _empty_regions_pos(0),
 316     _to_region(to_region),
 317     _from_region(nullptr),
 318     _compact_point(to_region->bottom()) {}
 319 
 320   void set_from_region(ShenandoahHeapRegion* from_region) {
 321     _from_region = from_region;
 322   }
 323 
 324   void finish_region() {
 325     assert(_to_region != nullptr, "should not happen");

 326     _to_region->set_new_top(_compact_point);
 327   }
 328 
 329   bool is_compact_same_region() {
 330     return _from_region == _to_region;
 331   }
 332 
 333   int empty_regions_pos() {
 334     return _empty_regions_pos;
 335   }
 336 
 337   void do_object(oop p) {
 338     assert(_from_region != nullptr, "must set before work");
 339     assert(_heap->complete_marking_context()->is_marked(p), "must be marked");
 340     assert(!_heap->complete_marking_context()->allocated_after_mark_start(p), "must be truly marked");
 341 
 342     size_t obj_size = p->size();
 343     if (_compact_point + obj_size > _to_region->end()) {
 344       finish_region();
 345 

 351       } else {
 352         // Out of empty region? Compact within the same region.
 353         new_to_region = _from_region;
 354       }
 355 
 356       assert(new_to_region != _to_region, "must not reuse same to-region");
 357       assert(new_to_region != nullptr, "must not be null");
 358       _to_region = new_to_region;
 359       _compact_point = _to_region->bottom();
 360     }
 361 
 362     // Object fits into current region, record new location:
 363     assert(_compact_point + obj_size <= _to_region->end(), "must fit");
 364     shenandoah_assert_not_forwarded(nullptr, p);
 365     _preserved_marks->push_if_necessary(p, p->mark());
 366     p->forward_to(cast_to_oop(_compact_point));
 367     _compact_point += obj_size;
 368   }
 369 };
 370 
 371 class ShenandoahPrepareForCompactionTask : public WorkerTask {
 372 private:
 373   PreservedMarksSet*        const _preserved_marks;
 374   ShenandoahHeap*           const _heap;
 375   ShenandoahHeapRegionSet** const _worker_slices;
 376 
 377 public:
 378   ShenandoahPrepareForCompactionTask(PreservedMarksSet *preserved_marks, ShenandoahHeapRegionSet **worker_slices) :

 379     WorkerTask("Shenandoah Prepare For Compaction"),
 380     _preserved_marks(preserved_marks),
 381     _heap(ShenandoahHeap::heap()), _worker_slices(worker_slices) {










 382   }
 383 
 384   static bool is_candidate_region(ShenandoahHeapRegion* r) {
 385     // Empty region: get it into the slice to defragment the slice itself.
 386     // We could have skipped this without violating correctness, but we really
 387     // want to compact all live regions to the start of the heap, which sometimes
 388     // means moving them into the fully empty regions.
 389     if (r->is_empty()) return true;
 390 
 391     // Can move the region, and this is not the humongous region. Humongous
 392     // moves are special cased here, because their moves are handled separately.
 393     return r->is_stw_move_allowed() && !r->is_humongous();
 394   }
 395 
 396   void work(uint worker_id) {
 397     ShenandoahParallelWorkerSession worker_session(worker_id);
 398     ShenandoahHeapRegionSet* slice = _worker_slices[worker_id];
 399     ShenandoahHeapRegionSetIterator it(slice);
 400     ShenandoahHeapRegion* from_region = it.next();
 401     // No work?
 402     if (from_region == nullptr) {
 403        return;













 404     }

 405 
 406     // Sliding compaction. Walk all regions in the slice, and compact them.
 407     // Remember empty regions and reuse them as needed.
 408     ResourceMark rm;
 409 
 410     GrowableArray<ShenandoahHeapRegion*> empty_regions((int)_heap->num_regions());
 411 
 412     ShenandoahPrepareForCompactionObjectClosure cl(_preserved_marks->get(worker_id), empty_regions, from_region);
 413 
 414     while (from_region != nullptr) {
 415       assert(is_candidate_region(from_region), "Sanity");
 416 
 417       cl.set_from_region(from_region);
 418       if (from_region->has_live()) {
 419         _heap->marked_object_iterate(from_region, &cl);
 420       }
 421 
 422       // Compacted the region to somewhere else? From-region is empty then.
 423       if (!cl.is_compact_same_region()) {
 424         empty_regions.append(from_region);
 425       }
 426       from_region = it.next();
 427     }
 428     cl.finish_region();
 429 
 430     // Mark all remaining regions as empty
 431     for (int pos = cl.empty_regions_pos(); pos < empty_regions.length(); ++pos) {
 432       ShenandoahHeapRegion* r = empty_regions.at(pos);
 433       r->set_new_top(r->bottom());
 434     }
 435   }
 436 };
 437 
 438 void ShenandoahFullGC::calculate_target_humongous_objects() {
 439   ShenandoahHeap* heap = ShenandoahHeap::heap();
 440 
 441   // Compute the new addresses for humongous objects. We need to do this after addresses
 442   // for regular objects are calculated, and we know what regions in heap suffix are
 443   // available for humongous moves.
 444   //
 445   // Scan the heap backwards, because we are compacting humongous regions towards the end.
 446   // Maintain the contiguous compaction window in [to_begin; to_end), so that we can slide
 447   // humongous start there.
 448   //
 449   // The complication is potential non-movable regions during the scan. If such region is
 450   // detected, then sliding restarts towards that non-movable region.
 451 
 452   size_t to_begin = heap->num_regions();
 453   size_t to_end = heap->num_regions();
 454 

 455   for (size_t c = heap->num_regions(); c > 0; c--) {
 456     ShenandoahHeapRegion *r = heap->get_region(c - 1);
 457     if (r->is_humongous_continuation() || (r->new_top() == r->bottom())) {
 458       // To-region candidate: record this, and continue scan
 459       to_begin = r->index();
 460       continue;
 461     }
 462 
 463     if (r->is_humongous_start() && r->is_stw_move_allowed()) {
 464       // From-region candidate: movable humongous region
 465       oop old_obj = cast_to_oop(r->bottom());
 466       size_t words_size = old_obj->size();
 467       size_t num_regions = ShenandoahHeapRegion::required_regions(words_size * HeapWordSize);
 468 
 469       size_t start = to_end - num_regions;
 470 
 471       if (start >= to_begin && start != r->index()) {
 472         // Fits into current window, and the move is non-trivial. Record the move then, and continue scan.
 473         _preserved_marks->get(0)->push_if_necessary(old_obj, old_obj->mark());
 474         old_obj->forward_to(cast_to_oop(heap->get_region(start)->bottom()));

 477       }
 478     }
 479 
 480     // Failed to fit. Scan starting from current region.
 481     to_begin = r->index();
 482     to_end = r->index();
 483   }
 484 }
 485 
 486 class ShenandoahEnsureHeapActiveClosure: public ShenandoahHeapRegionClosure {
 487 private:
 488   ShenandoahHeap* const _heap;
 489 
 490 public:
 491   ShenandoahEnsureHeapActiveClosure() : _heap(ShenandoahHeap::heap()) {}
 492   void heap_region_do(ShenandoahHeapRegion* r) {
 493     if (r->is_trash()) {
 494       r->recycle();
 495     }
 496     if (r->is_cset()) {

 497       r->make_regular_bypass();
 498     }
 499     if (r->is_empty_uncommitted()) {
 500       r->make_committed_bypass();
 501     }
 502     assert (r->is_committed(), "only committed regions in heap now, see region " SIZE_FORMAT, r->index());
 503 
 504     // Record current region occupancy: this communicates empty regions are free
 505     // to the rest of Full GC code.
 506     r->set_new_top(r->top());
 507   }
 508 };
 509 
 510 class ShenandoahTrashImmediateGarbageClosure: public ShenandoahHeapRegionClosure {
 511 private:
 512   ShenandoahHeap* const _heap;
 513   ShenandoahMarkingContext* const _ctx;
 514 
 515 public:
 516   ShenandoahTrashImmediateGarbageClosure() :
 517     _heap(ShenandoahHeap::heap()),
 518     _ctx(ShenandoahHeap::heap()->complete_marking_context()) {}
 519 
 520   void heap_region_do(ShenandoahHeapRegion* r) {






 521     if (r->is_humongous_start()) {
 522       oop humongous_obj = cast_to_oop(r->bottom());
 523       if (!_ctx->is_marked(humongous_obj)) {
 524         assert(!r->has_live(),
 525                "Region " SIZE_FORMAT " is not marked, should not have live", r->index());


 526         _heap->trash_humongous_region_at(r);
 527       } else {
 528         assert(r->has_live(),
 529                "Region " SIZE_FORMAT " should have live", r->index());
 530       }
 531     } else if (r->is_humongous_continuation()) {
 532       // If we hit continuation, the non-live humongous starts should have been trashed already
 533       assert(r->humongous_start_region()->has_live(),
 534              "Region " SIZE_FORMAT " should have live", r->index());
 535     } else if (r->is_regular()) {
 536       if (!r->has_live()) {

 537         r->make_trash_immediate();
 538       }
 539     }
 540   }
 541 };
 542 
 543 void ShenandoahFullGC::distribute_slices(ShenandoahHeapRegionSet** worker_slices) {
 544   ShenandoahHeap* heap = ShenandoahHeap::heap();
 545 
 546   uint n_workers = heap->workers()->active_workers();
 547   size_t n_regions = heap->num_regions();
 548 
 549   // What we want to accomplish: have the dense prefix of data, while still balancing
 550   // out the parallel work.
 551   //
 552   // Assuming the amount of work is driven by the live data that needs moving, we can slice
 553   // the entire heap into equal-live-sized prefix slices, and compact into them. So, each
 554   // thread takes all regions in its prefix subset, and then it takes some regions from
 555   // the tail.
 556   //

 665   for (size_t wid = 0; wid < n_workers; wid++) {
 666     ShenandoahHeapRegionSetIterator it(worker_slices[wid]);
 667     ShenandoahHeapRegion* r = it.next();
 668     while (r != nullptr) {
 669       size_t idx = r->index();
 670       assert(ShenandoahPrepareForCompactionTask::is_candidate_region(r), "Sanity: " SIZE_FORMAT, idx);
 671       assert(!map.at(idx), "No region distributed twice: " SIZE_FORMAT, idx);
 672       map.at_put(idx, true);
 673       r = it.next();
 674     }
 675   }
 676 
 677   for (size_t rid = 0; rid < n_regions; rid++) {
 678     bool is_candidate = ShenandoahPrepareForCompactionTask::is_candidate_region(heap->get_region(rid));
 679     bool is_distributed = map.at(rid);
 680     assert(is_distributed || !is_candidate, "All candidates are distributed: " SIZE_FORMAT, rid);
 681   }
 682 #endif
 683 }
 684 





 685 void ShenandoahFullGC::phase2_calculate_target_addresses(ShenandoahHeapRegionSet** worker_slices) {
 686   GCTraceTime(Info, gc, phases) time("Phase 2: Compute new object addresses", _gc_timer);
 687   ShenandoahGCPhase calculate_address_phase(ShenandoahPhaseTimings::full_gc_calculate_addresses);
 688 
 689   ShenandoahHeap* heap = ShenandoahHeap::heap();
 690 
 691   // About to figure out which regions can be compacted, make sure pinning status
 692   // had been updated in GC prologue.
 693   heap->assert_pinned_region_status();
 694 
 695   {
 696     // Trash the immediately collectible regions before computing addresses
 697     ShenandoahTrashImmediateGarbageClosure tigcl;
 698     heap->heap_region_iterate(&tigcl);
 699 
 700     // Make sure regions are in good state: committed, active, clean.
 701     // This is needed because we are potentially sliding the data through them.
 702     ShenandoahEnsureHeapActiveClosure ecl;
 703     heap->heap_region_iterate(&ecl);
 704   }
 705 
 706   // Compute the new addresses for regular objects
 707   {
 708     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_calculate_addresses_regular);
 709 
 710     distribute_slices(worker_slices);
 711 
 712     ShenandoahPrepareForCompactionTask task(_preserved_marks, worker_slices);



 713     heap->workers()->run_task(&task);
 714   }
 715 
 716   // Compute the new addresses for humongous objects
 717   {
 718     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_calculate_addresses_humong);
 719     calculate_target_humongous_objects();
 720   }
 721 }
 722 
 723 class ShenandoahAdjustPointersClosure : public MetadataVisitingOopIterateClosure {
 724 private:
 725   ShenandoahHeap* const _heap;
 726   ShenandoahMarkingContext* const _ctx;
 727 
 728   template <class T>
 729   inline void do_oop_work(T* p) {
 730     T o = RawAccess<>::oop_load(p);
 731     if (!CompressedOops::is_null(o)) {
 732       oop obj = CompressedOops::decode_not_null(o);

 766 
 767 class ShenandoahAdjustPointersTask : public WorkerTask {
 768 private:
 769   ShenandoahHeap*          const _heap;
 770   ShenandoahRegionIterator       _regions;
 771 
 772 public:
 773   ShenandoahAdjustPointersTask() :
 774     WorkerTask("Shenandoah Adjust Pointers"),
 775     _heap(ShenandoahHeap::heap()) {
 776   }
 777 
 778   void work(uint worker_id) {
 779     ShenandoahParallelWorkerSession worker_session(worker_id);
 780     ShenandoahAdjustPointersObjectClosure obj_cl;
 781     ShenandoahHeapRegion* r = _regions.next();
 782     while (r != nullptr) {
 783       if (!r->is_humongous_continuation() && r->has_live()) {
 784         _heap->marked_object_iterate(r, &obj_cl);
 785       }







 786       r = _regions.next();
 787     }
 788   }
 789 };
 790 
 791 class ShenandoahAdjustRootPointersTask : public WorkerTask {
 792 private:
 793   ShenandoahRootAdjuster* _rp;
 794   PreservedMarksSet* _preserved_marks;
 795 public:
 796   ShenandoahAdjustRootPointersTask(ShenandoahRootAdjuster* rp, PreservedMarksSet* preserved_marks) :
 797     WorkerTask("Shenandoah Adjust Root Pointers"),
 798     _rp(rp),
 799     _preserved_marks(preserved_marks) {}
 800 
 801   void work(uint worker_id) {
 802     ShenandoahParallelWorkerSession worker_session(worker_id);
 803     ShenandoahAdjustPointersClosure cl;
 804     _rp->roots_do(worker_id, &cl);
 805     _preserved_marks->get(worker_id)->adjust_during_full_gc();

 866     _worker_slices(worker_slices) {
 867   }
 868 
 869   void work(uint worker_id) {
 870     ShenandoahParallelWorkerSession worker_session(worker_id);
 871     ShenandoahHeapRegionSetIterator slice(_worker_slices[worker_id]);
 872 
 873     ShenandoahCompactObjectsClosure cl(worker_id);
 874     ShenandoahHeapRegion* r = slice.next();
 875     while (r != nullptr) {
 876       assert(!r->is_humongous(), "must not get humongous regions here");
 877       if (r->has_live()) {
 878         _heap->marked_object_iterate(r, &cl);
 879       }
 880       r->set_top(r->new_top());
 881       r = slice.next();
 882     }
 883   }
 884 };
 885 

















 886 class ShenandoahPostCompactClosure : public ShenandoahHeapRegionClosure {
 887 private:
 888   ShenandoahHeap* const _heap;
 889   size_t _live;


 890 
 891 public:
 892   ShenandoahPostCompactClosure() : _heap(ShenandoahHeap::heap()), _live(0) {








 893     _heap->free_set()->clear();
 894   }
 895 
 896   void heap_region_do(ShenandoahHeapRegion* r) {
 897     assert (!r->is_cset(), "cset regions should have been demoted already");
 898 
 899     // Need to reset the complete-top-at-mark-start pointer here because
 900     // the complete marking bitmap is no longer valid. This ensures
 901     // size-based iteration in marked_object_iterate().
 902     // NOTE: See blurb at ShenandoahMCResetCompleteBitmapTask on why we need to skip
 903     // pinned regions.
 904     if (!r->is_pinned()) {
 905       _heap->complete_marking_context()->reset_top_at_mark_start(r);
 906     }
 907 
 908     size_t live = r->used();
 909 
 910     // Make empty regions that have been allocated into regular
 911     if (r->is_empty() && live > 0) {




 912       r->make_regular_bypass();
 913       if (ZapUnusedHeapArea) {
 914         SpaceMangler::mangle_region(MemRegion(r->top(), r->end()));
 915       }
 916     }
 917 
 918     // Reclaim regular regions that became empty
 919     if (r->is_regular() && live == 0) {
 920       r->make_trash();
 921     }
 922 
 923     // Recycle all trash regions
 924     if (r->is_trash()) {
 925       live = 0;
 926       r->recycle();






 927     }
 928 
 929     r->set_live_data(live);
 930     r->reset_alloc_metadata();
 931     _live += live;
 932   }
 933 
 934   size_t get_live() {
 935     return _live;













 936   }
 937 };
 938 
 939 void ShenandoahFullGC::compact_humongous_objects() {
 940   // Compact humongous regions, based on their fwdptr objects.
 941   //
 942   // This code is serial, because doing the in-slice parallel sliding is tricky. In most cases,
 943   // humongous regions are already compacted, and do not require further moves, which alleviates
 944   // sliding costs. We may consider doing this in parallel in future.
 945 
 946   ShenandoahHeap* heap = ShenandoahHeap::heap();
 947 
 948   for (size_t c = heap->num_regions(); c > 0; c--) {
 949     ShenandoahHeapRegion* r = heap->get_region(c - 1);
 950     if (r->is_humongous_start()) {
 951       oop old_obj = cast_to_oop(r->bottom());
 952       if (!old_obj->is_forwarded()) {
 953         // No need to move the object, it stays at the same slot
 954         continue;
 955       }
 956       size_t words_size = old_obj->size();
 957       size_t num_regions = ShenandoahHeapRegion::required_regions(words_size * HeapWordSize);
 958 
 959       size_t old_start = r->index();
 960       size_t old_end   = old_start + num_regions - 1;
 961       size_t new_start = heap->heap_region_index_containing(old_obj->forwardee());
 962       size_t new_end   = new_start + num_regions - 1;
 963       assert(old_start != new_start, "must be real move");
 964       assert(r->is_stw_move_allowed(), "Region " SIZE_FORMAT " should be movable", r->index());
 965 
 966       Copy::aligned_conjoint_words(r->bottom(), heap->get_region(new_start)->bottom(), words_size);
 967       ContinuationGCSupport::relativize_stack_chunk(cast_to_oop<HeapWord*>(r->bottom()));





 968 
 969       oop new_obj = cast_to_oop(heap->get_region(new_start)->bottom());
 970       new_obj->init_mark();
 971 
 972       {

 973         for (size_t c = old_start; c <= old_end; c++) {
 974           ShenandoahHeapRegion* r = heap->get_region(c);

 975           r->make_regular_bypass();
 976           r->set_top(r->bottom());
 977         }
 978 
 979         for (size_t c = new_start; c <= new_end; c++) {
 980           ShenandoahHeapRegion* r = heap->get_region(c);
 981           if (c == new_start) {
 982             r->make_humongous_start_bypass();
 983           } else {
 984             r->make_humongous_cont_bypass();
 985           }
 986 
 987           // Trailing region may be non-full, record the remainder there
 988           size_t remainder = words_size & ShenandoahHeapRegion::region_size_words_mask();
 989           if ((c == new_end) && (remainder != 0)) {
 990             r->set_top(r->bottom() + remainder);
 991           } else {
 992             r->set_top(r->end());
 993           }
 994 
 995           r->reset_alloc_metadata();
 996         }
 997       }
 998     }
 999   }
1000 }
1001 
1002 // This is slightly different to ShHeap::reset_next_mark_bitmap:
1003 // we need to remain able to walk pinned regions.
1004 // Since pinned region do not move and don't get compacted, we will get holes with

1030 };
1031 
1032 void ShenandoahFullGC::phase4_compact_objects(ShenandoahHeapRegionSet** worker_slices) {
1033   GCTraceTime(Info, gc, phases) time("Phase 4: Move objects", _gc_timer);
1034   ShenandoahGCPhase compaction_phase(ShenandoahPhaseTimings::full_gc_copy_objects);
1035 
1036   ShenandoahHeap* heap = ShenandoahHeap::heap();
1037 
1038   // Compact regular objects first
1039   {
1040     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_regular);
1041     ShenandoahCompactObjectsTask compact_task(worker_slices);
1042     heap->workers()->run_task(&compact_task);
1043   }
1044 
1045   // Compact humongous objects after regular object moves
1046   {
1047     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_humong);
1048     compact_humongous_objects();
1049   }





1050 
1051   // Reset complete bitmap. We're about to reset the complete-top-at-mark-start pointer
1052   // and must ensure the bitmap is in sync.
1053   {
1054     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_reset_complete);
1055     ShenandoahMCResetCompleteBitmapTask task;
1056     heap->workers()->run_task(&task);
1057   }
1058 
1059   // Bring regions in proper states after the collection, and set heap properties.
1060   {
1061     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_rebuild);
1062 
1063     ShenandoahPostCompactClosure post_compact;
1064     heap->heap_region_iterate(&post_compact);
1065     heap->set_used(post_compact.get_live());














1066 




1067     heap->collection_set()->clear();
1068     heap->free_set()->rebuild();
1069   }












1070 
1071   heap->clear_cancelled_gc();












































1072 }

   1 /*
   2  * Copyright (c) 2014, 2021, Red Hat, Inc. All rights reserved.
   3  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 
  28 #include "compiler/oopMap.hpp"
  29 #include "gc/shared/continuationGCSupport.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/shenandoahConcurrentGC.hpp"
  36 #include "gc/shenandoah/shenandoahCollectionSet.hpp"
  37 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
  38 #include "gc/shenandoah/shenandoahFreeSet.hpp"
  39 #include "gc/shenandoah/shenandoahFullGC.hpp"
  40 #include "gc/shenandoah/shenandoahGlobalGeneration.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/shenandoahOldGeneration.hpp"
  50 #include "gc/shenandoah/shenandoahOopClosures.inline.hpp"
  51 #include "gc/shenandoah/shenandoahReferenceProcessor.hpp"
  52 #include "gc/shenandoah/shenandoahRootProcessor.inline.hpp"
  53 #include "gc/shenandoah/shenandoahSTWMark.hpp"
  54 #include "gc/shenandoah/shenandoahUtils.hpp"
  55 #include "gc/shenandoah/shenandoahVerifier.hpp"
  56 #include "gc/shenandoah/shenandoahVMOperations.hpp"
  57 #include "gc/shenandoah/shenandoahWorkerPolicy.hpp"
  58 #include "gc/shenandoah/shenandoahYoungGeneration.hpp"
  59 #include "memory/metaspaceUtils.hpp"
  60 #include "memory/universe.hpp"
  61 #include "oops/compressedOops.inline.hpp"
  62 #include "oops/oop.inline.hpp"
  63 #include "runtime/javaThread.hpp"
  64 #include "runtime/orderAccess.hpp"
  65 #include "runtime/vmThread.hpp"
  66 #include "utilities/copy.hpp"
  67 #include "utilities/events.hpp"
  68 #include "utilities/growableArray.hpp"
  69 
  70 // After Full GC is done, reconstruct the remembered set by iterating over OLD regions,
  71 // registering all objects between bottom() and top(), and setting remembered set cards to
  72 // DIRTY if they hold interesting pointers.
  73 class ShenandoahReconstructRememberedSetTask : public WorkerTask {
  74 private:
  75   ShenandoahRegionIterator _regions;
  76 
  77 public:
  78   ShenandoahReconstructRememberedSetTask() :
  79     WorkerTask("Shenandoah Reset Bitmap") { }
  80 
  81   void work(uint worker_id) {
  82     ShenandoahParallelWorkerSession worker_session(worker_id);
  83     ShenandoahHeapRegion* r = _regions.next();
  84     ShenandoahHeap* heap = ShenandoahHeap::heap();
  85     RememberedScanner* scanner = heap->card_scan();
  86     ShenandoahSetRememberedCardsToDirtyClosure dirty_cards_for_interesting_pointers;
  87 
  88     while (r != nullptr) {
  89       if (r->is_old() && r->is_active()) {
  90         HeapWord* obj_addr = r->bottom();
  91         if (r->is_humongous_start()) {
  92           // First, clear the remembered set
  93           oop obj = cast_to_oop(obj_addr);
  94           size_t size = obj->size();
  95 
  96           // First, clear the remembered set for all spanned humongous regions
  97           size_t num_regions = ShenandoahHeapRegion::required_regions(size * HeapWordSize);
  98           size_t region_span = num_regions * ShenandoahHeapRegion::region_size_words();
  99           scanner->reset_remset(r->bottom(), region_span);
 100           size_t region_index = r->index();
 101           ShenandoahHeapRegion* humongous_region = heap->get_region(region_index);
 102           while (num_regions-- != 0) {
 103             scanner->reset_object_range(humongous_region->bottom(), humongous_region->end());
 104             region_index++;
 105             humongous_region = heap->get_region(region_index);
 106           }
 107 
 108           // Then register the humongous object and DIRTY relevant remembered set cards
 109           scanner->register_object_without_lock(obj_addr);
 110           obj->oop_iterate(&dirty_cards_for_interesting_pointers);
 111         } else if (!r->is_humongous()) {
 112           // First, clear the remembered set
 113           scanner->reset_remset(r->bottom(), ShenandoahHeapRegion::region_size_words());
 114           scanner->reset_object_range(r->bottom(), r->end());
 115 
 116           // Then iterate over all objects, registering object and DIRTYing relevant remembered set cards
 117           HeapWord* t = r->top();
 118           while (obj_addr < t) {
 119             oop obj = cast_to_oop(obj_addr);
 120             size_t size = obj->size();
 121             scanner->register_object_without_lock(obj_addr);
 122             obj_addr += obj->oop_iterate_size(&dirty_cards_for_interesting_pointers);
 123           }
 124         } // else, ignore humongous continuation region
 125       }
 126       // else, this region is FREE or YOUNG or inactive and we can ignore it.
 127       // TODO: Assert this.
 128       r = _regions.next();
 129     }
 130   }
 131 };
 132 
 133 ShenandoahFullGC::ShenandoahFullGC() :
 134   _gc_timer(ShenandoahHeap::heap()->gc_timer()),
 135   _preserved_marks(new PreservedMarksSet(true)) {}
 136 
 137 ShenandoahFullGC::~ShenandoahFullGC() {
 138   delete _preserved_marks;
 139 }
 140 
 141 bool ShenandoahFullGC::collect(GCCause::Cause cause) {
 142   vmop_entry_full(cause);
 143   // Always success
 144   return true;
 145 }
 146 
 147 void ShenandoahFullGC::vmop_entry_full(GCCause::Cause cause) {
 148   ShenandoahHeap* const heap = ShenandoahHeap::heap();
 149   TraceCollectorStats tcs(heap->monitoring_support()->full_stw_collection_counters());
 150   ShenandoahTimingsTracker timing(ShenandoahPhaseTimings::full_gc_gross);
 151 
 152   heap->try_inject_alloc_failure();
 153   VM_ShenandoahFullGC op(cause, this);
 154   VMThread::execute(&op);
 155 }
 156 
 157 void ShenandoahFullGC::entry_full(GCCause::Cause cause) {
 158   static const char* msg = "Pause Full";
 159   ShenandoahPausePhase gc_phase(msg, ShenandoahPhaseTimings::full_gc, true /* log_heap_usage */);
 160   EventMark em("%s", msg);
 161 
 162   ShenandoahWorkerScope scope(ShenandoahHeap::heap()->workers(),
 163                               ShenandoahWorkerPolicy::calc_workers_for_fullgc(),
 164                               "full gc");
 165 
 166   op_full(cause);
 167 }
 168 
 169 void ShenandoahFullGC::op_full(GCCause::Cause cause) {
 170   ShenandoahHeap* const heap = ShenandoahHeap::heap();
 171   ShenandoahMetricsSnapshot metrics;
 172   metrics.snap_before();
 173 
 174   // Perform full GC
 175   do_it(cause);
 176 
 177   metrics.snap_after();
 178   if (heap->mode()->is_generational()) {
 179     // Full GC should reset time since last gc for young and old heuristics
 180     heap->young_generation()->heuristics()->record_cycle_end();
 181     heap->old_generation()->heuristics()->record_cycle_end();
 182 
 183     heap->mmu_tracker()->record_full(GCId::current());
 184     heap->log_heap_status("At end of Full GC");
 185 
 186     assert(heap->old_generation()->state() == ShenandoahOldGeneration::WAITING_FOR_BOOTSTRAP,
 187            "After full GC, old generation should be waiting for bootstrap.");
 188 
 189     // Since we allow temporary violation of these constraints during Full GC, we want to enforce that the assertions are
 190     // made valid by the time Full GC completes.
 191     assert(heap->old_generation()->used_regions_size() <= heap->old_generation()->max_capacity(),
 192            "Old generation affiliated regions must be less than capacity");
 193     assert(heap->young_generation()->used_regions_size() <= heap->young_generation()->max_capacity(),
 194            "Young generation affiliated regions must be less than capacity");
 195 
 196     assert((heap->young_generation()->used() + heap->young_generation()->get_humongous_waste())
 197            <= heap->young_generation()->used_regions_size(), "Young consumed can be no larger than span of affiliated regions");
 198     assert((heap->old_generation()->used() + heap->old_generation()->get_humongous_waste())
 199            <= heap->old_generation()->used_regions_size(), "Old consumed can be no larger than span of affiliated regions");
 200 
 201     // Establish baseline for next old-has-grown trigger.
 202     heap->old_generation()->set_live_bytes_after_last_mark(heap->old_generation()->used() +
 203                                                            heap->old_generation()->get_humongous_waste());
 204   }
 205   if (metrics.is_good_progress()) {
 206     ShenandoahHeap::heap()->notify_gc_progress();
 207   } else {
 208     // Nothing to do. Tell the allocation path that we have failed to make
 209     // progress, and it can finally fail.
 210     ShenandoahHeap::heap()->notify_gc_no_progress();
 211   }
 212 
 213   // Regardless if progress was made, we record that we completed a "successful" full GC.
 214   heap->global_generation()->heuristics()->record_success_full();
 215   heap->shenandoah_policy()->record_success_full();
 216 }
 217 
 218 void ShenandoahFullGC::do_it(GCCause::Cause gc_cause) {
 219   ShenandoahHeap* heap = ShenandoahHeap::heap();
 220   // Since we may arrive here from degenerated GC failure of either young or old, establish generation as GLOBAL.
 221   heap->set_gc_generation(heap->global_generation());
 222 
 223   if (heap->mode()->is_generational()) {
 224     // No need for old_gen->increase_used() as this was done when plabs were allocated.
 225     heap->set_young_evac_reserve(0);
 226     heap->set_old_evac_reserve(0);
 227     heap->set_promoted_reserve(0);
 228 
 229     // Full GC supersedes any marking or coalescing in old generation.
 230     heap->cancel_old_gc();
 231   }
 232 
 233   if (ShenandoahVerify) {
 234     heap->verifier()->verify_before_fullgc();
 235   }
 236 
 237   if (VerifyBeforeGC) {
 238     Universe::verify();
 239   }
 240 
 241   // Degenerated GC may carry concurrent root flags when upgrading to
 242   // full GC. We need to reset it before mutators resume.
 243   heap->set_concurrent_strong_root_in_progress(false);
 244   heap->set_concurrent_weak_root_in_progress(false);
 245 
 246   heap->set_full_gc_in_progress(true);
 247 
 248   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "must be at a safepoint");
 249   assert(Thread::current()->is_VM_thread(), "Do full GC only while world is stopped");
 250 
 251   {

 255 
 256   {
 257     ShenandoahGCPhase prepare_phase(ShenandoahPhaseTimings::full_gc_prepare);
 258     // Full GC is supposed to recover from any GC state:
 259 
 260     // a0. Remember if we have forwarded objects
 261     bool has_forwarded_objects = heap->has_forwarded_objects();
 262 
 263     // a1. Cancel evacuation, if in progress
 264     if (heap->is_evacuation_in_progress()) {
 265       heap->set_evacuation_in_progress(false);
 266     }
 267     assert(!heap->is_evacuation_in_progress(), "sanity");
 268 
 269     // a2. Cancel update-refs, if in progress
 270     if (heap->is_update_refs_in_progress()) {
 271       heap->set_update_refs_in_progress(false);
 272     }
 273     assert(!heap->is_update_refs_in_progress(), "sanity");
 274 
 275     // b. Cancel all concurrent marks, if in progress
 276     if (heap->is_concurrent_mark_in_progress()) {
 277       heap->cancel_concurrent_mark();

 278     }
 279     assert(!heap->is_concurrent_mark_in_progress(), "sanity");
 280 
 281     // c. Update roots if this full GC is due to evac-oom, which may carry from-space pointers in roots.
 282     if (has_forwarded_objects) {
 283       update_roots(true /*full_gc*/);
 284     }
 285 
 286     // d. Reset the bitmaps for new marking
 287     heap->global_generation()->reset_mark_bitmap();
 288     assert(heap->marking_context()->is_bitmap_clear(), "sanity");
 289     assert(!heap->global_generation()->is_mark_complete(), "sanity");
 290 
 291     // e. Abandon reference discovery and clear all discovered references.
 292     ShenandoahReferenceProcessor* rp = heap->global_generation()->ref_processor();
 293     rp->abandon_partial_discovery();
 294 
 295     // f. Sync pinned region status from the CP marks
 296     heap->sync_pinned_region_status();
 297 
 298     if (heap->mode()->is_generational()) {
 299       for (size_t i = 0; i < heap->num_regions(); i++) {
 300         ShenandoahHeapRegion* r = heap->get_region(i);
 301         if (r->get_top_before_promote() != nullptr) {
 302           r->restore_top_before_promote();
 303         }
 304       }
 305     }
 306 
 307     // The rest of prologue:
 308     _preserved_marks->init(heap->workers()->active_workers());
 309 
 310     assert(heap->has_forwarded_objects() == has_forwarded_objects, "This should not change");
 311   }
 312 
 313   if (UseTLAB) {
 314     // TODO: Do we need to explicitly retire PLABs?
 315     heap->gclabs_retire(ResizeTLAB);
 316     heap->tlabs_retire(ResizeTLAB);
 317   }
 318 
 319   OrderAccess::fence();
 320 
 321   phase1_mark_heap();
 322 
 323   // Once marking is done, which may have fixed up forwarded objects, we can drop it.
 324   // Coming out of Full GC, we would not have any forwarded objects.
 325   // This also prevents resolves with fwdptr from kicking in while adjusting pointers in phase3.
 326   heap->set_has_forwarded_objects(false);
 327 
 328   heap->set_full_gc_move_in_progress(true);
 329 
 330   // Setup workers for the rest
 331   OrderAccess::fence();
 332 
 333   // Initialize worker slices
 334   ShenandoahHeapRegionSet** worker_slices = NEW_C_HEAP_ARRAY(ShenandoahHeapRegionSet*, heap->max_workers(), mtGC);
 335   for (uint i = 0; i < heap->max_workers(); i++) {
 336     worker_slices[i] = new ShenandoahHeapRegionSet();
 337   }
 338 
 339   {
 340     // The rest of code performs region moves, where region status is undefined
 341     // until all phases run together.
 342     ShenandoahHeapLocker lock(heap->lock());
 343 
 344     phase2_calculate_target_addresses(worker_slices);
 345 
 346     OrderAccess::fence();
 347 
 348     phase3_update_references();
 349 
 350     phase4_compact_objects(worker_slices);
 351 
 352     phase5_epilog();
 353   }
 354 
 355   {
 356     // Epilogue
 357     // TODO: Merge with phase5_epilog?
 358     _preserved_marks->restore(heap->workers());
 359     _preserved_marks->reclaim();
 360 
 361     if (heap->mode()->is_generational()) {
 362       ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_reconstruct_remembered_set);
 363       ShenandoahReconstructRememberedSetTask task;
 364       heap->workers()->run_task(&task);
 365     }
 366   }
 367 
 368   // Resize metaspace
 369   MetaspaceGC::compute_new_size();
 370 
 371   // Free worker slices
 372   for (uint i = 0; i < heap->max_workers(); i++) {
 373     delete worker_slices[i];
 374   }
 375   FREE_C_HEAP_ARRAY(ShenandoahHeapRegionSet*, worker_slices);
 376 
 377   heap->set_full_gc_move_in_progress(false);
 378   heap->set_full_gc_in_progress(false);
 379 
 380   if (ShenandoahVerify) {
 381     heap->verifier()->verify_after_fullgc();
 382   }
 383 
 384   // Humongous regions are promoted on demand and are accounted for by normal Full GC mechanisms.
 385   if (VerifyAfterGC) {
 386     Universe::verify();
 387   }
 388 
 389   {
 390     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_heapdump_post);
 391     heap->post_full_gc_dump(_gc_timer);
 392   }
 393 }
 394 
 395 class ShenandoahPrepareForMarkClosure: public ShenandoahHeapRegionClosure {
 396 private:
 397   ShenandoahMarkingContext* const _ctx;
 398 
 399 public:
 400   ShenandoahPrepareForMarkClosure() : _ctx(ShenandoahHeap::heap()->marking_context()) {}
 401 
 402   void heap_region_do(ShenandoahHeapRegion *r) {
 403     if (r->affiliation() != FREE) {
 404       _ctx->capture_top_at_mark_start(r);
 405       r->clear_live_data();
 406     }
 407   }
 408 
 409   bool is_thread_safe() { return true; }
 410 };
 411 
 412 void ShenandoahFullGC::phase1_mark_heap() {
 413   GCTraceTime(Info, gc, phases) time("Phase 1: Mark live objects", _gc_timer);
 414   ShenandoahGCPhase mark_phase(ShenandoahPhaseTimings::full_gc_mark);
 415 
 416   ShenandoahHeap* heap = ShenandoahHeap::heap();
 417 
 418   ShenandoahPrepareForMarkClosure cl;
 419   heap->parallel_heap_region_iterate(&cl);
 420 
 421   heap->set_unload_classes(heap->global_generation()->heuristics()->can_unload_classes());
 422 
 423   ShenandoahReferenceProcessor* rp = heap->global_generation()->ref_processor();
 424   // enable ("weak") refs discovery
 425   rp->set_soft_reference_policy(true); // forcefully purge all soft references
 426 
 427   ShenandoahSTWMark mark(heap->global_generation(), true /*full_gc*/);
 428   mark.mark();
 429   heap->parallel_cleaning(true /* full_gc */);
 430 
 431   size_t live_bytes_in_old = 0;
 432   for (size_t i = 0; i < heap->num_regions(); i++) {
 433     ShenandoahHeapRegion* r = heap->get_region(i);
 434     if (r->is_old()) {
 435       live_bytes_in_old += r->get_live_data_bytes();
 436     }
 437   }
 438   log_info(gc)("Live bytes in old after STW mark: " PROPERFMT, PROPERFMTARGS(live_bytes_in_old));
 439 }
 440 
 441 class ShenandoahPrepareForCompactionTask : public WorkerTask {
 442 private:
 443   PreservedMarksSet*        const _preserved_marks;
 444   ShenandoahHeap*           const _heap;
 445   ShenandoahHeapRegionSet** const _worker_slices;
 446   size_t                    const _num_workers;
 447 
 448 public:
 449   ShenandoahPrepareForCompactionTask(PreservedMarksSet *preserved_marks,
 450                                      ShenandoahHeapRegionSet **worker_slices,
 451                                      size_t num_workers);
 452 
 453   static bool is_candidate_region(ShenandoahHeapRegion* r) {
 454     // Empty region: get it into the slice to defragment the slice itself.
 455     // We could have skipped this without violating correctness, but we really
 456     // want to compact all live regions to the start of the heap, which sometimes
 457     // means moving them into the fully empty regions.
 458     if (r->is_empty()) return true;
 459 
 460     // Can move the region, and this is not the humongous region. Humongous
 461     // moves are special cased here, because their moves are handled separately.
 462     return r->is_stw_move_allowed() && !r->is_humongous();
 463   }
 464 
 465   void work(uint worker_id);
 466 };
 467 
 468 class ShenandoahPrepareForGenerationalCompactionObjectClosure : public ObjectClosure {
 469 private:
 470   PreservedMarks*          const _preserved_marks;
 471   ShenandoahHeap*          const _heap;
 472   uint                           _tenuring_threshold;
 473 
 474   // _empty_regions is a thread-local list of heap regions that have been completely emptied by this worker thread's
 475   // compaction efforts.  The worker thread that drives these efforts adds compacted regions to this list if the
 476   // region has not been compacted onto itself.
 477   GrowableArray<ShenandoahHeapRegion*>& _empty_regions;
 478   int _empty_regions_pos;
 479   ShenandoahHeapRegion*          _old_to_region;
 480   ShenandoahHeapRegion*          _young_to_region;
 481   ShenandoahHeapRegion*          _from_region;
 482   ShenandoahAffiliation          _from_affiliation;
 483   HeapWord*                      _old_compact_point;
 484   HeapWord*                      _young_compact_point;
 485   uint                           _worker_id;
 486 
 487 public:
 488   ShenandoahPrepareForGenerationalCompactionObjectClosure(PreservedMarks* preserved_marks,
 489                                                           GrowableArray<ShenandoahHeapRegion*>& empty_regions,
 490                                                           ShenandoahHeapRegion* old_to_region,
 491                                                           ShenandoahHeapRegion* young_to_region, uint worker_id) :
 492       _preserved_marks(preserved_marks),
 493       _heap(ShenandoahHeap::heap()),
 494       _tenuring_threshold(0),
 495       _empty_regions(empty_regions),
 496       _empty_regions_pos(0),
 497       _old_to_region(old_to_region),
 498       _young_to_region(young_to_region),
 499       _from_region(nullptr),
 500       _old_compact_point((old_to_region != nullptr)? old_to_region->bottom(): nullptr),
 501       _young_compact_point((young_to_region != nullptr)? young_to_region->bottom(): nullptr),
 502       _worker_id(worker_id) {
 503     if (_heap->mode()->is_generational()) {
 504       _tenuring_threshold = _heap->age_census()->tenuring_threshold();
 505     }
 506   }
 507 
 508   void set_from_region(ShenandoahHeapRegion* from_region) {
 509     _from_region = from_region;
 510     _from_affiliation = from_region->affiliation();
 511     if (_from_region->has_live()) {
 512       if (_from_affiliation == ShenandoahAffiliation::OLD_GENERATION) {
 513         if (_old_to_region == nullptr) {
 514           _old_to_region = from_region;
 515           _old_compact_point = from_region->bottom();
 516         }
 517       } else {
 518         assert(_from_affiliation == ShenandoahAffiliation::YOUNG_GENERATION, "from_region must be OLD or YOUNG");
 519         if (_young_to_region == nullptr) {
 520           _young_to_region = from_region;
 521           _young_compact_point = from_region->bottom();
 522         }
 523       }
 524     } // else, we won't iterate over this _from_region so we don't need to set up to region to hold copies
 525   }
 526 
 527   void finish() {
 528     finish_old_region();
 529     finish_young_region();
 530   }
 531 
 532   void finish_old_region() {
 533     if (_old_to_region != nullptr) {
 534       log_debug(gc)("Planned compaction into Old Region " SIZE_FORMAT ", used: " SIZE_FORMAT " tabulated by worker %u",
 535                     _old_to_region->index(), _old_compact_point - _old_to_region->bottom(), _worker_id);
 536       _old_to_region->set_new_top(_old_compact_point);
 537       _old_to_region = nullptr;
 538     }
 539   }
 540 
 541   void finish_young_region() {
 542     if (_young_to_region != nullptr) {
 543       log_debug(gc)("Worker %u planned compaction into Young Region " SIZE_FORMAT ", used: " SIZE_FORMAT,
 544                     _worker_id, _young_to_region->index(), _young_compact_point - _young_to_region->bottom());
 545       _young_to_region->set_new_top(_young_compact_point);
 546       _young_to_region = nullptr;
 547     }
 548   }
 549 
 550   bool is_compact_same_region() {
 551     return (_from_region == _old_to_region) || (_from_region == _young_to_region);
 552   }
 553 
 554   int empty_regions_pos() {
 555     return _empty_regions_pos;
 556   }
 557 
 558   void do_object(oop p) {
 559     assert(_from_region != nullptr, "must set before work");
 560     assert((_from_region->bottom() <= cast_from_oop<HeapWord*>(p)) && (cast_from_oop<HeapWord*>(p) < _from_region->top()),
 561            "Object must reside in _from_region");
 562     assert(_heap->complete_marking_context()->is_marked(p), "must be marked");
 563     assert(!_heap->complete_marking_context()->allocated_after_mark_start(p), "must be truly marked");
 564 
 565     size_t obj_size = p->size();
 566     uint from_region_age = _from_region->age();
 567     uint object_age = p->age();
 568 
 569     bool promote_object = false;
 570     if ((_from_affiliation == ShenandoahAffiliation::YOUNG_GENERATION) &&
 571         (from_region_age + object_age >= _tenuring_threshold)) {
 572       if ((_old_to_region != nullptr) && (_old_compact_point + obj_size > _old_to_region->end())) {
 573         finish_old_region();
 574         _old_to_region = nullptr;
 575       }
 576       if (_old_to_region == nullptr) {
 577         if (_empty_regions_pos < _empty_regions.length()) {
 578           ShenandoahHeapRegion* new_to_region = _empty_regions.at(_empty_regions_pos);
 579           _empty_regions_pos++;
 580           new_to_region->set_affiliation(OLD_GENERATION);
 581           _old_to_region = new_to_region;
 582           _old_compact_point = _old_to_region->bottom();
 583           promote_object = true;
 584         }
 585         // Else this worker thread does not yet have any empty regions into which this aged object can be promoted so
 586         // we leave promote_object as false, deferring the promotion.
 587       } else {
 588         promote_object = true;
 589       }
 590     }
 591 
 592     if (promote_object || (_from_affiliation == ShenandoahAffiliation::OLD_GENERATION)) {
 593       assert(_old_to_region != nullptr, "_old_to_region should not be nullptr when evacuating to OLD region");
 594       if (_old_compact_point + obj_size > _old_to_region->end()) {
 595         ShenandoahHeapRegion* new_to_region;
 596 
 597         log_debug(gc)("Worker %u finishing old region " SIZE_FORMAT ", compact_point: " PTR_FORMAT ", obj_size: " SIZE_FORMAT
 598                       ", &compact_point[obj_size]: " PTR_FORMAT ", region end: " PTR_FORMAT,  _worker_id, _old_to_region->index(),
 599                       p2i(_old_compact_point), obj_size, p2i(_old_compact_point + obj_size), p2i(_old_to_region->end()));
 600 
 601         // Object does not fit.  Get a new _old_to_region.
 602         finish_old_region();
 603         if (_empty_regions_pos < _empty_regions.length()) {
 604           new_to_region = _empty_regions.at(_empty_regions_pos);
 605           _empty_regions_pos++;
 606           new_to_region->set_affiliation(OLD_GENERATION);
 607         } else {
 608           // If we've exhausted the previously selected _old_to_region, we know that the _old_to_region is distinct
 609           // from _from_region.  That's because there is always room for _from_region to be compacted into itself.
 610           // Since we're out of empty regions, let's use _from_region to hold the results of its own compaction.
 611           new_to_region = _from_region;
 612         }
 613 
 614         assert(new_to_region != _old_to_region, "must not reuse same OLD to-region");
 615         assert(new_to_region != nullptr, "must not be nullptr");
 616         _old_to_region = new_to_region;
 617         _old_compact_point = _old_to_region->bottom();
 618       }
 619 
 620       // Object fits into current region, record new location:
 621       assert(_old_compact_point + obj_size <= _old_to_region->end(), "must fit");
 622       shenandoah_assert_not_forwarded(nullptr, p);
 623       _preserved_marks->push_if_necessary(p, p->mark());
 624       p->forward_to(cast_to_oop(_old_compact_point));
 625       _old_compact_point += obj_size;
 626     } else {
 627       assert(_from_affiliation == ShenandoahAffiliation::YOUNG_GENERATION,
 628              "_from_region must be OLD_GENERATION or YOUNG_GENERATION");
 629       assert(_young_to_region != nullptr, "_young_to_region should not be nullptr when compacting YOUNG _from_region");
 630 
 631       // After full gc compaction, all regions have age 0.  Embed the region's age into the object's age in order to preserve
 632       // tenuring progress.
 633       if (_heap->is_aging_cycle()) {
 634         _heap->increase_object_age(p, from_region_age + 1);
 635       } else {
 636         _heap->increase_object_age(p, from_region_age);
 637       }
 638 
 639       if (_young_compact_point + obj_size > _young_to_region->end()) {
 640         ShenandoahHeapRegion* new_to_region;
 641 
 642         log_debug(gc)("Worker %u finishing young region " SIZE_FORMAT ", compact_point: " PTR_FORMAT ", obj_size: " SIZE_FORMAT
 643                       ", &compact_point[obj_size]: " PTR_FORMAT ", region end: " PTR_FORMAT,  _worker_id, _young_to_region->index(),
 644                       p2i(_young_compact_point), obj_size, p2i(_young_compact_point + obj_size), p2i(_young_to_region->end()));
 645 
 646         // Object does not fit.  Get a new _young_to_region.
 647         finish_young_region();
 648         if (_empty_regions_pos < _empty_regions.length()) {
 649           new_to_region = _empty_regions.at(_empty_regions_pos);
 650           _empty_regions_pos++;
 651           new_to_region->set_affiliation(YOUNG_GENERATION);
 652         } else {
 653           // If we've exhausted the previously selected _young_to_region, we know that the _young_to_region is distinct
 654           // from _from_region.  That's because there is always room for _from_region to be compacted into itself.
 655           // Since we're out of empty regions, let's use _from_region to hold the results of its own compaction.
 656           new_to_region = _from_region;
 657         }
 658 
 659         assert(new_to_region != _young_to_region, "must not reuse same OLD to-region");
 660         assert(new_to_region != nullptr, "must not be nullptr");
 661         _young_to_region = new_to_region;
 662         _young_compact_point = _young_to_region->bottom();
 663       }
 664 
 665       // Object fits into current region, record new location:
 666       assert(_young_compact_point + obj_size <= _young_to_region->end(), "must fit");
 667       shenandoah_assert_not_forwarded(nullptr, p);
 668       _preserved_marks->push_if_necessary(p, p->mark());
 669       p->forward_to(cast_to_oop(_young_compact_point));
 670       _young_compact_point += obj_size;
 671     }
 672   }
 673 };
 674 
 675 
 676 class ShenandoahPrepareForCompactionObjectClosure : public ObjectClosure {
 677 private:
 678   PreservedMarks*          const _preserved_marks;
 679   ShenandoahHeap*          const _heap;
 680   GrowableArray<ShenandoahHeapRegion*>& _empty_regions;
 681   int _empty_regions_pos;
 682   ShenandoahHeapRegion*          _to_region;
 683   ShenandoahHeapRegion*          _from_region;
 684   HeapWord* _compact_point;
 685 
 686 public:
 687   ShenandoahPrepareForCompactionObjectClosure(PreservedMarks* preserved_marks,
 688                                               GrowableArray<ShenandoahHeapRegion*>& empty_regions,
 689                                               ShenandoahHeapRegion* to_region) :
 690     _preserved_marks(preserved_marks),
 691     _heap(ShenandoahHeap::heap()),
 692     _empty_regions(empty_regions),
 693     _empty_regions_pos(0),
 694     _to_region(to_region),
 695     _from_region(nullptr),
 696     _compact_point(to_region->bottom()) {}
 697 
 698   void set_from_region(ShenandoahHeapRegion* from_region) {
 699     _from_region = from_region;
 700   }
 701 
 702   void finish_region() {
 703     assert(_to_region != nullptr, "should not happen");
 704     assert(!_heap->mode()->is_generational(), "Generational GC should use different Closure");
 705     _to_region->set_new_top(_compact_point);
 706   }
 707 
 708   bool is_compact_same_region() {
 709     return _from_region == _to_region;
 710   }
 711 
 712   int empty_regions_pos() {
 713     return _empty_regions_pos;
 714   }
 715 
 716   void do_object(oop p) {
 717     assert(_from_region != nullptr, "must set before work");
 718     assert(_heap->complete_marking_context()->is_marked(p), "must be marked");
 719     assert(!_heap->complete_marking_context()->allocated_after_mark_start(p), "must be truly marked");
 720 
 721     size_t obj_size = p->size();
 722     if (_compact_point + obj_size > _to_region->end()) {
 723       finish_region();
 724 

 730       } else {
 731         // Out of empty region? Compact within the same region.
 732         new_to_region = _from_region;
 733       }
 734 
 735       assert(new_to_region != _to_region, "must not reuse same to-region");
 736       assert(new_to_region != nullptr, "must not be null");
 737       _to_region = new_to_region;
 738       _compact_point = _to_region->bottom();
 739     }
 740 
 741     // Object fits into current region, record new location:
 742     assert(_compact_point + obj_size <= _to_region->end(), "must fit");
 743     shenandoah_assert_not_forwarded(nullptr, p);
 744     _preserved_marks->push_if_necessary(p, p->mark());
 745     p->forward_to(cast_to_oop(_compact_point));
 746     _compact_point += obj_size;
 747   }
 748 };
 749 





 750 
 751 ShenandoahPrepareForCompactionTask::ShenandoahPrepareForCompactionTask(PreservedMarksSet *preserved_marks,
 752                                                                        ShenandoahHeapRegionSet **worker_slices,
 753                                                                        size_t num_workers) :
 754     WorkerTask("Shenandoah Prepare For Compaction"),
 755     _preserved_marks(preserved_marks), _heap(ShenandoahHeap::heap()),
 756     _worker_slices(worker_slices), _num_workers(num_workers) { }
 757 
 758 
 759 void ShenandoahPrepareForCompactionTask::work(uint worker_id) {
 760   ShenandoahParallelWorkerSession worker_session(worker_id);
 761   ShenandoahHeapRegionSet* slice = _worker_slices[worker_id];
 762   ShenandoahHeapRegionSetIterator it(slice);
 763   ShenandoahHeapRegion* from_region = it.next();
 764   // No work?
 765   if (from_region == nullptr) {
 766     return;
 767   }
 768 
 769   // Sliding compaction. Walk all regions in the slice, and compact them.
 770   // Remember empty regions and reuse them as needed.
 771   ResourceMark rm;



 772 
 773   GrowableArray<ShenandoahHeapRegion*> empty_regions((int)_heap->num_regions());



 774 
 775   if (_heap->mode()->is_generational()) {
 776     ShenandoahHeapRegion* old_to_region = (from_region->is_old())? from_region: nullptr;
 777     ShenandoahHeapRegion* young_to_region = (from_region->is_young())? from_region: nullptr;
 778     ShenandoahPrepareForGenerationalCompactionObjectClosure cl(_preserved_marks->get(worker_id),
 779                                                                empty_regions,
 780                                                                old_to_region, young_to_region,
 781                                                                worker_id);
 782     while (from_region != nullptr) {
 783       assert(is_candidate_region(from_region), "Sanity");
 784       log_debug(gc)("Worker %u compacting %s Region " SIZE_FORMAT " which had used " SIZE_FORMAT " and %s live",
 785                     worker_id, from_region->affiliation_name(),
 786                     from_region->index(), from_region->used(), from_region->has_live()? "has": "does not have");
 787       cl.set_from_region(from_region);
 788       if (from_region->has_live()) {
 789         _heap->marked_object_iterate(from_region, &cl);
 790       }
 791       // Compacted the region to somewhere else? From-region is empty then.
 792       if (!cl.is_compact_same_region()) {
 793         empty_regions.append(from_region);
 794       }
 795       from_region = it.next();
 796     }
 797     cl.finish();
 798 
 799     // Mark all remaining regions as empty
 800     for (int pos = cl.empty_regions_pos(); pos < empty_regions.length(); ++pos) {
 801       ShenandoahHeapRegion* r = empty_regions.at(pos);
 802       r->set_new_top(r->bottom());
 803     }
 804   } else {
 805     ShenandoahPrepareForCompactionObjectClosure cl(_preserved_marks->get(worker_id), empty_regions, from_region);

 806     while (from_region != nullptr) {
 807       assert(is_candidate_region(from_region), "Sanity");

 808       cl.set_from_region(from_region);
 809       if (from_region->has_live()) {
 810         _heap->marked_object_iterate(from_region, &cl);
 811       }
 812 
 813       // Compacted the region to somewhere else? From-region is empty then.
 814       if (!cl.is_compact_same_region()) {
 815         empty_regions.append(from_region);
 816       }
 817       from_region = it.next();
 818     }
 819     cl.finish_region();
 820 
 821     // Mark all remaining regions as empty
 822     for (int pos = cl.empty_regions_pos(); pos < empty_regions.length(); ++pos) {
 823       ShenandoahHeapRegion* r = empty_regions.at(pos);
 824       r->set_new_top(r->bottom());
 825     }
 826   }
 827 }
 828 
 829 void ShenandoahFullGC::calculate_target_humongous_objects() {
 830   ShenandoahHeap* heap = ShenandoahHeap::heap();
 831 
 832   // Compute the new addresses for humongous objects. We need to do this after addresses
 833   // for regular objects are calculated, and we know what regions in heap suffix are
 834   // available for humongous moves.
 835   //
 836   // Scan the heap backwards, because we are compacting humongous regions towards the end.
 837   // Maintain the contiguous compaction window in [to_begin; to_end), so that we can slide
 838   // humongous start there.
 839   //
 840   // The complication is potential non-movable regions during the scan. If such region is
 841   // detected, then sliding restarts towards that non-movable region.
 842 
 843   size_t to_begin = heap->num_regions();
 844   size_t to_end = heap->num_regions();
 845 
 846   log_debug(gc)("Full GC calculating target humongous objects from end " SIZE_FORMAT, to_end);
 847   for (size_t c = heap->num_regions(); c > 0; c--) {
 848     ShenandoahHeapRegion *r = heap->get_region(c - 1);
 849     if (r->is_humongous_continuation() || (r->new_top() == r->bottom())) {
 850       // To-region candidate: record this, and continue scan
 851       to_begin = r->index();
 852       continue;
 853     }
 854 
 855     if (r->is_humongous_start() && r->is_stw_move_allowed()) {
 856       // From-region candidate: movable humongous region
 857       oop old_obj = cast_to_oop(r->bottom());
 858       size_t words_size = old_obj->size();
 859       size_t num_regions = ShenandoahHeapRegion::required_regions(words_size * HeapWordSize);
 860 
 861       size_t start = to_end - num_regions;
 862 
 863       if (start >= to_begin && start != r->index()) {
 864         // Fits into current window, and the move is non-trivial. Record the move then, and continue scan.
 865         _preserved_marks->get(0)->push_if_necessary(old_obj, old_obj->mark());
 866         old_obj->forward_to(cast_to_oop(heap->get_region(start)->bottom()));

 869       }
 870     }
 871 
 872     // Failed to fit. Scan starting from current region.
 873     to_begin = r->index();
 874     to_end = r->index();
 875   }
 876 }
 877 
 878 class ShenandoahEnsureHeapActiveClosure: public ShenandoahHeapRegionClosure {
 879 private:
 880   ShenandoahHeap* const _heap;
 881 
 882 public:
 883   ShenandoahEnsureHeapActiveClosure() : _heap(ShenandoahHeap::heap()) {}
 884   void heap_region_do(ShenandoahHeapRegion* r) {
 885     if (r->is_trash()) {
 886       r->recycle();
 887     }
 888     if (r->is_cset()) {
 889       // Leave affiliation unchanged
 890       r->make_regular_bypass();
 891     }
 892     if (r->is_empty_uncommitted()) {
 893       r->make_committed_bypass();
 894     }
 895     assert (r->is_committed(), "only committed regions in heap now, see region " SIZE_FORMAT, r->index());
 896 
 897     // Record current region occupancy: this communicates empty regions are free
 898     // to the rest of Full GC code.
 899     r->set_new_top(r->top());
 900   }
 901 };
 902 
 903 class ShenandoahTrashImmediateGarbageClosure: public ShenandoahHeapRegionClosure {
 904 private:
 905   ShenandoahHeap* const _heap;
 906   ShenandoahMarkingContext* const _ctx;
 907 
 908 public:
 909   ShenandoahTrashImmediateGarbageClosure() :
 910     _heap(ShenandoahHeap::heap()),
 911     _ctx(ShenandoahHeap::heap()->complete_marking_context()) {}
 912 
 913   void heap_region_do(ShenandoahHeapRegion* r) {
 914     if (!r->is_affiliated()) {
 915       // Ignore free regions
 916       // TODO: change iterators so they do not process FREE regions.
 917       return;
 918     }
 919 
 920     if (r->is_humongous_start()) {
 921       oop humongous_obj = cast_to_oop(r->bottom());
 922       if (!_ctx->is_marked(humongous_obj)) {
 923         assert(!r->has_live(),
 924                "Humongous Start %s Region " SIZE_FORMAT " is not marked, should not have live",
 925                r->affiliation_name(),  r->index());
 926         log_debug(gc)("Trashing immediate humongous region " SIZE_FORMAT " because not marked", r->index());
 927         _heap->trash_humongous_region_at(r);
 928       } else {
 929         assert(r->has_live(),
 930                "Humongous Start %s Region " SIZE_FORMAT " should have live", r->affiliation_name(),  r->index());
 931       }
 932     } else if (r->is_humongous_continuation()) {
 933       // If we hit continuation, the non-live humongous starts should have been trashed already
 934       assert(r->humongous_start_region()->has_live(),
 935              "Humongous Continuation %s Region " SIZE_FORMAT " should have live", r->affiliation_name(),  r->index());
 936     } else if (r->is_regular()) {
 937       if (!r->has_live()) {
 938         log_debug(gc)("Trashing immediate regular region " SIZE_FORMAT " because has no live", r->index());
 939         r->make_trash_immediate();
 940       }
 941     }
 942   }
 943 };
 944 
 945 void ShenandoahFullGC::distribute_slices(ShenandoahHeapRegionSet** worker_slices) {
 946   ShenandoahHeap* heap = ShenandoahHeap::heap();
 947 
 948   uint n_workers = heap->workers()->active_workers();
 949   size_t n_regions = heap->num_regions();
 950 
 951   // What we want to accomplish: have the dense prefix of data, while still balancing
 952   // out the parallel work.
 953   //
 954   // Assuming the amount of work is driven by the live data that needs moving, we can slice
 955   // the entire heap into equal-live-sized prefix slices, and compact into them. So, each
 956   // thread takes all regions in its prefix subset, and then it takes some regions from
 957   // the tail.
 958   //

1067   for (size_t wid = 0; wid < n_workers; wid++) {
1068     ShenandoahHeapRegionSetIterator it(worker_slices[wid]);
1069     ShenandoahHeapRegion* r = it.next();
1070     while (r != nullptr) {
1071       size_t idx = r->index();
1072       assert(ShenandoahPrepareForCompactionTask::is_candidate_region(r), "Sanity: " SIZE_FORMAT, idx);
1073       assert(!map.at(idx), "No region distributed twice: " SIZE_FORMAT, idx);
1074       map.at_put(idx, true);
1075       r = it.next();
1076     }
1077   }
1078 
1079   for (size_t rid = 0; rid < n_regions; rid++) {
1080     bool is_candidate = ShenandoahPrepareForCompactionTask::is_candidate_region(heap->get_region(rid));
1081     bool is_distributed = map.at(rid);
1082     assert(is_distributed || !is_candidate, "All candidates are distributed: " SIZE_FORMAT, rid);
1083   }
1084 #endif
1085 }
1086 
1087 // TODO:
1088 //  Consider compacting old-gen objects toward the high end of memory and young-gen objects towards the low-end
1089 //  of memory.  As currently implemented, all regions are compacted toward the low-end of memory.  This creates more
1090 //  fragmentation of the heap, because old-gen regions get scattered among low-address regions such that it becomes
1091 //  more difficult to find contiguous regions for humongous objects.
1092 void ShenandoahFullGC::phase2_calculate_target_addresses(ShenandoahHeapRegionSet** worker_slices) {
1093   GCTraceTime(Info, gc, phases) time("Phase 2: Compute new object addresses", _gc_timer);
1094   ShenandoahGCPhase calculate_address_phase(ShenandoahPhaseTimings::full_gc_calculate_addresses);
1095 
1096   ShenandoahHeap* heap = ShenandoahHeap::heap();
1097 
1098   // About to figure out which regions can be compacted, make sure pinning status
1099   // had been updated in GC prologue.
1100   heap->assert_pinned_region_status();
1101 
1102   {
1103     // Trash the immediately collectible regions before computing addresses
1104     ShenandoahTrashImmediateGarbageClosure tigcl;
1105     heap->heap_region_iterate(&tigcl);
1106 
1107     // Make sure regions are in good state: committed, active, clean.
1108     // This is needed because we are potentially sliding the data through them.
1109     ShenandoahEnsureHeapActiveClosure ecl;
1110     heap->heap_region_iterate(&ecl);
1111   }
1112 
1113   // Compute the new addresses for regular objects
1114   {
1115     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_calculate_addresses_regular);
1116 
1117     distribute_slices(worker_slices);
1118 
1119     size_t num_workers = heap->max_workers();
1120 
1121     ResourceMark rm;
1122     ShenandoahPrepareForCompactionTask task(_preserved_marks, worker_slices, num_workers);
1123     heap->workers()->run_task(&task);
1124   }
1125 
1126   // Compute the new addresses for humongous objects
1127   {
1128     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_calculate_addresses_humong);
1129     calculate_target_humongous_objects();
1130   }
1131 }
1132 
1133 class ShenandoahAdjustPointersClosure : public MetadataVisitingOopIterateClosure {
1134 private:
1135   ShenandoahHeap* const _heap;
1136   ShenandoahMarkingContext* const _ctx;
1137 
1138   template <class T>
1139   inline void do_oop_work(T* p) {
1140     T o = RawAccess<>::oop_load(p);
1141     if (!CompressedOops::is_null(o)) {
1142       oop obj = CompressedOops::decode_not_null(o);

1176 
1177 class ShenandoahAdjustPointersTask : public WorkerTask {
1178 private:
1179   ShenandoahHeap*          const _heap;
1180   ShenandoahRegionIterator       _regions;
1181 
1182 public:
1183   ShenandoahAdjustPointersTask() :
1184     WorkerTask("Shenandoah Adjust Pointers"),
1185     _heap(ShenandoahHeap::heap()) {
1186   }
1187 
1188   void work(uint worker_id) {
1189     ShenandoahParallelWorkerSession worker_session(worker_id);
1190     ShenandoahAdjustPointersObjectClosure obj_cl;
1191     ShenandoahHeapRegion* r = _regions.next();
1192     while (r != nullptr) {
1193       if (!r->is_humongous_continuation() && r->has_live()) {
1194         _heap->marked_object_iterate(r, &obj_cl);
1195       }
1196       if (r->is_pinned() && r->is_old() && r->is_active() && !r->is_humongous()) {
1197         // Pinned regions are not compacted so they may still hold unmarked objects with
1198         // reference to reclaimed memory. Remembered set scanning will crash if it attempts
1199         // to iterate the oops in these objects.
1200         r->begin_preemptible_coalesce_and_fill();
1201         r->oop_fill_and_coalesce_without_cancel();
1202       }
1203       r = _regions.next();
1204     }
1205   }
1206 };
1207 
1208 class ShenandoahAdjustRootPointersTask : public WorkerTask {
1209 private:
1210   ShenandoahRootAdjuster* _rp;
1211   PreservedMarksSet* _preserved_marks;
1212 public:
1213   ShenandoahAdjustRootPointersTask(ShenandoahRootAdjuster* rp, PreservedMarksSet* preserved_marks) :
1214     WorkerTask("Shenandoah Adjust Root Pointers"),
1215     _rp(rp),
1216     _preserved_marks(preserved_marks) {}
1217 
1218   void work(uint worker_id) {
1219     ShenandoahParallelWorkerSession worker_session(worker_id);
1220     ShenandoahAdjustPointersClosure cl;
1221     _rp->roots_do(worker_id, &cl);
1222     _preserved_marks->get(worker_id)->adjust_during_full_gc();

1283     _worker_slices(worker_slices) {
1284   }
1285 
1286   void work(uint worker_id) {
1287     ShenandoahParallelWorkerSession worker_session(worker_id);
1288     ShenandoahHeapRegionSetIterator slice(_worker_slices[worker_id]);
1289 
1290     ShenandoahCompactObjectsClosure cl(worker_id);
1291     ShenandoahHeapRegion* r = slice.next();
1292     while (r != nullptr) {
1293       assert(!r->is_humongous(), "must not get humongous regions here");
1294       if (r->has_live()) {
1295         _heap->marked_object_iterate(r, &cl);
1296       }
1297       r->set_top(r->new_top());
1298       r = slice.next();
1299     }
1300   }
1301 };
1302 
1303 static void account_for_region(ShenandoahHeapRegion* r, size_t &region_count, size_t &region_usage, size_t &humongous_waste) {
1304   region_count++;
1305   region_usage += r->used();
1306   if (r->is_humongous_start()) {
1307     // For each humongous object, we take this path once regardless of how many regions it spans.
1308     HeapWord* obj_addr = r->bottom();
1309     oop obj = cast_to_oop(obj_addr);
1310     size_t word_size = obj->size();
1311     size_t region_size_words = ShenandoahHeapRegion::region_size_words();
1312     size_t overreach = word_size % region_size_words;
1313     if (overreach != 0) {
1314       humongous_waste += (region_size_words - overreach) * HeapWordSize;
1315     }
1316     // else, this humongous object aligns exactly on region size, so no waste.
1317   }
1318 }
1319 
1320 class ShenandoahPostCompactClosure : public ShenandoahHeapRegionClosure {
1321 private:
1322   ShenandoahHeap* const _heap;
1323   bool _is_generational;
1324   size_t _young_regions, _young_usage, _young_humongous_waste;
1325   size_t _old_regions, _old_usage, _old_humongous_waste;
1326 
1327 public:
1328   ShenandoahPostCompactClosure() : _heap(ShenandoahHeap::heap()),
1329                                    _is_generational(_heap->mode()->is_generational()),
1330                                    _young_regions(0),
1331                                    _young_usage(0),
1332                                    _young_humongous_waste(0),
1333                                    _old_regions(0),
1334                                    _old_usage(0),
1335                                    _old_humongous_waste(0)
1336   {
1337     _heap->free_set()->clear();
1338   }
1339 
1340   void heap_region_do(ShenandoahHeapRegion* r) {
1341     assert (!r->is_cset(), "cset regions should have been demoted already");
1342 
1343     // Need to reset the complete-top-at-mark-start pointer here because
1344     // the complete marking bitmap is no longer valid. This ensures
1345     // size-based iteration in marked_object_iterate().
1346     // NOTE: See blurb at ShenandoahMCResetCompleteBitmapTask on why we need to skip
1347     // pinned regions.
1348     if (!r->is_pinned()) {
1349       _heap->complete_marking_context()->reset_top_at_mark_start(r);
1350     }
1351 
1352     size_t live = r->used();
1353 
1354     // Make empty regions that have been allocated into regular
1355     if (r->is_empty() && live > 0) {
1356       if (!_is_generational) {
1357         r->make_young_maybe();
1358       }
1359       // else, generational mode compaction has already established affiliation.
1360       r->make_regular_bypass();
1361       if (ZapUnusedHeapArea) {
1362         SpaceMangler::mangle_region(MemRegion(r->top(), r->end()));
1363       }
1364     }
1365 
1366     // Reclaim regular regions that became empty
1367     if (r->is_regular() && live == 0) {
1368       r->make_trash();
1369     }
1370 
1371     // Recycle all trash regions
1372     if (r->is_trash()) {
1373       live = 0;
1374       r->recycle();
1375     } else {
1376       if (r->is_old()) {
1377         account_for_region(r, _old_regions, _old_usage, _old_humongous_waste);
1378       } else if (r->is_young()) {
1379         account_for_region(r, _young_regions, _young_usage, _young_humongous_waste);
1380       }
1381     }

1382     r->set_live_data(live);
1383     r->reset_alloc_metadata();

1384   }
1385 
1386   void update_generation_usage() {
1387     if (_is_generational) {
1388       _heap->old_generation()->establish_usage(_old_regions, _old_usage, _old_humongous_waste);
1389       _heap->young_generation()->establish_usage(_young_regions, _young_usage, _young_humongous_waste);
1390     } else {
1391       assert(_old_regions == 0, "Old regions only expected in generational mode");
1392       assert(_old_usage == 0, "Old usage only expected in generational mode");
1393       assert(_old_humongous_waste == 0, "Old humongous waste only expected in generational mode");
1394     }
1395 
1396     // In generational mode, global usage should be the sum of young and old. This is also true
1397     // for non-generational modes except that there are no old regions.
1398     _heap->global_generation()->establish_usage(_old_regions + _young_regions,
1399                                                 _old_usage + _young_usage,
1400                                                 _old_humongous_waste + _young_humongous_waste);
1401   }
1402 };
1403 
1404 void ShenandoahFullGC::compact_humongous_objects() {
1405   // Compact humongous regions, based on their fwdptr objects.
1406   //
1407   // This code is serial, because doing the in-slice parallel sliding is tricky. In most cases,
1408   // humongous regions are already compacted, and do not require further moves, which alleviates
1409   // sliding costs. We may consider doing this in parallel in the future.
1410 
1411   ShenandoahHeap* heap = ShenandoahHeap::heap();
1412 
1413   for (size_t c = heap->num_regions(); c > 0; c--) {
1414     ShenandoahHeapRegion* r = heap->get_region(c - 1);
1415     if (r->is_humongous_start()) {
1416       oop old_obj = cast_to_oop(r->bottom());
1417       if (!old_obj->is_forwarded()) {
1418         // No need to move the object, it stays at the same slot
1419         continue;
1420       }
1421       size_t words_size = old_obj->size();
1422       size_t num_regions = ShenandoahHeapRegion::required_regions(words_size * HeapWordSize);
1423 
1424       size_t old_start = r->index();
1425       size_t old_end   = old_start + num_regions - 1;
1426       size_t new_start = heap->heap_region_index_containing(old_obj->forwardee());
1427       size_t new_end   = new_start + num_regions - 1;
1428       assert(old_start != new_start, "must be real move");
1429       assert(r->is_stw_move_allowed(), "Region " SIZE_FORMAT " should be movable", r->index());
1430 
1431       ContinuationGCSupport::relativize_stack_chunk(cast_to_oop<HeapWord*>(heap->get_region(old_start)->bottom()));
1432       log_debug(gc)("Full GC compaction moves humongous object from region " SIZE_FORMAT " to region " SIZE_FORMAT,
1433                     old_start, new_start);
1434 
1435       Copy::aligned_conjoint_words(heap->get_region(old_start)->bottom(),
1436                                    heap->get_region(new_start)->bottom(),
1437                                    words_size);
1438 
1439       oop new_obj = cast_to_oop(heap->get_region(new_start)->bottom());
1440       new_obj->init_mark();
1441 
1442       {
1443         ShenandoahAffiliation original_affiliation = r->affiliation();
1444         for (size_t c = old_start; c <= old_end; c++) {
1445           ShenandoahHeapRegion* r = heap->get_region(c);
1446           // Leave humongous region affiliation unchanged.
1447           r->make_regular_bypass();
1448           r->set_top(r->bottom());
1449         }
1450 
1451         for (size_t c = new_start; c <= new_end; c++) {
1452           ShenandoahHeapRegion* r = heap->get_region(c);
1453           if (c == new_start) {
1454             r->make_humongous_start_bypass(original_affiliation);
1455           } else {
1456             r->make_humongous_cont_bypass(original_affiliation);
1457           }
1458 
1459           // Trailing region may be non-full, record the remainder there
1460           size_t remainder = words_size & ShenandoahHeapRegion::region_size_words_mask();
1461           if ((c == new_end) && (remainder != 0)) {
1462             r->set_top(r->bottom() + remainder);
1463           } else {
1464             r->set_top(r->end());
1465           }
1466 
1467           r->reset_alloc_metadata();
1468         }
1469       }
1470     }
1471   }
1472 }
1473 
1474 // This is slightly different to ShHeap::reset_next_mark_bitmap:
1475 // we need to remain able to walk pinned regions.
1476 // Since pinned region do not move and don't get compacted, we will get holes with

1502 };
1503 
1504 void ShenandoahFullGC::phase4_compact_objects(ShenandoahHeapRegionSet** worker_slices) {
1505   GCTraceTime(Info, gc, phases) time("Phase 4: Move objects", _gc_timer);
1506   ShenandoahGCPhase compaction_phase(ShenandoahPhaseTimings::full_gc_copy_objects);
1507 
1508   ShenandoahHeap* heap = ShenandoahHeap::heap();
1509 
1510   // Compact regular objects first
1511   {
1512     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_regular);
1513     ShenandoahCompactObjectsTask compact_task(worker_slices);
1514     heap->workers()->run_task(&compact_task);
1515   }
1516 
1517   // Compact humongous objects after regular object moves
1518   {
1519     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_humong);
1520     compact_humongous_objects();
1521   }
1522 }
1523 
1524 void ShenandoahFullGC::phase5_epilog() {
1525   GCTraceTime(Info, gc, phases) time("Phase 5: Full GC epilog", _gc_timer);
1526   ShenandoahHeap* heap = ShenandoahHeap::heap();
1527 
1528   // Reset complete bitmap. We're about to reset the complete-top-at-mark-start pointer
1529   // and must ensure the bitmap is in sync.
1530   {
1531     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_reset_complete);
1532     ShenandoahMCResetCompleteBitmapTask task;
1533     heap->workers()->run_task(&task);
1534   }
1535 
1536   // Bring regions in proper states after the collection, and set heap properties.
1537   {
1538     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_rebuild);

1539     ShenandoahPostCompactClosure post_compact;
1540     heap->heap_region_iterate(&post_compact);
1541     post_compact.update_generation_usage();
1542     if (heap->mode()->is_generational()) {
1543       size_t old_usage = heap->old_generation()->used_regions_size();
1544       size_t old_capacity = heap->old_generation()->max_capacity();
1545 
1546       assert(old_usage % ShenandoahHeapRegion::region_size_bytes() == 0, "Old usage must aligh with region size");
1547       assert(old_capacity % ShenandoahHeapRegion::region_size_bytes() == 0, "Old capacity must aligh with region size");
1548 
1549       if (old_capacity > old_usage) {
1550         size_t excess_old_regions = (old_capacity - old_usage) / ShenandoahHeapRegion::region_size_bytes();
1551         heap->generation_sizer()->transfer_to_young(excess_old_regions);
1552       } else if (old_capacity < old_usage) {
1553         size_t old_regions_deficit = (old_usage - old_capacity) / ShenandoahHeapRegion::region_size_bytes();
1554         heap->generation_sizer()->force_transfer_to_old(old_regions_deficit);
1555       }
1556 
1557       log_info(gc)("FullGC done: young usage: " SIZE_FORMAT "%s, old usage: " SIZE_FORMAT "%s",
1558                    byte_size_in_proper_unit(heap->young_generation()->used()), proper_unit_for_byte_size(heap->young_generation()->used()),
1559                    byte_size_in_proper_unit(heap->old_generation()->used()),   proper_unit_for_byte_size(heap->old_generation()->used()));
1560     }
1561     heap->collection_set()->clear();
1562     size_t young_cset_regions, old_cset_regions;
1563     size_t first_old, last_old, num_old;
1564     heap->free_set()->prepare_to_rebuild(young_cset_regions, old_cset_regions, first_old, last_old, num_old);
1565 
1566     // We also do not expand old generation size following Full GC because we have scrambled age populations and
1567     // no longer have objects separated by age into distinct regions.
1568 
1569     // TODO: Do we need to fix FullGC so that it maintains aged segregation of objects into distinct regions?
1570     //       A partial solution would be to remember how many objects are of tenure age following Full GC, but
1571     //       this is probably suboptimal, because most of these objects will not reside in a region that will be
1572     //       selected for the next evacuation phase.
1573 
1574     // In case this Full GC resulted from degeneration, clear the tally on anticipated promotion.
1575     heap->clear_promotion_potential();
1576 
1577     if (heap->mode()->is_generational()) {
1578       // Invoke this in case we are able to transfer memory from OLD to YOUNG.
1579       heap->adjust_generation_sizes_for_next_cycle(0, 0, 0);
1580     }
1581     heap->free_set()->rebuild(young_cset_regions, old_cset_regions);
1582 
1583     // We defer generation resizing actions until after cset regions have been recycled.  We do this even following an
1584     // abbreviated cycle.
1585     if (heap->mode()->is_generational()) {
1586       bool success;
1587       size_t region_xfer;
1588       const char* region_destination;
1589       ShenandoahYoungGeneration* young_gen = heap->young_generation();
1590       ShenandoahGeneration* old_gen = heap->old_generation();
1591 
1592       size_t old_region_surplus = heap->get_old_region_surplus();
1593       size_t old_region_deficit = heap->get_old_region_deficit();
1594       if (old_region_surplus) {
1595         success = heap->generation_sizer()->transfer_to_young(old_region_surplus);
1596         region_destination = "young";
1597         region_xfer = old_region_surplus;
1598       } else if (old_region_deficit) {
1599         success = heap->generation_sizer()->transfer_to_old(old_region_deficit);
1600         region_destination = "old";
1601         region_xfer = old_region_deficit;
1602         if (!success) {
1603           ((ShenandoahOldHeuristics *) old_gen->heuristics())->trigger_cannot_expand();
1604         }
1605       } else {
1606         region_destination = "none";
1607         region_xfer = 0;
1608         success = true;
1609       }
1610       heap->set_old_region_surplus(0);
1611       heap->set_old_region_deficit(0);
1612       size_t young_available = young_gen->available();
1613       size_t old_available = old_gen->available();
1614       log_info(gc, ergo)("After cleanup, %s " SIZE_FORMAT " regions to %s to prepare for next gc, old available: "
1615                          SIZE_FORMAT "%s, young_available: " SIZE_FORMAT "%s",
1616                          success? "successfully transferred": "failed to transfer", region_xfer, region_destination,
1617                          byte_size_in_proper_unit(old_available), proper_unit_for_byte_size(old_available),
1618                          byte_size_in_proper_unit(young_available), proper_unit_for_byte_size(young_available));
1619     }
1620     heap->clear_cancelled_gc(true /* clear oom handler */);
1621   }
1622 }
< prev index next >