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