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