1 /*
   2  * Copyright (c) 2014, 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 com.oracle.java.testlibrary.*;
  26 import com.oracle.java.testlibrary.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, ExitCode.FAIL,
 125                 prepareOptionValue(defaultValue));
 126     }
 127 
 128     /**
 129      * Runs generic X86 test cases.
 130      * @throws Throwable
 131      */
 132     protected void runGenericX86TestCases() throws Throwable {
 133         verifyJVMStartup();
 134         verifyOptionValues();
 135     }
 136 
 137     protected void verifyJVMStartup() throws Throwable {
 138         String optionValue = prepareOptionValue(defaultValue);
 139         if (isExperimental) {
 140             // verify that option is experimental
 141             CommandLineOptionTest.verifySameJVMStartup(
 142                     new String[] { experimentalOptionError },
 143                     new String[] { errorMessage }, ExitCode.FAIL,
 144                     optionValue);
 145             // verify that it could be passed if experimental options
 146             // are unlocked
 147             CommandLineOptionTest.verifySameJVMStartup(null,
 148                     new String[] {
 149                             experimentalOptionError,
 150                             errorMessage
 151                     },
 152                     ExitCode.OK,
 153                     CommandLineOptionTest.UNLOCK_EXPERIMENTAL_VM_OPTIONS,
 154                     optionValue);
 155         } else {
 156             // verify that option could be passed
 157             CommandLineOptionTest.verifySameJVMStartup(null,
 158                     new String[]{errorMessage}, ExitCode.OK, optionValue);
 159         }
 160     }
 161 
 162     protected void verifyOptionValues() throws Throwable {
 163         // verify default value
 164         if (isExperimental) {
 165             CommandLineOptionTest.verifyOptionValueForSameVM(optionName,
 166                     defaultValue,
 167                     CommandLineOptionTest.UNLOCK_EXPERIMENTAL_VM_OPTIONS);
 168         } else {
 169             CommandLineOptionTest.verifyOptionValueForSameVM(optionName,
 170                     defaultValue);
 171         }
 172         // verify other specified option values
 173         if (optionValues == null) {
 174             return;
 175         }
 176 
 177         for (String value : optionValues) {
 178             if (isExperimental) {
 179                 CommandLineOptionTest.verifyOptionValueForSameVM(optionName,
 180                         value,
 181                         CommandLineOptionTest.UNLOCK_EXPERIMENTAL_VM_OPTIONS,
 182                         prepareOptionValue(value));
 183             } else {
 184                 CommandLineOptionTest.verifyOptionValueForSameVM(optionName,
 185                         value, prepareOptionValue(value));
 186             }
 187         }
 188     }
 189 
 190     protected String prepareOptionValue(String value) {
 191         if (isBoolean) {
 192             return CommandLineOptionTest.prepareBooleanFlag(optionName,
 193                     Boolean.valueOf(value));
 194         } else {
 195             return String.format("-XX:%s=%s", optionName, value);
 196         }
 197     }
 198 }