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