1 /* 2 * Copyright (c) 2002, 2010, 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 "incls/_precompiled.incl" 26 #include "incls/_psPromotionManager.cpp.incl" 27 28 PSPromotionManager** PSPromotionManager::_manager_array = NULL; 29 OopStarTaskQueueSet* PSPromotionManager::_stack_array_depth = NULL; 30 PSOldGen* PSPromotionManager::_old_gen = NULL; 31 MutableSpace* PSPromotionManager::_young_space = NULL; 32 33 void PSPromotionManager::initialize() { 34 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 35 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); 36 37 _old_gen = heap->old_gen(); 38 _young_space = heap->young_gen()->to_space(); 39 40 assert(_manager_array == NULL, "Attempt to initialize twice"); 41 _manager_array = NEW_C_HEAP_ARRAY(PSPromotionManager*, ParallelGCThreads+1 ); 42 guarantee(_manager_array != NULL, "Could not initialize promotion manager"); 43 44 _stack_array_depth = new OopStarTaskQueueSet(ParallelGCThreads); 45 guarantee(_stack_array_depth != NULL, "Cound not initialize promotion manager"); 46 47 // Create and register the PSPromotionManager(s) for the worker threads. 48 for(uint i=0; i<ParallelGCThreads; i++) { 49 _manager_array[i] = new PSPromotionManager(); 50 guarantee(_manager_array[i] != NULL, "Could not create PSPromotionManager"); 51 stack_array_depth()->register_queue(i, _manager_array[i]->claimed_stack_depth()); 52 } 53 54 // The VMThread gets its own PSPromotionManager, which is not available 55 // for work stealing. 56 _manager_array[ParallelGCThreads] = new PSPromotionManager(); 57 guarantee(_manager_array[ParallelGCThreads] != NULL, "Could not create PSPromotionManager"); 58 } 59 60 PSPromotionManager* PSPromotionManager::gc_thread_promotion_manager(int index) { 61 assert(index >= 0 && index < (int)ParallelGCThreads, "index out of range"); 62 assert(_manager_array != NULL, "Sanity"); 63 return _manager_array[index]; 64 } 65 66 PSPromotionManager* PSPromotionManager::vm_thread_promotion_manager() { 67 assert(_manager_array != NULL, "Sanity"); 68 return _manager_array[ParallelGCThreads]; 69 } 70 71 void PSPromotionManager::pre_scavenge() { 72 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 73 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); 74 75 _young_space = heap->young_gen()->to_space(); 76 77 for(uint i=0; i<ParallelGCThreads+1; i++) { 78 manager_array(i)->reset(); 79 } 80 } 81 82 void PSPromotionManager::post_scavenge() { 83 TASKQUEUE_STATS_ONLY(if (PrintGCDetails && ParallelGCVerbose) print_stats()); 84 for (uint i = 0; i < ParallelGCThreads + 1; i++) { 85 PSPromotionManager* manager = manager_array(i); 86 assert(manager->claimed_stack_depth()->is_empty(), "should be empty"); 87 manager->flush_labs(); 88 } 89 } 90 91 #if TASKQUEUE_STATS 92 void 93 PSPromotionManager::print_taskqueue_stats(uint i) const { 94 tty->print("%3u ", i); 95 _claimed_stack_depth.stats.print(); 96 tty->cr(); 97 } 98 99 void 100 PSPromotionManager::print_local_stats(uint i) const { 101 #define FMT " " SIZE_FORMAT_W(10) 102 tty->print_cr("%3u" FMT FMT FMT FMT, i, _masked_pushes, _masked_steals, 103 _arrays_chunked, _array_chunks_processed); 104 #undef FMT 105 } 106 107 static const char* const pm_stats_hdr[] = { 108 " --------masked------- arrays array", 109 "thr push steal chunked chunks", 110 "--- ---------- ---------- ---------- ----------" 111 }; 112 113 void 114 PSPromotionManager::print_stats() { 115 tty->print_cr("== GC Tasks Stats, GC %3d", 116 Universe::heap()->total_collections()); 117 118 tty->print("thr "); TaskQueueStats::print_header(1); tty->cr(); 119 tty->print("--- "); TaskQueueStats::print_header(2); tty->cr(); 120 for (uint i = 0; i < ParallelGCThreads + 1; ++i) { 121 manager_array(i)->print_taskqueue_stats(i); 122 } 123 124 const uint hlines = sizeof(pm_stats_hdr) / sizeof(pm_stats_hdr[0]); 125 for (uint i = 0; i < hlines; ++i) tty->print_cr(pm_stats_hdr[i]); 126 for (uint i = 0; i < ParallelGCThreads + 1; ++i) { 127 manager_array(i)->print_local_stats(i); 128 } 129 } 130 131 void 132 PSPromotionManager::reset_stats() { 133 claimed_stack_depth()->stats.reset(); 134 _masked_pushes = _masked_steals = 0; 135 _arrays_chunked = _array_chunks_processed = 0; 136 } 137 #endif // TASKQUEUE_STATS 138 139 PSPromotionManager::PSPromotionManager() { 140 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 141 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); 142 143 // We set the old lab's start array. 144 _old_lab.set_start_array(old_gen()->start_array()); 145 146 uint queue_size; 147 claimed_stack_depth()->initialize(); 148 queue_size = claimed_stack_depth()->max_elems(); 149 150 _totally_drain = (ParallelGCThreads == 1) || (GCDrainStackTargetSize == 0); 151 if (_totally_drain) { 152 _target_stack_size = 0; 153 } else { 154 // don't let the target stack size to be more than 1/4 of the entries 155 _target_stack_size = (uint) MIN2((uint) GCDrainStackTargetSize, 156 (uint) (queue_size / 4)); 157 } 158 159 _array_chunk_size = ParGCArrayScanChunk; 160 // let's choose 1.5x the chunk size 161 _min_array_size_for_chunking = 3 * _array_chunk_size / 2; 162 163 reset(); 164 } 165 166 void PSPromotionManager::reset() { 167 assert(stacks_empty(), "reset of non-empty stack"); 168 169 // We need to get an assert in here to make sure the labs are always flushed. 170 171 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 172 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); 173 174 // Do not prefill the LAB's, save heap wastage! 175 HeapWord* lab_base = young_space()->top(); 176 _young_lab.initialize(MemRegion(lab_base, (size_t)0)); 177 _young_gen_is_full = false; 178 179 lab_base = old_gen()->object_space()->top(); 180 _old_lab.initialize(MemRegion(lab_base, (size_t)0)); 181 _old_gen_is_full = false; 182 183 TASKQUEUE_STATS_ONLY(reset_stats()); 184 } 185 186 187 void PSPromotionManager::drain_stacks_depth(bool totally_drain) { 188 totally_drain = totally_drain || _totally_drain; 189 190 #ifdef ASSERT 191 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 192 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); 193 MutableSpace* to_space = heap->young_gen()->to_space(); 194 MutableSpace* old_space = heap->old_gen()->object_space(); 195 MutableSpace* perm_space = heap->perm_gen()->object_space(); 196 #endif /* ASSERT */ 197 198 OopStarTaskQueue* const tq = claimed_stack_depth(); 199 do { 200 StarTask p; 201 202 // Drain overflow stack first, so other threads can steal from 203 // claimed stack while we work. 204 while (tq->pop_overflow(p)) { 205 process_popped_location_depth(p); 206 } 207 208 if (totally_drain) { 209 while (tq->pop_local(p)) { 210 process_popped_location_depth(p); 211 } 212 } else { 213 while (tq->size() > _target_stack_size && tq->pop_local(p)) { 214 process_popped_location_depth(p); 215 } 216 } 217 } while (totally_drain && !tq->taskqueue_empty() || !tq->overflow_empty()); 218 219 assert(!totally_drain || tq->taskqueue_empty(), "Sanity"); 220 assert(totally_drain || tq->size() <= _target_stack_size, "Sanity"); 221 assert(tq->overflow_empty(), "Sanity"); 222 } 223 224 void PSPromotionManager::flush_labs() { 225 assert(stacks_empty(), "Attempt to flush lab with live stack"); 226 227 // If either promotion lab fills up, we can flush the 228 // lab but not refill it, so check first. 229 assert(!_young_lab.is_flushed() || _young_gen_is_full, "Sanity"); 230 if (!_young_lab.is_flushed()) 231 _young_lab.flush(); 232 233 assert(!_old_lab.is_flushed() || _old_gen_is_full, "Sanity"); 234 if (!_old_lab.is_flushed()) 235 _old_lab.flush(); 236 237 // Let PSScavenge know if we overflowed 238 if (_young_gen_is_full) { 239 PSScavenge::set_survivor_overflow(true); 240 } 241 } 242 243 // 244 // This method is pretty bulky. It would be nice to split it up 245 // into smaller submethods, but we need to be careful not to hurt 246 // performance. 247 // 248 249 oop PSPromotionManager::copy_to_survivor_space(oop o) { 250 assert(PSScavenge::should_scavenge(&o), "Sanity"); 251 252 oop new_obj = NULL; 253 254 // NOTE! We must be very careful with any methods that access the mark 255 // in o. There may be multiple threads racing on it, and it may be forwarded 256 // at any time. Do not use oop methods for accessing the mark! 257 markOop test_mark = o->mark(); 258 259 // The same test as "o->is_forwarded()" 260 if (!test_mark->is_marked()) { 261 bool new_obj_is_tenured = false; 262 size_t new_obj_size = o->size(); 263 264 // Find the objects age, MT safe. 265 int age = (test_mark->has_displaced_mark_helper() /* o->has_displaced_mark() */) ? 266 test_mark->displaced_mark_helper()->age() : test_mark->age(); 267 268 // Try allocating obj in to-space (unless too old) 269 if (age < PSScavenge::tenuring_threshold()) { 270 new_obj = (oop) _young_lab.allocate(new_obj_size); 271 if (new_obj == NULL && !_young_gen_is_full) { 272 // Do we allocate directly, or flush and refill? 273 if (new_obj_size > (YoungPLABSize / 2)) { 274 // Allocate this object directly 275 new_obj = (oop)young_space()->cas_allocate(new_obj_size); 276 } else { 277 // Flush and fill 278 _young_lab.flush(); 279 280 HeapWord* lab_base = young_space()->cas_allocate(YoungPLABSize); 281 if (lab_base != NULL) { 282 _young_lab.initialize(MemRegion(lab_base, YoungPLABSize)); 283 // Try the young lab allocation again. 284 new_obj = (oop) _young_lab.allocate(new_obj_size); 285 } else { 286 _young_gen_is_full = true; 287 } 288 } 289 } 290 } 291 292 // Otherwise try allocating obj tenured 293 if (new_obj == NULL) { 294 #ifndef PRODUCT 295 if (Universe::heap()->promotion_should_fail()) { 296 return oop_promotion_failed(o, test_mark); 297 } 298 #endif // #ifndef PRODUCT 299 300 new_obj = (oop) _old_lab.allocate(new_obj_size); 301 new_obj_is_tenured = true; 302 303 if (new_obj == NULL) { 304 if (!_old_gen_is_full) { 305 // Do we allocate directly, or flush and refill? 306 if (new_obj_size > (OldPLABSize / 2)) { 307 // Allocate this object directly 308 new_obj = (oop)old_gen()->cas_allocate(new_obj_size); 309 } else { 310 // Flush and fill 311 _old_lab.flush(); 312 313 HeapWord* lab_base = old_gen()->cas_allocate(OldPLABSize); 314 if(lab_base != NULL) { 315 _old_lab.initialize(MemRegion(lab_base, OldPLABSize)); 316 // Try the old lab allocation again. 317 new_obj = (oop) _old_lab.allocate(new_obj_size); 318 } 319 } 320 } 321 322 // This is the promotion failed test, and code handling. 323 // The code belongs here for two reasons. It is slightly 324 // different thatn the code below, and cannot share the 325 // CAS testing code. Keeping the code here also minimizes 326 // the impact on the common case fast path code. 327 328 if (new_obj == NULL) { 329 _old_gen_is_full = true; 330 return oop_promotion_failed(o, test_mark); 331 } 332 } 333 } 334 335 assert(new_obj != NULL, "allocation should have succeeded"); 336 337 // Copy obj 338 Copy::aligned_disjoint_words((HeapWord*)o, (HeapWord*)new_obj, new_obj_size); 339 340 // Now we have to CAS in the header. 341 if (o->cas_forward_to(new_obj, test_mark)) { 342 // We won any races, we "own" this object. 343 assert(new_obj == o->forwardee(), "Sanity"); 344 345 // Increment age if obj still in new generation. Now that 346 // we're dealing with a markOop that cannot change, it is 347 // okay to use the non mt safe oop methods. 348 if (!new_obj_is_tenured) { 349 new_obj->incr_age(); 350 assert(young_space()->contains(new_obj), "Attempt to push non-promoted obj"); 351 } 352 353 // Do the size comparison first with new_obj_size, which we 354 // already have. Hopefully, only a few objects are larger than 355 // _min_array_size_for_chunking, and most of them will be arrays. 356 // So, the is->objArray() test would be very infrequent. 357 if (new_obj_size > _min_array_size_for_chunking && 358 new_obj->is_objArray() && 359 PSChunkLargeArrays) { 360 // we'll chunk it 361 oop* const masked_o = mask_chunked_array_oop(o); 362 push_depth(masked_o); 363 TASKQUEUE_STATS_ONLY(++_arrays_chunked; ++_masked_pushes); 364 } else { 365 // we'll just push its contents 366 new_obj->push_contents(this); 367 } 368 } else { 369 // We lost, someone else "owns" this object 370 guarantee(o->is_forwarded(), "Object must be forwarded if the cas failed."); 371 372 // Try to deallocate the space. If it was directly allocated we cannot 373 // deallocate it, so we have to test. If the deallocation fails, 374 // overwrite with a filler object. 375 if (new_obj_is_tenured) { 376 if (!_old_lab.unallocate_object(new_obj)) { 377 CollectedHeap::fill_with_object((HeapWord*) new_obj, new_obj_size); 378 } 379 } else if (!_young_lab.unallocate_object(new_obj)) { 380 CollectedHeap::fill_with_object((HeapWord*) new_obj, new_obj_size); 381 } 382 383 // don't update this before the unallocation! 384 new_obj = o->forwardee(); 385 } 386 } else { 387 assert(o->is_forwarded(), "Sanity"); 388 new_obj = o->forwardee(); 389 } 390 391 #ifdef DEBUG 392 // This code must come after the CAS test, or it will print incorrect 393 // information. 394 if (TraceScavenge) { 395 gclog_or_tty->print_cr("{%s %s " PTR_FORMAT " -> " PTR_FORMAT " (" SIZE_FORMAT ")}", 396 PSScavenge::should_scavenge(&new_obj) ? "copying" : "tenuring", 397 new_obj->blueprint()->internal_name(), o, new_obj, new_obj->size()); 398 } 399 #endif 400 401 return new_obj; 402 } 403 404 template <class T> void PSPromotionManager::process_array_chunk_work( 405 oop obj, 406 int start, int end) { 407 assert(start < end, "invariant"); 408 T* const base = (T*)objArrayOop(obj)->base(); 409 T* p = base + start; 410 T* const chunk_end = base + end; 411 while (p < chunk_end) { 412 if (PSScavenge::should_scavenge(p)) { 413 claim_or_forward_depth(p); 414 } 415 ++p; 416 } 417 } 418 419 void PSPromotionManager::process_array_chunk(oop old) { 420 assert(PSChunkLargeArrays, "invariant"); 421 assert(old->is_objArray(), "invariant"); 422 assert(old->is_forwarded(), "invariant"); 423 424 TASKQUEUE_STATS_ONLY(++_array_chunks_processed); 425 426 oop const obj = old->forwardee(); 427 428 int start; 429 int const end = arrayOop(old)->length(); 430 if (end > (int) _min_array_size_for_chunking) { 431 // we'll chunk more 432 start = end - _array_chunk_size; 433 assert(start > 0, "invariant"); 434 arrayOop(old)->set_length(start); 435 push_depth(mask_chunked_array_oop(old)); 436 TASKQUEUE_STATS_ONLY(++_masked_pushes); 437 } else { 438 // this is the final chunk for this array 439 start = 0; 440 int const actual_length = arrayOop(obj)->length(); 441 arrayOop(old)->set_length(actual_length); 442 } 443 444 if (UseCompressedOops) { 445 process_array_chunk_work<narrowOop>(obj, start, end); 446 } else { 447 process_array_chunk_work<oop>(obj, start, end); 448 } 449 } 450 451 oop PSPromotionManager::oop_promotion_failed(oop obj, markOop obj_mark) { 452 assert(_old_gen_is_full || PromotionFailureALot, "Sanity"); 453 454 // Attempt to CAS in the header. 455 // This tests if the header is still the same as when 456 // this started. If it is the same (i.e., no forwarding 457 // pointer has been installed), then this thread owns 458 // it. 459 if (obj->cas_forward_to(obj, obj_mark)) { 460 // We won any races, we "own" this object. 461 assert(obj == obj->forwardee(), "Sanity"); 462 463 obj->push_contents(this); 464 465 // Save the mark if needed 466 PSScavenge::oop_promotion_failed(obj, obj_mark); 467 } else { 468 // We lost, someone else "owns" this object 469 guarantee(obj->is_forwarded(), "Object must be forwarded if the cas failed."); 470 471 // No unallocation to worry about. 472 obj = obj->forwardee(); 473 } 474 475 #ifdef DEBUG 476 if (TraceScavenge) { 477 gclog_or_tty->print_cr("{%s %s 0x%x (%d)}", 478 "promotion-failure", 479 obj->blueprint()->internal_name(), 480 obj, obj->size()); 481 482 } 483 #endif 484 485 return obj; 486 }