< 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.

*** 35,41 ***
  #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/shenandoahBarrierSet.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/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/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/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/mode/shenandoahPassiveMode.hpp"
  #include "gc/shenandoah/mode/shenandoahSATBMode.hpp"
  #if INCLUDE_JFR
  #include "gc/shenandoah/shenandoahJfrSupport.hpp"
  #endif
  
  #include "cds/archiveHeapWriter.hpp"
--- 36,52 ---
  #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/heuristics/shenandoahOldHeuristics.hpp"
+ #include "gc/shenandoah/heuristics/shenandoahYoungHeuristics.hpp"
+ #include "gc/shenandoah/shenandoahAllocRequest.hpp"
  #include "gc/shenandoah/shenandoahBarrierSet.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/shenandoahMonitoringSupport.hpp"
+ #include "gc/shenandoah/shenandoahOldGeneration.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/shenandoahPassiveMode.hpp"
  #include "gc/shenandoah/mode/shenandoahSATBMode.hpp"
+ #include "utilities/globalDefinitions.hpp"
+ 
  #if INCLUDE_JFR
  #include "gc/shenandoah/shenandoahJfrSupport.hpp"
  #endif
  
  #include "cds/archiveHeapWriter.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;
  
--- 171,10 ---

*** 215,10 ***
--- 224,32 ---
    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));
+ 
+   // Now we know the number of regions and heap sizes, initialize the heuristics.
+   initialize_heuristics();
+ 
+   assert(_heap_region.byte_size() == heap_rs.size(), "Need to know reserved size for card table");
+ 
+   //
+   // Worker threads must be initialized after the barrier is configured
+   //
+   _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_tag(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,
--- 286,18 ---
    MemTracker::record_virtual_memory_tag(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,

*** 346,10 ***
--- 377,11 ---
                                              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());
  

*** 362,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,
--- 394,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->finish_rebuild(young_cset_regions, old_cset_regions, num_old);
    }
  
    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,

*** 416,25 ***
    if (ShenandoahPacing) {
      _pacer = new ShenandoahPacer(this);
      _pacer->setup_for_idle();
    }
  
!   _control_thread = new ShenandoahControlThread();
  
!   ShenandoahInitLogger::print();
  
    FullGCForwarding::initialize(_heap_region);
  
    return JNI_OK;
  }
  
  void ShenandoahHeap::initialize_mode() {
    if (ShenandoahGCMode != nullptr) {
      if (strcmp(ShenandoahGCMode, "satb") == 0) {
        _gc_mode = new ShenandoahSATBMode();
      } 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)");
--- 454,35 ---
    if (ShenandoahPacing) {
      _pacer = new ShenandoahPacer(this);
      _pacer->setup_for_idle();
    }
  
!   initialize_controller();
  
!   print_init_logger();
  
    FullGCForwarding::initialize(_heap_region);
  
    return JNI_OK;
  }
  
+ void ShenandoahHeap::initialize_controller() {
+   _control_thread = new ShenandoahControlThread();
+ }
+ 
+ void ShenandoahHeap::print_init_logger() const {
+   ShenandoahInitLogger::print();
+ }
+ 
  void ShenandoahHeap::initialize_mode() {
    if (ShenandoahGCMode != nullptr) {
      if (strcmp(ShenandoahGCMode, "satb") == 0) {
        _gc_mode = new ShenandoahSATBMode();
      } 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)");

*** 451,121 ***
                      _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()),
--- 499,70 ---
                      _gc_mode->name()));
    }
  }
  
  void ShenandoahHeap::initialize_heuristics() {
!   _global_generation = new ShenandoahGlobalGeneration(mode()->is_generational(), max_workers(), max_capacity(), max_capacity());
!   _global_generation->initialize_heuristics(mode());
  }
  
  #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),
+   _active_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),
    _gc_state_changed(false),
    _gc_no_progress_count(0),
+   _cancel_requested_time(0),
+   _update_refs_iterator(this),
+   _global_generation(nullptr),
    _control_thread(nullptr),
+   _young_generation(nullptr),
+   _old_generation(nullptr),
    _shenandoah_policy(policy),
    _gc_mode(nullptr),
    _free_set(nullptr),
    _pacer(nullptr),
    _verifier(nullptr),
    _phase_timings(nullptr),
+   _mmu_tracker(),
    _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)
  {
!   // Initialize GC mode early, many subsequent initialization procedures depend on it
    initialize_mode();
  }
  
  #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()),

*** 576,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, ");
--- 573,16 ---
                 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 (!mode()->is_generational()) {
+     if (is_concurrent_mark_in_progress())      st->print("marking,");
+   } else {
+     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, ");

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

*** 640,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<true>(words);
      }
    }
  }
  
  size_t ShenandoahHeap::capacity() const {
--- 644,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 && ShenandoahHeapRegion::requires_humongous(req.actual_size())) {
+       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<true>(waste);
      }
    }
  }
  
  size_t ShenandoahHeap::capacity() const {

*** 823,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);
  }
--- 876,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);
  }

*** 842,21 ***
--- 893,24 ---
    // 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;
+ 
    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,
    // heuristics should catch up with them.
+   log_debug(gc, free)("Set new GCLAB size: " SIZE_FORMAT, new_size);
    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);

*** 882,10 ***
--- 936,11 ---
    }
    gclab->set_buf(gclab_buf, actual_size);
    return gclab->allocate(size);
  }
  
+ // 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);

*** 930,12 ***
--- 985,14 ---
      // Shenandoah will grind along for quite a while allocating one
      // object at a time using shared (non-tlab) allocations. This check
      // is testing that the GC overhead limit has not been exceeded.
      // This will notify the collector to start a cycle, but will raise
      // an OOME to the mutator if the last Full GCs have not made progress.
+     // gc_no_progress_count is incremented following each degen or full GC that fails to achieve is_good_progress().
      if (result == nullptr && !req.is_lab_alloc() && get_gc_no_progress_count() > ShenandoahNoProgressThreshold) {
        control_thread()->handle_alloc_failure(req, false);
+       req.set_actual_size(0);
        return nullptr;
      }
  
      if (result == nullptr) {
        // Block until control thread reacted, then retry allocation.

*** 946,23 ***
        // strategy is to try again, until at least one full GC has completed.
        //
        // Stop retrying and return nullptr to cause OOMError exception if our allocation failed even after:
        //   a) We experienced a GC that had good progress, or
        //   b) We experienced at least one Full GC (whether or not it had good progress)
-       //
-       // TODO: Consider GLOBAL GC rather than Full GC to remediate OOM condition: https://bugs.openjdk.org/browse/JDK-8335910
  
        size_t original_count = shenandoah_policy()->full_gc_count();
        while ((result == nullptr) && (original_count == shenandoah_policy()->full_gc_count())) {
          control_thread()->handle_alloc_failure(req, true);
          result = allocate_memory_under_lock(req, in_new_region);
        }
        if (result != nullptr) {
          // If our allocation request has been satisifed after it initially failed, we count this as good gc progress
          notify_gc_progress();
        }
!       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());
--- 1003,21 ---
        // strategy is to try again, until at least one full GC has completed.
        //
        // Stop retrying and return nullptr to cause OOMError exception if our allocation failed even after:
        //   a) We experienced a GC that had good progress, or
        //   b) We experienced at least one Full GC (whether or not it had good progress)
  
        size_t original_count = shenandoah_policy()->full_gc_count();
        while ((result == nullptr) && (original_count == shenandoah_policy()->full_gc_count())) {
          control_thread()->handle_alloc_failure(req, true);
          result = allocate_memory_under_lock(req, in_new_region);
        }
        if (result != nullptr) {
          // If our allocation request has been satisifed after it initially failed, we count this as good gc progress
          notify_gc_progress();
        }
!       if (log_develop_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());

*** 977,29 ***
  
    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;
  }
--- 1032,33 ---
  
    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;
  }

*** 1008,11 ***
    // 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);
--- 1067,47 ---
    // 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());
! 
+   // Make sure the old generation has room for either evacuations or promotions before trying to allocate.
+   if (req.is_old() && !old_generation()->can_allocate(req)) {
+     return nullptr;
+   }
+ 
+   // If TLAB request size is greater than available, allocate() will attempt to downsize request to fit within available
+   // memory.
+   HeapWord* result = _free_set->allocate(req, in_new_region);
+ 
+   // Record the plab configuration for this result and register the object.
+   if (result != nullptr && req.is_old()) {
+     old_generation()->configure_plab_for_current_thread(req);
+     if (req.type() == ShenandoahAllocRequest::_alloc_shared_gc) {
+       // 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.
+       old_generation()->card_scan()->register_object(result);
+     }
+   }
+ 
+   return result;
  }
  
  HeapWord* ShenandoahHeap::mem_allocate(size_t size,
                                          bool*  gc_overhead_limit_was_exceeded) {
    ShenandoahAllocRequest req = ShenandoahAllocRequest::for_shared(size);

*** 1023,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);
--- 1118,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);

*** 1122,24 ***
    ShenandoahEvacuationTask task(this, _collection_set, concurrent);
    workers()->run_task(&task);
  }
  
  oop ShenandoahHeap::evacuate_object(oop p, Thread* thread) {
!   if (ShenandoahThreadLocalData::is_oom_during_evac(Thread::current())) {
!     // This thread went through the OOM during evac protocol and it is safe to return
!     // the forward pointer. It must not attempt to evacuate any more.
      return ShenandoahBarrierSet::resolve_forwarded(p);
    }
  
    assert(ShenandoahThreadLocalData::is_evac_allowed(thread), "must be enclosed in oom-evac scope");
  
!   size_t size = ShenandoahForwarding::size(p);
  
!   assert(!heap_region_containing(p)->is_humongous(), "never evacuate humongous objects");
  
!   bool alloc_from_gclab = true;
    HeapWord* copy = nullptr;
  
  #ifdef ASSERT
    if (ShenandoahOOMDuringEvacALot &&
        (os::random() & 1) == 0) { // Simulate OOM every ~2nd slow-path call
      copy = nullptr;
--- 1217,33 ---
    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 = ShenandoahForwarding::size(p);
  
  #ifdef ASSERT
    if (ShenandoahOOMDuringEvacALot &&
        (os::random() & 1) == 0) { // Simulate OOM every ~2nd slow-path call
      copy = nullptr;

*** 1147,13 ***
  #endif
      if (UseTLAB) {
        copy = allocate_from_gclab(thread, size);
      }
      if (copy == nullptr) {
!       ShenandoahAllocRequest req = ShenandoahAllocRequest::for_shared_gc(size);
        copy = allocate_memory(req);
!       alloc_from_gclab = false;
      }
  #ifdef ASSERT
    }
  #endif
  
--- 1251,14 ---
  #endif
      if (UseTLAB) {
        copy = allocate_from_gclab(thread, size);
      }
      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
  

*** 1180,21 ***
      // 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.
!     //
!     // For GCLAB 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. For non-GCLAB 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.
-     if (alloc_from_gclab) {
        ShenandoahThreadLocalData::gclab(thread)->undo_allocation(copy, size);
      } else {
        fill_with_object(copy, size);
        shenandoah_assert_correct(nullptr, copy_val);
      }
      shenandoah_assert_correct(nullptr, result);
      return result;
    }
  }
--- 1285,23 ---
      // 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;
    }
  }

*** 1224,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);
--- 1331,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);

*** 1244,19 ***
--- 1351,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:

*** 1268,10 ***
--- 1382,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");

*** 1401,10 ***
--- 1528,50 ---
      ls.cr();
      ls.cr();
    }
  }
  
+ void ShenandoahHeap::set_gc_generation(ShenandoahGeneration* generation) {
+   shenandoah_assert_control_or_vm_thread_at_safepoint();
+   _gc_generation = generation;
+ }
+ 
+ // Active generation may only be set by the VM thread at a safepoint.
+ void ShenandoahHeap::set_active_generation() {
+   assert(Thread::current()->is_VM_thread(), "Only the VM Thread");
+   assert(SafepointSynchronize::is_at_safepoint(), "Only at a safepoint!");
+   assert(_gc_generation != nullptr, "Will set _active_generation to nullptr");
+   _active_generation = _gc_generation;
+ }
+ 
+ void ShenandoahHeap::on_cycle_start(GCCause::Cause cause, ShenandoahGeneration* generation) {
+   shenandoah_policy()->record_collection_cause(cause);
+ 
+   assert(gc_cause()  == GCCause::_no_gc, "Over-writing cause");
+   assert(_gc_generation == nullptr, "Over-writing _gc_generation");
+ 
+   set_gc_cause(cause);
+   set_gc_generation(generation);
+ 
+   generation->heuristics()->record_cycle_start();
+ }
+ 
+ void ShenandoahHeap::on_cycle_end(ShenandoahGeneration* generation) {
+   assert(gc_cause() != GCCause::_no_gc, "cause wasn't set");
+   assert(_gc_generation != nullptr, "_gc_generation wasn't set");
+ 
+   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_generation(nullptr);
+   set_gc_cause(GCCause::_no_gc);
+ }
+ 
  void ShenandoahHeap::verify(VerifyOption vo) {
    if (ShenandoahSafepoint::is_at_shenandoah_safepoint()) {
      if (ShenandoahVerify) {
        verifier()->verify_generic(vo);
      } else {

*** 1750,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");
  
--- 1917,25 ---
  
  void ShenandoahHeap::recycle_trash() {
    free_set()->recycle_trash();
  }
  
  void ShenandoahHeap::do_class_unloading() {
    _unloader.unload();
+   if (mode()->is_generational()) {
+     old_generation()->set_parsable(false);
+   }
  }
  
  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);
!   shenandoah_assert_generations_reconciled();
+   gc_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");
  

*** 1893,16 ***
  
  void ShenandoahHeap::set_gc_state(uint mask, bool value) {
    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);
--- 1965,67 ---
  
  void ShenandoahHeap::set_gc_state(uint mask, bool value) {
    assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at Shenandoah safepoint");
    _gc_state.set_cond(mask, value);
    _gc_state_changed = true;
+   // Check that if concurrent weak root is set then active_gen isn't null
+   assert(!is_concurrent_weak_root_in_progress() || active_generation() != nullptr, "Error");
+   shenandoah_assert_generations_reconciled();
+ }
+ 
+ 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()->is_preparing_for_mark();
+ }
+ 
+ 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);

*** 1931,15 ***
--- 2054,27 ---
  bool ShenandoahHeap::try_cancel_gc() {
    jbyte prev = _cancelled_gc.cmpxchg(CANCELLED, CANCELLABLE);
    return prev == CANCELLABLE;
  }
  
+ void ShenandoahHeap::cancel_concurrent_mark() {
+   if (mode()->is_generational()) {
+     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;

*** 2059,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);
  }
--- 2194,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);
  }

*** 2128,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 {
--- 2264,15 ---
  
  #ifdef ASSERT
  void ShenandoahHeap::assert_pinned_region_status() {
    for (size_t i = 0; i < num_regions(); i++) {
      ShenandoahHeapRegion* r = get_region(i);
!     shenandoah_assert_generations_reconciled();
!     if (gc_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 {

*** 2184,11 ***
  class ShenandoahUpdateHeapRefsTask : public WorkerTask {
  private:
    ShenandoahHeap* _heap;
    ShenandoahRegionIterator* _regions;
  public:
!   ShenandoahUpdateHeapRefsTask(ShenandoahRegionIterator* regions) :
      WorkerTask("Shenandoah Update References"),
      _heap(ShenandoahHeap::heap()),
      _regions(regions) {
    }
  
--- 2323,11 ---
  class ShenandoahUpdateHeapRefsTask : public WorkerTask {
  private:
    ShenandoahHeap* _heap;
    ShenandoahRegionIterator* _regions;
  public:
!   explicit ShenandoahUpdateHeapRefsTask(ShenandoahRegionIterator* regions) :
      WorkerTask("Shenandoah Update References"),
      _heap(ShenandoahHeap::heap()),
      _regions(regions) {
    }
  

*** 2204,31 ***
    }
  
  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_regions_from_collector_to_mutator(cset_regions);
      }
      // If !CONCURRENT, there's no value in expanding Mutator free set
! 
      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();
--- 2343,32 ---
    }
  
  private:
    template<class T>
    void do_work(uint worker_id) {
      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();
! 
!       // Now that evacuation is done, we can reassign any regions that had been reserved to hold the results of evacuation
+       // to the mutator free set.  At the end of GC, we will have cset_regions newly evacuated fully empty regions from
+       // which we will be able to replenish the Collector free set and the OldCollector free set in preparation for the
+       // next GC cycle.
        _heap->free_set()->move_regions_from_collector_to_mutator(cset_regions);
      }
      // If !CONCURRENT, there's no value in expanding Mutator free set
!     T cl;
      ShenandoahHeapRegion* r = _regions->next();
      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();

*** 2246,50 ***
      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();
-         }
-       } else {
-         if (r->pin_count() > 0) {
-           ShenandoahHeapLocker locker(_lock);
-           r->make_pinned();
-         }
        }
      }
    }
! 
-   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();
    }
  
    {
--- 2386,40 ---
      ShenandoahUpdateHeapRefsTask<false> task(&_update_refs_iterator);
      workers()->run_task(&task);
    }
  }
  
+ ShenandoahSynchronizePinnedRegionStates::ShenandoahSynchronizePinnedRegionStates() : _lock(ShenandoahHeap::heap()->lock()) { }
  
! void ShenandoahSynchronizePinnedRegionStates::heap_region_do(ShenandoahHeapRegion* r) {
!   // Drop "pinned" state from regions that no longer have a pinned count. Put
!   // regions with a pinned count into the "pinned" state.
!   if (r->is_active()) {
!     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();
        }
      }
    }
! }
  
  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);
! 
!     final_update_refs_update_region_states();
  
      assert_pinned_region_status();
    }
  
    {

*** 2298,17 ***
                              ShenandoahPhaseTimings::degen_gc_final_update_refs_trash_cset);
      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);
--- 2428,58 ---
                              ShenandoahPhaseTimings::degen_gc_final_update_refs_trash_cset);
      trash_cset_regions();
    }
  }
  
+ void ShenandoahHeap::final_update_refs_update_region_states() {
+   ShenandoahSynchronizePinnedRegionStates cl;
+   parallel_heap_region_iterate(&cl);
+ }
+ 
  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()) {
+ #ifdef ASSERT
+     if (ShenandoahVerify) {
+       verifier()->verify_before_rebuilding_free_set();
+     }
+ #endif
+ 
+     // 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.
+     ShenandoahGenerationalHeap* gen_heap = ShenandoahGenerationalHeap::heap();
+     size_t allocation_runway = gen_heap->young_generation()->heuristics()->bytes_of_allocation_runway_before_gc_trigger(young_cset_regions);
+     gen_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->finish_rebuild(young_cset_regions, old_cset_regions, old_region_count);
+ 
+   if (mode()->is_generational()) {
+     ShenandoahGenerationalHeap* gen_heap = ShenandoahGenerationalHeap::heap();
+     ShenandoahOldGeneration* old_gen = gen_heap->old_generation();
+     old_gen->heuristics()->evaluate_triggers(first_old_region, last_old_region, old_region_count, num_regions());
    }
  }
  
  void ShenandoahHeap::print_extended_on(outputStream *st) const {
    print_on(st);

*** 2427,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) {}
--- 2598,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) {}

*** 2569,5 ***
--- 2740,28 ---
    assert(end_reg->top() == end,
           "Must agree: archive-space-end: " PTR_FORMAT ", end-region-top: " PTR_FORMAT,
           p2i(end), p2i(end_reg->top()));
  #endif
  }
+ 
+ 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);
+   }
+ }
+ 
< prev index next >