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