1 /*
2 * Copyright (c) 2014, 2021, 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
49 protected final String optionName;
50 protected final String errorMessage;
51 protected final String experimentalOptionError;
52 protected final boolean isExperimental;
53 protected final boolean isBoolean;
54 protected final String defaultValue;
55 protected final String[] optionValues;
56
57 /**
58 * Constructs new genetic RTM CLI test, for option {@code optionName} which
59 * has default value {@code defaultValue}. Test cases will use option's
60 * values passed via {@code optionValues} for verification of correct
61 * option processing.
62 *
63 * Test constructed using this ctor will be started on any cpu regardless
64 * it's architecture and supported/unsupported features.
65 *
66 * @param optionName name of option to be tested
67 * @param isBoolean {@code true} if option is binary
68 * @param isExperimental {@code true} if option is experimental
69 * @param defaultValue default value of tested option
70 * @param optionValues different option values
71 */
72 public RTMGenericCommandLineOptionTest(
73 String optionName, boolean isBoolean, boolean isExperimental,
74 String defaultValue, String... optionValues) {
75 this.optionName = optionName;
76 this.isExperimental = isExperimental;
77 this.isBoolean = isBoolean;
78 this.defaultValue = defaultValue;
79 this.optionValues = optionValues;
80 this.errorMessage = CommandLineOptionTest.
81 getUnrecognizedOptionErrorMessage(optionName);
82 this.experimentalOptionError = CommandLineOptionTest.
83 getExperimentalOptionErrorMessage(optionName);
84 }
85
86 public void runTestCases() throws Throwable {
87 if (Platform.isX86() || Platform.isX64() || Platform.isPPC()) {
88 if (Platform.isServer()) {
89 runX86SupportedVMTestCases();
90 } else {
91 runX86UnsupportedVMTestCases();
92 }
93 } else {
94 runNonX86TestCases();
95 }
96 }
97
98 /**
99 * Runs test cases on X86 CPU if VM supports RTM locking.
100 * @throws Throwable
101 */
102 protected void runX86SupportedVMTestCases() throws Throwable {
103 runGenericX86TestCases();
104 }
105
106 /**
107 * Runs test cases on X86 CPU if VM does not support RTM locking.
108 * @throws Throwable
109 */
110 protected void runX86UnsupportedVMTestCases() throws Throwable {
111 runGenericX86TestCases();
112 }
113
114 /**
115 * Runs test cases on non-X86 CPU.
116 * @throws Throwable
117 */
118 protected void runNonX86TestCases() throws Throwable {
119 CommandLineOptionTest.verifySameJVMStartup(
120 new String[] { errorMessage }, null,
121 String.format("Option '%s' should be unknown on non-X86CPUs.%n"
122 + "JVM startup should fail", optionName), "", ExitCode.FAIL,
123 prepareOptionValue(defaultValue));
124 }
125
126 /**
127 * Runs generic X86 test cases.
128 * @throws Throwable
129 */
130 protected void runGenericX86TestCases() throws Throwable {
131 verifyJVMStartup();
132 verifyOptionValues();
133 }
134
135 protected void verifyJVMStartup() throws Throwable {
136 String optionValue = prepareOptionValue(defaultValue);
137 String shouldFailMessage = String.format("VM option '%s' is "
138 + "experimental.%nVM startup expected to fail without "
139 + "-XX:+UnlockExperimentalVMOptions option", optionName);
140 String shouldPassMessage = String.format("VM option '%s' is "
141 + "experimental%nVM startup should pass with "
142 + "-XX:+UnlockExperimentalVMOptions option", optionName);
143 if (isExperimental) {
144 // verify that option is experimental
145 CommandLineOptionTest.verifySameJVMStartup(
146 new String[] { experimentalOptionError },
147 new String[] { errorMessage }, shouldFailMessage,
148 shouldFailMessage, ExitCode.FAIL, optionValue);
149 // verify that it could be passed if experimental options
150 // are unlocked
151 CommandLineOptionTest.verifySameJVMStartup(null,
152 new String[] {
153 experimentalOptionError,
154 errorMessage
155 },
156 shouldPassMessage,
157 "JVM should start without any warnings or errors",
158 ExitCode.OK,
159 CommandLineOptionTest.UNLOCK_EXPERIMENTAL_VM_OPTIONS,
160 optionValue);
161 } else {
162 // verify that option could be passed
163 CommandLineOptionTest.verifySameJVMStartup(null,
164 new String[]{errorMessage},
165 String.format("VM startup shuld pass with '%s' option",
166 optionName),
167 "JVM should start without any warnings or errors",
168 ExitCode.OK, optionValue);
169 }
170 }
171
172 protected void verifyOptionValues() throws Throwable {
173 // verify default value
174 if (isExperimental) {
175 CommandLineOptionTest.verifyOptionValueForSameVM(optionName,
176 defaultValue,
177 String.format("Option '%s' is expected to have '%s' "
178 + "default value", optionName, defaultValue),
179 CommandLineOptionTest.UNLOCK_EXPERIMENTAL_VM_OPTIONS);
180 } else {
181 CommandLineOptionTest.verifyOptionValueForSameVM(optionName,
182 defaultValue,
183 String.format("Option '%s' is expected to have '%s' "
184 + "default value", optionName, defaultValue));
185 }
186 // verify other specified option values
187 if (optionValues == null) {
188 return;
189 }
190
191 for (String value : optionValues) {
192 if (isExperimental) {
193 CommandLineOptionTest.verifyOptionValueForSameVM(optionName,
194 value,
195 String.format("Option '%s' is set to have '%s' value",
196 optionName, value),
197 CommandLineOptionTest.UNLOCK_EXPERIMENTAL_VM_OPTIONS,
198 prepareOptionValue(value));
199 } else {
200 CommandLineOptionTest.verifyOptionValueForSameVM(optionName,
201 value,
202 String.format("Option '%s' is set to have '%s' value",
203 optionName, value), prepareOptionValue(value));
204 }
205 }
206 }
207
208 protected String prepareOptionValue(String value) {
209 if (isBoolean) {
210 return CommandLineOptionTest.prepareBooleanFlag(optionName,
211 Boolean.valueOf(value));
212 } else {
213 return String.format("-XX:%s=%s", optionName, value);
214 }
215 }
216 }