1 /*
   2  * Copyright (c) 2001, 2015, 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/shared/adaptiveSizePolicy.hpp"
  27 #include "gc_implementation/shared/gcPolicyCounters.hpp"
  28 #include "gc_implementation/shared/vmGCOperations.hpp"
  29 #include "memory/cardTableRS.hpp"
  30 #include "memory/collectorPolicy.hpp"
  31 #include "memory/gcLocker.inline.hpp"
  32 #include "memory/genCollectedHeap.hpp"
  33 #include "memory/generationSpec.hpp"
  34 #include "memory/space.hpp"
  35 #include "memory/universe.hpp"
  36 #include "runtime/arguments.hpp"
  37 #include "runtime/globals_extension.hpp"
  38 #include "runtime/handles.inline.hpp"
  39 #include "runtime/java.hpp"
  40 #include "runtime/thread.inline.hpp"
  41 #include "runtime/vmThread.hpp"
  42 #include "utilities/macros.hpp"
  43 
  44 // CollectorPolicy methods
  45 
  46 CollectorPolicy::CollectorPolicy() :
  47     _space_alignment(0),
  48     _heap_alignment(0),
  49     _initial_heap_byte_size(InitialHeapSize),
  50     _max_heap_byte_size(MaxHeapSize),
  51     _min_heap_byte_size(Arguments::min_heap_size()),
  52     _max_heap_size_cmdline(false),
  53     _size_policy(NULL),
  54     _should_clear_all_soft_refs(false),
  55     _all_soft_refs_clear(false)
  56 {}
  57 
  58 #ifdef ASSERT
  59 void CollectorPolicy::assert_flags() {
  60   assert(InitialHeapSize <= MaxHeapSize, "Ergonomics decided on incompatible initial and maximum heap sizes");
  61   assert(InitialHeapSize % _heap_alignment == 0, "InitialHeapSize alignment");
  62   assert(MaxHeapSize % _heap_alignment == 0, "MaxHeapSize alignment");
  63 }
  64 
  65 void CollectorPolicy::assert_size_info() {
  66   assert(InitialHeapSize == _initial_heap_byte_size, "Discrepancy between InitialHeapSize flag and local storage");
  67   assert(MaxHeapSize == _max_heap_byte_size, "Discrepancy between MaxHeapSize flag and local storage");
  68   assert(_max_heap_byte_size >= _min_heap_byte_size, "Ergonomics decided on incompatible minimum and maximum heap sizes");
  69   assert(_initial_heap_byte_size >= _min_heap_byte_size, "Ergonomics decided on incompatible initial and minimum heap sizes");
  70   assert(_max_heap_byte_size >= _initial_heap_byte_size, "Ergonomics decided on incompatible initial and maximum heap sizes");
  71   assert(_min_heap_byte_size % _heap_alignment == 0, "min_heap_byte_size alignment");
  72   assert(_initial_heap_byte_size % _heap_alignment == 0, "initial_heap_byte_size alignment");
  73   assert(_max_heap_byte_size % _heap_alignment == 0, "max_heap_byte_size alignment");
  74 }
  75 #endif // ASSERT
  76 
  77 void CollectorPolicy::initialize_flags() {
  78   assert(_space_alignment != 0, "Space alignment not set up properly");
  79   assert(_heap_alignment != 0, "Heap alignment not set up properly");
  80   assert(_heap_alignment >= _space_alignment,
  81          err_msg("heap_alignment: " SIZE_FORMAT " less than space_alignment: " SIZE_FORMAT,
  82                  _heap_alignment, _space_alignment));
  83   assert(_heap_alignment % _space_alignment == 0,
  84          err_msg("heap_alignment: " SIZE_FORMAT " not aligned by space_alignment: " SIZE_FORMAT,
  85                  _heap_alignment, _space_alignment));
  86 
  87   if (FLAG_IS_CMDLINE(MaxHeapSize)) {
  88     if (FLAG_IS_CMDLINE(InitialHeapSize) && InitialHeapSize > MaxHeapSize) {
  89       vm_exit_during_initialization("Initial heap size set to a larger value than the maximum heap size");
  90     }
  91     if (_min_heap_byte_size != 0 && MaxHeapSize < _min_heap_byte_size) {
  92       vm_exit_during_initialization("Incompatible minimum and maximum heap sizes specified");
  93     }
  94     _max_heap_size_cmdline = true;
  95   }
  96 
  97   // Check heap parameter properties
  98   if (InitialHeapSize < M) {
  99     vm_exit_during_initialization("Too small initial heap");
 100   }
 101   if (_min_heap_byte_size < M) {
 102     vm_exit_during_initialization("Too small minimum heap");
 103   }
 104 
 105   // User inputs from -Xmx and -Xms must be aligned
 106   _min_heap_byte_size = align_size_up(_min_heap_byte_size, _heap_alignment);
 107   size_t aligned_initial_heap_size = align_size_up(InitialHeapSize, _heap_alignment);
 108   size_t aligned_max_heap_size = align_size_up(MaxHeapSize, _heap_alignment);
 109 
 110   // Write back to flags if the values changed
 111   if (aligned_initial_heap_size != InitialHeapSize) {
 112     FLAG_SET_ERGO(size_t, InitialHeapSize, aligned_initial_heap_size);
 113   }
 114   if (aligned_max_heap_size != MaxHeapSize) {
 115     FLAG_SET_ERGO(size_t, MaxHeapSize, aligned_max_heap_size);
 116   }
 117 
 118   if (FLAG_IS_CMDLINE(InitialHeapSize) && _min_heap_byte_size != 0 &&
 119       InitialHeapSize < _min_heap_byte_size) {
 120     vm_exit_during_initialization("Incompatible minimum and initial heap sizes specified");
 121   }
 122   if (!FLAG_IS_DEFAULT(InitialHeapSize) && InitialHeapSize > MaxHeapSize) {
 123     FLAG_SET_ERGO(size_t, MaxHeapSize, InitialHeapSize);
 124   } else if (!FLAG_IS_DEFAULT(MaxHeapSize) && InitialHeapSize > MaxHeapSize) {
 125     FLAG_SET_ERGO(size_t, InitialHeapSize, MaxHeapSize);
 126     if (InitialHeapSize < _min_heap_byte_size) {
 127       _min_heap_byte_size = InitialHeapSize;
 128     }
 129   }
 130 
 131   _initial_heap_byte_size = InitialHeapSize;
 132   _max_heap_byte_size = MaxHeapSize;
 133 
 134   FLAG_SET_ERGO(size_t, MinHeapDeltaBytes, align_size_up(MinHeapDeltaBytes, _space_alignment));
 135 
 136   DEBUG_ONLY(CollectorPolicy::assert_flags();)
 137 }
 138 
 139 void CollectorPolicy::initialize_size_info() {
 140   if (PrintGCDetails && Verbose) {
 141     gclog_or_tty->print_cr("Minimum heap " SIZE_FORMAT "  Initial heap "
 142       SIZE_FORMAT "  Maximum heap " SIZE_FORMAT,
 143       _min_heap_byte_size, _initial_heap_byte_size, _max_heap_byte_size);
 144   }
 145 
 146   DEBUG_ONLY(CollectorPolicy::assert_size_info();)
 147 }
 148 
 149 bool CollectorPolicy::use_should_clear_all_soft_refs(bool v) {
 150   bool result = _should_clear_all_soft_refs;
 151   set_should_clear_all_soft_refs(false);
 152   return result;
 153 }
 154 
 155 GenRemSet* CollectorPolicy::create_rem_set(MemRegion whole_heap) {
 156   return new CardTableRS(whole_heap);
 157 }
 158 
 159 void CollectorPolicy::cleared_all_soft_refs() {
 160   // If near gc overhear limit, continue to clear SoftRefs.  SoftRefs may
 161   // have been cleared in the last collection but if the gc overhear
 162   // limit continues to be near, SoftRefs should still be cleared.
 163   if (size_policy() != NULL) {
 164     _should_clear_all_soft_refs = size_policy()->gc_overhead_limit_near();
 165   }
 166   _all_soft_refs_clear = true;
 167 }
 168 
 169 size_t CollectorPolicy::compute_heap_alignment() {
 170   // The card marking array and the offset arrays for old generations are
 171   // committed in os pages as well. Make sure they are entirely full (to
 172   // avoid partial page problems), e.g. if 512 bytes heap corresponds to 1
 173   // byte entry and the os page size is 4096, the maximum heap size should
 174   // be 512*4096 = 2MB aligned.
 175 
 176   size_t alignment = GenRemSet::max_alignment_constraint();
 177 
 178   if (UseLargePages) {
 179       // In presence of large pages we have to make sure that our
 180       // alignment is large page aware.
 181       alignment = lcm(os::large_page_size(), alignment);
 182   }
 183 
 184   return alignment;
 185 }
 186 
 187 // GenCollectorPolicy methods
 188 
 189 GenCollectorPolicy::GenCollectorPolicy() :
 190     _min_young_size(0),
 191     _initial_young_size(0),
 192     _max_young_size(0),
 193     _min_old_size(0),
 194     _initial_old_size(0),
 195     _max_old_size(0),
 196     _gen_alignment(0),
 197     _young_gen_spec(NULL),
 198     _old_gen_spec(NULL)
 199 {}
 200 
 201 size_t GenCollectorPolicy::scale_by_NewRatio_aligned(size_t base_size) {
 202   return align_size_down_bounded(base_size / (NewRatio + 1), _gen_alignment);
 203 }
 204 
 205 size_t GenCollectorPolicy::bound_minus_alignment(size_t desired_size,
 206                                                  size_t maximum_size) {
 207   size_t max_minus = maximum_size - _gen_alignment;
 208   return desired_size < max_minus ? desired_size : max_minus;
 209 }
 210 
 211 
 212 void GenCollectorPolicy::initialize_size_policy(size_t init_eden_size,
 213                                                 size_t init_promo_size,
 214                                                 size_t init_survivor_size) {
 215   const double max_gc_pause_sec = ((double) MaxGCPauseMillis) / 1000.0;
 216   _size_policy = new AdaptiveSizePolicy(init_eden_size,
 217                                         init_promo_size,
 218                                         init_survivor_size,
 219                                         max_gc_pause_sec,
 220                                         GCTimeRatio);
 221 }
 222 
 223 size_t GenCollectorPolicy::young_gen_size_lower_bound() {
 224   // The young generation must be aligned and have room for eden + two survivors
 225   return align_size_up(3 * _space_alignment, _gen_alignment);
 226 }
 227 
 228 #ifdef ASSERT
 229 void GenCollectorPolicy::assert_flags() {
 230   CollectorPolicy::assert_flags();
 231   assert(NewSize >= _min_young_size, "Ergonomics decided on a too small young gen size");
 232   assert(NewSize <= MaxNewSize, "Ergonomics decided on incompatible initial and maximum young gen sizes");
 233   assert(FLAG_IS_DEFAULT(MaxNewSize) || MaxNewSize < MaxHeapSize, "Ergonomics decided on incompatible maximum young gen and heap sizes");
 234   assert(NewSize % _gen_alignment == 0, "NewSize alignment");
 235   assert(FLAG_IS_DEFAULT(MaxNewSize) || MaxNewSize % _gen_alignment == 0, "MaxNewSize alignment");
 236   assert(OldSize + NewSize <= MaxHeapSize, "Ergonomics decided on incompatible generation and heap sizes");
 237   assert(OldSize % _gen_alignment == 0, "OldSize alignment");
 238 }
 239 
 240 void GenCollectorPolicy::assert_size_info() {
 241   CollectorPolicy::assert_size_info();
 242   // GenCollectorPolicy::initialize_size_info may update the MaxNewSize
 243   assert(MaxNewSize < MaxHeapSize, "Ergonomics decided on incompatible maximum young and heap sizes");
 244   assert(NewSize == _initial_young_size, "Discrepancy between NewSize flag and local storage");
 245   assert(MaxNewSize == _max_young_size, "Discrepancy between MaxNewSize flag and local storage");
 246   assert(OldSize == _initial_old_size, "Discrepancy between OldSize flag and local storage");
 247   assert(_min_young_size <= _initial_young_size, "Ergonomics decided on incompatible minimum and initial young gen sizes");
 248   assert(_initial_young_size <= _max_young_size, "Ergonomics decided on incompatible initial and maximum young gen sizes");
 249   assert(_min_young_size % _gen_alignment == 0, "_min_young_size alignment");
 250   assert(_initial_young_size % _gen_alignment == 0, "_initial_young_size alignment");
 251   assert(_max_young_size % _gen_alignment == 0, "_max_young_size alignment");
 252   assert(_min_young_size <= bound_minus_alignment(_min_young_size, _min_heap_byte_size),
 253       "Ergonomics made minimum young generation larger than minimum heap");
 254   assert(_initial_young_size <=  bound_minus_alignment(_initial_young_size, _initial_heap_byte_size),
 255       "Ergonomics made initial young generation larger than initial heap");
 256   assert(_max_young_size <= bound_minus_alignment(_max_young_size, _max_heap_byte_size),
 257       "Ergonomics made maximum young generation lager than maximum heap");
 258   assert(_min_old_size <= _initial_old_size, "Ergonomics decided on incompatible minimum and initial old gen sizes");
 259   assert(_initial_old_size <= _max_old_size, "Ergonomics decided on incompatible initial and maximum old gen sizes");
 260   assert(_max_old_size % _gen_alignment == 0, "_max_old_size alignment");
 261   assert(_initial_old_size % _gen_alignment == 0, "_initial_old_size alignment");
 262   assert(_max_heap_byte_size <= (_max_young_size + _max_old_size), "Total maximum heap sizes must be sum of generation maximum sizes");
 263   assert(_min_young_size + _min_old_size <= _min_heap_byte_size, "Minimum generation sizes exceed minimum heap size");
 264   assert(_initial_young_size + _initial_old_size == _initial_heap_byte_size, "Initial generation sizes should match initial heap size");
 265   assert(_max_young_size + _max_old_size == _max_heap_byte_size, "Maximum generation sizes should match maximum heap size");
 266 }
 267 #endif // ASSERT
 268 
 269 void GenCollectorPolicy::initialize_flags() {
 270   CollectorPolicy::initialize_flags();
 271 
 272   assert(_gen_alignment != 0, "Generation alignment not set up properly");
 273   assert(_heap_alignment >= _gen_alignment,
 274          err_msg("heap_alignment: " SIZE_FORMAT " less than gen_alignment: " SIZE_FORMAT,
 275                  _heap_alignment, _gen_alignment));
 276   assert(_gen_alignment % _space_alignment == 0,
 277          err_msg("gen_alignment: " SIZE_FORMAT " not aligned by space_alignment: " SIZE_FORMAT,
 278                  _gen_alignment, _space_alignment));
 279   assert(_heap_alignment % _gen_alignment == 0,
 280          err_msg("heap_alignment: " SIZE_FORMAT " not aligned by gen_alignment: " SIZE_FORMAT,
 281                  _heap_alignment, _gen_alignment));
 282 
 283   // All generational heaps have a youngest gen; handle those flags here
 284 
 285   // Make sure the heap is large enough for two generations
 286   size_t smallest_new_size = young_gen_size_lower_bound();
 287   size_t smallest_heap_size = align_size_up(smallest_new_size + align_size_up(_space_alignment, _gen_alignment),
 288                                            _heap_alignment);
 289   if (MaxHeapSize < smallest_heap_size) {
 290     FLAG_SET_ERGO(size_t, MaxHeapSize, smallest_heap_size);
 291     _max_heap_byte_size = MaxHeapSize;
 292   }
 293   // If needed, synchronize _min_heap_byte size and _initial_heap_byte_size
 294   if (_min_heap_byte_size < smallest_heap_size) {
 295     _min_heap_byte_size = smallest_heap_size;
 296     if (InitialHeapSize < _min_heap_byte_size) {
 297       FLAG_SET_ERGO(size_t, InitialHeapSize, smallest_heap_size);
 298       _initial_heap_byte_size = smallest_heap_size;
 299     }
 300   }
 301 
 302   // Make sure NewSize allows an old generation to fit even if set on the command line
 303   if (FLAG_IS_CMDLINE(NewSize) && NewSize >= _initial_heap_byte_size) {
 304     warning("NewSize was set larger than initial heap size, will use initial heap size.");
 305     NewSize = bound_minus_alignment(NewSize, _initial_heap_byte_size);
 306   }
 307 
 308   // Now take the actual NewSize into account. We will silently increase NewSize
 309   // if the user specified a smaller or unaligned value.
 310   size_t bounded_new_size = bound_minus_alignment(NewSize, MaxHeapSize);
 311   bounded_new_size = MAX2(smallest_new_size, (size_t)align_size_down(bounded_new_size, _gen_alignment));
 312   if (bounded_new_size != NewSize) {
 313     // Do not use FLAG_SET_ERGO to update NewSize here, since this will override
 314     // if NewSize was set on the command line or not. This information is needed
 315     // later when setting the initial and minimum young generation size.
 316     NewSize = bounded_new_size;
 317   }
 318   _min_young_size = smallest_new_size;
 319   _initial_young_size = NewSize;
 320 
 321   if (!FLAG_IS_DEFAULT(MaxNewSize)) {
 322     if (MaxNewSize >= MaxHeapSize) {
 323       // Make sure there is room for an old generation
 324       size_t smaller_max_new_size = MaxHeapSize - _gen_alignment;
 325       if (FLAG_IS_CMDLINE(MaxNewSize)) {
 326         warning("MaxNewSize (" SIZE_FORMAT "k) is equal to or greater than the entire "
 327                 "heap (" SIZE_FORMAT "k).  A new max generation size of " SIZE_FORMAT "k will be used.",
 328                 MaxNewSize/K, MaxHeapSize/K, smaller_max_new_size/K);
 329       }
 330       FLAG_SET_ERGO(size_t, MaxNewSize, smaller_max_new_size);
 331       if (NewSize > MaxNewSize) {
 332         FLAG_SET_ERGO(size_t, NewSize, MaxNewSize);
 333         _initial_young_size = NewSize;
 334       }
 335     } else if (MaxNewSize < _initial_young_size) {
 336       FLAG_SET_ERGO(size_t, MaxNewSize, _initial_young_size);
 337     } else if (!is_size_aligned(MaxNewSize, _gen_alignment)) {
 338       FLAG_SET_ERGO(size_t, MaxNewSize, align_size_down(MaxNewSize, _gen_alignment));
 339     }
 340     _max_young_size = MaxNewSize;
 341   }
 342 
 343   if (NewSize > MaxNewSize) {
 344     // At this point this should only happen if the user specifies a large NewSize and/or
 345     // a small (but not too small) MaxNewSize.
 346     if (FLAG_IS_CMDLINE(MaxNewSize)) {
 347       warning("NewSize (" SIZE_FORMAT "k) is greater than the MaxNewSize (" SIZE_FORMAT "k). "
 348               "A new max generation size of " SIZE_FORMAT "k will be used.",
 349               NewSize/K, MaxNewSize/K, NewSize/K);
 350     }
 351     FLAG_SET_ERGO(size_t, MaxNewSize, NewSize);
 352     _max_young_size = MaxNewSize;
 353   }
 354 
 355   if (SurvivorRatio < 1 || NewRatio < 1) {
 356     vm_exit_during_initialization("Invalid young gen ratio specified");
 357   }
 358 
 359   if (!is_size_aligned(OldSize, _gen_alignment)) {
 360     // Setting OldSize directly to preserve information about the possible
 361     // setting of OldSize on the command line.
 362     OldSize = align_size_down(OldSize, _gen_alignment);
 363   }
 364 
 365   if (FLAG_IS_CMDLINE(OldSize) && FLAG_IS_DEFAULT(MaxHeapSize)) {
 366     // NewRatio will be used later to set the young generation size so we use
 367     // it to calculate how big the heap should be based on the requested OldSize
 368     // and NewRatio.
 369     assert(NewRatio > 0, "NewRatio should have been set up earlier");
 370     size_t calculated_heapsize = (OldSize / NewRatio) * (NewRatio + 1);
 371 
 372     calculated_heapsize = align_size_up(calculated_heapsize, _heap_alignment);
 373     FLAG_SET_ERGO(size_t, MaxHeapSize, calculated_heapsize);
 374     _max_heap_byte_size = MaxHeapSize;
 375     FLAG_SET_ERGO(size_t, InitialHeapSize, calculated_heapsize);
 376     _initial_heap_byte_size = InitialHeapSize;
 377   }
 378 
 379   // Adjust NewSize and OldSize or MaxHeapSize to match each other
 380   if (NewSize + OldSize > MaxHeapSize) {
 381     if (_max_heap_size_cmdline) {
 382       // Somebody has set a maximum heap size with the intention that we should not
 383       // exceed it. Adjust New/OldSize as necessary.
 384       size_t calculated_size = NewSize + OldSize;
 385       double shrink_factor = (double) MaxHeapSize / calculated_size;
 386       size_t smaller_new_size = align_size_down((size_t)(NewSize * shrink_factor), _gen_alignment);
 387       FLAG_SET_ERGO(size_t, NewSize, MAX2(young_gen_size_lower_bound(), smaller_new_size));
 388       _initial_young_size = NewSize;
 389 
 390       // OldSize is already aligned because above we aligned MaxHeapSize to
 391       // _heap_alignment, and we just made sure that NewSize is aligned to
 392       // _gen_alignment. In initialize_flags() we verified that _heap_alignment
 393       // is a multiple of _gen_alignment.
 394       FLAG_SET_ERGO(size_t, OldSize, MaxHeapSize - NewSize);
 395     } else {
 396       FLAG_SET_ERGO(size_t, MaxHeapSize, align_size_up(NewSize + OldSize, _heap_alignment));
 397       _max_heap_byte_size = MaxHeapSize;
 398     }
 399   }
 400 
 401   // Update NewSize, if possible, to avoid sizing the young gen too small when only
 402   // OldSize is set on the command line.
 403   if (FLAG_IS_CMDLINE(OldSize) && !FLAG_IS_CMDLINE(NewSize)) {
 404     if (OldSize < _initial_heap_byte_size) {
 405       size_t new_size = _initial_heap_byte_size - OldSize;
 406       // Need to compare against the flag value for max since _max_young_size
 407       // might not have been set yet.
 408       if (new_size >= _min_young_size && new_size <= MaxNewSize) {
 409         FLAG_SET_ERGO(size_t, NewSize, new_size);
 410         _initial_young_size = NewSize;
 411       }
 412     }
 413   }
 414 
 415   always_do_update_barrier = UseConcMarkSweepGC;
 416 
 417   DEBUG_ONLY(GenCollectorPolicy::assert_flags();)
 418 }
 419 
 420 // Values set on the command line win over any ergonomically
 421 // set command line parameters.
 422 // Ergonomic choice of parameters are done before this
 423 // method is called.  Values for command line parameters such as NewSize
 424 // and MaxNewSize feed those ergonomic choices into this method.
 425 // This method makes the final generation sizings consistent with
 426 // themselves and with overall heap sizings.
 427 // In the absence of explicitly set command line flags, policies
 428 // such as the use of NewRatio are used to size the generation.
 429 
 430 // Minimum sizes of the generations may be different than
 431 // the initial sizes.  An inconsistency is permitted here
 432 // in the total size that can be specified explicitly by
 433 // command line specification of OldSize and NewSize and
 434 // also a command line specification of -Xms.  Issue a warning
 435 // but allow the values to pass.
 436 void GenCollectorPolicy::initialize_size_info() {
 437   CollectorPolicy::initialize_size_info();
 438 
 439   _initial_young_size = NewSize;
 440   _max_young_size = MaxNewSize;
 441   _initial_old_size = OldSize;
 442 
 443   // Determine maximum size of the young generation.
 444 
 445   if (FLAG_IS_DEFAULT(MaxNewSize)) {
 446     _max_young_size = scale_by_NewRatio_aligned(_max_heap_byte_size);
 447     // Bound the maximum size by NewSize below (since it historically
 448     // would have been NewSize and because the NewRatio calculation could
 449     // yield a size that is too small) and bound it by MaxNewSize above.
 450     // Ergonomics plays here by previously calculating the desired
 451     // NewSize and MaxNewSize.
 452     _max_young_size = MIN2(MAX2(_max_young_size, _initial_young_size), MaxNewSize);
 453   }
 454 
 455   // Given the maximum young size, determine the initial and
 456   // minimum young sizes.
 457 
 458   if (_max_heap_byte_size == _initial_heap_byte_size) {
 459     // The maximum and initial heap sizes are the same so the generation's
 460     // initial size must be the same as it maximum size. Use NewSize as the
 461     // size if set on command line.
 462     _max_young_size = FLAG_IS_CMDLINE(NewSize) ? NewSize : _max_young_size;
 463     _initial_young_size = _max_young_size;
 464 
 465     // Also update the minimum size if min == initial == max.
 466     if (_max_heap_byte_size == _min_heap_byte_size) {
 467       _min_young_size = _max_young_size;
 468     }
 469   } else {
 470     if (FLAG_IS_CMDLINE(NewSize)) {
 471       // If NewSize is set on the command line, we should use it as
 472       // the initial size, but make sure it is within the heap bounds.
 473       _initial_young_size =
 474         MIN2(_max_young_size, bound_minus_alignment(NewSize, _initial_heap_byte_size));
 475       _min_young_size = bound_minus_alignment(_initial_young_size, _min_heap_byte_size);
 476     } else {
 477       // For the case where NewSize is not set on the command line, use
 478       // NewRatio to size the initial generation size. Use the current
 479       // NewSize as the floor, because if NewRatio is overly large, the resulting
 480       // size can be too small.
 481       _initial_young_size =
 482         MIN2(_max_young_size, MAX2(scale_by_NewRatio_aligned(_initial_heap_byte_size), NewSize));
 483     }
 484   }
 485 
 486   if (PrintGCDetails && Verbose) {
 487     gclog_or_tty->print_cr("1: Minimum young " SIZE_FORMAT "  Initial young "
 488       SIZE_FORMAT "  Maximum young " SIZE_FORMAT,
 489       _min_young_size, _initial_young_size, _max_young_size);
 490   }
 491 
 492   // At this point the minimum, initial and maximum sizes
 493   // of the overall heap and of the young generation have been determined.
 494   // The maximum old size can be determined from the maximum young
 495   // and maximum heap size since no explicit flags exist
 496   // for setting the old generation maximum.
 497   _max_old_size = MAX2(_max_heap_byte_size - _max_young_size, _gen_alignment);
 498 
 499   // If no explicit command line flag has been set for the
 500   // old generation size, use what is left.
 501   if (!FLAG_IS_CMDLINE(OldSize)) {
 502     // The user has not specified any value but the ergonomics
 503     // may have chosen a value (which may or may not be consistent
 504     // with the overall heap size).  In either case make
 505     // the minimum, maximum and initial sizes consistent
 506     // with the young sizes and the overall heap sizes.
 507     _min_old_size = _gen_alignment;
 508     _initial_old_size = MIN2(_max_old_size, MAX2(_initial_heap_byte_size - _initial_young_size, _min_old_size));
 509     // _max_old_size has already been made consistent above.
 510   } else {
 511     // OldSize has been explicitly set on the command line. Use it
 512     // for the initial size but make sure the minimum allow a young
 513     // generation to fit as well.
 514     // If the user has explicitly set an OldSize that is inconsistent
 515     // with other command line flags, issue a warning.
 516     // The generation minimums and the overall heap minimum should
 517     // be within one generation alignment.
 518     if (_initial_old_size > _max_old_size) {
 519       warning("Inconsistency between maximum heap size and maximum "
 520           "generation sizes: using maximum heap = " SIZE_FORMAT
 521           " -XX:OldSize flag is being ignored",
 522           _max_heap_byte_size);
 523       _initial_old_size = _max_old_size;
 524     }
 525 
 526     _min_old_size = MIN2(_initial_old_size, _min_heap_byte_size - _min_young_size);
 527   }
 528 
 529   // The initial generation sizes should match the initial heap size,
 530   // if not issue a warning and resize the generations. This behavior
 531   // differs from JDK8 where the generation sizes have higher priority
 532   // than the initial heap size.
 533   if ((_initial_old_size + _initial_young_size) != _initial_heap_byte_size) {
 534     warning("Inconsistency between generation sizes and heap size, resizing "
 535             "the generations to fit the heap.");
 536 
 537     size_t desired_young_size = _initial_heap_byte_size - _initial_old_size;
 538     if (_initial_heap_byte_size < _initial_old_size) {
 539       // Old want all memory, use minimum for young and rest for old
 540       _initial_young_size = _min_young_size;
 541       _initial_old_size = _initial_heap_byte_size - _min_young_size;
 542     } else if (desired_young_size > _max_young_size) {
 543       // Need to increase both young and old generation
 544       _initial_young_size = _max_young_size;
 545       _initial_old_size = _initial_heap_byte_size - _max_young_size;
 546     } else if (desired_young_size < _min_young_size) {
 547       // Need to decrease both young and old generation
 548       _initial_young_size = _min_young_size;
 549       _initial_old_size = _initial_heap_byte_size - _min_young_size;
 550     } else {
 551       // The young generation boundaries allow us to only update the
 552       // young generation.
 553       _initial_young_size = desired_young_size;
 554     }
 555 
 556     if (PrintGCDetails && Verbose) {
 557       gclog_or_tty->print_cr("2: Minimum young " SIZE_FORMAT "  Initial young "
 558         SIZE_FORMAT "  Maximum young " SIZE_FORMAT,
 559         _min_young_size, _initial_young_size, _max_young_size);
 560     }
 561   }
 562 
 563   // Write back to flags if necessary.
 564   if (NewSize != _initial_young_size) {
 565     FLAG_SET_ERGO(size_t, NewSize, _initial_young_size);
 566   }
 567 
 568   if (MaxNewSize != _max_young_size) {
 569     FLAG_SET_ERGO(size_t, MaxNewSize, _max_young_size);
 570   }
 571 
 572   if (OldSize != _initial_old_size) {
 573     FLAG_SET_ERGO(size_t, OldSize, _initial_old_size);
 574   }
 575 
 576   if (PrintGCDetails && Verbose) {
 577     gclog_or_tty->print_cr("Minimum old " SIZE_FORMAT "  Initial old "
 578       SIZE_FORMAT "  Maximum old " SIZE_FORMAT,
 579       _min_old_size, _initial_old_size, _max_old_size);
 580   }
 581 
 582   DEBUG_ONLY(GenCollectorPolicy::assert_size_info();)
 583 }
 584 
 585 HeapWord* GenCollectorPolicy::mem_allocate_work(size_t size,
 586                                         bool is_tlab,
 587                                         bool* gc_overhead_limit_was_exceeded) {
 588   GenCollectedHeap *gch = GenCollectedHeap::heap();
 589 
 590   debug_only(gch->check_for_valid_allocation_state());
 591   assert(gch->no_gc_in_progress(), "Allocation during gc not allowed");
 592 
 593   // In general gc_overhead_limit_was_exceeded should be false so
 594   // set it so here and reset it to true only if the gc time
 595   // limit is being exceeded as checked below.
 596   *gc_overhead_limit_was_exceeded = false;
 597 
 598   HeapWord* result = NULL;
 599 
 600   // Loop until the allocation is satisfied, or unsatisfied after GC.
 601   for (uint try_count = 1, gclocker_stalled_count = 0; /* return or throw */; try_count += 1) {
 602     HandleMark hm; // Discard any handles allocated in each iteration.
 603 
 604     // First allocation attempt is lock-free.
 605     Generation *young = gch->young_gen();
 606     assert(young->supports_inline_contig_alloc(),
 607       "Otherwise, must do alloc within heap lock");
 608     if (young->should_allocate(size, is_tlab)) {
 609       result = young->par_allocate(size, is_tlab);
 610       if (result != NULL) {
 611         assert(gch->is_in_reserved(result), "result not in heap");
 612         return result;
 613       }
 614     }
 615     uint gc_count_before;  // Read inside the Heap_lock locked region.
 616     {
 617       MutexLocker ml(Heap_lock);
 618       if (PrintGC && Verbose) {
 619         gclog_or_tty->print_cr("GenCollectorPolicy::mem_allocate_work:"
 620                                " attempting locked slow path allocation");
 621       }
 622       // Note that only large objects get a shot at being
 623       // allocated in later generations.
 624       bool first_only = ! should_try_older_generation_allocation(size);
 625 
 626       result = gch->attempt_allocation(size, is_tlab, first_only);
 627       if (result != NULL) {
 628         assert(gch->is_in_reserved(result), "result not in heap");
 629         return result;
 630       }
 631 
 632       if (GC_locker::is_active_and_needs_gc()) {
 633         if (is_tlab) {
 634           return NULL;  // Caller will retry allocating individual object.
 635         }
 636         if (!gch->is_maximal_no_gc()) {
 637           // Try and expand heap to satisfy request.
 638           result = expand_heap_and_allocate(size, is_tlab);
 639           // Result could be null if we are out of space.
 640           if (result != NULL) {
 641             return result;
 642           }
 643         }
 644 
 645         if (gclocker_stalled_count > GCLockerRetryAllocationCount) {
 646           return NULL; // We didn't get to do a GC and we didn't get any memory.
 647         }
 648 
 649         // If this thread is not in a jni critical section, we stall
 650         // the requestor until the critical section has cleared and
 651         // GC allowed. When the critical section clears, a GC is
 652         // initiated by the last thread exiting the critical section; so
 653         // we retry the allocation sequence from the beginning of the loop,
 654         // rather than causing more, now probably unnecessary, GC attempts.
 655         JavaThread* jthr = JavaThread::current();
 656         if (!jthr->in_critical()) {
 657           MutexUnlocker mul(Heap_lock);
 658           // Wait for JNI critical section to be exited
 659           GC_locker::stall_until_clear();
 660           gclocker_stalled_count += 1;
 661           continue;
 662         } else {
 663           if (CheckJNICalls) {
 664             fatal("Possible deadlock due to allocating while"
 665                   " in jni critical section");
 666           }
 667           return NULL;
 668         }
 669       }
 670 
 671       // Read the gc count while the heap lock is held.
 672       gc_count_before = gch->total_collections();
 673     }
 674 
 675     VM_GenCollectForAllocation op(size, is_tlab, gc_count_before);
 676     VMThread::execute(&op);
 677     if (op.prologue_succeeded()) {
 678       result = op.result();
 679       if (op.gc_locked()) {
 680          assert(result == NULL, "must be NULL if gc_locked() is true");
 681          continue;  // Retry and/or stall as necessary.
 682       }
 683 
 684       // Allocation has failed and a collection
 685       // has been done.  If the gc time limit was exceeded the
 686       // this time, return NULL so that an out-of-memory
 687       // will be thrown.  Clear gc_overhead_limit_exceeded
 688       // so that the overhead exceeded does not persist.
 689 
 690       const bool limit_exceeded = size_policy()->gc_overhead_limit_exceeded();
 691       const bool softrefs_clear = all_soft_refs_clear();
 692 
 693       if (limit_exceeded && softrefs_clear) {
 694         *gc_overhead_limit_was_exceeded = true;
 695         size_policy()->set_gc_overhead_limit_exceeded(false);
 696         if (op.result() != NULL) {
 697           CollectedHeap::fill_with_object(op.result(), size);
 698         }
 699         return NULL;
 700       }
 701       assert(result == NULL || gch->is_in_reserved(result),
 702              "result not in heap");
 703       return result;
 704     }
 705 
 706     // Give a warning if we seem to be looping forever.
 707     if ((QueuedAllocationWarningCount > 0) &&
 708         (try_count % QueuedAllocationWarningCount == 0)) {
 709           warning("GenCollectorPolicy::mem_allocate_work retries %d times \n\t"
 710                   " size=" SIZE_FORMAT " %s", try_count, size, is_tlab ? "(TLAB)" : "");
 711     }
 712   }
 713 }
 714 
 715 HeapWord* GenCollectorPolicy::expand_heap_and_allocate(size_t size,
 716                                                        bool   is_tlab) {
 717   GenCollectedHeap *gch = GenCollectedHeap::heap();
 718   HeapWord* result = NULL;
 719   Generation *old = gch->old_gen();
 720   if (old->should_allocate(size, is_tlab)) {
 721     result = old->expand_and_allocate(size, is_tlab);
 722   }
 723   if (result == NULL) {
 724     Generation *young = gch->young_gen();
 725     if (young->should_allocate(size, is_tlab)) {
 726       result = young->expand_and_allocate(size, is_tlab);
 727     }
 728   }
 729   assert(result == NULL || gch->is_in_reserved(result), "result not in heap");
 730   return result;
 731 }
 732 
 733 HeapWord* GenCollectorPolicy::satisfy_failed_allocation(size_t size,
 734                                                         bool   is_tlab) {
 735   GenCollectedHeap *gch = GenCollectedHeap::heap();
 736   GCCauseSetter x(gch, GCCause::_allocation_failure);
 737   HeapWord* result = NULL;
 738 
 739   assert(size != 0, "Precondition violated");
 740   if (GC_locker::is_active_and_needs_gc()) {
 741     // GC locker is active; instead of a collection we will attempt
 742     // to expand the heap, if there's room for expansion.
 743     if (!gch->is_maximal_no_gc()) {
 744       result = expand_heap_and_allocate(size, is_tlab);
 745     }
 746     return result;   // Could be null if we are out of space.
 747   } else if (!gch->incremental_collection_will_fail(false /* don't consult_young */)) {
 748     // Do an incremental collection.
 749     gch->do_collection(false            /* full */,
 750                        false            /* clear_all_soft_refs */,
 751                        size             /* size */,
 752                        is_tlab          /* is_tlab */,
 753                        number_of_generations() - 1 /* max_level */);
 754   } else {
 755     if (Verbose && PrintGCDetails) {
 756       gclog_or_tty->print(" :: Trying full because partial may fail :: ");
 757     }
 758     // Try a full collection; see delta for bug id 6266275
 759     // for the original code and why this has been simplified
 760     // with from-space allocation criteria modified and
 761     // such allocation moved out of the safepoint path.
 762     gch->do_collection(true             /* full */,
 763                        false            /* clear_all_soft_refs */,
 764                        size             /* size */,
 765                        is_tlab          /* is_tlab */,
 766                        number_of_generations() - 1 /* max_level */);
 767   }
 768 
 769   result = gch->attempt_allocation(size, is_tlab, false /*first_only*/);
 770 
 771   if (result != NULL) {
 772     assert(gch->is_in_reserved(result), "result not in heap");
 773     return result;
 774   }
 775 
 776   // OK, collection failed, try expansion.
 777   result = expand_heap_and_allocate(size, is_tlab);
 778   if (result != NULL) {
 779     return result;
 780   }
 781 
 782   // If we reach this point, we're really out of memory. Try every trick
 783   // we can to reclaim memory. Force collection of soft references. Force
 784   // a complete compaction of the heap. Any additional methods for finding
 785   // free memory should be here, especially if they are expensive. If this
 786   // attempt fails, an OOM exception will be thrown.
 787   {
 788     UIntFlagSetting flag_change(MarkSweepAlwaysCompactCount, 1); // Make sure the heap is fully compacted
 789 
 790     gch->do_collection(true             /* full */,
 791                        true             /* clear_all_soft_refs */,
 792                        size             /* size */,
 793                        is_tlab          /* is_tlab */,
 794                        number_of_generations() - 1 /* max_level */);
 795   }
 796 
 797   result = gch->attempt_allocation(size, is_tlab, false /* first_only */);
 798   if (result != NULL) {
 799     assert(gch->is_in_reserved(result), "result not in heap");
 800     return result;
 801   }
 802 
 803   assert(!should_clear_all_soft_refs(),
 804     "Flag should have been handled and cleared prior to this point");
 805 
 806   // What else?  We might try synchronous finalization later.  If the total
 807   // space available is large enough for the allocation, then a more
 808   // complete compaction phase than we've tried so far might be
 809   // appropriate.
 810   return NULL;
 811 }
 812 
 813 MetaWord* CollectorPolicy::satisfy_failed_metadata_allocation(
 814                                                  ClassLoaderData* loader_data,
 815                                                  size_t word_size,
 816                                                  Metaspace::MetadataType mdtype) {
 817   uint loop_count = 0;
 818   uint gc_count = 0;
 819   uint full_gc_count = 0;
 820 
 821   assert(!Heap_lock->owned_by_self(), "Should not be holding the Heap_lock");
 822 
 823   do {
 824     MetaWord* result = loader_data->metaspace_non_null()->allocate(word_size, mdtype);
 825     if (result != NULL) {
 826       return result;
 827     }
 828 
 829     if (GC_locker::is_active_and_needs_gc()) {
 830       // If the GC_locker is active, just expand and allocate.
 831       // If that does not succeed, wait if this thread is not
 832       // in a critical section itself.
 833       result =
 834         loader_data->metaspace_non_null()->expand_and_allocate(word_size,
 835                                                                mdtype);
 836       if (result != NULL) {
 837         return result;
 838       }
 839       JavaThread* jthr = JavaThread::current();
 840       if (!jthr->in_critical()) {
 841         // Wait for JNI critical section to be exited
 842         GC_locker::stall_until_clear();
 843         // The GC invoked by the last thread leaving the critical
 844         // section will be a young collection and a full collection
 845         // is (currently) needed for unloading classes so continue
 846         // to the next iteration to get a full GC.
 847         continue;
 848       } else {
 849         if (CheckJNICalls) {
 850           fatal("Possible deadlock due to allocating while"
 851                 " in jni critical section");
 852         }
 853         return NULL;
 854       }
 855     }
 856 
 857     {  // Need lock to get self consistent gc_count's
 858       MutexLocker ml(Heap_lock);
 859       gc_count      = Universe::heap()->total_collections();
 860       full_gc_count = Universe::heap()->total_full_collections();
 861     }
 862 
 863     // Generate a VM operation
 864     VM_CollectForMetadataAllocation op(loader_data,
 865                                        word_size,
 866                                        mdtype,
 867                                        gc_count,
 868                                        full_gc_count,
 869                                        GCCause::_metadata_GC_threshold);
 870     VMThread::execute(&op);
 871 
 872     // If GC was locked out, try again. Check before checking success because the
 873     // prologue could have succeeded and the GC still have been locked out.
 874     if (op.gc_locked()) {
 875       continue;
 876     }
 877 
 878     if (op.prologue_succeeded()) {
 879       return op.result();
 880     }
 881     loop_count++;
 882     if ((QueuedAllocationWarningCount > 0) &&
 883         (loop_count % QueuedAllocationWarningCount == 0)) {
 884       warning("satisfy_failed_metadata_allocation() retries %d times \n\t"
 885               " size=" SIZE_FORMAT, loop_count, word_size);
 886     }
 887   } while (true);  // Until a GC is done
 888 }
 889 
 890 // Return true if any of the following is true:
 891 // . the allocation won't fit into the current young gen heap
 892 // . gc locker is occupied (jni critical section)
 893 // . heap memory is tight -- the most recent previous collection
 894 //   was a full collection because a partial collection (would
 895 //   have) failed and is likely to fail again
 896 bool GenCollectorPolicy::should_try_older_generation_allocation(
 897         size_t word_size) const {
 898   GenCollectedHeap* gch = GenCollectedHeap::heap();
 899   size_t young_capacity = gch->young_gen()->capacity_before_gc();
 900   return    (word_size > heap_word_size(young_capacity))
 901          || GC_locker::is_active_and_needs_gc()
 902          || gch->incremental_collection_failed();
 903 }
 904 
 905 
 906 //
 907 // MarkSweepPolicy methods
 908 //
 909 
 910 void MarkSweepPolicy::initialize_alignments() {
 911   _space_alignment = _gen_alignment = (size_t)Generation::GenGrain;
 912   _heap_alignment = compute_heap_alignment();
 913 }
 914 
 915 void MarkSweepPolicy::initialize_generations() {
 916   _young_gen_spec = new GenerationSpec(Generation::DefNew, _initial_young_size, _max_young_size, _gen_alignment);
 917   _old_gen_spec   = new GenerationSpec(Generation::MarkSweepCompact, _initial_old_size, _max_old_size, _gen_alignment);
 918 }
 919 
 920 void MarkSweepPolicy::initialize_gc_policy_counters() {
 921   // Initialize the policy counters - 2 collectors, 3 generations.
 922   _gc_policy_counters = new GCPolicyCounters("Copy:MSC", 2, 3);
 923 }
 924 
 925 /////////////// Unit tests ///////////////
 926 
 927 #ifndef PRODUCT
 928 // Testing that the NewSize flag is handled correct is hard because it
 929 // depends on so many other configurable variables. This test only tries to
 930 // verify that there are some basic rules for NewSize honored by the policies.
 931 class TestGenCollectorPolicy {
 932 public:
 933   static void test_new_size() {
 934     size_t flag_value;
 935 
 936     save_flags();
 937 
 938     // If NewSize is set on the command line, it should be used
 939     // for both min and initial young size if less than min heap.
 940     flag_value = 20 * M;
 941     set_basic_flag_values();
 942     FLAG_SET_CMDLINE(size_t, NewSize, flag_value);
 943     verify_young_min(flag_value);
 944 
 945     set_basic_flag_values();
 946     FLAG_SET_CMDLINE(size_t, NewSize, flag_value);
 947     verify_young_initial(flag_value);
 948 
 949     // If NewSize is set on command line, but is larger than the min
 950     // heap size, it should only be used for initial young size.
 951     flag_value = 80 * M;
 952     set_basic_flag_values();
 953     FLAG_SET_CMDLINE(size_t, NewSize, flag_value);
 954     verify_young_initial(flag_value);
 955 
 956     // If NewSize has been ergonomically set, the collector policy
 957     // should use it for min but calculate the initial young size
 958     // using NewRatio.
 959     flag_value = 20 * M;
 960     set_basic_flag_values();
 961     FLAG_SET_ERGO(size_t, NewSize, flag_value);
 962     verify_young_min(flag_value);
 963 
 964     set_basic_flag_values();
 965     FLAG_SET_ERGO(size_t, NewSize, flag_value);
 966     verify_scaled_young_initial(InitialHeapSize);
 967 
 968     restore_flags();
 969   }
 970 
 971   static void test_old_size() {
 972     size_t flag_value;
 973     size_t heap_alignment = CollectorPolicy::compute_heap_alignment();
 974 
 975     save_flags();
 976 
 977     // If OldSize is set on the command line, it should be used
 978     // for both min and initial old size if less than min heap.
 979     flag_value = 20 * M;
 980     set_basic_flag_values();
 981     FLAG_SET_CMDLINE(size_t, OldSize, flag_value);
 982     verify_old_min(flag_value);
 983 
 984     set_basic_flag_values();
 985     FLAG_SET_CMDLINE(size_t, OldSize, flag_value);
 986     // Calculate what we expect the flag to be.
 987     size_t expected_old_initial = align_size_up(InitialHeapSize, heap_alignment) - MaxNewSize;
 988     verify_old_initial(expected_old_initial);
 989 
 990     // If MaxNewSize is large, the maximum OldSize will be less than
 991     // what's requested on the command line and it should be reset
 992     // ergonomically.
 993     // We intentionally set MaxNewSize + OldSize > MaxHeapSize (see over_size).
 994     flag_value = 30 * M;
 995     set_basic_flag_values();
 996     FLAG_SET_CMDLINE(size_t, OldSize, flag_value);
 997     size_t over_size = 20*M;
 998     size_t new_size_value = align_size_up(MaxHeapSize, heap_alignment) - flag_value + over_size;
 999     FLAG_SET_CMDLINE(size_t, MaxNewSize, new_size_value);
1000     // Calculate what we expect the flag to be.
1001     expected_old_initial = align_size_up(MaxHeapSize, heap_alignment) - MaxNewSize;
1002     verify_old_initial(expected_old_initial);
1003     restore_flags();
1004   }
1005 
1006   static void verify_young_min(size_t expected) {
1007     MarkSweepPolicy msp;
1008     msp.initialize_all();
1009 
1010     assert(msp.min_young_size() <= expected, err_msg("%zu  > %zu", msp.min_young_size(), expected));
1011   }
1012 
1013   static void verify_young_initial(size_t expected) {
1014     MarkSweepPolicy msp;
1015     msp.initialize_all();
1016 
1017     assert(msp.initial_young_size() == expected, err_msg("%zu != %zu", msp.initial_young_size(), expected));
1018   }
1019 
1020   static void verify_scaled_young_initial(size_t initial_heap_size) {
1021     MarkSweepPolicy msp;
1022     msp.initialize_all();
1023 
1024     if (InitialHeapSize > initial_heap_size) {
1025       // InitialHeapSize was adapted by msp.initialize_all, e.g. due to alignment
1026       // caused by 64K page size.
1027       initial_heap_size = InitialHeapSize;
1028     }
1029 
1030     size_t expected = msp.scale_by_NewRatio_aligned(initial_heap_size);
1031     assert(msp.initial_young_size() == expected, err_msg("%zu != %zu", msp.initial_young_size(), expected));
1032     assert(FLAG_IS_ERGO(NewSize) && NewSize == expected,
1033         err_msg("NewSize should have been set ergonomically to %zu, but was %zu", expected, NewSize));
1034   }
1035 
1036   static void verify_old_min(size_t expected) {
1037     MarkSweepPolicy msp;
1038     msp.initialize_all();
1039 
1040     assert(msp.min_old_size() <= expected, err_msg("%zu  > %zu", msp.min_old_size(), expected));
1041   }
1042 
1043   static void verify_old_initial(size_t expected) {
1044     MarkSweepPolicy msp;
1045     msp.initialize_all();
1046 
1047     assert(msp.initial_old_size() == expected, err_msg("%zu != %zu", msp.initial_old_size(), expected));
1048   }
1049 
1050 
1051 private:
1052   static size_t original_InitialHeapSize;
1053   static size_t original_MaxHeapSize;
1054   static size_t original_MaxNewSize;
1055   static size_t original_MinHeapDeltaBytes;
1056   static size_t original_NewSize;
1057   static size_t original_OldSize;
1058 
1059   static void set_basic_flag_values() {
1060     FLAG_SET_ERGO(size_t, MaxHeapSize, 180 * M);
1061     FLAG_SET_ERGO(size_t, InitialHeapSize, 100 * M);
1062     FLAG_SET_ERGO(size_t, OldSize, 4 * M);
1063     FLAG_SET_ERGO(size_t, NewSize, 1 * M);
1064     FLAG_SET_ERGO(size_t, MaxNewSize, 80 * M);
1065     Arguments::set_min_heap_size(40 * M);
1066   }
1067 
1068   static void save_flags() {
1069     original_InitialHeapSize   = InitialHeapSize;
1070     original_MaxHeapSize       = MaxHeapSize;
1071     original_MaxNewSize        = MaxNewSize;
1072     original_MinHeapDeltaBytes = MinHeapDeltaBytes;
1073     original_NewSize           = NewSize;
1074     original_OldSize           = OldSize;
1075   }
1076 
1077   static void restore_flags() {
1078     InitialHeapSize   = original_InitialHeapSize;
1079     MaxHeapSize       = original_MaxHeapSize;
1080     MaxNewSize        = original_MaxNewSize;
1081     MinHeapDeltaBytes = original_MinHeapDeltaBytes;
1082     NewSize           = original_NewSize;
1083     OldSize           = original_OldSize;
1084   }
1085 };
1086 
1087 size_t TestGenCollectorPolicy::original_InitialHeapSize   = 0;
1088 size_t TestGenCollectorPolicy::original_MaxHeapSize       = 0;
1089 size_t TestGenCollectorPolicy::original_MaxNewSize        = 0;
1090 size_t TestGenCollectorPolicy::original_MinHeapDeltaBytes = 0;
1091 size_t TestGenCollectorPolicy::original_NewSize           = 0;
1092 size_t TestGenCollectorPolicy::original_OldSize           = 0;
1093 
1094 void TestNewSize_test() {
1095   TestGenCollectorPolicy::test_new_size();
1096 }
1097 
1098 void TestOldSize_test() {
1099   TestGenCollectorPolicy::test_old_size();
1100 }
1101 
1102 #endif