1 /*
   2  * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "gc_implementation/parallelScavenge/adjoiningGenerations.hpp"
  27 #include "gc_implementation/parallelScavenge/adjoiningVirtualSpaces.hpp"
  28 #include "gc_implementation/parallelScavenge/parallelScavengeHeap.hpp"
  29 
  30 // If boundary moving is being used, create the young gen and old
  31 // gen with ASPSYoungGen and ASPSOldGen, respectively.  Revert to
  32 // the old behavior otherwise (with PSYoungGen and PSOldGen).
  33 
  34 AdjoiningGenerations::AdjoiningGenerations(ReservedSpace old_young_rs,
  35                                            size_t init_low_byte_size,
  36                                            size_t min_low_byte_size,
  37                                            size_t max_low_byte_size,
  38                                            size_t init_high_byte_size,
  39                                            size_t min_high_byte_size,
  40                                            size_t max_high_byte_size,
  41                                            size_t alignment) :
  42   _virtual_spaces(old_young_rs, min_low_byte_size,
  43                   min_high_byte_size, alignment) {
  44   assert(min_low_byte_size <= init_low_byte_size &&
  45          init_low_byte_size <= max_low_byte_size, "Parameter check");
  46   assert(min_high_byte_size <= init_high_byte_size &&
  47          init_high_byte_size <= max_high_byte_size, "Parameter check");
  48   // Create the generations differently based on the option to
  49   // move the boundary.
  50   if (UseAdaptiveGCBoundary) {
  51     // Initialize the adjoining virtual spaces.  Then pass the
  52     // a virtual to each generation for initialization of the
  53     // generation.
  54 
  55     // Does the actual creation of the virtual spaces
  56     _virtual_spaces.initialize(max_low_byte_size,
  57                                init_low_byte_size,
  58                                init_high_byte_size);
  59 
  60     // Place the young gen at the high end.  Passes in the virtual space.
  61     _young_gen = new ASPSYoungGen(_virtual_spaces.high(),
  62                                   _virtual_spaces.high()->committed_size(),
  63                                   min_high_byte_size,
  64                                   _virtual_spaces.high_byte_size_limit());
  65 
  66     // Place the old gen at the low end. Passes in the virtual space.
  67     _old_gen = new ASPSOldGen(_virtual_spaces.low(),
  68                               _virtual_spaces.low()->committed_size(),
  69                               min_low_byte_size,
  70                               _virtual_spaces.low_byte_size_limit(),
  71                               "old", 1);
  72 
  73     young_gen()->initialize_work();
  74     assert(young_gen()->reserved().byte_size() <= young_gen()->gen_size_limit(),
  75      "Consistency check");
  76     assert(old_young_rs.size() >= young_gen()->gen_size_limit(),
  77      "Consistency check");
  78 
  79     old_gen()->initialize_work("old", 1);
  80     assert(old_gen()->reserved().byte_size() <= old_gen()->gen_size_limit(),
  81      "Consistency check");
  82     assert(old_young_rs.size() >= old_gen()->gen_size_limit(),
  83      "Consistency check");
  84   } else {
  85 
  86     // Layout the reserved space for the generations.
  87     ReservedSpace old_rs   =
  88       virtual_spaces()->reserved_space().first_part(max_low_byte_size);
  89     ReservedSpace heap_rs  =
  90       virtual_spaces()->reserved_space().last_part(max_low_byte_size);
  91     ReservedSpace young_rs = heap_rs.first_part(max_high_byte_size);
  92     assert(young_rs.size() == heap_rs.size(), "Didn't reserve all of the heap");
  93 
  94     // Create the generations.  Virtual spaces are not passed in.
  95     _young_gen = new PSYoungGen(init_high_byte_size,
  96                                 min_high_byte_size,
  97                                 max_high_byte_size);
  98     _old_gen = new PSOldGen(init_low_byte_size,
  99                             min_low_byte_size,
 100                             max_low_byte_size,
 101                             "old", 1);
 102 
 103     // The virtual spaces are created by the initialization of the gens.
 104     _young_gen->initialize(young_rs, alignment);
 105     assert(young_gen()->gen_size_limit() == young_rs.size(),
 106       "Consistency check");
 107     _old_gen->initialize(old_rs, alignment, "old", 1);
 108     assert(old_gen()->gen_size_limit() == old_rs.size(), "Consistency check");
 109   }
 110 }
 111 
 112 size_t AdjoiningGenerations::reserved_byte_size() {
 113   return virtual_spaces()->reserved_space().size();
 114 }
 115 
 116 
 117 // Make checks on the current sizes of the generations and
 118 // the contraints on the sizes of the generations.  Push
 119 // up the boundary within the contraints.  A partial
 120 // push can occur.
 121 void AdjoiningGenerations::request_old_gen_expansion(size_t expand_in_bytes) {
 122   assert(UseAdaptiveSizePolicy && UseAdaptiveGCBoundary, "runtime check");
 123 
 124   assert_lock_strong(ExpandHeap_lock);
 125   assert_locked_or_safepoint(Heap_lock);
 126 
 127   // These sizes limit the amount the boundaries can move.  Effectively,
 128   // the generation says how much it is willing to yield to the other
 129   // generation.
 130   const size_t young_gen_available = young_gen()->available_for_contraction();
 131   const size_t old_gen_available = old_gen()->available_for_expansion();
 132   const size_t alignment = virtual_spaces()->alignment();
 133   size_t change_in_bytes = MIN3(young_gen_available,
 134                                 old_gen_available,
 135                                 align_size_up_(expand_in_bytes, alignment));
 136 
 137   if (change_in_bytes == 0) {
 138     return;
 139   }
 140 
 141   if (TraceAdaptiveGCBoundary) {
 142     gclog_or_tty->print_cr("Before expansion of old gen with boundary move");
 143     gclog_or_tty->print_cr("  Requested change: 0x%x  Attempted change: 0x%x",
 144       expand_in_bytes, change_in_bytes);
 145     if (!PrintHeapAtGC) {
 146       Universe::print_on(gclog_or_tty);
 147     }
 148     gclog_or_tty->print_cr("  PSOldGen max size: " SIZE_FORMAT "K",
 149       old_gen()->max_gen_size()/K);
 150   }
 151 
 152   // Move the boundary between the generations up (smaller young gen).
 153   if (virtual_spaces()->adjust_boundary_up(change_in_bytes)) {
 154     young_gen()->reset_after_change();
 155     old_gen()->reset_after_change();
 156   }
 157 
 158   // The total reserved for the generations should match the sum
 159   // of the two even if the boundary is moving.
 160   assert(reserved_byte_size() ==
 161          old_gen()->max_gen_size() + young_gen()->max_size(),
 162          "Space is missing");
 163   young_gen()->space_invariants();
 164   old_gen()->space_invariants();
 165 
 166   if (TraceAdaptiveGCBoundary) {
 167     gclog_or_tty->print_cr("After expansion of old gen with boundary move");
 168     if (!PrintHeapAtGC) {
 169       Universe::print_on(gclog_or_tty);
 170     }
 171     gclog_or_tty->print_cr("  PSOldGen max size: " SIZE_FORMAT "K",
 172       old_gen()->max_gen_size()/K);
 173   }
 174 }
 175 
 176 // See comments on request_old_gen_expansion()
 177 bool AdjoiningGenerations::request_young_gen_expansion(size_t expand_in_bytes) {
 178   assert(UseAdaptiveSizePolicy && UseAdaptiveGCBoundary, "runtime check");
 179 
 180   // If eden is not empty, the boundary can be moved but no advantage
 181   // can be made of the move since eden cannot be moved.
 182   if (!young_gen()->eden_space()->is_empty()) {
 183     return false;
 184   }
 185 
 186 
 187   bool result = false;
 188   const size_t young_gen_available = young_gen()->available_for_expansion();
 189   const size_t old_gen_available = old_gen()->available_for_contraction();
 190   const size_t alignment = virtual_spaces()->alignment();
 191   size_t change_in_bytes = MIN3(young_gen_available,
 192                                 old_gen_available,
 193                                 align_size_up_(expand_in_bytes, alignment));
 194 
 195   if (change_in_bytes == 0) {
 196     return false;
 197   }
 198 
 199   if (TraceAdaptiveGCBoundary) {
 200     gclog_or_tty->print_cr("Before expansion of young gen with boundary move");
 201     gclog_or_tty->print_cr("  Requested change: 0x%x  Attempted change: 0x%x",
 202       expand_in_bytes, change_in_bytes);
 203     if (!PrintHeapAtGC) {
 204       Universe::print_on(gclog_or_tty);
 205     }
 206     gclog_or_tty->print_cr("  PSYoungGen max size: " SIZE_FORMAT "K",
 207       young_gen()->max_size()/K);
 208   }
 209 
 210   // Move the boundary between the generations down (smaller old gen).
 211   MutexLocker x(ExpandHeap_lock);
 212   if (virtual_spaces()->adjust_boundary_down(change_in_bytes)) {
 213     young_gen()->reset_after_change();
 214     old_gen()->reset_after_change();
 215     result = true;
 216   }
 217 
 218   // The total reserved for the generations should match the sum
 219   // of the two even if the boundary is moving.
 220   assert(reserved_byte_size() ==
 221          old_gen()->max_gen_size() + young_gen()->max_size(),
 222          "Space is missing");
 223   young_gen()->space_invariants();
 224   old_gen()->space_invariants();
 225 
 226   if (TraceAdaptiveGCBoundary) {
 227     gclog_or_tty->print_cr("After expansion of young gen with boundary move");
 228     if (!PrintHeapAtGC) {
 229       Universe::print_on(gclog_or_tty);
 230     }
 231     gclog_or_tty->print_cr("  PSYoungGen max size: " SIZE_FORMAT "K",
 232       young_gen()->max_size()/K);
 233   }
 234 
 235   return result;
 236 }
 237 
 238 // Additional space is needed in the old generation.  Try to move the boundary
 239 // up to meet the need.  Moves boundary up only
 240 void AdjoiningGenerations::adjust_boundary_for_old_gen_needs(
 241   size_t desired_free_space) {
 242   assert(UseAdaptiveSizePolicy && UseAdaptiveGCBoundary, "runtime check");
 243 
 244   // Stress testing.
 245   if (PSAdaptiveSizePolicyResizeVirtualSpaceAlot == 1) {
 246     MutexLocker x(ExpandHeap_lock);
 247     request_old_gen_expansion(virtual_spaces()->alignment() * 3 / 2);
 248   }
 249 
 250   // Expand only if the entire generation is already committed.
 251   if (old_gen()->virtual_space()->uncommitted_size() == 0) {
 252     if (old_gen()->free_in_bytes() < desired_free_space) {
 253       MutexLocker x(ExpandHeap_lock);
 254       request_old_gen_expansion(desired_free_space);
 255     }
 256   }
 257 }
 258 
 259 // See comment on adjust_boundary_for_old_gen_needss().
 260 // Adjust boundary down only.
 261 void AdjoiningGenerations::adjust_boundary_for_young_gen_needs(size_t eden_size,
 262     size_t survivor_size) {
 263 
 264   assert(UseAdaptiveSizePolicy && UseAdaptiveGCBoundary, "runtime check");
 265 
 266   // Stress testing.
 267   if (PSAdaptiveSizePolicyResizeVirtualSpaceAlot == 0) {
 268     request_young_gen_expansion(virtual_spaces()->alignment() * 3 / 2);
 269     eden_size = young_gen()->eden_space()->capacity_in_bytes();
 270   }
 271 
 272   // Expand only if the entire generation is already committed.
 273   if (young_gen()->virtual_space()->uncommitted_size() == 0) {
 274     size_t desired_size = eden_size + 2 * survivor_size;
 275     const size_t committed = young_gen()->virtual_space()->committed_size();
 276     if (desired_size > committed) {
 277       request_young_gen_expansion(desired_size - committed);
 278     }
 279   }
 280 }