1 /* 2 * Copyright (c) 2015, 2019, Red Hat, Inc. All rights reserved. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 * 22 */ 23 24 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP 25 #define SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP 26 27 #include "gc/shared/barrierSet.hpp" 28 #include "gc/shenandoah/shenandoahAsserts.hpp" 29 #include "gc/shenandoah/shenandoahBarrierSet.hpp" 30 #include "gc/shenandoah/shenandoahCollectionSet.inline.hpp" 31 #include "gc/shenandoah/shenandoahForwarding.inline.hpp" 32 #include "gc/shenandoah/shenandoahHeap.inline.hpp" 33 #include "gc/shenandoah/shenandoahHeapRegion.hpp" 34 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp" 35 #include "gc/shenandoah/shenandoahThreadLocalData.hpp" 36 #include "memory/iterator.inline.hpp" 37 #include "oops/oop.inline.hpp" 38 39 inline oop ShenandoahBarrierSet::resolve_forwarded_not_null(oop p) { 40 return ShenandoahForwarding::get_forwardee(p); 41 } 42 43 inline oop ShenandoahBarrierSet::resolve_forwarded(oop p) { 44 if (((HeapWord*) p) != NULL) { 45 return resolve_forwarded_not_null(p); 46 } else { 47 return p; 48 } 49 } 50 51 inline oop ShenandoahBarrierSet::resolve_forwarded_not_null_mutator(oop p) { 52 return ShenandoahForwarding::get_forwardee_mutator(p); 53 } 54 55 inline void ShenandoahBarrierSet::enqueue(oop obj) { 56 shenandoah_assert_not_forwarded_if(NULL, obj, _heap->is_concurrent_traversal_in_progress()); 57 assert(_satb_mark_queue_set.is_active(), "only get here when SATB active"); 58 59 // Filter marked objects before hitting the SATB queues. The same predicate would 60 // be used by SATBMQ::filter to eliminate already marked objects downstream, but 61 // filtering here helps to avoid wasteful SATB queueing work to begin with. 62 if (!_heap->requires_marking<false>(obj)) return; 63 64 ShenandoahThreadLocalData::satb_mark_queue(Thread::current()).enqueue_known_active(obj); 65 } 66 67 template <DecoratorSet decorators, typename T> 68 inline void ShenandoahBarrierSet::satb_barrier(T *field) { 69 if (HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value || 70 HasDecorator<decorators, AS_NO_KEEPALIVE>::value) { 71 return; 72 } 73 if (ShenandoahSATBBarrier && _heap->is_concurrent_mark_in_progress()) { 74 T heap_oop = RawAccess<>::oop_load(field); 75 if (!CompressedOops::is_null(heap_oop)) { 76 enqueue(CompressedOops::decode(heap_oop)); 77 } 78 } 79 } 80 81 inline void ShenandoahBarrierSet::satb_enqueue(oop value) { 82 assert(value != NULL, "checked before"); 83 if (ShenandoahSATBBarrier && _heap->is_concurrent_mark_in_progress()) { 84 enqueue(value); 85 } 86 } 87 88 inline void ShenandoahBarrierSet::storeval_barrier(oop obj) { 89 if (obj != NULL && ShenandoahStoreValEnqueueBarrier && _heap->is_concurrent_traversal_in_progress()) { 90 enqueue(obj); 91 } 92 } 93 94 inline void ShenandoahBarrierSet::keep_alive_barrier(oop value) { 95 assert(value != NULL, "checked before"); 96 if (ShenandoahKeepAliveBarrier && _heap->is_concurrent_mark_in_progress()) { 97 enqueue(value); 98 } 99 } 100 101 inline void ShenandoahBarrierSet::keep_alive_if_weak(DecoratorSet decorators, oop value) { 102 assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Reference strength must be known"); 103 const bool on_strong_oop_ref = (decorators & ON_STRONG_OOP_REF) != 0; 104 const bool peek = (decorators & AS_NO_KEEPALIVE) != 0; 105 if (!peek && !on_strong_oop_ref) { 106 keep_alive_barrier(value); 107 } 108 } 109 110 template <DecoratorSet decorators> 111 inline void ShenandoahBarrierSet::keep_alive_if_weak(oop value) { 112 assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Reference strength must be known"); 113 if (!HasDecorator<decorators, ON_STRONG_OOP_REF>::value && 114 !HasDecorator<decorators, AS_NO_KEEPALIVE>::value) { 115 keep_alive_barrier(value); 116 } 117 } 118 119 template <DecoratorSet decorators, typename BarrierSetT> 120 template <typename T> 121 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_not_in_heap(T* addr) { 122 oop value = Raw::oop_load_not_in_heap(addr); 123 if (value != NULL) { 124 ShenandoahBarrierSet *const bs = ShenandoahBarrierSet::barrier_set(); 125 value = bs->load_reference_barrier_native(value, addr); 126 if (value != NULL) { 127 bs->keep_alive_if_weak<decorators>(value); 128 } 129 } 130 return value; 131 } 132 133 template <DecoratorSet decorators, typename BarrierSetT> 134 template <typename T> 135 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap(T* addr) { 136 oop value = Raw::oop_load_in_heap(addr); 137 if (value != NULL) { 138 ShenandoahBarrierSet *const bs = ShenandoahBarrierSet::barrier_set(); 139 value = bs->load_reference_barrier_not_null(value); 140 bs->keep_alive_if_weak<decorators>(value); 141 } 142 return value; 143 } 144 145 template <DecoratorSet decorators, typename BarrierSetT> 146 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap_at(oop base, ptrdiff_t offset) { 147 oop value = Raw::oop_load_in_heap_at(base, offset); 148 if (value != NULL) { 149 ShenandoahBarrierSet *const bs = ShenandoahBarrierSet::barrier_set(); 150 value = bs->load_reference_barrier_not_null(value); 151 bs->keep_alive_if_weak(AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset), 152 value); 153 } 154 return value; 155 } 156 157 template <DecoratorSet decorators, typename BarrierSetT> 158 template <typename T> 159 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_not_in_heap(T* addr, oop value) { 160 shenandoah_assert_marked_if(NULL, value, !CompressedOops::is_null(value) && ShenandoahHeap::heap()->is_evacuation_in_progress()); 161 ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set(); 162 bs->storeval_barrier(value); 163 bs->satb_barrier<decorators>(addr); 164 Raw::oop_store(addr, value); 165 } 166 167 template <DecoratorSet decorators, typename BarrierSetT> 168 template <typename T> 169 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap(T* addr, oop value) { 170 shenandoah_assert_not_in_cset_loc_except(addr, ShenandoahHeap::heap()->cancelled_gc()); 171 shenandoah_assert_not_forwarded_except (addr, value, value == NULL || ShenandoahHeap::heap()->cancelled_gc() || !ShenandoahHeap::heap()->is_concurrent_mark_in_progress()); 172 shenandoah_assert_not_in_cset_except (addr, value, value == NULL || ShenandoahHeap::heap()->cancelled_gc() || !ShenandoahHeap::heap()->is_concurrent_mark_in_progress()); 173 174 oop_store_not_in_heap(addr, value); 175 } 176 177 template <DecoratorSet decorators, typename BarrierSetT> 178 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value) { 179 oop_store_in_heap(AccessInternal::oop_field_addr<decorators>(base, offset), value); 180 } 181 182 template <DecoratorSet decorators, typename BarrierSetT> 183 template <typename T> 184 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_not_in_heap(T* addr, oop compare_value, oop new_value) { 185 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set(); 186 bs->storeval_barrier(new_value); 187 188 oop res; 189 oop expected = compare_value; 190 do { 191 compare_value = expected; 192 res = Raw::oop_atomic_cmpxchg(addr, compare_value, new_value); 193 expected = res; 194 } while ((compare_value != expected) && (resolve_forwarded(compare_value) == resolve_forwarded(expected))); 195 196 // Note: We don't need a keep-alive-barrier here. We already enqueue any loaded reference for SATB anyway, 197 // because it must be the previous value. 198 if (res != NULL) { 199 res = ShenandoahBarrierSet::barrier_set()->load_reference_barrier_not_null(res); 200 bs->satb_enqueue(res); 201 } 202 return res; 203 } 204 205 template <DecoratorSet decorators, typename BarrierSetT> 206 template <typename T> 207 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap(T* addr, oop compare_value, oop new_value) { 208 return oop_atomic_cmpxchg_not_in_heap(addr, compare_value, new_value); 209 } 210 211 template <DecoratorSet decorators, typename BarrierSetT> 212 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap_at(oop base, ptrdiff_t offset, oop compare_value, oop new_value) { 213 return oop_atomic_cmpxchg_in_heap(AccessInternal::oop_field_addr<decorators>(base, offset), compare_value, new_value); 214 } 215 216 template <DecoratorSet decorators, typename BarrierSetT> 217 template <typename T> 218 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_not_in_heap(T* addr, oop new_value) { 219 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set(); 220 bs->storeval_barrier(new_value); 221 222 oop previous = Raw::oop_atomic_xchg(addr, new_value); 223 224 // Note: We don't need a keep-alive-barrier here. We already enqueue any loaded reference for SATB anyway, 225 // because it must be the previous value. 226 if (previous != NULL) { 227 previous = ShenandoahBarrierSet::barrier_set()->load_reference_barrier_not_null(previous); 228 bs->satb_enqueue(previous); 229 } 230 return previous; 231 } 232 233 template <DecoratorSet decorators, typename BarrierSetT> 234 template <typename T> 235 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap(T* addr, oop new_value) { 236 return oop_atomic_xchg_not_in_heap(addr, new_value); 237 } 238 239 template <DecoratorSet decorators, typename BarrierSetT> 240 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap_at(oop base, ptrdiff_t offset, oop new_value) { 241 return oop_atomic_xchg_in_heap(AccessInternal::oop_field_addr<decorators>(base, offset), new_value); 242 } 243 244 // Clone barrier support 245 template <DecoratorSet decorators, typename BarrierSetT> 246 void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::clone_in_heap(oop src, oop dst, size_t size) { 247 if (ShenandoahCloneBarrier) { 248 ShenandoahBarrierSet::barrier_set()->clone_barrier_runtime(src); 249 } 250 Raw::clone(src, dst, size); 251 } 252 253 template <DecoratorSet decorators, typename BarrierSetT> 254 template <typename T> 255 bool ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw, 256 arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw, 257 size_t length) { 258 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set(); 259 bs->arraycopy_pre(arrayOopDesc::obj_offset_to_raw(src_obj, src_offset_in_bytes, src_raw), 260 arrayOopDesc::obj_offset_to_raw(dst_obj, dst_offset_in_bytes, dst_raw), 261 length); 262 return Raw::oop_arraycopy_in_heap(src_obj, src_offset_in_bytes, src_raw, dst_obj, dst_offset_in_bytes, dst_raw, length); 263 } 264 265 template <class T, bool HAS_FWD, bool EVAC, bool ENQUEUE> 266 void ShenandoahBarrierSet::arraycopy_work(T* src, size_t count) { 267 assert(HAS_FWD == _heap->has_forwarded_objects(), "Forwarded object status is sane"); 268 269 Thread* thread = Thread::current(); 270 SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread); 271 ShenandoahMarkingContext* ctx = _heap->marking_context(); 272 const ShenandoahCollectionSet* const cset = _heap->collection_set(); 273 T* end = src + count; 274 for (T* elem_ptr = src; elem_ptr < end; elem_ptr++) { 275 T o = RawAccess<>::oop_load(elem_ptr); 276 if (!CompressedOops::is_null(o)) { 277 oop obj = CompressedOops::decode_not_null(o); 278 if (HAS_FWD && cset->is_in(obj)) { 279 oop fwd = resolve_forwarded_not_null(obj); 280 if (EVAC && obj == fwd) { 281 fwd = _heap->evacuate_object(obj, thread); 282 } 283 assert(obj != fwd || _heap->cancelled_gc(), "must be forwarded"); 284 oop witness = ShenandoahHeap::cas_oop(fwd, elem_ptr, o); 285 obj = fwd; 286 } 287 if (ENQUEUE && !ctx->is_marked(obj)) { 288 queue.enqueue_known_active(obj); 289 } 290 } 291 } 292 } 293 294 template <class T> 295 void ShenandoahBarrierSet::arraycopy_pre_work(T* src, T* dst, size_t count) { 296 if (_heap->is_concurrent_mark_in_progress() && 297 !_heap->marking_context()->allocated_after_mark_start(reinterpret_cast<HeapWord*>(dst))) { 298 arraycopy_work<T, false, false, true>(dst, count); 299 } 300 301 if (_heap->has_forwarded_objects()) { 302 arraycopy_update_impl(src, count); 303 } 304 } 305 306 void ShenandoahBarrierSet::arraycopy_pre(oop* src, oop* dst, size_t count) { 307 arraycopy_pre_work(src, dst, count); 308 } 309 310 void ShenandoahBarrierSet::arraycopy_pre(narrowOop* src, narrowOop* dst, size_t count) { 311 arraycopy_pre_work(src, dst, count); 312 } 313 314 inline bool ShenandoahBarrierSet::skip_bulk_update(HeapWord* dst) { 315 return dst >= _heap->heap_region_containing(dst)->get_update_watermark(); 316 } 317 318 template <class T> 319 void ShenandoahBarrierSet::arraycopy_update_impl(T* src, size_t count) { 320 if (skip_bulk_update(reinterpret_cast<HeapWord*>(src))) return; 321 if (_heap->is_evacuation_in_progress()) { 322 ShenandoahEvacOOMScope oom_evac; 323 arraycopy_work<T, true, true, false>(src, count); 324 } else if (_heap->is_concurrent_traversal_in_progress()){ 325 ShenandoahEvacOOMScope oom_evac; 326 arraycopy_work<T, true, true, true>(src, count); 327 } else if (_heap->has_forwarded_objects()) { 328 arraycopy_work<T, true, false, false>(src, count); 329 } 330 } 331 332 void ShenandoahBarrierSet::arraycopy_update(oop* src, size_t count) { 333 arraycopy_update_impl(src, count); 334 } 335 336 void ShenandoahBarrierSet::arraycopy_update(narrowOop* src, size_t count) { 337 arraycopy_update_impl(src, count); 338 } 339 340 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP