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