1 /*
   2  * Copyright (c) 2014, 2020, 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  * @test
  26  * @bug 8031320
  27  * @summary Verify UseRTMLocking option processing on CPUs without
  28  *          rtm support.
  29  * @library /test/lib /
  30  * @modules java.base/jdk.internal.misc
  31  *          java.management
  32  * @requires !vm.rtm.cpu & vm.rtm.compiler
  33  * @run driver compiler.rtm.cli.TestUseRTMLockingOptionOnUnsupportedCPU
  34  */
  35 
  36 package compiler.rtm.cli;
  37 
  38 import jdk.test.lib.process.ExitCode;
  39 import jdk.test.lib.Platform;
  40 import jdk.test.lib.cli.CommandLineOptionTest;
  41 
  42 public class TestUseRTMLockingOptionOnUnsupportedCPU {
  43     private static final String DEFAULT_VALUE = "false";
  44 
  45     public void runTestCases() throws Throwable {
  46         String unrecognizedOption
  47                 = CommandLineOptionTest.getUnrecognizedOptionErrorMessage(
  48                 "UseRTMLocking");
  49         String errorMessage = RTMGenericCommandLineOptionTest.RTM_INSTR_ERROR;
  50 
  51         if (Platform.isX86() || Platform.isX64() || Platform.isPPC()) {
  52             String shouldFailMessage = "JVM startup should fail with option " +
  53                                        "-XX:+UseRTMLocking on unsupported CPU";
  54 
  55             try {
  56                 // verify that we get an error when use +UseRTMLocking
  57                 // on unsupported CPU
  58                 CommandLineOptionTest.verifySameJVMStartup(
  59                         new String[] { errorMessage },
  60                         new String[] { unrecognizedOption }, shouldFailMessage,
  61                         shouldFailMessage + ". Error message should be shown.",
  62                         ExitCode.FAIL, "-XX:+UseRTMLocking");
  63             } catch (Throwable e) {
  64                 // verify that we get an error when use +UseRTMLocking
  65                 // on unsupported OS. It might be the case that although CPU
  66                 // supports RTM the OS version does not support RTM
  67                 if (Platform.isPPC()) {
  68                     String errorMessage2 = RTMGenericCommandLineOptionTest.RTM_OS_ERROR;
  69                     String shouldFailMessage2 = "JVM startup should fail with option " +
  70                                                 "-XX:+UseRTMLocking on unsupported CPU or " +
  71                                                 "OS version";
  72 
  73                     CommandLineOptionTest.verifySameJVMStartup(
  74                             new String[] { errorMessage2 },
  75                             new String[] { unrecognizedOption}, shouldFailMessage2,
  76                             shouldFailMessage2 + ". Error message should be shown.",
  77                             ExitCode.FAIL, "-XX:+UseRTMLocking");
  78                 } else {
  79                     throw e; // checking unsupported OS error is not necessary
  80                 }
  81             }
  82 
  83             String shouldPassMessage = "JVM startup should pass with option "
  84                     + "-XX:-UseRTMLocking even on unsupported CPU";
  85             // verify that we can pass -UseRTMLocking without
  86             // getting any error messages
  87             CommandLineOptionTest.verifySameJVMStartup(null, new String[] {
  88                     errorMessage, unrecognizedOption }, shouldPassMessage,
  89                     shouldPassMessage + " without any warnings", ExitCode.OK,
  90                     "-XX:-UseRTMLocking");
  91 
  92             // verify that UseRTMLocking is false by default
  93             CommandLineOptionTest.verifyOptionValueForSameVM("UseRTMLocking",
  94                     TestUseRTMLockingOptionOnUnsupportedCPU.DEFAULT_VALUE,
  95                     String.format("Default value of option 'UseRTMLocking' "
  96                         +"should be '%s'", DEFAULT_VALUE));
  97         } else {
  98             String shouldFailMessage = "RTMLocking should be unrecognized"
  99                     + " on non-x86 CPUs. JVM startup should fail."
 100                     + "Error message should be shown";
 101             // verify that on non-x86 CPUs RTMLocking could not be used
 102             CommandLineOptionTest.verifySameJVMStartup(
 103                     new String[] { unrecognizedOption },
 104                     null, shouldFailMessage, shouldFailMessage,
 105                     ExitCode.FAIL, "-XX:+UseRTMLocking");
 106 
 107             CommandLineOptionTest.verifySameJVMStartup(
 108                     new String[] { unrecognizedOption },
 109                     null, shouldFailMessage, shouldFailMessage,
 110                     ExitCode.FAIL, "-XX:-UseRTMLocking");
 111         }
 112     }
 113 
 114     public static void main(String args[]) throws Throwable {
 115         new TestUseRTMLockingOptionOnUnsupportedCPU().runTestCases();
 116     }
 117 }