johnc@3412: /* johnc@3412: * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. johnc@3412: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. johnc@3412: * johnc@3412: * This code is free software; you can redistribute it and/or modify it johnc@3412: * under the terms of the GNU General Public License version 2 only, as johnc@3412: * published by the Free Software Foundation. johnc@3412: * johnc@3412: * This code is distributed in the hope that it will be useful, but WITHOUT johnc@3412: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or johnc@3412: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License johnc@3412: * version 2 for more details (a copy is included in the LICENSE file that johnc@3412: * accompanied this code). johnc@3412: * johnc@3412: * You should have received a copy of the GNU General Public License version johnc@3412: * 2 along with this work; if not, write to the Free Software Foundation, johnc@3412: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. johnc@3412: * johnc@3412: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA johnc@3412: * or visit www.oracle.com if you need additional information or have any johnc@3412: * questions. johnc@3412: * johnc@3412: */ johnc@3412: johnc@3412: #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_G1EVACFAILURE_HPP johnc@3412: #define SHARE_VM_GC_IMPLEMENTATION_G1_G1EVACFAILURE_HPP johnc@3412: johnc@3412: #include "gc_implementation/g1/concurrentMark.inline.hpp" johnc@3412: #include "gc_implementation/g1/dirtyCardQueue.hpp" johnc@3412: #include "gc_implementation/g1/g1CollectedHeap.inline.hpp" johnc@3412: #include "gc_implementation/g1/g1_globals.hpp" johnc@3412: #include "gc_implementation/g1/g1OopClosures.inline.hpp" johnc@3412: #include "gc_implementation/g1/heapRegion.hpp" johnc@3412: #include "gc_implementation/g1/heapRegionRemSet.hpp" johnc@3412: #include "utilities/workgroup.hpp" johnc@3412: johnc@3412: // Closures and tasks associated with any self-forwarding pointers johnc@3412: // installed as a result of an evacuation failure. johnc@3412: johnc@3412: class UpdateRSetDeferred : public OopsInHeapRegionClosure { johnc@3412: private: johnc@3412: G1CollectedHeap* _g1; johnc@3412: DirtyCardQueue *_dcq; johnc@3412: CardTableModRefBS* _ct_bs; johnc@3412: johnc@3412: public: johnc@3412: UpdateRSetDeferred(G1CollectedHeap* g1, DirtyCardQueue* dcq) : johnc@3412: _g1(g1), _ct_bs((CardTableModRefBS*)_g1->barrier_set()), _dcq(dcq) {} johnc@3412: johnc@3412: virtual void do_oop(narrowOop* p) { do_oop_work(p); } johnc@3412: virtual void do_oop( oop* p) { do_oop_work(p); } johnc@3412: template void do_oop_work(T* p) { johnc@3412: assert(_from->is_in_reserved(p), "paranoia"); johnc@3412: if (!_from->is_in_reserved(oopDesc::load_decode_heap_oop(p)) && johnc@3412: !_from->is_survivor()) { johnc@3412: size_t card_index = _ct_bs->index_for(p); johnc@3412: if (_ct_bs->mark_card_deferred(card_index)) { johnc@3412: _dcq->enqueue((jbyte*)_ct_bs->byte_for_index(card_index)); johnc@3412: } johnc@3412: } johnc@3412: } johnc@3412: }; johnc@3412: johnc@3412: class RemoveSelfForwardPtrObjClosure: public ObjectClosure { johnc@3412: private: johnc@3412: G1CollectedHeap* _g1; johnc@3412: ConcurrentMark* _cm; johnc@3412: HeapRegion* _hr; johnc@3412: size_t _prev_marked_bytes; johnc@3412: size_t _next_marked_bytes; johnc@3412: OopsInHeapRegionClosure *_update_rset_cl; johnc@3412: public: johnc@3412: RemoveSelfForwardPtrObjClosure(G1CollectedHeap* g1, ConcurrentMark* cm, johnc@3412: HeapRegion* hr, johnc@3412: OopsInHeapRegionClosure* update_rset_cl) : johnc@3412: _g1(g1), _cm(cm), _hr(hr), johnc@3412: _update_rset_cl(update_rset_cl), johnc@3412: _prev_marked_bytes(0), _next_marked_bytes(0) {} johnc@3412: johnc@3412: size_t prev_marked_bytes() { return _prev_marked_bytes; } johnc@3412: size_t next_marked_bytes() { return _next_marked_bytes; } johnc@3412: johnc@3412: // johnc@3412: // The original idea here was to coalesce evacuated and dead objects. johnc@3412: // However that caused complications with the block offset table (BOT). johnc@3412: // In particular if there were two TLABs, one of them partially refined. johnc@3412: // |----- TLAB_1--------|----TLAB_2-~~~(partially refined part)~~~| johnc@3412: // The BOT entries of the unrefined part of TLAB_2 point to the start johnc@3412: // of TLAB_2. If the last object of the TLAB_1 and the first object johnc@3412: // of TLAB_2 are coalesced, then the cards of the unrefined part johnc@3412: // would point into middle of the filler object. johnc@3412: // The current approach is to not coalesce and leave the BOT contents intact. johnc@3412: // johnc@3412: // johnc@3412: // We now reset the BOT when we start the object iteration over the johnc@3412: // region and refine its entries for every object we come across. So johnc@3412: // the above comment is not really relevant and we should be able johnc@3412: // to coalesce dead objects if we want to. johnc@3412: void do_object(oop obj) { johnc@3412: HeapWord* obj_addr = (HeapWord*) obj; johnc@3412: assert(_hr->is_in(obj_addr), "sanity"); johnc@3412: size_t obj_size = obj->size(); johnc@3412: johnc@3412: _hr->update_bot_for_object(obj_addr, obj_size); johnc@3412: johnc@3412: if (obj->is_forwarded() && obj->forwardee() == obj) { johnc@3412: // The object failed to move. johnc@3412: assert(!_g1->is_obj_dead(obj), "We should not be preserving dead objs."); johnc@3412: _cm->markPrev(obj); johnc@3412: assert(_cm->isPrevMarked(obj), "Should be marked!"); johnc@3412: _prev_marked_bytes += (obj_size * HeapWordSize); johnc@3412: if (_g1->mark_in_progress() && !_g1->is_obj_ill(obj)) { johnc@3412: _cm->markAndGrayObjectIfNecessary(obj); johnc@3412: } johnc@3412: obj->set_mark(markOopDesc::prototype()); johnc@3412: johnc@3412: // While we were processing RSet buffers during the collection, johnc@3412: // we actually didn't scan any cards on the collection set, johnc@3412: // since we didn't want to update remembered sets with entries johnc@3412: // that point into the collection set, given that live objects johnc@3412: // from the collection set are about to move and such entries johnc@3412: // will be stale very soon. johnc@3412: // This change also dealt with a reliability issue which johnc@3412: // involved scanning a card in the collection set and coming johnc@3412: // across an array that was being chunked and looking malformed. johnc@3412: // The problem is that, if evacuation fails, we might have johnc@3412: // remembered set entries missing given that we skipped cards on johnc@3412: // the collection set. So, we'll recreate such entries now. johnc@3412: johnc@3412: obj->oop_iterate(_update_rset_cl); johnc@3412: assert(_cm->isPrevMarked(obj), "Should be marked!"); johnc@3412: } else { johnc@3412: // The object has been either evacuated or is dead. Fill it with a johnc@3412: // dummy object. johnc@3412: MemRegion mr((HeapWord*)obj, obj_size); johnc@3412: CollectedHeap::fill_with_object(mr); johnc@3412: _cm->clearRangeBothMaps(mr); johnc@3412: } johnc@3412: } johnc@3412: }; johnc@3412: johnc@3412: class RemoveSelfForwardPtrHRClosure: public HeapRegionClosure { johnc@3412: G1CollectedHeap* _g1h; johnc@3412: ConcurrentMark* _cm; johnc@3412: OopsInHeapRegionClosure *_update_rset_cl; johnc@3412: johnc@3412: public: johnc@3412: RemoveSelfForwardPtrHRClosure(G1CollectedHeap* g1h, johnc@3412: OopsInHeapRegionClosure* update_rset_cl) : johnc@3412: _g1h(g1h), _update_rset_cl(update_rset_cl), johnc@3412: _cm(_g1h->concurrent_mark()) { } johnc@3412: johnc@3412: bool doHeapRegion(HeapRegion *hr) { johnc@3412: assert(!hr->isHumongous(), "sanity"); johnc@3412: assert(hr->in_collection_set(), "bad CS"); johnc@3412: johnc@3412: if (hr->claimHeapRegion(HeapRegion::ParEvacFailureClaimValue)) { johnc@3412: if (hr->evacuation_failed()) { johnc@3412: RemoveSelfForwardPtrObjClosure rspc(_g1h, _cm, hr, _update_rset_cl); johnc@3412: johnc@3412: // In the common case (i.e. when there is no evacuation johnc@3412: // failure) we make sure that the following is done when johnc@3412: // the region is freed so that it is "ready-to-go" when it's johnc@3412: // re-allocated. However, when evacuation failure happens, a johnc@3412: // region will remain in the heap and might ultimately be added johnc@3412: // to a CSet in the future. So we have to be careful here and johnc@3412: // make sure the region's RSet is ready for parallel iteration johnc@3412: // whenever this might be required in the future. johnc@3412: hr->rem_set()->reset_for_par_iteration(); johnc@3412: hr->reset_bot(); johnc@3412: _update_rset_cl->set_region(hr); johnc@3412: hr->object_iterate(&rspc); johnc@3412: johnc@3412: // A number of manipulations to make the TAMS for this region johnc@3412: // be the current top, and the marked bytes be the ones observed johnc@3412: // in the iteration. johnc@3412: if (_cm->at_least_one_mark_complete()) { johnc@3412: // The comments below are the postconditions achieved by the johnc@3412: // calls. Note especially the last such condition, which says that johnc@3412: // the count of marked bytes has been properly restored. johnc@3412: hr->note_start_of_marking(false); johnc@3412: // _next_top_at_mark_start == top, _next_marked_bytes == 0 johnc@3412: hr->add_to_marked_bytes(rspc.prev_marked_bytes()); johnc@3412: // _next_marked_bytes == prev_marked_bytes. johnc@3412: hr->note_end_of_marking(); johnc@3412: // _prev_top_at_mark_start == top(), johnc@3412: // _prev_marked_bytes == prev_marked_bytes johnc@3412: } johnc@3412: // If there is no mark in progress, we modified the _next variables johnc@3412: // above needlessly, but harmlessly. johnc@3412: if (_g1h->mark_in_progress()) { johnc@3412: hr->note_start_of_marking(false); johnc@3412: // _next_top_at_mark_start == top, _next_marked_bytes == 0 johnc@3412: // _next_marked_bytes == next_marked_bytes. johnc@3412: } johnc@3412: } johnc@3412: } johnc@3412: return false; johnc@3412: } johnc@3412: }; johnc@3412: johnc@3412: class G1ParRemoveSelfForwardPtrsTask: public AbstractGangTask { johnc@3412: protected: johnc@3412: G1CollectedHeap* _g1h; johnc@3412: johnc@3412: public: johnc@3412: G1ParRemoveSelfForwardPtrsTask(G1CollectedHeap* g1h) : johnc@3412: AbstractGangTask("G1 Remove Self-forwarding Pointers"), johnc@3412: _g1h(g1h) { } johnc@3412: johnc@3412: void work(uint worker_id) { johnc@3412: UpdateRSetImmediate immediate_update(_g1h->g1_rem_set()); johnc@3412: DirtyCardQueue dcq(&_g1h->dirty_card_queue_set()); johnc@3412: UpdateRSetDeferred deferred_update(_g1h, &dcq); johnc@3412: johnc@3412: OopsInHeapRegionClosure *update_rset_cl = &deferred_update; johnc@3412: if (!G1DeferredRSUpdate) { johnc@3412: update_rset_cl = &immediate_update; johnc@3412: } johnc@3412: johnc@3412: RemoveSelfForwardPtrHRClosure rsfp_cl(_g1h, update_rset_cl); johnc@3412: johnc@3412: HeapRegion* hr = _g1h->start_cset_region_for_worker(worker_id); johnc@3412: _g1h->collection_set_iterate_from(hr, &rsfp_cl); johnc@3412: } johnc@3412: }; johnc@3412: johnc@3412: #endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1EVACFAILURE_HPP