< prev index next >

src/hotspot/share/gc/shenandoah/shenandoahHeap.inline.hpp

Print this page
*** 39,11 ***
--- 39,14 ---
  #include "gc/shenandoah/shenandoahWorkGroup.hpp"
  #include "gc/shenandoah/shenandoahHeapRegionSet.inline.hpp"
  #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
  #include "gc/shenandoah/shenandoahControlThread.hpp"
  #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
+ #include "gc/shenandoah/shenandoahScanRemembered.inline.hpp"
  #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
+ #include "gc/shenandoah/shenandoahScanRemembered.inline.hpp"
+ #include "gc/shenandoah/mode/shenandoahMode.hpp"
  #include "oops/compressedOops.inline.hpp"
  #include "oops/oop.inline.hpp"
  #include "runtime/atomic.hpp"
  #include "runtime/prefetch.inline.hpp"
  #include "runtime/thread.hpp"

*** 262,13 ***
    } else {
      return true;
    }
  }
  
! inline void ShenandoahHeap::clear_cancelled_gc() {
    _cancelled_gc.set(CANCELLABLE);
!   _oom_evac_handler.clear();
  }
  
  inline HeapWord* ShenandoahHeap::allocate_from_gclab(Thread* thread, size_t size) {
    assert(UseTLAB, "TLABs should be enabled");
  
--- 265,21 ---
    } else {
      return true;
    }
  }
  
! inline void ShenandoahHeap::clear_cancelled_gc(bool clear_oom_handler) {
    _cancelled_gc.set(CANCELLABLE);
!   if (_cancel_requested_time > 0) {
+     double cancel_time = os::elapsedTime() - _cancel_requested_time;
+     log_info(gc)("GC cancellation took %.3fs", cancel_time);
+     _cancel_requested_time = 0;
+   }
+ 
+   if (clear_oom_handler) {
+     _oom_evac_handler.clear();
+   }
  }
  
  inline HeapWord* ShenandoahHeap::allocate_from_gclab(Thread* thread, size_t size) {
    assert(UseTLAB, "TLABs should be enabled");
  

*** 281,89 ***
    }
    HeapWord* obj = gclab->allocate(size);
    if (obj != NULL) {
      return obj;
    }
-   // Otherwise...
    return allocate_from_gclab_slow(thread, size);
  }
  
  inline oop ShenandoahHeap::evacuate_object(oop p, Thread* thread) {
!   if (ShenandoahThreadLocalData::is_oom_during_evac(Thread::current())) {
      // This thread went through the OOM during evac protocol and it is safe to return
      // the forward pointer. It must not attempt to evacuate any more.
      return ShenandoahBarrierSet::resolve_forwarded(p);
    }
  
    assert(ShenandoahThreadLocalData::is_evac_allowed(thread), "must be enclosed in oom-evac scope");
  
!   size_t size = p->size();
  
!   assert(!heap_region_containing(p)->is_humongous(), "never evacuate humongous objects");
  
!   bool alloc_from_gclab = true;
    HeapWord* copy = NULL;
  
  #ifdef ASSERT
    if (ShenandoahOOMDuringEvacALot &&
        (os::random() & 1) == 0) { // Simulate OOM every ~2nd slow-path call
          copy = NULL;
    } else {
  #endif
      if (UseTLAB) {
!       copy = allocate_from_gclab(thread, size);
      }
      if (copy == NULL) {
!       ShenandoahAllocRequest req = ShenandoahAllocRequest::for_shared_gc(size);
!       copy = allocate_memory(req);
!       alloc_from_gclab = false;
      }
  #ifdef ASSERT
    }
  #endif
  
    if (copy == NULL) {
      control_thread()->handle_alloc_failure_evac(size);
  
      _oom_evac_handler.handle_out_of_memory_during_evacuation();
  
      return ShenandoahBarrierSet::resolve_forwarded(p);
    }
  
    // Copy the object:
    Copy::aligned_disjoint_words(cast_from_oop<HeapWord*>(p), copy, size);
  
-   // Try to install the new forwarding pointer.
    oop copy_val = cast_to_oop(copy);
    oop result = ShenandoahForwarding::try_update_forwardee(p, copy_val);
    if (result == copy_val) {
      // Successfully evacuated. Our copy is now the public one!
      shenandoah_assert_correct(NULL, copy_val);
      return copy_val;
    }  else {
      // Failed to evacuate. We need to deal with the object that is left behind. Since this
      // new allocation is certainly after TAMS, it will be considered live in the next cycle.
      // But if it happens to contain references to evacuated regions, those references would
      // not get updated for this stale copy during this cycle, and we will crash while scanning
      // it the next cycle.
!     //
!     // For GCLAB allocations, it is enough to rollback the allocation ptr. Either the next
!     // object will overwrite this stale copy, or the filler object on LAB retirement will
!     // do this. For non-GCLAB allocations, we have no way to retract the allocation, and
!     // have to explicitly overwrite the copy with the filler object. With that overwrite,
!     // we have to keep the fwdptr initialized and pointing to our (stale) copy.
!     if (alloc_from_gclab) {
!       ShenandoahThreadLocalData::gclab(thread)->undo_allocation(copy, size);
      } else {
        fill_with_object(copy, size);
        shenandoah_assert_correct(NULL, copy_val);
      }
      shenandoah_assert_correct(NULL, result);
      return result;
    }
  }
  
  inline bool ShenandoahHeap::requires_marking(const void* entry) const {
    oop obj = cast_to_oop(entry);
    return !_marking_context->is_marked_strong(obj);
  }
  
--- 292,229 ---
    }
    HeapWord* obj = gclab->allocate(size);
    if (obj != NULL) {
      return obj;
    }
    return allocate_from_gclab_slow(thread, size);
  }
  
+ inline HeapWord* ShenandoahHeap::allocate_from_plab(Thread* thread, size_t size, bool is_promotion) {
+   assert(UseTLAB, "TLABs should be enabled");
+ 
+   PLAB* plab = ShenandoahThreadLocalData::plab(thread);
+   if (is_promotion && !ShenandoahThreadLocalData::allow_plab_promotions(thread)) {
+     return NULL;
+   } else if (plab == NULL) {
+     assert(!thread->is_Java_thread() && !thread->is_Worker_thread(),
+            "Performance: thread should have PLAB: %s", thread->name());
+     // No PLABs in this thread, fallback to shared allocation
+     return NULL;
+   }
+   HeapWord* obj = plab->allocate(size);
+   if (obj == NULL) {
+     obj = allocate_from_plab_slow(thread, size, is_promotion);
+   }
+   if (is_promotion) {
+     ShenandoahThreadLocalData::add_to_plab_promoted(thread, size * HeapWordSize);
+   } else {
+     ShenandoahThreadLocalData::add_to_plab_evacuated(thread, size * HeapWordSize);
+   }
+   return obj;
+ }
+ 
  inline oop ShenandoahHeap::evacuate_object(oop p, Thread* thread) {
!   assert(thread == Thread::current(), "Expected thread parameter to be current thread.");
+   if (ShenandoahThreadLocalData::is_oom_during_evac(thread)) {
      // This thread went through the OOM during evac protocol and it is safe to return
      // the forward pointer. It must not attempt to evacuate any more.
      return ShenandoahBarrierSet::resolve_forwarded(p);
    }
  
    assert(ShenandoahThreadLocalData::is_evac_allowed(thread), "must be enclosed in oom-evac scope");
  
!   ShenandoahHeapRegion* r = heap_region_containing(p);
+   assert(!r->is_humongous(), "never evacuate humongous objects");
  
!   ShenandoahRegionAffiliation target_gen = r->affiliation();
+   if (mode()->is_generational() && ShenandoahHeap::heap()->is_gc_generation_young() &&
+       target_gen == YOUNG_GENERATION && ShenandoahPromoteTenuredObjects) {
+     markWord mark = p->mark();
+     if (mark.is_marked()) {
+       // Already forwarded.
+       return ShenandoahBarrierSet::resolve_forwarded(p);
+     }
+     if (mark.has_displaced_mark_helper()) {
+       // We don't want to deal with MT here just to ensure we read the right mark word.
+       // Skip the potential promotion attempt for this one.
+     } else if (r->age() + mark.age() >= InitialTenuringThreshold) {
+       oop result = try_evacuate_object(p, thread, r, OLD_GENERATION);
+       if (result != NULL) {
+         return result;
+       }
+       // If we failed to promote this aged object, we'll fall through to code below and evacuat to young-gen.
+     }
+   }
+   return try_evacuate_object(p, thread, r, target_gen);
+ }
  
! // try_evacuate_object registers the object and dirties the associated remembered set information when evacuating
+ // to OLD_GENERATION.
+ inline oop ShenandoahHeap::try_evacuate_object(oop p, Thread* thread, ShenandoahHeapRegion* from_region,
+                                                ShenandoahRegionAffiliation target_gen) {
+   bool alloc_from_lab = true;
    HeapWord* copy = NULL;
+   size_t size = p->size();
+   bool is_promotion = (target_gen == OLD_GENERATION) && from_region->is_young();
  
  #ifdef ASSERT
    if (ShenandoahOOMDuringEvacALot &&
        (os::random() & 1) == 0) { // Simulate OOM every ~2nd slow-path call
          copy = NULL;
    } else {
  #endif
      if (UseTLAB) {
!       switch (target_gen) {
+         case YOUNG_GENERATION: {
+            copy = allocate_from_gclab(thread, size);
+            if ((copy == nullptr) && (size < ShenandoahThreadLocalData::gclab_size(thread))) {
+              // GCLAB allocation failed because we are bumping up against the limit on young evacuation reserve.  Try resetting
+              // the desired GCLAB size and retry GCLAB allocation to avoid cascading of shared memory allocations.
+              ShenandoahThreadLocalData::set_gclab_size(thread, PLAB::min_size());
+              copy = allocate_from_gclab(thread, size);
+              // If we still get nullptr, we'll try a shared allocation below.
+            }
+            break;
+         }
+         case OLD_GENERATION: {
+            if (ShenandoahUsePLAB) {
+              copy = allocate_from_plab(thread, size, is_promotion);
+              if ((copy == nullptr) && (size < ShenandoahThreadLocalData::plab_size(thread))) {
+                // PLAB allocation failed because we are bumping up against the limit on old evacuation reserve.  Try resetting
+                // the desired PLAB size and retry PLAB allocation to avoid cascading of shared memory allocations.
+                ShenandoahThreadLocalData::set_plab_size(thread, PLAB::min_size());
+                copy = allocate_from_plab(thread, size, is_promotion);
+                // If we still get nullptr, we'll try a shared allocation below.
+              }
+            }
+            break;
+         }
+         default: {
+           ShouldNotReachHere();
+           break;
+         }
+       }
      }
+ 
      if (copy == NULL) {
!       // If we failed to allocated in LAB, we'll try a shared allocation.
!       ShenandoahAllocRequest req = ShenandoahAllocRequest::for_shared_gc(size, target_gen);
!       copy = allocate_memory(req, is_promotion);
+       alloc_from_lab = false;
      }
  #ifdef ASSERT
    }
  #endif
  
    if (copy == NULL) {
+     if (target_gen == OLD_GENERATION) {
+       assert(mode()->is_generational(), "Should only be here in generational mode.");
+       if (from_region->is_young()) {
+         // Signal that promotion failed. Will evacuate this old object somewhere in young gen.
+         handle_promotion_failure();
+         return NULL;
+       } else {
+         // Remember that evacuation to old gen failed. We'll want to trigger a full gc to recover from this
+         // after the evacuation threads have finished.
+         handle_old_evacuation_failure();
+       }
+     }
+ 
      control_thread()->handle_alloc_failure_evac(size);
  
      _oom_evac_handler.handle_out_of_memory_during_evacuation();
  
      return ShenandoahBarrierSet::resolve_forwarded(p);
    }
  
    // Copy the object:
    Copy::aligned_disjoint_words(cast_from_oop<HeapWord*>(p), copy, size);
  
    oop copy_val = cast_to_oop(copy);
+ 
+   if (mode()->is_generational() && target_gen == YOUNG_GENERATION && is_aging_cycle()) {
+     ShenandoahHeap::increase_object_age(copy_val, from_region->age() + 1);
+   }
+ 
+   // Try to install the new forwarding pointer.
    oop result = ShenandoahForwarding::try_update_forwardee(p, copy_val);
    if (result == copy_val) {
      // Successfully evacuated. Our copy is now the public one!
+     if (mode()->is_generational() && target_gen == OLD_GENERATION) {
+       handle_old_evacuation(copy, size, from_region->is_young());
+     }
      shenandoah_assert_correct(NULL, copy_val);
      return copy_val;
    }  else {
      // Failed to evacuate. We need to deal with the object that is left behind. Since this
      // new allocation is certainly after TAMS, it will be considered live in the next cycle.
      // But if it happens to contain references to evacuated regions, those references would
      // not get updated for this stale copy during this cycle, and we will crash while scanning
      // it the next cycle.
!     if (alloc_from_lab) {
!        // For LAB allocations, it is enough to rollback the allocation ptr. Either the next
!        // object will overwrite this stale copy, or the filler object on LAB retirement will
!        // do this.
!        switch (target_gen) {
!          case YOUNG_GENERATION: {
!              ShenandoahThreadLocalData::gclab(thread)->undo_allocation(copy, size);
!             break;
+          }
+          case OLD_GENERATION: {
+             ShenandoahThreadLocalData::plab(thread)->undo_allocation(copy, size);
+             if (is_promotion) {
+               ShenandoahThreadLocalData::subtract_from_plab_promoted(thread, size * HeapWordSize);
+             } else {
+               ShenandoahThreadLocalData::subtract_from_plab_evacuated(thread, size * HeapWordSize);
+             }
+             break;
+          }
+          default: {
+            ShouldNotReachHere();
+            break;
+          }
+        }
      } else {
+       // For non-LAB allocations, we have no way to retract the allocation, and
+       // have to explicitly overwrite the copy with the filler object. With that overwrite,
+       // we have to keep the fwdptr initialized and pointing to our (stale) copy.
        fill_with_object(copy, size);
        shenandoah_assert_correct(NULL, copy_val);
+       // For non-LAB allocations, the object has already been registered
      }
      shenandoah_assert_correct(NULL, result);
      return result;
    }
  }
  
+ void ShenandoahHeap::increase_object_age(oop obj, uint additional_age) {
+   markWord w = obj->has_displaced_mark() ? obj->displaced_mark() : obj->mark();
+   w = w.set_age(MIN2(markWord::max_age, w.age() + additional_age));
+   if (obj->has_displaced_mark()) {
+     obj->set_displaced_mark(w);
+   } else {
+     obj->set_mark(w);
+   }
+ }
+ 
+ inline bool ShenandoahHeap::clear_old_evacuation_failure() {
+   return _old_gen_oom_evac.try_unset();
+ }
+ 
+ inline bool ShenandoahHeap::is_old(oop obj) const {
+   return is_gc_generation_young() && is_in_old(obj);
+ }
+ 
  inline bool ShenandoahHeap::requires_marking(const void* entry) const {
    oop obj = cast_to_oop(entry);
    return !_marking_context->is_marked_strong(obj);
  }
  

*** 380,15 ***
  inline bool ShenandoahHeap::is_stable() const {
    return _gc_state.is_clear();
  }
  
  inline bool ShenandoahHeap::is_idle() const {
!   return _gc_state.is_unset(MARKING | EVACUATION | UPDATEREFS);
  }
  
  inline bool ShenandoahHeap::is_concurrent_mark_in_progress() const {
!   return _gc_state.is_set(MARKING);
  }
  
  inline bool ShenandoahHeap::is_evacuation_in_progress() const {
    return _gc_state.is_set(EVACUATION);
  }
--- 531,23 ---
  inline bool ShenandoahHeap::is_stable() const {
    return _gc_state.is_clear();
  }
  
  inline bool ShenandoahHeap::is_idle() const {
!   return _gc_state.is_unset(YOUNG_MARKING | OLD_MARKING | EVACUATION | UPDATEREFS);
  }
  
  inline bool ShenandoahHeap::is_concurrent_mark_in_progress() const {
!   return _gc_state.is_set(YOUNG_MARKING | OLD_MARKING);
+ }
+ 
+ inline bool ShenandoahHeap::is_concurrent_young_mark_in_progress() const {
+   return _gc_state.is_set(YOUNG_MARKING);
+ }
+ 
+ inline bool ShenandoahHeap::is_concurrent_old_mark_in_progress() const {
+   return _gc_state.is_set(OLD_MARKING);
  }
  
  inline bool ShenandoahHeap::is_evacuation_in_progress() const {
    return _gc_state.is_set(EVACUATION);
  }

*** 423,21 ***
  
  inline bool ShenandoahHeap::is_concurrent_weak_root_in_progress() const {
    return _gc_state.is_set(WEAK_ROOTS);
  }
  
  template<class T>
  inline void ShenandoahHeap::marked_object_iterate(ShenandoahHeapRegion* region, T* cl) {
    marked_object_iterate(region, cl, region->top());
  }
  
  template<class T>
  inline void ShenandoahHeap::marked_object_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* limit) {
    assert(! region->is_humongous_continuation(), "no humongous continuation regions here");
  
!   ShenandoahMarkingContext* const ctx = complete_marking_context();
-   assert(ctx->is_complete(), "sanity");
  
    HeapWord* tams = ctx->top_at_mark_start(region);
  
    size_t skip_bitmap_delta = 1;
    HeapWord* start = region->bottom();
--- 582,92 ---
  
  inline bool ShenandoahHeap::is_concurrent_weak_root_in_progress() const {
    return _gc_state.is_set(WEAK_ROOTS);
  }
  
+ inline bool ShenandoahHeap::is_aging_cycle() const {
+   return _is_aging_cycle.is_set();
+ }
+ 
+ inline size_t ShenandoahHeap::set_promotion_reserve(size_t new_val) {
+   size_t orig = _promotion_reserve;
+   _promotion_reserve = new_val;
+   return orig;
+ }
+ 
+ inline size_t ShenandoahHeap::get_promotion_reserve() const {
+   return _promotion_reserve;
+ }
+ 
+ // returns previous value
+ size_t ShenandoahHeap::capture_old_usage(size_t old_usage) {
+   size_t previous_value = _captured_old_usage;
+   _captured_old_usage = old_usage;
+   return previous_value;
+ }
+ 
+ void ShenandoahHeap::set_previous_promotion(size_t promoted_bytes) {
+   _previous_promotion = promoted_bytes;
+ }
+ 
+ size_t ShenandoahHeap::get_previous_promotion() const {
+   return _previous_promotion;
+ }
+ 
+ inline size_t ShenandoahHeap::set_old_evac_reserve(size_t new_val) {
+   size_t orig = _old_evac_reserve;
+   _old_evac_reserve = new_val;
+   return orig;
+ }
+ 
+ inline size_t ShenandoahHeap::get_old_evac_reserve() const {
+   return _old_evac_reserve;
+ }
+ 
+ inline void ShenandoahHeap::reset_old_evac_expended() {
+   _old_evac_expended = 0;
+ }
+ 
+ inline size_t ShenandoahHeap::expend_old_evac(size_t increment) {
+   _old_evac_expended += increment;
+   return _old_evac_expended;
+ }
+ 
+ inline size_t ShenandoahHeap::get_old_evac_expended() const {
+   return _old_evac_expended;
+ }
+ 
+ inline size_t ShenandoahHeap::set_young_evac_reserve(size_t new_val) {
+   size_t orig = _young_evac_reserve;
+   _young_evac_reserve = new_val;
+   return orig;
+ }
+ 
+ inline size_t ShenandoahHeap::get_young_evac_reserve() const {
+   return _young_evac_reserve;
+ }
+ 
+ inline intptr_t ShenandoahHeap::set_alloc_supplement_reserve(intptr_t new_val) {
+   intptr_t orig = _alloc_supplement_reserve;
+   _alloc_supplement_reserve = new_val;
+   return orig;
+ }
+ 
+ inline intptr_t ShenandoahHeap::get_alloc_supplement_reserve() const {
+   return _alloc_supplement_reserve;
+ }
+ 
  template<class T>
  inline void ShenandoahHeap::marked_object_iterate(ShenandoahHeapRegion* region, T* cl) {
    marked_object_iterate(region, cl, region->top());
  }
  
  template<class T>
  inline void ShenandoahHeap::marked_object_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* limit) {
    assert(! region->is_humongous_continuation(), "no humongous continuation regions here");
  
!   ShenandoahMarkingContext* const ctx = marking_context();
  
    HeapWord* tams = ctx->top_at_mark_start(region);
  
    size_t skip_bitmap_delta = 1;
    HeapWord* start = region->bottom();

*** 564,23 ***
    } else {
      return NULL;
    }
  }
  
- inline void ShenandoahHeap::mark_complete_marking_context() {
-   _marking_context->mark_complete();
- }
- 
- inline void ShenandoahHeap::mark_incomplete_marking_context() {
-   _marking_context->mark_incomplete();
- }
- 
  inline ShenandoahMarkingContext* ShenandoahHeap::complete_marking_context() const {
    assert (_marking_context->is_complete()," sanity");
    return _marking_context;
  }
  
  inline ShenandoahMarkingContext* ShenandoahHeap::marking_context() const {
    return _marking_context;
  }
  
  #endif // SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_INLINE_HPP
--- 794,39 ---
    } else {
      return NULL;
    }
  }
  
  inline ShenandoahMarkingContext* ShenandoahHeap::complete_marking_context() const {
    assert (_marking_context->is_complete()," sanity");
    return _marking_context;
  }
  
  inline ShenandoahMarkingContext* ShenandoahHeap::marking_context() const {
    return _marking_context;
  }
  
+ inline void ShenandoahHeap::clear_cards_for(ShenandoahHeapRegion* region) {
+   if (mode()->is_generational()) {
+     _card_scan->mark_range_as_empty(region->bottom(), pointer_delta(region->end(), region->bottom()));
+   }
+ }
+ 
+ inline void ShenandoahHeap::dirty_cards(HeapWord* start, HeapWord* end) {
+   assert(mode()->is_generational(), "Should only be used for generational mode");
+   size_t words = pointer_delta(end, start);
+   _card_scan->mark_range_as_dirty(start, words);
+ }
+ 
+ inline void ShenandoahHeap::clear_cards(HeapWord* start, HeapWord* end) {
+   assert(mode()->is_generational(), "Should only be used for generational mode");
+   size_t words = pointer_delta(end, start);
+   _card_scan->mark_range_as_clean(start, words);
+ }
+ 
+ inline void ShenandoahHeap::mark_card_as_dirty(void* location) {
+   if (mode()->is_generational()) {
+     _card_scan->mark_card_as_dirty((HeapWord*)location);
+   }
+ }
+ 
  #endif // SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_INLINE_HPP
< prev index next >