1 /*
   2  * Copyright (c) 2014, 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 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         extends CommandLineOptionTest {
  37     protected static final String RTM_INSTR_ERROR
  38             = "RTM instructions are not available on this CPU";
  39     protected static final String RTM_UNSUPPORTED_VM_ERROR
  40             = "RTM locking optimization is not supported in this VM";
  41     protected static final String RTM_ABORT_RATIO_WARNING
  42             = "RTMAbortRatio must be in the range 0 to 100, resetting it to 50";
  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             = "RTMTotalCountIncrRate 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 predicate predicate responsible for test's preconditions check
  70      * @param optionName name of option to be tested
  71      * @param isBoolean {@code true} if option is binary
  72      * @param isExperimental {@code true} if option is experimental
  73      * @param defaultValue default value of tested option
  74      * @param optionValues different option values
  75      */
  76     public RTMGenericCommandLineOptionTest(BooleanSupplier predicate,
  77             String optionName, boolean isBoolean, boolean isExperimental,
  78             String defaultValue, String... optionValues) {
  79         super(predicate);
  80         this.optionName = optionName;
  81         this.isExperimental = isExperimental;
  82         this.isBoolean = isBoolean;
  83         this.defaultValue = defaultValue;
  84         this.optionValues = optionValues;
  85         this.errorMessage = CommandLineOptionTest.
  86                 getUnrecognizedOptionErrorMessage(optionName);
  87         this.experimentalOptionError = CommandLineOptionTest.
  88                 getExperimentalOptionErrorMessage(optionName);
  89     }
  90 
  91     @Override
  92     public void runTestCases() throws Throwable {
  93         if (Platform.isX86() || Platform.isX64() || Platform.isPPC()) {
  94             if (Platform.isServer()) {
  95                 runX86SupportedVMTestCases();
  96             } else {
  97                 runX86UnsupportedVMTestCases();
  98             }
  99         } else {
 100             runNonX86TestCases();
 101         }
 102     }
 103 
 104     /**
 105      * Runs test cases on X86 CPU if VM supports RTM locking.
 106      * @throws Throwable
 107      */
 108     protected void runX86SupportedVMTestCases() throws Throwable {
 109         runGenericX86TestCases();
 110     }
 111 
 112     /**
 113      * Runs test cases on X86 CPU if VM does not support RTM locking.
 114      * @throws Throwable
 115      */
 116     protected void runX86UnsupportedVMTestCases() throws Throwable {
 117         runGenericX86TestCases();
 118     }
 119 
 120     /**
 121      * Runs test cases on non-X86 CPU.
 122      * @throws Throwable
 123      */
 124     protected void runNonX86TestCases() throws Throwable {
 125         CommandLineOptionTest.verifySameJVMStartup(
 126                 new String[] { errorMessage }, null,
 127                 String.format("Option '%s' should be unknown on non-X86CPUs.%n"
 128                 + "JVM startup should fail", optionName), "", ExitCode.FAIL,
 129                 prepareOptionValue(defaultValue));
 130     }
 131 
 132     /**
 133      * Runs generic X86 test cases.
 134      * @throws Throwable
 135      */
 136     protected void runGenericX86TestCases() throws Throwable {
 137         verifyJVMStartup();
 138         verifyOptionValues();
 139     }
 140 
 141     protected void verifyJVMStartup() throws Throwable {
 142         String optionValue = prepareOptionValue(defaultValue);
 143         String shouldFailMessage = String.format("VM option '%s' is "
 144                 + "experimental.%nVM startup expected to fail without "
 145                 + "-XX:+UnlockExperimentalVMOptions option", optionName);
 146         String shouldPassMessage = String.format("VM option '%s' is "
 147                 + "experimental%nVM startup should pass with "
 148                 + "-XX:+UnlockExperimentalVMOptions option", optionName);
 149         if (isExperimental) {
 150             // verify that option is experimental
 151             CommandLineOptionTest.verifySameJVMStartup(
 152                     new String[] { experimentalOptionError },
 153                     new String[] { errorMessage }, shouldFailMessage,
 154                     shouldFailMessage, ExitCode.FAIL, optionValue);
 155             // verify that it could be passed if experimental options
 156             // are unlocked
 157             CommandLineOptionTest.verifySameJVMStartup(null,
 158                     new String[] {
 159                             experimentalOptionError,
 160                             errorMessage
 161                     },
 162                     shouldPassMessage,
 163                     "JVM should start without any warnings or errors",
 164                     ExitCode.OK,
 165                     CommandLineOptionTest.UNLOCK_EXPERIMENTAL_VM_OPTIONS,
 166                     optionValue);
 167         } else {
 168             // verify that option could be passed
 169             CommandLineOptionTest.verifySameJVMStartup(null,
 170                     new String[]{errorMessage},
 171                     String.format("VM startup shuld pass with '%s' option",
 172                             optionName),
 173                     "JVM should start without any warnings or errors",
 174                     ExitCode.OK, optionValue);
 175         }
 176     }
 177 
 178     protected void verifyOptionValues() throws Throwable {
 179         // verify default value
 180         if (isExperimental) {
 181             CommandLineOptionTest.verifyOptionValueForSameVM(optionName,
 182                     defaultValue,
 183                     String.format("Option '%s' is expected to have '%s' "
 184                             + "default value", optionName, defaultValue),
 185                     CommandLineOptionTest.UNLOCK_EXPERIMENTAL_VM_OPTIONS);
 186         } else {
 187             CommandLineOptionTest.verifyOptionValueForSameVM(optionName,
 188                     defaultValue,
 189                     String.format("Option '%s' is expected to have '%s' "
 190                             + "default value", optionName, defaultValue));
 191         }
 192         // verify other specified option values
 193         if (optionValues == null) {
 194             return;
 195         }
 196 
 197         for (String value : optionValues) {
 198             if (isExperimental) {
 199                 CommandLineOptionTest.verifyOptionValueForSameVM(optionName,
 200                         value,
 201                         String.format("Option '%s' is set to have '%s' value",
 202                                 optionName, value),
 203                         CommandLineOptionTest.UNLOCK_EXPERIMENTAL_VM_OPTIONS,
 204                         prepareOptionValue(value));
 205             } else {
 206                 CommandLineOptionTest.verifyOptionValueForSameVM(optionName,
 207                         value,
 208                         String.format("Option '%s' is set to have '%s' value",
 209                                 optionName, value), prepareOptionValue(value));
 210             }
 211         }
 212     }
 213 
 214     protected String prepareOptionValue(String value) {
 215         if (isBoolean) {
 216             return CommandLineOptionTest.prepareBooleanFlag(optionName,
 217                     Boolean.valueOf(value));
 218         } else {
 219             return String.format("-XX:%s=%s", optionName, value);
 220         }
 221     }
 222 }