Path: blob/master/test/hotspot/jtreg/compiler/intrinsics/bmi/BMITestRunner.java
41155 views
/*1* Copyright (c) 2014, 2020, Oracle and/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*/2223package compiler.intrinsics.bmi;2425import jdk.test.lib.Asserts;26import jdk.test.lib.process.OutputAnalyzer;27import jdk.test.lib.process.ProcessTools;28import jdk.test.lib.Utils;2930import java.io.IOException;31import java.nio.file.Files;32import java.nio.file.Paths;33import java.util.Collections;34import java.util.LinkedList;35import java.util.List;36import java.util.Random;3738/**39* Test runner that invokes all methods implemented by particular Expr40* with random arguments in two different JVM processes and compares output.41* JVMs being started in different modes - one in int and other in comp42* with C2 and disabled tiered compilation.43*/44public class BMITestRunner {4546enum VMMode {47COMP, INT;48};4950public static int DEFAULT_ITERATIONS_COUNT = 4000;5152/**53* Execute all methods implemented by <b>expr</b> in int and comp modes54* and compare output.55* Test pass only of output obtained with different VM modes is equal.56* To control behaviour of test following options could be passed:57* <ul>58* <li>-iterations=<N> each operation implemented by59* <b>expr</b> will be executed <i>N</i> times. Default value60* is 4000.</li>61* </ul>62*63* @param expr operation that should be tested64* @param testOpts options to control test behaviour65* @param additionalVMOpts additional options for VM66*67* @throws Throwable if test failed.68*/69public static void runTests(Class<? extends Expr> expr,70String testOpts[],71String... additionalVMOpts)72throws Throwable {7374// ensure seed got printed out75Utils.getRandomInstance();76long seed = Utils.SEED;77int iterations = DEFAULT_ITERATIONS_COUNT;7879for (String testOption : testOpts) {80if (testOption.startsWith("-iterations=")) {81iterations = Integer.valueOf(testOption.82replace("-iterations=", ""));83}84}8586OutputAnalyzer intOutput = runTest(expr, VMMode.INT,87additionalVMOpts,88seed, iterations);89OutputAnalyzer compOutput = runTest(expr, VMMode.COMP,90additionalVMOpts,91seed, iterations);9293dumpOutput(intOutput, "int");94dumpOutput(compOutput, "comp");9596Asserts.assertStringsEqual(intOutput.getStdout(),97compOutput.getStdout(),98"Results obtained in -Xint and " +99"-Xcomp should be the same.");100}101102/**103* Execute tests on methods implemented by <b>expr</b> in new VM104* started in <b>testVMMode</b> mode.105*106* @param expr operation that should be tested107* @param testVMMode VM mode for test108* @param additionalVMOpts additional options for VM109* @param seed for RNG used it tests110* @param iterations that will be used to invoke <b>expr</b>'s methods.111*112* @return OutputAnalyzer for executed test.113* @throws Throwable when something goes wrong.114*/115public static OutputAnalyzer runTest(Class<? extends Expr> expr,116VMMode testVMMode,117String additionalVMOpts[],118long seed, int iterations)119throws Throwable {120121List<String> vmOpts = new LinkedList<String>();122123Collections.addAll(vmOpts, additionalVMOpts);124125//setup mode-specific options126switch (testVMMode) {127case INT:128Collections.addAll(vmOpts, new String[] { "-Xint" });129break;130case COMP:131Collections.addAll(vmOpts, new String[] {132"-Xcomp",133"-XX:-TieredCompilation",134String.format("-XX:CompileCommand=compileonly,%s::*",135expr.getName())136});137break;138}139140Collections.addAll(vmOpts, new String[] {141"-XX:+DisplayVMOutputToStderr",142"-D" + Utils.SEED_PROPERTY_NAME + "=" + seed,143Executor.class.getName(),144expr.getName(),145new Integer(iterations).toString()146});147148OutputAnalyzer outputAnalyzer = ProcessTools.executeTestJvm(vmOpts);149150outputAnalyzer.shouldHaveExitValue(0);151152return outputAnalyzer;153}154155/**156* Dump stdout and stderr of test process to <i>prefix</i>.test.out157* and <i>prefix</i>.test.err respectively.158*159* @param outputAnalyzer OutputAnalyzer whom output should be dumped160* @param prefix Prefix that will be used in file names.161* @throws IOException if unable to dump output to file.162*/163protected static void dumpOutput(OutputAnalyzer outputAnalyzer,164String prefix)165throws IOException {166Files.write(Paths.get(prefix + ".test.out"),167outputAnalyzer.getStdout().getBytes());168169Files.write(Paths.get(prefix + ".test.err"),170outputAnalyzer.getStderr().getBytes());171}172173174/**175* Executor that invoke all methods implemented by particular176* Expr instance.177*/178public static class Executor {179180/**181* Usage: BMITestRunner$Executor <ExprClassName> <iterations>182*/183public static void main(String args[]) throws Exception {184@SuppressWarnings("unchecked")185Class<? extends Expr> exprClass =186(Class<? extends Expr>)Class.forName(args[0]);187Expr expr = exprClass.getConstructor().newInstance();188int iterations = Integer.valueOf(args[1]);189runTests(expr, iterations, Utils.getRandomInstance());190}191192193public static int[] getIntBitShifts() {194//SIZE+1 shift is for zero.195int data[] = new int[Integer.SIZE+1];196for (int s = 0; s < data.length; s++) {197data[s] = 1<<s;198}199return data;200}201202public static long[] getLongBitShifts() {203//SIZE+1 shift is for zero.204long data[] = new long[Long.SIZE+1];205for (int s = 0; s < data.length; s++) {206data[s] = 1L<<s;207}208return data;209}210211public static void log(String format, Object... args) {212System.out.println(String.format(format, args));213}214215public static void runTests(Expr expr, int iterations, Random rng) {216runUnaryIntRegTest(expr, iterations, rng);217runUnaryIntMemTest(expr, iterations, rng);218runUnaryLongRegTest(expr, iterations, rng);219runUnaryLongMemTest(expr, iterations, rng);220runUnaryIntToLongRegTest(expr, iterations, rng);221runBinaryRegRegIntTest(expr, iterations, rng);222runBinaryRegMemIntTest(expr, iterations, rng);223runBinaryMemRegIntTest(expr, iterations, rng);224runBinaryMemMemIntTest(expr, iterations, rng);225runBinaryRegRegLongTest(expr, iterations, rng);226runBinaryRegMemLongTest(expr, iterations, rng);227runBinaryMemRegLongTest(expr, iterations, rng);228runBinaryMemMemLongTest(expr, iterations, rng);229}230231public static void runUnaryIntRegTest(Expr expr, int iterations,232Random rng) {233if (!(expr.isUnaryArgumentSupported()234&& expr.isIntExprSupported())) {235return;236}237238for (int value : getIntBitShifts()) {239log("UnaryIntReg(0X%x) -> 0X%x",240value, expr.intExpr(value));241}242243for (int i = 0; i < iterations; i++) {244int value = rng.nextInt();245log("UnaryIntReg(0X%x) -> 0X%x",246value, expr.intExpr(value));247}248}249250public static void runUnaryIntMemTest(Expr expr, int iterations,251Random rng) {252if (!(expr.isUnaryArgumentSupported()253&& expr.isIntExprSupported()254&& expr.isMemExprSupported())) {255return;256}257258for (int value : getIntBitShifts()) {259log("UnaryIntMem(0X%x) -> 0X%x",260value, expr.intExpr(new Expr.MemI(value)));261}262263for (int i = 0; i < iterations; i++) {264int value = rng.nextInt();265log("UnaryIntMem(0X%x) -> 0X%x",266value, expr.intExpr(new Expr.MemI(value)));267}268}269270public static void runUnaryLongRegTest(Expr expr, int iterations,271Random rng) {272if (!(expr.isUnaryArgumentSupported()273&& expr.isLongExprSupported())) {274return;275}276277for (long value : getLongBitShifts()) {278log("UnaryLongReg(0X%x) -> 0X%x",279value, expr.longExpr(value));280}281282for (int i = 0; i < iterations; i++) {283long value = rng.nextLong();284log("UnaryLongReg(0X%x) -> 0X%x",285value, expr.longExpr(value));286}287}288289public static void runUnaryLongMemTest(Expr expr, int iterations,290Random rng) {291if (!(expr.isUnaryArgumentSupported()292&& expr.isLongExprSupported()293&& expr.isMemExprSupported())) {294return;295}296297for (long value : getLongBitShifts()) {298log("UnaryLongMem(0X%x) -> 0X%x",299value, expr.longExpr(new Expr.MemL(value)));300}301302for (int i = 0; i < iterations; i++) {303long value = rng.nextLong();304log("UnaryLongMem(0X%x) -> 0X%x",305value, expr.longExpr(new Expr.MemL(value)));306}307}308309public static void runUnaryIntToLongRegTest(Expr expr, int iterations,310Random rng) {311if (!(expr.isUnaryArgumentSupported()312&& expr.isIntToLongExprSupported())) {313return;314}315316for (int value : getIntBitShifts()) {317log("UnaryIntToLongReg(0X%x) -> 0X%x",318value, expr.intToLongExpr(value));319}320321for (int i = 0; i < iterations; i++) {322int value = rng.nextInt();323log("UnaryIntToLongReg(0X%x) -> 0X%x",324value, expr.intToLongExpr(value));325}326}327328public static void runBinaryRegRegIntTest(Expr expr, int iterations,329Random rng) {330if (!(expr.isIntExprSupported()331&& expr.isBinaryArgumentSupported())) {332return;333}334335for (int i = 0; i < iterations; i++) {336int aValue = rng.nextInt();337int bValue = rng.nextInt();338log("BinaryIntRegReg(0X%x, 0X%x) -> 0X%x",339aValue, bValue, expr.intExpr(aValue, bValue));340}341}342343public static void runBinaryRegMemIntTest(Expr expr, int iterations,344Random rng) {345if (!(expr.isIntExprSupported()346&& expr.isBinaryArgumentSupported()347&& expr.isMemExprSupported())) {348return;349}350351for (int i = 0; i < iterations; i++) {352int aValue = rng.nextInt();353int bValue = rng.nextInt();354log("BinaryIntRegMem(0X%x, 0X%x) -> 0X%x", aValue, bValue,355expr.intExpr(aValue, new Expr.MemI(bValue)));356}357}358359public static void runBinaryMemRegIntTest(Expr expr, int iterations,360Random rng) {361if (!(expr.isIntExprSupported()362&& expr.isBinaryArgumentSupported()363&& expr.isMemExprSupported())) {364return;365}366367for (int i = 0; i < iterations; i++) {368int aValue = rng.nextInt();369int bValue = rng.nextInt();370log("BinaryIntMemReg(0X%x, 0X%x) -> 0X%x", aValue, bValue,371expr.intExpr(new Expr.MemI(aValue), bValue));372}373}374375public static void runBinaryMemMemIntTest(Expr expr, int iterations,376Random rng) {377if (!(expr.isIntExprSupported()378&& expr.isBinaryArgumentSupported()379&& expr.isMemExprSupported())) {380return;381}382383for (int i = 0; i < iterations; i++) {384int aValue = rng.nextInt();385int bValue = rng.nextInt();386log("BinaryIntMemMem(0X%x, 0X%x) -> 0X%x", aValue, bValue,387expr.intExpr(new Expr.MemI(aValue),388new Expr.MemI(bValue)));389}390}391392public static void runBinaryRegRegLongTest(Expr expr,393int iterations,394Random rng) {395if (!(expr.isLongExprSupported()396&& expr.isBinaryArgumentSupported())) {397return;398}399400for (int i = 0; i < iterations; i++) {401long aValue = rng.nextLong();402long bValue = rng.nextLong();403log("BinaryLongRegReg(0X%x, 0X%x) -> 0X%x", aValue, bValue,404expr.longExpr(aValue, bValue));405}406}407408public static void runBinaryRegMemLongTest(Expr expr,409int iterations,410Random rng) {411if (!(expr.isLongExprSupported()412&& expr.isBinaryArgumentSupported()413&& expr.isMemExprSupported())) {414return;415}416417for (int i = 0; i < iterations; i++) {418long aValue = rng.nextLong();419long bValue = rng.nextLong();420log("BinaryLongRegMem(0X%x, 0X%x) -> 0X%x", aValue, bValue,421expr.longExpr(aValue, new Expr.MemL(bValue)));422}423}424425public static void runBinaryMemRegLongTest(Expr expr,426int iterations,427Random rng) {428if (!(expr.isLongExprSupported()429&& expr.isBinaryArgumentSupported()430&& expr.isMemExprSupported())) {431return;432}433434for (int i = 0; i < iterations; i++) {435long aValue = rng.nextLong();436long bValue = rng.nextLong();437log("BinaryLongMemReg(0X%x, 0X%x) -> 0X%x", aValue, bValue,438expr.longExpr(new Expr.MemL(aValue), bValue));439}440}441442public static void runBinaryMemMemLongTest(Expr expr,443int iterations,444Random rng) {445if (!(expr.isLongExprSupported()446&& expr.isBinaryArgumentSupported()447&& expr.isMemExprSupported())) {448return;449}450451for (int i = 0; i < iterations; i++) {452long aValue = rng.nextLong();453long bValue = rng.nextLong();454log("BinaryLongMemMem(0X%x, 0X%x) -> 0X%x", aValue, bValue,455expr.longExpr(new Expr.MemL(aValue),456new Expr.MemL(bValue)));457}458}459}460}461462463