1 /*
2 * Copyright (c) 2001, 2026, 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_G1CONCURRENTMARK_INLINE_HPP
26 #define SHARE_GC_G1_G1CONCURRENTMARK_INLINE_HPP
27
28 #include "gc/g1/g1ConcurrentMark.hpp"
29
30 #include "gc/g1/g1CollectedHeap.inline.hpp"
31 #include "gc/g1/g1ConcurrentMarkBitMap.inline.hpp"
32 #include "gc/g1/g1HeapRegion.hpp"
33 #include "gc/g1/g1HeapRegionRemSet.inline.hpp"
34 #include "gc/g1/g1OopClosures.inline.hpp"
35 #include "gc/g1/g1Policy.hpp"
36 #include "gc/g1/g1RegionMarkStatsCache.inline.hpp"
37 #include "gc/g1/g1RemSetTrackingPolicy.hpp"
38 #include "gc/shared/suspendibleThreadSet.hpp"
39 #include "gc/shared/taskqueue.inline.hpp"
40 #include "utilities/bitMap.inline.hpp"
41 #include "utilities/checkedCast.hpp"
42
43 inline bool G1CMIsAliveClosure::do_object_b(oop obj) {
44 // Check whether the passed in object is null. During discovery the referent
45 // may be cleared between the initial check and being passed in here.
46 if (obj == nullptr) {
47 // Return true to avoid discovery when the referent is null.
48 return true;
49 }
50
51 // All objects allocated since the start of marking are considered live.
52 if (_cm->obj_allocated_since_mark_start(obj)) {
53 return true;
54 }
55
56 // All objects that are marked are live.
57 return _cm->is_marked_in_bitmap(obj);
58 }
59
60 inline bool G1CMSubjectToDiscoveryClosure::do_object_b(oop obj) {
61 assert(obj != nullptr, "precondition");
62 assert(_g1h->is_in_reserved(obj), "Trying to discover obj " PTR_FORMAT " not in heap", p2i(obj));
63
64 return _g1h->heap_region_containing(obj)->is_old_or_humongous();
65 }
66
67 inline bool G1ConcurrentMark::mark_in_bitmap(uint const worker_id, oop const obj) {
68 if (obj_allocated_since_mark_start(obj)) {
69 return false;
70 }
71
72 // Some callers may have stale objects to mark above TAMS after humongous reclaim.
73 // Can't assert that this is a valid object at this point, since it might be in the process of being copied by another thread.
74 DEBUG_ONLY(G1HeapRegion* const hr = _g1h->heap_region_containing(obj);)
75 assert(!hr->is_continues_humongous(),
76 "Should not try to mark object " PTR_FORMAT " in Humongous continues region %u above TAMS " PTR_FORMAT,
77 p2i(obj), hr->hrm_index(), p2i(top_at_mark_start(hr)));
78
79 bool success = _mark_bitmap.par_mark(obj);
80 if (success) {
81 add_to_liveness(worker_id, obj, obj->size());
82 }
83 return success;
84 }
85
86 #ifndef PRODUCT
87 template<typename Fn>
88 inline void G1CMMarkStack::iterate(Fn fn) const {
89 assert_at_safepoint_on_vm_thread();
90
91 size_t num_chunks = 0;
92
93 TaskQueueEntryChunk* cur = _chunk_list.load_relaxed();
94 while (cur != nullptr) {
95 guarantee(num_chunks <= _chunks_in_chunk_list, "Found %zu oop chunks which is more than there should be", num_chunks);
96
97 for (size_t i = 0; i < EntriesPerChunk; ++i) {
98 if (cur->data[i].is_null()) {
99 break;
100 }
101 fn(cur->data[i]);
102 }
103 cur = cur->next;
104 num_chunks++;
105 }
106 }
107 #endif
108
109 inline void G1CMTask::process_klass(Klass* klass) {
110 _cm_oop_closure->do_klass(klass);
111 }
112
113 // It scans an object and visits its children.
114 inline void G1CMTask::process_entry(G1TaskQueueEntry task_entry, bool stolen) {
115 assert(task_entry.is_partial_array_state() || _mark_bitmap->is_marked(cast_from_oop<HeapWord*>(task_entry.to_oop())),
116 "Any stolen object should be a slice or marked");
117
118 if (task_entry.is_partial_array_state()) {
119 _words_scanned += process_partial_array(task_entry, stolen);
120 } else {
121 oop obj = task_entry.to_oop();
122 if (should_be_sliced(obj)) {
123 _words_scanned += start_partial_array_processing(objArrayOop(obj));
124 } else {
125 _words_scanned += obj->oop_iterate_size(_cm_oop_closure);
126 }
127 }
128
129 check_limits();
130 }
131
132 inline void G1CMTask::push(G1TaskQueueEntry task_entry) {
133 assert(task_entry.is_partial_array_state() || _g1h->is_in_reserved(task_entry.to_oop()), "invariant");
134 assert(task_entry.is_partial_array_state() || !_g1h->is_on_master_free_list(
135 _g1h->heap_region_containing(task_entry.to_oop())), "invariant");
136 assert(task_entry.is_partial_array_state() || _mark_bitmap->is_marked(cast_from_oop<HeapWord*>(task_entry.to_oop())), "invariant");
137
138 if (!_task_queue->push(task_entry)) {
139 // The local task queue looks full. We need to push some entries
140 // to the global stack.
141 move_entries_to_global_stack();
142
143 // this should succeed since, even if we overflow the global
144 // stack, we should have definitely removed some entries from the
145 // local queue. So, there must be space on it.
146 bool success = _task_queue->push(task_entry);
147 assert(success, "invariant");
148 }
149 }
150
151 inline bool G1CMTask::is_below_finger(oop obj, HeapWord* global_finger) const {
152 // If obj is above the global finger, then the mark bitmap scan
153 // will find it later, and no push is needed. Similarly, if we have
154 // a current region and obj is between the local finger and the
155 // end of the current region, then no push is needed. The tradeoff
156 // of checking both vs only checking the global finger is that the
157 // local check will be more accurate and so result in fewer pushes,
158 // but may also be a little slower.
159 HeapWord* objAddr = cast_from_oop<HeapWord*>(obj);
160 if (_finger != nullptr) {
161 // We have a current region.
162
163 // Finger and region values are all null or all non-null. We
164 // use _finger to check since we immediately use its value.
165 assert(_curr_region != nullptr, "invariant");
166 assert(_region_limit != nullptr, "invariant");
167 assert(_region_limit <= global_finger, "invariant");
168
169 // True if obj is less than the local finger, or is between
170 // the region limit and the global finger.
171 if (objAddr < _finger) {
172 return true;
173 } else if (objAddr < _region_limit) {
174 return false;
175 } // Else check global finger.
176 }
177 // Check global finger.
178 return objAddr < global_finger;
179 }
180
181 inline bool G1CMTask::should_be_sliced(oop obj) {
182 return obj->is_array_with_oops() && ((objArrayOop)obj)->length() >= (int)ObjArrayMarkingStride;
183 }
184
185 inline void G1CMTask::process_array_chunk(objArrayOop obj, size_t start, size_t end) {
186 precond(obj->is_array_with_oops());
187 obj->oop_iterate_elements_range(_cm_oop_closure,
188 checked_cast<int>(start),
189 checked_cast<int>(end));
190 }
191
192 inline void G1ConcurrentMark::update_top_at_mark_start(G1HeapRegion* r) {
193 uint const region = r->hrm_index();
194 assert(region < _g1h->max_num_regions(), "Tried to access TAMS for region %u out of bounds", region);
195 _top_at_mark_starts[region].store_relaxed(r->top());
196 }
197
198 inline void G1ConcurrentMark::reset_top_at_mark_start(G1HeapRegion* r) {
199 _top_at_mark_starts[r->hrm_index()].store_relaxed(r->bottom());
200 }
201
202 inline HeapWord* G1ConcurrentMark::top_at_mark_start(const G1HeapRegion* r) const {
203 return top_at_mark_start(r->hrm_index());
204 }
205
206 inline HeapWord* G1ConcurrentMark::top_at_mark_start(uint region) const {
207 assert(region < _g1h->max_num_regions(), "Tried to access TARS for region %u out of bounds", region);
208 return _top_at_mark_starts[region].load_relaxed();
209 }
210
211 inline bool G1ConcurrentMark::obj_allocated_since_mark_start(oop obj) const {
212 uint const region = _g1h->addr_to_region(obj);
213 assert(region < _g1h->max_num_regions(), "obj " PTR_FORMAT " outside heap %u", p2i(obj), region);
214 return cast_from_oop<HeapWord*>(obj) >= top_at_mark_start(region);
215 }
216
217 inline HeapWord* G1ConcurrentMark::top_at_rebuild_start(G1HeapRegion* r) const {
218 return _top_at_rebuild_starts[r->hrm_index()].load_relaxed();
219 }
220
221 inline void G1ConcurrentMark::update_top_at_rebuild_start(G1HeapRegion* r) {
222 assert(r->is_old() || r->is_humongous(), "precondition");
223
224 uint const region = r->hrm_index();
225 assert(region < _g1h->max_num_regions(), "Tried to access TARS for region %u out of bounds", region);
226 assert(top_at_rebuild_start(r) == nullptr,
227 "TARS for region %u has already been set to " PTR_FORMAT " should be null",
228 region, p2i(top_at_rebuild_start(r)));
229 _top_at_rebuild_starts[region].store_relaxed(r->top());
230 }
231
232 inline void G1CMTask::update_liveness(oop const obj, const size_t obj_size) {
233 _mark_stats_cache.add_live_words(_g1h->addr_to_region(obj), obj_size);
234 }
235
236 inline void G1CMTask::inc_incoming_refs(oop const obj) {
237 _mark_stats_cache.inc_incoming_refs(_g1h->addr_to_region(obj));
238 }
239
240 inline void G1ConcurrentMark::add_to_liveness(uint worker_id, oop const obj, size_t size) {
241 task(worker_id)->update_liveness(obj, size);
242 }
243
244 inline void G1CMTask::abort_marking_if_regular_check_fail() {
245 if (!regular_clock_call()) {
246 set_has_aborted();
247 }
248 }
249
250 inline bool G1CMTask::make_reference_grey(oop obj) {
251 if (!_cm->mark_in_bitmap(_worker_id, obj)) {
252 return false;
253 }
254
255 // No OrderAccess:store_load() is needed. It is implicit in the
256 // CAS done in G1CMBitMap::parMark() call in the routine above.
257 HeapWord* global_finger = _cm->finger();
258
259 // We only need to push a newly grey object on the mark
260 // stack if it is in a section of memory the mark bitmap
261 // scan has already examined. Mark bitmap scanning
262 // maintains progress "fingers" for determining that.
263 //
264 // Notice that the global finger might be moving forward
265 // concurrently. This is not a problem. In the worst case, we
266 // mark the object while it is above the global finger and, by
267 // the time we read the global finger, it has moved forward
268 // past this object. In this case, the object will probably
269 // be visited when a task is scanning the region and will also
270 // be pushed on the stack. So, some duplicate work, but no
271 // correctness problems.
272 if (is_below_finger(obj, global_finger)) {
273 if (_g1h->can_be_marked_through_immediately(obj)) {
274 // Immediately process arrays of types without oops, rather
275 // than pushing on the mark stack. This keeps us from
276 // adding humongous objects to the mark stack that might
277 // be reclaimed before the entry is processed - see
278 // selection of candidates for eager reclaim of humongous
279 // objects. The cost of the additional type test is
280 // mitigated by avoiding a trip through the mark stack,
281 // by only doing a bookkeeping update and avoiding the
282 // actual scan of the object - the object contains no
283 // references (but the metadata must be processed).
284 process_klass(obj->klass());
285 } else {
286 G1TaskQueueEntry entry(obj);
287 push(entry);
288 }
289 }
290 return true;
291 }
292
293 template <class T>
294 inline bool G1CMTask::deal_with_reference(T* p) {
295 increment_refs_reached();
296 oop const obj = RawAccess<MO_RELAXED>::oop_load(p);
297 if (obj == nullptr) {
298 return false;
299 }
300
301 if (!G1HeapRegion::is_in_same_region(p, obj)) {
302 inc_incoming_refs(obj);
303 }
304 return make_reference_grey(obj);
305 }
306
307 inline void G1ConcurrentMark::raw_mark_in_bitmap(oop obj) {
308 _mark_bitmap.par_mark(obj);
309 }
310
311 bool G1ConcurrentMark::is_marked_in_bitmap(oop p) const {
312 assert(p != nullptr && oopDesc::is_oop(p), "expected an oop");
313 return _mark_bitmap.is_marked(cast_from_oop<HeapWord*>(p));
314 }
315
316 inline bool G1ConcurrentMark::do_yield_check() {
317 if (SuspendibleThreadSet::should_yield()) {
318 SuspendibleThreadSet::yield();
319 return true;
320 } else {
321 return false;
322 }
323 }
324
325 #endif // SHARE_GC_G1_G1CONCURRENTMARK_INLINE_HPP