Path: blob/master/test/hotspot/jtreg/compiler/codecache/stress/Helper.java
41153 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.codecache.stress;2425import jdk.test.lib.Asserts;26import jdk.test.lib.ByteCodeLoader;27import jdk.test.lib.InfiniteLoop;28import sun.hotspot.WhiteBox;2930import java.io.BufferedInputStream;31import java.io.ByteArrayOutputStream;32import java.io.IOException;33import java.util.concurrent.Callable;3435public final class Helper {36public static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();3738private static final long THRESHOLD = WHITE_BOX.getIntxVMFlag("CompileThreshold");39private static final String TEST_CASE_IMPL_CLASS_NAME = TestCaseImpl.class.getName();40private static byte[] CLASS_DATA;41static {42try {43CLASS_DATA = loadClassData(TEST_CASE_IMPL_CLASS_NAME);44} catch (IOException e) {45throw new Error("TESTBUG: cannot load class byte code " + TEST_CASE_IMPL_CLASS_NAME, e);46}47}4849private Helper() {50}5152public static void startInfiniteLoopThread(Runnable action) {53startInfiniteLoopThread(action, 0L);54}5556public static void startInfiniteLoopThread(Runnable action, long millis) {57Thread t = new Thread(new InfiniteLoop(action, millis));58t.setDaemon(true);59t.start();60}6162public static int callMethod(Callable<Integer> callable, int expected) {63int result = 0;64for (int i = 0; i < THRESHOLD; ++i) {65try {66result = callable.call();67} catch (Exception e) {68throw new AssertionError(69"Exception occurred during test method execution", e);70}71Asserts.assertEQ(result, expected, "Method returns unexpected value");72}73return result;74}7576private static byte[] loadClassData(String name) throws IOException {77try (BufferedInputStream in = new BufferedInputStream(78ClassLoader.getSystemResourceAsStream(name.replace(".", "/")79+ ".class"))) {80ByteArrayOutputStream result = new ByteArrayOutputStream();81byte[] buffer = new byte[1024];82int read;83while ((read = in.read(buffer)) != -1) {84result.write(buffer, 0, read);85}86return result.toByteArray();87}88}8990public interface TestCase {9192public static TestCase get() {93try {94Class clazz = ByteCodeLoader.load(95TEST_CASE_IMPL_CLASS_NAME, CLASS_DATA);96return (TestCase) clazz.newInstance();97} catch (ReflectiveOperationException e) {98throw new Error(String.format(99"TESTBUG: error while creating %s instance from reloaded class",100TEST_CASE_IMPL_CLASS_NAME), e);101}102}103104Callable<Integer> getCallable();105int method();106int expectedValue();107}108}109110111