hotspot/test/compiler/codecache/stress/Helper.java
author pchistyakov
Mon, 12 Jan 2015 18:58:49 +0300
changeset 28501 c9c359a66a15
child 30604 b8d532cb6420
permissions -rw-r--r--
8059551: JEP-JDK-8043304: Test task: stress tests Reviewed-by: drchase, kvn, iignatyev
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
/*
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
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
 */
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    24
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    25
import java.io.BufferedInputStream;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    26
import java.io.ByteArrayOutputStream;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    27
import java.io.IOException;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    28
import java.util.concurrent.Callable;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    29
import java.util.Random;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    30
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    31
import com.oracle.java.testlibrary.Asserts;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    32
import com.oracle.java.testlibrary.ByteCodeLoader;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    33
import com.oracle.java.testlibrary.InfiniteLoop;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    34
import com.oracle.java.testlibrary.Utils;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    35
import sun.hotspot.WhiteBox;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    36
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    37
public final class Helper {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    38
    public static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    39
    public static final Random RNG = Utils.getRandomInstance();
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    40
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    41
    private static final long THRESHOLD = WHITE_BOX.getIntxVMFlag("CompileThreshold");
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    42
    private static final String TEST_CASE_IMPL_CLASS_NAME = "Helper$TestCaseImpl";
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    43
    private static byte[] CLASS_DATA;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    44
    static {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    45
        try {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    46
            CLASS_DATA = loadClassData(TEST_CASE_IMPL_CLASS_NAME);
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    47
        } catch (IOException e) {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    48
            throw new Error("TESTBUG: cannot load class byte code", e);
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    49
        }
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
    private Helper() {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    53
    }
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
    public static void startInfiniteLoopThread(Runnable action) {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    56
        startInfiniteLoopThread(action, 0L);
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    57
    }
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
    public static void startInfiniteLoopThread(Runnable action, long millis) {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    60
        Thread t = new Thread(new InfiniteLoop(action, millis));
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    61
        t.setDaemon(true);
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    62
        t.start();
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    63
    }
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
    public static int callMethod(Callable<Integer> callable, int expected) {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    66
        int result = 0;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    67
        for (int i = 0; i < THRESHOLD; ++i) {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    68
            try {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    69
                result = callable.call();
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    70
            } catch (Exception e) {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    71
                throw new AssertionError(
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    72
                        "Exception occurred during test method execution", e);
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    73
            }
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    74
            Asserts.assertEQ(result, expected, "Method returns unexpected value");
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    75
        }
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    76
        return result;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    77
    }
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
    private static byte[] loadClassData(String name) throws IOException {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    80
        try (BufferedInputStream in = new BufferedInputStream(
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    81
                ClassLoader.getSystemResourceAsStream(name.replace(".", "/")
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    82
                        + ".class"))) {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    83
            ByteArrayOutputStream result = new ByteArrayOutputStream();
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    84
            byte[] buffer = new byte[1024];
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    85
            int read;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    86
            while ((read = in.read(buffer)) != -1) {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    87
                result.write(buffer, 0, read);
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    88
            }
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    89
            return result.toByteArray();
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    90
        }
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
    public interface TestCase {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    94
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    95
        public static TestCase get() {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    96
            try {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    97
                Class clazz = ByteCodeLoader.load(
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    98
                        TEST_CASE_IMPL_CLASS_NAME, CLASS_DATA);
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
    99
                return (TestCase) clazz.newInstance();
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   100
            } catch (ReflectiveOperationException e) {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   101
                throw new Error(String.format(
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   102
                        "TESTBUG: error while creating %s instance from reloaded class",
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   103
                        TEST_CASE_IMPL_CLASS_NAME), e);
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   104
            }
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
        Callable<Integer> getCallable();
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   108
        int method();
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   109
        int expectedValue();
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   110
    }
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
    public static class TestCaseImpl implements TestCase {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   113
        private static final int RETURN_VALUE = 42;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   114
        private static final int RECURSION_DEPTH = 10;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   115
        private volatile int i;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   116
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   117
        @Override
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   118
        public Callable<Integer> getCallable() {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   119
            return () -> {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   120
                i = 0;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   121
                return method();
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   122
            };
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
        @Override
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   126
        public int method() {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   127
            ++i;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   128
            int result = RETURN_VALUE;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   129
            if (i < RECURSION_DEPTH) {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   130
                return result + method();
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   131
            }
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   132
            return result;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   133
        }
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
        @Override
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   136
        public int expectedValue() {
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   137
            return RETURN_VALUE * RECURSION_DEPTH;
c9c359a66a15 8059551: JEP-JDK-8043304: Test task: stress tests
pchistyakov
parents:
diff changeset
   138
        }
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
}