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