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