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