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 /**
  26  * @test
  27  * @bug 8031320
  28  * @summary Verify UseRTMForStackLocks option processing on CPUs without
  29  *          rtm support and/or on VMs without rtm locking support.
  30  * @library /testlibrary /../../test/lib /compiler/testlibrary
  31  * @modules java.base/sun.misc
  32  *          java.management
  33  * @build TestUseRTMForStackLocksOptionOnUnsupportedConfig
  34  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  35  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  36  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
  37  *                   -XX:+WhiteBoxAPI
  38  *                    TestUseRTMForStackLocksOptionOnUnsupportedConfig
  39  */
  40 
  41 import com.oracle.java.testlibrary.ExitCode;
  42 import com.oracle.java.testlibrary.cli.CommandLineOptionTest;
  43 import com.oracle.java.testlibrary.cli.predicate.AndPredicate;
  44 import com.oracle.java.testlibrary.cli.predicate.NotPredicate;
  45 import rtm.predicate.SupportedCPU;
  46 import rtm.predicate.SupportedVM;
  47 
  48 public class TestUseRTMForStackLocksOptionOnUnsupportedConfig
  49         extends RTMGenericCommandLineOptionTest {
  50     private static final String DEFAULT_VALUE = "false";
  51 
  52     private TestUseRTMForStackLocksOptionOnUnsupportedConfig() {
  53         super(new NotPredicate(new AndPredicate(new SupportedCPU(),
  54                         new SupportedVM())),
  55                 "UseRTMForStackLocks", true, true,
  56                 TestUseRTMForStackLocksOptionOnUnsupportedConfig.DEFAULT_VALUE,
  57                 "true");
  58     }
  59 
  60     @Override
  61     protected void runX86SupportedVMTestCases() throws Throwable {
  62         String shouldFailMessage = String.format("VM option '%s' is "
  63                 + "experimental%nJVM startup should fail without "
  64                 + "-XX:+UnlockExperimentalVMOptions flag", optionName);
  65 
  66         // verify that option is experimental
  67         CommandLineOptionTest.verifySameJVMStartup(
  68                 new String[] { experimentalOptionError }, null,
  69                 shouldFailMessage, shouldFailMessage + "%nError message "
  70                         + "should be shown", ExitCode.FAIL,
  71                 prepareOptionValue("true"));
  72 
  73         CommandLineOptionTest.verifySameJVMStartup(
  74                 new String[]{ experimentalOptionError }, null,
  75                 shouldFailMessage, shouldFailMessage + "%nError message "
  76                         + "should be shown", ExitCode.FAIL,
  77                 prepareOptionValue("false"));
  78 
  79         String shouldPassMessage = String.format("VM option '%s' is "
  80                 + " experimental%nJVM startup should pass with "
  81                 + "-XX:+UnlockExperimentalVMOptions flag", optionName);
  82         // verify that if we turn it on, then VM output will contain
  83         // warning saying that this option could be turned on only
  84         // when we use rtm locking
  85         CommandLineOptionTest.verifySameJVMStartup(
  86                 new String[]{
  87                     RTMGenericCommandLineOptionTest.RTM_FOR_STACK_LOCKS_WARNING
  88                 }, null, shouldPassMessage, "There should be warning when try "
  89                     + "to use rtm for stack lock, but not using rtm locking",
  90                 ExitCode.OK,
  91                 CommandLineOptionTest.UNLOCK_EXPERIMENTAL_VM_OPTIONS,
  92                 prepareOptionValue("true")
  93         );
  94         // verify that options is turned off by default
  95         CommandLineOptionTest.verifyOptionValueForSameVM(optionName,
  96                 TestUseRTMForStackLocksOptionOnUnsupportedConfig.DEFAULT_VALUE,
  97                 String.format("Default value of option '%s' should be '%s'",
  98                         optionName, DEFAULT_VALUE),
  99                 CommandLineOptionTest.UNLOCK_EXPERIMENTAL_VM_OPTIONS);
 100         // verify that it could not be turned on without rtm locking
 101         CommandLineOptionTest.verifyOptionValueForSameVM(optionName,
 102                 TestUseRTMForStackLocksOptionOnUnsupportedConfig.DEFAULT_VALUE,
 103                 String.format("Value of '%s' shouldn't able to be set to "
 104                         + "'true' without setting -XX:+UseRTMLocking flag",
 105                         optionName),
 106                 CommandLineOptionTest.UNLOCK_EXPERIMENTAL_VM_OPTIONS,
 107                 prepareOptionValue("true"));
 108     }
 109 
 110     public static void main(String args[]) throws Throwable {
 111         new TestUseRTMForStackLocksOptionOnUnsupportedConfig().test();
 112     }
 113 }