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