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