1 /*
2 * Copyright (c) 2015, 2022, Red Hat, Inc. All rights reserved.
3 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
4 * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 *
7 * This code is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 only, as
9 * published by the Free Software Foundation.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 *
25 */
26
27 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHMARK_INLINE_HPP
28 #define SHARE_GC_SHENANDOAH_SHENANDOAHMARK_INLINE_HPP
29
30 #include "gc/shenandoah/shenandoahMark.hpp"
31
32 #include "gc/shared/continuationGCSupport.inline.hpp"
33 #include "gc/shenandoah/shenandoahAgeCensus.hpp"
34 #include "gc/shenandoah/shenandoahAsserts.hpp"
35 #include "gc/shenandoah/shenandoahBarrierSet.inline.hpp"
36 #include "gc/shenandoah/shenandoahClosures.inline.hpp"
37 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
38 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
39 #include "gc/shenandoah/shenandoahOldGeneration.hpp"
40 #include "gc/shenandoah/shenandoahScanRemembered.inline.hpp"
41 #include "gc/shenandoah/shenandoahTaskqueue.inline.hpp"
42 #include "gc/shenandoah/shenandoahUtils.hpp"
43 #include "memory/iterator.inline.hpp"
44 #include "oops/compressedOops.inline.hpp"
45 #include "oops/oop.inline.hpp"
46 #include "runtime/prefetch.inline.hpp"
47 #include "utilities/devirtualizer.inline.hpp"
48 #include "utilities/powerOfTwo.hpp"
49
50 template <class T, ShenandoahGenerationType GENERATION, bool STRING_DEDUP>
51 void ShenandoahMark::do_task(ShenandoahObjToScanQueue* q, T* cl, ShenandoahLiveData* live_data, StringDedup::Requests* const req, ShenandoahMarkTask* task, uint worker_id) {
52 oop obj = task->obj();
53
54 shenandoah_assert_not_forwarded(nullptr, obj);
55 shenandoah_assert_marked(nullptr, obj);
56 shenandoah_assert_not_in_cset_except(nullptr, obj, ShenandoahHeap::heap()->cancelled_gc());
57
58 // Are we in weak subgraph scan?
59 bool weak = task->is_weak();
60 cl->set_weak(weak);
61
62 if (task->is_not_chunked()) {
63 Klass* klass = obj->klass();
64 if (klass->is_instance_klass()) {
65 // Case 1: Normal oop, process as usual.
66 if (STRING_DEDUP && (klass == vmClasses::String_klass())) {
67 dedup_string(obj, req);
68 }
69 if (klass->is_stack_chunk_instance_klass()) {
70 // Loom doesn't support mixing of weak marking and strong marking of stack chunks.
71 cl->set_weak(false);
72 }
73 obj->oop_iterate(cl);
74 } else if (klass->is_refArray_klass()) {
75 // Case 2: Object array instance and no chunk is set. Must be the first
76 // time we visit it, start the chunked processing.
77 do_chunked_array_start<T>(q, cl, obj, klass, weak);
78 } else if (klass->is_flatArray_klass()) {
79 // Case 3: Flat array instance, all elements are embedded.
80 obj->oop_iterate(cl);
81 } else {
82 // Case 4: Primitive array. Do nothing, no oops there. We use the same
83 // performance tweak TypeArrayKlass::oop_oop_iterate_impl is using:
84 // We skip iterating over the klass pointer since we know that
85 // Universe::TypeArrayKlass never moves.
86 assert(klass->is_typeArray_klass(), "should be type array");
87 }
88 // Count liveness the last: push the outstanding work to the queues first
89 // Avoid double-counting objects that are visited twice due to upgrade
90 // from final- to strong mark.
91 if (task->count_liveness()) {
92 count_liveness<GENERATION>(live_data, obj, klass, worker_id);
93 }
94 } else {
95 // Case 5: Array chunk, has sensible chunk id. Process it.
96 do_chunked_array<T>(q, cl, obj, task->chunk(), task->pow(), weak);
97 }
98 }
99
100 inline void ShenandoahMark::dedup_string(oop obj, StringDedup::Requests* const req) {
101 assert(req != nullptr, "Should be available if dedup is enabled");
102
103 // Skip if already requested or dedup is forbidden.
104 // The overwhelming majority of Strings would be filtered here.
105 // These bits are also sticky, so older Strings would be filtered here too.
106 if (java_lang_String::deduplication_requested_or_forbidden(obj)) {
107 return;
108 }
109
110 // Accept deduplication request.
111 if (!java_lang_String::test_and_set_deduplication_requested(obj)) {
112 req->add(obj);
113 }
114 }
115
116 template <ShenandoahGenerationType GENERATION>
117 void ShenandoahMark::count_liveness(ShenandoahLiveData* live_data, oop obj, Klass* klass, uint worker_id) {
118 const ShenandoahHeap* const heap = ShenandoahHeap::heap();
119 const size_t region_idx = heap->heap_region_index_containing(obj);
120 ShenandoahHeapRegion* const region = heap->get_region(region_idx);
121 const size_t size = obj->size_given_klass(klass);
122
123 // Age census for objects in the young generation
124 if (GENERATION == YOUNG || (GENERATION == GLOBAL && region->is_young())) {
125 assert(heap->mode()->is_generational(), "Only if generational");
126 assert(region->is_young(), "Only for young objects");
127 const uint age = ShenandoahHeap::get_object_age(obj);
128 ShenandoahAgeCensus* const census = ShenandoahGenerationalHeap::heap()->age_census();
129 CENSUS_NOISE(census->add(age, region->age(), region->youth(), size, worker_id);)
130 NO_CENSUS_NOISE(census->add(age, region->age(), size, worker_id);)
131 }
132
133 if (!region->is_humongous_start()) {
134 assert(!region->is_humongous(), "Cannot have continuations here");
135 assert(region->is_affiliated(), "Do not count live data within Free Regular Region %zu", region_idx);
136 ShenandoahLiveData cur = live_data[region_idx];
137 size_t new_val = size + cur;
138 if (new_val >= SHENANDOAH_LIVEDATA_MAX) {
139 // overflow, flush to region data
140 region->increase_live_data_gc_words(new_val);
141 live_data[region_idx] = 0;
142 } else {
143 // still good, remember in locals
144 live_data[region_idx] = (ShenandoahLiveData) new_val;
145 }
146 } else {
147 shenandoah_assert_in_correct_region(nullptr, obj);
148 size_t num_regions = ShenandoahHeapRegion::required_regions(size * HeapWordSize);
149
150 assert(region->is_affiliated(), "Do not count live data within FREE Humongous Start Region %zu", region_idx);
151 for (size_t i = region_idx; i < region_idx + num_regions; i++) {
152 ShenandoahHeapRegion* chain_reg = heap->get_region(i);
153 assert(chain_reg->is_humongous(), "Expecting a humongous region");
154 assert(chain_reg->is_affiliated(), "Do not count live data within FREE Humongous Continuation Region %zu", i);
155 chain_reg->increase_live_data_gc_words(chain_reg->used() >> LogHeapWordSize);
156 }
157 }
158 }
159
160 template <class T>
161 void ShenandoahMark::do_chunked_array_start(ShenandoahObjToScanQueue* q, T* cl, oop obj, Klass* klass, bool weak) {
162 assert(obj->is_refArray(), "expect ref array");
163 refArrayOop array = refArrayOop(obj);
164 int len = array->length();
165
166 // Mark objArray klass metadata
167 if (Devirtualizer::do_metadata(cl)) {
168 Devirtualizer::do_klass(cl, klass);
169 }
170
171 if (len <= (int) ObjArrayMarkingStride*2) {
172 // A few slices only, process directly
173 array->oop_iterate_elements_range(cl, 0, len);
174 } else {
175 int bits = log2i_graceful(len);
176 // Compensate for non-power-of-two arrays, cover the array in excess:
177 if (len != (1 << bits)) bits++;
178
179 // Only allow full chunks on the queue. This frees do_chunked_array() from checking from/to
180 // boundaries against array->length(), touching the array header on every chunk.
181 //
182 // To do this, we cut the prefix in full-sized chunks, and submit them on the queue.
183 // If the array is not divided in chunk sizes, then there would be an irregular tail,
184 // which we will process separately.
185
186 int last_idx = 0;
187
188 int chunk = 1;
189 int pow = bits;
190
191 // Handle overflow
192 if (pow >= 31) {
193 assert (pow == 31, "sanity");
194 pow--;
195 chunk = 2;
196 last_idx = (1 << pow);
197 bool pushed = q->push(ShenandoahMarkTask(array, true, weak, 1, pow));
198 assert(pushed, "overflow queue should always succeed pushing");
199 }
200
201 // Split out tasks, as suggested in ShenandoahMarkTask docs. Record the last
202 // successful right boundary to figure out the irregular tail.
203 while ((1 << pow) > (int)ObjArrayMarkingStride &&
204 (chunk*2 < ShenandoahMarkTask::chunk_size())) {
205 pow--;
206 int left_chunk = chunk*2 - 1;
207 int right_chunk = chunk*2;
208 int left_chunk_end = left_chunk * (1 << pow);
209 if (left_chunk_end < len) {
210 bool pushed = q->push(ShenandoahMarkTask(array, true, weak, left_chunk, pow));
211 assert(pushed, "overflow queue should always succeed pushing");
212 chunk = right_chunk;
213 last_idx = left_chunk_end;
214 } else {
215 chunk = left_chunk;
216 }
217 }
218
219 // Process the irregular tail, if present
220 int from = last_idx;
221 if (from < len) {
222 array->oop_iterate_elements_range(cl, from, len);
223 }
224 }
225 }
226
227 template <class T>
228 void ShenandoahMark::do_chunked_array(ShenandoahObjToScanQueue* q, T* cl, oop obj, int chunk, int pow, bool weak) {
229 assert(obj->is_refArray(), "expect ref array");
230 refArrayOop array = refArrayOop(obj);
231
232 // Split out tasks, as suggested in ShenandoahMarkTask docs. Avoid pushing tasks that
233 // are known to start beyond the array.
234 while ((1 << pow) > (int)ObjArrayMarkingStride && (chunk*2 < ShenandoahMarkTask::chunk_size())) {
235 pow--;
236 chunk *= 2;
237 bool pushed = q->push(ShenandoahMarkTask(array, true, weak, chunk - 1, pow));
238 assert(pushed, "overflow queue should always succeed pushing");
239 }
240
241 int chunk_size = 1 << pow;
242
243 int from = (chunk - 1) * chunk_size;
244 int to = chunk * chunk_size;
245
246 #ifdef ASSERT
247 int len = array->length();
248 assert (0 <= from && from < len, "from is sane: %d/%d", from, len);
249 assert (0 < to && to <= len, "to is sane: %d/%d", to, len);
250 #endif
251
252 array->oop_iterate_elements_range(cl, from, to);
253 }
254
255 template <ShenandoahGenerationType GENERATION>
256 class ShenandoahSATBBufferClosure : public SATBBufferClosure {
257 private:
258 ShenandoahObjToScanQueue* _queue;
259 ShenandoahObjToScanQueue* _old_queue;
260 ShenandoahHeap* _heap;
261 ShenandoahMarkingContext* const _mark_context;
262 public:
263 ShenandoahSATBBufferClosure(ShenandoahObjToScanQueue* q, ShenandoahObjToScanQueue* old_q) :
264 _queue(q),
265 _old_queue(old_q),
266 _heap(ShenandoahHeap::heap()),
267 _mark_context(_heap->marking_context())
268 {
269 }
270
271 void do_buffer(void **buffer, size_t size) {
272 assert(size == 0 || !_heap->has_forwarded_objects() || _heap->is_concurrent_old_mark_in_progress(), "Forwarded objects are not expected here");
273 for (size_t i = 0; i < size; ++i) {
274 oop *p = (oop *) &buffer[i];
275 ShenandoahMark::mark_through_ref<oop, GENERATION>(p, _queue, _old_queue, _mark_context, false);
276 }
277 }
278 };
279
280 template<ShenandoahGenerationType GENERATION>
281 bool ShenandoahMark::in_generation(ShenandoahHeap* const heap, oop obj) {
282 // Each in-line expansion of in_generation() resolves GENERATION at compile time.
283 if (GENERATION == YOUNG) {
284 return heap->is_in_young(obj);
285 }
286
287 if (GENERATION == OLD) {
288 return heap->is_in_old(obj);
289 }
290
291 assert((GENERATION == GLOBAL || GENERATION == NON_GEN), "Unexpected generation type");
292 assert(heap->is_in(obj), "Object must be in heap");
293 return true;
294 }
295
296 template<class T, ShenandoahGenerationType GENERATION>
297 void ShenandoahMark::mark_through_ref(T *p, ShenandoahObjToScanQueue* q, ShenandoahObjToScanQueue* old_q, ShenandoahMarkingContext* const mark_context, bool weak) {
298 // Note: This is a very hot code path, so the code should be conditional on GENERATION template
299 // parameter where possible, in order to generate the most efficient code.
300
301 T o = RawAccess<>::oop_load(p);
302 if (!CompressedOops::is_null(o)) {
303 oop obj = CompressedOops::decode_not_null(o);
304
305 ShenandoahGenerationalHeap* heap = ShenandoahGenerationalHeap::heap();
306 shenandoah_assert_not_forwarded(p, obj);
307 shenandoah_assert_not_in_cset_except(p, obj, heap->cancelled_gc());
308 if (in_generation<GENERATION>(heap, obj)) {
309 mark_ref(q, mark_context, weak, obj);
310 shenandoah_assert_marked(p, obj);
311 if (GENERATION == YOUNG && heap->is_in_old(p)) {
312 // Mark card as dirty because remembered set scanning still finds interesting pointer.
313 heap->old_generation()->mark_card_as_dirty((HeapWord*)p);
314 } else if (GENERATION == GLOBAL && heap->is_in_old(p) && heap->is_in_young(obj)) {
315 // Mark card as dirty because GLOBAL marking finds interesting pointer.
316 heap->old_generation()->mark_card_as_dirty((HeapWord*)p);
317 }
318 } else if (old_q != nullptr) {
319 // Young mark, bootstrapping old_q or concurrent with old_q marking.
320 mark_ref(old_q, mark_context, weak, obj);
321 shenandoah_assert_marked(p, obj);
322 } else if (GENERATION == OLD) {
323 // Old mark, found a young pointer.
324 if (heap->is_in(p)) {
325 assert(heap->is_in_young(obj), "Expected young object.");
326 heap->old_generation()->mark_card_as_dirty(p);
327 }
328 }
329 }
330 }
331
332 template<>
333 ALWAYSINLINE
334 void ShenandoahMark::mark_through_ref<oop, ShenandoahGenerationType::NON_GEN>(oop *p, ShenandoahObjToScanQueue* q, ShenandoahObjToScanQueue* old_q, ShenandoahMarkingContext* const mark_context, bool weak) {
335 mark_non_generational_ref(p, q, mark_context, weak);
336 }
337
338 template<>
339 ALWAYSINLINE
340 void ShenandoahMark::mark_through_ref<narrowOop, ShenandoahGenerationType::NON_GEN>(narrowOop *p, ShenandoahObjToScanQueue* q, ShenandoahObjToScanQueue* old_q, ShenandoahMarkingContext* const mark_context, bool weak) {
341 mark_non_generational_ref(p, q, mark_context, weak);
342 }
343
344 template<class T>
345 void ShenandoahMark::mark_non_generational_ref(T* p, ShenandoahObjToScanQueue* q,
346 ShenandoahMarkingContext* const mark_context, bool weak) {
347 oop o = RawAccess<>::oop_load(p);
348 if (!CompressedOops::is_null(o)) {
349 oop obj = CompressedOops::decode_not_null(o);
350
351 shenandoah_assert_not_forwarded(p, obj);
352 shenandoah_assert_not_in_cset_except(p, obj, ShenandoahHeap::heap()->cancelled_gc());
353
354 mark_ref(q, mark_context, weak, obj);
355
356 shenandoah_assert_marked(p, obj);
357 }
358 }
359
360 inline void ShenandoahMark::mark_ref(ShenandoahObjToScanQueue* q,
361 ShenandoahMarkingContext* const mark_context,
362 bool weak, oop obj) {
363 bool skip_live = false;
364 bool marked;
365 if (weak) {
366 marked = mark_context->mark_weak(obj);
367 } else {
368 marked = mark_context->mark_strong(obj, /* was_upgraded = */ skip_live);
369 }
370 if (marked) {
371 bool pushed = q->push(ShenandoahMarkTask(obj, skip_live, weak));
372 assert(pushed, "overflow queue should always succeed pushing");
373 }
374 }
375
376 ShenandoahObjToScanQueueSet* ShenandoahMark::task_queues() const {
377 return _task_queues;
378 }
379
380 ShenandoahObjToScanQueue* ShenandoahMark::get_queue(uint index) const {
381 return _task_queues->queue(index);
382 }
383
384 ShenandoahObjToScanQueue* ShenandoahMark::get_old_queue(uint index) const {
385 if (_old_gen_task_queues != nullptr) {
386 return _old_gen_task_queues->queue(index);
387 }
388 return nullptr;
389 }
390
391 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHMARK_INLINE_HPP