1 /*
  2  * Copyright (c) 2001, 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 #ifndef SHARE_GC_G1_G1OOPCLOSURES_INLINE_HPP
 26 #define SHARE_GC_G1_G1OOPCLOSURES_INLINE_HPP
 27 
 28 #include "gc/g1/g1OopClosures.hpp"
 29 
 30 #include "gc/g1/g1CollectedHeap.hpp"
 31 #include "gc/g1/g1ConcurrentMark.inline.hpp"
 32 #include "gc/g1/g1ParScanThreadState.inline.hpp"
 33 #include "gc/g1/g1RemSet.hpp"
 34 #include "gc/g1/heapRegion.inline.hpp"
 35 #include "gc/g1/heapRegionRemSet.inline.hpp"
 36 #include "memory/iterator.inline.hpp"
 37 #include "oops/access.inline.hpp"
 38 #include "oops/compressedOops.inline.hpp"
 39 #include "oops/oopsHierarchy.hpp"
 40 #include "oops/oop.inline.hpp"
 41 #include "runtime/prefetch.inline.hpp"
 42 #include "utilities/align.hpp"
 43 
 44 template <class T>
 45 inline void G1ScanClosureBase::prefetch_and_push(T* p, const oop obj) {
 46   // We're not going to even bother checking whether the object is
 47   // already forwarded or not, as this usually causes an immediate
 48   // stall. We'll try to prefetch the object (for write, given that
 49   // we might need to install the forwarding reference) and we'll
 50   // get back to it when pop it from the queue
 51   Prefetch::write(obj->mark_addr(), 0);
 52   Prefetch::read(obj->mark_addr(), (HeapWordSize*2));
 53 
 54   // slightly paranoid test; I'm trying to catch potential
 55   // problems before we go into push_on_queue to know where the
 56   // problem is coming from
 57   assert((obj == RawAccess<>::oop_load(p)) ||
 58          (obj->is_forwarded() &&
 59          obj->forwardee() == RawAccess<>::oop_load(p)),
 60          "p should still be pointing to obj or to its forwardee");
 61 
 62   _par_scan_state->push_on_queue(ScannerTask(p));
 63 }
 64 
 65 template <class T>
 66 inline void G1ScanClosureBase::handle_non_cset_obj_common(G1HeapRegionAttr const region_attr, T* p, oop const obj) {
 67   if (region_attr.is_humongous()) {
 68     _g1h->set_humongous_is_live(obj);
 69   } else if (region_attr.is_optional()) {
 70     _par_scan_state->remember_reference_into_optional_region(p);
 71   }
 72 }
 73 
 74 inline void G1ScanClosureBase::trim_queue_partially() {
 75   _par_scan_state->trim_queue_partially();
 76 }
 77 
 78 template <class T>
 79 inline void G1ScanEvacuatedObjClosure::do_oop_work(T* p) {
 80   T heap_oop = RawAccess<>::oop_load(p);
 81 
 82   if (CompressedOops::is_null(heap_oop)) {
 83     return;
 84   }
 85   oop obj = CompressedOops::decode_not_null(heap_oop);
 86   const G1HeapRegionAttr region_attr = _g1h->region_attr(obj);
 87   if (region_attr.is_in_cset()) {
 88     prefetch_and_push(p, obj);
 89   } else if (!HeapRegion::is_in_same_region(p, obj)) {
 90     handle_non_cset_obj_common(region_attr, p, obj);
 91     assert(_skip_card_enqueue != Uninitialized, "Scan location has not been initialized.");
 92     if (_skip_card_enqueue == True) {
 93       return;
 94     }
 95     _par_scan_state->enqueue_card_if_tracked(region_attr, p, obj);
 96   }
 97 }
 98 
 99 template <class T>
100 inline void G1CMOopClosure::do_oop_work(T* p) {
101   _task->deal_with_reference(p);
102 }
103 
104 template <class T>
105 inline void G1RootRegionScanClosure::do_oop_work(T* p) {
106   T heap_oop = RawAccess<MO_RELAXED>::oop_load(p);
107   if (CompressedOops::is_null(heap_oop)) {
108     return;
109   }
110   oop obj = CompressedOops::decode_not_null(heap_oop);
111   _cm->mark_in_next_bitmap(_worker_id, obj);
112 }
113 
114 template <class T>
115 inline static void check_obj_during_refinement(T* p, oop const obj) {
116 #ifdef ASSERT
117   G1CollectedHeap* g1h = G1CollectedHeap::heap();
118   // can't do because of races
119   // assert(oopDesc::is_oop_or_null(obj), "expected an oop");
120   assert(is_object_aligned(obj), "oop must be aligned");
121   assert(g1h->is_in_reserved(obj), "oop must be in reserved");
122 
123   HeapRegion* from = g1h->heap_region_containing(p);
124 
125   assert(from != NULL, "from region must be non-NULL");
126   assert(from->is_in_reserved(p) ||
127          (from->is_humongous() &&
128           g1h->heap_region_containing(p)->is_humongous() &&
129           from->humongous_start_region() == g1h->heap_region_containing(p)->humongous_start_region()),
130          "p " PTR_FORMAT " is not in the same region %u or part of the correct humongous object starting at region %u.",
131          p2i(p), from->hrm_index(), from->humongous_start_region()->hrm_index());
132 #endif // ASSERT
133 }
134 
135 template <class T>
136 inline void G1ConcurrentRefineOopClosure::do_oop_work(T* p) {
137   T o = RawAccess<MO_RELAXED>::oop_load(p);
138   if (CompressedOops::is_null(o)) {
139     return;
140   }
141   oop obj = CompressedOops::decode_not_null(o);
142 
143   check_obj_during_refinement(p, obj);
144 
145   if (HeapRegion::is_in_same_region(p, obj)) {
146     // Normally this closure should only be called with cross-region references.
147     // But since Java threads are manipulating the references concurrently and we
148     // reload the values things may have changed.
149     // Also this check lets slip through references from a humongous continues region
150     // to its humongous start region, as they are in different regions, and adds a
151     // remembered set entry. This is benign (apart from memory usage), as we never
152     // try to either evacuate or eager reclaim humonguous arrays of j.l.O.
153     return;
154   }
155 
156   HeapRegionRemSet* to_rem_set = _g1h->heap_region_containing(obj)->rem_set();
157 
158   assert(to_rem_set != NULL, "Need per-region 'into' remsets.");
159   if (to_rem_set->is_tracked()) {
160     to_rem_set->add_reference(p, _worker_id);
161   }
162 }
163 
164 template <class T>
165 inline void G1ScanCardClosure::do_oop_work(T* p) {
166   T o = RawAccess<>::oop_load(p);
167   if (CompressedOops::is_null(o)) {
168     return;
169   }
170   oop obj = CompressedOops::decode_not_null(o);
171 
172   check_obj_during_refinement(p, obj);
173 
174   assert(!_g1h->is_in_cset((HeapWord*)p),
175          "Oop originates from " PTR_FORMAT " (region: %u) which is in the collection set.",
176          p2i(p), _g1h->addr_to_region((HeapWord*)p));
177 
178   const G1HeapRegionAttr region_attr = _g1h->region_attr(obj);
179   if (region_attr.is_in_cset()) {
180     // Since the source is always from outside the collection set, here we implicitly know
181     // that this is a cross-region reference too.
182     prefetch_and_push(p, obj);
183     _heap_roots_found++;
184   } else if (!HeapRegion::is_in_same_region(p, obj)) {
185     handle_non_cset_obj_common(region_attr, p, obj);
186     _par_scan_state->enqueue_card_if_tracked(region_attr, p, obj);
187   }
188 }
189 
190 template <class T>
191 inline void G1ScanRSForOptionalClosure::do_oop_work(T* p) {
192   const G1HeapRegionAttr region_attr = _g1h->region_attr(p);
193   // Entries in the optional collection set may start to originate from the collection
194   // set after one or more increments. In this case, previously optional regions
195   // became actual collection set regions. Filter them out here.
196   if (region_attr.is_in_cset()) {
197     return;
198   }
199   _scan_cl->do_oop_work(p);
200   _scan_cl->trim_queue_partially();
201 }
202 
203 void G1ParCopyHelper::do_cld_barrier(oop new_obj) {
204   if (_g1h->heap_region_containing(new_obj)->is_young()) {
205     _scanned_cld->record_modified_oops();
206   }
207 }
208 
209 void G1ParCopyHelper::mark_object(oop obj) {
210   assert(!_g1h->heap_region_containing(obj)->in_collection_set(), "should not mark objects in the CSet");
211 
212   // We know that the object is not moving so it's safe to read its size.
213   _cm->mark_in_next_bitmap(_worker_id, obj);
214 }
215 
216 void G1ParCopyHelper::trim_queue_partially() {
217   _par_scan_state->trim_queue_partially();
218 }
219 
220 template <G1Barrier barrier, bool should_mark>
221 template <class T>
222 void G1ParCopyClosure<barrier, should_mark>::do_oop_work(T* p) {
223   T heap_oop = RawAccess<>::oop_load(p);
224 
225   if (CompressedOops::is_null(heap_oop)) {
226     return;
227   }
228 
229   oop obj = CompressedOops::decode_not_null(heap_oop);
230 
231   assert(_worker_id == _par_scan_state->worker_id(), "sanity");
232 
233   const G1HeapRegionAttr state = _g1h->region_attr(obj);
234   if (state.is_in_cset()) {
235     oop forwardee;
236     markWord m = obj->mark();
237     if (m.is_marked()) {
238       forwardee = obj->forwardee(m);
239     } else {
240       forwardee = _par_scan_state->copy_to_survivor_space(state, obj, m);
241     }
242     assert(forwardee != NULL, "forwardee should not be NULL");
243     RawAccess<IS_NOT_NULL>::oop_store(p, forwardee);
244 
245     if (barrier == G1BarrierCLD) {
246       do_cld_barrier(forwardee);
247     }
248   } else {
249     if (state.is_humongous()) {
250       _g1h->set_humongous_is_live(obj);
251     } else if ((barrier != G1BarrierNoOptRoots) && state.is_optional()) {
252       _par_scan_state->remember_root_into_optional_region(p);
253     }
254 
255     // The object is not in the collection set. should_mark is true iff the
256     // current closure is applied on strong roots (and weak roots when class
257     // unloading is disabled) in a concurrent mark start pause.
258     if (should_mark) {
259       mark_object(obj);
260     }
261   }
262   trim_queue_partially();
263 }
264 
265 template <class T> void G1RebuildRemSetClosure::do_oop_work(T* p) {
266   oop const obj = RawAccess<MO_RELAXED>::oop_load(p);
267   if (obj == NULL) {
268     return;
269   }
270 
271   if (HeapRegion::is_in_same_region(p, obj)) {
272     return;
273   }
274 
275   HeapRegion* to = _g1h->heap_region_containing(obj);
276   HeapRegionRemSet* rem_set = to->rem_set();
277   rem_set->add_reference(p, _worker_id);
278 }
279 
280 #endif // SHARE_GC_G1_G1OOPCLOSURES_INLINE_HPP