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