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