1 /*
2 * Copyright Amazon.com Inc. 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 TestOnSpinWaitAArch64
26 * @summary Checks that java.lang.Thread.onSpinWait is intrinsified with instructions specified with '-XX:OnSpinWaitInst' and '-XX:OnSpinWaitInstCount'
27 * @bug 8186670
28 * @library /test/lib
29 *
30 * @requires vm.flagless
31 * @requires os.arch=="aarch64"
32 * @requires vm.debug
33 *
34 * @run driver compiler.onSpinWait.TestOnSpinWaitAArch64 c2 nop 7
35 * @run driver compiler.onSpinWait.TestOnSpinWaitAArch64 c2 isb 3
36 * @run driver compiler.onSpinWait.TestOnSpinWaitAArch64 c2 yield 1
37 * @run driver compiler.onSpinWait.TestOnSpinWaitAArch64 c2 sb 1
38 * @run driver compiler.onSpinWait.TestOnSpinWaitAArch64 c1 nop 7
39 * @run driver compiler.onSpinWait.TestOnSpinWaitAArch64 c1 isb 3
40 * @run driver compiler.onSpinWait.TestOnSpinWaitAArch64 c1 yield 1
41 * @run driver compiler.onSpinWait.TestOnSpinWaitAArch64 c1 sb 1
42 */
43
44 package compiler.onSpinWait;
45
46 import java.util.Arrays;
47 import java.util.ArrayList;
48 import java.util.Iterator;
49 import java.util.ListIterator;
50 import jdk.test.lib.process.OutputAnalyzer;
51 import jdk.test.lib.process.ProcessTools;
52
53 public class TestOnSpinWaitAArch64 {
54
55 public static void main(String[] args) throws Exception {
56 String compiler = args[0];
57 String spinWaitInst = args[1];
58 String spinWaitInstCount = args[2];
59 ArrayList<String> command = new ArrayList<String>();
60 command.add("-XX:+IgnoreUnrecognizedVMOptions");
61 command.add("-showversion");
62 command.add("-XX:-BackgroundCompilation");
63 command.add("-XX:+UnlockDiagnosticVMOptions");
64 if (compiler.equals("c2")) {
65 command.add("-XX:-TieredCompilation");
66 } else if (compiler.equals("c1")) {
67 command.add("-XX:+TieredCompilation");
68 command.add("-XX:TieredStopAtLevel=1");
69 } else {
70 throw new RuntimeException("Unknown compiler: " + compiler);
71 }
72 command.add("-Xbatch");
73 command.add("-XX:OnSpinWaitInst=" + spinWaitInst);
74 command.add("-XX:OnSpinWaitInstCount=" + spinWaitInstCount);
75 command.add("-XX:CompileCommand=compileonly," + Launcher.class.getName() + "::" + "test");
76 command.add("-XX:CompileCommand=print," + Launcher.class.getName() + "::" + "test");
77 command.add(Launcher.class.getName());
78
79 ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder(command);
80
81 OutputAnalyzer analyzer = new OutputAnalyzer(pb.start());
82
83 if ("sb".equals(spinWaitInst) && analyzer.contains("CPU does not support SB")) {
84 System.out.println("Skipping the test. The current CPU does not support SB instruction.");
85 return;
86 }
87
88 analyzer.shouldHaveExitValue(0);
89
90 System.out.println(analyzer.getOutput());
91
92 checkOutput(analyzer, spinWaitInst, Integer.parseInt(spinWaitInstCount));
93 }
94
95 private static String getSpinWaitInstHex(String spinWaitInst) {
96 if ("nop".equals(spinWaitInst)) {
97 return "1f2003d5";
98 } else if ("isb".equals(spinWaitInst)) {
99 return "df3f03d5";
100 } else if ("yield".equals(spinWaitInst)) {
101 return "3f2003d5";
102 } else if ("sb".equals(spinWaitInst)) {
103 return "ff3003d5";
104 } else {
105 throw new RuntimeException("Unknown spin wait instruction: " + spinWaitInst);
106 }
107 }
108
109 // The expected output for a spin wait with three NOPs
110 // if the hsdis library is available:
111 //
112 // ;; spin_wait {
113 // 0x0000000111dfa58c: nop
114 // 0x0000000111dfa590: nop
115 // 0x0000000111dfa594: nop
116 // ;; }
117 //
118 private static void checkOutput(OutputAnalyzer output, final String spinWaitInst, final int expectedCount) {
119 Iterator<String> iter = output.asLines().listIterator();
120
121 // 1. Check whether printed instructions are disassembled
122 boolean isDisassembled = false;
123 while (iter.hasNext()) {
124 String line = iter.next();
125 if (line.contains("[Disassembly]")) {
126 isDisassembled = true;
127 break;
128 }
129 if (line.contains("[MachCode]")) {
130 break;
131 }
132 }
133
134 // 2. Look for the block comment
135 boolean foundHead = false;
136 while (iter.hasNext()) {
137 String line = iter.next().trim();
138 if (line.contains(";; spin_wait {")) {
139 foundHead = true;
140 break;
141 }
142 }
143 if (!foundHead) {
144 throw new RuntimeException("Block comment not found");
145 }
146
147 // 3. Count spin wait instructions
148 final String expectedInst = isDisassembled ? spinWaitInst : getSpinWaitInstHex(spinWaitInst);
149 int foundCount = 0;
150 while (iter.hasNext()) {
151 String line = iter.next().trim();
152 if (line.startsWith(";; }")) {
153 break;
154 }
155 if (!line.startsWith("0x")) {
156 continue;
157 }
158 int pos = line.indexOf(':');
159 if (pos == -1 || pos == line.length() - 1) {
160 continue;
161 }
162 line = line.substring(pos + 1).replaceAll("\\s", "");
163 if (line.startsWith(";")) {
164 continue;
165 }
166 // When code is disassembled, we have one instruction per line.
167 // Otherwise, there can be multiple hex instructions separated by '|'.
168 foundCount += (int)Arrays.stream(line.split("\\|"))
169 .takeWhile(i -> i.startsWith(expectedInst))
170 .count();
171 }
172
173 if (foundCount != expectedCount) {
174 throw new RuntimeException("Expected " + expectedCount + " " + spinWaitInst + " instructions. Found: " + foundCount);
175 }
176 }
177
178 static class Launcher {
179 public static void main(final String[] args) throws Exception {
180 int end = 20_000;
181
182 for (int i=0; i < end; i++) {
183 test();
184 }
185 }
186 static void test() {
187 java.lang.Thread.onSpinWait();
188 }
189 }
190 }