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