Path: blob/master/src/hotspot/share/gc/shenandoah/shenandoahBarrierSet.inline.hpp
40960 views
/*1* Copyright (c) 2015, 2021, Red Hat, Inc. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 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 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP25#define SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP2627#include "gc/shenandoah/shenandoahBarrierSet.hpp"2829#include "gc/shared/accessBarrierSupport.inline.hpp"30#include "gc/shenandoah/shenandoahAsserts.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 "oops/oop.inline.hpp"3940inline oop ShenandoahBarrierSet::resolve_forwarded_not_null(oop p) {41return ShenandoahForwarding::get_forwardee(p);42}4344inline oop ShenandoahBarrierSet::resolve_forwarded(oop p) {45if (p != NULL) {46return resolve_forwarded_not_null(p);47} else {48return p;49}50}5152inline oop ShenandoahBarrierSet::resolve_forwarded_not_null_mutator(oop p) {53return ShenandoahForwarding::get_forwardee_mutator(p);54}5556template <class T>57inline oop ShenandoahBarrierSet::load_reference_barrier_mutator(oop obj, T* load_addr) {58assert(ShenandoahLoadRefBarrier, "should be enabled");59shenandoah_assert_in_cset(load_addr, obj);6061oop fwd = resolve_forwarded_not_null_mutator(obj);62if (obj == fwd) {63assert(_heap->is_evacuation_in_progress(),64"evac should be in progress");65Thread* const t = Thread::current();66ShenandoahEvacOOMScope scope(t);67fwd = _heap->evacuate_object(obj, t);68}6970if (load_addr != NULL && fwd != obj) {71// Since we are here and we know the load address, update the reference.72ShenandoahHeap::cas_oop(fwd, load_addr, obj);73}7475return fwd;76}7778inline oop ShenandoahBarrierSet::load_reference_barrier(oop obj) {79if (!ShenandoahLoadRefBarrier) {80return obj;81}82if (_heap->has_forwarded_objects() &&83_heap->in_collection_set(obj)) { // Subsumes NULL-check84assert(obj != NULL, "cset check must have subsumed NULL-check");85oop fwd = resolve_forwarded_not_null(obj);86// TODO: It should not be necessary to check evac-in-progress here.87// We do it for mark-compact, which may have forwarded objects,88// and objects in cset and gets here via runtime barriers.89// We can probably fix this as soon as mark-compact has its own90// marking phase.91if (obj == fwd && _heap->is_evacuation_in_progress()) {92Thread* t = Thread::current();93ShenandoahEvacOOMScope oom_evac_scope(t);94return _heap->evacuate_object(obj, t);95}96return fwd;97}98return obj;99}100101template <DecoratorSet decorators, class T>102inline oop ShenandoahBarrierSet::load_reference_barrier(oop obj, T* load_addr) {103if (obj == NULL) {104return NULL;105}106107// Prevent resurrection of unreachable phantom (i.e. weak-native) references.108if (HasDecorator<decorators, ON_PHANTOM_OOP_REF>::value &&109_heap->is_concurrent_weak_root_in_progress() &&110!_heap->marking_context()->is_marked(obj)) {111return NULL;112}113114// Prevent resurrection of unreachable weak references.115if ((HasDecorator<decorators, ON_WEAK_OOP_REF>::value || HasDecorator<decorators, ON_UNKNOWN_OOP_REF>::value) &&116_heap->is_concurrent_weak_root_in_progress() &&117!_heap->marking_context()->is_marked_strong(obj)) {118return NULL;119}120121// Prevent resurrection of unreachable objects that are visited during122// concurrent class-unloading.123if (HasDecorator<decorators, AS_NO_KEEPALIVE>::value &&124_heap->is_evacuation_in_progress() &&125!_heap->marking_context()->is_marked(obj)) {126return obj;127}128129oop fwd = load_reference_barrier(obj);130if (ShenandoahSelfFixing && load_addr != NULL && fwd != obj) {131// Since we are here and we know the load address, update the reference.132ShenandoahHeap::cas_oop(fwd, load_addr, obj);133}134135return fwd;136}137138inline void ShenandoahBarrierSet::enqueue(oop obj) {139assert(obj != NULL, "checked by caller");140assert(_satb_mark_queue_set.is_active(), "only get here when SATB active");141142// Filter marked objects before hitting the SATB queues. The same predicate would143// be used by SATBMQ::filter to eliminate already marked objects downstream, but144// filtering here helps to avoid wasteful SATB queueing work to begin with.145if (!_heap->requires_marking(obj)) return;146147SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(Thread::current());148_satb_mark_queue_set.enqueue_known_active(queue, obj);149}150151template <DecoratorSet decorators, typename T>152inline void ShenandoahBarrierSet::satb_barrier(T *field) {153if (HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value ||154HasDecorator<decorators, AS_NO_KEEPALIVE>::value) {155return;156}157if (ShenandoahSATBBarrier && _heap->is_concurrent_mark_in_progress()) {158T heap_oop = RawAccess<>::oop_load(field);159if (!CompressedOops::is_null(heap_oop)) {160enqueue(CompressedOops::decode(heap_oop));161}162}163}164165inline void ShenandoahBarrierSet::satb_enqueue(oop value) {166if (value != NULL && ShenandoahSATBBarrier && _heap->is_concurrent_mark_in_progress()) {167enqueue(value);168}169}170171inline void ShenandoahBarrierSet::iu_barrier(oop obj) {172if (ShenandoahIUBarrier && obj != NULL && _heap->is_concurrent_mark_in_progress()) {173enqueue(obj);174}175}176177inline void ShenandoahBarrierSet::keep_alive_if_weak(DecoratorSet decorators, oop value) {178assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Reference strength must be known");179const bool on_strong_oop_ref = (decorators & ON_STRONG_OOP_REF) != 0;180const bool peek = (decorators & AS_NO_KEEPALIVE) != 0;181if (!peek && !on_strong_oop_ref) {182satb_enqueue(value);183}184}185186template <DecoratorSet decorators>187inline void ShenandoahBarrierSet::keep_alive_if_weak(oop value) {188assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Reference strength must be known");189if (!HasDecorator<decorators, ON_STRONG_OOP_REF>::value &&190!HasDecorator<decorators, AS_NO_KEEPALIVE>::value) {191satb_enqueue(value);192}193}194195template <DecoratorSet decorators, typename BarrierSetT>196template <typename T>197inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_not_in_heap(T* addr) {198oop value = Raw::oop_load_not_in_heap(addr);199if (value != NULL) {200ShenandoahBarrierSet *const bs = ShenandoahBarrierSet::barrier_set();201value = bs->load_reference_barrier<decorators, T>(value, addr);202bs->keep_alive_if_weak<decorators>(value);203}204return value;205}206207template <DecoratorSet decorators, typename BarrierSetT>208template <typename T>209inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap(T* addr) {210oop value = Raw::oop_load_in_heap(addr);211ShenandoahBarrierSet *const bs = ShenandoahBarrierSet::barrier_set();212value = bs->load_reference_barrier<decorators, T>(value, addr);213bs->keep_alive_if_weak<decorators>(value);214return value;215}216217template <DecoratorSet decorators, typename BarrierSetT>218inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap_at(oop base, ptrdiff_t offset) {219oop value = Raw::oop_load_in_heap_at(base, offset);220ShenandoahBarrierSet *const bs = ShenandoahBarrierSet::barrier_set();221DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);222value = bs->load_reference_barrier<decorators>(value, AccessInternal::oop_field_addr<decorators>(base, offset));223bs->keep_alive_if_weak(resolved_decorators, value);224return value;225}226227template <DecoratorSet decorators, typename BarrierSetT>228template <typename T>229inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_not_in_heap(T* addr, oop value) {230shenandoah_assert_marked_if(NULL, value, !CompressedOops::is_null(value) && ShenandoahHeap::heap()->is_evacuation_in_progress());231shenandoah_assert_not_in_cset_if(addr, value, value != NULL && !ShenandoahHeap::heap()->cancelled_gc());232ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();233bs->iu_barrier(value);234bs->satb_barrier<decorators>(addr);235Raw::oop_store(addr, value);236}237238template <DecoratorSet decorators, typename BarrierSetT>239template <typename T>240inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap(T* addr, oop value) {241shenandoah_assert_not_in_cset_loc_except(addr, ShenandoahHeap::heap()->cancelled_gc());242shenandoah_assert_not_forwarded_except (addr, value, value == NULL || ShenandoahHeap::heap()->cancelled_gc() || !ShenandoahHeap::heap()->is_concurrent_mark_in_progress());243shenandoah_assert_not_in_cset_except (addr, value, value == NULL || ShenandoahHeap::heap()->cancelled_gc() || !ShenandoahHeap::heap()->is_concurrent_mark_in_progress());244245oop_store_not_in_heap(addr, value);246}247248template <DecoratorSet decorators, typename BarrierSetT>249inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value) {250oop_store_in_heap(AccessInternal::oop_field_addr<decorators>(base, offset), value);251}252253template <DecoratorSet decorators, typename BarrierSetT>254template <typename T>255inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_not_in_heap(T* addr, oop compare_value, oop new_value) {256ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();257bs->iu_barrier(new_value);258259oop res;260oop expected = compare_value;261do {262compare_value = expected;263res = Raw::oop_atomic_cmpxchg(addr, compare_value, new_value);264expected = res;265} while ((compare_value != expected) && (resolve_forwarded(compare_value) == resolve_forwarded(expected)));266267// Note: We don't need a keep-alive-barrier here. We already enqueue any loaded reference for SATB anyway,268// because it must be the previous value.269res = ShenandoahBarrierSet::barrier_set()->load_reference_barrier<decorators, T>(res, NULL);270bs->satb_enqueue(res);271return res;272}273274template <DecoratorSet decorators, typename BarrierSetT>275template <typename T>276inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap(T* addr, oop compare_value, oop new_value) {277return oop_atomic_cmpxchg_not_in_heap(addr, compare_value, new_value);278}279280template <DecoratorSet decorators, typename BarrierSetT>281inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap_at(oop base, ptrdiff_t offset, oop compare_value, oop new_value) {282return oop_atomic_cmpxchg_in_heap(AccessInternal::oop_field_addr<decorators>(base, offset), compare_value, new_value);283}284285template <DecoratorSet decorators, typename BarrierSetT>286template <typename T>287inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_not_in_heap(T* addr, oop new_value) {288ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();289bs->iu_barrier(new_value);290291oop previous = Raw::oop_atomic_xchg(addr, new_value);292293// Note: We don't need a keep-alive-barrier here. We already enqueue any loaded reference for SATB anyway,294// because it must be the previous value.295previous = ShenandoahBarrierSet::barrier_set()->load_reference_barrier<decorators, T>(previous, NULL);296bs->satb_enqueue(previous);297return previous;298}299300template <DecoratorSet decorators, typename BarrierSetT>301template <typename T>302inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap(T* addr, oop new_value) {303return oop_atomic_xchg_not_in_heap(addr, new_value);304}305306template <DecoratorSet decorators, typename BarrierSetT>307inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap_at(oop base, ptrdiff_t offset, oop new_value) {308return oop_atomic_xchg_in_heap(AccessInternal::oop_field_addr<decorators>(base, offset), new_value);309}310311// Clone barrier support312template <DecoratorSet decorators, typename BarrierSetT>313void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::clone_in_heap(oop src, oop dst, size_t size) {314if (ShenandoahCloneBarrier) {315ShenandoahBarrierSet::barrier_set()->clone_barrier_runtime(src);316}317Raw::clone(src, dst, size);318}319320template <DecoratorSet decorators, typename BarrierSetT>321template <typename T>322bool ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,323arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,324size_t length) {325ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();326bs->arraycopy_barrier(arrayOopDesc::obj_offset_to_raw(src_obj, src_offset_in_bytes, src_raw),327arrayOopDesc::obj_offset_to_raw(dst_obj, dst_offset_in_bytes, dst_raw),328length);329return Raw::oop_arraycopy_in_heap(src_obj, src_offset_in_bytes, src_raw, dst_obj, dst_offset_in_bytes, dst_raw, length);330}331332template <class T, bool HAS_FWD, bool EVAC, bool ENQUEUE>333void ShenandoahBarrierSet::arraycopy_work(T* src, size_t count) {334assert(HAS_FWD == _heap->has_forwarded_objects(), "Forwarded object status is sane");335336Thread* thread = Thread::current();337SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread);338ShenandoahMarkingContext* ctx = _heap->marking_context();339const ShenandoahCollectionSet* const cset = _heap->collection_set();340T* end = src + count;341for (T* elem_ptr = src; elem_ptr < end; elem_ptr++) {342T o = RawAccess<>::oop_load(elem_ptr);343if (!CompressedOops::is_null(o)) {344oop obj = CompressedOops::decode_not_null(o);345if (HAS_FWD && cset->is_in(obj)) {346oop fwd = resolve_forwarded_not_null(obj);347if (EVAC && obj == fwd) {348fwd = _heap->evacuate_object(obj, thread);349}350assert(obj != fwd || _heap->cancelled_gc(), "must be forwarded");351oop witness = ShenandoahHeap::cas_oop(fwd, elem_ptr, o);352obj = fwd;353}354if (ENQUEUE && !ctx->is_marked_strong(obj)) {355_satb_mark_queue_set.enqueue_known_active(queue, obj);356}357}358}359}360361template <class T>362void ShenandoahBarrierSet::arraycopy_barrier(T* src, T* dst, size_t count) {363if (count == 0) {364return;365}366int gc_state = _heap->gc_state();367if ((gc_state & ShenandoahHeap::MARKING) != 0) {368arraycopy_marking(src, dst, count);369} else if ((gc_state & ShenandoahHeap::EVACUATION) != 0) {370arraycopy_evacuation(src, count);371} else if ((gc_state & ShenandoahHeap::UPDATEREFS) != 0) {372arraycopy_update(src, count);373}374}375376template <class T>377void ShenandoahBarrierSet::arraycopy_marking(T* src, T* dst, size_t count) {378assert(_heap->is_concurrent_mark_in_progress(), "only during marking");379T* array = ShenandoahSATBBarrier ? dst : src;380if (!_heap->marking_context()->allocated_after_mark_start(reinterpret_cast<HeapWord*>(array))) {381arraycopy_work<T, false, false, true>(array, count);382}383}384385inline bool ShenandoahBarrierSet::need_bulk_update(HeapWord* ary) {386return ary < _heap->heap_region_containing(ary)->get_update_watermark();387}388389template <class T>390void ShenandoahBarrierSet::arraycopy_evacuation(T* src, size_t count) {391assert(_heap->is_evacuation_in_progress(), "only during evacuation");392if (need_bulk_update(reinterpret_cast<HeapWord*>(src))) {393ShenandoahEvacOOMScope oom_evac;394arraycopy_work<T, true, true, false>(src, count);395}396}397398template <class T>399void ShenandoahBarrierSet::arraycopy_update(T* src, size_t count) {400assert(_heap->is_update_refs_in_progress(), "only during update-refs");401if (need_bulk_update(reinterpret_cast<HeapWord*>(src))) {402arraycopy_work<T, true, false, false>(src, count);403}404}405406#endif // SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP407408409