1 /* 2 * Copyright (c) 2001, 2014, 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/psMarkSweepDecorator.hpp" 28 #include "gc_implementation/parallelScavenge/psScavenge.hpp" 29 #include "gc_implementation/parallelScavenge/psYoungGen.hpp" 30 #include "gc_implementation/shared/gcUtil.hpp" 31 #include "gc_implementation/shared/mutableNUMASpace.hpp" 32 #include "gc_implementation/shared/spaceDecorator.hpp" 33 #include "oops/oop.inline.hpp" 34 #include "runtime/java.hpp" 35 36 PSYoungGen::PSYoungGen(size_t initial_size, 37 size_t min_size, 38 size_t max_size) : 39 _init_gen_size(initial_size), 40 _min_gen_size(min_size), 41 _max_gen_size(max_size) 42 {} 43 44 void PSYoungGen::initialize_virtual_space(ReservedSpace rs, size_t alignment) { 45 assert(_init_gen_size != 0, "Should have a finite size"); 46 _virtual_space = new PSVirtualSpace(rs, alignment); 47 if (!virtual_space()->expand_by(_init_gen_size)) { 48 vm_exit_during_initialization("Could not reserve enough space for " 49 "object heap"); 50 } 51 } 52 53 void PSYoungGen::initialize(ReservedSpace rs, size_t alignment) { 54 initialize_virtual_space(rs, alignment); 55 initialize_work(); 56 } 57 58 void PSYoungGen::initialize_work() { 59 60 _reserved = MemRegion((HeapWord*)virtual_space()->low_boundary(), 61 (HeapWord*)virtual_space()->high_boundary()); 62 63 MemRegion cmr((HeapWord*)virtual_space()->low(), 64 (HeapWord*)virtual_space()->high()); 65 Universe::heap()->barrier_set()->resize_covered_region(cmr); 66 67 if (ZapUnusedHeapArea) { 68 // Mangle newly committed space immediately because it 69 // can be done here more simply that after the new 70 // spaces have been computed. 71 SpaceMangler::mangle_region(cmr); 72 } 73 74 if (UseNUMA) { 75 _eden_space = new MutableNUMASpace(virtual_space()->alignment()); 76 } else { 77 _eden_space = new MutableSpace(virtual_space()->alignment()); 78 } 79 _from_space = new MutableSpace(virtual_space()->alignment()); 80 _to_space = new MutableSpace(virtual_space()->alignment()); 81 82 if (_eden_space == NULL || _from_space == NULL || _to_space == NULL) { 83 vm_exit_during_initialization("Could not allocate a young gen space"); 84 } 85 86 // Allocate the mark sweep views of spaces 87 _eden_mark_sweep = 88 new PSMarkSweepDecorator(_eden_space, NULL, MarkSweepDeadRatio); 89 _from_mark_sweep = 90 new PSMarkSweepDecorator(_from_space, NULL, MarkSweepDeadRatio); 91 _to_mark_sweep = 92 new PSMarkSweepDecorator(_to_space, NULL, MarkSweepDeadRatio); 93 94 if (_eden_mark_sweep == NULL || 95 _from_mark_sweep == NULL || 96 _to_mark_sweep == NULL) { 97 vm_exit_during_initialization("Could not complete allocation" 98 " of the young generation"); 99 } 100 101 // Generation Counters - generation 0, 3 subspaces 102 _gen_counters = new PSGenerationCounters("new", 0, 3, _min_gen_size, 103 _max_gen_size, _virtual_space); 104 105 // Compute maximum space sizes for performance counters 106 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 107 size_t alignment = heap->space_alignment(); 108 size_t size = virtual_space()->reserved_size(); 109 110 size_t max_survivor_size; 111 size_t max_eden_size; 112 113 if (UseAdaptiveSizePolicy) { 114 max_survivor_size = size / MinSurvivorRatio; 115 116 // round the survivor space size down to the nearest alignment 117 // and make sure its size is greater than 0. 118 max_survivor_size = align_size_down(max_survivor_size, alignment); 119 max_survivor_size = MAX2(max_survivor_size, alignment); 120 121 // set the maximum size of eden to be the size of the young gen 122 // less two times the minimum survivor size. The minimum survivor 123 // size for UseAdaptiveSizePolicy is one alignment. 124 max_eden_size = size - 2 * alignment; 125 } else { 126 max_survivor_size = size / InitialSurvivorRatio; 127 128 // round the survivor space size down to the nearest alignment 129 // and make sure its size is greater than 0. 130 max_survivor_size = align_size_down(max_survivor_size, alignment); 131 max_survivor_size = MAX2(max_survivor_size, alignment); 132 133 // set the maximum size of eden to be the size of the young gen 134 // less two times the survivor size when the generation is 100% 135 // committed. The minimum survivor size for -UseAdaptiveSizePolicy 136 // is dependent on the committed portion (current capacity) of the 137 // generation - the less space committed, the smaller the survivor 138 // space, possibly as small as an alignment. However, we are interested 139 // in the case where the young generation is 100% committed, as this 140 // is the point where eden reaches its maximum size. At this point, 141 // the size of a survivor space is max_survivor_size. 142 max_eden_size = size - 2 * max_survivor_size; 143 } 144 145 _eden_counters = new SpaceCounters("eden", 0, max_eden_size, _eden_space, 146 _gen_counters); 147 _from_counters = new SpaceCounters("s0", 1, max_survivor_size, _from_space, 148 _gen_counters); 149 _to_counters = new SpaceCounters("s1", 2, max_survivor_size, _to_space, 150 _gen_counters); 151 152 compute_initial_space_boundaries(); 153 } 154 155 void PSYoungGen::compute_initial_space_boundaries() { 156 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 157 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); 158 159 // Compute sizes 160 size_t alignment = heap->space_alignment(); 161 size_t size = virtual_space()->committed_size(); 162 assert(size >= 3 * alignment, "Young space is not large enough for eden + 2 survivors"); 163 164 size_t survivor_size = size / InitialSurvivorRatio; 165 survivor_size = align_size_down(survivor_size, alignment); 166 // ... but never less than an alignment 167 survivor_size = MAX2(survivor_size, alignment); 168 169 // Young generation is eden + 2 survivor spaces 170 size_t eden_size = size - (2 * survivor_size); 171 172 // Now go ahead and set 'em. 173 set_space_boundaries(eden_size, survivor_size); 174 space_invariants(); 175 176 if (UsePerfData) { 177 _eden_counters->update_capacity(); 178 _from_counters->update_capacity(); 179 _to_counters->update_capacity(); 180 } 181 } 182 183 void PSYoungGen::set_space_boundaries(size_t eden_size, size_t survivor_size) { 184 assert(eden_size < virtual_space()->committed_size(), "just checking"); 185 assert(eden_size > 0 && survivor_size > 0, "just checking"); 186 187 // Initial layout is Eden, to, from. After swapping survivor spaces, 188 // that leaves us with Eden, from, to, which is step one in our two 189 // step resize-with-live-data procedure. 190 char *eden_start = virtual_space()->low(); 191 char *to_start = eden_start + eden_size; 192 char *from_start = to_start + survivor_size; 193 char *from_end = from_start + survivor_size; 194 195 assert(from_end == virtual_space()->high(), "just checking"); 196 assert(is_object_aligned((intptr_t)eden_start), "checking alignment"); 197 assert(is_object_aligned((intptr_t)to_start), "checking alignment"); 198 assert(is_object_aligned((intptr_t)from_start), "checking alignment"); 199 200 MemRegion eden_mr((HeapWord*)eden_start, (HeapWord*)to_start); 201 MemRegion to_mr ((HeapWord*)to_start, (HeapWord*)from_start); 202 MemRegion from_mr((HeapWord*)from_start, (HeapWord*)from_end); 203 204 eden_space()->initialize(eden_mr, true, ZapUnusedHeapArea); 205 to_space()->initialize(to_mr , true, ZapUnusedHeapArea); 206 from_space()->initialize(from_mr, true, ZapUnusedHeapArea); 207 } 208 209 #ifndef PRODUCT 210 void PSYoungGen::space_invariants() { 211 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 212 const size_t alignment = heap->space_alignment(); 213 214 // Currently, our eden size cannot shrink to zero 215 guarantee(eden_space()->capacity_in_bytes() >= alignment, "eden too small"); 216 guarantee(from_space()->capacity_in_bytes() >= alignment, "from too small"); 217 guarantee(to_space()->capacity_in_bytes() >= alignment, "to too small"); 218 219 // Relationship of spaces to each other 220 char* eden_start = (char*)eden_space()->bottom(); 221 char* eden_end = (char*)eden_space()->end(); 222 char* from_start = (char*)from_space()->bottom(); 223 char* from_end = (char*)from_space()->end(); 224 char* to_start = (char*)to_space()->bottom(); 225 char* to_end = (char*)to_space()->end(); 226 227 guarantee(eden_start >= virtual_space()->low(), "eden bottom"); 228 guarantee(eden_start < eden_end, "eden space consistency"); 229 guarantee(from_start < from_end, "from space consistency"); 230 guarantee(to_start < to_end, "to space consistency"); 231 232 // Check whether from space is below to space 233 if (from_start < to_start) { 234 // Eden, from, to 235 guarantee(eden_end <= from_start, "eden/from boundary"); 236 guarantee(from_end <= to_start, "from/to boundary"); 237 guarantee(to_end <= virtual_space()->high(), "to end"); 238 } else { 239 // Eden, to, from 240 guarantee(eden_end <= to_start, "eden/to boundary"); 241 guarantee(to_end <= from_start, "to/from boundary"); 242 guarantee(from_end <= virtual_space()->high(), "from end"); 243 } 244 245 // More checks that the virtual space is consistent with the spaces 246 assert(virtual_space()->committed_size() >= 247 (eden_space()->capacity_in_bytes() + 248 to_space()->capacity_in_bytes() + 249 from_space()->capacity_in_bytes()), "Committed size is inconsistent"); 250 assert(virtual_space()->committed_size() <= virtual_space()->reserved_size(), 251 "Space invariant"); 252 char* eden_top = (char*)eden_space()->top(); 253 char* from_top = (char*)from_space()->top(); 254 char* to_top = (char*)to_space()->top(); 255 assert(eden_top <= virtual_space()->high(), "eden top"); 256 assert(from_top <= virtual_space()->high(), "from top"); 257 assert(to_top <= virtual_space()->high(), "to top"); 258 259 virtual_space()->verify(); 260 } 261 #endif 262 263 void PSYoungGen::resize(size_t eden_size, size_t survivor_size) { 264 // Resize the generation if needed. If the generation resize 265 // reports false, do not attempt to resize the spaces. 266 if (resize_generation(eden_size, survivor_size)) { 267 // Then we lay out the spaces inside the generation 268 resize_spaces(eden_size, survivor_size); 269 270 space_invariants(); 271 272 if (PrintAdaptiveSizePolicy && Verbose) { 273 gclog_or_tty->print_cr("Young generation size: " 274 "desired eden: " SIZE_FORMAT " survivor: " SIZE_FORMAT 275 " used: " SIZE_FORMAT " capacity: " SIZE_FORMAT 276 " gen limits: " SIZE_FORMAT " / " SIZE_FORMAT, 277 eden_size, survivor_size, used_in_bytes(), capacity_in_bytes(), 278 _max_gen_size, min_gen_size()); 279 } 280 } 281 } 282 283 284 bool PSYoungGen::resize_generation(size_t eden_size, size_t survivor_size) { 285 const size_t alignment = virtual_space()->alignment(); 286 size_t orig_size = virtual_space()->committed_size(); 287 bool size_changed = false; 288 289 // There used to be this guarantee there. 290 // guarantee ((eden_size + 2*survivor_size) <= _max_gen_size, "incorrect input arguments"); 291 // Code below forces this requirement. In addition the desired eden 292 // size and desired survivor sizes are desired goals and may 293 // exceed the total generation size. 294 295 assert(min_gen_size() <= orig_size && orig_size <= max_size(), "just checking"); 296 297 // Adjust new generation size 298 const size_t eden_plus_survivors = 299 align_size_up(eden_size + 2 * survivor_size, alignment); 300 size_t desired_size = MAX2(MIN2(eden_plus_survivors, max_size()), 301 min_gen_size()); 302 assert(desired_size <= max_size(), "just checking"); 303 304 if (desired_size > orig_size) { 305 // Grow the generation 306 size_t change = desired_size - orig_size; 307 assert(change % alignment == 0, "just checking"); 308 HeapWord* prev_high = (HeapWord*) virtual_space()->high(); 309 if (!virtual_space()->expand_by(change)) { 310 return false; // Error if we fail to resize! 311 } 312 if (ZapUnusedHeapArea) { 313 // Mangle newly committed space immediately because it 314 // can be done here more simply that after the new 315 // spaces have been computed. 316 HeapWord* new_high = (HeapWord*) virtual_space()->high(); 317 MemRegion mangle_region(prev_high, new_high); 318 SpaceMangler::mangle_region(mangle_region); 319 } 320 size_changed = true; 321 } else if (desired_size < orig_size) { 322 size_t desired_change = orig_size - desired_size; 323 assert(desired_change % alignment == 0, "just checking"); 324 325 desired_change = limit_gen_shrink(desired_change); 326 327 if (desired_change > 0) { 328 virtual_space()->shrink_by(desired_change); 329 reset_survivors_after_shrink(); 330 331 size_changed = true; 332 } 333 } else { 334 if (Verbose && PrintGC) { 335 if (orig_size == gen_size_limit()) { 336 gclog_or_tty->print_cr("PSYoung generation size at maximum: " 337 SIZE_FORMAT "K", orig_size/K); 338 } else if (orig_size == min_gen_size()) { 339 gclog_or_tty->print_cr("PSYoung generation size at minium: " 340 SIZE_FORMAT "K", orig_size/K); 341 } 342 } 343 } 344 345 if (size_changed) { 346 post_resize(); 347 348 if (Verbose && PrintGC) { 349 size_t current_size = virtual_space()->committed_size(); 350 gclog_or_tty->print_cr("PSYoung generation size changed: " 351 SIZE_FORMAT "K->" SIZE_FORMAT "K", 352 orig_size/K, current_size/K); 353 } 354 } 355 356 guarantee(eden_plus_survivors <= virtual_space()->committed_size() || 357 virtual_space()->committed_size() == max_size(), "Sanity"); 358 359 return true; 360 } 361 362 #ifndef PRODUCT 363 // In the numa case eden is not mangled so a survivor space 364 // moving into a region previously occupied by a survivor 365 // may find an unmangled region. Also in the PS case eden 366 // to-space and from-space may not touch (i.e., there may be 367 // gaps between them due to movement while resizing the 368 // spaces). Those gaps must be mangled. 369 void PSYoungGen::mangle_survivors(MutableSpace* s1, 370 MemRegion s1MR, 371 MutableSpace* s2, 372 MemRegion s2MR) { 373 // Check eden and gap between eden and from-space, in deciding 374 // what to mangle in from-space. Check the gap between from-space 375 // and to-space when deciding what to mangle. 376 // 377 // +--------+ +----+ +---+ 378 // | eden | |s1 | |s2 | 379 // +--------+ +----+ +---+ 380 // +-------+ +-----+ 381 // |s1MR | |s2MR | 382 // +-------+ +-----+ 383 // All of survivor-space is properly mangled so find the 384 // upper bound on the mangling for any portion above current s1. 385 HeapWord* delta_end = MIN2(s1->bottom(), s1MR.end()); 386 MemRegion delta1_left; 387 if (s1MR.start() < delta_end) { 388 delta1_left = MemRegion(s1MR.start(), delta_end); 389 s1->mangle_region(delta1_left); 390 } 391 // Find any portion to the right of the current s1. 392 HeapWord* delta_start = MAX2(s1->end(), s1MR.start()); 393 MemRegion delta1_right; 394 if (delta_start < s1MR.end()) { 395 delta1_right = MemRegion(delta_start, s1MR.end()); 396 s1->mangle_region(delta1_right); 397 } 398 399 // Similarly for the second survivor space except that 400 // any of the new region that overlaps with the current 401 // region of the first survivor space has already been 402 // mangled. 403 delta_end = MIN2(s2->bottom(), s2MR.end()); 404 delta_start = MAX2(s2MR.start(), s1->end()); 405 MemRegion delta2_left; 406 if (s2MR.start() < delta_end) { 407 delta2_left = MemRegion(s2MR.start(), delta_end); 408 s2->mangle_region(delta2_left); 409 } 410 delta_start = MAX2(s2->end(), s2MR.start()); 411 MemRegion delta2_right; 412 if (delta_start < s2MR.end()) { 413 s2->mangle_region(delta2_right); 414 } 415 416 if (TraceZapUnusedHeapArea) { 417 // s1 418 gclog_or_tty->print_cr("Current region: [" PTR_FORMAT ", " PTR_FORMAT ") " 419 "New region: [" PTR_FORMAT ", " PTR_FORMAT ")", 420 p2i(s1->bottom()), p2i(s1->end()), 421 p2i(s1MR.start()), p2i(s1MR.end())); 422 gclog_or_tty->print_cr(" Mangle before: [" PTR_FORMAT ", " 423 PTR_FORMAT ") Mangle after: [" PTR_FORMAT ", " PTR_FORMAT ")", 424 p2i(delta1_left.start()), p2i(delta1_left.end()), 425 p2i(delta1_right.start()), p2i(delta1_right.end())); 426 427 // s2 428 gclog_or_tty->print_cr("Current region: [" PTR_FORMAT ", " PTR_FORMAT ") " 429 "New region: [" PTR_FORMAT ", " PTR_FORMAT ")", 430 p2i(s2->bottom()), p2i(s2->end()), 431 p2i(s2MR.start()), p2i(s2MR.end())); 432 gclog_or_tty->print_cr(" Mangle before: [" PTR_FORMAT ", " 433 PTR_FORMAT ") Mangle after: [" PTR_FORMAT ", " PTR_FORMAT ")", 434 p2i(delta2_left.start()), p2i(delta2_left.end()), 435 p2i(delta2_right.start()), p2i(delta2_right.end())); 436 } 437 438 } 439 #endif // NOT PRODUCT 440 441 void PSYoungGen::resize_spaces(size_t requested_eden_size, 442 size_t requested_survivor_size) { 443 assert(UseAdaptiveSizePolicy, "sanity check"); 444 assert(requested_eden_size > 0 && requested_survivor_size > 0, 445 "just checking"); 446 447 // We require eden and to space to be empty 448 if ((!eden_space()->is_empty()) || (!to_space()->is_empty())) { 449 return; 450 } 451 452 if (PrintAdaptiveSizePolicy && Verbose) { 453 gclog_or_tty->print_cr("PSYoungGen::resize_spaces(requested_eden_size: " 454 SIZE_FORMAT 455 ", requested_survivor_size: " SIZE_FORMAT ")", 456 requested_eden_size, requested_survivor_size); 457 gclog_or_tty->print_cr(" eden: [" PTR_FORMAT ".." PTR_FORMAT ") " 458 SIZE_FORMAT, 459 p2i(eden_space()->bottom()), 460 p2i(eden_space()->end()), 461 pointer_delta(eden_space()->end(), 462 eden_space()->bottom(), 463 sizeof(char))); 464 gclog_or_tty->print_cr(" from: [" PTR_FORMAT ".." PTR_FORMAT ") " 465 SIZE_FORMAT, 466 p2i(from_space()->bottom()), 467 p2i(from_space()->end()), 468 pointer_delta(from_space()->end(), 469 from_space()->bottom(), 470 sizeof(char))); 471 gclog_or_tty->print_cr(" to: [" PTR_FORMAT ".." PTR_FORMAT ") " 472 SIZE_FORMAT, 473 p2i(to_space()->bottom()), 474 p2i(to_space()->end()), 475 pointer_delta( to_space()->end(), 476 to_space()->bottom(), 477 sizeof(char))); 478 } 479 480 // There's nothing to do if the new sizes are the same as the current 481 if (requested_survivor_size == to_space()->capacity_in_bytes() && 482 requested_survivor_size == from_space()->capacity_in_bytes() && 483 requested_eden_size == eden_space()->capacity_in_bytes()) { 484 if (PrintAdaptiveSizePolicy && Verbose) { 485 gclog_or_tty->print_cr(" capacities are the right sizes, returning"); 486 } 487 return; 488 } 489 490 char* eden_start = (char*)eden_space()->bottom(); 491 char* eden_end = (char*)eden_space()->end(); 492 char* from_start = (char*)from_space()->bottom(); 493 char* from_end = (char*)from_space()->end(); 494 char* to_start = (char*)to_space()->bottom(); 495 char* to_end = (char*)to_space()->end(); 496 497 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 498 const size_t alignment = heap->space_alignment(); 499 const bool maintain_minimum = 500 (requested_eden_size + 2 * requested_survivor_size) <= min_gen_size(); 501 502 bool eden_from_to_order = from_start < to_start; 503 // Check whether from space is below to space 504 if (eden_from_to_order) { 505 // Eden, from, to 506 eden_from_to_order = true; 507 if (PrintAdaptiveSizePolicy && Verbose) { 508 gclog_or_tty->print_cr(" Eden, from, to:"); 509 } 510 511 // Set eden 512 // "requested_eden_size" is a goal for the size of eden 513 // and may not be attainable. "eden_size" below is 514 // calculated based on the location of from-space and 515 // the goal for the size of eden. from-space is 516 // fixed in place because it contains live data. 517 // The calculation is done this way to avoid 32bit 518 // overflow (i.e., eden_start + requested_eden_size 519 // may too large for representation in 32bits). 520 size_t eden_size; 521 if (maintain_minimum) { 522 // Only make eden larger than the requested size if 523 // the minimum size of the generation has to be maintained. 524 // This could be done in general but policy at a higher 525 // level is determining a requested size for eden and that 526 // should be honored unless there is a fundamental reason. 527 eden_size = pointer_delta(from_start, 528 eden_start, 529 sizeof(char)); 530 } else { 531 eden_size = MIN2(requested_eden_size, 532 pointer_delta(from_start, eden_start, sizeof(char))); 533 } 534 535 eden_end = eden_start + eden_size; 536 assert(eden_end >= eden_start, "addition overflowed"); 537 538 // To may resize into from space as long as it is clear of live data. 539 // From space must remain page aligned, though, so we need to do some 540 // extra calculations. 541 542 // First calculate an optimal to-space 543 to_end = (char*)virtual_space()->high(); 544 to_start = (char*)pointer_delta(to_end, (char*)requested_survivor_size, 545 sizeof(char)); 546 547 // Does the optimal to-space overlap from-space? 548 if (to_start < (char*)from_space()->end()) { 549 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); 550 551 // Calculate the minimum offset possible for from_end 552 size_t from_size = pointer_delta(from_space()->top(), from_start, sizeof(char)); 553 554 // Should we be in this method if from_space is empty? Why not the set_space method? FIX ME! 555 if (from_size == 0) { 556 from_size = alignment; 557 } else { 558 from_size = align_size_up(from_size, alignment); 559 } 560 561 from_end = from_start + from_size; 562 assert(from_end > from_start, "addition overflow or from_size problem"); 563 564 guarantee(from_end <= (char*)from_space()->end(), "from_end moved to the right"); 565 566 // Now update to_start with the new from_end 567 to_start = MAX2(from_end, to_start); 568 } 569 570 guarantee(to_start != to_end, "to space is zero sized"); 571 572 if (PrintAdaptiveSizePolicy && Verbose) { 573 gclog_or_tty->print_cr(" [eden_start .. eden_end): " 574 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 575 p2i(eden_start), 576 p2i(eden_end), 577 pointer_delta(eden_end, eden_start, sizeof(char))); 578 gclog_or_tty->print_cr(" [from_start .. from_end): " 579 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 580 p2i(from_start), 581 p2i(from_end), 582 pointer_delta(from_end, from_start, sizeof(char))); 583 gclog_or_tty->print_cr(" [ to_start .. to_end): " 584 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 585 p2i(to_start), 586 p2i(to_end), 587 pointer_delta( to_end, to_start, sizeof(char))); 588 } 589 } else { 590 // Eden, to, from 591 if (PrintAdaptiveSizePolicy && Verbose) { 592 gclog_or_tty->print_cr(" Eden, to, from:"); 593 } 594 595 // To space gets priority over eden resizing. Note that we position 596 // to space as if we were able to resize from space, even though from 597 // space is not modified. 598 // Giving eden priority was tried and gave poorer performance. 599 to_end = (char*)pointer_delta(virtual_space()->high(), 600 (char*)requested_survivor_size, 601 sizeof(char)); 602 to_end = MIN2(to_end, from_start); 603 to_start = (char*)pointer_delta(to_end, (char*)requested_survivor_size, 604 sizeof(char)); 605 // if the space sizes are to be increased by several times then 606 // 'to_start' will point beyond the young generation. In this case 607 // 'to_start' should be adjusted. 608 to_start = MAX2(to_start, eden_start + alignment); 609 610 // Compute how big eden can be, then adjust end. 611 // See comments above on calculating eden_end. 612 size_t eden_size; 613 if (maintain_minimum) { 614 eden_size = pointer_delta(to_start, eden_start, sizeof(char)); 615 } else { 616 eden_size = MIN2(requested_eden_size, 617 pointer_delta(to_start, eden_start, sizeof(char))); 618 } 619 eden_end = eden_start + eden_size; 620 assert(eden_end >= eden_start, "addition overflowed"); 621 622 // Could choose to not let eden shrink 623 // to_start = MAX2(to_start, eden_end); 624 625 // Don't let eden shrink down to 0 or less. 626 eden_end = MAX2(eden_end, eden_start + alignment); 627 to_start = MAX2(to_start, eden_end); 628 629 if (PrintAdaptiveSizePolicy && Verbose) { 630 gclog_or_tty->print_cr(" [eden_start .. eden_end): " 631 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 632 p2i(eden_start), 633 p2i(eden_end), 634 pointer_delta(eden_end, eden_start, sizeof(char))); 635 gclog_or_tty->print_cr(" [ to_start .. to_end): " 636 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 637 p2i(to_start), 638 p2i(to_end), 639 pointer_delta( to_end, to_start, sizeof(char))); 640 gclog_or_tty->print_cr(" [from_start .. from_end): " 641 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 642 p2i(from_start), 643 p2i(from_end), 644 pointer_delta(from_end, from_start, sizeof(char))); 645 } 646 } 647 648 649 guarantee((HeapWord*)from_start <= from_space()->bottom(), 650 "from start moved to the right"); 651 guarantee((HeapWord*)from_end >= from_space()->top(), 652 "from end moved into live data"); 653 assert(is_object_aligned((intptr_t)eden_start), "checking alignment"); 654 assert(is_object_aligned((intptr_t)from_start), "checking alignment"); 655 assert(is_object_aligned((intptr_t)to_start), "checking alignment"); 656 657 MemRegion edenMR((HeapWord*)eden_start, (HeapWord*)eden_end); 658 MemRegion toMR ((HeapWord*)to_start, (HeapWord*)to_end); 659 MemRegion fromMR((HeapWord*)from_start, (HeapWord*)from_end); 660 661 // Let's make sure the call to initialize doesn't reset "top"! 662 HeapWord* old_from_top = from_space()->top(); 663 664 // For PrintAdaptiveSizePolicy block below 665 size_t old_from = from_space()->capacity_in_bytes(); 666 size_t old_to = to_space()->capacity_in_bytes(); 667 668 if (ZapUnusedHeapArea) { 669 // NUMA is a special case because a numa space is not mangled 670 // in order to not prematurely bind its address to memory to 671 // the wrong memory (i.e., don't want the GC thread to first 672 // touch the memory). The survivor spaces are not numa 673 // spaces and are mangled. 674 if (UseNUMA) { 675 if (eden_from_to_order) { 676 mangle_survivors(from_space(), fromMR, to_space(), toMR); 677 } else { 678 mangle_survivors(to_space(), toMR, from_space(), fromMR); 679 } 680 } 681 682 // If not mangling the spaces, do some checking to verify that 683 // the spaces are already mangled. 684 // The spaces should be correctly mangled at this point so 685 // do some checking here. Note that they are not being mangled 686 // in the calls to initialize(). 687 // Must check mangling before the spaces are reshaped. Otherwise, 688 // the bottom or end of one space may have moved into an area 689 // covered by another space and a failure of the check may 690 // not correctly indicate which space is not properly mangled. 691 HeapWord* limit = (HeapWord*) virtual_space()->high(); 692 eden_space()->check_mangled_unused_area(limit); 693 from_space()->check_mangled_unused_area(limit); 694 to_space()->check_mangled_unused_area(limit); 695 } 696 // When an existing space is being initialized, it is not 697 // mangled because the space has been previously mangled. 698 eden_space()->initialize(edenMR, 699 SpaceDecorator::Clear, 700 SpaceDecorator::DontMangle); 701 to_space()->initialize(toMR, 702 SpaceDecorator::Clear, 703 SpaceDecorator::DontMangle); 704 from_space()->initialize(fromMR, 705 SpaceDecorator::DontClear, 706 SpaceDecorator::DontMangle); 707 708 assert(from_space()->top() == old_from_top, "from top changed!"); 709 710 if (PrintAdaptiveSizePolicy) { 711 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 712 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); 713 714 gclog_or_tty->print("AdaptiveSizePolicy::survivor space sizes: " 715 "collection: %d " 716 "(" SIZE_FORMAT ", " SIZE_FORMAT ") -> " 717 "(" SIZE_FORMAT ", " SIZE_FORMAT ") ", 718 heap->total_collections(), 719 old_from, old_to, 720 from_space()->capacity_in_bytes(), 721 to_space()->capacity_in_bytes()); 722 gclog_or_tty->cr(); 723 } 724 } 725 726 void PSYoungGen::swap_spaces() { 727 MutableSpace* s = from_space(); 728 _from_space = to_space(); 729 _to_space = s; 730 731 // Now update the decorators. 732 PSMarkSweepDecorator* md = from_mark_sweep(); 733 _from_mark_sweep = to_mark_sweep(); 734 _to_mark_sweep = md; 735 736 assert(from_mark_sweep()->space() == from_space(), "Sanity"); 737 assert(to_mark_sweep()->space() == to_space(), "Sanity"); 738 } 739 740 size_t PSYoungGen::capacity_in_bytes() const { 741 return eden_space()->capacity_in_bytes() 742 + from_space()->capacity_in_bytes(); // to_space() is only used during scavenge 743 } 744 745 746 size_t PSYoungGen::used_in_bytes() const { 747 return eden_space()->used_in_bytes() 748 + from_space()->used_in_bytes(); // to_space() is only used during scavenge 749 } 750 751 752 size_t PSYoungGen::free_in_bytes() const { 753 return eden_space()->free_in_bytes() 754 + from_space()->free_in_bytes(); // to_space() is only used during scavenge 755 } 756 757 size_t PSYoungGen::capacity_in_words() const { 758 return eden_space()->capacity_in_words() 759 + from_space()->capacity_in_words(); // to_space() is only used during scavenge 760 } 761 762 763 size_t PSYoungGen::used_in_words() const { 764 return eden_space()->used_in_words() 765 + from_space()->used_in_words(); // to_space() is only used during scavenge 766 } 767 768 769 size_t PSYoungGen::free_in_words() const { 770 return eden_space()->free_in_words() 771 + from_space()->free_in_words(); // to_space() is only used during scavenge 772 } 773 774 void PSYoungGen::object_iterate(ObjectClosure* blk) { 775 eden_space()->object_iterate(blk); 776 from_space()->object_iterate(blk); 777 to_space()->object_iterate(blk); 778 } 779 780 void PSYoungGen::precompact() { 781 eden_mark_sweep()->precompact(); 782 from_mark_sweep()->precompact(); 783 to_mark_sweep()->precompact(); 784 } 785 786 void PSYoungGen::adjust_pointers() { 787 eden_mark_sweep()->adjust_pointers(); 788 from_mark_sweep()->adjust_pointers(); 789 to_mark_sweep()->adjust_pointers(); 790 } 791 792 void PSYoungGen::compact() { 793 eden_mark_sweep()->compact(ZapUnusedHeapArea); 794 from_mark_sweep()->compact(ZapUnusedHeapArea); 795 // Mark sweep stores preserved markOops in to space, don't disturb! 796 to_mark_sweep()->compact(false); 797 } 798 799 void PSYoungGen::print() const { print_on(tty); } 800 void PSYoungGen::print_on(outputStream* st) const { 801 st->print(" %-15s", "PSYoungGen"); 802 if (PrintGCDetails && Verbose) { 803 st->print(" total " SIZE_FORMAT ", used " SIZE_FORMAT, 804 capacity_in_bytes(), used_in_bytes()); 805 } else { 806 st->print(" total " SIZE_FORMAT "K, used " SIZE_FORMAT "K", 807 capacity_in_bytes()/K, used_in_bytes()/K); 808 } 809 virtual_space()->print_space_boundaries_on(st); 810 st->print(" eden"); eden_space()->print_on(st); 811 st->print(" from"); from_space()->print_on(st); 812 st->print(" to "); to_space()->print_on(st); 813 } 814 815 // Note that a space is not printed before the [NAME: 816 void PSYoungGen::print_used_change(size_t prev_used) const { 817 gclog_or_tty->print("[%s:", name()); 818 gclog_or_tty->print(" " SIZE_FORMAT "K" 819 "->" SIZE_FORMAT "K" 820 "(" SIZE_FORMAT "K)", 821 prev_used / K, used_in_bytes() / K, 822 capacity_in_bytes() / K); 823 gclog_or_tty->print("]"); 824 } 825 826 size_t PSYoungGen::available_for_expansion() { 827 ShouldNotReachHere(); 828 return 0; 829 } 830 831 size_t PSYoungGen::available_for_contraction() { 832 ShouldNotReachHere(); 833 return 0; 834 } 835 836 size_t PSYoungGen::available_to_min_gen() { 837 assert(virtual_space()->committed_size() >= min_gen_size(), "Invariant"); 838 return virtual_space()->committed_size() - min_gen_size(); 839 } 840 841 // This method assumes that from-space has live data and that 842 // any shrinkage of the young gen is limited by location of 843 // from-space. 844 size_t PSYoungGen::available_to_live() { 845 size_t delta_in_survivor = 0; 846 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 847 const size_t space_alignment = heap->space_alignment(); 848 const size_t gen_alignment = heap->generation_alignment(); 849 850 MutableSpace* space_shrinking = NULL; 851 if (from_space()->end() > to_space()->end()) { 852 space_shrinking = from_space(); 853 } else { 854 space_shrinking = to_space(); 855 } 856 857 // Include any space that is committed but not included in 858 // the survivor spaces. 859 assert(((HeapWord*)virtual_space()->high()) >= space_shrinking->end(), 860 "Survivor space beyond high end"); 861 size_t unused_committed = pointer_delta(virtual_space()->high(), 862 space_shrinking->end(), sizeof(char)); 863 864 if (space_shrinking->is_empty()) { 865 // Don't let the space shrink to 0 866 assert(space_shrinking->capacity_in_bytes() >= space_alignment, 867 "Space is too small"); 868 delta_in_survivor = space_shrinking->capacity_in_bytes() - space_alignment; 869 } else { 870 delta_in_survivor = pointer_delta(space_shrinking->end(), 871 space_shrinking->top(), 872 sizeof(char)); 873 } 874 875 size_t delta_in_bytes = unused_committed + delta_in_survivor; 876 delta_in_bytes = align_size_down(delta_in_bytes, gen_alignment); 877 return delta_in_bytes; 878 } 879 880 // Return the number of bytes available for resizing down the young 881 // generation. This is the minimum of 882 // input "bytes" 883 // bytes to the minimum young gen size 884 // bytes to the size currently being used + some small extra 885 size_t PSYoungGen::limit_gen_shrink(size_t bytes) { 886 // Allow shrinkage into the current eden but keep eden large enough 887 // to maintain the minimum young gen size 888 bytes = MIN3(bytes, available_to_min_gen(), available_to_live()); 889 return align_size_down(bytes, virtual_space()->alignment()); 890 } 891 892 void PSYoungGen::reset_after_change() { 893 ShouldNotReachHere(); 894 } 895 896 void PSYoungGen::reset_survivors_after_shrink() { 897 _reserved = MemRegion((HeapWord*)virtual_space()->low_boundary(), 898 (HeapWord*)virtual_space()->high_boundary()); 899 PSScavenge::reference_processor()->set_span(_reserved); 900 901 MutableSpace* space_shrinking = NULL; 902 if (from_space()->end() > to_space()->end()) { 903 space_shrinking = from_space(); 904 } else { 905 space_shrinking = to_space(); 906 } 907 908 HeapWord* new_end = (HeapWord*)virtual_space()->high(); 909 assert(new_end >= space_shrinking->bottom(), "Shrink was too large"); 910 // Was there a shrink of the survivor space? 911 if (new_end < space_shrinking->end()) { 912 MemRegion mr(space_shrinking->bottom(), new_end); 913 space_shrinking->initialize(mr, 914 SpaceDecorator::DontClear, 915 SpaceDecorator::Mangle); 916 } 917 } 918 919 // This method currently does not expect to expand into eden (i.e., 920 // the virtual space boundaries is expected to be consistent 921 // with the eden boundaries.. 922 void PSYoungGen::post_resize() { 923 assert_locked_or_safepoint(Heap_lock); 924 assert((eden_space()->bottom() < to_space()->bottom()) && 925 (eden_space()->bottom() < from_space()->bottom()), 926 "Eden is assumed to be below the survivor spaces"); 927 928 MemRegion cmr((HeapWord*)virtual_space()->low(), 929 (HeapWord*)virtual_space()->high()); 930 Universe::heap()->barrier_set()->resize_covered_region(cmr); 931 space_invariants(); 932 } 933 934 935 936 void PSYoungGen::update_counters() { 937 if (UsePerfData) { 938 _eden_counters->update_all(); 939 _from_counters->update_all(); 940 _to_counters->update_all(); 941 _gen_counters->update_all(); 942 } 943 } 944 945 void PSYoungGen::verify() { 946 eden_space()->verify(); 947 from_space()->verify(); 948 to_space()->verify(); 949 } 950 951 #ifndef PRODUCT 952 void PSYoungGen::record_spaces_top() { 953 assert(ZapUnusedHeapArea, "Not mangling unused space"); 954 eden_space()->set_top_for_allocations(); 955 from_space()->set_top_for_allocations(); 956 to_space()->set_top_for_allocations(); 957 } 958 #endif