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