< prev index next >

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

Print this page
*** 1,7 ***
--- 1,8 ---
  /*
   * Copyright (c) 2014, 2021, 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.

*** 32,12 ***
--- 33,15 ---
  #include "gc/shared/workerThread.hpp"
  #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
  #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
  #include "gc/shenandoah/shenandoahConcurrentGC.hpp"
  #include "gc/shenandoah/shenandoahCollectionSet.hpp"
+ #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
  #include "gc/shenandoah/shenandoahFreeSet.hpp"
  #include "gc/shenandoah/shenandoahFullGC.hpp"
+ #include "gc/shenandoah/shenandoahGenerationalFullGC.hpp"
+ #include "gc/shenandoah/shenandoahGlobalGeneration.hpp"
  #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
  #include "gc/shenandoah/shenandoahMark.inline.hpp"
  #include "gc/shenandoah/shenandoahMonitoringSupport.hpp"
  #include "gc/shenandoah/shenandoahHeapRegionSet.hpp"
  #include "gc/shenandoah/shenandoahHeap.inline.hpp"

*** 54,11 ***
  #include "gc/shenandoah/shenandoahWorkerPolicy.hpp"
  #include "memory/metaspaceUtils.hpp"
  #include "memory/universe.hpp"
  #include "oops/compressedOops.inline.hpp"
  #include "oops/oop.inline.hpp"
- #include "runtime/javaThread.hpp"
  #include "runtime/orderAccess.hpp"
  #include "runtime/vmThread.hpp"
  #include "utilities/copy.hpp"
  #include "utilities/events.hpp"
  #include "utilities/growableArray.hpp"
--- 58,10 ---

*** 106,10 ***
--- 109,14 ---
    // Perform full GC
    do_it(cause);
  
    ShenandoahHeap* const heap = ShenandoahHeap::heap();
  
+   if (heap->mode()->is_generational()) {
+     ShenandoahGenerationalFullGC::handle_completion(heap);
+   }
+ 
    metrics.snap_after();
  
    if (metrics.is_good_progress()) {
      heap->notify_gc_progress();
    } else {

*** 117,17 ***
      // progress, and it can finally fail.
      heap->notify_gc_no_progress();
    }
  
    // Regardless if progress was made, we record that we completed a "successful" full GC.
!   heap->heuristics()->record_success_full();
    heap->shenandoah_policy()->record_success_full();
  }
  
  void ShenandoahFullGC::do_it(GCCause::Cause gc_cause) {
    ShenandoahHeap* heap = ShenandoahHeap::heap();
  
    if (ShenandoahVerify) {
      heap->verifier()->verify_before_fullgc();
    }
  
    if (VerifyBeforeGC) {
--- 124,21 ---
      // progress, and it can finally fail.
      heap->notify_gc_no_progress();
    }
  
    // Regardless if progress was made, we record that we completed a "successful" full GC.
!   heap->global_generation()->heuristics()->record_success_full();
    heap->shenandoah_policy()->record_success_full();
  }
  
  void ShenandoahFullGC::do_it(GCCause::Cause gc_cause) {
    ShenandoahHeap* heap = ShenandoahHeap::heap();
  
+   if (heap->mode()->is_generational()) {
+     ShenandoahGenerationalFullGC::prepare();
+   }
+ 
    if (ShenandoahVerify) {
      heap->verifier()->verify_before_fullgc();
    }
  
    if (VerifyBeforeGC) {

*** 166,41 ***
      if (heap->is_update_refs_in_progress()) {
        heap->set_update_refs_in_progress(false);
      }
      assert(!heap->is_update_refs_in_progress(), "sanity");
  
!     // b. Cancel concurrent mark, if in progress
      if (heap->is_concurrent_mark_in_progress()) {
!       ShenandoahConcurrentGC::cancel();
!       heap->set_concurrent_mark_in_progress(false);
      }
      assert(!heap->is_concurrent_mark_in_progress(), "sanity");
  
      // c. Update roots if this full GC is due to evac-oom, which may carry from-space pointers in roots.
      if (has_forwarded_objects) {
        update_roots(true /*full_gc*/);
      }
  
      // d. Reset the bitmaps for new marking
!     heap->reset_mark_bitmap();
      assert(heap->marking_context()->is_bitmap_clear(), "sanity");
!     assert(!heap->marking_context()->is_complete(), "sanity");
  
      // e. Abandon reference discovery and clear all discovered references.
!     ShenandoahReferenceProcessor* rp = heap->ref_processor();
      rp->abandon_partial_discovery();
  
      // f. Sync pinned region status from the CP marks
      heap->sync_pinned_region_status();
  
      // The rest of prologue:
      _preserved_marks->init(heap->workers()->active_workers());
  
      assert(heap->has_forwarded_objects() == has_forwarded_objects, "This should not change");
    }
  
    if (UseTLAB) {
      heap->gclabs_retire(ResizeTLAB);
      heap->tlabs_retire(ResizeTLAB);
    }
  
    OrderAccess::fence();
--- 177,46 ---
      if (heap->is_update_refs_in_progress()) {
        heap->set_update_refs_in_progress(false);
      }
      assert(!heap->is_update_refs_in_progress(), "sanity");
  
!     // b. Cancel all concurrent marks, if in progress
      if (heap->is_concurrent_mark_in_progress()) {
!       // TODO: Send cancel_concurrent_mark upstream? Does it really not have it already?
!       heap->cancel_concurrent_mark();
      }
      assert(!heap->is_concurrent_mark_in_progress(), "sanity");
  
      // c. Update roots if this full GC is due to evac-oom, which may carry from-space pointers in roots.
      if (has_forwarded_objects) {
        update_roots(true /*full_gc*/);
      }
  
      // d. Reset the bitmaps for new marking
!     heap->global_generation()->reset_mark_bitmap();
      assert(heap->marking_context()->is_bitmap_clear(), "sanity");
!     assert(!heap->global_generation()->is_mark_complete(), "sanity");
  
      // e. Abandon reference discovery and clear all discovered references.
!     ShenandoahReferenceProcessor* rp = heap->global_generation()->ref_processor();
      rp->abandon_partial_discovery();
  
      // f. Sync pinned region status from the CP marks
      heap->sync_pinned_region_status();
  
+     if (heap->mode()->is_generational()) {
+       ShenandoahGenerationalFullGC::restore_top_before_promote(heap);
+     }
+ 
      // The rest of prologue:
      _preserved_marks->init(heap->workers()->active_workers());
  
      assert(heap->has_forwarded_objects() == has_forwarded_objects, "This should not change");
    }
  
    if (UseTLAB) {
+     // TODO: Do we need to explicitly retire PLABs?
      heap->gclabs_retire(ResizeTLAB);
      heap->tlabs_retire(ResizeTLAB);
    }
  
    OrderAccess::fence();

*** 253,10 ***
--- 269,11 ---
  
    if (ShenandoahVerify) {
      heap->verifier()->verify_after_fullgc();
    }
  
+   // Humongous regions are promoted on demand and are accounted for by normal Full GC mechanisms.
    if (VerifyAfterGC) {
      Universe::verify();
    }
  
    {

*** 271,12 ***
  
  public:
    ShenandoahPrepareForMarkClosure() : _ctx(ShenandoahHeap::heap()->marking_context()) {}
  
    void heap_region_do(ShenandoahHeapRegion *r) {
!     _ctx->capture_top_at_mark_start(r);
!     r->clear_live_data();
    }
  
    bool is_thread_safe() { return true; }
  };
  
--- 288,15 ---
  
  public:
    ShenandoahPrepareForMarkClosure() : _ctx(ShenandoahHeap::heap()->marking_context()) {}
  
    void heap_region_do(ShenandoahHeapRegion *r) {
!     // TODO: Add API to heap to skip free regions
!     if (r->affiliation() != FREE) {
+       _ctx->capture_top_at_mark_start(r);
+       r->clear_live_data();
+     }
    }
  
    bool is_thread_safe() { return true; }
  };
  

*** 287,19 ***
    ShenandoahHeap* heap = ShenandoahHeap::heap();
  
    ShenandoahPrepareForMarkClosure cl;
    heap->parallel_heap_region_iterate(&cl);
  
!   heap->set_unload_classes(heap->heuristics()->can_unload_classes());
  
!   ShenandoahReferenceProcessor* rp = heap->ref_processor();
    // enable ("weak") refs discovery
    rp->set_soft_reference_policy(true); // forcefully purge all soft references
  
!   ShenandoahSTWMark mark(true /*full_gc*/);
    mark.mark();
    heap->parallel_cleaning(true /* full_gc */);
  }
  
  class ShenandoahPrepareForCompactionObjectClosure : public ObjectClosure {
  private:
    PreservedMarks*          const _preserved_marks;
--- 307,23 ---
    ShenandoahHeap* heap = ShenandoahHeap::heap();
  
    ShenandoahPrepareForMarkClosure cl;
    heap->parallel_heap_region_iterate(&cl);
  
!   heap->set_unload_classes(heap->global_generation()->heuristics()->can_unload_classes());
  
!   ShenandoahReferenceProcessor* rp = heap->global_generation()->ref_processor();
    // enable ("weak") refs discovery
    rp->set_soft_reference_policy(true); // forcefully purge all soft references
  
!   ShenandoahSTWMark mark(heap->global_generation(), true /*full_gc*/);
    mark.mark();
    heap->parallel_cleaning(true /* full_gc */);
+ 
+   if (ShenandoahHeap::heap()->mode()->is_generational()) {
+     ShenandoahGenerationalFullGC::log_live_in_old(heap);
+   }
  }
  
  class ShenandoahPrepareForCompactionObjectClosure : public ObjectClosure {
  private:
    PreservedMarks*          const _preserved_marks;

*** 423,12 ***
    // Remember empty regions and reuse them as needed.
    ResourceMark rm;
  
    GrowableArray<ShenandoahHeapRegion*> empty_regions((int)_heap->num_regions());
  
!   ShenandoahPrepareForCompactionObjectClosure cl(_preserved_marks->get(worker_id), empty_regions, from_region);
!   prepare_for_compaction(cl, empty_regions, it, from_region);
  }
  
  template<typename ClosureType>
  void ShenandoahPrepareForCompactionTask::prepare_for_compaction(ClosureType& cl,
                                                                  GrowableArray<ShenandoahHeapRegion*>& empty_regions,
--- 447,18 ---
    // Remember empty regions and reuse them as needed.
    ResourceMark rm;
  
    GrowableArray<ShenandoahHeapRegion*> empty_regions((int)_heap->num_regions());
  
!   if (_heap->mode()->is_generational()) {
!     ShenandoahPrepareForGenerationalCompactionObjectClosure cl(_preserved_marks->get(worker_id),
+                                                                empty_regions, from_region, worker_id);
+     prepare_for_compaction(cl, empty_regions, it, from_region);
+   } else {
+     ShenandoahPrepareForCompactionObjectClosure cl(_preserved_marks->get(worker_id), empty_regions, from_region);
+     prepare_for_compaction(cl, empty_regions, it, from_region);
+   }
  }
  
  template<typename ClosureType>
  void ShenandoahPrepareForCompactionTask::prepare_for_compaction(ClosureType& cl,
                                                                  GrowableArray<ShenandoahHeapRegion*>& empty_regions,

*** 471,10 ***
--- 501,11 ---
    // detected, then sliding restarts towards that non-movable region.
  
    size_t to_begin = heap->num_regions();
    size_t to_end = heap->num_regions();
  
+   log_debug(gc)("Full GC calculating target humongous objects from end " SIZE_FORMAT, to_end);
    for (size_t c = heap->num_regions(); c > 0; c--) {
      ShenandoahHeapRegion *r = heap->get_region(c - 1);
      if (r->is_humongous_continuation() || (r->new_top() == r->bottom())) {
        // To-region candidate: record this, and continue scan
        to_begin = r->index();

*** 513,10 ***
--- 544,11 ---
    void heap_region_do(ShenandoahHeapRegion* r) {
      if (r->is_trash()) {
        r->recycle();
      }
      if (r->is_cset()) {
+       // Leave affiliation unchanged
        r->make_regular_bypass();
      }
      if (r->is_empty_uncommitted()) {
        r->make_committed_bypass();
      }

*** 537,10 ***
--- 569,16 ---
    ShenandoahTrashImmediateGarbageClosure() :
      _heap(ShenandoahHeap::heap()),
      _ctx(ShenandoahHeap::heap()->complete_marking_context()) {}
  
    void heap_region_do(ShenandoahHeapRegion* r) {
+     if (!r->is_affiliated()) {
+       // Ignore free regions
+       // TODO: change iterators so they do not process FREE regions.
+       return;
+     }
+ 
      if (r->is_humongous_start()) {
        oop humongous_obj = cast_to_oop(r->bottom());
        if (!_ctx->is_marked(humongous_obj)) {
          assert(!r->has_live(),
                 "Region " SIZE_FORMAT " is not marked, should not have live", r->index());

*** 701,10 ***
--- 739,15 ---
      assert(is_distributed || !is_candidate, "All candidates are distributed: " SIZE_FORMAT, rid);
    }
  #endif
  }
  
+ // TODO:
+ //  Consider compacting old-gen objects toward the high end of memory and young-gen objects towards the low-end
+ //  of memory.  As currently implemented, all regions are compacted toward the low-end of memory.  This creates more
+ //  fragmentation of the heap, because old-gen regions get scattered among low-address regions such that it becomes
+ //  more difficult to find contiguous regions for humongous objects.
  void ShenandoahFullGC::phase2_calculate_target_addresses(ShenandoahHeapRegionSet** worker_slices) {
    GCTraceTime(Info, gc, phases) time("Phase 2: Compute new object addresses", _gc_timer);
    ShenandoahGCPhase calculate_address_phase(ShenandoahPhaseTimings::full_gc_calculate_addresses);
  
    ShenandoahHeap* heap = ShenandoahHeap::heap();

*** 728,10 ***
--- 771,12 ---
    {
      ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_calculate_addresses_regular);
  
      distribute_slices(worker_slices);
  
+     // TODO: This is ResourceMark is missing upstream.
+     ResourceMark rm;
      ShenandoahPrepareForCompactionTask task(_preserved_marks, worker_slices);
      heap->workers()->run_task(&task);
    }
  
    // Compute the new addresses for humongous objects

*** 802,10 ***
--- 847,13 ---
      ShenandoahHeapRegion* r = _regions.next();
      while (r != nullptr) {
        if (!r->is_humongous_continuation() && r->has_live()) {
          _heap->marked_object_iterate(r, &obj_cl);
        }
+       if (_heap->mode()->is_generational()) {
+         ShenandoahGenerationalFullGC::maybe_coalesce_and_fill_region(r);
+       }
        r = _regions.next();
      }
    }
  };
  

*** 906,14 ***
  };
  
  class ShenandoahPostCompactClosure : public ShenandoahHeapRegionClosure {
  private:
    ShenandoahHeap* const _heap;
!   size_t _live;
  
  public:
!   ShenandoahPostCompactClosure() : _heap(ShenandoahHeap::heap()), _live(0) {
      _heap->free_set()->clear();
    }
  
    void heap_region_do(ShenandoahHeapRegion* r) {
      assert (!r->is_cset(), "cset regions should have been demoted already");
--- 954,24 ---
  };
  
  class ShenandoahPostCompactClosure : public ShenandoahHeapRegionClosure {
  private:
    ShenandoahHeap* const _heap;
!   bool _is_generational;
+   size_t _young_regions, _young_usage, _young_humongous_waste;
+   size_t _old_regions, _old_usage, _old_humongous_waste;
  
  public:
!   ShenandoahPostCompactClosure() : _heap(ShenandoahHeap::heap()),
+                                    _is_generational(_heap->mode()->is_generational()),
+                                    _young_regions(0),
+                                    _young_usage(0),
+                                    _young_humongous_waste(0),
+                                    _old_regions(0),
+                                    _old_usage(0),
+                                    _old_humongous_waste(0)
+   {
      _heap->free_set()->clear();
    }
  
    void heap_region_do(ShenandoahHeapRegion* r) {
      assert (!r->is_cset(), "cset regions should have been demoted already");

*** 929,10 ***
--- 987,14 ---
  
      size_t live = r->used();
  
      // Make empty regions that have been allocated into regular
      if (r->is_empty() && live > 0) {
+       if (!_is_generational) {
+         r->make_young_maybe();
+       }
+       // else, generational mode compaction has already established affiliation.
        r->make_regular_bypass();
        if (ZapUnusedHeapArea) {
          SpaceMangler::mangle_region(MemRegion(r->top(), r->end()));
        }
      }

*** 944,19 ***
  
      // Recycle all trash regions
      if (r->is_trash()) {
        live = 0;
        r->recycle();
      }
- 
      r->set_live_data(live);
      r->reset_alloc_metadata();
-     _live += live;
    }
  
!   size_t get_live() {
!     return _live;
    }
  };
  
  void ShenandoahFullGC::compact_humongous_objects() {
    // Compact humongous regions, based on their fwdptr objects.
--- 1006,36 ---
  
      // Recycle all trash regions
      if (r->is_trash()) {
        live = 0;
        r->recycle();
+     } else {
+       if (r->is_old()) {
+         ShenandoahGenerationalFullGC::account_for_region(r, _old_regions, _old_usage, _old_humongous_waste);
+       } else if (r->is_young()) {
+         ShenandoahGenerationalFullGC::account_for_region(r, _young_regions, _young_usage, _young_humongous_waste);
+       }
      }
      r->set_live_data(live);
      r->reset_alloc_metadata();
    }
  
!   void update_generation_usage() {
!     if (_is_generational) {
+       _heap->old_generation()->establish_usage(_old_regions, _old_usage, _old_humongous_waste);
+       _heap->young_generation()->establish_usage(_young_regions, _young_usage, _young_humongous_waste);
+     } else {
+       assert(_old_regions == 0, "Old regions only expected in generational mode");
+       assert(_old_usage == 0, "Old usage only expected in generational mode");
+       assert(_old_humongous_waste == 0, "Old humongous waste only expected in generational mode");
+     }
+ 
+     // In generational mode, global usage should be the sum of young and old. This is also true
+     // for non-generational modes except that there are no old regions.
+     _heap->global_generation()->establish_usage(_old_regions + _young_regions,
+                                                 _old_usage + _young_usage,
+                                                 _old_humongous_waste + _young_humongous_waste);
    }
  };
  
  void ShenandoahFullGC::compact_humongous_objects() {
    // Compact humongous regions, based on their fwdptr objects.

*** 983,29 ***
        size_t new_start = heap->heap_region_index_containing(old_obj->forwardee());
        size_t new_end   = new_start + num_regions - 1;
        assert(old_start != new_start, "must be real move");
        assert(r->is_stw_move_allowed(), "Region " SIZE_FORMAT " should be movable", r->index());
  
        Copy::aligned_conjoint_words(r->bottom(), heap->get_region(new_start)->bottom(), words_size);
        ContinuationGCSupport::relativize_stack_chunk(cast_to_oop<HeapWord*>(r->bottom()));
  
        oop new_obj = cast_to_oop(heap->get_region(new_start)->bottom());
        new_obj->init_mark();
  
        {
          for (size_t c = old_start; c <= old_end; c++) {
            ShenandoahHeapRegion* r = heap->get_region(c);
            r->make_regular_bypass();
            r->set_top(r->bottom());
          }
  
          for (size_t c = new_start; c <= new_end; c++) {
            ShenandoahHeapRegion* r = heap->get_region(c);
            if (c == new_start) {
!             r->make_humongous_start_bypass();
            } else {
!             r->make_humongous_cont_bypass();
            }
  
            // Trailing region may be non-full, record the remainder there
            size_t remainder = words_size & ShenandoahHeapRegion::region_size_words_mask();
            if ((c == new_end) && (remainder != 0)) {
--- 1062,32 ---
        size_t new_start = heap->heap_region_index_containing(old_obj->forwardee());
        size_t new_end   = new_start + num_regions - 1;
        assert(old_start != new_start, "must be real move");
        assert(r->is_stw_move_allowed(), "Region " SIZE_FORMAT " should be movable", r->index());
  
+       log_debug(gc)("Full GC compaction moves humongous object from region " SIZE_FORMAT " to region " SIZE_FORMAT, old_start, new_start);
        Copy::aligned_conjoint_words(r->bottom(), heap->get_region(new_start)->bottom(), words_size);
        ContinuationGCSupport::relativize_stack_chunk(cast_to_oop<HeapWord*>(r->bottom()));
  
        oop new_obj = cast_to_oop(heap->get_region(new_start)->bottom());
        new_obj->init_mark();
  
        {
+         ShenandoahAffiliation original_affiliation = r->affiliation();
          for (size_t c = old_start; c <= old_end; c++) {
            ShenandoahHeapRegion* r = heap->get_region(c);
+           // Leave humongous region affiliation unchanged.
            r->make_regular_bypass();
            r->set_top(r->bottom());
          }
  
          for (size_t c = new_start; c <= new_end; c++) {
            ShenandoahHeapRegion* r = heap->get_region(c);
            if (c == new_start) {
!             r->make_humongous_start_bypass(original_affiliation);
            } else {
!             r->make_humongous_cont_bypass(original_affiliation);
            }
  
            // Trailing region may be non-full, record the remainder there
            size_t remainder = words_size & ShenandoahHeapRegion::region_size_words_mask();
            if ((c == new_end) && (remainder != 0)) {

*** 1086,15 ***
    // Bring regions in proper states after the collection, and set heap properties.
    {
      ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_rebuild);
      ShenandoahPostCompactClosure post_compact;
      heap->heap_region_iterate(&post_compact);
!     heap->set_used(post_compact.get_live());
  
      heap->collection_set()->clear();
!     heap->free_set()->rebuild();
!     heap->clear_cancelled_gc();
    }
  
    _preserved_marks->restore(heap->workers());
    _preserved_marks->reclaim();
  }
--- 1168,43 ---
    // Bring regions in proper states after the collection, and set heap properties.
    {
      ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_rebuild);
      ShenandoahPostCompactClosure post_compact;
      heap->heap_region_iterate(&post_compact);
!     post_compact.update_generation_usage();
+ 
+     if (heap->mode()->is_generational()) {
+       ShenandoahGenerationalFullGC::balance_generations_after_gc(heap);
+     }
  
      heap->collection_set()->clear();
!     size_t young_cset_regions, old_cset_regions;
!     size_t first_old, last_old, num_old;
+     heap->free_set()->prepare_to_rebuild(young_cset_regions, old_cset_regions, first_old, last_old, num_old);
+ 
+     // We also do not expand old generation size following Full GC because we have scrambled age populations and
+     // no longer have objects separated by age into distinct regions.
+ 
+     // TODO: Do we need to fix FullGC so that it maintains aged segregation of objects into distinct regions?
+     //       A partial solution would be to remember how many objects are of tenure age following Full GC, but
+     //       this is probably suboptimal, because most of these objects will not reside in a region that will be
+     //       selected for the next evacuation phase.
+ 
+ 
+     if (heap->mode()->is_generational()) {
+       ShenandoahGenerationalFullGC::compute_balances();
+     }
+ 
+     heap->free_set()->rebuild(young_cset_regions, old_cset_regions);
+ 
+     // 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()) {
+       ShenandoahGenerationalFullGC::balance_generations_after_rebuilding_free_set();
+       ShenandoahGenerationalFullGC::rebuild_remembered_set(heap);
+     }
+     heap->clear_cancelled_gc(true /* clear oom handler */);
    }
  
    _preserved_marks->restore(heap->workers());
    _preserved_marks->reclaim();
  }
< prev index next >