1 /* 2 * Copyright (c) 2002, 2018, 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 "gc/parallel/gcTaskManager.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/memRegion.hpp" 39 #include "memory/padded.inline.hpp" 40 #include "memory/resourceArea.hpp" 41 #include "oops/arrayOop.inline.hpp" 42 #include "oops/instanceKlass.inline.hpp" 43 #include "oops/instanceMirrorKlass.inline.hpp" 44 #include "oops/objArrayKlass.inline.hpp" 45 #include "oops/objArrayOop.inline.hpp" 46 #include "oops/oop.inline.hpp" 47 48 PaddedEnd<PSPromotionManager>* PSPromotionManager::_manager_array = NULL; 49 OopStarTaskQueueSet* PSPromotionManager::_stack_array_depth = NULL; 50 PreservedMarksSet* PSPromotionManager::_preserved_marks_set = NULL; 51 PSOldGen* PSPromotionManager::_old_gen = NULL; 52 MutableSpace* PSPromotionManager::_young_space = NULL; 53 54 void PSPromotionManager::initialize() { 55 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); 56 57 _old_gen = heap->old_gen(); 58 _young_space = heap->young_gen()->to_space(); 59 60 const uint promotion_manager_num = ParallelGCThreads + 1; 61 62 // To prevent false sharing, we pad the PSPromotionManagers 63 // and make sure that the first instance starts at a cache line. 64 assert(_manager_array == NULL, "Attempt to initialize twice"); 65 _manager_array = PaddedArray<PSPromotionManager, mtGC>::create_unfreeable(promotion_manager_num); 66 guarantee(_manager_array != NULL, "Could not initialize promotion manager"); 67 68 _stack_array_depth = new OopStarTaskQueueSet(ParallelGCThreads); 69 guarantee(_stack_array_depth != NULL, "Could not initialize promotion manager"); 70 71 // Create and register the PSPromotionManager(s) for the worker threads. 72 for(uint i=0; i<ParallelGCThreads; i++) { 73 stack_array_depth()->register_queue(i, _manager_array[i].claimed_stack_depth()); 74 } 75 // The VMThread gets its own PSPromotionManager, which is not available 76 // for work stealing. 77 78 assert(_preserved_marks_set == NULL, "Attempt to initialize twice"); 79 _preserved_marks_set = new PreservedMarksSet(true /* in_c_heap */); 80 guarantee(_preserved_marks_set != NULL, "Could not initialize preserved marks set"); 81 _preserved_marks_set->init(promotion_manager_num); 82 for (uint i = 0; i < promotion_manager_num; i += 1) { 83 _manager_array[i].register_preserved_marks(_preserved_marks_set->get(i)); 84 } 85 } 86 87 // Helper functions to get around the circular dependency between 88 // psScavenge.inline.hpp and psPromotionManager.inline.hpp. 89 bool PSPromotionManager::should_scavenge(oop* p, bool check_to_space) { 90 return PSScavenge::should_scavenge(p, check_to_space); 91 } 92 bool PSPromotionManager::should_scavenge(narrowOop* p, bool check_to_space) { 93 return PSScavenge::should_scavenge(p, check_to_space); 94 } 95 96 PSPromotionManager* PSPromotionManager::gc_thread_promotion_manager(uint index) { 97 assert(index < ParallelGCThreads, "index out of range"); 98 assert(_manager_array != NULL, "Sanity"); 99 return &_manager_array[index]; 100 } 101 102 PSPromotionManager* PSPromotionManager::vm_thread_promotion_manager() { 103 assert(_manager_array != NULL, "Sanity"); 104 return &_manager_array[ParallelGCThreads]; 105 } 106 107 void PSPromotionManager::pre_scavenge() { 108 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); 109 110 _preserved_marks_set->assert_empty(); 111 _young_space = heap->young_gen()->to_space(); 112 113 for(uint i=0; i<ParallelGCThreads+1; i++) { 114 manager_array(i)->reset(); 115 } 116 } 117 118 bool PSPromotionManager::post_scavenge(YoungGCTracer& gc_tracer) { 119 bool promotion_failure_occurred = false; 120 121 TASKQUEUE_STATS_ONLY(print_taskqueue_stats()); 122 for (uint i = 0; i < ParallelGCThreads + 1; i++) { 123 PSPromotionManager* manager = manager_array(i); 124 assert(manager->claimed_stack_depth()->is_empty(), "should be empty"); 125 if (manager->_promotion_failed_info.has_failed()) { 126 gc_tracer.report_promotion_failed(manager->_promotion_failed_info); 127 promotion_failure_occurred = true; 128 } 129 manager->flush_labs(); 130 } 131 if (!promotion_failure_occurred) { 132 // If there was no promotion failure, the preserved mark stacks 133 // should be empty. 134 _preserved_marks_set->assert_empty(); 135 } 136 return promotion_failure_occurred; 137 } 138 139 #if TASKQUEUE_STATS 140 void 141 PSPromotionManager::print_local_stats(outputStream* const out, uint i) const { 142 #define FMT " " SIZE_FORMAT_W(10) 143 out->print_cr("%3u" FMT FMT FMT FMT, i, _masked_pushes, _masked_steals, 144 _arrays_chunked, _array_chunks_processed); 145 #undef FMT 146 } 147 148 static const char* const pm_stats_hdr[] = { 149 " --------masked------- arrays array", 150 "thr push steal chunked chunks", 151 "--- ---------- ---------- ---------- ----------" 152 }; 153 154 void 155 PSPromotionManager::print_taskqueue_stats() { 156 if (!log_develop_is_enabled(Trace, gc, task, stats)) { 157 return; 158 } 159 Log(gc, task, stats) log; 160 ResourceMark rm; 161 LogStream ls(log.trace()); 162 outputStream* out = &ls; 163 out->print_cr("== GC Tasks Stats, GC %3d", 164 ParallelScavengeHeap::heap()->total_collections()); 165 166 TaskQueueStats totals; 167 out->print("thr "); TaskQueueStats::print_header(1, out); out->cr(); 168 out->print("--- "); TaskQueueStats::print_header(2, out); out->cr(); 169 for (uint i = 0; i < ParallelGCThreads + 1; ++i) { 170 TaskQueueStats& next = manager_array(i)->_claimed_stack_depth.stats; 171 out->print("%3d ", i); next.print(out); out->cr(); 172 totals += next; 173 } 174 out->print("tot "); totals.print(out); out->cr(); 175 176 const uint hlines = sizeof(pm_stats_hdr) / sizeof(pm_stats_hdr[0]); 177 for (uint i = 0; i < hlines; ++i) out->print_cr("%s", pm_stats_hdr[i]); 178 for (uint i = 0; i < ParallelGCThreads + 1; ++i) { 179 manager_array(i)->print_local_stats(out, i); 180 } 181 } 182 183 void 184 PSPromotionManager::reset_stats() { 185 claimed_stack_depth()->stats.reset(); 186 _masked_pushes = _masked_steals = 0; 187 _arrays_chunked = _array_chunks_processed = 0; 188 } 189 #endif // TASKQUEUE_STATS 190 191 PSPromotionManager::PSPromotionManager() { 192 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); 193 194 // We set the old lab's start array. 195 _old_lab.set_start_array(old_gen()->start_array()); 196 197 uint queue_size; 198 claimed_stack_depth()->initialize(); 199 queue_size = claimed_stack_depth()->max_elems(); 200 201 _totally_drain = (ParallelGCThreads == 1) || (GCDrainStackTargetSize == 0); 202 if (_totally_drain) { 203 _target_stack_size = 0; 204 } else { 205 // don't let the target stack size to be more than 1/4 of the entries 206 _target_stack_size = (uint) MIN2((uint) GCDrainStackTargetSize, 207 (uint) (queue_size / 4)); 208 } 209 210 _array_chunk_size = ParGCArrayScanChunk; 211 // let's choose 1.5x the chunk size 212 _min_array_size_for_chunking = 3 * _array_chunk_size / 2; 213 214 _preserved_marks = NULL; 215 216 reset(); 217 } 218 219 void PSPromotionManager::reset() { 220 assert(stacks_empty(), "reset of non-empty stack"); 221 222 // We need to get an assert in here to make sure the labs are always flushed. 223 224 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); 225 226 // Do not prefill the LAB's, save heap wastage! 227 HeapWord* lab_base = young_space()->top(); 228 _young_lab.initialize(MemRegion(lab_base, (size_t)0)); 229 _young_gen_is_full = false; 230 231 lab_base = old_gen()->object_space()->top(); 232 _old_lab.initialize(MemRegion(lab_base, (size_t)0)); 233 _old_gen_is_full = false; 234 235 _promotion_failed_info.reset(); 236 237 TASKQUEUE_STATS_ONLY(reset_stats()); 238 } 239 240 void PSPromotionManager::register_preserved_marks(PreservedMarks* preserved_marks) { 241 assert(_preserved_marks == NULL, "do not set it twice"); 242 _preserved_marks = preserved_marks; 243 } 244 245 class ParRestoreGCTask : public GCTask { 246 private: 247 const uint _id; 248 PreservedMarksSet* const _preserved_marks_set; 249 volatile size_t* const _total_size_addr; 250 251 public: 252 virtual char* name() { 253 return (char*) "preserved mark restoration task"; 254 } 255 256 virtual void do_it(GCTaskManager* manager, uint which){ 257 _preserved_marks_set->get(_id)->restore_and_increment(_total_size_addr); 258 } 259 260 ParRestoreGCTask(uint id, 261 PreservedMarksSet* preserved_marks_set, 262 volatile size_t* total_size_addr) 263 : _id(id), 264 _preserved_marks_set(preserved_marks_set), 265 _total_size_addr(total_size_addr) { } 266 }; 267 268 class PSRestorePreservedMarksTaskExecutor : public RestorePreservedMarksTaskExecutor { 269 private: 270 GCTaskManager* _gc_task_manager; 271 272 public: 273 PSRestorePreservedMarksTaskExecutor(GCTaskManager* gc_task_manager) 274 : _gc_task_manager(gc_task_manager) { } 275 276 void restore(PreservedMarksSet* preserved_marks_set, 277 volatile size_t* total_size_addr) { 278 // GCTask / GCTaskQueue are ResourceObjs 279 ResourceMark rm; 280 281 GCTaskQueue* q = GCTaskQueue::create(); 282 for (uint i = 0; i < preserved_marks_set->num(); i += 1) { 283 q->enqueue(new ParRestoreGCTask(i, preserved_marks_set, total_size_addr)); 284 } 285 _gc_task_manager->execute_and_wait(q); 286 } 287 }; 288 289 void PSPromotionManager::restore_preserved_marks() { 290 PSRestorePreservedMarksTaskExecutor task_executor(PSScavenge::gc_task_manager()); 291 _preserved_marks_set->restore(&task_executor); 292 } 293 294 void PSPromotionManager::drain_stacks_depth(bool totally_drain) { 295 totally_drain = totally_drain || _totally_drain; 296 297 #ifdef ASSERT 298 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); 299 MutableSpace* to_space = heap->young_gen()->to_space(); 300 MutableSpace* old_space = heap->old_gen()->object_space(); 301 #endif /* ASSERT */ 302 303 OopStarTaskQueue* const tq = claimed_stack_depth(); 304 do { 305 StarTask p; 306 307 // Drain overflow stack first, so other threads can steal from 308 // claimed stack while we work. 309 while (tq->pop_overflow(p)) { 310 process_popped_location_depth(p); 311 } 312 313 if (totally_drain) { 314 while (tq->pop_local(p)) { 315 process_popped_location_depth(p); 316 } 317 } else { 318 while (tq->size() > _target_stack_size && tq->pop_local(p)) { 319 process_popped_location_depth(p); 320 } 321 } 322 } while ((totally_drain && !tq->taskqueue_empty()) || !tq->overflow_empty()); 323 324 assert(!totally_drain || tq->taskqueue_empty(), "Sanity"); 325 assert(totally_drain || tq->size() <= _target_stack_size, "Sanity"); 326 assert(tq->overflow_empty(), "Sanity"); 327 } 328 329 void PSPromotionManager::flush_labs() { 330 assert(stacks_empty(), "Attempt to flush lab with live stack"); 331 332 // If either promotion lab fills up, we can flush the 333 // lab but not refill it, so check first. 334 assert(!_young_lab.is_flushed() || _young_gen_is_full, "Sanity"); 335 if (!_young_lab.is_flushed()) 336 _young_lab.flush(); 337 338 assert(!_old_lab.is_flushed() || _old_gen_is_full, "Sanity"); 339 if (!_old_lab.is_flushed()) 340 _old_lab.flush(); 341 342 // Let PSScavenge know if we overflowed 343 if (_young_gen_is_full) { 344 PSScavenge::set_survivor_overflow(true); 345 } 346 } 347 348 template <class T> void PSPromotionManager::process_array_chunk_work( 349 oop obj, 350 int start, int end) { 351 assert(start <= end, "invariant"); 352 T* const base = (T*)objArrayOop(obj)->base(); 353 T* p = base + start; 354 T* const chunk_end = base + end; 355 while (p < chunk_end) { 356 if (PSScavenge::should_scavenge(p)) { 357 claim_or_forward_depth(p); 358 } 359 ++p; 360 } 361 } 362 363 void PSPromotionManager::process_array_chunk(oop old) { 364 assert(PSChunkLargeArrays, "invariant"); 365 assert(old->is_objArray(), "invariant"); 366 assert(old->is_forwarded(), "invariant"); 367 368 TASKQUEUE_STATS_ONLY(++_array_chunks_processed); 369 370 oop const obj = old->forwardee(); 371 372 int start; 373 int const end = arrayOop(old)->length(); 374 if (end > (int) _min_array_size_for_chunking) { 375 // we'll chunk more 376 start = end - _array_chunk_size; 377 assert(start > 0, "invariant"); 378 arrayOop(old)->set_length(start); 379 push_depth(mask_chunked_array_oop(old)); 380 TASKQUEUE_STATS_ONLY(++_masked_pushes); 381 } else { 382 // this is the final chunk for this array 383 start = 0; 384 int const actual_length = arrayOop(obj)->length(); 385 arrayOop(old)->set_length(actual_length); 386 } 387 388 if (UseCompressedOops) { 389 process_array_chunk_work<narrowOop>(obj, start, end); 390 } else { 391 process_array_chunk_work<oop>(obj, start, end); 392 } 393 } 394 395 class PushContentsClosure : public ExtendedOopClosure { 396 PSPromotionManager* _pm; 397 public: 398 PushContentsClosure(PSPromotionManager* pm) : _pm(pm) {} 399 400 template <typename T> void do_oop_nv(T* p) { 401 if (PSScavenge::should_scavenge(p)) { 402 _pm->claim_or_forward_depth(p); 403 } 404 } 405 406 virtual void do_oop(oop* p) { do_oop_nv(p); } 407 virtual void do_oop(narrowOop* p) { do_oop_nv(p); } 408 409 // Don't use the oop verification code in the oop_oop_iterate framework. 410 debug_only(virtual bool should_verify_oops() { return false; }) 411 }; 412 413 void InstanceKlass::oop_ps_push_contents(oop obj, PSPromotionManager* pm) { 414 PushContentsClosure cl(pm); 415 oop_oop_iterate_oop_maps_reverse<true>(obj, &cl); 416 } 417 418 void InstanceMirrorKlass::oop_ps_push_contents(oop obj, PSPromotionManager* pm) { 419 // Note that we don't have to follow the mirror -> klass pointer, since all 420 // klasses that are dirty will be scavenged when we iterate over the 421 // ClassLoaderData objects. 422 423 InstanceKlass::oop_ps_push_contents(obj, pm); 424 425 PushContentsClosure cl(pm); 426 oop_oop_iterate_statics<true>(obj, &cl); 427 } 428 429 void InstanceClassLoaderKlass::oop_ps_push_contents(oop obj, PSPromotionManager* pm) { 430 InstanceKlass::oop_ps_push_contents(obj, pm); 431 432 // This is called by the young collector. It will already have taken care of 433 // all class loader data. So, we don't have to follow the class loader -> 434 // class loader data link. 435 } 436 437 template <class T> 438 static void oop_ps_push_contents_specialized(oop obj, InstanceRefKlass *klass, PSPromotionManager* pm) { 439 T* referent_addr = (T*)java_lang_ref_Reference::referent_addr_raw(obj); 440 if (PSScavenge::should_scavenge(referent_addr)) { 441 ReferenceProcessor* rp = PSScavenge::reference_processor(); 442 if (rp->discover_reference(obj, klass->reference_type())) { 443 // reference already enqueued, referent and next will be traversed later 444 klass->InstanceKlass::oop_ps_push_contents(obj, pm); 445 return; 446 } else { 447 // treat referent as normal oop 448 pm->claim_or_forward_depth(referent_addr); 449 } 450 } 451 // Treat discovered as normal oop, if ref is not "active", 452 // i.e. if next is non-NULL. 453 T* next_addr = (T*)java_lang_ref_Reference::next_addr_raw(obj); 454 T next_oop = oopDesc::load_heap_oop(next_addr); 455 if (!oopDesc::is_null(next_oop)) { // i.e. ref is not "active" 456 T* discovered_addr = (T*)java_lang_ref_Reference::discovered_addr_raw(obj); 457 log_develop_trace(gc, ref)(" Process discovered as normal " PTR_FORMAT, p2i(discovered_addr)); 458 if (PSScavenge::should_scavenge(discovered_addr)) { 459 pm->claim_or_forward_depth(discovered_addr); 460 } 461 } 462 // Treat next as normal oop; next is a link in the reference queue. 463 if (PSScavenge::should_scavenge(next_addr)) { 464 pm->claim_or_forward_depth(next_addr); 465 } 466 klass->InstanceKlass::oop_ps_push_contents(obj, pm); 467 } 468 469 void InstanceRefKlass::oop_ps_push_contents(oop obj, PSPromotionManager* pm) { 470 if (UseCompressedOops) { 471 oop_ps_push_contents_specialized<narrowOop>(obj, this, pm); 472 } else { 473 oop_ps_push_contents_specialized<oop>(obj, this, pm); 474 } 475 } 476 477 void ObjArrayKlass::oop_ps_push_contents(oop obj, PSPromotionManager* pm) { 478 assert(obj->is_objArray(), "obj must be obj array"); 479 PushContentsClosure cl(pm); 480 oop_oop_iterate_elements<true>(objArrayOop(obj), &cl); 481 } 482 483 void TypeArrayKlass::oop_ps_push_contents(oop obj, PSPromotionManager* pm) { 484 assert(obj->is_typeArray(),"must be a type array"); 485 ShouldNotReachHere(); 486 } 487 488 oop PSPromotionManager::oop_promotion_failed(oop obj, markOop obj_mark) { 489 assert(_old_gen_is_full || PromotionFailureALot, "Sanity"); 490 491 // Attempt to CAS in the header. 492 // This tests if the header is still the same as when 493 // this started. If it is the same (i.e., no forwarding 494 // pointer has been installed), then this thread owns 495 // it. 496 if (obj->cas_forward_to(obj, obj_mark)) { 497 // We won any races, we "own" this object. 498 assert(obj == obj->forwardee(), "Sanity"); 499 500 _promotion_failed_info.register_copy_failure(obj->size()); 501 502 push_contents(obj); 503 504 _preserved_marks->push_if_necessary(obj, obj_mark); 505 } else { 506 // We lost, someone else "owns" this object 507 guarantee(obj->is_forwarded(), "Object must be forwarded if the cas failed."); 508 509 // No unallocation to worry about. 510 obj = obj->forwardee(); 511 } 512 513 log_develop_trace(gc, scavenge)("{promotion-failure %s " PTR_FORMAT " (%d)}", obj->klass()->internal_name(), p2i(obj), obj->size()); 514 515 return obj; 516 }