hotspot/test/compiler/codecache/stress/Helper.java
author iignatyev
Mon, 22 May 2017 15:27:20 -0700
changeset 45245 48fed68b35c1
parent 41705 332239c052cc
permissions -rw-r--r--
8180793: move jdk.test.lib.wrappers.* to jdk.test.lib package Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28501
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
     1
/*
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 40059
diff changeset
     2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
28501
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
     4
 *
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
     8
 *
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    13
 * accompanied this code).
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    14
 *
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    18
 *
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    21
 * questions.
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    22
 */
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    23
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 30604
diff changeset
    24
package compiler.codecache.stress;
28501
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    25
30604
b8d532cb6420 8067013: Rename the com.oracle.java.testlibary package
ykantser
parents: 28501
diff changeset
    26
import jdk.test.lib.Asserts;
b8d532cb6420 8067013: Rename the com.oracle.java.testlibary package
ykantser
parents: 28501
diff changeset
    27
import jdk.test.lib.ByteCodeLoader;
45245
48fed68b35c1 8180793: move jdk.test.lib.wrappers.* to jdk.test.lib package
iignatyev
parents: 41705
diff changeset
    28
import jdk.test.lib.InfiniteLoop;
30604
b8d532cb6420 8067013: Rename the com.oracle.java.testlibary package
ykantser
parents: 28501
diff changeset
    29
import jdk.test.lib.Utils;
28501
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    30
import sun.hotspot.WhiteBox;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    31
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 30604
diff changeset
    32
import java.io.BufferedInputStream;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 30604
diff changeset
    33
import java.io.ByteArrayOutputStream;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 30604
diff changeset
    34
import java.io.IOException;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 30604
diff changeset
    35
import java.util.Random;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 30604
diff changeset
    36
import java.util.concurrent.Callable;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 30604
diff changeset
    37
28501
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    38
public final class Helper {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    39
    public static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    40
    public static final Random RNG = Utils.getRandomInstance();
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    41
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    42
    private static final long THRESHOLD = WHITE_BOX.getIntxVMFlag("CompileThreshold");
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 30604
diff changeset
    43
    private static final String TEST_CASE_IMPL_CLASS_NAME = "compiler.codecache.stress.Helper$TestCaseImpl";
28501
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    44
    private static byte[] CLASS_DATA;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    45
    static {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    46
        try {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    47
            CLASS_DATA = loadClassData(TEST_CASE_IMPL_CLASS_NAME);
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    48
        } catch (IOException e) {
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 30604
diff changeset
    49
            throw new Error("TESTBUG: cannot load class byte code " + TEST_CASE_IMPL_CLASS_NAME, e);
28501
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    50
        }
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    51
    }
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    52
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    53
    private Helper() {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    54
    }
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    55
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    56
    public static void startInfiniteLoopThread(Runnable action) {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    57
        startInfiniteLoopThread(action, 0L);
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    58
    }
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    59
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    60
    public static void startInfiniteLoopThread(Runnable action, long millis) {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    61
        Thread t = new Thread(new InfiniteLoop(action, millis));
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    62
        t.setDaemon(true);
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    63
        t.start();
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    64
    }
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    65
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    66
    public static int callMethod(Callable<Integer> callable, int expected) {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    67
        int result = 0;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    68
        for (int i = 0; i < THRESHOLD; ++i) {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    69
            try {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    70
                result = callable.call();
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    71
            } catch (Exception e) {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    72
                throw new AssertionError(
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    73
                        "Exception occurred during test method execution", e);
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    74
            }
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    75
            Asserts.assertEQ(result, expected, "Method returns unexpected value");
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    76
        }
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    77
        return result;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    78
    }
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    79
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    80
    private static byte[] loadClassData(String name) throws IOException {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    81
        try (BufferedInputStream in = new BufferedInputStream(
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    82
                ClassLoader.getSystemResourceAsStream(name.replace(".", "/")
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    83
                        + ".class"))) {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    84
            ByteArrayOutputStream result = new ByteArrayOutputStream();
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    85
            byte[] buffer = new byte[1024];
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    86
            int read;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    87
            while ((read = in.read(buffer)) != -1) {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    88
                result.write(buffer, 0, read);
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    89
            }
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    90
            return result.toByteArray();
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    91
        }
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    92
    }
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    93
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    94
    public interface TestCase {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    95
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    96
        public static TestCase get() {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    97
            try {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    98
                Class clazz = ByteCodeLoader.load(
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    99
                        TEST_CASE_IMPL_CLASS_NAME, CLASS_DATA);
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   100
                return (TestCase) clazz.newInstance();
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   101
            } catch (ReflectiveOperationException e) {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   102
                throw new Error(String.format(
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   103
                        "TESTBUG: error while creating %s instance from reloaded class",
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   104
                        TEST_CASE_IMPL_CLASS_NAME), e);
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   105
            }
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   106
        }
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   107
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   108
        Callable<Integer> getCallable();
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   109
        int method();
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   110
        int expectedValue();
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   111
    }
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   112
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   113
    public static class TestCaseImpl implements TestCase {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   114
        private static final int RETURN_VALUE = 42;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   115
        private static final int RECURSION_DEPTH = 10;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   116
        private volatile int i;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   117
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   118
        @Override
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   119
        public Callable<Integer> getCallable() {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   120
            return () -> {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   121
                i = 0;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   122
                return method();
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   123
            };
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   124
        }
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   125
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   126
        @Override
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   127
        public int method() {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   128
            ++i;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   129
            int result = RETURN_VALUE;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   130
            if (i < RECURSION_DEPTH) {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   131
                return result + method();
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   132
            }
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   133
            return result;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   134
        }
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   135
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   136
        @Override
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   137
        public int expectedValue() {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   138
            return RETURN_VALUE * RECURSION_DEPTH;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   139
        }
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   140
    }
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   141
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   142
}