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(SPARC)
 136   max_value = 1;
 137 #elif defined(X86)
 138   max_value = 3;
 139 #endif
 140   if (value < 0 || value > max_value) {
 141     JVMFlag::printError(verbose,
 142                         "AllocatePrefetchInstr (" INTX_FORMAT ") must be "
 143                         "between 0 and " INTX_FORMAT "\n", value, max_value);
 144     return JVMFlag::VIOLATES_CONSTRAINT;
 145   }
 146 
 147   return JVMFlag::SUCCESS;
 148 }
 149 
 150 JVMFlag::Error CompileThresholdConstraintFunc(intx value, bool verbose) {
 151   if (value < 0 || value > INT_MAX >> InvocationCounter::count_shift) {
 152     JVMFlag::printError(verbose,
 153                         "CompileThreshold (" INTX_FORMAT ") "
 154                         "must be between 0 and %d\n",
 155                         value,
 156                         INT_MAX >> InvocationCounter::count_shift);
 157     return JVMFlag::VIOLATES_CONSTRAINT;
 158   }
 159 
 160   return JVMFlag::SUCCESS;
 161 }
 162 
 163 JVMFlag::Error OnStackReplacePercentageConstraintFunc(intx value, bool verbose) {
 164   int64_t  max_percentage_limit = INT_MAX;
 165   if (!ProfileInterpreter) {
 166     max_percentage_limit = (max_percentage_limit>>InvocationCounter::count_shift);
 167   }
 168   max_percentage_limit = CompileThreshold == 0  ? max_percentage_limit*100 : max_percentage_limit*100/CompileThreshold;
 169 
 170   if (ProfileInterpreter) {
 171     if (value < InterpreterProfilePercentage) {
 172       JVMFlag::printError(verbose,
 173                           "OnStackReplacePercentage (" INTX_FORMAT ") must be "
 174                           "larger than InterpreterProfilePercentage (" INTX_FORMAT ")\n",
 175                           value, InterpreterProfilePercentage);
 176       return JVMFlag::VIOLATES_CONSTRAINT;
 177     }
 178 
 179     max_percentage_limit += InterpreterProfilePercentage;
 180     if (value > max_percentage_limit) {
 181       JVMFlag::printError(verbose,
 182                           "OnStackReplacePercentage (" INTX_FORMAT ") must be between 0 and " INT64_FORMAT "\n",
 183                           value,
 184                           max_percentage_limit);
 185       return JVMFlag::VIOLATES_CONSTRAINT;
 186     }
 187   } else {
 188     if (value < 0) {
 189       JVMFlag::printError(verbose,
 190                           "OnStackReplacePercentage (" INTX_FORMAT ") must be "
 191                           "non-negative\n", value);
 192       return JVMFlag::VIOLATES_CONSTRAINT;
 193     }
 194 
 195     if (value > max_percentage_limit) {
 196       JVMFlag::printError(verbose,
 197                           "OnStackReplacePercentage (" INTX_FORMAT ") must be between 0 and " INT64_FORMAT "\n",
 198                           value,
 199                           max_percentage_limit);
 200       return JVMFlag::VIOLATES_CONSTRAINT;
 201     }
 202   }
 203   return JVMFlag::SUCCESS;
 204 }
 205 
 206 JVMFlag::Error CodeCacheSegmentSizeConstraintFunc(uintx value, bool verbose) {
 207   if (CodeCacheSegmentSize < (uintx)CodeEntryAlignment) {
 208     JVMFlag::printError(verbose,
 209                         "CodeCacheSegmentSize  (" UINTX_FORMAT ") must be "
 210                         "larger than or equal to CodeEntryAlignment (" INTX_FORMAT ") "
 211                         "to align entry points\n",
 212                         CodeCacheSegmentSize, CodeEntryAlignment);
 213     return JVMFlag::VIOLATES_CONSTRAINT;
 214   }
 215 
 216   if (CodeCacheSegmentSize < sizeof(jdouble)) {
 217     JVMFlag::printError(verbose,
 218                         "CodeCacheSegmentSize  (" UINTX_FORMAT ") must be "
 219                         "at least " SIZE_FORMAT " to align constants\n",
 220                         CodeCacheSegmentSize, sizeof(jdouble));
 221     return JVMFlag::VIOLATES_CONSTRAINT;
 222   }
 223 
 224 #ifdef COMPILER2
 225   if (CodeCacheSegmentSize < (uintx)OptoLoopAlignment) {
 226     JVMFlag::printError(verbose,
 227                         "CodeCacheSegmentSize  (" UINTX_FORMAT ") must be "
 228                         "larger than or equal to OptoLoopAlignment (" INTX_FORMAT ") "
 229                         "to align inner loops\n",
 230                         CodeCacheSegmentSize, OptoLoopAlignment);
 231     return JVMFlag::VIOLATES_CONSTRAINT;
 232   }
 233 #endif
 234 
 235   return JVMFlag::SUCCESS;
 236 }
 237 
 238 JVMFlag::Error CompilerThreadPriorityConstraintFunc(intx value, bool verbose) {
 239 #ifdef SOLARIS
 240   if ((value < MinimumPriority || value > MaximumPriority) &&
 241       (value != -1) && (value != -FXCriticalPriority)) {
 242     JVMFlag::printError(verbose,
 243                         "CompileThreadPriority (" INTX_FORMAT ") must be "
 244                         "between %d and %d inclusively or -1 (means no change) "
 245                         "or %d (special value for critical thread class/priority)\n",
 246                         value, MinimumPriority, MaximumPriority, -FXCriticalPriority);
 247     return JVMFlag::VIOLATES_CONSTRAINT;
 248   }
 249 #endif
 250 
 251   return JVMFlag::SUCCESS;
 252 }
 253 
 254 JVMFlag::Error CodeEntryAlignmentConstraintFunc(intx value, bool verbose) {
 255 #ifdef SPARC
 256   if (CodeEntryAlignment % relocInfo::addr_unit() != 0) {
 257     JVMFlag::printError(verbose,
 258                         "CodeEntryAlignment (" INTX_FORMAT ") must be "
 259                         "multiple of NOP size\n", CodeEntryAlignment);
 260     return JVMFlag::VIOLATES_CONSTRAINT;
 261   }
 262 #endif
 263 
 264   if (!is_power_of_2(value)) {
 265     JVMFlag::printError(verbose,
 266                         "CodeEntryAlignment (" INTX_FORMAT ") must be "
 267                         "a power of two\n", CodeEntryAlignment);
 268     return JVMFlag::VIOLATES_CONSTRAINT;
 269   }
 270 
 271   if (CodeEntryAlignment < 16) {
 272       JVMFlag::printError(verbose,
 273                           "CodeEntryAlignment (" INTX_FORMAT ") must be "
 274                           "greater than or equal to %d\n",
 275                           CodeEntryAlignment, 16);
 276       return JVMFlag::VIOLATES_CONSTRAINT;
 277   }
 278 
 279   return JVMFlag::SUCCESS;
 280 }
 281 
 282 JVMFlag::Error OptoLoopAlignmentConstraintFunc(intx value, bool verbose) {
 283   if (!is_power_of_2(value)) {
 284     JVMFlag::printError(verbose,
 285                         "OptoLoopAlignment (" INTX_FORMAT ") "
 286                         "must be a power of two\n",
 287                         value);
 288     return JVMFlag::VIOLATES_CONSTRAINT;
 289   }
 290 
 291   // Relevant on ppc, s390, sparc. Will be optimized where
 292   // addr_unit() == 1.
 293   if (OptoLoopAlignment % relocInfo::addr_unit() != 0) {
 294     JVMFlag::printError(verbose,
 295                         "OptoLoopAlignment (" INTX_FORMAT ") must be "
 296                         "multiple of NOP size (%d)\n",
 297                         value, relocInfo::addr_unit());
 298     return JVMFlag::VIOLATES_CONSTRAINT;
 299   }
 300 
 301   return JVMFlag::SUCCESS;
 302 }
 303 
 304 JVMFlag::Error ArraycopyDstPrefetchDistanceConstraintFunc(uintx value, bool verbose) {
 305   if (value >= 4032) {
 306     JVMFlag::printError(verbose,
 307                         "ArraycopyDstPrefetchDistance (" UINTX_FORMAT ") must be"
 308                         "between 0 and 4031\n", value);
 309     return JVMFlag::VIOLATES_CONSTRAINT;
 310   }
 311 
 312   return JVMFlag::SUCCESS;
 313 }
 314 
 315 JVMFlag::Error ArraycopySrcPrefetchDistanceConstraintFunc(uintx value, bool verbose) {
 316   if (value >= 4032) {
 317     JVMFlag::printError(verbose,
 318                         "ArraycopySrcPrefetchDistance (" UINTX_FORMAT ") must be"
 319                         "between 0 and 4031\n", value);
 320     return JVMFlag::VIOLATES_CONSTRAINT;
 321   }
 322 
 323   return JVMFlag::SUCCESS;
 324 }
 325 
 326 JVMFlag::Error TypeProfileLevelConstraintFunc(uintx value, bool verbose) {
 327   for (int i = 0; i < 3; i++) {
 328     if (value % 10 > 2) {
 329       JVMFlag::printError(verbose,
 330                           "Invalid value (" UINTX_FORMAT ") "
 331                           "in TypeProfileLevel at position %d\n", value, i);
 332       return JVMFlag::VIOLATES_CONSTRAINT;
 333     }
 334     value = value / 10;
 335   }
 336 
 337   return JVMFlag::SUCCESS;
 338 }
 339 
 340 JVMFlag::Error InitArrayShortSizeConstraintFunc(intx value, bool verbose) {
 341   if (value % BytesPerLong != 0) {
 342     return JVMFlag::VIOLATES_CONSTRAINT;
 343   } else {
 344     return JVMFlag::SUCCESS;
 345   }
 346 }
 347 
 348 #ifdef COMPILER2
 349 JVMFlag::Error InteriorEntryAlignmentConstraintFunc(intx value, bool verbose) {
 350   if (InteriorEntryAlignment > CodeEntryAlignment) {
 351     JVMFlag::printError(verbose,
 352                        "InteriorEntryAlignment (" INTX_FORMAT ") must be "
 353                        "less than or equal to CodeEntryAlignment (" INTX_FORMAT ")\n",
 354                        InteriorEntryAlignment, CodeEntryAlignment);
 355     return JVMFlag::VIOLATES_CONSTRAINT;
 356   }
 357 
 358 #ifdef SPARC
 359   if (InteriorEntryAlignment % relocInfo::addr_unit() != 0) {
 360     JVMFlag::printError(verbose,
 361                         "InteriorEntryAlignment (" INTX_FORMAT ") must be "
 362                         "multiple of NOP size\n");
 363     return JVMFlag::VIOLATES_CONSTRAINT;
 364   }
 365 #endif
 366 
 367   if (!is_power_of_2(value)) {
 368      JVMFlag::printError(verbose,
 369                          "InteriorEntryAlignment (" INTX_FORMAT ") must be "
 370                          "a power of two\n", InteriorEntryAlignment);
 371      return JVMFlag::VIOLATES_CONSTRAINT;
 372    }
 373 
 374   int minimum_alignment = 16;
 375 #if defined(SPARC) || (defined(X86) && !defined(AMD64))
 376   minimum_alignment = 4;
 377 #elif defined(S390)
 378   minimum_alignment = 2;
 379 #endif
 380 
 381   if (InteriorEntryAlignment < minimum_alignment) {
 382     JVMFlag::printError(verbose,
 383                         "InteriorEntryAlignment (" INTX_FORMAT ") must be "
 384                         "greater than or equal to %d\n",
 385                         InteriorEntryAlignment, minimum_alignment);
 386     return JVMFlag::VIOLATES_CONSTRAINT;
 387   }
 388 
 389   return JVMFlag::SUCCESS;
 390 }
 391 
 392 JVMFlag::Error NodeLimitFudgeFactorConstraintFunc(intx value, bool verbose) {
 393   if (value < MaxNodeLimit * 2 / 100 || value > MaxNodeLimit * 40 / 100) {
 394     JVMFlag::printError(verbose,
 395                         "NodeLimitFudgeFactor must be between 2%% and 40%% "
 396                         "of MaxNodeLimit (" INTX_FORMAT ")\n",
 397                         MaxNodeLimit);
 398     return JVMFlag::VIOLATES_CONSTRAINT;
 399   }
 400 
 401   return JVMFlag::SUCCESS;
 402 }
 403 #endif // COMPILER2
 404 
 405 JVMFlag::Error RTMTotalCountIncrRateConstraintFunc(int value, bool verbose) {
 406 #if INCLUDE_RTM_OPT
 407   if (UseRTMLocking && !is_power_of_2(RTMTotalCountIncrRate)) {
 408     JVMFlag::printError(verbose,
 409                         "RTMTotalCountIncrRate (%d) must be "
 410                         "a power of 2, resetting it to 64\n",
 411                         RTMTotalCountIncrRate);
 412     FLAG_SET_DEFAULT(RTMTotalCountIncrRate, 64);
 413   }
 414 #endif
 415 
 416   return JVMFlag::SUCCESS;
 417 }