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