< prev index next >

test/hotspot/jtreg/compiler/onSpinWait/TestOnSpinWaitAArch64.java

Print this page

 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  *
 33  * @run driver compiler.onSpinWait.TestOnSpinWaitAArch64 c2 nop 7
 34  * @run driver compiler.onSpinWait.TestOnSpinWaitAArch64 c2 isb 3
 35  * @run driver compiler.onSpinWait.TestOnSpinWaitAArch64 c2 yield 1

 36  * @run driver compiler.onSpinWait.TestOnSpinWaitAArch64 c1 nop 7
 37  * @run driver compiler.onSpinWait.TestOnSpinWaitAArch64 c1 isb 3
 38  * @run driver compiler.onSpinWait.TestOnSpinWaitAArch64 c1 yield

 39  */
 40 
 41 package compiler.onSpinWait;
 42 

 43 import java.util.ArrayList;
 44 import java.util.Iterator;
 45 import java.util.ListIterator;
 46 import jdk.test.lib.process.OutputAnalyzer;
 47 import jdk.test.lib.process.ProcessTools;
 48 
 49 public class TestOnSpinWaitAArch64 {

 50     public static void main(String[] args) throws Exception {
 51         String compiler = args[0];
 52         String spinWaitInst = args[1];
 53         String spinWaitInstCount = (args.length == 3) ? args[2] : "1";
 54         ArrayList<String> command = new ArrayList<String>();
 55         command.add("-XX:+IgnoreUnrecognizedVMOptions");
 56         command.add("-showversion");
 57         command.add("-XX:-BackgroundCompilation");
 58         command.add("-XX:+UnlockDiagnosticVMOptions");
 59         command.add("-XX:+PrintAssembly");
 60         if (compiler.equals("c2")) {
 61             command.add("-XX:-TieredCompilation");
 62         } else if (compiler.equals("c1")) {
 63             command.add("-XX:+TieredCompilation");
 64             command.add("-XX:TieredStopAtLevel=1");
 65         } else {
 66             throw new RuntimeException("Unknown compiler: " + compiler);
 67         }
 68         command.add("-Xbatch");
 69         command.add("-XX:OnSpinWaitInst=" + spinWaitInst);
 70         command.add("-XX:OnSpinWaitInstCount=" + spinWaitInstCount);
 71         command.add("-XX:CompileCommand=compileonly," + Launcher.class.getName() + "::" + "test");

 72         command.add(Launcher.class.getName());
 73 
 74         ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder(command);
 75 
 76         OutputAnalyzer analyzer = new OutputAnalyzer(pb.start());
 77 





 78         analyzer.shouldHaveExitValue(0);
 79 
 80         System.out.println(analyzer.getOutput());
 81 
 82         checkOutput(analyzer, spinWaitInst, Integer.parseInt(spinWaitInstCount));
 83     }
 84 
 85     private static String getSpinWaitInstHex(String spinWaitInst) {
 86       if ("nop".equals(spinWaitInst)) {
 87           return "1f20 03d5";
 88       } else if ("isb".equals(spinWaitInst)) {
 89           return "df3f 03d5";
 90       } else if ("yield".equals(spinWaitInst)) {
 91           return "3f20 03d5";


 92       } else {
 93           throw new RuntimeException("Unknown spin wait instruction: " + spinWaitInst);
 94       }
 95     }
 96 
 97     private static void addInstrs(String line, ArrayList<String> instrs) {
 98         for (String instr : line.split("\\|")) {
 99             instrs.add(instr.trim());
100         }
101     }
102 
103     // The expected output of PrintAssembly for example for a spin wait with three NOPs:
104     //
105     // # {method} {0x0000ffff6ac00370} 'test' '()V' in 'compiler/onSpinWait/TestOnSpinWaitAArch64$Launcher'
106     // #           [sp+0x40]  (sp of caller)
107     // 0x0000ffff9d557680: 1f20 03d5 | e953 40d1 | 3f01 00f9 | ff03 01d1 | fd7b 03a9 | 1f20 03d5 | 1f20 03d5
108     //
109     // 0x0000ffff9d5576ac: ;*invokestatic onSpinWait {reexecute=0 rethrow=0 return_oop=0}
110     //                     ; - compiler.onSpinWait.TestOnSpinWaitAArch64$Launcher::test@0 (line 161)
111     // 0x0000ffff9d5576ac: 1f20 03d5 | fd7b 43a9 | ff03 0191
112     //
113     // The checkOutput method adds hex instructions before 'invokestatic onSpinWait' and from the line after
114     // it to a list. The list is traversed from the end to count spin wait instructions.



115     //
116     // If JVM finds the hsdis library the output is like:
117     //
118     // # {method} {0x0000ffff63000370} 'test' '()V' in 'compiler/onSpinWait/TestOnSpinWaitAArch64$Launcher'
119     // #           [sp+0x20]  (sp of caller)
120     // 0x0000ffffa409da80:   nop
121     // 0x0000ffffa409da84:   sub sp, sp, #0x20
122     // 0x0000ffffa409da88:   stp x29, x30, [sp, #16]         ;*synchronization entry
123     //                                                       ; - compiler.onSpinWait.TestOnSpinWaitAArch64$Launcher::test@-1 (line 187)
124     // 0x0000ffffa409da8c:   nop
125     // 0x0000ffffa409da90:   nop
126     // 0x0000ffffa409da94:   nop
127     // 0x0000ffffa409da98:   nop
128     // 0x0000ffffa409da9c:   nop
129     // 0x0000ffffa409daa0:   nop
130     // 0x0000ffffa409daa4:   nop                                 ;*invokestatic onSpinWait {reexecute=0 rethrow=0 return_oop=0}
131     //                                                           ; - compiler.onSpinWait.TestOnSpinWaitAArch64$Launcher::test@0 (line 187)
132     private static void checkOutput(OutputAnalyzer output, String spinWaitInst, int spinWaitInstCount) {
133         Iterator<String> iter = output.asLines().listIterator();
134 
135         String match = skipTo(iter, "'test' '()V' in 'compiler/onSpinWait/TestOnSpinWaitAArch64$Launcher'");
136         if (match == null) {
137             throw new RuntimeException("Missing compiler output for the method compiler.onSpinWait.TestOnSpinWaitAArch64$Launcher::test");
138         }
139 
140         ArrayList<String> instrs = new ArrayList<String>();
141         String line = null;
142         boolean hasHexInstInOutput = false;
143         while (iter.hasNext()) {
144             line = iter.next();
145             if (line.contains("*invokestatic onSpinWait")) {

146                 break;
147             }
148             if (!hasHexInstInOutput) {
149                 hasHexInstInOutput = line.contains("|");
150             }
151             if (line.contains("0x") && !line.contains(";")) {
152                 addInstrs(line, instrs);
153             }
154         }
155 
156         if (!iter.hasNext() || !iter.next().contains("- compiler.onSpinWait.TestOnSpinWaitAArch64$Launcher::test@0") || !iter.hasNext()) {
157             throw new RuntimeException("Missing compiler output for Thread.onSpinWait intrinsic");
158         }
159 
160         String strToSearch = null;
161         if (!hasHexInstInOutput) {
162             instrs.add(line.split(";")[0].trim());
163             strToSearch = spinWaitInst;
164         } else {
165             line = iter.next();
166             if (!line.contains("0x") || line.contains(";")) {
167                 throw new RuntimeException("Expected hex instructions");
168             }
169 
170             addInstrs(line, instrs);
171             strToSearch = getSpinWaitInstHex(spinWaitInst);
172         }
173 
174         int foundInstCount = 0;
175 
176         ListIterator<String> instrReverseIter = instrs.listIterator(instrs.size());
177         while (instrReverseIter.hasPrevious()) {
178             if (instrReverseIter.previous().endsWith(strToSearch)) {
179                 foundInstCount = 1;
180                 break;
181             }
182         }
183 
184         while (instrReverseIter.hasPrevious()) {
185             if (!instrReverseIter.previous().endsWith(strToSearch)) {




186                 break;
187             }
188             ++foundInstCount;
189         }
190 
191         if (foundInstCount != spinWaitInstCount) {
192             throw new RuntimeException("Wrong instruction " + strToSearch + " count " + foundInstCount + "!\n  -- expecting " + spinWaitInstCount);
193         }
194     }
195 
196     private static String skipTo(Iterator<String> iter, String substring) {


197         while (iter.hasNext()) {
198             String nextLine = iter.next();
199             if (nextLine.contains(substring)) {
200                 return nextLine;



201             }

















202         }
203         return null;
204     }
205 
206     static class Launcher {
207         public static void main(final String[] args) throws Exception {
208             int end = 20_000;
209 
210             for (int i=0; i < end; i++) {
211                 test();
212             }
213         }
214         static void test() {
215             java.lang.Thread.onSpinWait();
216         }
217     }
218 }

 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 }
< prev index next >