1 /*
   2  * Copyright (c) 2014, 2015, 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 import jdk.test.lib.*;
  26 import jdk.test.lib.cli.*;
  27 
  28 import java.util.function.BooleanSupplier;
  29 
  30 /**
  31  * Base for all RTM-related CLI tests.
  32  */
  33 public abstract class RTMGenericCommandLineOptionTest
  34         extends CommandLineOptionTest {
  35     protected static final String RTM_INSTR_ERROR
  36             = "RTM instructions are not available on this CPU";
  37     protected static final String RTM_UNSUPPORTED_VM_ERROR
  38             = "RTM locking optimization is not supported in this VM";
  39     protected static final String RTM_ABORT_RATIO_WARNING
  40             = "RTMAbortRatio must be in the range 0 to 100, resetting it to 50";
  41     protected static final String RTM_FOR_STACK_LOCKS_WARNING
  42             = "UseRTMForStackLocks flag should be off when UseRTMLocking "
  43             + "flag is off";
  44     protected static final String RTM_COUNT_INCR_WARNING
  45             = "RTMTotalCountIncrRate must be a power of 2, resetting it to 64";
  46     protected static final String RTM_BIASED_LOCKING_WARNING
  47             = "Biased locking is not supported with RTM locking; "
  48             + "ignoring UseBiasedLocking flag";
  49 
  50     protected final String optionName;
  51     protected final String errorMessage;
  52     protected final String experimentalOptionError;
  53     protected final boolean isExperimental;
  54     protected final boolean isBoolean;
  55     protected final String defaultValue;
  56     protected final String[] optionValues;
  57 
  58     /**
  59      * Constructs new genetic RTM CLI test, for option {@code optionName} which
  60      * has default value {@code defaultValue}. Test cases will use option's
  61      * values passed via {@code optionValues} for verification of correct
  62      * option processing.
  63      *
  64      * Test constructed using this ctor will be started on any cpu regardless
  65      * it's architecture and supported/unsupported features.
  66      *
  67      * @param predicate predicate responsible for test's preconditions check
  68      * @param optionName name of option to be tested
  69      * @param isBoolean {@code true} if option is binary
  70      * @param isExperimental {@code true} if option is experimental
  71      * @param defaultValue default value of tested option
  72      * @param optionValues different option values
  73      */
  74     public RTMGenericCommandLineOptionTest(BooleanSupplier predicate,
  75             String optionName, boolean isBoolean, boolean isExperimental,
  76             String defaultValue, String... optionValues) {
  77         super(predicate);
  78         this.optionName = optionName;
  79         this.isExperimental = isExperimental;
  80         this.isBoolean = isBoolean;
  81         this.defaultValue = defaultValue;
  82         this.optionValues = optionValues;
  83         this.errorMessage = CommandLineOptionTest.
  84                 getUnrecognizedOptionErrorMessage(optionName);
  85         this.experimentalOptionError = CommandLineOptionTest.
  86                 getExperimentalOptionErrorMessage(optionName);
  87     }
  88 
  89     @Override
  90     public void runTestCases() throws Throwable {
  91         if (Platform.isX86() || Platform.isX64()) {
  92             if (Platform.isServer() && !Platform.isEmbedded()) {
  93                 runX86SupportedVMTestCases();
  94             } else {
  95                 runX86UnsupportedVMTestCases();
  96             }
  97         } else {
  98             runNonX86TestCases();
  99         }
 100     }
 101 
 102     /**
 103      * Runs test cases on X86 CPU if VM supports RTM locking.
 104      * @throws Throwable
 105      */
 106     protected void runX86SupportedVMTestCases() throws Throwable {
 107         runGenericX86TestCases();
 108     }
 109 
 110     /**
 111      * Runs test cases on non-X86 CPU if VM does not support RTM locking.
 112      * @throws Throwable
 113      */
 114     protected void runX86UnsupportedVMTestCases() throws Throwable {
 115         runGenericX86TestCases();
 116     }
 117 
 118     /**
 119      * Runs test cases on non-X86 CPU.
 120      * @throws Throwable
 121      */
 122     protected void runNonX86TestCases() throws Throwable {
 123         CommandLineOptionTest.verifySameJVMStartup(
 124                 new String[] { errorMessage }, null,
 125                 String.format("Option '%s' should be unknown on non-X86CPUs.%n"
 126                 + "JVM startup should fail", optionName), "", ExitCode.FAIL,
 127                 prepareOptionValue(defaultValue));
 128     }
 129 
 130     /**
 131      * Runs generic X86 test cases.
 132      * @throws Throwable
 133      */
 134     protected void runGenericX86TestCases() throws Throwable {
 135         verifyJVMStartup();
 136         verifyOptionValues();
 137     }
 138 
 139     protected void verifyJVMStartup() throws Throwable {
 140         String optionValue = prepareOptionValue(defaultValue);
 141         String shouldFailMessage = String.format("VM option '%s' is "
 142                 + "experimental.%nVM startup expected to fail without "
 143                 + "-XX:+UnlockExperimentalVMOptions option", optionName);
 144         String shouldPassMessage = String.format("VM option '%s' is "
 145                 + "experimental%nVM startup should pass with "
 146                 + "-XX:+UnlockExperimentalVMOptions option", optionName);
 147         if (isExperimental) {
 148             // verify that option is experimental
 149             CommandLineOptionTest.verifySameJVMStartup(
 150                     new String[] { experimentalOptionError },
 151                     new String[] { errorMessage }, shouldFailMessage,
 152                     shouldFailMessage, ExitCode.FAIL, optionValue);
 153             // verify that it could be passed if experimental options
 154             // are unlocked
 155             CommandLineOptionTest.verifySameJVMStartup(null,
 156                     new String[] {
 157                             experimentalOptionError,
 158                             errorMessage
 159                     },
 160                     shouldPassMessage,
 161                     "JVM should start without any warnings or errors",
 162                     ExitCode.OK,
 163                     CommandLineOptionTest.UNLOCK_EXPERIMENTAL_VM_OPTIONS,
 164                     optionValue);
 165         } else {
 166             // verify that option could be passed
 167             CommandLineOptionTest.verifySameJVMStartup(null,
 168                     new String[]{errorMessage},
 169                     String.format("VM startup shuld pass with '%s' option",
 170                             optionName),
 171                     "JVM should start without any warnings or errors",
 172                     ExitCode.OK, optionValue);
 173         }
 174     }
 175 
 176     protected void verifyOptionValues() throws Throwable {
 177         // verify default value
 178         if (isExperimental) {
 179             CommandLineOptionTest.verifyOptionValueForSameVM(optionName,
 180                     defaultValue,
 181                     String.format("Option '%s' is expected to have '%s' "
 182                             + "default value", optionName, defaultValue),
 183                     CommandLineOptionTest.UNLOCK_EXPERIMENTAL_VM_OPTIONS);
 184         } else {
 185             CommandLineOptionTest.verifyOptionValueForSameVM(optionName,
 186                     defaultValue,
 187                     String.format("Option '%s' is expected to have '%s' "
 188                             + "default value", optionName, defaultValue));
 189         }
 190         // verify other specified option values
 191         if (optionValues == null) {
 192             return;
 193         }
 194 
 195         for (String value : optionValues) {
 196             if (isExperimental) {
 197                 CommandLineOptionTest.verifyOptionValueForSameVM(optionName,
 198                         value,
 199                         String.format("Option '%s' is set to have '%s' value",
 200                                 optionName, value),
 201                         CommandLineOptionTest.UNLOCK_EXPERIMENTAL_VM_OPTIONS,
 202                         prepareOptionValue(value));
 203             } else {
 204                 CommandLineOptionTest.verifyOptionValueForSameVM(optionName,
 205                         value,
 206                         String.format("Option '%s' is set to have '%s' value",
 207                                 optionName, value), prepareOptionValue(value));
 208             }
 209         }
 210     }
 211 
 212     protected String prepareOptionValue(String value) {
 213         if (isBoolean) {
 214             return CommandLineOptionTest.prepareBooleanFlag(optionName,
 215                     Boolean.valueOf(value));
 216         } else {
 217             return String.format("-XX:%s=%s", optionName, value);
 218         }
 219     }
 220 }