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