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