1 /*
   2  * Copyright (c) 2003, 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 "incls/_precompiled.incl"
  26 # include "incls/_adjoiningGenerations.cpp.incl"
  27 
  28 // If boundary moving is being used, create the young gen and old
  29 // gen with ASPSYoungGen and ASPSOldGen, respectively.  Revert to
  30 // the old behavior otherwise (with PSYoungGen and PSOldGen).
  31 
  32 AdjoiningGenerations::AdjoiningGenerations(ReservedSpace old_young_rs,
  33                                            size_t init_low_byte_size,
  34                                            size_t min_low_byte_size,
  35                                            size_t max_low_byte_size,
  36                                            size_t init_high_byte_size,
  37                                            size_t min_high_byte_size,
  38                                            size_t max_high_byte_size,
  39                                            size_t alignment) :
  40   _virtual_spaces(old_young_rs, min_low_byte_size,
  41                   min_high_byte_size, alignment) {
  42   assert(min_low_byte_size <= init_low_byte_size &&
  43          init_low_byte_size <= max_low_byte_size, "Parameter check");
  44   assert(min_high_byte_size <= init_high_byte_size &&
  45          init_high_byte_size <= max_high_byte_size, "Parameter check");
  46   // Create the generations differently based on the option to
  47   // move the boundary.
  48   if (UseAdaptiveGCBoundary) {
  49     // Initialize the adjoining virtual spaces.  Then pass the
  50     // a virtual to each generation for initialization of the
  51     // generation.
  52 
  53     // Does the actual creation of the virtual spaces
  54     _virtual_spaces.initialize(max_low_byte_size,
  55                                init_low_byte_size,
  56                                init_high_byte_size);
  57 
  58     // Place the young gen at the high end.  Passes in the virtual space.
  59     _young_gen = new ASPSYoungGen(_virtual_spaces.high(),
  60                                   _virtual_spaces.high()->committed_size(),
  61                                   min_high_byte_size,
  62                                   _virtual_spaces.high_byte_size_limit());
  63 
  64     // Place the old gen at the low end. Passes in the virtual space.
  65     _old_gen = new ASPSOldGen(_virtual_spaces.low(),
  66                               _virtual_spaces.low()->committed_size(),
  67                               min_low_byte_size,
  68                               _virtual_spaces.low_byte_size_limit(),
  69                               "old", 1);
  70 
  71     young_gen()->initialize_work();
  72     assert(young_gen()->reserved().byte_size() <= young_gen()->gen_size_limit(),
  73      "Consistency check");
  74     assert(old_young_rs.size() >= young_gen()->gen_size_limit(),
  75      "Consistency check");
  76 
  77     old_gen()->initialize_work("old", 1);
  78     assert(old_gen()->reserved().byte_size() <= old_gen()->gen_size_limit(),
  79      "Consistency check");
  80     assert(old_young_rs.size() >= old_gen()->gen_size_limit(),
  81      "Consistency check");
  82   } else {
  83 
  84     // Layout the reserved space for the generations.
  85     ReservedSpace old_rs   =
  86       virtual_spaces()->reserved_space().first_part(max_low_byte_size);
  87     ReservedSpace heap_rs  =
  88       virtual_spaces()->reserved_space().last_part(max_low_byte_size);
  89     ReservedSpace young_rs = heap_rs.first_part(max_high_byte_size);
  90     assert(young_rs.size() == heap_rs.size(), "Didn't reserve all of the heap");
  91 
  92     // Create the generations.  Virtual spaces are not passed in.
  93     _young_gen = new PSYoungGen(init_high_byte_size,
  94                                 min_high_byte_size,
  95                                 max_high_byte_size);
  96     _old_gen = new PSOldGen(init_low_byte_size,
  97                             min_low_byte_size,
  98                             max_low_byte_size,
  99                             "old", 1);
 100 
 101     // The virtual spaces are created by the initialization of the gens.
 102     _young_gen->initialize(young_rs, alignment);
 103     assert(young_gen()->gen_size_limit() == young_rs.size(),
 104       "Consistency check");
 105     _old_gen->initialize(old_rs, alignment, "old", 1);
 106     assert(old_gen()->gen_size_limit() == old_rs.size(), "Consistency check");
 107   }
 108 }
 109 
 110 size_t AdjoiningGenerations::reserved_byte_size() {
 111   return virtual_spaces()->reserved_space().size();
 112 }
 113 
 114 
 115 // Make checks on the current sizes of the generations and
 116 // the contraints on the sizes of the generations.  Push
 117 // up the boundary within the contraints.  A partial
 118 // push can occur.
 119 void AdjoiningGenerations::request_old_gen_expansion(size_t expand_in_bytes) {
 120   assert(UseAdaptiveSizePolicy && UseAdaptiveGCBoundary, "runtime check");
 121 
 122   assert_lock_strong(ExpandHeap_lock);
 123   assert_locked_or_safepoint(Heap_lock);
 124 
 125   // These sizes limit the amount the boundaries can move.  Effectively,
 126   // the generation says how much it is willing to yield to the other
 127   // generation.
 128   const size_t young_gen_available = young_gen()->available_for_contraction();
 129   const size_t old_gen_available = old_gen()->available_for_expansion();
 130   const size_t alignment = virtual_spaces()->alignment();
 131   size_t change_in_bytes = MIN3(young_gen_available,
 132                                 old_gen_available,
 133                                 align_size_up_(expand_in_bytes, alignment));
 134 
 135   if (change_in_bytes == 0) {
 136     return;
 137   }
 138 
 139   if (TraceAdaptiveGCBoundary) {
 140     gclog_or_tty->print_cr("Before expansion of old gen with boundary move");
 141     gclog_or_tty->print_cr("  Requested change: 0x%x  Attempted change: 0x%x",
 142       expand_in_bytes, change_in_bytes);
 143     if (!PrintHeapAtGC) {
 144       Universe::print_on(gclog_or_tty);
 145     }
 146     gclog_or_tty->print_cr("  PSOldGen max size: " SIZE_FORMAT "K",
 147       old_gen()->max_gen_size()/K);
 148   }
 149 
 150   // Move the boundary between the generations up (smaller young gen).
 151   if (virtual_spaces()->adjust_boundary_up(change_in_bytes)) {
 152     young_gen()->reset_after_change();
 153     old_gen()->reset_after_change();
 154   }
 155 
 156   // The total reserved for the generations should match the sum
 157   // of the two even if the boundary is moving.
 158   assert(reserved_byte_size() ==
 159          old_gen()->max_gen_size() + young_gen()->max_size(),
 160          "Space is missing");
 161   young_gen()->space_invariants();
 162   old_gen()->space_invariants();
 163 
 164   if (TraceAdaptiveGCBoundary) {
 165     gclog_or_tty->print_cr("After expansion of old gen with boundary move");
 166     if (!PrintHeapAtGC) {
 167       Universe::print_on(gclog_or_tty);
 168     }
 169     gclog_or_tty->print_cr("  PSOldGen max size: " SIZE_FORMAT "K",
 170       old_gen()->max_gen_size()/K);
 171   }
 172 }
 173 
 174 // See comments on request_old_gen_expansion()
 175 bool AdjoiningGenerations::request_young_gen_expansion(size_t expand_in_bytes) {
 176   assert(UseAdaptiveSizePolicy && UseAdaptiveGCBoundary, "runtime check");
 177 
 178   // If eden is not empty, the boundary can be moved but no advantage
 179   // can be made of the move since eden cannot be moved.
 180   if (!young_gen()->eden_space()->is_empty()) {
 181     return false;
 182   }
 183 
 184 
 185   bool result = false;
 186   const size_t young_gen_available = young_gen()->available_for_expansion();
 187   const size_t old_gen_available = old_gen()->available_for_contraction();
 188   const size_t alignment = virtual_spaces()->alignment();
 189   size_t change_in_bytes = MIN3(young_gen_available,
 190                                 old_gen_available,
 191                                 align_size_up_(expand_in_bytes, alignment));
 192 
 193   if (change_in_bytes == 0) {
 194     return false;
 195   }
 196 
 197   if (TraceAdaptiveGCBoundary) {
 198     gclog_or_tty->print_cr("Before expansion of young gen with boundary move");
 199     gclog_or_tty->print_cr("  Requested change: 0x%x  Attempted change: 0x%x",
 200       expand_in_bytes, change_in_bytes);
 201     if (!PrintHeapAtGC) {
 202       Universe::print_on(gclog_or_tty);
 203     }
 204     gclog_or_tty->print_cr("  PSYoungGen max size: " SIZE_FORMAT "K",
 205       young_gen()->max_size()/K);
 206   }
 207 
 208   // Move the boundary between the generations down (smaller old gen).
 209   MutexLocker x(ExpandHeap_lock);
 210   if (virtual_spaces()->adjust_boundary_down(change_in_bytes)) {
 211     young_gen()->reset_after_change();
 212     old_gen()->reset_after_change();
 213     result = true;
 214   }
 215 
 216   // The total reserved for the generations should match the sum
 217   // of the two even if the boundary is moving.
 218   assert(reserved_byte_size() ==
 219          old_gen()->max_gen_size() + young_gen()->max_size(),
 220          "Space is missing");
 221   young_gen()->space_invariants();
 222   old_gen()->space_invariants();
 223 
 224   if (TraceAdaptiveGCBoundary) {
 225     gclog_or_tty->print_cr("After expansion of young gen with boundary move");
 226     if (!PrintHeapAtGC) {
 227       Universe::print_on(gclog_or_tty);
 228     }
 229     gclog_or_tty->print_cr("  PSYoungGen max size: " SIZE_FORMAT "K",
 230       young_gen()->max_size()/K);
 231   }
 232 
 233   return result;
 234 }
 235 
 236 // Additional space is needed in the old generation.  Try to move the boundary
 237 // up to meet the need.  Moves boundary up only
 238 void AdjoiningGenerations::adjust_boundary_for_old_gen_needs(
 239   size_t desired_free_space) {
 240   assert(UseAdaptiveSizePolicy && UseAdaptiveGCBoundary, "runtime check");
 241 
 242   // Stress testing.
 243   if (PSAdaptiveSizePolicyResizeVirtualSpaceAlot == 1) {
 244     MutexLocker x(ExpandHeap_lock);
 245     request_old_gen_expansion(virtual_spaces()->alignment() * 3 / 2);
 246   }
 247 
 248   // Expand only if the entire generation is already committed.
 249   if (old_gen()->virtual_space()->uncommitted_size() == 0) {
 250     if (old_gen()->free_in_bytes() < desired_free_space) {
 251       MutexLocker x(ExpandHeap_lock);
 252       request_old_gen_expansion(desired_free_space);
 253     }
 254   }
 255 }
 256 
 257 // See comment on adjust_boundary_for_old_gen_needss().
 258 // Adjust boundary down only.
 259 void AdjoiningGenerations::adjust_boundary_for_young_gen_needs(size_t eden_size,
 260     size_t survivor_size) {
 261 
 262   assert(UseAdaptiveSizePolicy && UseAdaptiveGCBoundary, "runtime check");
 263 
 264   // Stress testing.
 265   if (PSAdaptiveSizePolicyResizeVirtualSpaceAlot == 0) {
 266     request_young_gen_expansion(virtual_spaces()->alignment() * 3 / 2);
 267     eden_size = young_gen()->eden_space()->capacity_in_bytes();
 268   }
 269 
 270   // Expand only if the entire generation is already committed.
 271   if (young_gen()->virtual_space()->uncommitted_size() == 0) {
 272     size_t desired_size = eden_size + 2 * survivor_size;
 273     const size_t committed = young_gen()->virtual_space()->committed_size();
 274     if (desired_size > committed) {
 275       request_young_gen_expansion(desired_size - committed);
 276     }
 277   }
 278 }