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 _max_eden_size = max_eden_size; 137 _max_survivor_size = max_survivor_size; 138 139 _eden_counters = new SpaceCounters("eden", 0, max_eden_size, _eden_space, 140 _gen_counters); 141 _from_counters = new SpaceCounters("s0", 1, max_survivor_size, _from_space, 142 _gen_counters); 143 _to_counters = new SpaceCounters("s1", 2, max_survivor_size, _to_space, 144 _gen_counters); 145 146 compute_initial_space_boundaries(); 147 } 148 149 void PSYoungGen::compute_initial_space_boundaries() { 150 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 151 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); 152 153 // Compute sizes 154 size_t alignment = heap->intra_heap_alignment(); 155 size_t size = virtual_space()->committed_size(); 156 157 size_t survivor_size = size / InitialSurvivorRatio; 158 survivor_size = align_size_down(survivor_size, alignment); 159 // ... but never less than an alignment 160 survivor_size = MAX2(survivor_size, alignment); 161 162 // Young generation is eden + 2 survivor spaces 163 size_t eden_size = size - (2 * survivor_size); 164 165 // Now go ahead and set 'em. 166 set_space_boundaries(eden_size, survivor_size); 167 space_invariants(); 168 169 _init_survivor_size = survivor_size; 170 _init_eden_size = eden_size; 171 172 if (UsePerfData) { 173 _eden_counters->update_capacity(); 174 _from_counters->update_capacity(); 175 _to_counters->update_capacity(); 176 } 177 } 178 179 void PSYoungGen::set_space_boundaries(size_t eden_size, size_t survivor_size) { 180 assert(eden_size < virtual_space()->committed_size(), "just checking"); 181 assert(eden_size > 0 && survivor_size > 0, "just checking"); 182 183 // Initial layout is Eden, to, from. After swapping survivor spaces, 184 // that leaves us with Eden, from, to, which is step one in our two 185 // step resize-with-live-data procedure. 186 char *eden_start = virtual_space()->low(); 187 char *to_start = eden_start + eden_size; 188 char *from_start = to_start + survivor_size; 189 char *from_end = from_start + survivor_size; 190 191 assert(from_end == virtual_space()->high(), "just checking"); 192 assert(is_object_aligned((intptr_t)eden_start), "checking alignment"); 193 assert(is_object_aligned((intptr_t)to_start), "checking alignment"); 194 assert(is_object_aligned((intptr_t)from_start), "checking alignment"); 195 196 MemRegion eden_mr((HeapWord*)eden_start, (HeapWord*)to_start); 197 MemRegion to_mr ((HeapWord*)to_start, (HeapWord*)from_start); 198 MemRegion from_mr((HeapWord*)from_start, (HeapWord*)from_end); 199 200 eden_space()->initialize(eden_mr, true, ZapUnusedHeapArea); 201 to_space()->initialize(to_mr , true, ZapUnusedHeapArea); 202 from_space()->initialize(from_mr, true, ZapUnusedHeapArea); 203 } 204 205 #ifndef PRODUCT 206 void PSYoungGen::space_invariants() { 207 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 208 const size_t alignment = heap->intra_heap_alignment(); 209 210 // Currently, our eden size cannot shrink to zero 211 guarantee(eden_space()->capacity_in_bytes() >= alignment, "eden too small"); 212 guarantee(from_space()->capacity_in_bytes() >= alignment, "from too small"); 213 guarantee(to_space()->capacity_in_bytes() >= alignment, "to too small"); 214 215 // Relationship of spaces to each other 216 char* eden_start = (char*)eden_space()->bottom(); 217 char* eden_end = (char*)eden_space()->end(); 218 char* from_start = (char*)from_space()->bottom(); 219 char* from_end = (char*)from_space()->end(); 220 char* to_start = (char*)to_space()->bottom(); 221 char* to_end = (char*)to_space()->end(); 222 223 guarantee(eden_start >= virtual_space()->low(), "eden bottom"); 224 guarantee(eden_start < eden_end, "eden space consistency"); 225 guarantee(from_start < from_end, "from space consistency"); 226 guarantee(to_start < to_end, "to space consistency"); 227 228 // Check whether from space is below to space 229 if (from_start < to_start) { 230 // Eden, from, to 231 guarantee(eden_end <= from_start, "eden/from boundary"); 232 guarantee(from_end <= to_start, "from/to boundary"); 233 guarantee(to_end <= virtual_space()->high(), "to end"); 234 } else { 235 // Eden, to, from 236 guarantee(eden_end <= to_start, "eden/to boundary"); 237 guarantee(to_end <= from_start, "to/from boundary"); 238 guarantee(from_end <= virtual_space()->high(), "from end"); 239 } 240 241 // More checks that the virtual space is consistent with the spaces 242 assert(virtual_space()->committed_size() >= 243 (eden_space()->capacity_in_bytes() + 244 to_space()->capacity_in_bytes() + 245 from_space()->capacity_in_bytes()), "Committed size is inconsistent"); 246 assert(virtual_space()->committed_size() <= virtual_space()->reserved_size(), 247 "Space invariant"); 248 char* eden_top = (char*)eden_space()->top(); 249 char* from_top = (char*)from_space()->top(); 250 char* to_top = (char*)to_space()->top(); 251 assert(eden_top <= virtual_space()->high(), "eden top"); 252 assert(from_top <= virtual_space()->high(), "from top"); 253 assert(to_top <= virtual_space()->high(), "to top"); 254 255 virtual_space()->verify(); 256 } 257 #endif 258 259 void PSYoungGen::resize(size_t eden_size, size_t survivor_size) { 260 // Resize the generation if needed. If the generation resize 261 // reports false, do not attempt to resize the spaces. 262 if (resize_generation(eden_size, survivor_size)) { 263 // Then we lay out the spaces inside the generation 264 resize_spaces(eden_size, survivor_size); 265 266 space_invariants(); 267 268 if (PrintAdaptiveSizePolicy && Verbose) { 269 gclog_or_tty->print_cr("Young generation size: " 270 "desired eden: " SIZE_FORMAT " survivor: " SIZE_FORMAT 271 " used: " SIZE_FORMAT " capacity: " SIZE_FORMAT 272 " gen limits: " SIZE_FORMAT " / " SIZE_FORMAT, 273 eden_size, survivor_size, used_in_bytes(), capacity_in_bytes(), 274 _max_gen_size, min_gen_size()); 275 } 276 } 277 } 278 279 280 bool PSYoungGen::resize_generation(size_t eden_size, size_t survivor_size) { 281 const size_t alignment = virtual_space()->alignment(); 282 size_t orig_size = virtual_space()->committed_size(); 283 bool size_changed = false; 284 285 // There used to be this guarantee there. 286 // guarantee ((eden_size + 2*survivor_size) <= _max_gen_size, "incorrect input arguments"); 287 // Code below forces this requirement. In addition the desired eden 288 // size and disired survivor sizes are desired goals and may 289 // exceed the total generation size. 290 291 assert(min_gen_size() <= orig_size && orig_size <= max_size(), "just checking"); 292 293 // Adjust new generation size 294 const size_t eden_plus_survivors = 295 align_size_up(eden_size + 2 * survivor_size, alignment); 296 size_t desired_size = MAX2(MIN2(eden_plus_survivors, max_size()), 297 min_gen_size()); 298 assert(desired_size <= max_size(), "just checking"); 299 300 if (desired_size > orig_size) { 301 // Grow the generation 302 size_t change = desired_size - orig_size; 303 assert(change % alignment == 0, "just checking"); 304 HeapWord* prev_high = (HeapWord*) virtual_space()->high(); 305 if (!virtual_space()->expand_by(change)) { 306 return false; // Error if we fail to resize! 307 } 308 if (ZapUnusedHeapArea) { 309 // Mangle newly committed space immediately because it 310 // can be done here more simply that after the new 311 // spaces have been computed. 312 HeapWord* new_high = (HeapWord*) virtual_space()->high(); 313 MemRegion mangle_region(prev_high, new_high); 314 SpaceMangler::mangle_region(mangle_region); 315 } 316 size_changed = true; 317 } else if (desired_size < orig_size) { 318 size_t desired_change = orig_size - desired_size; 319 assert(desired_change % alignment == 0, "just checking"); 320 321 desired_change = limit_gen_shrink(desired_change); 322 323 if (desired_change > 0) { 324 virtual_space()->shrink_by(desired_change); 325 reset_survivors_after_shrink(); 326 327 size_changed = true; 328 } 329 } else { 330 if (Verbose && PrintGC) { 331 if (orig_size == gen_size_limit()) { 332 gclog_or_tty->print_cr("PSYoung generation size at maximum: " 333 SIZE_FORMAT "K", orig_size/K); 334 } else if (orig_size == min_gen_size()) { 335 gclog_or_tty->print_cr("PSYoung generation size at minium: " 336 SIZE_FORMAT "K", orig_size/K); 337 } 338 } 339 } 340 341 if (size_changed) { 342 post_resize(); 343 344 if (Verbose && PrintGC) { 345 size_t current_size = virtual_space()->committed_size(); 346 gclog_or_tty->print_cr("PSYoung generation size changed: " 347 SIZE_FORMAT "K->" SIZE_FORMAT "K", 348 orig_size/K, current_size/K); 349 } 350 } 351 352 guarantee(eden_plus_survivors <= virtual_space()->committed_size() || 353 virtual_space()->committed_size() == max_size(), "Sanity"); 354 355 return true; 356 } 357 358 #ifndef PRODUCT 359 // In the numa case eden is not mangled so a survivor space 360 // moving into a region previously occupied by a survivor 361 // may find an unmangled region. Also in the PS case eden 362 // to-space and from-space may not touch (i.e., there may be 363 // gaps between them due to movement while resizing the 364 // spaces). Those gaps must be mangled. 365 void PSYoungGen::mangle_survivors(MutableSpace* s1, 366 MemRegion s1MR, 367 MutableSpace* s2, 368 MemRegion s2MR) { 369 // Check eden and gap between eden and from-space, in deciding 370 // what to mangle in from-space. Check the gap between from-space 371 // and to-space when deciding what to mangle. 372 // 373 // +--------+ +----+ +---+ 374 // | eden | |s1 | |s2 | 375 // +--------+ +----+ +---+ 376 // +-------+ +-----+ 377 // |s1MR | |s2MR | 378 // +-------+ +-----+ 379 // All of survivor-space is properly mangled so find the 380 // upper bound on the mangling for any portion above current s1. 381 HeapWord* delta_end = MIN2(s1->bottom(), s1MR.end()); 382 MemRegion delta1_left; 383 if (s1MR.start() < delta_end) { 384 delta1_left = MemRegion(s1MR.start(), delta_end); 385 s1->mangle_region(delta1_left); 386 } 387 // Find any portion to the right of the current s1. 388 HeapWord* delta_start = MAX2(s1->end(), s1MR.start()); 389 MemRegion delta1_right; 390 if (delta_start < s1MR.end()) { 391 delta1_right = MemRegion(delta_start, s1MR.end()); 392 s1->mangle_region(delta1_right); 393 } 394 395 // Similarly for the second survivor space except that 396 // any of the new region that overlaps with the current 397 // region of the first survivor space has already been 398 // mangled. 399 delta_end = MIN2(s2->bottom(), s2MR.end()); 400 delta_start = MAX2(s2MR.start(), s1->end()); 401 MemRegion delta2_left; 402 if (s2MR.start() < delta_end) { 403 delta2_left = MemRegion(s2MR.start(), delta_end); 404 s2->mangle_region(delta2_left); 405 } 406 delta_start = MAX2(s2->end(), s2MR.start()); 407 MemRegion delta2_right; 408 if (delta_start < s2MR.end()) { 409 s2->mangle_region(delta2_right); 410 } 411 412 if (TraceZapUnusedHeapArea) { 413 // s1 414 gclog_or_tty->print_cr("Current region: [" PTR_FORMAT ", " PTR_FORMAT ") " 415 "New region: [" PTR_FORMAT ", " PTR_FORMAT ")", 416 s1->bottom(), s1->end(), s1MR.start(), s1MR.end()); 417 gclog_or_tty->print_cr(" Mangle before: [" PTR_FORMAT ", " 418 PTR_FORMAT ") Mangle after: [" PTR_FORMAT ", " PTR_FORMAT ")", 419 delta1_left.start(), delta1_left.end(), delta1_right.start(), 420 delta1_right.end()); 421 422 // s2 423 gclog_or_tty->print_cr("Current region: [" PTR_FORMAT ", " PTR_FORMAT ") " 424 "New region: [" PTR_FORMAT ", " PTR_FORMAT ")", 425 s2->bottom(), s2->end(), s2MR.start(), s2MR.end()); 426 gclog_or_tty->print_cr(" Mangle before: [" PTR_FORMAT ", " 427 PTR_FORMAT ") Mangle after: [" PTR_FORMAT ", " PTR_FORMAT ")", 428 delta2_left.start(), delta2_left.end(), delta2_right.start(), 429 delta2_right.end()); 430 } 431 432 } 433 #endif // NOT PRODUCT 434 435 void PSYoungGen::resize_spaces(size_t requested_eden_size, 436 size_t requested_survivor_size) { 437 assert(UseAdaptiveSizePolicy, "sanity check"); 438 assert(requested_eden_size > 0 && requested_survivor_size > 0, 439 "just checking"); 440 441 // We require eden and to space to be empty 442 if ((!eden_space()->is_empty()) || (!to_space()->is_empty())) { 443 return; 444 } 445 446 if (PrintAdaptiveSizePolicy && Verbose) { 447 gclog_or_tty->print_cr("PSYoungGen::resize_spaces(requested_eden_size: " 448 SIZE_FORMAT 449 ", requested_survivor_size: " SIZE_FORMAT ")", 450 requested_eden_size, requested_survivor_size); 451 gclog_or_tty->print_cr(" eden: [" PTR_FORMAT ".." PTR_FORMAT ") " 452 SIZE_FORMAT, 453 eden_space()->bottom(), 454 eden_space()->end(), 455 pointer_delta(eden_space()->end(), 456 eden_space()->bottom(), 457 sizeof(char))); 458 gclog_or_tty->print_cr(" from: [" PTR_FORMAT ".." PTR_FORMAT ") " 459 SIZE_FORMAT, 460 from_space()->bottom(), 461 from_space()->end(), 462 pointer_delta(from_space()->end(), 463 from_space()->bottom(), 464 sizeof(char))); 465 gclog_or_tty->print_cr(" to: [" PTR_FORMAT ".." PTR_FORMAT ") " 466 SIZE_FORMAT, 467 to_space()->bottom(), 468 to_space()->end(), 469 pointer_delta( to_space()->end(), 470 to_space()->bottom(), 471 sizeof(char))); 472 } 473 474 // There's nothing to do if the new sizes are the same as the current 475 if (requested_survivor_size == to_space()->capacity_in_bytes() && 476 requested_survivor_size == from_space()->capacity_in_bytes() && 477 requested_eden_size == eden_space()->capacity_in_bytes()) { 478 if (PrintAdaptiveSizePolicy && Verbose) { 479 gclog_or_tty->print_cr(" capacities are the right sizes, returning"); 480 } 481 return; 482 } 483 484 char* eden_start = (char*)eden_space()->bottom(); 485 char* eden_end = (char*)eden_space()->end(); 486 char* from_start = (char*)from_space()->bottom(); 487 char* from_end = (char*)from_space()->end(); 488 char* to_start = (char*)to_space()->bottom(); 489 char* to_end = (char*)to_space()->end(); 490 491 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 492 const size_t alignment = heap->intra_heap_alignment(); 493 const bool maintain_minimum = 494 (requested_eden_size + 2 * requested_survivor_size) <= min_gen_size(); 495 496 bool eden_from_to_order = from_start < to_start; 497 // Check whether from space is below to space 498 if (eden_from_to_order) { 499 // Eden, from, to 500 eden_from_to_order = true; 501 if (PrintAdaptiveSizePolicy && Verbose) { 502 gclog_or_tty->print_cr(" Eden, from, to:"); 503 } 504 505 // Set eden 506 // "requested_eden_size" is a goal for the size of eden 507 // and may not be attainable. "eden_size" below is 508 // calculated based on the location of from-space and 509 // the goal for the size of eden. from-space is 510 // fixed in place because it contains live data. 511 // The calculation is done this way to avoid 32bit 512 // overflow (i.e., eden_start + requested_eden_size 513 // may too large for representation in 32bits). 514 size_t eden_size; 515 if (maintain_minimum) { 516 // Only make eden larger than the requested size if 517 // the minimum size of the generation has to be maintained. 518 // This could be done in general but policy at a higher 519 // level is determining a requested size for eden and that 520 // should be honored unless there is a fundamental reason. 521 eden_size = pointer_delta(from_start, 522 eden_start, 523 sizeof(char)); 524 } else { 525 eden_size = MIN2(requested_eden_size, 526 pointer_delta(from_start, eden_start, sizeof(char))); 527 } 528 529 eden_end = eden_start + eden_size; 530 assert(eden_end >= eden_start, "addition overflowed") 531 532 // To may resize into from space as long as it is clear of live data. 533 // From space must remain page aligned, though, so we need to do some 534 // extra calculations. 535 536 // First calculate an optimal to-space 537 to_end = (char*)virtual_space()->high(); 538 to_start = (char*)pointer_delta(to_end, (char*)requested_survivor_size, 539 sizeof(char)); 540 541 // Does the optimal to-space overlap from-space? 542 if (to_start < (char*)from_space()->end()) { 543 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); 544 545 // Calculate the minimum offset possible for from_end 546 size_t from_size = pointer_delta(from_space()->top(), from_start, sizeof(char)); 547 548 // Should we be in this method if from_space is empty? Why not the set_space method? FIX ME! 549 if (from_size == 0) { 550 from_size = alignment; 551 } else { 552 from_size = align_size_up(from_size, alignment); 553 } 554 555 from_end = from_start + from_size; 556 assert(from_end > from_start, "addition overflow or from_size problem"); 557 558 guarantee(from_end <= (char*)from_space()->end(), "from_end moved to the right"); 559 560 // Now update to_start with the new from_end 561 to_start = MAX2(from_end, to_start); 562 } 563 564 guarantee(to_start != to_end, "to space is zero sized"); 565 566 if (PrintAdaptiveSizePolicy && Verbose) { 567 gclog_or_tty->print_cr(" [eden_start .. eden_end): " 568 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 569 eden_start, 570 eden_end, 571 pointer_delta(eden_end, eden_start, sizeof(char))); 572 gclog_or_tty->print_cr(" [from_start .. from_end): " 573 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 574 from_start, 575 from_end, 576 pointer_delta(from_end, from_start, sizeof(char))); 577 gclog_or_tty->print_cr(" [ to_start .. to_end): " 578 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 579 to_start, 580 to_end, 581 pointer_delta( to_end, to_start, sizeof(char))); 582 } 583 } else { 584 // Eden, to, from 585 if (PrintAdaptiveSizePolicy && Verbose) { 586 gclog_or_tty->print_cr(" Eden, to, from:"); 587 } 588 589 // To space gets priority over eden resizing. Note that we position 590 // to space as if we were able to resize from space, even though from 591 // space is not modified. 592 // Giving eden priority was tried and gave poorer performance. 593 to_end = (char*)pointer_delta(virtual_space()->high(), 594 (char*)requested_survivor_size, 595 sizeof(char)); 596 to_end = MIN2(to_end, from_start); 597 to_start = (char*)pointer_delta(to_end, (char*)requested_survivor_size, 598 sizeof(char)); 599 // if the space sizes are to be increased by several times then 600 // 'to_start' will point beyond the young generation. In this case 601 // 'to_start' should be adjusted. 602 to_start = MAX2(to_start, eden_start + alignment); 603 604 // Compute how big eden can be, then adjust end. 605 // See comments above on calculating eden_end. 606 size_t eden_size; 607 if (maintain_minimum) { 608 eden_size = pointer_delta(to_start, eden_start, sizeof(char)); 609 } else { 610 eden_size = MIN2(requested_eden_size, 611 pointer_delta(to_start, eden_start, sizeof(char))); 612 } 613 eden_end = eden_start + eden_size; 614 assert(eden_end >= eden_start, "addition overflowed") 615 616 // Could choose to not let eden shrink 617 // to_start = MAX2(to_start, eden_end); 618 619 // Don't let eden shrink down to 0 or less. 620 eden_end = MAX2(eden_end, eden_start + alignment); 621 to_start = MAX2(to_start, eden_end); 622 623 if (PrintAdaptiveSizePolicy && Verbose) { 624 gclog_or_tty->print_cr(" [eden_start .. eden_end): " 625 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 626 eden_start, 627 eden_end, 628 pointer_delta(eden_end, eden_start, sizeof(char))); 629 gclog_or_tty->print_cr(" [ to_start .. to_end): " 630 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 631 to_start, 632 to_end, 633 pointer_delta( to_end, to_start, sizeof(char))); 634 gclog_or_tty->print_cr(" [from_start .. from_end): " 635 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 636 from_start, 637 from_end, 638 pointer_delta(from_end, from_start, sizeof(char))); 639 } 640 } 641 642 643 guarantee((HeapWord*)from_start <= from_space()->bottom(), 644 "from start moved to the right"); 645 guarantee((HeapWord*)from_end >= from_space()->top(), 646 "from end moved into live data"); 647 assert(is_object_aligned((intptr_t)eden_start), "checking alignment"); 648 assert(is_object_aligned((intptr_t)from_start), "checking alignment"); 649 assert(is_object_aligned((intptr_t)to_start), "checking alignment"); 650 651 MemRegion edenMR((HeapWord*)eden_start, (HeapWord*)eden_end); 652 MemRegion toMR ((HeapWord*)to_start, (HeapWord*)to_end); 653 MemRegion fromMR((HeapWord*)from_start, (HeapWord*)from_end); 654 655 // Let's make sure the call to initialize doesn't reset "top"! 656 HeapWord* old_from_top = from_space()->top(); 657 658 // For PrintAdaptiveSizePolicy block below 659 size_t old_from = from_space()->capacity_in_bytes(); 660 size_t old_to = to_space()->capacity_in_bytes(); 661 662 if (ZapUnusedHeapArea) { 663 // NUMA is a special case because a numa space is not mangled 664 // in order to not prematurely bind its address to memory to 665 // the wrong memory (i.e., don't want the GC thread to first 666 // touch the memory). The survivor spaces are not numa 667 // spaces and are mangled. 668 if (UseNUMA) { 669 if (eden_from_to_order) { 670 mangle_survivors(from_space(), fromMR, to_space(), toMR); 671 } else { 672 mangle_survivors(to_space(), toMR, from_space(), fromMR); 673 } 674 } 675 676 // If not mangling the spaces, do some checking to verify that 677 // the spaces are already mangled. 678 // The spaces should be correctly mangled at this point so 679 // do some checking here. Note that they are not being mangled 680 // in the calls to initialize(). 681 // Must check mangling before the spaces are reshaped. Otherwise, 682 // the bottom or end of one space may have moved into an area 683 // covered by another space and a failure of the check may 684 // not correctly indicate which space is not properly mangled. 685 HeapWord* limit = (HeapWord*) virtual_space()->high(); 686 eden_space()->check_mangled_unused_area(limit); 687 from_space()->check_mangled_unused_area(limit); 688 to_space()->check_mangled_unused_area(limit); 689 } 690 // When an existing space is being initialized, it is not 691 // mangled because the space has been previously mangled. 692 eden_space()->initialize(edenMR, 693 SpaceDecorator::Clear, 694 SpaceDecorator::DontMangle); 695 to_space()->initialize(toMR, 696 SpaceDecorator::Clear, 697 SpaceDecorator::DontMangle); 698 from_space()->initialize(fromMR, 699 SpaceDecorator::DontClear, 700 SpaceDecorator::DontMangle); 701 702 assert(from_space()->top() == old_from_top, "from top changed!"); 703 704 if (PrintAdaptiveSizePolicy) { 705 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 706 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); 707 708 gclog_or_tty->print("AdaptiveSizePolicy::survivor space sizes: " 709 "collection: %d " 710 "(" SIZE_FORMAT ", " SIZE_FORMAT ") -> " 711 "(" SIZE_FORMAT ", " SIZE_FORMAT ") ", 712 heap->total_collections(), 713 old_from, old_to, 714 from_space()->capacity_in_bytes(), 715 to_space()->capacity_in_bytes()); 716 gclog_or_tty->cr(); 717 } 718 } 719 720 void PSYoungGen::swap_spaces() { 721 MutableSpace* s = from_space(); 722 _from_space = to_space(); 723 _to_space = s; 724 725 // Now update the decorators. 726 PSMarkSweepDecorator* md = from_mark_sweep(); 727 _from_mark_sweep = to_mark_sweep(); 728 _to_mark_sweep = md; 729 730 assert(from_mark_sweep()->space() == from_space(), "Sanity"); 731 assert(to_mark_sweep()->space() == to_space(), "Sanity"); 732 } 733 734 size_t PSYoungGen::capacity_in_bytes() const { 735 return eden_space()->capacity_in_bytes() 736 + from_space()->capacity_in_bytes(); // to_space() is only used during scavenge 737 } 738 739 740 size_t PSYoungGen::used_in_bytes() const { 741 return eden_space()->used_in_bytes() 742 + from_space()->used_in_bytes(); // to_space() is only used during scavenge 743 } 744 745 746 size_t PSYoungGen::free_in_bytes() const { 747 return eden_space()->free_in_bytes() 748 + from_space()->free_in_bytes(); // to_space() is only used during scavenge 749 } 750 751 size_t PSYoungGen::capacity_in_words() const { 752 return eden_space()->capacity_in_words() 753 + from_space()->capacity_in_words(); // to_space() is only used during scavenge 754 } 755 756 757 size_t PSYoungGen::used_in_words() const { 758 return eden_space()->used_in_words() 759 + from_space()->used_in_words(); // to_space() is only used during scavenge 760 } 761 762 763 size_t PSYoungGen::free_in_words() const { 764 return eden_space()->free_in_words() 765 + from_space()->free_in_words(); // to_space() is only used during scavenge 766 } 767 768 void PSYoungGen::object_iterate(ObjectClosure* blk) { 769 eden_space()->object_iterate(blk); 770 from_space()->object_iterate(blk); 771 to_space()->object_iterate(blk); 772 } 773 774 void PSYoungGen::precompact() { 775 eden_mark_sweep()->precompact(); 776 from_mark_sweep()->precompact(); 777 to_mark_sweep()->precompact(); 778 } 779 780 void PSYoungGen::adjust_pointers() { 781 eden_mark_sweep()->adjust_pointers(); 782 from_mark_sweep()->adjust_pointers(); 783 to_mark_sweep()->adjust_pointers(); 784 } 785 786 void PSYoungGen::compact() { 787 eden_mark_sweep()->compact(ZapUnusedHeapArea); 788 from_mark_sweep()->compact(ZapUnusedHeapArea); 789 // Mark sweep stores preserved markOops in to space, don't disturb! 790 to_mark_sweep()->compact(false); 791 } 792 793 void PSYoungGen::move_and_update(ParCompactionManager* cm) { 794 PSParallelCompact::move_and_update(cm, PSParallelCompact::eden_space_id); 795 PSParallelCompact::move_and_update(cm, PSParallelCompact::from_space_id); 796 PSParallelCompact::move_and_update(cm, PSParallelCompact::to_space_id); 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 void PSYoungGen::print_used_change(size_t prev_used) const { 816 gclog_or_tty->print(" [%s:", name()); 817 gclog_or_tty->print(" " SIZE_FORMAT "K" 818 "->" SIZE_FORMAT "K" 819 "(" SIZE_FORMAT "K)", 820 prev_used / K, used_in_bytes() / K, 821 capacity_in_bytes() / K); 822 gclog_or_tty->print("]"); 823 } 824 825 size_t PSYoungGen::available_for_expansion() { 826 ShouldNotReachHere(); 827 return 0; 828 } 829 830 size_t PSYoungGen::available_for_contraction() { 831 ShouldNotReachHere(); 832 return 0; 833 } 834 835 size_t PSYoungGen::available_to_min_gen() { 836 assert(virtual_space()->committed_size() >= min_gen_size(), "Invariant"); 837 return virtual_space()->committed_size() - min_gen_size(); 838 } 839 840 // This method assumes that from-space has live data and that 841 // any shrinkage of the young gen is limited by location of 842 // from-space. 843 size_t PSYoungGen::available_to_live() { 844 size_t delta_in_survivor = 0; 845 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 846 const size_t space_alignment = heap->intra_heap_alignment(); 847 const size_t gen_alignment = heap->young_gen_alignment(); 848 849 MutableSpace* space_shrinking = NULL; 850 if (from_space()->end() > to_space()->end()) { 851 space_shrinking = from_space(); 852 } else { 853 space_shrinking = to_space(); 854 } 855 856 // Include any space that is committed but not included in 857 // the survivor spaces. 858 assert(((HeapWord*)virtual_space()->high()) >= space_shrinking->end(), 859 "Survivor space beyond high end"); 860 size_t unused_committed = pointer_delta(virtual_space()->high(), 861 space_shrinking->end(), sizeof(char)); 862 863 if (space_shrinking->is_empty()) { 864 // Don't let the space shrink to 0 865 assert(space_shrinking->capacity_in_bytes() >= space_alignment, 866 "Space is too small"); 867 delta_in_survivor = space_shrinking->capacity_in_bytes() - space_alignment; 868 } else { 869 delta_in_survivor = pointer_delta(space_shrinking->end(), 870 space_shrinking->top(), 871 sizeof(char)); 872 } 873 874 size_t delta_in_bytes = unused_committed + delta_in_survivor; 875 delta_in_bytes = align_size_down(delta_in_bytes, gen_alignment); 876 return delta_in_bytes; 877 } 878 879 // Return the number of bytes available for resizing down the young 880 // generation. This is the minimum of 881 // input "bytes" 882 // bytes to the minimum young gen size 883 // bytes to the size currently being used + some small extra 884 size_t PSYoungGen::limit_gen_shrink(size_t bytes) { 885 // Allow shrinkage into the current eden but keep eden large enough 886 // to maintain the minimum young gen size 887 bytes = MIN3(bytes, available_to_min_gen(), available_to_live()); 888 return align_size_down(bytes, virtual_space()->alignment()); 889 } 890 891 void PSYoungGen::reset_after_change() { 892 ShouldNotReachHere(); 893 } 894 895 void PSYoungGen::reset_survivors_after_shrink() { 896 _reserved = MemRegion((HeapWord*)virtual_space()->low_boundary(), 897 (HeapWord*)virtual_space()->high_boundary()); 898 PSScavenge::reference_processor()->set_span(_reserved); 899 900 MutableSpace* space_shrinking = NULL; 901 if (from_space()->end() > to_space()->end()) { 902 space_shrinking = from_space(); 903 } else { 904 space_shrinking = to_space(); 905 } 906 907 HeapWord* new_end = (HeapWord*)virtual_space()->high(); 908 assert(new_end >= space_shrinking->bottom(), "Shrink was too large"); 909 // Was there a shrink of the survivor space? 910 if (new_end < space_shrinking->end()) { 911 MemRegion mr(space_shrinking->bottom(), new_end); 912 space_shrinking->initialize(mr, 913 SpaceDecorator::DontClear, 914 SpaceDecorator::Mangle); 915 } 916 } 917 918 // This method currently does not expect to expand into eden (i.e., 919 // the virtual space boundaries is expected to be consistent 920 // with the eden boundaries.. 921 void PSYoungGen::post_resize() { 922 assert_locked_or_safepoint(Heap_lock); 923 assert((eden_space()->bottom() < to_space()->bottom()) && 924 (eden_space()->bottom() < from_space()->bottom()), 925 "Eden is assumed to be below the survivor spaces"); 926 927 MemRegion cmr((HeapWord*)virtual_space()->low(), 928 (HeapWord*)virtual_space()->high()); 929 Universe::heap()->barrier_set()->resize_covered_region(cmr); 930 space_invariants(); 931 } 932 933 934 935 void PSYoungGen::update_counters() { 936 if (UsePerfData) { 937 _eden_counters->update_all(); 938 _from_counters->update_all(); 939 _to_counters->update_all(); 940 _gen_counters->update_all(); 941 } 942 } 943 944 void PSYoungGen::verify(bool allow_dirty) { 945 eden_space()->verify(allow_dirty); 946 from_space()->verify(allow_dirty); 947 to_space()->verify(allow_dirty); 948 } 949 950 #ifndef PRODUCT 951 void PSYoungGen::record_spaces_top() { 952 assert(ZapUnusedHeapArea, "Not mangling unused space"); 953 eden_space()->set_top_for_allocations(); 954 from_space()->set_top_for_allocations(); 955 to_space()->set_top_for_allocations(); 956 } 957 #endif 958 959 void PSYoungGen::try_to_expand_by(size_t expand_bytes) { 960 size_t available_bytes = max_size() - capacity_in_bytes(); 961 if (available_bytes == 0) { 962 // cannot expand 963 return; 964 } 965 size_t eden_available_bytes = _max_eden_size - eden_space()->used_in_bytes(); 966 size_t survivor_available_bytes = _max_survivor_size - from_space()->used_in_bytes(); 967 size_t eden_expand_bytes = 968 (size_t) (expand_bytes * ((double) eden_available_bytes / available_bytes)); 969 size_t survivor_expand_bytes = 970 (size_t) (expand_bytes * ((double) survivor_available_bytes / available_bytes)); 971 972 // Don't expand unless it's significant 973 if (eden_expand_bytes < MinHeapDeltaBytes) { 974 eden_expand_bytes = 0; 975 } 976 if (survivor_expand_bytes < MinHeapDeltaBytes) { 977 survivor_expand_bytes = 0; 978 } 979 980 if (eden_expand_bytes == 0 && survivor_expand_bytes == 0) { 981 return; // no change, so return now 982 } 983 984 size_t eden_desired_capacity = 985 eden_space()->capacity_in_bytes() + eden_expand_bytes; 986 eden_desired_capacity = MIN2(eden_desired_capacity, _max_eden_size); 987 eden_desired_capacity = align_size_down(eden_desired_capacity, 988 virtual_space()->alignment()); 989 990 size_t survivor_desired_capacity = 991 from_space()->capacity_in_bytes() + survivor_expand_bytes; 992 survivor_desired_capacity = MIN2(survivor_desired_capacity, _max_survivor_size); 993 survivor_desired_capacity = align_size_down(survivor_desired_capacity, 994 virtual_space()->alignment()); 995 996 if (eden_desired_capacity == eden_space()->capacity_in_bytes() 997 && survivor_desired_capacity == from_space()->capacity_in_bytes()) { 998 return; // no change, so return now 999 } 1000 1001 if (PrintGC) { 1002 gclog_or_tty->print_cr(" Resizing young gen. expand_bytes=%d,%d", 1003 eden_expand_bytes, survivor_expand_bytes); 1004 gclog_or_tty->print("BEFORE: Young Gen: "); 1005 gclog_or_tty->print("eden capacity : " SIZE_FORMAT ", ", 1006 eden_space()->capacity_in_bytes()); 1007 gclog_or_tty->print("eden used : " SIZE_FORMAT ", " , 1008 eden_space()->used_in_bytes()); 1009 gclog_or_tty->print("survivor capacity : " SIZE_FORMAT ", ", 1010 from_space()->capacity_in_bytes()); 1011 gclog_or_tty->print_cr("survivor used : " SIZE_FORMAT ", " , 1012 from_space()->used_in_bytes()); 1013 } 1014 1015 resize(eden_desired_capacity, survivor_desired_capacity); 1016 1017 if (PrintGC) { 1018 gclog_or_tty->print("AFTER: Young Gen: "); 1019 gclog_or_tty->print("eden capacity : " SIZE_FORMAT ", ", 1020 eden_space()->capacity_in_bytes()); 1021 gclog_or_tty->print("eden used : " SIZE_FORMAT ", " , 1022 eden_space()->used_in_bytes()); 1023 gclog_or_tty->print("survivor capacity : " SIZE_FORMAT ", ", 1024 from_space()->capacity_in_bytes()); 1025 gclog_or_tty->print_cr("survivor used : " SIZE_FORMAT ", " , 1026 from_space()->used_in_bytes()); 1027 } 1028 } 1029 1030 void PSYoungGen::try_to_shrink_by(size_t shrink_bytes) { 1031 size_t free_bytes = capacity_in_bytes() - used_in_bytes(); 1032 if (free_bytes == 0) { 1033 // cannot shrink 1034 return; 1035 } 1036 size_t eden_free_bytes = eden_space()->capacity_in_bytes() - eden_space()->used_in_bytes(); 1037 size_t survivor_free_bytes = from_space()->capacity_in_bytes() - from_space()->used_in_bytes(); 1038 size_t eden_shrink_bytes = 1039 (size_t) (shrink_bytes * ((double) eden_free_bytes / free_bytes)); 1040 size_t survivor_shrink_bytes = 1041 (size_t) (shrink_bytes * ((double) survivor_free_bytes / free_bytes)); 1042 1043 // Don't shrink unless it's significant 1044 if (eden_shrink_bytes < MinHeapDeltaBytes) { 1045 eden_shrink_bytes = 0; 1046 } 1047 if (survivor_shrink_bytes < MinHeapDeltaBytes) { 1048 survivor_shrink_bytes = 0; 1049 } 1050 1051 if (eden_shrink_bytes == 0 && survivor_shrink_bytes == 0) { 1052 return; // no change, so return now 1053 } 1054 1055 size_t eden_desired_capacity = 1056 eden_space()->capacity_in_bytes() - eden_shrink_bytes; 1057 eden_desired_capacity = MAX2(eden_desired_capacity, _init_eden_size); 1058 assert(eden_space()->used_in_bytes() <= eden_desired_capacity, "sanity check"); 1059 eden_desired_capacity = align_size_down(eden_desired_capacity, 1060 virtual_space()->alignment()); 1061 1062 size_t survivor_desired_capacity = 1063 from_space()->capacity_in_bytes() - survivor_shrink_bytes; 1064 survivor_desired_capacity = MAX2(survivor_desired_capacity, _init_survivor_size); 1065 assert(from_space()->used_in_bytes() <= survivor_desired_capacity, "sanity check"); 1066 survivor_desired_capacity = align_size_down(survivor_desired_capacity, 1067 virtual_space()->alignment()); 1068 1069 if (eden_desired_capacity == eden_space()->capacity_in_bytes() 1070 && survivor_desired_capacity == from_space()->capacity_in_bytes()) { 1071 return; // no change, so return now 1072 } 1073 1074 if (PrintGC) { 1075 gclog_or_tty->print_cr(" Resizing young gen. shrink_bytes=%d,%d", 1076 eden_shrink_bytes, survivor_shrink_bytes); 1077 gclog_or_tty->print("BEFORE: Young Gen: "); 1078 gclog_or_tty->print("eden capacity : " SIZE_FORMAT ", ", 1079 eden_space()->capacity_in_bytes()); 1080 gclog_or_tty->print("eden used : " SIZE_FORMAT ", " , 1081 eden_space()->used_in_bytes()); 1082 gclog_or_tty->print("survivor capacity : " SIZE_FORMAT ", ", 1083 from_space()->capacity_in_bytes()); 1084 gclog_or_tty->print_cr("survivor used : " SIZE_FORMAT ", " , 1085 from_space()->used_in_bytes()); 1086 } 1087 1088 resize(eden_desired_capacity, survivor_desired_capacity); 1089 1090 if (PrintGC) { 1091 gclog_or_tty->print("AFTER: Young Gen: "); 1092 gclog_or_tty->print("eden capacity : " SIZE_FORMAT ", ", 1093 eden_space()->capacity_in_bytes()); 1094 gclog_or_tty->print("eden used : " SIZE_FORMAT ", " , 1095 eden_space()->used_in_bytes()); 1096 gclog_or_tty->print("survivor capacity : " SIZE_FORMAT ", ", 1097 from_space()->capacity_in_bytes()); 1098 gclog_or_tty->print_cr("survivor used : " SIZE_FORMAT ", " , 1099 from_space()->used_in_bytes()); 1100 } 1101 }