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, ParGCArrayScanChunk) 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_has_alloc_failure = false; 198 _young_gen_is_full = false; 199 200 lab_base = old_gen()->object_space()->top(); 201 _old_lab.initialize(MemRegion(lab_base, (size_t)0)); 202 _old_gen_is_full = false; 203 204 _promotion_failed_info.reset(); 205 } 206 207 void PSPromotionManager::register_preserved_marks(PreservedMarks* preserved_marks) { 208 assert(_preserved_marks == nullptr, "do not set it twice"); 209 _preserved_marks = preserved_marks; 210 } 211 212 void PSPromotionManager::restore_preserved_marks() { 213 _preserved_marks_set->restore(&ParallelScavengeHeap::heap()->workers()); 214 } 215 216 void PSPromotionManager::drain_stacks_depth(bool totally_drain) { 217 const uint threshold = totally_drain ? 0 218 : _target_stack_size; 219 220 PSScannerTasksQueue* const tq = claimed_stack_depth(); 221 do { 222 ScannerTask task; 223 224 // Drain overflow stack first, so other threads can steal from 225 // claimed stack while we work. 226 while (tq->pop_overflow(task)) { 227 if (!tq->try_push_to_taskqueue(task)) { 228 process_popped_location_depth(task, false); 229 } 230 } 231 232 while (tq->pop_local(task, threshold)) { 233 process_popped_location_depth(task, false); 234 } 235 } while (!tq->overflow_empty()); 236 237 assert(!totally_drain || tq->taskqueue_empty(), "Sanity"); 238 assert(totally_drain || tq->size() <= _target_stack_size, "Sanity"); 239 assert(tq->overflow_empty(), "Sanity"); 240 } 241 242 void PSPromotionManager::flush_labs() { 243 assert(stacks_empty(), "Attempt to flush lab with live stack"); 244 245 // If either promotion lab fills up, we can flush the 246 // lab but not refill it, so check first. 247 assert(!_young_lab.is_flushed() || _young_gen_is_full, "Sanity"); 248 if (!_young_lab.is_flushed()) 249 _young_lab.flush(); 250 251 assert(!_old_lab.is_flushed() || _old_gen_is_full, "Sanity"); 252 if (!_old_lab.is_flushed()) 253 _old_lab.flush(); 254 255 // Let PSScavenge know if we overflowed 256 if (_young_gen_is_full || _young_gen_has_alloc_failure) { 257 PSScavenge::set_survivor_overflow(true); 258 } 259 } 260 261 template <class T> 262 void PSPromotionManager::process_array_chunk_work(oop obj, int start, int end) { 263 assert(start <= end, "invariant"); 264 T* const base = (T*)objArrayOop(obj)->base(); 265 T* p = base + start; 266 T* const chunk_end = base + end; 267 while (p < chunk_end) { 268 claim_or_forward_depth(p); 269 ++p; 270 } 271 } 272 273 void PSPromotionManager::process_array_chunk(PartialArrayState* state, bool stolen) { 274 // Access before release by claim(). 275 oop new_obj = state->destination(); 276 PartialArraySplitter::Claim claim = 277 _partial_array_splitter.claim(state, &_claimed_stack_depth, stolen); 278 int start = checked_cast<int>(claim._start); 279 int end = checked_cast<int>(claim._end); 280 if (UseCompressedOops) { 281 process_array_chunk_work<narrowOop>(new_obj, start, end); 282 } else { 283 process_array_chunk_work<oop>(new_obj, start, end); 284 } 285 } 286 287 void PSPromotionManager::push_objArray(oop old_obj, oop new_obj) { 288 assert(old_obj->is_forwarded(), "precondition"); 289 assert(old_obj->forwardee() == new_obj, "precondition"); 290 assert(new_obj->is_objArray(), "precondition"); 291 292 objArrayOop to_array = objArrayOop(new_obj); 293 size_t array_length = to_array->length(); 294 size_t initial_chunk_size = 295 // The source array is unused when processing states. 296 _partial_array_splitter.start(&_claimed_stack_depth, nullptr, to_array, array_length); 297 int end = checked_cast<int>(initial_chunk_size); 298 if (UseCompressedOops) { 299 process_array_chunk_work<narrowOop>(to_array, 0, end); 300 } else { 301 process_array_chunk_work<oop>(to_array, 0, end); 302 } 303 } 304 305 oop PSPromotionManager::oop_promotion_failed(oop obj, markWord obj_mark) { 306 assert(_old_gen_is_full || PromotionFailureALot, "Sanity"); 307 308 // Attempt to CAS in the header. 309 // This tests if the header is still the same as when 310 // this started. If it is the same (i.e., no forwarding 311 // pointer has been installed), then this thread owns 312 // it. 313 if (obj->forward_to_self_atomic(obj_mark) == nullptr) { 314 // We won any races, we "own" this object. 315 assert(obj == obj->forwardee(), "Sanity"); 316 317 _promotion_failed_info.register_copy_failure(obj->size()); 318 319 ContinuationGCSupport::transform_stack_chunk(obj); 320 321 push_contents(obj); 322 323 // Save the markWord of promotion-failed objs in _preserved_marks for later 324 // restoration. This way we don't have to walk the young-gen to locate 325 // these promotion-failed objs. 326 _preserved_marks->push_always(obj, obj_mark); 327 } else { 328 // We lost, someone else "owns" this object 329 guarantee(obj->is_forwarded(), "Object must be forwarded if the cas failed."); 330 331 // No unallocation to worry about. 332 obj = obj->forwardee(); 333 } 334 335 return obj; 336 }