< prev index next >

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

Print this page
*** 31,10 ***
--- 31,11 ---
  #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
  #include "gc/shenandoah/shenandoahConcurrentGC.hpp"
  #include "gc/shenandoah/shenandoahCollectionSet.hpp"
  #include "gc/shenandoah/shenandoahFreeSet.hpp"
  #include "gc/shenandoah/shenandoahFullGC.hpp"
+ #include "gc/shenandoah/shenandoahGeneration.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"

*** 47,10 ***
--- 48,11 ---
  #include "gc/shenandoah/shenandoahSTWMark.hpp"
  #include "gc/shenandoah/shenandoahUtils.hpp"
  #include "gc/shenandoah/shenandoahVerifier.hpp"
  #include "gc/shenandoah/shenandoahVMOperations.hpp"
  #include "gc/shenandoah/shenandoahWorkerPolicy.hpp"
+ #include "gc/shenandoah/shenandoahYoungGeneration.hpp"
  #include "memory/metaspaceUtils.hpp"
  #include "memory/universe.hpp"
  #include "oops/compressedOops.inline.hpp"
  #include "oops/oop.inline.hpp"
  #include "runtime/orderAccess.hpp"

*** 59,10 ***
--- 61,73 ---
  #include "utilities/copy.hpp"
  #include "utilities/events.hpp"
  #include "utilities/growableArray.hpp"
  #include "gc/shared/workgroup.hpp"
  
+ // After Full GC is done, reconstruct the remembered set by iterating over OLD regions,
+ // registering all objects between bottom() and top(), and setting remembered set cards to
+ // DIRTY if they hold interesting pointers.
+ class ShenandoahReconstructRememberedSetTask : public AbstractGangTask {
+ private:
+   ShenandoahRegionIterator _regions;
+ 
+ public:
+   ShenandoahReconstructRememberedSetTask() :
+     AbstractGangTask("Shenandoah Reset Bitmap") { }
+ 
+   void work(uint worker_id) {
+     ShenandoahParallelWorkerSession worker_session(worker_id);
+     ShenandoahHeapRegion* r = _regions.next();
+     ShenandoahHeap* heap = ShenandoahHeap::heap();
+     RememberedScanner* scanner = heap->card_scan();
+     ShenandoahSetRememberedCardsToDirtyClosure dirty_cards_for_interesting_pointers;
+ 
+     while (r != NULL) {
+       if (r->is_old() && r->is_active()) {
+         HeapWord* obj_addr = r->bottom();
+         if (r->is_humongous_start()) {
+           // First, clear the remembered set
+           oop obj = cast_to_oop(obj_addr);
+           size_t size = obj->size();
+           HeapWord* end_object = r->bottom() + size;
+ 
+           // First, clear the remembered set for all spanned humongous regions
+           size_t num_regions = (size + ShenandoahHeapRegion::region_size_words() - 1) / ShenandoahHeapRegion::region_size_words();
+           size_t region_span = num_regions * ShenandoahHeapRegion::region_size_words();
+           scanner->reset_remset(r->bottom(), region_span);
+           size_t region_index = r->index();
+           ShenandoahHeapRegion* humongous_region = heap->get_region(region_index);
+           while (num_regions-- != 0) {
+             scanner->reset_object_range(humongous_region->bottom(), humongous_region->end());
+             region_index++;
+             humongous_region = heap->get_region(region_index);
+           }
+ 
+           // Then register the humongous object and DIRTY relevant remembered set cards
+           scanner->register_object_wo_lock(obj_addr);
+           obj->oop_iterate(&dirty_cards_for_interesting_pointers);
+         } else if (!r->is_humongous()) {
+           // First, clear the remembered set
+           scanner->reset_remset(r->bottom(), ShenandoahHeapRegion::region_size_words());
+           scanner->reset_object_range(r->bottom(), r->end());
+ 
+           // Then iterate over all objects, registering object and DIRTYing relevant remembered set cards
+           HeapWord* t = r->top();
+           while (obj_addr < t) {
+             oop obj = cast_to_oop(obj_addr);
+             size_t size = obj->size();
+             scanner->register_object_wo_lock(obj_addr);
+             obj_addr += obj->oop_iterate_size(&dirty_cards_for_interesting_pointers);
+           }
+         } // else, ignore humongous continuation region
+       }
+       // else, this region is FREE or YOUNG or inactive and we can ignore it.
+       r = _regions.next();
+     }
+   }
+ };
+ 
  ShenandoahFullGC::ShenandoahFullGC() :
    _gc_timer(ShenandoahHeap::heap()->gc_timer()),
    _preserved_marks(new PreservedMarksSet(true)) {}
  
  bool ShenandoahFullGC::collect(GCCause::Cause cause) {

*** 111,10 ***
--- 176,12 ---
    }
  }
  
  void ShenandoahFullGC::do_it(GCCause::Cause gc_cause) {
    ShenandoahHeap* heap = ShenandoahHeap::heap();
+   // Since we may arrive here from degenerated GC failure of either young or old, establish generation as GLOBAL.
+   heap->set_gc_generation(heap->global_generation());
  
    if (ShenandoahVerify) {
      heap->verifier()->verify_before_fullgc();
    }
  

*** 154,29 ***
      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();
  
--- 221,28 ---
      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()) {
!       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();
  

*** 185,10 ***
--- 251,11 ---
  
      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();

*** 227,10 ***
--- 294,16 ---
  
    {
      // Epilogue
      _preserved_marks->restore(heap->workers());
      _preserved_marks->reclaim();
+ 
+     if (heap->mode()->is_generational()) {
+       ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_reconstruct_remembered_set);
+       ShenandoahReconstructRememberedSetTask task;
+       heap->workers()->run_task(&task);
+     }
    }
  
    // Resize metaspace
    MetaspaceGC::compute_new_size();
  

*** 242,11 ***
  
    heap->set_full_gc_move_in_progress(false);
    heap->set_full_gc_in_progress(false);
  
    if (ShenandoahVerify) {
!     heap->verifier()->verify_after_fullgc();
    }
  
    if (VerifyAfterGC) {
      Universe::verify();
    }
--- 315,15 ---
  
    heap->set_full_gc_move_in_progress(false);
    heap->set_full_gc_in_progress(false);
  
    if (ShenandoahVerify) {
!     if (heap->mode()->is_generational()) {
+       heap->verifier()->verify_after_generational_fullgc();
+     } else {
+       heap->verifier()->verify_after_fullgc();
+     }
    }
  
    if (VerifyAfterGC) {
      Universe::verify();
    }

*** 263,35 ***
  
  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;
    ShenandoahHeap*          const _heap;
    GrowableArray<ShenandoahHeapRegion*>& _empty_regions;
--- 340,233 ---
  
  public:
    ShenandoahPrepareForMarkClosure() : _ctx(ShenandoahHeap::heap()->marking_context()) {}
  
    void heap_region_do(ShenandoahHeapRegion *r) {
!     if (r->affiliation() != FREE) {
!       _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 */);
  }
  
+ class ShenandoahPrepareForCompactionTask : public AbstractGangTask {
+ private:
+   PreservedMarksSet*        const _preserved_marks;
+   ShenandoahHeap*           const _heap;
+   ShenandoahHeapRegionSet** const _worker_slices;
+   size_t                    const _num_workers;
+ 
+ public:
+   ShenandoahPrepareForCompactionTask(PreservedMarksSet *preserved_marks, ShenandoahHeapRegionSet **worker_slices,
+                                      size_t num_workers);
+ 
+   static bool is_candidate_region(ShenandoahHeapRegion* r) {
+     // Empty region: get it into the slice to defragment the slice itself.
+     // We could have skipped this without violating correctness, but we really
+     // want to compact all live regions to the start of the heap, which sometimes
+     // means moving them into the fully empty regions.
+     if (r->is_empty()) return true;
+ 
+     // 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);
+ };
+ 
+ class ShenandoahPrepareForGenerationalCompactionObjectClosure : public ObjectClosure {
+ private:
+   ShenandoahPrepareForCompactionTask* _compactor;
+   PreservedMarks*          const _preserved_marks;
+   ShenandoahHeap*          const _heap;
+   GrowableArray<ShenandoahHeapRegion*>& _empty_regions;
+   int _empty_regions_pos;
+   ShenandoahHeapRegion*          _old_to_region;
+   ShenandoahHeapRegion*          _young_to_region;
+   ShenandoahHeapRegion*          _from_region;
+   ShenandoahRegionAffiliation    _from_affiliation;
+   HeapWord*                      _old_compact_point;
+   HeapWord*                      _young_compact_point;
+   uint                           _worker_id;
+ 
+ public:
+   ShenandoahPrepareForGenerationalCompactionObjectClosure(ShenandoahPrepareForCompactionTask* compactor,
+                                                           PreservedMarks* preserved_marks,
+                                                           GrowableArray<ShenandoahHeapRegion*>& empty_regions,
+                                                           ShenandoahHeapRegion* old_to_region,
+                                                           ShenandoahHeapRegion* young_to_region, uint worker_id) :
+       _compactor(compactor),
+       _preserved_marks(preserved_marks),
+       _heap(ShenandoahHeap::heap()),
+       _empty_regions(empty_regions),
+       _empty_regions_pos(0),
+       _old_to_region(old_to_region),
+       _young_to_region(young_to_region),
+       _from_region(NULL),
+       _old_compact_point((old_to_region != nullptr)? old_to_region->bottom(): nullptr),
+       _young_compact_point((young_to_region != nullptr)? young_to_region->bottom(): nullptr),
+       _worker_id(worker_id) {}
+ 
+   void set_from_region(ShenandoahHeapRegion* from_region) {
+     _from_region = from_region;
+     _from_affiliation = from_region->affiliation();
+     if (_from_region->has_live()) {
+       if (_from_affiliation == ShenandoahRegionAffiliation::OLD_GENERATION) {
+         if (_old_to_region == nullptr) {
+           _old_to_region = from_region;
+           _old_compact_point = from_region->bottom();
+         }
+       } else {
+         assert(_from_affiliation == ShenandoahRegionAffiliation::YOUNG_GENERATION, "from_region must be OLD or YOUNG");
+         if (_young_to_region == nullptr) {
+           _young_to_region = from_region;
+           _young_compact_point = from_region->bottom();
+         }
+       }
+     } // else, we won't iterate over this _from_region so we don't need to set up to region to hold copies
+   }
+ 
+   void finish() {
+     finish_old_region();
+     finish_young_region();
+   }
+ 
+   void finish_old_region() {
+     if (_old_to_region != nullptr) {
+       log_debug(gc)("Planned compaction into Old Region " SIZE_FORMAT ", used: " SIZE_FORMAT " tabulated by worker %u",
+                     _old_to_region->index(), _old_compact_point - _old_to_region->bottom(), _worker_id);
+       _old_to_region->set_new_top(_old_compact_point);
+       _old_to_region = nullptr;
+     }
+   }
+ 
+   void finish_young_region() {
+     if (_young_to_region != nullptr) {
+       log_debug(gc)("Worker %u planned compaction into Young Region " SIZE_FORMAT ", used: " SIZE_FORMAT,
+                     _worker_id, _young_to_region->index(), _young_compact_point - _young_to_region->bottom());
+       _young_to_region->set_new_top(_young_compact_point);
+       _young_to_region = nullptr;
+     }
+   }
+ 
+   bool is_compact_same_region() {
+     return (_from_region == _old_to_region) || (_from_region == _young_to_region);
+   }
+ 
+   int empty_regions_pos() {
+     return _empty_regions_pos;
+   }
+ 
+   void do_object(oop p) {
+     assert(_from_region != NULL, "must set before work");
+     assert((_from_region->bottom() <= cast_from_oop<HeapWord*>(p)) && (cast_from_oop<HeapWord*>(p) < _from_region->top()),
+            "Object must reside in _from_region");
+     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 (_from_affiliation == ShenandoahRegionAffiliation::OLD_GENERATION) {
+       assert(_old_to_region != nullptr, "_old_to_region should not be NULL when compacting OLD _from_region");
+       if (_old_compact_point + obj_size > _old_to_region->end()) {
+         ShenandoahHeapRegion* new_to_region;
+ 
+         log_debug(gc)("Worker %u finishing old region " SIZE_FORMAT ", compact_point: " PTR_FORMAT ", obj_size: " SIZE_FORMAT
+                       ", &compact_point[obj_size]: " PTR_FORMAT ", region end: " PTR_FORMAT,  _worker_id, _old_to_region->index(),
+                       p2i(_old_compact_point), obj_size, p2i(_old_compact_point + obj_size), p2i(_old_to_region->end()));
+ 
+         // Object does not fit.  Get a new _old_to_region.
+         finish_old_region();
+         if (_empty_regions_pos < _empty_regions.length()) {
+           new_to_region = _empty_regions.at(_empty_regions_pos);
+           _empty_regions_pos++;
+           new_to_region->set_affiliation(OLD_GENERATION);
+         } else {
+           // If we've exhausted the previously selected _old_to_region, we know that the _old_to_region is distinct
+           // from _from_region.  That's because there is always room for _from_region to be compacted into itself.
+           // Since we're out of empty regions, let's use _from_region to hold the results of its own compaction.
+           new_to_region = _from_region;
+         }
+ 
+         assert(new_to_region != _old_to_region, "must not reuse same OLD to-region");
+         assert(new_to_region != NULL, "must not be NULL");
+         _old_to_region = new_to_region;
+         _old_compact_point = _old_to_region->bottom();
+       }
+ 
+       // Object fits into current region, record new location:
+       assert(_old_compact_point + obj_size <= _old_to_region->end(), "must fit");
+       shenandoah_assert_not_forwarded(NULL, p);
+       _preserved_marks->push_if_necessary(p, p->mark());
+       p->forward_to(cast_to_oop(_old_compact_point));
+       _old_compact_point += obj_size;
+     } else {
+       assert(_from_affiliation == ShenandoahRegionAffiliation::YOUNG_GENERATION,
+              "_from_region must be OLD_GENERATION or YOUNG_GENERATION");
+ 
+       assert(_young_to_region != nullptr, "_young_to_region should not be NULL when compacting YOUNG _from_region");
+       if (_young_compact_point + obj_size > _young_to_region->end()) {
+         ShenandoahHeapRegion* new_to_region;
+ 
+ 
+         log_debug(gc)("Worker %u finishing young region " SIZE_FORMAT ", compact_point: " PTR_FORMAT ", obj_size: " SIZE_FORMAT
+                       ", &compact_point[obj_size]: " PTR_FORMAT ", region end: " PTR_FORMAT,  _worker_id, _young_to_region->index(),
+                       p2i(_young_compact_point), obj_size, p2i(_young_compact_point + obj_size), p2i(_young_to_region->end()));
+ 
+         // Object does not fit.  Get a new _young_to_region.
+         finish_young_region();
+         if (_empty_regions_pos < _empty_regions.length()) {
+           new_to_region = _empty_regions.at(_empty_regions_pos);
+           _empty_regions_pos++;
+           new_to_region->set_affiliation(YOUNG_GENERATION);
+         } else {
+           // If we've exhausted the previously selected _young_to_region, we know that the _young_to_region is distinct
+           // from _from_region.  That's because there is always room for _from_region to be compacted into itself.
+           // Since we're out of empty regions, let's use _from_region to hold the results of its own compaction.
+           new_to_region = _from_region;
+         }
+ 
+         assert(new_to_region != _young_to_region, "must not reuse same OLD to-region");
+         assert(new_to_region != NULL, "must not be NULL");
+         _young_to_region = new_to_region;
+         _young_compact_point = _young_to_region->bottom();
+       }
+ 
+       // Object fits into current region, record new location:
+       assert(_young_compact_point + obj_size <= _young_to_region->end(), "must fit");
+       shenandoah_assert_not_forwarded(NULL, p);
+       _preserved_marks->push_if_necessary(p, p->mark());
+       p->forward_to(cast_to_oop(_young_compact_point));
+       _young_compact_point += obj_size;
+     }
+   }
+ };
+ 
+ 
  class ShenandoahPrepareForCompactionObjectClosure : public ObjectClosure {
  private:
    PreservedMarks*          const _preserved_marks;
    ShenandoahHeap*          const _heap;
    GrowableArray<ShenandoahHeapRegion*>& _empty_regions;

*** 316,10 ***
--- 591,11 ---
      _from_region = from_region;
    }
  
    void finish_region() {
      assert(_to_region != NULL, "should not happen");
+     assert(!_heap->mode()->is_generational(), "Generational GC should use different Closure");
      _to_region->set_new_top(_compact_point);
    }
  
    bool is_compact_same_region() {
      return _from_region == _to_region;

*** 361,56 ***
      p->forward_to(cast_to_oop(_compact_point));
      _compact_point += obj_size;
    }
  };
  
- class ShenandoahPrepareForCompactionTask : public AbstractGangTask {
- private:
-   PreservedMarksSet*        const _preserved_marks;
-   ShenandoahHeap*           const _heap;
-   ShenandoahHeapRegionSet** const _worker_slices;
  
! public:
!   ShenandoahPrepareForCompactionTask(PreservedMarksSet *preserved_marks, ShenandoahHeapRegionSet **worker_slices) :
      AbstractGangTask("Shenandoah Prepare For Compaction"),
!     _preserved_marks(preserved_marks),
!     _heap(ShenandoahHeap::heap()), _worker_slices(worker_slices) {
    }
  
!   static bool is_candidate_region(ShenandoahHeapRegion* r) {
!     // Empty region: get it into the slice to defragment the slice itself.
!     // We could have skipped this without violating correctness, but we really
-     // want to compact all live regions to the start of the heap, which sometimes
-     // means moving them into the fully empty regions.
-     if (r->is_empty()) return true;
- 
-     // 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 == NULL) {
-        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 != NULL) {
        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);
        }
  
--- 637,67 ---
      p->forward_to(cast_to_oop(_compact_point));
      _compact_point += obj_size;
    }
  };
  
  
! ShenandoahPrepareForCompactionTask::ShenandoahPrepareForCompactionTask(PreservedMarksSet *preserved_marks,
!                                                                        ShenandoahHeapRegionSet **worker_slices,
+                                                                        size_t num_workers) :
      AbstractGangTask("Shenandoah Prepare For Compaction"),
!     _preserved_marks(preserved_marks), _heap(ShenandoahHeap::heap()),
!     _worker_slices(worker_slices), _num_workers(num_workers) { }
+ 
+ 
+ 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 == NULL) {
+     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()) {
!     ShenandoahHeapRegion* old_to_region = (from_region->is_old())? from_region: nullptr;
!     ShenandoahHeapRegion* young_to_region = (from_region->is_young())? from_region: nullptr;
+     ShenandoahPrepareForGenerationalCompactionObjectClosure cl(this, _preserved_marks->get(worker_id), empty_regions,
+                                                                old_to_region, young_to_region, worker_id);
+     while (from_region != NULL) {
+       assert(is_candidate_region(from_region), "Sanity");
+       log_debug(gc)("Worker %u compacting %s Region " SIZE_FORMAT " which had used " SIZE_FORMAT " and %s live",
+                     worker_id, affiliation_name(from_region->affiliation()),
+                     from_region->index(), from_region->used(), from_region->has_live()? "has": "does not have");
+       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());
+     }
+   } else {
      ShenandoahPrepareForCompactionObjectClosure cl(_preserved_marks->get(worker_id), empty_regions, from_region);
      while (from_region != NULL) {
        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);
        }
  

*** 426,11 ***
      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
--- 713,11 ---
      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

*** 445,10 ***
--- 732,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();

*** 511,29 ***
    ShenandoahTrashImmediateGarbageClosure() :
      _heap(ShenandoahHeap::heap()),
      _ctx(ShenandoahHeap::heap()->complete_marking_context()) {}
  
    void heap_region_do(ShenandoahHeapRegion* r) {
!     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());
!         _heap->trash_humongous_region_at(r);
!       } else {
!         assert(r->has_live(),
!                "Region " SIZE_FORMAT " should have live", r->index());
!       }
!     } else if (r->is_humongous_continuation()) {
!       // If we hit continuation, the non-live humongous starts should have been trashed already
!       assert(r->humongous_start_region()->has_live(),
!              "Region " SIZE_FORMAT " should have live", r->index());
!     } else if (r->is_regular()) {
!       if (!r->has_live()) {
!         r->make_trash_immediate();
        }
      }
    }
  };
  
  void ShenandoahFullGC::distribute_slices(ShenandoahHeapRegionSet** worker_slices) {
    ShenandoahHeap* heap = ShenandoahHeap::heap();
--- 799,36 ---
    ShenandoahTrashImmediateGarbageClosure() :
      _heap(ShenandoahHeap::heap()),
      _ctx(ShenandoahHeap::heap()->complete_marking_context()) {}
  
    void heap_region_do(ShenandoahHeapRegion* r) {
!     if (r->affiliation() != FREE) {
!       if (r->is_humongous_start()) {
!         oop humongous_obj = cast_to_oop(r->bottom());
!         if (!_ctx->is_marked(humongous_obj)) {
!           assert(!r->has_live(),
!                  "Humongous Start %s Region " SIZE_FORMAT " is not marked, should not have live",
!                  affiliation_name(r->affiliation()),  r->index());
!           log_debug(gc)("Trashing immediate humongous region " SIZE_FORMAT " because not marked", r->index());
!           _heap->trash_humongous_region_at(r);
!         } else {
!           assert(r->has_live(),
!                  "Humongous Start %s Region " SIZE_FORMAT " should have live", affiliation_name(r->affiliation()),  r->index());
!         }
!       } else if (r->is_humongous_continuation()) {
!         // If we hit continuation, the non-live humongous starts should have been trashed already
!         assert(r->humongous_start_region()->has_live(),
!                "Humongous Continuation %s Region " SIZE_FORMAT " should have live", affiliation_name(r->affiliation()),  r->index());
+       } else if (r->is_regular()) {
+         if (!r->has_live()) {
+           log_debug(gc)("Trashing immediate regular region " SIZE_FORMAT " because has no live", r->index());
+           r->make_trash_immediate();
+         }
        }
      }
+     // else, ignore this FREE region.
+     // TODO: change iterators so they do not process FREE regions.
    }
  };
  
  void ShenandoahFullGC::distribute_slices(ShenandoahHeapRegionSet** worker_slices) {
    ShenandoahHeap* heap = ShenandoahHeap::heap();

*** 696,17 ***
      // This is needed because we are potentially sliding the data through them.
      ShenandoahEnsureHeapActiveClosure ecl;
      heap->heap_region_iterate(&ecl);
    }
  
    // Compute the new addresses for regular objects
    {
      ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_calculate_addresses_regular);
  
      distribute_slices(worker_slices);
  
!     ShenandoahPrepareForCompactionTask task(_preserved_marks, worker_slices);
      heap->workers()->run_task(&task);
    }
  
    // Compute the new addresses for humongous objects
    {
--- 991,25 ---
      // This is needed because we are potentially sliding the data through them.
      ShenandoahEnsureHeapActiveClosure ecl;
      heap->heap_region_iterate(&ecl);
    }
  
+   if (heap->mode()->is_generational()) {
+     heap->young_generation()->clear_used();
+     heap->old_generation()->clear_used();
+   }
+ 
    // Compute the new addresses for regular objects
    {
      ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_calculate_addresses_regular);
  
      distribute_slices(worker_slices);
  
!     size_t num_workers = heap->max_workers();
+ 
+     ResourceMark rm;
+     ShenandoahPrepareForCompactionTask task(_preserved_marks, worker_slices, num_workers);
      heap->workers()->run_task(&task);
    }
  
    // Compute the new addresses for humongous objects
    {

*** 912,10 ***
--- 1215,19 ---
      if (r->is_trash()) {
        live = 0;
        r->recycle();
      }
  
+     // Update final usage for generations
+     if (_heap->mode()->is_generational() && live != 0) {
+       if (r->is_young()) {
+         _heap->young_generation()->increase_used(live);
+       } else if (r->is_old()) {
+         _heap->old_generation()->increase_used(live);
+       }
+     }
+ 
      r->set_live_data(live);
      r->reset_alloc_metadata();
      _live += live;
    }
  

*** 949,30 ***
        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(heap->get_region(old_start)->bottom(),
                                     heap->get_region(new_start)->bottom(),
                                     words_size);
  
        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)) {
--- 1261,34 ---
        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(heap->get_region(old_start)->bottom(),
                                     heap->get_region(new_start)->bottom(),
                                     words_size);
  
        oop new_obj = cast_to_oop(heap->get_region(new_start)->bottom());
        new_obj->init_mark();
  
        {
+         ShenandoahRegionAffiliation original_affiliation = r->affiliation();
          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(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)) {

*** 1047,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();
  }
--- 1363,24 ---
  
    // Bring regions in proper states after the collection, and set heap properties.
    {
      ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_rebuild);
  
+     if (heap->mode()->is_generational()) {
+       heap->young_generation()->clear_used();
+       heap->old_generation()->clear_used();
+     }
+ 
      ShenandoahPostCompactClosure post_compact;
      heap->heap_region_iterate(&post_compact);
      heap->set_used(post_compact.get_live());
+     if (heap->mode()->is_generational()) {
+       log_info(gc)("FullGC done: GLOBAL usage: " SIZE_FORMAT ", young usage: " SIZE_FORMAT ", old usage: " SIZE_FORMAT,
+                     post_compact.get_live(), heap->young_generation()->used(), heap->old_generation()->used());
+     }
  
      heap->collection_set()->clear();
      heap->free_set()->rebuild();
    }
  
!   heap->clear_cancelled_gc(true /* clear oom handler */);
  }
< prev index next >