1 /* 2 * Copyright (c) 2012, 2016, Oracle and/or its affiliates. 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 #include "precompiled.hpp" 26 #include "gc/g1/dirtyCardQueue.hpp" 27 #include "gc/g1/g1CollectedHeap.inline.hpp" 28 #include "gc/g1/g1CollectorState.hpp" 29 #include "gc/g1/g1ConcurrentMark.inline.hpp" 30 #include "gc/g1/g1EvacFailure.hpp" 31 #include "gc/g1/g1HeapVerifier.hpp" 32 #include "gc/g1/g1OopClosures.inline.hpp" 33 #include "gc/g1/g1_globals.hpp" 34 #include "gc/g1/heapRegion.hpp" 35 #include "gc/g1/heapRegionRemSet.hpp" 36 #include "gc/shared/preservedMarks.inline.hpp" 37 38 class UpdateRSetDeferred : public ExtendedOopClosure { 39 private: 40 G1CollectedHeap* _g1; 41 DirtyCardQueue *_dcq; 42 G1SATBCardTableModRefBS* _ct_bs; 43 HeapRegion* _from; 44 45 public: 46 UpdateRSetDeferred(DirtyCardQueue* dcq) : 47 _g1(G1CollectedHeap::heap()), _ct_bs(_g1->g1_barrier_set()), _dcq(dcq), _from(NULL) {} 48 49 virtual void do_oop(narrowOop* p) { do_oop_work(p); } 50 virtual void do_oop( oop* p) { do_oop_work(p); } 51 template <class T> void do_oop_work(T* p) { 52 assert(_from->is_in_reserved(p), "paranoia"); 53 assert(!_from->is_survivor(), "Unexpected evac failure in survivor region"); 54 55 if (!_from->is_in_reserved(oopDesc::load_decode_heap_oop(p))) { 56 size_t card_index = _ct_bs->index_for(p); 57 if (_ct_bs->mark_card_deferred(card_index)) { 58 _dcq->enqueue((jbyte*)_ct_bs->byte_for_index(card_index)); 59 } 60 } 61 } 62 63 void set_region(HeapRegion* from) { _from = from; } 64 }; 65 66 class RemoveSelfForwardPtrObjClosure: public ObjectClosure { 67 private: 68 G1CollectedHeap* _g1; 69 G1ConcurrentMark* _cm; 70 HeapRegion* _hr; 71 size_t _marked_bytes; 72 UpdateRSetDeferred* _update_rset_cl; 73 bool _during_initial_mark; 74 uint _worker_id; 75 HeapWord* _last_forwarded_object_end; 76 77 public: 78 RemoveSelfForwardPtrObjClosure(HeapRegion* hr, 79 UpdateRSetDeferred* update_rset_cl, 80 bool during_initial_mark, 81 uint worker_id) : 82 _g1(G1CollectedHeap::heap()), 83 _cm(_g1->concurrent_mark()), 84 _hr(hr), 85 _marked_bytes(0), 86 _update_rset_cl(update_rset_cl), 87 _during_initial_mark(during_initial_mark), 88 _worker_id(worker_id), 89 _last_forwarded_object_end(hr->bottom()) { } 90 91 size_t marked_bytes() { return _marked_bytes; } 92 93 // Iterate over the live objects in the region to find self-forwarded objects 94 // that need to be kept live. We need to update the remembered sets of these 95 // objects. Further update the BOT and marks. 96 // We can coalesce and overwrite the remaining heap contents with dummy objects 97 // as they have either been dead or evacuated (which are unreferenced now, i.e. 98 // dead too) already. 99 void do_object(oop obj) { 100 HeapWord* obj_addr = (HeapWord*) obj; 101 assert(_hr->is_in(obj_addr), "sanity"); 102 103 if (obj->is_forwarded() && obj->forwardee() == obj) { 104 // The object failed to move. 105 106 zap_dead_objects(_last_forwarded_object_end, obj_addr); 107 // We consider all objects that we find self-forwarded to be 108 // live. What we'll do is that we'll update the prev marking 109 // info so that they are all under PTAMS and explicitly marked. 110 if (!_cm->isPrevMarked(obj)) { 111 _cm->markPrev(obj); 112 } 113 if (_during_initial_mark) { 114 // For the next marking info we'll only mark the 115 // self-forwarded objects explicitly if we are during 116 // initial-mark (since, normally, we only mark objects pointed 117 // to by roots if we succeed in copying them). By marking all 118 // self-forwarded objects we ensure that we mark any that are 119 // still pointed to be roots. During concurrent marking, and 120 // after initial-mark, we don't need to mark any objects 121 // explicitly and all objects in the CSet are considered 122 // (implicitly) live. So, we won't mark them explicitly and 123 // we'll leave them over NTAMS. 124 _cm->grayRoot(obj, _hr); 125 } 126 size_t obj_size = obj->size(); 127 128 _marked_bytes += (obj_size * HeapWordSize); 129 PreservedMarks::init_forwarded_mark(obj); 130 131 // While we were processing RSet buffers during the collection, 132 // we actually didn't scan any cards on the collection set, 133 // since we didn't want to update remembered sets with entries 134 // that point into the collection set, given that live objects 135 // from the collection set are about to move and such entries 136 // will be stale very soon. 137 // This change also dealt with a reliability issue which 138 // involved scanning a card in the collection set and coming 139 // across an array that was being chunked and looking malformed. 140 // The problem is that, if evacuation fails, we might have 141 // remembered set entries missing given that we skipped cards on 142 // the collection set. So, we'll recreate such entries now. 143 obj->oop_iterate(_update_rset_cl); 144 145 HeapWord* obj_end = obj_addr + obj_size; 146 _last_forwarded_object_end = obj_end; 147 _hr->cross_threshold(obj_addr, obj_end); 148 } 149 } 150 151 // Fill the memory area from start to end with filler objects, and update the BOT 152 // and the mark bitmap accordingly. 153 void zap_dead_objects(HeapWord* start, HeapWord* end) { 154 if (start == end) { 155 return; 156 } 157 158 size_t gap_size = pointer_delta(end, start); 159 MemRegion mr(start, gap_size); 160 if (gap_size >= CollectedHeap::min_fill_size()) { 161 CollectedHeap::fill_with_objects(start, gap_size); 162 163 HeapWord* end_first_obj = start + ((oop)start)->size(); 164 _hr->cross_threshold(start, end_first_obj); 165 // Fill_with_objects() may have created multiple (i.e. two) 166 // objects, as the max_fill_size() is half a region. 167 // After updating the BOT for the first object, also update the 168 // BOT for the second object to make the BOT complete. 169 if (end_first_obj != end) { 170 _hr->cross_threshold(end_first_obj, end); 171 #ifdef ASSERT 172 size_t size_second_obj = ((oop)end_first_obj)->size(); 173 HeapWord* end_of_second_obj = end_first_obj + size_second_obj; 174 assert(end == end_of_second_obj, 175 "More than two objects were used to fill the area from " PTR_FORMAT " to " PTR_FORMAT ", " 176 "second objects size " SIZE_FORMAT " ends at " PTR_FORMAT, 177 p2i(start), p2i(end), size_second_obj, p2i(end_of_second_obj)); 178 #endif 179 } 180 } 181 _cm->clearRangePrevBitmap(mr); 182 } 183 184 void zap_remainder() { 185 zap_dead_objects(_last_forwarded_object_end, _hr->top()); 186 } 187 }; 188 189 class RemoveSelfForwardPtrHRClosure: public HeapRegionClosure { 190 G1CollectedHeap* _g1h; 191 uint _worker_id; 192 HeapRegionClaimer* _hrclaimer; 193 194 DirtyCardQueue _dcq; 195 UpdateRSetDeferred _update_rset_cl; 196 197 public: 198 RemoveSelfForwardPtrHRClosure(uint worker_id, 199 HeapRegionClaimer* hrclaimer) : 200 _g1h(G1CollectedHeap::heap()), 201 _dcq(&_g1h->dirty_card_queue_set()), 202 _update_rset_cl(&_dcq), 203 _worker_id(worker_id), 204 _hrclaimer(hrclaimer) { 205 } 206 207 size_t remove_self_forward_ptr_by_walking_hr(HeapRegion* hr, 208 bool during_initial_mark) { 209 RemoveSelfForwardPtrObjClosure rspc(hr, 210 &_update_rset_cl, 211 during_initial_mark, 212 _worker_id); 213 _update_rset_cl.set_region(hr); 214 hr->object_iterate(&rspc); 215 // Need to zap the remainder area of the processed region. 216 rspc.zap_remainder(); 217 218 return rspc.marked_bytes(); 219 } 220 221 bool doHeapRegion(HeapRegion *hr) { 222 assert(!hr->is_pinned(), "Unexpected pinned region at index %u", hr->hrm_index()); 223 assert(hr->in_collection_set(), "bad CS"); 224 225 if (_hrclaimer->claim_region(hr->hrm_index())) { 226 if (hr->evacuation_failed()) { 227 bool during_initial_mark = _g1h->collector_state()->during_initial_mark_pause(); 228 bool during_conc_mark = _g1h->collector_state()->mark_in_progress(); 229 230 hr->note_self_forwarding_removal_start(during_initial_mark, 231 during_conc_mark); 232 _g1h->verifier()->check_bitmaps("Self-Forwarding Ptr Removal", hr); 233 234 hr->reset_bot(); 235 236 size_t live_bytes = remove_self_forward_ptr_by_walking_hr(hr, during_initial_mark); 237 238 hr->rem_set()->clean_strong_code_roots(hr); 239 240 hr->note_self_forwarding_removal_end(live_bytes); 241 } 242 } 243 return false; 244 } 245 }; 246 247 G1ParRemoveSelfForwardPtrsTask::G1ParRemoveSelfForwardPtrsTask() : 248 AbstractGangTask("G1 Remove Self-forwarding Pointers"), 249 _g1h(G1CollectedHeap::heap()), 250 _hrclaimer(_g1h->workers()->active_workers()) { } 251 252 void G1ParRemoveSelfForwardPtrsTask::work(uint worker_id) { 253 RemoveSelfForwardPtrHRClosure rsfp_cl(worker_id, &_hrclaimer); 254 255 _g1h->collection_set_iterate_from(&rsfp_cl, worker_id); 256 }