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