1 /* 2 * Copyright (c) 2002, 2022, 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 = NULL; 48 PSPromotionManager::PSScannerTasksQueueSet* PSPromotionManager::_stack_array_depth = NULL; 49 PreservedMarksSet* PSPromotionManager::_preserved_marks_set = NULL; 50 PSOldGen* PSPromotionManager::_old_gen = NULL; 51 MutableSpace* PSPromotionManager::_young_space = NULL; 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 == NULL, "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 == NULL, "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 != NULL, "Sanity"); 95 return &_manager_array[index]; 96 } 97 98 PSPromotionManager* PSPromotionManager::vm_thread_promotion_manager() { 99 assert(_manager_array != NULL, "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; 183 queue_size = claimed_stack_depth()->max_elems(); 184 185 if (ParallelGCThreads == 1) { 186 _target_stack_size = 0; 187 } else { 188 // don't let the target stack size to be more than 1/4 of the entries 189 _target_stack_size = (uint) MIN2((uint) GCDrainStackTargetSize, 190 (uint) (queue_size / 4)); 191 } 192 193 _array_chunk_size = ParGCArrayScanChunk; 194 // let's choose 1.5x the chunk size 195 _min_array_size_for_chunking = 3 * _array_chunk_size / 2; 196 197 _preserved_marks = NULL; 198 199 reset(); 200 } 201 202 void PSPromotionManager::reset() { 203 assert(stacks_empty(), "reset of non-empty stack"); 204 205 // We need to get an assert in here to make sure the labs are always flushed. 206 207 // Do not prefill the LAB's, save heap wastage! 208 HeapWord* lab_base = young_space()->top(); 209 _young_lab.initialize(MemRegion(lab_base, (size_t)0)); 210 _young_gen_is_full = false; 211 212 lab_base = old_gen()->object_space()->top(); 213 _old_lab.initialize(MemRegion(lab_base, (size_t)0)); 214 _old_gen_is_full = false; 215 216 _promotion_failed_info.reset(); 217 218 TASKQUEUE_STATS_ONLY(reset_stats()); 219 } 220 221 void PSPromotionManager::register_preserved_marks(PreservedMarks* preserved_marks) { 222 assert(_preserved_marks == NULL, "do not set it twice"); 223 _preserved_marks = preserved_marks; 224 } 225 226 void PSPromotionManager::restore_preserved_marks() { 227 _preserved_marks_set->restore(&ParallelScavengeHeap::heap()->workers()); 228 } 229 230 void PSPromotionManager::drain_stacks_depth(bool totally_drain) { 231 totally_drain = totally_drain || (_target_stack_size == 0); 232 233 PSScannerTasksQueue* const tq = claimed_stack_depth(); 234 do { 235 ScannerTask task; 236 237 // Drain overflow stack first, so other threads can steal from 238 // claimed stack while we work. 239 while (tq->pop_overflow(task)) { 240 process_popped_location_depth(task); 241 } 242 243 if (totally_drain) { 244 while (tq->pop_local(task)) { 245 process_popped_location_depth(task); 246 } 247 } else { 248 while (tq->size() > _target_stack_size && tq->pop_local(task)) { 249 process_popped_location_depth(task); 250 } 251 } 252 } while ((totally_drain && !tq->taskqueue_empty()) || !tq->overflow_empty()); 253 254 assert(!totally_drain || tq->taskqueue_empty(), "Sanity"); 255 assert(totally_drain || tq->size() <= _target_stack_size, "Sanity"); 256 assert(tq->overflow_empty(), "Sanity"); 257 } 258 259 void PSPromotionManager::flush_labs() { 260 assert(stacks_empty(), "Attempt to flush lab with live stack"); 261 262 // If either promotion lab fills up, we can flush the 263 // lab but not refill it, so check first. 264 assert(!_young_lab.is_flushed() || _young_gen_is_full, "Sanity"); 265 if (!_young_lab.is_flushed()) 266 _young_lab.flush(); 267 268 assert(!_old_lab.is_flushed() || _old_gen_is_full, "Sanity"); 269 if (!_old_lab.is_flushed()) 270 _old_lab.flush(); 271 272 // Let PSScavenge know if we overflowed 273 if (_young_gen_is_full) { 274 PSScavenge::set_survivor_overflow(true); 275 } 276 } 277 278 template <class T> void PSPromotionManager::process_array_chunk_work( 279 oop obj, 280 int start, int end) { 281 assert(start <= end, "invariant"); 282 T* const base = (T*)objArrayOop(obj)->base(); 283 T* p = base + start; 284 T* const chunk_end = base + end; 285 while (p < chunk_end) { 286 if (PSScavenge::should_scavenge(p)) { 287 claim_or_forward_depth(p); 288 } 289 ++p; 290 } 291 } 292 293 void PSPromotionManager::process_array_chunk(PartialArrayScanTask task) { 294 assert(PSChunkLargeArrays, "invariant"); 295 296 oop old = task.to_source_array(); 297 assert(old->is_objArray(), "invariant"); 298 assert(old->is_forwarded(), "invariant"); 299 300 TASKQUEUE_STATS_ONLY(++_array_chunks_processed); 301 302 oop const obj = old->forwardee(); 303 304 int start; 305 int const end = arrayOop(old)->length(); 306 if (end > (int) _min_array_size_for_chunking) { 307 // we'll chunk more 308 start = end - _array_chunk_size; 309 assert(start > 0, "invariant"); 310 arrayOop(old)->set_length(start); 311 push_depth(ScannerTask(PartialArrayScanTask(old))); 312 TASKQUEUE_STATS_ONLY(++_array_chunk_pushes); 313 } else { 314 // this is the final chunk for this array 315 start = 0; 316 int const actual_length = arrayOop(obj)->length(); 317 arrayOop(old)->set_length(actual_length); 318 } 319 320 if (UseCompressedOops) { 321 process_array_chunk_work<narrowOop>(obj, start, end); 322 } else { 323 process_array_chunk_work<oop>(obj, start, end); 324 } 325 } 326 327 oop PSPromotionManager::oop_promotion_failed(oop obj, markWord obj_mark) { 328 assert(_old_gen_is_full || PromotionFailureALot, "Sanity"); 329 330 // Attempt to CAS in the header. 331 // This tests if the header is still the same as when 332 // this started. If it is the same (i.e., no forwarding 333 // pointer has been installed), then this thread owns 334 // it. 335 if (obj->forward_to_atomic(obj, obj_mark) == NULL) { 336 // We won any races, we "own" this object. 337 assert(obj == obj->forwardee(), "Sanity"); 338 339 _promotion_failed_info.register_copy_failure(obj->size()); 340 341 ContinuationGCSupport::transform_stack_chunk(obj); 342 343 push_contents(obj); 344 345 // Save the markWord of promotion-failed objs in _preserved_marks for later 346 // restoration. This way we don't have to walk the young-gen to locate 347 // these promotion-failed objs. 348 _preserved_marks->push_always(obj, obj_mark); 349 } else { 350 // We lost, someone else "owns" this object 351 guarantee(obj->is_forwarded(), "Object must be forwarded if the cas failed."); 352 353 // No unallocation to worry about. 354 obj = obj->forwardee(); 355 } 356 357 return obj; 358 }