Path: blob/master/test/hotspot/jtreg/compiler/onSpinWait/TestOnSpinWaitAArch64.java
64474 views
/*1* Copyright (c) 2021, Amazon.com Inc. or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test TestOnSpinWaitAArch6425* @summary Checks that java.lang.Thread.onSpinWait is intrinsified with instructions specified with '-XX:OnSpinWaitInst' and '-XX:OnSpinWaitInstCount'26* @bug 818667027* @library /test/lib28*29* @requires vm.flagless30* @requires os.arch=="aarch64"31*32* @run driver compiler.onSpinWait.TestOnSpinWaitAArch64 c2 nop 733* @run driver compiler.onSpinWait.TestOnSpinWaitAArch64 c2 isb 334* @run driver compiler.onSpinWait.TestOnSpinWaitAArch64 c2 yield 135* @run driver compiler.onSpinWait.TestOnSpinWaitAArch64 c1 nop 736* @run driver compiler.onSpinWait.TestOnSpinWaitAArch64 c1 isb 337* @run driver compiler.onSpinWait.TestOnSpinWaitAArch64 c1 yield38*/3940package compiler.onSpinWait;4142import java.util.ArrayList;43import java.util.Iterator;44import java.util.ListIterator;45import jdk.test.lib.process.OutputAnalyzer;46import jdk.test.lib.process.ProcessTools;4748public class TestOnSpinWaitAArch64 {49public static void main(String[] args) throws Exception {50String compiler = args[0];51String spinWaitInst = args[1];52String spinWaitInstCount = (args.length == 3) ? args[2] : "1";53ArrayList<String> command = new ArrayList<String>();54command.add("-XX:+IgnoreUnrecognizedVMOptions");55command.add("-showversion");56command.add("-XX:-BackgroundCompilation");57command.add("-XX:+UnlockDiagnosticVMOptions");58command.add("-XX:+PrintAssembly");59if (compiler.equals("c2")) {60command.add("-XX:-TieredCompilation");61} else if (compiler.equals("c1")) {62command.add("-XX:+TieredCompilation");63command.add("-XX:TieredStopAtLevel=1");64} else {65throw new RuntimeException("Unknown compiler: " + compiler);66}67command.add("-Xbatch");68command.add("-XX:OnSpinWaitInst=" + spinWaitInst);69command.add("-XX:OnSpinWaitInstCount=" + spinWaitInstCount);70command.add("-XX:CompileCommand=compileonly," + Launcher.class.getName() + "::" + "test");71command.add(Launcher.class.getName());7273ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(command);7475OutputAnalyzer analyzer = new OutputAnalyzer(pb.start());7677analyzer.shouldHaveExitValue(0);7879System.out.println(analyzer.getOutput());8081checkOutput(analyzer, spinWaitInst, Integer.parseInt(spinWaitInstCount));82}8384private static String getSpinWaitInstHex(String spinWaitInst) {85if ("nop".equals(spinWaitInst)) {86return "1f20 03d5";87} else if ("isb".equals(spinWaitInst)) {88return "df3f 03d5";89} else if ("yield".equals(spinWaitInst)) {90return "3f20 03d5";91} else {92throw new RuntimeException("Unknown spin wait instruction: " + spinWaitInst);93}94}9596private static void addInstrs(String line, ArrayList<String> instrs) {97for (String instr : line.split("\\|")) {98instrs.add(instr.trim());99}100}101102// The expected output of PrintAssembly for example for a spin wait with three NOPs:103//104// # {method} {0x0000ffff6ac00370} 'test' '()V' in 'compiler/onSpinWait/TestOnSpinWaitAArch64$Launcher'105// # [sp+0x40] (sp of caller)106// 0x0000ffff9d557680: 1f20 03d5 | e953 40d1 | 3f01 00f9 | ff03 01d1 | fd7b 03a9 | 1f20 03d5 | 1f20 03d5107//108// 0x0000ffff9d5576ac: ;*invokestatic onSpinWait {reexecute=0 rethrow=0 return_oop=0}109// ; - compiler.onSpinWait.TestOnSpinWaitAArch64$Launcher::test@0 (line 161)110// 0x0000ffff9d5576ac: 1f20 03d5 | fd7b 43a9 | ff03 0191111//112// The checkOutput method adds hex instructions before 'invokestatic onSpinWait' and from the line after113// it to a list. The list is traversed from the end to count spin wait instructions.114//115// If JVM finds the hsdis library the output is like:116//117// # {method} {0x0000ffff63000370} 'test' '()V' in 'compiler/onSpinWait/TestOnSpinWaitAArch64$Launcher'118// # [sp+0x20] (sp of caller)119// 0x0000ffffa409da80: nop120// 0x0000ffffa409da84: sub sp, sp, #0x20121// 0x0000ffffa409da88: stp x29, x30, [sp, #16] ;*synchronization entry122// ; - compiler.onSpinWait.TestOnSpinWaitAArch64$Launcher::test@-1 (line 187)123// 0x0000ffffa409da8c: nop124// 0x0000ffffa409da90: nop125// 0x0000ffffa409da94: nop126// 0x0000ffffa409da98: nop127// 0x0000ffffa409da9c: nop128// 0x0000ffffa409daa0: nop129// 0x0000ffffa409daa4: nop ;*invokestatic onSpinWait {reexecute=0 rethrow=0 return_oop=0}130// ; - compiler.onSpinWait.TestOnSpinWaitAArch64$Launcher::test@0 (line 187)131private static void checkOutput(OutputAnalyzer output, String spinWaitInst, int spinWaitInstCount) {132Iterator<String> iter = output.asLines().listIterator();133134String match = skipTo(iter, "'test' '()V' in 'compiler/onSpinWait/TestOnSpinWaitAArch64$Launcher'");135if (match == null) {136throw new RuntimeException("Missing compiler output for the method compiler.onSpinWait.TestOnSpinWaitAArch64$Launcher::test");137}138139ArrayList<String> instrs = new ArrayList<String>();140String line = null;141boolean hasHexInstInOutput = false;142while (iter.hasNext()) {143line = iter.next();144if (line.contains("*invokestatic onSpinWait")) {145break;146}147if (!hasHexInstInOutput) {148hasHexInstInOutput = line.contains("|");149}150if (line.contains("0x") && !line.contains(";")) {151addInstrs(line, instrs);152}153}154155if (!iter.hasNext() || !iter.next().contains("- compiler.onSpinWait.TestOnSpinWaitAArch64$Launcher::test@0") || !iter.hasNext()) {156throw new RuntimeException("Missing compiler output for Thread.onSpinWait intrinsic");157}158159String strToSearch = null;160if (!hasHexInstInOutput) {161instrs.add(line.split(";")[0].trim());162strToSearch = spinWaitInst;163} else {164line = iter.next();165if (!line.contains("0x") || line.contains(";")) {166throw new RuntimeException("Expected hex instructions");167}168169addInstrs(line, instrs);170strToSearch = getSpinWaitInstHex(spinWaitInst);171}172173int foundInstCount = 0;174175ListIterator<String> instrReverseIter = instrs.listIterator(instrs.size());176while (instrReverseIter.hasPrevious()) {177if (instrReverseIter.previous().endsWith(strToSearch)) {178foundInstCount = 1;179break;180}181}182183while (instrReverseIter.hasPrevious()) {184if (!instrReverseIter.previous().endsWith(strToSearch)) {185break;186}187++foundInstCount;188}189190if (foundInstCount != spinWaitInstCount) {191throw new RuntimeException("Wrong instruction " + strToSearch + " count " + foundInstCount + "!\n -- expecting " + spinWaitInstCount);192}193}194195private static String skipTo(Iterator<String> iter, String substring) {196while (iter.hasNext()) {197String nextLine = iter.next();198if (nextLine.contains(substring)) {199return nextLine;200}201}202return null;203}204205static class Launcher {206public static void main(final String[] args) throws Exception {207int end = 20_000;208209for (int i=0; i < end; i++) {210test();211}212}213static void test() {214java.lang.Thread.onSpinWait();215}216}217}218219220