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 "utilities/powerOfTwo.hpp"
  36 
  37 JVMFlag::Error AliasLevelConstraintFunc(intx value, bool verbose) {
  38   if ((value <= 1) && (Arguments::mode() == Arguments::_comp || Arguments::mode() == Arguments::_mixed)) {
  39     JVMFlag::printError(verbose,
  40                         "AliasLevel (" INTX_FORMAT ") is not "
  41                         "compatible with -Xcomp or -Xmixed\n",
  42                         value);
  43     return JVMFlag::VIOLATES_CONSTRAINT;
  44   } else {
  45     return JVMFlag::SUCCESS;
  46   }
  47 }
  48 
  49 /**
  50  * Validate the minimum number of compiler threads needed to run the
  51  * JVM. The following configurations are possible.
  52  *
  53  * 1) The JVM is build using an interpreter only. As a result, the minimum number of
  54  *    compiler threads is 0.
  55  * 2) The JVM is build using the compiler(s) and tiered compilation is disabled. As
  56  *    a result, either C1 or C2 is used, so the minimum number of compiler threads is 1.
  57  * 3) The JVM is build using the compiler(s) and tiered compilation is enabled. However,
  58  *    the option "TieredStopAtLevel < CompLevel_full_optimization". As a result, only
  59  *    C1 can be used, so the minimum number of compiler threads is 1.
  60  * 4) The JVM is build using the compilers and tiered compilation is enabled. The option
  61  *    'TieredStopAtLevel = CompLevel_full_optimization' (the default value). As a result,
  62  *    the minimum number of compiler threads is 2.
  63  * 5) Non-tiered emulation mode is on. CompilationModeFlag::disable_intermediate() == true.
  64  *    The minimum number of threads is 2. But if CompilationModeFlag::quick_internal() == false, then it's 1.
  65  */
  66 JVMFlag::Error CICompilerCountConstraintFunc(intx value, bool verbose) {
  67   int min_number_of_compiler_threads = 0;
  68 #if !defined(COMPILER1) && !defined(COMPILER2) && !INCLUDE_JVMCI
  69   // case 1
  70 #elif defined(TIERED)
  71   if (TieredCompilation) {
  72     if (TieredStopAtLevel < CompLevel_full_optimization || CompilationModeFlag::quick_only()) {
  73       min_number_of_compiler_threads = 1; // case 3
  74     } else if (CompilationModeFlag::disable_intermediate()) {
  75       // case 5
  76       if (CompilationModeFlag::quick_internal()) {
  77         min_number_of_compiler_threads = 2;
  78       } else {
  79         min_number_of_compiler_threads = 1;
  80       }
  81     } else {
  82       min_number_of_compiler_threads = 2;   // case 4 (tiered)
  83     }
  84   } else {
  85     min_number_of_compiler_threads = 1; // case 2
  86   }
  87 #else
  88   min_number_of_compiler_threads = 1; // case 2
  89 #endif
  90 
  91   // The default CICompilerCount's value is CI_COMPILER_COUNT.
  92   // With a client VM, -XX:+TieredCompilation causes TieredCompilation
  93   // to be true here (the option is validated later) and
  94   // min_number_of_compiler_threads to exceed CI_COMPILER_COUNT.
  95   min_number_of_compiler_threads = MIN2(min_number_of_compiler_threads, CI_COMPILER_COUNT);
  96 
  97   if (value < (intx)min_number_of_compiler_threads) {
  98     JVMFlag::printError(verbose,
  99                         "CICompilerCount (" INTX_FORMAT ") must be "
 100                         "at least %d \n",
 101                         value, min_number_of_compiler_threads);
 102     return JVMFlag::VIOLATES_CONSTRAINT;
 103   } else {
 104     return JVMFlag::SUCCESS;
 105   }
 106 }
 107 
 108 JVMFlag::Error AllocatePrefetchDistanceConstraintFunc(intx value, bool verbose) {
 109   if (value < 0 || value > 512) {
 110     JVMFlag::printError(verbose,
 111                         "AllocatePrefetchDistance (" INTX_FORMAT ") must be "
 112                         "between 0 and %d\n",
 113                         AllocatePrefetchDistance, 512);
 114     return JVMFlag::VIOLATES_CONSTRAINT;
 115   }
 116 
 117   return JVMFlag::SUCCESS;
 118 }
 119 
 120 JVMFlag::Error AllocatePrefetchStepSizeConstraintFunc(intx value, bool verbose) {
 121   if (AllocatePrefetchStyle == 3) {
 122     if (value % wordSize != 0) {
 123       JVMFlag::printError(verbose,
 124                           "AllocatePrefetchStepSize (" INTX_FORMAT ") must be multiple of %d\n",
 125                           value, wordSize);
 126       return JVMFlag::VIOLATES_CONSTRAINT;
 127     }
 128   }
 129   return JVMFlag::SUCCESS;
 130 }
 131 
 132 JVMFlag::Error AllocatePrefetchInstrConstraintFunc(intx value, bool verbose) {
 133   intx max_value = max_intx;
 134 #if defined(SPARC)
 135   max_value = 1;
 136 #elif 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 CompilerThreadPriorityConstraintFunc(intx value, bool verbose) {
 238 #ifdef SOLARIS
 239   if ((value < MinimumPriority || value > MaximumPriority) &&
 240       (value != -1) && (value != -FXCriticalPriority)) {
 241     JVMFlag::printError(verbose,
 242                         "CompileThreadPriority (" INTX_FORMAT ") must be "
 243                         "between %d and %d inclusively or -1 (means no change) "
 244                         "or %d (special value for critical thread class/priority)\n",
 245                         value, MinimumPriority, MaximumPriority, -FXCriticalPriority);
 246     return JVMFlag::VIOLATES_CONSTRAINT;
 247   }
 248 #endif
 249 
 250   return JVMFlag::SUCCESS;
 251 }
 252 
 253 JVMFlag::Error CodeEntryAlignmentConstraintFunc(intx value, bool verbose) {
 254 #ifdef SPARC
 255   if (CodeEntryAlignment % relocInfo::addr_unit() != 0) {
 256     JVMFlag::printError(verbose,
 257                         "CodeEntryAlignment (" INTX_FORMAT ") must be "
 258                         "multiple of NOP size\n", CodeEntryAlignment);
 259     return JVMFlag::VIOLATES_CONSTRAINT;
 260   }
 261 #endif
 262 
 263   if (!is_power_of_2(value)) {
 264     JVMFlag::printError(verbose,
 265                         "CodeEntryAlignment (" INTX_FORMAT ") must be "
 266                         "a power of two\n", CodeEntryAlignment);
 267     return JVMFlag::VIOLATES_CONSTRAINT;
 268   }
 269 
 270   if (CodeEntryAlignment < 16) {
 271       JVMFlag::printError(verbose,
 272                           "CodeEntryAlignment (" INTX_FORMAT ") must be "
 273                           "greater than or equal to %d\n",
 274                           CodeEntryAlignment, 16);
 275       return JVMFlag::VIOLATES_CONSTRAINT;
 276   }
 277 
 278   return JVMFlag::SUCCESS;
 279 }
 280 
 281 JVMFlag::Error OptoLoopAlignmentConstraintFunc(intx value, bool verbose) {
 282   if (!is_power_of_2(value)) {
 283     JVMFlag::printError(verbose,
 284                         "OptoLoopAlignment (" INTX_FORMAT ") "
 285                         "must be a power of two\n",
 286                         value);
 287     return JVMFlag::VIOLATES_CONSTRAINT;
 288   }
 289 
 290   // Relevant on ppc, s390, sparc. Will be optimized where
 291   // addr_unit() == 1.
 292   if (OptoLoopAlignment % relocInfo::addr_unit() != 0) {
 293     JVMFlag::printError(verbose,
 294                         "OptoLoopAlignment (" INTX_FORMAT ") must be "
 295                         "multiple of NOP size (%d)\n",
 296                         value, relocInfo::addr_unit());
 297     return JVMFlag::VIOLATES_CONSTRAINT;
 298   }
 299 
 300   return JVMFlag::SUCCESS;
 301 }
 302 
 303 JVMFlag::Error ArraycopyDstPrefetchDistanceConstraintFunc(uintx value, bool verbose) {
 304   if (value >= 4032) {
 305     JVMFlag::printError(verbose,
 306                         "ArraycopyDstPrefetchDistance (" UINTX_FORMAT ") must be"
 307                         "between 0 and 4031\n", value);
 308     return JVMFlag::VIOLATES_CONSTRAINT;
 309   }
 310 
 311   return JVMFlag::SUCCESS;
 312 }
 313 
 314 JVMFlag::Error ArraycopySrcPrefetchDistanceConstraintFunc(uintx value, bool verbose) {
 315   if (value >= 4032) {
 316     JVMFlag::printError(verbose,
 317                         "ArraycopySrcPrefetchDistance (" UINTX_FORMAT ") must be"
 318                         "between 0 and 4031\n", value);
 319     return JVMFlag::VIOLATES_CONSTRAINT;
 320   }
 321 
 322   return JVMFlag::SUCCESS;
 323 }
 324 
 325 JVMFlag::Error TypeProfileLevelConstraintFunc(uintx value, bool verbose) {
 326   for (int i = 0; i < 3; i++) {
 327     if (value % 10 > 2) {
 328       JVMFlag::printError(verbose,
 329                           "Invalid value (" UINTX_FORMAT ") "
 330                           "in TypeProfileLevel at position %d\n", value, i);
 331       return JVMFlag::VIOLATES_CONSTRAINT;
 332     }
 333     value = value / 10;
 334   }
 335 
 336   return JVMFlag::SUCCESS;
 337 }
 338 
 339 JVMFlag::Error InitArrayShortSizeConstraintFunc(intx value, bool verbose) {
 340   if (value % BytesPerLong != 0) {
 341     return JVMFlag::VIOLATES_CONSTRAINT;
 342   } else {
 343     return JVMFlag::SUCCESS;
 344   }
 345 }
 346 
 347 #ifdef COMPILER2
 348 JVMFlag::Error InteriorEntryAlignmentConstraintFunc(intx value, bool verbose) {
 349   if (InteriorEntryAlignment > CodeEntryAlignment) {
 350     JVMFlag::printError(verbose,
 351                        "InteriorEntryAlignment (" INTX_FORMAT ") must be "
 352                        "less than or equal to CodeEntryAlignment (" INTX_FORMAT ")\n",
 353                        InteriorEntryAlignment, CodeEntryAlignment);
 354     return JVMFlag::VIOLATES_CONSTRAINT;
 355   }
 356 
 357 #ifdef SPARC
 358   if (InteriorEntryAlignment % relocInfo::addr_unit() != 0) {
 359     JVMFlag::printError(verbose,
 360                         "InteriorEntryAlignment (" INTX_FORMAT ") must be "
 361                         "multiple of NOP size\n");
 362     return JVMFlag::VIOLATES_CONSTRAINT;
 363   }
 364 #endif
 365 
 366   if (!is_power_of_2(value)) {
 367      JVMFlag::printError(verbose,
 368                          "InteriorEntryAlignment (" INTX_FORMAT ") must be "
 369                          "a power of two\n", InteriorEntryAlignment);
 370      return JVMFlag::VIOLATES_CONSTRAINT;
 371    }
 372 
 373   int minimum_alignment = 16;
 374 #if defined(SPARC) || (defined(X86) && !defined(AMD64))
 375   minimum_alignment = 4;
 376 #elif defined(S390)
 377   minimum_alignment = 2;
 378 #endif
 379 
 380   if (InteriorEntryAlignment < minimum_alignment) {
 381     JVMFlag::printError(verbose,
 382                         "InteriorEntryAlignment (" INTX_FORMAT ") must be "
 383                         "greater than or equal to %d\n",
 384                         InteriorEntryAlignment, minimum_alignment);
 385     return JVMFlag::VIOLATES_CONSTRAINT;
 386   }
 387 
 388   return JVMFlag::SUCCESS;
 389 }
 390 
 391 JVMFlag::Error NodeLimitFudgeFactorConstraintFunc(intx value, bool verbose) {
 392   if (value < MaxNodeLimit * 2 / 100 || value > MaxNodeLimit * 40 / 100) {
 393     JVMFlag::printError(verbose,
 394                         "NodeLimitFudgeFactor must be between 2%% and 40%% "
 395                         "of MaxNodeLimit (" INTX_FORMAT ")\n",
 396                         MaxNodeLimit);
 397     return JVMFlag::VIOLATES_CONSTRAINT;
 398   }
 399 
 400   return JVMFlag::SUCCESS;
 401 }
 402 #endif // COMPILER2
 403 
 404 JVMFlag::Error RTMTotalCountIncrRateConstraintFunc(int value, bool verbose) {
 405 #if INCLUDE_RTM_OPT
 406   if (UseRTMLocking && !is_power_of_2(RTMTotalCountIncrRate)) {
 407     JVMFlag::printError(verbose,
 408                         "RTMTotalCountIncrRate (%d) must be "
 409                         "a power of 2, resetting it to 64\n",
 410                         RTMTotalCountIncrRate);
 411     FLAG_SET_DEFAULT(RTMTotalCountIncrRate, 64);
 412   }
 413 #endif
 414 
 415   return JVMFlag::SUCCESS;
 416 }