1 #ifdef USE_PRAGMA_IDENT_SRC
   2 #pragma ident "@(#)psYoungGen.cpp       1.68 07/10/04 10:49:36 JVM"
   3 #endif
   4 /*
   5  * Copyright 2001-2007 Sun Microsystems, Inc.  All Rights Reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23  * CA 95054 USA or visit www.sun.com if you need additional information or
  24  * have any questions.
  25  *  
  26  */
  27 
  28 # include "incls/_precompiled.incl"
  29 # include "incls/_psYoungGen.cpp.incl"
  30 
  31 PSYoungGen::PSYoungGen(size_t        initial_size,
  32                        size_t        min_size, 
  33                        size_t        max_size) :
  34   _init_gen_size(initial_size), 
  35   _min_gen_size(min_size),
  36   _max_gen_size(max_size)
  37 {}
  38 
  39 void PSYoungGen::initialize_virtual_space(ReservedSpace rs, size_t alignment) {
  40   assert(_init_gen_size != 0, "Should have a finite size");
  41   _virtual_space = new PSVirtualSpace(rs, alignment);
  42   if (!_virtual_space->expand_by(_init_gen_size)) {
  43     vm_exit_during_initialization("Could not reserve enough space for "
  44                                   "object heap");
  45   }
  46 }
  47 
  48 void PSYoungGen::initialize(ReservedSpace rs, size_t alignment) {
  49   initialize_virtual_space(rs, alignment);
  50   initialize_work();
  51 }
  52 
  53 void PSYoungGen::initialize_work() {
  54 
  55   _reserved = MemRegion((HeapWord*)_virtual_space->low_boundary(),
  56                         (HeapWord*)_virtual_space->high_boundary());
  57 
  58   MemRegion cmr((HeapWord*)_virtual_space->low(),
  59                 (HeapWord*)_virtual_space->high());
  60   Universe::heap()->barrier_set()->resize_covered_region(cmr);
  61 
  62   if (UseNUMA) {
  63     _eden_space = new MutableNUMASpace();
  64   } else {
  65     _eden_space = new MutableSpace();
  66   }
  67   _from_space = new MutableSpace();
  68   _to_space   = new MutableSpace();
  69 
  70   if (_eden_space == NULL || _from_space == NULL || _to_space == NULL) {
  71     vm_exit_during_initialization("Could not allocate a young gen space");
  72   }
  73 
  74   // Allocate the mark sweep views of spaces
  75   _eden_mark_sweep =
  76       new PSMarkSweepDecorator(_eden_space, NULL, MarkSweepDeadRatio);
  77   _from_mark_sweep =
  78       new PSMarkSweepDecorator(_from_space, NULL, MarkSweepDeadRatio);
  79   _to_mark_sweep =
  80       new PSMarkSweepDecorator(_to_space, NULL, MarkSweepDeadRatio);
  81 
  82   if (_eden_mark_sweep == NULL ||
  83       _from_mark_sweep == NULL ||
  84       _to_mark_sweep == NULL) {
  85     vm_exit_during_initialization("Could not complete allocation"
  86                                   " of the young generation");
  87   }
  88 
  89   // Generation Counters - generation 0, 3 subspaces
  90   _gen_counters = new PSGenerationCounters("new", 0, 3, _virtual_space);
  91 
  92   // Compute maximum space sizes for performance counters
  93   ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
  94   size_t alignment = heap->intra_generation_alignment();
  95   size_t size = _virtual_space->reserved_size();
  96 
  97   size_t max_survivor_size;
  98   size_t max_eden_size;
  99 
 100   if (UseAdaptiveSizePolicy) {
 101     max_survivor_size = size / MinSurvivorRatio;
 102 
 103     // round the survivor space size down to the nearest alignment
 104     // and make sure its size is greater than 0.
 105     max_survivor_size = align_size_down(max_survivor_size, alignment);
 106     max_survivor_size = MAX2(max_survivor_size, alignment);
 107 
 108     // set the maximum size of eden to be the size of the young gen
 109     // less two times the minimum survivor size. The minimum survivor
 110     // size for UseAdaptiveSizePolicy is one alignment.
 111     max_eden_size = size - 2 * alignment;
 112   } else {
 113     max_survivor_size = size / InitialSurvivorRatio;
 114 
 115     // round the survivor space size down to the nearest alignment
 116     // and make sure its size is greater than 0.
 117     max_survivor_size = align_size_down(max_survivor_size, alignment);
 118     max_survivor_size = MAX2(max_survivor_size, alignment);
 119 
 120     // set the maximum size of eden to be the size of the young gen
 121     // less two times the survivor size when the generation is 100%
 122     // committed. The minimum survivor size for -UseAdaptiveSizePolicy
 123     // is dependent on the committed portion (current capacity) of the
 124     // generation - the less space committed, the smaller the survivor
 125     // space, possibly as small as an alignment. However, we are interested
 126     // in the case where the young generation is 100% committed, as this
 127     // is the point where eden reachs its maximum size. At this point,
 128     // the size of a survivor space is max_survivor_size.
 129     max_eden_size = size - 2 * max_survivor_size;
 130   }
 131 
 132   _eden_counters = new SpaceCounters("eden", 0, max_eden_size, _eden_space,
 133                                      _gen_counters);
 134   _from_counters = new SpaceCounters("s0", 1, max_survivor_size, _from_space,
 135                                      _gen_counters);
 136   _to_counters = new SpaceCounters("s1", 2, max_survivor_size, _to_space,
 137                                    _gen_counters);
 138 
 139   compute_initial_space_boundaries();
 140 }
 141 
 142 void PSYoungGen::compute_initial_space_boundaries() {
 143   ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
 144   assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
 145 
 146   // Compute sizes
 147   size_t alignment = heap->intra_generation_alignment();
 148   size_t size = _virtual_space->committed_size();
 149 
 150   size_t survivor_size = size / InitialSurvivorRatio;
 151   survivor_size = align_size_down(survivor_size, alignment);
 152   // ... but never less than an alignment
 153   survivor_size = MAX2(survivor_size, alignment);
 154 
 155   // Young generation is eden + 2 survivor spaces
 156   size_t eden_size = size - (2 * survivor_size);
 157 
 158   // Now go ahead and set 'em.
 159   set_space_boundaries(eden_size, survivor_size);
 160   space_invariants();
 161 
 162   if (UsePerfData) {
 163     _eden_counters->update_capacity();
 164     _from_counters->update_capacity();
 165     _to_counters->update_capacity();
 166   }
 167 }
 168 
 169 void PSYoungGen::set_space_boundaries(size_t eden_size, size_t survivor_size) {
 170   assert(eden_size < _virtual_space->committed_size(), "just checking");
 171   assert(eden_size > 0  && survivor_size > 0, "just checking");
 172 
 173   // Initial layout is Eden, to, from. After swapping survivor spaces,
 174   // that leaves us with Eden, from, to, which is step one in our two
 175   // step resize-with-live-data procedure.
 176   char *eden_start = _virtual_space->low();
 177   char *to_start   = eden_start + eden_size;
 178   char *from_start = to_start   + survivor_size;
 179   char *from_end   = from_start + survivor_size;
 180 
 181   assert(from_end == _virtual_space->high(), "just checking");
 182   assert(is_object_aligned((intptr_t)eden_start), "checking alignment");
 183   assert(is_object_aligned((intptr_t)to_start),   "checking alignment");
 184   assert(is_object_aligned((intptr_t)from_start), "checking alignment");
 185 
 186   MemRegion eden_mr((HeapWord*)eden_start, (HeapWord*)to_start);
 187   MemRegion to_mr  ((HeapWord*)to_start, (HeapWord*)from_start);
 188   MemRegion from_mr((HeapWord*)from_start, (HeapWord*)from_end);
 189 
 190   eden_space()->initialize(eden_mr, true);
 191     to_space()->initialize(to_mr  , true);
 192   from_space()->initialize(from_mr, true);
 193 }
 194 
 195 #ifndef PRODUCT
 196 void PSYoungGen::space_invariants() {
 197   ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
 198   const size_t alignment = heap->intra_generation_alignment();
 199 
 200   // Currently, our eden size cannot shrink to zero
 201   guarantee(eden_space()->capacity_in_bytes() >= alignment, "eden too small");
 202   guarantee(from_space()->capacity_in_bytes() >= alignment, "from too small");
 203   guarantee(to_space()->capacity_in_bytes() >= alignment, "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     if (PrintAdaptiveSizePolicy && Verbose) {
 259       gclog_or_tty->print_cr("Young generation size: "
 260         "desired eden: " SIZE_FORMAT " survivor: " SIZE_FORMAT
 261         " used: " SIZE_FORMAT " capacity: " SIZE_FORMAT
 262         " gen limits: " SIZE_FORMAT " / " SIZE_FORMAT,
 263         eden_size, survivor_size, used_in_bytes(), capacity_in_bytes(),
 264         _max_gen_size, min_gen_size());
 265     }
 266   }
 267 }
 268 
 269 
 270 bool PSYoungGen::resize_generation(size_t eden_size, size_t survivor_size) {
 271   const size_t alignment = _virtual_space->alignment();
 272   size_t orig_size = _virtual_space->committed_size();
 273   bool size_changed = false;
 274 
 275   // There used to be this guarantee there.
 276   // guarantee ((eden_size + 2*survivor_size)  <= _max_gen_size, "incorrect input arguments");
 277   // Code below forces this requirement.  In addition the desired eden
 278   // size and disired survivor sizes are desired goals and may
 279   // exceed the total generation size.
 280 
 281   assert(min_gen_size() <= orig_size && orig_size <= max_size(), "just checking");
 282 
 283   // Adjust new generation size
 284   const size_t eden_plus_survivors =
 285           align_size_up(eden_size + 2 * survivor_size, alignment);
 286   size_t desired_size = MAX2(MIN2(eden_plus_survivors, max_size()), 
 287                              min_gen_size());
 288   assert(desired_size <= max_size(), "just checking");
 289 
 290   if (desired_size > orig_size) {
 291     // Grow the generation
 292     size_t change = desired_size - orig_size;
 293     assert(change % alignment == 0, "just checking");
 294     if (!_virtual_space->expand_by(change)) {
 295       return false; // Error if we fail to resize!
 296     }
 297 
 298     size_changed = true;
 299   } else if (desired_size < orig_size) {
 300     size_t desired_change = orig_size - desired_size;
 301     assert(desired_change % alignment == 0, "just checking");
 302 
 303     desired_change = limit_gen_shrink(desired_change);
 304 
 305     if (desired_change > 0) {
 306       virtual_space()->shrink_by(desired_change);
 307       reset_survivors_after_shrink();
 308 
 309       size_changed = true;
 310     }
 311   } else {
 312     if (Verbose && PrintGC) {
 313       if (orig_size == gen_size_limit()) {
 314         gclog_or_tty->print_cr("PSYoung generation size at maximum: "
 315           SIZE_FORMAT "K", orig_size/K);
 316       } else if (orig_size == min_gen_size()) {
 317         gclog_or_tty->print_cr("PSYoung generation size at minium: "
 318           SIZE_FORMAT "K", orig_size/K);
 319       }
 320     }
 321   }
 322 
 323   if (size_changed) {
 324     post_resize();
 325 
 326     if (Verbose && PrintGC) {
 327       size_t current_size  = _virtual_space->committed_size();
 328       gclog_or_tty->print_cr("PSYoung generation size changed: "
 329                              SIZE_FORMAT "K->" SIZE_FORMAT "K",
 330                              orig_size/K, current_size/K);
 331     }
 332   }
 333 
 334   guarantee(eden_plus_survivors <= _virtual_space->committed_size() ||
 335             _virtual_space->committed_size() == max_size(), "Sanity");
 336 
 337   return true;
 338 }
 339 
 340 
 341 void PSYoungGen::resize_spaces(size_t requested_eden_size, 
 342                                size_t requested_survivor_size) {
 343   assert(UseAdaptiveSizePolicy, "sanity check");
 344   assert(requested_eden_size > 0  && requested_survivor_size > 0, 
 345          "just checking");
 346 
 347   // We require eden and to space to be empty
 348   if ((!eden_space()->is_empty()) || (!to_space()->is_empty())) {
 349     return;
 350   }
 351 
 352   if (PrintAdaptiveSizePolicy && Verbose) {
 353     gclog_or_tty->print_cr("PSYoungGen::resize_spaces(requested_eden_size: " 
 354                   SIZE_FORMAT 
 355                   ", requested_survivor_size: " SIZE_FORMAT ")",
 356                   requested_eden_size, requested_survivor_size);
 357     gclog_or_tty->print_cr("    eden: [" PTR_FORMAT ".." PTR_FORMAT ") " 
 358                   SIZE_FORMAT, 
 359                   eden_space()->bottom(), 
 360                   eden_space()->end(), 
 361                   pointer_delta(eden_space()->end(),
 362                                 eden_space()->bottom(),
 363                                 sizeof(char)));
 364     gclog_or_tty->print_cr("    from: [" PTR_FORMAT ".." PTR_FORMAT ") " 
 365                   SIZE_FORMAT, 
 366                   from_space()->bottom(), 
 367                   from_space()->end(), 
 368                   pointer_delta(from_space()->end(),
 369                                 from_space()->bottom(),
 370                                 sizeof(char)));
 371     gclog_or_tty->print_cr("      to: [" PTR_FORMAT ".." PTR_FORMAT ") " 
 372                   SIZE_FORMAT, 
 373                   to_space()->bottom(),   
 374                   to_space()->end(), 
 375                   pointer_delta(  to_space()->end(),
 376                                   to_space()->bottom(),
 377                                   sizeof(char)));
 378   }
 379 
 380   // There's nothing to do if the new sizes are the same as the current
 381   if (requested_survivor_size == to_space()->capacity_in_bytes() && 
 382       requested_survivor_size == from_space()->capacity_in_bytes() &&
 383       requested_eden_size == eden_space()->capacity_in_bytes()) {
 384     if (PrintAdaptiveSizePolicy && Verbose) {
 385       gclog_or_tty->print_cr("    capacities are the right sizes, returning");
 386     }
 387     return;
 388   }
 389   
 390   char* eden_start = (char*)eden_space()->bottom();
 391   char* eden_end   = (char*)eden_space()->end();   
 392   char* from_start = (char*)from_space()->bottom();
 393   char* from_end   = (char*)from_space()->end();
 394   char* to_start   = (char*)to_space()->bottom();
 395   char* to_end     = (char*)to_space()->end();
 396 
 397   ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
 398   const size_t alignment = heap->intra_generation_alignment();
 399   const bool maintain_minimum = 
 400     (requested_eden_size + 2 * requested_survivor_size) <= min_gen_size();
 401 
 402   // Check whether from space is below to space
 403   if (from_start < to_start) {
 404     // Eden, from, to
 405     if (PrintAdaptiveSizePolicy && Verbose) {
 406       gclog_or_tty->print_cr("  Eden, from, to:");
 407     }
 408 
 409     // Set eden
 410     // "requested_eden_size" is a goal for the size of eden
 411     // and may not be attainable.  "eden_size" below is
 412     // calculated based on the location of from-space and
 413     // the goal for the size of eden.  from-space is
 414     // fixed in place because it contains live data.
 415     // The calculation is done this way to avoid 32bit
 416     // overflow (i.e., eden_start + requested_eden_size
 417     // may too large for representation in 32bits).
 418     size_t eden_size;
 419     if (maintain_minimum) {
 420       // Only make eden larger than the requested size if
 421       // the minimum size of the generation has to be maintained.
 422       // This could be done in general but policy at a higher
 423       // level is determining a requested size for eden and that
 424       // should be honored unless there is a fundamental reason.
 425       eden_size = pointer_delta(from_start, 
 426                                 eden_start, 
 427                                 sizeof(char));
 428     } else {
 429       eden_size = MIN2(requested_eden_size,
 430                        pointer_delta(from_start, eden_start, sizeof(char)));
 431     }
 432 
 433     eden_end = eden_start + eden_size;
 434     assert(eden_end >= eden_start, "addition overflowed")
 435 
 436     // To may resize into from space as long as it is clear of live data.
 437     // From space must remain page aligned, though, so we need to do some
 438     // extra calculations.
 439 
 440     // First calculate an optimal to-space
 441     to_end   = (char*)_virtual_space->high();
 442     to_start = (char*)pointer_delta(to_end, (char*)requested_survivor_size, 
 443                                     sizeof(char));
 444 
 445     // Does the optimal to-space overlap from-space?
 446     if (to_start < (char*)from_space()->end()) {
 447       assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
 448       
 449       // Calculate the minimum offset possible for from_end
 450       size_t from_size = pointer_delta(from_space()->top(), from_start, sizeof(char));
 451 
 452       // Should we be in this method if from_space is empty? Why not the set_space method? FIX ME!
 453       if (from_size == 0) {
 454         from_size = alignment;
 455       } else {
 456         from_size = align_size_up(from_size, alignment);
 457       }
 458 
 459       from_end = from_start + from_size;
 460       assert(from_end > from_start, "addition overflow or from_size problem");
 461 
 462       guarantee(from_end <= (char*)from_space()->end(), "from_end moved to the right");
 463 
 464       // Now update to_start with the new from_end
 465       to_start = MAX2(from_end, to_start);
 466     }
 467 
 468     guarantee(to_start != to_end, "to space is zero sized");
 469       
 470     if (PrintAdaptiveSizePolicy && Verbose) {
 471       gclog_or_tty->print_cr("    [eden_start .. eden_end): "
 472                     "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 
 473                     eden_start, 
 474                     eden_end, 
 475                     pointer_delta(eden_end, eden_start, sizeof(char)));
 476       gclog_or_tty->print_cr("    [from_start .. from_end): "
 477                     "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 
 478                     from_start, 
 479                     from_end, 
 480                     pointer_delta(from_end, from_start, sizeof(char)));
 481       gclog_or_tty->print_cr("    [  to_start ..   to_end): "
 482                     "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 
 483                     to_start,   
 484                     to_end, 
 485                     pointer_delta(  to_end,   to_start, sizeof(char)));
 486     }
 487   } else {
 488     // Eden, to, from
 489     if (PrintAdaptiveSizePolicy && Verbose) {
 490       gclog_or_tty->print_cr("  Eden, to, from:");
 491     }
 492 
 493     // To space gets priority over eden resizing. Note that we position
 494     // to space as if we were able to resize from space, even though from
 495     // space is not modified.
 496     // Giving eden priority was tried and gave poorer performance.
 497     to_end   = (char*)pointer_delta(_virtual_space->high(), 
 498                                     (char*)requested_survivor_size, 
 499                                     sizeof(char));
 500     to_end   = MIN2(to_end, from_start);
 501     to_start = (char*)pointer_delta(to_end, (char*)requested_survivor_size, 
 502                                     sizeof(char));
 503     // if the space sizes are to be increased by several times then
 504     // 'to_start' will point beyond the young generation. In this case
 505     // 'to_start' should be adjusted.
 506     to_start = MAX2(to_start, eden_start + alignment);
 507     
 508     // Compute how big eden can be, then adjust end.
 509     // See  comments above on calculating eden_end.
 510     size_t eden_size;
 511     if (maintain_minimum) {
 512       eden_size = pointer_delta(to_start, eden_start, sizeof(char));
 513     } else {
 514       eden_size = MIN2(requested_eden_size,
 515                        pointer_delta(to_start, eden_start, sizeof(char)));
 516     }
 517     eden_end = eden_start + eden_size;
 518     assert(eden_end >= eden_start, "addition overflowed")
 519 
 520     // Could choose to not let eden shrink
 521     // to_start = MAX2(to_start, eden_end);
 522 
 523     // Don't let eden shrink down to 0 or less.
 524     eden_end = MAX2(eden_end, eden_start + alignment);
 525     to_start = MAX2(to_start, eden_end);
 526 
 527     if (PrintAdaptiveSizePolicy && Verbose) {
 528       gclog_or_tty->print_cr("    [eden_start .. eden_end): "
 529                     "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 
 530                     eden_start, 
 531                     eden_end, 
 532                     pointer_delta(eden_end, eden_start, sizeof(char)));
 533       gclog_or_tty->print_cr("    [  to_start ..   to_end): " 
 534                     "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 
 535                     to_start,   
 536                     to_end, 
 537                     pointer_delta(  to_end,   to_start, sizeof(char)));
 538       gclog_or_tty->print_cr("    [from_start .. from_end): " 
 539                     "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 
 540                     from_start, 
 541                     from_end, 
 542                     pointer_delta(from_end, from_start, sizeof(char)));
 543     }
 544   }
 545   
 546 
 547   guarantee((HeapWord*)from_start <= from_space()->bottom(), 
 548             "from start moved to the right");
 549   guarantee((HeapWord*)from_end >= from_space()->top(),
 550             "from end moved into live data");
 551   assert(is_object_aligned((intptr_t)eden_start), "checking alignment");
 552   assert(is_object_aligned((intptr_t)from_start), "checking alignment");
 553   assert(is_object_aligned((intptr_t)to_start), "checking alignment");
 554 
 555   MemRegion edenMR((HeapWord*)eden_start, (HeapWord*)eden_end);
 556   MemRegion toMR  ((HeapWord*)to_start,   (HeapWord*)to_end);
 557   MemRegion fromMR((HeapWord*)from_start, (HeapWord*)from_end);
 558 
 559   // Let's make sure the call to initialize doesn't reset "top"!
 560   HeapWord* old_from_top = from_space()->top();
 561 
 562   // For PrintAdaptiveSizePolicy block  below
 563   size_t old_from = from_space()->capacity_in_bytes();
 564   size_t old_to   = to_space()->capacity_in_bytes();
 565 
 566   eden_space()->initialize(edenMR, true);
 567     to_space()->initialize(toMR  , true);
 568   from_space()->initialize(fromMR, false);     // Note, not cleared!
 569 
 570   assert(from_space()->top() == old_from_top, "from top changed!");
 571 
 572   if (PrintAdaptiveSizePolicy) {
 573     ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
 574     assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
 575 
 576     gclog_or_tty->print("AdaptiveSizePolicy::survivor space sizes: "
 577                   "collection: %d "
 578                   "(" SIZE_FORMAT ", " SIZE_FORMAT ") -> "
 579                   "(" SIZE_FORMAT ", " SIZE_FORMAT ") ",
 580                   heap->total_collections(),
 581                   old_from, old_to,
 582                   from_space()->capacity_in_bytes(),
 583                   to_space()->capacity_in_bytes());
 584     gclog_or_tty->cr();
 585   }
 586 }
 587 
 588 void PSYoungGen::swap_spaces() {
 589   MutableSpace* s    = from_space();
 590   _from_space        = to_space();
 591   _to_space          = s;
 592 
 593   // Now update the decorators.
 594   PSMarkSweepDecorator* md = from_mark_sweep();
 595   _from_mark_sweep           = to_mark_sweep();
 596   _to_mark_sweep             = md;
 597 
 598   assert(from_mark_sweep()->space() == from_space(), "Sanity");
 599   assert(to_mark_sweep()->space() == to_space(), "Sanity");
 600 }
 601 
 602 size_t PSYoungGen::capacity_in_bytes() const {
 603   return eden_space()->capacity_in_bytes()
 604        + from_space()->capacity_in_bytes();  // to_space() is only used during scavenge
 605 }
 606 
 607 
 608 size_t PSYoungGen::used_in_bytes() const {
 609   return eden_space()->used_in_bytes()
 610        + from_space()->used_in_bytes();      // to_space() is only used during scavenge
 611 }
 612 
 613 
 614 size_t PSYoungGen::free_in_bytes() const {
 615   return eden_space()->free_in_bytes()
 616        + from_space()->free_in_bytes();      // to_space() is only used during scavenge
 617 }
 618 
 619 size_t PSYoungGen::capacity_in_words() const {
 620   return eden_space()->capacity_in_words()
 621        + from_space()->capacity_in_words();  // to_space() is only used during scavenge
 622 }
 623 
 624 
 625 size_t PSYoungGen::used_in_words() const {
 626   return eden_space()->used_in_words()
 627        + from_space()->used_in_words();      // to_space() is only used during scavenge
 628 }
 629 
 630 
 631 size_t PSYoungGen::free_in_words() const {
 632   return eden_space()->free_in_words()
 633        + from_space()->free_in_words();      // to_space() is only used during scavenge
 634 }
 635 
 636 void PSYoungGen::object_iterate(ObjectClosure* blk) {
 637   eden_space()->object_iterate(blk);
 638   from_space()->object_iterate(blk);
 639   to_space()->object_iterate(blk);
 640 }
 641 
 642 void PSYoungGen::precompact() {
 643   eden_mark_sweep()->precompact();
 644   from_mark_sweep()->precompact();
 645   to_mark_sweep()->precompact();
 646 }
 647 
 648 void PSYoungGen::adjust_pointers() {
 649   eden_mark_sweep()->adjust_pointers();
 650   from_mark_sweep()->adjust_pointers();
 651   to_mark_sweep()->adjust_pointers();
 652 }
 653 
 654 void PSYoungGen::compact() {
 655   eden_mark_sweep()->compact(ZapUnusedHeapArea);
 656   from_mark_sweep()->compact(ZapUnusedHeapArea);
 657   // Mark sweep stores preserved markOops in to space, don't disturb!
 658   to_mark_sweep()->compact(false);
 659 }
 660 
 661 void PSYoungGen::move_and_update(ParCompactionManager* cm) {
 662   PSParallelCompact::move_and_update(cm, PSParallelCompact::eden_space_id);
 663   PSParallelCompact::move_and_update(cm, PSParallelCompact::from_space_id);
 664   PSParallelCompact::move_and_update(cm, PSParallelCompact::to_space_id);
 665 }
 666 
 667 void PSYoungGen::print() const { print_on(tty); }
 668 void PSYoungGen::print_on(outputStream* st) const {
 669   st->print(" %-15s", "PSYoungGen");
 670   if (PrintGCDetails && Verbose) {
 671     st->print(" total " SIZE_FORMAT ", used " SIZE_FORMAT,
 672                capacity_in_bytes(), used_in_bytes());
 673   } else {
 674     st->print(" total " SIZE_FORMAT "K, used " SIZE_FORMAT "K",
 675                capacity_in_bytes()/K, used_in_bytes()/K);
 676   }
 677   _virtual_space->print_space_boundaries_on(st);
 678   st->print("  eden"); eden_space()->print_on(st);
 679   st->print("  from"); from_space()->print_on(st);
 680   st->print("  to  "); to_space()->print_on(st);
 681 }
 682 
 683 void PSYoungGen::print_used_change(size_t prev_used) const {
 684   gclog_or_tty->print(" [%s:", name());
 685   gclog_or_tty->print(" "  SIZE_FORMAT "K"
 686                       "->" SIZE_FORMAT "K"
 687                       "("  SIZE_FORMAT "K)",
 688                       prev_used / K, used_in_bytes() / K,
 689                       capacity_in_bytes() / K);
 690   gclog_or_tty->print("]");
 691 }
 692 
 693 size_t PSYoungGen::available_for_expansion() {
 694   ShouldNotReachHere();
 695   return 0;
 696 }
 697 
 698 size_t PSYoungGen::available_for_contraction() {
 699   ShouldNotReachHere();
 700   return 0;
 701 }
 702 
 703 size_t PSYoungGen::available_to_min_gen() {
 704   assert(virtual_space()->committed_size() >= min_gen_size(), "Invariant");
 705   return virtual_space()->committed_size() - min_gen_size();
 706 }
 707 
 708 // This method assumes that from-space has live data and that
 709 // any shrinkage of the young gen is limited by location of
 710 // from-space.
 711 size_t PSYoungGen::available_to_live() {
 712   size_t delta_in_survivor = 0;
 713   ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
 714   const size_t space_alignment = heap->intra_generation_alignment();
 715   const size_t gen_alignment = heap->young_gen_alignment();
 716 
 717   MutableSpace* space_shrinking = NULL;
 718   if (from_space()->end() > to_space()->end()) {
 719     space_shrinking = from_space();
 720   } else {
 721     space_shrinking = to_space();
 722   }
 723 
 724   // Include any space that is committed but not included in
 725   // the survivor spaces.
 726   assert(((HeapWord*)virtual_space()->high()) >= space_shrinking->end(),
 727     "Survivor space beyond high end");
 728   size_t unused_committed = pointer_delta(virtual_space()->high(),
 729     space_shrinking->end(), sizeof(char));   
 730 
 731   if (space_shrinking->is_empty()) {
 732     // Don't let the space shrink to 0
 733     assert(space_shrinking->capacity_in_bytes() >= space_alignment, 
 734       "Space is too small");
 735     delta_in_survivor = space_shrinking->capacity_in_bytes() - space_alignment;
 736   } else {
 737     delta_in_survivor = pointer_delta(space_shrinking->end(), 
 738                                       space_shrinking->top(),
 739                                       sizeof(char));
 740   }
 741 
 742   size_t delta_in_bytes = unused_committed + delta_in_survivor;
 743   delta_in_bytes = align_size_down(delta_in_bytes, gen_alignment);
 744   return delta_in_bytes;
 745 }
 746 
 747 // Return the number of bytes available for resizing down the young
 748 // generation.  This is the minimum of
 749 //      input "bytes"
 750 //      bytes to the minimum young gen size
 751 //      bytes to the size currently being used + some small extra
 752 size_t PSYoungGen::limit_gen_shrink(size_t bytes) {
 753   // Allow shrinkage into the current eden but keep eden large enough
 754   // to maintain the minimum young gen size
 755   bytes = MIN3(bytes, available_to_min_gen(), available_to_live());
 756   return align_size_down(bytes, virtual_space()->alignment());
 757 }
 758 
 759 void PSYoungGen::reset_after_change() {
 760   ShouldNotReachHere();
 761 }
 762 
 763 void PSYoungGen::reset_survivors_after_shrink() {
 764   _reserved = MemRegion((HeapWord*)virtual_space()->low_boundary(),
 765                         (HeapWord*)virtual_space()->high_boundary());
 766   PSScavenge::reference_processor()->set_span(_reserved);
 767 
 768   MutableSpace* space_shrinking = NULL;
 769   if (from_space()->end() > to_space()->end()) {
 770     space_shrinking = from_space();
 771   } else {
 772     space_shrinking = to_space();
 773   }
 774 
 775   HeapWord* new_end = (HeapWord*)virtual_space()->high();
 776   assert(new_end >= space_shrinking->bottom(), "Shrink was too large");
 777   // Was there a shrink of the survivor space?
 778   if (new_end < space_shrinking->end()) {
 779     MemRegion mr(space_shrinking->bottom(), new_end);
 780     space_shrinking->initialize(mr, false /* clear */);
 781   }
 782 }
 783 
 784 // This method currently does not expect to expand into eden (i.e.,
 785 // the virtual space boundaries is expected to be consistent
 786 // with the eden boundaries..
 787 void PSYoungGen::post_resize() {
 788   assert_locked_or_safepoint(Heap_lock);
 789   assert((eden_space()->bottom() < to_space()->bottom()) &&
 790          (eden_space()->bottom() < from_space()->bottom()),
 791          "Eden is assumed to be below the survivor spaces");
 792 
 793   MemRegion cmr((HeapWord*)virtual_space()->low(),
 794                 (HeapWord*)virtual_space()->high());
 795   Universe::heap()->barrier_set()->resize_covered_region(cmr);
 796   space_invariants();
 797 }
 798 
 799 
 800 
 801 void PSYoungGen::update_counters() {
 802   if (UsePerfData) {
 803     _eden_counters->update_all();
 804     _from_counters->update_all();
 805     _to_counters->update_all();
 806     _gen_counters->update_all();
 807   }
 808 }
 809 
 810 void PSYoungGen::verify(bool allow_dirty) {
 811   eden_space()->verify(allow_dirty);
 812   from_space()->verify(allow_dirty);
 813   to_space()->verify(allow_dirty);
 814 }