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/parallel/mutableSpace.hpp" 27 #include "gc/parallel/parallelScavengeHeap.hpp" 28 #include "gc/parallel/psOldGen.hpp" 29 #include "gc/parallel/psPromotionManager.inline.hpp" 30 #include "gc/parallel/psScavenge.inline.hpp" 31 #include "gc/shared/gcTrace.hpp" 32 #include "gc/shared/preservedMarks.inline.hpp" 33 #include "gc/shared/taskqueue.inline.hpp" 34 #include "logging/log.hpp" 35 #include "memory/allocation.inline.hpp" 36 #include "memory/memRegion.hpp" 37 #include "memory/padded.inline.hpp" 38 #include "oops/instanceKlass.inline.hpp" 39 #include "oops/instanceMirrorKlass.inline.hpp" 40 #include "oops/objArrayKlass.inline.hpp" 41 #include "oops/oop.inline.hpp" 42 43 PaddedEnd<PSPromotionManager>* PSPromotionManager::_manager_array = NULL; 44 OopStarTaskQueueSet* PSPromotionManager::_stack_array_depth = NULL; 45 PreservedMarksSet* PSPromotionManager::_preserved_marks_set = NULL; 46 PSOldGen* PSPromotionManager::_old_gen = NULL; 47 MutableSpace* PSPromotionManager::_young_space = NULL; 48 49 void PSPromotionManager::initialize() { 50 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); 51 52 _old_gen = heap->old_gen(); 53 _young_space = heap->young_gen()->to_space(); 54 55 const uint promotion_manager_num = ParallelGCThreads + 1; 56 57 // To prevent false sharing, we pad the PSPromotionManagers 58 // and make sure that the first instance starts at a cache line. 59 assert(_manager_array == NULL, "Attempt to initialize twice"); 60 _manager_array = PaddedArray<PSPromotionManager, mtGC>::create_unfreeable(promotion_manager_num); 61 guarantee(_manager_array != NULL, "Could not initialize promotion manager"); 62 63 _stack_array_depth = new OopStarTaskQueueSet(ParallelGCThreads); 64 guarantee(_stack_array_depth != NULL, "Could not initialize promotion manager"); 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 guarantee(_preserved_marks_set != NULL, "Could not initialize preserved marks set"); 76 _preserved_marks_set->init(promotion_manager_num); 77 for (uint i = 0; i < promotion_manager_num; i += 1) { 78 _manager_array[i].register_preserved_marks(_preserved_marks_set->get(i)); 79 } 80 } 81 82 // Helper functions to get around the circular dependency between 83 // psScavenge.inline.hpp and psPromotionManager.inline.hpp. 84 bool PSPromotionManager::should_scavenge(oop* p, bool check_to_space) { 85 return PSScavenge::should_scavenge(p, check_to_space); 86 } 87 bool PSPromotionManager::should_scavenge(narrowOop* p, bool check_to_space) { 88 return PSScavenge::should_scavenge(p, check_to_space); 89 } 90 91 PSPromotionManager* PSPromotionManager::gc_thread_promotion_manager(uint index) { 92 assert(index < ParallelGCThreads, "index out of range"); 93 assert(_manager_array != NULL, "Sanity"); 94 return &_manager_array[index]; 95 } 96 97 PSPromotionManager* PSPromotionManager::vm_thread_promotion_manager() { 98 assert(_manager_array != NULL, "Sanity"); 99 return &_manager_array[ParallelGCThreads]; 100 } 101 102 void PSPromotionManager::pre_scavenge() { 103 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); 104 105 _preserved_marks_set->assert_empty(); 106 _young_space = heap->young_gen()->to_space(); 107 108 for(uint i=0; i<ParallelGCThreads+1; i++) { 109 manager_array(i)->reset(); 110 } 111 } 112 113 bool PSPromotionManager::post_scavenge(YoungGCTracer& gc_tracer) { 114 bool promotion_failure_occurred = false; 115 116 TASKQUEUE_STATS_ONLY(print_taskqueue_stats()); 117 for (uint i = 0; i < ParallelGCThreads + 1; i++) { 118 PSPromotionManager* manager = manager_array(i); 119 assert(manager->claimed_stack_depth()->is_empty(), "should be empty"); 120 if (manager->_promotion_failed_info.has_failed()) { 121 gc_tracer.report_promotion_failed(manager->_promotion_failed_info); 122 promotion_failure_occurred = true; 123 } 124 manager->flush_labs(); 125 } 126 if (!promotion_failure_occurred) { 127 // If there was no promotion failure, the preserved mark stacks 128 // should be empty. 129 _preserved_marks_set->assert_empty(); 130 } 131 return promotion_failure_occurred; 132 } 133 134 #if TASKQUEUE_STATS 135 void 136 PSPromotionManager::print_local_stats(outputStream* const out, uint i) const { 137 #define FMT " " SIZE_FORMAT_W(10) 138 out->print_cr("%3u" FMT FMT FMT FMT, i, _masked_pushes, _masked_steals, 139 _arrays_chunked, _array_chunks_processed); 140 #undef FMT 141 } 142 143 static const char* const pm_stats_hdr[] = { 144 " --------masked------- arrays array", 145 "thr push steal chunked chunks", 146 "--- ---------- ---------- ---------- ----------" 147 }; 148 149 void 150 PSPromotionManager::print_taskqueue_stats() { 151 if (!log_develop_is_enabled(Trace, gc, task, stats)) { 152 return; 153 } 154 LogHandle(gc, task, stats) log; 155 ResourceMark rm; 156 outputStream* out = log.trace_stream(); 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::drain_stacks_depth(bool totally_drain) { 240 totally_drain = totally_drain || _totally_drain; 241 242 #ifdef ASSERT 243 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); 244 MutableSpace* to_space = heap->young_gen()->to_space(); 245 MutableSpace* old_space = heap->old_gen()->object_space(); 246 #endif /* ASSERT */ 247 248 OopStarTaskQueue* const tq = claimed_stack_depth(); 249 do { 250 StarTask p; 251 252 // Drain overflow stack first, so other threads can steal from 253 // claimed stack while we work. 254 while (tq->pop_overflow(p)) { 255 process_popped_location_depth(p); 256 } 257 258 if (totally_drain) { 259 while (tq->pop_local(p)) { 260 process_popped_location_depth(p); 261 } 262 } else { 263 while (tq->size() > _target_stack_size && tq->pop_local(p)) { 264 process_popped_location_depth(p); 265 } 266 } 267 } while (totally_drain && !tq->taskqueue_empty() || !tq->overflow_empty()); 268 269 assert(!totally_drain || tq->taskqueue_empty(), "Sanity"); 270 assert(totally_drain || tq->size() <= _target_stack_size, "Sanity"); 271 assert(tq->overflow_empty(), "Sanity"); 272 } 273 274 void PSPromotionManager::flush_labs() { 275 assert(stacks_empty(), "Attempt to flush lab with live stack"); 276 277 // If either promotion lab fills up, we can flush the 278 // lab but not refill it, so check first. 279 assert(!_young_lab.is_flushed() || _young_gen_is_full, "Sanity"); 280 if (!_young_lab.is_flushed()) 281 _young_lab.flush(); 282 283 assert(!_old_lab.is_flushed() || _old_gen_is_full, "Sanity"); 284 if (!_old_lab.is_flushed()) 285 _old_lab.flush(); 286 287 // Let PSScavenge know if we overflowed 288 if (_young_gen_is_full) { 289 PSScavenge::set_survivor_overflow(true); 290 } 291 } 292 293 template <class T> void PSPromotionManager::process_array_chunk_work( 294 oop obj, 295 int start, int end) { 296 assert(start <= end, "invariant"); 297 T* const base = (T*)objArrayOop(obj)->base(); 298 T* p = base + start; 299 T* const chunk_end = base + end; 300 while (p < chunk_end) { 301 if (PSScavenge::should_scavenge(p)) { 302 claim_or_forward_depth(p); 303 } 304 ++p; 305 } 306 } 307 308 void PSPromotionManager::process_array_chunk(oop old) { 309 assert(PSChunkLargeArrays, "invariant"); 310 assert(old->is_objArray(), "invariant"); 311 assert(old->is_forwarded(), "invariant"); 312 313 TASKQUEUE_STATS_ONLY(++_array_chunks_processed); 314 315 oop const obj = old->forwardee(); 316 317 int start; 318 int const end = arrayOop(old)->length(); 319 if (end > (int) _min_array_size_for_chunking) { 320 // we'll chunk more 321 start = end - _array_chunk_size; 322 assert(start > 0, "invariant"); 323 arrayOop(old)->set_length(start); 324 push_depth(mask_chunked_array_oop(old)); 325 TASKQUEUE_STATS_ONLY(++_masked_pushes); 326 } else { 327 // this is the final chunk for this array 328 start = 0; 329 int const actual_length = arrayOop(obj)->length(); 330 arrayOop(old)->set_length(actual_length); 331 } 332 333 if (UseCompressedOops) { 334 process_array_chunk_work<narrowOop>(obj, start, end); 335 } else { 336 process_array_chunk_work<oop>(obj, start, end); 337 } 338 } 339 340 class PushContentsClosure : public ExtendedOopClosure { 341 PSPromotionManager* _pm; 342 public: 343 PushContentsClosure(PSPromotionManager* pm) : _pm(pm) {} 344 345 template <typename T> void do_oop_nv(T* p) { 346 if (PSScavenge::should_scavenge(p)) { 347 _pm->claim_or_forward_depth(p); 348 } 349 } 350 351 virtual void do_oop(oop* p) { do_oop_nv(p); } 352 virtual void do_oop(narrowOop* p) { do_oop_nv(p); } 353 354 // Don't use the oop verification code in the oop_oop_iterate framework. 355 debug_only(virtual bool should_verify_oops() { return false; }) 356 }; 357 358 void InstanceKlass::oop_ps_push_contents(oop obj, PSPromotionManager* pm) { 359 PushContentsClosure cl(pm); 360 oop_oop_iterate_oop_maps_reverse<true>(obj, &cl); 361 } 362 363 void InstanceMirrorKlass::oop_ps_push_contents(oop obj, PSPromotionManager* pm) { 364 // Note that we don't have to follow the mirror -> klass pointer, since all 365 // klasses that are dirty will be scavenged when we iterate over the 366 // ClassLoaderData objects. 367 368 InstanceKlass::oop_ps_push_contents(obj, pm); 369 370 PushContentsClosure cl(pm); 371 oop_oop_iterate_statics<true>(obj, &cl); 372 } 373 374 void InstanceClassLoaderKlass::oop_ps_push_contents(oop obj, PSPromotionManager* pm) { 375 InstanceKlass::oop_ps_push_contents(obj, pm); 376 377 // This is called by the young collector. It will already have taken care of 378 // all class loader data. So, we don't have to follow the class loader -> 379 // class loader data link. 380 } 381 382 template <class T> 383 static void oop_ps_push_contents_specialized(oop obj, InstanceRefKlass *klass, PSPromotionManager* pm) { 384 T* referent_addr = (T*)java_lang_ref_Reference::referent_addr(obj); 385 if (PSScavenge::should_scavenge(referent_addr)) { 386 ReferenceProcessor* rp = PSScavenge::reference_processor(); 387 if (rp->discover_reference(obj, klass->reference_type())) { 388 // reference already enqueued, referent and next will be traversed later 389 klass->InstanceKlass::oop_ps_push_contents(obj, pm); 390 return; 391 } else { 392 // treat referent as normal oop 393 pm->claim_or_forward_depth(referent_addr); 394 } 395 } 396 // Treat discovered as normal oop, if ref is not "active", 397 // i.e. if next is non-NULL. 398 T* next_addr = (T*)java_lang_ref_Reference::next_addr(obj); 399 T next_oop = oopDesc::load_heap_oop(next_addr); 400 if (!oopDesc::is_null(next_oop)) { // i.e. ref is not "active" 401 T* discovered_addr = (T*)java_lang_ref_Reference::discovered_addr(obj); 402 log_develop_trace(gc, ref)(" Process discovered as normal " PTR_FORMAT, p2i(discovered_addr)); 403 if (PSScavenge::should_scavenge(discovered_addr)) { 404 pm->claim_or_forward_depth(discovered_addr); 405 } 406 } 407 // Treat next as normal oop; next is a link in the reference queue. 408 if (PSScavenge::should_scavenge(next_addr)) { 409 pm->claim_or_forward_depth(next_addr); 410 } 411 klass->InstanceKlass::oop_ps_push_contents(obj, pm); 412 } 413 414 void InstanceRefKlass::oop_ps_push_contents(oop obj, PSPromotionManager* pm) { 415 if (UseCompressedOops) { 416 oop_ps_push_contents_specialized<narrowOop>(obj, this, pm); 417 } else { 418 oop_ps_push_contents_specialized<oop>(obj, this, pm); 419 } 420 } 421 422 void ObjArrayKlass::oop_ps_push_contents(oop obj, PSPromotionManager* pm) { 423 assert(obj->is_objArray(), "obj must be obj array"); 424 PushContentsClosure cl(pm); 425 oop_oop_iterate_elements<true>(objArrayOop(obj), &cl); 426 } 427 428 void TypeArrayKlass::oop_ps_push_contents(oop obj, PSPromotionManager* pm) { 429 assert(obj->is_typeArray(),"must be a type array"); 430 ShouldNotReachHere(); 431 } 432 433 oop PSPromotionManager::oop_promotion_failed(oop obj, markOop obj_mark) { 434 assert(_old_gen_is_full || PromotionFailureALot, "Sanity"); 435 436 // Attempt to CAS in the header. 437 // This tests if the header is still the same as when 438 // this started. If it is the same (i.e., no forwarding 439 // pointer has been installed), then this thread owns 440 // it. 441 if (obj->cas_forward_to(obj, obj_mark)) { 442 // We won any races, we "own" this object. 443 assert(obj == obj->forwardee(), "Sanity"); 444 445 _promotion_failed_info.register_copy_failure(obj->size()); 446 447 push_contents(obj); 448 449 _preserved_marks->push_if_necessary(obj, obj_mark); 450 } else { 451 // We lost, someone else "owns" this object 452 guarantee(obj->is_forwarded(), "Object must be forwarded if the cas failed."); 453 454 // No unallocation to worry about. 455 obj = obj->forwardee(); 456 } 457 458 log_develop_trace(gc, scavenge)("{promotion-failure %s " PTR_FORMAT " (%d)}", obj->klass()->internal_name(), p2i(obj), obj->size()); 459 460 return obj; 461 }