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