1 /*
   2  * Copyright (c) 2018, 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/adjoiningGenerationsForHeteroHeap.hpp"
  27 #include "gc/parallel/adjoiningVirtualSpaces.hpp"
  28 #include "gc/parallel/generationSizer.hpp"
  29 #include "gc/parallel/parallelScavengeHeap.hpp"
  30 #include "logging/log.hpp"
  31 #include "logging/logStream.hpp"
  32 #include "memory/resourceArea.hpp"
  33 #include "utilities/align.hpp"
  34 #include "utilities/ostream.hpp"
  35 
  36 // Create two virtual spaces (HeteroVirtualSpaces), low() on nv-dimm memory, high() on dram.
  37 // create ASPSOldGen and ASPSYoungGen the same way as in base class
  38 
  39 AdjoiningGenerationsForHeteroHeap::AdjoiningGenerationsForHeteroHeap(ReservedSpace old_young_rs, size_t total_size_limit,
  40                                                                      GenerationSizer* policy, size_t alignment) : _total_size_limit(total_size_limit) {
  41   size_t init_old_byte_size = policy->initial_old_size();
  42   size_t min_old_byte_size = policy->min_old_size();
  43   size_t max_old_byte_size = policy->max_old_size();
  44   size_t init_young_byte_size = policy->initial_young_size();
  45   size_t min_young_byte_size = policy->min_young_size();
  46   size_t max_young_byte_size = policy->max_young_size();
  47 
  48   // create HeteroVirtualSpaces which is composed of non-overlapping virtual spaces.
  49   _virtual_spaces = new HeteroVirtualSpaces(old_young_rs, min_old_byte_size,
  50                                             min_young_byte_size, _total_size_limit, alignment);
  51 
  52   assert(min_old_byte_size <= init_old_byte_size &&
  53          init_old_byte_size <= max_old_byte_size, "Parameter check");
  54   assert(min_young_byte_size <= init_young_byte_size &&
  55          init_young_byte_size <= max_young_byte_size, "Parameter check");
  56 
  57   assert(UseAdaptiveGCBoundary, "Should be used only when UseAdaptiveGCBoundary is true");
  58 
  59   // Initialize the virtual spaces. Then pass a virtual space to each generation
  60   // for initialization of the generation.
  61 
  62   // Does the actual creation of the virtual spaces
  63   _virtual_spaces->initialize(max_old_byte_size, init_old_byte_size, init_young_byte_size);
  64 
  65   _young_gen = new ASPSYoungGen(_virtual_spaces->high(),
  66                                 _virtual_spaces->high()->committed_size() /* intial_size */,
  67                                 min_young_byte_size,
  68                                 (static_cast <HeteroVirtualSpaces*>(_virtual_spaces))->max_young_size());
  69 
  70   _old_gen = new ASPSOldGen(_virtual_spaces->low(),
  71                             _virtual_spaces->low()->committed_size() /* intial_size */,
  72                             min_old_byte_size,
  73                             (static_cast <HeteroVirtualSpaces*>(_virtual_spaces))->max_old_size(), "old", 1);
  74 
  75   young_gen()->initialize_work();
  76   assert(young_gen()->reserved().byte_size() <= young_gen()->gen_size_limit(), "Consistency check");
  77   assert(old_young_rs.size() >= young_gen()->gen_size_limit(), "Consistency check");
  78 
  79   old_gen()->initialize_work("old", 1);
  80   assert(old_gen()->reserved().byte_size() <= old_gen()->gen_size_limit(), "Consistency check");
  81   assert(old_young_rs.size() >= old_gen()->gen_size_limit(), "Consistency check");
  82 }
  83 
  84 size_t AdjoiningGenerationsForHeteroHeap::required_reserved_memory(GenerationSizer* policy) {
  85   // This is the size that young gen can grow to, when AdaptiveGCBoundary is true.
  86   size_t max_yg_size = policy->max_heap_byte_size() - policy->min_old_size();
  87   // This is the size that old gen can grow to, when AdaptiveGCBoundary is true.
  88   size_t max_old_size = policy->max_heap_byte_size() - policy->min_young_size();
  89 
  90   return max_yg_size + max_old_size;
  91 }
  92 
  93 // We override this function since size of reservedspace here is more than heap size and
  94 // callers expect this function to return heap size.
  95 size_t AdjoiningGenerationsForHeteroHeap::reserved_byte_size() {
  96   return total_size_limit();
  97 }
  98 
  99 AdjoiningGenerationsForHeteroHeap::HeteroVirtualSpaces::HeteroVirtualSpaces(ReservedSpace rs, size_t min_old_byte_size, size_t min_yg_byte_size, size_t max_total_size, size_t alignment) : 
 100                                                                             AdjoiningVirtualSpaces(rs, min_old_byte_size, min_yg_byte_size, alignment),
 101                                                                             _min_old_byte_size(min_old_byte_size), _min_young_byte_size(min_yg_byte_size), 
 102                                                                             _max_old_byte_size(_max_total_size - _min_young_byte_size), 
 103                                                                             _max_young_byte_size(_max_total_size - _min_old_byte_size), 
 104                                                                             _max_total_size(max_total_size) {
 105 }
 106 
 107 void AdjoiningGenerationsForHeteroHeap::HeteroVirtualSpaces::initialize(size_t initial_old_reserved_size, size_t init_old_byte_size,
 108                                                                         size_t init_young_byte_size) {
 109 
 110   // This is the reserved space exclusively for old generation.
 111   ReservedSpace old_rs = _reserved_space.first_part(_max_old_byte_size, true);
 112   // Intially we only assign 'initial_old_reserved_size' of the reserved space to old virtual space.
 113   old_rs = old_rs.first_part(initial_old_reserved_size);
 114 
 115   // This is the reserved space exclusively for young generation.
 116   ReservedSpace young_rs = _reserved_space.last_part(_max_old_byte_size).first_part(_max_young_byte_size);
 117   
 118   // Carve out 'initial_young_reserved_size' of reserved space.
 119   size_t initial_young_reserved_size = _max_total_size - initial_old_reserved_size;
 120   young_rs = young_rs.last_part(_max_young_byte_size - initial_young_reserved_size);
 121 
 122   _old_vs = new PSFileBackedVirtualSpace(old_rs, alignment(), AllocateOldGenAt);
 123   if (!_old_vs->expand_by(init_old_byte_size)) {
 124     vm_exit_during_initialization("Could not reserve enough space for object heap");
 125   }
 126 
 127   _young_vs = new PSVirtualSpaceHighToLow(young_rs, alignment());
 128   if (!_young_vs->expand_by(init_young_byte_size)) {
 129     vm_exit_during_initialization("Could not reserve enough space for object heap");
 130   }
 131 }
 132 
 133 // Since the virtual spaces are non-overlapping, there is no boundary as such.
 134 // We replicate the same behavior and maintain the same invariants as base class - AdjoiningVirtualSpaces, by
 135 // increasing old generation size and decreasing young generation size by same amount.
 136 bool AdjoiningGenerationsForHeteroHeap::HeteroVirtualSpaces::adjust_boundary_up(size_t change_in_bytes) {
 137   assert(UseAdaptiveSizePolicy && UseAdaptiveGCBoundary, "runtime check");
 138   DEBUG_ONLY(size_t total_size_before = _young_vs->reserved_size() + _old_vs->reserved_size());
 139 
 140   size_t bytes_needed = change_in_bytes;
 141   size_t uncommitted_in_old = MIN2(_old_vs->uncommitted_size(), bytes_needed);
 142   bool old_expanded = false;
 143 
 144   // 1. Try to expand old within its reserved space.
 145   if (uncommitted_in_old != 0) {
 146     if (!_old_vs->expand_by(uncommitted_in_old)) {
 147       return false;
 148     }
 149     old_expanded = true;
 150     bytes_needed -= uncommitted_in_old;
 151     if (bytes_needed == 0) {
 152       return true;
 153     }
 154   }
 155 
 156   size_t bytes_to_add_in_old = 0;
 157 
 158   // 2. Get uncommitted memory from Young virtualspace.
 159   size_t young_uncommitted = MIN2(_young_vs->uncommitted_size(), bytes_needed);
 160   if (young_uncommitted > 0) {
 161     _young_vs->set_reserved(_young_vs->reserved_low_addr() + young_uncommitted,
 162                             _young_vs->reserved_high_addr(),
 163                             _young_vs->special());
 164     bytes_needed -= young_uncommitted;
 165     bytes_to_add_in_old = young_uncommitted;
 166   }
 167 
 168   // 3. Get committed memory from Young virtualspace
 169   if (bytes_needed > 0) {
 170     size_t shrink_size = align_down(bytes_needed, _young_vs->alignment());
 171     bool ret = _young_vs->shrink_by(shrink_size);
 172     assert(ret, "We should be able to shrink young space");
 173     _young_vs->set_reserved(_young_vs->reserved_low_addr() + shrink_size,
 174                             _young_vs->reserved_high_addr(),
 175                             _young_vs->special());
 176 
 177     bytes_to_add_in_old += shrink_size;
 178   }
 179 
 180   // 4. Increase size of old space
 181   _old_vs->set_reserved(_old_vs->reserved_low_addr(),
 182                         _old_vs->reserved_high_addr() + bytes_to_add_in_old,
 183                         _old_vs->special());
 184   if (!_old_vs->expand_by(bytes_to_add_in_old) && !old_expanded) {
 185     return false;
 186   }
 187 
 188   DEBUG_ONLY(size_t total_size_after = _young_vs->reserved_size() + _old_vs->reserved_size());
 189   assert(total_size_after == total_size_before, "should be equal");
 190 
 191   return true;
 192 }
 193 
 194 // Read comment for adjust_boundary_up()
 195 // Increase young generation size and decrease old generation size by same amount.
 196 bool AdjoiningGenerationsForHeteroHeap::HeteroVirtualSpaces::adjust_boundary_down(size_t change_in_bytes) {
 197   assert(UseAdaptiveSizePolicy && UseAdaptiveGCBoundary, "runtime check");
 198   DEBUG_ONLY(size_t total_size_before = _young_vs->reserved_size() + _old_vs->reserved_size());
 199 
 200   size_t bytes_needed = change_in_bytes;
 201   size_t uncommitted_in_young = MIN2(_young_vs->uncommitted_size(), bytes_needed);
 202   bool young_expanded = false;
 203 
 204   // 1. Try to expand old within its reserved space.
 205   if (uncommitted_in_young > 0) {
 206     if (!_young_vs->expand_by(uncommitted_in_young)) {
 207       return false;
 208     }
 209     young_expanded = true;
 210     bytes_needed -= uncommitted_in_young;
 211     if (bytes_needed == 0) {
 212       return true;
 213     }
 214   }
 215 
 216   size_t bytes_to_add_in_young = 0;
 217 
 218   // 2. Get uncommitted memory from Old virtualspace.
 219   size_t old_uncommitted = MIN2(_old_vs->uncommitted_size(), bytes_needed);
 220   if (old_uncommitted > 0) {
 221     _old_vs->set_reserved(_old_vs->reserved_low_addr(),
 222                           _old_vs->reserved_high_addr() - old_uncommitted,
 223                           _old_vs->special());
 224     bytes_needed -= old_uncommitted;
 225     bytes_to_add_in_young = old_uncommitted;
 226   }
 227 
 228   // 3. Get committed memory from Old virtualspace
 229   if (bytes_needed > 0) {
 230     size_t shrink_size = align_down(bytes_needed, _old_vs->alignment());
 231     bool ret = _old_vs->shrink_by(shrink_size);
 232     assert(ret, "We should be able to shrink young space");
 233            _old_vs->set_reserved(_old_vs->reserved_low_addr(),
 234            _old_vs->reserved_high_addr() - shrink_size,
 235            _old_vs->special());
 236 
 237     bytes_to_add_in_young += shrink_size;
 238   }
 239 
 240   assert(bytes_to_add_in_young <= change_in_bytes, "should not be more than requested size");
 241   // 4. Increase size of young space
 242   _young_vs->set_reserved(_young_vs->reserved_low_addr() - bytes_to_add_in_young,
 243                           _young_vs->reserved_high_addr(),
 244                           _young_vs->special());
 245   if (!_young_vs->expand_by(bytes_to_add_in_young) && !young_expanded) {
 246     return false;
 247   }
 248 
 249   DEBUG_ONLY(size_t total_size_after = _young_vs->reserved_size() + _old_vs->reserved_size());
 250   assert(total_size_after == total_size_before, "should be equal");
 251 
 252   return true;
 253 }
 254