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