1 #ifdef USE_PRAGMA_IDENT_SRC 2 #pragma ident "@(#)psOldGen.cpp 1.54 07/05/05 17:05:28 JVM" 3 #endif 4 /* 5 * Copyright 2001-2007 Sun Microsystems, Inc. All Rights Reserved. 6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 7 * 8 * This code is free software; you can redistribute it and/or modify it 9 * under the terms of the GNU General Public License version 2 only, as 10 * published by the Free Software Foundation. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 23 * CA 95054 USA or visit www.sun.com if you need additional information or 24 * have any questions. 25 * 26 */ 27 28 # include "incls/_precompiled.incl" 29 # include "incls/_psOldGen.cpp.incl" 30 31 inline const char* PSOldGen::select_name() { 32 return UseParallelOldGC ? "ParOldGen" : "PSOldGen"; 33 } 34 35 PSOldGen::PSOldGen(ReservedSpace rs, size_t alignment, 36 size_t initial_size, size_t min_size, size_t max_size, 37 const char* perf_data_name, int level): 38 _name(select_name()), _init_gen_size(initial_size), _min_gen_size(min_size), 39 _max_gen_size(max_size) 40 { 41 initialize(rs, alignment, perf_data_name, level); 42 } 43 44 PSOldGen::PSOldGen(size_t initial_size, 45 size_t min_size, size_t max_size, 46 const char* perf_data_name, int level): 47 _name(select_name()), _init_gen_size(initial_size), _min_gen_size(min_size), 48 _max_gen_size(max_size) 49 {} 50 51 void PSOldGen::initialize(ReservedSpace rs, size_t alignment, 52 const char* perf_data_name, int level) { 53 initialize_virtual_space(rs, alignment); 54 initialize_work(perf_data_name, level); 55 // The old gen can grow to gen_size_limit(). _reserve reflects only 56 // the current maximum that can be committed. 57 assert(_reserved.byte_size() <= gen_size_limit(), "Consistency check"); 58 } 59 60 void PSOldGen::initialize_virtual_space(ReservedSpace rs, size_t alignment) { 61 62 _virtual_space = new PSVirtualSpace(rs, alignment); 63 if (!_virtual_space->expand_by(_init_gen_size)) { 64 vm_exit_during_initialization("Could not reserve enough space for " 65 "object heap"); 66 } 67 } 68 69 void PSOldGen::initialize_work(const char* perf_data_name, int level) { 70 // 71 // Basic memory initialization 72 // 73 74 MemRegion limit_reserved((HeapWord*)virtual_space()->low_boundary(), 75 heap_word_size(_max_gen_size)); 76 assert(limit_reserved.byte_size() == _max_gen_size, 77 "word vs bytes confusion"); 78 // 79 // Object start stuff 80 // 81 82 start_array()->initialize(limit_reserved); 83 84 _reserved = MemRegion((HeapWord*)virtual_space()->low_boundary(), 85 (HeapWord*)virtual_space()->high_boundary()); 86 87 // 88 // Card table stuff 89 // 90 91 MemRegion cmr((HeapWord*)virtual_space()->low(), 92 (HeapWord*)virtual_space()->high()); 93 Universe::heap()->barrier_set()->resize_covered_region(cmr); 94 95 CardTableModRefBS* _ct = (CardTableModRefBS*)Universe::heap()->barrier_set(); 96 assert (_ct->kind() == BarrierSet::CardTableModRef, "Sanity"); 97 98 // Verify that the start and end of this generation is the start of a card. 99 // If this wasn't true, a single card could span more than one generation, 100 // which would cause problems when we commit/uncommit memory, and when we 101 // clear and dirty cards. 102 guarantee(_ct->is_card_aligned(_reserved.start()), "generation must be card aligned"); 103 if (_reserved.end() != Universe::heap()->reserved_region().end()) { 104 // Don't check at the very end of the heap as we'll assert that we're probing off 105 // the end if we try. 106 guarantee(_ct->is_card_aligned(_reserved.end()), "generation must be card aligned"); 107 } 108 109 // 110 // ObjectSpace stuff 111 // 112 113 _object_space = new MutableSpace(); 114 115 if (_object_space == NULL) 116 vm_exit_during_initialization("Could not allocate an old gen space"); 117 118 object_space()->initialize(cmr, true); 119 120 _object_mark_sweep = new PSMarkSweepDecorator(_object_space, start_array(), MarkSweepDeadRatio); 121 122 if (_object_mark_sweep == NULL) 123 vm_exit_during_initialization("Could not complete allocation of old generation"); 124 125 // Update the start_array 126 start_array()->set_covered_region(cmr); 127 128 // Generation Counters, generation 'level', 1 subspace 129 _gen_counters = new PSGenerationCounters(perf_data_name, level, 1, 130 virtual_space()); 131 _space_counters = new SpaceCounters(perf_data_name, 0, 132 virtual_space()->reserved_size(), 133 _object_space, _gen_counters); 134 } 135 136 // Assume that the generation has been allocated if its 137 // reserved size is not 0. 138 bool PSOldGen::is_allocated() { 139 return virtual_space()->reserved_size() != 0; 140 } 141 142 void PSOldGen::precompact() { 143 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 144 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); 145 146 // Reset start array first. 147 debug_only(if (!UseParallelOldGC || !VerifyParallelOldWithMarkSweep) {) 148 start_array()->reset(); 149 debug_only(}) 150 151 object_mark_sweep()->precompact(); 152 153 // Now compact the young gen 154 heap->young_gen()->precompact(); 155 } 156 157 void PSOldGen::adjust_pointers() { 158 object_mark_sweep()->adjust_pointers(); 159 } 160 161 void PSOldGen::compact() { 162 object_mark_sweep()->compact(ZapUnusedHeapArea); 163 } 164 165 void PSOldGen::move_and_update(ParCompactionManager* cm) { 166 PSParallelCompact::move_and_update(cm, PSParallelCompact::old_space_id); 167 } 168 169 size_t PSOldGen::contiguous_available() const { 170 return object_space()->free_in_bytes() + virtual_space()->uncommitted_size(); 171 } 172 173 // Allocation. We report all successful allocations to the size policy 174 // Note that the perm gen does not use this method, and should not! 175 HeapWord* PSOldGen::allocate(size_t word_size, bool is_tlab) { 176 assert_locked_or_safepoint(Heap_lock); 177 HeapWord* res = allocate_noexpand(word_size, is_tlab); 178 179 if (res == NULL) { 180 res = expand_and_allocate(word_size, is_tlab); 181 } 182 183 // Allocations in the old generation need to be reported 184 if (res != NULL) { 185 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 186 heap->size_policy()->tenured_allocation(word_size); 187 } 188 189 return res; 190 } 191 192 HeapWord* PSOldGen::expand_and_allocate(size_t word_size, bool is_tlab) { 193 assert(!is_tlab, "TLAB's are not supported in PSOldGen"); 194 expand(word_size*HeapWordSize); 195 if (GCExpandToAllocateDelayMillis > 0) { 196 os::sleep(Thread::current(), GCExpandToAllocateDelayMillis, false); 197 } 198 return allocate_noexpand(word_size, is_tlab); 199 } 200 201 HeapWord* PSOldGen::expand_and_cas_allocate(size_t word_size) { 202 expand(word_size*HeapWordSize); 203 if (GCExpandToAllocateDelayMillis > 0) { 204 os::sleep(Thread::current(), GCExpandToAllocateDelayMillis, false); 205 } 206 return cas_allocate_noexpand(word_size); 207 } 208 209 void PSOldGen::expand(size_t bytes) { 210 MutexLocker x(ExpandHeap_lock); 211 const size_t alignment = virtual_space()->alignment(); 212 size_t aligned_bytes = align_size_up(bytes, alignment); 213 size_t aligned_expand_bytes = align_size_up(MinHeapDeltaBytes, alignment); 214 215 bool success = false; 216 if (aligned_expand_bytes > aligned_bytes) { 217 success = expand_by(aligned_expand_bytes); 218 } 219 if (!success) { 220 success = expand_by(aligned_bytes); 221 } 222 if (!success) { 223 success = expand_to_reserved(); 224 } 225 226 if (GC_locker::is_active()) { 227 if (PrintGC && Verbose) { 228 gclog_or_tty->print_cr("Garbage collection disabled, expanded heap instead"); 229 } 230 } 231 } 232 233 bool PSOldGen::expand_by(size_t bytes) { 234 assert_lock_strong(ExpandHeap_lock); 235 assert_locked_or_safepoint(Heap_lock); 236 bool result = virtual_space()->expand_by(bytes); 237 if (result) { 238 post_resize(); 239 if (UsePerfData) { 240 _space_counters->update_capacity(); 241 _gen_counters->update_all(); 242 } 243 } 244 245 if (result && Verbose && PrintGC) { 246 size_t new_mem_size = virtual_space()->committed_size(); 247 size_t old_mem_size = new_mem_size - bytes; 248 gclog_or_tty->print_cr("Expanding %s from " SIZE_FORMAT "K by " 249 SIZE_FORMAT "K to " 250 SIZE_FORMAT "K", 251 name(), old_mem_size/K, bytes/K, new_mem_size/K); 252 } 253 254 return result; 255 } 256 257 bool PSOldGen::expand_to_reserved() { 258 assert_lock_strong(ExpandHeap_lock); 259 assert_locked_or_safepoint(Heap_lock); 260 261 bool result = true; 262 const size_t remaining_bytes = virtual_space()->uncommitted_size(); 263 if (remaining_bytes > 0) { 264 result = expand_by(remaining_bytes); 265 DEBUG_ONLY(if (!result) warning("grow to reserve failed")); 266 } 267 return result; 268 } 269 270 void PSOldGen::shrink(size_t bytes) { 271 assert_lock_strong(ExpandHeap_lock); 272 assert_locked_or_safepoint(Heap_lock); 273 274 size_t size = align_size_down(bytes, virtual_space()->alignment()); 275 if (size > 0) { 276 assert_lock_strong(ExpandHeap_lock); 277 virtual_space()->shrink_by(bytes); 278 post_resize(); 279 280 if (Verbose && PrintGC) { 281 size_t new_mem_size = virtual_space()->committed_size(); 282 size_t old_mem_size = new_mem_size + bytes; 283 gclog_or_tty->print_cr("Shrinking %s from " SIZE_FORMAT "K by " 284 SIZE_FORMAT "K to " 285 SIZE_FORMAT "K", 286 name(), old_mem_size/K, bytes/K, new_mem_size/K); 287 } 288 } 289 } 290 291 void PSOldGen::resize(size_t desired_free_space) { 292 const size_t alignment = virtual_space()->alignment(); 293 const size_t size_before = virtual_space()->committed_size(); 294 size_t new_size = used_in_bytes() + desired_free_space; 295 if (new_size < used_in_bytes()) { 296 // Overflowed the addition. 297 new_size = gen_size_limit(); 298 } 299 // Adjust according to our min and max 300 new_size = MAX2(MIN2(new_size, gen_size_limit()), min_gen_size()); 301 302 assert(gen_size_limit() >= reserved().byte_size(), "max new size problem?"); 303 new_size = align_size_up(new_size, alignment); 304 305 const size_t current_size = capacity_in_bytes(); 306 307 if (PrintAdaptiveSizePolicy && Verbose) { 308 gclog_or_tty->print_cr("AdaptiveSizePolicy::old generation size: " 309 "desired free: " SIZE_FORMAT " used: " SIZE_FORMAT 310 " new size: " SIZE_FORMAT " current size " SIZE_FORMAT 311 " gen limits: " SIZE_FORMAT " / " SIZE_FORMAT, 312 desired_free_space, used_in_bytes(), new_size, current_size, 313 gen_size_limit(), min_gen_size()); 314 } 315 316 if (new_size == current_size) { 317 // No change requested 318 return; 319 } 320 if (new_size > current_size) { 321 size_t change_bytes = new_size - current_size; 322 expand(change_bytes); 323 } else { 324 size_t change_bytes = current_size - new_size; 325 // shrink doesn't grab this lock, expand does. Is that right? 326 MutexLocker x(ExpandHeap_lock); 327 shrink(change_bytes); 328 } 329 330 if (PrintAdaptiveSizePolicy) { 331 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 332 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); 333 gclog_or_tty->print_cr("AdaptiveSizePolicy::old generation size: " 334 "collection: %d " 335 "(" SIZE_FORMAT ") -> (" SIZE_FORMAT ") ", 336 heap->total_collections(), 337 size_before, virtual_space()->committed_size()); 338 } 339 } 340 341 // NOTE! We need to be careful about resizing. During a GC, multiple 342 // allocators may be active during heap expansion. If we allow the 343 // heap resizing to become visible before we have correctly resized 344 // all heap related data structures, we may cause program failures. 345 void PSOldGen::post_resize() { 346 // First construct a memregion representing the new size 347 MemRegion new_memregion((HeapWord*)virtual_space()->low(), 348 (HeapWord*)virtual_space()->high()); 349 size_t new_word_size = new_memregion.word_size(); 350 351 start_array()->set_covered_region(new_memregion); 352 Universe::heap()->barrier_set()->resize_covered_region(new_memregion); 353 354 // Did we expand? 355 HeapWord* const virtual_space_high = (HeapWord*) virtual_space()->high(); 356 if (object_space()->end() < virtual_space_high) { 357 // We need to mangle the newly expanded area. The memregion spans 358 // end -> new_end, we assume that top -> end is already mangled. 359 // This cannot be safely tested for, as allocation may be taking 360 // place. 361 MemRegion mangle_region(object_space()->end(), virtual_space_high); 362 object_space()->mangle_region(mangle_region); 363 } 364 365 // ALWAYS do this last!! 366 object_space()->set_end(virtual_space_high); 367 368 assert(new_word_size == heap_word_size(object_space()->capacity_in_bytes()), 369 "Sanity"); 370 } 371 372 size_t PSOldGen::gen_size_limit() { 373 return _max_gen_size; 374 } 375 376 void PSOldGen::reset_after_change() { 377 ShouldNotReachHere(); 378 return; 379 } 380 381 size_t PSOldGen::available_for_expansion() { 382 ShouldNotReachHere(); 383 return 0; 384 } 385 386 size_t PSOldGen::available_for_contraction() { 387 ShouldNotReachHere(); 388 return 0; 389 } 390 391 void PSOldGen::print() const { print_on(tty);} 392 void PSOldGen::print_on(outputStream* st) const { 393 st->print(" %-15s", name()); 394 if (PrintGCDetails && Verbose) { 395 st->print(" total " SIZE_FORMAT ", used " SIZE_FORMAT, 396 capacity_in_bytes(), used_in_bytes()); 397 } else { 398 st->print(" total " SIZE_FORMAT "K, used " SIZE_FORMAT "K", 399 capacity_in_bytes()/K, used_in_bytes()/K); 400 } 401 st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT ")", 402 virtual_space()->low_boundary(), 403 virtual_space()->high(), 404 virtual_space()->high_boundary()); 405 406 st->print(" object"); object_space()->print_on(st); 407 } 408 409 void PSOldGen::print_used_change(size_t prev_used) const { 410 gclog_or_tty->print(" [%s:", name()); 411 gclog_or_tty->print(" " SIZE_FORMAT "K" 412 "->" SIZE_FORMAT "K" 413 "(" SIZE_FORMAT "K)", 414 prev_used / K, used_in_bytes() / K, 415 capacity_in_bytes() / K); 416 gclog_or_tty->print("]"); 417 } 418 419 void PSOldGen::update_counters() { 420 if (UsePerfData) { 421 _space_counters->update_all(); 422 _gen_counters->update_all(); 423 } 424 } 425 426 #ifndef PRODUCT 427 428 void PSOldGen::space_invariants() { 429 assert(object_space()->end() == (HeapWord*) virtual_space()->high(), 430 "Space invariant"); 431 assert(object_space()->bottom() == (HeapWord*) virtual_space()->low(), 432 "Space invariant"); 433 assert(virtual_space()->low_boundary() <= virtual_space()->low(), 434 "Space invariant"); 435 assert(virtual_space()->high_boundary() >= virtual_space()->high(), 436 "Space invariant"); 437 assert(virtual_space()->low_boundary() == (char*) _reserved.start(), 438 "Space invariant"); 439 assert(virtual_space()->high_boundary() == (char*) _reserved.end(), 440 "Space invariant"); 441 assert(virtual_space()->committed_size() <= virtual_space()->reserved_size(), 442 "Space invariant"); 443 } 444 #endif 445 446 void PSOldGen::verify(bool allow_dirty) { 447 object_space()->verify(allow_dirty); 448 } 449 class VerifyObjectStartArrayClosure : public ObjectClosure { 450 PSOldGen* _gen; 451 ObjectStartArray* _start_array; 452 453 public: 454 VerifyObjectStartArrayClosure(PSOldGen* gen, ObjectStartArray* start_array) : 455 _gen(gen), _start_array(start_array) { } 456 457 virtual void do_object(oop obj) { 458 HeapWord* test_addr = (HeapWord*)obj + 1; 459 guarantee(_start_array->object_start(test_addr) == (HeapWord*)obj, "ObjectStartArray cannot find start of object"); 460 guarantee(_start_array->is_block_allocated((HeapWord*)obj), "ObjectStartArray missing block allocation"); 461 } 462 }; 463 464 void PSOldGen::verify_object_start_array() { 465 VerifyObjectStartArrayClosure check( this, &_start_array ); 466 object_iterate(&check); 467 }