1 /*
   2  * Copyright (c) 2015, 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 "runtime/arguments.hpp"
  27 #include "runtime/commandLineFlagConstraintsGC.hpp"
  28 #include "runtime/commandLineFlagRangeList.hpp"
  29 #include "runtime/globals.hpp"
  30 #include "utilities/defaultStream.hpp"
  31 
  32 #if INCLUDE_ALL_GCS
  33 #include "gc/g1/g1_globals.hpp"
  34 #include "gc/g1/heapRegionBounds.inline.hpp"
  35 #include "gc/parallel/parallelScavengeHeap.hpp"
  36 #include "gc/shared/plab.hpp"
  37 #endif // INCLUDE_ALL_GCS
  38 #ifdef COMPILER1
  39 #include "c1/c1_globals.hpp"
  40 #endif // COMPILER1
  41 #ifdef COMPILER2
  42 #include "opto/c2_globals.hpp"
  43 #endif // COMPILER2
  44 
  45 static Flag::Error MinPLABSizeBounds(const char* name, size_t value, bool verbose) {
  46 #if INCLUDE_ALL_GCS
  47   if ((UseConcMarkSweepGC || UseG1GC) && (value < PLAB::min_size())) {
  48     CommandLineError::print(verbose,
  49                             "%s (" SIZE_FORMAT ") must be "
  50                             "greater than or equal to ergonomic PLAB minimum size (" SIZE_FORMAT ")\n",
  51                             name, value, PLAB::min_size());
  52     return Flag::VIOLATES_CONSTRAINT;
  53   }
  54 #endif // INCLUDE_ALL_GCS
  55   return Flag::SUCCESS;
  56 }
  57 
  58 static Flag::Error MaxPLABSizeBounds(const char* name, size_t value, bool verbose) {
  59 #if INCLUDE_ALL_GCS
  60   if ((UseConcMarkSweepGC || UseG1GC) && (value > PLAB::max_size())) {
  61     CommandLineError::print(verbose,
  62                             "%s (" SIZE_FORMAT ") must be "
  63                             "less than or equal to ergonomic PLAB maximum size (" SIZE_FORMAT ")\n",
  64                             name, value, PLAB::max_size());
  65     return Flag::VIOLATES_CONSTRAINT;
  66   }
  67 #endif // INCLUDE_ALL_GCS
  68   return Flag::SUCCESS;
  69 }
  70 
  71 static Flag::Error MinMaxPLABSizeBounds(const char* name, size_t value, bool verbose) {
  72   if (MinPLABSizeBounds(name, value, verbose) == Flag::SUCCESS) {
  73     return MaxPLABSizeBounds(name, value, verbose);
  74   }
  75   return Flag::VIOLATES_CONSTRAINT;
  76 }
  77 
  78 Flag::Error YoungPLABSizeConstraintFunc(size_t value, bool verbose) {
  79   return MinMaxPLABSizeBounds("YoungPLABSize", value, verbose);
  80 }
  81 
  82 Flag::Error MinHeapFreeRatioConstraintFunc(uintx value, bool verbose) {
  83   if (value > MaxHeapFreeRatio) {
  84     CommandLineError::print(verbose,
  85                             "MinHeapFreeRatio (" UINTX_FORMAT ") must be "
  86                             "less than or equal to MaxHeapFreeRatio (" UINTX_FORMAT ")\n",
  87                             value, MaxHeapFreeRatio);
  88     return Flag::VIOLATES_CONSTRAINT;
  89   } else {
  90     return Flag::SUCCESS;
  91   }
  92 }
  93 
  94 Flag::Error MaxHeapFreeRatioConstraintFunc(uintx value, bool verbose) {
  95   if (value < MinHeapFreeRatio) {
  96     CommandLineError::print(verbose,
  97                             "MaxHeapFreeRatio (" UINTX_FORMAT ") must be "
  98                             "greater than or equal to MinHeapFreeRatio (" UINTX_FORMAT ")\n",
  99                             value, MinHeapFreeRatio);
 100     return Flag::VIOLATES_CONSTRAINT;
 101   } else {
 102     return Flag::SUCCESS;
 103   }
 104 }
 105 
 106 Flag::Error MinMetaspaceFreeRatioConstraintFunc(uintx value, bool verbose) {
 107   if (value > MaxMetaspaceFreeRatio) {
 108     CommandLineError::print(verbose,
 109                             "MinMetaspaceFreeRatio (" UINTX_FORMAT ") must be "
 110                             "less than or equal to MaxMetaspaceFreeRatio (" UINTX_FORMAT ")\n",
 111                             value, MaxMetaspaceFreeRatio);
 112     return Flag::VIOLATES_CONSTRAINT;
 113   } else {
 114     return Flag::SUCCESS;
 115   }
 116 }
 117 
 118 Flag::Error MaxMetaspaceFreeRatioConstraintFunc(uintx value, bool verbose) {
 119   if (value < MinMetaspaceFreeRatio) {
 120     CommandLineError::print(verbose,
 121                             "MaxMetaspaceFreeRatio (" UINTX_FORMAT ") must be "
 122                             "greater than or equal to MinMetaspaceFreeRatio (" UINTX_FORMAT ")\n",
 123                             value, MinMetaspaceFreeRatio);
 124     return Flag::VIOLATES_CONSTRAINT;
 125   } else {
 126     return Flag::SUCCESS;
 127   }
 128 }
 129 
 130 // GC workaround for "-XX:+UseConcMarkSweepGC"
 131 // which sets InitialTenuringThreshold to 7 but leaves MaxTenuringThreshold remaining at 6
 132 // and therefore would invalidate the constraint
 133 #define UseConcMarkSweepGCWorkaroundIfNeeded(initial, max) { \
 134   if ((initial == 7) && (max == 6)) { \
 135     return Flag::SUCCESS; \
 136   } \
 137 }
 138 
 139 Flag::Error InitialTenuringThresholdConstraintFunc(uintx value, bool verbose) {
 140   UseConcMarkSweepGCWorkaroundIfNeeded(value, MaxTenuringThreshold);
 141 
 142   if (value > MaxTenuringThreshold) {
 143     CommandLineError::print(verbose,
 144                             "InitialTenuringThreshold (" UINTX_FORMAT ") must be "
 145                             "less than or equal to MaxTenuringThreshold (" UINTX_FORMAT ")\n",
 146                             value, MaxTenuringThreshold);
 147     return Flag::VIOLATES_CONSTRAINT;
 148   } else {
 149     return Flag::SUCCESS;
 150   }
 151 }
 152 
 153 Flag::Error MaxTenuringThresholdConstraintFunc(uintx value, bool verbose) {
 154   UseConcMarkSweepGCWorkaroundIfNeeded(InitialTenuringThreshold, value);
 155 
 156   if (value < InitialTenuringThreshold) {
 157     CommandLineError::print(verbose,
 158                             "MaxTenuringThreshold (" UINTX_FORMAT ") must be "
 159                             "greater than or equal to InitialTenuringThreshold (" UINTX_FORMAT ")\n",
 160                             value, InitialTenuringThreshold);
 161     return Flag::VIOLATES_CONSTRAINT;
 162   } else {
 163     return Flag::SUCCESS;
 164   }
 165 }
 166 
 167 #if INCLUDE_ALL_GCS
 168 Flag::Error G1NewSizePercentConstraintFunc(uintx value, bool verbose) {
 169   if (value > G1MaxNewSizePercent) {
 170     CommandLineError::print(verbose,
 171                             "G1NewSizePercent (" UINTX_FORMAT ") must be "
 172                             "less than or equal to G1MaxNewSizePercent (" UINTX_FORMAT ")\n",
 173                             value, G1MaxNewSizePercent);
 174     return Flag::VIOLATES_CONSTRAINT;
 175   } else {
 176     return Flag::SUCCESS;
 177   }
 178 }
 179 
 180 Flag::Error G1MaxNewSizePercentConstraintFunc(uintx value, bool verbose) {
 181   if (value < G1NewSizePercent) {
 182     CommandLineError::print(verbose,
 183                             "G1MaxNewSizePercent (" UINTX_FORMAT ") must be "
 184                             "greater than or equal to G1NewSizePercent (" UINTX_FORMAT ")\n",
 185                             value, G1NewSizePercent);
 186     return Flag::VIOLATES_CONSTRAINT;
 187   } else {
 188     return Flag::SUCCESS;
 189   }
 190 }
 191 
 192 #endif // INCLUDE_ALL_GCS
 193 
 194 Flag::Error CMSOldPLABMinConstraintFunc(size_t value, bool verbose) {
 195   if (value > CMSOldPLABMax) {
 196     CommandLineError::print(verbose,
 197                             "CMSOldPLABMin (" SIZE_FORMAT ") must be "
 198                             "less than or equal to CMSOldPLABMax (" SIZE_FORMAT ")\n",
 199                             value, CMSOldPLABMax);
 200     return Flag::VIOLATES_CONSTRAINT;
 201   } else {
 202     return Flag::SUCCESS;
 203   }
 204 }
 205 
 206 Flag::Error CMSPrecleanDenominatorConstraintFunc(uintx value, bool verbose) {
 207   if (value <= CMSPrecleanNumerator) {
 208     CommandLineError::print(verbose,
 209                             "CMSPrecleanDenominator (" UINTX_FORMAT ") must be "
 210                             "strickly greater than CMSPrecleanNumerator (" UINTX_FORMAT ")\n",
 211                             value, CMSPrecleanNumerator);
 212     return Flag::VIOLATES_CONSTRAINT;
 213   } else {
 214     return Flag::SUCCESS;
 215   }
 216 }
 217 
 218 Flag::Error CMSPrecleanNumeratorConstraintFunc(uintx value, bool verbose) {
 219   if (value > (CMSPrecleanDenominator - 1)) {
 220     CommandLineError::print(verbose,
 221                             "CMSPrecleanNumerator (" UINTX_FORMAT ") must be "
 222                             "less than or equal to CMSPrecleanDenominator - 1 (" UINTX_FORMAT ")\n",
 223                             value, CMSPrecleanDenominator - 1);
 224     return Flag::VIOLATES_CONSTRAINT;
 225   } else {
 226     return Flag::SUCCESS;
 227   }
 228 }
 229 
 230 Flag::Error SurvivorAlignmentInBytesConstraintFunc(intx value, bool verbose) {
 231   if (value != 0) {
 232     if (!is_power_of_2(value)) {
 233       CommandLineError::print(verbose,
 234                               "SurvivorAlignmentInBytes (" INTX_FORMAT ") must be "
 235                               "power of 2\n",
 236                               value);
 237       return Flag::VIOLATES_CONSTRAINT;
 238     }
 239     if (value < ObjectAlignmentInBytes) {
 240       CommandLineError::print(verbose,
 241                               "SurvivorAlignmentInBytes (" INTX_FORMAT ") must be "
 242                               "greater than or equal to ObjectAlignmentInBytes (" INTX_FORMAT ")\n",
 243                               value, ObjectAlignmentInBytes);
 244       return Flag::VIOLATES_CONSTRAINT;
 245     }
 246   }
 247   return Flag::SUCCESS;
 248 }