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