< prev index next >

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

Print this page
*** 1,8 ***
--- 1,9 ---
  /*
   * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
   * Copyright (c) 2021, 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.

*** 30,10 ***
--- 31,14 ---
  #include "gc/shared/continuationGCSupport.inline.hpp"
  #include "gc/shenandoah/shenandoahBreakpoint.hpp"
  #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
  #include "gc/shenandoah/shenandoahConcurrentGC.hpp"
  #include "gc/shenandoah/shenandoahFreeSet.hpp"
+ #include "gc/shenandoah/shenandoahGeneration.hpp"
+ #include "gc/shenandoah/shenandoahGenerationalHeap.hpp"
+ #include "gc/shenandoah/shenandoahOldGeneration.hpp"
+ #include "gc/shenandoah/shenandoahYoungGeneration.hpp"
  #include "gc/shenandoah/shenandoahLock.hpp"
  #include "gc/shenandoah/shenandoahMark.inline.hpp"
  #include "gc/shenandoah/shenandoahMonitoringSupport.hpp"
  #include "gc/shenandoah/shenandoahOopClosures.inline.hpp"
  #include "gc/shenandoah/shenandoahPhaseTimings.hpp"

*** 84,39 ***
        ShenandoahBreakpoint::at_before_marking_completed();
      }
    }
  };
  
! ShenandoahConcurrentGC::ShenandoahConcurrentGC() :
!   _mark(),
    _degen_point(ShenandoahDegenPoint::_degenerated_unset),
!   _abbreviated(false) {
  }
  
  ShenandoahGC::ShenandoahDegenPoint ShenandoahConcurrentGC::degen_point() const {
    return _degen_point;
  }
  
- void ShenandoahConcurrentGC::cancel() {
-   ShenandoahConcurrentMark::cancel();
- }
- 
  bool ShenandoahConcurrentGC::collect(GCCause::Cause cause) {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
    ShenandoahBreakpointGCScope breakpoint_gc_scope(cause);
  
    // Reset for upcoming marking
    entry_reset();
  
    // Start initial mark under STW
    vmop_entry_init_mark();
  
    {
      ShenandoahBreakpointMarkScope breakpoint_mark_scope(cause);
      // Concurrent mark roots
      entry_mark_roots();
!     if (check_cancellation_and_abort(ShenandoahDegenPoint::_degenerated_outside_cycle)) {
        return false;
      }
  
      // Continue concurrent mark
      entry_mark();
--- 89,47 ---
        ShenandoahBreakpoint::at_before_marking_completed();
      }
    }
  };
  
! ShenandoahConcurrentGC::ShenandoahConcurrentGC(ShenandoahGeneration* generation, bool do_old_gc_bootstrap) :
!   _mark(generation),
    _degen_point(ShenandoahDegenPoint::_degenerated_unset),
!   _abbreviated(false),
+   _do_old_gc_bootstrap(do_old_gc_bootstrap),
+   _generation(generation) {
  }
  
  ShenandoahGC::ShenandoahDegenPoint ShenandoahConcurrentGC::degen_point() const {
    return _degen_point;
  }
  
  bool ShenandoahConcurrentGC::collect(GCCause::Cause cause) {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
+ 
    ShenandoahBreakpointGCScope breakpoint_gc_scope(cause);
  
    // Reset for upcoming marking
    entry_reset();
  
    // Start initial mark under STW
    vmop_entry_init_mark();
  
    {
      ShenandoahBreakpointMarkScope breakpoint_mark_scope(cause);
+ 
+     // Reset task queue stats here, rather than in mark_concurrent_roots,
+     // because remembered set scan will `push` oops into the queues and
+     // resetting after this happens will lose those counts.
+     TASKQUEUE_STATS_ONLY(_mark.task_queues()->reset_taskqueue_stats());
+ 
+     // Concurrent remembered set scanning
+     entry_scan_remembered_set();
+ 
      // Concurrent mark roots
      entry_mark_roots();
!     if (check_cancellation_and_abort(ShenandoahDegenPoint::_degenerated_roots)) {
        return false;
      }
  
      // Continue concurrent mark
      entry_mark();

*** 130,11 ***
  
    // If the GC was cancelled before final mark, nothing happens on the safepoint. We are still
    // in the marking phase and must resume the degenerated cycle from there. If the GC was cancelled
    // after final mark, then we've entered the evacuation phase and must resume the degenerated cycle
    // from that phase.
!   if (heap->is_concurrent_mark_in_progress()) {
      bool cancelled = check_cancellation_and_abort(ShenandoahDegenPoint::_degenerated_mark);
      assert(cancelled, "GC must have been cancelled between concurrent and final mark");
      return false;
    }
  
--- 143,11 ---
  
    // If the GC was cancelled before final mark, nothing happens on the safepoint. We are still
    // in the marking phase and must resume the degenerated cycle from there. If the GC was cancelled
    // after final mark, then we've entered the evacuation phase and must resume the degenerated cycle
    // from that phase.
!   if (_generation->is_concurrent_mark_in_progress()) {
      bool cancelled = check_cancellation_and_abort(ShenandoahDegenPoint::_degenerated_mark);
      assert(cancelled, "GC must have been cancelled between concurrent and final mark");
      return false;
    }
  

*** 148,11 ***
      entry_weak_refs();
      entry_weak_roots();
    }
  
    // Final mark might have reclaimed some immediate garbage, kick cleanup to reclaim
!   // the space. This would be the last action if there is nothing to evacuate.
    entry_cleanup_early();
  
    heap->free_set()->log_status_under_lock();
  
    // Perform concurrent class unloading
--- 161,12 ---
      entry_weak_refs();
      entry_weak_roots();
    }
  
    // Final mark might have reclaimed some immediate garbage, kick cleanup to reclaim
!   // the space. This would be the last action if there is nothing to evacuate.  Note that
+   // we will not age young-gen objects in the case that we skip evacuation.
    entry_cleanup_early();
  
    heap->free_set()->log_status_under_lock();
  
    // Perform concurrent class unloading

*** 194,14 ***
--- 208,36 ---
      vmop_entry_final_updaterefs();
  
      // Update references freed up collection set, kick the cleanup to reclaim the space.
      entry_cleanup_complete();
    } else {
+     // We chose not to evacuate because we found sufficient immediate garbage.
+     // However, there may still be regions to promote in place, so do that now.
+     if (has_in_place_promotions(heap)) {
+       entry_promote_in_place();
+ 
+       // If the promote-in-place operation was cancelled, we can have the degenerated
+       // cycle complete the operation. It will see that no evacuations are in progress,
+       // and that there are regions wanting promotion. The risk with not handling the
+       // cancellation would be failing to restore top for these regions and leaving
+       // them unable to serve allocations for the old generation.
+       if (check_cancellation_and_abort(ShenandoahDegenPoint::_degenerated_evac)) {
+         return false;
+       }
+     }
+ 
+     // At this point, the cycle is effectively complete. If the cycle has been cancelled here,
+     // the control thread will detect it on its next iteration and run a degenerated young cycle.
      vmop_entry_final_roots();
      _abbreviated = true;
    }
  
+   // We defer generation resizing actions until after cset regions have been recycled.  We do this even following an
+   // abbreviated cycle.
+   if (heap->mode()->is_generational()) {
+     ShenandoahGenerationalHeap::heap()->complete_concurrent_cycle();
+   }
    return true;
  }
  
  void ShenandoahConcurrentGC::vmop_entry_init_mark() {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();

*** 298,30 ***
  
    op_final_updaterefs();
  }
  
  void ShenandoahConcurrentGC::entry_final_roots() {
!   static const char* msg = "Pause Final Roots";
    ShenandoahPausePhase gc_phase(msg, ShenandoahPhaseTimings::final_roots);
    EventMark em("%s", msg);
  
    op_final_roots();
  }
  
  void ShenandoahConcurrentGC::entry_reset() {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
    TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
!   static const char* msg = "Concurrent reset";
!   ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::conc_reset);
!   EventMark em("%s", msg);
  
!   ShenandoahWorkerScope scope(heap->workers(),
!                               ShenandoahWorkerPolicy::calc_workers_for_conc_reset(),
!                               "concurrent reset");
  
!   heap->try_inject_alloc_failure();
!   op_reset();
  }
  
  void ShenandoahConcurrentGC::entry_mark_roots() {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
    TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
--- 334,60 ---
  
    op_final_updaterefs();
  }
  
  void ShenandoahConcurrentGC::entry_final_roots() {
!   const char* msg = final_roots_event_message();
    ShenandoahPausePhase gc_phase(msg, ShenandoahPhaseTimings::final_roots);
    EventMark em("%s", msg);
  
    op_final_roots();
  }
  
  void ShenandoahConcurrentGC::entry_reset() {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
+   heap->try_inject_alloc_failure();
+ 
    TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
!   {
!     const char* msg = conc_reset_event_message();
!     ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::conc_reset);
+     EventMark em("%s", msg);
+ 
+     ShenandoahWorkerScope scope(heap->workers(),
+                                 ShenandoahWorkerPolicy::calc_workers_for_conc_reset(),
+                                 msg);
+     op_reset();
+   }
  
!   if (_do_old_gc_bootstrap) {
!     static const char* msg = "Concurrent reset (OLD)";
!     ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::conc_reset_old);
+     ShenandoahWorkerScope scope(ShenandoahHeap::heap()->workers(),
+                                 ShenandoahWorkerPolicy::calc_workers_for_conc_reset(),
+                                 msg);
+     EventMark em("%s", msg);
  
!     heap->old_generation()->prepare_gc();
!   }
+ }
+ 
+ void ShenandoahConcurrentGC::entry_scan_remembered_set() {
+   if (_generation->is_young()) {
+     ShenandoahHeap* const heap = ShenandoahHeap::heap();
+     TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
+     const char* msg = "Concurrent remembered set scanning";
+     ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::init_scan_rset);
+     EventMark em("%s", msg);
+ 
+     ShenandoahWorkerScope scope(heap->workers(),
+                                 ShenandoahWorkerPolicy::calc_workers_for_rs_scanning(),
+                                 msg);
+ 
+     heap->try_inject_alloc_failure();
+     _generation->scan_remembered_set(true /* is_concurrent */);
+   }
  }
  
  void ShenandoahConcurrentGC::entry_mark_roots() {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
    TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());

*** 366,11 ***
    op_thread_roots();
  }
  
  void ShenandoahConcurrentGC::entry_weak_refs() {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
!   static const char* msg = "Concurrent weak references";
    ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::conc_weak_refs);
    EventMark em("%s", msg);
  
    ShenandoahWorkerScope scope(heap->workers(),
                                ShenandoahWorkerPolicy::calc_workers_for_conc_refs_processing(),
--- 432,11 ---
    op_thread_roots();
  }
  
  void ShenandoahConcurrentGC::entry_weak_refs() {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
!   const char* msg = conc_weak_refs_event_message();
    ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::conc_weak_refs);
    EventMark em("%s", msg);
  
    ShenandoahWorkerScope scope(heap->workers(),
                                ShenandoahWorkerPolicy::calc_workers_for_conc_refs_processing(),

*** 381,11 ***
  }
  
  void ShenandoahConcurrentGC::entry_weak_roots() {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
    TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
!   static const char* msg = "Concurrent weak roots";
    ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::conc_weak_roots);
    EventMark em("%s", msg);
  
    ShenandoahWorkerScope scope(heap->workers(),
                                ShenandoahWorkerPolicy::calc_workers_for_conc_root_processing(),
--- 447,11 ---
  }
  
  void ShenandoahConcurrentGC::entry_weak_roots() {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
    TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
!   const char* msg = conc_weak_roots_event_message();
    ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::conc_weak_roots);
    EventMark em("%s", msg);
  
    ShenandoahWorkerScope scope(heap->workers(),
                                ShenandoahWorkerPolicy::calc_workers_for_conc_root_processing(),

*** 428,11 ***
  }
  
  void ShenandoahConcurrentGC::entry_cleanup_early() {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
    TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
!   static const char* msg = "Concurrent cleanup";
    ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::conc_cleanup_early, true /* log_heap_usage */);
    EventMark em("%s", msg);
  
    // This phase does not use workers, no need for setup
    heap->try_inject_alloc_failure();
--- 494,11 ---
  }
  
  void ShenandoahConcurrentGC::entry_cleanup_early() {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
    TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
!   const char* msg = conc_cleanup_event_message();
    ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::conc_cleanup_early, true /* log_heap_usage */);
    EventMark em("%s", msg);
  
    // This phase does not use workers, no need for setup
    heap->try_inject_alloc_failure();

*** 453,10 ***
--- 519,27 ---
  
    heap->try_inject_alloc_failure();
    op_evacuate();
  }
  
+ void ShenandoahConcurrentGC::entry_promote_in_place() {
+   shenandoah_assert_generational();
+ 
+   ShenandoahHeap* const heap = ShenandoahHeap::heap();
+   TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
+ 
+   static const char* msg = "Promote in place";
+   ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::promote_in_place);
+   EventMark em("%s", msg);
+ 
+   ShenandoahWorkerScope scope(heap->workers(),
+                               ShenandoahWorkerPolicy::calc_workers_for_conc_evac(),
+                               "promote in place");
+ 
+   ShenandoahGenerationalHeap::heap()->promote_regions_in_place(true);
+ }
+ 
  void ShenandoahConcurrentGC::entry_update_thread_roots() {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
    TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
  
    static const char* msg = "Concurrent update thread roots";

*** 484,11 ***
  }
  
  void ShenandoahConcurrentGC::entry_cleanup_complete() {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
    TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
!   static const char* msg = "Concurrent cleanup";
    ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::conc_cleanup_complete, true /* log_heap_usage */);
    EventMark em("%s", msg);
  
    // This phase does not use workers, no need for setup
    heap->try_inject_alloc_failure();
--- 567,11 ---
  }
  
  void ShenandoahConcurrentGC::entry_cleanup_complete() {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
    TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
!   const char* msg = conc_cleanup_event_message();
    ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::conc_cleanup_complete, true /* log_heap_usage */);
    EventMark em("%s", msg);
  
    // This phase does not use workers, no need for setup
    heap->try_inject_alloc_failure();

*** 498,12 ***
  void ShenandoahConcurrentGC::op_reset() {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
    if (ShenandoahPacing) {
      heap->pacer()->setup_for_reset();
    }
! 
-   heap->prepare_gc();
  }
  
  class ShenandoahInitMarkUpdateRegionStateClosure : public ShenandoahHeapRegionClosure {
  private:
    ShenandoahMarkingContext* const _ctx;
--- 581,11 ---
  void ShenandoahConcurrentGC::op_reset() {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
    if (ShenandoahPacing) {
      heap->pacer()->setup_for_reset();
    }
!   _generation->prepare_gc();
  }
  
  class ShenandoahInitMarkUpdateRegionStateClosure : public ShenandoahHeapRegionClosure {
  private:
    ShenandoahMarkingContext* const _ctx;

*** 512,11 ***
  
    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(),
--- 594,12 ---
  
    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.  Since most regions
+       // are not "active", this path is relatively rare.
        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(),

*** 534,34 ***
  void ShenandoahConcurrentGC::op_init_mark() {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
    assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Should be at safepoint");
    assert(Thread::current()->is_VM_thread(), "can only do this in VMThread");
  
!   assert(heap->marking_context()->is_bitmap_clear(), "need clear marking bitmap");
!   assert(!heap->marking_context()->is_complete(), "should not be complete");
    assert(!heap->has_forwarded_objects(), "No forwarded objects on this path");
  
    if (ShenandoahVerify) {
      heap->verifier()->verify_before_concmark();
    }
  
    if (VerifyBeforeGC) {
      Universe::verify();
    }
  
!   heap->set_concurrent_mark_in_progress(true);
  
    start_mark();
  
!   {
      ShenandoahGCPhase phase(ShenandoahPhaseTimings::init_update_region_states);
      ShenandoahInitMarkUpdateRegionStateClosure cl;
      heap->parallel_heap_region_iterate(&cl);
    }
  
    // Weak reference processing
!   ShenandoahReferenceProcessor* rp = heap->ref_processor();
    rp->reset_thread_locals();
    rp->set_soft_reference_policy(heap->soft_ref_policy()->should_clear_all_soft_refs());
  
    // Make above changes visible to worker threads
    OrderAccess::fence();
--- 617,61 ---
  void ShenandoahConcurrentGC::op_init_mark() {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
    assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Should be at safepoint");
    assert(Thread::current()->is_VM_thread(), "can only do this in VMThread");
  
!   assert(_generation->is_bitmap_clear(), "need clear marking bitmap");
!   assert(!_generation->is_mark_complete(), "should not be complete");
    assert(!heap->has_forwarded_objects(), "No forwarded objects on this path");
  
+ 
+   if (heap->mode()->is_generational()) {
+     if (_generation->is_young()) {
+       // The current implementation of swap_remembered_set() copies the write-card-table to the read-card-table.
+       ShenandoahGCPhase phase(ShenandoahPhaseTimings::init_swap_rset);
+       _generation->swap_remembered_set();
+     }
+ 
+     if (_generation->is_global()) {
+       heap->old_generation()->cancel_gc();
+     } else if (heap->is_concurrent_old_mark_in_progress()) {
+       // Purge the SATB buffers, transferring any valid, old pointers to the
+       // old generation mark queue. Any pointers in a young region will be
+       // abandoned.
+       ShenandoahGCPhase phase(ShenandoahPhaseTimings::init_transfer_satb);
+       heap->old_generation()->transfer_pointers_from_satb();
+     }
+   }
+ 
    if (ShenandoahVerify) {
      heap->verifier()->verify_before_concmark();
    }
  
    if (VerifyBeforeGC) {
      Universe::verify();
    }
  
!   _generation->set_concurrent_mark_in_progress(true);
  
    start_mark();
  
!   if (_do_old_gc_bootstrap) {
+     shenandoah_assert_generational();
+     // Update region state for both young and old regions
      ShenandoahGCPhase phase(ShenandoahPhaseTimings::init_update_region_states);
      ShenandoahInitMarkUpdateRegionStateClosure cl;
      heap->parallel_heap_region_iterate(&cl);
+     heap->old_generation()->ref_processor()->reset_thread_locals();
+   } else {
+     // Update region state for only young regions
+     ShenandoahGCPhase phase(ShenandoahPhaseTimings::init_update_region_states);
+     ShenandoahInitMarkUpdateRegionStateClosure cl;
+     _generation->parallel_heap_region_iterate(&cl);
    }
  
    // Weak reference processing
!   ShenandoahReferenceProcessor* rp = _generation->ref_processor();
    rp->reset_thread_locals();
    rp->set_soft_reference_policy(heap->soft_ref_policy()->should_clear_all_soft_refs());
  
    // Make above changes visible to worker threads
    OrderAccess::fence();

*** 597,16 ***
      assert(!heap->cancelled_gc(), "STW mark cannot OOM");
  
      // Notify JVMTI that the tagmap table will need cleaning.
      JvmtiTagMap::set_needs_cleaning();
  
!     heap->prepare_regions_and_collection_set(true /*concurrent*/);
  
      // Has to be done after cset selection
      heap->prepare_concurrent_roots();
  
      if (!heap->collection_set()->is_empty()) {
        if (ShenandoahVerify) {
          heap->verifier()->verify_before_evacuation();
        }
  
        heap->set_evacuation_in_progress(true);
--- 707,26 ---
      assert(!heap->cancelled_gc(), "STW mark cannot OOM");
  
      // Notify JVMTI that the tagmap table will need cleaning.
      JvmtiTagMap::set_needs_cleaning();
  
!     // The collection set is chosen by prepare_regions_and_collection_set(). Additionally, certain parameters have been
+     // established to govern the evacuation efforts that are about to begin.  Refer to comments on reserve members in
+     // ShenandoahGeneration and ShenandoahOldGeneration for more detail.
+     _generation->prepare_regions_and_collection_set(true /*concurrent*/);
  
      // Has to be done after cset selection
      heap->prepare_concurrent_roots();
  
      if (!heap->collection_set()->is_empty()) {
+       LogTarget(Debug, gc, cset) lt;
+       if (lt.is_enabled()) {
+         ResourceMark rm;
+         LogStream ls(lt);
+         heap->collection_set()->print_on(&ls);
+       }
+ 
        if (ShenandoahVerify) {
          heap->verifier()->verify_before_evacuation();
        }
  
        heap->set_evacuation_in_progress(true);

*** 620,74 ***
        if (ShenandoahPacing) {
          heap->pacer()->setup_for_evac();
        }
      } else {
        if (ShenandoahVerify) {
!         heap->verifier()->verify_after_concmark();
        }
  
        if (VerifyAfterGC) {
          Universe::verify();
        }
      }
    }
  }
  
  class ShenandoahConcurrentEvacThreadClosure : public ThreadClosure {
  private:
    OopClosure* const _oops;
- 
  public:
!   ShenandoahConcurrentEvacThreadClosure(OopClosure* oops);
-   void do_thread(Thread* thread);
- };
  
! ShenandoahConcurrentEvacThreadClosure::ShenandoahConcurrentEvacThreadClosure(OopClosure* oops) :
!   _oops(oops) {
! }
! 
! void ShenandoahConcurrentEvacThreadClosure::do_thread(Thread* thread) {
!   JavaThread* const jt = JavaThread::cast(thread);
!   StackWatermarkSet::finish_processing(jt, _oops, StackWatermarkKind::gc);
! }
  
  class ShenandoahConcurrentEvacUpdateThreadTask : public WorkerTask {
  private:
    ShenandoahJavaThreadsIterator _java_threads;
  
  public:
!   ShenandoahConcurrentEvacUpdateThreadTask(uint n_workers) :
      WorkerTask("Shenandoah Evacuate/Update Concurrent Thread Roots"),
      _java_threads(ShenandoahPhaseTimings::conc_thread_roots, n_workers) {
    }
  
!   void work(uint worker_id) {
      // ShenandoahEvacOOMScope has to be setup by ShenandoahContextEvacuateUpdateRootsClosure.
      // Otherwise, may deadlock with watermark lock
      ShenandoahContextEvacuateUpdateRootsClosure oops_cl;
!     ShenandoahConcurrentEvacThreadClosure thr_cl(&oops_cl);
      _java_threads.threads_do(&thr_cl, worker_id);
    }
  };
  
  void ShenandoahConcurrentGC::op_thread_roots() {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
    assert(heap->is_evacuation_in_progress(), "Checked by caller");
    ShenandoahGCWorkerPhase worker_phase(ShenandoahPhaseTimings::conc_thread_roots);
!   ShenandoahConcurrentEvacUpdateThreadTask task(heap->workers()->active_workers());
!   heap->workers()->run_task(&task);
  }
  
  void ShenandoahConcurrentGC::op_weak_refs() {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
    assert(heap->is_concurrent_weak_root_in_progress(), "Only during this phase");
    // Concurrent weak refs processing
    ShenandoahGCWorkerPhase worker_phase(ShenandoahPhaseTimings::conc_weak_refs);
    if (heap->gc_cause() == GCCause::_wb_breakpoint) {
      ShenandoahBreakpoint::at_after_reference_processing_started();
    }
!   heap->ref_processor()->process_references(ShenandoahPhaseTimings::conc_weak_refs, heap->workers(), true /* concurrent */);
  }
  
  class ShenandoahEvacUpdateCleanupOopStorageRootsClosure : public BasicOopIterateClosure {
  private:
    ShenandoahHeap* const _heap;
--- 740,91 ---
        if (ShenandoahPacing) {
          heap->pacer()->setup_for_evac();
        }
      } else {
        if (ShenandoahVerify) {
!         if (has_in_place_promotions(heap)) {
+           heap->verifier()->verify_after_concmark_with_promotions();
+         } else {
+           heap->verifier()->verify_after_concmark();
+         }
        }
  
        if (VerifyAfterGC) {
          Universe::verify();
        }
      }
    }
  }
  
+ bool ShenandoahConcurrentGC::has_in_place_promotions(ShenandoahHeap* heap) {
+   return heap->mode()->is_generational() && heap->old_generation()->has_in_place_promotions();
+ }
+ 
+ template<bool GENERATIONAL>
  class ShenandoahConcurrentEvacThreadClosure : public ThreadClosure {
  private:
    OopClosure* const _oops;
  public:
!   explicit ShenandoahConcurrentEvacThreadClosure(OopClosure* oops) : _oops(oops) {}
  
!   void do_thread(Thread* thread) override {
!     JavaThread* const jt = JavaThread::cast(thread);
!     StackWatermarkSet::finish_processing(jt, _oops, StackWatermarkKind::gc);
!     if (GENERATIONAL) {
!       ShenandoahThreadLocalData::enable_plab_promotions(thread);
!     }
!   }
! };
  
+ template<bool GENERATIONAL>
  class ShenandoahConcurrentEvacUpdateThreadTask : public WorkerTask {
  private:
    ShenandoahJavaThreadsIterator _java_threads;
  
  public:
!   explicit ShenandoahConcurrentEvacUpdateThreadTask(uint n_workers) :
      WorkerTask("Shenandoah Evacuate/Update Concurrent Thread Roots"),
      _java_threads(ShenandoahPhaseTimings::conc_thread_roots, n_workers) {
    }
  
!   void work(uint worker_id) override {
+     if (GENERATIONAL) {
+       Thread* worker_thread = Thread::current();
+       ShenandoahThreadLocalData::enable_plab_promotions(worker_thread);
+     }
+ 
      // ShenandoahEvacOOMScope has to be setup by ShenandoahContextEvacuateUpdateRootsClosure.
      // Otherwise, may deadlock with watermark lock
      ShenandoahContextEvacuateUpdateRootsClosure oops_cl;
!     ShenandoahConcurrentEvacThreadClosure<GENERATIONAL> thr_cl(&oops_cl);
      _java_threads.threads_do(&thr_cl, worker_id);
    }
  };
  
  void ShenandoahConcurrentGC::op_thread_roots() {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
    assert(heap->is_evacuation_in_progress(), "Checked by caller");
    ShenandoahGCWorkerPhase worker_phase(ShenandoahPhaseTimings::conc_thread_roots);
!   if (heap->mode()->is_generational()) {
!     ShenandoahConcurrentEvacUpdateThreadTask<true> task(heap->workers()->active_workers());
+     heap->workers()->run_task(&task);
+   } else {
+     ShenandoahConcurrentEvacUpdateThreadTask<false> task(heap->workers()->active_workers());
+     heap->workers()->run_task(&task);
+   }
  }
  
  void ShenandoahConcurrentGC::op_weak_refs() {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
    assert(heap->is_concurrent_weak_root_in_progress(), "Only during this phase");
    // Concurrent weak refs processing
    ShenandoahGCWorkerPhase worker_phase(ShenandoahPhaseTimings::conc_weak_refs);
    if (heap->gc_cause() == GCCause::_wb_breakpoint) {
      ShenandoahBreakpoint::at_after_reference_processing_started();
    }
!   _generation->ref_processor()->process_references(ShenandoahPhaseTimings::conc_weak_refs, heap->workers(), true /* concurrent */);
  }
  
  class ShenandoahEvacUpdateCleanupOopStorageRootsClosure : public BasicOopIterateClosure {
  private:
    ShenandoahHeap* const _heap;

*** 710,12 ***
  
  void ShenandoahEvacUpdateCleanupOopStorageRootsClosure::do_oop(oop* p) {
    const oop obj = RawAccess<>::oop_load(p);
    if (!CompressedOops::is_null(obj)) {
      if (!_mark_context->is_marked(obj)) {
!       // Note: The obj is dead here. Do not touch it, just clear.
!       ShenandoahHeap::atomic_clear_oop(p, obj);
      } else if (_evac_in_progress && _heap->in_collection_set(obj)) {
        oop resolved = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
        if (resolved == obj) {
          resolved = _heap->evacuate_object(obj, _thread);
        }
--- 847,15 ---
  
  void ShenandoahEvacUpdateCleanupOopStorageRootsClosure::do_oop(oop* p) {
    const oop obj = RawAccess<>::oop_load(p);
    if (!CompressedOops::is_null(obj)) {
      if (!_mark_context->is_marked(obj)) {
!       shenandoah_assert_generations_reconciled();
!       if (_heap->is_in_active_generation(obj)) {
+         // Note: The obj is dead here. Do not touch it, just clear.
+         ShenandoahHeap::atomic_clear_oop(p, obj);
+       }
      } else if (_evac_in_progress && _heap->in_collection_set(obj)) {
        oop resolved = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
        if (resolved == obj) {
          resolved = _heap->evacuate_object(obj, _thread);
        }

*** 817,10 ***
--- 957,13 ---
    // Perform handshake to flush out dead oops
    {
      ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_weak_roots_rendezvous);
      heap->rendezvous_threads("Shenandoah Concurrent Weak Roots");
    }
+   // We can only toggle concurrent_weak_root_in_progress flag
+   // at a safepoint, so that mutators see a consistent
+   // value. The flag will be cleared at the next safepoint.
  }
  
  void ShenandoahConcurrentGC::op_class_unloading() {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
    assert (heap->is_concurrent_weak_root_in_progress() &&

*** 913,15 ***
  void ShenandoahConcurrentGC::op_init_updaterefs() {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
    heap->set_evacuation_in_progress(false);
    heap->set_concurrent_weak_root_in_progress(false);
    heap->prepare_update_heap_references(true /*concurrent*/);
    if (ShenandoahVerify) {
      heap->verifier()->verify_before_updaterefs();
    }
- 
-   heap->set_update_refs_in_progress(true);
    if (ShenandoahPacing) {
      heap->pacer()->setup_for_updaterefs();
    }
  }
  
--- 1056,14 ---
  void ShenandoahConcurrentGC::op_init_updaterefs() {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
    heap->set_evacuation_in_progress(false);
    heap->set_concurrent_weak_root_in_progress(false);
    heap->prepare_update_heap_references(true /*concurrent*/);
+   heap->set_update_refs_in_progress(true);
    if (ShenandoahVerify) {
      heap->verifier()->verify_before_updaterefs();
    }
    if (ShenandoahPacing) {
      heap->pacer()->setup_for_updaterefs();
    }
  }
  

*** 962,23 ***
    heap->finish_concurrent_roots();
  
    // Clear cancelled GC, if set. On cancellation path, the block before would handle
    // everything.
    if (heap->cancelled_gc()) {
!     heap->clear_cancelled_gc();
    }
  
    // Has to be done before cset is clear
    if (ShenandoahVerify) {
      heap->verifier()->verify_roots_in_to_space();
    }
  
    heap->update_heap_region_states(true /*concurrent*/);
  
    heap->set_update_refs_in_progress(false);
    heap->set_has_forwarded_objects(false);
  
    if (ShenandoahVerify) {
      heap->verifier()->verify_after_updaterefs();
    }
  
    if (VerifyAfterGC) {
--- 1104,47 ---
    heap->finish_concurrent_roots();
  
    // Clear cancelled GC, if set. On cancellation path, the block before would handle
    // everything.
    if (heap->cancelled_gc()) {
!     heap->clear_cancelled_gc(true /* clear oom handler */);
    }
  
    // Has to be done before cset is clear
    if (ShenandoahVerify) {
      heap->verifier()->verify_roots_in_to_space();
    }
  
+   // If we are running in generational mode and this is an aging cycle, this will also age active
+   // regions that haven't been used for allocation.
    heap->update_heap_region_states(true /*concurrent*/);
  
    heap->set_update_refs_in_progress(false);
    heap->set_has_forwarded_objects(false);
  
+   if (heap->mode()->is_generational() && heap->is_concurrent_old_mark_in_progress()) {
+     // When the SATB barrier is left on to support concurrent old gen mark, it may pick up writes to
+     // objects in the collection set. After those objects are evacuated, the pointers in the
+     // SATB are no longer safe. Once we have finished update references, we are guaranteed that
+     // no more writes to the collection set are possible.
+     //
+     // This will transfer any old pointers in _active_ regions from the SATB to the old gen
+     // mark queues. All other pointers will be discarded. This would also discard any pointers
+     // in old regions that were included in a mixed evacuation. We aren't using the SATB filter
+     // methods here because we cannot control when they execute. If the SATB filter runs _after_
+     // a region has been recycled, we will not be able to detect the bad pointer.
+     //
+     // We are not concerned about skipping this step in abbreviated cycles because regions
+     // with no live objects cannot have been written to and so cannot have entries in the SATB
+     // buffers.
+     heap->old_generation()->transfer_pointers_from_satb();
+ 
+     // Aging_cycle is only relevant during evacuation cycle for individual objects and during final mark for
+     // entire regions.  Both of these relevant operations occur before final update refs.
+     ShenandoahGenerationalHeap::heap()->set_aging_cycle(false);
+   }
+ 
    if (ShenandoahVerify) {
      heap->verifier()->verify_after_updaterefs();
    }
  
    if (VerifyAfterGC) {

*** 987,11 ***
  
    heap->rebuild_free_set(true /*concurrent*/);
  }
  
  void ShenandoahConcurrentGC::op_final_roots() {
!   ShenandoahHeap::heap()->set_concurrent_weak_root_in_progress(false);
  }
  
  void ShenandoahConcurrentGC::op_cleanup_complete() {
    ShenandoahHeap::heap()->free_set()->recycle_trash();
  }
--- 1153,27 ---
  
    heap->rebuild_free_set(true /*concurrent*/);
  }
  
  void ShenandoahConcurrentGC::op_final_roots() {
! 
+   ShenandoahHeap *heap = ShenandoahHeap::heap();
+   heap->set_concurrent_weak_root_in_progress(false);
+   heap->set_evacuation_in_progress(false);
+ 
+   if (heap->mode()->is_generational()) {
+     // If the cycle was shortened for having enough immediate garbage, this could be
+     // the last GC safepoint before concurrent marking of old resumes. We must be sure
+     // that old mark threads don't see any pointers to garbage in the SATB buffers.
+     if (heap->is_concurrent_old_mark_in_progress()) {
+       heap->old_generation()->transfer_pointers_from_satb();
+     }
+ 
+     if (!_generation->is_old()) {
+       ShenandoahGenerationalHeap::heap()->update_region_ages(_generation->complete_marking_context());
+     }
+   }
  }
  
  void ShenandoahConcurrentGC::op_cleanup_complete() {
    ShenandoahHeap::heap()->free_set()->recycle_trash();
  }

*** 1006,30 ***
  
  const char* ShenandoahConcurrentGC::init_mark_event_message() const {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
    assert(!heap->has_forwarded_objects(), "Should not have forwarded objects here");
    if (heap->unload_classes()) {
!     return "Pause Init Mark (unload classes)";
    } else {
!     return "Pause Init Mark";
    }
  }
  
  const char* ShenandoahConcurrentGC::final_mark_event_message() const {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
!   assert(!heap->has_forwarded_objects(), "Should not have forwarded objects here");
    if (heap->unload_classes()) {
!     return "Pause Final Mark (unload classes)";
    } else {
!     return "Pause Final Mark";
    }
  }
  
  const char* ShenandoahConcurrentGC::conc_mark_event_message() const {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
!   assert(!heap->has_forwarded_objects(), "Should not have forwarded objects here");
    if (heap->unload_classes()) {
!     return "Concurrent marking (unload classes)";
    } else {
!     return "Concurrent marking";
    }
  }
--- 1188,73 ---
  
  const char* ShenandoahConcurrentGC::init_mark_event_message() const {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
    assert(!heap->has_forwarded_objects(), "Should not have forwarded objects here");
    if (heap->unload_classes()) {
!     SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Init Mark", " (unload classes)");
    } else {
!     SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Init Mark", "");
    }
  }
  
  const char* ShenandoahConcurrentGC::final_mark_event_message() const {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
!   assert(!heap->has_forwarded_objects() || heap->is_concurrent_old_mark_in_progress(),
+          "Should not have forwarded objects during final mark, unless old gen concurrent mark is running");
+ 
    if (heap->unload_classes()) {
!     SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Final Mark", " (unload classes)");
    } else {
!     SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Final Mark", "");
    }
  }
  
  const char* ShenandoahConcurrentGC::conc_mark_event_message() const {
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
!   assert(!heap->has_forwarded_objects() || heap->is_concurrent_old_mark_in_progress(),
+          "Should not have forwarded objects concurrent mark, unless old gen concurrent mark is running");
    if (heap->unload_classes()) {
!     SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Concurrent marking", " (unload classes)");
+   } else {
+     SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Concurrent marking", "");
+   }
+ }
+ 
+ const char* ShenandoahConcurrentGC::conc_reset_event_message() const {
+   if (ShenandoahHeap::heap()->unload_classes()) {
+     SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Concurrent reset", " (unload classes)");
+   } else {
+     SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Concurrent reset", "");
+   }
+ }
+ 
+ const char* ShenandoahConcurrentGC::final_roots_event_message() const {
+   if (ShenandoahHeap::heap()->unload_classes()) {
+     SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Final Roots", " (unload classes)");
+   } else {
+     SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Final Roots", "");
+   }
+ }
+ 
+ const char* ShenandoahConcurrentGC::conc_weak_refs_event_message() const {
+   if (ShenandoahHeap::heap()->unload_classes()) {
+     SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Concurrent weak references", " (unload classes)");
+   } else {
+     SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Concurrent weak references", "");
+   }
+ }
+ 
+ const char* ShenandoahConcurrentGC::conc_weak_roots_event_message() const {
+   if (ShenandoahHeap::heap()->unload_classes()) {
+     SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Concurrent weak roots", " (unload classes)");
+   } else {
+     SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Concurrent weak roots", "");
+   }
+ }
+ 
+ const char* ShenandoahConcurrentGC::conc_cleanup_event_message() const {
+   if (ShenandoahHeap::heap()->unload_classes()) {
+     SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Concurrent cleanup", " (unload classes)");
    } else {
!     SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Concurrent cleanup", "");
    }
  }
< prev index next >