aboutsummaryrefslogtreecommitdiff
path: root/test/compiler/intrinsics/bmi/BMITestRunner.java
blob: ea7fd821f33fb23a73286204344505d117c6f129 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

import java.util.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.charset.StandardCharsets;

import com.oracle.java.testlibrary.*;

/**
 * Test runner that invokes all methods implemented by particular Expr
 * with random arguments in two different JVM processes and compares output.
 * JVMs being started in different modes - one in int and other in comp
 * with C2 and disabled tiered compilation.
 */
public class BMITestRunner {

    enum VMMode {
        COMP, INT;
    };

    public static int DEFAULT_ITERATIONS_COUNT = 4000;

    /**
     * Execute all methods implemented by <b>expr</b> in int and comp modes
     * and compare output.
     * Test pass only of output obtained with different VM modes is equal.
     * To control behaviour of test following options could be passed:
     * <ul>
     *   <li>-iterations=&lt;N&gt; each operation implemented by
     *       <b>expr</b> will be executed <i>N</i> times. Default value
     *       is 4000.</li>
     *   <li>-seed=&lt;SEED&gt; arguments for <b>expr</b>'s methods
     *       obtained via RNG initiated with seed <i>SEED</i>. By default
     *       some random seed will be used.</li>
     * </ul>
     *
     * @param expr operation that should be tested
     * @param testOpts options to control test behaviour
     * @param additionalVMOpts additional options for VM
     *
     * @throws Throwable if test failed.
     */
    public static void runTests(Class<? extends Expr> expr,
                                String testOpts[],
                                String... additionalVMOpts)
                         throws Throwable {

        int seed = new Random().nextInt();
        int iterations = DEFAULT_ITERATIONS_COUNT;

        for (String testOption : testOpts) {
            if (testOption.startsWith("-iterations=")) {
                iterations = Integer.valueOf(testOption.
                                             replace("-iterations=", ""));
            } else if (testOption.startsWith("-seed=")) {
                seed = Integer.valueOf(testOption.replace("-seed=", ""));
            }
        }

        System.out.println("Running test with seed: " + seed);

        OutputAnalyzer intOutput = runTest(expr, VMMode.INT,
                                           additionalVMOpts,
                                           seed, iterations);
        OutputAnalyzer compOutput = runTest(expr, VMMode.COMP,
                                            additionalVMOpts,
                                            seed, iterations);

        dumpOutput(intOutput, "int");
        dumpOutput(compOutput, "comp");

        Asserts.assertStringsEqual(intOutput.getStdout(),
                                   compOutput.getStdout(),
                                   "Results obtained in -Xint and " +
                                   "-Xcomp should be the same.");
    }

    /**
     * Execute tests on methods implemented by <b>expr</b> in new VM
     * started in <b>testVMMode</b> mode.
     *
     * @param expr operation that should be tested
     * @param testVMMode VM mode for test
     * @param additionalVMOpts additional options for VM
     * @param seed for RNG used it tests
     * @param iterations that will be used to invoke <b>expr</b>'s methods.
     *
     * @return OutputAnalyzer for executed test.
     * @throws Throwable when something goes wrong.
     */
    public static OutputAnalyzer runTest(Class<? extends Expr> expr,
                                         VMMode testVMMode,
                                         String additionalVMOpts[],
                                         int seed, int iterations)
                                  throws Throwable {

        List<String> vmOpts = new LinkedList<String>();

        Collections.addAll(vmOpts, additionalVMOpts);

        //setup mode-specific options
        switch (testVMMode) {
        case INT:
            Collections.addAll(vmOpts, new String[] { "-Xint" });
            break;
        case COMP:
            Collections.addAll(vmOpts, new String[] {
                    "-Xcomp",
                    "-XX:-TieredCompilation",
                    String.format("-XX:CompileCommand=compileonly,%s::*",
                                  expr.getName())
                });
            break;
        }

        Collections.addAll(vmOpts, new String[] {
                "-XX:+DisplayVMOutputToStderr",
                Executor.class.getName(),
                expr.getName(),
                new Integer(seed).toString(),
                new Integer(iterations).toString()
            });

        OutputAnalyzer outputAnalyzer = ProcessTools.
            executeTestJvm(vmOpts.toArray(new String[vmOpts.size()]));

        outputAnalyzer.shouldHaveExitValue(0);

        return outputAnalyzer;
    }

    /**
     * Dump stdout and stderr of test process to <i>prefix</i>.test.out
     * and <i>prefix</i>.test.err respectively.
     *
     * @param outputAnalyzer OutputAnalyzer whom output should be dumped
     * @param prefix Prefix that will be used in file names.
     * @throws IOException if unable to dump output to file.
     */
    protected static void dumpOutput(OutputAnalyzer outputAnalyzer,
                                     String prefix)
                              throws IOException {
        Files.write(Paths.get(prefix + ".test.out"),
                    outputAnalyzer.getStdout().getBytes());

        Files.write(Paths.get(prefix + ".test.err"),
                    outputAnalyzer.getStderr().getBytes());
    }


    /**
     * Executor that invoke all methods implemented by particular
     * Expr instance.
     */
    public static class Executor {

        /**
         * Usage: BMITestRunner$Executor <ExprClassName> <seed> <iterations>
         */
        public static void main(String args[]) throws Exception {
            @SuppressWarnings("unchecked")
            Class<? extends Expr> exprClass =
                (Class<? extends Expr>)Class.forName(args[0]);
            Expr expr = exprClass.getConstructor().newInstance();
            Random rng = new Random(Integer.valueOf(args[1]));
            int iterations = Integer.valueOf(args[2]);
            runTests(expr, iterations, rng);
        }


        public static int[] getIntBitShifts() {
            //SIZE+1 shift is for zero.
            int data[] = new int[Integer.SIZE+1];
            for (int s = 0; s < data.length; s++) {
                data[s] = 1<<s;
            }
            return data;
        }

        public static long[] getLongBitShifts() {
            //SIZE+1 shift is for zero.
            long data[] = new long[Long.SIZE+1];
            for (int s = 0; s < data.length; s++) {
                data[s] = 1L<<s;
            }
            return data;
        }

        public static void log(String format, Object... args) {
            System.out.println(String.format(format, args));
        }

        public static void runTests(Expr expr, int iterations, Random rng) {
            runUnaryIntRegTest(expr, iterations, rng);
            runUnaryIntMemTest(expr, iterations, rng);
            runUnaryLongRegTest(expr, iterations, rng);
            runUnaryLongMemTest(expr, iterations, rng);
            runBinaryRegRegIntTest(expr, iterations, rng);
            runBinaryRegMemIntTest(expr, iterations, rng);
            runBinaryMemRegIntTest(expr, iterations, rng);
            runBinaryMemMemIntTest(expr, iterations, rng);
            runBinaryRegRegLongTest(expr, iterations, rng);
            runBinaryRegMemLongTest(expr, iterations, rng);
            runBinaryMemRegLongTest(expr, iterations, rng);
            runBinaryMemMemLongTest(expr, iterations, rng);
        }

        public static void runUnaryIntRegTest(Expr expr, int iterations,
                                              Random rng) {
            if (!(expr.isUnaryArgumentSupported()
                  && expr.isIntExprSupported())) {
                return;
            }

            for (int value : getIntBitShifts()) {
                log("UnaryIntReg(0X%x) -> 0X%x",
                    value, expr.intExpr(value));
            }

            for (int i = 0; i < iterations; i++) {
                int value = rng.nextInt();
                log("UnaryIntReg(0X%x) -> 0X%x",
                    value, expr.intExpr(value));
            }
        }

        public static void runUnaryIntMemTest(Expr expr, int iterations,
                                              Random rng) {
            if (!(expr.isUnaryArgumentSupported()
                  && expr.isIntExprSupported()
                  && expr.isMemExprSupported())) {
                return;
            }

            for (int value : getIntBitShifts()) {
                log("UnaryIntMem(0X%x) -> 0X%x",
                    value, expr.intExpr(new Expr.MemI(value)));
            }

            for (int i = 0; i < iterations; i++) {
                int value = rng.nextInt();
                log("UnaryIntMem(0X%x) -> 0X%x",
                    value, expr.intExpr(new Expr.MemI(value)));
            }
        }

        public static void runUnaryLongRegTest(Expr expr, int iterations,
                                               Random rng) {
            if (!(expr.isUnaryArgumentSupported()
                  && expr.isLongExprSupported())) {
                return;
            }

            for (long value : getLongBitShifts()) {
                log("UnaryLongReg(0X%x) -> 0X%x",
                    value, expr.longExpr(value));
            }

            for (int i = 0; i < iterations; i++) {
                long value = rng.nextLong();
                log("UnaryLongReg(0X%x) -> 0X%x",
                    value, expr.longExpr(value));
            }
        }

        public static void runUnaryLongMemTest(Expr expr, int iterations,
                                               Random rng) {
            if (!(expr.isUnaryArgumentSupported()
                  && expr.isLongExprSupported()
                  && expr.isMemExprSupported())) {
                return;
            }

            for (long value : getLongBitShifts()) {
                log("UnaryLongMem(0X%x) -> 0X%x",
                    value, expr.longExpr(new Expr.MemL(value)));
            }

            for (int i = 0; i < iterations; i++) {
                long value = rng.nextLong();
                log("UnaryLongMem(0X%x) -> 0X%x",
                    value, expr.longExpr(new Expr.MemL(value)));
            }
        }

        public static void runBinaryRegRegIntTest(Expr expr, int iterations,
                                                  Random rng) {
            if (!(expr.isIntExprSupported()
                  && expr.isBinaryArgumentSupported())) {
                return;
            }

            for (int i = 0; i < iterations; i++) {
                int aValue = rng.nextInt();
                int bValue = rng.nextInt();
                log("BinaryIntRegReg(0X%x, 0X%x) -> 0X%x",
                    aValue, bValue, expr.intExpr(aValue, bValue));
            }
        }

        public static void runBinaryRegMemIntTest(Expr expr, int iterations,
                                                  Random rng) {
            if (!(expr.isIntExprSupported()
                  && expr.isBinaryArgumentSupported()
                  && expr.isMemExprSupported())) {
                return;
            }

            for (int i = 0; i < iterations; i++) {
                int aValue = rng.nextInt();
                int bValue = rng.nextInt();
                log("BinaryIntRegMem(0X%x, 0X%x) -> 0X%x", aValue, bValue,
                    expr.intExpr(aValue, new Expr.MemI(bValue)));
            }
        }

        public static void runBinaryMemRegIntTest(Expr expr, int iterations,
                                                  Random rng) {
            if (!(expr.isIntExprSupported()
                  && expr.isBinaryArgumentSupported()
                  && expr.isMemExprSupported())) {
                return;
            }

            for (int i = 0; i < iterations; i++) {
                int aValue = rng.nextInt();
                int bValue = rng.nextInt();
                log("BinaryIntMemReg(0X%x, 0X%x) -> 0X%x", aValue, bValue,
                    expr.intExpr(new Expr.MemI(aValue), bValue));
            }
        }

        public static void runBinaryMemMemIntTest(Expr expr, int iterations,
                                                  Random rng) {
            if (!(expr.isIntExprSupported()
                  && expr.isBinaryArgumentSupported()
                  && expr.isMemExprSupported())) {
                return;
            }

            for (int i = 0; i < iterations; i++) {
                int aValue = rng.nextInt();
                int bValue = rng.nextInt();
                log("BinaryIntMemMem(0X%x, 0X%x) -> 0X%x", aValue, bValue,
                    expr.intExpr(new Expr.MemI(aValue),
                                 new Expr.MemI(bValue)));
            }
        }

        public static void runBinaryRegRegLongTest(Expr expr,
                                                   int iterations,
                                                   Random rng) {
            if (!(expr.isLongExprSupported()
                  && expr.isBinaryArgumentSupported())) {
                return;
            }

            for (int i = 0; i < iterations; i++) {
                long aValue = rng.nextLong();
                long bValue = rng.nextLong();
                log("BinaryLongRegReg(0X%x, 0X%x) -> 0X%x", aValue, bValue,
                    expr.longExpr(aValue, bValue));
            }
        }

        public static void runBinaryRegMemLongTest(Expr expr,
                                                   int iterations,
                                                   Random rng) {
            if (!(expr.isLongExprSupported()
                  && expr.isBinaryArgumentSupported()
                  && expr.isMemExprSupported())) {
                return;
            }

            for (int i = 0; i < iterations; i++) {
                long aValue = rng.nextLong();
                long bValue = rng.nextLong();
                log("BinaryLongRegMem(0X%x, 0X%x) -> 0X%x", aValue, bValue,
                    expr.longExpr(aValue, new Expr.MemL(bValue)));
            }
        }

        public static void runBinaryMemRegLongTest(Expr expr,
                                                   int iterations,
                                                   Random rng) {
            if (!(expr.isLongExprSupported()
                  && expr.isBinaryArgumentSupported()
                  && expr.isMemExprSupported())) {
                return;
            }

            for (int i = 0; i < iterations; i++) {
                long aValue = rng.nextLong();
                long bValue = rng.nextLong();
                log("BinaryLongMemReg(0X%x, 0X%x) -> 0X%x", aValue, bValue,
                    expr.longExpr(new Expr.MemL(aValue), bValue));
            }
        }

        public static void runBinaryMemMemLongTest(Expr expr,
                                                   int iterations,
                                                   Random rng) {
            if (!(expr.isLongExprSupported()
                  && expr.isBinaryArgumentSupported()
                  && expr.isMemExprSupported())) {
                return;
            }

            for (int i = 0; i < iterations; i++) {
                long aValue = rng.nextLong();
                long bValue = rng.nextLong();
                log("BinaryLongMemMem(0X%x, 0X%x) -> 0X%x", aValue, bValue,
                    expr.longExpr(new Expr.MemL(aValue),
                                  new Expr.MemL(bValue)));
            }
        }
    }
}