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