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