1 /*
   2  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
   3  * Copyright (c) 2025, Oracle and/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 "gc/shenandoah/shenandoahAgeCensus.hpp"
  27 #include "gc/shenandoah/shenandoahClosures.inline.hpp"
  28 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
  29 #include "gc/shenandoah/shenandoahFreeSet.hpp"
  30 #include "gc/shenandoah/shenandoahGeneration.hpp"
  31 #include "gc/shenandoah/shenandoahGenerationalControlThread.hpp"
  32 #include "gc/shenandoah/shenandoahGenerationalEvacuationTask.hpp"
  33 #include "gc/shenandoah/shenandoahGenerationalHeap.hpp"
  34 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  35 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
  36 #include "gc/shenandoah/shenandoahHeapRegionClosures.hpp"
  37 #include "gc/shenandoah/shenandoahInitLogger.hpp"
  38 #include "gc/shenandoah/shenandoahMemoryPool.hpp"
  39 #include "gc/shenandoah/shenandoahMonitoringSupport.hpp"
  40 #include "gc/shenandoah/shenandoahOldGeneration.hpp"
  41 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
  42 #include "gc/shenandoah/shenandoahRegulatorThread.hpp"
  43 #include "gc/shenandoah/shenandoahScanRemembered.inline.hpp"
  44 #include "gc/shenandoah/shenandoahUtils.hpp"
  45 #include "gc/shenandoah/shenandoahWorkerPolicy.hpp"
  46 #include "gc/shenandoah/shenandoahYoungGeneration.hpp"
  47 #include "logging/log.hpp"
  48 #include "utilities/events.hpp"
  49 
  50 
  51 class ShenandoahGenerationalInitLogger : public ShenandoahInitLogger {
  52 public:
  53   static void print() {
  54     ShenandoahGenerationalInitLogger logger;
  55     logger.print_all();
  56   }
  57 protected:
  58   void print_gc_specific() override {
  59     ShenandoahInitLogger::print_gc_specific();
  60 
  61     ShenandoahGenerationalHeap* heap = ShenandoahGenerationalHeap::heap();
  62     log_info(gc, init)("Young Heuristics: %s", heap->young_generation()->heuristics()->name());
  63     log_info(gc, init)("Old Heuristics: %s", heap->old_generation()->heuristics()->name());
  64   }
  65 };
  66 
  67 size_t ShenandoahGenerationalHeap::calculate_min_plab() {
  68   return align_up(PLAB::min_size(), CardTable::card_size_in_words());
  69 }
  70 
  71 size_t ShenandoahGenerationalHeap::calculate_max_plab() {
  72   size_t MaxTLABSizeWords = ShenandoahHeapRegion::max_tlab_size_words();
  73   return align_down(MaxTLABSizeWords, CardTable::card_size_in_words());
  74 }
  75 
  76 // Returns size in bytes
  77 size_t ShenandoahGenerationalHeap::unsafe_max_tlab_alloc() const {
  78   return MIN2(ShenandoahHeapRegion::max_tlab_size_bytes(), young_generation()->available());
  79 }
  80 
  81 ShenandoahGenerationalHeap::ShenandoahGenerationalHeap(ShenandoahCollectorPolicy* policy) :
  82   ShenandoahHeap(policy),
  83   _age_census(nullptr),
  84   _min_plab_size(calculate_min_plab()),
  85   _max_plab_size(calculate_max_plab()),
  86   _regulator_thread(nullptr),
  87   _young_gen_memory_pool(nullptr),
  88   _old_gen_memory_pool(nullptr) {
  89   assert(is_aligned(_min_plab_size, CardTable::card_size_in_words()), "min_plab_size must be aligned");
  90   assert(is_aligned(_max_plab_size, CardTable::card_size_in_words()), "max_plab_size must be aligned");
  91 }
  92 
  93 void ShenandoahGenerationalHeap::initialize_generations() {
  94   ShenandoahHeap::initialize_generations();
  95   _young_generation->post_initialize(this);
  96   _old_generation->post_initialize(this);
  97 }
  98 
  99 void ShenandoahGenerationalHeap::post_initialize() {
 100   ShenandoahHeap::post_initialize();
 101   _age_census = new ShenandoahAgeCensus();
 102 }
 103 
 104 void ShenandoahGenerationalHeap::post_initialize_heuristics() {
 105   ShenandoahHeap::post_initialize_heuristics();
 106   _young_generation->post_initialize_heuristics();
 107   _old_generation->post_initialize_heuristics();
 108 }
 109 
 110 void ShenandoahGenerationalHeap::print_init_logger() const {
 111   ShenandoahGenerationalInitLogger logger;
 112   logger.print_all();
 113 }
 114 
 115 void ShenandoahGenerationalHeap::initialize_heuristics() {
 116   // Initialize global generation and heuristics even in generational mode.
 117   ShenandoahHeap::initialize_heuristics();
 118 
 119   _young_generation = new ShenandoahYoungGeneration(max_workers());
 120   _old_generation = new ShenandoahOldGeneration(max_workers());
 121   _young_generation->initialize_heuristics(mode());
 122   _old_generation->initialize_heuristics(mode());
 123 }
 124 
 125 void ShenandoahGenerationalHeap::initialize_serviceability() {
 126   assert(mode()->is_generational(), "Only for the generational mode");
 127   _young_gen_memory_pool = new ShenandoahYoungGenMemoryPool(this);
 128   _old_gen_memory_pool = new ShenandoahOldGenMemoryPool(this);
 129   cycle_memory_manager()->add_pool(_young_gen_memory_pool);
 130   cycle_memory_manager()->add_pool(_old_gen_memory_pool);
 131   stw_memory_manager()->add_pool(_young_gen_memory_pool);
 132   stw_memory_manager()->add_pool(_old_gen_memory_pool);
 133 }
 134 
 135 GrowableArray<MemoryPool*> ShenandoahGenerationalHeap::memory_pools() {
 136   assert(mode()->is_generational(), "Only for the generational mode");
 137   GrowableArray<MemoryPool*> memory_pools(2);
 138   memory_pools.append(_young_gen_memory_pool);
 139   memory_pools.append(_old_gen_memory_pool);
 140   return memory_pools;
 141 }
 142 
 143 void ShenandoahGenerationalHeap::initialize_controller() {
 144   auto control_thread = new ShenandoahGenerationalControlThread();
 145   _control_thread = control_thread;
 146   _regulator_thread = new ShenandoahRegulatorThread(control_thread);
 147 }
 148 
 149 void ShenandoahGenerationalHeap::gc_threads_do(ThreadClosure* tcl) const {
 150   if (!shenandoah_policy()->is_at_shutdown()) {
 151     ShenandoahHeap::gc_threads_do(tcl);
 152     tcl->do_thread(regulator_thread());
 153   }
 154 }
 155 
 156 void ShenandoahGenerationalHeap::stop() {
 157   ShenandoahHeap::stop();
 158   regulator_thread()->stop();
 159 }
 160 
 161 void ShenandoahGenerationalHeap::start_idle_span() {
 162   young_generation()->heuristics()->start_idle_span();
 163 }
 164 
 165 bool ShenandoahGenerationalHeap::requires_barriers(stackChunkOop obj) const {
 166   if (is_idle()) {
 167     return false;
 168   }
 169 
 170   if (is_concurrent_young_mark_in_progress() && is_in_young(obj) && !marking_context()->allocated_after_mark_start(obj)) {
 171     // We are marking young, this object is in young, and it is below the TAMS
 172     return true;
 173   }
 174 
 175   if (is_in_old(obj)) {
 176     // Card marking barriers are required for objects in the old generation
 177     return true;
 178   }
 179 
 180   if (has_forwarded_objects()) {
 181     // Object may have pointers that need to be updated
 182     return true;
 183   }
 184 
 185   return false;
 186 }
 187 
 188 void ShenandoahGenerationalHeap::evacuate_collection_set(ShenandoahGeneration* generation, bool concurrent) {
 189   ShenandoahRegionIterator regions;
 190   ShenandoahGenerationalEvacuationTask task(this, generation, &regions, concurrent, false /* only promote regions */);
 191   workers()->run_task(&task);
 192 }
 193 
 194 void ShenandoahGenerationalHeap::promote_regions_in_place(ShenandoahGeneration* generation, bool concurrent) {
 195   ShenandoahRegionIterator regions;
 196   ShenandoahGenerationalEvacuationTask task(this, generation, &regions, concurrent, true /* only promote regions */);
 197   workers()->run_task(&task);
 198 }
 199 
 200 oop ShenandoahGenerationalHeap::evacuate_object(oop p, Thread* thread) {
 201   assert(thread == Thread::current(), "Expected thread parameter to be current thread.");
 202 
 203   ShenandoahHeapRegion* from_region = heap_region_containing(p);
 204   assert(!from_region->is_humongous(), "never evacuate humongous objects");
 205 
 206   // Try to keep the object in the same generation
 207   const ShenandoahAffiliation target_gen = from_region->affiliation();
 208 
 209   if (target_gen == YOUNG_GENERATION) {
 210     markWord mark = p->mark();
 211     if (mark.is_marked()) {
 212       // Already forwarded.
 213       return ShenandoahBarrierSet::resolve_forwarded(p);
 214     }
 215 
 216     if (mark.has_displaced_mark_helper()) {
 217       // We don't want to deal with MT here just to ensure we read the right mark word.
 218       // Skip the potential promotion attempt for this one.
 219     } else if (age_census()->is_tenurable(from_region->age() + mark.age())) {
 220       // If the object is tenurable, try to promote it
 221       oop result = try_evacuate_object<YOUNG_GENERATION, OLD_GENERATION>(p, thread, from_region->age());
 222 
 223       // If we failed to promote this aged object, we'll fall through to code below and evacuate to young-gen.
 224       if (result != nullptr) {
 225         return result;
 226       }
 227     }
 228     return try_evacuate_object<YOUNG_GENERATION, YOUNG_GENERATION>(p, thread, from_region->age());
 229   }
 230 
 231   assert(target_gen == OLD_GENERATION, "Expected evacuation to old");
 232   return try_evacuate_object<OLD_GENERATION, OLD_GENERATION>(p, thread, from_region->age());
 233 }
 234 
 235 // try_evacuate_object registers the object and dirties the associated remembered set information when evacuating
 236 // to OLD_GENERATION.
 237 template<ShenandoahAffiliation FROM_GENERATION, ShenandoahAffiliation TO_GENERATION>
 238 oop ShenandoahGenerationalHeap::try_evacuate_object(oop p, Thread* thread, uint from_region_age) {
 239   bool alloc_from_lab = true;
 240   bool has_plab = false;
 241   HeapWord* copy = nullptr;
 242   size_t size = ShenandoahForwarding::size(p);
 243   constexpr bool is_promotion = (TO_GENERATION == OLD_GENERATION) && (FROM_GENERATION == YOUNG_GENERATION);
 244 
 245 #ifdef ASSERT
 246   if (ShenandoahOOMDuringEvacALot &&
 247       (os::random() & 1) == 0) { // Simulate OOM every ~2nd slow-path call
 248     copy = nullptr;
 249   } else {
 250 #endif
 251     if (UseTLAB) {
 252       switch (TO_GENERATION) {
 253         case YOUNG_GENERATION: {
 254           copy = allocate_from_gclab(thread, size);
 255           if ((copy == nullptr) && (size < ShenandoahThreadLocalData::gclab_size(thread))) {
 256             // GCLAB allocation failed because we are bumping up against the limit on young evacuation reserve.  Try resetting
 257             // the desired GCLAB size and retry GCLAB allocation to avoid cascading of shared memory allocations.
 258             ShenandoahThreadLocalData::set_gclab_size(thread, PLAB::min_size());
 259             copy = allocate_from_gclab(thread, size);
 260             // If we still get nullptr, we'll try a shared allocation below.
 261           }
 262           break;
 263         }
 264         case OLD_GENERATION: {
 265           ShenandoahPLAB* shenandoah_plab = ShenandoahThreadLocalData::shenandoah_plab(thread);
 266           if (shenandoah_plab != nullptr) {
 267             has_plab = true;
 268             copy = shenandoah_plab->allocate(size, is_promotion);
 269             if (copy == nullptr && size < shenandoah_plab->desired_size() && shenandoah_plab->retries_enabled()) {
 270               // PLAB allocation failed because we are bumping up against the limit on old evacuation reserve or because
 271               // the requested object does not fit within the current plab but the plab still has an "abundance" of memory,
 272               // where abundance is defined as >= ShenGenHeap::plab_min_size().  In the former case, we try shrinking the
 273               // desired PLAB size to the minimum and retry PLAB allocation to avoid cascading of shared memory allocations.
 274               // Shrinking the desired PLAB size may allow us to eke out a small PLAB while staying beneath evacuation reserve.
 275               if (shenandoah_plab->plab()->words_remaining() < plab_min_size()) {
 276                 shenandoah_plab->set_desired_size(plab_min_size());
 277                 copy = shenandoah_plab->allocate(size, is_promotion);
 278                 if (copy == nullptr) {
 279                   // If we still get nullptr, we'll try a shared allocation below.
 280                   // However, don't continue to retry until we have success (probably in next GC pass)
 281                   shenandoah_plab->disable_retries();
 282                 }
 283               }
 284             }
 285           }
 286           break;
 287         }
 288         default: {
 289           ShouldNotReachHere();
 290           break;
 291         }
 292       }
 293     }
 294 
 295     if (copy == nullptr) {
 296       // If we failed to allocate in LAB, we'll try a shared allocation.
 297       if (!is_promotion || !has_plab || (size > PLAB::min_size())) {
 298         ShenandoahAllocRequest req = ShenandoahAllocRequest::for_shared_gc(size, TO_GENERATION, is_promotion);
 299         copy = allocate_memory(req);
 300         alloc_from_lab = false;
 301       }
 302       // else, we leave copy equal to nullptr, signaling a promotion failure below if appropriate.
 303       // We choose not to promote objects smaller than size_threshold by way of shared allocations as this is too
 304       // costly.  Instead, we'll simply "evacuate" to young-gen memory (using a GCLAB) and will promote in a future
 305       // evacuation pass.  This condition is denoted by: is_promotion && has_plab && (size <= size_threshhold).
 306     }
 307 #ifdef ASSERT
 308   }
 309 #endif
 310 
 311   if (copy == nullptr) {
 312     if (TO_GENERATION == OLD_GENERATION) {
 313       if (FROM_GENERATION == YOUNG_GENERATION) {
 314         // Signal that promotion failed. Will evacuate this old object somewhere in young gen.
 315         old_generation()->handle_failed_promotion(thread, size);
 316         return nullptr;
 317       } else {
 318         // Remember that evacuation to old gen failed. We'll want to trigger a full gc to recover from this
 319         // after the evacuation threads have finished.
 320         old_generation()->handle_failed_evacuation();
 321       }
 322     }
 323 
 324     control_thread()->handle_alloc_failure_evac(size);
 325 
 326     // Install the self-forwarded bit so other evacuators/LRBs see the
 327     // object as "already handled, do not try to evacuate". The CAS may
 328     // fail if another thread concurrently installed a real forwardee or
 329     // self-forwarded first.
 330     markWord old_mark = p->mark();
 331     if (old_mark.is_forwarded()) {
 332       return ShenandoahForwarding::get_forwardee(p);
 333     }
 334     oop winner = ShenandoahForwarding::try_forward_to_self(p, old_mark);
 335     if (winner == nullptr) {
 336       // We own the self-forwarding. Flag the from-region so the degen/full
 337       // GC entry drain knows to scan it for self_fwd bits to clear.
 338       heap_region_containing(p)->set_has_self_forwards();
 339       return p;
 340     }
 341     return winner;
 342   }
 343 
 344   if (ShenandoahEvacTracking) {
 345     evac_tracker()->begin_evacuation(thread, size * HeapWordSize, FROM_GENERATION, TO_GENERATION);
 346   }
 347 
 348   // Copy the object:
 349   Copy::aligned_disjoint_words(cast_from_oop<HeapWord*>(p), copy, size);
 350   oop copy_val = cast_to_oop(copy);
 351 
 352   // Update the age of the evacuated object
 353   if (TO_GENERATION == YOUNG_GENERATION && is_aging_cycle()) {
 354     increase_object_age(copy_val, from_region_age + 1);
 355   }
 356 
 357   // Relativize stack chunks before publishing the copy. After the forwarding CAS,
 358   // mutators can see the copy and thaw it via the fast path if flags == 0. We must
 359   // relativize derived pointers and set gc_mode before that happens. Skip if the
 360   // copy's mark word is already a forwarding pointer (another thread won the race
 361   // and overwrote the original's header before we copied it).
 362   if (!ShenandoahForwarding::is_forwarded(copy_val)) {
 363     ContinuationGCSupport::relativize_stack_chunk(copy_val);
 364   }
 365 
 366   // Try to install the new forwarding pointer.
 367   oop result = ShenandoahForwarding::try_update_forwardee(p, copy_val);
 368   if (result == copy_val) {
 369     // Successfully evacuated. Our copy is now the public one!
 370     if (ShenandoahEvacTracking) {
 371       // Record that the evacuation succeeded
 372       evac_tracker()->end_evacuation(thread, size * HeapWordSize, FROM_GENERATION, TO_GENERATION);
 373     }
 374   }  else {
 375     // Failed to evacuate. We need to deal with the object that is left behind. Since this
 376     // new allocation is certainly after TAMS, it will be considered live in the next cycle.
 377     // But if it happens to contain references to evacuated regions, those references would
 378     // not get updated for this stale copy during this cycle, and we will crash while scanning
 379     // it the next cycle.
 380     if (alloc_from_lab) {
 381       // For LAB allocations, it is enough to rollback the allocation ptr. Either the next
 382       // object will overwrite this stale copy, or the filler object on LAB retirement will
 383       // do this.
 384       switch (TO_GENERATION) {
 385         case YOUNG_GENERATION: {
 386           ShenandoahThreadLocalData::gclab(thread)->undo_allocation(copy, size);
 387           break;
 388         }
 389         case OLD_GENERATION: {
 390           ShenandoahThreadLocalData::shenandoah_plab(thread)->plab()->undo_allocation(copy, size);
 391           if (is_promotion) {
 392             ShenandoahThreadLocalData::shenandoah_plab(thread)->subtract_from_promoted(size * HeapWordSize);
 393           }
 394           break;
 395         }
 396         default: {
 397           ShouldNotReachHere();
 398           break;
 399         }
 400       }
 401     } else {
 402       // For non-LAB allocations, we have no way to retract the allocation, and
 403       // have to explicitly overwrite the copy with the filler object. With that overwrite,
 404       // we have to keep the fwdptr initialized and pointing to our (stale) copy.
 405       assert(size >= ShenandoahHeap::min_fill_size(), "previously allocated object known to be larger than min_size");
 406       fill_with_object(copy, size);
 407     }
 408   }
 409   shenandoah_assert_correct(nullptr, result);
 410   return result;
 411 }
 412 
 413 template oop ShenandoahGenerationalHeap::try_evacuate_object<YOUNG_GENERATION, YOUNG_GENERATION>(oop p, Thread* thread, uint from_region_age);
 414 template oop ShenandoahGenerationalHeap::try_evacuate_object<YOUNG_GENERATION, OLD_GENERATION>(oop p, Thread* thread, uint from_region_age);
 415 template oop ShenandoahGenerationalHeap::try_evacuate_object<OLD_GENERATION, OLD_GENERATION>(oop p, Thread* thread, uint from_region_age);
 416 
 417 // Call this function at the end of a GC cycle in order to establish proper sizes of young and old reserves,
 418 // setting the old-generation balance so that GC can perform the anticipated evacuations.
 419 //
 420 // Make sure old-generation is large enough, but no larger than is necessary, to hold mixed evacuations
 421 // and promotions, if we anticipate either. Any deficit is provided by the young generation, subject to
 422 // mutator_xfer_limit, and any surplus is transferred to the young generation.  mutator_xfer_limit is
 423 // the maximum we're able to transfer from young to old. The mutator_xfer_limit constrains the transfer
 424 // of memory from young to old.  It does not limit young reserves.
 425 void ShenandoahGenerationalHeap::compute_old_generation_balance(size_t mutator_xfer_limit,
 426                                                                 size_t old_trashed_regions, size_t young_trashed_regions) {
 427   shenandoah_assert_heaplocked();
 428   // We can limit the old reserve to the size of anticipated promotions:
 429   // max_old_reserve is an upper bound on memory evacuated from old and promoted to old,
 430   // clamped by the old generation space available.
 431   //
 432   // Here's the algebra.
 433   // Let SOEP = ShenandoahOldEvacPercent,
 434   //     OE = old evac,
 435   //     YE = young evac, and
 436   //     TE = total evac = OE + YE
 437   // By definition:
 438   //            SOEP/100 = OE/TE
 439   //                     = OE/(OE+YE)
 440   //  => SOEP/(100-SOEP) = OE/((OE+YE)-OE)      // componendo-dividendo: If a/b = c/d, then a/(b-a) = c/(d-c)
 441   //                     = OE/YE
 442   //  =>              OE = YE*SOEP/(100-SOEP)
 443 
 444   // We have to be careful in the event that SOEP is set to 100 by the user.
 445   assert(ShenandoahOldEvacPercent <= 100, "Error");
 446   const size_t region_size_bytes = ShenandoahHeapRegion::region_size_bytes();
 447 
 448   ShenandoahOldGeneration* old_gen = old_generation();
 449   size_t old_capacity = old_gen->max_capacity();
 450   size_t old_usage = old_gen->used(); // includes humongous waste
 451   size_t old_currently_available =
 452     ((old_capacity >= old_usage)? old_capacity - old_usage: 0) + old_trashed_regions * region_size_bytes;
 453 
 454   ShenandoahYoungGeneration* young_gen = young_generation();
 455   size_t young_capacity = young_gen->max_capacity();
 456   size_t young_usage = young_gen->used(); // includes humongous waste
 457   size_t young_available = ((young_capacity >= young_usage)? young_capacity - young_usage: 0);
 458   size_t freeset_available = free_set()->available_locked();
 459   if (young_available > freeset_available) {
 460     young_available = freeset_available;
 461   }
 462   young_available += young_trashed_regions * region_size_bytes;
 463 
 464   // The free set will reserve this amount of memory to hold young evacuations (initialized to the ideal reserve)
 465   size_t young_reserve = (young_generation()->max_capacity() * ShenandoahEvacReserve) / 100;
 466 
 467   // If ShenandoahOldEvacPercent equals 100, max_old_reserve is limited only by mutator_xfer_limit and young_reserve
 468   const size_t bound_on_old_reserve =
 469     ((old_currently_available + mutator_xfer_limit + young_reserve) * ShenandoahOldEvacPercent) / 100;
 470   size_t proposed_max_old = ((ShenandoahOldEvacPercent == 100)?
 471                              bound_on_old_reserve:
 472                              MIN2((young_reserve * ShenandoahOldEvacPercent) / (100 - ShenandoahOldEvacPercent),
 473                                   bound_on_old_reserve));
 474   assert(mutator_xfer_limit <= young_available,
 475          "Cannot transfer (%zu) memory that is not available (%zu)", mutator_xfer_limit, young_available);
 476 
 477   if (young_reserve > young_available) {
 478     young_reserve = young_available;
 479   }
 480   // We allow young_reserve to exceed mutator_xfer_limit. Essentially, this means the GC is already behind the pace
 481   // of mutator allocations, and we'll need to trigger the next GC as soon as possible.
 482   if (mutator_xfer_limit > young_reserve) {
 483     mutator_xfer_limit -= young_reserve;
 484   } else {
 485     mutator_xfer_limit = 0;
 486   }
 487 
 488   // Decide how much old space we should reserve for a mixed collection
 489   size_t proposed_reserve_for_mixed = 0;
 490   const size_t old_fragmented_available =
 491     old_currently_available - (old_generation()->free_unaffiliated_regions() + old_trashed_regions) * region_size_bytes;
 492 
 493   if (old_fragmented_available > proposed_max_old) {
 494     // In this case, the old_fragmented_available is greater than the desired amount of evacuation to old.
 495     // We'll use all of this memory to hold results of old evacuation, and we'll give back to the young generation
 496     // any old regions that are not fragmented.
 497     //
 498     // This scenario may happen after we have promoted many regions in place, and each of these regions had non-zero
 499     // unused memory, so there is now an abundance of old-fragmented available memory, even more than the desired
 500     // percentage for old reserve.  We cannot transfer these fragmented regions back to young.  Instead we make the
 501     // best of the situation by using this fragmented memory for both promotions and evacuations.
 502 
 503     proposed_max_old = old_fragmented_available;
 504   }
 505   // Otherwise: old_fragmented_available <= proposed_max_old. Do not shrink proposed_max_old from the original computation.
 506 
 507   // Though we initially set proposed_reserve_for_promo to equal the entirety of old fragmented available, we have the
 508   // opportunity below to shift some of this memory into the proposed_reserve_for_mixed.
 509   size_t proposed_reserve_for_promo = old_fragmented_available;
 510   const size_t max_old_reserve = proposed_max_old;
 511 
 512   const size_t mixed_candidate_live_memory = old_generation()->unprocessed_collection_candidates_live_memory();
 513   const bool doing_mixed = (mixed_candidate_live_memory > 0);
 514   if (doing_mixed) {
 515     // In the ideal, all of the memory reserved for mixed evacuation would be unfragmented, but we don't enforce
 516     // this.  Note that the initial value of  max_evac_need is conservative because we may not evacuate all of the
 517     // remaining mixed evacuation candidates in a single cycle.
 518     const size_t max_evac_need = (size_t) (mixed_candidate_live_memory * ShenandoahOldEvacWaste);
 519     assert(old_currently_available >= old_generation()->free_unaffiliated_regions() * region_size_bytes,
 520            "Unaffiliated available must be less than total available");
 521 
 522     // We prefer to evacuate all of mixed into unfragmented memory, and will expand old in order to do so, unless
 523     // we already have too much fragmented available memory in old.
 524     proposed_reserve_for_mixed = max_evac_need;
 525     if (proposed_reserve_for_mixed + proposed_reserve_for_promo > max_old_reserve) {
 526       // We're trying to reserve more memory than is available.  So we need to shrink our reserves.
 527       size_t excess_reserves = (proposed_reserve_for_mixed + proposed_reserve_for_promo) - max_old_reserve;
 528       // We need to shrink reserves by excess_reserves.  We prefer to shrink by reducing promotion, giving priority to mixed
 529       // evacuation.  If the promotion reserve is larger than the amount we need to shrink by, do all the shrinkage there.
 530       if (proposed_reserve_for_promo > excess_reserves) {
 531         proposed_reserve_for_promo -= excess_reserves;
 532       } else {
 533         // Otherwise, we'll shrink promotion reserve to zero and we'll shrink the mixed-evac reserve by the remaining excess.
 534         excess_reserves -= proposed_reserve_for_promo;
 535         proposed_reserve_for_promo = 0;
 536         proposed_reserve_for_mixed -= excess_reserves;
 537       }
 538     }
 539   }
 540   assert(proposed_reserve_for_mixed + proposed_reserve_for_promo <= max_old_reserve,
 541          "Reserve for mixed (%zu) plus reserve for promotions (%zu) must be less than maximum old reserve (%zu)",
 542          proposed_reserve_for_mixed, proposed_reserve_for_promo, max_old_reserve);
 543 
 544   // Decide how much additional space we should reserve for promotions from young.  We give priority to mixed evacations
 545   // over promotions.
 546   const size_t promo_load = old_generation()->get_promotion_potential();
 547   const bool doing_promotions = promo_load > 0;
 548 
 549   // promo_load represents the combined total of live memory within regions that have reached tenure age.  The true
 550   // promotion potential is larger than this, because individual objects within regions that have not yet reached tenure
 551   // age may be promotable. On the other hand, some of the objects that we intend to promote in the next GC cycle may
 552   // die before they are next marked.  In the future, the promo_load will include the total size of tenurable objects
 553   // residing in regions that have not yet reached tenure age.
 554 
 555   if (doing_promotions) {
 556     // We are always doing promotions, even when old_generation->get_promotion_potential() returns 0.  As currently implemented,
 557     // get_promotion_potential() only knows the total live memory contained within young-generation regions whose age is
 558     // tenurable. It does not know whether that memory will still be live at the end of the next mark cycle, and it doesn't
 559     // know how much memory is contained within objects whose individual ages are tenurable, which reside in regions with
 560     // non-tenurable age.  We use this, as adjusted by ShenandoahPromoEvacWaste, as an approximation of the total amount of
 561     // memory to be promoted.  In the near future, we expect to implement a change that will allow get_promotion_potential()
 562     // to account also for the total memory contained within individual objects that are tenure-ready even when they do
 563     // not reside in aged regions.  This will represent a conservative over approximation of promotable memory because
 564     // some of these objects may die before the next GC cycle executes.
 565 
 566     // Be careful not to ask for too much promotion reserves. We have observed jtreg test failures under which a greedy
 567     // promotion reserve causes a humongous allocation which is awaiting a full GC to fail (specifically
 568     // gc/TestAllocHumongousFragment.java). This happens if too much of the memory reclaimed by the full GC
 569     // is immediately reserved so that it cannot be allocated by the waiting mutator. It's not clear that this
 570     // particular test is representative of the needs of typical GenShen users.  It is really a test of high frequency
 571     // Full GCs under heap fragmentation stress.
 572 
 573     size_t promo_need = (size_t) (promo_load * ShenandoahPromoEvacWaste);
 574     if (promo_need > proposed_reserve_for_promo) {
 575       const size_t available_for_additional_promotions =
 576         max_old_reserve - (proposed_reserve_for_mixed + proposed_reserve_for_promo);
 577       if (proposed_reserve_for_promo + available_for_additional_promotions >= promo_need) {
 578         proposed_reserve_for_promo = promo_need;
 579       } else {
 580         proposed_reserve_for_promo += available_for_additional_promotions;
 581       }
 582     }
 583   }
 584   // else, leave proposed_reserve_for_promo as is.  By default, it is initialized to represent old_fragmented_available.
 585 
 586   // This is the total old we want to reserve (initialized to the ideal reserve)
 587   size_t proposed_old_reserve = proposed_reserve_for_mixed + proposed_reserve_for_promo;
 588 
 589   // We now check if the old generation is running a surplus or a deficit.
 590   size_t old_region_deficit = 0;
 591   size_t old_region_surplus = 0;
 592 
 593   size_t mutator_region_xfer_limit = mutator_xfer_limit / region_size_bytes;
 594   // align the mutator_xfer_limit on region size
 595   mutator_xfer_limit = mutator_region_xfer_limit * region_size_bytes;
 596 
 597   if (old_currently_available >= proposed_old_reserve) {
 598     // We are running a surplus, so the old region surplus can go to young
 599     const size_t old_surplus = old_currently_available - proposed_old_reserve;
 600     old_region_surplus = old_surplus / region_size_bytes;
 601     const size_t unaffiliated_old_regions = old_generation()->free_unaffiliated_regions() + old_trashed_regions;
 602     old_region_surplus = MIN2(old_region_surplus, unaffiliated_old_regions);
 603     old_generation()->set_region_balance(checked_cast<ssize_t>(old_region_surplus));
 604     old_currently_available -= old_region_surplus * region_size_bytes;
 605     young_available += old_region_surplus * region_size_bytes;
 606   } else if (old_currently_available + mutator_xfer_limit >= proposed_old_reserve) {
 607     // We know that old_currently_available < proposed_old_reserve because above test failed. Expand old_currently_available.
 608     // Mutator's xfer limit is sufficient to satisfy our need: transfer all memory from there.
 609     size_t old_deficit = proposed_old_reserve - old_currently_available;
 610     old_region_deficit = (old_deficit + region_size_bytes - 1) / region_size_bytes;
 611     old_generation()->set_region_balance(0 - checked_cast<ssize_t>(old_region_deficit));
 612     old_currently_available += old_region_deficit * region_size_bytes;
 613     young_available -= old_region_deficit * region_size_bytes;
 614   } else {
 615     // We know that (old_currently_available < proposed_old_reserve) and
 616     //   (old_currently_available + mutator_xfer_limit < proposed_old_reserve) because above tests failed.
 617     // We need to shrink proposed_old_reserves.
 618 
 619     // We could potentially shrink young_reserves in order to further expand proposed_old_reserves.  Let's not bother.  The
 620     // important thing is that we keep a total amount of memory in reserve in preparation for the next GC cycle.  At
 621     // the time we choose the next collection set, we'll have an opportunity to shift some of these young reserves
 622     // into old reserves if that makes sense.
 623 
 624     // Start by taking all of mutator_xfer_limit into old_currently_available.
 625     size_t old_region_deficit = mutator_region_xfer_limit;
 626     old_generation()->set_region_balance(0 - checked_cast<ssize_t>(old_region_deficit));
 627     old_currently_available += old_region_deficit * region_size_bytes;
 628     young_available -= old_region_deficit * region_size_bytes;
 629 
 630     assert(old_currently_available < proposed_old_reserve,
 631            "Old currently available (%zu) must be less than old reserve (%zu)", old_currently_available, proposed_old_reserve);
 632 
 633     // There's not enough memory to satisfy our desire.  Scale back our old-gen intentions.  We prefer to satisfy
 634     // the budget_overrun entirely from the promotion reserve, if that is large enough.  Otherwise, we'll satisfy
 635     // the overrun from a combination of promotion and mixed-evacuation reserves.
 636     size_t budget_overrun = proposed_old_reserve - old_currently_available;
 637     if (proposed_reserve_for_promo > budget_overrun) {
 638       proposed_reserve_for_promo -= budget_overrun;
 639       // Dead code:
 640       //  proposed_old_reserve -= budget_overrun;
 641     } else {
 642       budget_overrun -= proposed_reserve_for_promo;
 643       proposed_reserve_for_promo = 0;
 644       proposed_reserve_for_mixed = (proposed_reserve_for_mixed > budget_overrun)? proposed_reserve_for_mixed - budget_overrun: 0;
 645       // Dead code:
 646       //  Note: proposed_reserve_for_promo is 0 and proposed_reserve_for_mixed may equal 0.
 647       //  proposed_old_reserve = proposed_reserve_for_mixed;
 648     }
 649   }
 650 
 651   assert(old_region_deficit == 0 || old_region_surplus == 0,
 652          "Only surplus (%zu) or deficit (%zu), never both", old_region_surplus, old_region_deficit);
 653   assert(young_reserve + proposed_reserve_for_mixed + proposed_reserve_for_promo <= old_currently_available + young_available,
 654          "Cannot reserve more memory than is available: %zu + %zu + %zu <= %zu + %zu",
 655          young_reserve, proposed_reserve_for_mixed, proposed_reserve_for_promo, old_currently_available, young_available);
 656 
 657   // deficit/surplus adjustments to generation sizes will precede rebuild
 658   young_generation()->set_evacuation_reserve(young_reserve);
 659   old_generation()->set_evacuation_reserve(proposed_reserve_for_mixed);
 660   old_generation()->set_promoted_reserve(proposed_reserve_for_promo);
 661 }
 662 
 663 void ShenandoahGenerationalHeap::coalesce_and_fill_old_regions(bool concurrent) {
 664   class ShenandoahGlobalCoalesceAndFill : public WorkerTask {
 665   private:
 666       ShenandoahPhaseTimings::Phase _phase;
 667       ShenandoahRegionIterator _regions;
 668   public:
 669     explicit ShenandoahGlobalCoalesceAndFill(ShenandoahPhaseTimings::Phase phase) :
 670       WorkerTask("Shenandoah Global Coalesce"),
 671       _phase(phase) {}
 672 
 673     void work(uint worker_id) override {
 674       ShenandoahWorkerTimingsTracker timer(_phase,
 675                                            ShenandoahPhaseTimings::ScanClusters,
 676                                            worker_id, true);
 677       ShenandoahHeapRegion* region;
 678       while ((region = _regions.next()) != nullptr) {
 679         // old region is not in the collection set and was not immediately trashed
 680         if (region->is_old() && region->is_active() && !region->is_humongous()) {
 681           // Reset the coalesce and fill boundary because this is a global collect
 682           // and cannot be preempted by young collects. We want to be sure the entire
 683           // region is coalesced here and does not resume from a previously interrupted
 684           // or completed coalescing.
 685           region->begin_preemptible_coalesce_and_fill();
 686           region->oop_coalesce_and_fill(false);
 687         }
 688       }
 689     }
 690   };
 691 
 692   ShenandoahPhaseTimings::Phase phase = concurrent ?
 693           ShenandoahPhaseTimings::conc_coalesce_and_fill :
 694           ShenandoahPhaseTimings::degen_gc_coalesce_and_fill;
 695 
 696   // This is not cancellable
 697   ShenandoahGlobalCoalesceAndFill coalesce(phase);
 698   workers()->run_task(&coalesce);
 699   old_generation()->set_parsable(true);
 700 }
 701 
 702 template<bool CONCURRENT>
 703 class ShenandoahGenerationalUpdateHeapRefsTask : public WorkerTask {
 704 private:
 705   // For update refs, _generation will be young or global. Mixed collections use the young generation.
 706   ShenandoahGeneration* _generation;
 707   ShenandoahGenerationalHeap* _heap;
 708   ShenandoahRegionIterator* _regions;
 709   ShenandoahRegionChunkIterator* _work_chunks;
 710 
 711 public:
 712   ShenandoahGenerationalUpdateHeapRefsTask(ShenandoahGeneration* generation,
 713                                            ShenandoahRegionIterator* regions,
 714                                            ShenandoahRegionChunkIterator* work_chunks) :
 715           WorkerTask("Shenandoah Update References"),
 716           _generation(generation),
 717           _heap(ShenandoahGenerationalHeap::heap()),
 718           _regions(regions),
 719           _work_chunks(work_chunks)
 720   {
 721     const bool old_bitmap_stable = _heap->old_generation()->is_mark_complete();
 722     log_debug(gc, remset)("Update refs, scan remembered set using bitmap: %s", BOOL_TO_STR(old_bitmap_stable));
 723   }
 724 
 725   void work(uint worker_id) override {
 726     if (CONCURRENT) {
 727       ShenandoahConcurrentWorkerSession worker_session(worker_id);
 728       SuspendibleThreadSetJoiner stsj;
 729       do_work<ShenandoahConcUpdateRefsClosure>(worker_id);
 730     } else {
 731       ShenandoahParallelWorkerSession worker_session(worker_id);
 732       do_work<ShenandoahNonConcUpdateRefsClosure>(worker_id);
 733     }
 734   }
 735 
 736 private:
 737   template<class T>
 738   void do_work(uint worker_id) {
 739     T cl;
 740 
 741     if (CONCURRENT && (worker_id == 0)) {
 742       // We ask the first worker to replenish the Mutator free set by moving regions previously reserved to hold the
 743       // results of evacuation.  These reserves are no longer necessary because evacuation has completed.
 744       size_t cset_regions = _heap->collection_set()->count();
 745 
 746       // Now that evacuation is done, we can reassign any regions that had been reserved to hold the results of evacuation
 747       // to the mutator free set.  At the end of GC, we will have cset_regions newly evacuated fully empty regions from
 748       // which we will be able to replenish the Collector free set and the OldCollector free set in preparation for the
 749       // next GC cycle.
 750       _heap->free_set()->move_regions_from_collector_to_mutator(cset_regions);
 751     }
 752     // If !CONCURRENT, there's no value in expanding Mutator free set
 753 
 754     ShenandoahHeapRegion* r = _regions->next();
 755     // We update references for global, mixed, and young collections.
 756     assert(_generation->is_mark_complete(), "Expected complete marking");
 757     ShenandoahMarkingContext* const ctx = _heap->marking_context();
 758     bool is_mixed = _heap->collection_set()->has_old_regions();
 759     while (r != nullptr) {
 760       HeapWord* update_watermark = r->get_update_watermark();
 761       assert(update_watermark >= r->bottom(), "sanity");
 762 
 763       log_debug(gc)("Update refs worker " UINT32_FORMAT ", looking at region %zu", worker_id, r->index());
 764       if (r->is_active() && !r->is_cset()) {
 765         if (r->is_young()) {
 766           _heap->marked_object_oop_iterate(r, &cl, update_watermark);
 767         } else if (r->is_old()) {
 768           if (_generation->is_global()) {
 769 
 770             _heap->marked_object_oop_iterate(r, &cl, update_watermark);
 771           }
 772           // Otherwise, this is an old region in a young or mixed cycle.  Process it during a second phase, below.
 773         } else {
 774           // Because updating of references runs concurrently, it is possible that a FREE inactive region transitions
 775           // to a non-free active region while this loop is executing.  Whenever this happens, the changing of a region's
 776           // active status may propagate at a different speed than the changing of the region's affiliation.
 777 
 778           // When we reach this control point, it is because a race has allowed a region's is_active() status to be seen
 779           // by this thread before the region's affiliation() is seen by this thread.
 780 
 781           // It's ok for this race to occur because the newly transformed region does not have any references to be
 782           // updated.
 783 
 784           assert(r->get_update_watermark() == r->bottom(),
 785                  "%s Region %zu is_active but not recognized as YOUNG or OLD so must be newly transitioned from FREE",
 786                  r->affiliation_name(), r->index());
 787         }
 788       }
 789 
 790       if (_heap->check_cancelled_gc_and_yield(CONCURRENT)) {
 791         return;
 792       }
 793 
 794       r = _regions->next();
 795     }
 796 
 797     if (_generation->is_young()) {
 798       // Since this is generational and not GLOBAL, we have to process the remembered set.  There's no remembered
 799       // set processing if not in generational mode or if GLOBAL mode.
 800 
 801       // After this thread has exhausted its traditional update-refs work, it continues with updating refs within
 802       // remembered set. The remembered set workload is better balanced between threads, so threads that are "behind"
 803       // can catch up with other threads during this phase, allowing all threads to work more effectively in parallel.
 804       update_references_in_remembered_set(worker_id, cl, ctx, is_mixed);
 805     }
 806   }
 807 
 808   template<class T>
 809   void update_references_in_remembered_set(uint worker_id, T &cl, const ShenandoahMarkingContext* ctx, bool is_mixed) {
 810 
 811     struct ShenandoahRegionChunk assignment;
 812     ShenandoahScanRemembered* scanner = _heap->old_generation()->card_scan();
 813 
 814     while (!_heap->check_cancelled_gc_and_yield(CONCURRENT) && _work_chunks->next(&assignment)) {
 815       // Keep grabbing next work chunk to process until finished, or asked to yield
 816       ShenandoahHeapRegion* r = assignment._r;
 817       if (r->is_active() && !r->is_cset() && r->is_old()) {
 818         HeapWord* start_of_range = r->bottom() + assignment._chunk_offset;
 819         HeapWord* end_of_range = r->get_update_watermark();
 820         if (end_of_range > start_of_range + assignment._chunk_size) {
 821           end_of_range = start_of_range + assignment._chunk_size;
 822         }
 823 
 824         if (start_of_range >= end_of_range) {
 825           continue;
 826         }
 827 
 828         // Old region in a young cycle or mixed cycle.
 829         if (is_mixed) {
 830           if (r->is_humongous()) {
 831             // Need to examine both dirty and clean cards during mixed evac.
 832             r->oop_iterate_humongous_slice_all(&cl,start_of_range, assignment._chunk_size);
 833           } else {
 834             // Since this is mixed evacuation, old regions that are candidates for collection have not been coalesced
 835             // and filled.  This will use mark bits to find objects that need to be updated.
 836             update_references_in_old_region(cl, ctx, scanner, r, start_of_range, end_of_range);
 837           }
 838         } else {
 839           // This is a young evacuation
 840           size_t cluster_size = CardTable::card_size_in_words() * ShenandoahCardCluster::CardsPerCluster;
 841           size_t clusters = assignment._chunk_size / cluster_size;
 842           assert(clusters * cluster_size == assignment._chunk_size, "Chunk assignment must align on cluster boundaries");
 843           scanner->process_region_slice(r, assignment._chunk_offset, clusters, end_of_range, &cl, true, worker_id);
 844         }
 845       }
 846     }
 847   }
 848 
 849   template<class T>
 850   void update_references_in_old_region(T &cl, const ShenandoahMarkingContext* ctx, ShenandoahScanRemembered* scanner,
 851                                     const ShenandoahHeapRegion* r, HeapWord* start_of_range,
 852                                     HeapWord* end_of_range) const {
 853     // In case last object in my range spans boundary of my chunk, I may need to scan all the way to top()
 854     ShenandoahObjectToOopBoundedClosure<T> objs(&cl, start_of_range, r->top());
 855 
 856     // Any object that begins in a previous range is part of a different scanning assignment.  Any object that
 857     // starts after end_of_range is also not my responsibility.  (Either allocated during evacuation, so does
 858     // not hold pointers to from-space, or is beyond the range of my assigned work chunk.)
 859 
 860     // Find the first object that begins in my range, if there is one. Note that `p` will be set to `end_of_range`
 861     // when no live object is found in the range.
 862     HeapWord* tams = ctx->top_at_mark_start(r);
 863     HeapWord* p = get_first_object_start_word(ctx, scanner, tams, start_of_range, end_of_range);
 864 
 865     while (p < end_of_range) {
 866       // p is known to point to the beginning of marked object obj
 867       oop obj = cast_to_oop(p);
 868       objs.do_object(obj);
 869       HeapWord* prev_p = p;
 870       p += obj->size();
 871       if (p < tams) {
 872         p = ctx->get_next_marked_addr(p, tams);
 873         // If there are no more marked objects before tams, this returns tams.  Note that tams is
 874         // either >= end_of_range, or tams is the start of an object that is marked.
 875       }
 876       assert(p != prev_p, "Lack of forward progress");
 877     }
 878   }
 879 
 880   HeapWord* get_first_object_start_word(const ShenandoahMarkingContext* ctx, ShenandoahScanRemembered* scanner, HeapWord* tams,
 881                                         HeapWord* start_of_range, HeapWord* end_of_range) const {
 882     HeapWord* p = start_of_range;
 883 
 884     if (p >= tams) {
 885       // We cannot use ctx->is_marked(obj) to test whether an object begins at this address.  Instead,
 886       // we need to use the remembered set crossing map to advance p to the first object that starts
 887       // within the enclosing card.
 888       size_t card_index = scanner->card_index_for_addr(start_of_range);
 889       while (true) {
 890         HeapWord* first_object = scanner->first_object_in_card(card_index);
 891         if (first_object != nullptr) {
 892           p = first_object;
 893           break;
 894         } else if (scanner->addr_for_card_index(card_index + 1) < end_of_range) {
 895           card_index++;
 896         } else {
 897           // Signal that no object was found in range
 898           p = end_of_range;
 899           break;
 900         }
 901       }
 902     } else if (!ctx->is_marked(cast_to_oop(p))) {
 903       p = ctx->get_next_marked_addr(p, tams);
 904       // If there are no more marked objects before tams, this returns tams.
 905       // Note that tams is either >= end_of_range, or tams is the start of an object that is marked.
 906     }
 907     return p;
 908   }
 909 };
 910 
 911 void ShenandoahGenerationalHeap::update_heap_references(ShenandoahGeneration* generation, bool concurrent) {
 912   assert(!is_full_gc_in_progress(), "Only for concurrent and degenerated GC");
 913   const uint nworkers = workers()->active_workers();
 914   ShenandoahRegionChunkIterator work_list(nworkers);
 915   if (concurrent) {
 916     ShenandoahGenerationalUpdateHeapRefsTask<true> task(generation, &_update_refs_iterator, &work_list);
 917     workers()->run_task(&task);
 918   } else {
 919     ShenandoahGenerationalUpdateHeapRefsTask<false> task(generation, &_update_refs_iterator, &work_list);
 920     workers()->run_task(&task);
 921   }
 922 
 923   if (ShenandoahEnableCardStats) {
 924     // Only do this if we are collecting card stats
 925     ShenandoahScanRemembered* card_scan = old_generation()->card_scan();
 926     assert(card_scan != nullptr, "Card table must exist when card stats are enabled");
 927     card_scan->log_card_stats(nworkers, CARD_STAT_UPDATE_REFS);
 928   }
 929 }
 930 
 931 struct ShenandoahCompositeRegionClosure {
 932   template<typename C1, typename C2>
 933   class Closure : public ShenandoahHeapRegionClosure {
 934   private:
 935     C1 &_c1;
 936     C2 &_c2;
 937 
 938   public:
 939     Closure(C1 &c1, C2 &c2) : ShenandoahHeapRegionClosure(), _c1(c1), _c2(c2) {}
 940 
 941     void heap_region_do(ShenandoahHeapRegion* r) override {
 942       _c1.heap_region_do(r);
 943       _c2.heap_region_do(r);
 944     }
 945 
 946     bool is_thread_safe() override {
 947       return _c1.is_thread_safe() && _c2.is_thread_safe();
 948     }
 949   };
 950 
 951   template<typename C1, typename C2>
 952   static Closure<C1, C2> of(C1 &c1, C2 &c2) {
 953     return Closure<C1, C2>(c1, c2);
 954   }
 955 };
 956 
 957 class ShenandoahUpdateRegionAges : public ShenandoahHeapRegionClosure {
 958 private:
 959   ShenandoahMarkingContext* _ctx;
 960 
 961 public:
 962   explicit ShenandoahUpdateRegionAges(ShenandoahMarkingContext* ctx) : _ctx(ctx) { }
 963 
 964   void heap_region_do(ShenandoahHeapRegion* r) override {
 965     // Maintenance of region age must follow evacuation in order to account for
 966     // evacuation allocations within survivor regions.  We consult region age during
 967     // the subsequent evacuation to determine whether certain objects need to
 968     // be promoted.
 969     if (r->is_young() && r->is_active()) {
 970       HeapWord *tams = _ctx->top_at_mark_start(r);
 971       HeapWord *top = r->top();
 972 
 973       // Allocations move the watermark when top moves.  However, compacting
 974       // objects will sometimes lower top beneath the watermark, after which,
 975       // attempts to read the watermark will assert out (watermark should not be
 976       // higher than top).
 977       if (top > tams) {
 978         // There have been allocations in this region since the start of the cycle.
 979         // Any objects new to this region must not assimilate elevated age.
 980         r->reset_age();
 981       } else if (ShenandoahGenerationalHeap::heap()->is_aging_cycle()) {
 982         r->increment_age();
 983       }
 984     }
 985   }
 986 
 987   bool is_thread_safe() override {
 988     return true;
 989   }
 990 };
 991 
 992 void ShenandoahGenerationalHeap::final_update_refs_update_region_states() {
 993   ShenandoahSynchronizePinnedRegionStates pins;
 994   ShenandoahUpdateRegionAges ages(marking_context());
 995   auto cl = ShenandoahCompositeRegionClosure::of(pins, ages);
 996   parallel_heap_region_iterate(&cl);
 997 }
 998 
 999 void ShenandoahGenerationalHeap::complete_degenerated_cycle() {
1000   shenandoah_assert_heaplocked_or_safepoint();
1001   if (!old_generation()->is_parsable()) {
1002     ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_coalesce_and_fill);
1003     coalesce_and_fill_old_regions(false);
1004   }
1005 
1006   old_generation()->maybe_log_promotion_failure_stats(false);
1007 }
1008 
1009 void ShenandoahGenerationalHeap::complete_concurrent_cycle() {
1010   if (!old_generation()->is_parsable()) {
1011     // Class unloading may render the card offsets unusable, so we must rebuild them before
1012     // the next remembered set scan. We _could_ let the control thread do this sometime after
1013     // the global cycle has completed and before the next young collection, but under memory
1014     // pressure the control thread may not have the time (that is, because it's running back
1015     // to back GCs). In that scenario, we would have to make the old regions parsable before
1016     // we could start a young collection. This could delay the start of the young cycle and
1017     // throw off the heuristics.
1018     entry_global_coalesce_and_fill();
1019   }
1020 
1021   old_generation()->maybe_log_promotion_failure_stats(true);
1022 }
1023 
1024 void ShenandoahGenerationalHeap::entry_global_coalesce_and_fill() {
1025   const char* msg = "Coalescing and filling old regions";
1026   ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::conc_coalesce_and_fill);
1027 
1028   TraceCollectorStats tcs(monitoring_support()->concurrent_collection_counters());
1029   EventMark em("%s", msg);
1030   ShenandoahWorkerScope scope(workers(),
1031                               ShenandoahWorkerPolicy::calc_workers_for_conc_marking(),
1032                               "concurrent coalesce and fill");
1033 
1034   coalesce_and_fill_old_regions(true);
1035 }
1036 
1037 void ShenandoahGenerationalHeap::update_region_ages(ShenandoahMarkingContext* ctx) {
1038   ShenandoahUpdateRegionAges cl(ctx);
1039   parallel_heap_region_iterate(&cl);
1040 }