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