Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/java/lang/invoke/LFCaching/LFMultiThreadCachingTest.java
48795 views
/*1* Copyright (c) 2014, 2015, 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*/2223/*24* @test LFMultiThreadCachingTest25* @bug 804670326* @summary Test verifies that lambda forms are cached when run with multiple threads27* @author kshefov28* @library /lib/testlibrary/jsr292 /lib/testlibrary29* @build TestMethods30* @build LambdaFormTestCase31* @build LFCachingTestCase32* @build LFMultiThreadCachingTest33* @run main/othervm LFMultiThreadCachingTest34*/3536import java.lang.invoke.MethodHandle;37import java.util.Collections;38import java.util.EnumSet;39import java.util.HashMap;40import java.util.Map;41import java.util.concurrent.BrokenBarrierException;42import java.util.concurrent.ConcurrentLinkedQueue;43import java.util.concurrent.CountDownLatch;44import java.util.concurrent.CyclicBarrier;45import com.oracle.testlibrary.jsr292.CodeCacheOverflowProcessor;4647/**48* Multiple threaded lambda forms caching test class.49*/50public final class LFMultiThreadCachingTest extends LFCachingTestCase {5152private static final TestMethods.Kind[] KINDS;5354static {55EnumSet<TestMethods.Kind> set = EnumSet.complementOf(EnumSet.of(TestMethods.Kind.EXCEPT));56KINDS = set.toArray(new TestMethods.Kind[set.size()]);57if (KINDS.length < 2) {58throw new Error("TESTBUG: KINDS.length[" + KINDS.length + "] should be at least 2");59}60}61private static final int CORES = Math.max(KINDS.length, Runtime.getRuntime().availableProcessors());6263/**64* Constructor a for multiple threaded lambda forms caching test case.65*66* @param testMethod A method from {@code j.l.i.MethodHandles} class that67* returns a {@code j.l.i.MethodHandle} instance.68*/69public LFMultiThreadCachingTest(TestMethods testMethod) {70super(testMethod);71}7273@Override74public void doTest() {75Map<String, Object> data = getTestMethod().getTestCaseData();76ConcurrentLinkedQueue<MethodHandle> adapters = new ConcurrentLinkedQueue<>();77CyclicBarrier begin = new CyclicBarrier(CORES);78CountDownLatch end = new CountDownLatch(CORES);79final Map<Thread, Throwable> threadUncaughtExceptions80= Collections.synchronizedMap(new HashMap<Thread, Throwable>(CORES));81for (int i = 0; i < CORES; ++i) {82TestMethods.Kind kind = KINDS[i % KINDS.length];83Thread t = new Thread(() -> {84try {85begin.await();86adapters.add(getTestMethod().getTestCaseMH(data, kind));87} catch (Throwable ex) {88threadUncaughtExceptions.put(Thread.currentThread(), ex);89} finally {90end.countDown();91}92});93t.start();94}95try {96end.await();97boolean vmeThrown = false;98boolean nonVmeThrown = false;99Throwable vme = null;100for (Map.Entry<Thread,101Throwable> entry : threadUncaughtExceptions.entrySet()) {102Thread t = entry.getKey();103Throwable e = entry.getValue();104System.err.printf("%nA thread with name \"%s\" of %d threads"105+ " has thrown exception:%n", t.getName(), CORES);106e.printStackTrace();107if (CodeCacheOverflowProcessor.isThrowableCausedByVME(e)) {108vmeThrown = true;109vme = e;110} else {111nonVmeThrown = true;112}113if (nonVmeThrown) {114throw new Error("One ore more threads have"115+ " thrown unexpected exceptions. See log.");116}117if (vmeThrown) {118throw new Error("One ore more threads have"119+ " thrown VirtualMachineError caused by"120+ " code cache overflow. See log.", vme);121}122}123} catch (InterruptedException ex) {124throw new Error("Unexpected exception: ", ex);125}126if (adapters.size() < CORES) {127throw new Error("adapters size[" + adapters.size() + "] is less than " + CORES);128}129MethodHandle prev = adapters.poll();130for (MethodHandle current : adapters) {131checkLFCaching(prev, current);132prev = current;133}134}135136/**137* Main routine for multiple threaded lambda forms caching test.138*139* @param args Accepts no arguments.140*/141public static void main(String[] args) {142LambdaFormTestCase.runTests(LFMultiThreadCachingTest::new, EnumSet.allOf(TestMethods.class));143}144}145146147