< prev index next >

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

Print this page
*** 1,8 ***
--- 1,9 ---
  /*
   * Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
   * Copyright (c) 2013, 2022, Red Hat, Inc. All rights reserved.
+  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.

*** 34,43 ***
--- 35,57 ---
  #include "gc/shared/locationPrinter.inline.hpp"
  #include "gc/shared/memAllocator.hpp"
  #include "gc/shared/plab.hpp"
  #include "gc/shared/tlab_globals.hpp"
  
+ #include "gc/shenandoah/shenandoahAgeCensus.hpp"
+ #include "gc/shenandoah/heuristics/shenandoahOldHeuristics.hpp"
+ #include "gc/shenandoah/heuristics/shenandoahYoungHeuristics.hpp"
+ #include "gc/shenandoah/shenandoahAllocRequest.hpp"
  #include "gc/shenandoah/shenandoahBarrierSet.hpp"
+ #include "gc/shenandoah/shenandoahCardTable.hpp"
  #include "gc/shenandoah/shenandoahClosures.inline.hpp"
  #include "gc/shenandoah/shenandoahCollectionSet.hpp"
  #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
  #include "gc/shenandoah/shenandoahConcurrentMark.hpp"
  #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
  #include "gc/shenandoah/shenandoahControlThread.hpp"
  #include "gc/shenandoah/shenandoahFreeSet.hpp"
+ #include "gc/shenandoah/shenandoahGenerationalEvacuationTask.hpp"
+ #include "gc/shenandoah/shenandoahGenerationalHeap.hpp"
+ #include "gc/shenandoah/shenandoahGlobalGeneration.hpp"
  #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
  #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
  #include "gc/shenandoah/shenandoahHeapRegionSet.hpp"
  #include "gc/shenandoah/shenandoahInitLogger.hpp"
  #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
  #include "gc/shenandoah/shenandoahMemoryPool.hpp"
  #include "gc/shenandoah/shenandoahMetrics.hpp"
  #include "gc/shenandoah/shenandoahMonitoringSupport.hpp"
+ #include "gc/shenandoah/shenandoahOldGeneration.hpp"
  #include "gc/shenandoah/shenandoahOopClosures.inline.hpp"
  #include "gc/shenandoah/shenandoahPacer.inline.hpp"
  #include "gc/shenandoah/shenandoahPadding.hpp"
  #include "gc/shenandoah/shenandoahParallelCleaning.inline.hpp"
  #include "gc/shenandoah/shenandoahReferenceProcessor.hpp"
  #include "gc/shenandoah/shenandoahRootProcessor.inline.hpp"
+ #include "gc/shenandoah/shenandoahScanRemembered.inline.hpp"
  #include "gc/shenandoah/shenandoahSTWMark.hpp"
  #include "gc/shenandoah/shenandoahUtils.hpp"
  #include "gc/shenandoah/shenandoahVerifier.hpp"
  #include "gc/shenandoah/shenandoahCodeRoots.hpp"
  #include "gc/shenandoah/shenandoahVMOperations.hpp"
  #include "gc/shenandoah/shenandoahWorkGroup.hpp"
  #include "gc/shenandoah/shenandoahWorkerPolicy.hpp"
+ #include "gc/shenandoah/shenandoahYoungGeneration.hpp"
+ #include "gc/shenandoah/mode/shenandoahGenerationalMode.hpp"
  #include "gc/shenandoah/mode/shenandoahIUMode.hpp"
  #include "gc/shenandoah/mode/shenandoahPassiveMode.hpp"
  #include "gc/shenandoah/mode/shenandoahSATBMode.hpp"
+ #include "utilities/globalDefinitions.hpp"
+ 
  #if INCLUDE_JFR
  #include "gc/shenandoah/shenandoahJfrSupport.hpp"
  #endif
  
  #include "classfile/systemDictionary.hpp"

*** 159,13 ***
    _num_regions = ShenandoahHeapRegion::region_count();
    assert(_num_regions == (max_byte_size / reg_size_bytes),
           "Regions should cover entire heap exactly: " SIZE_FORMAT " != " SIZE_FORMAT "/" SIZE_FORMAT,
           _num_regions, max_byte_size, reg_size_bytes);
  
-   // Now we know the number of regions, initialize the heuristics.
-   initialize_heuristics();
- 
    size_t num_committed_regions = init_byte_size / reg_size_bytes;
    num_committed_regions = MIN2(num_committed_regions, _num_regions);
    assert(num_committed_regions <= _num_regions, "sanity");
    _initial_size = num_committed_regions * reg_size_bytes;
  
--- 174,10 ---

*** 177,10 ***
--- 189,13 ---
    // Default to max heap size.
    _soft_max_size = _num_regions * reg_size_bytes;
  
    _committed = _initial_size;
  
+   // Now we know the number of regions and heap sizes, initialize the heuristics.
+   initialize_heuristics_generations();
+ 
    size_t heap_page_size   = UseLargePages ? os::large_page_size() : os::vm_page_size();
    size_t bitmap_page_size = UseLargePages ? os::large_page_size() : os::vm_page_size();
    size_t region_page_size = UseLargePages ? os::large_page_size() : os::vm_page_size();
  
    //

*** 215,10 ***
--- 230,38 ---
    if (!_heap_region_special) {
      os::commit_memory_or_exit(sh_rs.base(), _initial_size, heap_alignment, false,
                                "Cannot commit heap memory");
    }
  
+   BarrierSet::set_barrier_set(new ShenandoahBarrierSet(this, _heap_region));
+ 
+   //
+   // After reserving the Java heap, create the card table, barriers, and workers, in dependency order
+   //
+   if (mode()->is_generational()) {
+     ShenandoahDirectCardMarkRememberedSet *rs;
+     ShenandoahCardTable* card_table = ShenandoahBarrierSet::barrier_set()->card_table();
+     size_t card_count = card_table->cards_required(heap_rs.size() / HeapWordSize);
+     rs = new ShenandoahDirectCardMarkRememberedSet(ShenandoahBarrierSet::barrier_set()->card_table(), card_count);
+     _card_scan = new ShenandoahScanRemembered<ShenandoahDirectCardMarkRememberedSet>(rs);
+ 
+     // Age census structure
+     _age_census = new ShenandoahAgeCensus();
+   }
+ 
+   _workers = new ShenandoahWorkerThreads("Shenandoah GC Threads", _max_workers);
+   if (_workers == nullptr) {
+     vm_exit_during_initialization("Failed necessary allocation.");
+   } else {
+     _workers->initialize_workers();
+   }
+ 
+   if (ParallelGCThreads > 1) {
+     _safepoint_workers = new ShenandoahWorkerThreads("Safepoint Cleanup Thread", ParallelGCThreads);
+     _safepoint_workers->initialize_workers();
+   }
+ 
    //
    // Reserve and commit memory for bitmap(s)
    //
  
    size_t bitmap_size_orig = ShenandoahMarkBitMap::compute_size(heap_rs.size());

*** 255,18 ***
    MemTracker::record_virtual_memory_type(bitmap.base(), mtGC);
    _bitmap_region = MemRegion((HeapWord*) bitmap.base(), bitmap.size() / HeapWordSize);
    _bitmap_region_special = bitmap.special();
  
    size_t bitmap_init_commit = _bitmap_bytes_per_slice *
!                               align_up(num_committed_regions, _bitmap_regions_per_slice) / _bitmap_regions_per_slice;
    bitmap_init_commit = MIN2(_bitmap_size, bitmap_init_commit);
    if (!_bitmap_region_special) {
      os::commit_memory_or_exit((char *) _bitmap_region.start(), bitmap_init_commit, bitmap_page_size, false,
                                "Cannot commit bitmap memory");
    }
  
!   _marking_context = new ShenandoahMarkingContext(_heap_region, _bitmap_region, _num_regions, _max_workers);
  
    if (ShenandoahVerify) {
      ReservedSpace verify_bitmap(_bitmap_size, bitmap_page_size);
      os::trace_page_sizes_for_requested_size("Verify Bitmap",
                                              bitmap_size_orig, bitmap_page_size,
--- 298,18 ---
    MemTracker::record_virtual_memory_type(bitmap.base(), mtGC);
    _bitmap_region = MemRegion((HeapWord*) bitmap.base(), bitmap.size() / HeapWordSize);
    _bitmap_region_special = bitmap.special();
  
    size_t bitmap_init_commit = _bitmap_bytes_per_slice *
!     align_up(num_committed_regions, _bitmap_regions_per_slice) / _bitmap_regions_per_slice;
    bitmap_init_commit = MIN2(_bitmap_size, bitmap_init_commit);
    if (!_bitmap_region_special) {
      os::commit_memory_or_exit((char *) _bitmap_region.start(), bitmap_init_commit, bitmap_page_size, false,
                                "Cannot commit bitmap memory");
    }
  
!   _marking_context = new ShenandoahMarkingContext(_heap_region, _bitmap_region, _num_regions);
  
    if (ShenandoahVerify) {
      ReservedSpace verify_bitmap(_bitmap_size, bitmap_page_size);
      os::trace_page_sizes_for_requested_size("Verify Bitmap",
                                              bitmap_size_orig, bitmap_page_size,

*** 353,15 ***
--- 396,17 ---
                                              cset_rs.base(),
                                              cset_rs.size(), cset_rs.page_size());
    }
  
    _regions = NEW_C_HEAP_ARRAY(ShenandoahHeapRegion*, _num_regions, mtGC);
+   _affiliations = NEW_C_HEAP_ARRAY(uint8_t, _num_regions, mtGC);
    _free_set = new ShenandoahFreeSet(this, _num_regions);
  
    {
      ShenandoahHeapLocker locker(lock());
  
+ 
      for (size_t i = 0; i < _num_regions; i++) {
        HeapWord* start = (HeapWord*)sh_rs.base() + ShenandoahHeapRegion::region_size_words() * i;
        bool is_committed = i < num_committed_regions;
        void* loc = region_storage.base() + i * region_align;
  

*** 369,16 ***
        assert(is_aligned(r, SHENANDOAH_CACHE_LINE_SIZE), "Sanity");
  
        _marking_context->initialize_top_at_mark_start(r);
        _regions[i] = r;
        assert(!collection_set()->is_in(i), "New region should not be in collection set");
      }
  
      // Initialize to complete
      _marking_context->mark_complete();
  
!     _free_set->rebuild();
    }
  
    if (AlwaysPreTouch) {
      // For NUMA, it is important to pre-touch the storage under bitmaps with worker threads,
      // before initialize() below zeroes it with initializing thread. For any given region,
--- 414,22 ---
        assert(is_aligned(r, SHENANDOAH_CACHE_LINE_SIZE), "Sanity");
  
        _marking_context->initialize_top_at_mark_start(r);
        _regions[i] = r;
        assert(!collection_set()->is_in(i), "New region should not be in collection set");
+ 
+       _affiliations[i] = ShenandoahAffiliation::FREE;
      }
  
      // Initialize to complete
      _marking_context->mark_complete();
+     size_t young_cset_regions, old_cset_regions;
  
!     // We are initializing free set.  We ignore cset region tallies.
+     size_t first_old, last_old, num_old;
+     _free_set->prepare_to_rebuild(young_cset_regions, old_cset_regions, first_old, last_old, num_old);
+     _free_set->rebuild(young_cset_regions, old_cset_regions);
    }
  
    if (AlwaysPreTouch) {
      // For NUMA, it is important to pre-touch the storage under bitmaps with worker threads,
      // before initialize() below zeroes it with initializing thread. For any given region,

*** 433,25 ***
    if (ShenandoahPacing) {
      _pacer = new ShenandoahPacer(this);
      _pacer->setup_for_idle();
    }
  
!   _control_thread = new ShenandoahControlThread();
  
!   ShenandoahInitLogger::print();
  
    return JNI_OK;
  }
  
! void ShenandoahHeap::initialize_mode() {
    if (ShenandoahGCMode != nullptr) {
      if (strcmp(ShenandoahGCMode, "satb") == 0) {
        _gc_mode = new ShenandoahSATBMode();
      } else if (strcmp(ShenandoahGCMode, "iu") == 0) {
        _gc_mode = new ShenandoahIUMode();
      } else if (strcmp(ShenandoahGCMode, "passive") == 0) {
        _gc_mode = new ShenandoahPassiveMode();
      } else {
        vm_exit_during_initialization("Unknown -XX:ShenandoahGCMode option");
      }
    } else {
      vm_exit_during_initialization("Unknown -XX:ShenandoahGCMode option (null)");
--- 484,35 ---
    if (ShenandoahPacing) {
      _pacer = new ShenandoahPacer(this);
      _pacer->setup_for_idle();
    }
  
!   initialize_controller();
  
!   print_init_logger();
  
    return JNI_OK;
  }
  
! void ShenandoahHeap::initialize_controller() {
+   _control_thread = new ShenandoahControlThread();
+ }
+ 
+ void ShenandoahHeap::print_init_logger() const {
+   ShenandoahInitLogger::print();
+ }
+ 
+ void ShenandoahHeap::initialize_heuristics_generations() {
    if (ShenandoahGCMode != nullptr) {
      if (strcmp(ShenandoahGCMode, "satb") == 0) {
        _gc_mode = new ShenandoahSATBMode();
      } else if (strcmp(ShenandoahGCMode, "iu") == 0) {
        _gc_mode = new ShenandoahIUMode();
      } else if (strcmp(ShenandoahGCMode, "passive") == 0) {
        _gc_mode = new ShenandoahPassiveMode();
+     } else if (strcmp(ShenandoahGCMode, "generational") == 0) {
+       _gc_mode = new ShenandoahGenerationalMode();
      } else {
        vm_exit_during_initialization("Unknown -XX:ShenandoahGCMode option");
      }
    } else {
      vm_exit_during_initialization("Unknown -XX:ShenandoahGCMode option (null)");

*** 465,124 ***
    if (_gc_mode->is_experimental() && !UnlockExperimentalVMOptions) {
      vm_exit_during_initialization(
              err_msg("GC mode \"%s\" is experimental, and must be enabled via -XX:+UnlockExperimentalVMOptions.",
                      _gc_mode->name()));
    }
- }
  
! void ShenandoahHeap::initialize_heuristics() {
!   assert(_gc_mode != nullptr, "Must be initialized");
!   _heuristics = _gc_mode->initialize_heuristics();
  
!   if (_heuristics->is_diagnostic() && !UnlockDiagnosticVMOptions) {
!     vm_exit_during_initialization(
!             err_msg("Heuristics \"%s\" is diagnostic, and must be enabled via -XX:+UnlockDiagnosticVMOptions.",
!                     _heuristics->name()));
!   }
!   if (_heuristics->is_experimental() && !UnlockExperimentalVMOptions) {
!     vm_exit_during_initialization(
-             err_msg("Heuristics \"%s\" is experimental, and must be enabled via -XX:+UnlockExperimentalVMOptions.",
-                     _heuristics->name()));
    }
  }
  
  #ifdef _MSC_VER
  #pragma warning( push )
  #pragma warning( disable:4355 ) // 'this' : used in base member initializer list
  #endif
  
  ShenandoahHeap::ShenandoahHeap(ShenandoahCollectorPolicy* policy) :
    CollectedHeap(),
    _initial_size(0),
-   _used(0),
    _committed(0),
!   _bytes_allocated_since_gc_start(0),
-   _max_workers(MAX2(ConcGCThreads, ParallelGCThreads)),
    _workers(nullptr),
    _safepoint_workers(nullptr),
    _heap_region_special(false),
    _num_regions(0),
    _regions(nullptr),
    _update_refs_iterator(this),
    _gc_state_changed(false),
    _gc_no_progress_count(0),
    _control_thread(nullptr),
    _shenandoah_policy(policy),
-   _gc_mode(nullptr),
-   _heuristics(nullptr),
    _free_set(nullptr),
    _pacer(nullptr),
    _verifier(nullptr),
    _phase_timings(nullptr),
    _monitoring_support(nullptr),
    _memory_pool(nullptr),
    _stw_memory_manager("Shenandoah Pauses"),
    _cycle_memory_manager("Shenandoah Cycles"),
    _gc_timer(new ConcurrentGCTimer()),
    _log_min_obj_alignment_in_bytes(LogMinObjAlignmentInBytes),
-   _ref_processor(new ShenandoahReferenceProcessor(MAX2(_max_workers, 1U))),
    _marking_context(nullptr),
    _bitmap_size(0),
    _bitmap_regions_per_slice(0),
    _bitmap_bytes_per_slice(0),
    _bitmap_region_special(false),
    _aux_bitmap_region_special(false),
    _liveness_cache(nullptr),
!   _collection_set(nullptr)
  {
-   // Initialize GC mode early, so we can adjust barrier support
-   initialize_mode();
-   BarrierSet::set_barrier_set(new ShenandoahBarrierSet(this));
- 
-   _max_workers = MAX2(_max_workers, 1U);
-   _workers = new ShenandoahWorkerThreads("Shenandoah GC Threads", _max_workers);
-   if (_workers == nullptr) {
-     vm_exit_during_initialization("Failed necessary allocation.");
-   } else {
-     _workers->initialize_workers();
-   }
- 
-   if (ParallelGCThreads > 1) {
-     _safepoint_workers = new ShenandoahWorkerThreads("Safepoint Cleanup Thread",
-                                                 ParallelGCThreads);
-     _safepoint_workers->initialize_workers();
-   }
  }
  
  #ifdef _MSC_VER
  #pragma warning( pop )
  #endif
  
- class ShenandoahResetBitmapTask : public WorkerTask {
- private:
-   ShenandoahRegionIterator _regions;
- 
- public:
-   ShenandoahResetBitmapTask() :
-     WorkerTask("Shenandoah Reset Bitmap") {}
- 
-   void work(uint worker_id) {
-     ShenandoahHeapRegion* region = _regions.next();
-     ShenandoahHeap* heap = ShenandoahHeap::heap();
-     ShenandoahMarkingContext* const ctx = heap->marking_context();
-     while (region != nullptr) {
-       if (heap->is_bitmap_slice_committed(region)) {
-         ctx->clear_bitmap(region);
-       }
-       region = _regions.next();
-     }
-   }
- };
- 
- void ShenandoahHeap::reset_mark_bitmap() {
-   assert_gc_workers(_workers->active_workers());
-   mark_incomplete_marking_context();
- 
-   ShenandoahResetBitmapTask task;
-   _workers->run_task(&task);
- }
- 
  void ShenandoahHeap::print_on(outputStream* st) const {
    st->print_cr("Shenandoah Heap");
    st->print_cr(" " SIZE_FORMAT "%s max, " SIZE_FORMAT "%s soft max, " SIZE_FORMAT "%s committed, " SIZE_FORMAT "%s used",
                 byte_size_in_proper_unit(max_capacity()), proper_unit_for_byte_size(max_capacity()),
                 byte_size_in_proper_unit(soft_max_capacity()), proper_unit_for_byte_size(soft_max_capacity()),
--- 526,88 ---
    if (_gc_mode->is_experimental() && !UnlockExperimentalVMOptions) {
      vm_exit_during_initialization(
              err_msg("GC mode \"%s\" is experimental, and must be enabled via -XX:+UnlockExperimentalVMOptions.",
                      _gc_mode->name()));
    }
  
!   // Max capacity is the maximum _allowed_ capacity. That is, the maximum allowed capacity
!   // for old would be total heap - minimum capacity of young. This means the sum of the maximum
!   // allowed for old and young could exceed the total heap size. It remains the case that the
+   // _actual_ capacity of young + old = total.
+   _generation_sizer.heap_size_changed(max_capacity());
+   size_t initial_capacity_young = _generation_sizer.max_young_size();
+   size_t max_capacity_young = _generation_sizer.max_young_size();
+   size_t initial_capacity_old = max_capacity() - max_capacity_young;
+   size_t max_capacity_old = max_capacity() - initial_capacity_young;
  
!   _young_generation = new ShenandoahYoungGeneration(_max_workers, max_capacity_young, initial_capacity_young);
!   _old_generation = new ShenandoahOldGeneration(_max_workers, max_capacity_old, initial_capacity_old);
!   _global_generation = new ShenandoahGlobalGeneration(_gc_mode->is_generational(), _max_workers, max_capacity(), max_capacity());
!   _global_generation->initialize_heuristics(_gc_mode);
!   if (mode()->is_generational()) {
!     _young_generation->initialize_heuristics(_gc_mode);
!     _old_generation->initialize_heuristics(_gc_mode);
    }
+   _evac_tracker = new ShenandoahEvacuationTracker(mode()->is_generational());
  }
  
  #ifdef _MSC_VER
  #pragma warning( push )
  #pragma warning( disable:4355 ) // 'this' : used in base member initializer list
  #endif
  
  ShenandoahHeap::ShenandoahHeap(ShenandoahCollectorPolicy* policy) :
    CollectedHeap(),
+   _gc_generation(nullptr),
    _initial_size(0),
    _committed(0),
!   _max_workers(MAX3(ConcGCThreads, ParallelGCThreads, 1U)),
    _workers(nullptr),
    _safepoint_workers(nullptr),
    _heap_region_special(false),
    _num_regions(0),
    _regions(nullptr),
+   _affiliations(nullptr),
    _update_refs_iterator(this),
    _gc_state_changed(false),
    _gc_no_progress_count(0),
+   _age_census(nullptr),
+   _cancel_requested_time(0),
+   _young_generation(nullptr),
+   _global_generation(nullptr),
+   _old_generation(nullptr),
    _control_thread(nullptr),
    _shenandoah_policy(policy),
    _free_set(nullptr),
    _pacer(nullptr),
    _verifier(nullptr),
    _phase_timings(nullptr),
+   _evac_tracker(nullptr),
+   _mmu_tracker(),
+   _generation_sizer(),
    _monitoring_support(nullptr),
    _memory_pool(nullptr),
    _stw_memory_manager("Shenandoah Pauses"),
    _cycle_memory_manager("Shenandoah Cycles"),
    _gc_timer(new ConcurrentGCTimer()),
    _log_min_obj_alignment_in_bytes(LogMinObjAlignmentInBytes),
    _marking_context(nullptr),
    _bitmap_size(0),
    _bitmap_regions_per_slice(0),
    _bitmap_bytes_per_slice(0),
    _bitmap_region_special(false),
    _aux_bitmap_region_special(false),
    _liveness_cache(nullptr),
!   _collection_set(nullptr),
+   _card_scan(nullptr)
  {
  }
  
  #ifdef _MSC_VER
  #pragma warning( pop )
  #endif
  
  void ShenandoahHeap::print_on(outputStream* st) const {
    st->print_cr("Shenandoah Heap");
    st->print_cr(" " SIZE_FORMAT "%s max, " SIZE_FORMAT "%s soft max, " SIZE_FORMAT "%s committed, " SIZE_FORMAT "%s used",
                 byte_size_in_proper_unit(max_capacity()), proper_unit_for_byte_size(max_capacity()),
                 byte_size_in_proper_unit(soft_max_capacity()), proper_unit_for_byte_size(soft_max_capacity()),

*** 593,11 ***
                 byte_size_in_proper_unit(ShenandoahHeapRegion::region_size_bytes()),
                 proper_unit_for_byte_size(ShenandoahHeapRegion::region_size_bytes()));
  
    st->print("Status: ");
    if (has_forwarded_objects())                 st->print("has forwarded objects, ");
!   if (is_concurrent_mark_in_progress())        st->print("marking, ");
    if (is_evacuation_in_progress())             st->print("evacuating, ");
    if (is_update_refs_in_progress())            st->print("updating refs, ");
    if (is_degenerated_gc_in_progress())         st->print("degenerated gc, ");
    if (is_full_gc_in_progress())                st->print("full gc, ");
    if (is_full_gc_move_in_progress())           st->print("full gc move, ");
--- 618,12 ---
                 byte_size_in_proper_unit(ShenandoahHeapRegion::region_size_bytes()),
                 proper_unit_for_byte_size(ShenandoahHeapRegion::region_size_bytes()));
  
    st->print("Status: ");
    if (has_forwarded_objects())                 st->print("has forwarded objects, ");
!   if (is_concurrent_old_mark_in_progress())    st->print("old marking, ");
+   if (is_concurrent_young_mark_in_progress())  st->print("young marking, ");
    if (is_evacuation_in_progress())             st->print("evacuating, ");
    if (is_update_refs_in_progress())            st->print("updating refs, ");
    if (is_degenerated_gc_in_progress())         st->print("degenerated gc, ");
    if (is_full_gc_in_progress())                st->print("full gc, ");
    if (is_full_gc_move_in_progress())           st->print("full gc move, ");

*** 644,10 ***
--- 670,12 ---
    }
  };
  
  void ShenandoahHeap::post_initialize() {
    CollectedHeap::post_initialize();
+   _mmu_tracker.initialize();
+ 
    MutexLocker ml(Threads_lock);
  
    ShenandoahInitWorkerGCLABClosure init_gclabs;
    _workers->threads_do(&init_gclabs);
  

*** 657,64 ***
    if (_safepoint_workers != nullptr) {
      _safepoint_workers->threads_do(&init_gclabs);
      _safepoint_workers->set_initialize_gclab();
    }
  
-   _heuristics->initialize();
- 
    JFR_ONLY(ShenandoahJFRSupport::register_jfr_type_serializers();)
  }
  
  size_t ShenandoahHeap::used() const {
!   return Atomic::load(&_used);
  }
  
  size_t ShenandoahHeap::committed() const {
    return Atomic::load(&_committed);
  }
  
- size_t ShenandoahHeap::available() const {
-   return free_set()->available();
- }
- 
  void ShenandoahHeap::increase_committed(size_t bytes) {
    shenandoah_assert_heaplocked_or_safepoint();
    _committed += bytes;
  }
  
  void ShenandoahHeap::decrease_committed(size_t bytes) {
    shenandoah_assert_heaplocked_or_safepoint();
    _committed -= bytes;
  }
  
! void ShenandoahHeap::increase_used(size_t bytes) {
!   Atomic::add(&_used, bytes, memory_order_relaxed);
  }
  
! void ShenandoahHeap::set_used(size_t bytes) {
!   Atomic::store(&_used, bytes);
  }
  
! void ShenandoahHeap::decrease_used(size_t bytes) {
!   assert(used() >= bytes, "never decrease heap size by more than we've left");
!   Atomic::sub(&_used, bytes, memory_order_relaxed);
  }
  
! void ShenandoahHeap::increase_allocated(size_t bytes) {
!   Atomic::add(&_bytes_allocated_since_gc_start, bytes, memory_order_relaxed);
  }
  
! void ShenandoahHeap::notify_mutator_alloc_words(size_t words, bool waste) {
!   size_t bytes = words * HeapWordSize;
!   if (!waste) {
!     increase_used(bytes);
    }
!   increase_allocated(bytes);
    if (ShenandoahPacing) {
      control_thread()->pacing_notify_alloc(words);
!     if (waste) {
!       pacer()->claim_for_alloc(words, true);
      }
    }
  }
  
  size_t ShenandoahHeap::capacity() const {
--- 685,113 ---
    if (_safepoint_workers != nullptr) {
      _safepoint_workers->threads_do(&init_gclabs);
      _safepoint_workers->set_initialize_gclab();
    }
  
    JFR_ONLY(ShenandoahJFRSupport::register_jfr_type_serializers();)
  }
  
+ ShenandoahHeuristics* ShenandoahHeap::heuristics() {
+   return _global_generation->heuristics();
+ }
+ 
  size_t ShenandoahHeap::used() const {
!   return global_generation()->used();
  }
  
  size_t ShenandoahHeap::committed() const {
    return Atomic::load(&_committed);
  }
  
  void ShenandoahHeap::increase_committed(size_t bytes) {
    shenandoah_assert_heaplocked_or_safepoint();
    _committed += bytes;
  }
  
  void ShenandoahHeap::decrease_committed(size_t bytes) {
    shenandoah_assert_heaplocked_or_safepoint();
    _committed -= bytes;
  }
  
! // For tracking usage based on allocations, it should be the case that:
! // * The sum of regions::used == heap::used
+ // * The sum of a generation's regions::used == generation::used
+ // * The sum of a generation's humongous regions::free == generation::humongous_waste
+ // These invariants are checked by the verifier on GC safepoints.
+ //
+ // Additional notes:
+ // * When a mutator's allocation request causes a region to be retired, the
+ //   free memory left in that region is considered waste. It does not contribute
+ //   to the usage, but it _does_ contribute to allocation rate.
+ // * The bottom of a PLAB must be aligned on card size. In some cases this will
+ //   require padding in front of the PLAB (a filler object). Because this padding
+ //   is included in the region's used memory we include the padding in the usage
+ //   accounting as waste.
+ // * Mutator allocations are used to compute an allocation rate. They are also
+ //   sent to the Pacer for those purposes.
+ // * There are three sources of waste:
+ //  1. The padding used to align a PLAB on card size
+ //  2. Region's free is less than minimum TLAB size and is retired
+ //  3. The unused portion of memory in the last region of a humongous object
+ void ShenandoahHeap::increase_used(const ShenandoahAllocRequest& req) {
+   size_t actual_bytes = req.actual_size() * HeapWordSize;
+   size_t wasted_bytes = req.waste() * HeapWordSize;
+   ShenandoahGeneration* generation = generation_for(req.affiliation());
+ 
+   if (req.is_gc_alloc()) {
+     assert(wasted_bytes == 0 || req.type() == ShenandoahAllocRequest::_alloc_plab, "Only PLABs have waste");
+     increase_used(generation, actual_bytes + wasted_bytes);
+   } else {
+     assert(req.is_mutator_alloc(), "Expected mutator alloc here");
+     // padding and actual size both count towards allocation counter
+     generation->increase_allocated(actual_bytes + wasted_bytes);
+ 
+     // only actual size counts toward usage for mutator allocations
+     increase_used(generation, actual_bytes);
+ 
+     // notify pacer of both actual size and waste
+     notify_mutator_alloc_words(req.actual_size(), req.waste());
+ 
+     if (wasted_bytes > 0 && req.actual_size() > ShenandoahHeapRegion::humongous_threshold_words()) {
+       increase_humongous_waste(generation,wasted_bytes);
+     }
+   }
  }
  
! void ShenandoahHeap::increase_humongous_waste(ShenandoahGeneration* generation, size_t bytes) {
!   generation->increase_humongous_waste(bytes);
+   if (!generation->is_global()) {
+     global_generation()->increase_humongous_waste(bytes);
+   }
  }
  
! void ShenandoahHeap::decrease_humongous_waste(ShenandoahGeneration* generation, size_t bytes) {
!   generation->decrease_humongous_waste(bytes);
!   if (!generation->is_global()) {
+     global_generation()->decrease_humongous_waste(bytes);
+   }
  }
  
! void ShenandoahHeap::increase_used(ShenandoahGeneration* generation, size_t bytes) {
!   generation->increase_used(bytes);
+   if (!generation->is_global()) {
+     global_generation()->increase_used(bytes);
+   }
  }
  
! void ShenandoahHeap::decrease_used(ShenandoahGeneration* generation, size_t bytes) {
!   generation->decrease_used(bytes);
!   if (!generation->is_global()) {
!     global_generation()->decrease_used(bytes);
    }
! }
+ 
+ void ShenandoahHeap::notify_mutator_alloc_words(size_t words, size_t waste) {
    if (ShenandoahPacing) {
      control_thread()->pacing_notify_alloc(words);
!     if (waste > 0) {
!       pacer()->claim_for_alloc(waste, true);
      }
    }
  }
  
  size_t ShenandoahHeap::capacity() const {

*** 746,16 ***
  
  size_t ShenandoahHeap::initial_capacity() const {
    return _initial_size;
  }
  
- bool ShenandoahHeap::is_in(const void* p) const {
-   HeapWord* heap_base = (HeapWord*) base();
-   HeapWord* last_region_end = heap_base + ShenandoahHeapRegion::region_size_words() * num_regions();
-   return p >= heap_base && p < last_region_end;
- }
- 
  void ShenandoahHeap::maybe_uncommit(double shrink_before, size_t shrink_until) {
    assert (ShenandoahUncommit, "should be enabled");
  
    // Determine if there is work to do. This avoids taking heap lock if there is
    // no work available, avoids spamming logs with superfluous logging messages,
--- 823,10 ---

*** 831,13 ***
  
  void ShenandoahHeap::notify_heap_changed() {
    // Update monitoring counters when we took a new region. This amortizes the
    // update costs on slow path.
    monitoring_support()->notify_heap_changed();
! 
-   // This is called from allocation path, and thus should be fast.
-   _heap_changed.try_set();
  }
  
  void ShenandoahHeap::set_forced_counters_update(bool value) {
    monitoring_support()->set_forced_counters_update(value);
  }
--- 902,11 ---
  
  void ShenandoahHeap::notify_heap_changed() {
    // Update monitoring counters when we took a new region. This amortizes the
    // update costs on slow path.
    monitoring_support()->notify_heap_changed();
!   _heap_changed.set();
  }
  
  void ShenandoahHeap::set_forced_counters_update(bool value) {
    monitoring_support()->set_forced_counters_update(value);
  }

*** 850,10 ***
--- 919,18 ---
    // New object should fit the GCLAB size
    size_t min_size = MAX2(size, PLAB::min_size());
  
    // Figure out size of new GCLAB, looking back at heuristics. Expand aggressively.
    size_t new_size = ShenandoahThreadLocalData::gclab_size(thread) * 2;
+ 
+   // Limit growth of GCLABs to ShenandoahMaxEvacLABRatio * the minimum size.  This enables more equitable distribution of
+   // available evacuation buidget between the many threads that are coordinating in the evacuation effort.
+   if (ShenandoahMaxEvacLABRatio > 0) {
+     log_debug(gc, free)("Allocate new gclab: " SIZE_FORMAT ", " SIZE_FORMAT, new_size, PLAB::min_size() * ShenandoahMaxEvacLABRatio);
+     new_size = MIN2(new_size, PLAB::min_size() * ShenandoahMaxEvacLABRatio);
+   }
+ 
    new_size = MIN2(new_size, PLAB::max_size());
    new_size = MAX2(new_size, PLAB::min_size());
  
    // Record new heuristic value even if we take any shortcut. This captures
    // the case when moderately-sized objects always take a shortcut. At some point,

*** 861,10 ***
--- 938,11 ---
    ShenandoahThreadLocalData::set_gclab_size(thread, new_size);
  
    if (new_size < size) {
      // New size still does not fit the object. Fall back to shared allocation.
      // This avoids retiring perfectly good GCLABs, when we encounter a large object.
+     log_debug(gc, free)("New gclab size (" SIZE_FORMAT ") is too small for " SIZE_FORMAT, new_size, size);
      return nullptr;
    }
  
    // Retire current GCLAB, and allocate a new one.
    PLAB* gclab = ShenandoahThreadLocalData::gclab(thread);

*** 890,10 ***
--- 968,31 ---
    }
    gclab->set_buf(gclab_buf, actual_size);
    return gclab->allocate(size);
  }
  
+ void ShenandoahHeap::cancel_old_gc() {
+   shenandoah_assert_safepoint();
+   assert(old_generation() != nullptr, "Should only have mixed collections in generation mode.");
+   if (old_generation()->state() == ShenandoahOldGeneration::WAITING_FOR_BOOTSTRAP) {
+ #ifdef ASSERT
+     old_generation()->validate_waiting_for_bootstrap();
+ #endif
+   } else {
+     log_info(gc)("Terminating old gc cycle.");
+     // Stop marking
+     old_generation()->cancel_marking();
+     // Stop tracking old regions
+     old_generation()->abandon_collection_candidates();
+     // Remove old generation access to young generation mark queues
+     young_generation()->set_old_gen_task_queues(nullptr);
+     // Transition to IDLE now.
+     old_generation()->transition_to(ShenandoahOldGeneration::WAITING_FOR_BOOTSTRAP);
+   }
+ }
+ 
+ // Called from stubs in JIT code or interpreter
  HeapWord* ShenandoahHeap::allocate_new_tlab(size_t min_size,
                                              size_t requested_size,
                                              size_t* actual_size) {
    ShenandoahAllocRequest req = ShenandoahAllocRequest::for_tlab(min_size, requested_size);
    HeapWord* res = allocate_memory(req);

*** 916,10 ***
--- 1015,13 ---
      *actual_size = 0;
    }
    return res;
  }
  
+ 
+ // is_promotion is true iff this allocation is known for sure to hold the result of young-gen evacuation
+ // to old-gen.  plab allocates are not known as such, since they may hold old-gen evacuations.
  HeapWord* ShenandoahHeap::allocate_memory(ShenandoahAllocRequest& req) {
    intptr_t pacer_epoch = 0;
    bool in_new_region = false;
    HeapWord* result = nullptr;
  

*** 953,19 ***
      // strategy is to try again, as long as GC makes progress (or until at least
      // one full GC has completed).
      size_t original_count = shenandoah_policy()->full_gc_count();
      while (result == nullptr
          && (get_gc_no_progress_count() == 0 || original_count == shenandoah_policy()->full_gc_count())) {
!       control_thread()->handle_alloc_failure(req);
        result = allocate_memory_under_lock(req, in_new_region);
      }
  
      if (log_is_enabled(Debug, gc, alloc)) {
        ResourceMark rm;
        log_debug(gc, alloc)("Thread: %s, Result: " PTR_FORMAT ", Request: %s, Size: " SIZE_FORMAT ", Original: " SIZE_FORMAT ", Latest: " SIZE_FORMAT,
                             Thread::current()->name(), p2i(result), req.type_string(), req.size(), original_count, get_gc_no_progress_count());
      }
    } else {
      assert(req.is_gc_alloc(), "Can only accept GC allocs here");
      result = allocate_memory_under_lock(req, in_new_region);
      // Do not call handle_alloc_failure() here, because we cannot block.
      // The allocation failure would be handled by the LRB slowpath with handle_alloc_failure_evac().
--- 1055,20 ---
      // strategy is to try again, as long as GC makes progress (or until at least
      // one full GC has completed).
      size_t original_count = shenandoah_policy()->full_gc_count();
      while (result == nullptr
          && (get_gc_no_progress_count() == 0 || original_count == shenandoah_policy()->full_gc_count())) {
!       control_thread()->handle_alloc_failure(req, true);
        result = allocate_memory_under_lock(req, in_new_region);
      }
  
      if (log_is_enabled(Debug, gc, alloc)) {
        ResourceMark rm;
        log_debug(gc, alloc)("Thread: %s, Result: " PTR_FORMAT ", Request: %s, Size: " SIZE_FORMAT ", Original: " SIZE_FORMAT ", Latest: " SIZE_FORMAT,
                             Thread::current()->name(), p2i(result), req.type_string(), req.size(), original_count, get_gc_no_progress_count());
      }
+ 
    } else {
      assert(req.is_gc_alloc(), "Can only accept GC allocs here");
      result = allocate_memory_under_lock(req, in_new_region);
      // Do not call handle_alloc_failure() here, because we cannot block.
      // The allocation failure would be handled by the LRB slowpath with handle_alloc_failure_evac().

*** 973,42 ***
  
    if (in_new_region) {
      notify_heap_changed();
    }
  
    if (result != nullptr) {
      size_t requested = req.size();
      size_t actual = req.actual_size();
  
      assert (req.is_lab_alloc() || (requested == actual),
              "Only LAB allocations are elastic: %s, requested = " SIZE_FORMAT ", actual = " SIZE_FORMAT,
              ShenandoahAllocRequest::alloc_type_to_string(req.type()), requested, actual);
  
      if (req.is_mutator_alloc()) {
-       notify_mutator_alloc_words(actual, false);
- 
        // If we requested more than we were granted, give the rest back to pacer.
        // This only matters if we are in the same pacing epoch: do not try to unpace
        // over the budget for the other phase.
        if (ShenandoahPacing && (pacer_epoch > 0) && (requested > actual)) {
          pacer()->unpace_for_alloc(pacer_epoch, requested - actual);
        }
-     } else {
-       increase_used(actual*HeapWordSize);
      }
    }
  
    return result;
  }
  
  HeapWord* ShenandoahHeap::allocate_memory_under_lock(ShenandoahAllocRequest& req, bool& in_new_region) {
!   // If we are dealing with mutator allocation, then we may need to block for safepoint.
!   // We cannot block for safepoint for GC allocations, because there is a high chance
!   // we are already running at safepoint or from stack watermark machinery, and we cannot
!   // block again.
!   ShenandoahHeapLocker locker(lock(), req.is_mutator_alloc());
!   return _free_set->allocate(req, in_new_region);
  }
  
  HeapWord* ShenandoahHeap::mem_allocate(size_t size,
                                          bool*  gc_overhead_limit_was_exceeded) {
    ShenandoahAllocRequest req = ShenandoahAllocRequest::for_shared(size);
--- 1076,224 ---
  
    if (in_new_region) {
      notify_heap_changed();
    }
  
+   if (result == nullptr) {
+     req.set_actual_size(0);
+   }
+ 
+   // This is called regardless of the outcome of the allocation to account
+   // for any waste created by retiring regions with this request.
+   increase_used(req);
+ 
    if (result != nullptr) {
      size_t requested = req.size();
      size_t actual = req.actual_size();
  
      assert (req.is_lab_alloc() || (requested == actual),
              "Only LAB allocations are elastic: %s, requested = " SIZE_FORMAT ", actual = " SIZE_FORMAT,
              ShenandoahAllocRequest::alloc_type_to_string(req.type()), requested, actual);
  
      if (req.is_mutator_alloc()) {
        // If we requested more than we were granted, give the rest back to pacer.
        // This only matters if we are in the same pacing epoch: do not try to unpace
        // over the budget for the other phase.
        if (ShenandoahPacing && (pacer_epoch > 0) && (requested > actual)) {
          pacer()->unpace_for_alloc(pacer_epoch, requested - actual);
        }
      }
    }
  
    return result;
  }
  
  HeapWord* ShenandoahHeap::allocate_memory_under_lock(ShenandoahAllocRequest& req, bool& in_new_region) {
!   bool try_smaller_lab_size = false;
!   size_t smaller_lab_size;
!   {
!     // promotion_eligible pertains only to PLAB allocations, denoting that the PLAB is allowed to allocate for promotions.
!     bool promotion_eligible = false;
!     bool allow_allocation = true;
+     bool plab_alloc = false;
+     size_t requested_bytes = req.size() * HeapWordSize;
+     HeapWord* result = nullptr;
+ 
+     // If we are dealing with mutator allocation, then we may need to block for safepoint.
+     // We cannot block for safepoint for GC allocations, because there is a high chance
+     // we are already running at safepoint or from stack watermark machinery, and we cannot
+     // block again.
+     ShenandoahHeapLocker locker(lock(), req.is_mutator_alloc());
+     Thread* thread = Thread::current();
+ 
+     if (mode()->is_generational()) {
+       if (req.affiliation() == YOUNG_GENERATION) {
+         if (req.is_mutator_alloc()) {
+           size_t young_words_available = young_generation()->available() / HeapWordSize;
+           if (req.is_lab_alloc() && (req.min_size() < young_words_available)) {
+             // Allow ourselves to try a smaller lab size even if requested_bytes <= young_available.  We may need a smaller
+             // lab size because young memory has become too fragmented.
+             try_smaller_lab_size = true;
+             smaller_lab_size = (young_words_available < req.size())? young_words_available: req.size();
+           } else if (req.size() > young_words_available) {
+             // Can't allocate because even min_size() is larger than remaining young_available
+             log_info(gc, ergo)("Unable to shrink %s alloc request of minimum size: " SIZE_FORMAT
+                                ", young words available: " SIZE_FORMAT, req.type_string(),
+                                HeapWordSize * (req.is_lab_alloc()? req.min_size(): req.size()), young_words_available);
+             return nullptr;
+           }
+         }
+       } else {                    // reg.affiliation() == OLD_GENERATION
+         assert(req.type() != ShenandoahAllocRequest::_alloc_gclab, "GCLAB pertains only to young-gen memory");
+         if (req.type() ==  ShenandoahAllocRequest::_alloc_plab) {
+           plab_alloc = true;
+           size_t promotion_avail = old_generation()->get_promoted_reserve();
+           size_t promotion_expended = old_generation()->get_promoted_expended();
+           if (promotion_expended + requested_bytes > promotion_avail) {
+             promotion_avail = 0;
+             if (old_generation()->get_evacuation_reserve() == 0) {
+               // There are no old-gen evacuations in this pass.  There's no value in creating a plab that cannot
+               // be used for promotions.
+               allow_allocation = false;
+             }
+           } else {
+             promotion_avail = promotion_avail - (promotion_expended + requested_bytes);
+             promotion_eligible = true;
+           }
+         } else if (req.is_promotion()) {
+           // This is a shared alloc for promotion
+           size_t promotion_avail = old_generation()->get_promoted_reserve();
+           size_t promotion_expended = old_generation()->get_promoted_expended();
+           if (promotion_expended + requested_bytes > promotion_avail) {
+             promotion_avail = 0;
+           } else {
+             promotion_avail = promotion_avail - (promotion_expended + requested_bytes);
+           }
+           if (promotion_avail == 0) {
+             // We need to reserve the remaining memory for evacuation.  Reject this allocation.  The object will be
+             // evacuated to young-gen memory and promoted during a future GC pass.
+             return nullptr;
+           }
+           // Else, we'll allow the allocation to proceed.  (Since we hold heap lock, the tested condition remains true.)
+         } else {
+           // This is a shared allocation for evacuation.  Memory has already been reserved for this purpose.
+         }
+       }
+     } // This ends the is_generational() block
+ 
+     // First try the original request.  If TLAB request size is greater than available, allocate() will attempt to downsize
+     // request to fit within available memory.
+     result = (allow_allocation)? _free_set->allocate(req, in_new_region): nullptr;
+     if (result != nullptr) {
+       if (req.is_old()) {
+         ShenandoahThreadLocalData::reset_plab_promoted(thread);
+         if (req.is_gc_alloc()) {
+           bool disable_plab_promotions = false;
+           if (req.type() ==  ShenandoahAllocRequest::_alloc_plab) {
+             if (promotion_eligible) {
+               size_t actual_size = req.actual_size() * HeapWordSize;
+               // The actual size of the allocation may be larger than the requested bytes (due to alignment on card boundaries).
+               // If this puts us over our promotion budget, we need to disable future PLAB promotions for this thread.
+               if (old_generation()->get_promoted_expended() + actual_size <= old_generation()->get_promoted_reserve()) {
+                 // Assume the entirety of this PLAB will be used for promotion.  This prevents promotion from overreach.
+                 // When we retire this plab, we'll unexpend what we don't really use.
+                 ShenandoahThreadLocalData::enable_plab_promotions(thread);
+                 old_generation()->expend_promoted(actual_size);
+                 ShenandoahThreadLocalData::set_plab_preallocated_promoted(thread, actual_size);
+               } else {
+                 disable_plab_promotions = true;
+               }
+             } else {
+               disable_plab_promotions = true;
+             }
+             if (disable_plab_promotions) {
+               // Disable promotions in this thread because entirety of this PLAB must be available to hold old-gen evacuations.
+               ShenandoahThreadLocalData::disable_plab_promotions(thread);
+               ShenandoahThreadLocalData::set_plab_preallocated_promoted(thread, 0);
+             }
+           } else if (req.is_promotion()) {
+             // Shared promotion.  Assume size is requested_bytes.
+             old_generation()->expend_promoted(requested_bytes);
+           }
+         }
+ 
+         // Register the newly allocated object while we're holding the global lock since there's no synchronization
+         // built in to the implementation of register_object().  There are potential races when multiple independent
+         // threads are allocating objects, some of which might span the same card region.  For example, consider
+         // a card table's memory region within which three objects are being allocated by three different threads:
+         //
+         // objects being "concurrently" allocated:
+         //    [-----a------][-----b-----][--------------c------------------]
+         //            [---- card table memory range --------------]
+         //
+         // Before any objects are allocated, this card's memory range holds no objects.  Note that allocation of object a
+         //   wants to set the starts-object, first-start, and last-start attributes of the preceding card region.
+         //   allocation of object b wants to set the starts-object, first-start, and last-start attributes of this card region.
+         //   allocation of object c also wants to set the starts-object, first-start, and last-start attributes of this
+         //   card region.
+         //
+         // The thread allocating b and the thread allocating c can "race" in various ways, resulting in confusion, such as
+         // last-start representing object b while first-start represents object c.  This is why we need to require all
+         // register_object() invocations to be "mutually exclusive" with respect to each card's memory range.
+         card_scan()->register_object(result);
+       }
+     } else {
+       // The allocation failed.  If this was a plab allocation, We've already retired it and no longer have a plab.
+       if (req.is_old() && req.is_gc_alloc() && (req.type() == ShenandoahAllocRequest::_alloc_plab)) {
+         // We don't need to disable PLAB promotions because there is no PLAB.  We leave promotions enabled because
+         // this allows the surrounding infrastructure to retry alloc_plab_slow() with a smaller PLAB size.
+         ShenandoahThreadLocalData::set_plab_preallocated_promoted(thread, 0);
+       }
+     }
+     if ((result != nullptr) || !try_smaller_lab_size) {
+       return result;
+     }
+     // else, fall through to try_smaller_lab_size
+   } // This closes the block that holds the heap lock, releasing the lock.
+ 
+   // We failed to allocate the originally requested lab size.  Let's see if we can allocate a smaller lab size.
+   if (req.size() == smaller_lab_size) {
+     // If we were already trying to allocate min size, no value in attempting to repeat the same.  End the recursion.
+     return nullptr;
+   }
+ 
+   // We arrive here if the tlab allocation request can be resized to fit within young_available
+   assert((req.affiliation() == YOUNG_GENERATION) && req.is_lab_alloc() && req.is_mutator_alloc() &&
+          (smaller_lab_size < req.size()), "Only shrink allocation request size for TLAB allocations");
+ 
+   // By convention, ShenandoahAllocationRequest is primarily read-only.  The only mutable instance data is represented by
+   // actual_size(), which is overwritten with the size of the allocaion when the allocation request is satisfied.  We use a
+   // recursive call here rather than introducing new methods to mutate the existing ShenandoahAllocationRequest argument.
+   // Mutation of the existing object might result in astonishing results if calling contexts assume the content of immutable
+   // fields remain constant.  The original TLAB allocation request was for memory that exceeded the current capacity.  We'll
+   // attempt to allocate a smaller TLAB.  If this is successful, we'll update actual_size() of our incoming
+   // ShenandoahAllocRequest.  If the recursive request fails, we'll simply return nullptr.
+ 
+   // Note that we've relinquished the HeapLock and some other thread may perform additional allocation before our recursive
+   // call reacquires the lock.  If that happens, we will need another recursive call to further reduce the size of our request
+   // for each time another thread allocates young memory during the brief intervals that the heap lock is available to
+   // interfering threads.  We expect this interference to be rare.  The recursion bottoms out when young_available is
+   // smaller than req.min_size().  The inner-nested call to allocate_memory_under_lock() uses the same min_size() value
+   // as this call, but it uses a preferred size() that is smaller than our preferred size, and is no larger than what we most
+   // recently saw as the memory currently available within the young generation.
+ 
+   // TODO: At the expense of code clarity, we could rewrite this recursive solution to use iteration.  We need at most one
+   // extra instance of the ShenandoahAllocRequest, which we can re-initialize multiple times inside a loop, with one iteration
+   // of the loop required for each time the existing solution would recurse.  An iterative solution would be more efficient
+   // in CPU time and stack memory utilization.  The expectation is that it is very rare that we would recurse more than once
+   // so making this change is not currently seen as a high priority.
+ 
+   ShenandoahAllocRequest smaller_req = ShenandoahAllocRequest::for_tlab(req.min_size(), smaller_lab_size);
+ 
+   // Note that shrinking the preferred size gets us past the gatekeeper that checks whether there's available memory to
+   // satisfy the allocation request.  The reality is the actual TLAB size is likely to be even smaller, because it will
+   // depend on how much memory is available within mutator regions that are not yet fully used.
+   HeapWord* result = allocate_memory_under_lock(smaller_req, in_new_region);
+   if (result != nullptr) {
+     req.set_actual_size(smaller_req.actual_size());
+   }
+   return result;
  }
  
  HeapWord* ShenandoahHeap::mem_allocate(size_t size,
                                          bool*  gc_overhead_limit_was_exceeded) {
    ShenandoahAllocRequest req = ShenandoahAllocRequest::for_shared(size);

*** 1019,12 ***
                                                               size_t size,
                                                               Metaspace::MetadataType mdtype) {
    MetaWord* result;
  
    // Inform metaspace OOM to GC heuristics if class unloading is possible.
!   if (heuristics()->can_unload_classes()) {
!     ShenandoahHeuristics* h = heuristics();
      h->record_metaspace_oom();
    }
  
    // Expand and retry allocation
    result = loader_data->metaspace_non_null()->expand_and_allocate(size, mdtype);
--- 1304,12 ---
                                                               size_t size,
                                                               Metaspace::MetadataType mdtype) {
    MetaWord* result;
  
    // Inform metaspace OOM to GC heuristics if class unloading is possible.
!   ShenandoahHeuristics* h = global_generation()->heuristics();
!   if (h->can_unload_classes()) {
      h->record_metaspace_oom();
    }
  
    // Expand and retry allocation
    result = loader_data->metaspace_non_null()->expand_and_allocate(size, mdtype);

*** 1113,12 ***
      }
    }
  };
  
  void ShenandoahHeap::evacuate_collection_set(bool concurrent) {
!   ShenandoahEvacuationTask task(this, _collection_set, concurrent);
!   workers()->run_task(&task);
  }
  
  void ShenandoahHeap::trash_cset_regions() {
    ShenandoahHeapLocker locker(lock());
  
--- 1398,119 ---
      }
    }
  };
  
  void ShenandoahHeap::evacuate_collection_set(bool concurrent) {
!   if (mode()->is_generational()) {
!     ShenandoahRegionIterator regions;
+     ShenandoahGenerationalEvacuationTask task(ShenandoahGenerationalHeap::heap(), &regions, concurrent);
+     workers()->run_task(&task);
+   } else {
+     ShenandoahEvacuationTask task(this, _collection_set, concurrent);
+     workers()->run_task(&task);
+   }
+ }
+ 
+ oop ShenandoahHeap::evacuate_object(oop p, Thread* thread) {
+   assert(thread == Thread::current(), "Expected thread parameter to be current thread.");
+   if (ShenandoahThreadLocalData::is_oom_during_evac(thread)) {
+     // This thread went through the OOM during evac protocol. It is safe to return
+     // the forward pointer. It must not attempt to evacuate any other objects.
+     return ShenandoahBarrierSet::resolve_forwarded(p);
+   }
+ 
+   assert(ShenandoahThreadLocalData::is_evac_allowed(thread), "must be enclosed in oom-evac scope");
+ 
+   ShenandoahHeapRegion* r = heap_region_containing(p);
+   assert(!r->is_humongous(), "never evacuate humongous objects");
+ 
+   ShenandoahAffiliation target_gen = r->affiliation();
+   return try_evacuate_object(p, thread, r, target_gen);
+ }
+ 
+ oop ShenandoahHeap::try_evacuate_object(oop p, Thread* thread, ShenandoahHeapRegion* from_region,
+                                                ShenandoahAffiliation target_gen) {
+   assert(target_gen == YOUNG_GENERATION, "Only expect evacuations to young in this mode");
+   assert(from_region->is_young(), "Only expect evacuations from young in this mode");
+   bool alloc_from_lab = true;
+   HeapWord* copy = nullptr;
+   size_t size = p->size();
+ 
+ #ifdef ASSERT
+   if (ShenandoahOOMDuringEvacALot &&
+       (os::random() & 1) == 0) { // Simulate OOM every ~2nd slow-path call
+     copy = nullptr;
+   } else {
+ #endif
+     if (UseTLAB) {
+       copy = allocate_from_gclab(thread, size);
+       if ((copy == nullptr) && (size < ShenandoahThreadLocalData::gclab_size(thread))) {
+         // GCLAB allocation failed because we are bumping up against the limit on young evacuation reserve.  Try resetting
+         // the desired GCLAB size and retry GCLAB allocation to avoid cascading of shared memory allocations.
+         // TODO: is this right? using PLAB::min_size() here for gc lab size?
+         ShenandoahThreadLocalData::set_gclab_size(thread, PLAB::min_size());
+         copy = allocate_from_gclab(thread, size);
+         // If we still get nullptr, we'll try a shared allocation below.
+       }
+     }
+ 
+     if (copy == nullptr) {
+       // If we failed to allocate in LAB, we'll try a shared allocation.
+       ShenandoahAllocRequest req = ShenandoahAllocRequest::for_shared_gc(size, target_gen);
+       copy = allocate_memory(req);
+       alloc_from_lab = false;
+     }
+ #ifdef ASSERT
+   }
+ #endif
+ 
+   if (copy == nullptr) {
+     control_thread()->handle_alloc_failure_evac(size);
+ 
+     _oom_evac_handler.handle_out_of_memory_during_evacuation();
+ 
+     return ShenandoahBarrierSet::resolve_forwarded(p);
+   }
+ 
+   // Copy the object:
+   _evac_tracker->begin_evacuation(thread, size * HeapWordSize);
+   Copy::aligned_disjoint_words(cast_from_oop<HeapWord*>(p), copy, size);
+ 
+   oop copy_val = cast_to_oop(copy);
+ 
+   // Try to install the new forwarding pointer.
+   ContinuationGCSupport::relativize_stack_chunk(copy_val);
+ 
+   oop result = ShenandoahForwarding::try_update_forwardee(p, copy_val);
+   if (result == copy_val) {
+     // Successfully evacuated. Our copy is now the public one!
+     _evac_tracker->end_evacuation(thread, size * HeapWordSize);
+     shenandoah_assert_correct(nullptr, copy_val);
+     return copy_val;
+   }  else {
+     // Failed to evacuate. We need to deal with the object that is left behind. Since this
+     // new allocation is certainly after TAMS, it will be considered live in the next cycle.
+     // But if it happens to contain references to evacuated regions, those references would
+     // not get updated for this stale copy during this cycle, and we will crash while scanning
+     // it the next cycle.
+     if (alloc_from_lab) {
+       // For LAB allocations, it is enough to rollback the allocation ptr. Either the next
+       // object will overwrite this stale copy, or the filler object on LAB retirement will
+       // do this.
+       ShenandoahThreadLocalData::gclab(thread)->undo_allocation(copy, size);
+     } else {
+       // For non-LAB allocations, we have no way to retract the allocation, and
+       // have to explicitly overwrite the copy with the filler object. With that overwrite,
+       // we have to keep the fwdptr initialized and pointing to our (stale) copy.
+       assert(size >= ShenandoahHeap::min_fill_size(), "previously allocated object known to be larger than min_size");
+       fill_with_object(copy, size);
+       shenandoah_assert_correct(nullptr, copy_val);
+       // For non-LAB allocations, the object has already been registered
+     }
+     shenandoah_assert_correct(nullptr, result);
+     return result;
+   }
  }
  
  void ShenandoahHeap::trash_cset_regions() {
    ShenandoahHeapLocker locker(lock());
  

*** 1144,11 ***
    for (size_t i = 0; i < num_regions(); i++) {
      get_region(i)->print_on(st);
    }
  }
  
! void ShenandoahHeap::trash_humongous_region_at(ShenandoahHeapRegion* start) {
    assert(start->is_humongous_start(), "reclaim regions starting with the first one");
  
    oop humongous_obj = cast_to_oop(start->bottom());
    size_t size = humongous_obj->size();
    size_t required_regions = ShenandoahHeapRegion::required_regions(size * HeapWordSize);
--- 1536,11 ---
    for (size_t i = 0; i < num_regions(); i++) {
      get_region(i)->print_on(st);
    }
  }
  
! size_t ShenandoahHeap::trash_humongous_region_at(ShenandoahHeapRegion* start) {
    assert(start->is_humongous_start(), "reclaim regions starting with the first one");
  
    oop humongous_obj = cast_to_oop(start->bottom());
    size_t size = humongous_obj->size();
    size_t required_regions = ShenandoahHeapRegion::required_regions(size * HeapWordSize);

*** 1164,19 ***
--- 1556,26 ---
      assert(region->is_humongous(), "expect correct humongous start or continuation");
      assert(!region->is_cset(), "Humongous region should not be in collection set");
  
      region->make_trash_immediate();
    }
+   return required_regions;
  }
  
  class ShenandoahCheckCleanGCLABClosure : public ThreadClosure {
  public:
    ShenandoahCheckCleanGCLABClosure() {}
    void do_thread(Thread* thread) {
      PLAB* gclab = ShenandoahThreadLocalData::gclab(thread);
      assert(gclab != nullptr, "GCLAB should be initialized for %s", thread->name());
      assert(gclab->words_remaining() == 0, "GCLAB should not need retirement");
+ 
+     if (ShenandoahHeap::heap()->mode()->is_generational()) {
+       PLAB* plab = ShenandoahThreadLocalData::plab(thread);
+       assert(plab != nullptr, "PLAB should be initialized for %s", thread->name());
+       assert(plab->words_remaining() == 0, "PLAB should not need retirement");
+     }
    }
  };
  
  class ShenandoahRetireGCLABClosure : public ThreadClosure {
  private:

*** 1188,10 ***
--- 1587,23 ---
      assert(gclab != nullptr, "GCLAB should be initialized for %s", thread->name());
      gclab->retire();
      if (_resize && ShenandoahThreadLocalData::gclab_size(thread) > 0) {
        ShenandoahThreadLocalData::set_gclab_size(thread, 0);
      }
+ 
+     if (ShenandoahHeap::heap()->mode()->is_generational()) {
+       PLAB* plab = ShenandoahThreadLocalData::plab(thread);
+       assert(plab != nullptr, "PLAB should be initialized for %s", thread->name());
+ 
+       // There are two reasons to retire all plabs between old-gen evacuation passes.
+       //  1. We need to make the plab memory parsable by remembered-set scanning.
+       //  2. We need to establish a trustworthy UpdateWaterMark value within each old-gen heap region
+       ShenandoahGenerationalHeap::heap()->retire_plab(plab, thread);
+       if (_resize && ShenandoahThreadLocalData::plab_size(thread) > 0) {
+         ShenandoahThreadLocalData::set_plab_size(thread, 0);
+       }
+     }
    }
  };
  
  void ShenandoahHeap::labs_make_parsable() {
    assert(UseTLAB, "Only call with UseTLAB");

*** 1247,12 ***
    }
  }
  
  // Returns size in bytes
  size_t ShenandoahHeap::unsafe_max_tlab_alloc(Thread *thread) const {
!   // Return the max allowed size, and let the allocation path
-   // figure out the safe size for current allocation.
    return ShenandoahHeapRegion::max_tlab_size_bytes();
  }
  
  size_t ShenandoahHeap::max_tlab_size() const {
    // Returns size in words
--- 1659,11 ---
    }
  }
  
  // Returns size in bytes
  size_t ShenandoahHeap::unsafe_max_tlab_alloc(Thread *thread) const {
!   // Return the max allowed size, and let the allocation path figure out the safe size for current allocation.
    return ShenandoahHeapRegion::max_tlab_size_bytes();
  }
  
  size_t ShenandoahHeap::max_tlab_size() const {
    // Returns size in words

*** 1289,11 ***
      labs_make_parsable();
    }
  }
  
  void ShenandoahHeap::gc_threads_do(ThreadClosure* tcl) const {
!   tcl->do_thread(_control_thread);
    workers()->threads_do(tcl);
    if (_safepoint_workers != nullptr) {
      _safepoint_workers->threads_do(tcl);
    }
  }
--- 1700,18 ---
      labs_make_parsable();
    }
  }
  
  void ShenandoahHeap::gc_threads_do(ThreadClosure* tcl) const {
!   if (_shenandoah_policy->is_at_shutdown()) {
+     return;
+   }
+ 
+   if (_control_thread != nullptr) {
+     tcl->do_thread(_control_thread);
+   }
+ 
    workers()->threads_do(tcl);
    if (_safepoint_workers != nullptr) {
      _safepoint_workers->threads_do(tcl);
    }
  }

*** 1309,15 ***
--- 1727,38 ---
      ls.cr();
      ls.cr();
  
      shenandoah_policy()->print_gc_stats(&ls);
  
+     ls.cr();
+ 
+     evac_tracker()->print_global_on(&ls);
+ 
      ls.cr();
      ls.cr();
    }
  }
  
+ void ShenandoahHeap::on_cycle_start(GCCause::Cause cause, ShenandoahGeneration* generation) {
+   shenandoah_policy()->record_collection_cause(cause);
+ 
+   set_gc_cause(cause);
+   set_gc_generation(generation);
+ 
+   generation->heuristics()->record_cycle_start();
+ }
+ 
+ void ShenandoahHeap::on_cycle_end(ShenandoahGeneration* generation) {
+   generation->heuristics()->record_cycle_end();
+   if (mode()->is_generational() && generation->is_global()) {
+     // If we just completed a GLOBAL GC, claim credit for completion of young-gen and old-gen GC as well
+     young_generation()->heuristics()->record_cycle_end();
+     old_generation()->heuristics()->record_cycle_end();
+   }
+   set_gc_cause(GCCause::_no_gc);
+ }
+ 
  void ShenandoahHeap::verify(VerifyOption vo) {
    if (ShenandoahSafepoint::is_at_shenandoah_safepoint()) {
      if (ShenandoahVerify) {
        verifier()->verify_generic(vo);
      } else {

*** 1635,33 ***
    } else {
      heap_region_iterate(blk);
    }
  }
  
- class ShenandoahInitMarkUpdateRegionStateClosure : public ShenandoahHeapRegionClosure {
- private:
-   ShenandoahMarkingContext* const _ctx;
- public:
-   ShenandoahInitMarkUpdateRegionStateClosure() : _ctx(ShenandoahHeap::heap()->marking_context()) {}
- 
-   void heap_region_do(ShenandoahHeapRegion* r) {
-     assert(!r->has_live(), "Region " SIZE_FORMAT " should have no live data", r->index());
-     if (r->is_active()) {
-       // Check if region needs updating its TAMS. We have updated it already during concurrent
-       // reset, so it is very likely we don't need to do another write here.
-       if (_ctx->top_at_mark_start(r) != r->top()) {
-         _ctx->capture_top_at_mark_start(r);
-       }
-     } else {
-       assert(_ctx->top_at_mark_start(r) == r->top(),
-              "Region " SIZE_FORMAT " should already have correct TAMS", r->index());
-     }
-   }
- 
-   bool is_thread_safe() { return true; }
- };
- 
  class ShenandoahRendezvousClosure : public HandshakeClosure {
  public:
    inline ShenandoahRendezvousClosure() : HandshakeClosure("ShenandoahRendezvous") {}
    inline void do_thread(Thread* thread) {}
  };
--- 2076,10 ---

*** 1673,120 ***
  
  void ShenandoahHeap::recycle_trash() {
    free_set()->recycle_trash();
  }
  
- class ShenandoahResetUpdateRegionStateClosure : public ShenandoahHeapRegionClosure {
- private:
-   ShenandoahMarkingContext* const _ctx;
- public:
-   ShenandoahResetUpdateRegionStateClosure() : _ctx(ShenandoahHeap::heap()->marking_context()) {}
- 
-   void heap_region_do(ShenandoahHeapRegion* r) {
-     if (r->is_active()) {
-       // Reset live data and set TAMS optimistically. We would recheck these under the pause
-       // anyway to capture any updates that happened since now.
-       r->clear_live_data();
-       _ctx->capture_top_at_mark_start(r);
-     }
-   }
- 
-   bool is_thread_safe() { return true; }
- };
- 
- void ShenandoahHeap::prepare_gc() {
-   reset_mark_bitmap();
- 
-   ShenandoahResetUpdateRegionStateClosure cl;
-   parallel_heap_region_iterate(&cl);
- }
- 
- class ShenandoahFinalMarkUpdateRegionStateClosure : public ShenandoahHeapRegionClosure {
- private:
-   ShenandoahMarkingContext* const _ctx;
-   ShenandoahHeapLock* const _lock;
- 
- public:
-   ShenandoahFinalMarkUpdateRegionStateClosure() :
-     _ctx(ShenandoahHeap::heap()->complete_marking_context()), _lock(ShenandoahHeap::heap()->lock()) {}
- 
-   void heap_region_do(ShenandoahHeapRegion* r) {
-     if (r->is_active()) {
-       // All allocations past TAMS are implicitly live, adjust the region data.
-       // Bitmaps/TAMS are swapped at this point, so we need to poll complete bitmap.
-       HeapWord *tams = _ctx->top_at_mark_start(r);
-       HeapWord *top = r->top();
-       if (top > tams) {
-         r->increase_live_data_alloc_words(pointer_delta(top, tams));
-       }
- 
-       // We are about to select the collection set, make sure it knows about
-       // current pinning status. Also, this allows trashing more regions that
-       // now have their pinning status dropped.
-       if (r->is_pinned()) {
-         if (r->pin_count() == 0) {
-           ShenandoahHeapLocker locker(_lock);
-           r->make_unpinned();
-         }
-       } else {
-         if (r->pin_count() > 0) {
-           ShenandoahHeapLocker locker(_lock);
-           r->make_pinned();
-         }
-       }
- 
-       // Remember limit for updating refs. It's guaranteed that we get no
-       // from-space-refs written from here on.
-       r->set_update_watermark_at_safepoint(r->top());
-     } else {
-       assert(!r->has_live(), "Region " SIZE_FORMAT " should have no live data", r->index());
-       assert(_ctx->top_at_mark_start(r) == r->top(),
-              "Region " SIZE_FORMAT " should have correct TAMS", r->index());
-     }
-   }
- 
-   bool is_thread_safe() { return true; }
- };
- 
- void ShenandoahHeap::prepare_regions_and_collection_set(bool concurrent) {
-   assert(!is_full_gc_in_progress(), "Only for concurrent and degenerated GC");
-   {
-     ShenandoahGCPhase phase(concurrent ? ShenandoahPhaseTimings::final_update_region_states :
-                                          ShenandoahPhaseTimings::degen_gc_final_update_region_states);
-     ShenandoahFinalMarkUpdateRegionStateClosure cl;
-     parallel_heap_region_iterate(&cl);
- 
-     assert_pinned_region_status();
-   }
- 
-   {
-     ShenandoahGCPhase phase(concurrent ? ShenandoahPhaseTimings::choose_cset :
-                                          ShenandoahPhaseTimings::degen_gc_choose_cset);
-     ShenandoahHeapLocker locker(lock());
-     _collection_set->clear();
-     heuristics()->choose_collection_set(_collection_set);
-   }
- 
-   {
-     ShenandoahGCPhase phase(concurrent ? ShenandoahPhaseTimings::final_rebuild_freeset :
-                                          ShenandoahPhaseTimings::degen_gc_final_rebuild_freeset);
-     ShenandoahHeapLocker locker(lock());
-     _free_set->rebuild();
-   }
- }
- 
  void ShenandoahHeap::do_class_unloading() {
    _unloader.unload();
  }
  
  void ShenandoahHeap::stw_weak_refs(bool full_gc) {
    // Weak refs processing
    ShenandoahPhaseTimings::Phase phase = full_gc ? ShenandoahPhaseTimings::full_gc_weakrefs
                                                  : ShenandoahPhaseTimings::degen_gc_weakrefs;
    ShenandoahTimingsTracker t(phase);
    ShenandoahGCWorkerPhase worker_phase(phase);
!   ref_processor()->process_references(phase, workers(), false /* concurrent */);
  }
  
  void ShenandoahHeap::prepare_update_heap_references(bool concurrent) {
    assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "must be at safepoint");
  
--- 2091,21 ---
  
  void ShenandoahHeap::recycle_trash() {
    free_set()->recycle_trash();
  }
  
  void ShenandoahHeap::do_class_unloading() {
    _unloader.unload();
  }
  
  void ShenandoahHeap::stw_weak_refs(bool full_gc) {
    // Weak refs processing
    ShenandoahPhaseTimings::Phase phase = full_gc ? ShenandoahPhaseTimings::full_gc_weakrefs
                                                  : ShenandoahPhaseTimings::degen_gc_weakrefs;
    ShenandoahTimingsTracker t(phase);
    ShenandoahGCWorkerPhase worker_phase(phase);
!   active_generation()->ref_processor()->process_references(phase, workers(), false /* concurrent */);
  }
  
  void ShenandoahHeap::prepare_update_heap_references(bool concurrent) {
    assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "must be at safepoint");
  

*** 1818,14 ***
    assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at Shenandoah safepoint");
    _gc_state.set_cond(mask, value);
    _gc_state_changed = true;
  }
  
! void ShenandoahHeap::set_concurrent_mark_in_progress(bool in_progress) {
!   assert(!has_forwarded_objects(), "Not expected before/after mark phase");
!   set_gc_state(MARKING, in_progress);
!   ShenandoahBarrierSet::satb_mark_queue_set().set_active_all_threads(in_progress, !in_progress);
  }
  
  void ShenandoahHeap::set_evacuation_in_progress(bool in_progress) {
    assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Only call this at safepoint");
    set_gc_state(EVACUATION, in_progress);
--- 2137,66 ---
    assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at Shenandoah safepoint");
    _gc_state.set_cond(mask, value);
    _gc_state_changed = true;
  }
  
! void ShenandoahHeap::set_concurrent_young_mark_in_progress(bool in_progress) {
!   uint mask;
!   assert(!has_forwarded_objects(), "Young marking is not concurrent with evacuation");
!   if (!in_progress && is_concurrent_old_mark_in_progress()) {
+     assert(mode()->is_generational(), "Only generational GC has old marking");
+     assert(_gc_state.is_set(MARKING), "concurrent_old_marking_in_progress implies MARKING");
+     // If old-marking is in progress when we turn off YOUNG_MARKING, leave MARKING (and OLD_MARKING) on
+     mask = YOUNG_MARKING;
+   } else {
+     mask = MARKING | YOUNG_MARKING;
+   }
+   set_gc_state(mask, in_progress);
+   manage_satb_barrier(in_progress);
+ }
+ 
+ void ShenandoahHeap::set_concurrent_old_mark_in_progress(bool in_progress) {
+ #ifdef ASSERT
+   // has_forwarded_objects() iff UPDATEREFS or EVACUATION
+   bool has_forwarded = has_forwarded_objects();
+   bool updating_or_evacuating = _gc_state.is_set(UPDATEREFS | EVACUATION);
+   bool evacuating = _gc_state.is_set(EVACUATION);
+   assert ((has_forwarded == updating_or_evacuating) || (evacuating && !has_forwarded && collection_set()->is_empty()),
+           "Updating or evacuating iff has forwarded objects, or if evacuation phase is promoting in place without forwarding");
+ #endif
+   if (!in_progress && is_concurrent_young_mark_in_progress()) {
+     // If young-marking is in progress when we turn off OLD_MARKING, leave MARKING (and YOUNG_MARKING) on
+     assert(_gc_state.is_set(MARKING), "concurrent_young_marking_in_progress implies MARKING");
+     set_gc_state(OLD_MARKING, in_progress);
+   } else {
+     set_gc_state(MARKING | OLD_MARKING, in_progress);
+   }
+   manage_satb_barrier(in_progress);
+ }
+ 
+ bool ShenandoahHeap::is_prepare_for_old_mark_in_progress() const {
+   return old_generation()->state() == ShenandoahOldGeneration::FILLING;
+ }
+ 
+ void ShenandoahHeap::set_aging_cycle(bool in_progress) {
+   _is_aging_cycle.set_cond(in_progress);
+ }
+ 
+ void ShenandoahHeap::manage_satb_barrier(bool active) {
+   if (is_concurrent_mark_in_progress()) {
+     // Ignore request to deactivate barrier while concurrent mark is in progress.
+     // Do not attempt to re-activate the barrier if it is already active.
+     if (active && !ShenandoahBarrierSet::satb_mark_queue_set().is_active()) {
+       ShenandoahBarrierSet::satb_mark_queue_set().set_active_all_threads(active, !active);
+     }
+   } else {
+     // No concurrent marking is in progress so honor request to deactivate,
+     // but only if the barrier is already active.
+     if (!active && ShenandoahBarrierSet::satb_mark_queue_set().is_active()) {
+       ShenandoahBarrierSet::satb_mark_queue_set().set_active_all_threads(active, !active);
+     }
+   }
  }
  
  void ShenandoahHeap::set_evacuation_in_progress(bool in_progress) {
    assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Only call this at safepoint");
    set_gc_state(EVACUATION, in_progress);

*** 1854,37 ***
  bool ShenandoahHeap::try_cancel_gc() {
    jbyte prev = _cancelled_gc.cmpxchg(CANCELLED, CANCELLABLE);
    return prev == CANCELLABLE;
  }
  
  void ShenandoahHeap::cancel_gc(GCCause::Cause cause) {
    if (try_cancel_gc()) {
      FormatBuffer<> msg("Cancelling GC: %s", GCCause::to_string(cause));
      log_info(gc)("%s", msg.buffer());
      Events::log(Thread::current(), "%s", msg.buffer());
    }
  }
  
  uint ShenandoahHeap::max_workers() {
    return _max_workers;
  }
  
  void ShenandoahHeap::stop() {
    // The shutdown sequence should be able to terminate when GC is running.
  
!   // Step 0. Notify policy to disable event recording.
    _shenandoah_policy->record_shutdown();
  
!   // Step 1. Notify control thread that we are in shutdown.
    // Note that we cannot do that with stop(), because stop() is blocking and waits for the actual shutdown.
    // Doing stop() here would wait for the normal GC cycle to complete, never falling through to cancel below.
    control_thread()->prepare_for_graceful_shutdown();
  
!   // Step 2. Notify GC workers that we are cancelling GC.
    cancel_gc(GCCause::_shenandoah_stop_vm);
  
!   // Step 3. Wait until GC worker exits normally.
    control_thread()->stop();
  }
  
  void ShenandoahHeap::stw_unload_classes(bool full_gc) {
    if (!unload_classes()) return;
--- 2225,46 ---
  bool ShenandoahHeap::try_cancel_gc() {
    jbyte prev = _cancelled_gc.cmpxchg(CANCELLED, CANCELLABLE);
    return prev == CANCELLABLE;
  }
  
+ void ShenandoahHeap::cancel_concurrent_mark() {
+   _young_generation->cancel_marking();
+   _old_generation->cancel_marking();
+   _global_generation->cancel_marking();
+ 
+   ShenandoahBarrierSet::satb_mark_queue_set().abandon_partial_marking();
+ }
+ 
  void ShenandoahHeap::cancel_gc(GCCause::Cause cause) {
    if (try_cancel_gc()) {
      FormatBuffer<> msg("Cancelling GC: %s", GCCause::to_string(cause));
      log_info(gc)("%s", msg.buffer());
      Events::log(Thread::current(), "%s", msg.buffer());
+     _cancel_requested_time = os::elapsedTime();
    }
  }
  
  uint ShenandoahHeap::max_workers() {
    return _max_workers;
  }
  
  void ShenandoahHeap::stop() {
    // The shutdown sequence should be able to terminate when GC is running.
  
!   // Step 1. Notify policy to disable event recording and prevent visiting gc threads during shutdown
    _shenandoah_policy->record_shutdown();
  
!   // Step 2. Notify control thread that we are in shutdown.
    // Note that we cannot do that with stop(), because stop() is blocking and waits for the actual shutdown.
    // Doing stop() here would wait for the normal GC cycle to complete, never falling through to cancel below.
    control_thread()->prepare_for_graceful_shutdown();
  
!   // Step 3. Notify GC workers that we are cancelling GC.
    cancel_gc(GCCause::_shenandoah_stop_vm);
  
!   // Step 4. Wait until GC worker exits normally.
    control_thread()->stop();
  }
  
  void ShenandoahHeap::stw_unload_classes(bool full_gc) {
    if (!unload_classes()) return;

*** 1982,16 ***
    ShenandoahHeap* heap = ShenandoahHeap::heap();
    assert(heap->collection_set() != nullptr, "Sanity");
    return (address) heap->collection_set()->biased_map_address();
  }
  
- size_t ShenandoahHeap::bytes_allocated_since_gc_start() const {
-   return Atomic::load(&_bytes_allocated_since_gc_start);
- }
- 
  void ShenandoahHeap::reset_bytes_allocated_since_gc_start() {
!   Atomic::store(&_bytes_allocated_since_gc_start, (size_t)0);
  }
  
  void ShenandoahHeap::set_degenerated_gc_in_progress(bool in_progress) {
    _degenerated_gc_in_progress.set_cond(in_progress);
  }
--- 2362,17 ---
    ShenandoahHeap* heap = ShenandoahHeap::heap();
    assert(heap->collection_set() != nullptr, "Sanity");
    return (address) heap->collection_set()->biased_map_address();
  }
  
  void ShenandoahHeap::reset_bytes_allocated_since_gc_start() {
!   if (mode()->is_generational()) {
+     young_generation()->reset_bytes_allocated_since_gc_start();
+     old_generation()->reset_bytes_allocated_since_gc_start();
+   }
+ 
+   global_generation()->reset_bytes_allocated_since_gc_start();
  }
  
  void ShenandoahHeap::set_degenerated_gc_in_progress(bool in_progress) {
    _degenerated_gc_in_progress.set_cond(in_progress);
  }

*** 2051,12 ***
  
  #ifdef ASSERT
  void ShenandoahHeap::assert_pinned_region_status() {
    for (size_t i = 0; i < num_regions(); i++) {
      ShenandoahHeapRegion* r = get_region(i);
!     assert((r->is_pinned() && r->pin_count() > 0) || (!r->is_pinned() && r->pin_count() == 0),
!            "Region " SIZE_FORMAT " pinning status is inconsistent", i);
    }
  }
  #endif
  
  ConcurrentGCTimer* ShenandoahHeap::gc_timer() const {
--- 2432,14 ---
  
  #ifdef ASSERT
  void ShenandoahHeap::assert_pinned_region_status() {
    for (size_t i = 0; i < num_regions(); i++) {
      ShenandoahHeapRegion* r = get_region(i);
!     if (active_generation()->contains(r)) {
!       assert((r->is_pinned() && r->pin_count() > 0) || (!r->is_pinned() && r->pin_count() == 0),
+              "Region " SIZE_FORMAT " pinning status is inconsistent", i);
+     }
    }
  }
  #endif
  
  ConcurrentGCTimer* ShenandoahHeap::gc_timer() const {

*** 2106,75 ***
  template<bool CONCURRENT>
  class ShenandoahUpdateHeapRefsTask : public WorkerTask {
  private:
    ShenandoahHeap* _heap;
    ShenandoahRegionIterator* _regions;
  public:
!   ShenandoahUpdateHeapRefsTask(ShenandoahRegionIterator* regions) :
      WorkerTask("Shenandoah Update References"),
      _heap(ShenandoahHeap::heap()),
!     _regions(regions) {
    }
  
    void work(uint worker_id) {
      if (CONCURRENT) {
        ShenandoahConcurrentWorkerSession worker_session(worker_id);
        ShenandoahSuspendibleThreadSetJoiner stsj;
!       do_work<ShenandoahConcUpdateRefsClosure>();
      } else {
        ShenandoahParallelWorkerSession worker_session(worker_id);
!       do_work<ShenandoahSTWUpdateRefsClosure>();
      }
    }
  
  private:
    template<class T>
!   void do_work() {
      T cl;
      ShenandoahHeapRegion* r = _regions->next();
!     ShenandoahMarkingContext* const ctx = _heap->complete_marking_context();
      while (r != nullptr) {
        HeapWord* update_watermark = r->get_update_watermark();
        assert (update_watermark >= r->bottom(), "sanity");
        if (r->is_active() && !r->is_cset()) {
!         _heap->marked_object_oop_iterate(r, &cl, update_watermark);
        }
!       if (ShenandoahPacing) {
          _heap->pacer()->report_updaterefs(pointer_delta(update_watermark, r->bottom()));
        }
        if (_heap->check_cancelled_gc_and_yield(CONCURRENT)) {
          return;
        }
        r = _regions->next();
      }
    }
  };
  
  void ShenandoahHeap::update_heap_references(bool concurrent) {
    assert(!is_full_gc_in_progress(), "Only for concurrent and degenerated GC");
  
    if (concurrent) {
!     ShenandoahUpdateHeapRefsTask<true> task(&_update_refs_iterator);
      workers()->run_task(&task);
    } else {
!     ShenandoahUpdateHeapRefsTask<false> task(&_update_refs_iterator);
      workers()->run_task(&task);
    }
  }
  
- 
  class ShenandoahFinalUpdateRefsUpdateRegionStateClosure : public ShenandoahHeapRegionClosure {
  private:
    ShenandoahHeapLock* const _lock;
  
  public:
!   ShenandoahFinalUpdateRefsUpdateRegionStateClosure() : _lock(ShenandoahHeap::heap()->lock()) {}
  
-   void heap_region_do(ShenandoahHeapRegion* r) {
      // Drop unnecessary "pinned" state from regions that does not have CP marks
      // anymore, as this would allow trashing them.
- 
      if (r->is_active()) {
        if (r->is_pinned()) {
          if (r->pin_count() == 0) {
            ShenandoahHeapLocker locker(_lock);
            r->make_unpinned();
--- 2489,270 ---
  template<bool CONCURRENT>
  class ShenandoahUpdateHeapRefsTask : public WorkerTask {
  private:
    ShenandoahHeap* _heap;
    ShenandoahRegionIterator* _regions;
+   ShenandoahRegionChunkIterator* _work_chunks;
+ 
  public:
!   explicit ShenandoahUpdateHeapRefsTask(ShenandoahRegionIterator* regions,
+                                         ShenandoahRegionChunkIterator* work_chunks) :
      WorkerTask("Shenandoah Update References"),
      _heap(ShenandoahHeap::heap()),
!     _regions(regions),
+     _work_chunks(work_chunks)
+   {
+     bool old_bitmap_stable = _heap->old_generation()->is_mark_complete();
+     log_info(gc, remset)("Scan remembered set using bitmap: %s", BOOL_TO_STR(old_bitmap_stable));
    }
  
    void work(uint worker_id) {
      if (CONCURRENT) {
        ShenandoahConcurrentWorkerSession worker_session(worker_id);
        ShenandoahSuspendibleThreadSetJoiner stsj;
!       do_work<ShenandoahConcUpdateRefsClosure>(worker_id);
      } else {
        ShenandoahParallelWorkerSession worker_session(worker_id);
!       do_work<ShenandoahSTWUpdateRefsClosure>(worker_id);
      }
    }
  
  private:
    template<class T>
!   void do_work(uint worker_id) {
      T cl;
+     if (CONCURRENT && (worker_id == 0)) {
+       // We ask the first worker to replenish the Mutator free set by moving regions previously reserved to hold the
+       // results of evacuation.  These reserves are no longer necessary because evacuation has completed.
+       size_t cset_regions = _heap->collection_set()->count();
+       // We cannot transfer any more regions than will be reclaimed when the existing collection set is recycled, because
+       // we need the reclaimed collection set regions to replenish the collector reserves
+       _heap->free_set()->move_collector_sets_to_mutator(cset_regions);
+     }
+     // If !CONCURRENT, there's no value in expanding Mutator free set
+ 
      ShenandoahHeapRegion* r = _regions->next();
!     // We update references for global, old, and young collections.
+     assert(_heap->active_generation()->is_mark_complete(), "Expected complete marking");
+     ShenandoahMarkingContext* const ctx = _heap->marking_context();
+     bool is_mixed = _heap->collection_set()->has_old_regions();
      while (r != nullptr) {
        HeapWord* update_watermark = r->get_update_watermark();
        assert (update_watermark >= r->bottom(), "sanity");
+ 
+       log_debug(gc)("ShenandoahUpdateHeapRefsTask::do_work(%u) looking at region " SIZE_FORMAT, worker_id, r->index());
+       bool region_progress = false;
        if (r->is_active() && !r->is_cset()) {
!         if (!_heap->mode()->is_generational() || r->is_young()) {
+           _heap->marked_object_oop_iterate(r, &cl, update_watermark);
+           region_progress = true;
+         } else if (r->is_old()) {
+           if (_heap->active_generation()->is_global()) {
+             // Note that GLOBAL collection is not as effectively balanced as young and mixed cycles.  This is because
+             // concurrent GC threads are parceled out entire heap regions of work at a time and there
+             // is no "catchup phase" consisting of remembered set scanning, during which parcels of work are smaller
+             // and more easily distributed more fairly across threads.
+ 
+             // TODO: Consider an improvement to load balance GLOBAL GC.
+             _heap->marked_object_oop_iterate(r, &cl, update_watermark);
+             region_progress = true;
+           }
+           // Otherwise, this is an old region in a young or mixed cycle.  Process it during a second phase, below.
+           // Don't bother to report pacing progress in this case.
+         } else {
+           // Because updating of references runs concurrently, it is possible that a FREE inactive region transitions
+           // to a non-free active region while this loop is executing.  Whenever this happens, the changing of a region's
+           // active status may propagate at a different speed than the changing of the region's affiliation.
+ 
+           // When we reach this control point, it is because a race has allowed a region's is_active() status to be seen
+           // by this thread before the region's affiliation() is seen by this thread.
+ 
+           // It's ok for this race to occur because the newly transformed region does not have any references to be
+           // updated.
+ 
+           assert(r->get_update_watermark() == r->bottom(),
+                  "%s Region " SIZE_FORMAT " is_active but not recognized as YOUNG or OLD so must be newly transitioned from FREE",
+                  r->affiliation_name(), r->index());
+         }
        }
!       if (region_progress && ShenandoahPacing) {
          _heap->pacer()->report_updaterefs(pointer_delta(update_watermark, r->bottom()));
        }
        if (_heap->check_cancelled_gc_and_yield(CONCURRENT)) {
          return;
        }
        r = _regions->next();
      }
+ 
+     if (_heap->mode()->is_generational() && !_heap->active_generation()->is_global()) {
+       // Since this is generational and not GLOBAL, we have to process the remembered set.  There's no remembered
+       // set processing if not in generational mode or if GLOBAL mode.
+ 
+       // After this thread has exhausted its traditional update-refs work, it continues with updating refs within remembered set.
+       // The remembered set workload is better balanced between threads, so threads that are "behind" can catch up with other
+       // threads during this phase, allowing all threads to work more effectively in parallel.
+       struct ShenandoahRegionChunk assignment;
+       RememberedScanner* scanner = _heap->card_scan();
+ 
+       while (!_heap->check_cancelled_gc_and_yield(CONCURRENT) && _work_chunks->next(&assignment)) {
+         // Keep grabbing next work chunk to process until finished, or asked to yield
+         ShenandoahHeapRegion* r = assignment._r;
+         if (r->is_active() && !r->is_cset() && r->is_old()) {
+           HeapWord* start_of_range = r->bottom() + assignment._chunk_offset;
+           HeapWord* end_of_range = r->get_update_watermark();
+           if (end_of_range > start_of_range + assignment._chunk_size) {
+             end_of_range = start_of_range + assignment._chunk_size;
+           }
+ 
+           // Old region in a young cycle or mixed cycle.
+           if (is_mixed) {
+             // TODO: For mixed evac, consider building an old-gen remembered set that allows restricted updating
+             // within old-gen HeapRegions.  This remembered set can be constructed by old-gen concurrent marking
+             // and augmented by card marking.  For example, old-gen concurrent marking can remember for each old-gen
+             // card which other old-gen regions it refers to: none, one-other specifically, multiple-other non-specific.
+             // Update-references when _mixed_evac processess each old-gen memory range that has a traditional DIRTY
+             // card or if the "old-gen remembered set" indicates that this card holds pointers specifically to an
+             // old-gen region in the most recent collection set, or if this card holds pointers to other non-specific
+             // old-gen heap regions.
+ 
+             if (r->is_humongous()) {
+               if (start_of_range < end_of_range) {
+                 // Need to examine both dirty and clean cards during mixed evac.
+                 r->oop_iterate_humongous_slice(&cl, false, start_of_range, assignment._chunk_size, true);
+               }
+             } else {
+               // Since this is mixed evacuation, old regions that are candidates for collection have not been coalesced
+               // and filled.  Use mark bits to find objects that need to be updated.
+               //
+               // Future TODO: establish a second remembered set to identify which old-gen regions point to other old-gen
+               // regions which are in the collection set for a particular mixed evacuation.
+               if (start_of_range < end_of_range) {
+                 HeapWord* p = nullptr;
+                 size_t card_index = scanner->card_index_for_addr(start_of_range);
+                 // In case last object in my range spans boundary of my chunk, I may need to scan all the way to top()
+                 ShenandoahObjectToOopBoundedClosure<T> objs(&cl, start_of_range, r->top());
+ 
+                 // Any object that begins in a previous range is part of a different scanning assignment.  Any object that
+                 // starts after end_of_range is also not my responsibility.  (Either allocated during evacuation, so does
+                 // not hold pointers to from-space, or is beyond the range of my assigned work chunk.)
+ 
+                 // Find the first object that begins in my range, if there is one.
+                 p = start_of_range;
+                 oop obj = cast_to_oop(p);
+                 HeapWord* tams = ctx->top_at_mark_start(r);
+                 if (p >= tams) {
+                   // We cannot use ctx->is_marked(obj) to test whether an object begins at this address.  Instead,
+                   // we need to use the remembered set crossing map to advance p to the first object that starts
+                   // within the enclosing card.
+ 
+                   while (true) {
+                     HeapWord* first_object = scanner->first_object_in_card(card_index);
+                     if (first_object != nullptr) {
+                       p = first_object;
+                       break;
+                     } else if (scanner->addr_for_card_index(card_index + 1) < end_of_range) {
+                       card_index++;
+                     } else {
+                       // Force the loop that follows to immediately terminate.
+                       p = end_of_range;
+                       break;
+                     }
+                   }
+                   obj = cast_to_oop(p);
+                   // Note: p may be >= end_of_range
+                 } else if (!ctx->is_marked(obj)) {
+                   p = ctx->get_next_marked_addr(p, tams);
+                   obj = cast_to_oop(p);
+                   // If there are no more marked objects before tams, this returns tams.
+                   // Note that tams is either >= end_of_range, or tams is the start of an object that is marked.
+                 }
+                 while (p < end_of_range) {
+                   // p is known to point to the beginning of marked object obj
+                   objs.do_object(obj);
+                   HeapWord* prev_p = p;
+                   p += obj->size();
+                   if (p < tams) {
+                     p = ctx->get_next_marked_addr(p, tams);
+                     // If there are no more marked objects before tams, this returns tams.  Note that tams is
+                     // either >= end_of_range, or tams is the start of an object that is marked.
+                   }
+                   assert(p != prev_p, "Lack of forward progress");
+                   obj = cast_to_oop(p);
+                 }
+               }
+             }
+           } else {
+             // This is a young evac..
+             if (start_of_range < end_of_range) {
+               size_t cluster_size =
+                 CardTable::card_size_in_words() * ShenandoahCardCluster<ShenandoahDirectCardMarkRememberedSet>::CardsPerCluster;
+               size_t clusters = assignment._chunk_size / cluster_size;
+               assert(clusters * cluster_size == assignment._chunk_size, "Chunk assignment must align on cluster boundaries");
+               scanner->process_region_slice(r, assignment._chunk_offset, clusters, end_of_range, &cl, true, worker_id);
+             }
+           }
+           if (ShenandoahPacing && (start_of_range < end_of_range)) {
+             _heap->pacer()->report_updaterefs(pointer_delta(end_of_range, start_of_range));
+           }
+         }
+       }
+     }
    }
  };
  
  void ShenandoahHeap::update_heap_references(bool concurrent) {
    assert(!is_full_gc_in_progress(), "Only for concurrent and degenerated GC");
+   uint nworkers = workers()->active_workers();
+   ShenandoahRegionChunkIterator work_list(nworkers);
  
    if (concurrent) {
!     ShenandoahUpdateHeapRefsTask<true> task(&_update_refs_iterator, &work_list);
      workers()->run_task(&task);
    } else {
!     ShenandoahUpdateHeapRefsTask<false> task(&_update_refs_iterator, &work_list);
      workers()->run_task(&task);
    }
+   if (ShenandoahEnableCardStats && card_scan()!=nullptr) { // generational check proxy
+     card_scan()->log_card_stats(nworkers, CARD_STAT_UPDATE_REFS);
+   }
  }
  
  class ShenandoahFinalUpdateRefsUpdateRegionStateClosure : public ShenandoahHeapRegionClosure {
  private:
+   ShenandoahMarkingContext* _ctx;
    ShenandoahHeapLock* const _lock;
+   bool _is_generational;
  
  public:
!   ShenandoahFinalUpdateRefsUpdateRegionStateClosure(ShenandoahMarkingContext* ctx) :
+     _ctx(ctx), _lock(ShenandoahHeap::heap()->lock()),
+     _is_generational(ShenandoahHeap::heap()->mode()->is_generational()) { }
+ 
+   void heap_region_do(ShenandoahHeapRegion* r) override {
+ 
+     // Maintenance of region age must follow evacuation in order to account for evacuation allocations within survivor
+     // regions.  We consult region age during the subsequent evacuation to determine whether certain objects need to
+     // be promoted.
+     if (_is_generational && r->is_young() && r->is_active()) {
+       HeapWord *tams = _ctx->top_at_mark_start(r);
+       HeapWord *top = r->top();
+ 
+       // Allocations move the watermark when top moves.  However, compacting
+       // objects will sometimes lower top beneath the watermark, after which,
+       // attempts to read the watermark will assert out (watermark should not be
+       // higher than top).
+       if (top > tams) {
+         // There have been allocations in this region since the start of the cycle.
+         // Any objects new to this region must not assimilate elevated age.
+         r->reset_age();
+       } else if (ShenandoahHeap::heap()->is_aging_cycle()) {
+         r->increment_age();
+       }
+     }
  
      // Drop unnecessary "pinned" state from regions that does not have CP marks
      // anymore, as this would allow trashing them.
      if (r->is_active()) {
        if (r->is_pinned()) {
          if (r->pin_count() == 0) {
            ShenandoahHeapLocker locker(_lock);
            r->make_unpinned();

*** 2186,22 ***
          }
        }
      }
    }
  
!   bool is_thread_safe() { return true; }
  };
  
  void ShenandoahHeap::update_heap_region_states(bool concurrent) {
    assert(SafepointSynchronize::is_at_safepoint(), "Must be at a safepoint");
    assert(!is_full_gc_in_progress(), "Only for concurrent and degenerated GC");
  
    {
      ShenandoahGCPhase phase(concurrent ?
                              ShenandoahPhaseTimings::final_update_refs_update_region_states :
                              ShenandoahPhaseTimings::degen_gc_final_update_refs_update_region_states);
!     ShenandoahFinalUpdateRefsUpdateRegionStateClosure cl;
      parallel_heap_region_iterate(&cl);
  
      assert_pinned_region_status();
    }
  
--- 2764,22 ---
          }
        }
      }
    }
  
!   bool is_thread_safe() override { return true; }
  };
  
  void ShenandoahHeap::update_heap_region_states(bool concurrent) {
    assert(SafepointSynchronize::is_at_safepoint(), "Must be at a safepoint");
    assert(!is_full_gc_in_progress(), "Only for concurrent and degenerated GC");
  
    {
      ShenandoahGCPhase phase(concurrent ?
                              ShenandoahPhaseTimings::final_update_refs_update_region_states :
                              ShenandoahPhaseTimings::degen_gc_final_update_refs_update_region_states);
!     ShenandoahFinalUpdateRefsUpdateRegionStateClosure cl (active_generation()->complete_marking_context());
      parallel_heap_region_iterate(&cl);
  
      assert_pinned_region_status();
    }
  

*** 2212,16 ***
      trash_cset_regions();
    }
  }
  
  void ShenandoahHeap::rebuild_free_set(bool concurrent) {
!   {
!     ShenandoahGCPhase phase(concurrent ?
!                             ShenandoahPhaseTimings::final_update_refs_rebuild_freeset :
!                             ShenandoahPhaseTimings::degen_gc_final_update_refs_rebuild_freeset);
!     ShenandoahHeapLocker locker(lock());
!     _free_set->rebuild();
    }
  }
  
  void ShenandoahHeap::print_extended_on(outputStream *st) const {
    print_on(st);
--- 2790,49 ---
      trash_cset_regions();
    }
  }
  
  void ShenandoahHeap::rebuild_free_set(bool concurrent) {
!   ShenandoahGCPhase phase(concurrent ?
!                           ShenandoahPhaseTimings::final_update_refs_rebuild_freeset :
!                           ShenandoahPhaseTimings::degen_gc_final_update_refs_rebuild_freeset);
!   ShenandoahHeapLocker locker(lock());
!   size_t young_cset_regions, old_cset_regions;
!   size_t first_old_region, last_old_region, old_region_count;
+   _free_set->prepare_to_rebuild(young_cset_regions, old_cset_regions, first_old_region, last_old_region, old_region_count);
+   // If there are no old regions, first_old_region will be greater than last_old_region
+   assert((first_old_region > last_old_region) ||
+          ((last_old_region + 1 - first_old_region >= old_region_count) &&
+           get_region(first_old_region)->is_old() && get_region(last_old_region)->is_old()),
+          "sanity: old_region_count: " SIZE_FORMAT ", first_old_region: " SIZE_FORMAT ", last_old_region: " SIZE_FORMAT,
+          old_region_count, first_old_region, last_old_region);
+ 
+   if (mode()->is_generational()) {
+     assert(verify_generation_usage(true, old_generation()->used_regions(),
+                                    old_generation()->used(), old_generation()->get_humongous_waste(),
+                                    true, young_generation()->used_regions(),
+                                    young_generation()->used(), young_generation()->get_humongous_waste()),
+            "Generation accounts are inaccurate");
+ 
+     // The computation of bytes_of_allocation_runway_before_gc_trigger is quite conservative so consider all of this
+     // available for transfer to old. Note that transfer of humongous regions does not impact available.
+     size_t allocation_runway = young_generation()->heuristics()->bytes_of_allocation_runway_before_gc_trigger(young_cset_regions);
+     ShenandoahGenerationalHeap::heap()->compute_old_generation_balance(allocation_runway, old_cset_regions);
+ 
+     // Total old_available may have been expanded to hold anticipated promotions.  We trigger if the fragmented available
+     // memory represents more than 16 regions worth of data.  Note that fragmentation may increase when we promote regular
+     // regions in place when many of these regular regions have an abundant amount of available memory within them.  Fragmentation
+     // will decrease as promote-by-copy consumes the available memory within these partially consumed regions.
+     //
+     // We consider old-gen to have excessive fragmentation if more than 12.5% of old-gen is free memory that resides
+     // within partially consumed regions of memory.
+   }
+   // Rebuild free set based on adjusted generation sizes.
+   _free_set->rebuild(young_cset_regions, old_cset_regions);
+ 
+   if (mode()->is_generational() && (ShenandoahGenerationalHumongousReserve > 0)) {
+     old_generation()->maybe_trigger_collection(first_old_region, last_old_region, old_region_count);
    }
  }
  
  void ShenandoahHeap::print_extended_on(outputStream *st) const {
    print_on(st);

*** 2340,11 ***
    memory_pools.append(_memory_pool);
    return memory_pools;
  }
  
  MemoryUsage ShenandoahHeap::memory_usage() {
!   return _memory_pool->get_memory_usage();
  }
  
  ShenandoahRegionIterator::ShenandoahRegionIterator() :
    _heap(ShenandoahHeap::heap()),
    _index(0) {}
--- 2951,11 ---
    memory_pools.append(_memory_pool);
    return memory_pools;
  }
  
  MemoryUsage ShenandoahHeap::memory_usage() {
!   return MemoryUsage(_initial_size, used(), committed(), max_capacity());
  }
  
  ShenandoahRegionIterator::ShenandoahRegionIterator() :
    _heap(ShenandoahHeap::heap()),
    _index(0) {}

*** 2378,10 ***
--- 2989,11 ---
  
  void ShenandoahHeap::flush_liveness_cache(uint worker_id) {
    assert(worker_id < _max_workers, "sanity");
    assert(_liveness_cache != nullptr, "sanity");
    ShenandoahLiveData* ld = _liveness_cache[worker_id];
+ 
    for (uint i = 0; i < num_regions(); i++) {
      ShenandoahLiveData live = ld[i];
      if (live > 0) {
        ShenandoahHeapRegion* r = get_region(i);
        r->increase_live_data_gc_words(live);

*** 2405,5 ***
--- 3017,94 ---
      return true;
    }
  
    return false;
  }
+ 
+ void ShenandoahHeap::transfer_old_pointers_from_satb() {
+   _old_generation->transfer_pointers_from_satb();
+ }
+ 
+ bool ShenandoahHeap::verify_generation_usage(bool verify_old, size_t old_regions, size_t old_bytes, size_t old_waste,
+                                              bool verify_young, size_t young_regions, size_t young_bytes, size_t young_waste) {
+   size_t tally_old_regions = 0;
+   size_t tally_old_bytes = 0;
+   size_t tally_old_waste = 0;
+   size_t tally_young_regions = 0;
+   size_t tally_young_bytes = 0;
+   size_t tally_young_waste = 0;
+ 
+   shenandoah_assert_heaplocked_or_safepoint();
+   for (size_t i = 0; i < num_regions(); i++) {
+     ShenandoahHeapRegion* r = get_region(i);
+     if (r->is_old()) {
+       tally_old_regions++;
+       tally_old_bytes += r->used();
+       if (r->is_humongous()) {
+         ShenandoahHeapRegion* start = r->humongous_start_region();
+         HeapWord* obj_addr = start->bottom();
+         oop obj = cast_to_oop(obj_addr);
+         size_t word_size = obj->size();
+         HeapWord* end_addr = obj_addr + word_size;
+         if (end_addr <= r->end()) {
+           tally_old_waste += (r->end() - end_addr) * HeapWordSize;
+         }
+       }
+     } else if (r->is_young()) {
+       tally_young_regions++;
+       tally_young_bytes += r->used();
+       if (r->is_humongous()) {
+         ShenandoahHeapRegion* start = r->humongous_start_region();
+         HeapWord* obj_addr = start->bottom();
+         oop obj = cast_to_oop(obj_addr);
+         size_t word_size = obj->size();
+         HeapWord* end_addr = obj_addr + word_size;
+         if (end_addr <= r->end()) {
+           tally_young_waste += (r->end() - end_addr) * HeapWordSize;
+         }
+       }
+     }
+   }
+   if (verify_young &&
+       ((young_regions != tally_young_regions) || (young_bytes != tally_young_bytes) || (young_waste != tally_young_waste))) {
+     return false;
+   } else if (verify_old &&
+              ((old_regions != tally_old_regions) || (old_bytes != tally_old_bytes) || (old_waste != tally_old_waste))) {
+     return false;
+   } else {
+     return true;
+   }
+ }
+ 
+ ShenandoahGeneration* ShenandoahHeap::generation_for(ShenandoahAffiliation affiliation) const {
+   if (!mode()->is_generational()) {
+     return global_generation();
+   } else if (affiliation == YOUNG_GENERATION) {
+     return young_generation();
+   } else if (affiliation == OLD_GENERATION) {
+     return old_generation();
+   }
+ 
+   ShouldNotReachHere();
+   return nullptr;
+ }
+ 
+ void ShenandoahHeap::log_heap_status(const char* msg) const {
+   if (mode()->is_generational()) {
+     young_generation()->log_status(msg);
+     old_generation()->log_status(msg);
+   } else {
+     global_generation()->log_status(msg);
+   }
+ }
+ 
+ void ShenandoahHeap::clear_cards_for(ShenandoahHeapRegion* region) {
+   if (mode()->is_generational()) {
+     _card_scan->mark_range_as_empty(region->bottom(), pointer_delta(region->end(), region->bottom()));
+   }
+ }
+ 
+ void ShenandoahHeap::mark_card_as_dirty(void* location) {
+   if (mode()->is_generational()) {
+     _card_scan->mark_card_as_dirty((HeapWord*)location);
+   }
+ }
< prev index next >