Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/gc_implementation/shenandoah/shenandoahHeap.inline.hpp
38920 views
/*1* Copyright (c) 2015, 2020, Red Hat, Inc. All rights reserved.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation.6*7* This code is distributed in the hope that it will be useful, but WITHOUT8* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or9* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License10* version 2 for more details (a copy is included in the LICENSE file that11* accompanied this code).12*13* You should have received a copy of the GNU General Public License version14* 2 along with this work; if not, write to the Free Software Foundation,15* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.16*17* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA18* or visit www.oracle.com if you need additional information or have any19* questions.20*21*/2223#ifndef SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEAP_INLINE_HPP24#define SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEAP_INLINE_HPP2526#include "gc_implementation/shared/markBitMap.inline.hpp"27#include "memory/threadLocalAllocBuffer.inline.hpp"28#include "gc_implementation/shenandoah/shenandoahAsserts.hpp"29#include "gc_implementation/shenandoah/shenandoahBarrierSet.inline.hpp"30#include "gc_implementation/shenandoah/shenandoahCollectionSet.hpp"31#include "gc_implementation/shenandoah/shenandoahCollectionSet.inline.hpp"32#include "gc_implementation/shenandoah/shenandoahForwarding.inline.hpp"33#include "gc_implementation/shenandoah/shenandoahControlThread.hpp"34#include "gc_implementation/shenandoah/shenandoahMarkingContext.inline.hpp"35#include "gc_implementation/shenandoah/shenandoahHeap.hpp"36#include "gc_implementation/shenandoah/shenandoahHeapRegionSet.hpp"37#include "gc_implementation/shenandoah/shenandoahHeapRegion.inline.hpp"38#include "oops/oop.inline.hpp"39#include "runtime/atomic.hpp"40#include "runtime/prefetch.hpp"41#include "runtime/prefetch.inline.hpp"42#include "utilities/copy.hpp"43#include "utilities/globalDefinitions.hpp"4445inline ShenandoahHeap* ShenandoahHeap::heap() {46assert(_heap != NULL, "Heap is not initialized yet");47return _heap;48}4950inline ShenandoahHeapRegion* ShenandoahRegionIterator::next() {51size_t new_index = Atomic::add((size_t) 1, &_index);52// get_region() provides the bounds-check and returns NULL on OOB.53return _heap->get_region(new_index - 1);54}5556inline bool ShenandoahHeap::has_forwarded_objects() const {57return _gc_state.is_set(HAS_FORWARDED);58}5960inline ShenandoahWorkGang* ShenandoahHeap::workers() const {61return (ShenandoahWorkGang*)_workers;62}6364inline size_t ShenandoahHeap::heap_region_index_containing(const void* addr) const {65uintptr_t region_start = ((uintptr_t) addr);66uintptr_t index = (region_start - (uintptr_t) base()) >> ShenandoahHeapRegion::region_size_bytes_shift();67assert(index < num_regions(), err_msg("Region index is in bounds: " PTR_FORMAT, p2i(addr)));68return index;69}7071inline ShenandoahHeapRegion* const ShenandoahHeap::heap_region_containing(const void* addr) const {72size_t index = heap_region_index_containing(addr);73ShenandoahHeapRegion* const result = get_region(index);74assert(addr >= result->bottom() && addr < result->end(), err_msg("Heap region contains the address: " PTR_FORMAT, p2i(addr)));75return result;76}7778template <class T>79inline oop ShenandoahHeap::update_with_forwarded_not_null(T* p, oop obj) {80if (in_collection_set(obj)) {81shenandoah_assert_forwarded_except(p, obj, is_full_gc_in_progress() || cancelled_gc() || is_degenerated_gc_in_progress());82obj = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);83oopDesc::encode_store_heap_oop(p, obj);84}85#ifdef ASSERT86else {87shenandoah_assert_not_forwarded(p, obj);88}89#endif90return obj;91}9293template <class T>94inline oop ShenandoahHeap::maybe_update_with_forwarded(T* p) {95T o = oopDesc::load_heap_oop(p);96if (! oopDesc::is_null(o)) {97oop obj = oopDesc::decode_heap_oop_not_null(o);98return maybe_update_with_forwarded_not_null(p, obj);99} else {100return NULL;101}102}103104template <class T>105inline oop ShenandoahHeap::evac_update_with_forwarded(T* p) {106T o = oopDesc::load_heap_oop(p);107if (!oopDesc::is_null(o)) {108oop heap_oop = oopDesc::decode_heap_oop_not_null(o);109if (in_collection_set(heap_oop)) {110oop forwarded_oop = ShenandoahBarrierSet::resolve_forwarded_not_null(heap_oop);111if (forwarded_oop == heap_oop) {112forwarded_oop = evacuate_object(heap_oop, Thread::current());113}114oop prev = cas_oop(forwarded_oop, p, heap_oop);115if (prev == heap_oop) {116return forwarded_oop;117} else {118return NULL;119}120}121return heap_oop;122} else {123return NULL;124}125}126127inline oop ShenandoahHeap::cas_oop(oop n, oop* addr, oop c) {128assert(is_ptr_aligned(addr, sizeof(narrowOop)), err_msg("Address should be aligned: " PTR_FORMAT, p2i(addr)));129return (oop) Atomic::cmpxchg_ptr(n, addr, c);130}131132inline oop ShenandoahHeap::cas_oop(oop n, narrowOop* addr, narrowOop c) {133narrowOop val = oopDesc::encode_heap_oop(n);134return oopDesc::decode_heap_oop((narrowOop) Atomic::cmpxchg(val, addr, c));135}136137inline oop ShenandoahHeap::cas_oop(oop n, narrowOop* addr, oop c) {138assert(is_ptr_aligned(addr, sizeof(narrowOop)), err_msg("Address should be aligned: " PTR_FORMAT, p2i(addr)));139narrowOop cmp = oopDesc::encode_heap_oop(c);140narrowOop val = oopDesc::encode_heap_oop(n);141return oopDesc::decode_heap_oop((narrowOop) Atomic::cmpxchg(val, addr, cmp));142}143144template <class T>145inline oop ShenandoahHeap::maybe_update_with_forwarded_not_null(T* p, oop heap_oop) {146shenandoah_assert_not_in_cset_loc_except(p, !is_in(p) || is_full_gc_in_progress() || is_degenerated_gc_in_progress());147shenandoah_assert_correct(p, heap_oop);148149if (in_collection_set(heap_oop)) {150oop forwarded_oop = ShenandoahBarrierSet::resolve_forwarded_not_null(heap_oop);151152shenandoah_assert_forwarded_except(p, heap_oop, is_full_gc_in_progress() || is_degenerated_gc_in_progress());153shenandoah_assert_not_forwarded(p, forwarded_oop);154shenandoah_assert_not_in_cset_except(p, forwarded_oop, cancelled_gc());155156// If this fails, another thread wrote to p before us, it will be logged in SATB and the157// reference be updated later.158oop witness = cas_oop(forwarded_oop, p, heap_oop);159160if (witness != heap_oop) {161// CAS failed, someone had beat us to it. Normally, we would return the failure witness,162// because that would be the proper write of to-space object, enforced by strong barriers.163// However, there is a corner case with arraycopy. It can happen that a Java thread164// beats us with an arraycopy, which first copies the array, which potentially contains165// from-space refs, and only afterwards updates all from-space refs to to-space refs,166// which leaves a short window where the new array elements can be from-space.167// In this case, we can just resolve the result again. As we resolve, we need to consider168// the contended write might have been NULL.169oop result = ShenandoahBarrierSet::resolve_forwarded(witness);170shenandoah_assert_not_forwarded_except(p, result, (result == NULL));171shenandoah_assert_not_in_cset_except(p, result, (result == NULL) || cancelled_gc());172return result;173} else {174// Success! We have updated with known to-space copy. We have already asserted it is sane.175return forwarded_oop;176}177} else {178shenandoah_assert_not_forwarded(p, heap_oop);179return heap_oop;180}181}182183inline bool ShenandoahHeap::cancelled_gc() const {184return _cancelled_gc.is_set();185}186187inline bool ShenandoahHeap::try_cancel_gc() {188return _cancelled_gc.try_set();189}190191inline void ShenandoahHeap::clear_cancelled_gc() {192_cancelled_gc.unset();193_oom_evac_handler.clear();194}195196inline HeapWord* ShenandoahHeap::allocate_from_gclab(Thread* thread, size_t size) {197assert(UseTLAB, "TLABs should be enabled");198199if (!thread->gclab().is_initialized()) {200assert(!thread->is_Java_thread() && !thread->is_Worker_thread(),201err_msg("Performance: thread should have GCLAB: %s", thread->name()));202// No GCLABs in this thread, fallback to shared allocation203return NULL;204}205HeapWord *obj = thread->gclab().allocate(size);206if (obj != NULL) {207return obj;208}209// Otherwise...210return allocate_from_gclab_slow(thread, size);211}212213inline oop ShenandoahHeap::evacuate_object(oop p, Thread* thread) {214if (Thread::current()->is_oom_during_evac()) {215// This thread went through the OOM during evac protocol and it is safe to return216// the forward pointer. It must not attempt to evacuate any more.217return ShenandoahBarrierSet::resolve_forwarded(p);218}219220assert(thread->is_evac_allowed(), "must be enclosed in in oom-evac scope");221222size_t size = p->size();223224assert(!heap_region_containing(p)->is_humongous(), "never evacuate humongous objects");225226bool alloc_from_gclab = true;227HeapWord* copy = NULL;228229#ifdef ASSERT230if (ShenandoahOOMDuringEvacALot &&231(os::random() & 1) == 0) { // Simulate OOM every ~2nd slow-path call232copy = NULL;233} else {234#endif235if (UseTLAB) {236copy = allocate_from_gclab(thread, size);237}238if (copy == NULL) {239ShenandoahAllocRequest req = ShenandoahAllocRequest::for_shared_gc(size);240copy = allocate_memory(req);241alloc_from_gclab = false;242}243#ifdef ASSERT244}245#endif246247if (copy == NULL) {248control_thread()->handle_alloc_failure_evac(size);249250_oom_evac_handler.handle_out_of_memory_during_evacuation();251252return ShenandoahBarrierSet::resolve_forwarded(p);253}254255// Copy the object:256Copy::aligned_disjoint_words((HeapWord*) p, copy, size);257258// Try to install the new forwarding pointer.259oop copy_val = oop(copy);260oop result = ShenandoahForwarding::try_update_forwardee(p, copy_val);261if (result == copy_val) {262// Successfully evacuated. Our copy is now the public one!263shenandoah_assert_correct(NULL, copy_val);264return copy_val;265} else {266// Failed to evacuate. We need to deal with the object that is left behind. Since this267// new allocation is certainly after TAMS, it will be considered live in the next cycle.268// But if it happens to contain references to evacuated regions, those references would269// not get updated for this stale copy during this cycle, and we will crash while scanning270// it the next cycle.271//272// For GCLAB allocations, it is enough to rollback the allocation ptr. Either the next273// object will overwrite this stale copy, or the filler object on LAB retirement will274// do this. For non-GCLAB allocations, we have no way to retract the allocation, and275// have to explicitly overwrite the copy with the filler object. With that overwrite,276// we have to keep the fwdptr initialized and pointing to our (stale) copy.277if (alloc_from_gclab) {278thread->gclab().rollback(size);279} else {280fill_with_object(copy, size);281shenandoah_assert_correct(NULL, copy_val);282}283shenandoah_assert_correct(NULL, result);284return result;285}286}287288inline bool ShenandoahHeap::requires_marking(const void* entry) const {289return !_marking_context->is_marked(oop(entry));290}291292inline bool ShenandoahHeap::in_collection_set(oop p) const {293assert(collection_set() != NULL, "Sanity");294return collection_set()->is_in(p);295}296297inline bool ShenandoahHeap::in_collection_set_loc(void* p) const {298assert(collection_set() != NULL, "Sanity");299return collection_set()->is_in_loc(p);300}301302inline bool ShenandoahHeap::is_stable() const {303return _gc_state.is_clear();304}305306inline bool ShenandoahHeap::is_idle() const {307return _gc_state.is_unset(MARKING | EVACUATION | UPDATEREFS);308}309310inline bool ShenandoahHeap::is_concurrent_mark_in_progress() const {311return _gc_state.is_set(MARKING);312}313314inline bool ShenandoahHeap::is_evacuation_in_progress() const {315return _gc_state.is_set(EVACUATION);316}317318inline bool ShenandoahHeap::is_gc_in_progress_mask(uint mask) const {319return _gc_state.is_set(mask);320}321322inline bool ShenandoahHeap::is_degenerated_gc_in_progress() const {323return _degenerated_gc_in_progress.is_set();324}325326inline bool ShenandoahHeap::is_full_gc_in_progress() const {327return _full_gc_in_progress.is_set();328}329330inline bool ShenandoahHeap::is_full_gc_move_in_progress() const {331return _full_gc_move_in_progress.is_set();332}333334inline bool ShenandoahHeap::is_update_refs_in_progress() const {335return _gc_state.is_set(UPDATEREFS);336}337338template<class T>339inline void ShenandoahHeap::marked_object_iterate(ShenandoahHeapRegion* region, T* cl) {340marked_object_iterate(region, cl, region->top());341}342343template<class T>344inline void ShenandoahHeap::marked_object_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* limit) {345assert(! region->is_humongous_continuation(), "no humongous continuation regions here");346347ShenandoahMarkingContext* const ctx = complete_marking_context();348assert(ctx->is_complete(), "sanity");349350MarkBitMap* mark_bit_map = ctx->mark_bit_map();351HeapWord* tams = ctx->top_at_mark_start(region);352353size_t skip_bitmap_delta = 1;354HeapWord* start = region->bottom();355HeapWord* end = MIN2(tams, region->end());356357// Step 1. Scan below the TAMS based on bitmap data.358HeapWord* limit_bitmap = MIN2(limit, tams);359360// Try to scan the initial candidate. If the candidate is above the TAMS, it would361// fail the subsequent "< limit_bitmap" checks, and fall through to Step 2.362HeapWord* cb = mark_bit_map->getNextMarkedWordAddress(start, end);363364intx dist = ShenandoahMarkScanPrefetch;365if (dist > 0) {366// Batched scan that prefetches the oop data, anticipating the access to367// either header, oop field, or forwarding pointer. Not that we cannot368// touch anything in oop, while it still being prefetched to get enough369// time for prefetch to work. This is why we try to scan the bitmap linearly,370// disregarding the object size. However, since we know forwarding pointer371// preceeds the object, we can skip over it. Once we cannot trust the bitmap,372// there is no point for prefetching the oop contents, as oop->size() will373// touch it prematurely.374375// No variable-length arrays in standard C++, have enough slots to fit376// the prefetch distance.377static const int SLOT_COUNT = 256;378guarantee(dist <= SLOT_COUNT, "adjust slot count");379HeapWord* slots[SLOT_COUNT];380381int avail;382do {383avail = 0;384for (int c = 0; (c < dist) && (cb < limit_bitmap); c++) {385Prefetch::read(cb, oopDesc::mark_offset_in_bytes());386slots[avail++] = cb;387cb += skip_bitmap_delta;388if (cb < limit_bitmap) {389cb = mark_bit_map->getNextMarkedWordAddress(cb, limit_bitmap);390}391}392393for (int c = 0; c < avail; c++) {394assert (slots[c] < tams, err_msg("only objects below TAMS here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(slots[c]), p2i(tams)));395assert (slots[c] < limit, err_msg("only objects below limit here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(slots[c]), p2i(limit)));396oop obj = oop(slots[c]);397assert(!oopDesc::is_null(obj), "sanity");398assert(obj->is_oop(), "sanity");399assert(_marking_context->is_marked(obj), "object expected to be marked");400cl->do_object(obj);401}402} while (avail > 0);403} else {404while (cb < limit_bitmap) {405assert (cb < tams, err_msg("only objects below TAMS here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(cb), p2i(tams)));406assert (cb < limit, err_msg("only objects below limit here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(cb), p2i(limit)));407oop obj = oop(cb);408assert(!oopDesc::is_null(obj), "sanity");409assert(obj->is_oop(), "sanity");410assert(_marking_context->is_marked(obj), "object expected to be marked");411cl->do_object(obj);412cb += skip_bitmap_delta;413if (cb < limit_bitmap) {414cb = mark_bit_map->getNextMarkedWordAddress(cb, limit_bitmap);415}416}417}418419// Step 2. Accurate size-based traversal, happens past the TAMS.420// This restarts the scan at TAMS, which makes sure we traverse all objects,421// regardless of what happened at Step 1.422HeapWord* cs = tams;423while (cs < limit) {424assert (cs >= tams, err_msg("only objects past TAMS here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(cs), p2i(tams)));425assert (cs < limit, err_msg("only objects below limit here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(cs), p2i(limit)));426oop obj = oop(cs);427int size = obj->size();428assert(!oopDesc::is_null(obj), "sanity");429assert(obj->is_oop(), "sanity");430assert(_marking_context->is_marked(obj), "object expected to be marked");431cl->do_object(obj);432cs += size;433}434}435436template <class T>437class ShenandoahObjectToOopClosure : public ObjectClosure {438T* _cl;439public:440ShenandoahObjectToOopClosure(T* cl) : _cl(cl) {}441442void do_object(oop obj) {443obj->oop_iterate(_cl);444}445};446447template <class T>448class ShenandoahObjectToOopBoundedClosure : public ObjectClosure {449T* _cl;450MemRegion _bounds;451public:452ShenandoahObjectToOopBoundedClosure(T* cl, HeapWord* bottom, HeapWord* top) :453_cl(cl), _bounds(bottom, top) {}454455void do_object(oop obj) {456obj->oop_iterate(_cl, _bounds);457}458};459460template<class T>461inline void ShenandoahHeap::marked_object_oop_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* top) {462if (region->is_humongous()) {463HeapWord* bottom = region->bottom();464if (top > bottom) {465region = region->humongous_start_region();466ShenandoahObjectToOopBoundedClosure<T> objs(cl, bottom, top);467marked_object_iterate(region, &objs);468}469} else {470ShenandoahObjectToOopClosure<T> objs(cl);471marked_object_iterate(region, &objs, top);472}473}474475inline ShenandoahHeapRegion* const ShenandoahHeap::get_region(size_t region_idx) const {476if (region_idx < _num_regions) {477return _regions[region_idx];478} else {479return NULL;480}481}482483inline void ShenandoahHeap::mark_complete_marking_context() {484_marking_context->mark_complete();485}486487inline void ShenandoahHeap::mark_incomplete_marking_context() {488_marking_context->mark_incomplete();489}490491inline ShenandoahMarkingContext* ShenandoahHeap::complete_marking_context() const {492assert (_marking_context->is_complete()," sanity");493return _marking_context;494}495496inline ShenandoahMarkingContext* ShenandoahHeap::marking_context() const {497return _marking_context;498}499500#endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEAP_INLINE_HPP501502503