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 CompilerThreadPriorityConstraintFunc(intx value, bool verbose) {
 237   return JVMFlag::SUCCESS;
 238 }
 239 
 240 JVMFlag::Error CodeEntryAlignmentConstraintFunc(intx value, bool verbose) {
 241   if (!is_power_of_2(value)) {
 242     JVMFlag::printError(verbose,
 243                         "CodeEntryAlignment (" INTX_FORMAT ") must be "
 244                         "a power of two\n", CodeEntryAlignment);
 245     return JVMFlag::VIOLATES_CONSTRAINT;
 246   }
 247 
 248   if (CodeEntryAlignment < 16) {
 249       JVMFlag::printError(verbose,
 250                           "CodeEntryAlignment (" INTX_FORMAT ") must be "
 251                           "greater than or equal to %d\n",
 252                           CodeEntryAlignment, 16);
 253       return JVMFlag::VIOLATES_CONSTRAINT;
 254   }
 255 
 256   return JVMFlag::SUCCESS;
 257 }
 258 
 259 JVMFlag::Error OptoLoopAlignmentConstraintFunc(intx value, bool verbose) {
 260   if (!is_power_of_2(value)) {
 261     JVMFlag::printError(verbose,
 262                         "OptoLoopAlignment (" INTX_FORMAT ") "
 263                         "must be a power of two\n",
 264                         value);
 265     return JVMFlag::VIOLATES_CONSTRAINT;
 266   }
 267 
 268   // Relevant on ppc, s390. Will be optimized where
 269   // addr_unit() == 1.
 270   if (OptoLoopAlignment % relocInfo::addr_unit() != 0) {
 271     JVMFlag::printError(verbose,
 272                         "OptoLoopAlignment (" INTX_FORMAT ") must be "
 273                         "multiple of NOP size (%d)\n",
 274                         value, relocInfo::addr_unit());
 275     return JVMFlag::VIOLATES_CONSTRAINT;
 276   }
 277 
 278   return JVMFlag::SUCCESS;
 279 }
 280 
 281 JVMFlag::Error ArraycopyDstPrefetchDistanceConstraintFunc(uintx value, bool verbose) {
 282   if (value >= 4032) {
 283     JVMFlag::printError(verbose,
 284                         "ArraycopyDstPrefetchDistance (" UINTX_FORMAT ") must be"
 285                         "between 0 and 4031\n", value);
 286     return JVMFlag::VIOLATES_CONSTRAINT;
 287   }
 288 
 289   return JVMFlag::SUCCESS;
 290 }
 291 
 292 JVMFlag::Error ArraycopySrcPrefetchDistanceConstraintFunc(uintx value, bool verbose) {
 293   if (value >= 4032) {
 294     JVMFlag::printError(verbose,
 295                         "ArraycopySrcPrefetchDistance (" UINTX_FORMAT ") must be"
 296                         "between 0 and 4031\n", value);
 297     return JVMFlag::VIOLATES_CONSTRAINT;
 298   }
 299 
 300   return JVMFlag::SUCCESS;
 301 }
 302 
 303 JVMFlag::Error TypeProfileLevelConstraintFunc(uintx value, bool verbose) {
 304   for (int i = 0; i < 3; i++) {
 305     if (value % 10 > 2) {
 306       JVMFlag::printError(verbose,
 307                           "Invalid value (" UINTX_FORMAT ") "
 308                           "in TypeProfileLevel at position %d\n", value, i);
 309       return JVMFlag::VIOLATES_CONSTRAINT;
 310     }
 311     value = value / 10;
 312   }
 313 
 314   return JVMFlag::SUCCESS;
 315 }
 316 
 317 JVMFlag::Error InitArrayShortSizeConstraintFunc(intx value, bool verbose) {
 318   if (value % BytesPerLong != 0) {
 319     return JVMFlag::VIOLATES_CONSTRAINT;
 320   } else {
 321     return JVMFlag::SUCCESS;
 322   }
 323 }
 324 
 325 #ifdef COMPILER2
 326 JVMFlag::Error InteriorEntryAlignmentConstraintFunc(intx value, bool verbose) {
 327   if (InteriorEntryAlignment > CodeEntryAlignment) {
 328     JVMFlag::printError(verbose,
 329                        "InteriorEntryAlignment (" INTX_FORMAT ") must be "
 330                        "less than or equal to CodeEntryAlignment (" INTX_FORMAT ")\n",
 331                        InteriorEntryAlignment, CodeEntryAlignment);
 332     return JVMFlag::VIOLATES_CONSTRAINT;
 333   }
 334 
 335   if (!is_power_of_2(value)) {
 336      JVMFlag::printError(verbose,
 337                          "InteriorEntryAlignment (" INTX_FORMAT ") must be "
 338                          "a power of two\n", InteriorEntryAlignment);
 339      return JVMFlag::VIOLATES_CONSTRAINT;
 340    }
 341 
 342   int minimum_alignment = 16;
 343 #if defined(X86) && !defined(AMD64)
 344   minimum_alignment = 4;
 345 #elif defined(S390)
 346   minimum_alignment = 2;
 347 #endif
 348 
 349   if (InteriorEntryAlignment < minimum_alignment) {
 350     JVMFlag::printError(verbose,
 351                         "InteriorEntryAlignment (" INTX_FORMAT ") must be "
 352                         "greater than or equal to %d\n",
 353                         InteriorEntryAlignment, minimum_alignment);
 354     return JVMFlag::VIOLATES_CONSTRAINT;
 355   }
 356 
 357   return JVMFlag::SUCCESS;
 358 }
 359 
 360 JVMFlag::Error NodeLimitFudgeFactorConstraintFunc(intx value, bool verbose) {
 361   if (value < MaxNodeLimit * 2 / 100 || value > MaxNodeLimit * 40 / 100) {
 362     JVMFlag::printError(verbose,
 363                         "NodeLimitFudgeFactor must be between 2%% and 40%% "
 364                         "of MaxNodeLimit (" INTX_FORMAT ")\n",
 365                         MaxNodeLimit);
 366     return JVMFlag::VIOLATES_CONSTRAINT;
 367   }
 368 
 369   return JVMFlag::SUCCESS;
 370 }
 371 #endif // COMPILER2
 372 
 373 JVMFlag::Error RTMTotalCountIncrRateConstraintFunc(int value, bool verbose) {
 374 #if INCLUDE_RTM_OPT
 375   if (UseRTMLocking && !is_power_of_2(RTMTotalCountIncrRate)) {
 376     JVMFlag::printError(verbose,
 377                         "RTMTotalCountIncrRate (%d) must be "
 378                         "a power of 2, resetting it to 64\n",
 379                         RTMTotalCountIncrRate);
 380     FLAG_SET_DEFAULT(RTMTotalCountIncrRate, 64);
 381   }
 382 #endif
 383 
 384   return JVMFlag::SUCCESS;
 385 }