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

*** 29,14 ***
--- 30,18 ---
  #include "gc/shared/gcTraceTime.inline.hpp"
  #include "gc/shared/preservedMarks.inline.hpp"
  #include "gc/shared/tlab_globals.hpp"
  #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"

*** 53,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 ---

*** 103,24 ***
    metrics.snap_before();
  
    // Perform full GC
    do_it(cause);
  
    metrics.snap_after();
  
    if (metrics.is_good_progress()) {
!     ShenandoahHeap::heap()->notify_gc_progress();
    } else {
      // Nothing to do. Tell the allocation path that we have failed to make
      // progress, and it can finally fail.
!     ShenandoahHeap::heap()->notify_gc_no_progress();
    }
  }
  
  void ShenandoahFullGC::do_it(GCCause::Cause gc_cause) {
    ShenandoahHeap* heap = ShenandoahHeap::heap();
  
    if (ShenandoahVerify) {
      heap->verifier()->verify_before_fullgc();
    }
  
    if (VerifyBeforeGC) {
--- 107,38 ---
    metrics.snap_before();
  
    // 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 {
      // Nothing to do. Tell the allocation path that we have failed to make
      // 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) {

*** 159,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();

*** 226,16 ***
      OrderAccess::fence();
  
      phase3_update_references();
  
      phase4_compact_objects(worker_slices);
-   }
  
!   {
-     // Epilogue
-     _preserved_marks->restore(heap->workers());
-     _preserved_marks->reclaim();
    }
  
    // Resize metaspace
    MetaspaceGC::compute_new_size();
  
--- 249,12 ---
      OrderAccess::fence();
  
      phase3_update_references();
  
      phase4_compact_objects(worker_slices);
  
!     phase5_epilog();
    }
  
    // Resize metaspace
    MetaspaceGC::compute_new_size();
  

*** 250,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();
    }
  
    {

*** 268,33 ***
  
  public:
    ShenandoahPrepareForMarkClosure() : _ctx(ShenandoahHeap::heap()->marking_context()) {}
  
    void heap_region_do(ShenandoahHeapRegion *r) {
!     _ctx->capture_top_at_mark_start(r);
!     r->clear_live_data();
    }
  };
  
  void ShenandoahFullGC::phase1_mark_heap() {
    GCTraceTime(Info, gc, phases) time("Phase 1: Mark live objects", _gc_timer);
    ShenandoahGCPhase mark_phase(ShenandoahPhaseTimings::full_gc_mark);
  
    ShenandoahHeap* heap = ShenandoahHeap::heap();
  
    ShenandoahPrepareForMarkClosure cl;
!   heap->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;
--- 288,42 ---
  
  public:
    ShenandoahPrepareForMarkClosure() : _ctx(ShenandoahHeap::heap()->marking_context()) {}
  
    void heap_region_do(ShenandoahHeapRegion *r) {
!     // TODO: Add API to heap to skip free regions
!     if (r->is_affiliated()) {
+       _ctx->capture_top_at_mark_start(r);
+       r->clear_live_data();
+     }
    }
+ 
+   bool is_thread_safe() { return true; }
  };
  
  void ShenandoahFullGC::phase1_mark_heap() {
    GCTraceTime(Info, gc, phases) time("Phase 1: Mark live objects", _gc_timer);
    ShenandoahGCPhase mark_phase(ShenandoahPhaseTimings::full_gc_mark);
  
    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;

*** 319,11 ***
  
    void set_from_region(ShenandoahHeapRegion* from_region) {
      _from_region = from_region;
    }
  
!   void finish_region() {
      assert(_to_region != nullptr, "should not happen");
      _to_region->set_new_top(_compact_point);
    }
  
    bool is_compact_same_region() {
--- 348,11 ---
  
    void set_from_region(ShenandoahHeapRegion* from_region) {
      _from_region = from_region;
    }
  
!   void finish() {
      assert(_to_region != nullptr, "should not happen");
      _to_region->set_new_top(_compact_point);
    }
  
    bool is_compact_same_region() {

*** 339,11 ***
      assert(_heap->complete_marking_context()->is_marked(p), "must be marked");
      assert(!_heap->complete_marking_context()->allocated_after_mark_start(p), "must be truly marked");
  
      size_t obj_size = p->size();
      if (_compact_point + obj_size > _to_region->end()) {
!       finish_region();
  
        // Object doesn't fit. Pick next empty region and start compacting there.
        ShenandoahHeapRegion* new_to_region;
        if (_empty_regions_pos < _empty_regions.length()) {
          new_to_region = _empty_regions.at(_empty_regions_pos);
--- 368,11 ---
      assert(_heap->complete_marking_context()->is_marked(p), "must be marked");
      assert(!_heap->complete_marking_context()->allocated_after_mark_start(p), "must be truly marked");
  
      size_t obj_size = p->size();
      if (_compact_point + obj_size > _to_region->end()) {
!       finish();
  
        // Object doesn't fit. Pick next empty region and start compacting there.
        ShenandoahHeapRegion* new_to_region;
        if (_empty_regions_pos < _empty_regions.length()) {
          new_to_region = _empty_regions.at(_empty_regions_pos);

*** 357,15 ***
        assert(new_to_region != nullptr, "must not be null");
        _to_region = new_to_region;
        _compact_point = _to_region->bottom();
      }
  
!     // Object fits into current region, record new location:
      assert(_compact_point + obj_size <= _to_region->end(), "must fit");
      shenandoah_assert_not_forwarded(nullptr, p);
!     _preserved_marks->push_if_necessary(p, p->mark());
!     p->forward_to(cast_to_oop(_compact_point));
      _compact_point += obj_size;
    }
  };
  
  class ShenandoahPrepareForCompactionTask : public WorkerTask {
--- 386,17 ---
        assert(new_to_region != nullptr, "must not be null");
        _to_region = new_to_region;
        _compact_point = _to_region->bottom();
      }
  
!     // Object fits into current region, record new location, if object does not move:
      assert(_compact_point + obj_size <= _to_region->end(), "must fit");
      shenandoah_assert_not_forwarded(nullptr, p);
!     if (_compact_point != cast_from_oop<HeapWord*>(p)) {
!       _preserved_marks->push_if_necessary(p, p->mark());
+       p->forward_to(cast_to_oop(_compact_point));
+     }
      _compact_point += obj_size;
    }
  };
  
  class ShenandoahPrepareForCompactionTask : public WorkerTask {

*** 391,51 ***
      // Can move the region, and this is not the humongous region. Humongous
      // moves are special cased here, because their moves are handled separately.
      return r->is_stw_move_allowed() && !r->is_humongous();
    }
  
!   void work(uint worker_id) {
!     ShenandoahParallelWorkerSession worker_session(worker_id);
!     ShenandoahHeapRegionSet* slice = _worker_slices[worker_id];
!     ShenandoahHeapRegionSetIterator it(slice);
!     ShenandoahHeapRegion* from_region = it.next();
!     // No work?
!     if (from_region == nullptr) {
!        return;
-     }
- 
-     // Sliding compaction. Walk all regions in the slice, and compact them.
-     // 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);
  
!     while (from_region != nullptr) {
-       assert(is_candidate_region(from_region), "Sanity");
  
!       cl.set_from_region(from_region);
!       if (from_region->has_live()) {
!         _heap->marked_object_iterate(from_region, &cl);
!       }
  
!       // Compacted the region to somewhere else? From-region is empty then.
!       if (!cl.is_compact_same_region()) {
!         empty_regions.append(from_region);
!       }
!       from_region = it.next();
      }
-     cl.finish_region();
  
!     // Mark all remaining regions as empty
!     for (int pos = cl.empty_regions_pos(); pos < empty_regions.length(); ++pos) {
!       ShenandoahHeapRegion* r = empty_regions.at(pos);
-       r->set_new_top(r->bottom());
      }
    }
! };
  
  void ShenandoahFullGC::calculate_target_humongous_objects() {
    ShenandoahHeap* heap = ShenandoahHeap::heap();
  
    // Compute the new addresses for humongous objects. We need to do this after addresses
--- 422,71 ---
      // Can move the region, and this is not the humongous region. Humongous
      // moves are special cased here, because their moves are handled separately.
      return r->is_stw_move_allowed() && !r->is_humongous();
    }
  
!   void work(uint worker_id) override;
! private:
!   template<typename ClosureType>
!   void prepare_for_compaction(ClosureType& cl,
!                               GrowableArray<ShenandoahHeapRegion*>& empty_regions,
!                               ShenandoahHeapRegionSetIterator& it,
!                               ShenandoahHeapRegion* from_region);
! };
  
! void ShenandoahPrepareForCompactionTask::work(uint worker_id) {
+   ShenandoahParallelWorkerSession worker_session(worker_id);
+   ShenandoahHeapRegionSet* slice = _worker_slices[worker_id];
+   ShenandoahHeapRegionSetIterator it(slice);
+   ShenandoahHeapRegion* from_region = it.next();
+   // No work?
+   if (from_region == nullptr) {
+     return;
+   }
  
!   // Sliding compaction. Walk all regions in the slice, and compact them.
+   // 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,
!                                                                 ShenandoahHeapRegionSetIterator& it,
!                                                                 ShenandoahHeapRegion* from_region) {
+   while (from_region != nullptr) {
+     assert(is_candidate_region(from_region), "Sanity");
+     cl.set_from_region(from_region);
+     if (from_region->has_live()) {
+       _heap->marked_object_iterate(from_region, &cl);
      }
  
!     // Compacted the region to somewhere else? From-region is empty then.
!     if (!cl.is_compact_same_region()) {
!       empty_regions.append(from_region);
      }
+     from_region = it.next();
    }
!   cl.finish();
+ 
+   // Mark all remaining regions as empty
+   for (int pos = cl.empty_regions_pos(); pos < empty_regions.length(); ++pos) {
+     ShenandoahHeapRegion* r = empty_regions.at(pos);
+     r->set_new_top(r->bottom());
+   }
+ }
  
  void ShenandoahFullGC::calculate_target_humongous_objects() {
    ShenandoahHeap* heap = ShenandoahHeap::heap();
  
    // Compute the new addresses for humongous objects. We need to do this after addresses

*** 450,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();

*** 492,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();
      }

*** 516,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());

*** 680,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();

*** 707,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

*** 781,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();
      }
    }
  };
  

*** 843,10 ***
--- 912,11 ---
      assert(_heap->complete_marking_context()->is_marked(p), "must be marked");
      size_t size = p->size();
      if (p->is_forwarded()) {
        HeapWord* compact_from = cast_from_oop<HeapWord*>(p);
        HeapWord* compact_to = cast_from_oop<HeapWord*>(p->forwardee());
+       assert(compact_from != compact_to, "Forwarded object should move");
        Copy::aligned_conjoint_words(compact_from, compact_to, size);
        oop new_obj = cast_to_oop(compact_to);
  
        ContinuationGCSupport::relativize_stack_chunk(new_obj);
        new_obj->init_mark();

*** 884,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");

*** 907,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()));
        }
      }

*** 922,28 ***
  
      // 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.
    //
    // This code is serial, because doing the in-slice parallel sliding is tricky. In most cases,
    // humongous regions are already compacted, and do not require further moves, which alleviates
!   // sliding costs. We may consider doing this in parallel in future.
  
    ShenandoahHeap* heap = ShenandoahHeap::heap();
  
    for (size_t c = heap->num_regions(); c > 0; c--) {
      ShenandoahHeapRegion* r = heap->get_region(c - 1);
--- 1006,45 ---
  
      // 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.
    //
    // This code is serial, because doing the in-slice parallel sliding is tricky. In most cases,
    // humongous regions are already compacted, and do not require further moves, which alleviates
!   // sliding costs. We may consider doing this in parallel in the future.
  
    ShenandoahHeap* heap = ShenandoahHeap::heap();
  
    for (size_t c = heap->num_regions(); c > 0; c--) {
      ShenandoahHeapRegion* r = heap->get_region(c - 1);

*** 961,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)) {

*** 1045,10 ***
--- 1149,15 ---
    // Compact humongous objects after regular object moves
    {
      ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_humong);
      compact_humongous_objects();
    }
+ }
+ 
+ void ShenandoahFullGC::phase5_epilog() {
+   GCTraceTime(Info, gc, phases) time("Phase 5: Full GC epilog", _gc_timer);
+   ShenandoahHeap* heap = ShenandoahHeap::heap();
  
    // Reset complete bitmap. We're about to reset the complete-top-at-mark-start pointer
    // and must ensure the bitmap is in sync.
    {
      ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_reset_complete);

*** 1057,16 ***
    }
  
    // 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();
  }
--- 1166,46 ---
    }
  
    // 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);
+ 
+     heap->clear_cancelled_gc(true /* clear oom handler */);
    }
  
!   _preserved_marks->restore(heap->workers());
+   _preserved_marks->reclaim();
+ 
+   // 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);
+   }
  }
< prev index next >