1 /*
   2  * Copyright (c) 2016, 2019, 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/g1/g1Arguments.hpp"
  27 #include "gc/g1/g1HeterogeneousHeapYoungGenSizer.hpp"
  28 #include "gc/g1/g1YoungGenSizer.hpp"
  29 #include "gc/g1/heapRegion.hpp"
  30 #include "logging/log.hpp"
  31 #include "runtime/globals_extension.hpp"
  32 
  33 G1YoungGenSizer::G1YoungGenSizer() : _sizer_kind(SizerDefaults),
  34   _use_adaptive_sizing(true), _min_desired_young_length(0), _max_desired_young_length(0) {
  35 
  36   if (FLAG_IS_CMDLINE(NewRatio)) {
  37     if (FLAG_IS_CMDLINE(NewSize) || FLAG_IS_CMDLINE(MaxNewSize)) {
  38       log_warning(gc, ergo)("-XX:NewSize and -XX:MaxNewSize override -XX:NewRatio");
  39     } else {
  40       _sizer_kind = SizerNewRatio;
  41       _use_adaptive_sizing = false;
  42       return;
  43     }
  44   }
  45 
  46   if (NewSize > MaxNewSize) {
  47     if (FLAG_IS_CMDLINE(MaxNewSize)) {
  48       log_warning(gc, ergo)("NewSize (" SIZE_FORMAT "k) is greater than the MaxNewSize (" SIZE_FORMAT "k). "
  49                             "A new max generation size of " SIZE_FORMAT "k will be used.",
  50                             NewSize/K, MaxNewSize/K, NewSize/K);
  51     }
  52     FLAG_SET_ERGO(MaxNewSize, NewSize);
  53   }
  54 
  55   if (FLAG_IS_CMDLINE(NewSize)) {
  56     _min_desired_young_length = MAX2((uint) (NewSize / HeapRegion::GrainBytes),
  57                                      1U);
  58     if (FLAG_IS_CMDLINE(MaxNewSize)) {
  59       _max_desired_young_length =
  60                              MAX2((uint) (MaxNewSize / HeapRegion::GrainBytes),
  61                                   1U);
  62       _sizer_kind = SizerMaxAndNewSize;
  63       _use_adaptive_sizing = _min_desired_young_length != _max_desired_young_length;
  64     } else {
  65       _sizer_kind = SizerNewSizeOnly;
  66     }
  67   } else if (FLAG_IS_CMDLINE(MaxNewSize)) {
  68     _max_desired_young_length =
  69                              MAX2((uint) (MaxNewSize / HeapRegion::GrainBytes),
  70                                   1U);
  71     _sizer_kind = SizerMaxNewSizeOnly;
  72   }
  73 }
  74 
  75 uint G1YoungGenSizer::calculate_default_min_length(uint new_number_of_heap_regions) {
  76   uint default_value = (new_number_of_heap_regions * G1NewSizePercent) / 100;
  77   return MAX2(1U, default_value);
  78 }
  79 
  80 uint G1YoungGenSizer::calculate_default_max_length(uint new_number_of_heap_regions) {
  81   uint default_value = (new_number_of_heap_regions * G1MaxNewSizePercent) / 100;
  82   return MAX2(1U, default_value);
  83 }
  84 
  85 void G1YoungGenSizer::recalculate_min_max_young_length(uint number_of_heap_regions, uint* min_young_length, uint* max_young_length) {
  86   assert(number_of_heap_regions > 0, "Heap must be initialized");
  87 
  88   switch (_sizer_kind) {
  89     case SizerDefaults:
  90       *min_young_length = calculate_default_min_length(number_of_heap_regions);
  91       *max_young_length = calculate_default_max_length(number_of_heap_regions);
  92       break;
  93     case SizerNewSizeOnly:
  94       *max_young_length = calculate_default_max_length(number_of_heap_regions);
  95       *max_young_length = MAX2(*min_young_length, *max_young_length);
  96       break;
  97     case SizerMaxNewSizeOnly:
  98       *min_young_length = calculate_default_min_length(number_of_heap_regions);
  99       *min_young_length = MIN2(*min_young_length, *max_young_length);
 100       break;
 101     case SizerMaxAndNewSize:
 102       // Do nothing. Values set on the command line, don't update them at runtime.
 103       break;
 104     case SizerNewRatio:
 105       *min_young_length = number_of_heap_regions / (NewRatio + 1);
 106       *max_young_length = *min_young_length;
 107       break;
 108     default:
 109       ShouldNotReachHere();
 110   }
 111 
 112   assert(*min_young_length <= *max_young_length, "Invalid min/max young gen size values");
 113 }
 114 
 115 void G1YoungGenSizer::adjust_max_new_size(uint number_of_heap_regions) {
 116 
 117   // We need to pass the desired values because recalculation may not update these
 118   // values in some cases.
 119   uint temp = _min_desired_young_length;
 120   uint result = _max_desired_young_length;
 121   recalculate_min_max_young_length(number_of_heap_regions, &temp, &result);
 122 
 123   size_t max_young_size = result * HeapRegion::GrainBytes;
 124   if (max_young_size != MaxNewSize) {
 125     FLAG_SET_ERGO(MaxNewSize, max_young_size);
 126   }
 127 }
 128 
 129 void G1YoungGenSizer::heap_size_changed(uint new_number_of_heap_regions) {
 130   recalculate_min_max_young_length(new_number_of_heap_regions, &_min_desired_young_length,
 131           &_max_desired_young_length);
 132 }
 133 
 134 G1YoungGenSizer* G1YoungGenSizer::create_gen_sizer() {
 135   if (G1Arguments::is_heterogeneous_heap()) {
 136     return new G1HeterogeneousHeapYoungGenSizer();
 137   } else {
 138     return new G1YoungGenSizer();
 139   }
 140 }