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