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