Path: blob/master/test/jdk/java/lang/invoke/LFCaching/LFMultiThreadCachingTest.java
41152 views
/*1* Copyright (c) 2014, 2017, 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* @key randomness27* @summary Test verifies that lambda forms are cached when run with multiple threads28* @author kshefov29* @library /lib/testlibrary /java/lang/invoke/common /test/lib30* @modules java.base/java.lang.invoke:open31* java.base/java.lang.ref:open32* java.management33* @build jdk.test.lib.TimeLimitedRunner34* @build TestMethods35* @build LambdaFormTestCase36* @build LFCachingTestCase37* @build LFMultiThreadCachingTest38* @run main/othervm LFMultiThreadCachingTest39*/4041import test.java.lang.invoke.lib.CodeCacheOverflowProcessor;4243import java.lang.invoke.MethodHandle;44import java.util.Collections;45import java.util.EnumSet;46import java.util.HashMap;47import java.util.Map;48import java.util.concurrent.ConcurrentLinkedQueue;49import java.util.concurrent.CountDownLatch;50import java.util.concurrent.CyclicBarrier;5152/**53* Multiple threaded lambda forms caching test class.54*/55public final class LFMultiThreadCachingTest extends LFCachingTestCase {5657private static final TestMethods.Kind[] KINDS;5859static {60EnumSet<TestMethods.Kind> set61= EnumSet.complementOf(EnumSet.of(TestMethods.Kind.EXCEPT));62KINDS = set.toArray(new TestMethods.Kind[set.size()]);63if (KINDS.length < 2) {64throw new Error("TESTBUG: KINDS.length[" + KINDS.length65+ "] should be at least 2");66}67}68private static final int CORES69= Math.max(KINDS.length, Runtime.getRuntime().availableProcessors());7071/**72* Constructor a for multiple threaded lambda forms caching test case.73*74* @param testMethod A method from {@code j.l.i.MethodHandles} class that75* returns a {@code j.l.i.MethodHandle} instance.76*/77public LFMultiThreadCachingTest(TestMethods testMethod) {78super(testMethod);79}8081@Override82public void doTest() {83Map<String, Object> data = getTestMethod().getTestCaseData();84ConcurrentLinkedQueue<MethodHandle> adapters = new ConcurrentLinkedQueue<>();85CyclicBarrier begin = new CyclicBarrier(CORES);86CountDownLatch end = new CountDownLatch(CORES);87final Map<Thread, Throwable> threadUncaughtExceptions88= Collections.synchronizedMap(new HashMap<Thread, Throwable>(CORES));89for (int i = 0; i < CORES; ++i) {90TestMethods.Kind kind = KINDS[i % KINDS.length];91Thread t = new Thread(() -> {92try {93begin.await();94adapters.add(getTestMethod().getTestCaseMH(data, kind));95} catch (Throwable ex) {96threadUncaughtExceptions.put(Thread.currentThread(), ex);97} finally {98end.countDown();99}100});101t.start();102}103try {104end.await();105boolean vmeThrown = false;106boolean nonVmeThrown = false;107Throwable vme = null;108for (Map.Entry<Thread,109Throwable> entry : threadUncaughtExceptions.entrySet()) {110Thread t = entry.getKey();111Throwable e = entry.getValue();112System.err.printf("%nA thread with name \"%s\" of %d threads"113+ " has thrown exception:%n", t.getName(), CORES);114e.printStackTrace();115if (CodeCacheOverflowProcessor.isThrowableCausedByVME(e)) {116vmeThrown = true;117vme = e;118} else {119nonVmeThrown = true;120}121if (nonVmeThrown) {122throw new Error("One ore more threads have"123+ " thrown unexpected exceptions. See log.");124}125if (vmeThrown) {126throw new Error("One ore more threads have"127+ " thrown VirtualMachineError caused by"128+ " code cache overflow. See log.", vme);129}130}131} catch (InterruptedException ex) {132throw new Error("Unexpected exception: ", ex);133}134if (adapters.size() < CORES) {135throw new Error("adapters size[" + adapters.size() + "] is less than " + CORES);136}137MethodHandle prev = adapters.poll();138for (MethodHandle current : adapters) {139checkLFCaching(prev, current);140prev = current;141}142}143144/**145* Main routine for multiple threaded lambda forms caching test.146*147* @param args Accepts no arguments.148*/149public static void main(String[] args) {150LambdaFormTestCase.runTests(LFMultiThreadCachingTest::new,151EnumSet.allOf(TestMethods.class));152}153}154155156