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