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/oopsHierarchy.hpp"
47 #include "utilities/checkedCast.hpp"
48 #include "utilities/debug.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("Oop Queue");
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 "Partial Array Task Stats");
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, ParGCArrayScanChunk)
163 {
164 // We set the old lab's start array.
165 _old_lab.set_start_array(old_gen()->start_array());
166
167 if (ParallelGCThreads == 1) {
168 _target_stack_size = 0;
169 } else {
170 _target_stack_size = GCDrainStackTargetSize;
171 }
172
173 // let's choose 1.5x the chunk size
174 _min_array_size_for_chunking = (3 * ParGCArrayScanChunk / 2);
175
176 _preserved_marks = nullptr;
177
178 reset();
179 }
180
181 void PSPromotionManager::reset() {
182 assert(stacks_empty(), "reset of non-empty stack");
183
184 // We need to get an assert in here to make sure the labs are always flushed.
185
186 // Do not prefill the LAB's, save heap wastage!
187 HeapWord* lab_base = young_space()->top();
188 _young_lab.initialize(MemRegion(lab_base, (size_t)0));
189 _young_gen_has_alloc_failure = false;
190 _young_gen_is_full = false;
191
192 lab_base = old_gen()->object_space()->top();
193 _old_lab.initialize(MemRegion(lab_base, (size_t)0));
194 _old_gen_is_full = false;
195
196 _promotion_failed_info.reset();
197 }
198
199 void PSPromotionManager::register_preserved_marks(PreservedMarks* preserved_marks) {
200 assert(_preserved_marks == nullptr, "do not set it twice");
201 _preserved_marks = preserved_marks;
202 }
203
204 void PSPromotionManager::restore_preserved_marks() {
205 _preserved_marks_set->restore(&ParallelScavengeHeap::heap()->workers());
206 }
207
208 void PSPromotionManager::drain_stacks(bool totally_drain) {
209 const uint threshold = totally_drain ? 0
210 : _target_stack_size;
211
212 PSScannerTasksQueue* const tq = claimed_stack_depth();
213 do {
214 ScannerTask task;
215
216 // Drain overflow stack first, so other threads can steal from
217 // claimed stack while we work.
218 while (tq->pop_overflow(task)) {
219 if (!tq->try_push_to_taskqueue(task)) {
220 process_popped_location_depth(task, false);
221 }
222 }
223
224 while (tq->pop_local(task, threshold)) {
225 process_popped_location_depth(task, false);
226 }
227 } while (!tq->overflow_empty());
228
229 assert(!totally_drain || tq->taskqueue_empty(), "Sanity");
230 assert(totally_drain || tq->size() <= _target_stack_size, "Sanity");
231 assert(tq->overflow_empty(), "Sanity");
232 }
233
234 void PSPromotionManager::flush_labs() {
235 assert(stacks_empty(), "Attempt to flush lab with live stack");
236
237 // If either promotion lab fills up, we can flush the
238 // lab but not refill it, so check first.
239 assert(!_young_lab.is_flushed() || _young_gen_is_full, "Sanity");
240 if (!_young_lab.is_flushed())
241 _young_lab.flush();
242
243 assert(!_old_lab.is_flushed() || _old_gen_is_full, "Sanity");
244 if (!_old_lab.is_flushed())
245 _old_lab.flush();
246
247 // Let PSScavenge know if we overflowed
248 if (_young_gen_is_full || _young_gen_has_alloc_failure) {
249 PSScavenge::set_survivor_overflow(true);
250 }
251 }
252
253 void PSPromotionManager::process_array_chunk(objArrayOop obj, size_t start, size_t end) {
254 PSPushContentsClosure pcc(this);
255 obj->oop_iterate_elements_range(&pcc,
256 checked_cast<int>(start),
257 checked_cast<int>(end));
258 }
259
260 void PSPromotionManager::process_array_chunk(PartialArrayState* state, bool stolen) {
261 // Access before release by claim().
262 objArrayOop to_array = objArrayOop(state->destination());
263 precond(to_array->is_array_with_oops());
264
265 PartialArraySplitter::Claim claim =
266 _partial_array_splitter.claim(state, &_claimed_stack_depth, stolen);
267
268 process_array_chunk(to_array, claim._start, claim._end);
269 }
270
271 void PSPromotionManager::push_objArray(oop old_obj, oop new_obj) {
272 assert(old_obj->is_forwarded(), "precondition");
273 assert(old_obj->forwardee() == new_obj, "precondition");
274 precond(new_obj->is_array_with_oops());
275
276 objArrayOop to_array = objArrayOop(new_obj);
277 size_t array_length = to_array->length();
278 size_t initial_chunk_size =
279 // The source array is unused when processing states.
280 _partial_array_splitter.start(&_claimed_stack_depth, nullptr, to_array, array_length);
281
282 process_array_chunk(to_array, 0, initial_chunk_size);
283 }
284
285 oop PSPromotionManager::oop_promotion_failed(oop obj, markWord obj_mark) {
286 assert(_old_gen_is_full || PromotionFailureALot, "Sanity");
287
288 // Attempt to CAS in the header.
289 // This tests if the header is still the same as when
290 // this started. If it is the same (i.e., no forwarding
291 // pointer has been installed), then this thread owns
292 // it.
293 if (obj->forward_to_self_atomic(obj_mark) == nullptr) {
294 // We won any races, we "own" this object.
295 assert(obj == obj->forwardee(), "Sanity");
296
297 _promotion_failed_info.register_copy_failure(obj->size());
298
299 ContinuationGCSupport::transform_stack_chunk(obj);
300
301 push_contents(obj);
302
303 // Save the markWord of promotion-failed objs in _preserved_marks for later
304 // restoration. This way we don't have to walk the young-gen to locate
305 // these promotion-failed objs.
306 _preserved_marks->push_always(obj, obj_mark);
307 } else {
308 // We lost, someone else "owns" this object
309 guarantee(obj->is_forwarded(), "Object must be forwarded if the cas failed.");
310
311 // No unallocation to worry about.
312 obj = obj->forwardee();
313 }
314
315 return obj;
316 }