1 #ifdef USE_PRAGMA_IDENT_SRC 2 #pragma ident "@(#)asPSYoungGen.cpp 1.24 07/10/04 10:49:36 JVM" 3 #endif 4 /* 5 * Copyright 2003-2008 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/_asPSYoungGen.cpp.incl" 30 31 ASPSYoungGen::ASPSYoungGen(size_t init_byte_size, 32 size_t minimum_byte_size, 33 size_t byte_size_limit) : 34 PSYoungGen(init_byte_size, minimum_byte_size, byte_size_limit), 35 _gen_size_limit(byte_size_limit) { 36 } 37 38 39 ASPSYoungGen::ASPSYoungGen(PSVirtualSpace* vs, 40 size_t init_byte_size, 41 size_t minimum_byte_size, 42 size_t byte_size_limit) : 43 //PSYoungGen(init_byte_size, minimum_byte_size, byte_size_limit), 44 PSYoungGen(vs->committed_size(), minimum_byte_size, byte_size_limit), 45 _gen_size_limit(byte_size_limit) { 46 47 assert(vs->committed_size() == init_byte_size, "Cannot replace with"); 48 49 _virtual_space = vs; 50 } 51 52 void ASPSYoungGen::initialize_virtual_space(ReservedSpace rs, 53 size_t alignment) { 54 assert(_init_gen_size != 0, "Should have a finite size"); 55 _virtual_space = new PSVirtualSpaceHighToLow(rs, alignment); 56 if (!_virtual_space->expand_by(_init_gen_size)) { 57 vm_exit_during_initialization("Could not reserve enough space for " 58 "object heap"); 59 } 60 } 61 62 void ASPSYoungGen::initialize(ReservedSpace rs, size_t alignment) { 63 initialize_virtual_space(rs, alignment); 64 initialize_work(); 65 } 66 67 size_t ASPSYoungGen::available_for_expansion() { 68 69 size_t current_committed_size = virtual_space()->committed_size(); 70 assert((gen_size_limit() >= current_committed_size), 71 "generation size limit is wrong"); 72 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 73 size_t result = gen_size_limit() - current_committed_size; 74 size_t result_aligned = align_size_down(result, heap->young_gen_alignment()); 75 return result_aligned; 76 } 77 78 // Return the number of bytes the young gen is willing give up. 79 // 80 // Future implementations could check the survivors and if to_space is in the 81 // right place (below from_space), take a chunk from to_space. 82 size_t ASPSYoungGen::available_for_contraction() { 83 84 size_t uncommitted_bytes = virtual_space()->uncommitted_size(); 85 if (uncommitted_bytes != 0) { 86 return uncommitted_bytes; 87 } 88 89 if (eden_space()->is_empty()) { 90 // Respect the minimum size for eden and for the young gen as a whole. 91 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 92 const size_t eden_alignment = heap->intra_heap_alignment(); 93 const size_t gen_alignment = heap->young_gen_alignment(); 94 95 assert(eden_space()->capacity_in_bytes() >= eden_alignment, 96 "Alignment is wrong"); 97 size_t eden_avail = eden_space()->capacity_in_bytes() - eden_alignment; 98 eden_avail = align_size_down(eden_avail, gen_alignment); 99 100 assert(virtual_space()->committed_size() >= min_gen_size(), 101 "minimum gen size is wrong"); 102 size_t gen_avail = virtual_space()->committed_size() - min_gen_size(); 103 assert(virtual_space()->is_aligned(gen_avail), "not aligned"); 104 105 const size_t max_contraction = MIN2(eden_avail, gen_avail); 106 // See comment for ASPSOldGen::available_for_contraction() 107 // for reasons the "increment" fraction is used. 108 PSAdaptiveSizePolicy* policy = heap->size_policy(); 109 size_t result = policy->eden_increment_aligned_down(max_contraction); 110 size_t result_aligned = align_size_down(result, gen_alignment); 111 if (PrintAdaptiveSizePolicy && Verbose) { 112 gclog_or_tty->print_cr("ASPSYoungGen::available_for_contraction: %d K", 113 result_aligned/K); 114 gclog_or_tty->print_cr(" max_contraction %d K", max_contraction/K); 115 gclog_or_tty->print_cr(" eden_avail %d K", eden_avail/K); 116 gclog_or_tty->print_cr(" gen_avail %d K", gen_avail/K); 117 } 118 return result_aligned; 119 120 } 121 122 return 0; 123 } 124 125 // The current implementation only considers to the end of eden. 126 // If to_space is below from_space, to_space is not considered. 127 // to_space can be. 128 size_t ASPSYoungGen::available_to_live() { 129 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 130 const size_t alignment = heap->intra_heap_alignment(); 131 132 // Include any space that is committed but is not in eden. 133 size_t available = pointer_delta(eden_space()->bottom(), 134 virtual_space()->low(), 135 sizeof(char)); 136 137 const size_t eden_capacity = eden_space()->capacity_in_bytes(); 138 if (eden_space()->is_empty() && eden_capacity > alignment) { 139 available += eden_capacity - alignment; 140 } 141 return available; 142 } 143 144 // Similar to PSYoungGen::resize_generation() but 145 // allows sum of eden_size and 2 * survivor_size to exceed _max_gen_size 146 // expands at the low end of the virtual space 147 // moves the boundary between the generations in order to expand 148 // some additional diagnostics 149 // If no additional changes are required, this can be deleted 150 // and the changes factored back into PSYoungGen::resize_generation(). 151 bool ASPSYoungGen::resize_generation(size_t eden_size, size_t survivor_size) { 152 const size_t alignment = virtual_space()->alignment(); 153 size_t orig_size = virtual_space()->committed_size(); 154 bool size_changed = false; 155 156 // There used to be a guarantee here that 157 // (eden_size + 2*survivor_size) <= _max_gen_size 158 // This requirement is enforced by the calculation of desired_size 159 // below. It may not be true on entry since the size of the 160 // eden_size is no bounded by the generation size. 161 162 assert(max_size() == reserved().byte_size(), "max gen size problem?"); 163 assert(min_gen_size() <= orig_size && orig_size <= max_size(), 164 "just checking"); 165 166 // Adjust new generation size 167 const size_t eden_plus_survivors = 168 align_size_up(eden_size + 2 * survivor_size, alignment); 169 size_t desired_size = MAX2(MIN2(eden_plus_survivors, gen_size_limit()), 170 min_gen_size()); 171 assert(desired_size <= gen_size_limit(), "just checking"); 172 173 if (desired_size > orig_size) { 174 // Grow the generation 175 size_t change = desired_size - orig_size; 176 HeapWord* prev_low = (HeapWord*) virtual_space()->low(); 177 if (!virtual_space()->expand_by(change)) { 178 return false; 179 } 180 if (ZapUnusedHeapArea) { 181 // Mangle newly committed space immediately because it 182 // can be done here more simply that after the new 183 // spaces have been computed. 184 HeapWord* new_low = (HeapWord*) virtual_space()->low(); 185 assert(new_low < prev_low, "Did not grow"); 186 187 MemRegion mangle_region(new_low, prev_low); 188 SpaceMangler::mangle_region(mangle_region); 189 } 190 size_changed = true; 191 } else if (desired_size < orig_size) { 192 size_t desired_change = orig_size - desired_size; 193 194 // How much is available for shrinking. 195 size_t available_bytes = limit_gen_shrink(desired_change); 196 size_t change = MIN2(desired_change, available_bytes); 197 virtual_space()->shrink_by(change); 198 size_changed = true; 199 } else { 200 if (Verbose && PrintGC) { 201 if (orig_size == gen_size_limit()) { 202 gclog_or_tty->print_cr("ASPSYoung generation size at maximum: " 203 SIZE_FORMAT "K", orig_size/K); 204 } else if (orig_size == min_gen_size()) { 205 gclog_or_tty->print_cr("ASPSYoung generation size at minium: " 206 SIZE_FORMAT "K", orig_size/K); 207 } 208 } 209 } 210 211 if (size_changed) { 212 reset_after_change(); 213 if (Verbose && PrintGC) { 214 size_t current_size = virtual_space()->committed_size(); 215 gclog_or_tty->print_cr("ASPSYoung generation size changed: " 216 SIZE_FORMAT "K->" SIZE_FORMAT "K", 217 orig_size/K, current_size/K); 218 } 219 } 220 221 guarantee(eden_plus_survivors <= virtual_space()->committed_size() || 222 virtual_space()->committed_size() == max_size(), "Sanity"); 223 224 return true; 225 } 226 227 // Similar to PSYoungGen::resize_spaces() but 228 // eden always starts at the low end of the committed virtual space 229 // current implementation does not allow holes between the spaces 230 // _young_generation_boundary has to be reset because it changes. 231 // so additional verification 232 233 void ASPSYoungGen::resize_spaces(size_t requested_eden_size, 234 size_t requested_survivor_size) { 235 assert(UseAdaptiveSizePolicy, "sanity check"); 236 assert(requested_eden_size > 0 && requested_survivor_size > 0, 237 "just checking"); 238 239 space_invariants(); 240 241 // We require eden and to space to be empty 242 if ((!eden_space()->is_empty()) || (!to_space()->is_empty())) { 243 return; 244 } 245 246 if (PrintAdaptiveSizePolicy && Verbose) { 247 gclog_or_tty->print_cr("PSYoungGen::resize_spaces(requested_eden_size: " 248 SIZE_FORMAT 249 ", requested_survivor_size: " SIZE_FORMAT ")", 250 requested_eden_size, requested_survivor_size); 251 gclog_or_tty->print_cr(" eden: [" PTR_FORMAT ".." PTR_FORMAT ") " 252 SIZE_FORMAT, 253 eden_space()->bottom(), 254 eden_space()->end(), 255 pointer_delta(eden_space()->end(), 256 eden_space()->bottom(), 257 sizeof(char))); 258 gclog_or_tty->print_cr(" from: [" PTR_FORMAT ".." PTR_FORMAT ") " 259 SIZE_FORMAT, 260 from_space()->bottom(), 261 from_space()->end(), 262 pointer_delta(from_space()->end(), 263 from_space()->bottom(), 264 sizeof(char))); 265 gclog_or_tty->print_cr(" to: [" PTR_FORMAT ".." PTR_FORMAT ") " 266 SIZE_FORMAT, 267 to_space()->bottom(), 268 to_space()->end(), 269 pointer_delta( to_space()->end(), 270 to_space()->bottom(), 271 sizeof(char))); 272 } 273 274 // There's nothing to do if the new sizes are the same as the current 275 if (requested_survivor_size == to_space()->capacity_in_bytes() && 276 requested_survivor_size == from_space()->capacity_in_bytes() && 277 requested_eden_size == eden_space()->capacity_in_bytes()) { 278 if (PrintAdaptiveSizePolicy && Verbose) { 279 gclog_or_tty->print_cr(" capacities are the right sizes, returning"); 280 } 281 return; 282 } 283 284 char* eden_start = (char*)virtual_space()->low(); 285 char* eden_end = (char*)eden_space()->end(); 286 char* from_start = (char*)from_space()->bottom(); 287 char* from_end = (char*)from_space()->end(); 288 char* to_start = (char*)to_space()->bottom(); 289 char* to_end = (char*)to_space()->end(); 290 291 assert(eden_start < from_start, "Cannot push into from_space"); 292 293 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 294 const size_t alignment = heap->intra_heap_alignment(); 295 const bool maintain_minimum = 296 (requested_eden_size + 2 * requested_survivor_size) <= min_gen_size(); 297 298 bool eden_from_to_order = from_start < to_start; 299 // Check whether from space is below to space 300 if (eden_from_to_order) { 301 // Eden, from, to 302 303 if (PrintAdaptiveSizePolicy && Verbose) { 304 gclog_or_tty->print_cr(" Eden, from, to:"); 305 } 306 307 // Set eden 308 // "requested_eden_size" is a goal for the size of eden 309 // and may not be attainable. "eden_size" below is 310 // calculated based on the location of from-space and 311 // the goal for the size of eden. from-space is 312 // fixed in place because it contains live data. 313 // The calculation is done this way to avoid 32bit 314 // overflow (i.e., eden_start + requested_eden_size 315 // may too large for representation in 32bits). 316 size_t eden_size; 317 if (maintain_minimum) { 318 // Only make eden larger than the requested size if 319 // the minimum size of the generation has to be maintained. 320 // This could be done in general but policy at a higher 321 // level is determining a requested size for eden and that 322 // should be honored unless there is a fundamental reason. 323 eden_size = pointer_delta(from_start, 324 eden_start, 325 sizeof(char)); 326 } else { 327 eden_size = MIN2(requested_eden_size, 328 pointer_delta(from_start, eden_start, sizeof(char))); 329 } 330 331 eden_end = eden_start + eden_size; 332 assert(eden_end >= eden_start, "addition overflowed") 333 334 // To may resize into from space as long as it is clear of live data. 335 // From space must remain page aligned, though, so we need to do some 336 // extra calculations. 337 338 // First calculate an optimal to-space 339 to_end = (char*)virtual_space()->high(); 340 to_start = (char*)pointer_delta(to_end, 341 (char*)requested_survivor_size, 342 sizeof(char)); 343 344 // Does the optimal to-space overlap from-space? 345 if (to_start < (char*)from_space()->end()) { 346 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); 347 348 // Calculate the minimum offset possible for from_end 349 size_t from_size = 350 pointer_delta(from_space()->top(), from_start, sizeof(char)); 351 352 // Should we be in this method if from_space is empty? Why not the set_space method? FIX ME! 353 if (from_size == 0) { 354 from_size = alignment; 355 } else { 356 from_size = align_size_up(from_size, alignment); 357 } 358 359 from_end = from_start + from_size; 360 assert(from_end > from_start, "addition overflow or from_size problem"); 361 362 guarantee(from_end <= (char*)from_space()->end(), 363 "from_end moved to the right"); 364 365 // Now update to_start with the new from_end 366 to_start = MAX2(from_end, to_start); 367 } 368 369 guarantee(to_start != to_end, "to space is zero sized"); 370 371 if (PrintAdaptiveSizePolicy && Verbose) { 372 gclog_or_tty->print_cr(" [eden_start .. eden_end): " 373 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 374 eden_start, 375 eden_end, 376 pointer_delta(eden_end, eden_start, sizeof(char))); 377 gclog_or_tty->print_cr(" [from_start .. from_end): " 378 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 379 from_start, 380 from_end, 381 pointer_delta(from_end, from_start, sizeof(char))); 382 gclog_or_tty->print_cr(" [ to_start .. to_end): " 383 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 384 to_start, 385 to_end, 386 pointer_delta( to_end, to_start, sizeof(char))); 387 } 388 } else { 389 // Eden, to, from 390 if (PrintAdaptiveSizePolicy && Verbose) { 391 gclog_or_tty->print_cr(" Eden, to, from:"); 392 } 393 394 // To space gets priority over eden resizing. Note that we position 395 // to space as if we were able to resize from space, even though from 396 // space is not modified. 397 // Giving eden priority was tried and gave poorer performance. 398 to_end = (char*)pointer_delta(virtual_space()->high(), 399 (char*)requested_survivor_size, 400 sizeof(char)); 401 to_end = MIN2(to_end, from_start); 402 to_start = (char*)pointer_delta(to_end, (char*)requested_survivor_size, 403 sizeof(char)); 404 // if the space sizes are to be increased by several times then 405 // 'to_start' will point beyond the young generation. In this case 406 // 'to_start' should be adjusted. 407 to_start = MAX2(to_start, eden_start + alignment); 408 409 // Compute how big eden can be, then adjust end. 410 // See comments above on calculating eden_end. 411 size_t eden_size; 412 if (maintain_minimum) { 413 eden_size = pointer_delta(to_start, eden_start, sizeof(char)); 414 } else { 415 eden_size = MIN2(requested_eden_size, 416 pointer_delta(to_start, eden_start, sizeof(char))); 417 } 418 eden_end = eden_start + eden_size; 419 assert(eden_end >= eden_start, "addition overflowed") 420 421 // Don't let eden shrink down to 0 or less. 422 eden_end = MAX2(eden_end, eden_start + alignment); 423 to_start = MAX2(to_start, eden_end); 424 425 if (PrintAdaptiveSizePolicy && Verbose) { 426 gclog_or_tty->print_cr(" [eden_start .. eden_end): " 427 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 428 eden_start, 429 eden_end, 430 pointer_delta(eden_end, eden_start, sizeof(char))); 431 gclog_or_tty->print_cr(" [ to_start .. to_end): " 432 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 433 to_start, 434 to_end, 435 pointer_delta( to_end, to_start, sizeof(char))); 436 gclog_or_tty->print_cr(" [from_start .. from_end): " 437 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 438 from_start, 439 from_end, 440 pointer_delta(from_end, from_start, sizeof(char))); 441 } 442 } 443 444 445 guarantee((HeapWord*)from_start <= from_space()->bottom(), 446 "from start moved to the right"); 447 guarantee((HeapWord*)from_end >= from_space()->top(), 448 "from end moved into live data"); 449 assert(is_object_aligned((intptr_t)eden_start), "checking alignment"); 450 assert(is_object_aligned((intptr_t)from_start), "checking alignment"); 451 assert(is_object_aligned((intptr_t)to_start), "checking alignment"); 452 453 MemRegion edenMR((HeapWord*)eden_start, (HeapWord*)eden_end); 454 MemRegion toMR ((HeapWord*)to_start, (HeapWord*)to_end); 455 MemRegion fromMR((HeapWord*)from_start, (HeapWord*)from_end); 456 457 // Let's make sure the call to initialize doesn't reset "top"! 458 DEBUG_ONLY(HeapWord* old_from_top = from_space()->top();) 459 460 // For PrintAdaptiveSizePolicy block below 461 size_t old_from = from_space()->capacity_in_bytes(); 462 size_t old_to = to_space()->capacity_in_bytes(); 463 464 if (ZapUnusedHeapArea) { 465 // NUMA is a special case because a numa space is not mangled 466 // in order to not prematurely bind its address to memory to 467 // the wrong memory (i.e., don't want the GC thread to first 468 // touch the memory). The survivor spaces are not numa 469 // spaces and are mangled. 470 if (UseNUMA) { 471 if (eden_from_to_order) { 472 mangle_survivors(from_space(), fromMR, to_space(), toMR); 473 } else { 474 mangle_survivors(to_space(), toMR, from_space(), fromMR); 475 } 476 } 477 478 // If not mangling the spaces, do some checking to verify that 479 // the spaces are already mangled. 480 // The spaces should be correctly mangled at this point so 481 // do some checking here. Note that they are not being mangled 482 // in the calls to initialize(). 483 // Must check mangling before the spaces are reshaped. Otherwise, 484 // the bottom or end of one space may have moved into an area 485 // covered by another space and a failure of the check may 486 // not correctly indicate which space is not properly mangled. 487 488 HeapWord* limit = (HeapWord*) virtual_space()->high(); 489 eden_space()->check_mangled_unused_area(limit); 490 from_space()->check_mangled_unused_area(limit); 491 to_space()->check_mangled_unused_area(limit); 492 } 493 // When an existing space is being initialized, it is not 494 // mangled because the space has been previously mangled. 495 eden_space()->initialize(edenMR, 496 SpaceDecorator::Clear, 497 SpaceDecorator::DontMangle); 498 to_space()->initialize(toMR, 499 SpaceDecorator::Clear, 500 SpaceDecorator::DontMangle); 501 from_space()->initialize(fromMR, 502 SpaceDecorator::DontClear, 503 SpaceDecorator::DontMangle); 504 505 PSScavenge::set_young_generation_boundary(eden_space()->bottom()); 506 507 assert(from_space()->top() == old_from_top, "from top changed!"); 508 509 if (PrintAdaptiveSizePolicy) { 510 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 511 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); 512 513 gclog_or_tty->print("AdaptiveSizePolicy::survivor space sizes: " 514 "collection: %d " 515 "(" SIZE_FORMAT ", " SIZE_FORMAT ") -> " 516 "(" SIZE_FORMAT ", " SIZE_FORMAT ") ", 517 heap->total_collections(), 518 old_from, old_to, 519 from_space()->capacity_in_bytes(), 520 to_space()->capacity_in_bytes()); 521 gclog_or_tty->cr(); 522 } 523 space_invariants(); 524 } 525 void ASPSYoungGen::reset_after_change() { 526 assert_locked_or_safepoint(Heap_lock); 527 528 _reserved = MemRegion((HeapWord*)virtual_space()->low_boundary(), 529 (HeapWord*)virtual_space()->high_boundary()); 530 PSScavenge::reference_processor()->set_span(_reserved); 531 532 HeapWord* new_eden_bottom = (HeapWord*)virtual_space()->low(); 533 HeapWord* eden_bottom = eden_space()->bottom(); 534 if (new_eden_bottom != eden_bottom) { 535 MemRegion eden_mr(new_eden_bottom, eden_space()->end()); 536 eden_space()->initialize(eden_mr, 537 SpaceDecorator::Clear, 538 SpaceDecorator::Mangle); 539 PSScavenge::set_young_generation_boundary(eden_space()->bottom()); 540 } 541 MemRegion cmr((HeapWord*)virtual_space()->low(), 542 (HeapWord*)virtual_space()->high()); 543 Universe::heap()->barrier_set()->resize_covered_region(cmr); 544 545 space_invariants(); 546 }