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