1 /* 2 * Copyright (c) 2001, 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 "precompiled.hpp" 26 #include "gc_implementation/parallelScavenge/parallelScavengeHeap.hpp" 27 #include "gc_implementation/parallelScavenge/psAdaptiveSizePolicy.hpp" 28 #include "gc_implementation/parallelScavenge/psMarkSweepDecorator.hpp" 29 #include "gc_implementation/parallelScavenge/psOldGen.hpp" 30 #include "gc_implementation/shared/spaceDecorator.hpp" 31 #include "memory/cardTableModRefBS.hpp" 32 #include "memory/gcLocker.inline.hpp" 33 #include "oops/oop.inline.hpp" 34 #include "runtime/java.hpp" 35 36 inline const char* PSOldGen::select_name() { 37 return UseParallelOldGC ? "ParOldGen" : "PSOldGen"; 38 } 39 40 PSOldGen::PSOldGen(ReservedSpace rs, size_t alignment, 41 size_t initial_size, size_t min_size, size_t max_size, 42 const char* perf_data_name, int level): 43 _name(select_name()), _init_gen_size(initial_size), _min_gen_size(min_size), 44 _max_gen_size(max_size) 45 { 46 initialize(rs, alignment, perf_data_name, level); 47 } 48 49 PSOldGen::PSOldGen(size_t initial_size, 50 size_t min_size, size_t max_size, 51 const char* perf_data_name, int level): 52 _name(select_name()), _init_gen_size(initial_size), _min_gen_size(min_size), 53 _max_gen_size(max_size) 54 {} 55 56 void PSOldGen::initialize(ReservedSpace rs, size_t alignment, 57 const char* perf_data_name, int level) { 58 initialize_virtual_space(rs, alignment); 59 initialize_work(perf_data_name, level); 60 // The old gen can grow to gen_size_limit(). _reserve reflects only 61 // the current maximum that can be committed. 62 assert(_reserved.byte_size() <= gen_size_limit(), "Consistency check"); 63 } 64 65 void PSOldGen::initialize_virtual_space(ReservedSpace rs, size_t alignment) { 66 67 _virtual_space = new PSVirtualSpace(rs, alignment); 68 if (!_virtual_space->expand_by(_init_gen_size)) { 69 vm_exit_during_initialization("Could not reserve enough space for " 70 "object heap"); 71 } 72 } 73 74 void PSOldGen::initialize_work(const char* perf_data_name, int level) { 75 // 76 // Basic memory initialization 77 // 78 79 MemRegion limit_reserved((HeapWord*)virtual_space()->low_boundary(), 80 heap_word_size(_max_gen_size)); 81 assert(limit_reserved.byte_size() == _max_gen_size, 82 "word vs bytes confusion"); 83 // 84 // Object start stuff 85 // 86 87 start_array()->initialize(limit_reserved); 88 89 _reserved = MemRegion((HeapWord*)virtual_space()->low_boundary(), 90 (HeapWord*)virtual_space()->high_boundary()); 91 92 // 93 // Card table stuff 94 // 95 96 MemRegion cmr((HeapWord*)virtual_space()->low(), 97 (HeapWord*)virtual_space()->high()); 98 if (ZapUnusedHeapArea) { 99 // Mangle newly committed space immediately rather than 100 // waiting for the initialization of the space even though 101 // mangling is related to spaces. Doing it here eliminates 102 // the need to carry along information that a complete mangling 103 // (bottom to end) needs to be done. 104 SpaceMangler::mangle_region(cmr); 105 } 106 107 Universe::heap()->barrier_set()->resize_covered_region(cmr); 108 109 CardTableModRefBS* _ct = (CardTableModRefBS*)Universe::heap()->barrier_set(); 110 assert (_ct->kind() == BarrierSet::CardTableModRef, "Sanity"); 111 112 // Verify that the start and end of this generation is the start of a card. 113 // If this wasn't true, a single card could span more than one generation, 114 // which would cause problems when we commit/uncommit memory, and when we 115 // clear and dirty cards. 116 guarantee(_ct->is_card_aligned(_reserved.start()), "generation must be card aligned"); 117 if (_reserved.end() != Universe::heap()->reserved_region().end()) { 118 // Don't check at the very end of the heap as we'll assert that we're probing off 119 // the end if we try. 120 guarantee(_ct->is_card_aligned(_reserved.end()), "generation must be card aligned"); 121 } 122 123 // 124 // ObjectSpace stuff 125 // 126 127 _object_space = new MutableSpace(virtual_space()->alignment()); 128 129 if (_object_space == NULL) 130 vm_exit_during_initialization("Could not allocate an old gen space"); 131 132 object_space()->initialize(cmr, 133 SpaceDecorator::Clear, 134 SpaceDecorator::Mangle); 135 136 _object_mark_sweep = new PSMarkSweepDecorator(_object_space, start_array(), MarkSweepDeadRatio); 137 138 if (_object_mark_sweep == NULL) 139 vm_exit_during_initialization("Could not complete allocation of old generation"); 140 141 // Update the start_array 142 start_array()->set_covered_region(cmr); 143 144 // Generation Counters, generation 'level', 1 subspace 145 _gen_counters = new PSGenerationCounters(perf_data_name, level, 1, 146 virtual_space()); 147 _space_counters = new SpaceCounters(perf_data_name, 0, 148 virtual_space()->reserved_size(), 149 _object_space, _gen_counters); 150 } 151 152 // Assume that the generation has been allocated if its 153 // reserved size is not 0. 154 bool PSOldGen::is_allocated() { 155 return virtual_space()->reserved_size() != 0; 156 } 157 158 void PSOldGen::precompact() { 159 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 160 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); 161 162 // Reset start array first. 163 start_array()->reset(); 164 165 object_mark_sweep()->precompact(); 166 167 // Now compact the young gen 168 heap->young_gen()->precompact(); 169 } 170 171 void PSOldGen::adjust_pointers() { 172 object_mark_sweep()->adjust_pointers(); 173 } 174 175 void PSOldGen::compact() { 176 object_mark_sweep()->compact(ZapUnusedHeapArea); 177 } 178 179 void PSOldGen::move_and_update(ParCompactionManager* cm) { 180 PSParallelCompact::move_and_update(cm, PSParallelCompact::old_space_id); 181 } 182 183 size_t PSOldGen::contiguous_available() const { 184 return object_space()->free_in_bytes() + virtual_space()->uncommitted_size(); 185 } 186 187 // Allocation. We report all successful allocations to the size policy 188 // Note that the perm gen does not use this method, and should not! 189 HeapWord* PSOldGen::allocate(size_t word_size, bool is_tlab) { 190 assert_locked_or_safepoint(Heap_lock); 191 HeapWord* res = allocate_noexpand(word_size, is_tlab); 192 193 if (res == NULL) { 194 res = expand_and_allocate(word_size, is_tlab); 195 } 196 197 // Allocations in the old generation need to be reported 198 if (res != NULL) { 199 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 200 heap->size_policy()->tenured_allocation(word_size); 201 } 202 203 return res; 204 } 205 206 HeapWord* PSOldGen::expand_and_allocate(size_t word_size, bool is_tlab) { 207 assert(!is_tlab, "TLAB's are not supported in PSOldGen"); 208 expand(word_size*HeapWordSize); 209 if (GCExpandToAllocateDelayMillis > 0) { 210 os::sleep(Thread::current(), GCExpandToAllocateDelayMillis, false); 211 } 212 return allocate_noexpand(word_size, is_tlab); 213 } 214 215 HeapWord* PSOldGen::expand_and_cas_allocate(size_t word_size) { 216 expand(word_size*HeapWordSize); 217 if (GCExpandToAllocateDelayMillis > 0) { 218 os::sleep(Thread::current(), GCExpandToAllocateDelayMillis, false); 219 } 220 return cas_allocate_noexpand(word_size); 221 } 222 223 void PSOldGen::expand(size_t bytes) { 224 if (bytes == 0) { 225 return; 226 } 227 MutexLocker x(ExpandHeap_lock); 228 const size_t alignment = virtual_space()->alignment(); 229 size_t aligned_bytes = align_size_up(bytes, alignment); 230 size_t aligned_expand_bytes = align_size_up(MinHeapDeltaBytes, alignment); 231 if (aligned_bytes == 0){ 232 // The alignment caused the number of bytes to wrap. An expand_by(0) will 233 // return true with the implication that and expansion was done when it 234 // was not. A call to expand implies a best effort to expand by "bytes" 235 // but not a guarantee. Align down to give a best effort. This is likely 236 // the most that the generation can expand since it has some capacity to 237 // start with. 238 aligned_bytes = align_size_down(bytes, alignment); 239 } 240 241 bool success = false; 242 if (aligned_expand_bytes > aligned_bytes) { 243 success = expand_by(aligned_expand_bytes); 244 } 245 if (!success) { 246 success = expand_by(aligned_bytes); 247 } 248 if (!success) { 249 success = expand_to_reserved(); 250 } 251 252 if (PrintGC && Verbose) { 253 if (success && GC_locker::is_active()) { 254 gclog_or_tty->print_cr("Garbage collection disabled, expanded heap instead"); 255 } 256 } 257 } 258 259 bool PSOldGen::expand_by(size_t bytes) { 260 assert_lock_strong(ExpandHeap_lock); 261 assert_locked_or_safepoint(Heap_lock); 262 if (bytes == 0) { 263 return true; // That's what virtual_space()->expand_by(0) would return 264 } 265 bool result = virtual_space()->expand_by(bytes); 266 if (result) { 267 if (ZapUnusedHeapArea) { 268 // We need to mangle the newly expanded area. The memregion spans 269 // end -> new_end, we assume that top -> end is already mangled. 270 // Do the mangling before post_resize() is called because 271 // the space is available for allocation after post_resize(); 272 HeapWord* const virtual_space_high = (HeapWord*) virtual_space()->high(); 273 assert(object_space()->end() < virtual_space_high, 274 "Should be true before post_resize()"); 275 MemRegion mangle_region(object_space()->end(), virtual_space_high); 276 // Note that the object space has not yet been updated to 277 // coincede with the new underlying virtual space. 278 SpaceMangler::mangle_region(mangle_region); 279 } 280 post_resize(); 281 if (UsePerfData) { 282 _space_counters->update_capacity(); 283 _gen_counters->update_all(); 284 } 285 } 286 287 if (result && Verbose && PrintGC) { 288 size_t new_mem_size = virtual_space()->committed_size(); 289 size_t old_mem_size = new_mem_size - bytes; 290 gclog_or_tty->print_cr("Expanding %s from " SIZE_FORMAT "K by " 291 SIZE_FORMAT "K to " 292 SIZE_FORMAT "K", 293 name(), old_mem_size/K, bytes/K, new_mem_size/K); 294 } 295 296 return result; 297 } 298 299 bool PSOldGen::expand_to_reserved() { 300 assert_lock_strong(ExpandHeap_lock); 301 assert_locked_or_safepoint(Heap_lock); 302 303 bool result = true; 304 const size_t remaining_bytes = virtual_space()->uncommitted_size(); 305 if (remaining_bytes > 0) { 306 result = expand_by(remaining_bytes); 307 DEBUG_ONLY(if (!result) warning("grow to reserve failed")); 308 } 309 return result; 310 } 311 312 void PSOldGen::shrink(size_t bytes) { 313 assert_lock_strong(ExpandHeap_lock); 314 assert_locked_or_safepoint(Heap_lock); 315 316 size_t size = align_size_down(bytes, virtual_space()->alignment()); 317 if (size > 0) { 318 assert_lock_strong(ExpandHeap_lock); 319 virtual_space()->shrink_by(bytes); 320 post_resize(); 321 322 if (Verbose && PrintGC) { 323 size_t new_mem_size = virtual_space()->committed_size(); 324 size_t old_mem_size = new_mem_size + bytes; 325 gclog_or_tty->print_cr("Shrinking %s from " SIZE_FORMAT "K by " 326 SIZE_FORMAT "K to " 327 SIZE_FORMAT "K", 328 name(), old_mem_size/K, bytes/K, new_mem_size/K); 329 } 330 } 331 } 332 333 void PSOldGen::resize(size_t desired_free_space) { 334 const size_t alignment = virtual_space()->alignment(); 335 const size_t size_before = virtual_space()->committed_size(); 336 size_t new_size = used_in_bytes() + desired_free_space; 337 if (new_size < used_in_bytes()) { 338 // Overflowed the addition. 339 new_size = gen_size_limit(); 340 } 341 // Adjust according to our min and max 342 new_size = MAX2(MIN2(new_size, gen_size_limit()), min_gen_size()); 343 344 assert(gen_size_limit() >= reserved().byte_size(), "max new size problem?"); 345 new_size = align_size_up(new_size, alignment); 346 347 const size_t current_size = capacity_in_bytes(); 348 349 if (PrintAdaptiveSizePolicy && Verbose) { 350 gclog_or_tty->print_cr("AdaptiveSizePolicy::old generation size: " 351 "desired free: " SIZE_FORMAT " used: " SIZE_FORMAT 352 " new size: " SIZE_FORMAT " current size " SIZE_FORMAT 353 " gen limits: " SIZE_FORMAT " / " SIZE_FORMAT, 354 desired_free_space, used_in_bytes(), new_size, current_size, 355 gen_size_limit(), min_gen_size()); 356 } 357 358 if (new_size == current_size) { 359 // No change requested 360 return; 361 } 362 if (new_size > current_size) { 363 size_t change_bytes = new_size - current_size; 364 expand(change_bytes); 365 } else { 366 size_t change_bytes = current_size - new_size; 367 // shrink doesn't grab this lock, expand does. Is that right? 368 MutexLocker x(ExpandHeap_lock); 369 shrink(change_bytes); 370 } 371 372 if (PrintAdaptiveSizePolicy) { 373 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 374 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); 375 gclog_or_tty->print_cr("AdaptiveSizePolicy::old generation size: " 376 "collection: %d " 377 "(" SIZE_FORMAT ") -> (" SIZE_FORMAT ") ", 378 heap->total_collections(), 379 size_before, virtual_space()->committed_size()); 380 } 381 } 382 383 // NOTE! We need to be careful about resizing. During a GC, multiple 384 // allocators may be active during heap expansion. If we allow the 385 // heap resizing to become visible before we have correctly resized 386 // all heap related data structures, we may cause program failures. 387 void PSOldGen::post_resize() { 388 // First construct a memregion representing the new size 389 MemRegion new_memregion((HeapWord*)virtual_space()->low(), 390 (HeapWord*)virtual_space()->high()); 391 size_t new_word_size = new_memregion.word_size(); 392 393 start_array()->set_covered_region(new_memregion); 394 Universe::heap()->barrier_set()->resize_covered_region(new_memregion); 395 396 // ALWAYS do this last!! 397 object_space()->initialize(new_memregion, 398 SpaceDecorator::DontClear, 399 SpaceDecorator::DontMangle); 400 401 assert(new_word_size == heap_word_size(object_space()->capacity_in_bytes()), 402 "Sanity"); 403 } 404 405 size_t PSOldGen::gen_size_limit() { 406 return _max_gen_size; 407 } 408 409 void PSOldGen::reset_after_change() { 410 ShouldNotReachHere(); 411 return; 412 } 413 414 size_t PSOldGen::available_for_expansion() { 415 ShouldNotReachHere(); 416 return 0; 417 } 418 419 size_t PSOldGen::available_for_contraction() { 420 ShouldNotReachHere(); 421 return 0; 422 } 423 424 void PSOldGen::print() const { print_on(tty);} 425 void PSOldGen::print_on(outputStream* st) const { 426 st->print(" %-15s", name()); 427 if (PrintGCDetails && Verbose) { 428 st->print(" total " SIZE_FORMAT ", used " SIZE_FORMAT, 429 capacity_in_bytes(), used_in_bytes()); 430 } else { 431 st->print(" total " SIZE_FORMAT "K, used " SIZE_FORMAT "K", 432 capacity_in_bytes()/K, used_in_bytes()/K); 433 } 434 st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT ")", 435 virtual_space()->low_boundary(), 436 virtual_space()->high(), 437 virtual_space()->high_boundary()); 438 439 st->print(" object"); object_space()->print_on(st); 440 } 441 442 void PSOldGen::print_used_change(size_t prev_used) const { 443 gclog_or_tty->print(" [%s:", name()); 444 gclog_or_tty->print(" " SIZE_FORMAT "K" 445 "->" SIZE_FORMAT "K" 446 "(" SIZE_FORMAT "K)", 447 prev_used / K, used_in_bytes() / K, 448 capacity_in_bytes() / K); 449 gclog_or_tty->print("]"); 450 } 451 452 void PSOldGen::update_counters() { 453 if (UsePerfData) { 454 _space_counters->update_all(); 455 _gen_counters->update_all(); 456 } 457 } 458 459 #ifndef PRODUCT 460 461 void PSOldGen::space_invariants() { 462 assert(object_space()->end() == (HeapWord*) virtual_space()->high(), 463 "Space invariant"); 464 assert(object_space()->bottom() == (HeapWord*) virtual_space()->low(), 465 "Space invariant"); 466 assert(virtual_space()->low_boundary() <= virtual_space()->low(), 467 "Space invariant"); 468 assert(virtual_space()->high_boundary() >= virtual_space()->high(), 469 "Space invariant"); 470 assert(virtual_space()->low_boundary() == (char*) _reserved.start(), 471 "Space invariant"); 472 assert(virtual_space()->high_boundary() == (char*) _reserved.end(), 473 "Space invariant"); 474 assert(virtual_space()->committed_size() <= virtual_space()->reserved_size(), 475 "Space invariant"); 476 } 477 #endif 478 479 void PSOldGen::verify(bool allow_dirty) { 480 object_space()->verify(allow_dirty); 481 } 482 class VerifyObjectStartArrayClosure : public ObjectClosure { 483 PSOldGen* _gen; 484 ObjectStartArray* _start_array; 485 486 public: 487 VerifyObjectStartArrayClosure(PSOldGen* gen, ObjectStartArray* start_array) : 488 _gen(gen), _start_array(start_array) { } 489 490 virtual void do_object(oop obj) { 491 HeapWord* test_addr = (HeapWord*)obj + 1; 492 guarantee(_start_array->object_start(test_addr) == (HeapWord*)obj, "ObjectStartArray cannot find start of object"); 493 guarantee(_start_array->is_block_allocated((HeapWord*)obj), "ObjectStartArray missing block allocation"); 494 } 495 }; 496 497 void PSOldGen::verify_object_start_array() { 498 VerifyObjectStartArrayClosure check( this, &_start_array ); 499 object_iterate(&check); 500 } 501 502 #ifndef PRODUCT 503 void PSOldGen::record_spaces_top() { 504 assert(ZapUnusedHeapArea, "Not mangling unused space"); 505 object_space()->set_top_for_allocations(); 506 } 507 #endif