1 /* 2 * Copyright (c) 2002, 2023, 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 "precompiled.hpp" 26 #include "classfile/javaClasses.inline.hpp" 27 #include "gc/parallel/mutableSpace.hpp" 28 #include "gc/parallel/parallelScavengeHeap.hpp" 29 #include "gc/parallel/psOldGen.hpp" 30 #include "gc/parallel/psPromotionManager.inline.hpp" 31 #include "gc/parallel/psScavenge.inline.hpp" 32 #include "gc/shared/continuationGCSupport.inline.hpp" 33 #include "gc/shared/gcTrace.hpp" 34 #include "gc/shared/preservedMarks.inline.hpp" 35 #include "gc/shared/taskqueue.inline.hpp" 36 #include "logging/log.hpp" 37 #include "logging/logStream.hpp" 38 #include "memory/allocation.inline.hpp" 39 #include "memory/iterator.inline.hpp" 40 #include "memory/memRegion.hpp" 41 #include "memory/padded.inline.hpp" 42 #include "memory/resourceArea.hpp" 43 #include "oops/access.inline.hpp" 44 #include "oops/compressedOops.inline.hpp" 45 #include "oops/flatArrayKlass.inline.hpp" 46 47 PaddedEnd<PSPromotionManager>* PSPromotionManager::_manager_array = nullptr; 48 PSPromotionManager::PSScannerTasksQueueSet* PSPromotionManager::_stack_array_depth = nullptr; 49 PreservedMarksSet* PSPromotionManager::_preserved_marks_set = nullptr; 50 PSOldGen* PSPromotionManager::_old_gen = nullptr; 51 MutableSpace* PSPromotionManager::_young_space = nullptr; 52 53 void PSPromotionManager::initialize() { 54 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); 55 56 _old_gen = heap->old_gen(); 57 _young_space = heap->young_gen()->to_space(); 58 59 const uint promotion_manager_num = ParallelGCThreads; 60 61 // To prevent false sharing, we pad the PSPromotionManagers 62 // and make sure that the first instance starts at a cache line. 63 assert(_manager_array == nullptr, "Attempt to initialize twice"); 64 _manager_array = PaddedArray<PSPromotionManager, mtGC>::create_unfreeable(promotion_manager_num); 65 66 _stack_array_depth = new PSScannerTasksQueueSet(ParallelGCThreads); 67 68 // Create and register the PSPromotionManager(s) for the worker threads. 69 for(uint i=0; i<ParallelGCThreads; i++) { 70 stack_array_depth()->register_queue(i, _manager_array[i].claimed_stack_depth()); 71 } 72 // The VMThread gets its own PSPromotionManager, which is not available 73 // for work stealing. 74 75 assert(_preserved_marks_set == nullptr, "Attempt to initialize twice"); 76 _preserved_marks_set = new PreservedMarksSet(true /* in_c_heap */); 77 _preserved_marks_set->init(promotion_manager_num); 78 for (uint i = 0; i < promotion_manager_num; i += 1) { 79 _manager_array[i].register_preserved_marks(_preserved_marks_set->get(i)); 80 } 81 } 82 83 // Helper functions to get around the circular dependency between 84 // psScavenge.inline.hpp and psPromotionManager.inline.hpp. 85 bool PSPromotionManager::should_scavenge(oop* p, bool check_to_space) { 86 return PSScavenge::should_scavenge(p, check_to_space); 87 } 88 bool PSPromotionManager::should_scavenge(narrowOop* p, bool check_to_space) { 89 return PSScavenge::should_scavenge(p, check_to_space); 90 } 91 92 PSPromotionManager* PSPromotionManager::gc_thread_promotion_manager(uint index) { 93 assert(index < ParallelGCThreads, "index out of range"); 94 assert(_manager_array != nullptr, "Sanity"); 95 return &_manager_array[index]; 96 } 97 98 PSPromotionManager* PSPromotionManager::vm_thread_promotion_manager() { 99 assert(_manager_array != nullptr, "Sanity"); 100 return &_manager_array[0]; 101 } 102 103 void PSPromotionManager::pre_scavenge() { 104 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); 105 106 _preserved_marks_set->assert_empty(); 107 _young_space = heap->young_gen()->to_space(); 108 109 for(uint i=0; i<ParallelGCThreads; i++) { 110 manager_array(i)->reset(); 111 } 112 } 113 114 bool PSPromotionManager::post_scavenge(YoungGCTracer& gc_tracer) { 115 bool promotion_failure_occurred = false; 116 117 TASKQUEUE_STATS_ONLY(print_taskqueue_stats()); 118 for (uint i = 0; i < ParallelGCThreads; i++) { 119 PSPromotionManager* manager = manager_array(i); 120 assert(manager->claimed_stack_depth()->is_empty(), "should be empty"); 121 if (manager->_promotion_failed_info.has_failed()) { 122 gc_tracer.report_promotion_failed(manager->_promotion_failed_info); 123 promotion_failure_occurred = true; 124 } 125 manager->flush_labs(); 126 manager->flush_string_dedup_requests(); 127 } 128 if (!promotion_failure_occurred) { 129 // If there was no promotion failure, the preserved mark stacks 130 // should be empty. 131 _preserved_marks_set->assert_empty(); 132 } 133 return promotion_failure_occurred; 134 } 135 136 #if TASKQUEUE_STATS 137 void 138 PSPromotionManager::print_local_stats(outputStream* const out, uint i) const { 139 #define FMT " " SIZE_FORMAT_W(10) 140 out->print_cr("%3u" FMT FMT FMT FMT, 141 i, _array_chunk_pushes, _array_chunk_steals, 142 _arrays_chunked, _array_chunks_processed); 143 #undef FMT 144 } 145 146 static const char* const pm_stats_hdr[] = { 147 " ----partial array---- arrays array", 148 "thr push steal chunked chunks", 149 "--- ---------- ---------- ---------- ----------" 150 }; 151 152 void PSPromotionManager::print_taskqueue_stats() { 153 if (!log_is_enabled(Trace, gc, task, stats)) { 154 return; 155 } 156 Log(gc, task, stats) log; 157 ResourceMark rm; 158 LogStream ls(log.trace()); 159 160 stack_array_depth()->print_taskqueue_stats(&ls, "Oop Queue"); 161 162 const uint hlines = sizeof(pm_stats_hdr) / sizeof(pm_stats_hdr[0]); 163 for (uint i = 0; i < hlines; ++i) ls.print_cr("%s", pm_stats_hdr[i]); 164 for (uint i = 0; i < ParallelGCThreads; ++i) { 165 manager_array(i)->print_local_stats(&ls, i); 166 } 167 } 168 169 void PSPromotionManager::reset_stats() { 170 claimed_stack_depth()->stats.reset(); 171 _array_chunk_pushes = _array_chunk_steals = 0; 172 _arrays_chunked = _array_chunks_processed = 0; 173 } 174 #endif // TASKQUEUE_STATS 175 176 PSPromotionManager::PSPromotionManager() { 177 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); 178 179 // We set the old lab's start array. 180 _old_lab.set_start_array(old_gen()->start_array()); 181 182 uint queue_size = claimed_stack_depth()->max_elems(); 183 184 if (ParallelGCThreads == 1) { 185 _target_stack_size = 0; 186 } else { 187 _target_stack_size = GCDrainStackTargetSize; 188 } 189 190 _array_chunk_size = ParGCArrayScanChunk; 191 // let's choose 1.5x the chunk size 192 _min_array_size_for_chunking = 3 * _array_chunk_size / 2; 193 194 _preserved_marks = nullptr; 195 196 reset(); 197 } 198 199 void PSPromotionManager::reset() { 200 assert(stacks_empty(), "reset of non-empty stack"); 201 202 // We need to get an assert in here to make sure the labs are always flushed. 203 204 // Do not prefill the LAB's, save heap wastage! 205 HeapWord* lab_base = young_space()->top(); 206 _young_lab.initialize(MemRegion(lab_base, (size_t)0)); 207 _young_gen_is_full = false; 208 209 lab_base = old_gen()->object_space()->top(); 210 _old_lab.initialize(MemRegion(lab_base, (size_t)0)); 211 _old_gen_is_full = false; 212 213 _promotion_failed_info.reset(); 214 215 TASKQUEUE_STATS_ONLY(reset_stats()); 216 } 217 218 void PSPromotionManager::register_preserved_marks(PreservedMarks* preserved_marks) { 219 assert(_preserved_marks == nullptr, "do not set it twice"); 220 _preserved_marks = preserved_marks; 221 } 222 223 void PSPromotionManager::restore_preserved_marks() { 224 _preserved_marks_set->restore(&ParallelScavengeHeap::heap()->workers()); 225 } 226 227 void PSPromotionManager::drain_stacks_depth(bool totally_drain) { 228 const uint threshold = totally_drain ? 0 229 : _target_stack_size; 230 231 PSScannerTasksQueue* const tq = claimed_stack_depth(); 232 do { 233 ScannerTask task; 234 235 // Drain overflow stack first, so other threads can steal from 236 // claimed stack while we work. 237 while (tq->pop_overflow(task)) { 238 if (!tq->try_push_to_taskqueue(task)) { 239 process_popped_location_depth(task); 240 } 241 } 242 243 while (tq->pop_local(task, threshold)) { 244 process_popped_location_depth(task); 245 } 246 } while (!tq->overflow_empty()); 247 248 assert(!totally_drain || tq->taskqueue_empty(), "Sanity"); 249 assert(totally_drain || tq->size() <= _target_stack_size, "Sanity"); 250 assert(tq->overflow_empty(), "Sanity"); 251 } 252 253 void PSPromotionManager::flush_labs() { 254 assert(stacks_empty(), "Attempt to flush lab with live stack"); 255 256 // If either promotion lab fills up, we can flush the 257 // lab but not refill it, so check first. 258 assert(!_young_lab.is_flushed() || _young_gen_is_full, "Sanity"); 259 if (!_young_lab.is_flushed()) 260 _young_lab.flush(); 261 262 assert(!_old_lab.is_flushed() || _old_gen_is_full, "Sanity"); 263 if (!_old_lab.is_flushed()) 264 _old_lab.flush(); 265 266 // Let PSScavenge know if we overflowed 267 if (_young_gen_is_full) { 268 PSScavenge::set_survivor_overflow(true); 269 } 270 } 271 272 template <class T> void PSPromotionManager::process_array_chunk_work( 273 oop obj, 274 int start, int end) { 275 assert(start <= end, "invariant"); 276 T* const base = (T*)objArrayOop(obj)->base(); 277 T* p = base + start; 278 T* const chunk_end = base + end; 279 while (p < chunk_end) { 280 if (PSScavenge::should_scavenge(p)) { 281 claim_or_forward_depth(p); 282 } 283 ++p; 284 } 285 } 286 287 void PSPromotionManager::process_array_chunk(PartialArrayScanTask task) { 288 assert(PSChunkLargeArrays, "invariant"); 289 290 oop old = task.to_source_array(); 291 assert(old->is_objArray(), "invariant"); 292 assert(old->is_forwarded(), "invariant"); 293 294 TASKQUEUE_STATS_ONLY(++_array_chunks_processed); 295 296 oop const obj = old->forwardee(); 297 298 int start; 299 int const end = arrayOop(old)->length(); 300 if (end > (int) _min_array_size_for_chunking) { 301 // we'll chunk more 302 start = end - _array_chunk_size; 303 assert(start > 0, "invariant"); 304 arrayOop(old)->set_length(start); 305 push_depth(ScannerTask(PartialArrayScanTask(old))); 306 TASKQUEUE_STATS_ONLY(++_array_chunk_pushes); 307 } else { 308 // this is the final chunk for this array 309 start = 0; 310 int const actual_length = arrayOop(obj)->length(); 311 arrayOop(old)->set_length(actual_length); 312 } 313 314 if (UseCompressedOops) { 315 process_array_chunk_work<narrowOop>(obj, start, end); 316 } else { 317 process_array_chunk_work<oop>(obj, start, end); 318 } 319 } 320 321 oop PSPromotionManager::oop_promotion_failed(oop obj, markWord obj_mark) { 322 assert(_old_gen_is_full || PromotionFailureALot, "Sanity"); 323 324 // Attempt to CAS in the header. 325 // This tests if the header is still the same as when 326 // this started. If it is the same (i.e., no forwarding 327 // pointer has been installed), then this thread owns 328 // it. 329 if (obj->forward_to_atomic(obj, obj_mark) == nullptr) { 330 // We won any races, we "own" this object. 331 assert(obj == obj->forwardee(), "Sanity"); 332 333 _promotion_failed_info.register_copy_failure(obj->size()); 334 335 ContinuationGCSupport::transform_stack_chunk(obj); 336 337 push_contents(obj); 338 339 // Save the markWord of promotion-failed objs in _preserved_marks for later 340 // restoration. This way we don't have to walk the young-gen to locate 341 // these promotion-failed objs. 342 _preserved_marks->push_always(obj, obj_mark); 343 } else { 344 // We lost, someone else "owns" this object 345 guarantee(obj->is_forwarded(), "Object must be forwarded if the cas failed."); 346 347 // No unallocation to worry about. 348 obj = obj->forwardee(); 349 } 350 351 return obj; 352 }