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