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/shenandoahGeneration.hpp"
 45 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
 46 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
 47 #include "gc/shenandoah/mode/shenandoahMode.hpp"
 48 #include "oops/compressedOops.inline.hpp"
 49 #include "oops/oop.inline.hpp"
 50 #include "runtime/atomic.hpp"
 51 #include "runtime/javaThread.hpp"
 52 #include "runtime/prefetch.inline.hpp"
 53 #include "runtime/objectMonitor.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);
 69 }
 70 
 71 inline WorkerThreads* ShenandoahHeap::workers() const {
 72   return _workers;
 73 }
 74 
 75 inline WorkerThreads* ShenandoahHeap::safepoint_workers() {
 76   return _safepoint_workers;
 77 }
 78 
 79 inline void ShenandoahHeap::notify_gc_progress() {
 80   Atomic::store(&_gc_no_progress_count, (size_t) 0);
 81 
 82 }
 83 inline void ShenandoahHeap::notify_gc_no_progress() {
 84   Atomic::inc(&_gc_no_progress_count);
 85 }
 86 
 87 inline size_t ShenandoahHeap::get_gc_no_progress_count() const {
 88   return Atomic::load(&_gc_no_progress_count);
 89 }
 90 
 91 inline size_t ShenandoahHeap::heap_region_index_containing(const void* addr) const {
 92   uintptr_t region_start = ((uintptr_t) addr);
 93   uintptr_t index = (region_start - (uintptr_t) base()) >> ShenandoahHeapRegion::region_size_bytes_shift();
 94   assert(index < num_regions(), "Region index is in bounds: " PTR_FORMAT, p2i(addr));
 95   return index;
 96 }
 97 
 98 inline ShenandoahHeapRegion* ShenandoahHeap::heap_region_containing(const void* addr) const {
 99   size_t index = heap_region_index_containing(addr);
100   ShenandoahHeapRegion* const result = get_region(index);
101   assert(addr >= result->bottom() && addr < result->end(), "Heap region contains the address: " PTR_FORMAT, p2i(addr));
102   return result;
103 }
104 
105 inline void ShenandoahHeap::enter_evacuation(Thread* t) {
106   _oom_evac_handler.enter_evacuation(t);
107 }
108 
109 inline void ShenandoahHeap::leave_evacuation(Thread* t) {
110   _oom_evac_handler.leave_evacuation(t);
111 }
112 
113 template <class T>
114 inline void ShenandoahHeap::update_with_forwarded(T* p) {
115   T o = RawAccess<>::oop_load(p);
116   if (!CompressedOops::is_null(o)) {
117     oop obj = CompressedOops::decode_not_null(o);
118     if (in_collection_set(obj)) {
119       // Corner case: when evacuation fails, there are objects in collection
120       // set that are not really forwarded. We can still go and try and update them
121       // (uselessly) to simplify the common path.
122       shenandoah_assert_forwarded_except(p, obj, cancelled_gc());
123       oop fwd = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
124       shenandoah_assert_not_in_cset_except(p, fwd, cancelled_gc());
125 
126       // Unconditionally store the update: no concurrent updates expected.
127       RawAccess<IS_NOT_NULL>::oop_store(p, fwd);
128     }
129   }
130 }
131 
132 template <class T>
133 inline void ShenandoahHeap::conc_update_with_forwarded(T* p) {
134   T o = RawAccess<>::oop_load(p);
135   if (!CompressedOops::is_null(o)) {
136     oop obj = CompressedOops::decode_not_null(o);
137     if (in_collection_set(obj)) {
138       // Corner case: when evacuation fails, there are objects in collection
139       // set that are not really forwarded. We can still go and try CAS-update them
140       // (uselessly) to simplify the common path.
141       shenandoah_assert_forwarded_except(p, obj, cancelled_gc());
142       oop fwd = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
143       shenandoah_assert_not_in_cset_except(p, fwd, cancelled_gc());
144 
145       // Sanity check: we should not be updating the cset regions themselves,
146       // unless we are recovering from the evacuation failure.
147       shenandoah_assert_not_in_cset_loc_except(p, !is_in(p) || cancelled_gc());
148 
149       // Either we succeed in updating the reference, or something else gets in our way.
150       // We don't care if that is another concurrent GC update, or another mutator update.
151       atomic_update_oop(fwd, p, obj);
152     }
153   }
154 }
155 
156 // Atomic updates of heap location. This is only expected to work with updating the same
157 // logical object with its forwardee. The reason why we need stronger-than-relaxed memory
158 // ordering has to do with coordination with GC barriers and mutator accesses.
159 //
160 // In essence, stronger CAS access is required to maintain the transitive chains that mutator
161 // accesses build by themselves. To illustrate this point, consider the following example.
162 //
163 // Suppose "o" is the object that has a field "x" and the reference to "o" is stored
164 // to field at "addr", which happens to be Java volatile field. Normally, the accesses to volatile
165 // field at "addr" would be matched with release/acquire barriers. This changes when GC moves
166 // the object under mutator feet.
167 //
168 // Thread 1 (Java)
169 //         // --- previous access starts here
170 //         ...
171 //   T1.1: store(&o.x, 1, mo_relaxed)
172 //   T1.2: store(&addr, o, mo_release) // volatile store
173 //
174 //         // --- new access starts here
175 //         // LRB: copy and install the new copy to fwdptr
176 //   T1.3: var copy = copy(o)
177 //   T1.4: cas(&fwd, t, copy, mo_release) // pointer-mediated publication
178 //         <access continues>
179 //
180 // Thread 2 (GC updater)
181 //   T2.1: var f = load(&fwd, mo_{consume|acquire}) // pointer-mediated acquisition
182 //   T2.2: cas(&addr, o, f, mo_release) // this method
183 //
184 // Thread 3 (Java)
185 //   T3.1: var o = load(&addr, mo_acquire) // volatile read
186 //   T3.2: if (o != null)
187 //   T3.3:   var r = load(&o.x, mo_relaxed)
188 //
189 // r is guaranteed to contain "1".
190 //
191 // Without GC involvement, there is synchronizes-with edge from T1.2 to T3.1,
192 // which guarantees this. With GC involvement, when LRB copies the object and
193 // another thread updates the reference to it, we need to have the transitive edge
194 // from T1.4 to T2.1 (that one is guaranteed by forwarding accesses), plus the edge
195 // from T2.2 to T3.1 (which is brought by this CAS).
196 //
197 // Note that we do not need to "acquire" in these methods, because we do not read the
198 // failure witnesses contents on any path, and "release" is enough.
199 //
200 
201 inline void ShenandoahHeap::atomic_update_oop(oop update, oop* addr, oop compare) {
202   assert(is_aligned(addr, HeapWordSize), "Address should be aligned: " PTR_FORMAT, p2i(addr));
203   Atomic::cmpxchg(addr, compare, update, memory_order_release);
204 }
205 
206 inline void ShenandoahHeap::atomic_update_oop(oop update, narrowOop* addr, narrowOop compare) {
207   assert(is_aligned(addr, sizeof(narrowOop)), "Address should be aligned: " PTR_FORMAT, p2i(addr));
208   narrowOop u = CompressedOops::encode(update);
209   Atomic::cmpxchg(addr, compare, u, memory_order_release);
210 }
211 
212 inline void ShenandoahHeap::atomic_update_oop(oop update, narrowOop* addr, oop compare) {
213   assert(is_aligned(addr, sizeof(narrowOop)), "Address should be aligned: " PTR_FORMAT, p2i(addr));
214   narrowOop c = CompressedOops::encode(compare);
215   narrowOop u = CompressedOops::encode(update);
216   Atomic::cmpxchg(addr, c, u, memory_order_release);
217 }
218 
219 inline bool ShenandoahHeap::atomic_update_oop_check(oop update, oop* addr, oop compare) {
220   assert(is_aligned(addr, HeapWordSize), "Address should be aligned: " PTR_FORMAT, p2i(addr));
221   return (oop) Atomic::cmpxchg(addr, compare, update, memory_order_release) == compare;
222 }
223 
224 inline bool ShenandoahHeap::atomic_update_oop_check(oop update, narrowOop* addr, narrowOop compare) {
225   assert(is_aligned(addr, sizeof(narrowOop)), "Address should be aligned: " PTR_FORMAT, p2i(addr));
226   narrowOop u = CompressedOops::encode(update);
227   return (narrowOop) Atomic::cmpxchg(addr, compare, u, memory_order_release) == compare;
228 }
229 
230 inline bool ShenandoahHeap::atomic_update_oop_check(oop update, narrowOop* addr, oop compare) {
231   assert(is_aligned(addr, sizeof(narrowOop)), "Address should be aligned: " PTR_FORMAT, p2i(addr));
232   narrowOop c = CompressedOops::encode(compare);
233   narrowOop u = CompressedOops::encode(update);
234   return CompressedOops::decode(Atomic::cmpxchg(addr, c, u, memory_order_release)) == compare;
235 }
236 
237 // The memory ordering discussion above does not apply for methods that store nulls:
238 // then, there is no transitive reads in mutator (as we see nulls), and we can do
239 // relaxed memory ordering there.
240 
241 inline void ShenandoahHeap::atomic_clear_oop(oop* addr, oop compare) {
242   assert(is_aligned(addr, HeapWordSize), "Address should be aligned: " PTR_FORMAT, p2i(addr));
243   Atomic::cmpxchg(addr, compare, oop(), memory_order_relaxed);
244 }
245 
246 inline void ShenandoahHeap::atomic_clear_oop(narrowOop* addr, oop compare) {
247   assert(is_aligned(addr, sizeof(narrowOop)), "Address should be aligned: " PTR_FORMAT, p2i(addr));
248   narrowOop cmp = CompressedOops::encode(compare);
249   Atomic::cmpxchg(addr, cmp, narrowOop(), memory_order_relaxed);
250 }
251 
252 inline void ShenandoahHeap::atomic_clear_oop(narrowOop* addr, narrowOop compare) {
253   assert(is_aligned(addr, sizeof(narrowOop)), "Address should be aligned: " PTR_FORMAT, p2i(addr));
254   Atomic::cmpxchg(addr, compare, narrowOop(), memory_order_relaxed);
255 }
256 
257 inline bool ShenandoahHeap::cancelled_gc() const {
258   return _cancelled_gc.get() == CANCELLED;
259 }
260 
261 inline bool ShenandoahHeap::check_cancelled_gc_and_yield(bool sts_active) {
262   if (sts_active && !cancelled_gc()) {
263     if (SuspendibleThreadSet::should_yield()) {
264       SuspendibleThreadSet::yield();
265     }
266   }
267   return cancelled_gc();
268 }
269 
270 inline void ShenandoahHeap::clear_cancelled_gc(bool clear_oom_handler) {
271   _cancelled_gc.set(CANCELLABLE);
272   if (_cancel_requested_time > 0) {
273     double cancel_time = os::elapsedTime() - _cancel_requested_time;
274     log_info(gc)("GC cancellation took %.3fs", cancel_time);
275     _cancel_requested_time = 0;
276   }
277 
278   if (clear_oom_handler) {
279     _oom_evac_handler.clear();
280   }
281 }
282 
283 inline HeapWord* ShenandoahHeap::allocate_from_gclab(Thread* thread, size_t size) {
284   assert(UseTLAB, "TLABs should be enabled");
285 
286   PLAB* gclab = ShenandoahThreadLocalData::gclab(thread);
287   if (gclab == nullptr) {
288     assert(!thread->is_Java_thread() && !thread->is_Worker_thread(),
289            "Performance: thread should have GCLAB: %s", thread->name());
290     // No GCLABs in this thread, fallback to shared allocation
291     return nullptr;
292   }
293   HeapWord* obj = gclab->allocate(size);
294   if (obj != nullptr) {
295     return obj;
296   }
297   return allocate_from_gclab_slow(thread, size);
298 }
299 
300 inline ShenandoahAgeCensus* ShenandoahHeap::age_census() const {
301   assert(mode()->is_generational(), "Only in generational mode");
302   assert(_age_census != nullptr, "Error: not initialized");
303   return _age_census;
304 }
305 
306 void ShenandoahHeap::increase_object_age(oop obj, uint additional_age) {
307   // This operates on new copy of an object. This means that the object's mark-word
308   // is thread-local and therefore safe to access. However, when the mark is
309   // displaced (i.e. stack-locked or monitor-locked), then it must be considered
310   // a shared memory location. It can be accessed by other threads.
311   // In particular, a competing evacuating thread can succeed to install its copy
312   // as the forwardee and continue to unlock the object, at which point 'our'
313   // write to the foreign stack-location would potentially over-write random
314   // information on that stack. Writing to a monitor is less problematic,
315   // but still not safe: while the ObjectMonitor would not randomly disappear,
316   // the other thread would also write to the same displaced header location,
317   // possibly leading to increase the age twice.
318   // For all these reasons, we take the conservative approach and not attempt
319   // to increase the age when the header is displaced.
320   markWord w = obj->mark();
321   // The mark-word has been copied from the original object. It can not be
322   // inflating, because inflation can not be interrupted by a safepoint,
323   // and after a safepoint, a Java thread would first have to successfully
324   // evacuate the object before it could inflate the monitor.
325   assert(!w.is_being_inflated() || LockingMode == LM_LIGHTWEIGHT, "must not inflate monitor before evacuation of object succeeds");
326   // It is possible that we have copied the object after another thread has
327   // already successfully completed evacuation. While harmless (we would never
328   // publish our copy), don't even attempt to modify the age when that
329   // happens.
330   if (!w.has_displaced_mark_helper() && !w.is_marked()) {
331     w = w.set_age(MIN2(markWord::max_age, w.age() + additional_age));
332     obj->set_mark(w);
333   }
334 }
335 
336 // Return the object's age, or a sentinel value when the age can't
337 // necessarily be determined because of concurrent locking by the
338 // mutator
339 uint ShenandoahHeap::get_object_age(oop obj) {
340   // This is impossible to do unless we "freeze" ABA-type oscillations
341   // With Lilliput, we can do this more easily.
342   markWord w = obj->mark();
343   assert(!w.is_marked(), "must not be forwarded");
344   if (w.has_monitor()) {
345     w = w.monitor()->header();
346   } else if (w.is_being_inflated() || w.has_displaced_mark_helper()) {
347     // Informs caller that we aren't able to determine the age
348     return markWord::max_age + 1; // sentinel
349   }
350   assert(w.age() <= markWord::max_age, "Impossible!");
351   return w.age();
352 }
353 
354 bool ShenandoahHeap::is_in(const void* p) const {
355   HeapWord* heap_base = (HeapWord*) base();
356   HeapWord* last_region_end = heap_base + ShenandoahHeapRegion::region_size_words() * num_regions();
357   return p >= heap_base && p < last_region_end;
358 }
359 
360 inline bool ShenandoahHeap::is_in_active_generation(oop obj) const {
361   if (!mode()->is_generational()) {
362     // everything is the same single generation
363     return true;
364   }
365 
366   if (active_generation() == nullptr) {
367     // no collection is happening, only expect this to be called
368     // when concurrent processing is active, but that could change
369     return false;
370   }
371 
372   assert(is_in(obj), "only check if is in active generation for objects (" PTR_FORMAT ") in heap", p2i(obj));
373   assert((active_generation() == (ShenandoahGeneration*) old_generation()) ||
374          (active_generation() == (ShenandoahGeneration*) young_generation()) ||
375          (active_generation() == global_generation()), "Active generation must be old, young, or global");
376 
377   size_t index = heap_region_containing(obj)->index();
378   switch (_affiliations[index]) {
379   case ShenandoahAffiliation::FREE:
380     // Free regions are in Old, Young, Global
381     return true;
382   case ShenandoahAffiliation::YOUNG_GENERATION:
383     // Young regions are in young_generation and global_generation, not in old_generation
384     return (active_generation() != (ShenandoahGeneration*) old_generation());
385   case ShenandoahAffiliation::OLD_GENERATION:
386     // Old regions are in old_generation and global_generation, not in young_generation
387     return (active_generation() != (ShenandoahGeneration*) young_generation());
388   default:
389     assert(false, "Bad affiliation (%d) for region " SIZE_FORMAT, _affiliations[index], index);
390     return false;
391   }
392 }
393 
394 inline bool ShenandoahHeap::is_in_young(const void* p) const {
395   return is_in(p) && (_affiliations[heap_region_index_containing(p)] == ShenandoahAffiliation::YOUNG_GENERATION);
396 }
397 
398 inline bool ShenandoahHeap::is_in_old(const void* p) const {
399   return is_in(p) && (_affiliations[heap_region_index_containing(p)] == ShenandoahAffiliation::OLD_GENERATION);
400 }
401 
402 inline bool ShenandoahHeap::is_old(oop obj) const {
403   return active_generation()->is_young() && is_in_old(obj);
404 }
405 
406 inline ShenandoahAffiliation ShenandoahHeap::region_affiliation(const ShenandoahHeapRegion *r) {
407   return (ShenandoahAffiliation) _affiliations[r->index()];
408 }
409 
410 inline void ShenandoahHeap::assert_lock_for_affiliation(ShenandoahAffiliation orig_affiliation,
411                                                         ShenandoahAffiliation new_affiliation) {
412   // A lock is required when changing from FREE to NON-FREE.  Though it may be possible to elide the lock when
413   // transitioning from in-use to FREE, the current implementation uses a lock for this transition.  A lock is
414   // not required to change from YOUNG to OLD (i.e. when promoting humongous region).
415   //
416   //         new_affiliation is:     FREE   YOUNG   OLD
417   //  orig_affiliation is:  FREE      X       L      L
418   //                       YOUNG      L       X
419   //                         OLD      L       X      X
420   //  X means state transition won't happen (so don't care)
421   //  L means lock should be held
422   //  Blank means no lock required because affiliation visibility will not be required until subsequent safepoint
423   //
424   // Note: during full GC, all transitions between states are possible.  During Full GC, we should be in a safepoint.
425 
426   if ((orig_affiliation == ShenandoahAffiliation::FREE) || (new_affiliation == ShenandoahAffiliation::FREE)) {
427     shenandoah_assert_heaplocked_or_fullgc_safepoint();
428   }
429 }
430 
431 inline void ShenandoahHeap::set_affiliation(ShenandoahHeapRegion* r, ShenandoahAffiliation new_affiliation) {
432 #ifdef ASSERT
433   assert_lock_for_affiliation(region_affiliation(r), new_affiliation);
434 #endif
435   _affiliations[r->index()] = (uint8_t) new_affiliation;
436 }
437 
438 inline ShenandoahAffiliation ShenandoahHeap::region_affiliation(size_t index) {
439   return (ShenandoahAffiliation) _affiliations[index];
440 }
441 
442 inline bool ShenandoahHeap::requires_marking(const void* entry) const {
443   oop obj = cast_to_oop(entry);
444   return !_marking_context->is_marked_strong(obj);
445 }
446 
447 inline bool ShenandoahHeap::in_collection_set(oop p) const {
448   assert(collection_set() != nullptr, "Sanity");
449   return collection_set()->is_in(p);
450 }
451 
452 inline bool ShenandoahHeap::in_collection_set_loc(void* p) const {
453   assert(collection_set() != nullptr, "Sanity");
454   return collection_set()->is_in_loc(p);
455 }
456 
457 
458 inline bool ShenandoahHeap::is_stable() const {
459   return _gc_state.is_clear();
460 }
461 
462 inline bool ShenandoahHeap::is_idle() const {
463   return _gc_state.is_unset(MARKING | EVACUATION | UPDATEREFS);
464 }
465 
466 inline bool ShenandoahHeap::is_concurrent_mark_in_progress() const {
467   return _gc_state.is_set(MARKING);
468 }
469 
470 inline bool ShenandoahHeap::is_concurrent_young_mark_in_progress() const {
471   return _gc_state.is_set(YOUNG_MARKING);
472 }
473 
474 inline bool ShenandoahHeap::is_concurrent_old_mark_in_progress() const {
475   return _gc_state.is_set(OLD_MARKING);
476 }
477 
478 inline bool ShenandoahHeap::is_evacuation_in_progress() const {
479   return _gc_state.is_set(EVACUATION);
480 }
481 
482 inline bool ShenandoahHeap::is_degenerated_gc_in_progress() const {
483   return _degenerated_gc_in_progress.is_set();
484 }
485 
486 inline bool ShenandoahHeap::is_full_gc_in_progress() const {
487   return _full_gc_in_progress.is_set();
488 }
489 
490 inline bool ShenandoahHeap::is_full_gc_move_in_progress() const {
491   return _full_gc_move_in_progress.is_set();
492 }
493 
494 inline bool ShenandoahHeap::is_update_refs_in_progress() const {
495   return _gc_state.is_set(UPDATEREFS);
496 }
497 
498 inline bool ShenandoahHeap::is_stw_gc_in_progress() const {
499   return is_full_gc_in_progress() || is_degenerated_gc_in_progress();
500 }
501 
502 inline bool ShenandoahHeap::is_concurrent_strong_root_in_progress() const {
503   return _concurrent_strong_root_in_progress.is_set();
504 }
505 
506 inline bool ShenandoahHeap::is_concurrent_weak_root_in_progress() const {
507   return _gc_state.is_set(WEAK_ROOTS);
508 }
509 
510 inline bool ShenandoahHeap::is_aging_cycle() const {
511   return _is_aging_cycle.is_set();
512 }
513 
514 template<class T>
515 inline void ShenandoahHeap::marked_object_iterate(ShenandoahHeapRegion* region, T* cl) {
516   marked_object_iterate(region, cl, region->top());
517 }
518 
519 template<class T>
520 inline void ShenandoahHeap::marked_object_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* limit) {
521   assert(! region->is_humongous_continuation(), "no humongous continuation regions here");
522 
523   ShenandoahMarkingContext* const ctx = marking_context();
524 
525   HeapWord* tams = ctx->top_at_mark_start(region);
526 
527   size_t skip_bitmap_delta = 1;
528   HeapWord* start = region->bottom();
529   HeapWord* end = MIN2(tams, region->end());
530 
531   // Step 1. Scan below the TAMS based on bitmap data.
532   HeapWord* limit_bitmap = MIN2(limit, tams);
533 
534   // Try to scan the initial candidate. If the candidate is above the TAMS, it would
535   // fail the subsequent "< limit_bitmap" checks, and fall through to Step 2.
536   HeapWord* cb = ctx->get_next_marked_addr(start, end);
537 
538   intx dist = ShenandoahMarkScanPrefetch;
539   if (dist > 0) {
540     // Batched scan that prefetches the oop data, anticipating the access to
541     // either header, oop field, or forwarding pointer. Not that we cannot
542     // touch anything in oop, while it still being prefetched to get enough
543     // time for prefetch to work. This is why we try to scan the bitmap linearly,
544     // disregarding the object size. However, since we know forwarding pointer
545     // precedes the object, we can skip over it. Once we cannot trust the bitmap,
546     // there is no point for prefetching the oop contents, as oop->size() will
547     // touch it prematurely.
548 
549     // No variable-length arrays in standard C++, have enough slots to fit
550     // the prefetch distance.
551     static const int SLOT_COUNT = 256;
552     guarantee(dist <= SLOT_COUNT, "adjust slot count");
553     HeapWord* slots[SLOT_COUNT];
554 
555     int avail;
556     do {
557       avail = 0;
558       for (int c = 0; (c < dist) && (cb < limit_bitmap); c++) {
559         Prefetch::read(cb, oopDesc::mark_offset_in_bytes());
560         slots[avail++] = cb;
561         cb += skip_bitmap_delta;
562         if (cb < limit_bitmap) {
563           cb = ctx->get_next_marked_addr(cb, limit_bitmap);
564         }
565       }
566 
567       for (int c = 0; c < avail; c++) {
568         assert (slots[c] < tams,  "only objects below TAMS here: "  PTR_FORMAT " (" PTR_FORMAT ")", p2i(slots[c]), p2i(tams));
569         assert (slots[c] < limit, "only objects below limit here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(slots[c]), p2i(limit));
570         oop obj = cast_to_oop(slots[c]);
571         assert(oopDesc::is_oop(obj), "sanity");
572         assert(ctx->is_marked(obj), "object expected to be marked");
573         cl->do_object(obj);
574       }
575     } while (avail > 0);
576   } else {
577     while (cb < limit_bitmap) {
578       assert (cb < tams,  "only objects below TAMS here: "  PTR_FORMAT " (" PTR_FORMAT ")", p2i(cb), p2i(tams));
579       assert (cb < limit, "only objects below limit here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(cb), p2i(limit));
580       oop obj = cast_to_oop(cb);
581       assert(oopDesc::is_oop(obj), "sanity");
582       assert(ctx->is_marked(obj), "object expected to be marked");
583       cl->do_object(obj);
584       cb += skip_bitmap_delta;
585       if (cb < limit_bitmap) {
586         cb = ctx->get_next_marked_addr(cb, limit_bitmap);
587       }
588     }
589   }
590 
591   // Step 2. Accurate size-based traversal, happens past the TAMS.
592   // This restarts the scan at TAMS, which makes sure we traverse all objects,
593   // regardless of what happened at Step 1.
594   HeapWord* cs = tams;
595   while (cs < limit) {
596     assert (cs >= tams, "only objects past TAMS here: "   PTR_FORMAT " (" PTR_FORMAT ")", p2i(cs), p2i(tams));
597     assert (cs < limit, "only objects below limit here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(cs), p2i(limit));
598     oop obj = cast_to_oop(cs);
599     assert(oopDesc::is_oop(obj), "sanity");
600     assert(ctx->is_marked(obj), "object expected to be marked");
601     size_t size = obj->size();
602     cl->do_object(obj);
603     cs += size;
604   }
605 }
606 
607 template <class T>
608 class ShenandoahObjectToOopClosure : public ObjectClosure {
609   T* _cl;
610 public:
611   ShenandoahObjectToOopClosure(T* cl) : _cl(cl) {}
612 
613   void do_object(oop obj) {
614     obj->oop_iterate(_cl);
615   }
616 };
617 
618 template <class T>
619 class ShenandoahObjectToOopBoundedClosure : public ObjectClosure {
620   T* _cl;
621   MemRegion _bounds;
622 public:
623   ShenandoahObjectToOopBoundedClosure(T* cl, HeapWord* bottom, HeapWord* top) :
624     _cl(cl), _bounds(bottom, top) {}
625 
626   void do_object(oop obj) {
627     obj->oop_iterate(_cl, _bounds);
628   }
629 };
630 
631 template<class T>
632 inline void ShenandoahHeap::marked_object_oop_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* top) {
633   if (region->is_humongous()) {
634     HeapWord* bottom = region->bottom();
635     if (top > bottom) {
636       region = region->humongous_start_region();
637       ShenandoahObjectToOopBoundedClosure<T> objs(cl, bottom, top);
638       marked_object_iterate(region, &objs);
639     }
640   } else {
641     ShenandoahObjectToOopClosure<T> objs(cl);
642     marked_object_iterate(region, &objs, top);
643   }
644 }
645 
646 inline ShenandoahHeapRegion* ShenandoahHeap::get_region(size_t region_idx) const {
647   if (region_idx < _num_regions) {
648     return _regions[region_idx];
649   } else {
650     return nullptr;
651   }
652 }
653 
654 inline ShenandoahMarkingContext* ShenandoahHeap::complete_marking_context() const {
655   assert (_marking_context->is_complete()," sanity");
656   return _marking_context;
657 }
658 
659 inline ShenandoahMarkingContext* ShenandoahHeap::marking_context() const {
660   return _marking_context;
661 }
662 
663 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_INLINE_HPP