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