1 /*
2 * Copyright (c) 2002, 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 #include "classfile/javaClasses.inline.hpp"
26 #include "gc/parallel/mutableSpace.hpp"
27 #include "gc/parallel/parallelScavengeHeap.hpp"
28 #include "gc/parallel/psOldGen.hpp"
29 #include "gc/parallel/psPromotionManager.inline.hpp"
30 #include "gc/parallel/psScavenge.hpp"
31 #include "gc/shared/continuationGCSupport.inline.hpp"
32 #include "gc/shared/gc_globals.hpp"
33 #include "gc/shared/gcTrace.hpp"
34 #include "gc/shared/partialArraySplitter.inline.hpp"
35 #include "gc/shared/partialArrayState.hpp"
36 #include "gc/shared/preservedMarks.inline.hpp"
37 #include "gc/shared/taskqueue.inline.hpp"
38 #include "logging/log.hpp"
39 #include "logging/logStream.hpp"
40 #include "memory/allocation.inline.hpp"
41 #include "memory/iterator.inline.hpp"
42 #include "memory/memRegion.hpp"
43 #include "memory/padded.inline.hpp"
44 #include "memory/resourceArea.hpp"
45 #include "oops/access.inline.hpp"
46 #include "oops/compressedOops.inline.hpp"
47 #include "oops/oopsHierarchy.hpp"
48 #include "utilities/checkedCast.hpp"
49
50 PaddedEnd<PSPromotionManager>* PSPromotionManager::_manager_array = nullptr;
51 PSPromotionManager::PSScannerTasksQueueSet* PSPromotionManager::_stack_array_depth = nullptr;
52 PreservedMarksSet* PSPromotionManager::_preserved_marks_set = nullptr;
53 PSOldGen* PSPromotionManager::_old_gen = nullptr;
54 MutableSpace* PSPromotionManager::_young_space = nullptr;
55 PartialArrayStateManager* PSPromotionManager::_partial_array_state_manager = nullptr;
56
57 void PSPromotionManager::initialize() {
58 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
59
60 _old_gen = heap->old_gen();
61 _young_space = heap->young_gen()->to_space();
62
63 const uint promotion_manager_num = ParallelGCThreads;
64
65 assert(_partial_array_state_manager == nullptr, "Attempt to initialize twice");
66 _partial_array_state_manager
67 = new PartialArrayStateManager(promotion_manager_num);
68
69 // To prevent false sharing, we pad the PSPromotionManagers
70 // and make sure that the first instance starts at a cache line.
71 assert(_manager_array == nullptr, "Attempt to initialize twice");
72 _manager_array = PaddedArray<PSPromotionManager, mtGC>::create_unfreeable(promotion_manager_num);
73
74 _stack_array_depth = new PSScannerTasksQueueSet(promotion_manager_num);
75
76 // Create and register the PSPromotionManager(s) for the worker threads.
77 for(uint i=0; i<ParallelGCThreads; i++) {
78 stack_array_depth()->register_queue(i, _manager_array[i].claimed_stack_depth());
79 }
80 // The VMThread gets its own PSPromotionManager, which is not available
81 // for work stealing.
82
83 assert(_preserved_marks_set == nullptr, "Attempt to initialize twice");
84 _preserved_marks_set = new PreservedMarksSet(true /* in_c_heap */);
85 _preserved_marks_set->init(promotion_manager_num);
86 for (uint i = 0; i < promotion_manager_num; i += 1) {
87 _manager_array[i].register_preserved_marks(_preserved_marks_set->get(i));
88 }
89 }
90
91 PSPromotionManager* PSPromotionManager::gc_thread_promotion_manager(uint index) {
92 assert(index < ParallelGCThreads, "index out of range");
93 assert(_manager_array != nullptr, "Sanity");
94 return &_manager_array[index];
95 }
96
97 PSPromotionManager* PSPromotionManager::vm_thread_promotion_manager() {
98 assert(_manager_array != nullptr, "Sanity");
99 return &_manager_array[0];
100 }
101
102 void PSPromotionManager::pre_scavenge() {
103 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
104
105 _preserved_marks_set->assert_empty();
106 _young_space = heap->young_gen()->to_space();
107
108 for(uint i=0; i<ParallelGCThreads; i++) {
109 manager_array(i)->reset();
110 }
111 }
112
113 bool PSPromotionManager::post_scavenge(YoungGCTracer& gc_tracer) {
114 bool promotion_failure_occurred = false;
115
116 TASKQUEUE_STATS_ONLY(print_and_reset_taskqueue_stats());
117 for (uint i = 0; i < ParallelGCThreads; i++) {
118 PSPromotionManager* manager = manager_array(i);
119 assert(manager->claimed_stack_depth()->is_empty(), "should be empty");
120 if (manager->_promotion_failed_info.has_failed()) {
121 gc_tracer.report_promotion_failed(manager->_promotion_failed_info);
122 promotion_failure_occurred = true;
123 }
124 manager->flush_labs();
125 manager->flush_string_dedup_requests();
126 }
127 // All PartialArrayStates have been returned to the allocator, since the
128 // claimed_stack_depths are all empty. Leave them there for use by future
129 // collections.
130
131 if (!promotion_failure_occurred) {
132 // If there was no promotion failure, the preserved mark stacks
133 // should be empty.
134 _preserved_marks_set->assert_empty();
135 }
136 return promotion_failure_occurred;
137 }
138
139 #if TASKQUEUE_STATS
140
141 void PSPromotionManager::print_and_reset_taskqueue_stats() {
142 stack_array_depth()->print_and_reset_taskqueue_stats("Young GC");
143
144 auto get_pa_stats = [&](uint i) {
145 return manager_array(i)->partial_array_task_stats();
146 };
147 PartialArrayTaskStats::log_set(ParallelGCThreads, get_pa_stats,
148 "Young GC Partial Array");
149 for (uint i = 0; i < ParallelGCThreads; ++i) {
150 get_pa_stats(i)->reset();
151 }
152 }
153
154 PartialArrayTaskStats* PSPromotionManager::partial_array_task_stats() {
155 return _partial_array_splitter.stats();
156 }
157
158 #endif // TASKQUEUE_STATS
159
160 // Most members are initialized either by initialize() or reset().
161 PSPromotionManager::PSPromotionManager()
162 : _partial_array_splitter(_partial_array_state_manager, ParallelGCThreads)
163 {
164 // We set the old lab's start array.
165 _old_lab.set_start_array(old_gen()->start_array());
166
167 // let's choose 1.5x the chunk size
168 _min_array_size_for_chunking = (3 * ParGCArrayScanChunk / 2);
169
170 _preserved_marks = nullptr;
171
172 reset();
173 }
174
175 void PSPromotionManager::reset() {
176 assert(stacks_empty(), "reset of non-empty stack");
177
178 // We need to get an assert in here to make sure the labs are always flushed.
179
180 // Do not prefill the LAB's, save heap wastage!
181 HeapWord* lab_base = young_space()->top();
182 _young_lab.initialize(MemRegion(lab_base, (size_t)0));
183 _young_gen_has_alloc_failure = false;
184 _young_gen_is_full = false;
185
186 lab_base = old_gen()->object_space()->top();
187 _old_lab.initialize(MemRegion(lab_base, (size_t)0));
188 _old_gen_is_full = false;
189
190 _promotion_failed_info.reset();
191 }
192
193 void PSPromotionManager::register_preserved_marks(PreservedMarks* preserved_marks) {
194 assert(_preserved_marks == nullptr, "do not set it twice");
195 _preserved_marks = preserved_marks;
196 }
197
198 void PSPromotionManager::restore_preserved_marks() {
199 _preserved_marks_set->restore(&ParallelScavengeHeap::heap()->workers());
200 }
201
202 void PSPromotionManager::trim_stacks_to_threshold(uint threshold) {
203 PSScannerTasksQueue* const tq = claimed_stack_depth();
204 do {
205 ScannerTask task;
206
207 // Drain overflow stack first, so other threads can steal from
208 // claimed stack while we work.
209 while (tq->pop_overflow(task)) {
210 if (!tq->try_push_to_taskqueue(task)) {
211 process_popped_location_depth(task, false);
212 }
213 }
214
215 while (tq->pop_local(task, threshold)) {
216 process_popped_location_depth(task, false);
217 }
218 } while (!tq->overflow_empty());
219
220 assert(tq->size() <= threshold, "Sanity");
221 assert(tq->overflow_empty(), "Sanity");
222 }
223
224 void PSPromotionManager::trim_stacks() {
225 const uint target_stack_size = GCDrainStackTargetSize;
226 const uint max_stack_size = target_stack_size * 2 + 1;
227
228 PSScannerTasksQueue* const tq = claimed_stack_depth();
229 if (!tq->overflow_empty() || tq->size() > max_stack_size) {
230 trim_stacks_to_threshold(target_stack_size);
231 }
232 }
233
234 void PSPromotionManager::drain_stacks() {
235 trim_stacks_to_threshold(0);
236 }
237
238 void PSPromotionManager::flush_labs() {
239 assert(stacks_empty(), "Attempt to flush lab with live stack");
240
241 // If either promotion lab fills up, we can flush the
242 // lab but not refill it, so check first.
243 assert(!_young_lab.is_flushed() || _young_gen_is_full, "Sanity");
244 if (!_young_lab.is_flushed())
245 _young_lab.flush();
246
247 assert(!_old_lab.is_flushed() || _old_gen_is_full, "Sanity");
248 if (!_old_lab.is_flushed())
249 _old_lab.flush();
250
251 // Let PSScavenge know if we overflowed
252 if (_young_gen_is_full || _young_gen_has_alloc_failure) {
253 PSScavenge::set_survivor_overflow(true);
254 }
255 }
256
257 void PSPromotionManager::process_array_chunk(objArrayOop obj, size_t start, size_t end) {
258 PSPushContentsClosure pcc(this);
259 obj->oop_iterate_elements_range(&pcc,
260 checked_cast<int>(start),
261 checked_cast<int>(end));
262 }
263
264 void PSPromotionManager::process_array_chunk(PartialArrayState* state, bool stolen) {
265 // Access before release by claim().
266 objArrayOop to_array = objArrayOop(state->destination());
267 PartialArraySplitter::Claim claim =
268 _partial_array_splitter.claim(state, &_claimed_stack_depth, stolen);
269 process_array_chunk(to_array, claim._start, claim._end);
270 }
271
272 void PSPromotionManager::push_objArray(oop old_obj, oop new_obj) {
273 assert(old_obj->is_forwarded(), "precondition");
274 assert(old_obj->forwardee() == new_obj, "precondition");
275 assert(new_obj->is_objArray(), "precondition");
276
277 objArrayOop to_array = objArrayOop(new_obj);
278 size_t array_length = to_array->length();
279 size_t initial_chunk_size =
280 // The source array is unused when processing states.
281 _partial_array_splitter.start(&_claimed_stack_depth, nullptr, to_array, array_length, ParGCArrayScanChunk);
282
283 process_array_chunk(to_array, 0, initial_chunk_size);
284 }
285
286 oop PSPromotionManager::oop_promotion_failed(oop obj, markWord obj_mark) {
287 assert(_old_gen_is_full || PromotionFailureALot, "Sanity");
288
289 // Attempt to CAS in the header.
290 // This tests if the header is still the same as when
291 // this started. If it is the same (i.e., no forwarding
292 // pointer has been installed), then this thread owns
293 // it.
294 if (obj->forward_to_self_atomic(obj_mark) == nullptr) {
295 // We won any races, we "own" this object.
296 assert(obj == obj->forwardee(), "Sanity");
297
298 _promotion_failed_info.register_copy_failure(obj->size());
299
300 ContinuationGCSupport::transform_stack_chunk(obj);
301
302 push_contents(obj, obj->klass());
303
304 // Save the markWord of promotion-failed objs in _preserved_marks for later
305 // restoration. This way we don't have to walk the young-gen to locate
306 // these promotion-failed objs.
307 _preserved_marks->push_always(obj, obj_mark);
308 } else {
309 // We lost, someone else "owns" this object
310 guarantee(obj->is_forwarded(), "Object must be forwarded if the cas failed.");
311
312 // No unallocation to worry about.
313 obj = obj->forwardee();
314 }
315
316 return obj;
317 }