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