1 /*
   2  * Copyright (c) 2015, 2018, 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 "code/relocInfo.hpp"
  27 #include "compiler/compilerDefinitions.hpp"
  28 #include "oops/metadata.hpp"
  29 #include "runtime/os.hpp"
  30 #include "interpreter/invocationCounter.hpp"
  31 #include "runtime/arguments.hpp"
  32 #include "runtime/flags/jvmFlag.hpp"
  33 #include "runtime/flags/jvmFlagConstraintsCompiler.hpp"
  34 #include "runtime/globals.hpp"
  35 #include "runtime/globals_extension.hpp"
  36 #include "utilities/powerOfTwo.hpp"
  37 
  38 JVMFlag::Error AliasLevelConstraintFunc(intx value, bool verbose) {
  39   if ((value <= 1) && (Arguments::mode() == Arguments::_comp || Arguments::mode() == Arguments::_mixed)) {
  40     JVMFlag::printError(verbose,
  41                         "AliasLevel (" INTX_FORMAT ") is not "
  42                         "compatible with -Xcomp or -Xmixed\n",
  43                         value);
  44     return JVMFlag::VIOLATES_CONSTRAINT;
  45   } else {
  46     return JVMFlag::SUCCESS;
  47   }
  48 }
  49 
  50 /**
  51  * Validate the minimum number of compiler threads needed to run the
  52  * JVM. The following configurations are possible.
  53  *
  54  * 1) The JVM is build using an interpreter only. As a result, the minimum number of
  55  *    compiler threads is 0.
  56  * 2) The JVM is build using the compiler(s) and tiered compilation is disabled. As
  57  *    a result, either C1 or C2 is used, so the minimum number of compiler threads is 1.
  58  * 3) The JVM is build using the compiler(s) and tiered compilation is enabled. However,
  59  *    the option "TieredStopAtLevel < CompLevel_full_optimization". As a result, only
  60  *    C1 can be used, so the minimum number of compiler threads is 1.
  61  * 4) The JVM is build using the compilers and tiered compilation is enabled. The option
  62  *    'TieredStopAtLevel = CompLevel_full_optimization' (the default value). As a result,
  63  *    the minimum number of compiler threads is 2.
  64  * 5) Non-tiered emulation mode is on. CompilationModeFlag::disable_intermediate() == true.
  65  *    The minimum number of threads is 2. But if CompilationModeFlag::quick_internal() == false, then it's 1.
  66  */
  67 JVMFlag::Error CICompilerCountConstraintFunc(intx value, bool verbose) {
  68   int min_number_of_compiler_threads = 0;
  69 #if !defined(COMPILER1) && !defined(COMPILER2) && !INCLUDE_JVMCI
  70   // case 1
  71 #elif defined(TIERED)
  72   if (TieredCompilation) {
  73     if (TieredStopAtLevel < CompLevel_full_optimization || CompilationModeFlag::quick_only()) {
  74       min_number_of_compiler_threads = 1; // case 3
  75     } else if (CompilationModeFlag::disable_intermediate()) {
  76       // case 5
  77       if (CompilationModeFlag::quick_internal()) {
  78         min_number_of_compiler_threads = 2;
  79       } else {
  80         min_number_of_compiler_threads = 1;
  81       }
  82     } else {
  83       min_number_of_compiler_threads = 2;   // case 4 (tiered)
  84     }
  85   } else {
  86     min_number_of_compiler_threads = 1; // case 2
  87   }
  88 #else
  89   min_number_of_compiler_threads = 1; // case 2
  90 #endif
  91 
  92   // The default CICompilerCount's value is CI_COMPILER_COUNT.
  93   // With a client VM, -XX:+TieredCompilation causes TieredCompilation
  94   // to be true here (the option is validated later) and
  95   // min_number_of_compiler_threads to exceed CI_COMPILER_COUNT.
  96   min_number_of_compiler_threads = MIN2(min_number_of_compiler_threads, CI_COMPILER_COUNT);
  97 
  98   if (value < (intx)min_number_of_compiler_threads) {
  99     JVMFlag::printError(verbose,
 100                         "CICompilerCount (" INTX_FORMAT ") must be "
 101                         "at least %d \n",
 102                         value, min_number_of_compiler_threads);
 103     return JVMFlag::VIOLATES_CONSTRAINT;
 104   } else {
 105     return JVMFlag::SUCCESS;
 106   }
 107 }
 108 
 109 JVMFlag::Error AllocatePrefetchDistanceConstraintFunc(intx value, bool verbose) {
 110   if (value < 0 || value > 512) {
 111     JVMFlag::printError(verbose,
 112                         "AllocatePrefetchDistance (" INTX_FORMAT ") must be "
 113                         "between 0 and %d\n",
 114                         AllocatePrefetchDistance, 512);
 115     return JVMFlag::VIOLATES_CONSTRAINT;
 116   }
 117 
 118   return JVMFlag::SUCCESS;
 119 }
 120 
 121 JVMFlag::Error AllocatePrefetchStepSizeConstraintFunc(intx value, bool verbose) {
 122   if (AllocatePrefetchStyle == 3) {
 123     if (value % wordSize != 0) {
 124       JVMFlag::printError(verbose,
 125                           "AllocatePrefetchStepSize (" INTX_FORMAT ") must be multiple of %d\n",
 126                           value, wordSize);
 127       return JVMFlag::VIOLATES_CONSTRAINT;
 128     }
 129   }
 130   return JVMFlag::SUCCESS;
 131 }
 132 
 133 JVMFlag::Error AllocatePrefetchInstrConstraintFunc(intx value, bool verbose) {
 134   intx max_value = max_intx;
 135 #if defined(X86)
 136   max_value = 3;
 137 #endif
 138   if (value < 0 || value > max_value) {
 139     JVMFlag::printError(verbose,
 140                         "AllocatePrefetchInstr (" INTX_FORMAT ") must be "
 141                         "between 0 and " INTX_FORMAT "\n", value, max_value);
 142     return JVMFlag::VIOLATES_CONSTRAINT;
 143   }
 144 
 145   return JVMFlag::SUCCESS;
 146 }
 147 
 148 JVMFlag::Error CompileThresholdConstraintFunc(intx value, bool verbose) {
 149   if (value < 0 || value > INT_MAX >> InvocationCounter::count_shift) {
 150     JVMFlag::printError(verbose,
 151                         "CompileThreshold (" INTX_FORMAT ") "
 152                         "must be between 0 and %d\n",
 153                         value,
 154                         INT_MAX >> InvocationCounter::count_shift);
 155     return JVMFlag::VIOLATES_CONSTRAINT;
 156   }
 157 
 158   return JVMFlag::SUCCESS;
 159 }
 160 
 161 JVMFlag::Error OnStackReplacePercentageConstraintFunc(intx value, bool verbose) {
 162   int64_t  max_percentage_limit = INT_MAX;
 163   if (!ProfileInterpreter) {
 164     max_percentage_limit = (max_percentage_limit>>InvocationCounter::count_shift);
 165   }
 166   max_percentage_limit = CompileThreshold == 0  ? max_percentage_limit*100 : max_percentage_limit*100/CompileThreshold;
 167 
 168   if (ProfileInterpreter) {
 169     if (value < InterpreterProfilePercentage) {
 170       JVMFlag::printError(verbose,
 171                           "OnStackReplacePercentage (" INTX_FORMAT ") must be "
 172                           "larger than InterpreterProfilePercentage (" INTX_FORMAT ")\n",
 173                           value, InterpreterProfilePercentage);
 174       return JVMFlag::VIOLATES_CONSTRAINT;
 175     }
 176 
 177     max_percentage_limit += InterpreterProfilePercentage;
 178     if (value > max_percentage_limit) {
 179       JVMFlag::printError(verbose,
 180                           "OnStackReplacePercentage (" INTX_FORMAT ") must be between 0 and " INT64_FORMAT "\n",
 181                           value,
 182                           max_percentage_limit);
 183       return JVMFlag::VIOLATES_CONSTRAINT;
 184     }
 185   } else {
 186     if (value < 0) {
 187       JVMFlag::printError(verbose,
 188                           "OnStackReplacePercentage (" INTX_FORMAT ") must be "
 189                           "non-negative\n", value);
 190       return JVMFlag::VIOLATES_CONSTRAINT;
 191     }
 192 
 193     if (value > max_percentage_limit) {
 194       JVMFlag::printError(verbose,
 195                           "OnStackReplacePercentage (" INTX_FORMAT ") must be between 0 and " INT64_FORMAT "\n",
 196                           value,
 197                           max_percentage_limit);
 198       return JVMFlag::VIOLATES_CONSTRAINT;
 199     }
 200   }
 201   return JVMFlag::SUCCESS;
 202 }
 203 
 204 JVMFlag::Error CodeCacheSegmentSizeConstraintFunc(uintx value, bool verbose) {
 205   if (CodeCacheSegmentSize < (uintx)CodeEntryAlignment) {
 206     JVMFlag::printError(verbose,
 207                         "CodeCacheSegmentSize  (" UINTX_FORMAT ") must be "
 208                         "larger than or equal to CodeEntryAlignment (" INTX_FORMAT ") "
 209                         "to align entry points\n",
 210                         CodeCacheSegmentSize, CodeEntryAlignment);
 211     return JVMFlag::VIOLATES_CONSTRAINT;
 212   }
 213 
 214   if (CodeCacheSegmentSize < sizeof(jdouble)) {
 215     JVMFlag::printError(verbose,
 216                         "CodeCacheSegmentSize  (" UINTX_FORMAT ") must be "
 217                         "at least " SIZE_FORMAT " to align constants\n",
 218                         CodeCacheSegmentSize, sizeof(jdouble));
 219     return JVMFlag::VIOLATES_CONSTRAINT;
 220   }
 221 
 222 #ifdef COMPILER2
 223   if (CodeCacheSegmentSize < (uintx)OptoLoopAlignment) {
 224     JVMFlag::printError(verbose,
 225                         "CodeCacheSegmentSize  (" UINTX_FORMAT ") must be "
 226                         "larger than or equal to OptoLoopAlignment (" INTX_FORMAT ") "
 227                         "to align inner loops\n",
 228                         CodeCacheSegmentSize, OptoLoopAlignment);
 229     return JVMFlag::VIOLATES_CONSTRAINT;
 230   }
 231 #endif
 232 
 233   return JVMFlag::SUCCESS;
 234 }
 235 
 236 JVMFlag::Error CodeEntryAlignmentConstraintFunc(intx value, bool verbose) {
 237   if (!is_power_of_2(value)) {
 238     JVMFlag::printError(verbose,
 239                         "CodeEntryAlignment (" INTX_FORMAT ") must be "
 240                         "a power of two\n", CodeEntryAlignment);
 241     return JVMFlag::VIOLATES_CONSTRAINT;
 242   }
 243 
 244   if (CodeEntryAlignment < 16) {
 245       JVMFlag::printError(verbose,
 246                           "CodeEntryAlignment (" INTX_FORMAT ") must be "
 247                           "greater than or equal to %d\n",
 248                           CodeEntryAlignment, 16);
 249       return JVMFlag::VIOLATES_CONSTRAINT;
 250   }
 251 
 252   return JVMFlag::SUCCESS;
 253 }
 254 
 255 JVMFlag::Error OptoLoopAlignmentConstraintFunc(intx value, bool verbose) {
 256   if (!is_power_of_2(value)) {
 257     JVMFlag::printError(verbose,
 258                         "OptoLoopAlignment (" INTX_FORMAT ") "
 259                         "must be a power of two\n",
 260                         value);
 261     return JVMFlag::VIOLATES_CONSTRAINT;
 262   }
 263 
 264   // Relevant on ppc, s390. Will be optimized where
 265   // addr_unit() == 1.
 266   if (OptoLoopAlignment % relocInfo::addr_unit() != 0) {
 267     JVMFlag::printError(verbose,
 268                         "OptoLoopAlignment (" INTX_FORMAT ") must be "
 269                         "multiple of NOP size (%d)\n",
 270                         value, relocInfo::addr_unit());
 271     return JVMFlag::VIOLATES_CONSTRAINT;
 272   }
 273 
 274   return JVMFlag::SUCCESS;
 275 }
 276 
 277 JVMFlag::Error ArraycopyDstPrefetchDistanceConstraintFunc(uintx value, bool verbose) {
 278   if (value >= 4032) {
 279     JVMFlag::printError(verbose,
 280                         "ArraycopyDstPrefetchDistance (" UINTX_FORMAT ") must be"
 281                         "between 0 and 4031\n", value);
 282     return JVMFlag::VIOLATES_CONSTRAINT;
 283   }
 284 
 285   return JVMFlag::SUCCESS;
 286 }
 287 
 288 JVMFlag::Error ArraycopySrcPrefetchDistanceConstraintFunc(uintx value, bool verbose) {
 289   if (value >= 4032) {
 290     JVMFlag::printError(verbose,
 291                         "ArraycopySrcPrefetchDistance (" UINTX_FORMAT ") must be"
 292                         "between 0 and 4031\n", value);
 293     return JVMFlag::VIOLATES_CONSTRAINT;
 294   }
 295 
 296   return JVMFlag::SUCCESS;
 297 }
 298 
 299 JVMFlag::Error TypeProfileLevelConstraintFunc(uintx value, bool verbose) {
 300   for (int i = 0; i < 3; i++) {
 301     if (value % 10 > 2) {
 302       JVMFlag::printError(verbose,
 303                           "Invalid value (" UINTX_FORMAT ") "
 304                           "in TypeProfileLevel at position %d\n", value, i);
 305       return JVMFlag::VIOLATES_CONSTRAINT;
 306     }
 307     value = value / 10;
 308   }
 309 
 310   return JVMFlag::SUCCESS;
 311 }
 312 
 313 JVMFlag::Error InitArrayShortSizeConstraintFunc(intx value, bool verbose) {
 314   if (value % BytesPerLong != 0) {
 315     return JVMFlag::VIOLATES_CONSTRAINT;
 316   } else {
 317     return JVMFlag::SUCCESS;
 318   }
 319 }
 320 
 321 #ifdef COMPILER2
 322 JVMFlag::Error InteriorEntryAlignmentConstraintFunc(intx value, bool verbose) {
 323   if (InteriorEntryAlignment > CodeEntryAlignment) {
 324     JVMFlag::printError(verbose,
 325                        "InteriorEntryAlignment (" INTX_FORMAT ") must be "
 326                        "less than or equal to CodeEntryAlignment (" INTX_FORMAT ")\n",
 327                        InteriorEntryAlignment, CodeEntryAlignment);
 328     return JVMFlag::VIOLATES_CONSTRAINT;
 329   }
 330 
 331   if (!is_power_of_2(value)) {
 332      JVMFlag::printError(verbose,
 333                          "InteriorEntryAlignment (" INTX_FORMAT ") must be "
 334                          "a power of two\n", InteriorEntryAlignment);
 335      return JVMFlag::VIOLATES_CONSTRAINT;
 336    }
 337 
 338   int minimum_alignment = 16;
 339 #if defined(X86) && !defined(AMD64)
 340   minimum_alignment = 4;
 341 #elif defined(S390)
 342   minimum_alignment = 2;
 343 #endif
 344 
 345   if (InteriorEntryAlignment < minimum_alignment) {
 346     JVMFlag::printError(verbose,
 347                         "InteriorEntryAlignment (" INTX_FORMAT ") must be "
 348                         "greater than or equal to %d\n",
 349                         InteriorEntryAlignment, minimum_alignment);
 350     return JVMFlag::VIOLATES_CONSTRAINT;
 351   }
 352 
 353   return JVMFlag::SUCCESS;
 354 }
 355 
 356 JVMFlag::Error NodeLimitFudgeFactorConstraintFunc(intx value, bool verbose) {
 357   if (value < MaxNodeLimit * 2 / 100 || value > MaxNodeLimit * 40 / 100) {
 358     JVMFlag::printError(verbose,
 359                         "NodeLimitFudgeFactor must be between 2%% and 40%% "
 360                         "of MaxNodeLimit (" INTX_FORMAT ")\n",
 361                         MaxNodeLimit);
 362     return JVMFlag::VIOLATES_CONSTRAINT;
 363   }
 364 
 365   return JVMFlag::SUCCESS;
 366 }
 367 #endif // COMPILER2
 368 
 369 JVMFlag::Error RTMTotalCountIncrRateConstraintFunc(int value, bool verbose) {
 370 #if INCLUDE_RTM_OPT
 371   if (UseRTMLocking && !is_power_of_2(RTMTotalCountIncrRate)) {
 372     JVMFlag::printError(verbose,
 373                         "RTMTotalCountIncrRate (%d) must be "
 374                         "a power of 2, resetting it to 64\n",
 375                         RTMTotalCountIncrRate);
 376     FLAG_SET_DEFAULT(RTMTotalCountIncrRate, 64);
 377   }
 378 #endif
 379 
 380   return JVMFlag::SUCCESS;
 381 }