1 /*
  2  * Copyright (c) 2015, 2020, Red Hat, Inc. All rights reserved.
  3  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_INLINE_HPP
 27 #define SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_INLINE_HPP
 28 
 29 #include "gc/shenandoah/shenandoahHeap.hpp"
 30 
 31 #include "classfile/javaClasses.inline.hpp"
 32 #include "gc/shared/markBitMap.inline.hpp"
 33 #include "gc/shared/threadLocalAllocBuffer.inline.hpp"
 34 #include "gc/shared/continuationGCSupport.inline.hpp"
 35 #include "gc/shared/suspendibleThreadSet.hpp"
 36 #include "gc/shared/tlab_globals.hpp"
 37 #include "gc/shenandoah/shenandoahAsserts.hpp"
 38 #include "gc/shenandoah/shenandoahBarrierSet.inline.hpp"
 39 #include "gc/shenandoah/shenandoahCollectionSet.inline.hpp"
 40 #include "gc/shenandoah/shenandoahForwarding.inline.hpp"
 41 #include "gc/shenandoah/shenandoahWorkGroup.hpp"
 42 #include "gc/shenandoah/shenandoahHeapRegionSet.inline.hpp"
 43 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
 44 #include "gc/shenandoah/shenandoahControlThread.hpp"
 45 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
 46 #include "gc/shenandoah/shenandoahScanRemembered.inline.hpp"
 47 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
 48 #include "gc/shenandoah/shenandoahScanRemembered.inline.hpp"
 49 #include "gc/shenandoah/mode/shenandoahMode.hpp"
 50 #include "oops/compressedOops.inline.hpp"
 51 #include "oops/oop.inline.hpp"
 52 #include "runtime/atomic.hpp"
 53 #include "runtime/javaThread.hpp"
 54 #include "runtime/prefetch.inline.hpp"
 55 #include "runtime/objectMonitor.inline.hpp"
 56 #include "utilities/copy.hpp"
 57 #include "utilities/globalDefinitions.hpp"
 58 
 59 inline ShenandoahHeap* ShenandoahHeap::heap() {
 60   return named_heap<ShenandoahHeap>(CollectedHeap::Shenandoah);
 61 }
 62 
 63 inline ShenandoahHeapRegion* ShenandoahRegionIterator::next() {
 64   size_t new_index = Atomic::add(&_index, (size_t) 1, memory_order_relaxed);
 65   // get_region() provides the bounds-check and returns null on OOB.
 66   return _heap->get_region(new_index - 1);
 67 }
 68 
 69 inline bool ShenandoahHeap::has_forwarded_objects() const {
 70   return _gc_state.is_set(HAS_FORWARDED);
 71 }
 72 
 73 inline WorkerThreads* ShenandoahHeap::workers() const {
 74   return _workers;
 75 }
 76 
 77 inline WorkerThreads* ShenandoahHeap::safepoint_workers() {
 78   return _safepoint_workers;
 79 }
 80 
 81 inline size_t ShenandoahHeap::heap_region_index_containing(const void* addr) const {
 82   uintptr_t region_start = ((uintptr_t) addr);
 83   uintptr_t index = (region_start - (uintptr_t) base()) >> ShenandoahHeapRegion::region_size_bytes_shift();
 84   assert(index < num_regions(), "Region index is in bounds: " PTR_FORMAT, p2i(addr));
 85   return index;
 86 }
 87 
 88 inline ShenandoahHeapRegion* ShenandoahHeap::heap_region_containing(const void* addr) const {
 89   size_t index = heap_region_index_containing(addr);
 90   ShenandoahHeapRegion* const result = get_region(index);
 91   assert(addr >= result->bottom() && addr < result->end(), "Heap region contains the address: " PTR_FORMAT, p2i(addr));
 92   return result;
 93 }
 94 
 95 inline void ShenandoahHeap::enter_evacuation(Thread* t) {
 96   _oom_evac_handler.enter_evacuation(t);
 97 }
 98 
 99 inline void ShenandoahHeap::leave_evacuation(Thread* t) {
100   _oom_evac_handler.leave_evacuation(t);
101 }
102 
103 template <class T>
104 inline void ShenandoahHeap::update_with_forwarded(T* p) {
105   T o = RawAccess<>::oop_load(p);
106   if (!CompressedOops::is_null(o)) {
107     oop obj = CompressedOops::decode_not_null(o);
108     if (in_collection_set(obj)) {
109       // Corner case: when evacuation fails, there are objects in collection
110       // set that are not really forwarded. We can still go and try and update them
111       // (uselessly) to simplify the common path.
112       shenandoah_assert_forwarded_except(p, obj, cancelled_gc());
113       oop fwd = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
114       shenandoah_assert_not_in_cset_except(p, fwd, cancelled_gc());
115 
116       // Unconditionally store the update: no concurrent updates expected.
117       RawAccess<IS_NOT_NULL>::oop_store(p, fwd);
118     }
119   }
120 }
121 
122 template <class T>
123 inline void ShenandoahHeap::conc_update_with_forwarded(T* p) {
124   T o = RawAccess<>::oop_load(p);
125   if (!CompressedOops::is_null(o)) {
126     oop obj = CompressedOops::decode_not_null(o);
127     if (in_collection_set(obj)) {
128       // Corner case: when evacuation fails, there are objects in collection
129       // set that are not really forwarded. We can still go and try CAS-update them
130       // (uselessly) to simplify the common path.
131       shenandoah_assert_forwarded_except(p, obj, cancelled_gc());
132       oop fwd = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
133       shenandoah_assert_not_in_cset_except(p, fwd, cancelled_gc());
134 
135       // Sanity check: we should not be updating the cset regions themselves,
136       // unless we are recovering from the evacuation failure.
137       shenandoah_assert_not_in_cset_loc_except(p, !is_in(p) || cancelled_gc());
138 
139       // Either we succeed in updating the reference, or something else gets in our way.
140       // We don't care if that is another concurrent GC update, or another mutator update.
141       atomic_update_oop(fwd, p, obj);
142     }
143   }
144 }
145 
146 // Atomic updates of heap location. This is only expected to work with updating the same
147 // logical object with its forwardee. The reason why we need stronger-than-relaxed memory
148 // ordering has to do with coordination with GC barriers and mutator accesses.
149 //
150 // In essence, stronger CAS access is required to maintain the transitive chains that mutator
151 // accesses build by themselves. To illustrate this point, consider the following example.
152 //
153 // Suppose "o" is the object that has a field "x" and the reference to "o" is stored
154 // to field at "addr", which happens to be Java volatile field. Normally, the accesses to volatile
155 // field at "addr" would be matched with release/acquire barriers. This changes when GC moves
156 // the object under mutator feet.
157 //
158 // Thread 1 (Java)
159 //         // --- previous access starts here
160 //         ...
161 //   T1.1: store(&o.x, 1, mo_relaxed)
162 //   T1.2: store(&addr, o, mo_release) // volatile store
163 //
164 //         // --- new access starts here
165 //         // LRB: copy and install the new copy to fwdptr
166 //   T1.3: var copy = copy(o)
167 //   T1.4: cas(&fwd, t, copy, mo_release) // pointer-mediated publication
168 //         <access continues>
169 //
170 // Thread 2 (GC updater)
171 //   T2.1: var f = load(&fwd, mo_{consume|acquire}) // pointer-mediated acquisition
172 //   T2.2: cas(&addr, o, f, mo_release) // this method
173 //
174 // Thread 3 (Java)
175 //   T3.1: var o = load(&addr, mo_acquire) // volatile read
176 //   T3.2: if (o != null)
177 //   T3.3:   var r = load(&o.x, mo_relaxed)
178 //
179 // r is guaranteed to contain "1".
180 //
181 // Without GC involvement, there is synchronizes-with edge from T1.2 to T3.1,
182 // which guarantees this. With GC involvement, when LRB copies the object and
183 // another thread updates the reference to it, we need to have the transitive edge
184 // from T1.4 to T2.1 (that one is guaranteed by forwarding accesses), plus the edge
185 // from T2.2 to T3.1 (which is brought by this CAS).
186 //
187 // Note that we do not need to "acquire" in these methods, because we do not read the
188 // failure witnesses contents on any path, and "release" is enough.
189 //
190 
191 inline void ShenandoahHeap::atomic_update_oop(oop update, oop* addr, oop compare) {
192   assert(is_aligned(addr, HeapWordSize), "Address should be aligned: " PTR_FORMAT, p2i(addr));
193   Atomic::cmpxchg(addr, compare, update, memory_order_release);
194 }
195 
196 inline void ShenandoahHeap::atomic_update_oop(oop update, narrowOop* addr, narrowOop compare) {
197   assert(is_aligned(addr, sizeof(narrowOop)), "Address should be aligned: " PTR_FORMAT, p2i(addr));
198   narrowOop u = CompressedOops::encode(update);
199   Atomic::cmpxchg(addr, compare, u, memory_order_release);
200 }
201 
202 inline void ShenandoahHeap::atomic_update_oop(oop update, narrowOop* addr, oop compare) {
203   assert(is_aligned(addr, sizeof(narrowOop)), "Address should be aligned: " PTR_FORMAT, p2i(addr));
204   narrowOop c = CompressedOops::encode(compare);
205   narrowOop u = CompressedOops::encode(update);
206   Atomic::cmpxchg(addr, c, u, memory_order_release);
207 }
208 
209 inline bool ShenandoahHeap::atomic_update_oop_check(oop update, oop* addr, oop compare) {
210   assert(is_aligned(addr, HeapWordSize), "Address should be aligned: " PTR_FORMAT, p2i(addr));
211   return (oop) Atomic::cmpxchg(addr, compare, update, memory_order_release) == compare;
212 }
213 
214 inline bool ShenandoahHeap::atomic_update_oop_check(oop update, narrowOop* addr, narrowOop compare) {
215   assert(is_aligned(addr, sizeof(narrowOop)), "Address should be aligned: " PTR_FORMAT, p2i(addr));
216   narrowOop u = CompressedOops::encode(update);
217   return (narrowOop) Atomic::cmpxchg(addr, compare, u, memory_order_release) == compare;
218 }
219 
220 inline bool ShenandoahHeap::atomic_update_oop_check(oop update, narrowOop* addr, oop compare) {
221   assert(is_aligned(addr, sizeof(narrowOop)), "Address should be aligned: " PTR_FORMAT, p2i(addr));
222   narrowOop c = CompressedOops::encode(compare);
223   narrowOop u = CompressedOops::encode(update);
224   return CompressedOops::decode(Atomic::cmpxchg(addr, c, u, memory_order_release)) == compare;
225 }
226 
227 // The memory ordering discussion above does not apply for methods that store nulls:
228 // then, there is no transitive reads in mutator (as we see nulls), and we can do
229 // relaxed memory ordering there.
230 
231 inline void ShenandoahHeap::atomic_clear_oop(oop* addr, oop compare) {
232   assert(is_aligned(addr, HeapWordSize), "Address should be aligned: " PTR_FORMAT, p2i(addr));
233   Atomic::cmpxchg(addr, compare, oop(), memory_order_relaxed);
234 }
235 
236 inline void ShenandoahHeap::atomic_clear_oop(narrowOop* addr, oop compare) {
237   assert(is_aligned(addr, sizeof(narrowOop)), "Address should be aligned: " PTR_FORMAT, p2i(addr));
238   narrowOop cmp = CompressedOops::encode(compare);
239   Atomic::cmpxchg(addr, cmp, narrowOop(), memory_order_relaxed);
240 }
241 
242 inline void ShenandoahHeap::atomic_clear_oop(narrowOop* addr, narrowOop compare) {
243   assert(is_aligned(addr, sizeof(narrowOop)), "Address should be aligned: " PTR_FORMAT, p2i(addr));
244   Atomic::cmpxchg(addr, compare, narrowOop(), memory_order_relaxed);
245 }
246 
247 inline bool ShenandoahHeap::cancelled_gc() const {
248   return _cancelled_gc.get() == CANCELLED;
249 }
250 
251 inline bool ShenandoahHeap::check_cancelled_gc_and_yield(bool sts_active) {
252   if (sts_active && !cancelled_gc()) {
253     if (SuspendibleThreadSet::should_yield()) {
254       SuspendibleThreadSet::yield();
255     }
256   }
257   return cancelled_gc();
258 }
259 
260 inline void ShenandoahHeap::clear_cancelled_gc(bool clear_oom_handler) {
261   _cancelled_gc.set(CANCELLABLE);
262   if (_cancel_requested_time > 0) {
263     double cancel_time = os::elapsedTime() - _cancel_requested_time;
264     log_info(gc)("GC cancellation took %.3fs", cancel_time);
265     _cancel_requested_time = 0;
266   }
267 
268   if (clear_oom_handler) {
269     _oom_evac_handler.clear();
270   }
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 == nullptr) {
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 nullptr;
282   }
283   HeapWord* obj = gclab->allocate(size);
284   if (obj != nullptr) {
285     return obj;
286   }
287   return allocate_from_gclab_slow(thread, size);
288 }
289 
290 inline HeapWord* ShenandoahHeap::allocate_from_plab(Thread* thread, size_t size, bool is_promotion) {
291   assert(UseTLAB, "TLABs should be enabled");
292 
293   PLAB* plab = ShenandoahThreadLocalData::plab(thread);
294   HeapWord* obj;
295 
296   if (plab == nullptr) {
297     assert(!thread->is_Java_thread() && !thread->is_Worker_thread(), "Performance: thread should have PLAB: %s", thread->name());
298     // No PLABs in this thread, fallback to shared allocation
299     return nullptr;
300   } else if (is_promotion && !ShenandoahThreadLocalData::allow_plab_promotions(thread)) {
301     return nullptr;
302   }
303   // if plab->word_size() <= 0, thread's plab not yet initialized for this pass, so allow_plab_promotions() is not trustworthy
304   obj = plab->allocate(size);
305   if ((obj == nullptr) && (plab->words_remaining() < PLAB::min_size())) {
306     // allocate_from_plab_slow will establish allow_plab_promotions(thread) for future invocations
307     obj = allocate_from_plab_slow(thread, size, is_promotion);
308   }
309   // if plab->words_remaining() >= PLAB::min_size(), just return nullptr so we can use a shared allocation
310   if (obj == nullptr) {
311     return nullptr;
312   }
313 
314   if (is_promotion) {
315     ShenandoahThreadLocalData::add_to_plab_promoted(thread, size * HeapWordSize);
316   } else {
317     ShenandoahThreadLocalData::add_to_plab_evacuated(thread, size * HeapWordSize);
318   }
319   return obj;
320 }
321 
322 inline ShenandoahAgeCensus* ShenandoahHeap::age_census() const {
323   assert(mode()->is_generational(), "Only in generational mode");
324   assert(_age_census != nullptr, "Error: not initialized");
325   return _age_census;
326 }
327 
328 inline oop ShenandoahHeap::evacuate_object(oop p, Thread* thread) {
329   assert(thread == Thread::current(), "Expected thread parameter to be current thread.");
330   if (ShenandoahThreadLocalData::is_oom_during_evac(thread)) {
331     // This thread went through the OOM during evac protocol and it is safe to return
332     // the forward pointer. It must not attempt to evacuate any more.
333     return ShenandoahBarrierSet::resolve_forwarded(p);
334   }
335 
336   assert(ShenandoahThreadLocalData::is_evac_allowed(thread), "must be enclosed in oom-evac scope");
337 
338   ShenandoahHeapRegion* r = heap_region_containing(p);
339   assert(!r->is_humongous(), "never evacuate humongous objects");
340 
341   ShenandoahAffiliation target_gen = r->affiliation();
342   if (mode()->is_generational() && ShenandoahHeap::heap()->is_gc_generation_young() &&
343       target_gen == YOUNG_GENERATION) {
344     markWord mark = p->mark();
345     if (mark.is_marked()) {
346       // Already forwarded.
347       return ShenandoahBarrierSet::resolve_forwarded(p);
348     }
349     if (mark.has_displaced_mark_helper()) {
350       // We don't want to deal with MT here just to ensure we read the right mark word.
351       // Skip the potential promotion attempt for this one.
352     } else if (r->age() + mark.age() >= age_census()->tenuring_threshold()) {
353       oop result = try_evacuate_object(p, thread, r, OLD_GENERATION);
354       if (result != nullptr) {
355         return result;
356       }
357       // If we failed to promote this aged object, we'll fall through to code below and evacuate to young-gen.
358     }
359   }
360   return try_evacuate_object(p, thread, r, target_gen);
361 }
362 
363 // try_evacuate_object registers the object and dirties the associated remembered set information when evacuating
364 // to OLD_GENERATION.
365 inline oop ShenandoahHeap::try_evacuate_object(oop p, Thread* thread, ShenandoahHeapRegion* from_region,
366                                                ShenandoahAffiliation target_gen) {
367   bool alloc_from_lab = true;
368   bool has_plab = false;
369   HeapWord* copy = nullptr;
370   size_t size = p->size();
371   bool is_promotion = (target_gen == OLD_GENERATION) && from_region->is_young();
372 
373 #ifdef ASSERT
374   if (ShenandoahOOMDuringEvacALot &&
375       (os::random() & 1) == 0) { // Simulate OOM every ~2nd slow-path call
376         copy = nullptr;
377   } else {
378 #endif
379     if (UseTLAB) {
380       switch (target_gen) {
381         case YOUNG_GENERATION: {
382            copy = allocate_from_gclab(thread, size);
383            if ((copy == nullptr) && (size < ShenandoahThreadLocalData::gclab_size(thread))) {
384              // GCLAB allocation failed because we are bumping up against the limit on young evacuation reserve.  Try resetting
385              // the desired GCLAB size and retry GCLAB allocation to avoid cascading of shared memory allocations.
386              ShenandoahThreadLocalData::set_gclab_size(thread, PLAB::min_size());
387              copy = allocate_from_gclab(thread, size);
388              // If we still get nullptr, we'll try a shared allocation below.
389            }
390            break;
391         }
392         case OLD_GENERATION: {
393            PLAB* plab = ShenandoahThreadLocalData::plab(thread);
394            if (plab != nullptr) {
395              has_plab = true;
396            }
397            copy = allocate_from_plab(thread, size, is_promotion);
398            if ((copy == nullptr) && (size < ShenandoahThreadLocalData::plab_size(thread)) &&
399                ShenandoahThreadLocalData::plab_retries_enabled(thread)) {
400              // PLAB allocation failed because we are bumping up against the limit on old evacuation reserve or because
401              // the requested object does not fit within the current plab but the plab still has an "abundance" of memory,
402              // where abundance is defined as >= PLAB::min_size().  In the former case, we try resetting the desired
403              // PLAB size and retry PLAB allocation to avoid cascading of shared memory allocations.
404 
405              // In this situation, PLAB memory is precious.  We'll try to preserve our existing PLAB by forcing
406              // this particular allocation to be shared.
407              if (plab->words_remaining() < PLAB::min_size()) {
408                ShenandoahThreadLocalData::set_plab_size(thread, PLAB::min_size());
409                copy = allocate_from_plab(thread, size, is_promotion);
410                // If we still get nullptr, we'll try a shared allocation below.
411                if (copy == nullptr) {
412                  // If retry fails, don't continue to retry until we have success (probably in next GC pass)
413                  ShenandoahThreadLocalData::disable_plab_retries(thread);
414                }
415              }
416              // else, copy still equals nullptr.  this causes shared allocation below, preserving this plab for future needs.
417            }
418            break;
419         }
420         default: {
421           ShouldNotReachHere();
422           break;
423         }
424       }
425     }
426 
427     if (copy == nullptr) {
428       // If we failed to allocate in LAB, we'll try a shared allocation.
429       if (!is_promotion || !has_plab || (size > PLAB::min_size())) {
430         ShenandoahAllocRequest req = ShenandoahAllocRequest::for_shared_gc(size, target_gen);
431         copy = allocate_memory(req, is_promotion);
432         alloc_from_lab = false;
433       }
434       // else, we leave copy equal to nullptr, signaling a promotion failure below if appropriate.
435       // We choose not to promote objects smaller than PLAB::min_size() by way of shared allocations, as this is too
436       // costly.  Instead, we'll simply "evacuate" to young-gen memory (using a GCLAB) and will promote in a future
437       // evacuation pass.  This condition is denoted by: is_promotion && has_plab && (size <= PLAB::min_size())
438     }
439 #ifdef ASSERT
440   }
441 #endif
442 
443   if (copy == nullptr) {
444     if (target_gen == OLD_GENERATION) {
445       assert(mode()->is_generational(), "Should only be here in generational mode.");
446       if (from_region->is_young()) {
447         // Signal that promotion failed. Will evacuate this old object somewhere in young gen.
448         report_promotion_failure(thread, size);
449         return nullptr;
450       } else {
451         // Remember that evacuation to old gen failed. We'll want to trigger a full gc to recover from this
452         // after the evacuation threads have finished.
453         handle_old_evacuation_failure();
454       }
455     }
456 
457     control_thread()->handle_alloc_failure_evac(size);
458 
459     _oom_evac_handler.handle_out_of_memory_during_evacuation();
460 
461     return ShenandoahBarrierSet::resolve_forwarded(p);
462   }
463 
464   // Copy the object:
465   _evac_tracker->begin_evacuation(thread, size * HeapWordSize);
466   Copy::aligned_disjoint_words(cast_from_oop<HeapWord*>(p), copy, size);
467 
468   oop copy_val = cast_to_oop(copy);
469 
470   if (mode()->is_generational() && target_gen == YOUNG_GENERATION && is_aging_cycle()) {
471     ShenandoahHeap::increase_object_age(copy_val, from_region->age() + 1);
472   }
473 
474   // Try to install the new forwarding pointer.
475   ContinuationGCSupport::relativize_stack_chunk(copy_val);
476 
477   oop result = ShenandoahForwarding::try_update_forwardee(p, copy_val);
478   if (result == copy_val) {
479     // Successfully evacuated. Our copy is now the public one!
480     _evac_tracker->end_evacuation(thread, size * HeapWordSize);
481     if (mode()->is_generational()) {
482       if (target_gen == OLD_GENERATION) {
483         handle_old_evacuation(copy, size, from_region->is_young());
484       } else {
485         // When copying to the old generation above, we don't care
486         // about recording object age in the census stats.
487         assert(target_gen == YOUNG_GENERATION, "Error");
488         // We record this census only when simulating pre-adaptive tenuring behavior, or
489         // when we have been asked to record the census at evacuation rather than at mark
490         if (ShenandoahGenerationalCensusAtEvac || !ShenandoahGenerationalAdaptiveTenuring) {
491           _evac_tracker->record_age(thread, size * HeapWordSize, ShenandoahHeap::get_object_age(copy_val));
492         }
493       }
494     }
495     shenandoah_assert_correct(nullptr, copy_val);
496     return copy_val;
497   }  else {
498     // Failed to evacuate. We need to deal with the object that is left behind. Since this
499     // new allocation is certainly after TAMS, it will be considered live in the next cycle.
500     // But if it happens to contain references to evacuated regions, those references would
501     // not get updated for this stale copy during this cycle, and we will crash while scanning
502     // it the next cycle.
503     if (alloc_from_lab) {
504        // For LAB allocations, it is enough to rollback the allocation ptr. Either the next
505        // object will overwrite this stale copy, or the filler object on LAB retirement will
506        // do this.
507        switch (target_gen) {
508          case YOUNG_GENERATION: {
509              ShenandoahThreadLocalData::gclab(thread)->undo_allocation(copy, size);
510             break;
511          }
512          case OLD_GENERATION: {
513             ShenandoahThreadLocalData::plab(thread)->undo_allocation(copy, size);
514             if (is_promotion) {
515               ShenandoahThreadLocalData::subtract_from_plab_promoted(thread, size * HeapWordSize);
516             } else {
517               ShenandoahThreadLocalData::subtract_from_plab_evacuated(thread, size * HeapWordSize);
518             }
519             break;
520          }
521          default: {
522            ShouldNotReachHere();
523            break;
524          }
525        }
526     } else {
527       // For non-LAB allocations, we have no way to retract the allocation, and
528       // have to explicitly overwrite the copy with the filler object. With that overwrite,
529       // we have to keep the fwdptr initialized and pointing to our (stale) copy.
530       assert(size >= ShenandoahHeap::min_fill_size(), "previously allocated object known to be larger than min_size");
531       fill_with_object(copy, size);
532       shenandoah_assert_correct(nullptr, copy_val);
533       // For non-LAB allocations, the object has already been registered
534     }
535     shenandoah_assert_correct(nullptr, result);
536     return result;
537   }
538 }
539 
540 void ShenandoahHeap::increase_object_age(oop obj, uint additional_age) {
541   // This operates on new copy of an object. This means that the object's mark-word
542   // is thread-local and therefore safe to access. However, when the mark is
543   // displaced (i.e. stack-locked or monitor-locked), then it must be considered
544   // a shared memory location. It can be accessed by other threads.
545   // In particular, a competing evacuating thread can succeed to install its copy
546   // as the forwardee and continue to unlock the object, at which point 'our'
547   // write to the foreign stack-location would potentially over-write random
548   // information on that stack. Writing to a monitor is less problematic,
549   // but still not safe: while the ObjectMonitor would not randomly disappear,
550   // the other thread would also write to the same displaced header location,
551   // possibly leading to increase the age twice.
552   // For all these reasons, we take the conservative approach and not attempt
553   // to increase the age when the header is displaced.
554   markWord w = obj->mark();
555   // The mark-word has been copied from the original object. It can not be
556   // inflating, because inflation can not be interrupted by a safepoint,
557   // and after a safepoint, a Java thread would first have to successfully
558   // evacuate the object before it could inflate the monitor.
559   assert(!w.is_being_inflated() || LockingMode == LM_LIGHTWEIGHT, "must not inflate monitor before evacuation of object succeeds");
560   // It is possible that we have copied the object after another thread has
561   // already successfully completed evacuation. While harmless (we would never
562   // publish our copy), don't even attempt to modify the age when that
563   // happens.
564   if (!w.has_displaced_mark_helper() && !w.is_marked()) {
565     w = w.set_age(MIN2(markWord::max_age, w.age() + additional_age));
566     obj->set_mark(w);
567   }
568 }
569 
570 // Return the object's age, or a sentinel value when the age can't
571 // necessarily be determined because of concurrent locking by the
572 // mutator
573 uint ShenandoahHeap::get_object_age(oop obj) {
574   // This is impossible to do unless we "freeze" ABA-type oscillations
575   // With Lilliput, we can do this more easily.
576   markWord w = obj->mark();
577   assert(!w.is_marked(), "must not be forwarded");
578   if (w.has_monitor()) {
579     w = w.monitor()->header();
580   } else if (w.is_being_inflated() || w.has_displaced_mark_helper()) {
581     // Informs caller that we aren't able to determine the age
582     return markWord::max_age + 1; // sentinel
583   }
584   assert(w.age() <= markWord::max_age, "Impossible!");
585   return w.age();
586 }
587 
588 inline bool ShenandoahHeap::clear_old_evacuation_failure() {
589   return _old_gen_oom_evac.try_unset();
590 }
591 
592 bool ShenandoahHeap::is_in(const void* p) const {
593   HeapWord* heap_base = (HeapWord*) base();
594   HeapWord* last_region_end = heap_base + ShenandoahHeapRegion::region_size_words() * num_regions();
595   return p >= heap_base && p < last_region_end;
596 }
597 
598 inline bool ShenandoahHeap::is_in_active_generation(oop obj) const {
599   if (!mode()->is_generational()) {
600     // everything is the same single generation
601     return true;
602   }
603 
604   if (active_generation() == nullptr) {
605     // no collection is happening, only expect this to be called
606     // when concurrent processing is active, but that could change
607     return false;
608   }
609 
610   assert(is_in(obj), "only check if is in active generation for objects (" PTR_FORMAT ") in heap", p2i(obj));
611   assert((active_generation() == (ShenandoahGeneration*) old_generation()) ||
612          (active_generation() == (ShenandoahGeneration*) young_generation()) ||
613          (active_generation() == global_generation()), "Active generation must be old, young, or global");
614 
615   size_t index = heap_region_containing(obj)->index();
616   switch (_affiliations[index]) {
617   case ShenandoahAffiliation::FREE:
618     // Free regions are in Old, Young, Global
619     return true;
620   case ShenandoahAffiliation::YOUNG_GENERATION:
621     // Young regions are in young_generation and global_generation, not in old_generation
622     return (active_generation() != (ShenandoahGeneration*) old_generation());
623   case ShenandoahAffiliation::OLD_GENERATION:
624     // Old regions are in old_generation and global_generation, not in young_generation
625     return (active_generation() != (ShenandoahGeneration*) young_generation());
626   default:
627     assert(false, "Bad affiliation (%d) for region " SIZE_FORMAT, _affiliations[index], index);
628     return false;
629   }
630 }
631 
632 inline bool ShenandoahHeap::is_in_young(const void* p) const {
633   return is_in(p) && (_affiliations[heap_region_index_containing(p)] == ShenandoahAffiliation::YOUNG_GENERATION);
634 }
635 
636 inline bool ShenandoahHeap::is_in_old(const void* p) const {
637   return is_in(p) && (_affiliations[heap_region_index_containing(p)] == ShenandoahAffiliation::OLD_GENERATION);
638 }
639 
640 inline bool ShenandoahHeap::is_old(oop obj) const {
641   return is_gc_generation_young() && is_in_old(obj);
642 }
643 
644 inline ShenandoahAffiliation ShenandoahHeap::region_affiliation(const ShenandoahHeapRegion *r) {
645   return (ShenandoahAffiliation) _affiliations[r->index()];
646 }
647 
648 inline void ShenandoahHeap::assert_lock_for_affiliation(ShenandoahAffiliation orig_affiliation,
649                                                         ShenandoahAffiliation new_affiliation) {
650   // A lock is required when changing from FREE to NON-FREE.  Though it may be possible to elide the lock when
651   // transitioning from in-use to FREE, the current implementation uses a lock for this transition.  A lock is
652   // not required to change from YOUNG to OLD (i.e. when promoting humongous region).
653   //
654   //         new_affiliation is:     FREE   YOUNG   OLD
655   //  orig_affiliation is:  FREE      X       L      L
656   //                       YOUNG      L       X
657   //                         OLD      L       X      X
658   //  X means state transition won't happen (so don't care)
659   //  L means lock should be held
660   //  Blank means no lock required because affiliation visibility will not be required until subsequent safepoint
661   //
662   // Note: during full GC, all transitions between states are possible.  During Full GC, we should be in a safepoint.
663 
664   if ((orig_affiliation == ShenandoahAffiliation::FREE) || (new_affiliation == ShenandoahAffiliation::FREE)) {
665     shenandoah_assert_heaplocked_or_fullgc_safepoint();
666   }
667 }
668 
669 inline void ShenandoahHeap::set_affiliation(ShenandoahHeapRegion* r, ShenandoahAffiliation new_affiliation) {
670 #ifdef ASSERT
671   assert_lock_for_affiliation(region_affiliation(r), new_affiliation);
672 #endif
673   _affiliations[r->index()] = (uint8_t) new_affiliation;
674 }
675 
676 inline ShenandoahAffiliation ShenandoahHeap::region_affiliation(size_t index) {
677   return (ShenandoahAffiliation) _affiliations[index];
678 }
679 
680 inline bool ShenandoahHeap::requires_marking(const void* entry) const {
681   oop obj = cast_to_oop(entry);
682   return !_marking_context->is_marked_strong(obj);
683 }
684 
685 inline bool ShenandoahHeap::in_collection_set(oop p) const {
686   assert(collection_set() != nullptr, "Sanity");
687   return collection_set()->is_in(p);
688 }
689 
690 inline bool ShenandoahHeap::in_collection_set_loc(void* p) const {
691   assert(collection_set() != nullptr, "Sanity");
692   return collection_set()->is_in_loc(p);
693 }
694 
695 
696 inline bool ShenandoahHeap::is_stable() const {
697   return _gc_state.is_clear();
698 }
699 
700 inline bool ShenandoahHeap::has_evacuation_reserve_quantities() const {
701   return _has_evacuation_reserve_quantities;
702 }
703 
704 inline bool ShenandoahHeap::is_idle() const {
705   return _gc_state.is_unset(MARKING | EVACUATION | UPDATEREFS);
706 }
707 
708 inline bool ShenandoahHeap::is_concurrent_mark_in_progress() const {
709   return _gc_state.is_set(MARKING);
710 }
711 
712 inline bool ShenandoahHeap::is_concurrent_young_mark_in_progress() const {
713   return _gc_state.is_set(YOUNG_MARKING);
714 }
715 
716 inline bool ShenandoahHeap::is_concurrent_old_mark_in_progress() const {
717   return _gc_state.is_set(OLD_MARKING);
718 }
719 
720 inline bool ShenandoahHeap::is_evacuation_in_progress() const {
721   return _gc_state.is_set(EVACUATION);
722 }
723 
724 inline bool ShenandoahHeap::is_degenerated_gc_in_progress() const {
725   return _degenerated_gc_in_progress.is_set();
726 }
727 
728 inline bool ShenandoahHeap::is_full_gc_in_progress() const {
729   return _full_gc_in_progress.is_set();
730 }
731 
732 inline bool ShenandoahHeap::is_full_gc_move_in_progress() const {
733   return _full_gc_move_in_progress.is_set();
734 }
735 
736 inline bool ShenandoahHeap::is_update_refs_in_progress() const {
737   return _gc_state.is_set(UPDATEREFS);
738 }
739 
740 inline bool ShenandoahHeap::is_stw_gc_in_progress() const {
741   return is_full_gc_in_progress() || is_degenerated_gc_in_progress();
742 }
743 
744 inline bool ShenandoahHeap::is_concurrent_strong_root_in_progress() const {
745   return _concurrent_strong_root_in_progress.is_set();
746 }
747 
748 inline bool ShenandoahHeap::is_concurrent_weak_root_in_progress() const {
749   return _gc_state.is_set(WEAK_ROOTS);
750 }
751 
752 inline bool ShenandoahHeap::is_aging_cycle() const {
753   return _is_aging_cycle.is_set();
754 }
755 
756 inline size_t ShenandoahHeap::set_promoted_reserve(size_t new_val) {
757   size_t orig = _promoted_reserve;
758   _promoted_reserve = new_val;
759   return orig;
760 }
761 
762 inline size_t ShenandoahHeap::get_promoted_reserve() const {
763   return _promoted_reserve;
764 }
765 
766 inline size_t ShenandoahHeap::set_old_evac_reserve(size_t new_val) {
767   size_t orig = _old_evac_reserve;
768   _old_evac_reserve = new_val;
769   return orig;
770 }
771 
772 inline size_t ShenandoahHeap::get_old_evac_reserve() const {
773   return _old_evac_reserve;
774 }
775 
776 inline void ShenandoahHeap::augment_old_evac_reserve(size_t increment) {
777   _old_evac_reserve += increment;
778 }
779 
780 inline void ShenandoahHeap::augment_promo_reserve(size_t increment) {
781   _promoted_reserve += increment;
782 }
783 
784 inline void ShenandoahHeap::reset_promoted_expended() {
785   Atomic::store(&_promoted_expended, (size_t) 0);
786 }
787 
788 inline size_t ShenandoahHeap::expend_promoted(size_t increment) {
789   return Atomic::add(&_promoted_expended, increment);
790 }
791 
792 inline size_t ShenandoahHeap::unexpend_promoted(size_t decrement) {
793   return Atomic::sub(&_promoted_expended, decrement);
794 }
795 
796 inline size_t ShenandoahHeap::get_promoted_expended() {
797   return Atomic::load(&_promoted_expended);
798 }
799 
800 inline size_t ShenandoahHeap::set_young_evac_reserve(size_t new_val) {
801   size_t orig = _young_evac_reserve;
802   _young_evac_reserve = new_val;
803   return orig;
804 }
805 
806 inline size_t ShenandoahHeap::get_young_evac_reserve() const {
807   return _young_evac_reserve;
808 }
809 
810 template<class T>
811 inline void ShenandoahHeap::marked_object_iterate(ShenandoahHeapRegion* region, T* cl) {
812   marked_object_iterate(region, cl, region->top());
813 }
814 
815 template<class T>
816 inline void ShenandoahHeap::marked_object_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* limit) {
817   assert(! region->is_humongous_continuation(), "no humongous continuation regions here");
818 
819   ShenandoahMarkingContext* const ctx = marking_context();
820 
821   HeapWord* tams = ctx->top_at_mark_start(region);
822 
823   size_t skip_bitmap_delta = 1;
824   HeapWord* start = region->bottom();
825   HeapWord* end = MIN2(tams, region->end());
826 
827   // Step 1. Scan below the TAMS based on bitmap data.
828   HeapWord* limit_bitmap = MIN2(limit, tams);
829 
830   // Try to scan the initial candidate. If the candidate is above the TAMS, it would
831   // fail the subsequent "< limit_bitmap" checks, and fall through to Step 2.
832   HeapWord* cb = ctx->get_next_marked_addr(start, end);
833 
834   intx dist = ShenandoahMarkScanPrefetch;
835   if (dist > 0) {
836     // Batched scan that prefetches the oop data, anticipating the access to
837     // either header, oop field, or forwarding pointer. Not that we cannot
838     // touch anything in oop, while it still being prefetched to get enough
839     // time for prefetch to work. This is why we try to scan the bitmap linearly,
840     // disregarding the object size. However, since we know forwarding pointer
841     // precedes the object, we can skip over it. Once we cannot trust the bitmap,
842     // there is no point for prefetching the oop contents, as oop->size() will
843     // touch it prematurely.
844 
845     // No variable-length arrays in standard C++, have enough slots to fit
846     // the prefetch distance.
847     static const int SLOT_COUNT = 256;
848     guarantee(dist <= SLOT_COUNT, "adjust slot count");
849     HeapWord* slots[SLOT_COUNT];
850 
851     int avail;
852     do {
853       avail = 0;
854       for (int c = 0; (c < dist) && (cb < limit_bitmap); c++) {
855         Prefetch::read(cb, oopDesc::mark_offset_in_bytes());
856         slots[avail++] = cb;
857         cb += skip_bitmap_delta;
858         if (cb < limit_bitmap) {
859           cb = ctx->get_next_marked_addr(cb, limit_bitmap);
860         }
861       }
862 
863       for (int c = 0; c < avail; c++) {
864         assert (slots[c] < tams,  "only objects below TAMS here: "  PTR_FORMAT " (" PTR_FORMAT ")", p2i(slots[c]), p2i(tams));
865         assert (slots[c] < limit, "only objects below limit here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(slots[c]), p2i(limit));
866         oop obj = cast_to_oop(slots[c]);
867         assert(oopDesc::is_oop(obj), "sanity");
868         assert(ctx->is_marked(obj), "object expected to be marked");
869         cl->do_object(obj);
870       }
871     } while (avail > 0);
872   } else {
873     while (cb < limit_bitmap) {
874       assert (cb < tams,  "only objects below TAMS here: "  PTR_FORMAT " (" PTR_FORMAT ")", p2i(cb), p2i(tams));
875       assert (cb < limit, "only objects below limit here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(cb), p2i(limit));
876       oop obj = cast_to_oop(cb);
877       assert(oopDesc::is_oop(obj), "sanity");
878       assert(ctx->is_marked(obj), "object expected to be marked");
879       cl->do_object(obj);
880       cb += skip_bitmap_delta;
881       if (cb < limit_bitmap) {
882         cb = ctx->get_next_marked_addr(cb, limit_bitmap);
883       }
884     }
885   }
886 
887   // Step 2. Accurate size-based traversal, happens past the TAMS.
888   // This restarts the scan at TAMS, which makes sure we traverse all objects,
889   // regardless of what happened at Step 1.
890   HeapWord* cs = tams;
891   while (cs < limit) {
892     assert (cs >= tams, "only objects past TAMS here: "   PTR_FORMAT " (" PTR_FORMAT ")", p2i(cs), p2i(tams));
893     assert (cs < limit, "only objects below limit here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(cs), p2i(limit));
894     oop obj = cast_to_oop(cs);
895     assert(oopDesc::is_oop(obj), "sanity");
896     assert(ctx->is_marked(obj), "object expected to be marked");
897     size_t size = obj->size();
898     cl->do_object(obj);
899     cs += size;
900   }
901 }
902 
903 template <class T>
904 class ShenandoahObjectToOopClosure : public ObjectClosure {
905   T* _cl;
906 public:
907   ShenandoahObjectToOopClosure(T* cl) : _cl(cl) {}
908 
909   void do_object(oop obj) {
910     obj->oop_iterate(_cl);
911   }
912 };
913 
914 template <class T>
915 class ShenandoahObjectToOopBoundedClosure : public ObjectClosure {
916   T* _cl;
917   MemRegion _bounds;
918 public:
919   ShenandoahObjectToOopBoundedClosure(T* cl, HeapWord* bottom, HeapWord* top) :
920     _cl(cl), _bounds(bottom, top) {}
921 
922   void do_object(oop obj) {
923     obj->oop_iterate(_cl, _bounds);
924   }
925 };
926 
927 template<class T>
928 inline void ShenandoahHeap::marked_object_oop_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* top) {
929   if (region->is_humongous()) {
930     HeapWord* bottom = region->bottom();
931     if (top > bottom) {
932       region = region->humongous_start_region();
933       ShenandoahObjectToOopBoundedClosure<T> objs(cl, bottom, top);
934       marked_object_iterate(region, &objs);
935     }
936   } else {
937     ShenandoahObjectToOopClosure<T> objs(cl);
938     marked_object_iterate(region, &objs, top);
939   }
940 }
941 
942 inline ShenandoahHeapRegion* ShenandoahHeap::get_region(size_t region_idx) const {
943   if (region_idx < _num_regions) {
944     return _regions[region_idx];
945   } else {
946     return nullptr;
947   }
948 }
949 
950 inline ShenandoahMarkingContext* ShenandoahHeap::complete_marking_context() const {
951   assert (_marking_context->is_complete()," sanity");
952   return _marking_context;
953 }
954 
955 inline ShenandoahMarkingContext* ShenandoahHeap::marking_context() const {
956   return _marking_context;
957 }
958 
959 inline void ShenandoahHeap::clear_cards_for(ShenandoahHeapRegion* region) {
960   if (mode()->is_generational()) {
961     _card_scan->mark_range_as_empty(region->bottom(), pointer_delta(region->end(), region->bottom()));
962   }
963 }
964 
965 inline void ShenandoahHeap::mark_card_as_dirty(void* location) {
966   if (mode()->is_generational()) {
967     _card_scan->mark_card_as_dirty((HeapWord*)location);
968   }
969 }
970 
971 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_INLINE_HPP