1 /*
  2  * Copyright (c) 2012, 2021, 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/g1EvacFailure.hpp"
 30 #include "gc/g1/g1EvacFailureRegions.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.inline.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 RemoveSelfForwardPtrObjClosure: public ObjectClosure {
 41   G1CollectedHeap* _g1h;
 42   G1ConcurrentMark* _cm;
 43   HeapRegion* _hr;
 44   size_t _marked_words;
 45   bool _during_concurrent_start;
 46   uint _worker_id;
 47   HeapWord* _last_forwarded_object_end;
 48 
 49 public:
 50   RemoveSelfForwardPtrObjClosure(HeapRegion* hr,
 51                                  bool during_concurrent_start,
 52                                  uint worker_id) :
 53     _g1h(G1CollectedHeap::heap()),
 54     _cm(_g1h->concurrent_mark()),
 55     _hr(hr),
 56     _marked_words(0),
 57     _during_concurrent_start(during_concurrent_start),
 58     _worker_id(worker_id),
 59     _last_forwarded_object_end(hr->bottom()) { }
 60 
 61   size_t marked_bytes() { return _marked_words * HeapWordSize; }
 62 
 63   // Iterate over the live objects in the region to find self-forwarded objects
 64   // that need to be kept live. We need to update the remembered sets of these
 65   // objects. Further update the BOT and marks.
 66   // We can coalesce and overwrite the remaining heap contents with dummy objects
 67   // as they have either been dead or evacuated (which are unreferenced now, i.e.
 68   // dead too) already.
 69   void do_object(oop obj) {
 70     HeapWord* obj_addr = cast_from_oop<HeapWord*>(obj);
 71     assert(_last_forwarded_object_end <= obj_addr, "should iterate in ascending address order");
 72     assert(_hr->is_in(obj_addr), "sanity");
 73 
 74     // The object failed to move.
 75     assert(obj->is_forwarded() && obj->forwardee() == obj, "sanity");
 76 
 77     zap_dead_objects(_last_forwarded_object_end, obj_addr);
 78     // We consider all objects that we find self-forwarded to be
 79     // live. What we'll do is that we'll update the prev marking
 80     // info so that they are all under PTAMS and explicitly marked.
 81     if (!_cm->is_marked_in_prev_bitmap(obj)) {
 82       _cm->mark_in_prev_bitmap(obj);
 83     }
 84     if (_during_concurrent_start) {
 85       // For the next marking info we'll only mark the
 86       // self-forwarded objects explicitly if we are during
 87       // concurrent start (since, normally, we only mark objects pointed
 88       // to by roots if we succeed in copying them). By marking all
 89       // self-forwarded objects we ensure that we mark any that are
 90       // still pointed to be roots. During concurrent marking, and
 91       // after concurrent start, we don't need to mark any objects
 92       // explicitly and all objects in the CSet are considered
 93       // (implicitly) live. So, we won't mark them explicitly and
 94       // we'll leave them over NTAMS.
 95       _cm->mark_in_next_bitmap(_worker_id, _hr, obj);
 96     }
 97     size_t obj_size = obj->size();
 98 
 99     _marked_words += obj_size;
100     PreservedMarks::init_forwarded_mark(obj);
101 
102     HeapWord* obj_end = obj_addr + obj_size;
103     _last_forwarded_object_end = obj_end;
104     _hr->alloc_block_in_bot(obj_addr, obj_end);
105   }
106 
107   // Fill the memory area from start to end with filler objects, and update the BOT
108   // and the mark bitmap accordingly.
109   void zap_dead_objects(HeapWord* start, HeapWord* end) {
110     if (start == end) {
111       return;
112     }
113 
114     size_t gap_size = pointer_delta(end, start);
115     MemRegion mr(start, gap_size);
116     if (gap_size >= CollectedHeap::min_fill_size()) {
117       CollectedHeap::fill_with_objects(start, gap_size);
118 
119       HeapWord* end_first_obj = start + cast_to_oop(start)->size();
120       _hr->alloc_block_in_bot(start, end_first_obj);
121       // Fill_with_objects() may have created multiple (i.e. two)
122       // objects, as the max_fill_size() is half a region.
123       // After updating the BOT for the first object, also update the
124       // BOT for the second object to make the BOT complete.
125       if (end_first_obj != end) {
126         _hr->alloc_block_in_bot(end_first_obj, end);
127 #ifdef ASSERT
128         size_t size_second_obj = cast_to_oop(end_first_obj)->size();
129         HeapWord* end_of_second_obj = end_first_obj + size_second_obj;
130         assert(end == end_of_second_obj,
131                "More than two objects were used to fill the area from " PTR_FORMAT " to " PTR_FORMAT ", "
132                "second objects size " SIZE_FORMAT " ends at " PTR_FORMAT,
133                p2i(start), p2i(end), size_second_obj, p2i(end_of_second_obj));
134 #endif
135       }
136     }
137     _cm->clear_range_in_prev_bitmap(mr);
138   }
139 
140   void zap_remainder() {
141     zap_dead_objects(_last_forwarded_object_end, _hr->top());
142   }
143 };
144 
145 class RemoveSelfForwardPtrHRClosure: public HeapRegionClosure {
146   G1CollectedHeap* _g1h;
147   uint _worker_id;
148 
149   G1EvacFailureRegions* _evac_failure_regions;
150 
151 public:
152   RemoveSelfForwardPtrHRClosure(uint worker_id,
153                                 G1EvacFailureRegions* evac_failure_regions) :
154     _g1h(G1CollectedHeap::heap()),
155     _worker_id(worker_id),
156     _evac_failure_regions(evac_failure_regions) {
157   }
158 
159   size_t remove_self_forward_ptr_by_walking_hr(HeapRegion* hr,
160                                                bool during_concurrent_start) {
161     RemoveSelfForwardPtrObjClosure rspc(hr,
162                                         during_concurrent_start,
163                                         _worker_id);
164     // Iterates evac failure objs which are recorded during evacuation.
165     hr->process_and_drop_evac_failure_objs(&rspc);
166     // Need to zap the remainder area of the processed region.
167     rspc.zap_remainder();
168 
169     return rspc.marked_bytes();
170   }
171 
172   bool do_heap_region(HeapRegion *hr) {
173     assert(!hr->is_pinned(), "Unexpected pinned region at index %u", hr->hrm_index());
174     assert(hr->in_collection_set(), "bad CS");
175 
176     if (_evac_failure_regions->contains(hr->hrm_index())) {
177       hr->clear_index_in_opt_cset();
178 
179       bool during_concurrent_start = _g1h->collector_state()->in_concurrent_start_gc();
180       bool during_concurrent_mark = _g1h->collector_state()->mark_or_rebuild_in_progress();
181 
182       hr->note_self_forwarding_removal_start(during_concurrent_start,
183                                              during_concurrent_mark);
184       _g1h->verifier()->check_bitmaps("Self-Forwarding Ptr Removal", hr);
185 
186       hr->reset_bot();
187 
188       size_t live_bytes = remove_self_forward_ptr_by_walking_hr(hr, during_concurrent_start);
189 
190       hr->rem_set()->clean_strong_code_roots(hr);
191       hr->rem_set()->clear_locked(true);
192 
193       hr->note_self_forwarding_removal_end(live_bytes);
194     }
195     return false;
196   }
197 };
198 
199 G1ParRemoveSelfForwardPtrsTask::G1ParRemoveSelfForwardPtrsTask(G1EvacFailureRegions* evac_failure_regions) :
200   WorkerTask("G1 Remove Self-forwarding Pointers"),
201   _g1h(G1CollectedHeap::heap()),
202   _hrclaimer(_g1h->workers()->active_workers()),
203   _evac_failure_regions(evac_failure_regions) { }
204 
205 void G1ParRemoveSelfForwardPtrsTask::work(uint worker_id) {
206   RemoveSelfForwardPtrHRClosure rsfp_cl(worker_id, _evac_failure_regions);
207 
208   // Iterate through all regions that failed evacuation during the entire collection.
209   _evac_failure_regions->par_iterate(&rsfp_cl, &_hrclaimer, worker_id);
210 }
211 
212 uint G1ParRemoveSelfForwardPtrsTask::num_failed_regions() const {
213   return _evac_failure_regions->num_regions_failed_evacuation();
214 }