1 /*
  2  * Copyright (c) 2001, 2020, 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.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(_scanning_in_young != Uninitialized, "Scan location has not been initialized.");
 92     if (_scanning_in_young == 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   } else if (!HeapRegion::is_in_same_region(p, obj)) {
184     handle_non_cset_obj_common(region_attr, p, obj);
185     _par_scan_state->enqueue_card_if_tracked(region_attr, p, obj);
186   }
187 }
188 
189 template <class T>
190 inline void G1ScanRSForOptionalClosure::do_oop_work(T* p) {
191   const G1HeapRegionAttr region_attr = _g1h->region_attr(p);
192   // Entries in the optional collection set may start to originate from the collection
193   // set after one or more increments. In this case, previously optional regions
194   // became actual collection set regions. Filter them out here.
195   if (region_attr.is_in_cset()) {
196     return;
197   }
198   _scan_cl->do_oop_work(p);
199   _scan_cl->trim_queue_partially();
200 }
201 
202 void G1ParCopyHelper::do_cld_barrier(oop new_obj) {
203   if (_g1h->heap_region_containing(new_obj)->is_young()) {
204     _scanned_cld->record_modified_oops();
205   }
206 }
207 
208 void G1ParCopyHelper::mark_object(oop obj) {
209   assert(!_g1h->heap_region_containing(obj)->in_collection_set(), "should not mark objects in the CSet");
210 
211   // We know that the object is not moving so it's safe to read its size.
212   _cm->mark_in_next_bitmap(_worker_id, obj);
213 }
214 
215 void G1ParCopyHelper::trim_queue_partially() {
216   _par_scan_state->trim_queue_partially();
217 }
218 
219 template <G1Barrier barrier, bool should_mark>
220 template <class T>
221 void G1ParCopyClosure<barrier, should_mark>::do_oop_work(T* p) {
222   T heap_oop = RawAccess<>::oop_load(p);
223 
224   if (CompressedOops::is_null(heap_oop)) {
225     return;
226   }
227 
228   oop obj = CompressedOops::decode_not_null(heap_oop);
229 
230   assert(_worker_id == _par_scan_state->worker_id(), "sanity");
231 
232   const G1HeapRegionAttr state = _g1h->region_attr(obj);
233   if (state.is_in_cset()) {
234     oop forwardee;
235     markWord m = obj->mark();
236     if (m.is_marked()) {
237       forwardee = obj->forwardee(m);
238     } else {
239       forwardee = _par_scan_state->copy_to_survivor_space(state, obj, m);
240     }
241     assert(forwardee != NULL, "forwardee should not be NULL");
242     RawAccess<IS_NOT_NULL>::oop_store(p, forwardee);
243 
244     if (barrier == G1BarrierCLD) {
245       do_cld_barrier(forwardee);
246     }
247   } else {
248     if (state.is_humongous()) {
249       _g1h->set_humongous_is_live(obj);
250     } else if ((barrier != G1BarrierNoOptRoots) && state.is_optional()) {
251       _par_scan_state->remember_root_into_optional_region(p);
252     }
253 
254     // The object is not in the collection set. should_mark is true iff the
255     // current closure is applied on strong roots (and weak roots when class
256     // unloading is disabled) in a concurrent mark start pause.
257     if (should_mark) {
258       mark_object(obj);
259     }
260   }
261   trim_queue_partially();
262 }
263 
264 template <class T> void G1RebuildRemSetClosure::do_oop_work(T* p) {
265   oop const obj = RawAccess<MO_RELAXED>::oop_load(p);
266   if (obj == NULL) {
267     return;
268   }
269 
270   if (HeapRegion::is_in_same_region(p, obj)) {
271     return;
272   }
273 
274   HeapRegion* to = _g1h->heap_region_containing(obj);
275   HeapRegionRemSet* rem_set = to->rem_set();
276   rem_set->add_reference(p, _worker_id);
277 }
278 
279 #endif // SHARE_GC_G1_G1OOPCLOSURES_INLINE_HPP
--- EOF ---