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 import jdk.test.lib.TimeLimitedRunner;
  25 import jdk.test.lib.Utils;
  26 import test.java.lang.invoke.lib.CodeCacheOverflowProcessor;
  27 import test.java.lang.invoke.lib.Helper;
  28 
  29 import java.lang.invoke.MethodHandle;
  30 import java.lang.management.GarbageCollectorMXBean;
  31 import java.lang.management.ManagementFactory;
  32 import java.lang.ref.Reference;
  33 import java.lang.reflect.Field;
  34 import java.lang.reflect.Method;
  35 import java.util.Collection;
  36 import java.util.List;
  37 import java.util.function.Function;
  38 
  39 /**
  40  * Lambda forms caching test case class. Contains all necessary test routines to
  41  * test lambda forms caching in method handles returned by methods of
  42  * MethodHandles class.
  43  *
  44  * @author kshefov
  45  */
  46 public abstract class LambdaFormTestCase {
  47 
  48     private static final long TIMEOUT = Helper.IS_THOROUGH ?
  49             0L : (long) (Utils.adjustTimeout(Utils.DEFAULT_TEST_TIMEOUT) * 0.9);
  50 
  51     /**
  52      * Reflection link to {@code j.l.i.MethodHandle.internalForm} method. It is
  53      * used to get a lambda form from a method handle.
  54      */
  55     protected static final Method INTERNAL_FORM;
  56     protected static final Field KIND_FIELD;
  57     protected static final Field REF_FIELD;
  58     private static final List<GarbageCollectorMXBean> gcInfo;
  59 
  60     private static long gcCount() {
  61         return gcInfo.stream().mapToLong(GarbageCollectorMXBean::getCollectionCount).sum();
  62     }
  63 
  64     static {
  65         try {
  66             INTERNAL_FORM = MethodHandle.class.getDeclaredMethod("internalForm");
  67             INTERNAL_FORM.setAccessible(true);
  68 
  69             KIND_FIELD = Class.forName("java.lang.invoke.LambdaForm").getDeclaredField("kind");
  70             KIND_FIELD.setAccessible(true);
  71 
  72             REF_FIELD = Reference.class.getDeclaredField("referent");
  73             REF_FIELD.setAccessible(true);
  74         } catch (Exception ex) {
  75             throw new Error("Unexpected exception", ex);
  76         }
  77 
  78         gcInfo = ManagementFactory.getGarbageCollectorMXBeans();
  79         if (gcInfo.size() == 0) {
  80             throw new Error("No GarbageCollectorMXBeans found.");
  81         }
  82     }
  83 
  84     private final TestMethods testMethod;
  85     private long gcCountAtStart;
  86 
  87     private static class TestRun {
  88 
  89         final Function<TestMethods, LambdaFormTestCase> ctor;
  90         final Collection<TestMethods> testMethods;
  91         final long totalIterations;
  92         long doneIterations;
  93         long testCounter;
  94         long failCounter;
  95         boolean passed;
  96 
  97         TestRun(Function<TestMethods, LambdaFormTestCase> ctor,
  98                 Collection<TestMethods> testMethods) {
  99             this.ctor = ctor;
 100             this.testMethods = testMethods;
 101             long testCaseNum = testMethods.size();
 102             long iterations = Math.max(1, Helper.TEST_LIMIT / testCaseNum);
 103             System.out.printf("Number of iterations according to -DtestLimit is %d (%d cases)%n",
 104                     iterations, iterations * testCaseNum);
 105             System.out.printf("Number of iterations is set to %d (%d cases)%n",
 106                     iterations, iterations * testCaseNum);
 107             System.out.flush();
 108             totalIterations = iterations;
 109             doneIterations = 0L;
 110             testCounter = 0L;
 111             failCounter = 0L;
 112             passed = true;
 113         }
 114 
 115         Boolean doIteration() {
 116             if (doneIterations >= totalIterations) {
 117                 return false;
 118             }
 119             System.err.println(String.format("Iteration %d:", doneIterations));
 120             for (TestMethods testMethod : testMethods) {
 121                 LambdaFormTestCase testCase = ctor.apply(testMethod);
 122                 try {
 123                     System.err.printf("Tested LF caching feature"
 124                             + " with MethodHandles.%s method.%n",
 125                             testCase.getTestMethod().name);
 126                     Throwable t = CodeCacheOverflowProcessor
 127                             .runMHTest(testCase::doTest);
 128                     if (t != null) {
 129                         return false;
 130                     }
 131                     System.err.println("PASSED");
 132                 } catch (OutOfMemoryError oome) {
 133                     // Don't swallow OOME so a heap dump can be created.
 134                     System.err.println("FAILED");
 135                     throw oome;
 136                 } catch (Throwable t) {
 137                     t.printStackTrace();
 138                     System.err.printf("FAILED. Caused by %s%n", t.getMessage());
 139                     passed = false;
 140                     failCounter++;
 141                 }
 142                 testCounter++;
 143             }
 144             doneIterations++;
 145             return true;
 146         }
 147     }
 148 
 149     /**
 150      * Test case constructor. Generates test cases with random method types for
 151      * given methods form {@code j.l.i.MethodHandles} class.
 152      *
 153      * @param testMethod A method from {@code j.l.i.MethodHandles} class which
 154      * returns a {@code j.l.i.MethodHandle}.
 155      */
 156     protected LambdaFormTestCase(TestMethods testMethod) {
 157         this.testMethod = testMethod;
 158         this.gcCountAtStart = gcCount();
 159     }
 160 
 161     public TestMethods getTestMethod() {
 162         return testMethod;
 163     }
 164 
 165     protected boolean noGCHappened() {
 166         return gcCount() == gcCountAtStart;
 167     }
 168 
 169     /**
 170      * Routine that executes a test case.
 171      */
 172     public abstract void doTest();
 173 
 174     /**
 175      * Runs a number of test cases defined by the size of testCases list.
 176      *
 177      * @param ctor constructor of LambdaFormCachingTest or its child classes
 178      * object.
 179      * @param testMethods list of test methods
 180      */
 181     public static void runTests(Function<TestMethods, LambdaFormTestCase> ctor,
 182                                 Collection<TestMethods> testMethods) {
 183         LambdaFormTestCase.TestRun run
 184                 = new LambdaFormTestCase.TestRun(ctor, testMethods);
 185         TimeLimitedRunner runner
 186                 = new TimeLimitedRunner(TIMEOUT, 4.0d, run::doIteration);
 187         try {
 188             runner.call();
 189         } catch (Exception ex) {
 190             System.err.println("FAILED");
 191             throw new Error("Unexpected error!", ex);
 192         }
 193         if (!run.passed) {
 194             throw new Error(String.format("%d of %d test cases FAILED! %n"
 195                     + "Rerun the test with the same \"-Dseed=\" option as in the log file!",
 196                     run.failCounter, run.testCounter));
 197         } else {
 198             System.err.printf("All %d test cases PASSED!%n", run.testCounter);
 199         }
 200     }
 201 }