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