< prev index next >

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

Print this page

 25 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_INLINE_HPP
 26 #define SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_INLINE_HPP
 27 
 28 #include "gc/shenandoah/shenandoahHeap.hpp"
 29 
 30 #include "classfile/javaClasses.inline.hpp"
 31 #include "gc/shared/markBitMap.inline.hpp"
 32 #include "gc/shared/threadLocalAllocBuffer.inline.hpp"
 33 #include "gc/shared/continuationGCSupport.inline.hpp"
 34 #include "gc/shared/suspendibleThreadSet.hpp"
 35 #include "gc/shared/tlab_globals.hpp"
 36 #include "gc/shenandoah/shenandoahAsserts.hpp"
 37 #include "gc/shenandoah/shenandoahBarrierSet.inline.hpp"
 38 #include "gc/shenandoah/shenandoahCollectionSet.inline.hpp"
 39 #include "gc/shenandoah/shenandoahForwarding.inline.hpp"
 40 #include "gc/shenandoah/shenandoahWorkGroup.hpp"
 41 #include "gc/shenandoah/shenandoahHeapRegionSet.inline.hpp"
 42 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
 43 #include "gc/shenandoah/shenandoahControlThread.hpp"
 44 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"

 45 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"


 46 #include "oops/compressedOops.inline.hpp"
 47 #include "oops/oop.inline.hpp"
 48 #include "runtime/atomic.hpp"
 49 #include "runtime/javaThread.hpp"
 50 #include "runtime/prefetch.inline.hpp"
 51 #include "utilities/copy.hpp"
 52 #include "utilities/globalDefinitions.hpp"
 53 
 54 inline ShenandoahHeap* ShenandoahHeap::heap() {
 55   return named_heap<ShenandoahHeap>(CollectedHeap::Shenandoah);
 56 }
 57 
 58 inline ShenandoahHeapRegion* ShenandoahRegionIterator::next() {
 59   size_t new_index = Atomic::add(&_index, (size_t) 1, memory_order_relaxed);
 60   // get_region() provides the bounds-check and returns NULL on OOB.
 61   return _heap->get_region(new_index - 1);
 62 }
 63 
 64 inline bool ShenandoahHeap::has_forwarded_objects() const {
 65   return _gc_state.is_set(HAS_FORWARDED);

248     return cancelled_gc();
249   }
250 
251   jbyte prev = _cancelled_gc.cmpxchg(NOT_CANCELLED, CANCELLABLE);
252   if (prev == CANCELLABLE || prev == NOT_CANCELLED) {
253     if (SuspendibleThreadSet::should_yield()) {
254       SuspendibleThreadSet::yield();
255     }
256 
257     // Back to CANCELLABLE. The thread that poked NOT_CANCELLED first gets
258     // to restore to CANCELLABLE.
259     if (prev == CANCELLABLE) {
260       _cancelled_gc.set(CANCELLABLE);
261     }
262     return false;
263   } else {
264     return true;
265   }
266 }
267 
268 inline void ShenandoahHeap::clear_cancelled_gc() {
269   _cancelled_gc.set(CANCELLABLE);
270   _oom_evac_handler.clear();








271 }
272 
273 inline HeapWord* ShenandoahHeap::allocate_from_gclab(Thread* thread, size_t size) {
274   assert(UseTLAB, "TLABs should be enabled");
275 
276   PLAB* gclab = ShenandoahThreadLocalData::gclab(thread);
277   if (gclab == NULL) {
278     assert(!thread->is_Java_thread() && !thread->is_Worker_thread(),
279            "Performance: thread should have GCLAB: %s", thread->name());
280     // No GCLABs in this thread, fallback to shared allocation
281     return NULL;
282   }
283   HeapWord* obj = gclab->allocate(size);
284   if (obj != NULL) {
285     return obj;
286   }
287   // Otherwise...
288   return allocate_from_gclab_slow(thread, size);
289 }
290 































291 inline oop ShenandoahHeap::evacuate_object(oop p, Thread* thread) {
292   if (ShenandoahThreadLocalData::is_oom_during_evac(Thread::current())) {

293     // This thread went through the OOM during evac protocol and it is safe to return
294     // the forward pointer. It must not attempt to evacuate any more.
295     return ShenandoahBarrierSet::resolve_forwarded(p);
296   }
297 
298   assert(ShenandoahThreadLocalData::is_evac_allowed(thread), "must be enclosed in oom-evac scope");
299 
300   size_t size = p->size();

301 
302   assert(!heap_region_containing(p)->is_humongous(), "never evacuate humongous objects");




















303 
304   bool alloc_from_gclab = true;





305   HeapWord* copy = NULL;


306 
307 #ifdef ASSERT
308   if (ShenandoahOOMDuringEvacALot &&
309       (os::random() & 1) == 0) { // Simulate OOM every ~2nd slow-path call
310         copy = NULL;
311   } else {
312 #endif
313     if (UseTLAB) {
314       copy = allocate_from_gclab(thread, size);














































315     }

316     if (copy == NULL) {
317       ShenandoahAllocRequest req = ShenandoahAllocRequest::for_shared_gc(size);
318       copy = allocate_memory(req);
319       alloc_from_gclab = false;







320     }
321 #ifdef ASSERT
322   }
323 #endif
324 
325   if (copy == NULL) {






























































326     control_thread()->handle_alloc_failure_evac(size);
327 
328     _oom_evac_handler.handle_out_of_memory_during_evacuation();
329 
330     return ShenandoahBarrierSet::resolve_forwarded(p);
331   }
332 
333   // Copy the object:
334   Copy::aligned_disjoint_words(cast_from_oop<HeapWord*>(p), copy, size);
335 
336   // Try to install the new forwarding pointer.
337   oop copy_val = cast_to_oop(copy);






338   ContinuationGCSupport::relativize_stack_chunk(copy_val);
339 
340   oop result = ShenandoahForwarding::try_update_forwardee(p, copy_val);
341   if (result == copy_val) {
342     // Successfully evacuated. Our copy is now the public one!



343     shenandoah_assert_correct(NULL, copy_val);
344     return copy_val;
345   }  else {
346     // Failed to evacuate. We need to deal with the object that is left behind. Since this
347     // new allocation is certainly after TAMS, it will be considered live in the next cycle.
348     // But if it happens to contain references to evacuated regions, those references would
349     // not get updated for this stale copy during this cycle, and we will crash while scanning
350     // it the next cycle.
351     //
352     // For GCLAB allocations, it is enough to rollback the allocation ptr. Either the next
353     // object will overwrite this stale copy, or the filler object on LAB retirement will
354     // do this. For non-GCLAB allocations, we have no way to retract the allocation, and
355     // have to explicitly overwrite the copy with the filler object. With that overwrite,
356     // we have to keep the fwdptr initialized and pointing to our (stale) copy.
357     if (alloc_from_gclab) {
358       ShenandoahThreadLocalData::gclab(thread)->undo_allocation(copy, size);















359     } else {



360       fill_with_object(copy, size);
361       shenandoah_assert_correct(NULL, copy_val);

362     }
363     shenandoah_assert_correct(NULL, result);
364     return result;
365   }
366 }
367 


















368 inline bool ShenandoahHeap::requires_marking(const void* entry) const {
369   oop obj = cast_to_oop(entry);
370   return !_marking_context->is_marked_strong(obj);
371 }
372 
373 inline bool ShenandoahHeap::in_collection_set(oop p) const {
374   assert(collection_set() != NULL, "Sanity");
375   return collection_set()->is_in(p);
376 }
377 
378 inline bool ShenandoahHeap::in_collection_set_loc(void* p) const {
379   assert(collection_set() != NULL, "Sanity");
380   return collection_set()->is_in_loc(p);
381 }
382 
383 inline bool ShenandoahHeap::is_stable() const {
384   return _gc_state.is_clear();
385 }
386 
387 inline bool ShenandoahHeap::is_idle() const {
388   return _gc_state.is_unset(MARKING | EVACUATION | UPDATEREFS);
389 }
390 
391 inline bool ShenandoahHeap::is_concurrent_mark_in_progress() const {
392   return _gc_state.is_set(MARKING);








393 }
394 
395 inline bool ShenandoahHeap::is_evacuation_in_progress() const {
396   return _gc_state.is_set(EVACUATION);
397 }
398 
399 inline bool ShenandoahHeap::is_gc_in_progress_mask(uint mask) const {
400   return _gc_state.is_set(mask);
401 }
402 
403 inline bool ShenandoahHeap::is_degenerated_gc_in_progress() const {
404   return _degenerated_gc_in_progress.is_set();
405 }
406 
407 inline bool ShenandoahHeap::is_full_gc_in_progress() const {
408   return _full_gc_in_progress.is_set();
409 }
410 
411 inline bool ShenandoahHeap::is_full_gc_move_in_progress() const {
412   return _full_gc_move_in_progress.is_set();
413 }
414 
415 inline bool ShenandoahHeap::is_update_refs_in_progress() const {
416   return _gc_state.is_set(UPDATEREFS);
417 }
418 
419 inline bool ShenandoahHeap::is_stw_gc_in_progress() const {
420   return is_full_gc_in_progress() || is_degenerated_gc_in_progress();
421 }
422 
423 inline bool ShenandoahHeap::is_concurrent_strong_root_in_progress() const {
424   return _concurrent_strong_root_in_progress.is_set();
425 }
426 
427 inline bool ShenandoahHeap::is_concurrent_weak_root_in_progress() const {
428   return _gc_state.is_set(WEAK_ROOTS);
429 }
430 




























































































431 template<class T>
432 inline void ShenandoahHeap::marked_object_iterate(ShenandoahHeapRegion* region, T* cl) {
433   marked_object_iterate(region, cl, region->top());
434 }
435 
436 template<class T>
437 inline void ShenandoahHeap::marked_object_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* limit) {
438   assert(! region->is_humongous_continuation(), "no humongous continuation regions here");
439 
440   ShenandoahMarkingContext* const ctx = complete_marking_context();
441   assert(ctx->is_complete(), "sanity");
442 
443   HeapWord* tams = ctx->top_at_mark_start(region);
444 
445   size_t skip_bitmap_delta = 1;
446   HeapWord* start = region->bottom();
447   HeapWord* end = MIN2(tams, region->end());
448 
449   // Step 1. Scan below the TAMS based on bitmap data.
450   HeapWord* limit_bitmap = MIN2(limit, tams);
451 
452   // Try to scan the initial candidate. If the candidate is above the TAMS, it would
453   // fail the subsequent "< limit_bitmap" checks, and fall through to Step 2.
454   HeapWord* cb = ctx->get_next_marked_addr(start, end);
455 
456   intx dist = ShenandoahMarkScanPrefetch;
457   if (dist > 0) {
458     // Batched scan that prefetches the oop data, anticipating the access to
459     // either header, oop field, or forwarding pointer. Not that we cannot
460     // touch anything in oop, while it still being prefetched to get enough
461     // time for prefetch to work. This is why we try to scan the bitmap linearly,

552     HeapWord* bottom = region->bottom();
553     if (top > bottom) {
554       region = region->humongous_start_region();
555       ShenandoahObjectToOopBoundedClosure<T> objs(cl, bottom, top);
556       marked_object_iterate(region, &objs);
557     }
558   } else {
559     ShenandoahObjectToOopClosure<T> objs(cl);
560     marked_object_iterate(region, &objs, top);
561   }
562 }
563 
564 inline ShenandoahHeapRegion* const ShenandoahHeap::get_region(size_t region_idx) const {
565   if (region_idx < _num_regions) {
566     return _regions[region_idx];
567   } else {
568     return NULL;
569   }
570 }
571 
572 inline void ShenandoahHeap::mark_complete_marking_context() {
573   _marking_context->mark_complete();
574 }
575 
576 inline void ShenandoahHeap::mark_incomplete_marking_context() {
577   _marking_context->mark_incomplete();
578 }
579 
580 inline ShenandoahMarkingContext* ShenandoahHeap::complete_marking_context() const {
581   assert (_marking_context->is_complete()," sanity");
582   return _marking_context;
583 }
584 
585 inline ShenandoahMarkingContext* ShenandoahHeap::marking_context() const {
586   return _marking_context;
587 }
588 
























589 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_INLINE_HPP

 25 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_INLINE_HPP
 26 #define SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_INLINE_HPP
 27 
 28 #include "gc/shenandoah/shenandoahHeap.hpp"
 29 
 30 #include "classfile/javaClasses.inline.hpp"
 31 #include "gc/shared/markBitMap.inline.hpp"
 32 #include "gc/shared/threadLocalAllocBuffer.inline.hpp"
 33 #include "gc/shared/continuationGCSupport.inline.hpp"
 34 #include "gc/shared/suspendibleThreadSet.hpp"
 35 #include "gc/shared/tlab_globals.hpp"
 36 #include "gc/shenandoah/shenandoahAsserts.hpp"
 37 #include "gc/shenandoah/shenandoahBarrierSet.inline.hpp"
 38 #include "gc/shenandoah/shenandoahCollectionSet.inline.hpp"
 39 #include "gc/shenandoah/shenandoahForwarding.inline.hpp"
 40 #include "gc/shenandoah/shenandoahWorkGroup.hpp"
 41 #include "gc/shenandoah/shenandoahHeapRegionSet.inline.hpp"
 42 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
 43 #include "gc/shenandoah/shenandoahControlThread.hpp"
 44 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
 45 #include "gc/shenandoah/shenandoahScanRemembered.inline.hpp"
 46 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
 47 #include "gc/shenandoah/shenandoahScanRemembered.inline.hpp"
 48 #include "gc/shenandoah/mode/shenandoahMode.hpp"
 49 #include "oops/compressedOops.inline.hpp"
 50 #include "oops/oop.inline.hpp"
 51 #include "runtime/atomic.hpp"
 52 #include "runtime/javaThread.hpp"
 53 #include "runtime/prefetch.inline.hpp"
 54 #include "utilities/copy.hpp"
 55 #include "utilities/globalDefinitions.hpp"
 56 
 57 inline ShenandoahHeap* ShenandoahHeap::heap() {
 58   return named_heap<ShenandoahHeap>(CollectedHeap::Shenandoah);
 59 }
 60 
 61 inline ShenandoahHeapRegion* ShenandoahRegionIterator::next() {
 62   size_t new_index = Atomic::add(&_index, (size_t) 1, memory_order_relaxed);
 63   // get_region() provides the bounds-check and returns NULL on OOB.
 64   return _heap->get_region(new_index - 1);
 65 }
 66 
 67 inline bool ShenandoahHeap::has_forwarded_objects() const {
 68   return _gc_state.is_set(HAS_FORWARDED);

251     return cancelled_gc();
252   }
253 
254   jbyte prev = _cancelled_gc.cmpxchg(NOT_CANCELLED, CANCELLABLE);
255   if (prev == CANCELLABLE || prev == NOT_CANCELLED) {
256     if (SuspendibleThreadSet::should_yield()) {
257       SuspendibleThreadSet::yield();
258     }
259 
260     // Back to CANCELLABLE. The thread that poked NOT_CANCELLED first gets
261     // to restore to CANCELLABLE.
262     if (prev == CANCELLABLE) {
263       _cancelled_gc.set(CANCELLABLE);
264     }
265     return false;
266   } else {
267     return true;
268   }
269 }
270 
271 inline void ShenandoahHeap::clear_cancelled_gc(bool clear_oom_handler) {
272   _cancelled_gc.set(CANCELLABLE);
273   if (_cancel_requested_time > 0) {
274     double cancel_time = os::elapsedTime() - _cancel_requested_time;
275     log_info(gc)("GC cancellation took %.3fs", cancel_time);
276     _cancel_requested_time = 0;
277   }
278 
279   if (clear_oom_handler) {
280     _oom_evac_handler.clear();
281   }
282 }
283 
284 inline HeapWord* ShenandoahHeap::allocate_from_gclab(Thread* thread, size_t size) {
285   assert(UseTLAB, "TLABs should be enabled");
286 
287   PLAB* gclab = ShenandoahThreadLocalData::gclab(thread);
288   if (gclab == NULL) {
289     assert(!thread->is_Java_thread() && !thread->is_Worker_thread(),
290            "Performance: thread should have GCLAB: %s", thread->name());
291     // No GCLABs in this thread, fallback to shared allocation
292     return NULL;
293   }
294   HeapWord* obj = gclab->allocate(size);
295   if (obj != NULL) {
296     return obj;
297   }

298   return allocate_from_gclab_slow(thread, size);
299 }
300 
301 inline HeapWord* ShenandoahHeap::allocate_from_plab(Thread* thread, size_t size, bool is_promotion) {
302   assert(UseTLAB, "TLABs should be enabled");
303 
304   PLAB* plab = ShenandoahThreadLocalData::plab(thread);
305   HeapWord* obj;
306   if (plab == NULL) {
307     assert(!thread->is_Java_thread() && !thread->is_Worker_thread(), "Performance: thread should have PLAB: %s", thread->name());
308     // No PLABs in this thread, fallback to shared allocation
309     return nullptr;
310   } else if (is_promotion && (plab->words_remaining() > 0) && !ShenandoahThreadLocalData::allow_plab_promotions(thread)) {
311     return nullptr;
312   }
313   // if plab->word_size() <= 0, thread's plab not yet initialized for this pass, so allow_plab_promotions() is not trustworthy
314   obj = plab->allocate(size);
315   if ((obj == nullptr) && (plab->words_remaining() < PLAB::min_size())) {
316     // allocate_from_plab_slow will establish allow_plab_promotions(thread) for future invocations
317     obj = allocate_from_plab_slow(thread, size, is_promotion);
318   }
319   // if plab->words_remaining() >= PLAB::min_size(), just return nullptr so we can use a shared allocation
320   if (obj == nullptr) {
321     return nullptr;
322   }
323 
324   if (is_promotion) {
325     ShenandoahThreadLocalData::add_to_plab_promoted(thread, size * HeapWordSize);
326   } else {
327     ShenandoahThreadLocalData::add_to_plab_evacuated(thread, size * HeapWordSize);
328   }
329   return obj;
330 }
331 
332 inline oop ShenandoahHeap::evacuate_object(oop p, Thread* thread) {
333   assert(thread == Thread::current(), "Expected thread parameter to be current thread.");
334   if (ShenandoahThreadLocalData::is_oom_during_evac(thread)) {
335     // This thread went through the OOM during evac protocol and it is safe to return
336     // the forward pointer. It must not attempt to evacuate any more.
337     return ShenandoahBarrierSet::resolve_forwarded(p);
338   }
339 
340   assert(ShenandoahThreadLocalData::is_evac_allowed(thread), "must be enclosed in oom-evac scope");
341 
342   ShenandoahHeapRegion* r = heap_region_containing(p);
343   assert(!r->is_humongous(), "never evacuate humongous objects");
344 
345   ShenandoahRegionAffiliation target_gen = r->affiliation();
346   if (mode()->is_generational() && ShenandoahHeap::heap()->is_gc_generation_young() &&
347       target_gen == YOUNG_GENERATION && ShenandoahPromoteTenuredObjects) {
348     markWord mark = p->mark();
349     if (mark.is_marked()) {
350       // Already forwarded.
351       return ShenandoahBarrierSet::resolve_forwarded(p);
352     }
353     if (mark.has_displaced_mark_helper()) {
354       // We don't want to deal with MT here just to ensure we read the right mark word.
355       // Skip the potential promotion attempt for this one.
356     } else if (r->age() + mark.age() >= InitialTenuringThreshold) {
357       oop result = try_evacuate_object(p, thread, r, OLD_GENERATION);
358       if (result != NULL) {
359         return result;
360       }
361       // If we failed to promote this aged object, we'll fall through to code below and evacuate to young-gen.
362     }
363   }
364   return try_evacuate_object(p, thread, r, target_gen);
365 }
366 
367 // try_evacuate_object registers the object and dirties the associated remembered set information when evacuating
368 // to OLD_GENERATION.
369 inline oop ShenandoahHeap::try_evacuate_object(oop p, Thread* thread, ShenandoahHeapRegion* from_region,
370                                                ShenandoahRegionAffiliation target_gen) {
371   bool alloc_from_lab = true;
372   bool has_plab = false;
373   HeapWord* copy = NULL;
374   size_t size = p->size();
375   bool is_promotion = (target_gen == OLD_GENERATION) && from_region->is_young();
376 
377 #ifdef ASSERT
378   if (ShenandoahOOMDuringEvacALot &&
379       (os::random() & 1) == 0) { // Simulate OOM every ~2nd slow-path call
380         copy = NULL;
381   } else {
382 #endif
383     if (UseTLAB) {
384       switch (target_gen) {
385         case YOUNG_GENERATION: {
386            copy = allocate_from_gclab(thread, size);
387            if ((copy == nullptr) && (size < ShenandoahThreadLocalData::gclab_size(thread))) {
388              // GCLAB allocation failed because we are bumping up against the limit on young evacuation reserve.  Try resetting
389              // the desired GCLAB size and retry GCLAB allocation to avoid cascading of shared memory allocations.
390              ShenandoahThreadLocalData::set_gclab_size(thread, PLAB::min_size());
391              copy = allocate_from_gclab(thread, size);
392              // If we still get nullptr, we'll try a shared allocation below.
393            }
394            break;
395         }
396         case OLD_GENERATION: {
397            if (ShenandoahUsePLAB) {
398              PLAB* plab = ShenandoahThreadLocalData::plab(thread);
399              if (plab != nullptr) {
400                has_plab = true;
401              }
402              copy = allocate_from_plab(thread, size, is_promotion);
403              if ((copy == nullptr) && (size < ShenandoahThreadLocalData::plab_size(thread)) &&
404                  ShenandoahThreadLocalData::plab_retries_enabled(thread)) {
405                // PLAB allocation failed because we are bumping up against the limit on old evacuation reserve or because
406                // the requested object does not fit within the current plab but the plab still has an "abundance" of memory,
407                // where abundance is defined as >= PLAB::min_size().  In the former case, we try resetting the desired
408                // PLAB size and retry PLAB allocation to avoid cascading of shared memory allocations.
409 
410                // In this situation, PLAB memory is precious.  We'll try to preserve our existing PLAB by forcing
411                // this particular allocation to be shared.
412                if (plab->words_remaining() < PLAB::min_size()) {
413                  ShenandoahThreadLocalData::set_plab_size(thread, PLAB::min_size());
414                  copy = allocate_from_plab(thread, size, is_promotion);
415                  // If we still get nullptr, we'll try a shared allocation below.
416                  if (copy == nullptr) {
417                    // If retry fails, don't continue to retry until we have success (probably in next GC pass)
418                    ShenandoahThreadLocalData::disable_plab_retries(thread);
419                  }
420                }
421                // else, copy still equals nullptr.  this causes shared allocation below, preserving this plab for future needs.
422              }
423            }
424            break;
425         }
426         default: {
427           ShouldNotReachHere();
428           break;
429         }
430       }
431     }
432 
433     if (copy == NULL) {
434       // If we failed to allocate in LAB, we'll try a shared allocation.
435       if (!is_promotion || !has_plab || (size > PLAB::min_size())) {
436         ShenandoahAllocRequest req = ShenandoahAllocRequest::for_shared_gc(size, target_gen);
437         copy = allocate_memory(req, is_promotion);
438         alloc_from_lab = false;
439       }
440       // else, we leave copy equal to NULL, signaling a promotion failure below if appropriate.
441       // We choose not to promote objects smaller than PLAB::min_size() by way of shared allocations, as this is too
442       // costly.  Instead, we'll simply "evacuate" to young-gen memory (using a GCLAB) and will promote in a future
443       // evacuation pass.  This condition is denoted by: is_promotion && has_plab && (size <= PLAB::min_size())
444     }
445 #ifdef ASSERT
446   }
447 #endif
448 
449   if (copy == NULL) {
450     if (target_gen == OLD_GENERATION) {
451       assert(mode()->is_generational(), "Should only be here in generational mode.");
452       if (from_region->is_young()) {
453         // Signal that promotion failed. Will evacuate this old object somewhere in young gen.
454 
455         // We squelch excessive reports to reduce noise in logs.  Squelch enforcement is not "perfect" because
456         // this same code can be in-lined in multiple contexts, and each context will have its own copy of the static
457         // last_report_epoch and this_epoch_report_count variables.
458         const uint MaxReportsPerEpoch = 4;
459         static uint last_report_epoch = 0;
460         static uint epoch_report_count = 0;
461         PLAB* plab = ShenandoahThreadLocalData::plab(thread);
462         size_t words_remaining = (plab == nullptr)? 0: plab->words_remaining();
463         const char* promote_enabled = ShenandoahThreadLocalData::allow_plab_promotions(thread)? "enabled": "disabled";
464         size_t promotion_reserve;
465         size_t promotion_expended;
466         // We can only query GCId::current() if current thread is a named thread.  If current thread is not a
467         // named thread, then we don't even try to squelch the promotion failure report, we don't update the
468         // the last_report_epoch, and we don't increment the epoch_report_count
469         if (thread->is_Named_thread()) {
470           uint gc_id = GCId::current();
471           if ((gc_id != last_report_epoch) || (epoch_report_count++ < MaxReportsPerEpoch)) {
472             {
473               // Promotion failures should be very rare.  Invest in providing useful diagnostic info.
474               ShenandoahHeapLocker locker(lock());
475               promotion_reserve = get_promoted_reserve();
476               promotion_expended = get_promoted_expended();
477             }
478             log_info(gc, ergo)("Promotion failed, size " SIZE_FORMAT ", has plab? %s, PLAB remaining: " SIZE_FORMAT
479                                ", plab promotions %s, promotion reserve: " SIZE_FORMAT ", promotion expended: " SIZE_FORMAT,
480                                size, plab == nullptr? "no": "yes",
481                                words_remaining, promote_enabled, promotion_reserve, promotion_expended);
482             if ((gc_id == last_report_epoch) && (epoch_report_count >= MaxReportsPerEpoch)) {
483               log_info(gc, ergo)("Squelching additional promotion failure reports for epoch %d\n", last_report_epoch);
484             } else if (gc_id != last_report_epoch) {
485               last_report_epoch = gc_id;;
486               epoch_report_count = 1;
487             }
488           }
489         } else if (epoch_report_count < MaxReportsPerEpoch) {
490           // Unnamed threads are much less common than named threads.  In the rare case that an unnamed thread experiences
491           // a promotion failure before a named thread within a given epoch, the report for the unnamed thread will be squelched.
492           {
493             // Promotion failures should be very rare.  Invest in providing useful diagnostic info.
494             ShenandoahHeapLocker locker(lock());
495             promotion_reserve = get_promoted_reserve();
496             promotion_expended = get_promoted_expended();
497           }
498           log_info(gc, ergo)("Promotion failed (unfiltered), size " SIZE_FORMAT ", has plab? %s, PLAB remaining: " SIZE_FORMAT
499                              ", plab promotions %s, promotion reserve: " SIZE_FORMAT ", promotion expended: " SIZE_FORMAT,
500                              size, plab == nullptr? "no": "yes",
501                              words_remaining, promote_enabled, promotion_reserve, promotion_expended);
502         }
503         handle_promotion_failure();
504         return NULL;
505       } else {
506         // Remember that evacuation to old gen failed. We'll want to trigger a full gc to recover from this
507         // after the evacuation threads have finished.
508         handle_old_evacuation_failure();
509       }
510     }
511 
512     control_thread()->handle_alloc_failure_evac(size);
513 
514     _oom_evac_handler.handle_out_of_memory_during_evacuation();
515 
516     return ShenandoahBarrierSet::resolve_forwarded(p);
517   }
518 
519   // Copy the object:
520   Copy::aligned_disjoint_words(cast_from_oop<HeapWord*>(p), copy, size);
521 

522   oop copy_val = cast_to_oop(copy);
523 
524   if (mode()->is_generational() && target_gen == YOUNG_GENERATION && is_aging_cycle()) {
525     ShenandoahHeap::increase_object_age(copy_val, from_region->age() + 1);
526   }
527 
528   // Try to install the new forwarding pointer.
529   ContinuationGCSupport::relativize_stack_chunk(copy_val);
530 
531   oop result = ShenandoahForwarding::try_update_forwardee(p, copy_val);
532   if (result == copy_val) {
533     // Successfully evacuated. Our copy is now the public one!
534     if (mode()->is_generational() && target_gen == OLD_GENERATION) {
535       handle_old_evacuation(copy, size, from_region->is_young());
536     }
537     shenandoah_assert_correct(NULL, copy_val);
538     return copy_val;
539   }  else {
540     // Failed to evacuate. We need to deal with the object that is left behind. Since this
541     // new allocation is certainly after TAMS, it will be considered live in the next cycle.
542     // But if it happens to contain references to evacuated regions, those references would
543     // not get updated for this stale copy during this cycle, and we will crash while scanning
544     // it the next cycle.
545     if (alloc_from_lab) {
546        // For LAB allocations, it is enough to rollback the allocation ptr. Either the next
547        // object will overwrite this stale copy, or the filler object on LAB retirement will
548        // do this.
549        switch (target_gen) {
550          case YOUNG_GENERATION: {
551              ShenandoahThreadLocalData::gclab(thread)->undo_allocation(copy, size);
552             break;
553          }
554          case OLD_GENERATION: {
555             ShenandoahThreadLocalData::plab(thread)->undo_allocation(copy, size);
556             if (is_promotion) {
557               ShenandoahThreadLocalData::subtract_from_plab_promoted(thread, size * HeapWordSize);
558             } else {
559               ShenandoahThreadLocalData::subtract_from_plab_evacuated(thread, size * HeapWordSize);
560             }
561             break;
562          }
563          default: {
564            ShouldNotReachHere();
565            break;
566          }
567        }
568     } else {
569       // For non-LAB allocations, we have no way to retract the allocation, and
570       // have to explicitly overwrite the copy with the filler object. With that overwrite,
571       // we have to keep the fwdptr initialized and pointing to our (stale) copy.
572       fill_with_object(copy, size);
573       shenandoah_assert_correct(NULL, copy_val);
574       // For non-LAB allocations, the object has already been registered
575     }
576     shenandoah_assert_correct(NULL, result);
577     return result;
578   }
579 }
580 
581 void ShenandoahHeap::increase_object_age(oop obj, uint additional_age) {
582   markWord w = obj->has_displaced_mark() ? obj->displaced_mark() : obj->mark();
583   w = w.set_age(MIN2(markWord::max_age, w.age() + additional_age));
584   if (obj->has_displaced_mark()) {
585     obj->set_displaced_mark(w);
586   } else {
587     obj->set_mark(w);
588   }
589 }
590 
591 inline bool ShenandoahHeap::clear_old_evacuation_failure() {
592   return _old_gen_oom_evac.try_unset();
593 }
594 
595 inline bool ShenandoahHeap::is_old(oop obj) const {
596   return is_gc_generation_young() && is_in_old(obj);
597 }
598 
599 inline bool ShenandoahHeap::requires_marking(const void* entry) const {
600   oop obj = cast_to_oop(entry);
601   return !_marking_context->is_marked_strong(obj);
602 }
603 
604 inline bool ShenandoahHeap::in_collection_set(oop p) const {
605   assert(collection_set() != NULL, "Sanity");
606   return collection_set()->is_in(p);
607 }
608 
609 inline bool ShenandoahHeap::in_collection_set_loc(void* p) const {
610   assert(collection_set() != NULL, "Sanity");
611   return collection_set()->is_in_loc(p);
612 }
613 
614 inline bool ShenandoahHeap::is_stable() const {
615   return _gc_state.is_clear();
616 }
617 
618 inline bool ShenandoahHeap::is_idle() const {
619   return _gc_state.is_unset(YOUNG_MARKING | OLD_MARKING | EVACUATION | UPDATEREFS);
620 }
621 
622 inline bool ShenandoahHeap::is_concurrent_mark_in_progress() const {
623   return _gc_state.is_set(YOUNG_MARKING | OLD_MARKING);
624 }
625 
626 inline bool ShenandoahHeap::is_concurrent_young_mark_in_progress() const {
627   return _gc_state.is_set(YOUNG_MARKING);
628 }
629 
630 inline bool ShenandoahHeap::is_concurrent_old_mark_in_progress() const {
631   return _gc_state.is_set(OLD_MARKING);
632 }
633 
634 inline bool ShenandoahHeap::is_evacuation_in_progress() const {
635   return _gc_state.is_set(EVACUATION);
636 }
637 
638 inline bool ShenandoahHeap::is_gc_in_progress_mask(uint mask) const {
639   return _gc_state.is_set(mask);
640 }
641 
642 inline bool ShenandoahHeap::is_degenerated_gc_in_progress() const {
643   return _degenerated_gc_in_progress.is_set();
644 }
645 
646 inline bool ShenandoahHeap::is_full_gc_in_progress() const {
647   return _full_gc_in_progress.is_set();
648 }
649 
650 inline bool ShenandoahHeap::is_full_gc_move_in_progress() const {
651   return _full_gc_move_in_progress.is_set();
652 }
653 
654 inline bool ShenandoahHeap::is_update_refs_in_progress() const {
655   return _gc_state.is_set(UPDATEREFS);
656 }
657 
658 inline bool ShenandoahHeap::is_stw_gc_in_progress() const {
659   return is_full_gc_in_progress() || is_degenerated_gc_in_progress();
660 }
661 
662 inline bool ShenandoahHeap::is_concurrent_strong_root_in_progress() const {
663   return _concurrent_strong_root_in_progress.is_set();
664 }
665 
666 inline bool ShenandoahHeap::is_concurrent_weak_root_in_progress() const {
667   return _gc_state.is_set(WEAK_ROOTS);
668 }
669 
670 inline bool ShenandoahHeap::is_aging_cycle() const {
671   return _is_aging_cycle.is_set();
672 }
673 
674 inline bool ShenandoahHeap::is_prepare_for_old_mark_in_progress() const {
675   return _prepare_for_old_mark;
676 }
677 
678 inline size_t ShenandoahHeap::set_promoted_reserve(size_t new_val) {
679   size_t orig = _promoted_reserve;
680   _promoted_reserve = new_val;
681   return orig;
682 }
683 
684 inline size_t ShenandoahHeap::get_promoted_reserve() const {
685   return _promoted_reserve;
686 }
687 
688 // returns previous value
689 size_t ShenandoahHeap::capture_old_usage(size_t old_usage) {
690   size_t previous_value = _captured_old_usage;
691   _captured_old_usage = old_usage;
692   return previous_value;
693 }
694 
695 void ShenandoahHeap::set_previous_promotion(size_t promoted_bytes) {
696   shenandoah_assert_heaplocked();
697   _previous_promotion = promoted_bytes;
698 }
699 
700 size_t ShenandoahHeap::get_previous_promotion() const {
701   return _previous_promotion;
702 }
703 
704 inline size_t ShenandoahHeap::set_old_evac_reserve(size_t new_val) {
705   size_t orig = _old_evac_reserve;
706   _old_evac_reserve = new_val;
707   return orig;
708 }
709 
710 inline size_t ShenandoahHeap::get_old_evac_reserve() const {
711   return _old_evac_reserve;
712 }
713 
714 inline void ShenandoahHeap::reset_old_evac_expended() {
715   Atomic::store(&_old_evac_expended, (size_t) 0);
716 }
717 
718 inline size_t ShenandoahHeap::expend_old_evac(size_t increment) {
719   return Atomic::add(&_old_evac_expended, increment);
720 }
721 
722 inline size_t ShenandoahHeap::get_old_evac_expended() {
723   return Atomic::load(&_old_evac_expended);
724 }
725 
726 inline void ShenandoahHeap::reset_promoted_expended() {
727   Atomic::store(&_promoted_expended, (size_t) 0);
728 }
729 
730 inline size_t ShenandoahHeap::expend_promoted(size_t increment) {
731   return Atomic::add(&_promoted_expended, increment);
732 }
733 
734 inline size_t ShenandoahHeap::unexpend_promoted(size_t decrement) {
735   return Atomic::sub(&_promoted_expended, decrement);
736 }
737 
738 inline size_t ShenandoahHeap::get_promoted_expended() {
739   return Atomic::load(&_promoted_expended);
740 }
741 
742 inline size_t ShenandoahHeap::set_young_evac_reserve(size_t new_val) {
743   size_t orig = _young_evac_reserve;
744   _young_evac_reserve = new_val;
745   return orig;
746 }
747 
748 inline size_t ShenandoahHeap::get_young_evac_reserve() const {
749   return _young_evac_reserve;
750 }
751 
752 inline intptr_t ShenandoahHeap::set_alloc_supplement_reserve(intptr_t new_val) {
753   intptr_t orig = _alloc_supplement_reserve;
754   _alloc_supplement_reserve = new_val;
755   return orig;
756 }
757 
758 inline intptr_t ShenandoahHeap::get_alloc_supplement_reserve() const {
759   return _alloc_supplement_reserve;
760 }
761 
762 template<class T>
763 inline void ShenandoahHeap::marked_object_iterate(ShenandoahHeapRegion* region, T* cl) {
764   marked_object_iterate(region, cl, region->top());
765 }
766 
767 template<class T>
768 inline void ShenandoahHeap::marked_object_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* limit) {
769   assert(! region->is_humongous_continuation(), "no humongous continuation regions here");
770 
771   ShenandoahMarkingContext* const ctx = marking_context();

772 
773   HeapWord* tams = ctx->top_at_mark_start(region);
774 
775   size_t skip_bitmap_delta = 1;
776   HeapWord* start = region->bottom();
777   HeapWord* end = MIN2(tams, region->end());
778 
779   // Step 1. Scan below the TAMS based on bitmap data.
780   HeapWord* limit_bitmap = MIN2(limit, tams);
781 
782   // Try to scan the initial candidate. If the candidate is above the TAMS, it would
783   // fail the subsequent "< limit_bitmap" checks, and fall through to Step 2.
784   HeapWord* cb = ctx->get_next_marked_addr(start, end);
785 
786   intx dist = ShenandoahMarkScanPrefetch;
787   if (dist > 0) {
788     // Batched scan that prefetches the oop data, anticipating the access to
789     // either header, oop field, or forwarding pointer. Not that we cannot
790     // touch anything in oop, while it still being prefetched to get enough
791     // time for prefetch to work. This is why we try to scan the bitmap linearly,

882     HeapWord* bottom = region->bottom();
883     if (top > bottom) {
884       region = region->humongous_start_region();
885       ShenandoahObjectToOopBoundedClosure<T> objs(cl, bottom, top);
886       marked_object_iterate(region, &objs);
887     }
888   } else {
889     ShenandoahObjectToOopClosure<T> objs(cl);
890     marked_object_iterate(region, &objs, top);
891   }
892 }
893 
894 inline ShenandoahHeapRegion* const ShenandoahHeap::get_region(size_t region_idx) const {
895   if (region_idx < _num_regions) {
896     return _regions[region_idx];
897   } else {
898     return NULL;
899   }
900 }
901 








902 inline ShenandoahMarkingContext* ShenandoahHeap::complete_marking_context() const {
903   assert (_marking_context->is_complete()," sanity");
904   return _marking_context;
905 }
906 
907 inline ShenandoahMarkingContext* ShenandoahHeap::marking_context() const {
908   return _marking_context;
909 }
910 
911 inline void ShenandoahHeap::clear_cards_for(ShenandoahHeapRegion* region) {
912   if (mode()->is_generational()) {
913     _card_scan->mark_range_as_empty(region->bottom(), pointer_delta(region->end(), region->bottom()));
914   }
915 }
916 
917 inline void ShenandoahHeap::dirty_cards(HeapWord* start, HeapWord* end) {
918   assert(mode()->is_generational(), "Should only be used for generational mode");
919   size_t words = pointer_delta(end, start);
920   _card_scan->mark_range_as_dirty(start, words);
921 }
922 
923 inline void ShenandoahHeap::clear_cards(HeapWord* start, HeapWord* end) {
924   assert(mode()->is_generational(), "Should only be used for generational mode");
925   size_t words = pointer_delta(end, start);
926   _card_scan->mark_range_as_clean(start, words);
927 }
928 
929 inline void ShenandoahHeap::mark_card_as_dirty(void* location) {
930   if (mode()->is_generational()) {
931     _card_scan->mark_card_as_dirty((HeapWord*)location);
932   }
933 }
934 
935 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_INLINE_HPP
< prev index next >